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