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_REPORT_LEN_ID2 39
64 #define ETP_I2C_REPORT_MAX_LEN 39
65 #define ETP_I2C_DESC_LENGTH 30
66 #define ETP_I2C_REPORT_DESC_LENGTH 158
67 #define ETP_I2C_INF_LENGTH 2
68 #define ETP_I2C_IAP_PASSWORD 0x1EA5
69 #define ETP_I2C_IAP_RESET 0xF0F0
70 #define ETP_I2C_MAIN_MODE_ON (1 << 9)
71 #define ETP_I2C_IAP_REG_L 0x01
72 #define ETP_I2C_IAP_REG_H 0x06
74 static int elan_i2c_read_block(struct i2c_client *client,
75 u16 reg, u8 *val, u16 len)
80 struct i2c_msg msgs[] = {
83 .flags = client->flags & I2C_M_TEN,
89 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
96 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
97 return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO);
100 static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val)
104 retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
106 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
113 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
119 struct i2c_msg msg = {
120 .addr = client->addr,
121 .flags = client->flags & I2C_M_TEN,
127 ret = i2c_transfer(client->adapter, &msg, 1);
131 dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n",
139 static int elan_i2c_initialize(struct i2c_client *client)
141 struct device *dev = &client->dev;
145 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
147 dev_err(dev, "device reset failed: %d\n", error);
151 /* Wait for the device to reset */
154 /* get reset acknowledgement 0000 */
155 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
157 dev_err(dev, "failed to read reset response: %d\n", error);
161 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
162 val, ETP_I2C_DESC_LENGTH);
164 dev_err(dev, "cannot get device descriptor: %d\n", error);
168 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
169 val, ETP_I2C_REPORT_DESC_LENGTH);
171 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
178 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
180 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
181 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
184 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
190 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
192 dev_err(&client->dev,
193 "failed to read current power state: %d\n",
198 reg = le16_to_cpup((__le16 *)val);
200 reg &= ~ETP_DISABLE_POWER;
202 reg |= ETP_DISABLE_POWER;
204 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
206 dev_err(&client->dev,
207 "failed to write current power state: %d\n",
215 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
217 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
221 static int elan_i2c_calibrate(struct i2c_client *client)
223 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
226 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
228 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
231 static int elan_i2c_get_baseline_data(struct i2c_client *client,
232 bool max_baseline, u8 *value)
237 error = elan_i2c_read_cmd(client,
238 max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
239 ETP_I2C_MIN_BASELINE_CMD,
244 *value = le16_to_cpup((__le16 *)val);
249 static int elan_i2c_get_pattern(struct i2c_client *client, u8 *pattern)
254 error = elan_i2c_read_cmd(client, ETP_I2C_PATTERN_CMD, val);
256 dev_err(&client->dev, "failed to get pattern: %d\n", error);
261 * Not all versions of firmware implement "get pattern" command.
262 * When this command is not implemented the device will respond
263 * with 0xFF 0xFF, which we will treat as "old" pattern 0.
265 *pattern = val[0] == 0xFF && val[1] == 0xFF ? 0 : val[1];
270 static int elan_i2c_get_version(struct i2c_client *client,
271 u8 pattern, bool iap, u8 *version)
278 cmd = ETP_I2C_FW_VERSION_CMD;
279 else if (pattern == 0)
280 cmd = ETP_I2C_IAP_VERSION_P0_CMD;
282 cmd = ETP_I2C_IAP_VERSION_CMD;
284 error = elan_i2c_read_cmd(client, cmd, val);
286 dev_err(&client->dev, "failed to get %s version: %d\n",
287 iap ? "IAP" : "FW", error);
292 *version = iap ? val[1] : val[0];
298 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 pattern,
299 u16 *ic_type, u8 *version, u8 *clickpad)
304 if (pattern >= 0x01) {
305 error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_CMD, val);
307 dev_err(&client->dev, "failed to get ic type: %d\n",
311 *ic_type = be16_to_cpup((__be16 *)val);
313 error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD,
316 dev_err(&client->dev, "failed to get SM version: %d\n",
321 *clickpad = val[0] & 0x10;
323 error = elan_i2c_read_cmd(client, ETP_I2C_OSM_VERSION_CMD, val);
325 dev_err(&client->dev, "failed to get SM version: %d\n",
331 error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_P0_CMD, val);
333 dev_err(&client->dev, "failed to get ic type: %d\n",
339 error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD,
342 dev_err(&client->dev, "failed to get SM version: %d\n",
346 *clickpad = val[0] & 0x10;
352 static int elan_i2c_get_product_id(struct i2c_client *client, u16 *id)
357 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
359 dev_err(&client->dev, "failed to get product ID: %d\n", error);
363 *id = le16_to_cpup((__le16 *)val);
367 static int elan_i2c_get_checksum(struct i2c_client *client,
373 error = elan_i2c_read_cmd(client,
374 iap ? ETP_I2C_IAP_CHECKSUM_CMD :
375 ETP_I2C_FW_CHECKSUM_CMD,
378 dev_err(&client->dev, "failed to get %s checksum: %d\n",
379 iap ? "IAP" : "FW", error);
383 *csum = le16_to_cpup((__le16 *)val);
387 static int elan_i2c_get_max(struct i2c_client *client,
388 unsigned int *max_x, unsigned int *max_y)
393 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
395 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
399 *max_x = le16_to_cpup((__le16 *)val);
401 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
403 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
407 *max_y = le16_to_cpup((__le16 *)val);
412 static int elan_i2c_get_resolution(struct i2c_client *client,
413 u8 *hw_res_x, u8 *hw_res_y)
418 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
420 dev_err(&client->dev, "failed to get resolution: %d\n", error);
430 static int elan_i2c_get_num_traces(struct i2c_client *client,
431 unsigned int *x_traces,
432 unsigned int *y_traces)
437 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
439 dev_err(&client->dev, "failed to get trace info: %d\n", error);
449 static int elan_i2c_get_pressure_adjustment(struct i2c_client *client,
455 error = elan_i2c_read_cmd(client, ETP_I2C_PRESSURE_CMD, val);
457 dev_err(&client->dev, "failed to get pressure format: %d\n",
462 if ((val[0] >> 4) & 0x1)
465 *adjustment = ETP_PRESSURE_OFFSET;
470 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
476 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
478 dev_err(&client->dev,
479 "failed to read iap control register: %d\n",
484 constant = le16_to_cpup((__le16 *)val);
485 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
487 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
492 static int elan_i2c_iap_reset(struct i2c_client *client)
496 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
499 dev_err(&client->dev, "cannot reset IC: %d\n", error);
506 static int elan_i2c_set_flash_key(struct i2c_client *client)
510 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
511 ETP_I2C_IAP_PASSWORD);
513 dev_err(&client->dev, "cannot set flash key: %d\n", error);
520 static int elan_read_write_iap_type(struct i2c_client *client)
528 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_TYPE_CMD,
529 ETP_I2C_IAP_TYPE_REG);
531 dev_err(&client->dev,
532 "cannot write iap type: %d\n", error);
536 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_TYPE_CMD, val);
538 dev_err(&client->dev,
539 "failed to read iap type register: %d\n",
543 constant = le16_to_cpup((__le16 *)val);
544 dev_dbg(&client->dev, "iap type reg: 0x%04x\n", constant);
546 if (constant == ETP_I2C_IAP_TYPE_REG)
549 } while (--retry > 0);
551 dev_err(&client->dev, "cannot set iap type\n");
555 static int elan_i2c_prepare_fw_update(struct i2c_client *client, u16 ic_type,
558 struct device *dev = &client->dev;
564 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
565 error = elan_i2c_iap_get_mode(client, &mode);
569 if (mode == IAP_MODE) {
571 error = elan_i2c_iap_reset(client);
579 error = elan_i2c_set_flash_key(client);
583 /* Wait for F/W IAP initialization */
584 msleep(mode == MAIN_MODE ? 100 : 30);
586 /* Check if we are in IAP mode or not */
587 error = elan_i2c_iap_get_mode(client, &mode);
591 if (mode == MAIN_MODE) {
592 dev_err(dev, "wrong mode: %d\n", mode);
596 if (ic_type >= 0x0D && iap_version >= 1) {
597 error = elan_read_write_iap_type(client);
602 /* Set flash key again */
603 error = elan_i2c_set_flash_key(client);
607 /* Wait for F/W IAP initialization */
610 /* read back to check we actually enabled successfully. */
611 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
613 dev_err(dev, "cannot read iap password: %d\n",
618 password = le16_to_cpup((__le16 *)val);
619 if (password != ETP_I2C_IAP_PASSWORD) {
620 dev_err(dev, "wrong iap password: 0x%X\n", password);
627 static int elan_i2c_write_fw_block(struct i2c_client *client, u16 fw_page_size,
628 const u8 *page, u16 checksum, int idx)
630 struct device *dev = &client->dev;
636 page_store = kmalloc(fw_page_size + 4, GFP_KERNEL);
640 page_store[0] = ETP_I2C_IAP_REG_L;
641 page_store[1] = ETP_I2C_IAP_REG_H;
642 memcpy(&page_store[2], page, fw_page_size);
643 /* recode checksum at last two bytes */
644 put_unaligned_le16(checksum, &page_store[fw_page_size + 2]);
646 ret = i2c_master_send(client, page_store, fw_page_size + 4);
647 if (ret != fw_page_size + 4) {
648 error = ret < 0 ? ret : -EIO;
649 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
653 /* Wait for F/W to update one page ROM data. */
654 msleep(fw_page_size == ETP_FW_PAGE_SIZE_512 ? 50 : 35);
656 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
658 dev_err(dev, "Failed to read IAP write result: %d\n", error);
662 result = le16_to_cpup((__le16 *)val);
663 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
664 dev_err(dev, "IAP reports failed write: %04hx\n",
675 static int elan_i2c_finish_fw_update(struct i2c_client *client,
676 struct completion *completion)
678 struct device *dev = &client->dev;
681 u8 buffer[ETP_I2C_REPORT_MAX_LEN];
683 len = i2c_master_recv(client, buffer, ETP_I2C_REPORT_MAX_LEN);
685 error = len < 0 ? len : -EIO;
686 dev_warn(dev, "failed to read I2C data after FW WDT reset: %d (%d)\n",
690 reinit_completion(completion);
691 enable_irq(client->irq);
693 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
695 dev_err(dev, "device reset failed: %d\n", error);
696 } else if (!wait_for_completion_timeout(completion,
697 msecs_to_jiffies(300))) {
698 dev_err(dev, "timeout waiting for device reset\n");
702 disable_irq(client->irq);
707 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
708 if (len != ETP_I2C_INF_LENGTH) {
709 error = len < 0 ? len : -EIO;
710 dev_err(dev, "failed to read INT signal: %d (%d)\n",
718 static int elan_i2c_get_report_features(struct i2c_client *client, u8 pattern,
719 unsigned int *features,
720 unsigned int *report_len)
722 *features = ETP_FEATURE_REPORT_MK;
723 *report_len = pattern <= 0x01 ?
724 ETP_I2C_REPORT_LEN : ETP_I2C_REPORT_LEN_ID2;
728 static int elan_i2c_get_report(struct i2c_client *client,
729 u8 *report, unsigned int report_len)
733 len = i2c_master_recv(client, report, report_len);
735 dev_err(&client->dev, "failed to read report data: %d\n", len);
739 if (len != report_len) {
740 dev_err(&client->dev,
741 "wrong report length (%d vs %d expected)\n",
749 const struct elan_transport_ops elan_i2c_ops = {
750 .initialize = elan_i2c_initialize,
751 .sleep_control = elan_i2c_sleep_control,
752 .power_control = elan_i2c_power_control,
753 .set_mode = elan_i2c_set_mode,
755 .calibrate = elan_i2c_calibrate,
756 .calibrate_result = elan_i2c_calibrate_result,
758 .get_baseline_data = elan_i2c_get_baseline_data,
760 .get_version = elan_i2c_get_version,
761 .get_sm_version = elan_i2c_get_sm_version,
762 .get_product_id = elan_i2c_get_product_id,
763 .get_checksum = elan_i2c_get_checksum,
764 .get_pressure_adjustment = elan_i2c_get_pressure_adjustment,
766 .get_max = elan_i2c_get_max,
767 .get_resolution = elan_i2c_get_resolution,
768 .get_num_traces = elan_i2c_get_num_traces,
770 .iap_get_mode = elan_i2c_iap_get_mode,
771 .iap_reset = elan_i2c_iap_reset,
773 .prepare_fw_update = elan_i2c_prepare_fw_update,
774 .write_fw_block = elan_i2c_write_fw_block,
775 .finish_fw_update = elan_i2c_finish_fw_update,
777 .get_pattern = elan_i2c_get_pattern,
779 .get_report_features = elan_i2c_get_report_features,
780 .get_report = elan_i2c_get_report,