GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / input / touchscreen / goodix.c
1 /*
2  *  Driver for Goodix Touchscreens
3  *
4  *  Copyright (c) 2014 Red Hat Inc.
5  *  Copyright (c) 2015 K. Merker <merker@debian.org>
6  *
7  *  This code is based on gt9xx.c authored by andrew@goodix.com:
8  *
9  *  2010 - 2012 Goodix Technology.
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; version 2 of the License.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/dmi.h>
20 #include <linux/firmware.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/i2c.h>
23 #include <linux/input.h>
24 #include <linux/input/mt.h>
25 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/irq.h>
28 #include <linux/interrupt.h>
29 #include <linux/slab.h>
30 #include <linux/acpi.h>
31 #include <linux/of.h>
32 #include <asm/unaligned.h>
33
34 struct goodix_ts_data {
35         struct i2c_client *client;
36         struct input_dev *input_dev;
37         int abs_x_max;
38         int abs_y_max;
39         bool swapped_x_y;
40         bool inverted_x;
41         bool inverted_y;
42         unsigned int max_touch_num;
43         unsigned int int_trigger_type;
44         int cfg_len;
45         struct gpio_desc *gpiod_int;
46         struct gpio_desc *gpiod_rst;
47         u16 id;
48         u16 version;
49         const char *cfg_name;
50         struct completion firmware_loading_complete;
51         unsigned long irq_flags;
52 };
53
54 #define GOODIX_GPIO_INT_NAME            "irq"
55 #define GOODIX_GPIO_RST_NAME            "reset"
56
57 #define GOODIX_MAX_HEIGHT               4096
58 #define GOODIX_MAX_WIDTH                4096
59 #define GOODIX_INT_TRIGGER              1
60 #define GOODIX_CONTACT_SIZE             8
61 #define GOODIX_MAX_CONTACTS             10
62
63 #define GOODIX_CONFIG_MAX_LENGTH        240
64 #define GOODIX_CONFIG_911_LENGTH        186
65 #define GOODIX_CONFIG_967_LENGTH        228
66
67 /* Register defines */
68 #define GOODIX_REG_COMMAND              0x8040
69 #define GOODIX_CMD_SCREEN_OFF           0x05
70
71 #define GOODIX_READ_COOR_ADDR           0x814E
72 #define GOODIX_REG_CONFIG_DATA          0x8047
73 #define GOODIX_REG_ID                   0x8140
74
75 #define GOODIX_BUFFER_STATUS_READY      BIT(7)
76 #define GOODIX_BUFFER_STATUS_TIMEOUT    20
77
78 #define RESOLUTION_LOC          1
79 #define MAX_CONTACTS_LOC        5
80 #define TRIGGER_LOC             6
81
82 static const unsigned long goodix_irq_flags[] = {
83         IRQ_TYPE_EDGE_RISING,
84         IRQ_TYPE_EDGE_FALLING,
85         IRQ_TYPE_LEVEL_LOW,
86         IRQ_TYPE_LEVEL_HIGH,
87 };
88
89 /*
90  * Those tablets have their coordinates origin at the bottom right
91  * of the tablet, as if rotated 180 degrees
92  */
93 static const struct dmi_system_id rotated_screen[] = {
94 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
95         {
96                 .ident = "Teclast X89",
97                 .matches = {
98                         /* tPAD is too generic, also match on bios date */
99                         DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
100                         DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
101                         DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"),
102                 },
103         },
104         {
105                 .ident = "Teclast X98 Pro",
106                 .matches = {
107                         /*
108                          * Only match BIOS date, because the manufacturers
109                          * BIOS does not report the board name at all
110                          * (sometimes)...
111                          */
112                         DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
113                         DMI_MATCH(DMI_BIOS_DATE, "10/28/2015"),
114                 },
115         },
116         {
117                 .ident = "WinBook TW100",
118                 .matches = {
119                         DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
120                         DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
121                 }
122         },
123         {
124                 .ident = "WinBook TW700",
125                 .matches = {
126                         DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
127                         DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
128                 },
129         },
130 #endif
131         {}
132 };
133
134 /**
135  * goodix_i2c_read - read data from a register of the i2c slave device.
136  *
137  * @client: i2c device.
138  * @reg: the register to read from.
139  * @buf: raw write data buffer.
140  * @len: length of the buffer to write
141  */
142 static int goodix_i2c_read(struct i2c_client *client,
143                            u16 reg, u8 *buf, int len)
144 {
145         struct i2c_msg msgs[2];
146         u16 wbuf = cpu_to_be16(reg);
147         int ret;
148
149         msgs[0].flags = 0;
150         msgs[0].addr  = client->addr;
151         msgs[0].len   = 2;
152         msgs[0].buf   = (u8 *)&wbuf;
153
154         msgs[1].flags = I2C_M_RD;
155         msgs[1].addr  = client->addr;
156         msgs[1].len   = len;
157         msgs[1].buf   = buf;
158
159         ret = i2c_transfer(client->adapter, msgs, 2);
160         return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
161 }
162
163 /**
164  * goodix_i2c_write - write data to a register of the i2c slave device.
165  *
166  * @client: i2c device.
167  * @reg: the register to write to.
168  * @buf: raw data buffer to write.
169  * @len: length of the buffer to write
170  */
171 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
172                             unsigned len)
173 {
174         u8 *addr_buf;
175         struct i2c_msg msg;
176         int ret;
177
178         addr_buf = kmalloc(len + 2, GFP_KERNEL);
179         if (!addr_buf)
180                 return -ENOMEM;
181
182         addr_buf[0] = reg >> 8;
183         addr_buf[1] = reg & 0xFF;
184         memcpy(&addr_buf[2], buf, len);
185
186         msg.flags = 0;
187         msg.addr = client->addr;
188         msg.buf = addr_buf;
189         msg.len = len + 2;
190
191         ret = i2c_transfer(client->adapter, &msg, 1);
192         kfree(addr_buf);
193         return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
194 }
195
196 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
197 {
198         return goodix_i2c_write(client, reg, &value, sizeof(value));
199 }
200
201 static int goodix_get_cfg_len(u16 id)
202 {
203         switch (id) {
204         case 911:
205         case 9271:
206         case 9110:
207         case 927:
208         case 928:
209                 return GOODIX_CONFIG_911_LENGTH;
210
211         case 912:
212         case 967:
213                 return GOODIX_CONFIG_967_LENGTH;
214
215         default:
216                 return GOODIX_CONFIG_MAX_LENGTH;
217         }
218 }
219
220 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
221 {
222         unsigned long max_timeout;
223         int touch_num;
224         int error;
225
226         /*
227          * The 'buffer status' bit, which indicates that the data is valid, is
228          * not set as soon as the interrupt is raised, but slightly after.
229          * This takes around 10 ms to happen, so we poll for 20 ms.
230          */
231         max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
232         do {
233                 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR,
234                                         data, GOODIX_CONTACT_SIZE + 1);
235                 if (error) {
236                         dev_err(&ts->client->dev, "I2C transfer error: %d\n",
237                                         error);
238                         return error;
239                 }
240
241                 if (data[0] & GOODIX_BUFFER_STATUS_READY) {
242                         touch_num = data[0] & 0x0f;
243                         if (touch_num > ts->max_touch_num)
244                                 return -EPROTO;
245
246                         if (touch_num > 1) {
247                                 data += 1 + GOODIX_CONTACT_SIZE;
248                                 error = goodix_i2c_read(ts->client,
249                                                 GOODIX_READ_COOR_ADDR +
250                                                         1 + GOODIX_CONTACT_SIZE,
251                                                 data,
252                                                 GOODIX_CONTACT_SIZE *
253                                                         (touch_num - 1));
254                                 if (error)
255                                         return error;
256                         }
257
258                         return touch_num;
259                 }
260
261                 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
262         } while (time_before(jiffies, max_timeout));
263
264         /*
265          * The Goodix panel will send spurious interrupts after a
266          * 'finger up' event, which will always cause a timeout.
267          */
268         return 0;
269 }
270
271 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
272 {
273         int id = coor_data[0] & 0x0F;
274         int input_x = get_unaligned_le16(&coor_data[1]);
275         int input_y = get_unaligned_le16(&coor_data[3]);
276         int input_w = get_unaligned_le16(&coor_data[5]);
277
278         /* Inversions have to happen before axis swapping */
279         if (ts->inverted_x)
280                 input_x = ts->abs_x_max - input_x;
281         if (ts->inverted_y)
282                 input_y = ts->abs_y_max - input_y;
283         if (ts->swapped_x_y)
284                 swap(input_x, input_y);
285
286         input_mt_slot(ts->input_dev, id);
287         input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
288         input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
289         input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
290         input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
291         input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
292 }
293
294 /**
295  * goodix_process_events - Process incoming events
296  *
297  * @ts: our goodix_ts_data pointer
298  *
299  * Called when the IRQ is triggered. Read the current device state, and push
300  * the input events to the user space.
301  */
302 static void goodix_process_events(struct goodix_ts_data *ts)
303 {
304         u8  point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
305         int touch_num;
306         int i;
307
308         touch_num = goodix_ts_read_input_report(ts, point_data);
309         if (touch_num < 0)
310                 return;
311
312         /*
313          * Bit 4 of the first byte reports the status of the capacitive
314          * Windows/Home button.
315          */
316         input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
317
318         for (i = 0; i < touch_num; i++)
319                 goodix_ts_report_touch(ts,
320                                 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
321
322         input_mt_sync_frame(ts->input_dev);
323         input_sync(ts->input_dev);
324 }
325
326 /**
327  * goodix_ts_irq_handler - The IRQ handler
328  *
329  * @irq: interrupt number.
330  * @dev_id: private data pointer.
331  */
332 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
333 {
334         struct goodix_ts_data *ts = dev_id;
335
336         goodix_process_events(ts);
337
338         if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
339                 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
340
341         return IRQ_HANDLED;
342 }
343
344 static void goodix_free_irq(struct goodix_ts_data *ts)
345 {
346         devm_free_irq(&ts->client->dev, ts->client->irq, ts);
347 }
348
349 static int goodix_request_irq(struct goodix_ts_data *ts)
350 {
351         return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
352                                          NULL, goodix_ts_irq_handler,
353                                          ts->irq_flags, ts->client->name, ts);
354 }
355
356 /**
357  * goodix_check_cfg - Checks if config fw is valid
358  *
359  * @ts: goodix_ts_data pointer
360  * @cfg: firmware config data
361  */
362 static int goodix_check_cfg(struct goodix_ts_data *ts,
363                             const struct firmware *cfg)
364 {
365         int i, raw_cfg_len;
366         u8 check_sum = 0;
367
368         if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
369                 dev_err(&ts->client->dev,
370                         "The length of the config fw is not correct");
371                 return -EINVAL;
372         }
373
374         raw_cfg_len = cfg->size - 2;
375         for (i = 0; i < raw_cfg_len; i++)
376                 check_sum += cfg->data[i];
377         check_sum = (~check_sum) + 1;
378         if (check_sum != cfg->data[raw_cfg_len]) {
379                 dev_err(&ts->client->dev,
380                         "The checksum of the config fw is not correct");
381                 return -EINVAL;
382         }
383
384         if (cfg->data[raw_cfg_len + 1] != 1) {
385                 dev_err(&ts->client->dev,
386                         "Config fw must have Config_Fresh register set");
387                 return -EINVAL;
388         }
389
390         return 0;
391 }
392
393 /**
394  * goodix_send_cfg - Write fw config to device
395  *
396  * @ts: goodix_ts_data pointer
397  * @cfg: config firmware to write to device
398  */
399 static int goodix_send_cfg(struct goodix_ts_data *ts,
400                            const struct firmware *cfg)
401 {
402         int error;
403
404         error = goodix_check_cfg(ts, cfg);
405         if (error)
406                 return error;
407
408         error = goodix_i2c_write(ts->client, GOODIX_REG_CONFIG_DATA, cfg->data,
409                                  cfg->size);
410         if (error) {
411                 dev_err(&ts->client->dev, "Failed to write config data: %d",
412                         error);
413                 return error;
414         }
415         dev_dbg(&ts->client->dev, "Config sent successfully.");
416
417         /* Let the firmware reconfigure itself, so sleep for 10ms */
418         usleep_range(10000, 11000);
419
420         return 0;
421 }
422
423 static int goodix_int_sync(struct goodix_ts_data *ts)
424 {
425         int error;
426
427         error = gpiod_direction_output(ts->gpiod_int, 0);
428         if (error)
429                 return error;
430
431         msleep(50);                             /* T5: 50ms */
432
433         error = gpiod_direction_input(ts->gpiod_int);
434         if (error)
435                 return error;
436
437         return 0;
438 }
439
440 /**
441  * goodix_reset - Reset device during power on
442  *
443  * @ts: goodix_ts_data pointer
444  */
445 static int goodix_reset(struct goodix_ts_data *ts)
446 {
447         int error;
448
449         /* begin select I2C slave addr */
450         error = gpiod_direction_output(ts->gpiod_rst, 0);
451         if (error)
452                 return error;
453
454         msleep(20);                             /* T2: > 10ms */
455
456         /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
457         error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
458         if (error)
459                 return error;
460
461         usleep_range(100, 2000);                /* T3: > 100us */
462
463         error = gpiod_direction_output(ts->gpiod_rst, 1);
464         if (error)
465                 return error;
466
467         usleep_range(6000, 10000);              /* T4: > 5ms */
468
469         /* end select I2C slave addr */
470         error = gpiod_direction_input(ts->gpiod_rst);
471         if (error)
472                 return error;
473
474         error = goodix_int_sync(ts);
475         if (error)
476                 return error;
477
478         return 0;
479 }
480
481 /**
482  * goodix_get_gpio_config - Get GPIO config from ACPI/DT
483  *
484  * @ts: goodix_ts_data pointer
485  */
486 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
487 {
488         int error;
489         struct device *dev;
490         struct gpio_desc *gpiod;
491
492         if (!ts->client)
493                 return -EINVAL;
494         dev = &ts->client->dev;
495
496         /* Get the interrupt GPIO pin number */
497         gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
498         if (IS_ERR(gpiod)) {
499                 error = PTR_ERR(gpiod);
500                 if (error != -EPROBE_DEFER)
501                         dev_dbg(dev, "Failed to get %s GPIO: %d\n",
502                                 GOODIX_GPIO_INT_NAME, error);
503                 return error;
504         }
505
506         ts->gpiod_int = gpiod;
507
508         /* Get the reset line GPIO pin number */
509         gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
510         if (IS_ERR(gpiod)) {
511                 error = PTR_ERR(gpiod);
512                 if (error != -EPROBE_DEFER)
513                         dev_dbg(dev, "Failed to get %s GPIO: %d\n",
514                                 GOODIX_GPIO_RST_NAME, error);
515                 return error;
516         }
517
518         ts->gpiod_rst = gpiod;
519
520         return 0;
521 }
522
523 /**
524  * goodix_read_config - Read the embedded configuration of the panel
525  *
526  * @ts: our goodix_ts_data pointer
527  *
528  * Must be called during probe
529  */
530 static void goodix_read_config(struct goodix_ts_data *ts)
531 {
532         u8 config[GOODIX_CONFIG_MAX_LENGTH];
533         int error;
534
535         error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
536                                 config, ts->cfg_len);
537         if (error) {
538                 dev_warn(&ts->client->dev,
539                          "Error reading config (%d), using defaults\n",
540                          error);
541                 ts->abs_x_max = GOODIX_MAX_WIDTH;
542                 ts->abs_y_max = GOODIX_MAX_HEIGHT;
543                 if (ts->swapped_x_y)
544                         swap(ts->abs_x_max, ts->abs_y_max);
545                 ts->int_trigger_type = GOODIX_INT_TRIGGER;
546                 ts->max_touch_num = GOODIX_MAX_CONTACTS;
547                 return;
548         }
549
550         ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
551         ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
552         if (ts->swapped_x_y)
553                 swap(ts->abs_x_max, ts->abs_y_max);
554         ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
555         ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
556         if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
557                 dev_err(&ts->client->dev,
558                         "Invalid config, using defaults\n");
559                 ts->abs_x_max = GOODIX_MAX_WIDTH;
560                 ts->abs_y_max = GOODIX_MAX_HEIGHT;
561                 if (ts->swapped_x_y)
562                         swap(ts->abs_x_max, ts->abs_y_max);
563                 ts->max_touch_num = GOODIX_MAX_CONTACTS;
564         }
565
566         if (dmi_check_system(rotated_screen)) {
567                 ts->inverted_x = true;
568                 ts->inverted_y = true;
569                 dev_dbg(&ts->client->dev,
570                          "Applying '180 degrees rotated screen' quirk\n");
571         }
572 }
573
574 /**
575  * goodix_read_version - Read goodix touchscreen version
576  *
577  * @ts: our goodix_ts_data pointer
578  */
579 static int goodix_read_version(struct goodix_ts_data *ts)
580 {
581         int error;
582         u8 buf[6];
583         char id_str[5];
584
585         error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
586         if (error) {
587                 dev_err(&ts->client->dev, "read version failed: %d\n", error);
588                 return error;
589         }
590
591         memcpy(id_str, buf, 4);
592         id_str[4] = 0;
593         if (kstrtou16(id_str, 10, &ts->id))
594                 ts->id = 0x1001;
595
596         ts->version = get_unaligned_le16(&buf[4]);
597
598         dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
599                  ts->version);
600
601         return 0;
602 }
603
604 /**
605  * goodix_i2c_test - I2C test function to check if the device answers.
606  *
607  * @client: the i2c client
608  */
609 static int goodix_i2c_test(struct i2c_client *client)
610 {
611         int retry = 0;
612         int error;
613         u8 test;
614
615         while (retry++ < 2) {
616                 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
617                                         &test, 1);
618                 if (!error)
619                         return 0;
620
621                 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
622                         retry, error);
623                 msleep(20);
624         }
625
626         return error;
627 }
628
629 /**
630  * goodix_request_input_dev - Allocate, populate and register the input device
631  *
632  * @ts: our goodix_ts_data pointer
633  *
634  * Must be called during probe
635  */
636 static int goodix_request_input_dev(struct goodix_ts_data *ts)
637 {
638         int error;
639
640         ts->input_dev = devm_input_allocate_device(&ts->client->dev);
641         if (!ts->input_dev) {
642                 dev_err(&ts->client->dev, "Failed to allocate input device.");
643                 return -ENOMEM;
644         }
645
646         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
647                              0, ts->abs_x_max, 0, 0);
648         input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
649                              0, ts->abs_y_max, 0, 0);
650         input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
651         input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
652
653         input_mt_init_slots(ts->input_dev, ts->max_touch_num,
654                             INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
655
656         ts->input_dev->name = "Goodix Capacitive TouchScreen";
657         ts->input_dev->phys = "input/ts";
658         ts->input_dev->id.bustype = BUS_I2C;
659         ts->input_dev->id.vendor = 0x0416;
660         ts->input_dev->id.product = ts->id;
661         ts->input_dev->id.version = ts->version;
662
663         /* Capacitive Windows/Home button on some devices */
664         input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
665
666         error = input_register_device(ts->input_dev);
667         if (error) {
668                 dev_err(&ts->client->dev,
669                         "Failed to register input device: %d", error);
670                 return error;
671         }
672
673         return 0;
674 }
675
676 /**
677  * goodix_configure_dev - Finish device initialization
678  *
679  * @ts: our goodix_ts_data pointer
680  *
681  * Must be called from probe to finish initialization of the device.
682  * Contains the common initialization code for both devices that
683  * declare gpio pins and devices that do not. It is either called
684  * directly from probe or from request_firmware_wait callback.
685  */
686 static int goodix_configure_dev(struct goodix_ts_data *ts)
687 {
688         int error;
689
690         ts->swapped_x_y = device_property_read_bool(&ts->client->dev,
691                                                     "touchscreen-swapped-x-y");
692         ts->inverted_x = device_property_read_bool(&ts->client->dev,
693                                                    "touchscreen-inverted-x");
694         ts->inverted_y = device_property_read_bool(&ts->client->dev,
695                                                    "touchscreen-inverted-y");
696
697         goodix_read_config(ts);
698
699         error = goodix_request_input_dev(ts);
700         if (error)
701                 return error;
702
703         ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
704         error = goodix_request_irq(ts);
705         if (error) {
706                 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
707                 return error;
708         }
709
710         return 0;
711 }
712
713 /**
714  * goodix_config_cb - Callback to finish device init
715  *
716  * @ts: our goodix_ts_data pointer
717  *
718  * request_firmware_wait callback that finishes
719  * initialization of the device.
720  */
721 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
722 {
723         struct goodix_ts_data *ts = ctx;
724         int error;
725
726         if (cfg) {
727                 /* send device configuration to the firmware */
728                 error = goodix_send_cfg(ts, cfg);
729                 if (error)
730                         goto err_release_cfg;
731         }
732
733         goodix_configure_dev(ts);
734
735 err_release_cfg:
736         release_firmware(cfg);
737         complete_all(&ts->firmware_loading_complete);
738 }
739
740 static int goodix_ts_probe(struct i2c_client *client,
741                            const struct i2c_device_id *id)
742 {
743         struct goodix_ts_data *ts;
744         int error;
745
746         dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
747
748         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
749                 dev_err(&client->dev, "I2C check functionality failed.\n");
750                 return -ENXIO;
751         }
752
753         ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
754         if (!ts)
755                 return -ENOMEM;
756
757         ts->client = client;
758         i2c_set_clientdata(client, ts);
759         init_completion(&ts->firmware_loading_complete);
760
761         error = goodix_get_gpio_config(ts);
762         if (error)
763                 return error;
764
765         if (ts->gpiod_int && ts->gpiod_rst) {
766                 /* reset the controller */
767                 error = goodix_reset(ts);
768                 if (error) {
769                         dev_err(&client->dev, "Controller reset failed.\n");
770                         return error;
771                 }
772         }
773
774         error = goodix_i2c_test(client);
775         if (error) {
776                 dev_err(&client->dev, "I2C communication failure: %d\n", error);
777                 return error;
778         }
779
780         error = goodix_read_version(ts);
781         if (error) {
782                 dev_err(&client->dev, "Read version failed.\n");
783                 return error;
784         }
785
786         ts->cfg_len = goodix_get_cfg_len(ts->id);
787
788         if (ts->gpiod_int && ts->gpiod_rst) {
789                 /* update device config */
790                 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
791                                               "/*(DEBLOBBED)*/", ts->id);
792                 if (!ts->cfg_name)
793                         return -ENOMEM;
794
795                 error = reject_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
796                                                 &client->dev, GFP_KERNEL, ts,
797                                                 goodix_config_cb);
798                 if (error) {
799                         dev_err(&client->dev,
800                                 "Failed to invoke firmware loader: %d\n",
801                                 error);
802                         return error;
803                 }
804
805                 return 0;
806         } else {
807                 error = goodix_configure_dev(ts);
808                 if (error)
809                         return error;
810         }
811
812         return 0;
813 }
814
815 static int goodix_ts_remove(struct i2c_client *client)
816 {
817         struct goodix_ts_data *ts = i2c_get_clientdata(client);
818
819         if (ts->gpiod_int && ts->gpiod_rst)
820                 wait_for_completion(&ts->firmware_loading_complete);
821
822         return 0;
823 }
824
825 static int __maybe_unused goodix_suspend(struct device *dev)
826 {
827         struct i2c_client *client = to_i2c_client(dev);
828         struct goodix_ts_data *ts = i2c_get_clientdata(client);
829         int error;
830
831         /* We need gpio pins to suspend/resume */
832         if (!ts->gpiod_int || !ts->gpiod_rst) {
833                 disable_irq(client->irq);
834                 return 0;
835         }
836
837         wait_for_completion(&ts->firmware_loading_complete);
838
839         /* Free IRQ as IRQ pin is used as output in the suspend sequence */
840         goodix_free_irq(ts);
841
842         /* Output LOW on the INT pin for 5 ms */
843         error = gpiod_direction_output(ts->gpiod_int, 0);
844         if (error) {
845                 goodix_request_irq(ts);
846                 return error;
847         }
848
849         usleep_range(5000, 6000);
850
851         error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
852                                     GOODIX_CMD_SCREEN_OFF);
853         if (error) {
854                 dev_err(&ts->client->dev, "Screen off command failed\n");
855                 gpiod_direction_input(ts->gpiod_int);
856                 goodix_request_irq(ts);
857                 return -EAGAIN;
858         }
859
860         /*
861          * The datasheet specifies that the interval between sending screen-off
862          * command and wake-up should be longer than 58 ms. To avoid waking up
863          * sooner, delay 58ms here.
864          */
865         msleep(58);
866         return 0;
867 }
868
869 static int __maybe_unused goodix_resume(struct device *dev)
870 {
871         struct i2c_client *client = to_i2c_client(dev);
872         struct goodix_ts_data *ts = i2c_get_clientdata(client);
873         int error;
874
875         if (!ts->gpiod_int || !ts->gpiod_rst) {
876                 enable_irq(client->irq);
877                 return 0;
878         }
879
880         /*
881          * Exit sleep mode by outputting HIGH level to INT pin
882          * for 2ms~5ms.
883          */
884         error = gpiod_direction_output(ts->gpiod_int, 1);
885         if (error)
886                 return error;
887
888         usleep_range(2000, 5000);
889
890         error = goodix_int_sync(ts);
891         if (error)
892                 return error;
893
894         error = goodix_request_irq(ts);
895         if (error)
896                 return error;
897
898         return 0;
899 }
900
901 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
902
903 static const struct i2c_device_id goodix_ts_id[] = {
904         { "GDIX1001:00", 0 },
905         { }
906 };
907 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
908
909 #ifdef CONFIG_ACPI
910 static const struct acpi_device_id goodix_acpi_match[] = {
911         { "GDIX1001", 0 },
912         { "GDIX1002", 0 },
913         { }
914 };
915 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
916 #endif
917
918 #ifdef CONFIG_OF
919 static const struct of_device_id goodix_of_match[] = {
920         { .compatible = "goodix,gt911" },
921         { .compatible = "goodix,gt9110" },
922         { .compatible = "goodix,gt912" },
923         { .compatible = "goodix,gt927" },
924         { .compatible = "goodix,gt9271" },
925         { .compatible = "goodix,gt928" },
926         { .compatible = "goodix,gt967" },
927         { }
928 };
929 MODULE_DEVICE_TABLE(of, goodix_of_match);
930 #endif
931
932 static struct i2c_driver goodix_ts_driver = {
933         .probe = goodix_ts_probe,
934         .remove = goodix_ts_remove,
935         .id_table = goodix_ts_id,
936         .driver = {
937                 .name = "Goodix-TS",
938                 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
939                 .of_match_table = of_match_ptr(goodix_of_match),
940                 .pm = &goodix_pm_ops,
941         },
942 };
943 module_i2c_driver(goodix_ts_driver);
944
945 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
946 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
947 MODULE_DESCRIPTION("Goodix touchscreen driver");
948 MODULE_LICENSE("GPL v2");