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