2 * Elan I2C/SMBus Touchpad driver - I2C interface
4 * Copyright (c) 2013 ELAN Microelectronics Corp.
6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
8 * Based on cyapa driver:
9 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
10 * copyright (c) 2011-2012 Google, Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
16 * Trademarks are the property of their respective owners.
19 #include <linux/completion.h>
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <asm/unaligned.h>
30 /* Elan i2c commands */
31 #define ETP_I2C_RESET 0x0100
32 #define ETP_I2C_WAKE_UP 0x0800
33 #define ETP_I2C_SLEEP 0x0801
34 #define ETP_I2C_DESC_CMD 0x0001
35 #define ETP_I2C_REPORT_DESC_CMD 0x0002
36 #define ETP_I2C_STAND_CMD 0x0005
37 #define ETP_I2C_PATTERN_CMD 0x0100
38 #define ETP_I2C_UNIQUEID_CMD 0x0101
39 #define ETP_I2C_FW_VERSION_CMD 0x0102
40 #define ETP_I2C_IC_TYPE_CMD 0x0103
41 #define ETP_I2C_OSM_VERSION_CMD 0x0103
42 #define ETP_I2C_NSM_VERSION_CMD 0x0104
43 #define ETP_I2C_XY_TRACENUM_CMD 0x0105
44 #define ETP_I2C_MAX_X_AXIS_CMD 0x0106
45 #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107
46 #define ETP_I2C_RESOLUTION_CMD 0x0108
47 #define ETP_I2C_PRESSURE_CMD 0x010A
48 #define ETP_I2C_IAP_VERSION_CMD 0x0110
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
60 #define ETP_I2C_REPORT_LEN 34
61 #define ETP_I2C_DESC_LENGTH 30
62 #define ETP_I2C_REPORT_DESC_LENGTH 158
63 #define ETP_I2C_INF_LENGTH 2
64 #define ETP_I2C_IAP_PASSWORD 0x1EA5
65 #define ETP_I2C_IAP_RESET 0xF0F0
66 #define ETP_I2C_MAIN_MODE_ON (1 << 9)
67 #define ETP_I2C_IAP_REG_L 0x01
68 #define ETP_I2C_IAP_REG_H 0x06
70 static int elan_i2c_read_block(struct i2c_client *client,
71 u16 reg, u8 *val, u16 len)
76 struct i2c_msg msgs[] = {
79 .flags = client->flags & I2C_M_TEN,
85 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
92 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
93 return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO);
96 static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val)
100 retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
102 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
109 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
115 struct i2c_msg msg = {
116 .addr = client->addr,
117 .flags = client->flags & I2C_M_TEN,
123 ret = i2c_transfer(client->adapter, &msg, 1);
127 dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n",
135 static int elan_i2c_initialize(struct i2c_client *client)
137 struct device *dev = &client->dev;
141 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
143 dev_err(dev, "device reset failed: %d\n", error);
147 /* Wait for the device to reset */
150 /* get reset acknowledgement 0000 */
151 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
153 dev_err(dev, "failed to read reset response: %d\n", error);
157 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
158 val, ETP_I2C_DESC_LENGTH);
160 dev_err(dev, "cannot get device descriptor: %d\n", error);
164 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
165 val, ETP_I2C_REPORT_DESC_LENGTH);
167 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
174 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
176 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
177 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
180 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
186 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
188 dev_err(&client->dev,
189 "failed to read current power state: %d\n",
194 reg = le16_to_cpup((__le16 *)val);
196 reg &= ~ETP_DISABLE_POWER;
198 reg |= ETP_DISABLE_POWER;
200 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
202 dev_err(&client->dev,
203 "failed to write current power state: %d\n",
211 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
213 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
217 static int elan_i2c_calibrate(struct i2c_client *client)
219 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
222 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
224 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
227 static int elan_i2c_get_baseline_data(struct i2c_client *client,
228 bool max_baseline, u8 *value)
233 error = elan_i2c_read_cmd(client,
234 max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
235 ETP_I2C_MIN_BASELINE_CMD,
240 *value = le16_to_cpup((__le16 *)val);
245 static int elan_i2c_get_pattern(struct i2c_client *client, u8 *pattern)
250 error = elan_i2c_read_cmd(client, ETP_I2C_PATTERN_CMD, val);
252 dev_err(&client->dev, "failed to get pattern: %d\n", error);
260 static int elan_i2c_get_version(struct i2c_client *client,
261 bool iap, u8 *version)
267 error = elan_i2c_get_pattern(client, &pattern_ver);
269 dev_err(&client->dev, "failed to get pattern version\n");
273 error = elan_i2c_read_cmd(client,
274 iap ? ETP_I2C_IAP_VERSION_CMD :
275 ETP_I2C_FW_VERSION_CMD,
278 dev_err(&client->dev, "failed to get %s version: %d\n",
279 iap ? "IAP" : "FW", error);
283 if (pattern_ver == 0x01)
284 *version = iap ? val[1] : val[0];
290 static int elan_i2c_get_sm_version(struct i2c_client *client,
291 u16 *ic_type, u8 *version)
297 error = elan_i2c_get_pattern(client, &pattern_ver);
299 dev_err(&client->dev, "failed to get pattern version\n");
303 if (pattern_ver == 0x01) {
304 error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_CMD, val);
306 dev_err(&client->dev, "failed to get ic type: %d\n",
310 *ic_type = be16_to_cpup((__be16 *)val);
312 error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD,
315 dev_err(&client->dev, "failed to get SM version: %d\n",
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",
334 static int elan_i2c_get_product_id(struct i2c_client *client, u16 *id)
339 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
341 dev_err(&client->dev, "failed to get product ID: %d\n", error);
345 *id = le16_to_cpup((__le16 *)val);
349 static int elan_i2c_get_checksum(struct i2c_client *client,
355 error = elan_i2c_read_cmd(client,
356 iap ? ETP_I2C_IAP_CHECKSUM_CMD :
357 ETP_I2C_FW_CHECKSUM_CMD,
360 dev_err(&client->dev, "failed to get %s checksum: %d\n",
361 iap ? "IAP" : "FW", error);
365 *csum = le16_to_cpup((__le16 *)val);
369 static int elan_i2c_get_max(struct i2c_client *client,
370 unsigned int *max_x, unsigned int *max_y)
375 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
377 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
381 *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
383 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
385 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
389 *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
394 static int elan_i2c_get_resolution(struct i2c_client *client,
395 u8 *hw_res_x, u8 *hw_res_y)
400 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
402 dev_err(&client->dev, "failed to get resolution: %d\n", error);
412 static int elan_i2c_get_num_traces(struct i2c_client *client,
413 unsigned int *x_traces,
414 unsigned int *y_traces)
419 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
421 dev_err(&client->dev, "failed to get trace info: %d\n", error);
431 static int elan_i2c_get_pressure_adjustment(struct i2c_client *client,
437 error = elan_i2c_read_cmd(client, ETP_I2C_PRESSURE_CMD, val);
439 dev_err(&client->dev, "failed to get pressure format: %d\n",
444 if ((val[0] >> 4) & 0x1)
447 *adjustment = ETP_PRESSURE_OFFSET;
452 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
458 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
460 dev_err(&client->dev,
461 "failed to read iap control register: %d\n",
466 constant = le16_to_cpup((__le16 *)val);
467 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
469 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
474 static int elan_i2c_iap_reset(struct i2c_client *client)
478 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
481 dev_err(&client->dev, "cannot reset IC: %d\n", error);
488 static int elan_i2c_set_flash_key(struct i2c_client *client)
492 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
493 ETP_I2C_IAP_PASSWORD);
495 dev_err(&client->dev, "cannot set flash key: %d\n", error);
502 static int elan_i2c_prepare_fw_update(struct i2c_client *client)
504 struct device *dev = &client->dev;
510 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
511 error = elan_i2c_iap_get_mode(client, &mode);
515 if (mode == IAP_MODE) {
517 error = elan_i2c_iap_reset(client);
525 error = elan_i2c_set_flash_key(client);
529 /* Wait for F/W IAP initialization */
530 msleep(mode == MAIN_MODE ? 100 : 30);
532 /* Check if we are in IAP mode or not */
533 error = elan_i2c_iap_get_mode(client, &mode);
537 if (mode == MAIN_MODE) {
538 dev_err(dev, "wrong mode: %d\n", mode);
542 /* Set flash key again */
543 error = elan_i2c_set_flash_key(client);
547 /* Wait for F/W IAP initialization */
550 /* read back to check we actually enabled successfully. */
551 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
553 dev_err(dev, "cannot read iap password: %d\n",
558 password = le16_to_cpup((__le16 *)val);
559 if (password != ETP_I2C_IAP_PASSWORD) {
560 dev_err(dev, "wrong iap password: 0x%X\n", password);
567 static int elan_i2c_write_fw_block(struct i2c_client *client,
568 const u8 *page, u16 checksum, int idx)
570 struct device *dev = &client->dev;
571 u8 page_store[ETP_FW_PAGE_SIZE + 4];
576 page_store[0] = ETP_I2C_IAP_REG_L;
577 page_store[1] = ETP_I2C_IAP_REG_H;
578 memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
579 /* recode checksum at last two bytes */
580 put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
582 ret = i2c_master_send(client, page_store, sizeof(page_store));
583 if (ret != sizeof(page_store)) {
584 error = ret < 0 ? ret : -EIO;
585 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
589 /* Wait for F/W to update one page ROM data. */
592 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
594 dev_err(dev, "Failed to read IAP write result: %d\n", error);
598 result = le16_to_cpup((__le16 *)val);
599 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
600 dev_err(dev, "IAP reports failed write: %04hx\n",
608 static int elan_i2c_finish_fw_update(struct i2c_client *client,
609 struct completion *completion)
611 struct device *dev = &client->dev;
614 u8 buffer[ETP_I2C_REPORT_LEN];
616 len = i2c_master_recv(client, buffer, ETP_I2C_REPORT_LEN);
617 if (len != ETP_I2C_REPORT_LEN) {
618 error = len < 0 ? len : -EIO;
619 dev_warn(dev, "failed to read I2C data after FW WDT reset: %d (%d)\n",
623 reinit_completion(completion);
624 enable_irq(client->irq);
626 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
628 dev_err(dev, "device reset failed: %d\n", error);
629 } else if (!wait_for_completion_timeout(completion,
630 msecs_to_jiffies(300))) {
631 dev_err(dev, "timeout waiting for device reset\n");
635 disable_irq(client->irq);
640 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
641 if (len != ETP_I2C_INF_LENGTH) {
642 error = len < 0 ? len : -EIO;
643 dev_err(dev, "failed to read INT signal: %d (%d)\n",
651 static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
655 len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
657 dev_err(&client->dev, "failed to read report data: %d\n", len);
661 if (len != ETP_I2C_REPORT_LEN) {
662 dev_err(&client->dev,
663 "wrong report length (%d vs %d expected)\n",
664 len, ETP_I2C_REPORT_LEN);
671 const struct elan_transport_ops elan_i2c_ops = {
672 .initialize = elan_i2c_initialize,
673 .sleep_control = elan_i2c_sleep_control,
674 .power_control = elan_i2c_power_control,
675 .set_mode = elan_i2c_set_mode,
677 .calibrate = elan_i2c_calibrate,
678 .calibrate_result = elan_i2c_calibrate_result,
680 .get_baseline_data = elan_i2c_get_baseline_data,
682 .get_version = elan_i2c_get_version,
683 .get_sm_version = elan_i2c_get_sm_version,
684 .get_product_id = elan_i2c_get_product_id,
685 .get_checksum = elan_i2c_get_checksum,
686 .get_pressure_adjustment = elan_i2c_get_pressure_adjustment,
688 .get_max = elan_i2c_get_max,
689 .get_resolution = elan_i2c_get_resolution,
690 .get_num_traces = elan_i2c_get_num_traces,
692 .iap_get_mode = elan_i2c_iap_get_mode,
693 .iap_reset = elan_i2c_iap_reset,
695 .prepare_fw_update = elan_i2c_prepare_fw_update,
696 .write_fw_block = elan_i2c_write_fw_block,
697 .finish_fw_update = elan_i2c_finish_fw_update,
699 .get_pattern = elan_i2c_get_pattern,
701 .get_report = elan_i2c_get_report,