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