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