2 * Elan I2C/SMBus Touchpad driver - I2C interface
4 * Copyright (c) 2013 ELAN Microelectronics Corp.
6 * 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 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published
15 * by the Free Software Foundation.
17 * Trademarks are the property of their respective owners.
20 #include <linux/completion.h>
21 #include <linux/delay.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/jiffies.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <asm/unaligned.h>
31 /* Elan i2c commands */
32 #define ETP_I2C_RESET 0x0100
33 #define ETP_I2C_WAKE_UP 0x0800
34 #define ETP_I2C_SLEEP 0x0801
35 #define ETP_I2C_DESC_CMD 0x0001
36 #define ETP_I2C_REPORT_DESC_CMD 0x0002
37 #define ETP_I2C_STAND_CMD 0x0005
38 #define ETP_I2C_UNIQUEID_CMD 0x0101
39 #define ETP_I2C_FW_VERSION_CMD 0x0102
40 #define ETP_I2C_SM_VERSION_CMD 0x0103
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_IAP_VERSION_CMD 0x0110
46 #define ETP_I2C_SET_CMD 0x0300
47 #define ETP_I2C_POWER_CMD 0x0307
48 #define ETP_I2C_FW_CHECKSUM_CMD 0x030F
49 #define ETP_I2C_IAP_CTRL_CMD 0x0310
50 #define ETP_I2C_IAP_CMD 0x0311
51 #define ETP_I2C_IAP_RESET_CMD 0x0314
52 #define ETP_I2C_IAP_CHECKSUM_CMD 0x0315
53 #define ETP_I2C_CALIBRATE_CMD 0x0316
54 #define ETP_I2C_MAX_BASELINE_CMD 0x0317
55 #define ETP_I2C_MIN_BASELINE_CMD 0x0318
57 #define ETP_I2C_REPORT_LEN 34
58 #define ETP_I2C_DESC_LENGTH 30
59 #define ETP_I2C_REPORT_DESC_LENGTH 158
60 #define ETP_I2C_INF_LENGTH 2
61 #define ETP_I2C_IAP_PASSWORD 0x1EA5
62 #define ETP_I2C_IAP_RESET 0xF0F0
63 #define ETP_I2C_MAIN_MODE_ON (1 << 9)
64 #define ETP_I2C_IAP_REG_L 0x01
65 #define ETP_I2C_IAP_REG_H 0x06
67 static int elan_i2c_read_block(struct i2c_client *client,
68 u16 reg, u8 *val, u16 len)
73 struct i2c_msg msgs[] = {
76 .flags = client->flags & I2C_M_TEN,
82 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
89 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
90 return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO);
93 static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val)
97 retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
99 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
106 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
112 struct i2c_msg msg = {
113 .addr = client->addr,
114 .flags = client->flags & I2C_M_TEN,
120 ret = i2c_transfer(client->adapter, &msg, 1);
121 return ret == 1 ? 0 : (ret < 0 ? ret : -EIO);
124 static int elan_i2c_initialize(struct i2c_client *client)
126 struct device *dev = &client->dev;
130 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
132 dev_err(dev, "device reset failed: %d\n", error);
136 /* Wait for the device to reset */
139 /* get reset acknowledgement 0000 */
140 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
142 dev_err(dev, "failed to read reset response: %d\n", error);
146 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
147 val, ETP_I2C_DESC_LENGTH);
149 dev_err(dev, "cannot get device descriptor: %d\n", error);
153 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
154 val, ETP_I2C_REPORT_DESC_LENGTH);
156 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
163 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
165 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
166 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
169 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
175 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
177 dev_err(&client->dev,
178 "failed to read current power state: %d\n",
183 reg = le16_to_cpup((__le16 *)val);
185 reg &= ~ETP_DISABLE_POWER;
187 reg |= ETP_DISABLE_POWER;
189 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
191 dev_err(&client->dev,
192 "failed to write current power state: %d\n",
200 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
202 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
206 static int elan_i2c_calibrate(struct i2c_client *client)
208 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
211 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
213 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
216 static int elan_i2c_get_baseline_data(struct i2c_client *client,
217 bool max_baseline, u8 *value)
222 error = elan_i2c_read_cmd(client,
223 max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
224 ETP_I2C_MIN_BASELINE_CMD,
229 *value = le16_to_cpup((__le16 *)val);
234 static int elan_i2c_get_version(struct i2c_client *client,
235 bool iap, u8 *version)
240 error = elan_i2c_read_cmd(client,
241 iap ? ETP_I2C_IAP_VERSION_CMD :
242 ETP_I2C_FW_VERSION_CMD,
245 dev_err(&client->dev, "failed to get %s version: %d\n",
246 iap ? "IAP" : "FW", error);
254 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 *version)
259 error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val);
261 dev_err(&client->dev, "failed to get SM version: %d\n", error);
269 static int elan_i2c_get_product_id(struct i2c_client *client, u8 *id)
274 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
276 dev_err(&client->dev, "failed to get product ID: %d\n", error);
284 static int elan_i2c_get_checksum(struct i2c_client *client,
290 error = elan_i2c_read_cmd(client,
291 iap ? ETP_I2C_IAP_CHECKSUM_CMD :
292 ETP_I2C_FW_CHECKSUM_CMD,
295 dev_err(&client->dev, "failed to get %s checksum: %d\n",
296 iap ? "IAP" : "FW", error);
300 *csum = le16_to_cpup((__le16 *)val);
304 static int elan_i2c_get_max(struct i2c_client *client,
305 unsigned int *max_x, unsigned int *max_y)
310 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
312 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
316 *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
318 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
320 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
324 *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
329 static int elan_i2c_get_resolution(struct i2c_client *client,
330 u8 *hw_res_x, u8 *hw_res_y)
335 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
337 dev_err(&client->dev, "failed to get resolution: %d\n", error);
347 static int elan_i2c_get_num_traces(struct i2c_client *client,
348 unsigned int *x_traces,
349 unsigned int *y_traces)
354 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
356 dev_err(&client->dev, "failed to get trace info: %d\n", error);
360 *x_traces = val[0] - 1;
361 *y_traces = val[1] - 1;
366 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
372 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
374 dev_err(&client->dev,
375 "failed to read iap control register: %d\n",
380 constant = le16_to_cpup((__le16 *)val);
381 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
383 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
388 static int elan_i2c_iap_reset(struct i2c_client *client)
392 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
395 dev_err(&client->dev, "cannot reset IC: %d\n", error);
402 static int elan_i2c_set_flash_key(struct i2c_client *client)
406 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
407 ETP_I2C_IAP_PASSWORD);
409 dev_err(&client->dev, "cannot set flash key: %d\n", error);
416 static int elan_i2c_prepare_fw_update(struct i2c_client *client)
418 struct device *dev = &client->dev;
424 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
425 error = elan_i2c_iap_get_mode(client, &mode);
429 if (mode == IAP_MODE) {
431 error = elan_i2c_iap_reset(client);
439 error = elan_i2c_set_flash_key(client);
443 /* Wait for F/W IAP initialization */
444 msleep(mode == MAIN_MODE ? 100 : 30);
446 /* Check if we are in IAP mode or not */
447 error = elan_i2c_iap_get_mode(client, &mode);
451 if (mode == MAIN_MODE) {
452 dev_err(dev, "wrong mode: %d\n", mode);
456 /* Set flash key again */
457 error = elan_i2c_set_flash_key(client);
461 /* Wait for F/W IAP initialization */
464 /* read back to check we actually enabled successfully. */
465 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
467 dev_err(dev, "cannot read iap password: %d\n",
472 password = le16_to_cpup((__le16 *)val);
473 if (password != ETP_I2C_IAP_PASSWORD) {
474 dev_err(dev, "wrong iap password: 0x%X\n", password);
481 static int elan_i2c_write_fw_block(struct i2c_client *client,
482 const u8 *page, u16 checksum, int idx)
484 struct device *dev = &client->dev;
485 u8 page_store[ETP_FW_PAGE_SIZE + 4];
490 page_store[0] = ETP_I2C_IAP_REG_L;
491 page_store[1] = ETP_I2C_IAP_REG_H;
492 memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
493 /* recode checksum at last two bytes */
494 put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
496 ret = i2c_master_send(client, page_store, sizeof(page_store));
497 if (ret != sizeof(page_store)) {
498 error = ret < 0 ? ret : -EIO;
499 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
503 /* Wait for F/W to update one page ROM data. */
506 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
508 dev_err(dev, "Failed to read IAP write result: %d\n", error);
512 result = le16_to_cpup((__le16 *)val);
513 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
514 dev_err(dev, "IAP reports failed write: %04hx\n",
522 static int elan_i2c_finish_fw_update(struct i2c_client *client,
523 struct completion *completion)
525 struct device *dev = &client->dev;
529 u8 buffer[ETP_I2C_INF_LENGTH];
531 reinit_completion(completion);
532 enable_irq(client->irq);
534 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
536 ret = wait_for_completion_interruptible_timeout(completion,
537 msecs_to_jiffies(300));
538 disable_irq(client->irq);
541 dev_err(dev, "device reset failed: %d\n", error);
543 } else if (ret == 0) {
544 dev_err(dev, "timeout waiting for device reset\n");
546 } else if (ret < 0) {
548 dev_err(dev, "error waiting for device reset: %d\n", error);
552 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
553 if (len != ETP_I2C_INF_LENGTH) {
554 error = len < 0 ? len : -EIO;
555 dev_err(dev, "failed to read INT signal: %d (%d)\n",
563 static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
567 len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
569 dev_err(&client->dev, "failed to read report data: %d\n", len);
573 if (len != ETP_I2C_REPORT_LEN) {
574 dev_err(&client->dev,
575 "wrong report length (%d vs %d expected)\n",
576 len, ETP_I2C_REPORT_LEN);
583 const struct elan_transport_ops elan_i2c_ops = {
584 .initialize = elan_i2c_initialize,
585 .sleep_control = elan_i2c_sleep_control,
586 .power_control = elan_i2c_power_control,
587 .set_mode = elan_i2c_set_mode,
589 .calibrate = elan_i2c_calibrate,
590 .calibrate_result = elan_i2c_calibrate_result,
592 .get_baseline_data = elan_i2c_get_baseline_data,
594 .get_version = elan_i2c_get_version,
595 .get_sm_version = elan_i2c_get_sm_version,
596 .get_product_id = elan_i2c_get_product_id,
597 .get_checksum = elan_i2c_get_checksum,
599 .get_max = elan_i2c_get_max,
600 .get_resolution = elan_i2c_get_resolution,
601 .get_num_traces = elan_i2c_get_num_traces,
603 .iap_get_mode = elan_i2c_iap_get_mode,
604 .iap_reset = elan_i2c_iap_reset,
606 .prepare_fw_update = elan_i2c_prepare_fw_update,
607 .write_fw_block = elan_i2c_write_fw_block,
608 .finish_fw_update = elan_i2c_finish_fw_update,
610 .get_report = elan_i2c_get_report,