1 // SPDX-License-Identifier: GPL-2.0-only
3 * Elan Microelectronics touch panels with I2C interface
5 * Copyright (C) 2014 Elan Microelectronics Corporation.
6 * Scott Liu <scott.liu@emc.com.tw>
8 * This code is partly based on hid-multitouch.c:
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
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 #include <linux/bits.h>
23 #include <linux/module.h>
24 #include <linux/input.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
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>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/input/mt.h>
36 #include <linux/input/touchscreen.h>
37 #include <linux/acpi.h>
39 #include <linux/pm_wakeirq.h>
40 #include <linux/gpio/consumer.h>
41 #include <linux/regulator/consumer.h>
42 #include <linux/uuid.h>
43 #include <linux/unaligned.h>
45 /* Device, Driver information */
46 #define DEVICE_NAME "elants_i2c"
48 /* Convert from rows or columns into resolution */
49 #define ELAN_TS_RESOLUTION(n, m) (((n) - 1) * (m))
54 #define FW_HDR_COUNT 1
55 #define FW_HDR_LENGTH 2
57 /* Buffer mode Queue Header information */
58 #define QUEUE_HEADER_SINGLE 0x62
59 #define QUEUE_HEADER_NORMAL 0X63
60 #define QUEUE_HEADER_WAIT 0x64
61 #define QUEUE_HEADER_NORMAL2 0x66
63 /* Command header definition */
64 #define CMD_HEADER_WRITE 0x54
65 #define CMD_HEADER_READ 0x53
66 #define CMD_HEADER_6B_READ 0x5B
67 #define CMD_HEADER_ROM_READ 0x96
68 #define CMD_HEADER_RESP 0x52
69 #define CMD_HEADER_6B_RESP 0x9B
70 #define CMD_HEADER_ROM_RESP 0x95
71 #define CMD_HEADER_HELLO 0x55
72 #define CMD_HEADER_REK 0x66
74 /* FW position data */
75 #define PACKET_SIZE_OLD 40
76 #define PACKET_SIZE 55
77 #define MAX_CONTACT_NUM 10
78 #define FW_POS_HEADER 0
79 #define FW_POS_STATE 1
80 #define FW_POS_TOTAL 2
82 #define FW_POS_TOOL_TYPE 33
83 #define FW_POS_CHECKSUM 34
84 #define FW_POS_WIDTH 35
85 #define FW_POS_PRESSURE 45
87 #define HEADER_REPORT_10_FINGER 0x62
89 /* Header (4 bytes) plus 3 full 10-finger packets */
90 #define MAX_PACKET_SIZE 169
92 #define BOOT_TIME_DELAY_MS 50
94 /* FW read command, 0x53 0x?? 0x0, 0x01 */
95 #define E_ELAN_INFO_FW_VER 0x00
96 #define E_ELAN_INFO_BC_VER 0x10
97 #define E_ELAN_INFO_X_RES 0x60
98 #define E_ELAN_INFO_Y_RES 0x63
99 #define E_ELAN_INFO_REK 0xD0
100 #define E_ELAN_INFO_TEST_VER 0xE0
101 #define E_ELAN_INFO_FW_ID 0xF0
102 #define E_INFO_OSR 0xD6
103 #define E_INFO_PHY_SCAN 0xD7
104 #define E_INFO_PHY_DRIVER 0xD8
106 /* FW write command, 0x54 0x?? 0x0, 0x01 */
107 #define E_POWER_STATE_SLEEP 0x50
108 #define E_POWER_STATE_RESUME 0x58
110 #define MAX_RETRIES 3
111 #define MAX_FW_UPDATE_RETRIES 30
113 #define ELAN_FW_PAGESIZE 132
115 /* calibration timeout definition */
116 #define ELAN_CALI_TIMEOUT_MSEC 12000
118 #define ELAN_POWERON_DELAY_USEC 5000
119 #define ELAN_RESET_DELAY_MSEC 20
121 /* FW boot code version */
122 #define BC_VER_H_BYTE_FOR_EKTH3900x1_I2C 0x72
123 #define BC_VER_H_BYTE_FOR_EKTH3900x2_I2C 0x82
124 #define BC_VER_H_BYTE_FOR_EKTH3900x3_I2C 0x92
125 #define BC_VER_H_BYTE_FOR_EKTH5312x1_I2C 0x6D
126 #define BC_VER_H_BYTE_FOR_EKTH5312x2_I2C 0x6E
127 #define BC_VER_H_BYTE_FOR_EKTH5312cx1_I2C 0x77
128 #define BC_VER_H_BYTE_FOR_EKTH5312cx2_I2C 0x78
129 #define BC_VER_H_BYTE_FOR_EKTH5312x1_I2C_USB 0x67
130 #define BC_VER_H_BYTE_FOR_EKTH5312x2_I2C_USB 0x68
131 #define BC_VER_H_BYTE_FOR_EKTH5312cx1_I2C_USB 0x74
132 #define BC_VER_H_BYTE_FOR_EKTH5312cx2_I2C_USB 0x75
134 enum elants_chip_id
{
141 ELAN_WAIT_QUEUE_HEADER
,
142 ELAN_WAIT_RECALIBRATION
,
145 enum elants_iap_mode
{
146 ELAN_IAP_OPERATIONAL
,
150 /* struct elants_data - represents state of Elan touchscreen device */
152 struct i2c_client
*client
;
153 struct input_dev
*input
;
155 struct regulator
*vcc33
;
156 struct regulator
*vccio
;
157 struct gpio_desc
*reset_gpio
;
166 unsigned int x_res
; /* resolution in units/mm */
172 struct touchscreen_properties prop
;
174 enum elants_state state
;
175 enum elants_chip_id chip_id
;
176 enum elants_iap_mode iap_mode
;
178 /* Guards against concurrent access to the device via sysfs */
179 struct mutex sysfs_mutex
;
181 u8 cmd_resp
[HEADER_SIZE
];
182 struct completion cmd_done
;
184 bool keep_power_in_suspend
;
186 /* Must be last to be used for DMA operations */
187 u8 buf
[MAX_PACKET_SIZE
] ____cacheline_aligned
;
190 static int elants_i2c_send(struct i2c_client
*client
,
191 const void *data
, size_t size
)
195 ret
= i2c_master_send(client
, data
, size
);
202 dev_err(&client
->dev
, "%s failed (%*ph): %d\n",
203 __func__
, (int)size
, data
, ret
);
208 static int elants_i2c_read(struct i2c_client
*client
, void *data
, size_t size
)
212 ret
= i2c_master_recv(client
, data
, size
);
219 dev_err(&client
->dev
, "%s failed: %d\n", __func__
, ret
);
224 static int elants_i2c_execute_command(struct i2c_client
*client
,
225 const u8
*cmd
, size_t cmd_size
,
226 u8
*resp
, size_t resp_size
,
227 int retries
, const char *cmd_name
)
229 struct i2c_msg msgs
[2];
231 u8 expected_response
;
234 case CMD_HEADER_READ
:
235 expected_response
= CMD_HEADER_RESP
;
238 case CMD_HEADER_6B_READ
:
239 expected_response
= CMD_HEADER_6B_RESP
;
242 case CMD_HEADER_ROM_READ
:
243 expected_response
= CMD_HEADER_ROM_RESP
;
247 dev_err(&client
->dev
, "(%s): invalid command: %*ph\n",
248 cmd_name
, (int)cmd_size
, cmd
);
253 msgs
[0].addr
= client
->addr
;
254 msgs
[0].flags
= client
->flags
& I2C_M_TEN
;
255 msgs
[0].len
= cmd_size
;
256 msgs
[0].buf
= (u8
*)cmd
;
258 msgs
[1].addr
= client
->addr
;
259 msgs
[1].flags
= (client
->flags
& I2C_M_TEN
) | I2C_M_RD
;
260 msgs
[1].flags
|= I2C_M_RD
;
261 msgs
[1].len
= resp_size
;
264 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
267 dev_dbg(&client
->dev
,
268 "(%s) I2C transfer failed: %pe (retrying)\n",
269 cmd_name
, ERR_PTR(ret
));
273 dev_err(&client
->dev
,
274 "(%s) I2C transfer failed: %pe\n",
275 cmd_name
, ERR_PTR(ret
));
279 if (ret
!= ARRAY_SIZE(msgs
) ||
280 resp
[FW_HDR_TYPE
] != expected_response
) {
282 dev_dbg(&client
->dev
,
283 "(%s) unexpected response: %*ph (retrying)\n",
284 cmd_name
, ret
, resp
);
288 dev_err(&client
->dev
,
289 "(%s) unexpected response: %*ph\n",
290 cmd_name
, ret
, resp
);
298 static int elants_i2c_calibrate(struct elants_data
*ts
)
300 struct i2c_client
*client
= ts
->client
;
302 static const u8 w_flashkey
[] = { CMD_HEADER_WRITE
, 0xC0, 0xE1, 0x5A };
303 static const u8 rek
[] = { CMD_HEADER_WRITE
, 0x29, 0x00, 0x01 };
304 static const u8 rek_resp
[] = { CMD_HEADER_REK
, 0x66, 0x66, 0x66 };
306 disable_irq(client
->irq
);
308 ts
->state
= ELAN_WAIT_RECALIBRATION
;
309 reinit_completion(&ts
->cmd_done
);
311 elants_i2c_send(client
, w_flashkey
, sizeof(w_flashkey
));
312 elants_i2c_send(client
, rek
, sizeof(rek
));
314 enable_irq(client
->irq
);
316 ret
= wait_for_completion_interruptible_timeout(&ts
->cmd_done
,
317 msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC
));
319 ts
->state
= ELAN_STATE_NORMAL
;
322 error
= ret
< 0 ? ret
: -ETIMEDOUT
;
323 dev_err(&client
->dev
,
324 "error while waiting for calibration to complete: %d\n",
329 if (memcmp(rek_resp
, ts
->cmd_resp
, sizeof(rek_resp
))) {
330 dev_err(&client
->dev
,
331 "unexpected calibration response: %*ph\n",
332 (int)sizeof(ts
->cmd_resp
), ts
->cmd_resp
);
339 static int elants_i2c_sw_reset(struct i2c_client
*client
)
341 const u8 soft_rst_cmd
[] = { 0x77, 0x77, 0x77, 0x77 };
344 error
= elants_i2c_send(client
, soft_rst_cmd
,
345 sizeof(soft_rst_cmd
));
347 dev_err(&client
->dev
, "software reset failed: %d\n", error
);
352 * We should wait at least 10 msec (but no more than 40) before
353 * sending fastboot or IAP command to the device.
360 static u16
elants_i2c_parse_version(u8
*buf
)
362 return get_unaligned_be32(buf
) >> 4;
365 static int elants_i2c_query_hw_version(struct elants_data
*ts
)
367 struct i2c_client
*client
= ts
->client
;
368 int retry_cnt
= MAX_RETRIES
;
369 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_FW_ID
, 0x00, 0x01 };
370 u8 resp
[HEADER_SIZE
];
373 while (retry_cnt
--) {
374 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
375 resp
, sizeof(resp
), 1,
380 ts
->hw_version
= elants_i2c_parse_version(resp
);
381 if (ts
->hw_version
!= 0xffff)
385 dev_err(&client
->dev
, "Invalid fw id: %#04x\n", ts
->hw_version
);
390 static int elants_i2c_query_fw_version(struct elants_data
*ts
)
392 struct i2c_client
*client
= ts
->client
;
393 int retry_cnt
= MAX_RETRIES
;
394 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_FW_VER
, 0x00, 0x01 };
395 u8 resp
[HEADER_SIZE
];
398 while (retry_cnt
--) {
399 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
400 resp
, sizeof(resp
), 1,
405 ts
->fw_version
= elants_i2c_parse_version(resp
);
406 if (ts
->fw_version
!= 0x0000 && ts
->fw_version
!= 0xffff)
409 dev_dbg(&client
->dev
, "(read fw version) resp %*phC\n",
410 (int)sizeof(resp
), resp
);
413 dev_err(&client
->dev
, "Invalid fw ver: %#04x\n", ts
->fw_version
);
418 static int elants_i2c_query_test_version(struct elants_data
*ts
)
420 struct i2c_client
*client
= ts
->client
;
423 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_TEST_VER
, 0x00, 0x01 };
424 u8 resp
[HEADER_SIZE
];
426 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
427 resp
, sizeof(resp
), MAX_RETRIES
,
428 "read test version");
430 dev_err(&client
->dev
, "Failed to read test version\n");
434 version
= elants_i2c_parse_version(resp
);
435 ts
->test_version
= version
>> 8;
436 ts
->solution_version
= version
& 0xff;
441 static int elants_i2c_query_bc_version(struct elants_data
*ts
)
443 struct i2c_client
*client
= ts
->client
;
444 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_BC_VER
, 0x00, 0x01 };
445 u8 resp
[HEADER_SIZE
];
449 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
450 resp
, sizeof(resp
), 1,
455 version
= elants_i2c_parse_version(resp
);
456 ts
->bc_version
= version
>> 8;
457 ts
->iap_version
= version
& 0xff;
462 static int elants_i2c_query_ts_info_ektf(struct elants_data
*ts
)
464 struct i2c_client
*client
= ts
->client
;
468 const u8 get_xres_cmd
[] = {
469 CMD_HEADER_READ
, E_ELAN_INFO_X_RES
, 0x00, 0x00
471 const u8 get_yres_cmd
[] = {
472 CMD_HEADER_READ
, E_ELAN_INFO_Y_RES
, 0x00, 0x00
475 /* Get X/Y size in mm */
476 error
= elants_i2c_execute_command(client
, get_xres_cmd
,
477 sizeof(get_xres_cmd
),
478 resp
, sizeof(resp
), 1,
483 phy_x
= resp
[2] | ((resp
[3] & 0xF0) << 4);
485 error
= elants_i2c_execute_command(client
, get_yres_cmd
,
486 sizeof(get_yres_cmd
),
487 resp
, sizeof(resp
), 1,
492 phy_y
= resp
[2] | ((resp
[3] & 0xF0) << 4);
494 dev_dbg(&client
->dev
, "phy_x=%d, phy_y=%d\n", phy_x
, phy_y
);
499 /* eKTF doesn't report max size, set it to default values */
500 ts
->x_max
= 2240 - 1;
501 ts
->y_max
= 1408 - 1;
506 static int elants_i2c_query_ts_info_ekth(struct elants_data
*ts
)
508 struct i2c_client
*client
= ts
->client
;
511 u16 phy_x
, phy_y
, rows
, cols
, osr
;
512 const u8 get_resolution_cmd
[] = {
513 CMD_HEADER_6B_READ
, 0x00, 0x00, 0x00, 0x00, 0x00
515 const u8 get_osr_cmd
[] = {
516 CMD_HEADER_READ
, E_INFO_OSR
, 0x00, 0x01
518 const u8 get_physical_scan_cmd
[] = {
519 CMD_HEADER_READ
, E_INFO_PHY_SCAN
, 0x00, 0x01
521 const u8 get_physical_drive_cmd
[] = {
522 CMD_HEADER_READ
, E_INFO_PHY_DRIVER
, 0x00, 0x01
525 /* Get trace number */
526 error
= elants_i2c_execute_command(client
,
528 sizeof(get_resolution_cmd
),
529 resp
, sizeof(resp
), 1,
534 rows
= resp
[2] + resp
[6] + resp
[10];
535 cols
= resp
[3] + resp
[7] + resp
[11];
537 /* Get report resolution value of ABS_MT_TOUCH_MAJOR */
538 ts
->major_res
= resp
[16];
540 /* Process mm_to_pixel information */
541 error
= elants_i2c_execute_command(client
,
542 get_osr_cmd
, sizeof(get_osr_cmd
),
543 resp
, sizeof(resp
), 1, "get osr");
549 error
= elants_i2c_execute_command(client
,
550 get_physical_scan_cmd
,
551 sizeof(get_physical_scan_cmd
),
552 resp
, sizeof(resp
), 1,
553 "get physical scan");
557 phy_x
= get_unaligned_be16(&resp
[2]);
559 error
= elants_i2c_execute_command(client
,
560 get_physical_drive_cmd
,
561 sizeof(get_physical_drive_cmd
),
562 resp
, sizeof(resp
), 1,
563 "get physical drive");
567 phy_y
= get_unaligned_be16(&resp
[2]);
569 dev_dbg(&client
->dev
, "phy_x=%d, phy_y=%d\n", phy_x
, phy_y
);
571 if (rows
== 0 || cols
== 0 || osr
== 0) {
572 dev_warn(&client
->dev
,
573 "invalid trace number data: %d, %d, %d\n",
576 /* translate trace number to TS resolution */
577 ts
->x_max
= ELAN_TS_RESOLUTION(rows
, osr
);
578 ts
->x_res
= DIV_ROUND_CLOSEST(ts
->x_max
, phy_x
);
579 ts
->y_max
= ELAN_TS_RESOLUTION(cols
, osr
);
580 ts
->y_res
= DIV_ROUND_CLOSEST(ts
->y_max
, phy_y
);
588 static int elants_i2c_fastboot(struct i2c_client
*client
)
590 const u8 boot_cmd
[] = { 0x4D, 0x61, 0x69, 0x6E };
593 error
= elants_i2c_send(client
, boot_cmd
, sizeof(boot_cmd
));
595 dev_err(&client
->dev
, "boot failed: %d\n", error
);
599 dev_dbg(&client
->dev
, "boot success -- 0x%x\n", client
->addr
);
603 static int elants_i2c_initialize(struct elants_data
*ts
)
605 struct i2c_client
*client
= ts
->client
;
606 int error
, error2
, retry_cnt
;
607 const u8 hello_packet
[] = { 0x55, 0x55, 0x55, 0x55 };
608 const u8 recov_packet
[] = { 0x55, 0x55, 0x80, 0x80 };
611 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
612 error
= elants_i2c_sw_reset(client
);
614 /* Continue initializing if it's the last try */
615 if (retry_cnt
< MAX_RETRIES
- 1)
619 error
= elants_i2c_fastboot(client
);
621 /* Continue initializing if it's the last try */
622 if (retry_cnt
< MAX_RETRIES
- 1)
626 /* Wait for Hello packet */
627 msleep(BOOT_TIME_DELAY_MS
);
629 error
= elants_i2c_read(client
, buf
, sizeof(buf
));
631 dev_err(&client
->dev
,
632 "failed to read 'hello' packet: %d\n", error
);
633 } else if (!memcmp(buf
, hello_packet
, sizeof(hello_packet
))) {
634 ts
->iap_mode
= ELAN_IAP_OPERATIONAL
;
636 } else if (!memcmp(buf
, recov_packet
, sizeof(recov_packet
))) {
638 * Setting error code will mark device
639 * in recovery mode below.
645 dev_err(&client
->dev
,
646 "invalid 'hello' packet: %*ph\n",
647 (int)sizeof(buf
), buf
);
651 /* hw version is available even if device in recovery state */
652 error2
= elants_i2c_query_hw_version(ts
);
654 error2
= elants_i2c_query_bc_version(ts
);
659 error
= elants_i2c_query_fw_version(ts
);
661 error
= elants_i2c_query_test_version(ts
);
663 switch (ts
->chip_id
) {
666 error
= elants_i2c_query_ts_info_ekth(ts
);
670 error
= elants_i2c_query_ts_info_ektf(ts
);
677 ts
->iap_mode
= ELAN_IAP_RECOVERY
;
683 * Firmware update interface.
686 static int elants_i2c_fw_write_page(struct i2c_client
*client
,
689 const u8 ack_ok
[] = { 0xaa, 0xaa };
694 for (retry
= 0; retry
< MAX_FW_UPDATE_RETRIES
; retry
++) {
695 error
= elants_i2c_send(client
, page
, ELAN_FW_PAGESIZE
);
697 dev_err(&client
->dev
,
698 "IAP Write Page failed: %d\n", error
);
702 error
= elants_i2c_read(client
, buf
, 2);
704 dev_err(&client
->dev
,
705 "IAP Ack read failed: %d\n", error
);
709 if (!memcmp(buf
, ack_ok
, sizeof(ack_ok
)))
713 dev_err(&client
->dev
,
714 "IAP Get Ack Error [%02x:%02x]\n",
721 static int elants_i2c_validate_remark_id(struct elants_data
*ts
,
722 const struct firmware
*fw
)
724 struct i2c_client
*client
= ts
->client
;
726 const u8 cmd
[] = { CMD_HEADER_ROM_READ
, 0x80, 0x1F, 0x00, 0x00, 0x21 };
728 u16 ts_remark_id
= 0;
729 u16 fw_remark_id
= 0;
731 /* Compare TS Remark ID and FW Remark ID */
732 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
734 1, "read Remark ID");
738 ts_remark_id
= get_unaligned_be16(&resp
[3]);
740 fw_remark_id
= get_unaligned_le16(&fw
->data
[fw
->size
- 4]);
742 if (fw_remark_id
!= ts_remark_id
) {
743 dev_err(&client
->dev
,
744 "Remark ID Mismatched: ts_remark_id=0x%04x, fw_remark_id=0x%04x.\n",
745 ts_remark_id
, fw_remark_id
);
752 static bool elants_i2c_should_check_remark_id(struct elants_data
*ts
)
754 struct i2c_client
*client
= ts
->client
;
755 const u8 bootcode_version
= ts
->iap_version
;
758 /* I2C eKTH3900 and eKTH5312 are NOT support Remark ID */
759 if ((bootcode_version
== BC_VER_H_BYTE_FOR_EKTH3900x1_I2C
) ||
760 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH3900x2_I2C
) ||
761 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH3900x3_I2C
) ||
762 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312x1_I2C
) ||
763 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312x2_I2C
) ||
764 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312cx1_I2C
) ||
765 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312cx2_I2C
) ||
766 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312x1_I2C_USB
) ||
767 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312x2_I2C_USB
) ||
768 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312cx1_I2C_USB
) ||
769 (bootcode_version
== BC_VER_H_BYTE_FOR_EKTH5312cx2_I2C_USB
)) {
770 dev_dbg(&client
->dev
,
771 "eKTH3900/eKTH5312(0x%02x) are not support remark id\n",
774 } else if (bootcode_version
>= 0x60) {
783 static int elants_i2c_do_update_firmware(struct i2c_client
*client
,
784 const struct firmware
*fw
,
787 struct elants_data
*ts
= i2c_get_clientdata(client
);
788 const u8 enter_iap
[] = { 0x45, 0x49, 0x41, 0x50 };
789 const u8 enter_iap2
[] = { 0x54, 0x00, 0x12, 0x34 };
790 const u8 iap_ack
[] = { 0x55, 0xaa, 0x33, 0xcc };
791 const u8 close_idle
[] = { 0x54, 0x2c, 0x01, 0x01 };
794 int page
, n_fw_pages
;
796 bool check_remark_id
= elants_i2c_should_check_remark_id(ts
);
798 /* Recovery mode detection! */
800 dev_dbg(&client
->dev
, "Recovery mode procedure\n");
802 if (check_remark_id
) {
803 error
= elants_i2c_validate_remark_id(ts
, fw
);
808 error
= elants_i2c_send(client
, enter_iap2
, sizeof(enter_iap2
));
810 dev_err(&client
->dev
, "failed to enter IAP mode: %d\n",
815 /* Start IAP Procedure */
816 dev_dbg(&client
->dev
, "Normal IAP procedure\n");
818 /* Close idle mode */
819 error
= elants_i2c_send(client
, close_idle
, sizeof(close_idle
));
821 dev_err(&client
->dev
, "Failed close idle: %d\n", error
);
824 elants_i2c_sw_reset(client
);
827 if (check_remark_id
) {
828 error
= elants_i2c_validate_remark_id(ts
, fw
);
833 error
= elants_i2c_send(client
, enter_iap
, sizeof(enter_iap
));
835 dev_err(&client
->dev
, "failed to enter IAP mode: %d\n",
843 /* check IAP state */
844 error
= elants_i2c_read(client
, buf
, 4);
846 dev_err(&client
->dev
,
847 "failed to read IAP acknowledgement: %d\n",
852 if (memcmp(buf
, iap_ack
, sizeof(iap_ack
))) {
853 dev_err(&client
->dev
,
854 "failed to enter IAP: %*ph (expected %*ph)\n",
855 (int)sizeof(buf
), buf
, (int)sizeof(iap_ack
), iap_ack
);
859 dev_info(&client
->dev
, "successfully entered IAP mode");
861 send_id
= client
->addr
;
862 error
= elants_i2c_send(client
, &send_id
, 1);
864 dev_err(&client
->dev
, "sending dummy byte failed: %d\n",
869 /* Clear the last page of Master */
870 error
= elants_i2c_send(client
, fw
->data
, ELAN_FW_PAGESIZE
);
872 dev_err(&client
->dev
, "clearing of the last page failed: %d\n",
877 error
= elants_i2c_read(client
, buf
, 2);
879 dev_err(&client
->dev
,
880 "failed to read ACK for clearing the last page: %d\n",
885 n_fw_pages
= fw
->size
/ ELAN_FW_PAGESIZE
;
886 dev_dbg(&client
->dev
, "IAP Pages = %d\n", n_fw_pages
);
888 for (page
= 0; page
< n_fw_pages
; page
++) {
889 error
= elants_i2c_fw_write_page(client
,
890 fw
->data
+ page
* ELAN_FW_PAGESIZE
);
892 dev_err(&client
->dev
,
893 "failed to write FW page %d: %d\n",
899 /* Old iap needs to wait 200ms for WDT and rest is for hello packets */
902 dev_info(&client
->dev
, "firmware update completed\n");
906 static int elants_i2c_fw_update(struct elants_data
*ts
)
908 struct i2c_client
*client
= ts
->client
;
909 const struct firmware
*fw
;
913 fw_name
= kasprintf(GFP_KERNEL
, "elants_i2c_%04x.bin", ts
->hw_version
);
917 dev_info(&client
->dev
, "requesting fw name = %s\n", fw_name
);
918 error
= request_firmware(&fw
, fw_name
, &client
->dev
);
921 dev_err(&client
->dev
, "failed to request firmware: %d\n",
926 if (fw
->size
% ELAN_FW_PAGESIZE
) {
927 dev_err(&client
->dev
, "invalid firmware length: %zu\n",
933 disable_irq(client
->irq
);
935 error
= elants_i2c_do_update_firmware(client
, fw
,
936 ts
->iap_mode
== ELAN_IAP_RECOVERY
);
938 dev_err(&client
->dev
, "firmware update failed: %d\n", error
);
939 ts
->iap_mode
= ELAN_IAP_RECOVERY
;
943 error
= elants_i2c_initialize(ts
);
945 dev_err(&client
->dev
,
946 "failed to initialize device after firmware update: %d\n",
948 ts
->iap_mode
= ELAN_IAP_RECOVERY
;
952 ts
->iap_mode
= ELAN_IAP_OPERATIONAL
;
955 ts
->state
= ELAN_STATE_NORMAL
;
956 enable_irq(client
->irq
);
960 elants_i2c_calibrate(ts
);
962 release_firmware(fw
);
970 static void elants_i2c_mt_event(struct elants_data
*ts
, u8
*buf
,
973 struct input_dev
*input
= ts
->input
;
974 unsigned int n_fingers
;
975 unsigned int tool_type
;
979 n_fingers
= buf
[FW_POS_STATE
+ 1] & 0x0f;
980 finger_state
= ((buf
[FW_POS_STATE
+ 1] & 0x30) << 4) |
983 dev_dbg(&ts
->client
->dev
,
984 "n_fingers: %u, state: %04x\n", n_fingers
, finger_state
);
986 /* Note: all fingers have the same tool type */
987 tool_type
= buf
[FW_POS_TOOL_TYPE
] & BIT(0) ?
988 MT_TOOL_FINGER
: MT_TOOL_PALM
;
990 for (i
= 0; i
< MAX_CONTACT_NUM
&& n_fingers
; i
++) {
991 if (finger_state
& 1) {
992 unsigned int x
, y
, p
, w
;
995 pos
= &buf
[FW_POS_XY
+ i
* 3];
996 x
= (((u16
)pos
[0] & 0xf0) << 4) | pos
[1];
997 y
= (((u16
)pos
[0] & 0x0f) << 8) | pos
[2];
1000 * eKTF3624 may have use "old" touch-report format,
1001 * depending on a device and TS firmware version.
1002 * For example, ASUS Transformer devices use the "old"
1003 * format, while ASUS Nexus 7 uses the "new" formant.
1005 if (packet_size
== PACKET_SIZE_OLD
&&
1006 ts
->chip_id
== EKTF3624
) {
1007 w
= buf
[FW_POS_WIDTH
+ i
/ 2];
1013 p
= buf
[FW_POS_PRESSURE
+ i
];
1014 w
= buf
[FW_POS_WIDTH
+ i
];
1017 dev_dbg(&ts
->client
->dev
, "i=%d x=%d y=%d p=%d w=%d\n",
1020 input_mt_slot(input
, i
);
1021 input_mt_report_slot_state(input
, tool_type
, true);
1022 touchscreen_report_pos(input
, &ts
->prop
, x
, y
, true);
1023 input_event(input
, EV_ABS
, ABS_MT_PRESSURE
, p
);
1024 input_event(input
, EV_ABS
, ABS_MT_TOUCH_MAJOR
, w
);
1032 input_mt_sync_frame(input
);
1036 static u8
elants_i2c_calculate_checksum(u8
*buf
)
1041 for (i
= 0; i
< FW_POS_CHECKSUM
; i
++)
1047 static void elants_i2c_event(struct elants_data
*ts
, u8
*buf
,
1050 u8 checksum
= elants_i2c_calculate_checksum(buf
);
1052 if (unlikely(buf
[FW_POS_CHECKSUM
] != checksum
))
1053 dev_warn(&ts
->client
->dev
,
1054 "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
1055 __func__
, buf
[FW_POS_HEADER
],
1056 checksum
, buf
[FW_POS_CHECKSUM
]);
1057 else if (unlikely(buf
[FW_POS_HEADER
] != HEADER_REPORT_10_FINGER
))
1058 dev_warn(&ts
->client
->dev
,
1059 "%s: unknown packet type: %02x\n",
1060 __func__
, buf
[FW_POS_HEADER
]);
1062 elants_i2c_mt_event(ts
, buf
, packet_size
);
1065 static irqreturn_t
elants_i2c_irq(int irq
, void *_dev
)
1067 const u8 wait_packet
[] = { 0x64, 0x64, 0x64, 0x64 };
1068 struct elants_data
*ts
= _dev
;
1069 struct i2c_client
*client
= ts
->client
;
1070 int report_count
, report_len
;
1074 len
= i2c_master_recv_dmasafe(client
, ts
->buf
, sizeof(ts
->buf
));
1076 dev_err(&client
->dev
, "%s: failed to read data: %d\n",
1081 dev_dbg(&client
->dev
, "%s: packet %*ph\n",
1082 __func__
, HEADER_SIZE
, ts
->buf
);
1084 switch (ts
->state
) {
1085 case ELAN_WAIT_RECALIBRATION
:
1086 if (ts
->buf
[FW_HDR_TYPE
] == CMD_HEADER_REK
) {
1087 memcpy(ts
->cmd_resp
, ts
->buf
, sizeof(ts
->cmd_resp
));
1088 complete(&ts
->cmd_done
);
1089 ts
->state
= ELAN_STATE_NORMAL
;
1093 case ELAN_WAIT_QUEUE_HEADER
:
1094 if (ts
->buf
[FW_HDR_TYPE
] != QUEUE_HEADER_NORMAL
)
1097 ts
->state
= ELAN_STATE_NORMAL
;
1100 case ELAN_STATE_NORMAL
:
1102 switch (ts
->buf
[FW_HDR_TYPE
]) {
1103 case CMD_HEADER_HELLO
:
1104 case CMD_HEADER_RESP
:
1107 case QUEUE_HEADER_WAIT
:
1108 if (memcmp(ts
->buf
, wait_packet
, sizeof(wait_packet
))) {
1109 dev_err(&client
->dev
,
1110 "invalid wait packet %*ph\n",
1111 HEADER_SIZE
, ts
->buf
);
1113 ts
->state
= ELAN_WAIT_QUEUE_HEADER
;
1118 case QUEUE_HEADER_SINGLE
:
1119 elants_i2c_event(ts
, &ts
->buf
[HEADER_SIZE
],
1120 ts
->buf
[FW_HDR_LENGTH
]);
1123 case QUEUE_HEADER_NORMAL2
: /* CMD_HEADER_REK */
1125 * Depending on firmware version, eKTF3624 touchscreens
1126 * may utilize one of these opcodes for the touch events:
1127 * 0x63 (NORMAL) and 0x66 (NORMAL2). The 0x63 is used by
1128 * older firmware version and differs from 0x66 such that
1129 * touch pressure value needs to be adjusted. The 0x66
1130 * opcode of newer firmware is equal to 0x63 of eKTH3500.
1132 if (ts
->chip_id
!= EKTF3624
)
1137 case QUEUE_HEADER_NORMAL
:
1138 report_count
= ts
->buf
[FW_HDR_COUNT
];
1139 if (report_count
== 0 || report_count
> 3) {
1140 dev_err(&client
->dev
,
1141 "bad report count: %*ph\n",
1142 HEADER_SIZE
, ts
->buf
);
1146 report_len
= ts
->buf
[FW_HDR_LENGTH
] / report_count
;
1148 if (report_len
== PACKET_SIZE_OLD
&&
1149 ts
->chip_id
== EKTF3624
) {
1150 dev_dbg_once(&client
->dev
,
1151 "using old report format\n");
1152 } else if (report_len
!= PACKET_SIZE
) {
1153 dev_err(&client
->dev
,
1154 "mismatching report length: %*ph\n",
1155 HEADER_SIZE
, ts
->buf
);
1159 for (i
= 0; i
< report_count
; i
++) {
1160 u8
*buf
= ts
->buf
+ HEADER_SIZE
+
1162 elants_i2c_event(ts
, buf
, report_len
);
1167 dev_err(&client
->dev
, "unknown packet %*ph\n",
1168 HEADER_SIZE
, ts
->buf
);
1181 static ssize_t
calibrate_store(struct device
*dev
,
1182 struct device_attribute
*attr
,
1183 const char *buf
, size_t count
)
1185 struct i2c_client
*client
= to_i2c_client(dev
);
1186 struct elants_data
*ts
= i2c_get_clientdata(client
);
1189 error
= mutex_lock_interruptible(&ts
->sysfs_mutex
);
1193 error
= elants_i2c_calibrate(ts
);
1195 mutex_unlock(&ts
->sysfs_mutex
);
1196 return error
?: count
;
1199 static ssize_t
write_update_fw(struct device
*dev
,
1200 struct device_attribute
*attr
,
1201 const char *buf
, size_t count
)
1203 struct i2c_client
*client
= to_i2c_client(dev
);
1204 struct elants_data
*ts
= i2c_get_clientdata(client
);
1207 error
= mutex_lock_interruptible(&ts
->sysfs_mutex
);
1211 error
= elants_i2c_fw_update(ts
);
1212 dev_dbg(dev
, "firmware update result: %d\n", error
);
1214 mutex_unlock(&ts
->sysfs_mutex
);
1215 return error
?: count
;
1218 static ssize_t
show_iap_mode(struct device
*dev
,
1219 struct device_attribute
*attr
, char *buf
)
1221 struct i2c_client
*client
= to_i2c_client(dev
);
1222 struct elants_data
*ts
= i2c_get_clientdata(client
);
1224 return sprintf(buf
, "%s\n",
1225 ts
->iap_mode
== ELAN_IAP_OPERATIONAL
?
1226 "Normal" : "Recovery");
1229 static ssize_t
show_calibration_count(struct device
*dev
,
1230 struct device_attribute
*attr
, char *buf
)
1232 struct i2c_client
*client
= to_i2c_client(dev
);
1233 const u8 cmd
[] = { CMD_HEADER_READ
, E_ELAN_INFO_REK
, 0x00, 0x01 };
1234 u8 resp
[HEADER_SIZE
];
1238 error
= elants_i2c_execute_command(client
, cmd
, sizeof(cmd
),
1239 resp
, sizeof(resp
), 1,
1242 return sprintf(buf
, "%d\n", error
);
1244 rek_count
= get_unaligned_be16(&resp
[2]);
1245 return sprintf(buf
, "0x%04x\n", rek_count
);
1248 static DEVICE_ATTR_WO(calibrate
);
1249 static DEVICE_ATTR(iap_mode
, S_IRUGO
, show_iap_mode
, NULL
);
1250 static DEVICE_ATTR(calibration_count
, S_IRUGO
, show_calibration_count
, NULL
);
1251 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, write_update_fw
);
1253 struct elants_version_attribute
{
1254 struct device_attribute dattr
;
1255 size_t field_offset
;
1259 #define __ELANTS_FIELD_SIZE(_field) \
1260 sizeof(((struct elants_data *)NULL)->_field)
1261 #define __ELANTS_VERIFY_SIZE(_field) \
1262 (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \
1263 __ELANTS_FIELD_SIZE(_field))
1264 #define ELANTS_VERSION_ATTR(_field) \
1265 struct elants_version_attribute elants_ver_attr_##_field = { \
1266 .dattr = __ATTR(_field, S_IRUGO, \
1267 elants_version_attribute_show, NULL), \
1268 .field_offset = offsetof(struct elants_data, _field), \
1269 .field_size = __ELANTS_VERIFY_SIZE(_field), \
1272 static ssize_t
elants_version_attribute_show(struct device
*dev
,
1273 struct device_attribute
*dattr
,
1276 struct i2c_client
*client
= to_i2c_client(dev
);
1277 struct elants_data
*ts
= i2c_get_clientdata(client
);
1278 struct elants_version_attribute
*attr
=
1279 container_of(dattr
, struct elants_version_attribute
, dattr
);
1280 u8
*field
= (u8
*)((char *)ts
+ attr
->field_offset
);
1281 unsigned int fmt_size
;
1284 if (attr
->field_size
== 1) {
1286 fmt_size
= 2; /* 2 HEX digits */
1288 val
= *(u16
*)field
;
1289 fmt_size
= 4; /* 4 HEX digits */
1292 return sprintf(buf
, "%0*x\n", fmt_size
, val
);
1295 static ELANTS_VERSION_ATTR(fw_version
);
1296 static ELANTS_VERSION_ATTR(hw_version
);
1297 static ELANTS_VERSION_ATTR(test_version
);
1298 static ELANTS_VERSION_ATTR(solution_version
);
1299 static ELANTS_VERSION_ATTR(bc_version
);
1300 static ELANTS_VERSION_ATTR(iap_version
);
1302 static struct attribute
*elants_i2c_attrs
[] = {
1303 &dev_attr_calibrate
.attr
,
1304 &dev_attr_update_fw
.attr
,
1305 &dev_attr_iap_mode
.attr
,
1306 &dev_attr_calibration_count
.attr
,
1308 &elants_ver_attr_fw_version
.dattr
.attr
,
1309 &elants_ver_attr_hw_version
.dattr
.attr
,
1310 &elants_ver_attr_test_version
.dattr
.attr
,
1311 &elants_ver_attr_solution_version
.dattr
.attr
,
1312 &elants_ver_attr_bc_version
.dattr
.attr
,
1313 &elants_ver_attr_iap_version
.dattr
.attr
,
1316 ATTRIBUTE_GROUPS(elants_i2c
);
1318 static int elants_i2c_power_on(struct elants_data
*ts
)
1323 * If we do not have reset gpio assume platform firmware
1324 * controls regulators and does power them on for us.
1326 if (IS_ERR_OR_NULL(ts
->reset_gpio
))
1329 error
= regulator_enable(ts
->vcc33
);
1331 dev_err(&ts
->client
->dev
,
1332 "failed to enable vcc33 regulator: %d\n",
1337 error
= regulator_enable(ts
->vccio
);
1339 dev_err(&ts
->client
->dev
,
1340 "failed to enable vccio regulator: %d\n",
1342 regulator_disable(ts
->vcc33
);
1347 * We need to wait a bit after powering on controller before
1348 * we are allowed to release reset GPIO.
1350 usleep_range(ELAN_POWERON_DELAY_USEC
, ELAN_POWERON_DELAY_USEC
+ 100);
1352 gpiod_set_value_cansleep(ts
->reset_gpio
, 0);
1354 msleep(ELAN_RESET_DELAY_MSEC
);
1359 static void elants_i2c_power_off(void *_data
)
1361 struct elants_data
*ts
= _data
;
1363 if (!IS_ERR_OR_NULL(ts
->reset_gpio
)) {
1365 * Activate reset gpio to prevent leakage through the
1366 * pin once we shut off power to the controller.
1368 gpiod_set_value_cansleep(ts
->reset_gpio
, 1);
1369 regulator_disable(ts
->vccio
);
1370 regulator_disable(ts
->vcc33
);
1375 static const struct acpi_device_id i2c_hid_ids
[] = {
1381 static const guid_t i2c_hid_guid
=
1382 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
1383 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
1385 static bool elants_acpi_is_hid_device(struct device
*dev
)
1387 acpi_handle handle
= ACPI_HANDLE(dev
);
1388 union acpi_object
*obj
;
1390 if (acpi_match_device_ids(ACPI_COMPANION(dev
), i2c_hid_ids
))
1393 obj
= acpi_evaluate_dsm_typed(handle
, &i2c_hid_guid
, 1, 1, NULL
, ACPI_TYPE_INTEGER
);
1402 static bool elants_acpi_is_hid_device(struct device
*dev
)
1408 static int elants_i2c_probe(struct i2c_client
*client
)
1410 union i2c_smbus_data dummy
;
1411 struct elants_data
*ts
;
1412 unsigned long irqflags
;
1415 /* Don't bind to i2c-hid compatible devices, these are handled by the i2c-hid drv. */
1416 if (elants_acpi_is_hid_device(&client
->dev
)) {
1417 dev_warn(&client
->dev
, "This device appears to be an I2C-HID device, not binding\n");
1421 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1422 dev_err(&client
->dev
, "I2C check functionality error\n");
1426 ts
= devm_kzalloc(&client
->dev
, sizeof(struct elants_data
), GFP_KERNEL
);
1430 mutex_init(&ts
->sysfs_mutex
);
1431 init_completion(&ts
->cmd_done
);
1433 ts
->client
= client
;
1434 ts
->chip_id
= (enum elants_chip_id
)(uintptr_t)device_get_match_data(&client
->dev
);
1435 i2c_set_clientdata(client
, ts
);
1437 ts
->vcc33
= devm_regulator_get(&client
->dev
, "vcc33");
1438 if (IS_ERR(ts
->vcc33
))
1439 return dev_err_probe(&client
->dev
, PTR_ERR(ts
->vcc33
),
1440 "Failed to get 'vcc33' regulator\n");
1442 ts
->vccio
= devm_regulator_get(&client
->dev
, "vccio");
1443 if (IS_ERR(ts
->vccio
))
1444 return dev_err_probe(&client
->dev
, PTR_ERR(ts
->vccio
),
1445 "Failed to get 'vccio' regulator\n");
1447 ts
->reset_gpio
= devm_gpiod_get(&client
->dev
, "reset", GPIOD_OUT_HIGH
);
1448 if (IS_ERR(ts
->reset_gpio
)) {
1449 error
= PTR_ERR(ts
->reset_gpio
);
1451 if (error
== -EPROBE_DEFER
)
1454 if (error
!= -ENOENT
&& error
!= -ENOSYS
) {
1455 dev_err(&client
->dev
,
1456 "failed to get reset gpio: %d\n",
1461 ts
->keep_power_in_suspend
= true;
1464 error
= elants_i2c_power_on(ts
);
1468 error
= devm_add_action_or_reset(&client
->dev
,
1469 elants_i2c_power_off
, ts
);
1471 dev_err(&client
->dev
,
1472 "failed to install power off action: %d\n", error
);
1476 /* Make sure there is something at this address */
1477 if (i2c_smbus_xfer(client
->adapter
, client
->addr
, 0,
1478 I2C_SMBUS_READ
, 0, I2C_SMBUS_BYTE
, &dummy
) < 0) {
1479 dev_err(&client
->dev
, "nothing at this address\n");
1483 error
= elants_i2c_initialize(ts
);
1485 dev_err(&client
->dev
, "failed to initialize: %d\n", error
);
1489 ts
->input
= devm_input_allocate_device(&client
->dev
);
1491 dev_err(&client
->dev
, "Failed to allocate input device\n");
1495 ts
->input
->name
= "Elan Touchscreen";
1496 ts
->input
->id
.bustype
= BUS_I2C
;
1498 /* Multitouch input params setup */
1500 input_set_abs_params(ts
->input
, ABS_MT_POSITION_X
, 0, ts
->x_max
, 0, 0);
1501 input_set_abs_params(ts
->input
, ABS_MT_POSITION_Y
, 0, ts
->y_max
, 0, 0);
1502 input_set_abs_params(ts
->input
, ABS_MT_TOUCH_MAJOR
, 0, 255, 0, 0);
1503 input_set_abs_params(ts
->input
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
1504 input_set_abs_params(ts
->input
, ABS_MT_TOOL_TYPE
,
1505 0, MT_TOOL_PALM
, 0, 0);
1507 touchscreen_parse_properties(ts
->input
, true, &ts
->prop
);
1509 if (ts
->chip_id
== EKTF3624
&& ts
->phy_x
&& ts
->phy_y
) {
1510 /* calculate resolution from size */
1511 ts
->x_res
= DIV_ROUND_CLOSEST(ts
->prop
.max_x
, ts
->phy_x
);
1512 ts
->y_res
= DIV_ROUND_CLOSEST(ts
->prop
.max_y
, ts
->phy_y
);
1515 input_abs_set_res(ts
->input
, ABS_MT_POSITION_X
, ts
->x_res
);
1516 input_abs_set_res(ts
->input
, ABS_MT_POSITION_Y
, ts
->y_res
);
1517 input_abs_set_res(ts
->input
, ABS_MT_TOUCH_MAJOR
, ts
->major_res
);
1519 error
= input_mt_init_slots(ts
->input
, MAX_CONTACT_NUM
,
1520 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
1522 dev_err(&client
->dev
,
1523 "failed to initialize MT slots: %d\n", error
);
1527 error
= input_register_device(ts
->input
);
1529 dev_err(&client
->dev
,
1530 "unable to register input device: %d\n", error
);
1535 * Platform code (ACPI, DTS) should normally set up interrupt
1536 * for us, but in case it did not let's fall back to using falling
1537 * edge to be compatible with older Chromebooks.
1539 irqflags
= irq_get_trigger_type(client
->irq
);
1541 irqflags
= IRQF_TRIGGER_FALLING
;
1543 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1544 NULL
, elants_i2c_irq
,
1545 irqflags
| IRQF_ONESHOT
,
1548 dev_err(&client
->dev
, "Failed to register interrupt\n");
1555 static int elants_i2c_suspend(struct device
*dev
)
1557 struct i2c_client
*client
= to_i2c_client(dev
);
1558 struct elants_data
*ts
= i2c_get_clientdata(client
);
1559 const u8 set_sleep_cmd
[] = {
1560 CMD_HEADER_WRITE
, E_POWER_STATE_SLEEP
, 0x00, 0x01
1565 /* Command not support in IAP recovery mode */
1566 if (ts
->iap_mode
!= ELAN_IAP_OPERATIONAL
)
1569 disable_irq(client
->irq
);
1571 if (device_may_wakeup(dev
)) {
1573 * The device will automatically enter idle mode
1574 * that has reduced power consumption.
1577 } else if (ts
->keep_power_in_suspend
) {
1578 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
1579 error
= elants_i2c_send(client
, set_sleep_cmd
,
1580 sizeof(set_sleep_cmd
));
1584 dev_err(&client
->dev
,
1585 "suspend command failed: %d\n", error
);
1588 elants_i2c_power_off(ts
);
1594 static int elants_i2c_resume(struct device
*dev
)
1596 struct i2c_client
*client
= to_i2c_client(dev
);
1597 struct elants_data
*ts
= i2c_get_clientdata(client
);
1598 const u8 set_active_cmd
[] = {
1599 CMD_HEADER_WRITE
, E_POWER_STATE_RESUME
, 0x00, 0x01
1604 if (device_may_wakeup(dev
)) {
1605 elants_i2c_sw_reset(client
);
1606 } else if (ts
->keep_power_in_suspend
) {
1607 for (retry_cnt
= 0; retry_cnt
< MAX_RETRIES
; retry_cnt
++) {
1608 error
= elants_i2c_send(client
, set_active_cmd
,
1609 sizeof(set_active_cmd
));
1613 dev_err(&client
->dev
,
1614 "resume command failed: %d\n", error
);
1617 elants_i2c_power_on(ts
);
1618 elants_i2c_initialize(ts
);
1621 ts
->state
= ELAN_STATE_NORMAL
;
1622 enable_irq(client
->irq
);
1627 static DEFINE_SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops
,
1628 elants_i2c_suspend
, elants_i2c_resume
);
1630 static const struct i2c_device_id elants_i2c_id
[] = {
1631 { DEVICE_NAME
, EKTH3500
},
1632 { "ekth3500", EKTH3500
},
1633 { "ektf3624", EKTF3624
},
1636 MODULE_DEVICE_TABLE(i2c
, elants_i2c_id
);
1639 static const struct acpi_device_id elants_acpi_id
[] = {
1640 { "ELAN0001", EKTH3500
},
1643 MODULE_DEVICE_TABLE(acpi
, elants_acpi_id
);
1647 static const struct of_device_id elants_of_match
[] = {
1648 { .compatible
= "elan,ekth3500", .data
= (void *)EKTH3500
},
1649 { .compatible
= "elan,ektf3624", .data
= (void *)EKTF3624
},
1652 MODULE_DEVICE_TABLE(of
, elants_of_match
);
1655 static struct i2c_driver elants_i2c_driver
= {
1656 .probe
= elants_i2c_probe
,
1657 .id_table
= elants_i2c_id
,
1659 .name
= DEVICE_NAME
,
1660 .dev_groups
= elants_i2c_groups
,
1661 .pm
= pm_sleep_ptr(&elants_i2c_pm_ops
),
1662 .acpi_match_table
= ACPI_PTR(elants_acpi_id
),
1663 .of_match_table
= of_match_ptr(elants_of_match
),
1664 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1667 module_i2c_driver(elants_i2c_driver
);
1669 MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>");
1670 MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
1671 MODULE_LICENSE("GPL");