udf: improve error management in udf_CS0toUTF8()
[linux/fpc-iii.git] / drivers / input / mouse / elan_i2c_i2c.c
bloba0acbbf83bfd4faaa7d14a09dbe332848dc648e4
1 /*
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>
28 #include "elan_i2c.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_PRESSURE_CMD 0x010A
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)
70 __le16 buf[] = {
71 cpu_to_le16(reg),
73 struct i2c_msg msgs[] = {
75 .addr = client->addr,
76 .flags = client->flags & I2C_M_TEN,
77 .len = sizeof(buf),
78 .buf = (u8 *)buf,
81 .addr = client->addr,
82 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
83 .len = len,
84 .buf = val,
87 int ret;
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)
95 int retval;
97 retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
98 if (retval < 0) {
99 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
100 return retval;
103 return 0;
106 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
108 __le16 buf[] = {
109 cpu_to_le16(reg),
110 cpu_to_le16(cmd),
112 struct i2c_msg msg = {
113 .addr = client->addr,
114 .flags = client->flags & I2C_M_TEN,
115 .len = sizeof(buf),
116 .buf = (u8 *)buf,
118 int ret;
120 ret = i2c_transfer(client->adapter, &msg, 1);
121 if (ret != 1) {
122 if (ret >= 0)
123 ret = -EIO;
124 dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n",
125 reg, ret);
126 return ret;
129 return 0;
132 static int elan_i2c_initialize(struct i2c_client *client)
134 struct device *dev = &client->dev;
135 int error;
136 u8 val[256];
138 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
139 if (error) {
140 dev_err(dev, "device reset failed: %d\n", error);
141 return error;
144 /* Wait for the device to reset */
145 msleep(100);
147 /* get reset acknowledgement 0000 */
148 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
149 if (error < 0) {
150 dev_err(dev, "failed to read reset response: %d\n", error);
151 return error;
154 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
155 val, ETP_I2C_DESC_LENGTH);
156 if (error) {
157 dev_err(dev, "cannot get device descriptor: %d\n", error);
158 return error;
161 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
162 val, ETP_I2C_REPORT_DESC_LENGTH);
163 if (error) {
164 dev_err(dev, "fetching report descriptor failed.: %d\n", error);
165 return error;
168 return 0;
171 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
173 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
174 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
177 static int elan_i2c_power_control(struct i2c_client *client, bool enable)
179 u8 val[2];
180 u16 reg;
181 int error;
183 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
184 if (error) {
185 dev_err(&client->dev,
186 "failed to read current power state: %d\n",
187 error);
188 return error;
191 reg = le16_to_cpup((__le16 *)val);
192 if (enable)
193 reg &= ~ETP_DISABLE_POWER;
194 else
195 reg |= ETP_DISABLE_POWER;
197 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
198 if (error) {
199 dev_err(&client->dev,
200 "failed to write current power state: %d\n",
201 error);
202 return error;
205 return 0;
208 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
210 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
214 static int elan_i2c_calibrate(struct i2c_client *client)
216 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
219 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
221 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
224 static int elan_i2c_get_baseline_data(struct i2c_client *client,
225 bool max_baseline, u8 *value)
227 int error;
228 u8 val[3];
230 error = elan_i2c_read_cmd(client,
231 max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
232 ETP_I2C_MIN_BASELINE_CMD,
233 val);
234 if (error)
235 return error;
237 *value = le16_to_cpup((__le16 *)val);
239 return 0;
242 static int elan_i2c_get_version(struct i2c_client *client,
243 bool iap, u8 *version)
245 int error;
246 u8 val[3];
248 error = elan_i2c_read_cmd(client,
249 iap ? ETP_I2C_IAP_VERSION_CMD :
250 ETP_I2C_FW_VERSION_CMD,
251 val);
252 if (error) {
253 dev_err(&client->dev, "failed to get %s version: %d\n",
254 iap ? "IAP" : "FW", error);
255 return error;
258 *version = val[0];
259 return 0;
262 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 *version)
264 int error;
265 u8 val[3];
267 error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val);
268 if (error) {
269 dev_err(&client->dev, "failed to get SM version: %d\n", error);
270 return error;
273 *version = val[0];
274 return 0;
277 static int elan_i2c_get_product_id(struct i2c_client *client, u8 *id)
279 int error;
280 u8 val[3];
282 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
283 if (error) {
284 dev_err(&client->dev, "failed to get product ID: %d\n", error);
285 return error;
288 *id = val[0];
289 return 0;
292 static int elan_i2c_get_checksum(struct i2c_client *client,
293 bool iap, u16 *csum)
295 int error;
296 u8 val[3];
298 error = elan_i2c_read_cmd(client,
299 iap ? ETP_I2C_IAP_CHECKSUM_CMD :
300 ETP_I2C_FW_CHECKSUM_CMD,
301 val);
302 if (error) {
303 dev_err(&client->dev, "failed to get %s checksum: %d\n",
304 iap ? "IAP" : "FW", error);
305 return error;
308 *csum = le16_to_cpup((__le16 *)val);
309 return 0;
312 static int elan_i2c_get_max(struct i2c_client *client,
313 unsigned int *max_x, unsigned int *max_y)
315 int error;
316 u8 val[3];
318 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
319 if (error) {
320 dev_err(&client->dev, "failed to get X dimension: %d\n", error);
321 return error;
324 *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
326 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
327 if (error) {
328 dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
329 return error;
332 *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
334 return 0;
337 static int elan_i2c_get_resolution(struct i2c_client *client,
338 u8 *hw_res_x, u8 *hw_res_y)
340 int error;
341 u8 val[3];
343 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
344 if (error) {
345 dev_err(&client->dev, "failed to get resolution: %d\n", error);
346 return error;
349 *hw_res_x = val[0];
350 *hw_res_y = val[1];
352 return 0;
355 static int elan_i2c_get_num_traces(struct i2c_client *client,
356 unsigned int *x_traces,
357 unsigned int *y_traces)
359 int error;
360 u8 val[3];
362 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
363 if (error) {
364 dev_err(&client->dev, "failed to get trace info: %d\n", error);
365 return error;
368 *x_traces = val[0];
369 *y_traces = val[1];
371 return 0;
374 static int elan_i2c_get_pressure_adjustment(struct i2c_client *client,
375 int *adjustment)
377 int error;
378 u8 val[3];
380 error = elan_i2c_read_cmd(client, ETP_I2C_PRESSURE_CMD, val);
381 if (error) {
382 dev_err(&client->dev, "failed to get pressure format: %d\n",
383 error);
384 return error;
387 if ((val[0] >> 4) & 0x1)
388 *adjustment = 0;
389 else
390 *adjustment = ETP_PRESSURE_OFFSET;
392 return 0;
395 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
397 int error;
398 u16 constant;
399 u8 val[3];
401 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
402 if (error) {
403 dev_err(&client->dev,
404 "failed to read iap control register: %d\n",
405 error);
406 return error;
409 constant = le16_to_cpup((__le16 *)val);
410 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
412 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
414 return 0;
417 static int elan_i2c_iap_reset(struct i2c_client *client)
419 int error;
421 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
422 ETP_I2C_IAP_RESET);
423 if (error) {
424 dev_err(&client->dev, "cannot reset IC: %d\n", error);
425 return error;
428 return 0;
431 static int elan_i2c_set_flash_key(struct i2c_client *client)
433 int error;
435 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
436 ETP_I2C_IAP_PASSWORD);
437 if (error) {
438 dev_err(&client->dev, "cannot set flash key: %d\n", error);
439 return error;
442 return 0;
445 static int elan_i2c_prepare_fw_update(struct i2c_client *client)
447 struct device *dev = &client->dev;
448 int error;
449 enum tp_mode mode;
450 u8 val[3];
451 u16 password;
453 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
454 error = elan_i2c_iap_get_mode(client, &mode);
455 if (error)
456 return error;
458 if (mode == IAP_MODE) {
459 /* Reset IC */
460 error = elan_i2c_iap_reset(client);
461 if (error)
462 return error;
464 msleep(30);
467 /* Set flash key*/
468 error = elan_i2c_set_flash_key(client);
469 if (error)
470 return error;
472 /* Wait for F/W IAP initialization */
473 msleep(mode == MAIN_MODE ? 100 : 30);
475 /* Check if we are in IAP mode or not */
476 error = elan_i2c_iap_get_mode(client, &mode);
477 if (error)
478 return error;
480 if (mode == MAIN_MODE) {
481 dev_err(dev, "wrong mode: %d\n", mode);
482 return -EIO;
485 /* Set flash key again */
486 error = elan_i2c_set_flash_key(client);
487 if (error)
488 return error;
490 /* Wait for F/W IAP initialization */
491 msleep(30);
493 /* read back to check we actually enabled successfully. */
494 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
495 if (error) {
496 dev_err(dev, "cannot read iap password: %d\n",
497 error);
498 return error;
501 password = le16_to_cpup((__le16 *)val);
502 if (password != ETP_I2C_IAP_PASSWORD) {
503 dev_err(dev, "wrong iap password: 0x%X\n", password);
504 return -EIO;
507 return 0;
510 static int elan_i2c_write_fw_block(struct i2c_client *client,
511 const u8 *page, u16 checksum, int idx)
513 struct device *dev = &client->dev;
514 u8 page_store[ETP_FW_PAGE_SIZE + 4];
515 u8 val[3];
516 u16 result;
517 int ret, error;
519 page_store[0] = ETP_I2C_IAP_REG_L;
520 page_store[1] = ETP_I2C_IAP_REG_H;
521 memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
522 /* recode checksum at last two bytes */
523 put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
525 ret = i2c_master_send(client, page_store, sizeof(page_store));
526 if (ret != sizeof(page_store)) {
527 error = ret < 0 ? ret : -EIO;
528 dev_err(dev, "Failed to write page %d: %d\n", idx, error);
529 return error;
532 /* Wait for F/W to update one page ROM data. */
533 msleep(20);
535 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
536 if (error) {
537 dev_err(dev, "Failed to read IAP write result: %d\n", error);
538 return error;
541 result = le16_to_cpup((__le16 *)val);
542 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
543 dev_err(dev, "IAP reports failed write: %04hx\n",
544 result);
545 return -EIO;
548 return 0;
551 static int elan_i2c_finish_fw_update(struct i2c_client *client,
552 struct completion *completion)
554 struct device *dev = &client->dev;
555 long ret;
556 int error;
557 int len;
558 u8 buffer[ETP_I2C_INF_LENGTH];
560 reinit_completion(completion);
561 enable_irq(client->irq);
563 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
564 if (!error)
565 ret = wait_for_completion_interruptible_timeout(completion,
566 msecs_to_jiffies(300));
567 disable_irq(client->irq);
569 if (error) {
570 dev_err(dev, "device reset failed: %d\n", error);
571 return error;
572 } else if (ret == 0) {
573 dev_err(dev, "timeout waiting for device reset\n");
574 return -ETIMEDOUT;
575 } else if (ret < 0) {
576 error = ret;
577 dev_err(dev, "error waiting for device reset: %d\n", error);
578 return error;
581 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
582 if (len != ETP_I2C_INF_LENGTH) {
583 error = len < 0 ? len : -EIO;
584 dev_err(dev, "failed to read INT signal: %d (%d)\n",
585 error, len);
586 return error;
589 return 0;
592 static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
594 int len;
596 len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
597 if (len < 0) {
598 dev_err(&client->dev, "failed to read report data: %d\n", len);
599 return len;
602 if (len != ETP_I2C_REPORT_LEN) {
603 dev_err(&client->dev,
604 "wrong report length (%d vs %d expected)\n",
605 len, ETP_I2C_REPORT_LEN);
606 return -EIO;
609 return 0;
612 const struct elan_transport_ops elan_i2c_ops = {
613 .initialize = elan_i2c_initialize,
614 .sleep_control = elan_i2c_sleep_control,
615 .power_control = elan_i2c_power_control,
616 .set_mode = elan_i2c_set_mode,
618 .calibrate = elan_i2c_calibrate,
619 .calibrate_result = elan_i2c_calibrate_result,
621 .get_baseline_data = elan_i2c_get_baseline_data,
623 .get_version = elan_i2c_get_version,
624 .get_sm_version = elan_i2c_get_sm_version,
625 .get_product_id = elan_i2c_get_product_id,
626 .get_checksum = elan_i2c_get_checksum,
627 .get_pressure_adjustment = elan_i2c_get_pressure_adjustment,
629 .get_max = elan_i2c_get_max,
630 .get_resolution = elan_i2c_get_resolution,
631 .get_num_traces = elan_i2c_get_num_traces,
633 .iap_get_mode = elan_i2c_iap_get_mode,
634 .iap_reset = elan_i2c_iap_reset,
636 .prepare_fw_update = elan_i2c_prepare_fw_update,
637 .write_fw_block = elan_i2c_write_fw_block,
638 .finish_fw_update = elan_i2c_finish_fw_update,
640 .get_report = elan_i2c_get_report,