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_UNIQUEID_CMD 0x0101
38 #define ETP_I2C_FW_VERSION_CMD 0x0102
39 #define ETP_I2C_SM_VERSION_CMD 0x0103
40 #define ETP_I2C_XY_TRACENUM_CMD 0x0105
41 #define ETP_I2C_MAX_X_AXIS_CMD 0x0106
42 #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107
43 #define ETP_I2C_RESOLUTION_CMD 0x0108
44 #define ETP_I2C_IAP_VERSION_CMD 0x0110
45 #define ETP_I2C_SET_CMD 0x0300
46 #define ETP_I2C_POWER_CMD 0x0307
47 #define ETP_I2C_FW_CHECKSUM_CMD 0x030F
48 #define ETP_I2C_IAP_CTRL_CMD 0x0310
49 #define ETP_I2C_IAP_CMD 0x0311
50 #define ETP_I2C_IAP_RESET_CMD 0x0314
51 #define ETP_I2C_IAP_CHECKSUM_CMD 0x0315
52 #define ETP_I2C_CALIBRATE_CMD 0x0316
53 #define ETP_I2C_MAX_BASELINE_CMD 0x0317
54 #define ETP_I2C_MIN_BASELINE_CMD 0x0318
56 #define ETP_I2C_REPORT_LEN 34
57 #define ETP_I2C_DESC_LENGTH 30
58 #define ETP_I2C_REPORT_DESC_LENGTH 158
59 #define ETP_I2C_INF_LENGTH 2
60 #define ETP_I2C_IAP_PASSWORD 0x1EA5
61 #define ETP_I2C_IAP_RESET 0xF0F0
62 #define ETP_I2C_MAIN_MODE_ON (1 << 9)
63 #define ETP_I2C_IAP_REG_L 0x01
64 #define ETP_I2C_IAP_REG_H 0x06
66 static int elan_i2c_read_block(struct i2c_client *client,
67 u16 reg, u8 *val, u16 len)
72 struct i2c_msg msgs[] = {
75 .flags = client->flags & I2C_M_TEN,
81 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
88 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
89 return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO);
92 static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val)
96 retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
98 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
105 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
111 struct i2c_msg msg = {
112 .addr = client->addr,
113 .flags = client->flags & I2C_M_TEN,
119 ret = i2c_transfer(client->adapter, &msg, 1);
120 return ret == 1 ? 0 : (ret < 0 ? ret : -EIO);
123 static int elan_i2c_initialize(struct i2c_client *client)
125 struct device *dev = &client->dev;
129 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
131 dev_err(dev, "device reset failed: %d\n", error);
135 /* Wait for the device to reset */
138 /* get reset acknowledgement 0000 */
139 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
141 dev_err(dev, "failed to read reset response: %d\n", error);
145 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
146 val, ETP_I2C_DESC_LENGTH);
148 dev_err(dev, "cannot get device descriptor: %d\n", error);
152 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
153 val, ETP_I2C_REPORT_DESC_LENGTH);
155 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
162 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
164 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
165 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
168 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
174 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
176 dev_err(&client->dev,
177 "failed to read current power state: %d\n",
182 reg = le16_to_cpup((__le16 *)val);
184 reg &= ~ETP_DISABLE_POWER;
186 reg |= ETP_DISABLE_POWER;
188 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
190 dev_err(&client->dev,
191 "failed to write current power state: %d\n",
199 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
201 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
205 static int elan_i2c_calibrate(struct i2c_client *client)
207 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
210 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
212 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
215 static int elan_i2c_get_baseline_data(struct i2c_client *client,
216 bool max_baseline, u8 *value)
221 error = elan_i2c_read_cmd(client,
222 max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
223 ETP_I2C_MIN_BASELINE_CMD,
228 *value = le16_to_cpup((__le16 *)val);
233 static int elan_i2c_get_version(struct i2c_client *client,
234 bool iap, u8 *version)
239 error = elan_i2c_read_cmd(client,
240 iap ? ETP_I2C_IAP_VERSION_CMD :
241 ETP_I2C_FW_VERSION_CMD,
244 dev_err(&client->dev, "failed to get %s version: %d\n",
245 iap ? "IAP" : "FW", error);
253 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 *version)
258 error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val);
260 dev_err(&client->dev, "failed to get SM version: %d\n", error);
268 static int elan_i2c_get_product_id(struct i2c_client *client, u8 *id)
273 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
275 dev_err(&client->dev, "failed to get product ID: %d\n", error);
283 static int elan_i2c_get_checksum(struct i2c_client *client,
289 error = elan_i2c_read_cmd(client,
290 iap ? ETP_I2C_IAP_CHECKSUM_CMD :
291 ETP_I2C_FW_CHECKSUM_CMD,
294 dev_err(&client->dev, "failed to get %s checksum: %d\n",
295 iap ? "IAP" : "FW", error);
299 *csum = le16_to_cpup((__le16 *)val);
303 static int elan_i2c_get_max(struct i2c_client *client,
304 unsigned int *max_x, unsigned int *max_y)
309 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
311 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
315 *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
317 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
319 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
323 *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
328 static int elan_i2c_get_resolution(struct i2c_client *client,
329 u8 *hw_res_x, u8 *hw_res_y)
334 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
336 dev_err(&client->dev, "failed to get resolution: %d\n", error);
346 static int elan_i2c_get_num_traces(struct i2c_client *client,
347 unsigned int *x_traces,
348 unsigned int *y_traces)
353 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
355 dev_err(&client->dev, "failed to get trace info: %d\n", error);
359 *x_traces = val[0] - 1;
360 *y_traces = val[1] - 1;
365 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
371 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
373 dev_err(&client->dev,
374 "failed to read iap control register: %d\n",
379 constant = le16_to_cpup((__le16 *)val);
380 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
382 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
387 static int elan_i2c_iap_reset(struct i2c_client *client)
391 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
394 dev_err(&client->dev, "cannot reset IC: %d\n", error);
401 static int elan_i2c_set_flash_key(struct i2c_client *client)
405 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
406 ETP_I2C_IAP_PASSWORD);
408 dev_err(&client->dev, "cannot set flash key: %d\n", error);
415 static int elan_i2c_prepare_fw_update(struct i2c_client *client)
417 struct device *dev = &client->dev;
423 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
424 error = elan_i2c_iap_get_mode(client, &mode);
428 if (mode == IAP_MODE) {
430 error = elan_i2c_iap_reset(client);
438 error = elan_i2c_set_flash_key(client);
442 /* Wait for F/W IAP initialization */
443 msleep(mode == MAIN_MODE ? 100 : 30);
445 /* Check if we are in IAP mode or not */
446 error = elan_i2c_iap_get_mode(client, &mode);
450 if (mode == MAIN_MODE) {
451 dev_err(dev, "wrong mode: %d\n", mode);
455 /* Set flash key again */
456 error = elan_i2c_set_flash_key(client);
460 /* Wait for F/W IAP initialization */
463 /* read back to check we actually enabled successfully. */
464 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
466 dev_err(dev, "cannot read iap password: %d\n",
471 password = le16_to_cpup((__le16 *)val);
472 if (password != ETP_I2C_IAP_PASSWORD) {
473 dev_err(dev, "wrong iap password: 0x%X\n", password);
480 static int elan_i2c_write_fw_block(struct i2c_client *client,
481 const u8 *page, u16 checksum, int idx)
483 struct device *dev = &client->dev;
484 u8 page_store[ETP_FW_PAGE_SIZE + 4];
489 page_store[0] = ETP_I2C_IAP_REG_L;
490 page_store[1] = ETP_I2C_IAP_REG_H;
491 memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
492 /* recode checksum at last two bytes */
493 put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
495 ret = i2c_master_send(client, page_store, sizeof(page_store));
496 if (ret != sizeof(page_store)) {
497 error = ret < 0 ? ret : -EIO;
498 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
502 /* Wait for F/W to update one page ROM data. */
505 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
507 dev_err(dev, "Failed to read IAP write result: %d\n", error);
511 result = le16_to_cpup((__le16 *)val);
512 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
513 dev_err(dev, "IAP reports failed write: %04hx\n",
521 static int elan_i2c_finish_fw_update(struct i2c_client *client,
522 struct completion *completion)
524 struct device *dev = &client->dev;
528 u8 buffer[ETP_I2C_INF_LENGTH];
530 reinit_completion(completion);
531 enable_irq(client->irq);
533 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
535 ret = wait_for_completion_interruptible_timeout(completion,
536 msecs_to_jiffies(300));
537 disable_irq(client->irq);
540 dev_err(dev, "device reset failed: %d\n", error);
542 } else if (ret == 0) {
543 dev_err(dev, "timeout waiting for device reset\n");
545 } else if (ret < 0) {
547 dev_err(dev, "error waiting for device reset: %d\n", error);
551 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
552 if (len != ETP_I2C_INF_LENGTH) {
553 error = len < 0 ? len : -EIO;
554 dev_err(dev, "failed to read INT signal: %d (%d)\n",
562 static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
566 len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
568 dev_err(&client->dev, "failed to read report data: %d\n", len);
572 if (len != ETP_I2C_REPORT_LEN) {
573 dev_err(&client->dev,
574 "wrong report length (%d vs %d expected)\n",
575 len, ETP_I2C_REPORT_LEN);
582 const struct elan_transport_ops elan_i2c_ops = {
583 .initialize = elan_i2c_initialize,
584 .sleep_control = elan_i2c_sleep_control,
585 .power_control = elan_i2c_power_control,
586 .set_mode = elan_i2c_set_mode,
588 .calibrate = elan_i2c_calibrate,
589 .calibrate_result = elan_i2c_calibrate_result,
591 .get_baseline_data = elan_i2c_get_baseline_data,
593 .get_version = elan_i2c_get_version,
594 .get_sm_version = elan_i2c_get_sm_version,
595 .get_product_id = elan_i2c_get_product_id,
596 .get_checksum = elan_i2c_get_checksum,
598 .get_max = elan_i2c_get_max,
599 .get_resolution = elan_i2c_get_resolution,
600 .get_num_traces = elan_i2c_get_num_traces,
602 .iap_get_mode = elan_i2c_iap_get_mode,
603 .iap_reset = elan_i2c_iap_reset,
605 .prepare_fw_update = elan_i2c_prepare_fw_update,
606 .write_fw_block = elan_i2c_write_fw_block,
607 .finish_fw_update = elan_i2c_finish_fw_update,
609 .get_report = elan_i2c_get_report,