GNU Linux-libre 5.10.217-gnu1
[releases.git] / drivers / input / touchscreen / goodix.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Driver for Goodix Touchscreens
4  *
5  *  Copyright (c) 2014 Red Hat Inc.
6  *  Copyright (c) 2015 K. Merker <merker@debian.org>
7  *
8  *  This code is based on gt9xx.c authored by andrew@goodix.com:
9  *
10  *  2010 - 2012 Goodix Technology.
11  */
12
13
14 #include <linux/kernel.h>
15 #include <linux/dmi.h>
16 #include <linux/firmware.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/acpi.h>
29 #include <linux/of.h>
30 #include <asm/unaligned.h>
31
32 #define GOODIX_GPIO_INT_NAME            "irq"
33 #define GOODIX_GPIO_RST_NAME            "reset"
34
35 #define GOODIX_MAX_HEIGHT               4096
36 #define GOODIX_MAX_WIDTH                4096
37 #define GOODIX_INT_TRIGGER              1
38 #define GOODIX_CONTACT_SIZE             8
39 #define GOODIX_MAX_CONTACT_SIZE         9
40 #define GOODIX_MAX_CONTACTS             10
41 #define GOODIX_MAX_KEYS                 7
42
43 #define GOODIX_CONFIG_MIN_LENGTH        186
44 #define GOODIX_CONFIG_911_LENGTH        186
45 #define GOODIX_CONFIG_967_LENGTH        228
46 #define GOODIX_CONFIG_GT9X_LENGTH       240
47 #define GOODIX_CONFIG_MAX_LENGTH        240
48
49 /* Register defines */
50 #define GOODIX_REG_COMMAND              0x8040
51 #define GOODIX_CMD_SCREEN_OFF           0x05
52
53 #define GOODIX_READ_COOR_ADDR           0x814E
54 #define GOODIX_GT1X_REG_CONFIG_DATA     0x8050
55 #define GOODIX_GT9X_REG_CONFIG_DATA     0x8047
56 #define GOODIX_REG_ID                   0x8140
57
58 #define GOODIX_BUFFER_STATUS_READY      BIT(7)
59 #define GOODIX_HAVE_KEY                 BIT(4)
60 #define GOODIX_BUFFER_STATUS_TIMEOUT    20
61
62 #define RESOLUTION_LOC          1
63 #define MAX_CONTACTS_LOC        5
64 #define TRIGGER_LOC             6
65
66 /* Our special handling for GPIO accesses through ACPI is x86 specific */
67 #if defined CONFIG_X86 && defined CONFIG_ACPI
68 #define ACPI_GPIO_SUPPORT
69 #endif
70
71 struct goodix_ts_data;
72
73 enum goodix_irq_pin_access_method {
74         IRQ_PIN_ACCESS_NONE,
75         IRQ_PIN_ACCESS_GPIO,
76         IRQ_PIN_ACCESS_ACPI_GPIO,
77         IRQ_PIN_ACCESS_ACPI_METHOD,
78 };
79
80 struct goodix_chip_data {
81         u16 config_addr;
82         int config_len;
83         int (*check_config)(struct goodix_ts_data *ts, const u8 *cfg, int len);
84         void (*calc_config_checksum)(struct goodix_ts_data *ts);
85 };
86
87 struct goodix_chip_id {
88         const char *id;
89         const struct goodix_chip_data *data;
90 };
91
92 #define GOODIX_ID_MAX_LEN       4
93
94 struct goodix_ts_data {
95         struct i2c_client *client;
96         struct input_dev *input_dev;
97         const struct goodix_chip_data *chip;
98         struct touchscreen_properties prop;
99         unsigned int max_touch_num;
100         unsigned int int_trigger_type;
101         struct regulator *avdd28;
102         struct regulator *vddio;
103         struct gpio_desc *gpiod_int;
104         struct gpio_desc *gpiod_rst;
105         int gpio_count;
106         int gpio_int_idx;
107         char id[GOODIX_ID_MAX_LEN + 1];
108         u16 version;
109         const char *cfg_name;
110         bool reset_controller_at_probe;
111         bool load_cfg_from_disk;
112         struct completion firmware_loading_complete;
113         unsigned long irq_flags;
114         enum goodix_irq_pin_access_method irq_pin_access_method;
115         unsigned int contact_size;
116         u8 config[GOODIX_CONFIG_MAX_LENGTH];
117         unsigned short keymap[GOODIX_MAX_KEYS];
118 };
119
120 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
121                               const u8 *cfg, int len);
122 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
123                                const u8 *cfg, int len);
124 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
125 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
126
127 static const struct goodix_chip_data gt1x_chip_data = {
128         .config_addr            = GOODIX_GT1X_REG_CONFIG_DATA,
129         .config_len             = GOODIX_CONFIG_GT9X_LENGTH,
130         .check_config           = goodix_check_cfg_16,
131         .calc_config_checksum   = goodix_calc_cfg_checksum_16,
132 };
133
134 static const struct goodix_chip_data gt911_chip_data = {
135         .config_addr            = GOODIX_GT9X_REG_CONFIG_DATA,
136         .config_len             = GOODIX_CONFIG_911_LENGTH,
137         .check_config           = goodix_check_cfg_8,
138         .calc_config_checksum   = goodix_calc_cfg_checksum_8,
139 };
140
141 static const struct goodix_chip_data gt967_chip_data = {
142         .config_addr            = GOODIX_GT9X_REG_CONFIG_DATA,
143         .config_len             = GOODIX_CONFIG_967_LENGTH,
144         .check_config           = goodix_check_cfg_8,
145         .calc_config_checksum   = goodix_calc_cfg_checksum_8,
146 };
147
148 static const struct goodix_chip_data gt9x_chip_data = {
149         .config_addr            = GOODIX_GT9X_REG_CONFIG_DATA,
150         .config_len             = GOODIX_CONFIG_GT9X_LENGTH,
151         .check_config           = goodix_check_cfg_8,
152         .calc_config_checksum   = goodix_calc_cfg_checksum_8,
153 };
154
155 static const struct goodix_chip_id goodix_chip_ids[] = {
156         { .id = "1151", .data = &gt1x_chip_data },
157         { .id = "1158", .data = &gt1x_chip_data },
158         { .id = "5663", .data = &gt1x_chip_data },
159         { .id = "5688", .data = &gt1x_chip_data },
160         { .id = "917S", .data = &gt1x_chip_data },
161         { .id = "9286", .data = &gt1x_chip_data },
162
163         { .id = "911", .data = &gt911_chip_data },
164         { .id = "9271", .data = &gt911_chip_data },
165         { .id = "9110", .data = &gt911_chip_data },
166         { .id = "9111", .data = &gt911_chip_data },
167         { .id = "927", .data = &gt911_chip_data },
168         { .id = "928", .data = &gt911_chip_data },
169
170         { .id = "912", .data = &gt967_chip_data },
171         { .id = "9147", .data = &gt967_chip_data },
172         { .id = "967", .data = &gt967_chip_data },
173         { }
174 };
175
176 static const unsigned long goodix_irq_flags[] = {
177         IRQ_TYPE_EDGE_RISING,
178         IRQ_TYPE_EDGE_FALLING,
179         IRQ_TYPE_LEVEL_LOW,
180         IRQ_TYPE_LEVEL_HIGH,
181 };
182
183 static const struct dmi_system_id nine_bytes_report[] = {
184 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
185         {
186                 /* Lenovo Yoga Book X90F / X90L */
187                 .matches = {
188                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
189                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "CHERRYVIEW D1 PLATFORM"),
190                         DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "YETI-11"),
191                 }
192         },
193         {
194                 /* Lenovo Yoga Book X91F / X91L */
195                 .matches = {
196                         /* Non exact match to match F + L versions */
197                         DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X91"),
198                 }
199         },
200 #endif
201         {}
202 };
203
204 /*
205  * Those tablets have their x coordinate inverted
206  */
207 static const struct dmi_system_id inverted_x_screen[] = {
208 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
209         {
210                 .ident = "Cube I15-TC",
211                 .matches = {
212                         DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
213                         DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
214                 },
215         },
216 #endif
217         {}
218 };
219
220 /**
221  * goodix_i2c_read - read data from a register of the i2c slave device.
222  *
223  * @client: i2c device.
224  * @reg: the register to read from.
225  * @buf: raw write data buffer.
226  * @len: length of the buffer to write
227  */
228 static int goodix_i2c_read(struct i2c_client *client,
229                            u16 reg, u8 *buf, int len)
230 {
231         struct i2c_msg msgs[2];
232         __be16 wbuf = cpu_to_be16(reg);
233         int ret;
234
235         msgs[0].flags = 0;
236         msgs[0].addr  = client->addr;
237         msgs[0].len   = 2;
238         msgs[0].buf   = (u8 *)&wbuf;
239
240         msgs[1].flags = I2C_M_RD;
241         msgs[1].addr  = client->addr;
242         msgs[1].len   = len;
243         msgs[1].buf   = buf;
244
245         ret = i2c_transfer(client->adapter, msgs, 2);
246         return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
247 }
248
249 /**
250  * goodix_i2c_write - write data to a register of the i2c slave device.
251  *
252  * @client: i2c device.
253  * @reg: the register to write to.
254  * @buf: raw data buffer to write.
255  * @len: length of the buffer to write
256  */
257 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
258                             unsigned len)
259 {
260         u8 *addr_buf;
261         struct i2c_msg msg;
262         int ret;
263
264         addr_buf = kmalloc(len + 2, GFP_KERNEL);
265         if (!addr_buf)
266                 return -ENOMEM;
267
268         addr_buf[0] = reg >> 8;
269         addr_buf[1] = reg & 0xFF;
270         memcpy(&addr_buf[2], buf, len);
271
272         msg.flags = 0;
273         msg.addr = client->addr;
274         msg.buf = addr_buf;
275         msg.len = len + 2;
276
277         ret = i2c_transfer(client->adapter, &msg, 1);
278         kfree(addr_buf);
279         return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
280 }
281
282 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
283 {
284         return goodix_i2c_write(client, reg, &value, sizeof(value));
285 }
286
287 static const struct goodix_chip_data *goodix_get_chip_data(const char *id)
288 {
289         unsigned int i;
290
291         for (i = 0; goodix_chip_ids[i].id; i++) {
292                 if (!strcmp(goodix_chip_ids[i].id, id))
293                         return goodix_chip_ids[i].data;
294         }
295
296         return &gt9x_chip_data;
297 }
298
299 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
300 {
301         unsigned long max_timeout;
302         int touch_num;
303         int error;
304         u16 addr = GOODIX_READ_COOR_ADDR;
305         /*
306          * We are going to read 1-byte header,
307          * ts->contact_size * max(1, touch_num) bytes of coordinates
308          * and 1-byte footer which contains the touch-key code.
309          */
310         const int header_contact_keycode_size = 1 + ts->contact_size + 1;
311
312         /*
313          * The 'buffer status' bit, which indicates that the data is valid, is
314          * not set as soon as the interrupt is raised, but slightly after.
315          * This takes around 10 ms to happen, so we poll for 20 ms.
316          */
317         max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
318         do {
319                 error = goodix_i2c_read(ts->client, addr, data,
320                                         header_contact_keycode_size);
321                 if (error) {
322                         dev_err(&ts->client->dev, "I2C transfer error: %d\n",
323                                         error);
324                         return error;
325                 }
326
327                 if (data[0] & GOODIX_BUFFER_STATUS_READY) {
328                         touch_num = data[0] & 0x0f;
329                         if (touch_num > ts->max_touch_num)
330                                 return -EPROTO;
331
332                         if (touch_num > 1) {
333                                 addr += header_contact_keycode_size;
334                                 data += header_contact_keycode_size;
335                                 error = goodix_i2c_read(ts->client,
336                                                 addr, data,
337                                                 ts->contact_size *
338                                                         (touch_num - 1));
339                                 if (error)
340                                         return error;
341                         }
342
343                         return touch_num;
344                 }
345
346                 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
347         } while (time_before(jiffies, max_timeout));
348
349         /*
350          * The Goodix panel will send spurious interrupts after a
351          * 'finger up' event, which will always cause a timeout.
352          */
353         return -ENOMSG;
354 }
355
356 static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
357 {
358         int id = coor_data[0] & 0x0F;
359         int input_x = get_unaligned_le16(&coor_data[1]);
360         int input_y = get_unaligned_le16(&coor_data[3]);
361         int input_w = get_unaligned_le16(&coor_data[5]);
362
363         input_mt_slot(ts->input_dev, id);
364         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
365         touchscreen_report_pos(ts->input_dev, &ts->prop,
366                                input_x, input_y, true);
367         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
368         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
369 }
370
371 static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
372 {
373         int id = coor_data[1] & 0x0F;
374         int input_x = get_unaligned_le16(&coor_data[3]);
375         int input_y = get_unaligned_le16(&coor_data[5]);
376         int input_w = get_unaligned_le16(&coor_data[7]);
377
378         input_mt_slot(ts->input_dev, id);
379         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
380         touchscreen_report_pos(ts->input_dev, &ts->prop,
381                                input_x, input_y, true);
382         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
383         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
384 }
385
386 static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
387 {
388         int touch_num;
389         u8 key_value;
390         int i;
391
392         if (data[0] & GOODIX_HAVE_KEY) {
393                 touch_num = data[0] & 0x0f;
394                 key_value = data[1 + ts->contact_size * touch_num];
395                 for (i = 0; i < GOODIX_MAX_KEYS; i++)
396                         if (key_value & BIT(i))
397                                 input_report_key(ts->input_dev,
398                                                  ts->keymap[i], 1);
399         } else {
400                 for (i = 0; i < GOODIX_MAX_KEYS; i++)
401                         input_report_key(ts->input_dev, ts->keymap[i], 0);
402         }
403 }
404
405 /**
406  * goodix_process_events - Process incoming events
407  *
408  * @ts: our goodix_ts_data pointer
409  *
410  * Called when the IRQ is triggered. Read the current device state, and push
411  * the input events to the user space.
412  */
413 static void goodix_process_events(struct goodix_ts_data *ts)
414 {
415         u8  point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
416         int touch_num;
417         int i;
418
419         touch_num = goodix_ts_read_input_report(ts, point_data);
420         if (touch_num < 0)
421                 return;
422
423         goodix_ts_report_key(ts, point_data);
424
425         for (i = 0; i < touch_num; i++)
426                 if (ts->contact_size == 9)
427                         goodix_ts_report_touch_9b(ts,
428                                 &point_data[1 + ts->contact_size * i]);
429                 else
430                         goodix_ts_report_touch_8b(ts,
431                                 &point_data[1 + ts->contact_size * i]);
432
433         input_mt_sync_frame(ts->input_dev);
434         input_sync(ts->input_dev);
435 }
436
437 /**
438  * goodix_ts_irq_handler - The IRQ handler
439  *
440  * @irq: interrupt number.
441  * @dev_id: private data pointer.
442  */
443 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
444 {
445         struct goodix_ts_data *ts = dev_id;
446
447         goodix_process_events(ts);
448
449         if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
450                 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
451
452         return IRQ_HANDLED;
453 }
454
455 static void goodix_free_irq(struct goodix_ts_data *ts)
456 {
457         devm_free_irq(&ts->client->dev, ts->client->irq, ts);
458 }
459
460 static int goodix_request_irq(struct goodix_ts_data *ts)
461 {
462         return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
463                                          NULL, goodix_ts_irq_handler,
464                                          ts->irq_flags, ts->client->name, ts);
465 }
466
467 static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
468 {
469         int i, raw_cfg_len = len - 2;
470         u8 check_sum = 0;
471
472         for (i = 0; i < raw_cfg_len; i++)
473                 check_sum += cfg[i];
474         check_sum = (~check_sum) + 1;
475         if (check_sum != cfg[raw_cfg_len]) {
476                 dev_err(&ts->client->dev,
477                         "The checksum of the config fw is not correct");
478                 return -EINVAL;
479         }
480
481         if (cfg[raw_cfg_len + 1] != 1) {
482                 dev_err(&ts->client->dev,
483                         "Config fw must have Config_Fresh register set");
484                 return -EINVAL;
485         }
486
487         return 0;
488 }
489
490 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
491 {
492         int i, raw_cfg_len = ts->chip->config_len - 2;
493         u8 check_sum = 0;
494
495         for (i = 0; i < raw_cfg_len; i++)
496                 check_sum += ts->config[i];
497         check_sum = (~check_sum) + 1;
498
499         ts->config[raw_cfg_len] = check_sum;
500         ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
501 }
502
503 static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
504                                int len)
505 {
506         int i, raw_cfg_len = len - 3;
507         u16 check_sum = 0;
508
509         for (i = 0; i < raw_cfg_len; i += 2)
510                 check_sum += get_unaligned_be16(&cfg[i]);
511         check_sum = (~check_sum) + 1;
512         if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
513                 dev_err(&ts->client->dev,
514                         "The checksum of the config fw is not correct");
515                 return -EINVAL;
516         }
517
518         if (cfg[raw_cfg_len + 2] != 1) {
519                 dev_err(&ts->client->dev,
520                         "Config fw must have Config_Fresh register set");
521                 return -EINVAL;
522         }
523
524         return 0;
525 }
526
527 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
528 {
529         int i, raw_cfg_len = ts->chip->config_len - 3;
530         u16 check_sum = 0;
531
532         for (i = 0; i < raw_cfg_len; i += 2)
533                 check_sum += get_unaligned_be16(&ts->config[i]);
534         check_sum = (~check_sum) + 1;
535
536         put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
537         ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
538 }
539
540 /**
541  * goodix_check_cfg - Checks if config fw is valid
542  *
543  * @ts: goodix_ts_data pointer
544  * @cfg: firmware config data
545  */
546 static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
547 {
548         if (len < GOODIX_CONFIG_MIN_LENGTH ||
549             len > GOODIX_CONFIG_MAX_LENGTH) {
550                 dev_err(&ts->client->dev,
551                         "The length of the config fw is not correct");
552                 return -EINVAL;
553         }
554
555         return ts->chip->check_config(ts, cfg, len);
556 }
557
558 /**
559  * goodix_send_cfg - Write fw config to device
560  *
561  * @ts: goodix_ts_data pointer
562  * @cfg: config firmware to write to device
563  */
564 static int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
565 {
566         int error;
567
568         error = goodix_check_cfg(ts, cfg, len);
569         if (error)
570                 return error;
571
572         error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
573         if (error) {
574                 dev_err(&ts->client->dev, "Failed to write config data: %d",
575                         error);
576                 return error;
577         }
578         dev_dbg(&ts->client->dev, "Config sent successfully.");
579
580         /* Let the firmware reconfigure itself, so sleep for 10ms */
581         usleep_range(10000, 11000);
582
583         return 0;
584 }
585
586 #ifdef ACPI_GPIO_SUPPORT
587 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
588 {
589         acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
590         acpi_status status;
591
592         status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
593         return ACPI_SUCCESS(status) ? 0 : -EIO;
594 }
595
596 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
597 {
598         acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
599         acpi_status status;
600
601         status = acpi_execute_simple_method(handle, "INTO", value);
602         return ACPI_SUCCESS(status) ? 0 : -EIO;
603 }
604 #else
605 static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
606 {
607         dev_err(&ts->client->dev,
608                 "%s called on device without ACPI support\n", __func__);
609         return -EINVAL;
610 }
611
612 static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
613 {
614         dev_err(&ts->client->dev,
615                 "%s called on device without ACPI support\n", __func__);
616         return -EINVAL;
617 }
618 #endif
619
620 static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
621 {
622         switch (ts->irq_pin_access_method) {
623         case IRQ_PIN_ACCESS_NONE:
624                 dev_err(&ts->client->dev,
625                         "%s called without an irq_pin_access_method set\n",
626                         __func__);
627                 return -EINVAL;
628         case IRQ_PIN_ACCESS_GPIO:
629                 return gpiod_direction_output(ts->gpiod_int, value);
630         case IRQ_PIN_ACCESS_ACPI_GPIO:
631                 /*
632                  * The IRQ pin triggers on a falling edge, so its gets marked
633                  * as active-low, use output_raw to avoid the value inversion.
634                  */
635                 return gpiod_direction_output_raw(ts->gpiod_int, value);
636         case IRQ_PIN_ACCESS_ACPI_METHOD:
637                 return goodix_pin_acpi_output_method(ts, value);
638         }
639
640         return -EINVAL; /* Never reached */
641 }
642
643 static int goodix_irq_direction_input(struct goodix_ts_data *ts)
644 {
645         switch (ts->irq_pin_access_method) {
646         case IRQ_PIN_ACCESS_NONE:
647                 dev_err(&ts->client->dev,
648                         "%s called without an irq_pin_access_method set\n",
649                         __func__);
650                 return -EINVAL;
651         case IRQ_PIN_ACCESS_GPIO:
652                 return gpiod_direction_input(ts->gpiod_int);
653         case IRQ_PIN_ACCESS_ACPI_GPIO:
654                 return gpiod_direction_input(ts->gpiod_int);
655         case IRQ_PIN_ACCESS_ACPI_METHOD:
656                 return goodix_pin_acpi_direction_input(ts);
657         }
658
659         return -EINVAL; /* Never reached */
660 }
661
662 static int goodix_int_sync(struct goodix_ts_data *ts)
663 {
664         int error;
665
666         error = goodix_irq_direction_output(ts, 0);
667         if (error)
668                 return error;
669
670         msleep(50);                             /* T5: 50ms */
671
672         error = goodix_irq_direction_input(ts);
673         if (error)
674                 return error;
675
676         return 0;
677 }
678
679 /**
680  * goodix_reset - Reset device during power on
681  *
682  * @ts: goodix_ts_data pointer
683  */
684 static int goodix_reset(struct goodix_ts_data *ts)
685 {
686         int error;
687
688         /* begin select I2C slave addr */
689         error = gpiod_direction_output(ts->gpiod_rst, 0);
690         if (error)
691                 return error;
692
693         msleep(20);                             /* T2: > 10ms */
694
695         /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
696         error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
697         if (error)
698                 return error;
699
700         usleep_range(100, 2000);                /* T3: > 100us */
701
702         error = gpiod_direction_output(ts->gpiod_rst, 1);
703         if (error)
704                 return error;
705
706         usleep_range(6000, 10000);              /* T4: > 5ms */
707
708         /* end select I2C slave addr */
709         error = gpiod_direction_input(ts->gpiod_rst);
710         if (error)
711                 return error;
712
713         error = goodix_int_sync(ts);
714         if (error)
715                 return error;
716
717         return 0;
718 }
719
720 #ifdef ACPI_GPIO_SUPPORT
721 #include <asm/cpu_device_id.h>
722 #include <asm/intel-family.h>
723
724 static const struct x86_cpu_id baytrail_cpu_ids[] = {
725         { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT, X86_FEATURE_ANY, },
726         {}
727 };
728
729 static inline bool is_byt(void)
730 {
731         const struct x86_cpu_id *id = x86_match_cpu(baytrail_cpu_ids);
732
733         return !!id;
734 }
735
736 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
737 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
738
739 static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
740         { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
741         { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
742         { },
743 };
744
745 static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
746         { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
747         { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
748         { },
749 };
750
751 static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
752         { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
753         { },
754 };
755
756 static int goodix_resource(struct acpi_resource *ares, void *data)
757 {
758         struct goodix_ts_data *ts = data;
759         struct device *dev = &ts->client->dev;
760         struct acpi_resource_gpio *gpio;
761
762         switch (ares->type) {
763         case ACPI_RESOURCE_TYPE_GPIO:
764                 gpio = &ares->data.gpio;
765                 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
766                         if (ts->gpio_int_idx == -1) {
767                                 ts->gpio_int_idx = ts->gpio_count;
768                         } else {
769                                 dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
770                                 ts->gpio_int_idx = -2;
771                         }
772                 }
773                 ts->gpio_count++;
774                 break;
775         default:
776                 break;
777         }
778
779         return 0;
780 }
781
782 /*
783  * This function gets called in case we fail to get the irq GPIO directly
784  * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
785  * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
786  * In that case we add our own mapping and then goodix_get_gpio_config()
787  * retries to get the GPIOs based on the added mapping.
788  */
789 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
790 {
791         const struct acpi_gpio_mapping *gpio_mapping = NULL;
792         struct device *dev = &ts->client->dev;
793         LIST_HEAD(resources);
794         int ret;
795
796         ts->gpio_count = 0;
797         ts->gpio_int_idx = -1;
798         ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
799                                      goodix_resource, ts);
800         if (ret < 0) {
801                 dev_err(dev, "Error getting ACPI resources: %d\n", ret);
802                 return ret;
803         }
804
805         acpi_dev_free_resource_list(&resources);
806
807         if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) {
808                 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
809                 gpio_mapping = acpi_goodix_int_first_gpios;
810         } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
811                 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
812                 gpio_mapping = acpi_goodix_int_last_gpios;
813         } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
814                    acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
815                    acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
816                 dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
817                 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
818                 gpio_mapping = acpi_goodix_reset_only_gpios;
819         } else if (is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
820                 dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
821                 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
822                 gpio_mapping = acpi_goodix_int_last_gpios;
823         } else if (ts->gpio_count == 1 && ts->gpio_int_idx == 0) {
824                 /*
825                  * On newer devices there is only 1 GpioInt resource and _PS0
826                  * does the whole reset sequence for us.
827                  */
828                 acpi_device_fix_up_power(ACPI_COMPANION(dev));
829
830                 /*
831                  * Before the _PS0 call the int GPIO may have been in output
832                  * mode and the call should have put the int GPIO in input mode,
833                  * but the GPIO subsys cached state may still think it is
834                  * in output mode, causing gpiochip_lock_as_irq() failure.
835                  *
836                  * Add a mapping for the int GPIO to make the
837                  * gpiod_int = gpiod_get(..., GPIOD_IN) call succeed,
838                  * which will explicitly set the direction to input.
839                  */
840                 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
841                 gpio_mapping = acpi_goodix_int_first_gpios;
842         } else {
843                 dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
844                          ts->gpio_count, ts->gpio_int_idx);
845                 return -EINVAL;
846         }
847
848         return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
849 }
850 #else
851 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
852 {
853         return -EINVAL;
854 }
855 #endif /* CONFIG_X86 && CONFIG_ACPI */
856
857 /**
858  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
859  *
860  * @ts: goodix_ts_data pointer
861  */
862 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
863 {
864         int error;
865         struct device *dev;
866         struct gpio_desc *gpiod;
867         bool added_acpi_mappings = false;
868
869         if (!ts->client)
870                 return -EINVAL;
871         dev = &ts->client->dev;
872
873         ts->avdd28 = devm_regulator_get(dev, "AVDD28");
874         if (IS_ERR(ts->avdd28)) {
875                 error = PTR_ERR(ts->avdd28);
876                 if (error != -EPROBE_DEFER)
877                         dev_err(dev,
878                                 "Failed to get AVDD28 regulator: %d\n", error);
879                 return error;
880         }
881
882         ts->vddio = devm_regulator_get(dev, "VDDIO");
883         if (IS_ERR(ts->vddio)) {
884                 error = PTR_ERR(ts->vddio);
885                 if (error != -EPROBE_DEFER)
886                         dev_err(dev,
887                                 "Failed to get VDDIO regulator: %d\n", error);
888                 return error;
889         }
890
891 retry_get_irq_gpio:
892         /* Get the interrupt GPIO pin number */
893         gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
894         if (IS_ERR(gpiod)) {
895                 error = PTR_ERR(gpiod);
896                 if (error != -EPROBE_DEFER)
897                         dev_dbg(dev, "Failed to get %s GPIO: %d\n",
898                                 GOODIX_GPIO_INT_NAME, error);
899                 return error;
900         }
901         if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
902                 added_acpi_mappings = true;
903                 if (goodix_add_acpi_gpio_mappings(ts) == 0)
904                         goto retry_get_irq_gpio;
905         }
906
907         ts->gpiod_int = gpiod;
908
909         /* Get the reset line GPIO pin number */
910         gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
911         if (IS_ERR(gpiod)) {
912                 error = PTR_ERR(gpiod);
913                 if (error != -EPROBE_DEFER)
914                         dev_dbg(dev, "Failed to get %s GPIO: %d\n",
915                                 GOODIX_GPIO_RST_NAME, error);
916                 return error;
917         }
918
919         ts->gpiod_rst = gpiod;
920
921         switch (ts->irq_pin_access_method) {
922         case IRQ_PIN_ACCESS_ACPI_GPIO:
923                 /*
924                  * We end up here if goodix_add_acpi_gpio_mappings() has
925                  * called devm_acpi_dev_add_driver_gpios() because the ACPI
926                  * tables did not contain name to index mappings.
927                  * Check that we successfully got both GPIOs after we've
928                  * added our own acpi_gpio_mapping and if we did not get both
929                  * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
930                  */
931                 if (!ts->gpiod_int || !ts->gpiod_rst)
932                         ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
933                 break;
934         case IRQ_PIN_ACCESS_ACPI_METHOD:
935                 if (!ts->gpiod_rst)
936                         ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
937                 break;
938         default:
939                 if (ts->gpiod_int && ts->gpiod_rst) {
940                         ts->reset_controller_at_probe = true;
941                         ts->load_cfg_from_disk = true;
942                         ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
943                 }
944         }
945
946         return 0;
947 }
948
949 /**
950  * goodix_read_config - Read the embedded configuration of the panel
951  *
952  * @ts: our goodix_ts_data pointer
953  *
954  * Must be called during probe
955  */
956 static void goodix_read_config(struct goodix_ts_data *ts)
957 {
958         int x_max, y_max;
959         int error;
960
961         error = goodix_i2c_read(ts->client, ts->chip->config_addr,
962                                 ts->config, ts->chip->config_len);
963         if (error) {
964                 dev_warn(&ts->client->dev, "Error reading config: %d\n",
965                          error);
966                 ts->int_trigger_type = GOODIX_INT_TRIGGER;
967                 ts->max_touch_num = GOODIX_MAX_CONTACTS;
968                 return;
969         }
970
971         ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
972         ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
973
974         x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
975         y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
976         if (x_max && y_max) {
977                 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
978                 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
979         }
980
981         ts->chip->calc_config_checksum(ts);
982 }
983
984 /**
985  * goodix_read_version - Read goodix touchscreen version
986  *
987  * @ts: our goodix_ts_data pointer
988  */
989 static int goodix_read_version(struct goodix_ts_data *ts)
990 {
991         int error;
992         u8 buf[6];
993         char id_str[GOODIX_ID_MAX_LEN + 1];
994
995         error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
996         if (error) {
997                 dev_err(&ts->client->dev, "read version failed: %d\n", error);
998                 return error;
999         }
1000
1001         memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
1002         id_str[GOODIX_ID_MAX_LEN] = 0;
1003         strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
1004
1005         ts->version = get_unaligned_le16(&buf[4]);
1006
1007         dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
1008                  ts->version);
1009
1010         return 0;
1011 }
1012
1013 /**
1014  * goodix_i2c_test - I2C test function to check if the device answers.
1015  *
1016  * @client: the i2c client
1017  */
1018 static int goodix_i2c_test(struct i2c_client *client)
1019 {
1020         int retry = 0;
1021         int error;
1022         u8 test;
1023
1024         while (retry++ < 2) {
1025                 error = goodix_i2c_read(client, GOODIX_REG_ID,
1026                                         &test, 1);
1027                 if (!error)
1028                         return 0;
1029
1030                 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
1031                         retry, error);
1032                 msleep(20);
1033         }
1034
1035         return error;
1036 }
1037
1038 /**
1039  * goodix_configure_dev - Finish device initialization
1040  *
1041  * @ts: our goodix_ts_data pointer
1042  *
1043  * Must be called from probe to finish initialization of the device.
1044  * Contains the common initialization code for both devices that
1045  * declare gpio pins and devices that do not. It is either called
1046  * directly from probe or from request_firmware_wait callback.
1047  */
1048 static int goodix_configure_dev(struct goodix_ts_data *ts)
1049 {
1050         int error;
1051         int i;
1052
1053         ts->int_trigger_type = GOODIX_INT_TRIGGER;
1054         ts->max_touch_num = GOODIX_MAX_CONTACTS;
1055
1056         ts->input_dev = devm_input_allocate_device(&ts->client->dev);
1057         if (!ts->input_dev) {
1058                 dev_err(&ts->client->dev, "Failed to allocate input device.");
1059                 return -ENOMEM;
1060         }
1061
1062         ts->input_dev->name = "Goodix Capacitive TouchScreen";
1063         ts->input_dev->phys = "input/ts";
1064         ts->input_dev->id.bustype = BUS_I2C;
1065         ts->input_dev->id.vendor = 0x0416;
1066         if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
1067                 ts->input_dev->id.product = 0x1001;
1068         ts->input_dev->id.version = ts->version;
1069
1070         ts->input_dev->keycode = ts->keymap;
1071         ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
1072         ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
1073
1074         /* Capacitive Windows/Home button on some devices */
1075         for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
1076                 if (i == 0)
1077                         ts->keymap[i] = KEY_LEFTMETA;
1078                 else
1079                         ts->keymap[i] = KEY_F1 + (i - 1);
1080
1081                 input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
1082         }
1083
1084         input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
1085         input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
1086         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
1087         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1088
1089 retry_read_config:
1090         /* Read configuration and apply touchscreen parameters */
1091         goodix_read_config(ts);
1092
1093         /* Try overriding touchscreen parameters via device properties */
1094         touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
1095
1096         if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
1097                 if (!ts->reset_controller_at_probe &&
1098                     ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1099                         dev_info(&ts->client->dev, "Config not set, resetting controller\n");
1100                         /* Retry after a controller reset */
1101                         ts->reset_controller_at_probe = true;
1102                         error = goodix_reset(ts);
1103                         if (error)
1104                                 return error;
1105                         goto retry_read_config;
1106                 }
1107                 dev_err(&ts->client->dev,
1108                         "Invalid config (%d, %d, %d), using defaults\n",
1109                         ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
1110                 ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
1111                 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
1112                 ts->max_touch_num = GOODIX_MAX_CONTACTS;
1113                 input_abs_set_max(ts->input_dev,
1114                                   ABS_MT_POSITION_X, ts->prop.max_x);
1115                 input_abs_set_max(ts->input_dev,
1116                                   ABS_MT_POSITION_Y, ts->prop.max_y);
1117         }
1118
1119         if (dmi_check_system(nine_bytes_report)) {
1120                 ts->contact_size = 9;
1121
1122                 dev_dbg(&ts->client->dev,
1123                         "Non-standard 9-bytes report format quirk\n");
1124         }
1125
1126         if (dmi_check_system(inverted_x_screen)) {
1127                 ts->prop.invert_x = true;
1128                 dev_dbg(&ts->client->dev,
1129                         "Applying 'inverted x screen' quirk\n");
1130         }
1131
1132         error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
1133                                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1134         if (error) {
1135                 dev_err(&ts->client->dev,
1136                         "Failed to initialize MT slots: %d", error);
1137                 return error;
1138         }
1139
1140         error = input_register_device(ts->input_dev);
1141         if (error) {
1142                 dev_err(&ts->client->dev,
1143                         "Failed to register input device: %d", error);
1144                 return error;
1145         }
1146
1147         ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
1148         error = goodix_request_irq(ts);
1149         if (error) {
1150                 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
1151                 return error;
1152         }
1153
1154         return 0;
1155 }
1156
1157 /**
1158  * goodix_config_cb - Callback to finish device init
1159  *
1160  * @ts: our goodix_ts_data pointer
1161  *
1162  * request_firmware_wait callback that finishes
1163  * initialization of the device.
1164  */
1165 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
1166 {
1167         struct goodix_ts_data *ts = ctx;
1168         int error;
1169
1170         if (cfg) {
1171                 /* send device configuration to the firmware */
1172                 error = goodix_send_cfg(ts, cfg->data, cfg->size);
1173                 if (error)
1174                         goto err_release_cfg;
1175         }
1176
1177         goodix_configure_dev(ts);
1178
1179 err_release_cfg:
1180         release_firmware(cfg);
1181         complete_all(&ts->firmware_loading_complete);
1182 }
1183
1184 static void goodix_disable_regulators(void *arg)
1185 {
1186         struct goodix_ts_data *ts = arg;
1187
1188         regulator_disable(ts->vddio);
1189         regulator_disable(ts->avdd28);
1190 }
1191
1192 static int goodix_ts_probe(struct i2c_client *client,
1193                            const struct i2c_device_id *id)
1194 {
1195         struct goodix_ts_data *ts;
1196         int error;
1197
1198         dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
1199
1200         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1201                 dev_err(&client->dev, "I2C check functionality failed.\n");
1202                 return -ENXIO;
1203         }
1204
1205         ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1206         if (!ts)
1207                 return -ENOMEM;
1208
1209         ts->client = client;
1210         i2c_set_clientdata(client, ts);
1211         init_completion(&ts->firmware_loading_complete);
1212         ts->contact_size = GOODIX_CONTACT_SIZE;
1213
1214         error = goodix_get_gpio_config(ts);
1215         if (error)
1216                 return error;
1217
1218         /* power up the controller */
1219         error = regulator_enable(ts->avdd28);
1220         if (error) {
1221                 dev_err(&client->dev,
1222                         "Failed to enable AVDD28 regulator: %d\n",
1223                         error);
1224                 return error;
1225         }
1226
1227         error = regulator_enable(ts->vddio);
1228         if (error) {
1229                 dev_err(&client->dev,
1230                         "Failed to enable VDDIO regulator: %d\n",
1231                         error);
1232                 regulator_disable(ts->avdd28);
1233                 return error;
1234         }
1235
1236         error = devm_add_action_or_reset(&client->dev,
1237                                          goodix_disable_regulators, ts);
1238         if (error)
1239                 return error;
1240
1241 reset:
1242         if (ts->reset_controller_at_probe) {
1243                 /* reset the controller */
1244                 error = goodix_reset(ts);
1245                 if (error) {
1246                         dev_err(&client->dev, "Controller reset failed.\n");
1247                         return error;
1248                 }
1249         }
1250
1251         error = goodix_i2c_test(client);
1252         if (error) {
1253                 if (!ts->reset_controller_at_probe &&
1254                     ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1255                         /* Retry after a controller reset */
1256                         ts->reset_controller_at_probe = true;
1257                         goto reset;
1258                 }
1259                 dev_err(&client->dev, "I2C communication failure: %d\n", error);
1260                 return error;
1261         }
1262
1263         error = goodix_read_version(ts);
1264         if (error) {
1265                 dev_err(&client->dev, "Read version failed.\n");
1266                 return error;
1267         }
1268
1269         ts->chip = goodix_get_chip_data(ts->id);
1270
1271         if (ts->load_cfg_from_disk) {
1272                 /* update device config */
1273                 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
1274                                               "/*(DEBLOBBED)*/", ts->id);
1275                 if (!ts->cfg_name)
1276                         return -ENOMEM;
1277
1278                 error = reject_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
1279                                                 &client->dev, GFP_KERNEL, ts,
1280                                                 goodix_config_cb);
1281                 if (error) {
1282                         dev_err(&client->dev,
1283                                 "Failed to invoke firmware loader: %d\n",
1284                                 error);
1285                         return error;
1286                 }
1287
1288                 return 0;
1289         } else {
1290                 error = goodix_configure_dev(ts);
1291                 if (error)
1292                         return error;
1293         }
1294
1295         return 0;
1296 }
1297
1298 static int goodix_ts_remove(struct i2c_client *client)
1299 {
1300         struct goodix_ts_data *ts = i2c_get_clientdata(client);
1301
1302         if (ts->load_cfg_from_disk)
1303                 wait_for_completion(&ts->firmware_loading_complete);
1304
1305         return 0;
1306 }
1307
1308 static int __maybe_unused goodix_suspend(struct device *dev)
1309 {
1310         struct i2c_client *client = to_i2c_client(dev);
1311         struct goodix_ts_data *ts = i2c_get_clientdata(client);
1312         int error;
1313
1314         if (ts->load_cfg_from_disk)
1315                 wait_for_completion(&ts->firmware_loading_complete);
1316
1317         /* We need gpio pins to suspend/resume */
1318         if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1319                 disable_irq(client->irq);
1320                 return 0;
1321         }
1322
1323         /* Free IRQ as IRQ pin is used as output in the suspend sequence */
1324         goodix_free_irq(ts);
1325
1326         /* Output LOW on the INT pin for 5 ms */
1327         error = goodix_irq_direction_output(ts, 0);
1328         if (error) {
1329                 goodix_request_irq(ts);
1330                 return error;
1331         }
1332
1333         usleep_range(5000, 6000);
1334
1335         error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
1336                                     GOODIX_CMD_SCREEN_OFF);
1337         if (error) {
1338                 dev_err(&ts->client->dev, "Screen off command failed\n");
1339                 goodix_irq_direction_input(ts);
1340                 goodix_request_irq(ts);
1341                 return -EAGAIN;
1342         }
1343
1344         /*
1345          * The datasheet specifies that the interval between sending screen-off
1346          * command and wake-up should be longer than 58 ms. To avoid waking up
1347          * sooner, delay 58ms here.
1348          */
1349         msleep(58);
1350         return 0;
1351 }
1352
1353 static int __maybe_unused goodix_resume(struct device *dev)
1354 {
1355         struct i2c_client *client = to_i2c_client(dev);
1356         struct goodix_ts_data *ts = i2c_get_clientdata(client);
1357         u8 config_ver;
1358         int error;
1359
1360         if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1361                 enable_irq(client->irq);
1362                 return 0;
1363         }
1364
1365         /*
1366          * Exit sleep mode by outputting HIGH level to INT pin
1367          * for 2ms~5ms.
1368          */
1369         error = goodix_irq_direction_output(ts, 1);
1370         if (error)
1371                 return error;
1372
1373         usleep_range(2000, 5000);
1374
1375         error = goodix_int_sync(ts);
1376         if (error)
1377                 return error;
1378
1379         error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1380                                 &config_ver, 1);
1381         if (error)
1382                 dev_warn(dev, "Error reading config version: %d, resetting controller\n",
1383                          error);
1384         else if (config_ver != ts->config[0])
1385                 dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
1386                          config_ver, ts->config[0]);
1387
1388         if (error != 0 || config_ver != ts->config[0]) {
1389                 error = goodix_reset(ts);
1390                 if (error) {
1391                         dev_err(dev, "Controller reset failed.\n");
1392                         return error;
1393                 }
1394
1395                 error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
1396                 if (error)
1397                         return error;
1398         }
1399
1400         error = goodix_request_irq(ts);
1401         if (error)
1402                 return error;
1403
1404         return 0;
1405 }
1406
1407 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1408
1409 static const struct i2c_device_id goodix_ts_id[] = {
1410         { "GDIX1001:00", 0 },
1411         { }
1412 };
1413 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1414
1415 #ifdef CONFIG_ACPI
1416 static const struct acpi_device_id goodix_acpi_match[] = {
1417         { "GDIX1001", 0 },
1418         { "GDIX1002", 0 },
1419         { }
1420 };
1421 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1422 #endif
1423
1424 #ifdef CONFIG_OF
1425 static const struct of_device_id goodix_of_match[] = {
1426         { .compatible = "goodix,gt1151" },
1427         { .compatible = "goodix,gt1158" },
1428         { .compatible = "goodix,gt5663" },
1429         { .compatible = "goodix,gt5688" },
1430         { .compatible = "goodix,gt911" },
1431         { .compatible = "goodix,gt9110" },
1432         { .compatible = "goodix,gt912" },
1433         { .compatible = "goodix,gt9147" },
1434         { .compatible = "goodix,gt917s" },
1435         { .compatible = "goodix,gt927" },
1436         { .compatible = "goodix,gt9271" },
1437         { .compatible = "goodix,gt928" },
1438         { .compatible = "goodix,gt9286" },
1439         { .compatible = "goodix,gt967" },
1440         { }
1441 };
1442 MODULE_DEVICE_TABLE(of, goodix_of_match);
1443 #endif
1444
1445 static struct i2c_driver goodix_ts_driver = {
1446         .probe = goodix_ts_probe,
1447         .remove = goodix_ts_remove,
1448         .id_table = goodix_ts_id,
1449         .driver = {
1450                 .name = "Goodix-TS",
1451                 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
1452                 .of_match_table = of_match_ptr(goodix_of_match),
1453                 .pm = &goodix_pm_ops,
1454         },
1455 };
1456 module_i2c_driver(goodix_ts_driver);
1457
1458 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1459 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1460 MODULE_DESCRIPTION("Goodix touchscreen driver");
1461 MODULE_LICENSE("GPL v2");