2 * Elan Microelectronics touch panels with I2C interface
4 * Copyright (C) 2014 Elan Microelectronics Corporation.
5 * Scott Liu <scott.liu@emc.com.tw>
7 * This code is partly based on hid-multitouch.c:
9 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
10 * Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
11 * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
14 * This code is partly based on i2c-hid.c:
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
22 * This software is licensed under the terms of the GNU General Public
23 * License version 2, as published by the Free Software Foundation, and
24 * may be copied, distributed, and modified under those terms.
27 #include <linux/module.h>
28 #include <linux/input.h>
29 #include <linux/interrupt.h>
30 #include <linux/platform_device.h>
31 #include <linux/async.h>
32 #include <linux/i2c.h>
33 #include <linux/delay.h>
34 #include <linux/uaccess.h>
35 #include <linux/buffer_head.h>
36 #include <linux/slab.h>
37 #include <linux/firmware.h>
38 #include <linux/input/mt.h>
39 #include <linux/acpi.h>
41 #include <asm/unaligned.h>
43 /* Device, Driver information */
44 #define DEVICE_NAME "elants_i2c"
45 #define DRV_VERSION "1.0.9"
47 /* Convert from rows or columns into resolution */
48 #define ELAN_TS_RESOLUTION(n, m) (((n) - 1) * (m))
53 #define FW_HDR_COUNT 1
54 #define FW_HDR_LENGTH 2
56 /* Buffer mode Queue Header information */
57 #define QUEUE_HEADER_SINGLE 0x62
58 #define QUEUE_HEADER_NORMAL 0X63
59 #define QUEUE_HEADER_WAIT 0x64
61 /* Command header definition */
62 #define CMD_HEADER_WRITE 0x54
63 #define CMD_HEADER_READ 0x53
64 #define CMD_HEADER_6B_READ 0x5B
65 #define CMD_HEADER_RESP 0x52
66 #define CMD_HEADER_6B_RESP 0x9B
67 #define CMD_HEADER_HELLO 0x55
68 #define CMD_HEADER_REK 0x66
70 /* FW position data */
71 #define PACKET_SIZE 55
72 #define MAX_CONTACT_NUM 10
73 #define FW_POS_HEADER 0
74 #define FW_POS_STATE 1
75 #define FW_POS_TOTAL 2
77 #define FW_POS_CHECKSUM 34
78 #define FW_POS_WIDTH 35
79 #define FW_POS_PRESSURE 45
81 #define HEADER_REPORT_10_FINGER 0x62
83 /* Header (4 bytes) plus 3 fill 10-finger packets */
84 #define MAX_PACKET_SIZE 169
86 #define BOOT_TIME_DELAY_MS 50
88 /* FW read command, 0x53 0x?? 0x0, 0x01 */
89 #define E_ELAN_INFO_FW_VER 0x00
90 #define E_ELAN_INFO_BC_VER 0x10
91 #define E_ELAN_INFO_TEST_VER 0xE0
92 #define E_ELAN_INFO_FW_ID 0xF0
93 #define E_INFO_OSR 0xD6
94 #define E_INFO_PHY_SCAN 0xD7
95 #define E_INFO_PHY_DRIVER 0xD8
98 #define MAX_FW_UPDATE_RETRIES 30
100 #define ELAN_FW_PAGESIZE 132
102 /* calibration timeout definition */
103 #define ELAN_CALI_TIMEOUT_MSEC 10000
107 ELAN_WAIT_QUEUE_HEADER
,
108 ELAN_WAIT_RECALIBRATION
,
111 enum elants_iap_mode
{
112 ELAN_IAP_OPERATIONAL
,
116 /* struct elants_data - represents state of Elan touchscreen device */
118 struct i2c_client
*client
;
119 struct input_dev
*input
;
127 unsigned int x_res
; /* resolution in units/mm */
132 enum elants_state state
;
133 enum elants_iap_mode iap_mode
;
135 /* Guards against concurrent access to the device via sysfs */
136 struct mutex sysfs_mutex
;
138 u8 cmd_resp
[HEADER_SIZE
];
139 struct completion cmd_done
;
141 u8 buf
[MAX_PACKET_SIZE
];
143 bool wake_irq_enabled
;
146 static int elants_i2c_send(struct i2c_client
*client
,
147 const void *data
, size_t size
)
151 ret
= i2c_master_send(client
, data
, size
);
158 dev_err(&client
->dev
, "%s failed (%*ph): %d\n",
159 __func__
, (int)size
, data
, ret
);
164 static int elants_i2c_read(struct i2c_client
*client
, void *data
, size_t size
)
168 ret
= i2c_master_recv(client
, data
, size
);
175 dev_err(&client
->dev
, "%s failed: %d\n", __func__
, ret
);
180 static int elants_i2c_execute_command(struct i2c_client
*client
,
181 const u8
*cmd
, size_t cmd_size
,
182 u8
*resp
, size_t resp_size
)
184 struct i2c_msg msgs
[2];
186 u8 expected_response
;
189 case CMD_HEADER_READ
:
190 expected_response
= CMD_HEADER_RESP
;
193 case CMD_HEADER_6B_READ
:
194 expected_response
= CMD_HEADER_6B_RESP
;
198 dev_err(&client
->dev
, "%s: invalid command %*ph\n",
199 __func__
, (int)cmd_size
, cmd
);
203 msgs
[0].addr
= client
->addr
;
204 msgs
[0].flags
= client
->flags
& I2C_M_TEN
;
205 msgs
[0].len
= cmd_size
;
206 msgs
[0].buf
= (u8
*)cmd
;
208 msgs
[1].addr
= client
->addr
;
209 msgs
[1].flags
= client
->flags
& I2C_M_TEN
;
210 msgs
[1].flags
|= I2C_M_RD
;
211 msgs
[1].len
= resp_size
;
214 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
218 if (ret
!= ARRAY_SIZE(msgs
) || resp
[FW_HDR_TYPE
] != expected_response
)
224 static int elants_i2c_calibrate(struct elants_data
*ts
)
226 struct i2c_client
*client
= ts
->client
;
228 static const u8 w_flashkey
[] = { 0x54, 0xC0, 0xE1, 0x5A };
229 static const u8 rek
[] = { 0x54, 0x29, 0x00, 0x01 };
230 static const u8 rek_resp
[] = { CMD_HEADER_REK
, 0x66, 0x66, 0x66 };
232 disable_irq(client
->irq
);
234 ts
->state
= ELAN_WAIT_RECALIBRATION
;
235 reinit_completion(&ts
->cmd_done
);
237 elants_i2c_send(client
, w_flashkey
, sizeof(w_flashkey
));
238 elants_i2c_send(client
, rek
, sizeof(rek
));
240 enable_irq(client
->irq
);
242 ret
= wait_for_completion_interruptible_timeout(&ts
->cmd_done
,
243 msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC
));
245 ts
->state
= ELAN_STATE_NORMAL
;
248 error
= ret
< 0 ? ret
: -ETIMEDOUT
;
249 dev_err(&client
->dev
,
250 "error while waiting for calibration to complete: %d\n",
255 if (memcmp(rek_resp
, ts
->cmd_resp
, sizeof(rek_resp
))) {
256 dev_err(&client
->dev
,
257 "unexpected calibration response: %*ph\n",
258 (int)sizeof(ts
->cmd_resp
), ts
->cmd_resp
);
265 static int elants_i2c_sw_reset(struct i2c_client
*client
)
267 const u8 soft_rst_cmd
[] = { 0x77, 0x77, 0x77, 0x77 };
270 error
= elants_i2c_send(client
, soft_rst_cmd
,
271 sizeof(soft_rst_cmd
));
273 dev_err(&client
->dev
, "software reset failed: %d\n", error
);
278 * We should wait at least 10 msec (but no more than 40) before
279 * sending fastboot or IAP command to the device.
286 static u16
elants_i2c_parse_version(u8
*buf
)
288 return get_unaligned_be32(buf
) >> 4;
291 static int elants_i2c_query_fw_id(struct elants_data
*ts
)
293 struct i2c_client
*client
= ts
->client
;
294 int error
, retry_cnt
;
295 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_FW_ID
, 0x00, 0x01 };
296 u8 resp
[HEADER_SIZE
];
298 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
299 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
302 ts
->hw_version
= elants_i2c_parse_version(resp
);
303 if (ts
->hw_version
!= 0xffff)
307 dev_dbg(&client
->dev
, "read fw id error=%d, buf=%*phC\n",
308 error
, (int)sizeof(resp
), resp
);
311 dev_err(&client
->dev
,
312 "Failed to read fw id or fw id is invalid\n");
317 static int elants_i2c_query_fw_version(struct elants_data
*ts
)
319 struct i2c_client
*client
= ts
->client
;
320 int error
, retry_cnt
;
321 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_FW_VER
, 0x00, 0x01 };
322 u8 resp
[HEADER_SIZE
];
324 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
325 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
328 ts
->fw_version
= elants_i2c_parse_version(resp
);
329 if (ts
->fw_version
!= 0x0000 &&
330 ts
->fw_version
!= 0xffff)
334 dev_dbg(&client
->dev
, "read fw version error=%d, buf=%*phC\n",
335 error
, (int)sizeof(resp
), resp
);
338 dev_err(&client
->dev
,
339 "Failed to read fw version or fw version is invalid\n");
344 static int elants_i2c_query_test_version(struct elants_data
*ts
)
346 struct i2c_client
*client
= ts
->client
;
347 int error
, retry_cnt
;
349 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_TEST_VER
, 0x00, 0x01 };
350 u8 resp
[HEADER_SIZE
];
352 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
353 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
356 version
= elants_i2c_parse_version(resp
);
357 ts
->test_version
= version
>> 8;
358 ts
->solution_version
= version
& 0xff;
363 dev_dbg(&client
->dev
,
364 "read test version error rc=%d, buf=%*phC\n",
365 error
, (int)sizeof(resp
), resp
);
368 dev_err(&client
->dev
, "Failed to read test version\n");
373 static int elants_i2c_query_bc_version(struct elants_data
*ts
)
375 struct i2c_client
*client
= ts
->client
;
376 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_BC_VER
, 0x00, 0x01 };
377 u8 resp
[HEADER_SIZE
];
381 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
384 dev_err(&client
->dev
,
385 "read BC version error=%d, buf=%*phC\n",
386 error
, (int)sizeof(resp
), resp
);
390 version
= elants_i2c_parse_version(resp
);
391 ts
->bc_version
= version
>> 8;
392 ts
->iap_version
= version
& 0xff;
397 static int elants_i2c_query_ts_info(struct elants_data
*ts
)
399 struct i2c_client
*client
= ts
->client
;
402 u16 phy_x
, phy_y
, rows
, cols
, osr
;
403 const u8 get_resolution_cmd
[] = {
404 CMD_HEADER_6B_READ
, 0x00, 0x00, 0x00, 0x00, 0x00
406 const u8 get_osr_cmd
[] = {
407 CMD_HEADER_READ
, E_INFO_OSR
, 0x00, 0x01
409 const u8 get_physical_scan_cmd
[] = {
410 CMD_HEADER_READ
, E_INFO_PHY_SCAN
, 0x00, 0x01
412 const u8 get_physical_drive_cmd
[] = {
413 CMD_HEADER_READ
, E_INFO_PHY_DRIVER
, 0x00, 0x01
416 /* Get trace number */
417 error
= elants_i2c_execute_command(client
,
419 sizeof(get_resolution_cmd
),
422 dev_err(&client
->dev
, "get resolution command failed: %d\n",
427 rows
= resp
[2] + resp
[6] + resp
[10];
428 cols
= resp
[3] + resp
[7] + resp
[11];
430 /* Process mm_to_pixel information */
431 error
= elants_i2c_execute_command(client
,
432 get_osr_cmd
, sizeof(get_osr_cmd
),
435 dev_err(&client
->dev
, "get osr command failed: %d\n",
442 error
= elants_i2c_execute_command(client
,
443 get_physical_scan_cmd
,
444 sizeof(get_physical_scan_cmd
),
447 dev_err(&client
->dev
, "get physical scan command failed: %d\n",
452 phy_x
= get_unaligned_be16(&resp
[2]);
454 error
= elants_i2c_execute_command(client
,
455 get_physical_drive_cmd
,
456 sizeof(get_physical_drive_cmd
),
459 dev_err(&client
->dev
, "get physical drive command failed: %d\n",
464 phy_y
= get_unaligned_be16(&resp
[2]);
466 dev_dbg(&client
->dev
, "phy_x=%d, phy_y=%d\n", phy_x
, phy_y
);
468 if (rows
== 0 || cols
== 0 || osr
== 0) {
469 dev_warn(&client
->dev
,
470 "invalid trace number data: %d, %d, %d\n",
473 /* translate trace number to TS resolution */
474 ts
->x_max
= ELAN_TS_RESOLUTION(rows
, osr
);
475 ts
->x_res
= DIV_ROUND_CLOSEST(ts
->x_max
, phy_x
);
476 ts
->y_max
= ELAN_TS_RESOLUTION(cols
, osr
);
477 ts
->y_res
= DIV_ROUND_CLOSEST(ts
->y_max
, phy_y
);
483 static int elants_i2c_fastboot(struct i2c_client
*client
)
485 const u8 boot_cmd
[] = { 0x4D, 0x61, 0x69, 0x6E };
488 error
= elants_i2c_send(client
, boot_cmd
, sizeof(boot_cmd
));
490 dev_err(&client
->dev
, "boot failed: %d\n", error
);
494 dev_dbg(&client
->dev
, "boot success -- 0x%x\n", client
->addr
);
498 static int elants_i2c_initialize(struct elants_data
*ts
)
500 struct i2c_client
*client
= ts
->client
;
501 int error
, retry_cnt
;
502 const u8 hello_packet
[] = { 0x55, 0x55, 0x55, 0x55 };
503 const u8 recov_packet
[] = { 0x55, 0x55, 0x80, 0x80 };
506 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
507 error
= elants_i2c_sw_reset(client
);
509 /* Continue initializing if it's the last try */
510 if (retry_cnt
< MAX_RETRIES
- 1)
514 error
= elants_i2c_fastboot(client
);
516 /* Continue initializing if it's the last try */
517 if (retry_cnt
< MAX_RETRIES
- 1)
521 /* Wait for Hello packet */
522 msleep(BOOT_TIME_DELAY_MS
);
524 error
= elants_i2c_read(client
, buf
, sizeof(buf
));
526 dev_err(&client
->dev
,
527 "failed to read 'hello' packet: %d\n", error
);
528 } else if (!memcmp(buf
, hello_packet
, sizeof(hello_packet
))) {
529 ts
->iap_mode
= ELAN_IAP_OPERATIONAL
;
531 } else if (!memcmp(buf
, recov_packet
, sizeof(recov_packet
))) {
533 * Setting error code will mark device
534 * in recovery mode below.
540 dev_err(&client
->dev
,
541 "invalid 'hello' packet: %*ph\n",
542 (int)sizeof(buf
), buf
);
547 error
= elants_i2c_query_fw_id(ts
);
549 error
= elants_i2c_query_fw_version(ts
);
552 ts
->iap_mode
= ELAN_IAP_RECOVERY
;
554 elants_i2c_query_test_version(ts
);
555 elants_i2c_query_bc_version(ts
);
556 elants_i2c_query_ts_info(ts
);
563 * Firmware update interface.
566 static int elants_i2c_fw_write_page(struct i2c_client
*client
,
569 const u8 ack_ok
[] = { 0xaa, 0xaa };
574 for (retry
= 0; retry
< MAX_FW_UPDATE_RETRIES
; retry
++) {
575 error
= elants_i2c_send(client
, page
, ELAN_FW_PAGESIZE
);
577 dev_err(&client
->dev
,
578 "IAP Write Page failed: %d\n", error
);
582 error
= elants_i2c_read(client
, buf
, 2);
584 dev_err(&client
->dev
,
585 "IAP Ack read failed: %d\n", error
);
589 if (!memcmp(buf
, ack_ok
, sizeof(ack_ok
)))
593 dev_err(&client
->dev
,
594 "IAP Get Ack Error [%02x:%02x]\n",
601 static int elants_i2c_do_update_firmware(struct i2c_client
*client
,
602 const struct firmware
*fw
,
605 const u8 enter_iap
[] = { 0x45, 0x49, 0x41, 0x50 };
606 const u8 enter_iap2
[] = { 0x54, 0x00, 0x12, 0x34 };
607 const u8 iap_ack
[] = { 0x55, 0xaa, 0x33, 0xcc };
610 int page
, n_fw_pages
;
613 /* Recovery mode detection! */
615 dev_dbg(&client
->dev
, "Recovery mode procedure\n");
616 error
= elants_i2c_send(client
, enter_iap2
, sizeof(enter_iap2
));
618 /* Start IAP Procedure */
619 dev_dbg(&client
->dev
, "Normal IAP procedure\n");
620 elants_i2c_sw_reset(client
);
622 error
= elants_i2c_send(client
, enter_iap
, sizeof(enter_iap
));
626 dev_err(&client
->dev
, "failed to enter IAP mode: %d\n", error
);
632 /* check IAP state */
633 error
= elants_i2c_read(client
, buf
, 4);
635 dev_err(&client
->dev
,
636 "failed to read IAP acknowledgement: %d\n",
641 if (memcmp(buf
, iap_ack
, sizeof(iap_ack
))) {
642 dev_err(&client
->dev
,
643 "failed to enter IAP: %*ph (expected %*ph)\n",
644 (int)sizeof(buf
), buf
, (int)sizeof(iap_ack
), iap_ack
);
648 dev_info(&client
->dev
, "successfully entered IAP mode");
650 send_id
= client
->addr
;
651 error
= elants_i2c_send(client
, &send_id
, 1);
653 dev_err(&client
->dev
, "sending dummy byte failed: %d\n",
658 /* Clear the last page of Master */
659 error
= elants_i2c_send(client
, fw
->data
, ELAN_FW_PAGESIZE
);
661 dev_err(&client
->dev
, "clearing of the last page failed: %d\n",
666 error
= elants_i2c_read(client
, buf
, 2);
668 dev_err(&client
->dev
,
669 "failed to read ACK for clearing the last page: %d\n",
674 n_fw_pages
= fw
->size
/ ELAN_FW_PAGESIZE
;
675 dev_dbg(&client
->dev
, "IAP Pages = %d\n", n_fw_pages
);
677 for (page
= 0; page
< n_fw_pages
; page
++) {
678 error
= elants_i2c_fw_write_page(client
,
679 fw
->data
+ page
* ELAN_FW_PAGESIZE
);
681 dev_err(&client
->dev
,
682 "failed to write FW page %d: %d\n",
688 /* Old iap needs to wait 200ms for WDT and rest is for hello packets */
691 dev_info(&client
->dev
, "firmware update completed\n");
695 static int elants_i2c_fw_update(struct elants_data
*ts
)
697 struct i2c_client
*client
= ts
->client
;
698 const struct firmware
*fw
;
702 fw_name
= kasprintf(GFP_KERNEL
, "elants_i2c_%04x.bin", ts
->hw_version
);
706 dev_info(&client
->dev
, "requesting fw name = %s\n", fw_name
);
707 error
= request_firmware(&fw
, fw_name
, &client
->dev
);
710 dev_err(&client
->dev
, "failed to request firmware: %d\n",
715 if (fw
->size
% ELAN_FW_PAGESIZE
) {
716 dev_err(&client
->dev
, "invalid firmware length: %zu\n",
722 disable_irq(client
->irq
);
724 error
= elants_i2c_do_update_firmware(client
, fw
,
725 ts
->iap_mode
== ELAN_IAP_RECOVERY
);
727 dev_err(&client
->dev
, "firmware update failed: %d\n", error
);
728 ts
->iap_mode
= ELAN_IAP_RECOVERY
;
732 error
= elants_i2c_initialize(ts
);
734 dev_err(&client
->dev
,
735 "failed to initialize device after firmware update: %d\n",
737 ts
->iap_mode
= ELAN_IAP_RECOVERY
;
741 ts
->iap_mode
= ELAN_IAP_OPERATIONAL
;
744 ts
->state
= ELAN_STATE_NORMAL
;
745 enable_irq(client
->irq
);
749 elants_i2c_calibrate(ts
);
751 release_firmware(fw
);
759 static void elants_i2c_mt_event(struct elants_data
*ts
, u8
*buf
)
761 struct input_dev
*input
= ts
->input
;
762 unsigned int n_fingers
;
766 n_fingers
= buf
[FW_POS_STATE
+ 1] & 0x0f;
767 finger_state
= ((buf
[FW_POS_STATE
+ 1] & 0x30) << 4) |
770 dev_dbg(&ts
->client
->dev
,
771 "n_fingers: %u, state: %04x\n", n_fingers
, finger_state
);
773 for (i
= 0; i
< MAX_CONTACT_NUM
&& n_fingers
; i
++) {
774 if (finger_state
& 1) {
775 unsigned int x
, y
, p
, w
;
778 pos
= &buf
[FW_POS_XY
+ i
* 3];
779 x
= (((u16
)pos
[0] & 0xf0) << 4) | pos
[1];
780 y
= (((u16
)pos
[0] & 0x0f) << 8) | pos
[2];
781 p
= buf
[FW_POS_PRESSURE
+ i
];
782 w
= buf
[FW_POS_WIDTH
+ i
];
784 dev_dbg(&ts
->client
->dev
, "i=%d x=%d y=%d p=%d w=%d\n",
787 input_mt_slot(input
, i
);
788 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
789 input_event(input
, EV_ABS
, ABS_MT_POSITION_X
, x
);
790 input_event(input
, EV_ABS
, ABS_MT_POSITION_Y
, y
);
791 input_event(input
, EV_ABS
, ABS_MT_PRESSURE
, p
);
792 input_event(input
, EV_ABS
, ABS_MT_TOUCH_MAJOR
, w
);
800 input_mt_sync_frame(input
);
804 static u8
elants_i2c_calculate_checksum(u8
*buf
)
809 for (i
= 0; i
< FW_POS_CHECKSUM
; i
++)
815 static void elants_i2c_event(struct elants_data
*ts
, u8
*buf
)
817 u8 checksum
= elants_i2c_calculate_checksum(buf
);
819 if (unlikely(buf
[FW_POS_CHECKSUM
] != checksum
))
820 dev_warn(&ts
->client
->dev
,
821 "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
822 __func__
, buf
[FW_POS_HEADER
],
823 checksum
, buf
[FW_POS_CHECKSUM
]);
824 else if (unlikely(buf
[FW_POS_HEADER
] != HEADER_REPORT_10_FINGER
))
825 dev_warn(&ts
->client
->dev
,
826 "%s: unknown packet type: %02x\n",
827 __func__
, buf
[FW_POS_HEADER
]);
829 elants_i2c_mt_event(ts
, buf
);
832 static irqreturn_t
elants_i2c_irq(int irq
, void *_dev
)
834 const u8 wait_packet
[] = { 0x64, 0x64, 0x64, 0x64 };
835 struct elants_data
*ts
= _dev
;
836 struct i2c_client
*client
= ts
->client
;
837 int report_count
, report_len
;
841 len
= i2c_master_recv(client
, ts
->buf
, sizeof(ts
->buf
));
843 dev_err(&client
->dev
, "%s: failed to read data: %d\n",
848 dev_dbg(&client
->dev
, "%s: packet %*ph\n",
849 __func__
, HEADER_SIZE
, ts
->buf
);
852 case ELAN_WAIT_RECALIBRATION
:
853 if (ts
->buf
[FW_HDR_TYPE
] == CMD_HEADER_REK
) {
854 memcpy(ts
->cmd_resp
, ts
->buf
, sizeof(ts
->cmd_resp
));
855 complete(&ts
->cmd_done
);
856 ts
->state
= ELAN_STATE_NORMAL
;
860 case ELAN_WAIT_QUEUE_HEADER
:
861 if (ts
->buf
[FW_HDR_TYPE
] != QUEUE_HEADER_NORMAL
)
864 ts
->state
= ELAN_STATE_NORMAL
;
867 case ELAN_STATE_NORMAL
:
869 switch (ts
->buf
[FW_HDR_TYPE
]) {
870 case CMD_HEADER_HELLO
:
871 case CMD_HEADER_RESP
:
875 case QUEUE_HEADER_WAIT
:
876 if (memcmp(ts
->buf
, wait_packet
, sizeof(wait_packet
))) {
877 dev_err(&client
->dev
,
878 "invalid wait packet %*ph\n",
879 HEADER_SIZE
, ts
->buf
);
881 ts
->state
= ELAN_WAIT_QUEUE_HEADER
;
886 case QUEUE_HEADER_SINGLE
:
887 elants_i2c_event(ts
, &ts
->buf
[HEADER_SIZE
]);
890 case QUEUE_HEADER_NORMAL
:
891 report_count
= ts
->buf
[FW_HDR_COUNT
];
892 if (report_count
> 3) {
893 dev_err(&client
->dev
,
894 "too large report count: %*ph\n",
895 HEADER_SIZE
, ts
->buf
);
899 report_len
= ts
->buf
[FW_HDR_LENGTH
] / report_count
;
900 if (report_len
!= PACKET_SIZE
) {
901 dev_err(&client
->dev
,
902 "mismatching report length: %*ph\n",
903 HEADER_SIZE
, ts
->buf
);
907 for (i
= 0; i
< report_count
; i
++) {
908 u8
*buf
= ts
->buf
+ HEADER_SIZE
+
910 elants_i2c_event(ts
, buf
);
915 dev_err(&client
->dev
, "unknown packet %*ph\n",
916 HEADER_SIZE
, ts
->buf
);
929 static ssize_t
calibrate_store(struct device
*dev
,
930 struct device_attribute
*attr
,
931 const char *buf
, size_t count
)
933 struct i2c_client
*client
= to_i2c_client(dev
);
934 struct elants_data
*ts
= i2c_get_clientdata(client
);
937 error
= mutex_lock_interruptible(&ts
->sysfs_mutex
);
941 error
= elants_i2c_calibrate(ts
);
943 mutex_unlock(&ts
->sysfs_mutex
);
944 return error
?: count
;
947 static ssize_t
write_update_fw(struct device
*dev
,
948 struct device_attribute
*attr
,
949 const char *buf
, size_t count
)
951 struct i2c_client
*client
= to_i2c_client(dev
);
952 struct elants_data
*ts
= i2c_get_clientdata(client
);
955 error
= mutex_lock_interruptible(&ts
->sysfs_mutex
);
959 error
= elants_i2c_fw_update(ts
);
960 dev_dbg(dev
, "firmware update result: %d\n", error
);
962 mutex_unlock(&ts
->sysfs_mutex
);
963 return error
?: count
;
966 static ssize_t
show_iap_mode(struct device
*dev
,
967 struct device_attribute
*attr
, char *buf
)
969 struct i2c_client
*client
= to_i2c_client(dev
);
970 struct elants_data
*ts
= i2c_get_clientdata(client
);
972 return sprintf(buf
, "%s\n",
973 ts
->iap_mode
== ELAN_IAP_OPERATIONAL
?
974 "Normal" : "Recovery");
977 static DEVICE_ATTR(calibrate
, S_IWUSR
, NULL
, calibrate_store
);
978 static DEVICE_ATTR(iap_mode
, S_IRUGO
, show_iap_mode
, NULL
);
979 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, write_update_fw
);
981 struct elants_version_attribute
{
982 struct device_attribute dattr
;
987 #define __ELANTS_FIELD_SIZE(_field) \
988 sizeof(((struct elants_data *)NULL)->_field)
989 #define __ELANTS_VERIFY_SIZE(_field) \
990 (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \
991 __ELANTS_FIELD_SIZE(_field))
992 #define ELANTS_VERSION_ATTR(_field) \
993 struct elants_version_attribute elants_ver_attr_##_field = { \
994 .dattr = __ATTR(_field, S_IRUGO, \
995 elants_version_attribute_show, NULL), \
996 .field_offset = offsetof(struct elants_data, _field), \
997 .field_size = __ELANTS_VERIFY_SIZE(_field), \
1000 static ssize_t
elants_version_attribute_show(struct device
*dev
,
1001 struct device_attribute
*dattr
,
1004 struct i2c_client
*client
= to_i2c_client(dev
);
1005 struct elants_data
*ts
= i2c_get_clientdata(client
);
1006 struct elants_version_attribute
*attr
=
1007 container_of(dattr
, struct elants_version_attribute
, dattr
);
1008 u8
*field
= (u8
*)((char *)ts
+ attr
->field_offset
);
1009 unsigned int fmt_size
;
1012 if (attr
->field_size
== 1) {
1014 fmt_size
= 2; /* 2 HEX digits */
1016 val
= *(u16
*)field
;
1017 fmt_size
= 4; /* 4 HEX digits */
1020 return sprintf(buf
, "%0*x\n", fmt_size
, val
);
1023 static ELANTS_VERSION_ATTR(fw_version
);
1024 static ELANTS_VERSION_ATTR(hw_version
);
1025 static ELANTS_VERSION_ATTR(test_version
);
1026 static ELANTS_VERSION_ATTR(solution_version
);
1027 static ELANTS_VERSION_ATTR(bc_version
);
1028 static ELANTS_VERSION_ATTR(iap_version
);
1030 static struct attribute
*elants_attributes
[] = {
1031 &dev_attr_calibrate
.attr
,
1032 &dev_attr_update_fw
.attr
,
1033 &dev_attr_iap_mode
.attr
,
1035 &elants_ver_attr_fw_version
.dattr
.attr
,
1036 &elants_ver_attr_hw_version
.dattr
.attr
,
1037 &elants_ver_attr_test_version
.dattr
.attr
,
1038 &elants_ver_attr_solution_version
.dattr
.attr
,
1039 &elants_ver_attr_bc_version
.dattr
.attr
,
1040 &elants_ver_attr_iap_version
.dattr
.attr
,
1044 static struct attribute_group elants_attribute_group
= {
1045 .attrs
= elants_attributes
,
1048 static void elants_i2c_remove_sysfs_group(void *_data
)
1050 struct elants_data
*ts
= _data
;
1052 sysfs_remove_group(&ts
->client
->dev
.kobj
, &elants_attribute_group
);
1055 static int elants_i2c_probe(struct i2c_client
*client
,
1056 const struct i2c_device_id
*id
)
1058 union i2c_smbus_data dummy
;
1059 struct elants_data
*ts
;
1060 unsigned long irqflags
;
1063 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1064 dev_err(&client
->dev
,
1065 "%s: i2c check functionality error\n", DEVICE_NAME
);
1069 /* Make sure there is something at this address */
1070 if (i2c_smbus_xfer(client
->adapter
, client
->addr
, 0,
1071 I2C_SMBUS_READ
, 0, I2C_SMBUS_BYTE
, &dummy
) < 0) {
1072 dev_err(&client
->dev
, "nothing at this address\n");
1076 ts
= devm_kzalloc(&client
->dev
, sizeof(struct elants_data
), GFP_KERNEL
);
1080 mutex_init(&ts
->sysfs_mutex
);
1081 init_completion(&ts
->cmd_done
);
1083 ts
->client
= client
;
1084 i2c_set_clientdata(client
, ts
);
1086 error
= elants_i2c_initialize(ts
);
1088 dev_err(&client
->dev
, "failed to initialize: %d\n", error
);
1092 ts
->input
= devm_input_allocate_device(&client
->dev
);
1094 dev_err(&client
->dev
, "Failed to allocate input device\n");
1098 ts
->input
->name
= "Elan Touchscreen";
1099 ts
->input
->id
.bustype
= BUS_I2C
;
1101 __set_bit(BTN_TOUCH
, ts
->input
->keybit
);
1102 __set_bit(EV_ABS
, ts
->input
->evbit
);
1103 __set_bit(EV_KEY
, ts
->input
->evbit
);
1105 /* Single touch input params setup */
1106 input_set_abs_params(ts
->input
, ABS_X
, 0, ts
->x_max
, 0, 0);
1107 input_set_abs_params(ts
->input
, ABS_Y
, 0, ts
->y_max
, 0, 0);
1108 input_set_abs_params(ts
->input
, ABS_PRESSURE
, 0, 255, 0, 0);
1109 input_abs_set_res(ts
->input
, ABS_X
, ts
->x_res
);
1110 input_abs_set_res(ts
->input
, ABS_Y
, ts
->y_res
);
1112 /* Multitouch input params setup */
1113 error
= input_mt_init_slots(ts
->input
, MAX_CONTACT_NUM
,
1114 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
1116 dev_err(&client
->dev
,
1117 "failed to initialize MT slots: %d\n", error
);
1121 input_set_abs_params(ts
->input
, ABS_MT_POSITION_X
, 0, ts
->x_max
, 0, 0);
1122 input_set_abs_params(ts
->input
, ABS_MT_POSITION_Y
, 0, ts
->y_max
, 0, 0);
1123 input_set_abs_params(ts
->input
, ABS_MT_TOUCH_MAJOR
, 0, 255, 0, 0);
1124 input_set_abs_params(ts
->input
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
1125 input_abs_set_res(ts
->input
, ABS_MT_POSITION_X
, ts
->x_res
);
1126 input_abs_set_res(ts
->input
, ABS_MT_POSITION_Y
, ts
->y_res
);
1128 input_set_drvdata(ts
->input
, ts
);
1130 error
= input_register_device(ts
->input
);
1132 dev_err(&client
->dev
,
1133 "unable to register input device: %d\n", error
);
1138 * Systems using device tree should set up interrupt via DTS,
1139 * the rest will use the default falling edge interrupts.
1141 irqflags
= client
->dev
.of_node
? 0 : IRQF_TRIGGER_FALLING
;
1143 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1144 NULL
, elants_i2c_irq
,
1145 irqflags
| IRQF_ONESHOT
,
1148 dev_err(&client
->dev
, "Failed to register interrupt\n");
1153 * Systems using device tree should set up wakeup via DTS,
1154 * the rest will configure device as wakeup source by default.
1156 if (!client
->dev
.of_node
)
1157 device_init_wakeup(&client
->dev
, true);
1159 error
= sysfs_create_group(&client
->dev
.kobj
, &elants_attribute_group
);
1161 dev_err(&client
->dev
, "failed to create sysfs attributes: %d\n",
1166 error
= devm_add_action(&client
->dev
,
1167 elants_i2c_remove_sysfs_group
, ts
);
1169 elants_i2c_remove_sysfs_group(ts
);
1170 dev_err(&client
->dev
,
1171 "Failed to add sysfs cleanup action: %d\n",
1179 static int __maybe_unused
elants_i2c_suspend(struct device
*dev
)
1181 struct i2c_client
*client
= to_i2c_client(dev
);
1182 struct elants_data
*ts
= i2c_get_clientdata(client
);
1183 const u8 set_sleep_cmd
[] = { 0x54, 0x50, 0x00, 0x01 };
1187 /* Command not support in IAP recovery mode */
1188 if (ts
->iap_mode
!= ELAN_IAP_OPERATIONAL
)
1191 disable_irq(client
->irq
);
1193 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
1194 error
= elants_i2c_send(client
, set_sleep_cmd
,
1195 sizeof(set_sleep_cmd
));
1199 dev_err(&client
->dev
, "suspend command failed: %d\n", error
);
1202 if (device_may_wakeup(dev
))
1203 ts
->wake_irq_enabled
= (enable_irq_wake(client
->irq
) == 0);
1208 static int __maybe_unused
elants_i2c_resume(struct device
*dev
)
1210 struct i2c_client
*client
= to_i2c_client(dev
);
1211 struct elants_data
*ts
= i2c_get_clientdata(client
);
1212 const u8 set_active_cmd
[] = { 0x54, 0x58, 0x00, 0x01 };
1216 if (device_may_wakeup(dev
) && ts
->wake_irq_enabled
)
1217 disable_irq_wake(client
->irq
);
1219 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
1220 error
= elants_i2c_send(client
, set_active_cmd
,
1221 sizeof(set_active_cmd
));
1225 dev_err(&client
->dev
, "resume command failed: %d\n", error
);
1228 ts
->state
= ELAN_STATE_NORMAL
;
1229 enable_irq(client
->irq
);
1234 static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops
,
1235 elants_i2c_suspend
, elants_i2c_resume
);
1237 static const struct i2c_device_id elants_i2c_id
[] = {
1241 MODULE_DEVICE_TABLE(i2c
, elants_i2c_id
);
1244 static const struct acpi_device_id elants_acpi_id
[] = {
1248 MODULE_DEVICE_TABLE(acpi
, elants_acpi_id
);
1252 static const struct of_device_id elants_of_match
[] = {
1253 { .compatible
= "elan,ekth3500" },
1256 MODULE_DEVICE_TABLE(of
, elants_of_match
);
1259 static struct i2c_driver elants_i2c_driver
= {
1260 .probe
= elants_i2c_probe
,
1261 .id_table
= elants_i2c_id
,
1263 .name
= DEVICE_NAME
,
1264 .owner
= THIS_MODULE
,
1265 .pm
= &elants_i2c_pm_ops
,
1266 .acpi_match_table
= ACPI_PTR(elants_acpi_id
),
1267 .of_match_table
= of_match_ptr(elants_of_match
),
1270 module_i2c_driver(elants_i2c_driver
);
1272 MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>");
1273 MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
1274 MODULE_VERSION(DRV_VERSION
);
1275 MODULE_LICENSE("GPL");