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