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