2 * Elan Microelectronics touch panels with I2C interface
4 * Copyright (C) 2014 Elan Microelectronics Corporation.
7 * This code is partly based on hid-multitouch.c:
11 * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
14 * This code is partly based on i2c-hid.c:
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
101 #define ELAN_FW_FILENAME "elants_i2c.bin"
103 /* calibration timeout definition */
104 #define ELAN_CALI_TIMEOUT_MSEC 10000
108 ELAN_WAIT_QUEUE_HEADER,
109 ELAN_WAIT_RECALIBRATION,
112 enum elants_iap_mode {
113 ELAN_IAP_OPERATIONAL,
117 /* struct elants_data - represents state of Elan touchscreen device */
119 struct i2c_client *client;
120 struct input_dev *input;
128 unsigned int x_res; /* resolution in units/mm */
133 enum elants_state state;
134 enum elants_iap_mode iap_mode;
136 /* Guards against concurrent access to the device via sysfs */
137 struct mutex sysfs_mutex;
139 u8 cmd_resp[HEADER_SIZE];
140 struct completion cmd_done;
142 u8 buf[MAX_PACKET_SIZE];
144 bool wake_irq_enabled;
147 static int elants_i2c_send(struct i2c_client *client,
148 const void *data, size_t size)
152 ret = i2c_master_send(client, data, size);
159 dev_err(&client->dev, "%s failed (%*ph): %d\n",
160 __func__, (int)size, data, ret);
165 static int elants_i2c_read(struct i2c_client *client, void *data, size_t size)
169 ret = i2c_master_recv(client, data, size);
176 dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
181 static int elants_i2c_execute_command(struct i2c_client *client,
182 const u8 *cmd, size_t cmd_size,
183 u8 *resp, size_t resp_size)
185 struct i2c_msg msgs[2];
187 u8 expected_response;
190 case CMD_HEADER_READ:
191 expected_response = CMD_HEADER_RESP;
194 case CMD_HEADER_6B_READ:
195 expected_response = CMD_HEADER_6B_RESP;
199 dev_err(&client->dev, "%s: invalid command %*ph\n",
200 __func__, (int)cmd_size, cmd);
204 msgs[0].addr = client->addr;
205 msgs[0].flags = client->flags & I2C_M_TEN;
206 msgs[0].len = cmd_size;
207 msgs[0].buf = (u8 *)cmd;
209 msgs[1].addr = client->addr;
210 msgs[1].flags = client->flags & I2C_M_TEN;
211 msgs[1].flags |= I2C_M_RD;
212 msgs[1].len = resp_size;
215 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
219 if (ret != ARRAY_SIZE(msgs) || resp[FW_HDR_TYPE] != expected_response)
225 static int elants_i2c_calibrate(struct elants_data *ts)
227 struct i2c_client *client = ts->client;
229 static const u8 w_flashkey[] = { 0x54, 0xC0, 0xE1, 0x5A };
230 static const u8 rek[] = { 0x54, 0x29, 0x00, 0x01 };
231 static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 };
233 disable_irq(client->irq);
235 ts->state = ELAN_WAIT_RECALIBRATION;
236 reinit_completion(&ts->cmd_done);
238 elants_i2c_send(client, w_flashkey, sizeof(w_flashkey));
239 elants_i2c_send(client, rek, sizeof(rek));
241 enable_irq(client->irq);
243 ret = wait_for_completion_interruptible_timeout(&ts->cmd_done,
244 msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC));
246 ts->state = ELAN_STATE_NORMAL;
249 error = ret < 0 ? ret : -ETIMEDOUT;
250 dev_err(&client->dev,
251 "error while waiting for calibration to complete: %d\n",
256 if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) {
257 dev_err(&client->dev,
258 "unexpected calibration response: %*ph\n",
259 (int)sizeof(ts->cmd_resp), ts->cmd_resp);
266 static int elants_i2c_sw_reset(struct i2c_client *client)
268 const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 };
271 error = elants_i2c_send(client, soft_rst_cmd,
272 sizeof(soft_rst_cmd));
274 dev_err(&client->dev, "software reset failed: %d\n", error);
279 * We should wait at least 10 msec (but no more than 40) before
280 * sending fastboot or IAP command to the device.
287 static u16 elants_i2c_parse_version(u8 *buf)
289 return get_unaligned_be32(buf) >> 4;
292 static int elants_i2c_query_fw_id(struct elants_data *ts)
294 struct i2c_client *client = ts->client;
295 int error, retry_cnt;
296 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 };
297 u8 resp[HEADER_SIZE];
299 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
300 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
303 ts->hw_version = elants_i2c_parse_version(resp);
304 if (ts->hw_version != 0xffff)
308 dev_dbg(&client->dev, "read fw id error=%d, buf=%*phC\n",
309 error, (int)sizeof(resp), resp);
312 dev_err(&client->dev,
313 "Failed to read fw id or fw id is invalid\n");
318 static int elants_i2c_query_fw_version(struct elants_data *ts)
320 struct i2c_client *client = ts->client;
321 int error, retry_cnt;
322 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 };
323 u8 resp[HEADER_SIZE];
325 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
326 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
329 ts->fw_version = elants_i2c_parse_version(resp);
330 if (ts->fw_version != 0x0000 &&
331 ts->fw_version != 0xffff)
335 dev_dbg(&client->dev, "read fw version error=%d, buf=%*phC\n",
336 error, (int)sizeof(resp), resp);
339 dev_err(&client->dev,
340 "Failed to read fw version or fw version is invalid\n");
345 static int elants_i2c_query_test_version(struct elants_data *ts)
347 struct i2c_client *client = ts->client;
348 int error, retry_cnt;
350 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 };
351 u8 resp[HEADER_SIZE];
353 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
354 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
357 version = elants_i2c_parse_version(resp);
358 ts->test_version = version >> 8;
359 ts->solution_version = version & 0xff;
364 dev_dbg(&client->dev,
365 "read test version error rc=%d, buf=%*phC\n",
366 error, (int)sizeof(resp), resp);
369 dev_err(&client->dev, "Failed to read test version\n");
374 static int elants_i2c_query_bc_version(struct elants_data *ts)
376 struct i2c_client *client = ts->client;
377 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 };
378 u8 resp[HEADER_SIZE];
382 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
385 dev_err(&client->dev,
386 "read BC version error=%d, buf=%*phC\n",
387 error, (int)sizeof(resp), resp);
391 version = elants_i2c_parse_version(resp);
392 ts->bc_version = version >> 8;
393 ts->iap_version = version & 0xff;
398 static int elants_i2c_query_ts_info(struct elants_data *ts)
400 struct i2c_client *client = ts->client;
403 u16 phy_x, phy_y, rows, cols, osr;
404 const u8 get_resolution_cmd[] = {
405 CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00
407 const u8 get_osr_cmd[] = {
408 CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01
410 const u8 get_physical_scan_cmd[] = {
411 CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01
413 const u8 get_physical_drive_cmd[] = {
414 CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01
417 /* Get trace number */
418 error = elants_i2c_execute_command(client,
420 sizeof(get_resolution_cmd),
423 dev_err(&client->dev, "get resolution command failed: %d\n",
428 rows = resp[2] + resp[6] + resp[10];
429 cols = resp[3] + resp[7] + resp[11];
431 /* Process mm_to_pixel information */
432 error = elants_i2c_execute_command(client,
433 get_osr_cmd, sizeof(get_osr_cmd),
436 dev_err(&client->dev, "get osr command failed: %d\n",
443 error = elants_i2c_execute_command(client,
444 get_physical_scan_cmd,
445 sizeof(get_physical_scan_cmd),
448 dev_err(&client->dev, "get physical scan command failed: %d\n",
453 phy_x = get_unaligned_be16(&resp[2]);
455 error = elants_i2c_execute_command(client,
456 get_physical_drive_cmd,
457 sizeof(get_physical_drive_cmd),
460 dev_err(&client->dev, "get physical drive command failed: %d\n",
465 phy_y = get_unaligned_be16(&resp[2]);
467 dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
469 if (rows == 0 || cols == 0 || osr == 0) {
470 dev_warn(&client->dev,
471 "invalid trace number data: %d, %d, %d\n",
474 /* translate trace number to TS resolution */
475 ts->x_max = ELAN_TS_RESOLUTION(rows, osr);
476 ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x);
477 ts->y_max = ELAN_TS_RESOLUTION(cols, osr);
478 ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y);
484 static int elants_i2c_fastboot(struct i2c_client *client)
486 const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E };
489 error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd));
491 dev_err(&client->dev, "boot failed: %d\n", error);
495 dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr);
499 static int elants_i2c_initialize(struct elants_data *ts)
501 struct i2c_client *client = ts->client;
502 int error, retry_cnt;
503 const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 };
504 const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 };
507 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
508 error = elants_i2c_sw_reset(client);
510 /* Continue initializing if it's the last try */
511 if (retry_cnt < MAX_RETRIES - 1)
515 error = elants_i2c_fastboot(client);
517 /* Continue initializing if it's the last try */
518 if (retry_cnt < MAX_RETRIES - 1)
522 /* Wait for Hello packet */
523 msleep(BOOT_TIME_DELAY_MS);
525 error = elants_i2c_read(client, buf, sizeof(buf));
527 dev_err(&client->dev,
528 "failed to read 'hello' packet: %d\n", error);
529 } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) {
530 ts->iap_mode = ELAN_IAP_OPERATIONAL;
532 } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) {
534 * Setting error code will mark device
535 * in recovery mode below.
541 dev_err(&client->dev,
542 "invalid 'hello' packet: %*ph\n",
543 (int)sizeof(buf), buf);
548 error = elants_i2c_query_fw_id(ts);
550 error = elants_i2c_query_fw_version(ts);
553 ts->iap_mode = ELAN_IAP_RECOVERY;
555 elants_i2c_query_test_version(ts);
556 elants_i2c_query_bc_version(ts);
557 elants_i2c_query_ts_info(ts);
564 * Firmware update interface.
567 static int elants_i2c_fw_write_page(struct i2c_client *client,
570 const u8 ack_ok[] = { 0xaa, 0xaa };
575 for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) {
576 error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE);
578 dev_err(&client->dev,
579 "IAP Write Page failed: %d\n", error);
583 error = elants_i2c_read(client, buf, 2);
585 dev_err(&client->dev,
586 "IAP Ack read failed: %d\n", error);
590 if (!memcmp(buf, ack_ok, sizeof(ack_ok)))
594 dev_err(&client->dev,
595 "IAP Get Ack Error [%02x:%02x]\n",
602 static int elants_i2c_do_update_firmware(struct i2c_client *client,
603 const struct firmware *fw,
606 const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 };
607 const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 };
608 const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc };
611 int page, n_fw_pages;
614 /* Recovery mode detection! */
616 dev_dbg(&client->dev, "Recovery mode procedure\n");
617 error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2));
619 /* Start IAP Procedure */
620 dev_dbg(&client->dev, "Normal IAP procedure\n");
621 elants_i2c_sw_reset(client);
623 error = elants_i2c_send(client, enter_iap, sizeof(enter_iap));
627 dev_err(&client->dev, "failed to enter IAP mode: %d\n", error);
633 /* check IAP state */
634 error = elants_i2c_read(client, buf, 4);
636 dev_err(&client->dev,
637 "failed to read IAP acknowledgement: %d\n",
642 if (memcmp(buf, iap_ack, sizeof(iap_ack))) {
643 dev_err(&client->dev,
644 "failed to enter IAP: %*ph (expected %*ph)\n",
645 (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack);
649 dev_info(&client->dev, "successfully entered IAP mode");
651 send_id = client->addr;
652 error = elants_i2c_send(client, &send_id, 1);
654 dev_err(&client->dev, "sending dummy byte failed: %d\n",
659 /* Clear the last page of Master */
660 error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE);
662 dev_err(&client->dev, "clearing of the last page failed: %d\n",
667 error = elants_i2c_read(client, buf, 2);
669 dev_err(&client->dev,
670 "failed to read ACK for clearing the last page: %d\n",
675 n_fw_pages = fw->size / ELAN_FW_PAGESIZE;
676 dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages);
678 for (page = 0; page < n_fw_pages; page++) {
679 error = elants_i2c_fw_write_page(client,
680 fw->data + page * ELAN_FW_PAGESIZE);
682 dev_err(&client->dev,
683 "failed to write FW page %d: %d\n",
689 /* Old iap needs to wait 200ms for WDT and rest is for hello packets */
692 dev_info(&client->dev, "firmware update completed\n");
696 static int elants_i2c_fw_update(struct elants_data *ts)
698 struct i2c_client *client = ts->client;
699 const struct firmware *fw;
702 error = request_firmware(&fw, ELAN_FW_FILENAME, &client->dev);
704 dev_err(&client->dev, "failed to request firmware %s: %d\n",
705 ELAN_FW_FILENAME, error);
709 if (fw->size % ELAN_FW_PAGESIZE) {
710 dev_err(&client->dev, "invalid firmware length: %zu\n",
716 disable_irq(client->irq);
718 error = elants_i2c_do_update_firmware(client, fw,
719 ts->iap_mode == ELAN_IAP_RECOVERY);
721 dev_err(&client->dev, "firmware update failed: %d\n", error);
722 ts->iap_mode = ELAN_IAP_RECOVERY;
726 error = elants_i2c_initialize(ts);
728 dev_err(&client->dev,
729 "failed to initialize device after firmware update: %d\n",
731 ts->iap_mode = ELAN_IAP_RECOVERY;
735 ts->iap_mode = ELAN_IAP_OPERATIONAL;
738 ts->state = ELAN_STATE_NORMAL;
739 enable_irq(client->irq);
743 elants_i2c_calibrate(ts);
745 release_firmware(fw);
753 static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf)
755 struct input_dev *input = ts->input;
756 unsigned int n_fingers;
760 n_fingers = buf[FW_POS_STATE + 1] & 0x0f;
761 finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) |
764 dev_dbg(&ts->client->dev,
765 "n_fingers: %u, state: %04x\n", n_fingers, finger_state);
767 for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) {
768 if (finger_state & 1) {
769 unsigned int x, y, p, w;
772 pos = &buf[FW_POS_XY + i * 3];
773 x = (((u16)pos[0] & 0xf0) << 4) | pos[1];
774 y = (((u16)pos[0] & 0x0f) << 8) | pos[2];
775 p = buf[FW_POS_PRESSURE + i];
776 w = buf[FW_POS_WIDTH + i];
778 dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n",
781 input_mt_slot(input, i);
782 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
783 input_event(input, EV_ABS, ABS_MT_POSITION_X, x);
784 input_event(input, EV_ABS, ABS_MT_POSITION_Y, y);
785 input_event(input, EV_ABS, ABS_MT_PRESSURE, p);
786 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w);
794 input_mt_sync_frame(input);
798 static u8 elants_i2c_calculate_checksum(u8 *buf)
803 for (i = 0; i < FW_POS_CHECKSUM; i++)
809 static void elants_i2c_event(struct elants_data *ts, u8 *buf)
811 u8 checksum = elants_i2c_calculate_checksum(buf);
813 if (unlikely(buf[FW_POS_CHECKSUM] != checksum))
814 dev_warn(&ts->client->dev,
815 "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
816 __func__, buf[FW_POS_HEADER],
817 checksum, buf[FW_POS_CHECKSUM]);
818 else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER))
819 dev_warn(&ts->client->dev,
820 "%s: unknown packet type: %02x\n",
821 __func__, buf[FW_POS_HEADER]);
823 elants_i2c_mt_event(ts, buf);
826 static irqreturn_t elants_i2c_irq(int irq, void *_dev)
828 const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 };
829 struct elants_data *ts = _dev;
830 struct i2c_client *client = ts->client;
831 int report_count, report_len;
835 len = i2c_master_recv(client, ts->buf, sizeof(ts->buf));
837 dev_err(&client->dev, "%s: failed to read data: %d\n",
842 dev_dbg(&client->dev, "%s: packet %*ph\n",
843 __func__, HEADER_SIZE, ts->buf);
846 case ELAN_WAIT_RECALIBRATION:
847 if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) {
848 memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp));
849 complete(&ts->cmd_done);
850 ts->state = ELAN_STATE_NORMAL;
854 case ELAN_WAIT_QUEUE_HEADER:
855 if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL)
858 ts->state = ELAN_STATE_NORMAL;
861 case ELAN_STATE_NORMAL:
863 switch (ts->buf[FW_HDR_TYPE]) {
864 case CMD_HEADER_HELLO:
865 case CMD_HEADER_RESP:
869 case QUEUE_HEADER_WAIT:
870 if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) {
871 dev_err(&client->dev,
872 "invalid wait packet %*ph\n",
873 HEADER_SIZE, ts->buf);
875 ts->state = ELAN_WAIT_QUEUE_HEADER;
880 case QUEUE_HEADER_SINGLE:
881 elants_i2c_event(ts, &ts->buf[HEADER_SIZE]);
884 case QUEUE_HEADER_NORMAL:
885 report_count = ts->buf[FW_HDR_COUNT];
886 if (report_count > 3) {
887 dev_err(&client->dev,
888 "too large report count: %*ph\n",
889 HEADER_SIZE, ts->buf);
893 report_len = ts->buf[FW_HDR_LENGTH] / report_count;
894 if (report_len != PACKET_SIZE) {
895 dev_err(&client->dev,
896 "mismatching report length: %*ph\n",
897 HEADER_SIZE, ts->buf);
901 for (i = 0; i < report_count; i++) {
902 u8 *buf = ts->buf + HEADER_SIZE +
904 elants_i2c_event(ts, buf);
909 dev_err(&client->dev, "unknown packet %*ph\n",
910 HEADER_SIZE, ts->buf);
923 static ssize_t calibrate_store(struct device *dev,
924 struct device_attribute *attr,
925 const char *buf, size_t count)
927 struct i2c_client *client = to_i2c_client(dev);
928 struct elants_data *ts = i2c_get_clientdata(client);
931 error = mutex_lock_interruptible(&ts->sysfs_mutex);
935 error = elants_i2c_calibrate(ts);
937 mutex_unlock(&ts->sysfs_mutex);
938 return error ?: count;
941 static ssize_t write_update_fw(struct device *dev,
942 struct device_attribute *attr,
943 const char *buf, size_t count)
945 struct i2c_client *client = to_i2c_client(dev);
946 struct elants_data *ts = i2c_get_clientdata(client);
949 error = mutex_lock_interruptible(&ts->sysfs_mutex);
953 error = elants_i2c_fw_update(ts);
954 dev_dbg(dev, "firmware update result: %d\n", error);
956 mutex_unlock(&ts->sysfs_mutex);
957 return error ?: count;
960 static ssize_t show_iap_mode(struct device *dev,
961 struct device_attribute *attr, char *buf)
963 struct i2c_client *client = to_i2c_client(dev);
964 struct elants_data *ts = i2c_get_clientdata(client);
966 return sprintf(buf, "%s\n",
967 ts->iap_mode == ELAN_IAP_OPERATIONAL ?
968 "Normal" : "Recovery");
971 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, calibrate_store);
972 static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL);
973 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw);
975 struct elants_version_attribute {
976 struct device_attribute dattr;
981 #define __ELANTS_FIELD_SIZE(_field) \
982 sizeof(((struct elants_data *)NULL)->_field)
983 #define __ELANTS_VERIFY_SIZE(_field) \
984 (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \
985 __ELANTS_FIELD_SIZE(_field))
986 #define ELANTS_VERSION_ATTR(_field) \
987 struct elants_version_attribute elants_ver_attr_##_field = { \
988 .dattr = __ATTR(_field, S_IRUGO, \
989 elants_version_attribute_show, NULL), \
990 .field_offset = offsetof(struct elants_data, _field), \
991 .field_size = __ELANTS_VERIFY_SIZE(_field), \
994 static ssize_t elants_version_attribute_show(struct device *dev,
995 struct device_attribute *dattr,
998 struct i2c_client *client = to_i2c_client(dev);
999 struct elants_data *ts = i2c_get_clientdata(client);
1000 struct elants_version_attribute *attr =
1001 container_of(dattr, struct elants_version_attribute, dattr);
1002 u8 *field = (u8 *)((char *)ts + attr->field_offset);
1003 unsigned int fmt_size;
1006 if (attr->field_size == 1) {
1008 fmt_size = 2; /* 2 HEX digits */
1010 val = *(u16 *)field;
1011 fmt_size = 4; /* 4 HEX digits */
1014 return sprintf(buf, "%0*x\n", fmt_size, val);
1017 static ELANTS_VERSION_ATTR(fw_version);
1018 static ELANTS_VERSION_ATTR(hw_version);
1019 static ELANTS_VERSION_ATTR(test_version);
1020 static ELANTS_VERSION_ATTR(solution_version);
1021 static ELANTS_VERSION_ATTR(bc_version);
1022 static ELANTS_VERSION_ATTR(iap_version);
1024 static struct attribute *elants_attributes[] = {
1025 &dev_attr_calibrate.attr,
1026 &dev_attr_update_fw.attr,
1027 &dev_attr_iap_mode.attr,
1029 &elants_ver_attr_fw_version.dattr.attr,
1030 &elants_ver_attr_hw_version.dattr.attr,
1031 &elants_ver_attr_test_version.dattr.attr,
1032 &elants_ver_attr_solution_version.dattr.attr,
1033 &elants_ver_attr_bc_version.dattr.attr,
1034 &elants_ver_attr_iap_version.dattr.attr,
1038 static struct attribute_group elants_attribute_group = {
1039 .attrs = elants_attributes,
1042 static void elants_i2c_remove_sysfs_group(void *_data)
1044 struct elants_data *ts = _data;
1046 sysfs_remove_group(&ts->client->dev.kobj, &elants_attribute_group);
1049 static int elants_i2c_probe(struct i2c_client *client,
1050 const struct i2c_device_id *id)
1052 union i2c_smbus_data dummy;
1053 struct elants_data *ts;
1054 unsigned long irqflags;
1057 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1058 dev_err(&client->dev,
1059 "%s: i2c check functionality error\n", DEVICE_NAME);
1063 /* Make sure there is something at this address */
1064 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1065 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1066 dev_err(&client->dev, "nothing at this address\n");
1070 ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL);
1074 mutex_init(&ts->sysfs_mutex);
1075 init_completion(&ts->cmd_done);
1077 ts->client = client;
1078 i2c_set_clientdata(client, ts);
1080 error = elants_i2c_initialize(ts);
1082 dev_err(&client->dev, "failed to initialize: %d\n", error);
1086 ts->input = devm_input_allocate_device(&client->dev);
1088 dev_err(&client->dev, "Failed to allocate input device\n");
1092 ts->input->name = "Elan Touchscreen";
1093 ts->input->id.bustype = BUS_I2C;
1095 __set_bit(BTN_TOUCH, ts->input->keybit);
1096 __set_bit(EV_ABS, ts->input->evbit);
1097 __set_bit(EV_KEY, ts->input->evbit);
1099 /* Single touch input params setup */
1100 input_set_abs_params(ts->input, ABS_X, 0, ts->x_max, 0, 0);
1101 input_set_abs_params(ts->input, ABS_Y, 0, ts->y_max, 0, 0);
1102 input_set_abs_params(ts->input, ABS_PRESSURE, 0, 255, 0, 0);
1103 input_abs_set_res(ts->input, ABS_X, ts->x_res);
1104 input_abs_set_res(ts->input, ABS_Y, ts->y_res);
1106 /* Multitouch input params setup */
1107 error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM,
1108 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1110 dev_err(&client->dev,
1111 "failed to initialize MT slots: %d\n", error);
1115 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
1116 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
1117 input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1118 input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1119 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
1120 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
1122 input_set_drvdata(ts->input, ts);
1124 error = input_register_device(ts->input);
1126 dev_err(&client->dev,
1127 "unable to register input device: %d\n", error);
1132 * Systems using device tree should set up interrupt via DTS,
1133 * the rest will use the default falling edge interrupts.
1135 irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
1137 error = devm_request_threaded_irq(&client->dev, client->irq,
1138 NULL, elants_i2c_irq,
1139 irqflags | IRQF_ONESHOT,
1142 dev_err(&client->dev, "Failed to register interrupt\n");
1147 * Systems using device tree should set up wakeup via DTS,
1148 * the rest will configure device as wakeup source by default.
1150 if (!client->dev.of_node)
1151 device_init_wakeup(&client->dev, true);
1153 error = sysfs_create_group(&client->dev.kobj, &elants_attribute_group);
1155 dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1160 error = devm_add_action(&client->dev,
1161 elants_i2c_remove_sysfs_group, ts);
1163 elants_i2c_remove_sysfs_group(ts);
1164 dev_err(&client->dev,
1165 "Failed to add sysfs cleanup action: %d\n",
1173 static int __maybe_unused elants_i2c_suspend(struct device *dev)
1175 struct i2c_client *client = to_i2c_client(dev);
1176 struct elants_data *ts = i2c_get_clientdata(client);
1177 const u8 set_sleep_cmd[] = { 0x54, 0x50, 0x00, 0x01 };
1181 /* Command not support in IAP recovery mode */
1182 if (ts->iap_mode != ELAN_IAP_OPERATIONAL)
1185 disable_irq(client->irq);
1187 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1188 error = elants_i2c_send(client, set_sleep_cmd,
1189 sizeof(set_sleep_cmd));
1193 dev_err(&client->dev, "suspend command failed: %d\n", error);
1196 if (device_may_wakeup(dev))
1197 ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
1202 static int __maybe_unused elants_i2c_resume(struct device *dev)
1204 struct i2c_client *client = to_i2c_client(dev);
1205 struct elants_data *ts = i2c_get_clientdata(client);
1206 const u8 set_active_cmd[] = { 0x54, 0x58, 0x00, 0x01 };
1210 if (device_may_wakeup(dev) && ts->wake_irq_enabled)
1211 disable_irq_wake(client->irq);
1213 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1214 error = elants_i2c_send(client, set_active_cmd,
1215 sizeof(set_active_cmd));
1219 dev_err(&client->dev, "resume command failed: %d\n", error);
1222 ts->state = ELAN_STATE_NORMAL;
1223 enable_irq(client->irq);
1228 static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops,
1229 elants_i2c_suspend, elants_i2c_resume);
1231 static const struct i2c_device_id elants_i2c_id[] = {
1235 MODULE_DEVICE_TABLE(i2c, elants_i2c_id);
1238 static const struct acpi_device_id elants_acpi_id[] = {
1242 MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
1246 static const struct of_device_id elants_of_match[] = {
1247 { .compatible = "elan,ekth3500" },
1250 MODULE_DEVICE_TABLE(of, elants_of_match);
1253 static struct i2c_driver elants_i2c_driver = {
1254 .probe = elants_i2c_probe,
1255 .id_table = elants_i2c_id,
1257 .name = DEVICE_NAME,
1258 .owner = THIS_MODULE,
1259 .pm = &elants_i2c_pm_ops,
1260 .acpi_match_table = ACPI_PTR(elants_acpi_id),
1261 .of_match_table = of_match_ptr(elants_of_match),
1264 module_i2c_driver(elants_i2c_driver);
1267 MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
1268 MODULE_VERSION(DRV_VERSION);
1269 MODULE_LICENSE("GPL");