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