1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * Elan I2C/SMBus Touchpad driver - I2C interface |
4 | * |
5 | * Copyright (c) 2013 ELAN Microelectronics Corp. |
6 | * |
7 | * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> |
8 | * |
9 | * Based on cyapa driver: |
10 | * copyright (c) 2011-2012 Cypress Semiconductor, Inc. |
11 | * copyright (c) 2011-2012 Google, Inc. |
12 | * |
13 | * Trademarks are the property of their respective owners. |
14 | */ |
15 | |
16 | #include <linux/completion.h> |
17 | #include <linux/delay.h> |
18 | #include <linux/i2c.h> |
19 | #include <linux/interrupt.h> |
20 | #include <linux/jiffies.h> |
21 | #include <linux/kernel.h> |
22 | #include <linux/slab.h> |
23 | #include <linux/sched.h> |
24 | #include <asm/unaligned.h> |
25 | |
26 | #include "elan_i2c.h" |
27 | |
28 | /* Elan i2c commands */ |
29 | #define ETP_I2C_RESET 0x0100 |
30 | #define ETP_I2C_WAKE_UP 0x0800 |
31 | #define ETP_I2C_SLEEP 0x0801 |
32 | #define ETP_I2C_DESC_CMD 0x0001 |
33 | #define ETP_I2C_REPORT_DESC_CMD 0x0002 |
34 | #define ETP_I2C_STAND_CMD 0x0005 |
35 | #define ETP_I2C_PATTERN_CMD 0x0100 |
36 | #define ETP_I2C_UNIQUEID_CMD 0x0101 |
37 | #define ETP_I2C_FW_VERSION_CMD 0x0102 |
38 | #define ETP_I2C_IC_TYPE_CMD 0x0103 |
39 | #define ETP_I2C_OSM_VERSION_CMD 0x0103 |
40 | #define ETP_I2C_NSM_VERSION_CMD 0x0104 |
41 | #define ETP_I2C_XY_TRACENUM_CMD 0x0105 |
42 | #define ETP_I2C_MAX_X_AXIS_CMD 0x0106 |
43 | #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107 |
44 | #define ETP_I2C_RESOLUTION_CMD 0x0108 |
45 | #define ETP_I2C_PRESSURE_CMD 0x010A |
46 | #define ETP_I2C_IAP_VERSION_CMD 0x0110 |
47 | #define ETP_I2C_IC_TYPE_P0_CMD 0x0110 |
48 | #define ETP_I2C_IAP_VERSION_P0_CMD 0x0111 |
49 | #define ETP_I2C_SET_CMD 0x0300 |
50 | #define ETP_I2C_POWER_CMD 0x0307 |
51 | #define ETP_I2C_FW_CHECKSUM_CMD 0x030F |
52 | #define ETP_I2C_IAP_CTRL_CMD 0x0310 |
53 | #define ETP_I2C_IAP_CMD 0x0311 |
54 | #define ETP_I2C_IAP_RESET_CMD 0x0314 |
55 | #define ETP_I2C_IAP_CHECKSUM_CMD 0x0315 |
56 | #define ETP_I2C_CALIBRATE_CMD 0x0316 |
57 | #define ETP_I2C_MAX_BASELINE_CMD 0x0317 |
58 | #define ETP_I2C_MIN_BASELINE_CMD 0x0318 |
59 | #define ETP_I2C_IAP_TYPE_REG 0x0040 |
60 | #define ETP_I2C_IAP_TYPE_CMD 0x0304 |
61 | |
62 | #define ETP_I2C_REPORT_LEN 34 |
63 | #define ETP_I2C_REPORT_LEN_ID2 39 |
64 | #define ETP_I2C_REPORT_MAX_LEN 39 |
65 | #define ETP_I2C_DESC_LENGTH 30 |
66 | #define ETP_I2C_REPORT_DESC_LENGTH 158 |
67 | #define ETP_I2C_INF_LENGTH 2 |
68 | #define ETP_I2C_IAP_PASSWORD 0x1EA5 |
69 | #define ETP_I2C_IAP_RESET 0xF0F0 |
70 | #define ETP_I2C_MAIN_MODE_ON (1 << 9) |
71 | #define ETP_I2C_IAP_REG_L 0x01 |
72 | #define ETP_I2C_IAP_REG_H 0x06 |
73 | |
74 | static int elan_i2c_read_block(struct i2c_client *client, |
75 | u16 reg, u8 *val, u16 len) |
76 | { |
77 | __le16 buf[] = { |
78 | cpu_to_le16(reg), |
79 | }; |
80 | struct i2c_msg msgs[] = { |
81 | { |
82 | .addr = client->addr, |
83 | .flags = client->flags & I2C_M_TEN, |
84 | .len = sizeof(buf), |
85 | .buf = (u8 *)buf, |
86 | }, |
87 | { |
88 | .addr = client->addr, |
89 | .flags = (client->flags & I2C_M_TEN) | I2C_M_RD, |
90 | .len = len, |
91 | .buf = val, |
92 | } |
93 | }; |
94 | int ret; |
95 | |
96 | ret = i2c_transfer(adap: client->adapter, msgs, ARRAY_SIZE(msgs)); |
97 | return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO); |
98 | } |
99 | |
100 | static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val) |
101 | { |
102 | int retval; |
103 | |
104 | retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH); |
105 | if (retval < 0) { |
106 | dev_err(&client->dev, "reading cmd (0x%04x) fail.\n" , reg); |
107 | return retval; |
108 | } |
109 | |
110 | return 0; |
111 | } |
112 | |
113 | static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd) |
114 | { |
115 | __le16 buf[] = { |
116 | cpu_to_le16(reg), |
117 | cpu_to_le16(cmd), |
118 | }; |
119 | struct i2c_msg msg = { |
120 | .addr = client->addr, |
121 | .flags = client->flags & I2C_M_TEN, |
122 | .len = sizeof(buf), |
123 | .buf = (u8 *)buf, |
124 | }; |
125 | int ret; |
126 | |
127 | ret = i2c_transfer(adap: client->adapter, msgs: &msg, num: 1); |
128 | if (ret != 1) { |
129 | if (ret >= 0) |
130 | ret = -EIO; |
131 | dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n" , |
132 | reg, ret); |
133 | return ret; |
134 | } |
135 | |
136 | return 0; |
137 | } |
138 | |
139 | static int elan_i2c_initialize(struct i2c_client *client) |
140 | { |
141 | struct device *dev = &client->dev; |
142 | int error; |
143 | u8 val[256]; |
144 | |
145 | error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET); |
146 | if (error) { |
147 | dev_err(dev, "device reset failed: %d\n" , error); |
148 | return error; |
149 | } |
150 | |
151 | /* Wait for the device to reset */ |
152 | msleep(msecs: 100); |
153 | |
154 | /* get reset acknowledgement 0000 */ |
155 | error = i2c_master_recv(client, buf: val, ETP_I2C_INF_LENGTH); |
156 | if (error < 0) { |
157 | dev_err(dev, "failed to read reset response: %d\n" , error); |
158 | return error; |
159 | } |
160 | |
161 | error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD, |
162 | val, ETP_I2C_DESC_LENGTH); |
163 | if (error) { |
164 | dev_err(dev, "cannot get device descriptor: %d\n" , error); |
165 | return error; |
166 | } |
167 | |
168 | error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD, |
169 | val, ETP_I2C_REPORT_DESC_LENGTH); |
170 | if (error) { |
171 | dev_err(dev, "fetching report descriptor failed.: %d\n" , error); |
172 | return error; |
173 | } |
174 | |
175 | return 0; |
176 | } |
177 | |
178 | static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep) |
179 | { |
180 | return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, |
181 | cmd: sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP); |
182 | } |
183 | |
184 | static int elan_i2c_power_control(struct i2c_client *client, bool enable) |
185 | { |
186 | u8 val[2]; |
187 | u16 reg; |
188 | int error; |
189 | |
190 | error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val); |
191 | if (error) { |
192 | dev_err(&client->dev, |
193 | "failed to read current power state: %d\n" , |
194 | error); |
195 | return error; |
196 | } |
197 | |
198 | reg = le16_to_cpup(p: (__le16 *)val); |
199 | if (enable) |
200 | reg &= ~ETP_DISABLE_POWER; |
201 | else |
202 | reg |= ETP_DISABLE_POWER; |
203 | |
204 | error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, cmd: reg); |
205 | if (error) { |
206 | dev_err(&client->dev, |
207 | "failed to write current power state: %d\n" , |
208 | error); |
209 | return error; |
210 | } |
211 | |
212 | return 0; |
213 | } |
214 | |
215 | static int elan_i2c_set_mode(struct i2c_client *client, u8 mode) |
216 | { |
217 | return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, cmd: mode); |
218 | } |
219 | |
220 | |
221 | static int elan_i2c_calibrate(struct i2c_client *client) |
222 | { |
223 | return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, cmd: 1); |
224 | } |
225 | |
226 | static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val) |
227 | { |
228 | return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, len: 1); |
229 | } |
230 | |
231 | static int elan_i2c_get_baseline_data(struct i2c_client *client, |
232 | bool max_baseline, u8 *value) |
233 | { |
234 | int error; |
235 | u8 val[3]; |
236 | |
237 | error = elan_i2c_read_cmd(client, |
238 | reg: max_baseline ? ETP_I2C_MAX_BASELINE_CMD : |
239 | ETP_I2C_MIN_BASELINE_CMD, |
240 | val); |
241 | if (error) |
242 | return error; |
243 | |
244 | *value = le16_to_cpup(p: (__le16 *)val); |
245 | |
246 | return 0; |
247 | } |
248 | |
249 | static int elan_i2c_get_pattern(struct i2c_client *client, u8 *pattern) |
250 | { |
251 | int error; |
252 | u8 val[3]; |
253 | |
254 | error = elan_i2c_read_cmd(client, ETP_I2C_PATTERN_CMD, val); |
255 | if (error) { |
256 | dev_err(&client->dev, "failed to get pattern: %d\n" , error); |
257 | return error; |
258 | } |
259 | |
260 | /* |
261 | * Not all versions of firmware implement "get pattern" command. |
262 | * When this command is not implemented the device will respond |
263 | * with 0xFF 0xFF, which we will treat as "old" pattern 0. |
264 | */ |
265 | *pattern = val[0] == 0xFF && val[1] == 0xFF ? 0 : val[1]; |
266 | |
267 | return 0; |
268 | } |
269 | |
270 | static int elan_i2c_get_version(struct i2c_client *client, |
271 | u8 pattern, bool iap, u8 *version) |
272 | { |
273 | int error; |
274 | u16 cmd; |
275 | u8 val[3]; |
276 | |
277 | if (!iap) |
278 | cmd = ETP_I2C_FW_VERSION_CMD; |
279 | else if (pattern == 0) |
280 | cmd = ETP_I2C_IAP_VERSION_P0_CMD; |
281 | else |
282 | cmd = ETP_I2C_IAP_VERSION_CMD; |
283 | |
284 | error = elan_i2c_read_cmd(client, reg: cmd, val); |
285 | if (error) { |
286 | dev_err(&client->dev, "failed to get %s version: %d\n" , |
287 | iap ? "IAP" : "FW" , error); |
288 | return error; |
289 | } |
290 | |
291 | if (pattern >= 0x01) |
292 | *version = iap ? val[1] : val[0]; |
293 | else |
294 | *version = val[0]; |
295 | return 0; |
296 | } |
297 | |
298 | static int elan_i2c_get_sm_version(struct i2c_client *client, u8 pattern, |
299 | u16 *ic_type, u8 *version, u8 *clickpad) |
300 | { |
301 | int error; |
302 | u8 val[3]; |
303 | |
304 | if (pattern >= 0x01) { |
305 | error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_CMD, val); |
306 | if (error) { |
307 | dev_err(&client->dev, "failed to get ic type: %d\n" , |
308 | error); |
309 | return error; |
310 | } |
311 | *ic_type = be16_to_cpup(p: (__be16 *)val); |
312 | |
313 | error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD, |
314 | val); |
315 | if (error) { |
316 | dev_err(&client->dev, "failed to get SM version: %d\n" , |
317 | error); |
318 | return error; |
319 | } |
320 | *version = val[1]; |
321 | *clickpad = val[0] & 0x10; |
322 | } else { |
323 | error = elan_i2c_read_cmd(client, ETP_I2C_OSM_VERSION_CMD, val); |
324 | if (error) { |
325 | dev_err(&client->dev, "failed to get SM version: %d\n" , |
326 | error); |
327 | return error; |
328 | } |
329 | *version = val[0]; |
330 | |
331 | error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_P0_CMD, val); |
332 | if (error) { |
333 | dev_err(&client->dev, "failed to get ic type: %d\n" , |
334 | error); |
335 | return error; |
336 | } |
337 | *ic_type = val[0]; |
338 | |
339 | error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD, |
340 | val); |
341 | if (error) { |
342 | dev_err(&client->dev, "failed to get SM version: %d\n" , |
343 | error); |
344 | return error; |
345 | } |
346 | *clickpad = val[0] & 0x10; |
347 | } |
348 | |
349 | return 0; |
350 | } |
351 | |
352 | static int elan_i2c_get_product_id(struct i2c_client *client, u16 *id) |
353 | { |
354 | int error; |
355 | u8 val[3]; |
356 | |
357 | error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val); |
358 | if (error) { |
359 | dev_err(&client->dev, "failed to get product ID: %d\n" , error); |
360 | return error; |
361 | } |
362 | |
363 | *id = le16_to_cpup(p: (__le16 *)val); |
364 | return 0; |
365 | } |
366 | |
367 | static int elan_i2c_get_checksum(struct i2c_client *client, |
368 | bool iap, u16 *csum) |
369 | { |
370 | int error; |
371 | u8 val[3]; |
372 | |
373 | error = elan_i2c_read_cmd(client, |
374 | reg: iap ? ETP_I2C_IAP_CHECKSUM_CMD : |
375 | ETP_I2C_FW_CHECKSUM_CMD, |
376 | val); |
377 | if (error) { |
378 | dev_err(&client->dev, "failed to get %s checksum: %d\n" , |
379 | iap ? "IAP" : "FW" , error); |
380 | return error; |
381 | } |
382 | |
383 | *csum = le16_to_cpup(p: (__le16 *)val); |
384 | return 0; |
385 | } |
386 | |
387 | static int elan_i2c_get_max(struct i2c_client *client, |
388 | unsigned int *max_x, unsigned int *max_y) |
389 | { |
390 | int error; |
391 | u8 val[3]; |
392 | |
393 | error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val); |
394 | if (error) { |
395 | dev_err(&client->dev, "failed to get X dimension: %d\n" , error); |
396 | return error; |
397 | } |
398 | |
399 | *max_x = le16_to_cpup(p: (__le16 *)val); |
400 | |
401 | error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val); |
402 | if (error) { |
403 | dev_err(&client->dev, "failed to get Y dimension: %d\n" , error); |
404 | return error; |
405 | } |
406 | |
407 | *max_y = le16_to_cpup(p: (__le16 *)val); |
408 | |
409 | return 0; |
410 | } |
411 | |
412 | static int elan_i2c_get_resolution(struct i2c_client *client, |
413 | u8 *hw_res_x, u8 *hw_res_y) |
414 | { |
415 | int error; |
416 | u8 val[3]; |
417 | |
418 | error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val); |
419 | if (error) { |
420 | dev_err(&client->dev, "failed to get resolution: %d\n" , error); |
421 | return error; |
422 | } |
423 | |
424 | *hw_res_x = val[0]; |
425 | *hw_res_y = val[1]; |
426 | |
427 | return 0; |
428 | } |
429 | |
430 | static int elan_i2c_get_num_traces(struct i2c_client *client, |
431 | unsigned int *x_traces, |
432 | unsigned int *y_traces) |
433 | { |
434 | int error; |
435 | u8 val[3]; |
436 | |
437 | error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val); |
438 | if (error) { |
439 | dev_err(&client->dev, "failed to get trace info: %d\n" , error); |
440 | return error; |
441 | } |
442 | |
443 | *x_traces = val[0]; |
444 | *y_traces = val[1]; |
445 | |
446 | return 0; |
447 | } |
448 | |
449 | static int elan_i2c_get_pressure_adjustment(struct i2c_client *client, |
450 | int *adjustment) |
451 | { |
452 | int error; |
453 | u8 val[3]; |
454 | |
455 | error = elan_i2c_read_cmd(client, ETP_I2C_PRESSURE_CMD, val); |
456 | if (error) { |
457 | dev_err(&client->dev, "failed to get pressure format: %d\n" , |
458 | error); |
459 | return error; |
460 | } |
461 | |
462 | if ((val[0] >> 4) & 0x1) |
463 | *adjustment = 0; |
464 | else |
465 | *adjustment = ETP_PRESSURE_OFFSET; |
466 | |
467 | return 0; |
468 | } |
469 | |
470 | static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode) |
471 | { |
472 | int error; |
473 | u16 constant; |
474 | u8 val[3]; |
475 | |
476 | error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val); |
477 | if (error) { |
478 | dev_err(&client->dev, |
479 | "failed to read iap control register: %d\n" , |
480 | error); |
481 | return error; |
482 | } |
483 | |
484 | constant = le16_to_cpup(p: (__le16 *)val); |
485 | dev_dbg(&client->dev, "iap control reg: 0x%04x.\n" , constant); |
486 | |
487 | *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE; |
488 | |
489 | return 0; |
490 | } |
491 | |
492 | static int elan_i2c_iap_reset(struct i2c_client *client) |
493 | { |
494 | int error; |
495 | |
496 | error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD, |
497 | ETP_I2C_IAP_RESET); |
498 | if (error) { |
499 | dev_err(&client->dev, "cannot reset IC: %d\n" , error); |
500 | return error; |
501 | } |
502 | |
503 | return 0; |
504 | } |
505 | |
506 | static int elan_i2c_set_flash_key(struct i2c_client *client) |
507 | { |
508 | int error; |
509 | |
510 | error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD, |
511 | ETP_I2C_IAP_PASSWORD); |
512 | if (error) { |
513 | dev_err(&client->dev, "cannot set flash key: %d\n" , error); |
514 | return error; |
515 | } |
516 | |
517 | return 0; |
518 | } |
519 | |
520 | static int elan_read_write_iap_type(struct i2c_client *client, u16 fw_page_size) |
521 | { |
522 | int error; |
523 | u16 constant; |
524 | u8 val[3]; |
525 | int retry = 3; |
526 | |
527 | do { |
528 | error = elan_i2c_write_cmd(client, ETP_I2C_IAP_TYPE_CMD, |
529 | cmd: fw_page_size / 2); |
530 | if (error) { |
531 | dev_err(&client->dev, |
532 | "cannot write iap type: %d\n" , error); |
533 | return error; |
534 | } |
535 | |
536 | error = elan_i2c_read_cmd(client, ETP_I2C_IAP_TYPE_CMD, val); |
537 | if (error) { |
538 | dev_err(&client->dev, |
539 | "failed to read iap type register: %d\n" , |
540 | error); |
541 | return error; |
542 | } |
543 | constant = le16_to_cpup(p: (__le16 *)val); |
544 | dev_dbg(&client->dev, "iap type reg: 0x%04x\n" , constant); |
545 | |
546 | if (constant == fw_page_size / 2) |
547 | return 0; |
548 | |
549 | } while (--retry > 0); |
550 | |
551 | dev_err(&client->dev, "cannot set iap type\n" ); |
552 | return -EIO; |
553 | } |
554 | |
555 | static int elan_i2c_prepare_fw_update(struct i2c_client *client, u16 ic_type, |
556 | u8 iap_version, u16 fw_page_size) |
557 | { |
558 | struct device *dev = &client->dev; |
559 | int error; |
560 | enum tp_mode mode; |
561 | u8 val[3]; |
562 | u16 password; |
563 | |
564 | /* Get FW in which mode (IAP_MODE/MAIN_MODE) */ |
565 | error = elan_i2c_iap_get_mode(client, mode: &mode); |
566 | if (error) |
567 | return error; |
568 | |
569 | if (mode == IAP_MODE) { |
570 | /* Reset IC */ |
571 | error = elan_i2c_iap_reset(client); |
572 | if (error) |
573 | return error; |
574 | |
575 | msleep(msecs: 30); |
576 | } |
577 | |
578 | /* Set flash key*/ |
579 | error = elan_i2c_set_flash_key(client); |
580 | if (error) |
581 | return error; |
582 | |
583 | /* Wait for F/W IAP initialization */ |
584 | msleep(msecs: mode == MAIN_MODE ? 100 : 30); |
585 | |
586 | /* Check if we are in IAP mode or not */ |
587 | error = elan_i2c_iap_get_mode(client, mode: &mode); |
588 | if (error) |
589 | return error; |
590 | |
591 | if (mode == MAIN_MODE) { |
592 | dev_err(dev, "wrong mode: %d\n" , mode); |
593 | return -EIO; |
594 | } |
595 | |
596 | if (ic_type >= 0x0D && iap_version >= 1) { |
597 | error = elan_read_write_iap_type(client, fw_page_size); |
598 | if (error) |
599 | return error; |
600 | } |
601 | |
602 | /* Set flash key again */ |
603 | error = elan_i2c_set_flash_key(client); |
604 | if (error) |
605 | return error; |
606 | |
607 | /* Wait for F/W IAP initialization */ |
608 | msleep(msecs: 30); |
609 | |
610 | /* read back to check we actually enabled successfully. */ |
611 | error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val); |
612 | if (error) { |
613 | dev_err(dev, "cannot read iap password: %d\n" , |
614 | error); |
615 | return error; |
616 | } |
617 | |
618 | password = le16_to_cpup(p: (__le16 *)val); |
619 | if (password != ETP_I2C_IAP_PASSWORD) { |
620 | dev_err(dev, "wrong iap password: 0x%X\n" , password); |
621 | return -EIO; |
622 | } |
623 | |
624 | return 0; |
625 | } |
626 | |
627 | static int elan_i2c_write_fw_block(struct i2c_client *client, u16 fw_page_size, |
628 | const u8 *page, u16 checksum, int idx) |
629 | { |
630 | struct device *dev = &client->dev; |
631 | u8 *page_store; |
632 | u8 val[3]; |
633 | u16 result; |
634 | int ret, error; |
635 | |
636 | page_store = kmalloc(size: fw_page_size + 4, GFP_KERNEL); |
637 | if (!page_store) |
638 | return -ENOMEM; |
639 | |
640 | page_store[0] = ETP_I2C_IAP_REG_L; |
641 | page_store[1] = ETP_I2C_IAP_REG_H; |
642 | memcpy(&page_store[2], page, fw_page_size); |
643 | /* recode checksum at last two bytes */ |
644 | put_unaligned_le16(val: checksum, p: &page_store[fw_page_size + 2]); |
645 | |
646 | ret = i2c_master_send(client, buf: page_store, count: fw_page_size + 4); |
647 | if (ret != fw_page_size + 4) { |
648 | error = ret < 0 ? ret : -EIO; |
649 | dev_err(dev, "Failed to write page %d: %d\n" , idx, error); |
650 | goto exit; |
651 | } |
652 | |
653 | /* Wait for F/W to update one page ROM data. */ |
654 | msleep(msecs: fw_page_size == ETP_FW_PAGE_SIZE_512 ? 50 : 35); |
655 | |
656 | error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val); |
657 | if (error) { |
658 | dev_err(dev, "Failed to read IAP write result: %d\n" , error); |
659 | goto exit; |
660 | } |
661 | |
662 | result = le16_to_cpup(p: (__le16 *)val); |
663 | if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) { |
664 | dev_err(dev, "IAP reports failed write: %04hx\n" , |
665 | result); |
666 | error = -EIO; |
667 | goto exit; |
668 | } |
669 | |
670 | exit: |
671 | kfree(objp: page_store); |
672 | return error; |
673 | } |
674 | |
675 | static int elan_i2c_finish_fw_update(struct i2c_client *client, |
676 | struct completion *completion) |
677 | { |
678 | struct device *dev = &client->dev; |
679 | int error = 0; |
680 | int len; |
681 | u8 buffer[ETP_I2C_REPORT_MAX_LEN]; |
682 | |
683 | len = i2c_master_recv(client, buf: buffer, ETP_I2C_REPORT_MAX_LEN); |
684 | if (len <= 0) { |
685 | error = len < 0 ? len : -EIO; |
686 | dev_warn(dev, "failed to read I2C data after FW WDT reset: %d (%d)\n" , |
687 | error, len); |
688 | } |
689 | |
690 | reinit_completion(x: completion); |
691 | enable_irq(irq: client->irq); |
692 | |
693 | error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET); |
694 | if (error) { |
695 | dev_err(dev, "device reset failed: %d\n" , error); |
696 | } else if (!wait_for_completion_timeout(x: completion, |
697 | timeout: msecs_to_jiffies(m: 300))) { |
698 | dev_err(dev, "timeout waiting for device reset\n" ); |
699 | error = -ETIMEDOUT; |
700 | } |
701 | |
702 | disable_irq(irq: client->irq); |
703 | |
704 | if (error) |
705 | return error; |
706 | |
707 | len = i2c_master_recv(client, buf: buffer, ETP_I2C_INF_LENGTH); |
708 | if (len != ETP_I2C_INF_LENGTH) { |
709 | error = len < 0 ? len : -EIO; |
710 | dev_err(dev, "failed to read INT signal: %d (%d)\n" , |
711 | error, len); |
712 | return error; |
713 | } |
714 | |
715 | return 0; |
716 | } |
717 | |
718 | static int elan_i2c_get_report_features(struct i2c_client *client, u8 pattern, |
719 | unsigned int *features, |
720 | unsigned int *report_len) |
721 | { |
722 | *features = ETP_FEATURE_REPORT_MK; |
723 | *report_len = pattern <= 0x01 ? |
724 | ETP_I2C_REPORT_LEN : ETP_I2C_REPORT_LEN_ID2; |
725 | return 0; |
726 | } |
727 | |
728 | static int elan_i2c_get_report(struct i2c_client *client, |
729 | u8 *report, unsigned int report_len) |
730 | { |
731 | int len; |
732 | |
733 | len = i2c_master_recv(client, buf: report, count: report_len); |
734 | if (len < 0) { |
735 | dev_err(&client->dev, "failed to read report data: %d\n" , len); |
736 | return len; |
737 | } |
738 | |
739 | if (len != report_len) { |
740 | dev_err(&client->dev, |
741 | "wrong report length (%d vs %d expected)\n" , |
742 | len, report_len); |
743 | return -EIO; |
744 | } |
745 | |
746 | return 0; |
747 | } |
748 | |
749 | const struct elan_transport_ops elan_i2c_ops = { |
750 | .initialize = elan_i2c_initialize, |
751 | .sleep_control = elan_i2c_sleep_control, |
752 | .power_control = elan_i2c_power_control, |
753 | .set_mode = elan_i2c_set_mode, |
754 | |
755 | .calibrate = elan_i2c_calibrate, |
756 | .calibrate_result = elan_i2c_calibrate_result, |
757 | |
758 | .get_baseline_data = elan_i2c_get_baseline_data, |
759 | |
760 | .get_version = elan_i2c_get_version, |
761 | .get_sm_version = elan_i2c_get_sm_version, |
762 | .get_product_id = elan_i2c_get_product_id, |
763 | .get_checksum = elan_i2c_get_checksum, |
764 | .get_pressure_adjustment = elan_i2c_get_pressure_adjustment, |
765 | |
766 | .get_max = elan_i2c_get_max, |
767 | .get_resolution = elan_i2c_get_resolution, |
768 | .get_num_traces = elan_i2c_get_num_traces, |
769 | |
770 | .iap_get_mode = elan_i2c_iap_get_mode, |
771 | .iap_reset = elan_i2c_iap_reset, |
772 | |
773 | .prepare_fw_update = elan_i2c_prepare_fw_update, |
774 | .write_fw_block = elan_i2c_write_fw_block, |
775 | .finish_fw_update = elan_i2c_finish_fw_update, |
776 | |
777 | .get_pattern = elan_i2c_get_pattern, |
778 | |
779 | .get_report_features = elan_i2c_get_report_features, |
780 | .get_report = elan_i2c_get_report, |
781 | }; |
782 | |