elan_i2c_i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. *
  8. * Based on cyapa driver:
  9. * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
  10. * copyright (c) 2011-2012 Google, Inc.
  11. *
  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.
  15. *
  16. * Trademarks are the property of their respective owners.
  17. */
  18. #include <linux/completion.h>
  19. #include <linux/delay.h>
  20. #include <linux/i2c.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <asm/unaligned.h>
  26. #include "elan_i2c.h"
  27. /* Elan i2c commands */
  28. #define ETP_I2C_RESET 0x0100
  29. #define ETP_I2C_WAKE_UP 0x0800
  30. #define ETP_I2C_SLEEP 0x0801
  31. #define ETP_I2C_DESC_CMD 0x0001
  32. #define ETP_I2C_REPORT_DESC_CMD 0x0002
  33. #define ETP_I2C_STAND_CMD 0x0005
  34. #define ETP_I2C_UNIQUEID_CMD 0x0101
  35. #define ETP_I2C_FW_VERSION_CMD 0x0102
  36. #define ETP_I2C_SM_VERSION_CMD 0x0103
  37. #define ETP_I2C_XY_TRACENUM_CMD 0x0105
  38. #define ETP_I2C_MAX_X_AXIS_CMD 0x0106
  39. #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107
  40. #define ETP_I2C_RESOLUTION_CMD 0x0108
  41. #define ETP_I2C_PRESSURE_CMD 0x010A
  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. if (ret != 1) {
  111. if (ret >= 0)
  112. ret = -EIO;
  113. dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n",
  114. reg, ret);
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. static int elan_i2c_initialize(struct i2c_client *client)
  120. {
  121. struct device *dev = &client->dev;
  122. int error;
  123. u8 val[256];
  124. error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
  125. if (error) {
  126. dev_err(dev, "device reset failed: %d\n", error);
  127. return error;
  128. }
  129. /* Wait for the device to reset */
  130. msleep(100);
  131. /* get reset acknowledgement 0000 */
  132. error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH);
  133. if (error < 0) {
  134. dev_err(dev, "failed to read reset response: %d\n", error);
  135. return error;
  136. }
  137. error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD,
  138. val, ETP_I2C_DESC_LENGTH);
  139. if (error) {
  140. dev_err(dev, "cannot get device descriptor: %d\n", error);
  141. return error;
  142. }
  143. error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD,
  144. val, ETP_I2C_REPORT_DESC_LENGTH);
  145. if (error) {
  146. dev_err(dev, "fetching report descriptor failed.: %d\n", error);
  147. return error;
  148. }
  149. return 0;
  150. }
  151. static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep)
  152. {
  153. return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD,
  154. sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP);
  155. }
  156. static int elan_i2c_power_control(struct i2c_client *client, bool enable)
  157. {
  158. u8 val[2];
  159. u16 reg;
  160. int error;
  161. error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val);
  162. if (error) {
  163. dev_err(&client->dev,
  164. "failed to read current power state: %d\n",
  165. error);
  166. return error;
  167. }
  168. reg = le16_to_cpup((__le16 *)val);
  169. if (enable)
  170. reg &= ~ETP_DISABLE_POWER;
  171. else
  172. reg |= ETP_DISABLE_POWER;
  173. error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg);
  174. if (error) {
  175. dev_err(&client->dev,
  176. "failed to write current power state: %d\n",
  177. error);
  178. return error;
  179. }
  180. return 0;
  181. }
  182. static int elan_i2c_set_mode(struct i2c_client *client, u8 mode)
  183. {
  184. return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode);
  185. }
  186. static int elan_i2c_calibrate(struct i2c_client *client)
  187. {
  188. return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1);
  189. }
  190. static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val)
  191. {
  192. return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1);
  193. }
  194. static int elan_i2c_get_baseline_data(struct i2c_client *client,
  195. bool max_baseline, u8 *value)
  196. {
  197. int error;
  198. u8 val[3];
  199. error = elan_i2c_read_cmd(client,
  200. max_baseline ? ETP_I2C_MAX_BASELINE_CMD :
  201. ETP_I2C_MIN_BASELINE_CMD,
  202. val);
  203. if (error)
  204. return error;
  205. *value = le16_to_cpup((__le16 *)val);
  206. return 0;
  207. }
  208. static int elan_i2c_get_version(struct i2c_client *client,
  209. bool iap, u8 *version)
  210. {
  211. int error;
  212. u8 val[3];
  213. error = elan_i2c_read_cmd(client,
  214. iap ? ETP_I2C_IAP_VERSION_CMD :
  215. ETP_I2C_FW_VERSION_CMD,
  216. val);
  217. if (error) {
  218. dev_err(&client->dev, "failed to get %s version: %d\n",
  219. iap ? "IAP" : "FW", error);
  220. return error;
  221. }
  222. *version = val[0];
  223. return 0;
  224. }
  225. static int elan_i2c_get_sm_version(struct i2c_client *client,
  226. u8 *ic_type, u8 *version)
  227. {
  228. int error;
  229. u8 val[3];
  230. error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val);
  231. if (error) {
  232. dev_err(&client->dev, "failed to get SM version: %d\n", error);
  233. return error;
  234. }
  235. *version = val[0];
  236. *ic_type = val[1];
  237. return 0;
  238. }
  239. static int elan_i2c_get_product_id(struct i2c_client *client, u16 *id)
  240. {
  241. int error;
  242. u8 val[3];
  243. error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val);
  244. if (error) {
  245. dev_err(&client->dev, "failed to get product ID: %d\n", error);
  246. return error;
  247. }
  248. *id = le16_to_cpup((__le16 *)val);
  249. return 0;
  250. }
  251. static int elan_i2c_get_checksum(struct i2c_client *client,
  252. bool iap, u16 *csum)
  253. {
  254. int error;
  255. u8 val[3];
  256. error = elan_i2c_read_cmd(client,
  257. iap ? ETP_I2C_IAP_CHECKSUM_CMD :
  258. ETP_I2C_FW_CHECKSUM_CMD,
  259. val);
  260. if (error) {
  261. dev_err(&client->dev, "failed to get %s checksum: %d\n",
  262. iap ? "IAP" : "FW", error);
  263. return error;
  264. }
  265. *csum = le16_to_cpup((__le16 *)val);
  266. return 0;
  267. }
  268. static int elan_i2c_get_max(struct i2c_client *client,
  269. unsigned int *max_x, unsigned int *max_y)
  270. {
  271. int error;
  272. u8 val[3];
  273. error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val);
  274. if (error) {
  275. dev_err(&client->dev, "failed to get X dimension: %d\n", error);
  276. return error;
  277. }
  278. *max_x = le16_to_cpup((__le16 *)val) & 0x0fff;
  279. error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val);
  280. if (error) {
  281. dev_err(&client->dev, "failed to get Y dimension: %d\n", error);
  282. return error;
  283. }
  284. *max_y = le16_to_cpup((__le16 *)val) & 0x0fff;
  285. return 0;
  286. }
  287. static int elan_i2c_get_resolution(struct i2c_client *client,
  288. u8 *hw_res_x, u8 *hw_res_y)
  289. {
  290. int error;
  291. u8 val[3];
  292. error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val);
  293. if (error) {
  294. dev_err(&client->dev, "failed to get resolution: %d\n", error);
  295. return error;
  296. }
  297. *hw_res_x = val[0];
  298. *hw_res_y = val[1];
  299. return 0;
  300. }
  301. static int elan_i2c_get_num_traces(struct i2c_client *client,
  302. unsigned int *x_traces,
  303. unsigned int *y_traces)
  304. {
  305. int error;
  306. u8 val[3];
  307. error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val);
  308. if (error) {
  309. dev_err(&client->dev, "failed to get trace info: %d\n", error);
  310. return error;
  311. }
  312. *x_traces = val[0];
  313. *y_traces = val[1];
  314. return 0;
  315. }
  316. static int elan_i2c_get_pressure_adjustment(struct i2c_client *client,
  317. int *adjustment)
  318. {
  319. int error;
  320. u8 val[3];
  321. error = elan_i2c_read_cmd(client, ETP_I2C_PRESSURE_CMD, val);
  322. if (error) {
  323. dev_err(&client->dev, "failed to get pressure format: %d\n",
  324. error);
  325. return error;
  326. }
  327. if ((val[0] >> 4) & 0x1)
  328. *adjustment = 0;
  329. else
  330. *adjustment = ETP_PRESSURE_OFFSET;
  331. return 0;
  332. }
  333. static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode)
  334. {
  335. int error;
  336. u16 constant;
  337. u8 val[3];
  338. error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
  339. if (error) {
  340. dev_err(&client->dev,
  341. "failed to read iap control register: %d\n",
  342. error);
  343. return error;
  344. }
  345. constant = le16_to_cpup((__le16 *)val);
  346. dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
  347. *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE;
  348. return 0;
  349. }
  350. static int elan_i2c_iap_reset(struct i2c_client *client)
  351. {
  352. int error;
  353. error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD,
  354. ETP_I2C_IAP_RESET);
  355. if (error) {
  356. dev_err(&client->dev, "cannot reset IC: %d\n", error);
  357. return error;
  358. }
  359. return 0;
  360. }
  361. static int elan_i2c_set_flash_key(struct i2c_client *client)
  362. {
  363. int error;
  364. error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD,
  365. ETP_I2C_IAP_PASSWORD);
  366. if (error) {
  367. dev_err(&client->dev, "cannot set flash key: %d\n", error);
  368. return error;
  369. }
  370. return 0;
  371. }
  372. static int elan_i2c_prepare_fw_update(struct i2c_client *client)
  373. {
  374. struct device *dev = &client->dev;
  375. int error;
  376. enum tp_mode mode;
  377. u8 val[3];
  378. u16 password;
  379. /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
  380. error = elan_i2c_iap_get_mode(client, &mode);
  381. if (error)
  382. return error;
  383. if (mode == IAP_MODE) {
  384. /* Reset IC */
  385. error = elan_i2c_iap_reset(client);
  386. if (error)
  387. return error;
  388. msleep(30);
  389. }
  390. /* Set flash key*/
  391. error = elan_i2c_set_flash_key(client);
  392. if (error)
  393. return error;
  394. /* Wait for F/W IAP initialization */
  395. msleep(mode == MAIN_MODE ? 100 : 30);
  396. /* Check if we are in IAP mode or not */
  397. error = elan_i2c_iap_get_mode(client, &mode);
  398. if (error)
  399. return error;
  400. if (mode == MAIN_MODE) {
  401. dev_err(dev, "wrong mode: %d\n", mode);
  402. return -EIO;
  403. }
  404. /* Set flash key again */
  405. error = elan_i2c_set_flash_key(client);
  406. if (error)
  407. return error;
  408. /* Wait for F/W IAP initialization */
  409. msleep(30);
  410. /* read back to check we actually enabled successfully. */
  411. error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val);
  412. if (error) {
  413. dev_err(dev, "cannot read iap password: %d\n",
  414. error);
  415. return error;
  416. }
  417. password = le16_to_cpup((__le16 *)val);
  418. if (password != ETP_I2C_IAP_PASSWORD) {
  419. dev_err(dev, "wrong iap password: 0x%X\n", password);
  420. return -EIO;
  421. }
  422. return 0;
  423. }
  424. static int elan_i2c_write_fw_block(struct i2c_client *client,
  425. const u8 *page, u16 checksum, int idx)
  426. {
  427. struct device *dev = &client->dev;
  428. u8 page_store[ETP_FW_PAGE_SIZE + 4];
  429. u8 val[3];
  430. u16 result;
  431. int ret, error;
  432. page_store[0] = ETP_I2C_IAP_REG_L;
  433. page_store[1] = ETP_I2C_IAP_REG_H;
  434. memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE);
  435. /* recode checksum at last two bytes */
  436. put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]);
  437. ret = i2c_master_send(client, page_store, sizeof(page_store));
  438. if (ret != sizeof(page_store)) {
  439. error = ret < 0 ? ret : -EIO;
  440. dev_err(dev, "Failed to write page %d: %d\n", idx, error);
  441. return error;
  442. }
  443. /* Wait for F/W to update one page ROM data. */
  444. msleep(20);
  445. error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val);
  446. if (error) {
  447. dev_err(dev, "Failed to read IAP write result: %d\n", error);
  448. return error;
  449. }
  450. result = le16_to_cpup((__le16 *)val);
  451. if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
  452. dev_err(dev, "IAP reports failed write: %04hx\n",
  453. result);
  454. return -EIO;
  455. }
  456. return 0;
  457. }
  458. static int elan_i2c_finish_fw_update(struct i2c_client *client,
  459. struct completion *completion)
  460. {
  461. struct device *dev = &client->dev;
  462. long ret;
  463. int error;
  464. int len;
  465. u8 buffer[ETP_I2C_REPORT_LEN];
  466. len = i2c_master_recv(client, buffer, ETP_I2C_REPORT_LEN);
  467. if (len != ETP_I2C_REPORT_LEN) {
  468. error = len < 0 ? len : -EIO;
  469. dev_warn(dev, "failed to read I2C data after FW WDT reset: %d (%d)\n",
  470. error, len);
  471. }
  472. reinit_completion(completion);
  473. enable_irq(client->irq);
  474. error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET);
  475. if (!error)
  476. ret = wait_for_completion_interruptible_timeout(completion,
  477. msecs_to_jiffies(300));
  478. disable_irq(client->irq);
  479. if (error) {
  480. dev_err(dev, "device reset failed: %d\n", error);
  481. return error;
  482. } else if (ret == 0) {
  483. dev_err(dev, "timeout waiting for device reset\n");
  484. return -ETIMEDOUT;
  485. } else if (ret < 0) {
  486. error = ret;
  487. dev_err(dev, "error waiting for device reset: %d\n", error);
  488. return error;
  489. }
  490. len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH);
  491. if (len != ETP_I2C_INF_LENGTH) {
  492. error = len < 0 ? len : -EIO;
  493. dev_err(dev, "failed to read INT signal: %d (%d)\n",
  494. error, len);
  495. return error;
  496. }
  497. return 0;
  498. }
  499. static int elan_i2c_get_report(struct i2c_client *client, u8 *report)
  500. {
  501. int len;
  502. len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN);
  503. if (len < 0) {
  504. dev_err(&client->dev, "failed to read report data: %d\n", len);
  505. return len;
  506. }
  507. if (len != ETP_I2C_REPORT_LEN) {
  508. dev_err(&client->dev,
  509. "wrong report length (%d vs %d expected)\n",
  510. len, ETP_I2C_REPORT_LEN);
  511. return -EIO;
  512. }
  513. return 0;
  514. }
  515. const struct elan_transport_ops elan_i2c_ops = {
  516. .initialize = elan_i2c_initialize,
  517. .sleep_control = elan_i2c_sleep_control,
  518. .power_control = elan_i2c_power_control,
  519. .set_mode = elan_i2c_set_mode,
  520. .calibrate = elan_i2c_calibrate,
  521. .calibrate_result = elan_i2c_calibrate_result,
  522. .get_baseline_data = elan_i2c_get_baseline_data,
  523. .get_version = elan_i2c_get_version,
  524. .get_sm_version = elan_i2c_get_sm_version,
  525. .get_product_id = elan_i2c_get_product_id,
  526. .get_checksum = elan_i2c_get_checksum,
  527. .get_pressure_adjustment = elan_i2c_get_pressure_adjustment,
  528. .get_max = elan_i2c_get_max,
  529. .get_resolution = elan_i2c_get_resolution,
  530. .get_num_traces = elan_i2c_get_num_traces,
  531. .iap_get_mode = elan_i2c_iap_get_mode,
  532. .iap_reset = elan_i2c_iap_reset,
  533. .prepare_fw_update = elan_i2c_prepare_fw_update,
  534. .write_fw_block = elan_i2c_write_fw_block,
  535. .finish_fw_update = elan_i2c_finish_fw_update,
  536. .get_report = elan_i2c_get_report,
  537. };