elan_i2c_smbus.c 12 KB

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