Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* -------------------------------------------------------------------------
0003  * Copyright (C) 2014-2015, Intel Corporation
0004  *
0005  * Derived from:
0006  *  gslX68X.c
0007  *  Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
0008  *
0009  * -------------------------------------------------------------------------
0010  */
0011 
0012 #include <linux/i2c.h>
0013 #include <linux/module.h>
0014 #include <linux/acpi.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/delay.h>
0018 #include <linux/firmware.h>
0019 #include <linux/input.h>
0020 #include <linux/input/mt.h>
0021 #include <linux/input/touchscreen.h>
0022 #include <linux/pm.h>
0023 #include <linux/pm_runtime.h>
0024 #include <linux/irq.h>
0025 #include <linux/regulator/consumer.h>
0026 
0027 #include <asm/unaligned.h>
0028 
0029 #define SILEAD_TS_NAME      "silead_ts"
0030 
0031 #define SILEAD_REG_RESET    0xE0
0032 #define SILEAD_REG_DATA     0x80
0033 #define SILEAD_REG_TOUCH_NR 0x80
0034 #define SILEAD_REG_POWER    0xBC
0035 #define SILEAD_REG_CLOCK    0xE4
0036 #define SILEAD_REG_STATUS   0xB0
0037 #define SILEAD_REG_ID       0xFC
0038 #define SILEAD_REG_MEM_CHECK    0xB0
0039 
0040 #define SILEAD_STATUS_OK    0x5A5A5A5A
0041 #define SILEAD_TS_DATA_LEN  44
0042 #define SILEAD_CLOCK        0x04
0043 
0044 #define SILEAD_CMD_RESET    0x88
0045 #define SILEAD_CMD_START    0x00
0046 
0047 #define SILEAD_POINT_DATA_LEN   0x04
0048 #define SILEAD_POINT_Y_OFF      0x00
0049 #define SILEAD_POINT_Y_MSB_OFF  0x01
0050 #define SILEAD_POINT_X_OFF  0x02
0051 #define SILEAD_POINT_X_MSB_OFF  0x03
0052 #define SILEAD_EXTRA_DATA_MASK  0xF0
0053 
0054 #define SILEAD_CMD_SLEEP_MIN    10000
0055 #define SILEAD_CMD_SLEEP_MAX    20000
0056 #define SILEAD_POWER_SLEEP  20
0057 #define SILEAD_STARTUP_SLEEP    30
0058 
0059 #define SILEAD_MAX_FINGERS  10
0060 
0061 enum silead_ts_power {
0062     SILEAD_POWER_ON  = 1,
0063     SILEAD_POWER_OFF = 0
0064 };
0065 
0066 struct silead_ts_data {
0067     struct i2c_client *client;
0068     struct gpio_desc *gpio_power;
0069     struct input_dev *input;
0070     struct input_dev *pen_input;
0071     struct regulator_bulk_data regulators[2];
0072     char fw_name[64];
0073     struct touchscreen_properties prop;
0074     u32 max_fingers;
0075     u32 chip_id;
0076     struct input_mt_pos pos[SILEAD_MAX_FINGERS];
0077     int slots[SILEAD_MAX_FINGERS];
0078     int id[SILEAD_MAX_FINGERS];
0079     u32 efi_fw_min_max[4];
0080     bool efi_fw_min_max_set;
0081     bool pen_supported;
0082     bool pen_down;
0083     u32 pen_x_res;
0084     u32 pen_y_res;
0085     int pen_up_count;
0086 };
0087 
0088 struct silead_fw_data {
0089     u32 offset;
0090     u32 val;
0091 };
0092 
0093 static void silead_apply_efi_fw_min_max(struct silead_ts_data *data)
0094 {
0095     struct input_absinfo *absinfo_x = &data->input->absinfo[ABS_MT_POSITION_X];
0096     struct input_absinfo *absinfo_y = &data->input->absinfo[ABS_MT_POSITION_Y];
0097 
0098     if (!data->efi_fw_min_max_set)
0099         return;
0100 
0101     absinfo_x->minimum = data->efi_fw_min_max[0];
0102     absinfo_x->maximum = data->efi_fw_min_max[1];
0103     absinfo_y->minimum = data->efi_fw_min_max[2];
0104     absinfo_y->maximum = data->efi_fw_min_max[3];
0105 
0106     if (data->prop.invert_x) {
0107         absinfo_x->maximum -= absinfo_x->minimum;
0108         absinfo_x->minimum = 0;
0109     }
0110 
0111     if (data->prop.invert_y) {
0112         absinfo_y->maximum -= absinfo_y->minimum;
0113         absinfo_y->minimum = 0;
0114     }
0115 
0116     if (data->prop.swap_x_y) {
0117         swap(absinfo_x->minimum, absinfo_y->minimum);
0118         swap(absinfo_x->maximum, absinfo_y->maximum);
0119     }
0120 }
0121 
0122 static int silead_ts_request_input_dev(struct silead_ts_data *data)
0123 {
0124     struct device *dev = &data->client->dev;
0125     int error;
0126 
0127     data->input = devm_input_allocate_device(dev);
0128     if (!data->input) {
0129         dev_err(dev,
0130             "Failed to allocate input device\n");
0131         return -ENOMEM;
0132     }
0133 
0134     input_set_abs_params(data->input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
0135     input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
0136     touchscreen_parse_properties(data->input, true, &data->prop);
0137     silead_apply_efi_fw_min_max(data);
0138 
0139     input_mt_init_slots(data->input, data->max_fingers,
0140                 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
0141                 INPUT_MT_TRACK);
0142 
0143     if (device_property_read_bool(dev, "silead,home-button"))
0144         input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
0145 
0146     data->input->name = SILEAD_TS_NAME;
0147     data->input->phys = "input/ts";
0148     data->input->id.bustype = BUS_I2C;
0149 
0150     error = input_register_device(data->input);
0151     if (error) {
0152         dev_err(dev, "Failed to register input device: %d\n", error);
0153             return error;
0154     }
0155 
0156     return 0;
0157 }
0158 
0159 static int silead_ts_request_pen_input_dev(struct silead_ts_data *data)
0160 {
0161     struct device *dev = &data->client->dev;
0162     int error;
0163 
0164     if (!data->pen_supported)
0165         return 0;
0166 
0167     data->pen_input = devm_input_allocate_device(dev);
0168     if (!data->pen_input)
0169         return -ENOMEM;
0170 
0171     input_set_abs_params(data->pen_input, ABS_X, 0, 4095, 0, 0);
0172     input_set_abs_params(data->pen_input, ABS_Y, 0, 4095, 0, 0);
0173     input_set_capability(data->pen_input, EV_KEY, BTN_TOUCH);
0174     input_set_capability(data->pen_input, EV_KEY, BTN_TOOL_PEN);
0175     set_bit(INPUT_PROP_DIRECT, data->pen_input->propbit);
0176     touchscreen_parse_properties(data->pen_input, false, &data->prop);
0177     input_abs_set_res(data->pen_input, ABS_X, data->pen_x_res);
0178     input_abs_set_res(data->pen_input, ABS_Y, data->pen_y_res);
0179 
0180     data->pen_input->name = SILEAD_TS_NAME " pen";
0181     data->pen_input->phys = "input/pen";
0182     data->input->id.bustype = BUS_I2C;
0183 
0184     error = input_register_device(data->pen_input);
0185     if (error) {
0186         dev_err(dev, "Failed to register pen input device: %d\n", error);
0187         return error;
0188     }
0189 
0190     return 0;
0191 }
0192 
0193 static void silead_ts_set_power(struct i2c_client *client,
0194                 enum silead_ts_power state)
0195 {
0196     struct silead_ts_data *data = i2c_get_clientdata(client);
0197 
0198     if (data->gpio_power) {
0199         gpiod_set_value_cansleep(data->gpio_power, state);
0200         msleep(SILEAD_POWER_SLEEP);
0201     }
0202 }
0203 
0204 static bool silead_ts_handle_pen_data(struct silead_ts_data *data, u8 *buf)
0205 {
0206     u8 *coord = buf + SILEAD_POINT_DATA_LEN;
0207     struct input_mt_pos pos;
0208 
0209     if (!data->pen_supported || buf[2] != 0x00 || buf[3] != 0x00)
0210         return false;
0211 
0212     if (buf[0] == 0x00 && buf[1] == 0x00 && data->pen_down) {
0213         data->pen_up_count++;
0214         if (data->pen_up_count == 6) {
0215             data->pen_down = false;
0216             goto sync;
0217         }
0218         return true;
0219     }
0220 
0221     if (buf[0] == 0x01 && buf[1] == 0x08) {
0222         touchscreen_set_mt_pos(&pos, &data->prop,
0223             get_unaligned_le16(&coord[SILEAD_POINT_X_OFF]) & 0xfff,
0224             get_unaligned_le16(&coord[SILEAD_POINT_Y_OFF]) & 0xfff);
0225 
0226         input_report_abs(data->pen_input, ABS_X, pos.x);
0227         input_report_abs(data->pen_input, ABS_Y, pos.y);
0228 
0229         data->pen_up_count = 0;
0230         data->pen_down = true;
0231         goto sync;
0232     }
0233 
0234     return false;
0235 
0236 sync:
0237     input_report_key(data->pen_input, BTN_TOOL_PEN, data->pen_down);
0238     input_report_key(data->pen_input, BTN_TOUCH, data->pen_down);
0239     input_sync(data->pen_input);
0240     return true;
0241 }
0242 
0243 static void silead_ts_read_data(struct i2c_client *client)
0244 {
0245     struct silead_ts_data *data = i2c_get_clientdata(client);
0246     struct input_dev *input = data->input;
0247     struct device *dev = &client->dev;
0248     u8 *bufp, buf[SILEAD_TS_DATA_LEN];
0249     int touch_nr, softbutton, error, i;
0250     bool softbutton_pressed = false;
0251 
0252     error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
0253                           SILEAD_TS_DATA_LEN, buf);
0254     if (error < 0) {
0255         dev_err(dev, "Data read error %d\n", error);
0256         return;
0257     }
0258 
0259     if (buf[0] > data->max_fingers) {
0260         dev_warn(dev, "More touches reported then supported %d > %d\n",
0261              buf[0], data->max_fingers);
0262         buf[0] = data->max_fingers;
0263     }
0264 
0265     if (silead_ts_handle_pen_data(data, buf))
0266         goto sync; /* Pen is down, release all previous touches */
0267 
0268     touch_nr = 0;
0269     bufp = buf + SILEAD_POINT_DATA_LEN;
0270     for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
0271         softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
0272                   SILEAD_EXTRA_DATA_MASK) >> 4;
0273 
0274         if (softbutton) {
0275             /*
0276              * For now only respond to softbutton == 0x01, some
0277              * tablets *without* a capacative button send 0x04
0278              * when crossing the edges of the screen.
0279              */
0280             if (softbutton == 0x01)
0281                 softbutton_pressed = true;
0282 
0283             continue;
0284         }
0285 
0286         /*
0287          * Bits 4-7 are the touch id, note not all models have
0288          * hardware touch ids so atm we don't use these.
0289          */
0290         data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
0291                       SILEAD_EXTRA_DATA_MASK) >> 4;
0292         touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
0293             get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
0294             get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
0295         touch_nr++;
0296     }
0297 
0298     input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
0299 
0300     for (i = 0; i < touch_nr; i++) {
0301         input_mt_slot(input, data->slots[i]);
0302         input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
0303         input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
0304         input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
0305 
0306         dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
0307             data->pos[i].y, data->id[i], data->slots[i]);
0308     }
0309 
0310 sync:
0311     input_mt_sync_frame(input);
0312     input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
0313     input_sync(input);
0314 }
0315 
0316 static int silead_ts_init(struct i2c_client *client)
0317 {
0318     struct silead_ts_data *data = i2c_get_clientdata(client);
0319     int error;
0320 
0321     error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
0322                       SILEAD_CMD_RESET);
0323     if (error)
0324         goto i2c_write_err;
0325     usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
0326 
0327     error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
0328                     data->max_fingers);
0329     if (error)
0330         goto i2c_write_err;
0331     usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
0332 
0333     error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
0334                       SILEAD_CLOCK);
0335     if (error)
0336         goto i2c_write_err;
0337     usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
0338 
0339     error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
0340                       SILEAD_CMD_START);
0341     if (error)
0342         goto i2c_write_err;
0343     usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
0344 
0345     return 0;
0346 
0347 i2c_write_err:
0348     dev_err(&client->dev, "Registers clear error %d\n", error);
0349     return error;
0350 }
0351 
0352 static int silead_ts_reset(struct i2c_client *client)
0353 {
0354     int error;
0355 
0356     error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
0357                       SILEAD_CMD_RESET);
0358     if (error)
0359         goto i2c_write_err;
0360     usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
0361 
0362     error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
0363                       SILEAD_CLOCK);
0364     if (error)
0365         goto i2c_write_err;
0366     usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
0367 
0368     error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
0369                       SILEAD_CMD_START);
0370     if (error)
0371         goto i2c_write_err;
0372     usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
0373 
0374     return 0;
0375 
0376 i2c_write_err:
0377     dev_err(&client->dev, "Chip reset error %d\n", error);
0378     return error;
0379 }
0380 
0381 static int silead_ts_startup(struct i2c_client *client)
0382 {
0383     int error;
0384 
0385     error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
0386     if (error) {
0387         dev_err(&client->dev, "Startup error %d\n", error);
0388         return error;
0389     }
0390 
0391     msleep(SILEAD_STARTUP_SLEEP);
0392 
0393     return 0;
0394 }
0395 
0396 static int silead_ts_load_fw(struct i2c_client *client)
0397 {
0398     struct device *dev = &client->dev;
0399     struct silead_ts_data *data = i2c_get_clientdata(client);
0400     const struct firmware *fw = NULL;
0401     struct silead_fw_data *fw_data;
0402     unsigned int fw_size, i;
0403     int error;
0404 
0405     dev_dbg(dev, "Firmware file name: %s", data->fw_name);
0406 
0407     /*
0408      * Unfortunately, at the time of writing this comment, we have been unable to
0409      * get permission from Silead, or from device OEMs, to distribute the necessary
0410      * Silead firmware files in linux-firmware.
0411      *
0412      * On a whole bunch of devices the UEFI BIOS code contains a touchscreen driver,
0413      * which contains an embedded copy of the firmware. The fw-loader code has a
0414      * "platform" fallback mechanism, which together with info on the firmware
0415      * from drivers/platform/x86/touchscreen_dmi.c will use the firmware from the
0416      * UEFI driver when the firmware is missing from /lib/firmware. This makes the
0417      * touchscreen work OOTB without users needing to manually download the firmware.
0418      *
0419      * The firmware bundled with the original Windows/Android is usually newer then
0420      * the firmware in the UEFI driver and it is better calibrated. This better
0421      * calibration can lead to significant differences in the reported min/max
0422      * coordinates.
0423      *
0424      * To deal with this we first try to load the firmware without "platform"
0425      * fallback. If that fails we retry with "platform" fallback and if that
0426      * succeeds we apply an (optional) set of alternative min/max values from the
0427      * "silead,efi-fw-min-max" property.
0428      */
0429     error = firmware_request_nowarn(&fw, data->fw_name, dev);
0430     if (error) {
0431         error = firmware_request_platform(&fw, data->fw_name, dev);
0432         if (error) {
0433             dev_err(dev, "Firmware request error %d\n", error);
0434             return error;
0435         }
0436 
0437         error = device_property_read_u32_array(dev, "silead,efi-fw-min-max",
0438                                data->efi_fw_min_max,
0439                                ARRAY_SIZE(data->efi_fw_min_max));
0440         if (!error)
0441             data->efi_fw_min_max_set = true;
0442 
0443         /* The EFI (platform) embedded fw does not have pen support */
0444         if (data->pen_supported) {
0445             dev_warn(dev, "Warning loading '%s' from filesystem failed, using EFI embedded copy.\n",
0446                  data->fw_name);
0447             dev_warn(dev, "Warning pen support is known to be broken in the EFI embedded fw version\n");
0448             data->pen_supported = false;
0449         }
0450     }
0451 
0452     fw_size = fw->size / sizeof(*fw_data);
0453     fw_data = (struct silead_fw_data *)fw->data;
0454 
0455     for (i = 0; i < fw_size; i++) {
0456         error = i2c_smbus_write_i2c_block_data(client,
0457                                fw_data[i].offset,
0458                                4,
0459                                (u8 *)&fw_data[i].val);
0460         if (error) {
0461             dev_err(dev, "Firmware load error %d\n", error);
0462             break;
0463         }
0464     }
0465 
0466     release_firmware(fw);
0467     return error ?: 0;
0468 }
0469 
0470 static u32 silead_ts_get_status(struct i2c_client *client)
0471 {
0472     int error;
0473     __le32 status;
0474 
0475     error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
0476                           sizeof(status), (u8 *)&status);
0477     if (error < 0) {
0478         dev_err(&client->dev, "Status read error %d\n", error);
0479         return error;
0480     }
0481 
0482     return le32_to_cpu(status);
0483 }
0484 
0485 static int silead_ts_get_id(struct i2c_client *client)
0486 {
0487     struct silead_ts_data *data = i2c_get_clientdata(client);
0488     __le32 chip_id;
0489     int error;
0490 
0491     error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
0492                           sizeof(chip_id), (u8 *)&chip_id);
0493     if (error < 0)
0494         return error;
0495 
0496     data->chip_id = le32_to_cpu(chip_id);
0497     dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
0498 
0499     return 0;
0500 }
0501 
0502 static int silead_ts_setup(struct i2c_client *client)
0503 {
0504     int error;
0505     u32 status;
0506 
0507     /*
0508      * Some buggy BIOS-es bring up the chip in a stuck state where it
0509      * blocks the I2C bus. The following steps are necessary to
0510      * unstuck the chip / bus:
0511      * 1. Turn off the Silead chip.
0512      * 2. Try to do an I2C transfer with the chip, this will fail in
0513      *    response to which the I2C-bus-driver will call:
0514      *    i2c_recover_bus() which will unstuck the I2C-bus. Note the
0515      *    unstuck-ing of the I2C bus only works if we first drop the
0516      *    chip off the bus by turning it off.
0517      * 3. Turn the chip back on.
0518      *
0519      * On the x86/ACPI systems were this problem is seen, step 1. and
0520      * 3. require making ACPI calls and dealing with ACPI Power
0521      * Resources. The workaround below runtime-suspends the chip to
0522      * turn it off, leaving it up to the ACPI subsystem to deal with
0523      * this.
0524      */
0525 
0526     if (device_property_read_bool(&client->dev,
0527                       "silead,stuck-controller-bug")) {
0528         pm_runtime_set_active(&client->dev);
0529         pm_runtime_enable(&client->dev);
0530         pm_runtime_allow(&client->dev);
0531 
0532         pm_runtime_suspend(&client->dev);
0533 
0534         dev_warn(&client->dev, FW_BUG "Stuck I2C bus: please ignore the next 'controller timed out' error\n");
0535         silead_ts_get_id(client);
0536 
0537         /* The forbid will also resume the device */
0538         pm_runtime_forbid(&client->dev);
0539         pm_runtime_disable(&client->dev);
0540     }
0541 
0542     silead_ts_set_power(client, SILEAD_POWER_OFF);
0543     silead_ts_set_power(client, SILEAD_POWER_ON);
0544 
0545     error = silead_ts_get_id(client);
0546     if (error) {
0547         dev_err(&client->dev, "Chip ID read error %d\n", error);
0548         return error;
0549     }
0550 
0551     error = silead_ts_init(client);
0552     if (error)
0553         return error;
0554 
0555     error = silead_ts_reset(client);
0556     if (error)
0557         return error;
0558 
0559     error = silead_ts_load_fw(client);
0560     if (error)
0561         return error;
0562 
0563     error = silead_ts_startup(client);
0564     if (error)
0565         return error;
0566 
0567     status = silead_ts_get_status(client);
0568     if (status != SILEAD_STATUS_OK) {
0569         dev_err(&client->dev,
0570             "Initialization error, status: 0x%X\n", status);
0571         return -ENODEV;
0572     }
0573 
0574     return 0;
0575 }
0576 
0577 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
0578 {
0579     struct silead_ts_data *data = id;
0580     struct i2c_client *client = data->client;
0581 
0582     silead_ts_read_data(client);
0583 
0584     return IRQ_HANDLED;
0585 }
0586 
0587 static void silead_ts_read_props(struct i2c_client *client)
0588 {
0589     struct silead_ts_data *data = i2c_get_clientdata(client);
0590     struct device *dev = &client->dev;
0591     const char *str;
0592     int error;
0593 
0594     error = device_property_read_u32(dev, "silead,max-fingers",
0595                      &data->max_fingers);
0596     if (error) {
0597         dev_dbg(dev, "Max fingers read error %d\n", error);
0598         data->max_fingers = 5; /* Most devices handle up-to 5 fingers */
0599     }
0600 
0601     error = device_property_read_string(dev, "firmware-name", &str);
0602     if (!error)
0603         snprintf(data->fw_name, sizeof(data->fw_name),
0604              "silead/%s", str);
0605     else
0606         dev_dbg(dev, "Firmware file name read error. Using default.");
0607 
0608     data->pen_supported = device_property_read_bool(dev, "silead,pen-supported");
0609     device_property_read_u32(dev, "silead,pen-resolution-x", &data->pen_x_res);
0610     device_property_read_u32(dev, "silead,pen-resolution-y", &data->pen_y_res);
0611 }
0612 
0613 #ifdef CONFIG_ACPI
0614 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
0615                      const struct i2c_device_id *id)
0616 {
0617     const struct acpi_device_id *acpi_id;
0618     struct device *dev = &data->client->dev;
0619     int i;
0620 
0621     if (ACPI_HANDLE(dev)) {
0622         acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
0623         if (!acpi_id)
0624             return -ENODEV;
0625 
0626         snprintf(data->fw_name, sizeof(data->fw_name),
0627              "silead/%s.fw", acpi_id->id);
0628 
0629         for (i = 0; i < strlen(data->fw_name); i++)
0630             data->fw_name[i] = tolower(data->fw_name[i]);
0631     } else {
0632         snprintf(data->fw_name, sizeof(data->fw_name),
0633              "silead/%s.fw", id->name);
0634     }
0635 
0636     return 0;
0637 }
0638 #else
0639 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
0640                      const struct i2c_device_id *id)
0641 {
0642     snprintf(data->fw_name, sizeof(data->fw_name),
0643          "silead/%s.fw", id->name);
0644     return 0;
0645 }
0646 #endif
0647 
0648 static void silead_disable_regulator(void *arg)
0649 {
0650     struct silead_ts_data *data = arg;
0651 
0652     regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
0653 }
0654 
0655 static int silead_ts_probe(struct i2c_client *client,
0656                const struct i2c_device_id *id)
0657 {
0658     struct silead_ts_data *data;
0659     struct device *dev = &client->dev;
0660     int error;
0661 
0662     if (!i2c_check_functionality(client->adapter,
0663                      I2C_FUNC_I2C |
0664                      I2C_FUNC_SMBUS_READ_I2C_BLOCK |
0665                      I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
0666         dev_err(dev, "I2C functionality check failed\n");
0667         return -ENXIO;
0668     }
0669 
0670     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0671     if (!data)
0672         return -ENOMEM;
0673 
0674     i2c_set_clientdata(client, data);
0675     data->client = client;
0676 
0677     error = silead_ts_set_default_fw_name(data, id);
0678     if (error)
0679         return error;
0680 
0681     silead_ts_read_props(client);
0682 
0683     /* We must have the IRQ provided by DT or ACPI subsystem */
0684     if (client->irq <= 0)
0685         return -ENODEV;
0686 
0687     data->regulators[0].supply = "vddio";
0688     data->regulators[1].supply = "avdd";
0689     error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
0690                     data->regulators);
0691     if (error)
0692         return error;
0693 
0694     /*
0695      * Enable regulators at probe and disable them at remove, we need
0696      * to keep the chip powered otherwise it forgets its firmware.
0697      */
0698     error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
0699                       data->regulators);
0700     if (error)
0701         return error;
0702 
0703     error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
0704     if (error)
0705         return error;
0706 
0707     /* Power GPIO pin */
0708     data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
0709     if (IS_ERR(data->gpio_power)) {
0710         if (PTR_ERR(data->gpio_power) != -EPROBE_DEFER)
0711             dev_err(dev, "Shutdown GPIO request failed\n");
0712         return PTR_ERR(data->gpio_power);
0713     }
0714 
0715     error = silead_ts_setup(client);
0716     if (error)
0717         return error;
0718 
0719     error = silead_ts_request_input_dev(data);
0720     if (error)
0721         return error;
0722 
0723     error = silead_ts_request_pen_input_dev(data);
0724     if (error)
0725         return error;
0726 
0727     error = devm_request_threaded_irq(dev, client->irq,
0728                       NULL, silead_ts_threaded_irq_handler,
0729                       IRQF_ONESHOT, client->name, data);
0730     if (error) {
0731         if (error != -EPROBE_DEFER)
0732             dev_err(dev, "IRQ request failed %d\n", error);
0733         return error;
0734     }
0735 
0736     return 0;
0737 }
0738 
0739 static int __maybe_unused silead_ts_suspend(struct device *dev)
0740 {
0741     struct i2c_client *client = to_i2c_client(dev);
0742 
0743     disable_irq(client->irq);
0744     silead_ts_set_power(client, SILEAD_POWER_OFF);
0745     return 0;
0746 }
0747 
0748 static int __maybe_unused silead_ts_resume(struct device *dev)
0749 {
0750     struct i2c_client *client = to_i2c_client(dev);
0751     bool second_try = false;
0752     int error, status;
0753 
0754     silead_ts_set_power(client, SILEAD_POWER_ON);
0755 
0756  retry:
0757     error = silead_ts_reset(client);
0758     if (error)
0759         return error;
0760 
0761     if (second_try) {
0762         error = silead_ts_load_fw(client);
0763         if (error)
0764             return error;
0765     }
0766 
0767     error = silead_ts_startup(client);
0768     if (error)
0769         return error;
0770 
0771     status = silead_ts_get_status(client);
0772     if (status != SILEAD_STATUS_OK) {
0773         if (!second_try) {
0774             second_try = true;
0775             dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
0776             goto retry;
0777         }
0778         dev_err(dev, "Resume error, status: 0x%02x\n", status);
0779         return -ENODEV;
0780     }
0781 
0782     enable_irq(client->irq);
0783 
0784     return 0;
0785 }
0786 
0787 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
0788 
0789 static const struct i2c_device_id silead_ts_id[] = {
0790     { "gsl1680", 0 },
0791     { "gsl1688", 0 },
0792     { "gsl3670", 0 },
0793     { "gsl3675", 0 },
0794     { "gsl3692", 0 },
0795     { "mssl1680", 0 },
0796     { }
0797 };
0798 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
0799 
0800 #ifdef CONFIG_ACPI
0801 static const struct acpi_device_id silead_ts_acpi_match[] = {
0802     { "GSL1680", 0 },
0803     { "GSL1688", 0 },
0804     { "GSL3670", 0 },
0805     { "GSL3675", 0 },
0806     { "GSL3692", 0 },
0807     { "MSSL1680", 0 },
0808     { "MSSL0001", 0 },
0809     { "MSSL0002", 0 },
0810     { "MSSL0017", 0 },
0811     { }
0812 };
0813 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
0814 #endif
0815 
0816 #ifdef CONFIG_OF
0817 static const struct of_device_id silead_ts_of_match[] = {
0818     { .compatible = "silead,gsl1680" },
0819     { .compatible = "silead,gsl1688" },
0820     { .compatible = "silead,gsl3670" },
0821     { .compatible = "silead,gsl3675" },
0822     { .compatible = "silead,gsl3692" },
0823     { },
0824 };
0825 MODULE_DEVICE_TABLE(of, silead_ts_of_match);
0826 #endif
0827 
0828 static struct i2c_driver silead_ts_driver = {
0829     .probe = silead_ts_probe,
0830     .id_table = silead_ts_id,
0831     .driver = {
0832         .name = SILEAD_TS_NAME,
0833         .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
0834         .of_match_table = of_match_ptr(silead_ts_of_match),
0835         .pm = &silead_ts_pm,
0836     },
0837 };
0838 module_i2c_driver(silead_ts_driver);
0839 
0840 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
0841 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
0842 MODULE_LICENSE("GPL");