Input: dlink-dir685-touchkeys - fix a typo in driver name
[linux/fpc-iii.git] / drivers / input / touchscreen / goodix.c
blobb20ba65992735253bf71ebce5a10ab2ea1b14d40
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 = "Teclast X89",
132 .matches = {
133 /* tPAD is too generic, also match on bios date */
134 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
135 DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
136 DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"),
140 .ident = "WinBook TW100",
141 .matches = {
142 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
143 DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
147 .ident = "WinBook TW700",
148 .matches = {
149 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
150 DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
153 #endif
158 * goodix_i2c_read - read data from a register of the i2c slave device.
160 * @client: i2c device.
161 * @reg: the register to read from.
162 * @buf: raw write data buffer.
163 * @len: length of the buffer to write
165 static int goodix_i2c_read(struct i2c_client *client,
166 u16 reg, u8 *buf, int len)
168 struct i2c_msg msgs[2];
169 __be16 wbuf = cpu_to_be16(reg);
170 int ret;
172 msgs[0].flags = 0;
173 msgs[0].addr = client->addr;
174 msgs[0].len = 2;
175 msgs[0].buf = (u8 *)&wbuf;
177 msgs[1].flags = I2C_M_RD;
178 msgs[1].addr = client->addr;
179 msgs[1].len = len;
180 msgs[1].buf = buf;
182 ret = i2c_transfer(client->adapter, msgs, 2);
183 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
187 * goodix_i2c_write - write data to a register of the i2c slave device.
189 * @client: i2c device.
190 * @reg: the register to write to.
191 * @buf: raw data buffer to write.
192 * @len: length of the buffer to write
194 static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
195 unsigned len)
197 u8 *addr_buf;
198 struct i2c_msg msg;
199 int ret;
201 addr_buf = kmalloc(len + 2, GFP_KERNEL);
202 if (!addr_buf)
203 return -ENOMEM;
205 addr_buf[0] = reg >> 8;
206 addr_buf[1] = reg & 0xFF;
207 memcpy(&addr_buf[2], buf, len);
209 msg.flags = 0;
210 msg.addr = client->addr;
211 msg.buf = addr_buf;
212 msg.len = len + 2;
214 ret = i2c_transfer(client->adapter, &msg, 1);
215 kfree(addr_buf);
216 return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
219 static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
221 return goodix_i2c_write(client, reg, &value, sizeof(value));
224 static const struct goodix_chip_data *goodix_get_chip_data(u16 id)
226 switch (id) {
227 case 1151:
228 return &gt1x_chip_data;
230 case 911:
231 case 9271:
232 case 9110:
233 case 927:
234 case 928:
235 return &gt911_chip_data;
237 case 912:
238 case 967:
239 return &gt967_chip_data;
241 default:
242 return &gt9x_chip_data;
246 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
248 unsigned long max_timeout;
249 int touch_num;
250 int error;
253 * The 'buffer status' bit, which indicates that the data is valid, is
254 * not set as soon as the interrupt is raised, but slightly after.
255 * This takes around 10 ms to happen, so we poll for 20 ms.
257 max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
258 do {
259 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR,
260 data, GOODIX_CONTACT_SIZE + 1);
261 if (error) {
262 dev_err(&ts->client->dev, "I2C transfer error: %d\n",
263 error);
264 return error;
267 if (data[0] & GOODIX_BUFFER_STATUS_READY) {
268 touch_num = data[0] & 0x0f;
269 if (touch_num > ts->max_touch_num)
270 return -EPROTO;
272 if (touch_num > 1) {
273 data += 1 + GOODIX_CONTACT_SIZE;
274 error = goodix_i2c_read(ts->client,
275 GOODIX_READ_COOR_ADDR +
276 1 + GOODIX_CONTACT_SIZE,
277 data,
278 GOODIX_CONTACT_SIZE *
279 (touch_num - 1));
280 if (error)
281 return error;
284 return touch_num;
287 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
288 } while (time_before(jiffies, max_timeout));
291 * The Goodix panel will send spurious interrupts after a
292 * 'finger up' event, which will always cause a timeout.
294 return 0;
297 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
299 int id = coor_data[0] & 0x0F;
300 int input_x = get_unaligned_le16(&coor_data[1]);
301 int input_y = get_unaligned_le16(&coor_data[3]);
302 int input_w = get_unaligned_le16(&coor_data[5]);
304 input_mt_slot(ts->input_dev, id);
305 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
306 touchscreen_report_pos(ts->input_dev, &ts->prop,
307 input_x, input_y, true);
308 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
309 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
313 * goodix_process_events - Process incoming events
315 * @ts: our goodix_ts_data pointer
317 * Called when the IRQ is triggered. Read the current device state, and push
318 * the input events to the user space.
320 static void goodix_process_events(struct goodix_ts_data *ts)
322 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
323 int touch_num;
324 int i;
326 touch_num = goodix_ts_read_input_report(ts, point_data);
327 if (touch_num < 0)
328 return;
331 * Bit 4 of the first byte reports the status of the capacitive
332 * Windows/Home button.
334 input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
336 for (i = 0; i < touch_num; i++)
337 goodix_ts_report_touch(ts,
338 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
340 input_mt_sync_frame(ts->input_dev);
341 input_sync(ts->input_dev);
345 * goodix_ts_irq_handler - The IRQ handler
347 * @irq: interrupt number.
348 * @dev_id: private data pointer.
350 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
352 struct goodix_ts_data *ts = dev_id;
354 goodix_process_events(ts);
356 if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
357 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
359 return IRQ_HANDLED;
362 static void goodix_free_irq(struct goodix_ts_data *ts)
364 devm_free_irq(&ts->client->dev, ts->client->irq, ts);
367 static int goodix_request_irq(struct goodix_ts_data *ts)
369 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
370 NULL, goodix_ts_irq_handler,
371 ts->irq_flags, ts->client->name, ts);
374 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
375 const struct firmware *cfg)
377 int i, raw_cfg_len = cfg->size - 2;
378 u8 check_sum = 0;
380 for (i = 0; i < raw_cfg_len; i++)
381 check_sum += cfg->data[i];
382 check_sum = (~check_sum) + 1;
383 if (check_sum != cfg->data[raw_cfg_len]) {
384 dev_err(&ts->client->dev,
385 "The checksum of the config fw is not correct");
386 return -EINVAL;
389 if (cfg->data[raw_cfg_len + 1] != 1) {
390 dev_err(&ts->client->dev,
391 "Config fw must have Config_Fresh register set");
392 return -EINVAL;
395 return 0;
398 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
399 const struct firmware *cfg)
401 int i, raw_cfg_len = cfg->size - 3;
402 u16 check_sum = 0;
404 for (i = 0; i < raw_cfg_len; i += 2)
405 check_sum += get_unaligned_be16(&cfg->data[i]);
406 check_sum = (~check_sum) + 1;
407 if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) {
408 dev_err(&ts->client->dev,
409 "The checksum of the config fw is not correct");
410 return -EINVAL;
413 if (cfg->data[raw_cfg_len + 2] != 1) {
414 dev_err(&ts->client->dev,
415 "Config fw must have Config_Fresh register set");
416 return -EINVAL;
419 return 0;
423 * goodix_check_cfg - Checks if config fw is valid
425 * @ts: goodix_ts_data pointer
426 * @cfg: firmware config data
428 static int goodix_check_cfg(struct goodix_ts_data *ts,
429 const struct firmware *cfg)
431 if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
432 dev_err(&ts->client->dev,
433 "The length of the config fw is not correct");
434 return -EINVAL;
437 return ts->chip->check_config(ts, cfg);
441 * goodix_send_cfg - Write fw config to device
443 * @ts: goodix_ts_data pointer
444 * @cfg: config firmware to write to device
446 static int goodix_send_cfg(struct goodix_ts_data *ts,
447 const struct firmware *cfg)
449 int error;
451 error = goodix_check_cfg(ts, cfg);
452 if (error)
453 return error;
455 error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data,
456 cfg->size);
457 if (error) {
458 dev_err(&ts->client->dev, "Failed to write config data: %d",
459 error);
460 return error;
462 dev_dbg(&ts->client->dev, "Config sent successfully.");
464 /* Let the firmware reconfigure itself, so sleep for 10ms */
465 usleep_range(10000, 11000);
467 return 0;
470 static int goodix_int_sync(struct goodix_ts_data *ts)
472 int error;
474 error = gpiod_direction_output(ts->gpiod_int, 0);
475 if (error)
476 return error;
478 msleep(50); /* T5: 50ms */
480 error = gpiod_direction_input(ts->gpiod_int);
481 if (error)
482 return error;
484 return 0;
488 * goodix_reset - Reset device during power on
490 * @ts: goodix_ts_data pointer
492 static int goodix_reset(struct goodix_ts_data *ts)
494 int error;
496 /* begin select I2C slave addr */
497 error = gpiod_direction_output(ts->gpiod_rst, 0);
498 if (error)
499 return error;
501 msleep(20); /* T2: > 10ms */
503 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
504 error = gpiod_direction_output(ts->gpiod_int, ts->client->addr == 0x14);
505 if (error)
506 return error;
508 usleep_range(100, 2000); /* T3: > 100us */
510 error = gpiod_direction_output(ts->gpiod_rst, 1);
511 if (error)
512 return error;
514 usleep_range(6000, 10000); /* T4: > 5ms */
516 /* end select I2C slave addr */
517 error = gpiod_direction_input(ts->gpiod_rst);
518 if (error)
519 return error;
521 error = goodix_int_sync(ts);
522 if (error)
523 return error;
525 return 0;
529 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
531 * @ts: goodix_ts_data pointer
533 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
535 int error;
536 struct device *dev;
537 struct gpio_desc *gpiod;
539 if (!ts->client)
540 return -EINVAL;
541 dev = &ts->client->dev;
543 /* Get the interrupt GPIO pin number */
544 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
545 if (IS_ERR(gpiod)) {
546 error = PTR_ERR(gpiod);
547 if (error != -EPROBE_DEFER)
548 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
549 GOODIX_GPIO_INT_NAME, error);
550 return error;
553 ts->gpiod_int = gpiod;
555 /* Get the reset line GPIO pin number */
556 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
557 if (IS_ERR(gpiod)) {
558 error = PTR_ERR(gpiod);
559 if (error != -EPROBE_DEFER)
560 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
561 GOODIX_GPIO_RST_NAME, error);
562 return error;
565 ts->gpiod_rst = gpiod;
567 return 0;
571 * goodix_read_config - Read the embedded configuration of the panel
573 * @ts: our goodix_ts_data pointer
575 * Must be called during probe
577 static void goodix_read_config(struct goodix_ts_data *ts)
579 u8 config[GOODIX_CONFIG_MAX_LENGTH];
580 int x_max, y_max;
581 int error;
583 error = goodix_i2c_read(ts->client, ts->chip->config_addr,
584 config, ts->chip->config_len);
585 if (error) {
586 dev_warn(&ts->client->dev, "Error reading config: %d\n",
587 error);
588 ts->int_trigger_type = GOODIX_INT_TRIGGER;
589 ts->max_touch_num = GOODIX_MAX_CONTACTS;
590 return;
593 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
594 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
596 x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
597 y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
598 if (x_max && y_max) {
599 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
600 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
605 * goodix_read_version - Read goodix touchscreen version
607 * @ts: our goodix_ts_data pointer
609 static int goodix_read_version(struct goodix_ts_data *ts)
611 int error;
612 u8 buf[6];
613 char id_str[5];
615 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
616 if (error) {
617 dev_err(&ts->client->dev, "read version failed: %d\n", error);
618 return error;
621 memcpy(id_str, buf, 4);
622 id_str[4] = 0;
623 if (kstrtou16(id_str, 10, &ts->id))
624 ts->id = 0x1001;
626 ts->version = get_unaligned_le16(&buf[4]);
628 dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
629 ts->version);
631 return 0;
635 * goodix_i2c_test - I2C test function to check if the device answers.
637 * @client: the i2c client
639 static int goodix_i2c_test(struct i2c_client *client)
641 int retry = 0;
642 int error;
643 u8 test;
645 while (retry++ < 2) {
646 error = goodix_i2c_read(client, GOODIX_REG_ID,
647 &test, 1);
648 if (!error)
649 return 0;
651 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
652 retry, error);
653 msleep(20);
656 return error;
660 * goodix_configure_dev - Finish device initialization
662 * @ts: our goodix_ts_data pointer
664 * Must be called from probe to finish initialization of the device.
665 * Contains the common initialization code for both devices that
666 * declare gpio pins and devices that do not. It is either called
667 * directly from probe or from request_firmware_wait callback.
669 static int goodix_configure_dev(struct goodix_ts_data *ts)
671 int error;
673 ts->int_trigger_type = GOODIX_INT_TRIGGER;
674 ts->max_touch_num = GOODIX_MAX_CONTACTS;
676 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
677 if (!ts->input_dev) {
678 dev_err(&ts->client->dev, "Failed to allocate input device.");
679 return -ENOMEM;
682 ts->input_dev->name = "Goodix Capacitive TouchScreen";
683 ts->input_dev->phys = "input/ts";
684 ts->input_dev->id.bustype = BUS_I2C;
685 ts->input_dev->id.vendor = 0x0416;
686 ts->input_dev->id.product = ts->id;
687 ts->input_dev->id.version = ts->version;
689 /* Capacitive Windows/Home button on some devices */
690 input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
692 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
693 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
694 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
695 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
697 /* Read configuration and apply touchscreen parameters */
698 goodix_read_config(ts);
700 /* Try overriding touchscreen parameters via device properties */
701 touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
703 if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
704 dev_err(&ts->client->dev, "Invalid config, using defaults\n");
705 ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
706 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
707 ts->max_touch_num = GOODIX_MAX_CONTACTS;
708 input_abs_set_max(ts->input_dev,
709 ABS_MT_POSITION_X, ts->prop.max_x);
710 input_abs_set_max(ts->input_dev,
711 ABS_MT_POSITION_Y, ts->prop.max_y);
714 if (dmi_check_system(rotated_screen)) {
715 ts->prop.invert_x = true;
716 ts->prop.invert_y = true;
717 dev_dbg(&ts->client->dev,
718 "Applying '180 degrees rotated screen' quirk\n");
721 error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
722 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
723 if (error) {
724 dev_err(&ts->client->dev,
725 "Failed to initialize MT slots: %d", error);
726 return error;
729 error = input_register_device(ts->input_dev);
730 if (error) {
731 dev_err(&ts->client->dev,
732 "Failed to register input device: %d", error);
733 return error;
736 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
737 error = goodix_request_irq(ts);
738 if (error) {
739 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
740 return error;
743 return 0;
747 * goodix_config_cb - Callback to finish device init
749 * @ts: our goodix_ts_data pointer
751 * request_firmware_wait callback that finishes
752 * initialization of the device.
754 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
756 struct goodix_ts_data *ts = ctx;
757 int error;
759 if (cfg) {
760 /* send device configuration to the firmware */
761 error = goodix_send_cfg(ts, cfg);
762 if (error)
763 goto err_release_cfg;
766 goodix_configure_dev(ts);
768 err_release_cfg:
769 release_firmware(cfg);
770 complete_all(&ts->firmware_loading_complete);
773 static int goodix_ts_probe(struct i2c_client *client,
774 const struct i2c_device_id *id)
776 struct goodix_ts_data *ts;
777 int error;
779 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
781 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
782 dev_err(&client->dev, "I2C check functionality failed.\n");
783 return -ENXIO;
786 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
787 if (!ts)
788 return -ENOMEM;
790 ts->client = client;
791 i2c_set_clientdata(client, ts);
792 init_completion(&ts->firmware_loading_complete);
794 error = goodix_get_gpio_config(ts);
795 if (error)
796 return error;
798 if (ts->gpiod_int && ts->gpiod_rst) {
799 /* reset the controller */
800 error = goodix_reset(ts);
801 if (error) {
802 dev_err(&client->dev, "Controller reset failed.\n");
803 return error;
807 error = goodix_i2c_test(client);
808 if (error) {
809 dev_err(&client->dev, "I2C communication failure: %d\n", error);
810 return error;
813 error = goodix_read_version(ts);
814 if (error) {
815 dev_err(&client->dev, "Read version failed.\n");
816 return error;
819 ts->chip = goodix_get_chip_data(ts->id);
821 if (ts->gpiod_int && ts->gpiod_rst) {
822 /* update device config */
823 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
824 "goodix_%d_cfg.bin", ts->id);
825 if (!ts->cfg_name)
826 return -ENOMEM;
828 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
829 &client->dev, GFP_KERNEL, ts,
830 goodix_config_cb);
831 if (error) {
832 dev_err(&client->dev,
833 "Failed to invoke firmware loader: %d\n",
834 error);
835 return error;
838 return 0;
839 } else {
840 error = goodix_configure_dev(ts);
841 if (error)
842 return error;
845 return 0;
848 static int goodix_ts_remove(struct i2c_client *client)
850 struct goodix_ts_data *ts = i2c_get_clientdata(client);
852 if (ts->gpiod_int && ts->gpiod_rst)
853 wait_for_completion(&ts->firmware_loading_complete);
855 return 0;
858 static int __maybe_unused goodix_suspend(struct device *dev)
860 struct i2c_client *client = to_i2c_client(dev);
861 struct goodix_ts_data *ts = i2c_get_clientdata(client);
862 int error;
864 /* We need gpio pins to suspend/resume */
865 if (!ts->gpiod_int || !ts->gpiod_rst) {
866 disable_irq(client->irq);
867 return 0;
870 wait_for_completion(&ts->firmware_loading_complete);
872 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
873 goodix_free_irq(ts);
875 /* Output LOW on the INT pin for 5 ms */
876 error = gpiod_direction_output(ts->gpiod_int, 0);
877 if (error) {
878 goodix_request_irq(ts);
879 return error;
882 usleep_range(5000, 6000);
884 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
885 GOODIX_CMD_SCREEN_OFF);
886 if (error) {
887 dev_err(&ts->client->dev, "Screen off command failed\n");
888 gpiod_direction_input(ts->gpiod_int);
889 goodix_request_irq(ts);
890 return -EAGAIN;
894 * The datasheet specifies that the interval between sending screen-off
895 * command and wake-up should be longer than 58 ms. To avoid waking up
896 * sooner, delay 58ms here.
898 msleep(58);
899 return 0;
902 static int __maybe_unused goodix_resume(struct device *dev)
904 struct i2c_client *client = to_i2c_client(dev);
905 struct goodix_ts_data *ts = i2c_get_clientdata(client);
906 int error;
908 if (!ts->gpiod_int || !ts->gpiod_rst) {
909 enable_irq(client->irq);
910 return 0;
914 * Exit sleep mode by outputting HIGH level to INT pin
915 * for 2ms~5ms.
917 error = gpiod_direction_output(ts->gpiod_int, 1);
918 if (error)
919 return error;
921 usleep_range(2000, 5000);
923 error = goodix_int_sync(ts);
924 if (error)
925 return error;
927 error = goodix_request_irq(ts);
928 if (error)
929 return error;
931 return 0;
934 static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
936 static const struct i2c_device_id goodix_ts_id[] = {
937 { "GDIX1001:00", 0 },
940 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
942 #ifdef CONFIG_ACPI
943 static const struct acpi_device_id goodix_acpi_match[] = {
944 { "GDIX1001", 0 },
945 { "GDIX1002", 0 },
948 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
949 #endif
951 #ifdef CONFIG_OF
952 static const struct of_device_id goodix_of_match[] = {
953 { .compatible = "goodix,gt1151" },
954 { .compatible = "goodix,gt911" },
955 { .compatible = "goodix,gt9110" },
956 { .compatible = "goodix,gt912" },
957 { .compatible = "goodix,gt927" },
958 { .compatible = "goodix,gt9271" },
959 { .compatible = "goodix,gt928" },
960 { .compatible = "goodix,gt967" },
963 MODULE_DEVICE_TABLE(of, goodix_of_match);
964 #endif
966 static struct i2c_driver goodix_ts_driver = {
967 .probe = goodix_ts_probe,
968 .remove = goodix_ts_remove,
969 .id_table = goodix_ts_id,
970 .driver = {
971 .name = "Goodix-TS",
972 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
973 .of_match_table = of_match_ptr(goodix_of_match),
974 .pm = &goodix_pm_ops,
977 module_i2c_driver(goodix_ts_driver);
979 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
980 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
981 MODULE_DESCRIPTION("Goodix touchscreen driver");
982 MODULE_LICENSE("GPL v2");