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