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