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