Linux 5.1.15
[linux/fpc-iii.git] / drivers / input / touchscreen / goodix.c
blobdd029e6899038fd9428578c8fd9a1499068b6d85
1 /*
2 * Driver for Goodix Touchscreens
4 * Copyright (c) 2014 Red Hat Inc.
5 * Copyright (c) 2015 K. Merker <merker@debian.org>
7 * This code is based on gt9xx.c authored by andrew@goodix.com:
9 * 2010 - 2012 Goodix Technology.
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.
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/input/touchscreen.h>
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/slab.h>
31 #include <linux/acpi.h>
32 #include <linux/of.h>
33 #include <asm/unaligned.h>
35 struct goodix_ts_data;
37 struct goodix_chip_data {
38 u16 config_addr;
39 int config_len;
40 int (*check_config)(struct goodix_ts_data *, const struct firmware *);
43 struct goodix_ts_data {
44 struct i2c_client *client;
45 struct input_dev *input_dev;
46 const struct goodix_chip_data *chip;
47 struct touchscreen_properties prop;
48 unsigned int max_touch_num;
49 unsigned int int_trigger_type;
50 struct gpio_desc *gpiod_int;
51 struct gpio_desc *gpiod_rst;
52 u16 id;
53 u16 version;
54 const char *cfg_name;
55 struct completion firmware_loading_complete;
56 unsigned long irq_flags;
59 #define GOODIX_GPIO_INT_NAME "irq"
60 #define GOODIX_GPIO_RST_NAME "reset"
62 #define GOODIX_MAX_HEIGHT 4096
63 #define GOODIX_MAX_WIDTH 4096
64 #define GOODIX_INT_TRIGGER 1
65 #define GOODIX_CONTACT_SIZE 8
66 #define GOODIX_MAX_CONTACTS 10
68 #define GOODIX_CONFIG_MAX_LENGTH 240
69 #define GOODIX_CONFIG_911_LENGTH 186
70 #define GOODIX_CONFIG_967_LENGTH 228
72 /* Register defines */
73 #define GOODIX_REG_COMMAND 0x8040
74 #define GOODIX_CMD_SCREEN_OFF 0x05
76 #define GOODIX_READ_COOR_ADDR 0x814E
77 #define GOODIX_GT1X_REG_CONFIG_DATA 0x8050
78 #define GOODIX_GT9X_REG_CONFIG_DATA 0x8047
79 #define GOODIX_REG_ID 0x8140
81 #define GOODIX_BUFFER_STATUS_READY BIT(7)
82 #define GOODIX_BUFFER_STATUS_TIMEOUT 20
84 #define RESOLUTION_LOC 1
85 #define MAX_CONTACTS_LOC 5
86 #define TRIGGER_LOC 6
88 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
89 const struct firmware *cfg);
90 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
91 const struct firmware *cfg);
93 static const struct goodix_chip_data gt1x_chip_data = {
94 .config_addr = GOODIX_GT1X_REG_CONFIG_DATA,
95 .config_len = GOODIX_CONFIG_MAX_LENGTH,
96 .check_config = goodix_check_cfg_16,
99 static const struct goodix_chip_data gt911_chip_data = {
100 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
101 .config_len = GOODIX_CONFIG_911_LENGTH,
102 .check_config = goodix_check_cfg_8,
105 static const struct goodix_chip_data gt967_chip_data = {
106 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
107 .config_len = GOODIX_CONFIG_967_LENGTH,
108 .check_config = goodix_check_cfg_8,
111 static const struct goodix_chip_data gt9x_chip_data = {
112 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
113 .config_len = GOODIX_CONFIG_MAX_LENGTH,
114 .check_config = goodix_check_cfg_8,
117 static const unsigned long goodix_irq_flags[] = {
118 IRQ_TYPE_EDGE_RISING,
119 IRQ_TYPE_EDGE_FALLING,
120 IRQ_TYPE_LEVEL_LOW,
121 IRQ_TYPE_LEVEL_HIGH,
125 * Those tablets have their coordinates origin at the bottom right
126 * of the tablet, as if rotated 180 degrees
128 static const struct dmi_system_id rotated_screen[] = {
129 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
131 .ident = "WinBook TW100",
132 .matches = {
133 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
134 DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
138 .ident = "WinBook TW700",
139 .matches = {
140 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
141 DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
144 #endif
149 * goodix_i2c_read - read data from a register of the i2c slave device.
151 * @client: i2c device.
152 * @reg: the register to read from.
153 * @buf: raw write data buffer.
154 * @len: length of the buffer to write
156 static int goodix_i2c_read(struct i2c_client *client,
157 u16 reg, u8 *buf, int len)
159 struct i2c_msg msgs[2];
160 __be16 wbuf = cpu_to_be16(reg);
161 int ret;
163 msgs[0].flags = 0;
164 msgs[0].addr = client->addr;
165 msgs[0].len = 2;
166 msgs[0].buf = (u8 *)&wbuf;
168 msgs[1].flags = I2C_M_RD;
169 msgs[1].addr = client->addr;
170 msgs[1].len = len;
171 msgs[1].buf = buf;
173 ret = i2c_transfer(client->adapter, msgs, 2);
174 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
178 * goodix_i2c_write - write data to a register of the i2c slave device.
180 * @client: i2c device.
181 * @reg: the register to write to.
182 * @buf: raw data buffer to write.
183 * @len: length of the buffer to write
185 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
186 unsigned len)
188 u8 *addr_buf;
189 struct i2c_msg msg;
190 int ret;
192 addr_buf = kmalloc(len + 2, GFP_KERNEL);
193 if (!addr_buf)
194 return -ENOMEM;
196 addr_buf[0] = reg >> 8;
197 addr_buf[1] = reg & 0xFF;
198 memcpy(&addr_buf[2], buf, len);
200 msg.flags = 0;
201 msg.addr = client->addr;
202 msg.buf = addr_buf;
203 msg.len = len + 2;
205 ret = i2c_transfer(client->adapter, &msg, 1);
206 kfree(addr_buf);
207 return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
210 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
212 return goodix_i2c_write(client, reg, &value, sizeof(value));
215 static const struct goodix_chip_data *goodix_get_chip_data(u16 id)
217 switch (id) {
218 case 1151:
219 case 5663:
220 case 5688:
221 return &gt1x_chip_data;
223 case 911:
224 case 9271:
225 case 9110:
226 case 927:
227 case 928:
228 return &gt911_chip_data;
230 case 912:
231 case 967:
232 return &gt967_chip_data;
234 default:
235 return &gt9x_chip_data;
239 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
241 unsigned long max_timeout;
242 int touch_num;
243 int error;
246 * The 'buffer status' bit, which indicates that the data is valid, is
247 * not set as soon as the interrupt is raised, but slightly after.
248 * This takes around 10 ms to happen, so we poll for 20 ms.
250 max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
251 do {
252 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR,
253 data, GOODIX_CONTACT_SIZE + 1);
254 if (error) {
255 dev_err(&ts->client->dev, "I2C transfer error: %d\n",
256 error);
257 return error;
260 if (data[0] & GOODIX_BUFFER_STATUS_READY) {
261 touch_num = data[0] & 0x0f;
262 if (touch_num > ts->max_touch_num)
263 return -EPROTO;
265 if (touch_num > 1) {
266 data += 1 + GOODIX_CONTACT_SIZE;
267 error = goodix_i2c_read(ts->client,
268 GOODIX_READ_COOR_ADDR +
269 1 + GOODIX_CONTACT_SIZE,
270 data,
271 GOODIX_CONTACT_SIZE *
272 (touch_num - 1));
273 if (error)
274 return error;
277 return touch_num;
280 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
281 } while (time_before(jiffies, max_timeout));
284 * The Goodix panel will send spurious interrupts after a
285 * 'finger up' event, which will always cause a timeout.
287 return 0;
290 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
292 int id = coor_data[0] & 0x0F;
293 int input_x = get_unaligned_le16(&coor_data[1]);
294 int input_y = get_unaligned_le16(&coor_data[3]);
295 int input_w = get_unaligned_le16(&coor_data[5]);
297 input_mt_slot(ts->input_dev, id);
298 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
299 touchscreen_report_pos(ts->input_dev, &ts->prop,
300 input_x, input_y, true);
301 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
302 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
306 * goodix_process_events - Process incoming events
308 * @ts: our goodix_ts_data pointer
310 * Called when the IRQ is triggered. Read the current device state, and push
311 * the input events to the user space.
313 static void goodix_process_events(struct goodix_ts_data *ts)
315 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
316 int touch_num;
317 int i;
319 touch_num = goodix_ts_read_input_report(ts, point_data);
320 if (touch_num < 0)
321 return;
324 * Bit 4 of the first byte reports the status of the capacitive
325 * Windows/Home button.
327 input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
329 for (i = 0; i < touch_num; i++)
330 goodix_ts_report_touch(ts,
331 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
333 input_mt_sync_frame(ts->input_dev);
334 input_sync(ts->input_dev);
338 * goodix_ts_irq_handler - The IRQ handler
340 * @irq: interrupt number.
341 * @dev_id: private data pointer.
343 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
345 struct goodix_ts_data *ts = dev_id;
347 goodix_process_events(ts);
349 if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
350 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
352 return IRQ_HANDLED;
355 static void goodix_free_irq(struct goodix_ts_data *ts)
357 devm_free_irq(&ts->client->dev, ts->client->irq, ts);
360 static int goodix_request_irq(struct goodix_ts_data *ts)
362 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
363 NULL, goodix_ts_irq_handler,
364 ts->irq_flags, ts->client->name, ts);
367 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
368 const struct firmware *cfg)
370 int i, raw_cfg_len = cfg->size - 2;
371 u8 check_sum = 0;
373 for (i = 0; i < raw_cfg_len; i++)
374 check_sum += cfg->data[i];
375 check_sum = (~check_sum) + 1;
376 if (check_sum != cfg->data[raw_cfg_len]) {
377 dev_err(&ts->client->dev,
378 "The checksum of the config fw is not correct");
379 return -EINVAL;
382 if (cfg->data[raw_cfg_len + 1] != 1) {
383 dev_err(&ts->client->dev,
384 "Config fw must have Config_Fresh register set");
385 return -EINVAL;
388 return 0;
391 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
392 const struct firmware *cfg)
394 int i, raw_cfg_len = cfg->size - 3;
395 u16 check_sum = 0;
397 for (i = 0; i < raw_cfg_len; i += 2)
398 check_sum += get_unaligned_be16(&cfg->data[i]);
399 check_sum = (~check_sum) + 1;
400 if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) {
401 dev_err(&ts->client->dev,
402 "The checksum of the config fw is not correct");
403 return -EINVAL;
406 if (cfg->data[raw_cfg_len + 2] != 1) {
407 dev_err(&ts->client->dev,
408 "Config fw must have Config_Fresh register set");
409 return -EINVAL;
412 return 0;
416 * goodix_check_cfg - Checks if config fw is valid
418 * @ts: goodix_ts_data pointer
419 * @cfg: firmware config data
421 static int goodix_check_cfg(struct goodix_ts_data *ts,
422 const struct firmware *cfg)
424 if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
425 dev_err(&ts->client->dev,
426 "The length of the config fw is not correct");
427 return -EINVAL;
430 return ts->chip->check_config(ts, cfg);
434 * goodix_send_cfg - Write fw config to device
436 * @ts: goodix_ts_data pointer
437 * @cfg: config firmware to write to device
439 static int goodix_send_cfg(struct goodix_ts_data *ts,
440 const struct firmware *cfg)
442 int error;
444 error = goodix_check_cfg(ts, cfg);
445 if (error)
446 return error;
448 error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data,
449 cfg->size);
450 if (error) {
451 dev_err(&ts->client->dev, "Failed to write config data: %d",
452 error);
453 return error;
455 dev_dbg(&ts->client->dev, "Config sent successfully.");
457 /* Let the firmware reconfigure itself, so sleep for 10ms */
458 usleep_range(10000, 11000);
460 return 0;
463 static int goodix_int_sync(struct goodix_ts_data *ts)
465 int error;
467 error = gpiod_direction_output(ts->gpiod_int, 0);
468 if (error)
469 return error;
471 msleep(50); /* T5: 50ms */
473 error = gpiod_direction_input(ts->gpiod_int);
474 if (error)
475 return error;
477 return 0;
481 * goodix_reset - Reset device during power on
483 * @ts: goodix_ts_data pointer
485 static int goodix_reset(struct goodix_ts_data *ts)
487 int error;
489 /* begin select I2C slave addr */
490 error = gpiod_direction_output(ts->gpiod_rst, 0);
491 if (error)
492 return error;
494 msleep(20); /* T2: > 10ms */
496 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
497 error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
498 if (error)
499 return error;
501 usleep_range(100, 2000); /* T3: > 100us */
503 error = gpiod_direction_output(ts->gpiod_rst, 1);
504 if (error)
505 return error;
507 usleep_range(6000, 10000); /* T4: > 5ms */
509 /* end select I2C slave addr */
510 error = gpiod_direction_input(ts->gpiod_rst);
511 if (error)
512 return error;
514 error = goodix_int_sync(ts);
515 if (error)
516 return error;
518 return 0;
522 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
524 * @ts: goodix_ts_data pointer
526 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
528 int error;
529 struct device *dev;
530 struct gpio_desc *gpiod;
532 if (!ts->client)
533 return -EINVAL;
534 dev = &ts->client->dev;
536 /* Get the interrupt GPIO pin number */
537 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
538 if (IS_ERR(gpiod)) {
539 error = PTR_ERR(gpiod);
540 if (error != -EPROBE_DEFER)
541 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
542 GOODIX_GPIO_INT_NAME, error);
543 return error;
546 ts->gpiod_int = gpiod;
548 /* Get the reset line GPIO pin number */
549 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
550 if (IS_ERR(gpiod)) {
551 error = PTR_ERR(gpiod);
552 if (error != -EPROBE_DEFER)
553 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
554 GOODIX_GPIO_RST_NAME, error);
555 return error;
558 ts->gpiod_rst = gpiod;
560 return 0;
564 * goodix_read_config - Read the embedded configuration of the panel
566 * @ts: our goodix_ts_data pointer
568 * Must be called during probe
570 static void goodix_read_config(struct goodix_ts_data *ts)
572 u8 config[GOODIX_CONFIG_MAX_LENGTH];
573 int x_max, y_max;
574 int error;
576 error = goodix_i2c_read(ts->client, ts->chip->config_addr,
577 config, ts->chip->config_len);
578 if (error) {
579 dev_warn(&ts->client->dev, "Error reading config: %d\n",
580 error);
581 ts->int_trigger_type = GOODIX_INT_TRIGGER;
582 ts->max_touch_num = GOODIX_MAX_CONTACTS;
583 return;
586 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
587 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
589 x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
590 y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
591 if (x_max && y_max) {
592 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
593 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
598 * goodix_read_version - Read goodix touchscreen version
600 * @ts: our goodix_ts_data pointer
602 static int goodix_read_version(struct goodix_ts_data *ts)
604 int error;
605 u8 buf[6];
606 char id_str[5];
608 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
609 if (error) {
610 dev_err(&ts->client->dev, "read version failed: %d\n", error);
611 return error;
614 memcpy(id_str, buf, 4);
615 id_str[4] = 0;
616 if (kstrtou16(id_str, 10, &ts->id))
617 ts->id = 0x1001;
619 ts->version = get_unaligned_le16(&buf[4]);
621 dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
622 ts->version);
624 return 0;
628 * goodix_i2c_test - I2C test function to check if the device answers.
630 * @client: the i2c client
632 static int goodix_i2c_test(struct i2c_client *client)
634 int retry = 0;
635 int error;
636 u8 test;
638 while (retry++ < 2) {
639 error = goodix_i2c_read(client, GOODIX_REG_ID,
640 &test, 1);
641 if (!error)
642 return 0;
644 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
645 retry, error);
646 msleep(20);
649 return error;
653 * goodix_configure_dev - Finish device initialization
655 * @ts: our goodix_ts_data pointer
657 * Must be called from probe to finish initialization of the device.
658 * Contains the common initialization code for both devices that
659 * declare gpio pins and devices that do not. It is either called
660 * directly from probe or from request_firmware_wait callback.
662 static int goodix_configure_dev(struct goodix_ts_data *ts)
664 int error;
666 ts->int_trigger_type = GOODIX_INT_TRIGGER;
667 ts->max_touch_num = GOODIX_MAX_CONTACTS;
669 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
670 if (!ts->input_dev) {
671 dev_err(&ts->client->dev, "Failed to allocate input device.");
672 return -ENOMEM;
675 ts->input_dev->name = "Goodix Capacitive TouchScreen";
676 ts->input_dev->phys = "input/ts";
677 ts->input_dev->id.bustype = BUS_I2C;
678 ts->input_dev->id.vendor = 0x0416;
679 ts->input_dev->id.product = ts->id;
680 ts->input_dev->id.version = ts->version;
682 /* Capacitive Windows/Home button on some devices */
683 input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
685 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
686 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
687 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
688 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
690 /* Read configuration and apply touchscreen parameters */
691 goodix_read_config(ts);
693 /* Try overriding touchscreen parameters via device properties */
694 touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
696 if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
697 dev_err(&ts->client->dev,
698 "Invalid config (%d, %d, %d), using defaults\n",
699 ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
700 ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
701 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
702 ts->max_touch_num = GOODIX_MAX_CONTACTS;
703 input_abs_set_max(ts->input_dev,
704 ABS_MT_POSITION_X, ts->prop.max_x);
705 input_abs_set_max(ts->input_dev,
706 ABS_MT_POSITION_Y, ts->prop.max_y);
709 if (dmi_check_system(rotated_screen)) {
710 ts->prop.invert_x = true;
711 ts->prop.invert_y = true;
712 dev_dbg(&ts->client->dev,
713 "Applying '180 degrees rotated screen' quirk\n");
716 error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
717 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
718 if (error) {
719 dev_err(&ts->client->dev,
720 "Failed to initialize MT slots: %d", error);
721 return error;
724 error = input_register_device(ts->input_dev);
725 if (error) {
726 dev_err(&ts->client->dev,
727 "Failed to register input device: %d", error);
728 return error;
731 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
732 error = goodix_request_irq(ts);
733 if (error) {
734 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
735 return error;
738 return 0;
742 * goodix_config_cb - Callback to finish device init
744 * @ts: our goodix_ts_data pointer
746 * request_firmware_wait callback that finishes
747 * initialization of the device.
749 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
751 struct goodix_ts_data *ts = ctx;
752 int error;
754 if (cfg) {
755 /* send device configuration to the firmware */
756 error = goodix_send_cfg(ts, cfg);
757 if (error)
758 goto err_release_cfg;
761 goodix_configure_dev(ts);
763 err_release_cfg:
764 release_firmware(cfg);
765 complete_all(&ts->firmware_loading_complete);
768 static int goodix_ts_probe(struct i2c_client *client,
769 const struct i2c_device_id *id)
771 struct goodix_ts_data *ts;
772 int error;
774 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
776 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
777 dev_err(&client->dev, "I2C check functionality failed.\n");
778 return -ENXIO;
781 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
782 if (!ts)
783 return -ENOMEM;
785 ts->client = client;
786 i2c_set_clientdata(client, ts);
787 init_completion(&ts->firmware_loading_complete);
789 error = goodix_get_gpio_config(ts);
790 if (error)
791 return error;
793 if (ts->gpiod_int && ts->gpiod_rst) {
794 /* reset the controller */
795 error = goodix_reset(ts);
796 if (error) {
797 dev_err(&client->dev, "Controller reset failed.\n");
798 return error;
802 error = goodix_i2c_test(client);
803 if (error) {
804 dev_err(&client->dev, "I2C communication failure: %d\n", error);
805 return error;
808 error = goodix_read_version(ts);
809 if (error) {
810 dev_err(&client->dev, "Read version failed.\n");
811 return error;
814 ts->chip = goodix_get_chip_data(ts->id);
816 if (ts->gpiod_int && ts->gpiod_rst) {
817 /* update device config */
818 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
819 "goodix_%d_cfg.bin", ts->id);
820 if (!ts->cfg_name)
821 return -ENOMEM;
823 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
824 &client->dev, GFP_KERNEL, ts,
825 goodix_config_cb);
826 if (error) {
827 dev_err(&client->dev,
828 "Failed to invoke firmware loader: %d\n",
829 error);
830 return error;
833 return 0;
834 } else {
835 error = goodix_configure_dev(ts);
836 if (error)
837 return error;
840 return 0;
843 static int goodix_ts_remove(struct i2c_client *client)
845 struct goodix_ts_data *ts = i2c_get_clientdata(client);
847 if (ts->gpiod_int && ts->gpiod_rst)
848 wait_for_completion(&ts->firmware_loading_complete);
850 return 0;
853 static int __maybe_unused goodix_suspend(struct device *dev)
855 struct i2c_client *client = to_i2c_client(dev);
856 struct goodix_ts_data *ts = i2c_get_clientdata(client);
857 int error;
859 /* We need gpio pins to suspend/resume */
860 if (!ts->gpiod_int || !ts->gpiod_rst) {
861 disable_irq(client->irq);
862 return 0;
865 wait_for_completion(&ts->firmware_loading_complete);
867 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
868 goodix_free_irq(ts);
870 /* Output LOW on the INT pin for 5 ms */
871 error = gpiod_direction_output(ts->gpiod_int, 0);
872 if (error) {
873 goodix_request_irq(ts);
874 return error;
877 usleep_range(5000, 6000);
879 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
880 GOODIX_CMD_SCREEN_OFF);
881 if (error) {
882 dev_err(&ts->client->dev, "Screen off command failed\n");
883 gpiod_direction_input(ts->gpiod_int);
884 goodix_request_irq(ts);
885 return -EAGAIN;
889 * The datasheet specifies that the interval between sending screen-off
890 * command and wake-up should be longer than 58 ms. To avoid waking up
891 * sooner, delay 58ms here.
893 msleep(58);
894 return 0;
897 static int __maybe_unused goodix_resume(struct device *dev)
899 struct i2c_client *client = to_i2c_client(dev);
900 struct goodix_ts_data *ts = i2c_get_clientdata(client);
901 int error;
903 if (!ts->gpiod_int || !ts->gpiod_rst) {
904 enable_irq(client->irq);
905 return 0;
909 * Exit sleep mode by outputting HIGH level to INT pin
910 * for 2ms~5ms.
912 error = gpiod_direction_output(ts->gpiod_int, 1);
913 if (error)
914 return error;
916 usleep_range(2000, 5000);
918 error = goodix_int_sync(ts);
919 if (error)
920 return error;
922 error = goodix_request_irq(ts);
923 if (error)
924 return error;
926 return 0;
929 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
931 static const struct i2c_device_id goodix_ts_id[] = {
932 { "GDIX1001:00", 0 },
935 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
937 #ifdef CONFIG_ACPI
938 static const struct acpi_device_id goodix_acpi_match[] = {
939 { "GDIX1001", 0 },
940 { "GDIX1002", 0 },
943 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
944 #endif
946 #ifdef CONFIG_OF
947 static const struct of_device_id goodix_of_match[] = {
948 { .compatible = "goodix,gt1151" },
949 { .compatible = "goodix,gt5663" },
950 { .compatible = "goodix,gt5688" },
951 { .compatible = "goodix,gt911" },
952 { .compatible = "goodix,gt9110" },
953 { .compatible = "goodix,gt912" },
954 { .compatible = "goodix,gt927" },
955 { .compatible = "goodix,gt9271" },
956 { .compatible = "goodix,gt928" },
957 { .compatible = "goodix,gt967" },
960 MODULE_DEVICE_TABLE(of, goodix_of_match);
961 #endif
963 static struct i2c_driver goodix_ts_driver = {
964 .probe = goodix_ts_probe,
965 .remove = goodix_ts_remove,
966 .id_table = goodix_ts_id,
967 .driver = {
968 .name = "Goodix-TS",
969 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
970 .of_match_table = of_match_ptr(goodix_of_match),
971 .pm = &goodix_pm_ops,
974 module_i2c_driver(goodix_ts_driver);
976 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
977 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
978 MODULE_DESCRIPTION("Goodix touchscreen driver");
979 MODULE_LICENSE("GPL v2");