elan_i2c_i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Elan I2C/SMBus Touchpad driver - I2C interface
  3. *
  4. * Copyright (c) 2013 ELAN Microelectronics Corp.
  5. *
  6. * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
  7. * Version: 1.5.5
  8. *
  9. * Based on cyapa driver:
  10. * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
  11. * copyright (c) 2011-2012 Google, Inc.
  12. *
  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.
  16. *
  17. * Trademarks are the property of their respective owners.
  18. */
  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>
  27. #include "elan_i2c.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_UNIQUEID_CMD 0x0101
  36. #define ETP_I2C_FW_VERSION_CMD 0x0102
  37. #define ETP_I2C_SM_VERSION_CMD 0x0103
  38. #define ETP_I2C_XY_TRACENUM_CMD 0x0105
  39. #define ETP_I2C_MAX_X_AXIS_CMD 0x0106
  40. #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107
  41. #define ETP_I2C_RESOLUTION_CMD 0x0108
  42. #define ETP_I2C_IAP_VERSION_CMD 0x0110
  43. #define ETP_I2C_SET_CMD 0x0300
  44. #define ETP_I2C_POWER_CMD 0x0307
  45. #define ETP_I2C_FW_CHECKSUM_CMD 0x030F
  46. #define ETP_I2C_IAP_CTRL_CMD 0x0310
  47. #define ETP_I2C_IAP_CMD 0x0311
  48. #define ETP_I2C_IAP_RESET_CMD 0x0314
  49. #define ETP_I2C_IAP_CHECKSUM_CMD 0x0315
  50. #define ETP_I2C_CALIBRATE_CMD 0x0316
  51. #define ETP_I2C_MAX_BASELINE_CMD 0x0317
  52. #define ETP_I2C_MIN_BASELINE_CMD 0x0318
  53. #define ETP_I2C_REPORT_LEN 34
  54. #define ETP_I2C_DESC_LENGTH 30
  55. #define ETP_I2C_REPORT_DESC_LENGTH 158
  56. #define ETP_I2C_INF_LENGTH 2
  57. #define ETP_I2C_IAP_PASSWORD 0x1EA5
  58. #define ETP_I2C_IAP_RESET 0xF0F0
  59. #define ETP_I2C_MAIN_MODE_ON (1 << 9)
  60. #define ETP_I2C_IAP_REG_L 0x01
  61. #define ETP_I2C_IAP_REG_H 0x06
  62. static int elan_i2c_read_block(struct i2c_client *client,
  63. u16 reg, u8 *val, u16 len)
  64. {
  65. __le16 buf[] = {
  66. cpu_to_le16(reg),
  67. };
  68. struct i2c_msg msgs[] = {
  69. {
  70. .addr = client->addr,
  71. .flags = client->flags & I2C_M_TEN,
  72. .len = sizeof(buf),
  73. .buf = (u8 *)buf,
  74. },
  75. {
  76. .addr = client->addr,
  77. .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
  78. .len = len,
  79. .buf = val,
  80. }
  81. };
  82. int ret;
  83. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  84. return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO);
  85. }
  86. static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val)
  87. {
  88. int retval;
  89. retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH);
  90. if (retval < 0) {
  91. dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg);
  92. return retval;
  93. }
  94. return 0;
  95. }
  96. static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd)
  97. {
  98. __le16 buf[] = {
  99. cpu_to_le16(reg),
  100. cpu_to_le16(cmd),
  101. };
  102. struct i2c_msg msg = {
  103. .addr = client->addr,
  104. .flags = client->flags & I2C_M_TEN,
  105. .len = sizeof(buf),
  106. .buf = (u8 *)buf,
  107. };
  108. int ret;
  109. ret = i2c_transfer(client->adapter, &msg, 1);
  110. return ret == 1 ? 0 : (ret < 0 ? ret : -EIO);
  111. }
  112. static int elan_i2c_initialize(struct i2c_client *client)
  113. {
  114. struct device *dev = &client->dev;
  115. int error;
  116. u8 val[256];
  117. error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
  118. if (error) {
  119. dev_err(dev, "device reset failed: %d\n", error);
  120. return error;
  121. }
  122. /* Wait for the device to reset */
  123. msleep(100);
  124. /* get reset acknowledgement 0000 */
  125. error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
  126. if (error < 0) {
  127. dev_err(dev, "failed to read reset response: %d\n", error);
  128. return error;
  129. }
  130. error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
  131. val, ETP_I2C_DESC_LENGTH);
  132. if (error) {
  133. dev_err(dev, "cannot get device descriptor: %d\n", error);
  134. return error;
  135. }
  136. error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
  137. val, ETP_I2C_REPORT_DESC_LENGTH);
  138. if (error) {
  139. dev_err(dev, "fetching report descriptor failed.: %d\n", error);
  140. return error;
  141. }
  142. return 0;
  143. }
  144. static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
  145. {
  146. return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
  147. sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
  148. }
  149. static int elan_i2c_power_control(struct i2c_client *client, bool enable)
  150. {
  151. u8 val[2];
  152. u16 reg;
  153. int error;
  154. error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
  155. if (error) {
  156. dev_err(&client->dev,
  157. "failed to read current power state: %d\n",
  158. error);
  159. return error;
  160. }
  161. reg = le16_to_cpup((__le16 *)val);
  162. if (enable)
  163. reg &= ~ETP_DISABLE_POWER;
  164. else
  165. reg |= ETP_DISABLE_POWER;
  166. error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
  167. if (error) {
  168. dev_err(&client->dev,
  169. "failed to write current power state: %d\n",
  170. error);
  171. return error;
  172. }
  173. return 0;
  174. }
  175. static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
  176. {
  177. return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
  178. }
  179. static int elan_i2c_calibrate(struct i2c_client *client)
  180. {
  181. return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
  182. }
  183. static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
  184. {
  185. return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
  186. }
  187. static int elan_i2c_get_baseline_data(struct i2c_client *client,
  188. bool max_baseline, u8 *value)
  189. {
  190. int error;
  191. u8 val[3];
  192. error = elan_i2c_read_cmd(client,
  193. max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
  194. ETP_I2C_MIN_BASELINE_CMD,
  195. val);
  196. if (error)
  197. return error;
  198. *value = le16_to_cpup((__le16 *)val);
  199. return 0;
  200. }
  201. static int elan_i2c_get_version(struct i2c_client *client,
  202. bool iap, u8 *version)
  203. {
  204. int error;
  205. u8 val[3];
  206. error = elan_i2c_read_cmd(client,
  207. iap ? ETP_I2C_IAP_VERSION_CMD :
  208. ETP_I2C_FW_VERSION_CMD,
  209. val);
  210. if (error) {
  211. dev_err(&client->dev, "failed to get %s version: %d\n",
  212. iap ? "IAP" : "FW", error);
  213. return error;
  214. }
  215. *version = val[0];
  216. return 0;
  217. }
  218. static int elan_i2c_get_sm_version(struct i2c_client *client, u8 *version)
  219. {
  220. int error;
  221. u8 val[3];
  222. error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val);
  223. if (error) {
  224. dev_err(&client->dev, "failed to get SM version: %d\n", error);
  225. return error;
  226. }
  227. *version = val[0];
  228. return 0;
  229. }
  230. static int elan_i2c_get_product_id(struct i2c_client *client, u8 *id)
  231. {
  232. int error;
  233. u8 val[3];
  234. error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
  235. if (error) {
  236. dev_err(&client->dev, "failed to get product ID: %d\n", error);
  237. return error;
  238. }
  239. *id = val[0];
  240. return 0;
  241. }
  242. static int elan_i2c_get_checksum(struct i2c_client *client,
  243. bool iap, u16 *csum)
  244. {
  245. int error;
  246. u8 val[3];
  247. error = elan_i2c_read_cmd(client,
  248. iap ? ETP_I2C_IAP_CHECKSUM_CMD :
  249. ETP_I2C_FW_CHECKSUM_CMD,
  250. val);
  251. if (error) {
  252. dev_err(&client->dev, "failed to get %s checksum: %d\n",
  253. iap ? "IAP" : "FW", error);
  254. return error;
  255. }
  256. *csum = le16_to_cpup((__le16 *)val);
  257. return 0;
  258. }
  259. static int elan_i2c_get_max(struct i2c_client *client,
  260. unsigned int *max_x, unsigned int *max_y)
  261. {
  262. int error;
  263. u8 val[3];
  264. error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
  265. if (error) {
  266. dev_err(&client->dev, "failed to get X dimension: %d\n", error);
  267. return error;
  268. }
  269. *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
  270. error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
  271. if (error) {
  272. dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
  273. return error;
  274. }
  275. *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
  276. return 0;
  277. }
  278. static int elan_i2c_get_resolution(struct i2c_client *client,
  279. u8 *hw_res_x, u8 *hw_res_y)
  280. {
  281. int error;
  282. u8 val[3];
  283. error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
  284. if (error) {
  285. dev_err(&client->dev, "failed to get resolution: %d\n", error);
  286. return error;
  287. }
  288. *hw_res_x = val[0];
  289. *hw_res_y = val[1];
  290. return 0;
  291. }
  292. static int elan_i2c_get_num_traces(struct i2c_client *client,
  293. unsigned int *x_traces,
  294. unsigned int *y_traces)
  295. {
  296. int error;
  297. u8 val[3];
  298. error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
  299. if (error) {
  300. dev_err(&client->dev, "failed to get trace info: %d\n", error);
  301. return error;
  302. }
  303. *x_traces = val[0] - 1;
  304. *y_traces = val[1] - 1;
  305. return 0;
  306. }
  307. static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
  308. {
  309. int error;
  310. u16 constant;
  311. u8 val[3];
  312. error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
  313. if (error) {
  314. dev_err(&client->dev,
  315. "failed to read iap control register: %d\n",
  316. error);
  317. return error;
  318. }
  319. constant = le16_to_cpup((__le16 *)val);
  320. dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
  321. *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
  322. return 0;
  323. }
  324. static int elan_i2c_iap_reset(struct i2c_client *client)
  325. {
  326. int error;
  327. error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
  328. ETP_I2C_IAP_RESET);
  329. if (error) {
  330. dev_err(&client->dev, "cannot reset IC: %d\n", error);
  331. return error;
  332. }
  333. return 0;
  334. }
  335. static int elan_i2c_set_flash_key(struct i2c_client *client)
  336. {
  337. int error;
  338. error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
  339. ETP_I2C_IAP_PASSWORD);
  340. if (error) {
  341. dev_err(&client->dev, "cannot set flash key: %d\n", error);
  342. return error;
  343. }
  344. return 0;
  345. }
  346. static int elan_i2c_prepare_fw_update(struct i2c_client *client)
  347. {
  348. struct device *dev = &client->dev;
  349. int error;
  350. enum tp_mode mode;
  351. u8 val[3];
  352. u16 password;
  353. /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
  354. error = elan_i2c_iap_get_mode(client, &mode);
  355. if (error)
  356. return error;
  357. if (mode == IAP_MODE) {
  358. /* Reset IC */
  359. error = elan_i2c_iap_reset(client);
  360. if (error)
  361. return error;
  362. msleep(30);
  363. }
  364. /* Set flash key*/
  365. error = elan_i2c_set_flash_key(client);
  366. if (error)
  367. return error;
  368. /* Wait for F/W IAP initialization */
  369. msleep(mode == MAIN_MODE ? 100 : 30);
  370. /* Check if we are in IAP mode or not */
  371. error = elan_i2c_iap_get_mode(client, &mode);
  372. if (error)
  373. return error;
  374. if (mode == MAIN_MODE) {
  375. dev_err(dev, "wrong mode: %d\n", mode);
  376. return -EIO;
  377. }
  378. /* Set flash key again */
  379. error = elan_i2c_set_flash_key(client);
  380. if (error)
  381. return error;
  382. /* Wait for F/W IAP initialization */
  383. msleep(30);
  384. /* read back to check we actually enabled successfully. */
  385. error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
  386. if (error) {
  387. dev_err(dev, "cannot read iap password: %d\n",
  388. error);
  389. return error;
  390. }
  391. password = le16_to_cpup((__le16 *)val);
  392. if (password != ETP_I2C_IAP_PASSWORD) {
  393. dev_err(dev, "wrong iap password: 0x%X\n", password);
  394. return -EIO;
  395. }
  396. return 0;
  397. }
  398. static int elan_i2c_write_fw_block(struct i2c_client *client,
  399. const u8 *page, u16 checksum, int idx)
  400. {
  401. struct device *dev = &client->dev;
  402. u8 page_store[ETP_FW_PAGE_SIZE + 4];
  403. u8 val[3];
  404. u16 result;
  405. int ret, error;
  406. page_store[0] = ETP_I2C_IAP_REG_L;
  407. page_store[1] = ETP_I2C_IAP_REG_H;
  408. memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
  409. /* recode checksum at last two bytes */
  410. put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
  411. ret = i2c_master_send(client, page_store, sizeof(page_store));
  412. if (ret != sizeof(page_store)) {
  413. error = ret < 0 ? ret : -EIO;
  414. dev_err(dev, "Failed to write page %d: %d\n", idx, error);
  415. return error;
  416. }
  417. /* Wait for F/W to update one page ROM data. */
  418. msleep(20);
  419. error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
  420. if (error) {
  421. dev_err(dev, "Failed to read IAP write result: %d\n", error);
  422. return error;
  423. }
  424. result = le16_to_cpup((__le16 *)val);
  425. if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
  426. dev_err(dev, "IAP reports failed write: %04hx\n",
  427. result);
  428. return -EIO;
  429. }
  430. return 0;
  431. }
  432. static int elan_i2c_finish_fw_update(struct i2c_client *client,
  433. struct completion *completion)
  434. {
  435. struct device *dev = &client->dev;
  436. long ret;
  437. int error;
  438. int len;
  439. u8 buffer[ETP_I2C_INF_LENGTH];
  440. reinit_completion(completion);
  441. enable_irq(client->irq);
  442. error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
  443. if (!error)
  444. ret = wait_for_completion_interruptible_timeout(completion,
  445. msecs_to_jiffies(300));
  446. disable_irq(client->irq);
  447. if (error) {
  448. dev_err(dev, "device reset failed: %d\n", error);
  449. return error;
  450. } else if (ret == 0) {
  451. dev_err(dev, "timeout waiting for device reset\n");
  452. return -ETIMEDOUT;
  453. } else if (ret < 0) {
  454. error = ret;
  455. dev_err(dev, "error waiting for device reset: %d\n", error);
  456. return error;
  457. }
  458. len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
  459. if (len != ETP_I2C_INF_LENGTH) {
  460. error = len < 0 ? len : -EIO;
  461. dev_err(dev, "failed to read INT signal: %d (%d)\n",
  462. error, len);
  463. return error;
  464. }
  465. return 0;
  466. }
  467. static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
  468. {
  469. int len;
  470. len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
  471. if (len < 0) {
  472. dev_err(&client->dev, "failed to read report data: %d\n", len);
  473. return len;
  474. }
  475. if (len != ETP_I2C_REPORT_LEN) {
  476. dev_err(&client->dev,
  477. "wrong report length (%d vs %d expected)\n",
  478. len, ETP_I2C_REPORT_LEN);
  479. return -EIO;
  480. }
  481. return 0;
  482. }
  483. const struct elan_transport_ops elan_i2c_ops = {
  484. .initialize = elan_i2c_initialize,
  485. .sleep_control = elan_i2c_sleep_control,
  486. .power_control = elan_i2c_power_control,
  487. .set_mode = elan_i2c_set_mode,
  488. .calibrate = elan_i2c_calibrate,
  489. .calibrate_result = elan_i2c_calibrate_result,
  490. .get_baseline_data = elan_i2c_get_baseline_data,
  491. .get_version = elan_i2c_get_version,
  492. .get_sm_version = elan_i2c_get_sm_version,
  493. .get_product_id = elan_i2c_get_product_id,
  494. .get_checksum = elan_i2c_get_checksum,
  495. .get_max = elan_i2c_get_max,
  496. .get_resolution = elan_i2c_get_resolution,
  497. .get_num_traces = elan_i2c_get_num_traces,
  498. .iap_get_mode = elan_i2c_iap_get_mode,
  499. .iap_reset = elan_i2c_iap_reset,
  500. .prepare_fw_update = elan_i2c_prepare_fw_update,
  501. .write_fw_block = elan_i2c_write_fw_block,
  502. .finish_fw_update = elan_i2c_finish_fw_update,
  503. .get_report = elan_i2c_get_report,
  504. };