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