Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Driver for Goodix Touchscreens
0004  *
0005  *  Copyright (c) 2014 Red Hat Inc.
0006  *  Copyright (c) 2015 K. Merker <merker@debian.org>
0007  *
0008  *  This code is based on gt9xx.c authored by andrew@goodix.com:
0009  *
0010  *  2010 - 2012 Goodix Technology.
0011  */
0012 
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/dmi.h>
0016 #include <linux/firmware.h>
0017 #include <linux/module.h>
0018 #include <linux/delay.h>
0019 #include <linux/irq.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/platform_data/x86/soc.h>
0022 #include <linux/slab.h>
0023 #include <linux/acpi.h>
0024 #include <linux/of.h>
0025 #include <asm/unaligned.h>
0026 #include "goodix.h"
0027 
0028 #define GOODIX_GPIO_INT_NAME        "irq"
0029 #define GOODIX_GPIO_RST_NAME        "reset"
0030 
0031 #define GOODIX_MAX_HEIGHT       4096
0032 #define GOODIX_MAX_WIDTH        4096
0033 #define GOODIX_INT_TRIGGER      1
0034 #define GOODIX_CONTACT_SIZE     8
0035 #define GOODIX_MAX_CONTACT_SIZE     9
0036 #define GOODIX_MAX_CONTACTS     10
0037 
0038 #define GOODIX_CONFIG_MIN_LENGTH    186
0039 #define GOODIX_CONFIG_911_LENGTH    186
0040 #define GOODIX_CONFIG_967_LENGTH    228
0041 #define GOODIX_CONFIG_GT9X_LENGTH   240
0042 
0043 #define GOODIX_BUFFER_STATUS_READY  BIT(7)
0044 #define GOODIX_HAVE_KEY         BIT(4)
0045 #define GOODIX_BUFFER_STATUS_TIMEOUT    20
0046 
0047 #define RESOLUTION_LOC      1
0048 #define MAX_CONTACTS_LOC    5
0049 #define TRIGGER_LOC     6
0050 
0051 /* Our special handling for GPIO accesses through ACPI is x86 specific */
0052 #if defined CONFIG_X86 && defined CONFIG_ACPI
0053 #define ACPI_GPIO_SUPPORT
0054 #endif
0055 
0056 struct goodix_chip_id {
0057     const char *id;
0058     const struct goodix_chip_data *data;
0059 };
0060 
0061 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
0062                   const u8 *cfg, int len);
0063 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
0064                    const u8 *cfg, int len);
0065 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
0066 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
0067 
0068 static const struct goodix_chip_data gt1x_chip_data = {
0069     .config_addr        = GOODIX_GT1X_REG_CONFIG_DATA,
0070     .config_len     = GOODIX_CONFIG_GT9X_LENGTH,
0071     .check_config       = goodix_check_cfg_16,
0072     .calc_config_checksum   = goodix_calc_cfg_checksum_16,
0073 };
0074 
0075 static const struct goodix_chip_data gt911_chip_data = {
0076     .config_addr        = GOODIX_GT9X_REG_CONFIG_DATA,
0077     .config_len     = GOODIX_CONFIG_911_LENGTH,
0078     .check_config       = goodix_check_cfg_8,
0079     .calc_config_checksum   = goodix_calc_cfg_checksum_8,
0080 };
0081 
0082 static const struct goodix_chip_data gt967_chip_data = {
0083     .config_addr        = GOODIX_GT9X_REG_CONFIG_DATA,
0084     .config_len     = GOODIX_CONFIG_967_LENGTH,
0085     .check_config       = goodix_check_cfg_8,
0086     .calc_config_checksum   = goodix_calc_cfg_checksum_8,
0087 };
0088 
0089 static const struct goodix_chip_data gt9x_chip_data = {
0090     .config_addr        = GOODIX_GT9X_REG_CONFIG_DATA,
0091     .config_len     = GOODIX_CONFIG_GT9X_LENGTH,
0092     .check_config       = goodix_check_cfg_8,
0093     .calc_config_checksum   = goodix_calc_cfg_checksum_8,
0094 };
0095 
0096 static const struct goodix_chip_id goodix_chip_ids[] = {
0097     { .id = "1151", .data = &gt1x_chip_data },
0098     { .id = "1158", .data = &gt1x_chip_data },
0099     { .id = "5663", .data = &gt1x_chip_data },
0100     { .id = "5688", .data = &gt1x_chip_data },
0101     { .id = "917S", .data = &gt1x_chip_data },
0102     { .id = "9286", .data = &gt1x_chip_data },
0103 
0104     { .id = "911", .data = &gt911_chip_data },
0105     { .id = "9271", .data = &gt911_chip_data },
0106     { .id = "9110", .data = &gt911_chip_data },
0107     { .id = "9111", .data = &gt911_chip_data },
0108     { .id = "927", .data = &gt911_chip_data },
0109     { .id = "928", .data = &gt911_chip_data },
0110 
0111     { .id = "912", .data = &gt967_chip_data },
0112     { .id = "9147", .data = &gt967_chip_data },
0113     { .id = "967", .data = &gt967_chip_data },
0114     { }
0115 };
0116 
0117 static const unsigned long goodix_irq_flags[] = {
0118     IRQ_TYPE_EDGE_RISING,
0119     IRQ_TYPE_EDGE_FALLING,
0120     IRQ_TYPE_LEVEL_LOW,
0121     IRQ_TYPE_LEVEL_HIGH,
0122 };
0123 
0124 static const struct dmi_system_id nine_bytes_report[] = {
0125 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
0126     {
0127         .ident = "Lenovo YogaBook",
0128         /* YB1-X91L/F and YB1-X90L/F */
0129         .matches = {
0130             DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9")
0131         }
0132     },
0133 #endif
0134     {}
0135 };
0136 
0137 /*
0138  * Those tablets have their x coordinate inverted
0139  */
0140 static const struct dmi_system_id inverted_x_screen[] = {
0141 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
0142     {
0143         .ident = "Cube I15-TC",
0144         .matches = {
0145             DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
0146             DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
0147         },
0148     },
0149 #endif
0150     {}
0151 };
0152 
0153 /**
0154  * goodix_i2c_read - read data from a register of the i2c slave device.
0155  *
0156  * @client: i2c device.
0157  * @reg: the register to read from.
0158  * @buf: raw write data buffer.
0159  * @len: length of the buffer to write
0160  */
0161 int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len)
0162 {
0163     struct i2c_msg msgs[2];
0164     __be16 wbuf = cpu_to_be16(reg);
0165     int ret;
0166 
0167     msgs[0].flags = 0;
0168     msgs[0].addr  = client->addr;
0169     msgs[0].len   = 2;
0170     msgs[0].buf   = (u8 *)&wbuf;
0171 
0172     msgs[1].flags = I2C_M_RD;
0173     msgs[1].addr  = client->addr;
0174     msgs[1].len   = len;
0175     msgs[1].buf   = buf;
0176 
0177     ret = i2c_transfer(client->adapter, msgs, 2);
0178     if (ret >= 0)
0179         ret = (ret == ARRAY_SIZE(msgs) ? 0 : -EIO);
0180 
0181     if (ret)
0182         dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d\n",
0183             len, reg, ret);
0184     return ret;
0185 }
0186 
0187 /**
0188  * goodix_i2c_write - write data to a register of the i2c slave device.
0189  *
0190  * @client: i2c device.
0191  * @reg: the register to write to.
0192  * @buf: raw data buffer to write.
0193  * @len: length of the buffer to write
0194  */
0195 int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, int len)
0196 {
0197     u8 *addr_buf;
0198     struct i2c_msg msg;
0199     int ret;
0200 
0201     addr_buf = kmalloc(len + 2, GFP_KERNEL);
0202     if (!addr_buf)
0203         return -ENOMEM;
0204 
0205     addr_buf[0] = reg >> 8;
0206     addr_buf[1] = reg & 0xFF;
0207     memcpy(&addr_buf[2], buf, len);
0208 
0209     msg.flags = 0;
0210     msg.addr = client->addr;
0211     msg.buf = addr_buf;
0212     msg.len = len + 2;
0213 
0214     ret = i2c_transfer(client->adapter, &msg, 1);
0215     if (ret >= 0)
0216         ret = (ret == 1 ? 0 : -EIO);
0217 
0218     kfree(addr_buf);
0219 
0220     if (ret)
0221         dev_err(&client->dev, "Error writing %d bytes to 0x%04x: %d\n",
0222             len, reg, ret);
0223     return ret;
0224 }
0225 
0226 int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
0227 {
0228     return goodix_i2c_write(client, reg, &value, sizeof(value));
0229 }
0230 
0231 static const struct goodix_chip_data *goodix_get_chip_data(const char *id)
0232 {
0233     unsigned int i;
0234 
0235     for (i = 0; goodix_chip_ids[i].id; i++) {
0236         if (!strcmp(goodix_chip_ids[i].id, id))
0237             return goodix_chip_ids[i].data;
0238     }
0239 
0240     return &gt9x_chip_data;
0241 }
0242 
0243 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
0244 {
0245     unsigned long max_timeout;
0246     int touch_num;
0247     int error;
0248     u16 addr = GOODIX_READ_COOR_ADDR;
0249     /*
0250      * We are going to read 1-byte header,
0251      * ts->contact_size * max(1, touch_num) bytes of coordinates
0252      * and 1-byte footer which contains the touch-key code.
0253      */
0254     const int header_contact_keycode_size = 1 + ts->contact_size + 1;
0255 
0256     /*
0257      * The 'buffer status' bit, which indicates that the data is valid, is
0258      * not set as soon as the interrupt is raised, but slightly after.
0259      * This takes around 10 ms to happen, so we poll for 20 ms.
0260      */
0261     max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
0262     do {
0263         error = goodix_i2c_read(ts->client, addr, data,
0264                     header_contact_keycode_size);
0265         if (error)
0266             return error;
0267 
0268         if (data[0] & GOODIX_BUFFER_STATUS_READY) {
0269             touch_num = data[0] & 0x0f;
0270             if (touch_num > ts->max_touch_num)
0271                 return -EPROTO;
0272 
0273             if (touch_num > 1) {
0274                 addr += header_contact_keycode_size;
0275                 data += header_contact_keycode_size;
0276                 error = goodix_i2c_read(ts->client,
0277                         addr, data,
0278                         ts->contact_size *
0279                             (touch_num - 1));
0280                 if (error)
0281                     return error;
0282             }
0283 
0284             return touch_num;
0285         }
0286 
0287         if (data[0] == 0 && ts->firmware_name) {
0288             if (goodix_handle_fw_request(ts))
0289                 return 0;
0290         }
0291 
0292         usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
0293     } while (time_before(jiffies, max_timeout));
0294 
0295     /*
0296      * The Goodix panel will send spurious interrupts after a
0297      * 'finger up' event, which will always cause a timeout.
0298      */
0299     return -ENOMSG;
0300 }
0301 
0302 static int goodix_create_pen_input(struct goodix_ts_data *ts)
0303 {
0304     struct device *dev = &ts->client->dev;
0305     struct input_dev *input;
0306 
0307     input = devm_input_allocate_device(dev);
0308     if (!input)
0309         return -ENOMEM;
0310 
0311     input_copy_abs(input, ABS_X, ts->input_dev, ABS_MT_POSITION_X);
0312     input_copy_abs(input, ABS_Y, ts->input_dev, ABS_MT_POSITION_Y);
0313     /*
0314      * The resolution of these touchscreens is about 10 units/mm, the actual
0315      * resolution does not matter much since we set INPUT_PROP_DIRECT.
0316      * Userspace wants something here though, so just set it to 10 units/mm.
0317      */
0318     input_abs_set_res(input, ABS_X, 10);
0319     input_abs_set_res(input, ABS_Y, 10);
0320     input_set_abs_params(input, ABS_PRESSURE, 0, 255, 0, 0);
0321 
0322     input_set_capability(input, EV_KEY, BTN_TOUCH);
0323     input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
0324     input_set_capability(input, EV_KEY, BTN_STYLUS);
0325     input_set_capability(input, EV_KEY, BTN_STYLUS2);
0326     __set_bit(INPUT_PROP_DIRECT, input->propbit);
0327 
0328     input->name = "Goodix Active Pen";
0329     input->phys = "input/pen";
0330     input->id.bustype = BUS_I2C;
0331     input->id.vendor = 0x0416;
0332     if (kstrtou16(ts->id, 10, &input->id.product))
0333         input->id.product = 0x1001;
0334     input->id.version = ts->version;
0335 
0336     ts->input_pen = input;
0337     return 0;
0338 }
0339 
0340 static void goodix_ts_report_pen_down(struct goodix_ts_data *ts, u8 *data)
0341 {
0342     int input_x, input_y, input_w, error;
0343     u8 key_value;
0344 
0345     if (!ts->pen_input_registered) {
0346         error = input_register_device(ts->input_pen);
0347         ts->pen_input_registered = (error == 0) ? 1 : error;
0348     }
0349 
0350     if (ts->pen_input_registered < 0)
0351         return;
0352 
0353     if (ts->contact_size == 9) {
0354         input_x = get_unaligned_le16(&data[4]);
0355         input_y = get_unaligned_le16(&data[6]);
0356         input_w = get_unaligned_le16(&data[8]);
0357     } else {
0358         input_x = get_unaligned_le16(&data[2]);
0359         input_y = get_unaligned_le16(&data[4]);
0360         input_w = get_unaligned_le16(&data[6]);
0361     }
0362 
0363     touchscreen_report_pos(ts->input_pen, &ts->prop, input_x, input_y, false);
0364     input_report_abs(ts->input_pen, ABS_PRESSURE, input_w);
0365 
0366     input_report_key(ts->input_pen, BTN_TOUCH, 1);
0367     input_report_key(ts->input_pen, BTN_TOOL_PEN, 1);
0368 
0369     if (data[0] & GOODIX_HAVE_KEY) {
0370         key_value = data[1 + ts->contact_size];
0371         input_report_key(ts->input_pen, BTN_STYLUS, key_value & 0x10);
0372         input_report_key(ts->input_pen, BTN_STYLUS2, key_value & 0x20);
0373     } else {
0374         input_report_key(ts->input_pen, BTN_STYLUS, 0);
0375         input_report_key(ts->input_pen, BTN_STYLUS2, 0);
0376     }
0377 
0378     input_sync(ts->input_pen);
0379 }
0380 
0381 static void goodix_ts_report_pen_up(struct goodix_ts_data *ts)
0382 {
0383     if (!ts->input_pen)
0384         return;
0385 
0386     input_report_key(ts->input_pen, BTN_TOUCH, 0);
0387     input_report_key(ts->input_pen, BTN_TOOL_PEN, 0);
0388     input_report_key(ts->input_pen, BTN_STYLUS, 0);
0389     input_report_key(ts->input_pen, BTN_STYLUS2, 0);
0390 
0391     input_sync(ts->input_pen);
0392 }
0393 
0394 static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
0395 {
0396     int id = coor_data[0] & 0x0F;
0397     int input_x = get_unaligned_le16(&coor_data[1]);
0398     int input_y = get_unaligned_le16(&coor_data[3]);
0399     int input_w = get_unaligned_le16(&coor_data[5]);
0400 
0401     input_mt_slot(ts->input_dev, id);
0402     input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
0403     touchscreen_report_pos(ts->input_dev, &ts->prop,
0404                    input_x, input_y, true);
0405     input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
0406     input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
0407 }
0408 
0409 static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
0410 {
0411     int id = coor_data[1] & 0x0F;
0412     int input_x = get_unaligned_le16(&coor_data[3]);
0413     int input_y = get_unaligned_le16(&coor_data[5]);
0414     int input_w = get_unaligned_le16(&coor_data[7]);
0415 
0416     input_mt_slot(ts->input_dev, id);
0417     input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
0418     touchscreen_report_pos(ts->input_dev, &ts->prop,
0419                    input_x, input_y, true);
0420     input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
0421     input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
0422 }
0423 
0424 static void goodix_ts_release_keys(struct goodix_ts_data *ts)
0425 {
0426     int i;
0427 
0428     for (i = 0; i < GOODIX_MAX_KEYS; i++)
0429         input_report_key(ts->input_dev, ts->keymap[i], 0);
0430 }
0431 
0432 static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
0433 {
0434     int touch_num;
0435     u8 key_value;
0436     int i;
0437 
0438     if (data[0] & GOODIX_HAVE_KEY) {
0439         touch_num = data[0] & 0x0f;
0440         key_value = data[1 + ts->contact_size * touch_num];
0441         for (i = 0; i < GOODIX_MAX_KEYS; i++)
0442             if (key_value & BIT(i))
0443                 input_report_key(ts->input_dev,
0444                          ts->keymap[i], 1);
0445     } else {
0446         goodix_ts_release_keys(ts);
0447     }
0448 }
0449 
0450 /**
0451  * goodix_process_events - Process incoming events
0452  *
0453  * @ts: our goodix_ts_data pointer
0454  *
0455  * Called when the IRQ is triggered. Read the current device state, and push
0456  * the input events to the user space.
0457  */
0458 static void goodix_process_events(struct goodix_ts_data *ts)
0459 {
0460     u8  point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
0461     int touch_num;
0462     int i;
0463 
0464     touch_num = goodix_ts_read_input_report(ts, point_data);
0465     if (touch_num < 0)
0466         return;
0467 
0468     /* The pen being down is always reported as a single touch */
0469     if (touch_num == 1 && (point_data[1] & 0x80)) {
0470         goodix_ts_report_pen_down(ts, point_data);
0471         goodix_ts_release_keys(ts);
0472         goto sync; /* Release any previously registered touches */
0473     } else {
0474         goodix_ts_report_pen_up(ts);
0475     }
0476 
0477     goodix_ts_report_key(ts, point_data);
0478 
0479     for (i = 0; i < touch_num; i++)
0480         if (ts->contact_size == 9)
0481             goodix_ts_report_touch_9b(ts,
0482                 &point_data[1 + ts->contact_size * i]);
0483         else
0484             goodix_ts_report_touch_8b(ts,
0485                 &point_data[1 + ts->contact_size * i]);
0486 
0487 sync:
0488     input_mt_sync_frame(ts->input_dev);
0489     input_sync(ts->input_dev);
0490 }
0491 
0492 /**
0493  * goodix_ts_irq_handler - The IRQ handler
0494  *
0495  * @irq: interrupt number.
0496  * @dev_id: private data pointer.
0497  */
0498 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
0499 {
0500     struct goodix_ts_data *ts = dev_id;
0501 
0502     goodix_process_events(ts);
0503     goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
0504 
0505     return IRQ_HANDLED;
0506 }
0507 
0508 static void goodix_free_irq(struct goodix_ts_data *ts)
0509 {
0510     devm_free_irq(&ts->client->dev, ts->client->irq, ts);
0511 }
0512 
0513 static int goodix_request_irq(struct goodix_ts_data *ts)
0514 {
0515     return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
0516                      NULL, goodix_ts_irq_handler,
0517                      ts->irq_flags, ts->client->name, ts);
0518 }
0519 
0520 static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
0521 {
0522     int i, raw_cfg_len = len - 2;
0523     u8 check_sum = 0;
0524 
0525     for (i = 0; i < raw_cfg_len; i++)
0526         check_sum += cfg[i];
0527     check_sum = (~check_sum) + 1;
0528     if (check_sum != cfg[raw_cfg_len]) {
0529         dev_err(&ts->client->dev,
0530             "The checksum of the config fw is not correct");
0531         return -EINVAL;
0532     }
0533 
0534     if (cfg[raw_cfg_len + 1] != 1) {
0535         dev_err(&ts->client->dev,
0536             "Config fw must have Config_Fresh register set");
0537         return -EINVAL;
0538     }
0539 
0540     return 0;
0541 }
0542 
0543 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
0544 {
0545     int i, raw_cfg_len = ts->chip->config_len - 2;
0546     u8 check_sum = 0;
0547 
0548     for (i = 0; i < raw_cfg_len; i++)
0549         check_sum += ts->config[i];
0550     check_sum = (~check_sum) + 1;
0551 
0552     ts->config[raw_cfg_len] = check_sum;
0553     ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
0554 }
0555 
0556 static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
0557                    int len)
0558 {
0559     int i, raw_cfg_len = len - 3;
0560     u16 check_sum = 0;
0561 
0562     for (i = 0; i < raw_cfg_len; i += 2)
0563         check_sum += get_unaligned_be16(&cfg[i]);
0564     check_sum = (~check_sum) + 1;
0565     if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
0566         dev_err(&ts->client->dev,
0567             "The checksum of the config fw is not correct");
0568         return -EINVAL;
0569     }
0570 
0571     if (cfg[raw_cfg_len + 2] != 1) {
0572         dev_err(&ts->client->dev,
0573             "Config fw must have Config_Fresh register set");
0574         return -EINVAL;
0575     }
0576 
0577     return 0;
0578 }
0579 
0580 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
0581 {
0582     int i, raw_cfg_len = ts->chip->config_len - 3;
0583     u16 check_sum = 0;
0584 
0585     for (i = 0; i < raw_cfg_len; i += 2)
0586         check_sum += get_unaligned_be16(&ts->config[i]);
0587     check_sum = (~check_sum) + 1;
0588 
0589     put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
0590     ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
0591 }
0592 
0593 /**
0594  * goodix_check_cfg - Checks if config fw is valid
0595  *
0596  * @ts: goodix_ts_data pointer
0597  * @cfg: firmware config data
0598  * @len: config data length
0599  */
0600 static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
0601 {
0602     if (len < GOODIX_CONFIG_MIN_LENGTH ||
0603         len > GOODIX_CONFIG_MAX_LENGTH) {
0604         dev_err(&ts->client->dev,
0605             "The length of the config fw is not correct");
0606         return -EINVAL;
0607     }
0608 
0609     return ts->chip->check_config(ts, cfg, len);
0610 }
0611 
0612 /**
0613  * goodix_send_cfg - Write fw config to device
0614  *
0615  * @ts: goodix_ts_data pointer
0616  * @cfg: config firmware to write to device
0617  * @len: config data length
0618  */
0619 int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
0620 {
0621     int error;
0622 
0623     error = goodix_check_cfg(ts, cfg, len);
0624     if (error)
0625         return error;
0626 
0627     error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
0628     if (error)
0629         return error;
0630 
0631     dev_dbg(&ts->client->dev, "Config sent successfully.");
0632 
0633     /* Let the firmware reconfigure itself, so sleep for 10ms */
0634     usleep_range(10000, 11000);
0635 
0636     return 0;
0637 }
0638 
0639 #ifdef ACPI_GPIO_SUPPORT
0640 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
0641 {
0642     acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
0643     acpi_status status;
0644 
0645     status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
0646     return ACPI_SUCCESS(status) ? 0 : -EIO;
0647 }
0648 
0649 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
0650 {
0651     acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
0652     acpi_status status;
0653 
0654     status = acpi_execute_simple_method(handle, "INTO", value);
0655     return ACPI_SUCCESS(status) ? 0 : -EIO;
0656 }
0657 #else
0658 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
0659 {
0660     dev_err(&ts->client->dev,
0661         "%s called on device without ACPI support\n", __func__);
0662     return -EINVAL;
0663 }
0664 
0665 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
0666 {
0667     dev_err(&ts->client->dev,
0668         "%s called on device without ACPI support\n", __func__);
0669     return -EINVAL;
0670 }
0671 #endif
0672 
0673 static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
0674 {
0675     switch (ts->irq_pin_access_method) {
0676     case IRQ_PIN_ACCESS_NONE:
0677         dev_err(&ts->client->dev,
0678             "%s called without an irq_pin_access_method set\n",
0679             __func__);
0680         return -EINVAL;
0681     case IRQ_PIN_ACCESS_GPIO:
0682         return gpiod_direction_output(ts->gpiod_int, value);
0683     case IRQ_PIN_ACCESS_ACPI_GPIO:
0684         /*
0685          * The IRQ pin triggers on a falling edge, so its gets marked
0686          * as active-low, use output_raw to avoid the value inversion.
0687          */
0688         return gpiod_direction_output_raw(ts->gpiod_int, value);
0689     case IRQ_PIN_ACCESS_ACPI_METHOD:
0690         return goodix_pin_acpi_output_method(ts, value);
0691     }
0692 
0693     return -EINVAL; /* Never reached */
0694 }
0695 
0696 static int goodix_irq_direction_input(struct goodix_ts_data *ts)
0697 {
0698     switch (ts->irq_pin_access_method) {
0699     case IRQ_PIN_ACCESS_NONE:
0700         dev_err(&ts->client->dev,
0701             "%s called without an irq_pin_access_method set\n",
0702             __func__);
0703         return -EINVAL;
0704     case IRQ_PIN_ACCESS_GPIO:
0705         return gpiod_direction_input(ts->gpiod_int);
0706     case IRQ_PIN_ACCESS_ACPI_GPIO:
0707         return gpiod_direction_input(ts->gpiod_int);
0708     case IRQ_PIN_ACCESS_ACPI_METHOD:
0709         return goodix_pin_acpi_direction_input(ts);
0710     }
0711 
0712     return -EINVAL; /* Never reached */
0713 }
0714 
0715 int goodix_int_sync(struct goodix_ts_data *ts)
0716 {
0717     int error;
0718 
0719     error = goodix_irq_direction_output(ts, 0);
0720     if (error)
0721         goto error;
0722 
0723     msleep(50);             /* T5: 50ms */
0724 
0725     error = goodix_irq_direction_input(ts);
0726     if (error)
0727         goto error;
0728 
0729     return 0;
0730 
0731 error:
0732     dev_err(&ts->client->dev, "Controller irq sync failed.\n");
0733     return error;
0734 }
0735 
0736 /**
0737  * goodix_reset_no_int_sync - Reset device, leaving interrupt line in output mode
0738  *
0739  * @ts: goodix_ts_data pointer
0740  */
0741 int goodix_reset_no_int_sync(struct goodix_ts_data *ts)
0742 {
0743     int error;
0744 
0745     /* begin select I2C slave addr */
0746     error = gpiod_direction_output(ts->gpiod_rst, 0);
0747     if (error)
0748         goto error;
0749 
0750     msleep(20);             /* T2: > 10ms */
0751 
0752     /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
0753     error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
0754     if (error)
0755         goto error;
0756 
0757     usleep_range(100, 2000);        /* T3: > 100us */
0758 
0759     error = gpiod_direction_output(ts->gpiod_rst, 1);
0760     if (error)
0761         goto error;
0762 
0763     usleep_range(6000, 10000);      /* T4: > 5ms */
0764 
0765     /*
0766      * Put the reset pin back in to input / high-impedance mode to save
0767      * power. Only do this in the non ACPI case since some ACPI boards
0768      * don't have a pull-up, so there the reset pin must stay active-high.
0769      */
0770     if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_GPIO) {
0771         error = gpiod_direction_input(ts->gpiod_rst);
0772         if (error)
0773             goto error;
0774     }
0775 
0776     return 0;
0777 
0778 error:
0779     dev_err(&ts->client->dev, "Controller reset failed.\n");
0780     return error;
0781 }
0782 
0783 /**
0784  * goodix_reset - Reset device during power on
0785  *
0786  * @ts: goodix_ts_data pointer
0787  */
0788 static int goodix_reset(struct goodix_ts_data *ts)
0789 {
0790     int error;
0791 
0792     error = goodix_reset_no_int_sync(ts);
0793     if (error)
0794         return error;
0795 
0796     return goodix_int_sync(ts);
0797 }
0798 
0799 #ifdef ACPI_GPIO_SUPPORT
0800 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
0801 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
0802 
0803 static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
0804     { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
0805     { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
0806     { },
0807 };
0808 
0809 static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
0810     { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
0811     { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
0812     { },
0813 };
0814 
0815 static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
0816     { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
0817     { },
0818 };
0819 
0820 static int goodix_resource(struct acpi_resource *ares, void *data)
0821 {
0822     struct goodix_ts_data *ts = data;
0823     struct device *dev = &ts->client->dev;
0824     struct acpi_resource_gpio *gpio;
0825 
0826     if (acpi_gpio_get_irq_resource(ares, &gpio)) {
0827         if (ts->gpio_int_idx == -1) {
0828             ts->gpio_int_idx = ts->gpio_count;
0829         } else {
0830             dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
0831             ts->gpio_int_idx = -2;
0832         }
0833         ts->gpio_count++;
0834     } else if (acpi_gpio_get_io_resource(ares, &gpio))
0835         ts->gpio_count++;
0836 
0837     return 0;
0838 }
0839 
0840 /*
0841  * This function gets called in case we fail to get the irq GPIO directly
0842  * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
0843  * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
0844  * In that case we add our own mapping and then goodix_get_gpio_config()
0845  * retries to get the GPIOs based on the added mapping.
0846  */
0847 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
0848 {
0849     const struct acpi_gpio_mapping *gpio_mapping = NULL;
0850     struct device *dev = &ts->client->dev;
0851     LIST_HEAD(resources);
0852     int irq, ret;
0853 
0854     ts->gpio_count = 0;
0855     ts->gpio_int_idx = -1;
0856     ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
0857                      goodix_resource, ts);
0858     if (ret < 0) {
0859         dev_err(dev, "Error getting ACPI resources: %d\n", ret);
0860         return ret;
0861     }
0862 
0863     acpi_dev_free_resource_list(&resources);
0864 
0865     /*
0866      * CHT devices should have a GpioInt + a regular GPIO ACPI resource.
0867      * Some CHT devices have a bug (where the also is bogus Interrupt
0868      * resource copied from a previous BYT based generation). i2c-core-acpi
0869      * will use the non-working Interrupt resource, fix this up.
0870      */
0871     if (soc_intel_is_cht() && ts->gpio_count == 2 && ts->gpio_int_idx != -1) {
0872         irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
0873         if (irq > 0 && irq != ts->client->irq) {
0874             dev_warn(dev, "Overriding IRQ %d -> %d\n", ts->client->irq, irq);
0875             ts->client->irq = irq;
0876         }
0877     }
0878 
0879     if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) {
0880         ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
0881         gpio_mapping = acpi_goodix_int_first_gpios;
0882     } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
0883         ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
0884         gpio_mapping = acpi_goodix_int_last_gpios;
0885     } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
0886            acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
0887            acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
0888         dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
0889         ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
0890         gpio_mapping = acpi_goodix_reset_only_gpios;
0891     } else if (soc_intel_is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
0892         dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
0893         ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
0894         gpio_mapping = acpi_goodix_int_last_gpios;
0895     } else {
0896         dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
0897              ts->gpio_count, ts->gpio_int_idx);
0898         /*
0899          * On some devices _PS0 does a reset for us and
0900          * sometimes this is necessary for things to work.
0901          */
0902         acpi_device_fix_up_power(ACPI_COMPANION(dev));
0903         return -EINVAL;
0904     }
0905 
0906     /*
0907      * Normally we put the reset pin in input / high-impedance mode to save
0908      * power. But some x86/ACPI boards don't have a pull-up, so for the ACPI
0909      * case, leave the pin as is. This results in the pin not being touched
0910      * at all on x86/ACPI boards, except when needed for error-recover.
0911      */
0912     ts->gpiod_rst_flags = GPIOD_ASIS;
0913 
0914     return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
0915 }
0916 #else
0917 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
0918 {
0919     return -EINVAL;
0920 }
0921 #endif /* CONFIG_X86 && CONFIG_ACPI */
0922 
0923 /**
0924  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
0925  *
0926  * @ts: goodix_ts_data pointer
0927  */
0928 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
0929 {
0930     int error;
0931     struct device *dev;
0932     struct gpio_desc *gpiod;
0933     bool added_acpi_mappings = false;
0934 
0935     if (!ts->client)
0936         return -EINVAL;
0937     dev = &ts->client->dev;
0938 
0939     /*
0940      * By default we request the reset pin as input, leaving it in
0941      * high-impedance when not resetting the controller to save power.
0942      */
0943     ts->gpiod_rst_flags = GPIOD_IN;
0944 
0945     ts->avdd28 = devm_regulator_get(dev, "AVDD28");
0946     if (IS_ERR(ts->avdd28)) {
0947         error = PTR_ERR(ts->avdd28);
0948         if (error != -EPROBE_DEFER)
0949             dev_err(dev,
0950                 "Failed to get AVDD28 regulator: %d\n", error);
0951         return error;
0952     }
0953 
0954     ts->vddio = devm_regulator_get(dev, "VDDIO");
0955     if (IS_ERR(ts->vddio)) {
0956         error = PTR_ERR(ts->vddio);
0957         if (error != -EPROBE_DEFER)
0958             dev_err(dev,
0959                 "Failed to get VDDIO regulator: %d\n", error);
0960         return error;
0961     }
0962 
0963 retry_get_irq_gpio:
0964     /* Get the interrupt GPIO pin number */
0965     gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
0966     if (IS_ERR(gpiod)) {
0967         error = PTR_ERR(gpiod);
0968         if (error != -EPROBE_DEFER)
0969             dev_err(dev, "Failed to get %s GPIO: %d\n",
0970                 GOODIX_GPIO_INT_NAME, error);
0971         return error;
0972     }
0973     if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
0974         added_acpi_mappings = true;
0975         if (goodix_add_acpi_gpio_mappings(ts) == 0)
0976             goto retry_get_irq_gpio;
0977     }
0978 
0979     ts->gpiod_int = gpiod;
0980 
0981     /* Get the reset line GPIO pin number */
0982     gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, ts->gpiod_rst_flags);
0983     if (IS_ERR(gpiod)) {
0984         error = PTR_ERR(gpiod);
0985         if (error != -EPROBE_DEFER)
0986             dev_err(dev, "Failed to get %s GPIO: %d\n",
0987                 GOODIX_GPIO_RST_NAME, error);
0988         return error;
0989     }
0990 
0991     ts->gpiod_rst = gpiod;
0992 
0993     switch (ts->irq_pin_access_method) {
0994     case IRQ_PIN_ACCESS_ACPI_GPIO:
0995         /*
0996          * We end up here if goodix_add_acpi_gpio_mappings() has
0997          * called devm_acpi_dev_add_driver_gpios() because the ACPI
0998          * tables did not contain name to index mappings.
0999          * Check that we successfully got both GPIOs after we've
1000          * added our own acpi_gpio_mapping and if we did not get both
1001          * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
1002          */
1003         if (!ts->gpiod_int || !ts->gpiod_rst)
1004             ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
1005         break;
1006     case IRQ_PIN_ACCESS_ACPI_METHOD:
1007         if (!ts->gpiod_rst)
1008             ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
1009         break;
1010     default:
1011         if (ts->gpiod_int && ts->gpiod_rst) {
1012             ts->reset_controller_at_probe = true;
1013             ts->load_cfg_from_disk = true;
1014             ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
1015         }
1016     }
1017 
1018     return 0;
1019 }
1020 
1021 /**
1022  * goodix_read_config - Read the embedded configuration of the panel
1023  *
1024  * @ts: our goodix_ts_data pointer
1025  *
1026  * Must be called during probe
1027  */
1028 static void goodix_read_config(struct goodix_ts_data *ts)
1029 {
1030     int x_max, y_max;
1031     int error;
1032 
1033     /*
1034      * On controllers where we need to upload the firmware
1035      * (controllers without flash) ts->config already has the config
1036      * at this point and the controller itself does not have it yet!
1037      */
1038     if (!ts->firmware_name) {
1039         error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1040                     ts->config, ts->chip->config_len);
1041         if (error) {
1042             ts->int_trigger_type = GOODIX_INT_TRIGGER;
1043             ts->max_touch_num = GOODIX_MAX_CONTACTS;
1044             return;
1045         }
1046     }
1047 
1048     ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
1049     ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
1050 
1051     x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
1052     y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
1053     if (x_max && y_max) {
1054         input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
1055         input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
1056     }
1057 
1058     ts->chip->calc_config_checksum(ts);
1059 }
1060 
1061 /**
1062  * goodix_read_version - Read goodix touchscreen version
1063  *
1064  * @ts: our goodix_ts_data pointer
1065  */
1066 static int goodix_read_version(struct goodix_ts_data *ts)
1067 {
1068     int error;
1069     u8 buf[6];
1070     char id_str[GOODIX_ID_MAX_LEN + 1];
1071 
1072     error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
1073     if (error)
1074         return error;
1075 
1076     memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
1077     id_str[GOODIX_ID_MAX_LEN] = 0;
1078     strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
1079 
1080     ts->version = get_unaligned_le16(&buf[4]);
1081 
1082     dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
1083          ts->version);
1084 
1085     return 0;
1086 }
1087 
1088 /**
1089  * goodix_i2c_test - I2C test function to check if the device answers.
1090  *
1091  * @client: the i2c client
1092  */
1093 static int goodix_i2c_test(struct i2c_client *client)
1094 {
1095     int retry = 0;
1096     int error;
1097     u8 test;
1098 
1099     while (retry++ < 2) {
1100         error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1);
1101         if (!error)
1102             return 0;
1103 
1104         msleep(20);
1105     }
1106 
1107     return error;
1108 }
1109 
1110 /**
1111  * goodix_configure_dev - Finish device initialization
1112  *
1113  * @ts: our goodix_ts_data pointer
1114  *
1115  * Must be called from probe to finish initialization of the device.
1116  * Contains the common initialization code for both devices that
1117  * declare gpio pins and devices that do not. It is either called
1118  * directly from probe or from request_firmware_wait callback.
1119  */
1120 static int goodix_configure_dev(struct goodix_ts_data *ts)
1121 {
1122     int error;
1123     int i;
1124 
1125     ts->int_trigger_type = GOODIX_INT_TRIGGER;
1126     ts->max_touch_num = GOODIX_MAX_CONTACTS;
1127 
1128     ts->input_dev = devm_input_allocate_device(&ts->client->dev);
1129     if (!ts->input_dev) {
1130         dev_err(&ts->client->dev, "Failed to allocate input device.");
1131         return -ENOMEM;
1132     }
1133 
1134     ts->input_dev->name = "Goodix Capacitive TouchScreen";
1135     ts->input_dev->phys = "input/ts";
1136     ts->input_dev->id.bustype = BUS_I2C;
1137     ts->input_dev->id.vendor = 0x0416;
1138     if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
1139         ts->input_dev->id.product = 0x1001;
1140     ts->input_dev->id.version = ts->version;
1141 
1142     ts->input_dev->keycode = ts->keymap;
1143     ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
1144     ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
1145 
1146     /* Capacitive Windows/Home button on some devices */
1147     for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
1148         if (i == 0)
1149             ts->keymap[i] = KEY_LEFTMETA;
1150         else
1151             ts->keymap[i] = KEY_F1 + (i - 1);
1152 
1153         input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
1154     }
1155 
1156     input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
1157     input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
1158     input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
1159     input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1160 
1161     /* Read configuration and apply touchscreen parameters */
1162     goodix_read_config(ts);
1163 
1164     /* Try overriding touchscreen parameters via device properties */
1165     touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
1166 
1167     if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
1168         dev_err(&ts->client->dev,
1169             "Invalid config (%d, %d, %d), using defaults\n",
1170             ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
1171         ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
1172         ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
1173         ts->max_touch_num = GOODIX_MAX_CONTACTS;
1174         input_abs_set_max(ts->input_dev,
1175                   ABS_MT_POSITION_X, ts->prop.max_x);
1176         input_abs_set_max(ts->input_dev,
1177                   ABS_MT_POSITION_Y, ts->prop.max_y);
1178     }
1179 
1180     if (dmi_check_system(nine_bytes_report)) {
1181         ts->contact_size = 9;
1182 
1183         dev_dbg(&ts->client->dev,
1184             "Non-standard 9-bytes report format quirk\n");
1185     }
1186 
1187     if (dmi_check_system(inverted_x_screen)) {
1188         ts->prop.invert_x = true;
1189         dev_dbg(&ts->client->dev,
1190             "Applying 'inverted x screen' quirk\n");
1191     }
1192 
1193     error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
1194                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1195     if (error) {
1196         dev_err(&ts->client->dev,
1197             "Failed to initialize MT slots: %d", error);
1198         return error;
1199     }
1200 
1201     error = input_register_device(ts->input_dev);
1202     if (error) {
1203         dev_err(&ts->client->dev,
1204             "Failed to register input device: %d", error);
1205         return error;
1206     }
1207 
1208     /*
1209      * Create the input_pen device before goodix_request_irq() calls
1210      * devm_request_threaded_irq() so that the devm framework frees
1211      * it after disabling the irq.
1212      * Unfortunately there is no way to detect if the touchscreen has pen
1213      * support, so registering the dev is delayed till the first pen event.
1214      */
1215     error = goodix_create_pen_input(ts);
1216     if (error)
1217         return error;
1218 
1219     ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
1220     error = goodix_request_irq(ts);
1221     if (error) {
1222         dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
1223         return error;
1224     }
1225 
1226     return 0;
1227 }
1228 
1229 /**
1230  * goodix_config_cb - Callback to finish device init
1231  *
1232  * @cfg: firmware config
1233  * @ctx: our goodix_ts_data pointer
1234  *
1235  * request_firmware_wait callback that finishes
1236  * initialization of the device.
1237  */
1238 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
1239 {
1240     struct goodix_ts_data *ts = ctx;
1241     int error;
1242 
1243     if (ts->firmware_name) {
1244         if (!cfg)
1245             goto err_release_cfg;
1246 
1247         error = goodix_check_cfg(ts, cfg->data, cfg->size);
1248         if (error)
1249             goto err_release_cfg;
1250 
1251         memcpy(ts->config, cfg->data, cfg->size);
1252     } else if (cfg) {
1253         /* send device configuration to the firmware */
1254         error = goodix_send_cfg(ts, cfg->data, cfg->size);
1255         if (error)
1256             goto err_release_cfg;
1257     }
1258 
1259     goodix_configure_dev(ts);
1260 
1261 err_release_cfg:
1262     release_firmware(cfg);
1263     complete_all(&ts->firmware_loading_complete);
1264 }
1265 
1266 static void goodix_disable_regulators(void *arg)
1267 {
1268     struct goodix_ts_data *ts = arg;
1269 
1270     regulator_disable(ts->vddio);
1271     regulator_disable(ts->avdd28);
1272 }
1273 
1274 static int goodix_ts_probe(struct i2c_client *client,
1275                const struct i2c_device_id *id)
1276 {
1277     struct goodix_ts_data *ts;
1278     const char *cfg_name;
1279     int error;
1280 
1281     dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
1282 
1283     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1284         dev_err(&client->dev, "I2C check functionality failed.\n");
1285         return -ENXIO;
1286     }
1287 
1288     ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1289     if (!ts)
1290         return -ENOMEM;
1291 
1292     ts->client = client;
1293     i2c_set_clientdata(client, ts);
1294     init_completion(&ts->firmware_loading_complete);
1295     ts->contact_size = GOODIX_CONTACT_SIZE;
1296 
1297     error = goodix_get_gpio_config(ts);
1298     if (error)
1299         return error;
1300 
1301     /* power up the controller */
1302     error = regulator_enable(ts->avdd28);
1303     if (error) {
1304         dev_err(&client->dev,
1305             "Failed to enable AVDD28 regulator: %d\n",
1306             error);
1307         return error;
1308     }
1309 
1310     error = regulator_enable(ts->vddio);
1311     if (error) {
1312         dev_err(&client->dev,
1313             "Failed to enable VDDIO regulator: %d\n",
1314             error);
1315         regulator_disable(ts->avdd28);
1316         return error;
1317     }
1318 
1319     error = devm_add_action_or_reset(&client->dev,
1320                      goodix_disable_regulators, ts);
1321     if (error)
1322         return error;
1323 
1324 reset:
1325     if (ts->reset_controller_at_probe) {
1326         /* reset the controller */
1327         error = goodix_reset(ts);
1328         if (error)
1329             return error;
1330     }
1331 
1332     error = goodix_i2c_test(client);
1333     if (error) {
1334         if (!ts->reset_controller_at_probe &&
1335             ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1336             /* Retry after a controller reset */
1337             ts->reset_controller_at_probe = true;
1338             goto reset;
1339         }
1340         dev_err(&client->dev, "I2C communication failure: %d\n", error);
1341         return error;
1342     }
1343 
1344     error = goodix_firmware_check(ts);
1345     if (error)
1346         return error;
1347 
1348     error = goodix_read_version(ts);
1349     if (error)
1350         return error;
1351 
1352     ts->chip = goodix_get_chip_data(ts->id);
1353 
1354     if (ts->load_cfg_from_disk) {
1355         /* update device config */
1356         error = device_property_read_string(&client->dev,
1357                             "goodix,config-name",
1358                             &cfg_name);
1359         if (!error)
1360             snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1361                  "goodix/%s", cfg_name);
1362         else
1363             snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1364                  "goodix_%s_cfg.bin", ts->id);
1365 
1366         error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
1367                         &client->dev, GFP_KERNEL, ts,
1368                         goodix_config_cb);
1369         if (error) {
1370             dev_err(&client->dev,
1371                 "Failed to invoke firmware loader: %d\n",
1372                 error);
1373             return error;
1374         }
1375 
1376         return 0;
1377     } else {
1378         error = goodix_configure_dev(ts);
1379         if (error)
1380             return error;
1381     }
1382 
1383     return 0;
1384 }
1385 
1386 static int goodix_ts_remove(struct i2c_client *client)
1387 {
1388     struct goodix_ts_data *ts = i2c_get_clientdata(client);
1389 
1390     if (ts->load_cfg_from_disk)
1391         wait_for_completion(&ts->firmware_loading_complete);
1392 
1393     return 0;
1394 }
1395 
1396 static int __maybe_unused goodix_suspend(struct device *dev)
1397 {
1398     struct i2c_client *client = to_i2c_client(dev);
1399     struct goodix_ts_data *ts = i2c_get_clientdata(client);
1400     int error;
1401 
1402     if (ts->load_cfg_from_disk)
1403         wait_for_completion(&ts->firmware_loading_complete);
1404 
1405     /* We need gpio pins to suspend/resume */
1406     if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1407         disable_irq(client->irq);
1408         return 0;
1409     }
1410 
1411     /* Free IRQ as IRQ pin is used as output in the suspend sequence */
1412     goodix_free_irq(ts);
1413 
1414     /* Save reference (calibration) info if necessary */
1415     goodix_save_bak_ref(ts);
1416 
1417     /* Output LOW on the INT pin for 5 ms */
1418     error = goodix_irq_direction_output(ts, 0);
1419     if (error) {
1420         goodix_request_irq(ts);
1421         return error;
1422     }
1423 
1424     usleep_range(5000, 6000);
1425 
1426     error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
1427                     GOODIX_CMD_SCREEN_OFF);
1428     if (error) {
1429         goodix_irq_direction_input(ts);
1430         goodix_request_irq(ts);
1431         return -EAGAIN;
1432     }
1433 
1434     /*
1435      * The datasheet specifies that the interval between sending screen-off
1436      * command and wake-up should be longer than 58 ms. To avoid waking up
1437      * sooner, delay 58ms here.
1438      */
1439     msleep(58);
1440     return 0;
1441 }
1442 
1443 static int __maybe_unused goodix_resume(struct device *dev)
1444 {
1445     struct i2c_client *client = to_i2c_client(dev);
1446     struct goodix_ts_data *ts = i2c_get_clientdata(client);
1447     u8 config_ver;
1448     int error;
1449 
1450     if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1451         enable_irq(client->irq);
1452         return 0;
1453     }
1454 
1455     /*
1456      * Exit sleep mode by outputting HIGH level to INT pin
1457      * for 2ms~5ms.
1458      */
1459     error = goodix_irq_direction_output(ts, 1);
1460     if (error)
1461         return error;
1462 
1463     usleep_range(2000, 5000);
1464 
1465     error = goodix_int_sync(ts);
1466     if (error)
1467         return error;
1468 
1469     error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1470                 &config_ver, 1);
1471     if (!error && config_ver != ts->config[0])
1472         dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
1473              config_ver, ts->config[0]);
1474 
1475     if (error != 0 || config_ver != ts->config[0]) {
1476         error = goodix_reset(ts);
1477         if (error)
1478             return error;
1479 
1480         error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
1481         if (error)
1482             return error;
1483     }
1484 
1485     error = goodix_request_irq(ts);
1486     if (error)
1487         return error;
1488 
1489     return 0;
1490 }
1491 
1492 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1493 
1494 static const struct i2c_device_id goodix_ts_id[] = {
1495     { "GDIX1001:00", 0 },
1496     { }
1497 };
1498 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1499 
1500 #ifdef CONFIG_ACPI
1501 static const struct acpi_device_id goodix_acpi_match[] = {
1502     { "GDIX1001", 0 },
1503     { "GDIX1002", 0 },
1504     { }
1505 };
1506 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1507 #endif
1508 
1509 #ifdef CONFIG_OF
1510 static const struct of_device_id goodix_of_match[] = {
1511     { .compatible = "goodix,gt1151" },
1512     { .compatible = "goodix,gt1158" },
1513     { .compatible = "goodix,gt5663" },
1514     { .compatible = "goodix,gt5688" },
1515     { .compatible = "goodix,gt911" },
1516     { .compatible = "goodix,gt9110" },
1517     { .compatible = "goodix,gt912" },
1518     { .compatible = "goodix,gt9147" },
1519     { .compatible = "goodix,gt917s" },
1520     { .compatible = "goodix,gt927" },
1521     { .compatible = "goodix,gt9271" },
1522     { .compatible = "goodix,gt928" },
1523     { .compatible = "goodix,gt9286" },
1524     { .compatible = "goodix,gt967" },
1525     { }
1526 };
1527 MODULE_DEVICE_TABLE(of, goodix_of_match);
1528 #endif
1529 
1530 static struct i2c_driver goodix_ts_driver = {
1531     .probe = goodix_ts_probe,
1532     .remove = goodix_ts_remove,
1533     .id_table = goodix_ts_id,
1534     .driver = {
1535         .name = "Goodix-TS",
1536         .acpi_match_table = ACPI_PTR(goodix_acpi_match),
1537         .of_match_table = of_match_ptr(goodix_of_match),
1538         .pm = &goodix_pm_ops,
1539     },
1540 };
1541 module_i2c_driver(goodix_ts_driver);
1542 
1543 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1544 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1545 MODULE_DESCRIPTION("Goodix touchscreen driver");
1546 MODULE_LICENSE("GPL v2");