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