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