Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Goodix Touchscreens
4 *
5 * Copyright (c) 2014 Red Hat Inc.
6 * Copyright (c) 2015 K. Merker <merker@debian.org>
7 *
8 * This code is based on gt9xx.c authored by andrew@goodix.com:
9 *
10 * 2010 - 2012 Goodix Technology.
11 */
12
13
14#include <linux/kernel.h>
15#include <linux/dmi.h>
16#include <linux/firmware.h>
17#include <linux/module.h>
18#include <linux/delay.h>
19#include <linux/irq.h>
20#include <linux/interrupt.h>
21#include <linux/platform_data/x86/soc.h>
22#include <linux/slab.h>
23#include <linux/acpi.h>
24#include <linux/of.h>
25#include <linux/unaligned.h>
26#include "goodix.h"
27
28#define GOODIX_GPIO_INT_NAME "irq"
29#define GOODIX_GPIO_RST_NAME "reset"
30
31#define GOODIX_MAX_HEIGHT 4096
32#define GOODIX_MAX_WIDTH 4096
33#define GOODIX_INT_TRIGGER 1
34#define GOODIX_CONTACT_SIZE 8
35#define GOODIX_MAX_CONTACT_SIZE 9
36#define GOODIX_MAX_CONTACTS 10
37
38#define GOODIX_CONFIG_MIN_LENGTH 186
39#define GOODIX_CONFIG_911_LENGTH 186
40#define GOODIX_CONFIG_967_LENGTH 228
41#define GOODIX_CONFIG_GT9X_LENGTH 240
42
43#define GOODIX_BUFFER_STATUS_READY BIT(7)
44#define GOODIX_HAVE_KEY BIT(4)
45#define GOODIX_BUFFER_STATUS_TIMEOUT 20
46
47#define RESOLUTION_LOC 1
48#define MAX_CONTACTS_LOC 5
49#define TRIGGER_LOC 6
50
51#define GOODIX_POLL_INTERVAL_MS 17 /* 17ms = 60fps */
52
53/* Our special handling for GPIO accesses through ACPI is x86 specific */
54#if defined CONFIG_X86 && defined CONFIG_ACPI
55#define ACPI_GPIO_SUPPORT
56#endif
57
58struct goodix_chip_id {
59 const char *id;
60 const struct goodix_chip_data *data;
61};
62
63static int goodix_check_cfg_8(struct goodix_ts_data *ts,
64 const u8 *cfg, int len);
65static int goodix_check_cfg_16(struct goodix_ts_data *ts,
66 const u8 *cfg, int len);
67static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
68static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
69
70static const struct goodix_chip_data gt1x_chip_data = {
71 .config_addr = GOODIX_GT1X_REG_CONFIG_DATA,
72 .config_len = GOODIX_CONFIG_GT9X_LENGTH,
73 .check_config = goodix_check_cfg_16,
74 .calc_config_checksum = goodix_calc_cfg_checksum_16,
75};
76
77static const struct goodix_chip_data gt911_chip_data = {
78 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
79 .config_len = GOODIX_CONFIG_911_LENGTH,
80 .check_config = goodix_check_cfg_8,
81 .calc_config_checksum = goodix_calc_cfg_checksum_8,
82};
83
84static const struct goodix_chip_data gt967_chip_data = {
85 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
86 .config_len = GOODIX_CONFIG_967_LENGTH,
87 .check_config = goodix_check_cfg_8,
88 .calc_config_checksum = goodix_calc_cfg_checksum_8,
89};
90
91static const struct goodix_chip_data gt9x_chip_data = {
92 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
93 .config_len = GOODIX_CONFIG_GT9X_LENGTH,
94 .check_config = goodix_check_cfg_8,
95 .calc_config_checksum = goodix_calc_cfg_checksum_8,
96};
97
98static const struct goodix_chip_id goodix_chip_ids[] = {
99 { .id = "1151", .data = >1x_chip_data },
100 { .id = "1158", .data = >1x_chip_data },
101 { .id = "5663", .data = >1x_chip_data },
102 { .id = "5688", .data = >1x_chip_data },
103 { .id = "917S", .data = >1x_chip_data },
104 { .id = "9286", .data = >1x_chip_data },
105
106 { .id = "911", .data = >911_chip_data },
107 { .id = "9271", .data = >911_chip_data },
108 { .id = "9110", .data = >911_chip_data },
109 { .id = "9111", .data = >911_chip_data },
110 { .id = "927", .data = >911_chip_data },
111 { .id = "928", .data = >911_chip_data },
112
113 { .id = "912", .data = >967_chip_data },
114 { .id = "9147", .data = >967_chip_data },
115 { .id = "967", .data = >967_chip_data },
116 { }
117};
118
119static const unsigned long goodix_irq_flags[] = {
120 IRQ_TYPE_EDGE_RISING,
121 IRQ_TYPE_EDGE_FALLING,
122 IRQ_TYPE_LEVEL_LOW,
123 IRQ_TYPE_LEVEL_HIGH,
124};
125
126static const struct dmi_system_id nine_bytes_report[] = {
127#if defined(CONFIG_DMI) && defined(CONFIG_X86)
128 {
129 /* Lenovo Yoga Book X90F / X90L */
130 .matches = {
131 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
132 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "CHERRYVIEW D1 PLATFORM"),
133 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "YETI-11"),
134 }
135 },
136 {
137 /* Lenovo Yoga Book X91F / X91L */
138 .matches = {
139 /* Non exact match to match F + L versions */
140 DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X91"),
141 }
142 },
143#endif
144 {}
145};
146
147/*
148 * Those tablets have their x coordinate inverted
149 */
150static const struct dmi_system_id inverted_x_screen[] = {
151#if defined(CONFIG_DMI) && defined(CONFIG_X86)
152 {
153 .ident = "Cube I15-TC",
154 .matches = {
155 DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
156 DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
157 },
158 },
159#endif
160 {}
161};
162
163/**
164 * goodix_i2c_read - read data from a register of the i2c slave device.
165 *
166 * @client: i2c device.
167 * @reg: the register to read from.
168 * @buf: raw write data buffer.
169 * @len: length of the buffer to write
170 */
171int goodix_i2c_read(struct i2c_client *client, u16 reg, u8 *buf, int len)
172{
173 struct i2c_msg msgs[2];
174 __be16 wbuf = cpu_to_be16(reg);
175 int ret;
176
177 msgs[0].flags = 0;
178 msgs[0].addr = client->addr;
179 msgs[0].len = 2;
180 msgs[0].buf = (u8 *)&wbuf;
181
182 msgs[1].flags = I2C_M_RD;
183 msgs[1].addr = client->addr;
184 msgs[1].len = len;
185 msgs[1].buf = buf;
186
187 ret = i2c_transfer(client->adapter, msgs, 2);
188 if (ret >= 0)
189 ret = (ret == ARRAY_SIZE(msgs) ? 0 : -EIO);
190
191 if (ret)
192 dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d\n",
193 len, reg, ret);
194 return ret;
195}
196
197/**
198 * goodix_i2c_write - write data to a register of the i2c slave device.
199 *
200 * @client: i2c device.
201 * @reg: the register to write to.
202 * @buf: raw data buffer to write.
203 * @len: length of the buffer to write
204 */
205int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf, int len)
206{
207 u8 *addr_buf;
208 struct i2c_msg msg;
209 int ret;
210
211 addr_buf = kmalloc(len + 2, GFP_KERNEL);
212 if (!addr_buf)
213 return -ENOMEM;
214
215 addr_buf[0] = reg >> 8;
216 addr_buf[1] = reg & 0xFF;
217 memcpy(&addr_buf[2], buf, len);
218
219 msg.flags = 0;
220 msg.addr = client->addr;
221 msg.buf = addr_buf;
222 msg.len = len + 2;
223
224 ret = i2c_transfer(client->adapter, &msg, 1);
225 if (ret >= 0)
226 ret = (ret == 1 ? 0 : -EIO);
227
228 kfree(addr_buf);
229
230 if (ret)
231 dev_err(&client->dev, "Error writing %d bytes to 0x%04x: %d\n",
232 len, reg, ret);
233 return ret;
234}
235
236int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
237{
238 return goodix_i2c_write(client, reg, &value, sizeof(value));
239}
240
241static const struct goodix_chip_data *goodix_get_chip_data(const char *id)
242{
243 unsigned int i;
244
245 for (i = 0; goodix_chip_ids[i].id; i++) {
246 if (!strcmp(goodix_chip_ids[i].id, id))
247 return goodix_chip_ids[i].data;
248 }
249
250 return >9x_chip_data;
251}
252
253static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
254{
255 unsigned long max_timeout;
256 int touch_num;
257 int error;
258 u16 addr = GOODIX_READ_COOR_ADDR;
259 /*
260 * We are going to read 1-byte header,
261 * ts->contact_size * max(1, touch_num) bytes of coordinates
262 * and 1-byte footer which contains the touch-key code.
263 */
264 const int header_contact_keycode_size = 1 + ts->contact_size + 1;
265
266 /*
267 * The 'buffer status' bit, which indicates that the data is valid, is
268 * not set as soon as the interrupt is raised, but slightly after.
269 * This takes around 10 ms to happen, so we poll for 20 ms.
270 */
271 max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
272 do {
273 error = goodix_i2c_read(ts->client, addr, data,
274 header_contact_keycode_size);
275 if (error)
276 return error;
277
278 if (data[0] & GOODIX_BUFFER_STATUS_READY) {
279 touch_num = data[0] & 0x0f;
280 if (touch_num > ts->max_touch_num)
281 return -EPROTO;
282
283 if (touch_num > 1) {
284 addr += header_contact_keycode_size;
285 data += header_contact_keycode_size;
286 error = goodix_i2c_read(ts->client,
287 addr, data,
288 ts->contact_size *
289 (touch_num - 1));
290 if (error)
291 return error;
292 }
293
294 return touch_num;
295 }
296
297 if (data[0] == 0 && ts->firmware_name) {
298 if (goodix_handle_fw_request(ts))
299 return 0;
300 }
301
302 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
303 } while (time_before(jiffies, max_timeout));
304
305 /*
306 * The Goodix panel will send spurious interrupts after a
307 * 'finger up' event, which will always cause a timeout.
308 */
309 return -ENOMSG;
310}
311
312static int goodix_create_pen_input(struct goodix_ts_data *ts)
313{
314 struct device *dev = &ts->client->dev;
315 struct input_dev *input;
316
317 input = devm_input_allocate_device(dev);
318 if (!input)
319 return -ENOMEM;
320
321 input_copy_abs(input, ABS_X, ts->input_dev, ABS_MT_POSITION_X);
322 input_copy_abs(input, ABS_Y, ts->input_dev, ABS_MT_POSITION_Y);
323 /*
324 * The resolution of these touchscreens is about 10 units/mm, the actual
325 * resolution does not matter much since we set INPUT_PROP_DIRECT.
326 * Userspace wants something here though, so just set it to 10 units/mm.
327 */
328 input_abs_set_res(input, ABS_X, 10);
329 input_abs_set_res(input, ABS_Y, 10);
330 input_set_abs_params(input, ABS_PRESSURE, 0, 255, 0, 0);
331
332 input_set_capability(input, EV_KEY, BTN_TOUCH);
333 input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
334 input_set_capability(input, EV_KEY, BTN_STYLUS);
335 input_set_capability(input, EV_KEY, BTN_STYLUS2);
336 __set_bit(INPUT_PROP_DIRECT, input->propbit);
337
338 input->name = "Goodix Active Pen";
339 input->phys = "input/pen";
340 input->id.bustype = BUS_I2C;
341 input->id.vendor = 0x0416;
342 if (kstrtou16(ts->id, 10, &input->id.product))
343 input->id.product = 0x1001;
344 input->id.version = ts->version;
345
346 ts->input_pen = input;
347 return 0;
348}
349
350static void goodix_ts_report_pen_down(struct goodix_ts_data *ts, u8 *data)
351{
352 int input_x, input_y, input_w, error;
353 u8 key_value;
354
355 if (!ts->pen_input_registered) {
356 error = input_register_device(ts->input_pen);
357 ts->pen_input_registered = (error == 0) ? 1 : error;
358 }
359
360 if (ts->pen_input_registered < 0)
361 return;
362
363 if (ts->contact_size == 9) {
364 input_x = get_unaligned_le16(&data[4]);
365 input_y = get_unaligned_le16(&data[6]);
366 input_w = get_unaligned_le16(&data[8]);
367 } else {
368 input_x = get_unaligned_le16(&data[2]);
369 input_y = get_unaligned_le16(&data[4]);
370 input_w = get_unaligned_le16(&data[6]);
371 }
372
373 touchscreen_report_pos(ts->input_pen, &ts->prop, input_x, input_y, false);
374 input_report_abs(ts->input_pen, ABS_PRESSURE, input_w);
375
376 input_report_key(ts->input_pen, BTN_TOUCH, 1);
377 input_report_key(ts->input_pen, BTN_TOOL_PEN, 1);
378
379 if (data[0] & GOODIX_HAVE_KEY) {
380 key_value = data[1 + ts->contact_size];
381 input_report_key(ts->input_pen, BTN_STYLUS, key_value & 0x10);
382 input_report_key(ts->input_pen, BTN_STYLUS2, key_value & 0x20);
383 } else {
384 input_report_key(ts->input_pen, BTN_STYLUS, 0);
385 input_report_key(ts->input_pen, BTN_STYLUS2, 0);
386 }
387
388 input_sync(ts->input_pen);
389}
390
391static void goodix_ts_report_pen_up(struct goodix_ts_data *ts)
392{
393 if (!ts->input_pen)
394 return;
395
396 input_report_key(ts->input_pen, BTN_TOUCH, 0);
397 input_report_key(ts->input_pen, BTN_TOOL_PEN, 0);
398 input_report_key(ts->input_pen, BTN_STYLUS, 0);
399 input_report_key(ts->input_pen, BTN_STYLUS2, 0);
400
401 input_sync(ts->input_pen);
402}
403
404static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
405{
406 int id = coor_data[0] & 0x0F;
407 int input_x = get_unaligned_le16(&coor_data[1]);
408 int input_y = get_unaligned_le16(&coor_data[3]);
409 int input_w = get_unaligned_le16(&coor_data[5]);
410
411 input_mt_slot(ts->input_dev, id);
412 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
413 touchscreen_report_pos(ts->input_dev, &ts->prop,
414 input_x, input_y, true);
415 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
416 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
417}
418
419static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
420{
421 int id = coor_data[1] & 0x0F;
422 int input_x = get_unaligned_le16(&coor_data[3]);
423 int input_y = get_unaligned_le16(&coor_data[5]);
424 int input_w = get_unaligned_le16(&coor_data[7]);
425
426 input_mt_slot(ts->input_dev, id);
427 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
428 touchscreen_report_pos(ts->input_dev, &ts->prop,
429 input_x, input_y, true);
430 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
431 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
432}
433
434static void goodix_ts_release_keys(struct goodix_ts_data *ts)
435{
436 int i;
437
438 for (i = 0; i < GOODIX_MAX_KEYS; i++)
439 input_report_key(ts->input_dev, ts->keymap[i], 0);
440}
441
442static void goodix_ts_report_key(struct goodix_ts_data *ts, u8 *data)
443{
444 int touch_num;
445 u8 key_value;
446 int i;
447
448 if (data[0] & GOODIX_HAVE_KEY) {
449 touch_num = data[0] & 0x0f;
450 key_value = data[1 + ts->contact_size * touch_num];
451 for (i = 0; i < GOODIX_MAX_KEYS; i++)
452 if (key_value & BIT(i))
453 input_report_key(ts->input_dev,
454 ts->keymap[i], 1);
455 } else {
456 goodix_ts_release_keys(ts);
457 }
458}
459
460/**
461 * goodix_process_events - Process incoming events
462 *
463 * @ts: our goodix_ts_data pointer
464 *
465 * Called when the IRQ is triggered. Read the current device state, and push
466 * the input events to the user space.
467 */
468static void goodix_process_events(struct goodix_ts_data *ts)
469{
470 u8 point_data[2 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
471 int touch_num;
472 int i;
473
474 touch_num = goodix_ts_read_input_report(ts, point_data);
475 if (touch_num < 0)
476 return;
477
478 /* The pen being down is always reported as a single touch */
479 if (touch_num == 1 && (point_data[1] & 0x80)) {
480 goodix_ts_report_pen_down(ts, point_data);
481 goodix_ts_release_keys(ts);
482 goto sync; /* Release any previously registered touches */
483 } else {
484 goodix_ts_report_pen_up(ts);
485 }
486
487 goodix_ts_report_key(ts, point_data);
488
489 for (i = 0; i < touch_num; i++)
490 if (ts->contact_size == 9)
491 goodix_ts_report_touch_9b(ts,
492 &point_data[1 + ts->contact_size * i]);
493 else
494 goodix_ts_report_touch_8b(ts,
495 &point_data[1 + ts->contact_size * i]);
496
497sync:
498 input_mt_sync_frame(ts->input_dev);
499 input_sync(ts->input_dev);
500}
501
502static void goodix_ts_work_i2c_poll(struct input_dev *input)
503{
504 struct goodix_ts_data *ts = input_get_drvdata(input);
505
506 goodix_process_events(ts);
507 goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
508}
509
510/**
511 * goodix_ts_irq_handler - The IRQ handler
512 *
513 * @irq: interrupt number.
514 * @dev_id: private data pointer.
515 */
516static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
517{
518 struct goodix_ts_data *ts = dev_id;
519
520 goodix_process_events(ts);
521 goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0);
522
523 return IRQ_HANDLED;
524}
525
526static void goodix_enable_irq(struct goodix_ts_data *ts)
527{
528 if (ts->client->irq)
529 enable_irq(ts->client->irq);
530}
531
532static void goodix_disable_irq(struct goodix_ts_data *ts)
533{
534 if (ts->client->irq)
535 disable_irq(ts->client->irq);
536}
537
538static void goodix_free_irq(struct goodix_ts_data *ts)
539{
540 if (ts->client->irq)
541 devm_free_irq(&ts->client->dev, ts->client->irq, ts);
542}
543
544static int goodix_request_irq(struct goodix_ts_data *ts)
545{
546 if (!ts->client->irq)
547 return 0;
548
549 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
550 NULL, goodix_ts_irq_handler,
551 ts->irq_flags, ts->client->name, ts);
552}
553
554static int goodix_check_cfg_8(struct goodix_ts_data *ts, const u8 *cfg, int len)
555{
556 int i, raw_cfg_len = len - 2;
557 u8 check_sum = 0;
558
559 for (i = 0; i < raw_cfg_len; i++)
560 check_sum += cfg[i];
561 check_sum = (~check_sum) + 1;
562 if (check_sum != cfg[raw_cfg_len]) {
563 dev_err(&ts->client->dev,
564 "The checksum of the config fw is not correct");
565 return -EINVAL;
566 }
567
568 if (cfg[raw_cfg_len + 1] != 1) {
569 dev_err(&ts->client->dev,
570 "Config fw must have Config_Fresh register set");
571 return -EINVAL;
572 }
573
574 return 0;
575}
576
577static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts)
578{
579 int i, raw_cfg_len = ts->chip->config_len - 2;
580 u8 check_sum = 0;
581
582 for (i = 0; i < raw_cfg_len; i++)
583 check_sum += ts->config[i];
584 check_sum = (~check_sum) + 1;
585
586 ts->config[raw_cfg_len] = check_sum;
587 ts->config[raw_cfg_len + 1] = 1; /* Set "config_fresh" bit */
588}
589
590static int goodix_check_cfg_16(struct goodix_ts_data *ts, const u8 *cfg,
591 int len)
592{
593 int i, raw_cfg_len = len - 3;
594 u16 check_sum = 0;
595
596 for (i = 0; i < raw_cfg_len; i += 2)
597 check_sum += get_unaligned_be16(&cfg[i]);
598 check_sum = (~check_sum) + 1;
599 if (check_sum != get_unaligned_be16(&cfg[raw_cfg_len])) {
600 dev_err(&ts->client->dev,
601 "The checksum of the config fw is not correct");
602 return -EINVAL;
603 }
604
605 if (cfg[raw_cfg_len + 2] != 1) {
606 dev_err(&ts->client->dev,
607 "Config fw must have Config_Fresh register set");
608 return -EINVAL;
609 }
610
611 return 0;
612}
613
614static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts)
615{
616 int i, raw_cfg_len = ts->chip->config_len - 3;
617 u16 check_sum = 0;
618
619 for (i = 0; i < raw_cfg_len; i += 2)
620 check_sum += get_unaligned_be16(&ts->config[i]);
621 check_sum = (~check_sum) + 1;
622
623 put_unaligned_be16(check_sum, &ts->config[raw_cfg_len]);
624 ts->config[raw_cfg_len + 2] = 1; /* Set "config_fresh" bit */
625}
626
627/**
628 * goodix_check_cfg - Checks if config fw is valid
629 *
630 * @ts: goodix_ts_data pointer
631 * @cfg: firmware config data
632 * @len: config data length
633 */
634static int goodix_check_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
635{
636 if (len < GOODIX_CONFIG_MIN_LENGTH ||
637 len > GOODIX_CONFIG_MAX_LENGTH) {
638 dev_err(&ts->client->dev,
639 "The length of the config fw is not correct");
640 return -EINVAL;
641 }
642
643 return ts->chip->check_config(ts, cfg, len);
644}
645
646/**
647 * goodix_send_cfg - Write fw config to device
648 *
649 * @ts: goodix_ts_data pointer
650 * @cfg: config firmware to write to device
651 * @len: config data length
652 */
653int goodix_send_cfg(struct goodix_ts_data *ts, const u8 *cfg, int len)
654{
655 int error;
656
657 error = goodix_check_cfg(ts, cfg, len);
658 if (error)
659 return error;
660
661 error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg, len);
662 if (error)
663 return error;
664
665 dev_dbg(&ts->client->dev, "Config sent successfully.");
666
667 /* Let the firmware reconfigure itself, so sleep for 10ms */
668 usleep_range(10000, 11000);
669
670 return 0;
671}
672
673#ifdef ACPI_GPIO_SUPPORT
674static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
675{
676 acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
677 acpi_status status;
678
679 status = acpi_evaluate_object(handle, "INTI", NULL, NULL);
680 return ACPI_SUCCESS(status) ? 0 : -EIO;
681}
682
683static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
684{
685 acpi_handle handle = ACPI_HANDLE(&ts->client->dev);
686 acpi_status status;
687
688 status = acpi_execute_simple_method(handle, "INTO", value);
689 return ACPI_SUCCESS(status) ? 0 : -EIO;
690}
691#else
692static int goodix_pin_acpi_direction_input(struct goodix_ts_data *ts)
693{
694 dev_err(&ts->client->dev,
695 "%s called on device without ACPI support\n", __func__);
696 return -EINVAL;
697}
698
699static int goodix_pin_acpi_output_method(struct goodix_ts_data *ts, int value)
700{
701 dev_err(&ts->client->dev,
702 "%s called on device without ACPI support\n", __func__);
703 return -EINVAL;
704}
705#endif
706
707static int goodix_irq_direction_output(struct goodix_ts_data *ts, int value)
708{
709 switch (ts->irq_pin_access_method) {
710 case IRQ_PIN_ACCESS_NONE:
711 dev_err(&ts->client->dev,
712 "%s called without an irq_pin_access_method set\n",
713 __func__);
714 return -EINVAL;
715 case IRQ_PIN_ACCESS_GPIO:
716 return gpiod_direction_output(ts->gpiod_int, value);
717 case IRQ_PIN_ACCESS_ACPI_GPIO:
718 /*
719 * The IRQ pin triggers on a falling edge, so its gets marked
720 * as active-low, use output_raw to avoid the value inversion.
721 */
722 return gpiod_direction_output_raw(ts->gpiod_int, value);
723 case IRQ_PIN_ACCESS_ACPI_METHOD:
724 return goodix_pin_acpi_output_method(ts, value);
725 }
726
727 return -EINVAL; /* Never reached */
728}
729
730static int goodix_irq_direction_input(struct goodix_ts_data *ts)
731{
732 switch (ts->irq_pin_access_method) {
733 case IRQ_PIN_ACCESS_NONE:
734 dev_err(&ts->client->dev,
735 "%s called without an irq_pin_access_method set\n",
736 __func__);
737 return -EINVAL;
738 case IRQ_PIN_ACCESS_GPIO:
739 return gpiod_direction_input(ts->gpiod_int);
740 case IRQ_PIN_ACCESS_ACPI_GPIO:
741 return gpiod_direction_input(ts->gpiod_int);
742 case IRQ_PIN_ACCESS_ACPI_METHOD:
743 return goodix_pin_acpi_direction_input(ts);
744 }
745
746 return -EINVAL; /* Never reached */
747}
748
749int goodix_int_sync(struct goodix_ts_data *ts)
750{
751 int error;
752
753 error = goodix_irq_direction_output(ts, 0);
754 if (error)
755 goto error;
756
757 msleep(50); /* T5: 50ms */
758
759 error = goodix_irq_direction_input(ts);
760 if (error)
761 goto error;
762
763 return 0;
764
765error:
766 dev_err(&ts->client->dev, "Controller irq sync failed.\n");
767 return error;
768}
769
770/**
771 * goodix_reset_no_int_sync - Reset device, leaving interrupt line in output mode
772 *
773 * @ts: goodix_ts_data pointer
774 */
775int goodix_reset_no_int_sync(struct goodix_ts_data *ts)
776{
777 int error;
778
779 /* begin select I2C slave addr */
780 error = gpiod_direction_output(ts->gpiod_rst, 0);
781 if (error)
782 goto error;
783
784 msleep(20); /* T2: > 10ms */
785
786 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
787 error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
788 if (error)
789 goto error;
790
791 usleep_range(100, 2000); /* T3: > 100us */
792
793 error = gpiod_direction_output(ts->gpiod_rst, 1);
794 if (error)
795 goto error;
796
797 usleep_range(6000, 10000); /* T4: > 5ms */
798
799 return 0;
800
801error:
802 dev_err(&ts->client->dev, "Controller reset failed.\n");
803 return error;
804}
805
806/**
807 * goodix_reset - Reset device during power on
808 *
809 * @ts: goodix_ts_data pointer
810 */
811static int goodix_reset(struct goodix_ts_data *ts)
812{
813 int error;
814
815 error = goodix_reset_no_int_sync(ts);
816 if (error)
817 return error;
818
819 return goodix_int_sync(ts);
820}
821
822#ifdef ACPI_GPIO_SUPPORT
823static const struct acpi_gpio_params first_gpio = { 0, 0, false };
824static const struct acpi_gpio_params second_gpio = { 1, 0, false };
825
826static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
827 { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
828 { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
829 { },
830};
831
832static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
833 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
834 { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
835 { },
836};
837
838static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
839 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
840 { },
841};
842
843static int goodix_resource(struct acpi_resource *ares, void *data)
844{
845 struct goodix_ts_data *ts = data;
846 struct device *dev = &ts->client->dev;
847 struct acpi_resource_gpio *gpio;
848
849 if (acpi_gpio_get_irq_resource(ares, &gpio)) {
850 if (ts->gpio_int_idx == -1) {
851 ts->gpio_int_idx = ts->gpio_count;
852 } else {
853 dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
854 ts->gpio_int_idx = -2;
855 }
856 ts->gpio_count++;
857 } else if (acpi_gpio_get_io_resource(ares, &gpio))
858 ts->gpio_count++;
859
860 return 0;
861}
862
863/*
864 * This function gets called in case we fail to get the irq GPIO directly
865 * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
866 * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
867 * In that case we add our own mapping and then goodix_get_gpio_config()
868 * retries to get the GPIOs based on the added mapping.
869 */
870static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
871{
872 const struct acpi_gpio_mapping *gpio_mapping = NULL;
873 struct device *dev = &ts->client->dev;
874 LIST_HEAD(resources);
875 int irq, ret;
876
877 ts->gpio_count = 0;
878 ts->gpio_int_idx = -1;
879 ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
880 goodix_resource, ts);
881 if (ret < 0) {
882 dev_err(dev, "Error getting ACPI resources: %d\n", ret);
883 return ret;
884 }
885
886 acpi_dev_free_resource_list(&resources);
887
888 /*
889 * CHT devices should have a GpioInt + a regular GPIO ACPI resource.
890 * Some CHT devices have a bug (where the also is bogus Interrupt
891 * resource copied from a previous BYT based generation). i2c-core-acpi
892 * will use the non-working Interrupt resource, fix this up.
893 */
894 if (soc_intel_is_cht() && ts->gpio_count == 2 && ts->gpio_int_idx != -1) {
895 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
896 if (irq > 0 && irq != ts->client->irq) {
897 dev_warn(dev, "Overriding IRQ %d -> %d\n", ts->client->irq, irq);
898 ts->client->irq = irq;
899 }
900 }
901
902 /* Some devices with gpio_int_idx 0 list a third unused GPIO */
903 if ((ts->gpio_count == 2 || ts->gpio_count == 3) && ts->gpio_int_idx == 0) {
904 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
905 gpio_mapping = acpi_goodix_int_first_gpios;
906 } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
907 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
908 gpio_mapping = acpi_goodix_int_last_gpios;
909 } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
910 acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
911 acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
912 dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
913 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
914 gpio_mapping = acpi_goodix_reset_only_gpios;
915 } else if (soc_intel_is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
916 dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
917 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
918 gpio_mapping = acpi_goodix_int_last_gpios;
919 } else if (ts->gpio_count == 1 && ts->gpio_int_idx == 0) {
920 /*
921 * On newer devices there is only 1 GpioInt resource and _PS0
922 * does the whole reset sequence for us.
923 */
924 acpi_device_fix_up_power(ACPI_COMPANION(dev));
925
926 /*
927 * Before the _PS0 call the int GPIO may have been in output
928 * mode and the call should have put the int GPIO in input mode,
929 * but the GPIO subsys cached state may still think it is
930 * in output mode, causing gpiochip_lock_as_irq() failure.
931 *
932 * Add a mapping for the int GPIO to make the
933 * gpiod_int = gpiod_get(..., GPIOD_IN) call succeed,
934 * which will explicitly set the direction to input.
935 */
936 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
937 gpio_mapping = acpi_goodix_int_first_gpios;
938 } else {
939 dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
940 ts->gpio_count, ts->gpio_int_idx);
941 /*
942 * On some devices _PS0 does a reset for us and
943 * sometimes this is necessary for things to work.
944 */
945 acpi_device_fix_up_power(ACPI_COMPANION(dev));
946 return -EINVAL;
947 }
948
949 return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
950}
951#else
952static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
953{
954 return -EINVAL;
955}
956#endif /* CONFIG_X86 && CONFIG_ACPI */
957
958/**
959 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
960 *
961 * @ts: goodix_ts_data pointer
962 */
963static int goodix_get_gpio_config(struct goodix_ts_data *ts)
964{
965 struct device *dev;
966 struct gpio_desc *gpiod;
967 bool added_acpi_mappings = false;
968
969 if (!ts->client)
970 return -EINVAL;
971 dev = &ts->client->dev;
972
973 ts->avdd28 = devm_regulator_get(dev, "AVDD28");
974 if (IS_ERR(ts->avdd28))
975 return dev_err_probe(dev, PTR_ERR(ts->avdd28), "Failed to get AVDD28 regulator\n");
976
977 ts->vddio = devm_regulator_get(dev, "VDDIO");
978 if (IS_ERR(ts->vddio))
979 return dev_err_probe(dev, PTR_ERR(ts->vddio), "Failed to get VDDIO regulator\n");
980
981retry_get_irq_gpio:
982 /* Get the interrupt GPIO pin number */
983 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
984 if (IS_ERR(gpiod))
985 return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
986 GOODIX_GPIO_INT_NAME);
987
988 if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
989 added_acpi_mappings = true;
990 if (goodix_add_acpi_gpio_mappings(ts) == 0)
991 goto retry_get_irq_gpio;
992 }
993
994 ts->gpiod_int = gpiod;
995
996 /* Get the reset line GPIO pin number */
997 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_ASIS);
998 if (IS_ERR(gpiod))
999 return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
1000 GOODIX_GPIO_RST_NAME);
1001
1002 ts->gpiod_rst = gpiod;
1003
1004 switch (ts->irq_pin_access_method) {
1005 case IRQ_PIN_ACCESS_ACPI_GPIO:
1006 /*
1007 * We end up here if goodix_add_acpi_gpio_mappings() has
1008 * called devm_acpi_dev_add_driver_gpios() because the ACPI
1009 * tables did not contain name to index mappings.
1010 * Check that we successfully got both GPIOs after we've
1011 * added our own acpi_gpio_mapping and if we did not get both
1012 * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
1013 */
1014 if (!ts->gpiod_int || !ts->gpiod_rst)
1015 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
1016 break;
1017 case IRQ_PIN_ACCESS_ACPI_METHOD:
1018 if (!ts->gpiod_rst)
1019 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
1020 break;
1021 default:
1022 if (ts->gpiod_int && ts->gpiod_rst) {
1023 ts->reset_controller_at_probe = true;
1024 ts->load_cfg_from_disk = true;
1025 ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
1026 }
1027 }
1028
1029 return 0;
1030}
1031
1032/**
1033 * goodix_read_config - Read the embedded configuration of the panel
1034 *
1035 * @ts: our goodix_ts_data pointer
1036 *
1037 * Must be called during probe
1038 */
1039static void goodix_read_config(struct goodix_ts_data *ts)
1040{
1041 int x_max, y_max;
1042 int error;
1043
1044 /*
1045 * On controllers where we need to upload the firmware
1046 * (controllers without flash) ts->config already has the config
1047 * at this point and the controller itself does not have it yet!
1048 */
1049 if (!ts->firmware_name) {
1050 error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1051 ts->config, ts->chip->config_len);
1052 if (error) {
1053 ts->int_trigger_type = GOODIX_INT_TRIGGER;
1054 ts->max_touch_num = GOODIX_MAX_CONTACTS;
1055 return;
1056 }
1057 }
1058
1059 ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
1060 ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
1061
1062 x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
1063 y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
1064 if (x_max && y_max) {
1065 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
1066 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
1067 }
1068
1069 ts->chip->calc_config_checksum(ts);
1070}
1071
1072/**
1073 * goodix_read_version - Read goodix touchscreen version
1074 *
1075 * @ts: our goodix_ts_data pointer
1076 */
1077static int goodix_read_version(struct goodix_ts_data *ts)
1078{
1079 int error;
1080 u8 buf[6];
1081 char id_str[GOODIX_ID_MAX_LEN + 1];
1082
1083 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
1084 if (error)
1085 return error;
1086
1087 memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
1088 id_str[GOODIX_ID_MAX_LEN] = 0;
1089 strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
1090
1091 ts->version = get_unaligned_le16(&buf[4]);
1092
1093 dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
1094 ts->version);
1095
1096 return 0;
1097}
1098
1099/**
1100 * goodix_i2c_test - I2C test function to check if the device answers.
1101 *
1102 * @client: the i2c client
1103 */
1104static int goodix_i2c_test(struct i2c_client *client)
1105{
1106 int retry = 0;
1107 int error;
1108 u8 test;
1109
1110 while (retry++ < 2) {
1111 error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1);
1112 if (!error)
1113 return 0;
1114
1115 msleep(20);
1116 }
1117
1118 return error;
1119}
1120
1121/**
1122 * goodix_configure_dev - Finish device initialization
1123 *
1124 * @ts: our goodix_ts_data pointer
1125 *
1126 * Must be called from probe to finish initialization of the device.
1127 * Contains the common initialization code for both devices that
1128 * declare gpio pins and devices that do not. It is either called
1129 * directly from probe or from request_firmware_wait callback.
1130 */
1131static int goodix_configure_dev(struct goodix_ts_data *ts)
1132{
1133 int error;
1134 int i;
1135
1136 ts->int_trigger_type = GOODIX_INT_TRIGGER;
1137 ts->max_touch_num = GOODIX_MAX_CONTACTS;
1138
1139 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
1140 if (!ts->input_dev) {
1141 dev_err(&ts->client->dev, "Failed to allocate input device.");
1142 return -ENOMEM;
1143 }
1144
1145 ts->input_dev->name = "Goodix Capacitive TouchScreen";
1146 ts->input_dev->phys = "input/ts";
1147 ts->input_dev->id.bustype = BUS_I2C;
1148 ts->input_dev->id.vendor = 0x0416;
1149 if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
1150 ts->input_dev->id.product = 0x1001;
1151 ts->input_dev->id.version = ts->version;
1152
1153 ts->input_dev->keycode = ts->keymap;
1154 ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
1155 ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
1156
1157 /* Capacitive Windows/Home button on some devices */
1158 for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
1159 if (i == 0)
1160 ts->keymap[i] = KEY_LEFTMETA;
1161 else
1162 ts->keymap[i] = KEY_F1 + (i - 1);
1163
1164 input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
1165 }
1166
1167 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
1168 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
1169 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
1170 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1171
1172retry_read_config:
1173 /* Read configuration and apply touchscreen parameters */
1174 goodix_read_config(ts);
1175
1176 /* Try overriding touchscreen parameters via device properties */
1177 touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
1178
1179 if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
1180 if (!ts->reset_controller_at_probe &&
1181 ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1182 dev_info(&ts->client->dev, "Config not set, resetting controller\n");
1183 /* Retry after a controller reset */
1184 ts->reset_controller_at_probe = true;
1185 error = goodix_reset(ts);
1186 if (error)
1187 return error;
1188 goto retry_read_config;
1189 }
1190 dev_err(&ts->client->dev,
1191 "Invalid config (%d, %d, %d), using defaults\n",
1192 ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
1193 ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
1194 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
1195 ts->max_touch_num = GOODIX_MAX_CONTACTS;
1196 input_abs_set_max(ts->input_dev,
1197 ABS_MT_POSITION_X, ts->prop.max_x);
1198 input_abs_set_max(ts->input_dev,
1199 ABS_MT_POSITION_Y, ts->prop.max_y);
1200 }
1201
1202 if (dmi_check_system(nine_bytes_report)) {
1203 ts->contact_size = 9;
1204
1205 dev_dbg(&ts->client->dev,
1206 "Non-standard 9-bytes report format quirk\n");
1207 }
1208
1209 if (dmi_check_system(inverted_x_screen)) {
1210 ts->prop.invert_x = true;
1211 dev_dbg(&ts->client->dev,
1212 "Applying 'inverted x screen' quirk\n");
1213 }
1214
1215 error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
1216 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1217 if (error) {
1218 dev_err(&ts->client->dev,
1219 "Failed to initialize MT slots: %d", error);
1220 return error;
1221 }
1222
1223 input_set_drvdata(ts->input_dev, ts);
1224
1225 if (!ts->client->irq) {
1226 error = input_setup_polling(ts->input_dev, goodix_ts_work_i2c_poll);
1227 if (error) {
1228 dev_err(&ts->client->dev,
1229 "could not set up polling mode, %d\n", error);
1230 return error;
1231 }
1232 input_set_poll_interval(ts->input_dev, GOODIX_POLL_INTERVAL_MS);
1233 }
1234
1235 error = input_register_device(ts->input_dev);
1236 if (error) {
1237 dev_err(&ts->client->dev,
1238 "Failed to register input device: %d", error);
1239 return error;
1240 }
1241
1242 /*
1243 * Create the input_pen device before goodix_request_irq() calls
1244 * devm_request_threaded_irq() so that the devm framework frees
1245 * it after disabling the irq.
1246 * Unfortunately there is no way to detect if the touchscreen has pen
1247 * support, so registering the dev is delayed till the first pen event.
1248 */
1249 error = goodix_create_pen_input(ts);
1250 if (error)
1251 return error;
1252
1253 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
1254 error = goodix_request_irq(ts);
1255 if (error) {
1256 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
1257 return error;
1258 }
1259
1260 return 0;
1261}
1262
1263/**
1264 * goodix_config_cb - Callback to finish device init
1265 *
1266 * @cfg: firmware config
1267 * @ctx: our goodix_ts_data pointer
1268 *
1269 * request_firmware_wait callback that finishes
1270 * initialization of the device.
1271 */
1272static void goodix_config_cb(const struct firmware *cfg, void *ctx)
1273{
1274 struct goodix_ts_data *ts = ctx;
1275 int error;
1276
1277 if (ts->firmware_name) {
1278 if (!cfg)
1279 goto err_release_cfg;
1280
1281 error = goodix_check_cfg(ts, cfg->data, cfg->size);
1282 if (error)
1283 goto err_release_cfg;
1284
1285 memcpy(ts->config, cfg->data, cfg->size);
1286 } else if (cfg) {
1287 /* send device configuration to the firmware */
1288 error = goodix_send_cfg(ts, cfg->data, cfg->size);
1289 if (error)
1290 goto err_release_cfg;
1291 }
1292
1293 goodix_configure_dev(ts);
1294
1295err_release_cfg:
1296 release_firmware(cfg);
1297 complete_all(&ts->firmware_loading_complete);
1298}
1299
1300static void goodix_disable_regulators(void *arg)
1301{
1302 struct goodix_ts_data *ts = arg;
1303
1304 regulator_disable(ts->vddio);
1305 regulator_disable(ts->avdd28);
1306}
1307
1308static int goodix_ts_probe(struct i2c_client *client)
1309{
1310 struct goodix_ts_data *ts;
1311 const char *cfg_name;
1312 int error;
1313
1314 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
1315
1316 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1317 dev_err(&client->dev, "I2C check functionality failed.\n");
1318 return -ENXIO;
1319 }
1320
1321 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1322 if (!ts)
1323 return -ENOMEM;
1324
1325 ts->client = client;
1326 i2c_set_clientdata(client, ts);
1327 init_completion(&ts->firmware_loading_complete);
1328 ts->contact_size = GOODIX_CONTACT_SIZE;
1329
1330 error = goodix_get_gpio_config(ts);
1331 if (error)
1332 return error;
1333
1334 /* power up the controller */
1335 error = regulator_enable(ts->avdd28);
1336 if (error) {
1337 dev_err(&client->dev,
1338 "Failed to enable AVDD28 regulator: %d\n",
1339 error);
1340 return error;
1341 }
1342
1343 error = regulator_enable(ts->vddio);
1344 if (error) {
1345 dev_err(&client->dev,
1346 "Failed to enable VDDIO regulator: %d\n",
1347 error);
1348 regulator_disable(ts->avdd28);
1349 return error;
1350 }
1351
1352 error = devm_add_action_or_reset(&client->dev,
1353 goodix_disable_regulators, ts);
1354 if (error)
1355 return error;
1356
1357reset:
1358 if (ts->reset_controller_at_probe) {
1359 /* reset the controller */
1360 error = goodix_reset(ts);
1361 if (error)
1362 return error;
1363 }
1364
1365 error = goodix_i2c_test(client);
1366 if (error) {
1367 if (!ts->reset_controller_at_probe &&
1368 ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1369 /* Retry after a controller reset */
1370 ts->reset_controller_at_probe = true;
1371 goto reset;
1372 }
1373 dev_err(&client->dev, "I2C communication failure: %d\n", error);
1374 return error;
1375 }
1376
1377 error = goodix_firmware_check(ts);
1378 if (error)
1379 return error;
1380
1381 error = goodix_read_version(ts);
1382 if (error)
1383 return error;
1384
1385 ts->chip = goodix_get_chip_data(ts->id);
1386
1387 if (ts->load_cfg_from_disk) {
1388 /* update device config */
1389 error = device_property_read_string(&client->dev,
1390 "goodix,config-name",
1391 &cfg_name);
1392 if (!error)
1393 snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1394 "goodix/%s", cfg_name);
1395 else
1396 snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1397 "goodix_%s_cfg.bin", ts->id);
1398
1399 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
1400 &client->dev, GFP_KERNEL, ts,
1401 goodix_config_cb);
1402 if (error) {
1403 dev_err(&client->dev,
1404 "Failed to invoke firmware loader: %d\n",
1405 error);
1406 return error;
1407 }
1408
1409 return 0;
1410 } else {
1411 error = goodix_configure_dev(ts);
1412 if (error)
1413 return error;
1414 }
1415
1416 return 0;
1417}
1418
1419static void goodix_ts_remove(struct i2c_client *client)
1420{
1421 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1422
1423 if (ts->load_cfg_from_disk)
1424 wait_for_completion(&ts->firmware_loading_complete);
1425}
1426
1427static int goodix_suspend(struct device *dev)
1428{
1429 struct i2c_client *client = to_i2c_client(dev);
1430 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1431 int error;
1432
1433 if (ts->load_cfg_from_disk)
1434 wait_for_completion(&ts->firmware_loading_complete);
1435
1436 /* We need gpio pins to suspend/resume */
1437 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1438 goodix_disable_irq(ts);
1439 return 0;
1440 }
1441
1442 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
1443 goodix_free_irq(ts);
1444
1445 /* Save reference (calibration) info if necessary */
1446 goodix_save_bak_ref(ts);
1447
1448 /* Output LOW on the INT pin for 5 ms */
1449 error = goodix_irq_direction_output(ts, 0);
1450 if (error) {
1451 goodix_request_irq(ts);
1452 return error;
1453 }
1454
1455 usleep_range(5000, 6000);
1456
1457 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
1458 GOODIX_CMD_SCREEN_OFF);
1459 if (error) {
1460 goodix_irq_direction_input(ts);
1461 goodix_request_irq(ts);
1462 return -EAGAIN;
1463 }
1464
1465 /*
1466 * The datasheet specifies that the interval between sending screen-off
1467 * command and wake-up should be longer than 58 ms. To avoid waking up
1468 * sooner, delay 58ms here.
1469 */
1470 msleep(58);
1471 return 0;
1472}
1473
1474static int goodix_resume(struct device *dev)
1475{
1476 struct i2c_client *client = to_i2c_client(dev);
1477 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1478 u8 config_ver;
1479 int error;
1480
1481 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1482 goodix_enable_irq(ts);
1483 return 0;
1484 }
1485
1486 /*
1487 * Exit sleep mode by outputting HIGH level to INT pin
1488 * for 2ms~5ms.
1489 */
1490 error = goodix_irq_direction_output(ts, 1);
1491 if (error)
1492 return error;
1493
1494 usleep_range(2000, 5000);
1495
1496 error = goodix_int_sync(ts);
1497 if (error)
1498 return error;
1499
1500 error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1501 &config_ver, 1);
1502 if (!error && config_ver != ts->config[0])
1503 dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
1504 config_ver, ts->config[0]);
1505
1506 if (error != 0 || config_ver != ts->config[0]) {
1507 error = goodix_reset(ts);
1508 if (error)
1509 return error;
1510
1511 error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
1512 if (error)
1513 return error;
1514 }
1515
1516 error = goodix_request_irq(ts);
1517 if (error)
1518 return error;
1519
1520 return 0;
1521}
1522
1523static DEFINE_SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1524
1525static const struct i2c_device_id goodix_ts_id[] = {
1526 { "GDIX1001:00" },
1527 { }
1528};
1529MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1530
1531#ifdef CONFIG_ACPI
1532static const struct acpi_device_id goodix_acpi_match[] = {
1533 { "GDIX1001", 0 },
1534 { "GDIX1002", 0 },
1535 { "GDIX1003", 0 },
1536 { "GDX9110", 0 },
1537 { }
1538};
1539MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1540#endif
1541
1542#ifdef CONFIG_OF
1543static const struct of_device_id goodix_of_match[] = {
1544 { .compatible = "goodix,gt1151" },
1545 { .compatible = "goodix,gt1158" },
1546 { .compatible = "goodix,gt5663" },
1547 { .compatible = "goodix,gt5688" },
1548 { .compatible = "goodix,gt911" },
1549 { .compatible = "goodix,gt9110" },
1550 { .compatible = "goodix,gt912" },
1551 { .compatible = "goodix,gt9147" },
1552 { .compatible = "goodix,gt917s" },
1553 { .compatible = "goodix,gt927" },
1554 { .compatible = "goodix,gt9271" },
1555 { .compatible = "goodix,gt928" },
1556 { .compatible = "goodix,gt9286" },
1557 { .compatible = "goodix,gt967" },
1558 { }
1559};
1560MODULE_DEVICE_TABLE(of, goodix_of_match);
1561#endif
1562
1563static struct i2c_driver goodix_ts_driver = {
1564 .probe = goodix_ts_probe,
1565 .remove = goodix_ts_remove,
1566 .id_table = goodix_ts_id,
1567 .driver = {
1568 .name = "Goodix-TS",
1569 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
1570 .of_match_table = of_match_ptr(goodix_of_match),
1571 .pm = pm_sleep_ptr(&goodix_pm_ops),
1572 },
1573};
1574module_i2c_driver(goodix_ts_driver);
1575
1576MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1577MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1578MODULE_DESCRIPTION("Goodix touchscreen driver");
1579MODULE_LICENSE("GPL v2");