]>
Commit | Line | Data |
---|---|---|
ac1dc6b2 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
66aee900 SL |
2 | /* |
3 | * Elan Microelectronics touch panels with I2C interface | |
4 | * | |
5 | * Copyright (C) 2014 Elan Microelectronics Corporation. | |
6 | * Scott Liu <scott.liu@emc.com.tw> | |
7 | * | |
8 | * This code is partly based on hid-multitouch.c: | |
9 | * | |
10 | * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr> | |
11 | * Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com> | |
12 | * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France | |
13 | * | |
66aee900 SL |
14 | * This code is partly based on i2c-hid.c: |
15 | * | |
16 | * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com> | |
17 | * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France | |
18 | * Copyright (c) 2012 Red Hat, Inc | |
19 | */ | |
20 | ||
66aee900 | 21 | |
f27ad893 | 22 | #include <linux/bits.h> |
66aee900 SL |
23 | #include <linux/module.h> |
24 | #include <linux/input.h> | |
25 | #include <linux/interrupt.h> | |
4c83c071 | 26 | #include <linux/irq.h> |
66aee900 SL |
27 | #include <linux/platform_device.h> |
28 | #include <linux/async.h> | |
29 | #include <linux/i2c.h> | |
30 | #include <linux/delay.h> | |
31 | #include <linux/uaccess.h> | |
32 | #include <linux/buffer_head.h> | |
66aee900 SL |
33 | #include <linux/slab.h> |
34 | #include <linux/firmware.h> | |
66aee900 SL |
35 | #include <linux/input/mt.h> |
36 | #include <linux/acpi.h> | |
37 | #include <linux/of.h> | |
afe10358 DT |
38 | #include <linux/gpio/consumer.h> |
39 | #include <linux/regulator/consumer.h> | |
66aee900 SL |
40 | #include <asm/unaligned.h> |
41 | ||
42 | /* Device, Driver information */ | |
43 | #define DEVICE_NAME "elants_i2c" | |
66aee900 SL |
44 | |
45 | /* Convert from rows or columns into resolution */ | |
46 | #define ELAN_TS_RESOLUTION(n, m) (((n) - 1) * (m)) | |
47 | ||
48 | /* FW header data */ | |
49 | #define HEADER_SIZE 4 | |
50 | #define FW_HDR_TYPE 0 | |
51 | #define FW_HDR_COUNT 1 | |
52 | #define FW_HDR_LENGTH 2 | |
53 | ||
54 | /* Buffer mode Queue Header information */ | |
55 | #define QUEUE_HEADER_SINGLE 0x62 | |
56 | #define QUEUE_HEADER_NORMAL 0X63 | |
57 | #define QUEUE_HEADER_WAIT 0x64 | |
58 | ||
59 | /* Command header definition */ | |
60 | #define CMD_HEADER_WRITE 0x54 | |
61 | #define CMD_HEADER_READ 0x53 | |
62 | #define CMD_HEADER_6B_READ 0x5B | |
f0b57e19 | 63 | #define CMD_HEADER_ROM_READ 0x96 |
66aee900 SL |
64 | #define CMD_HEADER_RESP 0x52 |
65 | #define CMD_HEADER_6B_RESP 0x9B | |
f0b57e19 | 66 | #define CMD_HEADER_ROM_RESP 0x95 |
66aee900 SL |
67 | #define CMD_HEADER_HELLO 0x55 |
68 | #define CMD_HEADER_REK 0x66 | |
69 | ||
70 | /* FW position data */ | |
71 | #define PACKET_SIZE 55 | |
72 | #define MAX_CONTACT_NUM 10 | |
73 | #define FW_POS_HEADER 0 | |
74 | #define FW_POS_STATE 1 | |
75 | #define FW_POS_TOTAL 2 | |
76 | #define FW_POS_XY 3 | |
f27ad893 | 77 | #define FW_POS_TOOL_TYPE 33 |
66aee900 SL |
78 | #define FW_POS_CHECKSUM 34 |
79 | #define FW_POS_WIDTH 35 | |
80 | #define FW_POS_PRESSURE 45 | |
81 | ||
82 | #define HEADER_REPORT_10_FINGER 0x62 | |
83 | ||
84 | /* Header (4 bytes) plus 3 fill 10-finger packets */ | |
85 | #define MAX_PACKET_SIZE 169 | |
86 | ||
87 | #define BOOT_TIME_DELAY_MS 50 | |
88 | ||
89 | /* FW read command, 0x53 0x?? 0x0, 0x01 */ | |
90 | #define E_ELAN_INFO_FW_VER 0x00 | |
91 | #define E_ELAN_INFO_BC_VER 0x10 | |
92 | #define E_ELAN_INFO_TEST_VER 0xE0 | |
93 | #define E_ELAN_INFO_FW_ID 0xF0 | |
94 | #define E_INFO_OSR 0xD6 | |
95 | #define E_INFO_PHY_SCAN 0xD7 | |
96 | #define E_INFO_PHY_DRIVER 0xD8 | |
97 | ||
98 | #define MAX_RETRIES 3 | |
99 | #define MAX_FW_UPDATE_RETRIES 30 | |
100 | ||
101 | #define ELAN_FW_PAGESIZE 132 | |
66aee900 SL |
102 | |
103 | /* calibration timeout definition */ | |
22c15e5e | 104 | #define ELAN_CALI_TIMEOUT_MSEC 12000 |
66aee900 | 105 | |
afe10358 DT |
106 | #define ELAN_POWERON_DELAY_USEC 500 |
107 | #define ELAN_RESET_DELAY_MSEC 20 | |
108 | ||
66aee900 SL |
109 | enum elants_state { |
110 | ELAN_STATE_NORMAL, | |
111 | ELAN_WAIT_QUEUE_HEADER, | |
112 | ELAN_WAIT_RECALIBRATION, | |
113 | }; | |
114 | ||
115 | enum elants_iap_mode { | |
116 | ELAN_IAP_OPERATIONAL, | |
117 | ELAN_IAP_RECOVERY, | |
118 | }; | |
119 | ||
120 | /* struct elants_data - represents state of Elan touchscreen device */ | |
121 | struct elants_data { | |
122 | struct i2c_client *client; | |
123 | struct input_dev *input; | |
124 | ||
afe10358 DT |
125 | struct regulator *vcc33; |
126 | struct regulator *vccio; | |
127 | struct gpio_desc *reset_gpio; | |
128 | ||
66aee900 SL |
129 | u16 fw_version; |
130 | u8 test_version; | |
131 | u8 solution_version; | |
132 | u8 bc_version; | |
133 | u8 iap_version; | |
134 | u16 hw_version; | |
135 | unsigned int x_res; /* resolution in units/mm */ | |
136 | unsigned int y_res; | |
137 | unsigned int x_max; | |
138 | unsigned int y_max; | |
139 | ||
140 | enum elants_state state; | |
141 | enum elants_iap_mode iap_mode; | |
142 | ||
143 | /* Guards against concurrent access to the device via sysfs */ | |
144 | struct mutex sysfs_mutex; | |
145 | ||
146 | u8 cmd_resp[HEADER_SIZE]; | |
147 | struct completion cmd_done; | |
148 | ||
66aee900 | 149 | bool wake_irq_enabled; |
afe10358 | 150 | bool keep_power_in_suspend; |
00f73f97 SB |
151 | |
152 | /* Must be last to be used for DMA operations */ | |
153 | u8 buf[MAX_PACKET_SIZE] ____cacheline_aligned; | |
66aee900 SL |
154 | }; |
155 | ||
156 | static int elants_i2c_send(struct i2c_client *client, | |
157 | const void *data, size_t size) | |
158 | { | |
159 | int ret; | |
160 | ||
161 | ret = i2c_master_send(client, data, size); | |
162 | if (ret == size) | |
163 | return 0; | |
164 | ||
165 | if (ret >= 0) | |
166 | ret = -EIO; | |
167 | ||
168 | dev_err(&client->dev, "%s failed (%*ph): %d\n", | |
169 | __func__, (int)size, data, ret); | |
170 | ||
171 | return ret; | |
172 | } | |
173 | ||
174 | static int elants_i2c_read(struct i2c_client *client, void *data, size_t size) | |
175 | { | |
176 | int ret; | |
177 | ||
178 | ret = i2c_master_recv(client, data, size); | |
179 | if (ret == size) | |
180 | return 0; | |
181 | ||
182 | if (ret >= 0) | |
183 | ret = -EIO; | |
184 | ||
185 | dev_err(&client->dev, "%s failed: %d\n", __func__, ret); | |
186 | ||
187 | return ret; | |
188 | } | |
189 | ||
190 | static int elants_i2c_execute_command(struct i2c_client *client, | |
191 | const u8 *cmd, size_t cmd_size, | |
192 | u8 *resp, size_t resp_size) | |
193 | { | |
194 | struct i2c_msg msgs[2]; | |
195 | int ret; | |
196 | u8 expected_response; | |
197 | ||
198 | switch (cmd[0]) { | |
199 | case CMD_HEADER_READ: | |
200 | expected_response = CMD_HEADER_RESP; | |
201 | break; | |
202 | ||
203 | case CMD_HEADER_6B_READ: | |
204 | expected_response = CMD_HEADER_6B_RESP; | |
205 | break; | |
206 | ||
f0b57e19 JC |
207 | case CMD_HEADER_ROM_READ: |
208 | expected_response = CMD_HEADER_ROM_RESP; | |
209 | break; | |
210 | ||
66aee900 SL |
211 | default: |
212 | dev_err(&client->dev, "%s: invalid command %*ph\n", | |
213 | __func__, (int)cmd_size, cmd); | |
214 | return -EINVAL; | |
215 | } | |
216 | ||
217 | msgs[0].addr = client->addr; | |
218 | msgs[0].flags = client->flags & I2C_M_TEN; | |
219 | msgs[0].len = cmd_size; | |
220 | msgs[0].buf = (u8 *)cmd; | |
221 | ||
222 | msgs[1].addr = client->addr; | |
223 | msgs[1].flags = client->flags & I2C_M_TEN; | |
224 | msgs[1].flags |= I2C_M_RD; | |
225 | msgs[1].len = resp_size; | |
226 | msgs[1].buf = resp; | |
227 | ||
228 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | |
229 | if (ret < 0) | |
230 | return ret; | |
231 | ||
232 | if (ret != ARRAY_SIZE(msgs) || resp[FW_HDR_TYPE] != expected_response) | |
233 | return -EIO; | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | static int elants_i2c_calibrate(struct elants_data *ts) | |
239 | { | |
240 | struct i2c_client *client = ts->client; | |
241 | int ret, error; | |
242 | static const u8 w_flashkey[] = { 0x54, 0xC0, 0xE1, 0x5A }; | |
243 | static const u8 rek[] = { 0x54, 0x29, 0x00, 0x01 }; | |
244 | static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 }; | |
245 | ||
246 | disable_irq(client->irq); | |
247 | ||
248 | ts->state = ELAN_WAIT_RECALIBRATION; | |
249 | reinit_completion(&ts->cmd_done); | |
250 | ||
251 | elants_i2c_send(client, w_flashkey, sizeof(w_flashkey)); | |
252 | elants_i2c_send(client, rek, sizeof(rek)); | |
253 | ||
254 | enable_irq(client->irq); | |
255 | ||
256 | ret = wait_for_completion_interruptible_timeout(&ts->cmd_done, | |
257 | msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC)); | |
258 | ||
259 | ts->state = ELAN_STATE_NORMAL; | |
260 | ||
261 | if (ret <= 0) { | |
262 | error = ret < 0 ? ret : -ETIMEDOUT; | |
263 | dev_err(&client->dev, | |
264 | "error while waiting for calibration to complete: %d\n", | |
265 | error); | |
266 | return error; | |
267 | } | |
268 | ||
269 | if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) { | |
270 | dev_err(&client->dev, | |
271 | "unexpected calibration response: %*ph\n", | |
272 | (int)sizeof(ts->cmd_resp), ts->cmd_resp); | |
273 | return -EINVAL; | |
274 | } | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
279 | static int elants_i2c_sw_reset(struct i2c_client *client) | |
280 | { | |
281 | const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 }; | |
282 | int error; | |
283 | ||
284 | error = elants_i2c_send(client, soft_rst_cmd, | |
285 | sizeof(soft_rst_cmd)); | |
286 | if (error) { | |
287 | dev_err(&client->dev, "software reset failed: %d\n", error); | |
288 | return error; | |
289 | } | |
290 | ||
291 | /* | |
292 | * We should wait at least 10 msec (but no more than 40) before | |
293 | * sending fastboot or IAP command to the device. | |
294 | */ | |
295 | msleep(30); | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | static u16 elants_i2c_parse_version(u8 *buf) | |
301 | { | |
302 | return get_unaligned_be32(buf) >> 4; | |
303 | } | |
304 | ||
bc1d57fe | 305 | static int elants_i2c_query_hw_version(struct elants_data *ts) |
66aee900 SL |
306 | { |
307 | struct i2c_client *client = ts->client; | |
308 | int error, retry_cnt; | |
309 | const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 }; | |
310 | u8 resp[HEADER_SIZE]; | |
311 | ||
312 | for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { | |
313 | error = elants_i2c_execute_command(client, cmd, sizeof(cmd), | |
314 | resp, sizeof(resp)); | |
315 | if (!error) { | |
316 | ts->hw_version = elants_i2c_parse_version(resp); | |
317 | if (ts->hw_version != 0xffff) | |
318 | return 0; | |
319 | } | |
320 | ||
321 | dev_dbg(&client->dev, "read fw id error=%d, buf=%*phC\n", | |
322 | error, (int)sizeof(resp), resp); | |
323 | } | |
324 | ||
bc1d57fe JC |
325 | if (error) { |
326 | dev_err(&client->dev, | |
327 | "Failed to read fw id: %d\n", error); | |
328 | return error; | |
329 | } | |
330 | ||
331 | dev_err(&client->dev, "Invalid fw id: %#04x\n", ts->hw_version); | |
66aee900 SL |
332 | |
333 | return -EINVAL; | |
334 | } | |
335 | ||
336 | static int elants_i2c_query_fw_version(struct elants_data *ts) | |
337 | { | |
338 | struct i2c_client *client = ts->client; | |
339 | int error, retry_cnt; | |
340 | const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 }; | |
341 | u8 resp[HEADER_SIZE]; | |
342 | ||
343 | for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { | |
344 | error = elants_i2c_execute_command(client, cmd, sizeof(cmd), | |
345 | resp, sizeof(resp)); | |
346 | if (!error) { | |
347 | ts->fw_version = elants_i2c_parse_version(resp); | |
348 | if (ts->fw_version != 0x0000 && | |
349 | ts->fw_version != 0xffff) | |
350 | return 0; | |
351 | } | |
352 | ||
353 | dev_dbg(&client->dev, "read fw version error=%d, buf=%*phC\n", | |
354 | error, (int)sizeof(resp), resp); | |
355 | } | |
356 | ||
357 | dev_err(&client->dev, | |
358 | "Failed to read fw version or fw version is invalid\n"); | |
359 | ||
360 | return -EINVAL; | |
361 | } | |
362 | ||
363 | static int elants_i2c_query_test_version(struct elants_data *ts) | |
364 | { | |
365 | struct i2c_client *client = ts->client; | |
366 | int error, retry_cnt; | |
367 | u16 version; | |
368 | const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 }; | |
369 | u8 resp[HEADER_SIZE]; | |
370 | ||
371 | for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { | |
372 | error = elants_i2c_execute_command(client, cmd, sizeof(cmd), | |
373 | resp, sizeof(resp)); | |
374 | if (!error) { | |
375 | version = elants_i2c_parse_version(resp); | |
376 | ts->test_version = version >> 8; | |
377 | ts->solution_version = version & 0xff; | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | dev_dbg(&client->dev, | |
383 | "read test version error rc=%d, buf=%*phC\n", | |
384 | error, (int)sizeof(resp), resp); | |
385 | } | |
386 | ||
387 | dev_err(&client->dev, "Failed to read test version\n"); | |
388 | ||
389 | return -EINVAL; | |
390 | } | |
391 | ||
392 | static int elants_i2c_query_bc_version(struct elants_data *ts) | |
393 | { | |
394 | struct i2c_client *client = ts->client; | |
395 | const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 }; | |
396 | u8 resp[HEADER_SIZE]; | |
397 | u16 version; | |
398 | int error; | |
399 | ||
400 | error = elants_i2c_execute_command(client, cmd, sizeof(cmd), | |
401 | resp, sizeof(resp)); | |
402 | if (error) { | |
403 | dev_err(&client->dev, | |
404 | "read BC version error=%d, buf=%*phC\n", | |
405 | error, (int)sizeof(resp), resp); | |
406 | return error; | |
407 | } | |
408 | ||
409 | version = elants_i2c_parse_version(resp); | |
410 | ts->bc_version = version >> 8; | |
411 | ts->iap_version = version & 0xff; | |
412 | ||
413 | return 0; | |
414 | } | |
415 | ||
416 | static int elants_i2c_query_ts_info(struct elants_data *ts) | |
417 | { | |
418 | struct i2c_client *client = ts->client; | |
419 | int error; | |
420 | u8 resp[17]; | |
421 | u16 phy_x, phy_y, rows, cols, osr; | |
422 | const u8 get_resolution_cmd[] = { | |
423 | CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00 | |
424 | }; | |
425 | const u8 get_osr_cmd[] = { | |
426 | CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01 | |
427 | }; | |
428 | const u8 get_physical_scan_cmd[] = { | |
429 | CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01 | |
430 | }; | |
431 | const u8 get_physical_drive_cmd[] = { | |
432 | CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01 | |
433 | }; | |
434 | ||
435 | /* Get trace number */ | |
436 | error = elants_i2c_execute_command(client, | |
437 | get_resolution_cmd, | |
438 | sizeof(get_resolution_cmd), | |
439 | resp, sizeof(resp)); | |
440 | if (error) { | |
441 | dev_err(&client->dev, "get resolution command failed: %d\n", | |
442 | error); | |
443 | return error; | |
444 | } | |
445 | ||
446 | rows = resp[2] + resp[6] + resp[10]; | |
447 | cols = resp[3] + resp[7] + resp[11]; | |
448 | ||
449 | /* Process mm_to_pixel information */ | |
450 | error = elants_i2c_execute_command(client, | |
451 | get_osr_cmd, sizeof(get_osr_cmd), | |
452 | resp, sizeof(resp)); | |
453 | if (error) { | |
454 | dev_err(&client->dev, "get osr command failed: %d\n", | |
455 | error); | |
456 | return error; | |
457 | } | |
458 | ||
459 | osr = resp[3]; | |
460 | ||
461 | error = elants_i2c_execute_command(client, | |
462 | get_physical_scan_cmd, | |
463 | sizeof(get_physical_scan_cmd), | |
464 | resp, sizeof(resp)); | |
465 | if (error) { | |
466 | dev_err(&client->dev, "get physical scan command failed: %d\n", | |
467 | error); | |
468 | return error; | |
469 | } | |
470 | ||
471 | phy_x = get_unaligned_be16(&resp[2]); | |
472 | ||
473 | error = elants_i2c_execute_command(client, | |
474 | get_physical_drive_cmd, | |
475 | sizeof(get_physical_drive_cmd), | |
476 | resp, sizeof(resp)); | |
477 | if (error) { | |
478 | dev_err(&client->dev, "get physical drive command failed: %d\n", | |
479 | error); | |
480 | return error; | |
481 | } | |
482 | ||
483 | phy_y = get_unaligned_be16(&resp[2]); | |
484 | ||
485 | dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y); | |
486 | ||
487 | if (rows == 0 || cols == 0 || osr == 0) { | |
488 | dev_warn(&client->dev, | |
489 | "invalid trace number data: %d, %d, %d\n", | |
490 | rows, cols, osr); | |
491 | } else { | |
492 | /* translate trace number to TS resolution */ | |
493 | ts->x_max = ELAN_TS_RESOLUTION(rows, osr); | |
494 | ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x); | |
495 | ts->y_max = ELAN_TS_RESOLUTION(cols, osr); | |
496 | ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y); | |
497 | } | |
498 | ||
499 | return 0; | |
500 | } | |
501 | ||
502 | static int elants_i2c_fastboot(struct i2c_client *client) | |
503 | { | |
504 | const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E }; | |
505 | int error; | |
506 | ||
507 | error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd)); | |
508 | if (error) { | |
509 | dev_err(&client->dev, "boot failed: %d\n", error); | |
510 | return error; | |
511 | } | |
512 | ||
513 | dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr); | |
514 | return 0; | |
515 | } | |
516 | ||
517 | static int elants_i2c_initialize(struct elants_data *ts) | |
518 | { | |
519 | struct i2c_client *client = ts->client; | |
bc1d57fe | 520 | int error, error2, retry_cnt; |
66aee900 SL |
521 | const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 }; |
522 | const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 }; | |
523 | u8 buf[HEADER_SIZE]; | |
524 | ||
525 | for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { | |
526 | error = elants_i2c_sw_reset(client); | |
527 | if (error) { | |
528 | /* Continue initializing if it's the last try */ | |
529 | if (retry_cnt < MAX_RETRIES - 1) | |
530 | continue; | |
531 | } | |
532 | ||
533 | error = elants_i2c_fastboot(client); | |
534 | if (error) { | |
535 | /* Continue initializing if it's the last try */ | |
536 | if (retry_cnt < MAX_RETRIES - 1) | |
537 | continue; | |
538 | } | |
539 | ||
540 | /* Wait for Hello packet */ | |
541 | msleep(BOOT_TIME_DELAY_MS); | |
542 | ||
543 | error = elants_i2c_read(client, buf, sizeof(buf)); | |
544 | if (error) { | |
545 | dev_err(&client->dev, | |
546 | "failed to read 'hello' packet: %d\n", error); | |
547 | } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) { | |
548 | ts->iap_mode = ELAN_IAP_OPERATIONAL; | |
549 | break; | |
550 | } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) { | |
551 | /* | |
552 | * Setting error code will mark device | |
553 | * in recovery mode below. | |
554 | */ | |
555 | error = -EIO; | |
556 | break; | |
557 | } else { | |
558 | error = -EINVAL; | |
559 | dev_err(&client->dev, | |
560 | "invalid 'hello' packet: %*ph\n", | |
561 | (int)sizeof(buf), buf); | |
562 | } | |
563 | } | |
564 | ||
bc1d57fe JC |
565 | /* hw version is available even if device in recovery state */ |
566 | error2 = elants_i2c_query_hw_version(ts); | |
f0b57e19 JC |
567 | if (!error2) |
568 | error2 = elants_i2c_query_bc_version(ts); | |
66aee900 | 569 | if (!error) |
bc1d57fe JC |
570 | error = error2; |
571 | ||
66aee900 SL |
572 | if (!error) |
573 | error = elants_i2c_query_fw_version(ts); | |
bc1d57fe JC |
574 | if (!error) |
575 | error = elants_i2c_query_test_version(ts); | |
bc1d57fe JC |
576 | if (!error) |
577 | error = elants_i2c_query_ts_info(ts); | |
66aee900 | 578 | |
bc1d57fe | 579 | if (error) |
66aee900 | 580 | ts->iap_mode = ELAN_IAP_RECOVERY; |
66aee900 SL |
581 | |
582 | return 0; | |
583 | } | |
584 | ||
585 | /* | |
586 | * Firmware update interface. | |
587 | */ | |
588 | ||
589 | static int elants_i2c_fw_write_page(struct i2c_client *client, | |
590 | const void *page) | |
591 | { | |
592 | const u8 ack_ok[] = { 0xaa, 0xaa }; | |
593 | u8 buf[2]; | |
594 | int retry; | |
595 | int error; | |
596 | ||
597 | for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) { | |
598 | error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE); | |
599 | if (error) { | |
600 | dev_err(&client->dev, | |
601 | "IAP Write Page failed: %d\n", error); | |
602 | continue; | |
603 | } | |
604 | ||
605 | error = elants_i2c_read(client, buf, 2); | |
606 | if (error) { | |
607 | dev_err(&client->dev, | |
608 | "IAP Ack read failed: %d\n", error); | |
609 | return error; | |
610 | } | |
611 | ||
612 | if (!memcmp(buf, ack_ok, sizeof(ack_ok))) | |
613 | return 0; | |
614 | ||
615 | error = -EIO; | |
616 | dev_err(&client->dev, | |
617 | "IAP Get Ack Error [%02x:%02x]\n", | |
618 | buf[0], buf[1]); | |
619 | } | |
620 | ||
621 | return error; | |
622 | } | |
623 | ||
f0b57e19 JC |
624 | static int elants_i2c_validate_remark_id(struct elants_data *ts, |
625 | const struct firmware *fw) | |
626 | { | |
627 | struct i2c_client *client = ts->client; | |
628 | int error; | |
629 | const u8 cmd[] = { CMD_HEADER_ROM_READ, 0x80, 0x1F, 0x00, 0x00, 0x21 }; | |
630 | u8 resp[6] = { 0 }; | |
631 | u16 ts_remark_id = 0; | |
632 | u16 fw_remark_id = 0; | |
633 | ||
634 | /* Compare TS Remark ID and FW Remark ID */ | |
635 | error = elants_i2c_execute_command(client, cmd, sizeof(cmd), | |
636 | resp, sizeof(resp)); | |
637 | if (error) { | |
638 | dev_err(&client->dev, "failed to query Remark ID: %d\n", error); | |
639 | return error; | |
640 | } | |
641 | ||
642 | ts_remark_id = get_unaligned_be16(&resp[3]); | |
643 | ||
644 | fw_remark_id = get_unaligned_le16(&fw->data[fw->size - 4]); | |
645 | ||
646 | if (fw_remark_id != ts_remark_id) { | |
647 | dev_err(&client->dev, | |
648 | "Remark ID Mismatched: ts_remark_id=0x%04x, fw_remark_id=0x%04x.\n", | |
649 | ts_remark_id, fw_remark_id); | |
650 | return -EINVAL; | |
651 | } | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
66aee900 SL |
656 | static int elants_i2c_do_update_firmware(struct i2c_client *client, |
657 | const struct firmware *fw, | |
658 | bool force) | |
659 | { | |
f0b57e19 | 660 | struct elants_data *ts = i2c_get_clientdata(client); |
66aee900 SL |
661 | const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 }; |
662 | const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 }; | |
663 | const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc }; | |
f0b57e19 | 664 | const u8 close_idle[] = { 0x54, 0x2c, 0x01, 0x01 }; |
66aee900 SL |
665 | u8 buf[HEADER_SIZE]; |
666 | u16 send_id; | |
667 | int page, n_fw_pages; | |
668 | int error; | |
f0b57e19 | 669 | bool check_remark_id = ts->iap_version >= 0x60; |
66aee900 SL |
670 | |
671 | /* Recovery mode detection! */ | |
672 | if (force) { | |
673 | dev_dbg(&client->dev, "Recovery mode procedure\n"); | |
f0b57e19 JC |
674 | |
675 | if (check_remark_id) { | |
676 | error = elants_i2c_validate_remark_id(ts, fw); | |
677 | if (error) | |
678 | return error; | |
679 | } | |
680 | ||
66aee900 | 681 | error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2)); |
f0b57e19 JC |
682 | if (error) { |
683 | dev_err(&client->dev, "failed to enter IAP mode: %d\n", | |
684 | error); | |
685 | return error; | |
686 | } | |
66aee900 SL |
687 | } else { |
688 | /* Start IAP Procedure */ | |
689 | dev_dbg(&client->dev, "Normal IAP procedure\n"); | |
f0b57e19 | 690 | |
6fd38502 JC |
691 | /* Close idle mode */ |
692 | error = elants_i2c_send(client, close_idle, sizeof(close_idle)); | |
693 | if (error) | |
694 | dev_err(&client->dev, "Failed close idle: %d\n", error); | |
695 | msleep(60); | |
f0b57e19 | 696 | |
66aee900 | 697 | elants_i2c_sw_reset(client); |
6fd38502 | 698 | msleep(20); |
66aee900 | 699 | |
f0b57e19 JC |
700 | if (check_remark_id) { |
701 | error = elants_i2c_validate_remark_id(ts, fw); | |
702 | if (error) | |
703 | return error; | |
704 | } | |
705 | ||
706 | error = elants_i2c_send(client, enter_iap, sizeof(enter_iap)); | |
707 | if (error) { | |
708 | dev_err(&client->dev, "failed to enter IAP mode: %d\n", | |
709 | error); | |
710 | return error; | |
711 | } | |
66aee900 SL |
712 | } |
713 | ||
714 | msleep(20); | |
715 | ||
716 | /* check IAP state */ | |
717 | error = elants_i2c_read(client, buf, 4); | |
718 | if (error) { | |
719 | dev_err(&client->dev, | |
720 | "failed to read IAP acknowledgement: %d\n", | |
721 | error); | |
722 | return error; | |
723 | } | |
724 | ||
725 | if (memcmp(buf, iap_ack, sizeof(iap_ack))) { | |
726 | dev_err(&client->dev, | |
727 | "failed to enter IAP: %*ph (expected %*ph)\n", | |
728 | (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack); | |
729 | return -EIO; | |
730 | } | |
731 | ||
732 | dev_info(&client->dev, "successfully entered IAP mode"); | |
733 | ||
734 | send_id = client->addr; | |
735 | error = elants_i2c_send(client, &send_id, 1); | |
736 | if (error) { | |
737 | dev_err(&client->dev, "sending dummy byte failed: %d\n", | |
738 | error); | |
739 | return error; | |
740 | } | |
741 | ||
742 | /* Clear the last page of Master */ | |
743 | error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE); | |
744 | if (error) { | |
745 | dev_err(&client->dev, "clearing of the last page failed: %d\n", | |
746 | error); | |
747 | return error; | |
748 | } | |
749 | ||
750 | error = elants_i2c_read(client, buf, 2); | |
751 | if (error) { | |
752 | dev_err(&client->dev, | |
753 | "failed to read ACK for clearing the last page: %d\n", | |
754 | error); | |
755 | return error; | |
756 | } | |
757 | ||
758 | n_fw_pages = fw->size / ELAN_FW_PAGESIZE; | |
759 | dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages); | |
760 | ||
761 | for (page = 0; page < n_fw_pages; page++) { | |
762 | error = elants_i2c_fw_write_page(client, | |
763 | fw->data + page * ELAN_FW_PAGESIZE); | |
764 | if (error) { | |
765 | dev_err(&client->dev, | |
766 | "failed to write FW page %d: %d\n", | |
767 | page, error); | |
768 | return error; | |
769 | } | |
770 | } | |
771 | ||
772 | /* Old iap needs to wait 200ms for WDT and rest is for hello packets */ | |
773 | msleep(300); | |
774 | ||
775 | dev_info(&client->dev, "firmware update completed\n"); | |
776 | return 0; | |
777 | } | |
778 | ||
779 | static int elants_i2c_fw_update(struct elants_data *ts) | |
780 | { | |
781 | struct i2c_client *client = ts->client; | |
782 | const struct firmware *fw; | |
37dee1ac | 783 | char *fw_name; |
66aee900 SL |
784 | int error; |
785 | ||
8c0776a8 | 786 | fw_name = kasprintf(GFP_KERNEL, "elants_i2c_%04x.bin", ts->hw_version); |
37dee1ac CM |
787 | if (!fw_name) |
788 | return -ENOMEM; | |
789 | ||
790 | dev_info(&client->dev, "requesting fw name = %s\n", fw_name); | |
791 | error = request_firmware(&fw, fw_name, &client->dev); | |
792 | kfree(fw_name); | |
66aee900 | 793 | if (error) { |
37dee1ac CM |
794 | dev_err(&client->dev, "failed to request firmware: %d\n", |
795 | error); | |
66aee900 SL |
796 | return error; |
797 | } | |
798 | ||
799 | if (fw->size % ELAN_FW_PAGESIZE) { | |
800 | dev_err(&client->dev, "invalid firmware length: %zu\n", | |
801 | fw->size); | |
802 | error = -EINVAL; | |
803 | goto out; | |
804 | } | |
805 | ||
806 | disable_irq(client->irq); | |
807 | ||
808 | error = elants_i2c_do_update_firmware(client, fw, | |
809 | ts->iap_mode == ELAN_IAP_RECOVERY); | |
810 | if (error) { | |
811 | dev_err(&client->dev, "firmware update failed: %d\n", error); | |
812 | ts->iap_mode = ELAN_IAP_RECOVERY; | |
813 | goto out_enable_irq; | |
814 | } | |
815 | ||
816 | error = elants_i2c_initialize(ts); | |
817 | if (error) { | |
818 | dev_err(&client->dev, | |
819 | "failed to initialize device after firmware update: %d\n", | |
820 | error); | |
821 | ts->iap_mode = ELAN_IAP_RECOVERY; | |
822 | goto out_enable_irq; | |
823 | } | |
824 | ||
825 | ts->iap_mode = ELAN_IAP_OPERATIONAL; | |
826 | ||
827 | out_enable_irq: | |
828 | ts->state = ELAN_STATE_NORMAL; | |
829 | enable_irq(client->irq); | |
830 | msleep(100); | |
831 | ||
832 | if (!error) | |
833 | elants_i2c_calibrate(ts); | |
834 | out: | |
835 | release_firmware(fw); | |
836 | return error; | |
837 | } | |
838 | ||
839 | /* | |
840 | * Event reporting. | |
841 | */ | |
842 | ||
843 | static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf) | |
844 | { | |
845 | struct input_dev *input = ts->input; | |
846 | unsigned int n_fingers; | |
f27ad893 | 847 | unsigned int tool_type; |
66aee900 SL |
848 | u16 finger_state; |
849 | int i; | |
850 | ||
851 | n_fingers = buf[FW_POS_STATE + 1] & 0x0f; | |
852 | finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) | | |
853 | buf[FW_POS_STATE]; | |
854 | ||
855 | dev_dbg(&ts->client->dev, | |
856 | "n_fingers: %u, state: %04x\n", n_fingers, finger_state); | |
857 | ||
f27ad893 JC |
858 | /* Note: all fingers have the same tool type */ |
859 | tool_type = buf[FW_POS_TOOL_TYPE] & BIT(0) ? | |
860 | MT_TOOL_FINGER : MT_TOOL_PALM; | |
861 | ||
66aee900 SL |
862 | for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) { |
863 | if (finger_state & 1) { | |
864 | unsigned int x, y, p, w; | |
865 | u8 *pos; | |
866 | ||
867 | pos = &buf[FW_POS_XY + i * 3]; | |
868 | x = (((u16)pos[0] & 0xf0) << 4) | pos[1]; | |
869 | y = (((u16)pos[0] & 0x0f) << 8) | pos[2]; | |
870 | p = buf[FW_POS_PRESSURE + i]; | |
871 | w = buf[FW_POS_WIDTH + i]; | |
872 | ||
873 | dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n", | |
874 | i, x, y, p, w); | |
875 | ||
876 | input_mt_slot(input, i); | |
f27ad893 | 877 | input_mt_report_slot_state(input, tool_type, true); |
66aee900 SL |
878 | input_event(input, EV_ABS, ABS_MT_POSITION_X, x); |
879 | input_event(input, EV_ABS, ABS_MT_POSITION_Y, y); | |
880 | input_event(input, EV_ABS, ABS_MT_PRESSURE, p); | |
881 | input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w); | |
882 | ||
883 | n_fingers--; | |
884 | } | |
885 | ||
886 | finger_state >>= 1; | |
887 | } | |
888 | ||
889 | input_mt_sync_frame(input); | |
890 | input_sync(input); | |
891 | } | |
892 | ||
893 | static u8 elants_i2c_calculate_checksum(u8 *buf) | |
894 | { | |
895 | u8 checksum = 0; | |
896 | u8 i; | |
897 | ||
898 | for (i = 0; i < FW_POS_CHECKSUM; i++) | |
899 | checksum += buf[i]; | |
900 | ||
901 | return checksum; | |
902 | } | |
903 | ||
904 | static void elants_i2c_event(struct elants_data *ts, u8 *buf) | |
905 | { | |
906 | u8 checksum = elants_i2c_calculate_checksum(buf); | |
907 | ||
908 | if (unlikely(buf[FW_POS_CHECKSUM] != checksum)) | |
909 | dev_warn(&ts->client->dev, | |
910 | "%s: invalid checksum for packet %02x: %02x vs. %02x\n", | |
911 | __func__, buf[FW_POS_HEADER], | |
912 | checksum, buf[FW_POS_CHECKSUM]); | |
913 | else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER)) | |
914 | dev_warn(&ts->client->dev, | |
915 | "%s: unknown packet type: %02x\n", | |
916 | __func__, buf[FW_POS_HEADER]); | |
917 | else | |
918 | elants_i2c_mt_event(ts, buf); | |
919 | } | |
920 | ||
921 | static irqreturn_t elants_i2c_irq(int irq, void *_dev) | |
922 | { | |
923 | const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 }; | |
924 | struct elants_data *ts = _dev; | |
925 | struct i2c_client *client = ts->client; | |
926 | int report_count, report_len; | |
927 | int i; | |
928 | int len; | |
929 | ||
00f73f97 | 930 | len = i2c_master_recv_dmasafe(client, ts->buf, sizeof(ts->buf)); |
66aee900 SL |
931 | if (len < 0) { |
932 | dev_err(&client->dev, "%s: failed to read data: %d\n", | |
933 | __func__, len); | |
934 | goto out; | |
935 | } | |
936 | ||
937 | dev_dbg(&client->dev, "%s: packet %*ph\n", | |
938 | __func__, HEADER_SIZE, ts->buf); | |
939 | ||
940 | switch (ts->state) { | |
941 | case ELAN_WAIT_RECALIBRATION: | |
942 | if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) { | |
943 | memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp)); | |
944 | complete(&ts->cmd_done); | |
945 | ts->state = ELAN_STATE_NORMAL; | |
946 | } | |
947 | break; | |
948 | ||
949 | case ELAN_WAIT_QUEUE_HEADER: | |
950 | if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL) | |
951 | break; | |
952 | ||
953 | ts->state = ELAN_STATE_NORMAL; | |
954 | /* fall through */ | |
955 | ||
956 | case ELAN_STATE_NORMAL: | |
957 | ||
958 | switch (ts->buf[FW_HDR_TYPE]) { | |
959 | case CMD_HEADER_HELLO: | |
960 | case CMD_HEADER_RESP: | |
961 | case CMD_HEADER_REK: | |
962 | break; | |
963 | ||
964 | case QUEUE_HEADER_WAIT: | |
965 | if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) { | |
966 | dev_err(&client->dev, | |
967 | "invalid wait packet %*ph\n", | |
968 | HEADER_SIZE, ts->buf); | |
969 | } else { | |
970 | ts->state = ELAN_WAIT_QUEUE_HEADER; | |
971 | udelay(30); | |
972 | } | |
973 | break; | |
974 | ||
975 | case QUEUE_HEADER_SINGLE: | |
976 | elants_i2c_event(ts, &ts->buf[HEADER_SIZE]); | |
977 | break; | |
978 | ||
979 | case QUEUE_HEADER_NORMAL: | |
980 | report_count = ts->buf[FW_HDR_COUNT]; | |
1c3415a0 | 981 | if (report_count == 0 || report_count > 3) { |
66aee900 | 982 | dev_err(&client->dev, |
1c3415a0 | 983 | "bad report count: %*ph\n", |
66aee900 SL |
984 | HEADER_SIZE, ts->buf); |
985 | break; | |
986 | } | |
987 | ||
988 | report_len = ts->buf[FW_HDR_LENGTH] / report_count; | |
989 | if (report_len != PACKET_SIZE) { | |
990 | dev_err(&client->dev, | |
991 | "mismatching report length: %*ph\n", | |
992 | HEADER_SIZE, ts->buf); | |
993 | break; | |
994 | } | |
995 | ||
996 | for (i = 0; i < report_count; i++) { | |
997 | u8 *buf = ts->buf + HEADER_SIZE + | |
998 | i * PACKET_SIZE; | |
999 | elants_i2c_event(ts, buf); | |
1000 | } | |
1001 | break; | |
1002 | ||
1003 | default: | |
1004 | dev_err(&client->dev, "unknown packet %*ph\n", | |
1005 | HEADER_SIZE, ts->buf); | |
1006 | break; | |
1007 | } | |
1008 | break; | |
1009 | } | |
1010 | ||
1011 | out: | |
1012 | return IRQ_HANDLED; | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * sysfs interface | |
1017 | */ | |
1018 | static ssize_t calibrate_store(struct device *dev, | |
1019 | struct device_attribute *attr, | |
1020 | const char *buf, size_t count) | |
1021 | { | |
1022 | struct i2c_client *client = to_i2c_client(dev); | |
1023 | struct elants_data *ts = i2c_get_clientdata(client); | |
1024 | int error; | |
1025 | ||
1026 | error = mutex_lock_interruptible(&ts->sysfs_mutex); | |
1027 | if (error) | |
1028 | return error; | |
1029 | ||
1030 | error = elants_i2c_calibrate(ts); | |
1031 | ||
1032 | mutex_unlock(&ts->sysfs_mutex); | |
1033 | return error ?: count; | |
1034 | } | |
1035 | ||
1036 | static ssize_t write_update_fw(struct device *dev, | |
1037 | struct device_attribute *attr, | |
1038 | const char *buf, size_t count) | |
1039 | { | |
1040 | struct i2c_client *client = to_i2c_client(dev); | |
1041 | struct elants_data *ts = i2c_get_clientdata(client); | |
1042 | int error; | |
1043 | ||
1044 | error = mutex_lock_interruptible(&ts->sysfs_mutex); | |
1045 | if (error) | |
1046 | return error; | |
1047 | ||
1048 | error = elants_i2c_fw_update(ts); | |
1049 | dev_dbg(dev, "firmware update result: %d\n", error); | |
1050 | ||
1051 | mutex_unlock(&ts->sysfs_mutex); | |
1052 | return error ?: count; | |
1053 | } | |
1054 | ||
1055 | static ssize_t show_iap_mode(struct device *dev, | |
1056 | struct device_attribute *attr, char *buf) | |
1057 | { | |
1058 | struct i2c_client *client = to_i2c_client(dev); | |
1059 | struct elants_data *ts = i2c_get_clientdata(client); | |
1060 | ||
1061 | return sprintf(buf, "%s\n", | |
1062 | ts->iap_mode == ELAN_IAP_OPERATIONAL ? | |
1063 | "Normal" : "Recovery"); | |
1064 | } | |
1065 | ||
6cbaefb4 | 1066 | static DEVICE_ATTR_WO(calibrate); |
66aee900 SL |
1067 | static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL); |
1068 | static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw); | |
1069 | ||
1070 | struct elants_version_attribute { | |
1071 | struct device_attribute dattr; | |
1072 | size_t field_offset; | |
1073 | size_t field_size; | |
1074 | }; | |
1075 | ||
1076 | #define __ELANTS_FIELD_SIZE(_field) \ | |
1077 | sizeof(((struct elants_data *)NULL)->_field) | |
1078 | #define __ELANTS_VERIFY_SIZE(_field) \ | |
1079 | (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \ | |
1080 | __ELANTS_FIELD_SIZE(_field)) | |
1081 | #define ELANTS_VERSION_ATTR(_field) \ | |
1082 | struct elants_version_attribute elants_ver_attr_##_field = { \ | |
1083 | .dattr = __ATTR(_field, S_IRUGO, \ | |
1084 | elants_version_attribute_show, NULL), \ | |
1085 | .field_offset = offsetof(struct elants_data, _field), \ | |
1086 | .field_size = __ELANTS_VERIFY_SIZE(_field), \ | |
1087 | } | |
1088 | ||
1089 | static ssize_t elants_version_attribute_show(struct device *dev, | |
1090 | struct device_attribute *dattr, | |
1091 | char *buf) | |
1092 | { | |
1093 | struct i2c_client *client = to_i2c_client(dev); | |
1094 | struct elants_data *ts = i2c_get_clientdata(client); | |
1095 | struct elants_version_attribute *attr = | |
1096 | container_of(dattr, struct elants_version_attribute, dattr); | |
1097 | u8 *field = (u8 *)((char *)ts + attr->field_offset); | |
1098 | unsigned int fmt_size; | |
1099 | unsigned int val; | |
1100 | ||
1101 | if (attr->field_size == 1) { | |
1102 | val = *field; | |
1103 | fmt_size = 2; /* 2 HEX digits */ | |
1104 | } else { | |
1105 | val = *(u16 *)field; | |
1106 | fmt_size = 4; /* 4 HEX digits */ | |
1107 | } | |
1108 | ||
1109 | return sprintf(buf, "%0*x\n", fmt_size, val); | |
1110 | } | |
1111 | ||
1112 | static ELANTS_VERSION_ATTR(fw_version); | |
1113 | static ELANTS_VERSION_ATTR(hw_version); | |
1114 | static ELANTS_VERSION_ATTR(test_version); | |
1115 | static ELANTS_VERSION_ATTR(solution_version); | |
1116 | static ELANTS_VERSION_ATTR(bc_version); | |
1117 | static ELANTS_VERSION_ATTR(iap_version); | |
1118 | ||
1119 | static struct attribute *elants_attributes[] = { | |
1120 | &dev_attr_calibrate.attr, | |
1121 | &dev_attr_update_fw.attr, | |
1122 | &dev_attr_iap_mode.attr, | |
1123 | ||
1124 | &elants_ver_attr_fw_version.dattr.attr, | |
1125 | &elants_ver_attr_hw_version.dattr.attr, | |
1126 | &elants_ver_attr_test_version.dattr.attr, | |
1127 | &elants_ver_attr_solution_version.dattr.attr, | |
1128 | &elants_ver_attr_bc_version.dattr.attr, | |
1129 | &elants_ver_attr_iap_version.dattr.attr, | |
1130 | NULL | |
1131 | }; | |
1132 | ||
48f960dd | 1133 | static const struct attribute_group elants_attribute_group = { |
66aee900 SL |
1134 | .attrs = elants_attributes, |
1135 | }; | |
1136 | ||
afe10358 DT |
1137 | static int elants_i2c_power_on(struct elants_data *ts) |
1138 | { | |
1139 | int error; | |
1140 | ||
1141 | /* | |
1142 | * If we do not have reset gpio assume platform firmware | |
1143 | * controls regulators and does power them on for us. | |
1144 | */ | |
1145 | if (IS_ERR_OR_NULL(ts->reset_gpio)) | |
1146 | return 0; | |
1147 | ||
1148 | gpiod_set_value_cansleep(ts->reset_gpio, 1); | |
1149 | ||
1150 | error = regulator_enable(ts->vcc33); | |
1151 | if (error) { | |
1152 | dev_err(&ts->client->dev, | |
1153 | "failed to enable vcc33 regulator: %d\n", | |
1154 | error); | |
1155 | goto release_reset_gpio; | |
1156 | } | |
1157 | ||
1158 | error = regulator_enable(ts->vccio); | |
1159 | if (error) { | |
1160 | dev_err(&ts->client->dev, | |
1161 | "failed to enable vccio regulator: %d\n", | |
1162 | error); | |
1163 | regulator_disable(ts->vcc33); | |
1164 | goto release_reset_gpio; | |
1165 | } | |
1166 | ||
1167 | /* | |
1168 | * We need to wait a bit after powering on controller before | |
1169 | * we are allowed to release reset GPIO. | |
1170 | */ | |
1171 | udelay(ELAN_POWERON_DELAY_USEC); | |
1172 | ||
1173 | release_reset_gpio: | |
1174 | gpiod_set_value_cansleep(ts->reset_gpio, 0); | |
1175 | if (error) | |
1176 | return error; | |
1177 | ||
1178 | msleep(ELAN_RESET_DELAY_MSEC); | |
1179 | ||
1180 | return 0; | |
1181 | } | |
1182 | ||
1183 | static void elants_i2c_power_off(void *_data) | |
1184 | { | |
1185 | struct elants_data *ts = _data; | |
1186 | ||
1187 | if (!IS_ERR_OR_NULL(ts->reset_gpio)) { | |
1188 | /* | |
1189 | * Activate reset gpio to prevent leakage through the | |
1190 | * pin once we shut off power to the controller. | |
1191 | */ | |
1192 | gpiod_set_value_cansleep(ts->reset_gpio, 1); | |
1193 | regulator_disable(ts->vccio); | |
1194 | regulator_disable(ts->vcc33); | |
1195 | } | |
1196 | } | |
1197 | ||
66aee900 SL |
1198 | static int elants_i2c_probe(struct i2c_client *client, |
1199 | const struct i2c_device_id *id) | |
1200 | { | |
1201 | union i2c_smbus_data dummy; | |
1202 | struct elants_data *ts; | |
1203 | unsigned long irqflags; | |
1204 | int error; | |
1205 | ||
1206 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
1207 | dev_err(&client->dev, | |
1208 | "%s: i2c check functionality error\n", DEVICE_NAME); | |
1209 | return -ENXIO; | |
1210 | } | |
1211 | ||
66aee900 SL |
1212 | ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL); |
1213 | if (!ts) | |
1214 | return -ENOMEM; | |
1215 | ||
1216 | mutex_init(&ts->sysfs_mutex); | |
1217 | init_completion(&ts->cmd_done); | |
1218 | ||
1219 | ts->client = client; | |
1220 | i2c_set_clientdata(client, ts); | |
1221 | ||
afe10358 DT |
1222 | ts->vcc33 = devm_regulator_get(&client->dev, "vcc33"); |
1223 | if (IS_ERR(ts->vcc33)) { | |
1224 | error = PTR_ERR(ts->vcc33); | |
1225 | if (error != -EPROBE_DEFER) | |
1226 | dev_err(&client->dev, | |
1227 | "Failed to get 'vcc33' regulator: %d\n", | |
1228 | error); | |
1229 | return error; | |
1230 | } | |
1231 | ||
1232 | ts->vccio = devm_regulator_get(&client->dev, "vccio"); | |
1233 | if (IS_ERR(ts->vccio)) { | |
1234 | error = PTR_ERR(ts->vccio); | |
1235 | if (error != -EPROBE_DEFER) | |
1236 | dev_err(&client->dev, | |
1237 | "Failed to get 'vccio' regulator: %d\n", | |
1238 | error); | |
1239 | return error; | |
1240 | } | |
1241 | ||
7229b87b | 1242 | ts->reset_gpio = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_LOW); |
afe10358 DT |
1243 | if (IS_ERR(ts->reset_gpio)) { |
1244 | error = PTR_ERR(ts->reset_gpio); | |
1245 | ||
1246 | if (error == -EPROBE_DEFER) | |
1247 | return error; | |
1248 | ||
1249 | if (error != -ENOENT && error != -ENOSYS) { | |
1250 | dev_err(&client->dev, | |
1251 | "failed to get reset gpio: %d\n", | |
1252 | error); | |
1253 | return error; | |
1254 | } | |
1255 | ||
1256 | ts->keep_power_in_suspend = true; | |
afe10358 DT |
1257 | } |
1258 | ||
1259 | error = elants_i2c_power_on(ts); | |
1260 | if (error) | |
1261 | return error; | |
1262 | ||
1263 | error = devm_add_action(&client->dev, elants_i2c_power_off, ts); | |
1264 | if (error) { | |
1265 | dev_err(&client->dev, | |
1266 | "failed to install power off action: %d\n", error); | |
1267 | elants_i2c_power_off(ts); | |
1268 | return error; | |
1269 | } | |
1270 | ||
1271 | /* Make sure there is something at this address */ | |
1272 | if (i2c_smbus_xfer(client->adapter, client->addr, 0, | |
1273 | I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) { | |
1274 | dev_err(&client->dev, "nothing at this address\n"); | |
1275 | return -ENXIO; | |
1276 | } | |
1277 | ||
66aee900 SL |
1278 | error = elants_i2c_initialize(ts); |
1279 | if (error) { | |
1280 | dev_err(&client->dev, "failed to initialize: %d\n", error); | |
1281 | return error; | |
1282 | } | |
1283 | ||
1284 | ts->input = devm_input_allocate_device(&client->dev); | |
1285 | if (!ts->input) { | |
1286 | dev_err(&client->dev, "Failed to allocate input device\n"); | |
1287 | return -ENOMEM; | |
1288 | } | |
1289 | ||
1290 | ts->input->name = "Elan Touchscreen"; | |
1291 | ts->input->id.bustype = BUS_I2C; | |
1292 | ||
1293 | __set_bit(BTN_TOUCH, ts->input->keybit); | |
1294 | __set_bit(EV_ABS, ts->input->evbit); | |
1295 | __set_bit(EV_KEY, ts->input->evbit); | |
1296 | ||
1297 | /* Single touch input params setup */ | |
1298 | input_set_abs_params(ts->input, ABS_X, 0, ts->x_max, 0, 0); | |
1299 | input_set_abs_params(ts->input, ABS_Y, 0, ts->y_max, 0, 0); | |
1300 | input_set_abs_params(ts->input, ABS_PRESSURE, 0, 255, 0, 0); | |
1301 | input_abs_set_res(ts->input, ABS_X, ts->x_res); | |
1302 | input_abs_set_res(ts->input, ABS_Y, ts->y_res); | |
1303 | ||
1304 | /* Multitouch input params setup */ | |
1305 | error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM, | |
1306 | INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); | |
1307 | if (error) { | |
1308 | dev_err(&client->dev, | |
1309 | "failed to initialize MT slots: %d\n", error); | |
1310 | return error; | |
1311 | } | |
1312 | ||
1313 | input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0); | |
1314 | input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0); | |
1315 | input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); | |
1316 | input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0); | |
f27ad893 JC |
1317 | input_set_abs_params(ts->input, ABS_MT_TOOL_TYPE, |
1318 | 0, MT_TOOL_PALM, 0, 0); | |
66aee900 SL |
1319 | input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res); |
1320 | input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res); | |
06170671 | 1321 | input_abs_set_res(ts->input, ABS_MT_TOUCH_MAJOR, 1); |
66aee900 | 1322 | |
66aee900 SL |
1323 | error = input_register_device(ts->input); |
1324 | if (error) { | |
1325 | dev_err(&client->dev, | |
1326 | "unable to register input device: %d\n", error); | |
1327 | return error; | |
1328 | } | |
1329 | ||
1330 | /* | |
4c83c071 DT |
1331 | * Platform code (ACPI, DTS) should normally set up interrupt |
1332 | * for us, but in case it did not let's fall back to using falling | |
1333 | * edge to be compatible with older Chromebooks. | |
66aee900 | 1334 | */ |
4c83c071 DT |
1335 | irqflags = irq_get_trigger_type(client->irq); |
1336 | if (!irqflags) | |
1337 | irqflags = IRQF_TRIGGER_FALLING; | |
66aee900 SL |
1338 | |
1339 | error = devm_request_threaded_irq(&client->dev, client->irq, | |
1340 | NULL, elants_i2c_irq, | |
1341 | irqflags | IRQF_ONESHOT, | |
1342 | client->name, ts); | |
1343 | if (error) { | |
1344 | dev_err(&client->dev, "Failed to register interrupt\n"); | |
1345 | return error; | |
1346 | } | |
1347 | ||
1348 | /* | |
1349 | * Systems using device tree should set up wakeup via DTS, | |
1350 | * the rest will configure device as wakeup source by default. | |
1351 | */ | |
1352 | if (!client->dev.of_node) | |
1353 | device_init_wakeup(&client->dev, true); | |
1354 | ||
8db69a9a | 1355 | error = devm_device_add_group(&client->dev, &elants_attribute_group); |
66aee900 SL |
1356 | if (error) { |
1357 | dev_err(&client->dev, "failed to create sysfs attributes: %d\n", | |
1358 | error); | |
1359 | return error; | |
1360 | } | |
1361 | ||
66aee900 SL |
1362 | return 0; |
1363 | } | |
1364 | ||
1365 | static int __maybe_unused elants_i2c_suspend(struct device *dev) | |
1366 | { | |
1367 | struct i2c_client *client = to_i2c_client(dev); | |
1368 | struct elants_data *ts = i2c_get_clientdata(client); | |
1369 | const u8 set_sleep_cmd[] = { 0x54, 0x50, 0x00, 0x01 }; | |
1370 | int retry_cnt; | |
1371 | int error; | |
1372 | ||
1373 | /* Command not support in IAP recovery mode */ | |
1374 | if (ts->iap_mode != ELAN_IAP_OPERATIONAL) | |
1375 | return -EBUSY; | |
1376 | ||
1377 | disable_irq(client->irq); | |
1378 | ||
478e5ed1 JC |
1379 | if (device_may_wakeup(dev)) { |
1380 | /* | |
1381 | * The device will automatically enter idle mode | |
1382 | * that has reduced power consumption. | |
1383 | */ | |
1384 | ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0); | |
1385 | } else if (ts->keep_power_in_suspend) { | |
afe10358 DT |
1386 | for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { |
1387 | error = elants_i2c_send(client, set_sleep_cmd, | |
1388 | sizeof(set_sleep_cmd)); | |
1389 | if (!error) | |
1390 | break; | |
66aee900 | 1391 | |
afe10358 DT |
1392 | dev_err(&client->dev, |
1393 | "suspend command failed: %d\n", error); | |
1394 | } | |
afe10358 DT |
1395 | } else { |
1396 | elants_i2c_power_off(ts); | |
1397 | } | |
66aee900 SL |
1398 | |
1399 | return 0; | |
1400 | } | |
1401 | ||
1402 | static int __maybe_unused elants_i2c_resume(struct device *dev) | |
1403 | { | |
1404 | struct i2c_client *client = to_i2c_client(dev); | |
1405 | struct elants_data *ts = i2c_get_clientdata(client); | |
1406 | const u8 set_active_cmd[] = { 0x54, 0x58, 0x00, 0x01 }; | |
1407 | int retry_cnt; | |
1408 | int error; | |
1409 | ||
478e5ed1 JC |
1410 | if (device_may_wakeup(dev)) { |
1411 | if (ts->wake_irq_enabled) | |
1412 | disable_irq_wake(client->irq); | |
1413 | elants_i2c_sw_reset(client); | |
1414 | } else if (ts->keep_power_in_suspend) { | |
afe10358 DT |
1415 | for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { |
1416 | error = elants_i2c_send(client, set_active_cmd, | |
1417 | sizeof(set_active_cmd)); | |
1418 | if (!error) | |
1419 | break; | |
66aee900 | 1420 | |
afe10358 DT |
1421 | dev_err(&client->dev, |
1422 | "resume command failed: %d\n", error); | |
1423 | } | |
1424 | } else { | |
1425 | elants_i2c_power_on(ts); | |
1426 | elants_i2c_initialize(ts); | |
66aee900 SL |
1427 | } |
1428 | ||
1429 | ts->state = ELAN_STATE_NORMAL; | |
1430 | enable_irq(client->irq); | |
1431 | ||
1432 | return 0; | |
1433 | } | |
1434 | ||
1435 | static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops, | |
1436 | elants_i2c_suspend, elants_i2c_resume); | |
1437 | ||
1438 | static const struct i2c_device_id elants_i2c_id[] = { | |
1439 | { DEVICE_NAME, 0 }, | |
1440 | { } | |
1441 | }; | |
1442 | MODULE_DEVICE_TABLE(i2c, elants_i2c_id); | |
1443 | ||
1444 | #ifdef CONFIG_ACPI | |
1445 | static const struct acpi_device_id elants_acpi_id[] = { | |
1446 | { "ELAN0001", 0 }, | |
1447 | { } | |
1448 | }; | |
1449 | MODULE_DEVICE_TABLE(acpi, elants_acpi_id); | |
1450 | #endif | |
1451 | ||
1452 | #ifdef CONFIG_OF | |
1453 | static const struct of_device_id elants_of_match[] = { | |
1454 | { .compatible = "elan,ekth3500" }, | |
1455 | { /* sentinel */ } | |
1456 | }; | |
1457 | MODULE_DEVICE_TABLE(of, elants_of_match); | |
1458 | #endif | |
1459 | ||
1460 | static struct i2c_driver elants_i2c_driver = { | |
1461 | .probe = elants_i2c_probe, | |
1462 | .id_table = elants_i2c_id, | |
1463 | .driver = { | |
1464 | .name = DEVICE_NAME, | |
66aee900 SL |
1465 | .pm = &elants_i2c_pm_ops, |
1466 | .acpi_match_table = ACPI_PTR(elants_acpi_id), | |
1467 | .of_match_table = of_match_ptr(elants_of_match), | |
9f6a07b6 | 1468 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
66aee900 SL |
1469 | }, |
1470 | }; | |
1471 | module_i2c_driver(elants_i2c_driver); | |
1472 | ||
1473 | MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>"); | |
1474 | MODULE_DESCRIPTION("Elan I2c Touchscreen driver"); | |
66aee900 | 1475 | MODULE_LICENSE("GPL"); |