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