elan_i2c_smbus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Elan I2C/SMBus Touchpad driver - SMBus 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/delay.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include "elan_i2c.h"
  24. /* Elan SMbus commands */
  25. #define ETP_SMBUS_IAP_CMD 0x00
  26. #define ETP_SMBUS_ENABLE_TP 0x20
  27. #define ETP_SMBUS_SLEEP_CMD 0x21
  28. #define ETP_SMBUS_IAP_PASSWORD_WRITE 0x29
  29. #define ETP_SMBUS_IAP_PASSWORD_READ 0x80
  30. #define ETP_SMBUS_WRITE_FW_BLOCK 0x2A
  31. #define ETP_SMBUS_IAP_RESET_CMD 0x2B
  32. #define ETP_SMBUS_RANGE_CMD 0xA0
  33. #define ETP_SMBUS_FW_VERSION_CMD 0xA1
  34. #define ETP_SMBUS_XY_TRACENUM_CMD 0xA2
  35. #define ETP_SMBUS_SM_VERSION_CMD 0xA3
  36. #define ETP_SMBUS_UNIQUEID_CMD 0xA3
  37. #define ETP_SMBUS_RESOLUTION_CMD 0xA4
  38. #define ETP_SMBUS_HELLOPACKET_CMD 0xA7
  39. #define ETP_SMBUS_PACKET_QUERY 0xA8
  40. #define ETP_SMBUS_IAP_VERSION_CMD 0xAC
  41. #define ETP_SMBUS_IAP_CTRL_CMD 0xAD
  42. #define ETP_SMBUS_IAP_CHECKSUM_CMD 0xAE
  43. #define ETP_SMBUS_FW_CHECKSUM_CMD 0xAF
  44. #define ETP_SMBUS_MAX_BASELINE_CMD 0xC3
  45. #define ETP_SMBUS_MIN_BASELINE_CMD 0xC4
  46. #define ETP_SMBUS_CALIBRATE_QUERY 0xC5
  47. #define ETP_SMBUS_REPORT_LEN 32
  48. #define ETP_SMBUS_REPORT_OFFSET 2
  49. #define ETP_SMBUS_HELLOPACKET_LEN 5
  50. #define ETP_SMBUS_IAP_PASSWORD 0x1234
  51. #define ETP_SMBUS_IAP_MODE_ON (1 << 6)
  52. static int elan_smbus_initialize(struct i2c_client *client)
  53. {
  54. u8 check[ETP_SMBUS_HELLOPACKET_LEN] = { 0x55, 0x55, 0x55, 0x55, 0x55 };
  55. u8 values[ETP_SMBUS_HELLOPACKET_LEN] = { 0, 0, 0, 0, 0 };
  56. int len, error;
  57. /* Get hello packet */
  58. len = i2c_smbus_read_block_data(client,
  59. ETP_SMBUS_HELLOPACKET_CMD, values);
  60. if (len != ETP_SMBUS_HELLOPACKET_LEN) {
  61. dev_err(&client->dev, "hello packet length fail: %d\n", len);
  62. error = len < 0 ? len : -EIO;
  63. return error;
  64. }
  65. /* compare hello packet */
  66. if (memcmp(values, check, ETP_SMBUS_HELLOPACKET_LEN)) {
  67. dev_err(&client->dev, "hello packet fail [%*px]\n",
  68. ETP_SMBUS_HELLOPACKET_LEN, values);
  69. return -ENXIO;
  70. }
  71. /* enable tp */
  72. error = i2c_smbus_write_byte(client, ETP_SMBUS_ENABLE_TP);
  73. if (error) {
  74. dev_err(&client->dev, "failed to enable touchpad: %d\n", error);
  75. return error;
  76. }
  77. return 0;
  78. }
  79. static int elan_smbus_set_mode(struct i2c_client *client, u8 mode)
  80. {
  81. u8 cmd[4] = { 0x00, 0x07, 0x00, mode };
  82. return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  83. sizeof(cmd), cmd);
  84. }
  85. static int elan_smbus_sleep_control(struct i2c_client *client, bool sleep)
  86. {
  87. if (sleep)
  88. return i2c_smbus_write_byte(client, ETP_SMBUS_SLEEP_CMD);
  89. else
  90. return 0; /* XXX should we send ETP_SMBUS_ENABLE_TP here? */
  91. }
  92. static int elan_smbus_power_control(struct i2c_client *client, bool enable)
  93. {
  94. return 0; /* A no-op */
  95. }
  96. static int elan_smbus_calibrate(struct i2c_client *client)
  97. {
  98. u8 cmd[4] = { 0x00, 0x08, 0x00, 0x01 };
  99. return i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  100. sizeof(cmd), cmd);
  101. }
  102. static int elan_smbus_calibrate_result(struct i2c_client *client, u8 *val)
  103. {
  104. int error;
  105. error = i2c_smbus_read_block_data(client,
  106. ETP_SMBUS_CALIBRATE_QUERY, val);
  107. if (error < 0)
  108. return error;
  109. return 0;
  110. }
  111. static int elan_smbus_get_baseline_data(struct i2c_client *client,
  112. bool max_baseline, u8 *value)
  113. {
  114. int error;
  115. u8 val[3];
  116. error = i2c_smbus_read_block_data(client,
  117. max_baseline ?
  118. ETP_SMBUS_MAX_BASELINE_CMD :
  119. ETP_SMBUS_MIN_BASELINE_CMD,
  120. val);
  121. if (error < 0)
  122. return error;
  123. *value = be16_to_cpup((__be16 *)val);
  124. return 0;
  125. }
  126. static int elan_smbus_get_version(struct i2c_client *client,
  127. bool iap, u8 *version)
  128. {
  129. int error;
  130. u8 val[3];
  131. error = i2c_smbus_read_block_data(client,
  132. iap ? ETP_SMBUS_IAP_VERSION_CMD :
  133. ETP_SMBUS_FW_VERSION_CMD,
  134. val);
  135. if (error < 0) {
  136. dev_err(&client->dev, "failed to get %s version: %d\n",
  137. iap ? "IAP" : "FW", error);
  138. return error;
  139. }
  140. *version = val[2];
  141. return 0;
  142. }
  143. static int elan_smbus_get_sm_version(struct i2c_client *client, u8 *version)
  144. {
  145. int error;
  146. u8 val[3];
  147. error = i2c_smbus_read_block_data(client,
  148. ETP_SMBUS_SM_VERSION_CMD, val);
  149. if (error < 0) {
  150. dev_err(&client->dev, "failed to get SM version: %d\n", error);
  151. return error;
  152. }
  153. *version = val[0]; /* XXX Why 0 and not 2 as in IAP/FW versions? */
  154. return 0;
  155. }
  156. static int elan_smbus_get_product_id(struct i2c_client *client, u8 *id)
  157. {
  158. int error;
  159. u8 val[3];
  160. error = i2c_smbus_read_block_data(client,
  161. ETP_SMBUS_UNIQUEID_CMD, val);
  162. if (error < 0) {
  163. dev_err(&client->dev, "failed to get product ID: %d\n", error);
  164. return error;
  165. }
  166. *id = val[1];
  167. return 0;
  168. }
  169. static int elan_smbus_get_checksum(struct i2c_client *client,
  170. bool iap, u16 *csum)
  171. {
  172. int error;
  173. u8 val[3];
  174. error = i2c_smbus_read_block_data(client,
  175. iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
  176. ETP_SMBUS_IAP_CHECKSUM_CMD,
  177. val);
  178. if (error < 0) {
  179. dev_err(&client->dev, "failed to get %s checksum: %d\n",
  180. iap ? "IAP" : "FW", error);
  181. return error;
  182. }
  183. *csum = be16_to_cpup((__be16 *)val);
  184. return 0;
  185. }
  186. static int elan_smbus_get_max(struct i2c_client *client,
  187. unsigned int *max_x, unsigned int *max_y)
  188. {
  189. int error;
  190. u8 val[3];
  191. error = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
  192. if (error) {
  193. dev_err(&client->dev, "failed to get dimensions: %d\n", error);
  194. return error;
  195. }
  196. *max_x = (0x0f & val[0]) << 8 | val[1];
  197. *max_y = (0xf0 & val[0]) << 4 | val[2];
  198. return 0;
  199. }
  200. static int elan_smbus_get_resolution(struct i2c_client *client,
  201. u8 *hw_res_x, u8 *hw_res_y)
  202. {
  203. int error;
  204. u8 val[3];
  205. error = i2c_smbus_read_block_data(client,
  206. ETP_SMBUS_RESOLUTION_CMD, val);
  207. if (error) {
  208. dev_err(&client->dev, "failed to get resolution: %d\n", error);
  209. return error;
  210. }
  211. *hw_res_x = val[1] & 0x0F;
  212. *hw_res_y = (val[1] & 0xF0) >> 4;
  213. return 0;
  214. }
  215. static int elan_smbus_get_num_traces(struct i2c_client *client,
  216. unsigned int *x_traces,
  217. unsigned int *y_traces)
  218. {
  219. int error;
  220. u8 val[3];
  221. error = i2c_smbus_read_block_data(client,
  222. ETP_SMBUS_XY_TRACENUM_CMD, val);
  223. if (error) {
  224. dev_err(&client->dev, "failed to get trace info: %d\n", error);
  225. return error;
  226. }
  227. *x_traces = val[1] - 1;
  228. *y_traces = val[2] - 1;
  229. return 0;
  230. }
  231. static int elan_smbus_iap_get_mode(struct i2c_client *client,
  232. enum tp_mode *mode)
  233. {
  234. int error;
  235. u16 constant;
  236. u8 val[3];
  237. error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
  238. if (error < 0) {
  239. dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
  240. error);
  241. return error;
  242. }
  243. constant = be16_to_cpup((__be16 *)val);
  244. dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
  245. *mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
  246. return 0;
  247. }
  248. static int elan_smbus_iap_reset(struct i2c_client *client)
  249. {
  250. int error;
  251. error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
  252. if (error) {
  253. dev_err(&client->dev, "cannot reset IC: %d\n", error);
  254. return error;
  255. }
  256. return 0;
  257. }
  258. static int elan_smbus_set_flash_key(struct i2c_client *client)
  259. {
  260. int error;
  261. u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
  262. error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  263. sizeof(cmd), cmd);
  264. if (error) {
  265. dev_err(&client->dev, "cannot set flash key: %d\n", error);
  266. return error;
  267. }
  268. return 0;
  269. }
  270. static int elan_smbus_prepare_fw_update(struct i2c_client *client)
  271. {
  272. struct device *dev = &client->dev;
  273. int len;
  274. int error;
  275. enum tp_mode mode;
  276. u8 val[3];
  277. u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
  278. u16 password;
  279. /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
  280. error = elan_smbus_iap_get_mode(client, &mode);
  281. if (error)
  282. return error;
  283. if (mode == MAIN_MODE) {
  284. /* set flash key */
  285. error = elan_smbus_set_flash_key(client);
  286. if (error)
  287. return error;
  288. /* write iap password */
  289. if (i2c_smbus_write_byte(client,
  290. ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
  291. dev_err(dev, "cannot write iap password\n");
  292. return -EIO;
  293. }
  294. error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  295. sizeof(cmd), cmd);
  296. if (error) {
  297. dev_err(dev, "failed to write iap password: %d\n",
  298. error);
  299. return error;
  300. }
  301. /*
  302. * Read back password to make sure we enabled flash
  303. * successfully.
  304. */
  305. len = i2c_smbus_read_block_data(client,
  306. ETP_SMBUS_IAP_PASSWORD_READ,
  307. val);
  308. if (len < sizeof(u16)) {
  309. error = len < 0 ? len : -EIO;
  310. dev_err(dev, "failed to read iap password: %d\n",
  311. error);
  312. return error;
  313. }
  314. password = be16_to_cpup((__be16 *)val);
  315. if (password != ETP_SMBUS_IAP_PASSWORD) {
  316. dev_err(dev, "wrong iap password = 0x%X\n", password);
  317. return -EIO;
  318. }
  319. /* Wait 30ms for MAIN_MODE change to IAP_MODE */
  320. msleep(30);
  321. }
  322. error = elan_smbus_set_flash_key(client);
  323. if (error)
  324. return error;
  325. /* Reset IC */
  326. error = elan_smbus_iap_reset(client);
  327. if (error)
  328. return error;
  329. return 0;
  330. }
  331. static int elan_smbus_write_fw_block(struct i2c_client *client,
  332. const u8 *page, u16 checksum, int idx)
  333. {
  334. struct device *dev = &client->dev;
  335. int error;
  336. u16 result;
  337. u8 val[3];
  338. /*
  339. * Due to the limitation of smbus protocol limiting
  340. * transfer to 32 bytes at a time, we must split block
  341. * in 2 transfers.
  342. */
  343. error = i2c_smbus_write_block_data(client,
  344. ETP_SMBUS_WRITE_FW_BLOCK,
  345. ETP_FW_PAGE_SIZE / 2,
  346. page);
  347. if (error) {
  348. dev_err(dev, "Failed to write page %d (part %d): %d\n",
  349. idx, 1, error);
  350. return error;
  351. }
  352. error = i2c_smbus_write_block_data(client,
  353. ETP_SMBUS_WRITE_FW_BLOCK,
  354. ETP_FW_PAGE_SIZE / 2,
  355. page + ETP_FW_PAGE_SIZE / 2);
  356. if (error) {
  357. dev_err(dev, "Failed to write page %d (part %d): %d\n",
  358. idx, 2, error);
  359. return error;
  360. }
  361. /* Wait for F/W to update one page ROM data. */
  362. usleep_range(8000, 10000);
  363. error = i2c_smbus_read_block_data(client,
  364. ETP_SMBUS_IAP_CTRL_CMD, val);
  365. if (error < 0) {
  366. dev_err(dev, "Failed to read IAP write result: %d\n",
  367. error);
  368. return error;
  369. }
  370. result = be16_to_cpup((__be16 *)val);
  371. if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
  372. dev_err(dev, "IAP reports failed write: %04hx\n",
  373. result);
  374. return -EIO;
  375. }
  376. return 0;
  377. }
  378. static int elan_smbus_get_report(struct i2c_client *client, u8 *report)
  379. {
  380. int len;
  381. len = i2c_smbus_read_block_data(client,
  382. ETP_SMBUS_PACKET_QUERY,
  383. &report[ETP_SMBUS_REPORT_OFFSET]);
  384. if (len < 0) {
  385. dev_err(&client->dev, "failed to read report data: %d\n", len);
  386. return len;
  387. }
  388. if (len != ETP_SMBUS_REPORT_LEN) {
  389. dev_err(&client->dev,
  390. "wrong report length (%d vs %d expected)\n",
  391. len, ETP_SMBUS_REPORT_LEN);
  392. return -EIO;
  393. }
  394. return 0;
  395. }
  396. static int elan_smbus_finish_fw_update(struct i2c_client *client,
  397. struct completion *fw_completion)
  398. {
  399. /* No special handling unlike I2C transport */
  400. return 0;
  401. }
  402. const struct elan_transport_ops elan_smbus_ops = {
  403. .initialize = elan_smbus_initialize,
  404. .sleep_control = elan_smbus_sleep_control,
  405. .power_control = elan_smbus_power_control,
  406. .set_mode = elan_smbus_set_mode,
  407. .calibrate = elan_smbus_calibrate,
  408. .calibrate_result = elan_smbus_calibrate_result,
  409. .get_baseline_data = elan_smbus_get_baseline_data,
  410. .get_version = elan_smbus_get_version,
  411. .get_sm_version = elan_smbus_get_sm_version,
  412. .get_product_id = elan_smbus_get_product_id,
  413. .get_checksum = elan_smbus_get_checksum,
  414. .get_max = elan_smbus_get_max,
  415. .get_resolution = elan_smbus_get_resolution,
  416. .get_num_traces = elan_smbus_get_num_traces,
  417. .iap_get_mode = elan_smbus_iap_get_mode,
  418. .iap_reset = elan_smbus_iap_reset,
  419. .prepare_fw_update = elan_smbus_prepare_fw_update,
  420. .write_fw_block = elan_smbus_write_fw_block,
  421. .finish_fw_update = elan_smbus_finish_fw_update,
  422. .get_report = elan_smbus_get_report,
  423. };