1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Goodix Touchscreens
5 * Copyright (c) 2014 Red Hat Inc.
6 * Copyright (c) 2015 K. Merker <merker@debian.org>
8 * This code is based on gt9xx.c authored by andrew@goodix.com:
10 * 2010 - 2012 Goodix Technology.
14 #include <linux/kernel.h>
15 #include <linux/dmi.h>
16 #include <linux/firmware.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/acpi.h>
30 #include <asm/unaligned.h>
32 struct goodix_ts_data
;
34 struct goodix_chip_data
{
37 int (*check_config
)(struct goodix_ts_data
*, const struct firmware
*);
40 struct goodix_ts_data
{
41 struct i2c_client
*client
;
42 struct input_dev
*input_dev
;
43 const struct goodix_chip_data
*chip
;
44 struct touchscreen_properties prop
;
45 unsigned int max_touch_num
;
46 unsigned int int_trigger_type
;
47 struct regulator
*avdd28
;
48 struct regulator
*vddio
;
49 struct gpio_desc
*gpiod_int
;
50 struct gpio_desc
*gpiod_rst
;
54 struct completion firmware_loading_complete
;
55 unsigned long irq_flags
;
58 #define GOODIX_GPIO_INT_NAME "irq"
59 #define GOODIX_GPIO_RST_NAME "reset"
61 #define GOODIX_MAX_HEIGHT 4096
62 #define GOODIX_MAX_WIDTH 4096
63 #define GOODIX_INT_TRIGGER 1
64 #define GOODIX_CONTACT_SIZE 8
65 #define GOODIX_MAX_CONTACTS 10
67 #define GOODIX_CONFIG_MAX_LENGTH 240
68 #define GOODIX_CONFIG_911_LENGTH 186
69 #define GOODIX_CONFIG_967_LENGTH 228
71 /* Register defines */
72 #define GOODIX_REG_COMMAND 0x8040
73 #define GOODIX_CMD_SCREEN_OFF 0x05
75 #define GOODIX_READ_COOR_ADDR 0x814E
76 #define GOODIX_GT1X_REG_CONFIG_DATA 0x8050
77 #define GOODIX_GT9X_REG_CONFIG_DATA 0x8047
78 #define GOODIX_REG_ID 0x8140
80 #define GOODIX_BUFFER_STATUS_READY BIT(7)
81 #define GOODIX_BUFFER_STATUS_TIMEOUT 20
83 #define RESOLUTION_LOC 1
84 #define MAX_CONTACTS_LOC 5
87 static int goodix_check_cfg_8(struct goodix_ts_data
*ts
,
88 const struct firmware
*cfg
);
89 static int goodix_check_cfg_16(struct goodix_ts_data
*ts
,
90 const struct firmware
*cfg
);
92 static const struct goodix_chip_data gt1x_chip_data
= {
93 .config_addr
= GOODIX_GT1X_REG_CONFIG_DATA
,
94 .config_len
= GOODIX_CONFIG_MAX_LENGTH
,
95 .check_config
= goodix_check_cfg_16
,
98 static const struct goodix_chip_data gt911_chip_data
= {
99 .config_addr
= GOODIX_GT9X_REG_CONFIG_DATA
,
100 .config_len
= GOODIX_CONFIG_911_LENGTH
,
101 .check_config
= goodix_check_cfg_8
,
104 static const struct goodix_chip_data gt967_chip_data
= {
105 .config_addr
= GOODIX_GT9X_REG_CONFIG_DATA
,
106 .config_len
= GOODIX_CONFIG_967_LENGTH
,
107 .check_config
= goodix_check_cfg_8
,
110 static const struct goodix_chip_data gt9x_chip_data
= {
111 .config_addr
= GOODIX_GT9X_REG_CONFIG_DATA
,
112 .config_len
= GOODIX_CONFIG_MAX_LENGTH
,
113 .check_config
= goodix_check_cfg_8
,
116 static const unsigned long goodix_irq_flags
[] = {
117 IRQ_TYPE_EDGE_RISING
,
118 IRQ_TYPE_EDGE_FALLING
,
124 * Those tablets have their coordinates origin at the bottom right
125 * of the tablet, as if rotated 180 degrees
127 static const struct dmi_system_id rotated_screen
[] = {
128 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
130 .ident
= "WinBook TW100",
132 DMI_MATCH(DMI_SYS_VENDOR
, "WinBook"),
133 DMI_MATCH(DMI_PRODUCT_NAME
, "TW100")
137 .ident
= "WinBook TW700",
139 DMI_MATCH(DMI_SYS_VENDOR
, "WinBook"),
140 DMI_MATCH(DMI_PRODUCT_NAME
, "TW700")
148 * goodix_i2c_read - read data from a register of the i2c slave device.
150 * @client: i2c device.
151 * @reg: the register to read from.
152 * @buf: raw write data buffer.
153 * @len: length of the buffer to write
155 static int goodix_i2c_read(struct i2c_client
*client
,
156 u16 reg
, u8
*buf
, int len
)
158 struct i2c_msg msgs
[2];
159 __be16 wbuf
= cpu_to_be16(reg
);
163 msgs
[0].addr
= client
->addr
;
165 msgs
[0].buf
= (u8
*)&wbuf
;
167 msgs
[1].flags
= I2C_M_RD
;
168 msgs
[1].addr
= client
->addr
;
172 ret
= i2c_transfer(client
->adapter
, msgs
, 2);
173 return ret
< 0 ? ret
: (ret
!= ARRAY_SIZE(msgs
) ? -EIO
: 0);
177 * goodix_i2c_write - write data to a register of the i2c slave device.
179 * @client: i2c device.
180 * @reg: the register to write to.
181 * @buf: raw data buffer to write.
182 * @len: length of the buffer to write
184 static int goodix_i2c_write(struct i2c_client
*client
, u16 reg
, const u8
*buf
,
191 addr_buf
= kmalloc(len
+ 2, GFP_KERNEL
);
195 addr_buf
[0] = reg
>> 8;
196 addr_buf
[1] = reg
& 0xFF;
197 memcpy(&addr_buf
[2], buf
, len
);
200 msg
.addr
= client
->addr
;
204 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
206 return ret
< 0 ? ret
: (ret
!= 1 ? -EIO
: 0);
209 static int goodix_i2c_write_u8(struct i2c_client
*client
, u16 reg
, u8 value
)
211 return goodix_i2c_write(client
, reg
, &value
, sizeof(value
));
214 static const struct goodix_chip_data
*goodix_get_chip_data(u16 id
)
220 return >1x_chip_data
;
227 return >911_chip_data
;
231 return >967_chip_data
;
234 return >9x_chip_data
;
238 static int goodix_ts_read_input_report(struct goodix_ts_data
*ts
, u8
*data
)
240 unsigned long max_timeout
;
245 * The 'buffer status' bit, which indicates that the data is valid, is
246 * not set as soon as the interrupt is raised, but slightly after.
247 * This takes around 10 ms to happen, so we poll for 20 ms.
249 max_timeout
= jiffies
+ msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT
);
251 error
= goodix_i2c_read(ts
->client
, GOODIX_READ_COOR_ADDR
,
252 data
, GOODIX_CONTACT_SIZE
+ 1);
254 dev_err(&ts
->client
->dev
, "I2C transfer error: %d\n",
259 if (data
[0] & GOODIX_BUFFER_STATUS_READY
) {
260 touch_num
= data
[0] & 0x0f;
261 if (touch_num
> ts
->max_touch_num
)
265 data
+= 1 + GOODIX_CONTACT_SIZE
;
266 error
= goodix_i2c_read(ts
->client
,
267 GOODIX_READ_COOR_ADDR
+
268 1 + GOODIX_CONTACT_SIZE
,
270 GOODIX_CONTACT_SIZE
*
279 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
280 } while (time_before(jiffies
, max_timeout
));
283 * The Goodix panel will send spurious interrupts after a
284 * 'finger up' event, which will always cause a timeout.
289 static void goodix_ts_report_touch(struct goodix_ts_data
*ts
, u8
*coor_data
)
291 int id
= coor_data
[0] & 0x0F;
292 int input_x
= get_unaligned_le16(&coor_data
[1]);
293 int input_y
= get_unaligned_le16(&coor_data
[3]);
294 int input_w
= get_unaligned_le16(&coor_data
[5]);
296 input_mt_slot(ts
->input_dev
, id
);
297 input_mt_report_slot_state(ts
->input_dev
, MT_TOOL_FINGER
, true);
298 touchscreen_report_pos(ts
->input_dev
, &ts
->prop
,
299 input_x
, input_y
, true);
300 input_report_abs(ts
->input_dev
, ABS_MT_TOUCH_MAJOR
, input_w
);
301 input_report_abs(ts
->input_dev
, ABS_MT_WIDTH_MAJOR
, input_w
);
305 * goodix_process_events - Process incoming events
307 * @ts: our goodix_ts_data pointer
309 * Called when the IRQ is triggered. Read the current device state, and push
310 * the input events to the user space.
312 static void goodix_process_events(struct goodix_ts_data
*ts
)
314 u8 point_data
[1 + GOODIX_CONTACT_SIZE
* GOODIX_MAX_CONTACTS
];
318 touch_num
= goodix_ts_read_input_report(ts
, point_data
);
323 * Bit 4 of the first byte reports the status of the capacitive
324 * Windows/Home button.
326 input_report_key(ts
->input_dev
, KEY_LEFTMETA
, point_data
[0] & BIT(4));
328 for (i
= 0; i
< touch_num
; i
++)
329 goodix_ts_report_touch(ts
,
330 &point_data
[1 + GOODIX_CONTACT_SIZE
* i
]);
332 input_mt_sync_frame(ts
->input_dev
);
333 input_sync(ts
->input_dev
);
337 * goodix_ts_irq_handler - The IRQ handler
339 * @irq: interrupt number.
340 * @dev_id: private data pointer.
342 static irqreturn_t
goodix_ts_irq_handler(int irq
, void *dev_id
)
344 struct goodix_ts_data
*ts
= dev_id
;
346 goodix_process_events(ts
);
348 if (goodix_i2c_write_u8(ts
->client
, GOODIX_READ_COOR_ADDR
, 0) < 0)
349 dev_err(&ts
->client
->dev
, "I2C write end_cmd error\n");
354 static void goodix_free_irq(struct goodix_ts_data
*ts
)
356 devm_free_irq(&ts
->client
->dev
, ts
->client
->irq
, ts
);
359 static int goodix_request_irq(struct goodix_ts_data
*ts
)
361 return devm_request_threaded_irq(&ts
->client
->dev
, ts
->client
->irq
,
362 NULL
, goodix_ts_irq_handler
,
363 ts
->irq_flags
, ts
->client
->name
, ts
);
366 static int goodix_check_cfg_8(struct goodix_ts_data
*ts
,
367 const struct firmware
*cfg
)
369 int i
, raw_cfg_len
= cfg
->size
- 2;
372 for (i
= 0; i
< raw_cfg_len
; i
++)
373 check_sum
+= cfg
->data
[i
];
374 check_sum
= (~check_sum
) + 1;
375 if (check_sum
!= cfg
->data
[raw_cfg_len
]) {
376 dev_err(&ts
->client
->dev
,
377 "The checksum of the config fw is not correct");
381 if (cfg
->data
[raw_cfg_len
+ 1] != 1) {
382 dev_err(&ts
->client
->dev
,
383 "Config fw must have Config_Fresh register set");
390 static int goodix_check_cfg_16(struct goodix_ts_data
*ts
,
391 const struct firmware
*cfg
)
393 int i
, raw_cfg_len
= cfg
->size
- 3;
396 for (i
= 0; i
< raw_cfg_len
; i
+= 2)
397 check_sum
+= get_unaligned_be16(&cfg
->data
[i
]);
398 check_sum
= (~check_sum
) + 1;
399 if (check_sum
!= get_unaligned_be16(&cfg
->data
[raw_cfg_len
])) {
400 dev_err(&ts
->client
->dev
,
401 "The checksum of the config fw is not correct");
405 if (cfg
->data
[raw_cfg_len
+ 2] != 1) {
406 dev_err(&ts
->client
->dev
,
407 "Config fw must have Config_Fresh register set");
415 * goodix_check_cfg - Checks if config fw is valid
417 * @ts: goodix_ts_data pointer
418 * @cfg: firmware config data
420 static int goodix_check_cfg(struct goodix_ts_data
*ts
,
421 const struct firmware
*cfg
)
423 if (cfg
->size
> GOODIX_CONFIG_MAX_LENGTH
) {
424 dev_err(&ts
->client
->dev
,
425 "The length of the config fw is not correct");
429 return ts
->chip
->check_config(ts
, cfg
);
433 * goodix_send_cfg - Write fw config to device
435 * @ts: goodix_ts_data pointer
436 * @cfg: config firmware to write to device
438 static int goodix_send_cfg(struct goodix_ts_data
*ts
,
439 const struct firmware
*cfg
)
443 error
= goodix_check_cfg(ts
, cfg
);
447 error
= goodix_i2c_write(ts
->client
, ts
->chip
->config_addr
, cfg
->data
,
450 dev_err(&ts
->client
->dev
, "Failed to write config data: %d",
454 dev_dbg(&ts
->client
->dev
, "Config sent successfully.");
456 /* Let the firmware reconfigure itself, so sleep for 10ms */
457 usleep_range(10000, 11000);
462 static int goodix_int_sync(struct goodix_ts_data
*ts
)
466 error
= gpiod_direction_output(ts
->gpiod_int
, 0);
470 msleep(50); /* T5: 50ms */
472 error
= gpiod_direction_input(ts
->gpiod_int
);
480 * goodix_reset - Reset device during power on
482 * @ts: goodix_ts_data pointer
484 static int goodix_reset(struct goodix_ts_data
*ts
)
488 /* begin select I2C slave addr */
489 error
= gpiod_direction_output(ts
->gpiod_rst
, 0);
493 msleep(20); /* T2: > 10ms */
495 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
496 error
= gpiod_direction_output(ts
->gpiod_int
, ts
->client
->addr
== 0x14);
500 usleep_range(100, 2000); /* T3: > 100us */
502 error
= gpiod_direction_output(ts
->gpiod_rst
, 1);
506 usleep_range(6000, 10000); /* T4: > 5ms */
508 /* end select I2C slave addr */
509 error
= gpiod_direction_input(ts
->gpiod_rst
);
513 error
= goodix_int_sync(ts
);
521 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
523 * @ts: goodix_ts_data pointer
525 static int goodix_get_gpio_config(struct goodix_ts_data
*ts
)
529 struct gpio_desc
*gpiod
;
533 dev
= &ts
->client
->dev
;
535 ts
->avdd28
= devm_regulator_get(dev
, "AVDD28");
536 if (IS_ERR(ts
->avdd28
)) {
537 error
= PTR_ERR(ts
->avdd28
);
538 if (error
!= -EPROBE_DEFER
)
540 "Failed to get AVDD28 regulator: %d\n", error
);
544 ts
->vddio
= devm_regulator_get(dev
, "VDDIO");
545 if (IS_ERR(ts
->vddio
)) {
546 error
= PTR_ERR(ts
->vddio
);
547 if (error
!= -EPROBE_DEFER
)
549 "Failed to get VDDIO regulator: %d\n", error
);
553 /* Get the interrupt GPIO pin number */
554 gpiod
= devm_gpiod_get_optional(dev
, GOODIX_GPIO_INT_NAME
, GPIOD_IN
);
556 error
= PTR_ERR(gpiod
);
557 if (error
!= -EPROBE_DEFER
)
558 dev_dbg(dev
, "Failed to get %s GPIO: %d\n",
559 GOODIX_GPIO_INT_NAME
, error
);
563 ts
->gpiod_int
= gpiod
;
565 /* Get the reset line GPIO pin number */
566 gpiod
= devm_gpiod_get_optional(dev
, GOODIX_GPIO_RST_NAME
, GPIOD_IN
);
568 error
= PTR_ERR(gpiod
);
569 if (error
!= -EPROBE_DEFER
)
570 dev_dbg(dev
, "Failed to get %s GPIO: %d\n",
571 GOODIX_GPIO_RST_NAME
, error
);
575 ts
->gpiod_rst
= gpiod
;
581 * goodix_read_config - Read the embedded configuration of the panel
583 * @ts: our goodix_ts_data pointer
585 * Must be called during probe
587 static void goodix_read_config(struct goodix_ts_data
*ts
)
589 u8 config
[GOODIX_CONFIG_MAX_LENGTH
];
593 error
= goodix_i2c_read(ts
->client
, ts
->chip
->config_addr
,
594 config
, ts
->chip
->config_len
);
596 dev_warn(&ts
->client
->dev
, "Error reading config: %d\n",
598 ts
->int_trigger_type
= GOODIX_INT_TRIGGER
;
599 ts
->max_touch_num
= GOODIX_MAX_CONTACTS
;
603 ts
->int_trigger_type
= config
[TRIGGER_LOC
] & 0x03;
604 ts
->max_touch_num
= config
[MAX_CONTACTS_LOC
] & 0x0f;
606 x_max
= get_unaligned_le16(&config
[RESOLUTION_LOC
]);
607 y_max
= get_unaligned_le16(&config
[RESOLUTION_LOC
+ 2]);
608 if (x_max
&& y_max
) {
609 input_abs_set_max(ts
->input_dev
, ABS_MT_POSITION_X
, x_max
- 1);
610 input_abs_set_max(ts
->input_dev
, ABS_MT_POSITION_Y
, y_max
- 1);
615 * goodix_read_version - Read goodix touchscreen version
617 * @ts: our goodix_ts_data pointer
619 static int goodix_read_version(struct goodix_ts_data
*ts
)
625 error
= goodix_i2c_read(ts
->client
, GOODIX_REG_ID
, buf
, sizeof(buf
));
627 dev_err(&ts
->client
->dev
, "read version failed: %d\n", error
);
631 memcpy(id_str
, buf
, 4);
633 if (kstrtou16(id_str
, 10, &ts
->id
))
636 ts
->version
= get_unaligned_le16(&buf
[4]);
638 dev_info(&ts
->client
->dev
, "ID %d, version: %04x\n", ts
->id
,
645 * goodix_i2c_test - I2C test function to check if the device answers.
647 * @client: the i2c client
649 static int goodix_i2c_test(struct i2c_client
*client
)
655 while (retry
++ < 2) {
656 error
= goodix_i2c_read(client
, GOODIX_REG_ID
,
661 dev_err(&client
->dev
, "i2c test failed attempt %d: %d\n",
670 * goodix_configure_dev - Finish device initialization
672 * @ts: our goodix_ts_data pointer
674 * Must be called from probe to finish initialization of the device.
675 * Contains the common initialization code for both devices that
676 * declare gpio pins and devices that do not. It is either called
677 * directly from probe or from request_firmware_wait callback.
679 static int goodix_configure_dev(struct goodix_ts_data
*ts
)
683 ts
->int_trigger_type
= GOODIX_INT_TRIGGER
;
684 ts
->max_touch_num
= GOODIX_MAX_CONTACTS
;
686 ts
->input_dev
= devm_input_allocate_device(&ts
->client
->dev
);
687 if (!ts
->input_dev
) {
688 dev_err(&ts
->client
->dev
, "Failed to allocate input device.");
692 ts
->input_dev
->name
= "Goodix Capacitive TouchScreen";
693 ts
->input_dev
->phys
= "input/ts";
694 ts
->input_dev
->id
.bustype
= BUS_I2C
;
695 ts
->input_dev
->id
.vendor
= 0x0416;
696 ts
->input_dev
->id
.product
= ts
->id
;
697 ts
->input_dev
->id
.version
= ts
->version
;
699 /* Capacitive Windows/Home button on some devices */
700 input_set_capability(ts
->input_dev
, EV_KEY
, KEY_LEFTMETA
);
702 input_set_capability(ts
->input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
703 input_set_capability(ts
->input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
704 input_set_abs_params(ts
->input_dev
, ABS_MT_WIDTH_MAJOR
, 0, 255, 0, 0);
705 input_set_abs_params(ts
->input_dev
, ABS_MT_TOUCH_MAJOR
, 0, 255, 0, 0);
707 /* Read configuration and apply touchscreen parameters */
708 goodix_read_config(ts
);
710 /* Try overriding touchscreen parameters via device properties */
711 touchscreen_parse_properties(ts
->input_dev
, true, &ts
->prop
);
713 if (!ts
->prop
.max_x
|| !ts
->prop
.max_y
|| !ts
->max_touch_num
) {
714 dev_err(&ts
->client
->dev
,
715 "Invalid config (%d, %d, %d), using defaults\n",
716 ts
->prop
.max_x
, ts
->prop
.max_y
, ts
->max_touch_num
);
717 ts
->prop
.max_x
= GOODIX_MAX_WIDTH
- 1;
718 ts
->prop
.max_y
= GOODIX_MAX_HEIGHT
- 1;
719 ts
->max_touch_num
= GOODIX_MAX_CONTACTS
;
720 input_abs_set_max(ts
->input_dev
,
721 ABS_MT_POSITION_X
, ts
->prop
.max_x
);
722 input_abs_set_max(ts
->input_dev
,
723 ABS_MT_POSITION_Y
, ts
->prop
.max_y
);
726 if (dmi_check_system(rotated_screen
)) {
727 ts
->prop
.invert_x
= true;
728 ts
->prop
.invert_y
= true;
729 dev_dbg(&ts
->client
->dev
,
730 "Applying '180 degrees rotated screen' quirk\n");
733 error
= input_mt_init_slots(ts
->input_dev
, ts
->max_touch_num
,
734 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
736 dev_err(&ts
->client
->dev
,
737 "Failed to initialize MT slots: %d", error
);
741 error
= input_register_device(ts
->input_dev
);
743 dev_err(&ts
->client
->dev
,
744 "Failed to register input device: %d", error
);
748 ts
->irq_flags
= goodix_irq_flags
[ts
->int_trigger_type
] | IRQF_ONESHOT
;
749 error
= goodix_request_irq(ts
);
751 dev_err(&ts
->client
->dev
, "request IRQ failed: %d\n", error
);
759 * goodix_config_cb - Callback to finish device init
761 * @ts: our goodix_ts_data pointer
763 * request_firmware_wait callback that finishes
764 * initialization of the device.
766 static void goodix_config_cb(const struct firmware
*cfg
, void *ctx
)
768 struct goodix_ts_data
*ts
= ctx
;
772 /* send device configuration to the firmware */
773 error
= goodix_send_cfg(ts
, cfg
);
775 goto err_release_cfg
;
778 goodix_configure_dev(ts
);
781 release_firmware(cfg
);
782 complete_all(&ts
->firmware_loading_complete
);
785 static void goodix_disable_regulators(void *arg
)
787 struct goodix_ts_data
*ts
= arg
;
789 regulator_disable(ts
->vddio
);
790 regulator_disable(ts
->avdd28
);
793 static int goodix_ts_probe(struct i2c_client
*client
,
794 const struct i2c_device_id
*id
)
796 struct goodix_ts_data
*ts
;
799 dev_dbg(&client
->dev
, "I2C Address: 0x%02x\n", client
->addr
);
801 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
802 dev_err(&client
->dev
, "I2C check functionality failed.\n");
806 ts
= devm_kzalloc(&client
->dev
, sizeof(*ts
), GFP_KERNEL
);
811 i2c_set_clientdata(client
, ts
);
812 init_completion(&ts
->firmware_loading_complete
);
814 error
= goodix_get_gpio_config(ts
);
818 /* power up the controller */
819 error
= regulator_enable(ts
->avdd28
);
821 dev_err(&client
->dev
,
822 "Failed to enable AVDD28 regulator: %d\n",
827 error
= regulator_enable(ts
->vddio
);
829 dev_err(&client
->dev
,
830 "Failed to enable VDDIO regulator: %d\n",
832 regulator_disable(ts
->avdd28
);
836 error
= devm_add_action_or_reset(&client
->dev
,
837 goodix_disable_regulators
, ts
);
841 if (ts
->gpiod_int
&& ts
->gpiod_rst
) {
842 /* reset the controller */
843 error
= goodix_reset(ts
);
845 dev_err(&client
->dev
, "Controller reset failed.\n");
850 error
= goodix_i2c_test(client
);
852 dev_err(&client
->dev
, "I2C communication failure: %d\n", error
);
856 error
= goodix_read_version(ts
);
858 dev_err(&client
->dev
, "Read version failed.\n");
862 ts
->chip
= goodix_get_chip_data(ts
->id
);
864 if (ts
->gpiod_int
&& ts
->gpiod_rst
) {
865 /* update device config */
866 ts
->cfg_name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
867 "goodix_%d_cfg.bin", ts
->id
);
871 error
= request_firmware_nowait(THIS_MODULE
, true, ts
->cfg_name
,
872 &client
->dev
, GFP_KERNEL
, ts
,
875 dev_err(&client
->dev
,
876 "Failed to invoke firmware loader: %d\n",
883 error
= goodix_configure_dev(ts
);
891 static int goodix_ts_remove(struct i2c_client
*client
)
893 struct goodix_ts_data
*ts
= i2c_get_clientdata(client
);
895 if (ts
->gpiod_int
&& ts
->gpiod_rst
)
896 wait_for_completion(&ts
->firmware_loading_complete
);
901 static int __maybe_unused
goodix_suspend(struct device
*dev
)
903 struct i2c_client
*client
= to_i2c_client(dev
);
904 struct goodix_ts_data
*ts
= i2c_get_clientdata(client
);
907 /* We need gpio pins to suspend/resume */
908 if (!ts
->gpiod_int
|| !ts
->gpiod_rst
) {
909 disable_irq(client
->irq
);
913 wait_for_completion(&ts
->firmware_loading_complete
);
915 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
918 /* Output LOW on the INT pin for 5 ms */
919 error
= gpiod_direction_output(ts
->gpiod_int
, 0);
921 goodix_request_irq(ts
);
925 usleep_range(5000, 6000);
927 error
= goodix_i2c_write_u8(ts
->client
, GOODIX_REG_COMMAND
,
928 GOODIX_CMD_SCREEN_OFF
);
930 dev_err(&ts
->client
->dev
, "Screen off command failed\n");
931 gpiod_direction_input(ts
->gpiod_int
);
932 goodix_request_irq(ts
);
937 * The datasheet specifies that the interval between sending screen-off
938 * command and wake-up should be longer than 58 ms. To avoid waking up
939 * sooner, delay 58ms here.
945 static int __maybe_unused
goodix_resume(struct device
*dev
)
947 struct i2c_client
*client
= to_i2c_client(dev
);
948 struct goodix_ts_data
*ts
= i2c_get_clientdata(client
);
951 if (!ts
->gpiod_int
|| !ts
->gpiod_rst
) {
952 enable_irq(client
->irq
);
957 * Exit sleep mode by outputting HIGH level to INT pin
960 error
= gpiod_direction_output(ts
->gpiod_int
, 1);
964 usleep_range(2000, 5000);
966 error
= goodix_int_sync(ts
);
970 error
= goodix_request_irq(ts
);
977 static SIMPLE_DEV_PM_OPS(goodix_pm_ops
, goodix_suspend
, goodix_resume
);
979 static const struct i2c_device_id goodix_ts_id
[] = {
980 { "GDIX1001:00", 0 },
983 MODULE_DEVICE_TABLE(i2c
, goodix_ts_id
);
986 static const struct acpi_device_id goodix_acpi_match
[] = {
991 MODULE_DEVICE_TABLE(acpi
, goodix_acpi_match
);
995 static const struct of_device_id goodix_of_match
[] = {
996 { .compatible
= "goodix,gt1151" },
997 { .compatible
= "goodix,gt5663" },
998 { .compatible
= "goodix,gt5688" },
999 { .compatible
= "goodix,gt911" },
1000 { .compatible
= "goodix,gt9110" },
1001 { .compatible
= "goodix,gt912" },
1002 { .compatible
= "goodix,gt927" },
1003 { .compatible
= "goodix,gt9271" },
1004 { .compatible
= "goodix,gt928" },
1005 { .compatible
= "goodix,gt967" },
1008 MODULE_DEVICE_TABLE(of
, goodix_of_match
);
1011 static struct i2c_driver goodix_ts_driver
= {
1012 .probe
= goodix_ts_probe
,
1013 .remove
= goodix_ts_remove
,
1014 .id_table
= goodix_ts_id
,
1016 .name
= "Goodix-TS",
1017 .acpi_match_table
= ACPI_PTR(goodix_acpi_match
),
1018 .of_match_table
= of_match_ptr(goodix_of_match
),
1019 .pm
= &goodix_pm_ops
,
1022 module_i2c_driver(goodix_ts_driver
);
1024 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1025 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1026 MODULE_DESCRIPTION("Goodix touchscreen driver");
1027 MODULE_LICENSE("GPL v2");