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/version.h>
37 #include <linux/slab.h>
38 #include <linux/firmware.h>
39 #include <linux/version.h>
40 #include <linux/input/mt.h>
41 #include <linux/acpi.h>
43 #include <asm/unaligned.h>
45 /* Device, Driver information */
46 #define DEVICE_NAME "elants_i2c"
47 #define DRV_VERSION "1.0.9"
49 /* Convert from rows or columns into resolution */
50 #define ELAN_TS_RESOLUTION(n, m) (((n) - 1) * (m))
55 #define FW_HDR_COUNT 1
56 #define FW_HDR_LENGTH 2
58 /* Buffer mode Queue Header information */
59 #define QUEUE_HEADER_SINGLE 0x62
60 #define QUEUE_HEADER_NORMAL 0X63
61 #define QUEUE_HEADER_WAIT 0x64
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_RESP 0x52
68 #define CMD_HEADER_6B_RESP 0x9B
69 #define CMD_HEADER_HELLO 0x55
70 #define CMD_HEADER_REK 0x66
72 /* FW position data */
73 #define PACKET_SIZE 55
74 #define MAX_CONTACT_NUM 10
75 #define FW_POS_HEADER 0
76 #define FW_POS_STATE 1
77 #define FW_POS_TOTAL 2
79 #define FW_POS_CHECKSUM 34
80 #define FW_POS_WIDTH 35
81 #define FW_POS_PRESSURE 45
83 #define HEADER_REPORT_10_FINGER 0x62
85 /* Header (4 bytes) plus 3 fill 10-finger packets */
86 #define MAX_PACKET_SIZE 169
88 #define BOOT_TIME_DELAY_MS 50
90 /* FW read command, 0x53 0x?? 0x0, 0x01 */
91 #define E_ELAN_INFO_FW_VER 0x00
92 #define E_ELAN_INFO_BC_VER 0x10
93 #define E_ELAN_INFO_TEST_VER 0xE0
94 #define E_ELAN_INFO_FW_ID 0xF0
95 #define E_INFO_OSR 0xD6
96 #define E_INFO_PHY_SCAN 0xD7
97 #define E_INFO_PHY_DRIVER 0xD8
100 #define MAX_FW_UPDATE_RETRIES 30
102 #define ELAN_FW_PAGESIZE 132
103 #define ELAN_FW_FILENAME "elants_i2c.bin"
105 /* calibration timeout definition */
106 #define ELAN_CALI_TIMEOUT_MSEC 10000
110 ELAN_WAIT_QUEUE_HEADER,
111 ELAN_WAIT_RECALIBRATION,
114 enum elants_iap_mode {
115 ELAN_IAP_OPERATIONAL,
119 /* struct elants_data - represents state of Elan touchscreen device */
121 struct i2c_client *client;
122 struct input_dev *input;
130 unsigned int x_res; /* resolution in units/mm */
135 enum elants_state state;
136 enum elants_iap_mode iap_mode;
138 /* Guards against concurrent access to the device via sysfs */
139 struct mutex sysfs_mutex;
141 u8 cmd_resp[HEADER_SIZE];
142 struct completion cmd_done;
144 u8 buf[MAX_PACKET_SIZE];
146 bool wake_irq_enabled;
149 static int elants_i2c_send(struct i2c_client *client,
150 const void *data, size_t size)
154 ret = i2c_master_send(client, data, size);
161 dev_err(&client->dev, "%s failed (%*ph): %d\n",
162 __func__, (int)size, data, ret);
167 static int elants_i2c_read(struct i2c_client *client, void *data, size_t size)
171 ret = i2c_master_recv(client, data, size);
178 dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
183 static int elants_i2c_execute_command(struct i2c_client *client,
184 const u8 *cmd, size_t cmd_size,
185 u8 *resp, size_t resp_size)
187 struct i2c_msg msgs[2];
189 u8 expected_response;
192 case CMD_HEADER_READ:
193 expected_response = CMD_HEADER_RESP;
196 case CMD_HEADER_6B_READ:
197 expected_response = CMD_HEADER_6B_RESP;
201 dev_err(&client->dev, "%s: invalid command %*ph\n",
202 __func__, (int)cmd_size, cmd);
206 msgs[0].addr = client->addr;
207 msgs[0].flags = client->flags & I2C_M_TEN;
208 msgs[0].len = cmd_size;
209 msgs[0].buf = (u8 *)cmd;
211 msgs[1].addr = client->addr;
212 msgs[1].flags = client->flags & I2C_M_TEN;
213 msgs[1].flags |= I2C_M_RD;
214 msgs[1].len = resp_size;
217 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
221 if (ret != ARRAY_SIZE(msgs) || resp[FW_HDR_TYPE] != expected_response)
227 static int elants_i2c_calibrate(struct elants_data *ts)
229 struct i2c_client *client = ts->client;
231 static const u8 w_flashkey[] = { 0x54, 0xC0, 0xE1, 0x5A };
232 static const u8 rek[] = { 0x54, 0x29, 0x00, 0x01 };
233 static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 };
235 disable_irq(client->irq);
237 ts->state = ELAN_WAIT_RECALIBRATION;
238 reinit_completion(&ts->cmd_done);
240 elants_i2c_send(client, w_flashkey, sizeof(w_flashkey));
241 elants_i2c_send(client, rek, sizeof(rek));
243 enable_irq(client->irq);
245 ret = wait_for_completion_interruptible_timeout(&ts->cmd_done,
246 msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC));
248 ts->state = ELAN_STATE_NORMAL;
251 error = ret < 0 ? ret : -ETIMEDOUT;
252 dev_err(&client->dev,
253 "error while waiting for calibration to complete: %d\n",
258 if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) {
259 dev_err(&client->dev,
260 "unexpected calibration response: %*ph\n",
261 (int)sizeof(ts->cmd_resp), ts->cmd_resp);
268 static int elants_i2c_sw_reset(struct i2c_client *client)
270 const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 };
273 error = elants_i2c_send(client, soft_rst_cmd,
274 sizeof(soft_rst_cmd));
276 dev_err(&client->dev, "software reset failed: %d\n", error);
281 * We should wait at least 10 msec (but no more than 40) before
282 * sending fastboot or IAP command to the device.
289 static u16 elants_i2c_parse_version(u8 *buf)
291 return get_unaligned_be32(buf) >> 4;
294 static int elants_i2c_query_fw_id(struct elants_data *ts)
296 struct i2c_client *client = ts->client;
297 int error, retry_cnt;
298 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 };
299 u8 resp[HEADER_SIZE];
301 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
302 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
305 ts->hw_version = elants_i2c_parse_version(resp);
306 if (ts->hw_version != 0xffff)
310 dev_dbg(&client->dev, "read fw id error=%d, buf=%*phC\n",
311 error, (int)sizeof(resp), resp);
314 dev_err(&client->dev,
315 "Failed to read fw id or fw id is invalid\n");
320 static int elants_i2c_query_fw_version(struct elants_data *ts)
322 struct i2c_client *client = ts->client;
323 int error, retry_cnt;
324 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 };
325 u8 resp[HEADER_SIZE];
327 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
328 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
331 ts->fw_version = elants_i2c_parse_version(resp);
332 if (ts->fw_version != 0x0000 &&
333 ts->fw_version != 0xffff)
337 dev_dbg(&client->dev, "read fw version error=%d, buf=%*phC\n",
338 error, (int)sizeof(resp), resp);
341 dev_err(&client->dev,
342 "Failed to read fw version or fw version is invalid\n");
347 static int elants_i2c_query_test_version(struct elants_data *ts)
349 struct i2c_client *client = ts->client;
350 int error, retry_cnt;
352 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 };
353 u8 resp[HEADER_SIZE];
355 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
356 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
359 version = elants_i2c_parse_version(resp);
360 ts->test_version = version >> 8;
361 ts->solution_version = version & 0xff;
366 dev_dbg(&client->dev,
367 "read test version error rc=%d, buf=%*phC\n",
368 error, (int)sizeof(resp), resp);
371 dev_err(&client->dev, "Failed to read test version\n");
376 static int elants_i2c_query_bc_version(struct elants_data *ts)
378 struct i2c_client *client = ts->client;
379 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 };
380 u8 resp[HEADER_SIZE];
384 error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
387 dev_err(&client->dev,
388 "read BC version error=%d, buf=%*phC\n",
389 error, (int)sizeof(resp), resp);
393 version = elants_i2c_parse_version(resp);
394 ts->bc_version = version >> 8;
395 ts->iap_version = version & 0xff;
400 static int elants_i2c_query_ts_info(struct elants_data *ts)
402 struct i2c_client *client = ts->client;
405 u16 phy_x, phy_y, rows, cols, osr;
406 const u8 get_resolution_cmd[] = {
407 CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00
409 const u8 get_osr_cmd[] = {
410 CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01
412 const u8 get_physical_scan_cmd[] = {
413 CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01
415 const u8 get_physical_drive_cmd[] = {
416 CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01
419 /* Get trace number */
420 error = elants_i2c_execute_command(client,
422 sizeof(get_resolution_cmd),
425 dev_err(&client->dev, "get resolution command failed: %d\n",
430 rows = resp[2] + resp[6] + resp[10];
431 cols = resp[3] + resp[7] + resp[11];
433 /* Process mm_to_pixel information */
434 error = elants_i2c_execute_command(client,
435 get_osr_cmd, sizeof(get_osr_cmd),
438 dev_err(&client->dev, "get osr command failed: %d\n",
445 error = elants_i2c_execute_command(client,
446 get_physical_scan_cmd,
447 sizeof(get_physical_scan_cmd),
450 dev_err(&client->dev, "get physical scan command failed: %d\n",
455 phy_x = get_unaligned_be16(&resp[2]);
457 error = elants_i2c_execute_command(client,
458 get_physical_drive_cmd,
459 sizeof(get_physical_drive_cmd),
462 dev_err(&client->dev, "get physical drive command failed: %d\n",
467 phy_y = get_unaligned_be16(&resp[2]);
469 dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
471 if (rows == 0 || cols == 0 || osr == 0) {
472 dev_warn(&client->dev,
473 "invalid trace number data: %d, %d, %d\n",
476 /* translate trace number to TS resolution */
477 ts->x_max = ELAN_TS_RESOLUTION(rows, osr);
478 ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x);
479 ts->y_max = ELAN_TS_RESOLUTION(cols, osr);
480 ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y);
486 static int elants_i2c_fastboot(struct i2c_client *client)
488 const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E };
491 error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd));
493 dev_err(&client->dev, "boot failed: %d\n", error);
497 dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr);
501 static int elants_i2c_initialize(struct elants_data *ts)
503 struct i2c_client *client = ts->client;
504 int error, retry_cnt;
505 const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 };
506 const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 };
509 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
510 error = elants_i2c_sw_reset(client);
512 /* Continue initializing if it's the last try */
513 if (retry_cnt < MAX_RETRIES - 1)
517 error = elants_i2c_fastboot(client);
519 /* Continue initializing if it's the last try */
520 if (retry_cnt < MAX_RETRIES - 1)
524 /* Wait for Hello packet */
525 msleep(BOOT_TIME_DELAY_MS);
527 error = elants_i2c_read(client, buf, sizeof(buf));
529 dev_err(&client->dev,
530 "failed to read 'hello' packet: %d\n", error);
531 } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) {
532 ts->iap_mode = ELAN_IAP_OPERATIONAL;
534 } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) {
536 * Setting error code will mark device
537 * in recovery mode below.
543 dev_err(&client->dev,
544 "invalid 'hello' packet: %*ph\n",
545 (int)sizeof(buf), buf);
550 error = elants_i2c_query_fw_id(ts);
552 error = elants_i2c_query_fw_version(ts);
555 ts->iap_mode = ELAN_IAP_RECOVERY;
557 elants_i2c_query_test_version(ts);
558 elants_i2c_query_bc_version(ts);
559 elants_i2c_query_ts_info(ts);
566 * Firmware update interface.
569 static int elants_i2c_fw_write_page(struct i2c_client *client,
572 const u8 ack_ok[] = { 0xaa, 0xaa };
577 for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) {
578 error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE);
580 dev_err(&client->dev,
581 "IAP Write Page failed: %d\n", error);
585 error = elants_i2c_read(client, buf, 2);
587 dev_err(&client->dev,
588 "IAP Ack read failed: %d\n", error);
592 if (!memcmp(buf, ack_ok, sizeof(ack_ok)))
596 dev_err(&client->dev,
597 "IAP Get Ack Error [%02x:%02x]\n",
604 static int elants_i2c_do_update_firmware(struct i2c_client *client,
605 const struct firmware *fw,
608 const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 };
609 const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 };
610 const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc };
613 int page, n_fw_pages;
616 /* Recovery mode detection! */
618 dev_dbg(&client->dev, "Recovery mode procedure\n");
619 error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2));
621 /* Start IAP Procedure */
622 dev_dbg(&client->dev, "Normal IAP procedure\n");
623 elants_i2c_sw_reset(client);
625 error = elants_i2c_send(client, enter_iap, sizeof(enter_iap));
629 dev_err(&client->dev, "failed to enter IAP mode: %d\n", error);
635 /* check IAP state */
636 error = elants_i2c_read(client, buf, 4);
638 dev_err(&client->dev,
639 "failed to read IAP acknowledgement: %d\n",
644 if (memcmp(buf, iap_ack, sizeof(iap_ack))) {
645 dev_err(&client->dev,
646 "failed to enter IAP: %*ph (expected %*ph)\n",
647 (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack);
651 dev_info(&client->dev, "successfully entered IAP mode");
653 send_id = client->addr;
654 error = elants_i2c_send(client, &send_id, 1);
656 dev_err(&client->dev, "sending dummy byte failed: %d\n",
661 /* Clear the last page of Master */
662 error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE);
664 dev_err(&client->dev, "clearing of the last page failed: %d\n",
669 error = elants_i2c_read(client, buf, 2);
671 dev_err(&client->dev,
672 "failed to read ACK for clearing the last page: %d\n",
677 n_fw_pages = fw->size / ELAN_FW_PAGESIZE;
678 dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages);
680 for (page = 0; page < n_fw_pages; page++) {
681 error = elants_i2c_fw_write_page(client,
682 fw->data + page * ELAN_FW_PAGESIZE);
684 dev_err(&client->dev,
685 "failed to write FW page %d: %d\n",
691 /* Old iap needs to wait 200ms for WDT and rest is for hello packets */
694 dev_info(&client->dev, "firmware update completed\n");
698 static int elants_i2c_fw_update(struct elants_data *ts)
700 struct i2c_client *client = ts->client;
701 const struct firmware *fw;
704 error = request_firmware(&fw, ELAN_FW_FILENAME, &client->dev);
706 dev_err(&client->dev, "failed to request firmware %s: %d\n",
707 ELAN_FW_FILENAME, error);
711 if (fw->size % ELAN_FW_PAGESIZE) {
712 dev_err(&client->dev, "invalid firmware length: %zu\n",
718 disable_irq(client->irq);
720 error = elants_i2c_do_update_firmware(client, fw,
721 ts->iap_mode == ELAN_IAP_RECOVERY);
723 dev_err(&client->dev, "firmware update failed: %d\n", error);
724 ts->iap_mode = ELAN_IAP_RECOVERY;
728 error = elants_i2c_initialize(ts);
730 dev_err(&client->dev,
731 "failed to initialize device after firmware update: %d\n",
733 ts->iap_mode = ELAN_IAP_RECOVERY;
737 ts->iap_mode = ELAN_IAP_OPERATIONAL;
740 ts->state = ELAN_STATE_NORMAL;
741 enable_irq(client->irq);
745 elants_i2c_calibrate(ts);
747 release_firmware(fw);
755 static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf)
757 struct input_dev *input = ts->input;
758 unsigned int n_fingers;
762 n_fingers = buf[FW_POS_STATE + 1] & 0x0f;
763 finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) |
766 dev_dbg(&ts->client->dev,
767 "n_fingers: %u, state: %04x\n", n_fingers, finger_state);
769 for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) {
770 if (finger_state & 1) {
771 unsigned int x, y, p, w;
774 pos = &buf[FW_POS_XY + i * 3];
775 x = (((u16)pos[0] & 0xf0) << 4) | pos[1];
776 y = (((u16)pos[0] & 0x0f) << 8) | pos[2];
777 p = buf[FW_POS_PRESSURE + i];
778 w = buf[FW_POS_WIDTH + i];
780 dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n",
783 input_mt_slot(input, i);
784 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
785 input_event(input, EV_ABS, ABS_MT_POSITION_X, x);
786 input_event(input, EV_ABS, ABS_MT_POSITION_Y, y);
787 input_event(input, EV_ABS, ABS_MT_PRESSURE, p);
788 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w);
796 input_mt_sync_frame(input);
800 static u8 elants_i2c_calculate_checksum(u8 *buf)
805 for (i = 0; i < FW_POS_CHECKSUM; i++)
811 static void elants_i2c_event(struct elants_data *ts, u8 *buf)
813 u8 checksum = elants_i2c_calculate_checksum(buf);
815 if (unlikely(buf[FW_POS_CHECKSUM] != checksum))
816 dev_warn(&ts->client->dev,
817 "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
818 __func__, buf[FW_POS_HEADER],
819 checksum, buf[FW_POS_CHECKSUM]);
820 else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER))
821 dev_warn(&ts->client->dev,
822 "%s: unknown packet type: %02x\n",
823 __func__, buf[FW_POS_HEADER]);
825 elants_i2c_mt_event(ts, buf);
828 static irqreturn_t elants_i2c_irq(int irq, void *_dev)
830 const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 };
831 struct elants_data *ts = _dev;
832 struct i2c_client *client = ts->client;
833 int report_count, report_len;
837 len = i2c_master_recv(client, ts->buf, sizeof(ts->buf));
839 dev_err(&client->dev, "%s: failed to read data: %d\n",
844 dev_dbg(&client->dev, "%s: packet %*ph\n",
845 __func__, HEADER_SIZE, ts->buf);
848 case ELAN_WAIT_RECALIBRATION:
849 if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) {
850 memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp));
851 complete(&ts->cmd_done);
852 ts->state = ELAN_STATE_NORMAL;
856 case ELAN_WAIT_QUEUE_HEADER:
857 if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL)
860 ts->state = ELAN_STATE_NORMAL;
863 case ELAN_STATE_NORMAL:
865 switch (ts->buf[FW_HDR_TYPE]) {
866 case CMD_HEADER_HELLO:
867 case CMD_HEADER_RESP:
871 case QUEUE_HEADER_WAIT:
872 if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) {
873 dev_err(&client->dev,
874 "invalid wait packet %*ph\n",
875 HEADER_SIZE, ts->buf);
877 ts->state = ELAN_WAIT_QUEUE_HEADER;
882 case QUEUE_HEADER_SINGLE:
883 elants_i2c_event(ts, &ts->buf[HEADER_SIZE]);
886 case QUEUE_HEADER_NORMAL:
887 report_count = ts->buf[FW_HDR_COUNT];
888 if (report_count > 3) {
889 dev_err(&client->dev,
890 "too large report count: %*ph\n",
891 HEADER_SIZE, ts->buf);
895 report_len = ts->buf[FW_HDR_LENGTH] / report_count;
896 if (report_len != PACKET_SIZE) {
897 dev_err(&client->dev,
898 "mismatching report length: %*ph\n",
899 HEADER_SIZE, ts->buf);
903 for (i = 0; i < report_count; i++) {
904 u8 *buf = ts->buf + HEADER_SIZE +
906 elants_i2c_event(ts, buf);
911 dev_err(&client->dev, "unknown packet %*ph\n",
912 HEADER_SIZE, ts->buf);
925 static ssize_t calibrate_store(struct device *dev,
926 struct device_attribute *attr,
927 const char *buf, size_t count)
929 struct i2c_client *client = to_i2c_client(dev);
930 struct elants_data *ts = i2c_get_clientdata(client);
933 error = mutex_lock_interruptible(&ts->sysfs_mutex);
937 error = elants_i2c_calibrate(ts);
939 mutex_unlock(&ts->sysfs_mutex);
940 return error ?: count;
943 static ssize_t write_update_fw(struct device *dev,
944 struct device_attribute *attr,
945 const char *buf, size_t count)
947 struct i2c_client *client = to_i2c_client(dev);
948 struct elants_data *ts = i2c_get_clientdata(client);
951 error = mutex_lock_interruptible(&ts->sysfs_mutex);
955 error = elants_i2c_fw_update(ts);
956 dev_dbg(dev, "firmware update result: %d\n", error);
958 mutex_unlock(&ts->sysfs_mutex);
959 return error ?: count;
962 static ssize_t show_iap_mode(struct device *dev,
963 struct device_attribute *attr, char *buf)
965 struct i2c_client *client = to_i2c_client(dev);
966 struct elants_data *ts = i2c_get_clientdata(client);
968 return sprintf(buf, "%s\n",
969 ts->iap_mode == ELAN_IAP_OPERATIONAL ?
970 "Normal" : "Recovery");
973 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, calibrate_store);
974 static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL);
975 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw);
977 struct elants_version_attribute {
978 struct device_attribute dattr;
983 #define __ELANTS_FIELD_SIZE(_field) \
984 sizeof(((struct elants_data *)NULL)->_field)
985 #define __ELANTS_VERIFY_SIZE(_field) \
986 (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \
987 __ELANTS_FIELD_SIZE(_field))
988 #define ELANTS_VERSION_ATTR(_field) \
989 struct elants_version_attribute elants_ver_attr_##_field = { \
990 .dattr = __ATTR(_field, S_IRUGO, \
991 elants_version_attribute_show, NULL), \
992 .field_offset = offsetof(struct elants_data, _field), \
993 .field_size = __ELANTS_VERIFY_SIZE(_field), \
996 static ssize_t elants_version_attribute_show(struct device *dev,
997 struct device_attribute *dattr,
1000 struct i2c_client *client = to_i2c_client(dev);
1001 struct elants_data *ts = i2c_get_clientdata(client);
1002 struct elants_version_attribute *attr =
1003 container_of(dattr, struct elants_version_attribute, dattr);
1004 u8 *field = (u8 *)((char *)ts + attr->field_offset);
1005 unsigned int fmt_size;
1008 if (attr->field_size == 1) {
1010 fmt_size = 2; /* 2 HEX digits */
1012 val = *(u16 *)field;
1013 fmt_size = 4; /* 4 HEX digits */
1016 return sprintf(buf, "%0*x\n", fmt_size, val);
1019 static ELANTS_VERSION_ATTR(fw_version);
1020 static ELANTS_VERSION_ATTR(hw_version);
1021 static ELANTS_VERSION_ATTR(test_version);
1022 static ELANTS_VERSION_ATTR(solution_version);
1023 static ELANTS_VERSION_ATTR(bc_version);
1024 static ELANTS_VERSION_ATTR(iap_version);
1026 static struct attribute *elants_attributes[] = {
1027 &dev_attr_calibrate.attr,
1028 &dev_attr_update_fw.attr,
1029 &dev_attr_iap_mode.attr,
1031 &elants_ver_attr_fw_version.dattr.attr,
1032 &elants_ver_attr_hw_version.dattr.attr,
1033 &elants_ver_attr_test_version.dattr.attr,
1034 &elants_ver_attr_solution_version.dattr.attr,
1035 &elants_ver_attr_bc_version.dattr.attr,
1036 &elants_ver_attr_iap_version.dattr.attr,
1040 static struct attribute_group elants_attribute_group = {
1041 .attrs = elants_attributes,
1044 static void elants_i2c_remove_sysfs_group(void *_data)
1046 struct elants_data *ts = _data;
1048 sysfs_remove_group(&ts->client->dev.kobj, &elants_attribute_group);
1051 static int elants_i2c_probe(struct i2c_client *client,
1052 const struct i2c_device_id *id)
1054 union i2c_smbus_data dummy;
1055 struct elants_data *ts;
1056 unsigned long irqflags;
1059 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1060 dev_err(&client->dev,
1061 "%s: i2c check functionality error\n", DEVICE_NAME);
1065 /* Make sure there is something at this address */
1066 if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1067 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1068 dev_err(&client->dev, "nothing at this address\n");
1072 ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL);
1076 mutex_init(&ts->sysfs_mutex);
1077 init_completion(&ts->cmd_done);
1079 ts->client = client;
1080 i2c_set_clientdata(client, ts);
1082 error = elants_i2c_initialize(ts);
1084 dev_err(&client->dev, "failed to initialize: %d\n", error);
1088 ts->input = devm_input_allocate_device(&client->dev);
1090 dev_err(&client->dev, "Failed to allocate input device\n");
1094 ts->input->name = "Elan Touchscreen";
1095 ts->input->id.bustype = BUS_I2C;
1097 __set_bit(BTN_TOUCH, ts->input->keybit);
1098 __set_bit(EV_ABS, ts->input->evbit);
1099 __set_bit(EV_KEY, ts->input->evbit);
1101 /* Single touch input params setup */
1102 input_set_abs_params(ts->input, ABS_X, 0, ts->x_max, 0, 0);
1103 input_set_abs_params(ts->input, ABS_Y, 0, ts->y_max, 0, 0);
1104 input_set_abs_params(ts->input, ABS_PRESSURE, 0, 255, 0, 0);
1105 input_abs_set_res(ts->input, ABS_X, ts->x_res);
1106 input_abs_set_res(ts->input, ABS_Y, ts->y_res);
1108 /* Multitouch input params setup */
1109 error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM,
1110 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1112 dev_err(&client->dev,
1113 "failed to initialize MT slots: %d\n", error);
1117 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
1118 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
1119 input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1120 input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1121 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
1122 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
1124 input_set_drvdata(ts->input, ts);
1126 error = input_register_device(ts->input);
1128 dev_err(&client->dev,
1129 "unable to register input device: %d\n", error);
1134 * Systems using device tree should set up interrupt via DTS,
1135 * the rest will use the default falling edge interrupts.
1137 irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
1139 error = devm_request_threaded_irq(&client->dev, client->irq,
1140 NULL, elants_i2c_irq,
1141 irqflags | IRQF_ONESHOT,
1144 dev_err(&client->dev, "Failed to register interrupt\n");
1149 * Systems using device tree should set up wakeup via DTS,
1150 * the rest will configure device as wakeup source by default.
1152 if (!client->dev.of_node)
1153 device_init_wakeup(&client->dev, true);
1155 error = sysfs_create_group(&client->dev.kobj, &elants_attribute_group);
1157 dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1162 error = devm_add_action(&client->dev,
1163 elants_i2c_remove_sysfs_group, ts);
1165 elants_i2c_remove_sysfs_group(ts);
1166 dev_err(&client->dev,
1167 "Failed to add sysfs cleanup action: %d\n",
1175 static int __maybe_unused elants_i2c_suspend(struct device *dev)
1177 struct i2c_client *client = to_i2c_client(dev);
1178 struct elants_data *ts = i2c_get_clientdata(client);
1179 const u8 set_sleep_cmd[] = { 0x54, 0x50, 0x00, 0x01 };
1183 /* Command not support in IAP recovery mode */
1184 if (ts->iap_mode != ELAN_IAP_OPERATIONAL)
1187 disable_irq(client->irq);
1189 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1190 error = elants_i2c_send(client, set_sleep_cmd,
1191 sizeof(set_sleep_cmd));
1195 dev_err(&client->dev, "suspend command failed: %d\n", error);
1198 if (device_may_wakeup(dev))
1199 ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
1204 static int __maybe_unused elants_i2c_resume(struct device *dev)
1206 struct i2c_client *client = to_i2c_client(dev);
1207 struct elants_data *ts = i2c_get_clientdata(client);
1208 const u8 set_active_cmd[] = { 0x54, 0x58, 0x00, 0x01 };
1212 if (device_may_wakeup(dev) && ts->wake_irq_enabled)
1213 disable_irq_wake(client->irq);
1215 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
1216 error = elants_i2c_send(client, set_active_cmd,
1217 sizeof(set_active_cmd));
1221 dev_err(&client->dev, "resume command failed: %d\n", error);
1224 ts->state = ELAN_STATE_NORMAL;
1225 enable_irq(client->irq);
1230 static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops,
1231 elants_i2c_suspend, elants_i2c_resume);
1233 static const struct i2c_device_id elants_i2c_id[] = {
1237 MODULE_DEVICE_TABLE(i2c, elants_i2c_id);
1240 static const struct acpi_device_id elants_acpi_id[] = {
1244 MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
1248 static const struct of_device_id elants_of_match[] = {
1249 { .compatible = "elan,ekth3500" },
1252 MODULE_DEVICE_TABLE(of, elants_of_match);
1255 static struct i2c_driver elants_i2c_driver = {
1256 .probe = elants_i2c_probe,
1257 .id_table = elants_i2c_id,
1259 .name = DEVICE_NAME,
1260 .owner = THIS_MODULE,
1261 .pm = &elants_i2c_pm_ops,
1262 .acpi_match_table = ACPI_PTR(elants_acpi_id),
1263 .of_match_table = of_match_ptr(elants_of_match),
1266 module_i2c_driver(elants_i2c_driver);
1268 MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>");
1269 MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
1270 MODULE_VERSION(DRV_VERSION);
1271 MODULE_LICENSE("GPL");