1 // SPDX-License-Identifier: GPL-2.0-only
3 * Elan I2C/SMBus Touchpad driver - I2C interface
5 * Copyright (c) 2013 ELAN Microelectronics Corp.
7 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
9 * Based on cyapa driver:
10 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
11 * copyright (c) 2011-2012 Google, Inc.
13 * Trademarks are the property of their respective owners.
16 #include <linux/completion.h>
17 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/interrupt.h>
20 #include <linux/jiffies.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <asm/unaligned.h>
28 /* Elan i2c commands */
29 #define ETP_I2C_RESET 0x0100
30 #define ETP_I2C_WAKE_UP 0x0800
31 #define ETP_I2C_SLEEP 0x0801
32 #define ETP_I2C_DESC_CMD 0x0001
33 #define ETP_I2C_REPORT_DESC_CMD 0x0002
34 #define ETP_I2C_STAND_CMD 0x0005
35 #define ETP_I2C_PATTERN_CMD 0x0100
36 #define ETP_I2C_UNIQUEID_CMD 0x0101
37 #define ETP_I2C_FW_VERSION_CMD 0x0102
38 #define ETP_I2C_IC_TYPE_CMD 0x0103
39 #define ETP_I2C_OSM_VERSION_CMD 0x0103
40 #define ETP_I2C_NSM_VERSION_CMD 0x0104
41 #define ETP_I2C_XY_TRACENUM_CMD 0x0105
42 #define ETP_I2C_MAX_X_AXIS_CMD 0x0106
43 #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107
44 #define ETP_I2C_RESOLUTION_CMD 0x0108
45 #define ETP_I2C_PRESSURE_CMD 0x010A
46 #define ETP_I2C_IAP_VERSION_CMD 0x0110
47 #define ETP_I2C_IC_TYPE_P0_CMD 0x0110
48 #define ETP_I2C_IAP_VERSION_P0_CMD 0x0111
49 #define ETP_I2C_SET_CMD 0x0300
50 #define ETP_I2C_POWER_CMD 0x0307
51 #define ETP_I2C_FW_CHECKSUM_CMD 0x030F
52 #define ETP_I2C_IAP_CTRL_CMD 0x0310
53 #define ETP_I2C_IAP_CMD 0x0311
54 #define ETP_I2C_IAP_RESET_CMD 0x0314
55 #define ETP_I2C_IAP_CHECKSUM_CMD 0x0315
56 #define ETP_I2C_CALIBRATE_CMD 0x0316
57 #define ETP_I2C_MAX_BASELINE_CMD 0x0317
58 #define ETP_I2C_MIN_BASELINE_CMD 0x0318
59 #define ETP_I2C_IAP_TYPE_REG 0x0040
60 #define ETP_I2C_IAP_TYPE_CMD 0x0304
62 #define ETP_I2C_REPORT_LEN 34
63 #define ETP_I2C_DESC_LENGTH 30
64 #define ETP_I2C_REPORT_DESC_LENGTH 158
65 #define ETP_I2C_INF_LENGTH 2
66 #define ETP_I2C_IAP_PASSWORD 0x1EA5
67 #define ETP_I2C_IAP_RESET 0xF0F0
68 #define ETP_I2C_MAIN_MODE_ON (1 << 9)
69 #define ETP_I2C_IAP_REG_L 0x01
70 #define ETP_I2C_IAP_REG_H 0x06
72 static int elan_i2c_read_block(struct i2c_client *client,
73 u16 reg, u8 *val, u16 len)
78 struct i2c_msg msgs[] = {
81 .flags = client->flags & I2C_M_TEN,
87 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
94 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
95 return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO);
98 static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val)
102 retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
104 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
111 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
117 struct i2c_msg msg = {
118 .addr = client->addr,
119 .flags = client->flags & I2C_M_TEN,
125 ret = i2c_transfer(client->adapter, &msg, 1);
129 dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n",
137 static int elan_i2c_initialize(struct i2c_client *client)
139 struct device *dev = &client->dev;
143 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
145 dev_err(dev, "device reset failed: %d\n", error);
149 /* Wait for the device to reset */
152 /* get reset acknowledgement 0000 */
153 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
155 dev_err(dev, "failed to read reset response: %d\n", error);
159 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
160 val, ETP_I2C_DESC_LENGTH);
162 dev_err(dev, "cannot get device descriptor: %d\n", error);
166 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
167 val, ETP_I2C_REPORT_DESC_LENGTH);
169 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
176 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
178 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
179 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
182 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
188 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
190 dev_err(&client->dev,
191 "failed to read current power state: %d\n",
196 reg = le16_to_cpup((__le16 *)val);
198 reg &= ~ETP_DISABLE_POWER;
200 reg |= ETP_DISABLE_POWER;
202 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
204 dev_err(&client->dev,
205 "failed to write current power state: %d\n",
213 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
215 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
219 static int elan_i2c_calibrate(struct i2c_client *client)
221 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
224 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
226 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
229 static int elan_i2c_get_baseline_data(struct i2c_client *client,
230 bool max_baseline, u8 *value)
235 error = elan_i2c_read_cmd(client,
236 max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
237 ETP_I2C_MIN_BASELINE_CMD,
242 *value = le16_to_cpup((__le16 *)val);
247 static int elan_i2c_get_pattern(struct i2c_client *client, u8 *pattern)
252 error = elan_i2c_read_cmd(client, ETP_I2C_PATTERN_CMD, val);
254 dev_err(&client->dev, "failed to get pattern: %d\n", error);
259 * Not all versions of firmware implement "get pattern" command.
260 * When this command is not implemented the device will respond
261 * with 0xFF 0xFF, which we will treat as "old" pattern 0.
263 *pattern = val[0] == 0xFF && val[1] == 0xFF ? 0 : val[1];
268 static int elan_i2c_get_version(struct i2c_client *client,
269 u8 pattern, bool iap, u8 *version)
276 cmd = ETP_I2C_FW_VERSION_CMD;
277 else if (pattern == 0)
278 cmd = ETP_I2C_IAP_VERSION_P0_CMD;
280 cmd = ETP_I2C_IAP_VERSION_CMD;
282 error = elan_i2c_read_cmd(client, cmd, val);
284 dev_err(&client->dev, "failed to get %s version: %d\n",
285 iap ? "IAP" : "FW", error);
290 *version = iap ? val[1] : val[0];
296 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 pattern,
297 u16 *ic_type, u8 *version, u8 *clickpad)
302 if (pattern >= 0x01) {
303 error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_CMD, val);
305 dev_err(&client->dev, "failed to get ic type: %d\n",
309 *ic_type = be16_to_cpup((__be16 *)val);
311 error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD,
314 dev_err(&client->dev, "failed to get SM version: %d\n",
319 *clickpad = val[0] & 0x10;
321 error = elan_i2c_read_cmd(client, ETP_I2C_OSM_VERSION_CMD, val);
323 dev_err(&client->dev, "failed to get SM version: %d\n",
329 error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_P0_CMD, val);
331 dev_err(&client->dev, "failed to get ic type: %d\n",
337 error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD,
340 dev_err(&client->dev, "failed to get SM version: %d\n",
344 *clickpad = val[0] & 0x10;
350 static int elan_i2c_get_product_id(struct i2c_client *client, u16 *id)
355 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
357 dev_err(&client->dev, "failed to get product ID: %d\n", error);
361 *id = le16_to_cpup((__le16 *)val);
365 static int elan_i2c_get_checksum(struct i2c_client *client,
371 error = elan_i2c_read_cmd(client,
372 iap ? ETP_I2C_IAP_CHECKSUM_CMD :
373 ETP_I2C_FW_CHECKSUM_CMD,
376 dev_err(&client->dev, "failed to get %s checksum: %d\n",
377 iap ? "IAP" : "FW", error);
381 *csum = le16_to_cpup((__le16 *)val);
385 static int elan_i2c_get_max(struct i2c_client *client,
386 unsigned int *max_x, unsigned int *max_y)
391 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
393 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
397 *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
399 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
401 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
405 *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
410 static int elan_i2c_get_resolution(struct i2c_client *client,
411 u8 *hw_res_x, u8 *hw_res_y)
416 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
418 dev_err(&client->dev, "failed to get resolution: %d\n", error);
428 static int elan_i2c_get_num_traces(struct i2c_client *client,
429 unsigned int *x_traces,
430 unsigned int *y_traces)
435 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
437 dev_err(&client->dev, "failed to get trace info: %d\n", error);
447 static int elan_i2c_get_pressure_adjustment(struct i2c_client *client,
453 error = elan_i2c_read_cmd(client, ETP_I2C_PRESSURE_CMD, val);
455 dev_err(&client->dev, "failed to get pressure format: %d\n",
460 if ((val[0] >> 4) & 0x1)
463 *adjustment = ETP_PRESSURE_OFFSET;
468 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
474 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
476 dev_err(&client->dev,
477 "failed to read iap control register: %d\n",
482 constant = le16_to_cpup((__le16 *)val);
483 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
485 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
490 static int elan_i2c_iap_reset(struct i2c_client *client)
494 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
497 dev_err(&client->dev, "cannot reset IC: %d\n", error);
504 static int elan_i2c_set_flash_key(struct i2c_client *client)
508 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
509 ETP_I2C_IAP_PASSWORD);
511 dev_err(&client->dev, "cannot set flash key: %d\n", error);
518 static int elan_read_write_iap_type(struct i2c_client *client)
526 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_TYPE_CMD,
527 ETP_I2C_IAP_TYPE_REG);
529 dev_err(&client->dev,
530 "cannot write iap type: %d\n", error);
534 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_TYPE_CMD, val);
536 dev_err(&client->dev,
537 "failed to read iap type register: %d\n",
541 constant = le16_to_cpup((__le16 *)val);
542 dev_dbg(&client->dev, "iap type reg: 0x%04x\n", constant);
544 if (constant == ETP_I2C_IAP_TYPE_REG)
547 } while (--retry > 0);
549 dev_err(&client->dev, "cannot set iap type\n");
553 static int elan_i2c_prepare_fw_update(struct i2c_client *client, u16 ic_type,
556 struct device *dev = &client->dev;
562 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
563 error = elan_i2c_iap_get_mode(client, &mode);
567 if (mode == IAP_MODE) {
569 error = elan_i2c_iap_reset(client);
577 error = elan_i2c_set_flash_key(client);
581 /* Wait for F/W IAP initialization */
582 msleep(mode == MAIN_MODE ? 100 : 30);
584 /* Check if we are in IAP mode or not */
585 error = elan_i2c_iap_get_mode(client, &mode);
589 if (mode == MAIN_MODE) {
590 dev_err(dev, "wrong mode: %d\n", mode);
594 if (ic_type >= 0x0D && iap_version >= 1) {
595 error = elan_read_write_iap_type(client);
600 /* Set flash key again */
601 error = elan_i2c_set_flash_key(client);
605 /* Wait for F/W IAP initialization */
608 /* read back to check we actually enabled successfully. */
609 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
611 dev_err(dev, "cannot read iap password: %d\n",
616 password = le16_to_cpup((__le16 *)val);
617 if (password != ETP_I2C_IAP_PASSWORD) {
618 dev_err(dev, "wrong iap password: 0x%X\n", password);
625 static int elan_i2c_write_fw_block(struct i2c_client *client, u16 fw_page_size,
626 const u8 *page, u16 checksum, int idx)
628 struct device *dev = &client->dev;
634 page_store = kmalloc(fw_page_size + 4, GFP_KERNEL);
638 page_store[0] = ETP_I2C_IAP_REG_L;
639 page_store[1] = ETP_I2C_IAP_REG_H;
640 memcpy(&page_store[2], page, fw_page_size);
641 /* recode checksum at last two bytes */
642 put_unaligned_le16(checksum, &page_store[fw_page_size + 2]);
644 ret = i2c_master_send(client, page_store, fw_page_size + 4);
645 if (ret != fw_page_size + 4) {
646 error = ret < 0 ? ret : -EIO;
647 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
651 /* Wait for F/W to update one page ROM data. */
652 msleep(fw_page_size == ETP_FW_PAGE_SIZE_512 ? 50 : 35);
654 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
656 dev_err(dev, "Failed to read IAP write result: %d\n", error);
660 result = le16_to_cpup((__le16 *)val);
661 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
662 dev_err(dev, "IAP reports failed write: %04hx\n",
673 static int elan_i2c_finish_fw_update(struct i2c_client *client,
674 struct completion *completion)
676 struct device *dev = &client->dev;
679 u8 buffer[ETP_I2C_REPORT_LEN];
681 len = i2c_master_recv(client, buffer, ETP_I2C_REPORT_LEN);
682 if (len != ETP_I2C_REPORT_LEN) {
683 error = len < 0 ? len : -EIO;
684 dev_warn(dev, "failed to read I2C data after FW WDT reset: %d (%d)\n",
688 reinit_completion(completion);
689 enable_irq(client->irq);
691 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
693 dev_err(dev, "device reset failed: %d\n", error);
694 } else if (!wait_for_completion_timeout(completion,
695 msecs_to_jiffies(300))) {
696 dev_err(dev, "timeout waiting for device reset\n");
700 disable_irq(client->irq);
705 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
706 if (len != ETP_I2C_INF_LENGTH) {
707 error = len < 0 ? len : -EIO;
708 dev_err(dev, "failed to read INT signal: %d (%d)\n",
716 static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
720 len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
722 dev_err(&client->dev, "failed to read report data: %d\n", len);
726 if (len != ETP_I2C_REPORT_LEN) {
727 dev_err(&client->dev,
728 "wrong report length (%d vs %d expected)\n",
729 len, ETP_I2C_REPORT_LEN);
736 const struct elan_transport_ops elan_i2c_ops = {
737 .initialize = elan_i2c_initialize,
738 .sleep_control = elan_i2c_sleep_control,
739 .power_control = elan_i2c_power_control,
740 .set_mode = elan_i2c_set_mode,
742 .calibrate = elan_i2c_calibrate,
743 .calibrate_result = elan_i2c_calibrate_result,
745 .get_baseline_data = elan_i2c_get_baseline_data,
747 .get_version = elan_i2c_get_version,
748 .get_sm_version = elan_i2c_get_sm_version,
749 .get_product_id = elan_i2c_get_product_id,
750 .get_checksum = elan_i2c_get_checksum,
751 .get_pressure_adjustment = elan_i2c_get_pressure_adjustment,
753 .get_max = elan_i2c_get_max,
754 .get_resolution = elan_i2c_get_resolution,
755 .get_num_traces = elan_i2c_get_num_traces,
757 .iap_get_mode = elan_i2c_iap_get_mode,
758 .iap_reset = elan_i2c_iap_reset,
760 .prepare_fw_update = elan_i2c_prepare_fw_update,
761 .write_fw_block = elan_i2c_write_fw_block,
762 .finish_fw_update = elan_i2c_finish_fw_update,
764 .get_pattern = elan_i2c_get_pattern,
766 .get_report = elan_i2c_get_report,