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