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
58 struct goodix_chip_id {
59 const char *id;
60 const struct goodix_chip_data *data;
61 };
62
63 static int goodix_check_cfg_8(struct goodix_ts_data *ts,
64 const u8 *cfg, int len);
65 static int goodix_check_cfg_16(struct goodix_ts_data *ts,
66 const u8 *cfg, int len);
67 static void goodix_calc_cfg_checksum_8(struct goodix_ts_data *ts);
68 static void goodix_calc_cfg_checksum_16(struct goodix_ts_data *ts);
69
70 static 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
77 static 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
84 static 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
91 static 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
98 static 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
119 static 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
126 static 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 */
150 static 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 */
goodix_i2c_read(struct i2c_client * client,u16 reg,u8 * buf,int len)171 int 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 */
goodix_i2c_write(struct i2c_client * client,u16 reg,const u8 * buf,int len)205 int 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
goodix_i2c_write_u8(struct i2c_client * client,u16 reg,u8 value)236 int 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
goodix_get_chip_data(const char * id)241 static 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
goodix_ts_read_input_report(struct goodix_ts_data * ts,u8 * data)253 static 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
goodix_create_pen_input(struct goodix_ts_data * ts)312 static 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
goodix_ts_report_pen_down(struct goodix_ts_data * ts,u8 * data)350 static 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
goodix_ts_report_pen_up(struct goodix_ts_data * ts)391 static 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
goodix_ts_report_touch_8b(struct goodix_ts_data * ts,u8 * coor_data)404 static 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
goodix_ts_report_touch_9b(struct goodix_ts_data * ts,u8 * coor_data)419 static 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
goodix_ts_release_keys(struct goodix_ts_data * ts)434 static 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
goodix_ts_report_key(struct goodix_ts_data * ts,u8 * data)442 static 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 */
goodix_process_events(struct goodix_ts_data * ts)468 static 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
497 sync:
498 input_mt_sync_frame(ts->input_dev);
499 input_sync(ts->input_dev);
500 }
501
goodix_ts_work_i2c_poll(struct input_dev * input)502 static 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 */
goodix_ts_irq_handler(int irq,void * dev_id)516 static 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
goodix_enable_irq(struct goodix_ts_data * ts)526 static void goodix_enable_irq(struct goodix_ts_data *ts)
527 {
528 if (ts->client->irq)
529 enable_irq(ts->client->irq);
530 }
531
goodix_disable_irq(struct goodix_ts_data * ts)532 static void goodix_disable_irq(struct goodix_ts_data *ts)
533 {
534 if (ts->client->irq)
535 disable_irq(ts->client->irq);
536 }
537
goodix_free_irq(struct goodix_ts_data * ts)538 static 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
goodix_request_irq(struct goodix_ts_data * ts)544 static 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
goodix_check_cfg_8(struct goodix_ts_data * ts,const u8 * cfg,int len)554 static 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
goodix_calc_cfg_checksum_8(struct goodix_ts_data * ts)577 static 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
goodix_check_cfg_16(struct goodix_ts_data * ts,const u8 * cfg,int len)590 static 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
goodix_calc_cfg_checksum_16(struct goodix_ts_data * ts)614 static 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 */
goodix_check_cfg(struct goodix_ts_data * ts,const u8 * cfg,int len)634 static 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 */
goodix_send_cfg(struct goodix_ts_data * ts,const u8 * cfg,int len)653 int 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
goodix_pin_acpi_direction_input(struct goodix_ts_data * ts)674 static 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
goodix_pin_acpi_output_method(struct goodix_ts_data * ts,int value)683 static 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
goodix_pin_acpi_direction_input(struct goodix_ts_data * ts)692 static 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
goodix_pin_acpi_output_method(struct goodix_ts_data * ts,int value)699 static 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
goodix_irq_direction_output(struct goodix_ts_data * ts,int value)707 static 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
goodix_irq_direction_input(struct goodix_ts_data * ts)730 static 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
goodix_int_sync(struct goodix_ts_data * ts)749 int 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
765 error:
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 */
goodix_reset_no_int_sync(struct goodix_ts_data * ts)775 int 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 /*
800 * Put the reset pin back in to input / high-impedance mode to save
801 * power. Only do this in the non ACPI case since some ACPI boards
802 * don't have a pull-up, so there the reset pin must stay active-high.
803 */
804 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_GPIO) {
805 error = gpiod_direction_input(ts->gpiod_rst);
806 if (error)
807 goto error;
808 }
809
810 return 0;
811
812 error:
813 dev_err(&ts->client->dev, "Controller reset failed.\n");
814 return error;
815 }
816
817 /**
818 * goodix_reset - Reset device during power on
819 *
820 * @ts: goodix_ts_data pointer
821 */
goodix_reset(struct goodix_ts_data * ts)822 static int goodix_reset(struct goodix_ts_data *ts)
823 {
824 int error;
825
826 error = goodix_reset_no_int_sync(ts);
827 if (error)
828 return error;
829
830 return goodix_int_sync(ts);
831 }
832
833 #ifdef ACPI_GPIO_SUPPORT
834 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
835 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
836
837 static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
838 { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
839 { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
840 { },
841 };
842
843 static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
844 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
845 { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
846 { },
847 };
848
849 static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
850 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
851 { },
852 };
853
goodix_resource(struct acpi_resource * ares,void * data)854 static int goodix_resource(struct acpi_resource *ares, void *data)
855 {
856 struct goodix_ts_data *ts = data;
857 struct device *dev = &ts->client->dev;
858 struct acpi_resource_gpio *gpio;
859
860 if (acpi_gpio_get_irq_resource(ares, &gpio)) {
861 if (ts->gpio_int_idx == -1) {
862 ts->gpio_int_idx = ts->gpio_count;
863 } else {
864 dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
865 ts->gpio_int_idx = -2;
866 }
867 ts->gpio_count++;
868 } else if (acpi_gpio_get_io_resource(ares, &gpio))
869 ts->gpio_count++;
870
871 return 0;
872 }
873
874 /*
875 * This function gets called in case we fail to get the irq GPIO directly
876 * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
877 * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
878 * In that case we add our own mapping and then goodix_get_gpio_config()
879 * retries to get the GPIOs based on the added mapping.
880 */
goodix_add_acpi_gpio_mappings(struct goodix_ts_data * ts)881 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
882 {
883 const struct acpi_gpio_mapping *gpio_mapping = NULL;
884 struct device *dev = &ts->client->dev;
885 LIST_HEAD(resources);
886 int irq, ret;
887
888 ts->gpio_count = 0;
889 ts->gpio_int_idx = -1;
890 ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
891 goodix_resource, ts);
892 if (ret < 0) {
893 dev_err(dev, "Error getting ACPI resources: %d\n", ret);
894 return ret;
895 }
896
897 acpi_dev_free_resource_list(&resources);
898
899 /*
900 * CHT devices should have a GpioInt + a regular GPIO ACPI resource.
901 * Some CHT devices have a bug (where the also is bogus Interrupt
902 * resource copied from a previous BYT based generation). i2c-core-acpi
903 * will use the non-working Interrupt resource, fix this up.
904 */
905 if (soc_intel_is_cht() && ts->gpio_count == 2 && ts->gpio_int_idx != -1) {
906 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
907 if (irq > 0 && irq != ts->client->irq) {
908 dev_warn(dev, "Overriding IRQ %d -> %d\n", ts->client->irq, irq);
909 ts->client->irq = irq;
910 }
911 }
912
913 /* Some devices with gpio_int_idx 0 list a third unused GPIO */
914 if ((ts->gpio_count == 2 || ts->gpio_count == 3) && ts->gpio_int_idx == 0) {
915 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
916 gpio_mapping = acpi_goodix_int_first_gpios;
917 } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
918 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
919 gpio_mapping = acpi_goodix_int_last_gpios;
920 } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
921 acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
922 acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
923 dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
924 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
925 gpio_mapping = acpi_goodix_reset_only_gpios;
926 } else if (soc_intel_is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
927 dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
928 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
929 gpio_mapping = acpi_goodix_int_last_gpios;
930 } else if (ts->gpio_count == 1 && ts->gpio_int_idx == 0) {
931 /*
932 * On newer devices there is only 1 GpioInt resource and _PS0
933 * does the whole reset sequence for us.
934 */
935 acpi_device_fix_up_power(ACPI_COMPANION(dev));
936
937 /*
938 * Before the _PS0 call the int GPIO may have been in output
939 * mode and the call should have put the int GPIO in input mode,
940 * but the GPIO subsys cached state may still think it is
941 * in output mode, causing gpiochip_lock_as_irq() failure.
942 *
943 * Add a mapping for the int GPIO to make the
944 * gpiod_int = gpiod_get(..., GPIOD_IN) call succeed,
945 * which will explicitly set the direction to input.
946 */
947 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
948 gpio_mapping = acpi_goodix_int_first_gpios;
949 } else {
950 dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
951 ts->gpio_count, ts->gpio_int_idx);
952 /*
953 * On some devices _PS0 does a reset for us and
954 * sometimes this is necessary for things to work.
955 */
956 acpi_device_fix_up_power(ACPI_COMPANION(dev));
957 return -EINVAL;
958 }
959
960 /*
961 * Normally we put the reset pin in input / high-impedance mode to save
962 * power. But some x86/ACPI boards don't have a pull-up, so for the ACPI
963 * case, leave the pin as is. This results in the pin not being touched
964 * at all on x86/ACPI boards, except when needed for error-recover.
965 */
966 ts->gpiod_rst_flags = GPIOD_ASIS;
967
968 return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
969 }
970 #else
goodix_add_acpi_gpio_mappings(struct goodix_ts_data * ts)971 static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
972 {
973 return -EINVAL;
974 }
975 #endif /* CONFIG_X86 && CONFIG_ACPI */
976
977 /**
978 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
979 *
980 * @ts: goodix_ts_data pointer
981 */
goodix_get_gpio_config(struct goodix_ts_data * ts)982 static int goodix_get_gpio_config(struct goodix_ts_data *ts)
983 {
984 struct device *dev;
985 struct gpio_desc *gpiod;
986 bool added_acpi_mappings = false;
987
988 if (!ts->client)
989 return -EINVAL;
990 dev = &ts->client->dev;
991
992 /*
993 * By default we request the reset pin as input, leaving it in
994 * high-impedance when not resetting the controller to save power.
995 */
996 ts->gpiod_rst_flags = GPIOD_IN;
997
998 ts->avdd28 = devm_regulator_get(dev, "AVDD28");
999 if (IS_ERR(ts->avdd28))
1000 return dev_err_probe(dev, PTR_ERR(ts->avdd28), "Failed to get AVDD28 regulator\n");
1001
1002 ts->vddio = devm_regulator_get(dev, "VDDIO");
1003 if (IS_ERR(ts->vddio))
1004 return dev_err_probe(dev, PTR_ERR(ts->vddio), "Failed to get VDDIO regulator\n");
1005
1006 retry_get_irq_gpio:
1007 /* Get the interrupt GPIO pin number */
1008 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
1009 if (IS_ERR(gpiod))
1010 return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
1011 GOODIX_GPIO_INT_NAME);
1012
1013 if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
1014 added_acpi_mappings = true;
1015 if (goodix_add_acpi_gpio_mappings(ts) == 0)
1016 goto retry_get_irq_gpio;
1017 }
1018
1019 ts->gpiod_int = gpiod;
1020
1021 /* Get the reset line GPIO pin number */
1022 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, ts->gpiod_rst_flags);
1023 if (IS_ERR(gpiod))
1024 return dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get %s GPIO\n",
1025 GOODIX_GPIO_RST_NAME);
1026
1027 ts->gpiod_rst = gpiod;
1028
1029 switch (ts->irq_pin_access_method) {
1030 case IRQ_PIN_ACCESS_ACPI_GPIO:
1031 /*
1032 * We end up here if goodix_add_acpi_gpio_mappings() has
1033 * called devm_acpi_dev_add_driver_gpios() because the ACPI
1034 * tables did not contain name to index mappings.
1035 * Check that we successfully got both GPIOs after we've
1036 * added our own acpi_gpio_mapping and if we did not get both
1037 * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
1038 */
1039 if (!ts->gpiod_int || !ts->gpiod_rst)
1040 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
1041 break;
1042 case IRQ_PIN_ACCESS_ACPI_METHOD:
1043 if (!ts->gpiod_rst)
1044 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
1045 break;
1046 default:
1047 if (ts->gpiod_int && ts->gpiod_rst) {
1048 ts->reset_controller_at_probe = true;
1049 ts->load_cfg_from_disk = true;
1050 ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
1051 }
1052 }
1053
1054 return 0;
1055 }
1056
1057 /**
1058 * goodix_read_config - Read the embedded configuration of the panel
1059 *
1060 * @ts: our goodix_ts_data pointer
1061 *
1062 * Must be called during probe
1063 */
goodix_read_config(struct goodix_ts_data * ts)1064 static void goodix_read_config(struct goodix_ts_data *ts)
1065 {
1066 int x_max, y_max;
1067 int error;
1068
1069 /*
1070 * On controllers where we need to upload the firmware
1071 * (controllers without flash) ts->config already has the config
1072 * at this point and the controller itself does not have it yet!
1073 */
1074 if (!ts->firmware_name) {
1075 error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1076 ts->config, ts->chip->config_len);
1077 if (error) {
1078 ts->int_trigger_type = GOODIX_INT_TRIGGER;
1079 ts->max_touch_num = GOODIX_MAX_CONTACTS;
1080 return;
1081 }
1082 }
1083
1084 ts->int_trigger_type = ts->config[TRIGGER_LOC] & 0x03;
1085 ts->max_touch_num = ts->config[MAX_CONTACTS_LOC] & 0x0f;
1086
1087 x_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC]);
1088 y_max = get_unaligned_le16(&ts->config[RESOLUTION_LOC + 2]);
1089 if (x_max && y_max) {
1090 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
1091 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
1092 }
1093
1094 ts->chip->calc_config_checksum(ts);
1095 }
1096
1097 /**
1098 * goodix_read_version - Read goodix touchscreen version
1099 *
1100 * @ts: our goodix_ts_data pointer
1101 */
goodix_read_version(struct goodix_ts_data * ts)1102 static int goodix_read_version(struct goodix_ts_data *ts)
1103 {
1104 int error;
1105 u8 buf[6];
1106 char id_str[GOODIX_ID_MAX_LEN + 1];
1107
1108 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
1109 if (error)
1110 return error;
1111
1112 memcpy(id_str, buf, GOODIX_ID_MAX_LEN);
1113 id_str[GOODIX_ID_MAX_LEN] = 0;
1114 strscpy(ts->id, id_str, GOODIX_ID_MAX_LEN + 1);
1115
1116 ts->version = get_unaligned_le16(&buf[4]);
1117
1118 dev_info(&ts->client->dev, "ID %s, version: %04x\n", ts->id,
1119 ts->version);
1120
1121 return 0;
1122 }
1123
1124 /**
1125 * goodix_i2c_test - I2C test function to check if the device answers.
1126 *
1127 * @client: the i2c client
1128 */
goodix_i2c_test(struct i2c_client * client)1129 static int goodix_i2c_test(struct i2c_client *client)
1130 {
1131 int retry = 0;
1132 int error;
1133 u8 test;
1134
1135 while (retry++ < 2) {
1136 error = goodix_i2c_read(client, GOODIX_REG_ID, &test, 1);
1137 if (!error)
1138 return 0;
1139
1140 msleep(20);
1141 }
1142
1143 return error;
1144 }
1145
1146 /**
1147 * goodix_configure_dev - Finish device initialization
1148 *
1149 * @ts: our goodix_ts_data pointer
1150 *
1151 * Must be called from probe to finish initialization of the device.
1152 * Contains the common initialization code for both devices that
1153 * declare gpio pins and devices that do not. It is either called
1154 * directly from probe or from request_firmware_wait callback.
1155 */
goodix_configure_dev(struct goodix_ts_data * ts)1156 static int goodix_configure_dev(struct goodix_ts_data *ts)
1157 {
1158 int error;
1159 int i;
1160
1161 ts->int_trigger_type = GOODIX_INT_TRIGGER;
1162 ts->max_touch_num = GOODIX_MAX_CONTACTS;
1163
1164 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
1165 if (!ts->input_dev) {
1166 dev_err(&ts->client->dev, "Failed to allocate input device.");
1167 return -ENOMEM;
1168 }
1169
1170 ts->input_dev->name = "Goodix Capacitive TouchScreen";
1171 ts->input_dev->phys = "input/ts";
1172 ts->input_dev->id.bustype = BUS_I2C;
1173 ts->input_dev->id.vendor = 0x0416;
1174 if (kstrtou16(ts->id, 10, &ts->input_dev->id.product))
1175 ts->input_dev->id.product = 0x1001;
1176 ts->input_dev->id.version = ts->version;
1177
1178 ts->input_dev->keycode = ts->keymap;
1179 ts->input_dev->keycodesize = sizeof(ts->keymap[0]);
1180 ts->input_dev->keycodemax = GOODIX_MAX_KEYS;
1181
1182 /* Capacitive Windows/Home button on some devices */
1183 for (i = 0; i < GOODIX_MAX_KEYS; ++i) {
1184 if (i == 0)
1185 ts->keymap[i] = KEY_LEFTMETA;
1186 else
1187 ts->keymap[i] = KEY_F1 + (i - 1);
1188
1189 input_set_capability(ts->input_dev, EV_KEY, ts->keymap[i]);
1190 }
1191
1192 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
1193 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
1194 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
1195 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1196
1197 retry_read_config:
1198 /* Read configuration and apply touchscreen parameters */
1199 goodix_read_config(ts);
1200
1201 /* Try overriding touchscreen parameters via device properties */
1202 touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
1203
1204 if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
1205 if (!ts->reset_controller_at_probe &&
1206 ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1207 dev_info(&ts->client->dev, "Config not set, resetting controller\n");
1208 /* Retry after a controller reset */
1209 ts->reset_controller_at_probe = true;
1210 error = goodix_reset(ts);
1211 if (error)
1212 return error;
1213 goto retry_read_config;
1214 }
1215 dev_err(&ts->client->dev,
1216 "Invalid config (%d, %d, %d), using defaults\n",
1217 ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
1218 ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
1219 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
1220 ts->max_touch_num = GOODIX_MAX_CONTACTS;
1221 input_abs_set_max(ts->input_dev,
1222 ABS_MT_POSITION_X, ts->prop.max_x);
1223 input_abs_set_max(ts->input_dev,
1224 ABS_MT_POSITION_Y, ts->prop.max_y);
1225 }
1226
1227 if (dmi_check_system(nine_bytes_report)) {
1228 ts->contact_size = 9;
1229
1230 dev_dbg(&ts->client->dev,
1231 "Non-standard 9-bytes report format quirk\n");
1232 }
1233
1234 if (dmi_check_system(inverted_x_screen)) {
1235 ts->prop.invert_x = true;
1236 dev_dbg(&ts->client->dev,
1237 "Applying 'inverted x screen' quirk\n");
1238 }
1239
1240 error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
1241 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1242 if (error) {
1243 dev_err(&ts->client->dev,
1244 "Failed to initialize MT slots: %d", error);
1245 return error;
1246 }
1247
1248 input_set_drvdata(ts->input_dev, ts);
1249
1250 if (!ts->client->irq) {
1251 error = input_setup_polling(ts->input_dev, goodix_ts_work_i2c_poll);
1252 if (error) {
1253 dev_err(&ts->client->dev,
1254 "could not set up polling mode, %d\n", error);
1255 return error;
1256 }
1257 input_set_poll_interval(ts->input_dev, GOODIX_POLL_INTERVAL_MS);
1258 }
1259
1260 error = input_register_device(ts->input_dev);
1261 if (error) {
1262 dev_err(&ts->client->dev,
1263 "Failed to register input device: %d", error);
1264 return error;
1265 }
1266
1267 /*
1268 * Create the input_pen device before goodix_request_irq() calls
1269 * devm_request_threaded_irq() so that the devm framework frees
1270 * it after disabling the irq.
1271 * Unfortunately there is no way to detect if the touchscreen has pen
1272 * support, so registering the dev is delayed till the first pen event.
1273 */
1274 error = goodix_create_pen_input(ts);
1275 if (error)
1276 return error;
1277
1278 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
1279 error = goodix_request_irq(ts);
1280 if (error) {
1281 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
1282 return error;
1283 }
1284
1285 return 0;
1286 }
1287
1288 /**
1289 * goodix_config_cb - Callback to finish device init
1290 *
1291 * @cfg: firmware config
1292 * @ctx: our goodix_ts_data pointer
1293 *
1294 * request_firmware_wait callback that finishes
1295 * initialization of the device.
1296 */
goodix_config_cb(const struct firmware * cfg,void * ctx)1297 static void goodix_config_cb(const struct firmware *cfg, void *ctx)
1298 {
1299 struct goodix_ts_data *ts = ctx;
1300 int error;
1301
1302 if (ts->firmware_name) {
1303 if (!cfg)
1304 goto err_release_cfg;
1305
1306 error = goodix_check_cfg(ts, cfg->data, cfg->size);
1307 if (error)
1308 goto err_release_cfg;
1309
1310 memcpy(ts->config, cfg->data, cfg->size);
1311 } else if (cfg) {
1312 /* send device configuration to the firmware */
1313 error = goodix_send_cfg(ts, cfg->data, cfg->size);
1314 if (error)
1315 goto err_release_cfg;
1316 }
1317
1318 goodix_configure_dev(ts);
1319
1320 err_release_cfg:
1321 release_firmware(cfg);
1322 complete_all(&ts->firmware_loading_complete);
1323 }
1324
goodix_disable_regulators(void * arg)1325 static void goodix_disable_regulators(void *arg)
1326 {
1327 struct goodix_ts_data *ts = arg;
1328
1329 regulator_disable(ts->vddio);
1330 regulator_disable(ts->avdd28);
1331 }
1332
goodix_ts_probe(struct i2c_client * client)1333 static int goodix_ts_probe(struct i2c_client *client)
1334 {
1335 struct goodix_ts_data *ts;
1336 const char *cfg_name;
1337 int error;
1338
1339 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
1340
1341 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1342 dev_err(&client->dev, "I2C check functionality failed.\n");
1343 return -ENXIO;
1344 }
1345
1346 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1347 if (!ts)
1348 return -ENOMEM;
1349
1350 ts->client = client;
1351 i2c_set_clientdata(client, ts);
1352 init_completion(&ts->firmware_loading_complete);
1353 ts->contact_size = GOODIX_CONTACT_SIZE;
1354
1355 error = goodix_get_gpio_config(ts);
1356 if (error)
1357 return error;
1358
1359 /* power up the controller */
1360 error = regulator_enable(ts->avdd28);
1361 if (error) {
1362 dev_err(&client->dev,
1363 "Failed to enable AVDD28 regulator: %d\n",
1364 error);
1365 return error;
1366 }
1367
1368 error = regulator_enable(ts->vddio);
1369 if (error) {
1370 dev_err(&client->dev,
1371 "Failed to enable VDDIO regulator: %d\n",
1372 error);
1373 regulator_disable(ts->avdd28);
1374 return error;
1375 }
1376
1377 error = devm_add_action_or_reset(&client->dev,
1378 goodix_disable_regulators, ts);
1379 if (error)
1380 return error;
1381
1382 reset:
1383 if (ts->reset_controller_at_probe) {
1384 /* reset the controller */
1385 error = goodix_reset(ts);
1386 if (error)
1387 return error;
1388 }
1389
1390 error = goodix_i2c_test(client);
1391 if (error) {
1392 if (!ts->reset_controller_at_probe &&
1393 ts->irq_pin_access_method != IRQ_PIN_ACCESS_NONE) {
1394 /* Retry after a controller reset */
1395 ts->reset_controller_at_probe = true;
1396 goto reset;
1397 }
1398 dev_err(&client->dev, "I2C communication failure: %d\n", error);
1399 return error;
1400 }
1401
1402 error = goodix_firmware_check(ts);
1403 if (error)
1404 return error;
1405
1406 error = goodix_read_version(ts);
1407 if (error)
1408 return error;
1409
1410 ts->chip = goodix_get_chip_data(ts->id);
1411
1412 if (ts->load_cfg_from_disk) {
1413 /* update device config */
1414 error = device_property_read_string(&client->dev,
1415 "goodix,config-name",
1416 &cfg_name);
1417 if (!error)
1418 snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1419 "goodix/%s", cfg_name);
1420 else
1421 snprintf(ts->cfg_name, sizeof(ts->cfg_name),
1422 "goodix_%s_cfg.bin", ts->id);
1423
1424 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
1425 &client->dev, GFP_KERNEL, ts,
1426 goodix_config_cb);
1427 if (error) {
1428 dev_err(&client->dev,
1429 "Failed to invoke firmware loader: %d\n",
1430 error);
1431 return error;
1432 }
1433
1434 return 0;
1435 } else {
1436 error = goodix_configure_dev(ts);
1437 if (error)
1438 return error;
1439 }
1440
1441 return 0;
1442 }
1443
goodix_ts_remove(struct i2c_client * client)1444 static void goodix_ts_remove(struct i2c_client *client)
1445 {
1446 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1447
1448 if (ts->load_cfg_from_disk)
1449 wait_for_completion(&ts->firmware_loading_complete);
1450 }
1451
goodix_suspend(struct device * dev)1452 static int goodix_suspend(struct device *dev)
1453 {
1454 struct i2c_client *client = to_i2c_client(dev);
1455 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1456 int error;
1457
1458 if (ts->load_cfg_from_disk)
1459 wait_for_completion(&ts->firmware_loading_complete);
1460
1461 /* We need gpio pins to suspend/resume */
1462 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1463 goodix_disable_irq(ts);
1464 return 0;
1465 }
1466
1467 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
1468 goodix_free_irq(ts);
1469
1470 /* Save reference (calibration) info if necessary */
1471 goodix_save_bak_ref(ts);
1472
1473 /* Output LOW on the INT pin for 5 ms */
1474 error = goodix_irq_direction_output(ts, 0);
1475 if (error) {
1476 goodix_request_irq(ts);
1477 return error;
1478 }
1479
1480 usleep_range(5000, 6000);
1481
1482 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
1483 GOODIX_CMD_SCREEN_OFF);
1484 if (error) {
1485 goodix_irq_direction_input(ts);
1486 goodix_request_irq(ts);
1487 return -EAGAIN;
1488 }
1489
1490 /*
1491 * The datasheet specifies that the interval between sending screen-off
1492 * command and wake-up should be longer than 58 ms. To avoid waking up
1493 * sooner, delay 58ms here.
1494 */
1495 msleep(58);
1496 return 0;
1497 }
1498
goodix_resume(struct device * dev)1499 static int goodix_resume(struct device *dev)
1500 {
1501 struct i2c_client *client = to_i2c_client(dev);
1502 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1503 u8 config_ver;
1504 int error;
1505
1506 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
1507 goodix_enable_irq(ts);
1508 return 0;
1509 }
1510
1511 /*
1512 * Exit sleep mode by outputting HIGH level to INT pin
1513 * for 2ms~5ms.
1514 */
1515 error = goodix_irq_direction_output(ts, 1);
1516 if (error)
1517 return error;
1518
1519 usleep_range(2000, 5000);
1520
1521 error = goodix_int_sync(ts);
1522 if (error)
1523 return error;
1524
1525 error = goodix_i2c_read(ts->client, ts->chip->config_addr,
1526 &config_ver, 1);
1527 if (!error && config_ver != ts->config[0])
1528 dev_info(dev, "Config version mismatch %d != %d, resetting controller\n",
1529 config_ver, ts->config[0]);
1530
1531 if (error != 0 || config_ver != ts->config[0]) {
1532 error = goodix_reset(ts);
1533 if (error)
1534 return error;
1535
1536 error = goodix_send_cfg(ts, ts->config, ts->chip->config_len);
1537 if (error)
1538 return error;
1539 }
1540
1541 error = goodix_request_irq(ts);
1542 if (error)
1543 return error;
1544
1545 return 0;
1546 }
1547
1548 static DEFINE_SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1549
1550 static const struct i2c_device_id goodix_ts_id[] = {
1551 { "GDIX1001:00" },
1552 { }
1553 };
1554 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
1555
1556 #ifdef CONFIG_ACPI
1557 static const struct acpi_device_id goodix_acpi_match[] = {
1558 { "GDIX1001", 0 },
1559 { "GDIX1002", 0 },
1560 { "GDX9110", 0 },
1561 { }
1562 };
1563 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
1564 #endif
1565
1566 #ifdef CONFIG_OF
1567 static const struct of_device_id goodix_of_match[] = {
1568 { .compatible = "goodix,gt1151" },
1569 { .compatible = "goodix,gt1158" },
1570 { .compatible = "goodix,gt5663" },
1571 { .compatible = "goodix,gt5688" },
1572 { .compatible = "goodix,gt911" },
1573 { .compatible = "goodix,gt9110" },
1574 { .compatible = "goodix,gt912" },
1575 { .compatible = "goodix,gt9147" },
1576 { .compatible = "goodix,gt917s" },
1577 { .compatible = "goodix,gt927" },
1578 { .compatible = "goodix,gt9271" },
1579 { .compatible = "goodix,gt928" },
1580 { .compatible = "goodix,gt9286" },
1581 { .compatible = "goodix,gt967" },
1582 { }
1583 };
1584 MODULE_DEVICE_TABLE(of, goodix_of_match);
1585 #endif
1586
1587 static struct i2c_driver goodix_ts_driver = {
1588 .probe = goodix_ts_probe,
1589 .remove = goodix_ts_remove,
1590 .id_table = goodix_ts_id,
1591 .driver = {
1592 .name = "Goodix-TS",
1593 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
1594 .of_match_table = of_match_ptr(goodix_of_match),
1595 .pm = pm_sleep_ptr(&goodix_pm_ops),
1596 },
1597 };
1598 module_i2c_driver(goodix_ts_driver);
1599
1600 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1601 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1602 MODULE_DESCRIPTION("Goodix touchscreen driver");
1603 MODULE_LICENSE("GPL v2");
1604