elan_i2c_smbus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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[I2C_SMBUS_BLOCK_MAX] = {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. u8 buf[I2C_SMBUS_BLOCK_MAX] = {0};
  105. BUILD_BUG_ON(ETP_CALIBRATE_MAX_LEN > sizeof(buf));
  106. error = i2c_smbus_read_block_data(client,
  107. ETP_SMBUS_CALIBRATE_QUERY, buf);
  108. if (error < 0)
  109. return error;
  110. memcpy(val, buf, ETP_CALIBRATE_MAX_LEN);
  111. return 0;
  112. }
  113. static int elan_smbus_get_baseline_data(struct i2c_client *client,
  114. bool max_baseline, u8 *value)
  115. {
  116. int error;
  117. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  118. error = i2c_smbus_read_block_data(client,
  119. max_baseline ?
  120. ETP_SMBUS_MAX_BASELINE_CMD :
  121. ETP_SMBUS_MIN_BASELINE_CMD,
  122. val);
  123. if (error < 0)
  124. return error;
  125. *value = be16_to_cpup((__be16 *)val);
  126. return 0;
  127. }
  128. static int elan_smbus_get_version(struct i2c_client *client,
  129. bool iap, u8 *version)
  130. {
  131. int error;
  132. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  133. error = i2c_smbus_read_block_data(client,
  134. iap ? ETP_SMBUS_IAP_VERSION_CMD :
  135. ETP_SMBUS_FW_VERSION_CMD,
  136. val);
  137. if (error < 0) {
  138. dev_err(&client->dev, "failed to get %s version: %d\n",
  139. iap ? "IAP" : "FW", error);
  140. return error;
  141. }
  142. *version = val[2];
  143. return 0;
  144. }
  145. static int elan_smbus_get_sm_version(struct i2c_client *client,
  146. u8 *ic_type, u8 *version)
  147. {
  148. int error;
  149. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  150. error = i2c_smbus_read_block_data(client,
  151. ETP_SMBUS_SM_VERSION_CMD, val);
  152. if (error < 0) {
  153. dev_err(&client->dev, "failed to get SM version: %d\n", error);
  154. return error;
  155. }
  156. *version = val[0];
  157. *ic_type = val[1];
  158. return 0;
  159. }
  160. static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id)
  161. {
  162. int error;
  163. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  164. error = i2c_smbus_read_block_data(client,
  165. ETP_SMBUS_UNIQUEID_CMD, val);
  166. if (error < 0) {
  167. dev_err(&client->dev, "failed to get product ID: %d\n", error);
  168. return error;
  169. }
  170. *id = be16_to_cpup((__be16 *)val);
  171. return 0;
  172. }
  173. static int elan_smbus_get_checksum(struct i2c_client *client,
  174. bool iap, u16 *csum)
  175. {
  176. int error;
  177. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  178. error = i2c_smbus_read_block_data(client,
  179. iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
  180. ETP_SMBUS_IAP_CHECKSUM_CMD,
  181. val);
  182. if (error < 0) {
  183. dev_err(&client->dev, "failed to get %s checksum: %d\n",
  184. iap ? "IAP" : "FW", error);
  185. return error;
  186. }
  187. *csum = be16_to_cpup((__be16 *)val);
  188. return 0;
  189. }
  190. static int elan_smbus_get_max(struct i2c_client *client,
  191. unsigned int *max_x, unsigned int *max_y)
  192. {
  193. int error;
  194. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  195. error = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
  196. if (error) {
  197. dev_err(&client->dev, "failed to get dimensions: %d\n", error);
  198. return error;
  199. }
  200. *max_x = (0x0f & val[0]) << 8 | val[1];
  201. *max_y = (0xf0 & val[0]) << 4 | val[2];
  202. return 0;
  203. }
  204. static int elan_smbus_get_resolution(struct i2c_client *client,
  205. u8 *hw_res_x, u8 *hw_res_y)
  206. {
  207. int error;
  208. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  209. error = i2c_smbus_read_block_data(client,
  210. ETP_SMBUS_RESOLUTION_CMD, val);
  211. if (error) {
  212. dev_err(&client->dev, "failed to get resolution: %d\n", error);
  213. return error;
  214. }
  215. *hw_res_x = val[1] & 0x0F;
  216. *hw_res_y = (val[1] & 0xF0) >> 4;
  217. return 0;
  218. }
  219. static int elan_smbus_get_num_traces(struct i2c_client *client,
  220. unsigned int *x_traces,
  221. unsigned int *y_traces)
  222. {
  223. int error;
  224. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  225. error = i2c_smbus_read_block_data(client,
  226. ETP_SMBUS_XY_TRACENUM_CMD, val);
  227. if (error) {
  228. dev_err(&client->dev, "failed to get trace info: %d\n", error);
  229. return error;
  230. }
  231. *x_traces = val[1];
  232. *y_traces = val[2];
  233. return 0;
  234. }
  235. static int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
  236. int *adjustment)
  237. {
  238. *adjustment = ETP_PRESSURE_OFFSET;
  239. return 0;
  240. }
  241. static int elan_smbus_iap_get_mode(struct i2c_client *client,
  242. enum tp_mode *mode)
  243. {
  244. int error;
  245. u16 constant;
  246. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  247. error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
  248. if (error < 0) {
  249. dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
  250. error);
  251. return error;
  252. }
  253. constant = be16_to_cpup((__be16 *)val);
  254. dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
  255. *mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
  256. return 0;
  257. }
  258. static int elan_smbus_iap_reset(struct i2c_client *client)
  259. {
  260. int error;
  261. error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
  262. if (error) {
  263. dev_err(&client->dev, "cannot reset IC: %d\n", error);
  264. return error;
  265. }
  266. return 0;
  267. }
  268. static int elan_smbus_set_flash_key(struct i2c_client *client)
  269. {
  270. int error;
  271. u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
  272. error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  273. sizeof(cmd), cmd);
  274. if (error) {
  275. dev_err(&client->dev, "cannot set flash key: %d\n", error);
  276. return error;
  277. }
  278. return 0;
  279. }
  280. static int elan_smbus_prepare_fw_update(struct i2c_client *client)
  281. {
  282. struct device *dev = &client->dev;
  283. int len;
  284. int error;
  285. enum tp_mode mode;
  286. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  287. u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
  288. u16 password;
  289. /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
  290. error = elan_smbus_iap_get_mode(client, &mode);
  291. if (error)
  292. return error;
  293. if (mode == MAIN_MODE) {
  294. /* set flash key */
  295. error = elan_smbus_set_flash_key(client);
  296. if (error)
  297. return error;
  298. /* write iap password */
  299. if (i2c_smbus_write_byte(client,
  300. ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
  301. dev_err(dev, "cannot write iap password\n");
  302. return -EIO;
  303. }
  304. error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  305. sizeof(cmd), cmd);
  306. if (error) {
  307. dev_err(dev, "failed to write iap password: %d\n",
  308. error);
  309. return error;
  310. }
  311. /*
  312. * Read back password to make sure we enabled flash
  313. * successfully.
  314. */
  315. len = i2c_smbus_read_block_data(client,
  316. ETP_SMBUS_IAP_PASSWORD_READ,
  317. val);
  318. if (len < sizeof(u16)) {
  319. error = len < 0 ? len : -EIO;
  320. dev_err(dev, "failed to read iap password: %d\n",
  321. error);
  322. return error;
  323. }
  324. password = be16_to_cpup((__be16 *)val);
  325. if (password != ETP_SMBUS_IAP_PASSWORD) {
  326. dev_err(dev, "wrong iap password = 0x%X\n", password);
  327. return -EIO;
  328. }
  329. /* Wait 30ms for MAIN_MODE change to IAP_MODE */
  330. msleep(30);
  331. }
  332. error = elan_smbus_set_flash_key(client);
  333. if (error)
  334. return error;
  335. /* Reset IC */
  336. error = elan_smbus_iap_reset(client);
  337. if (error)
  338. return error;
  339. return 0;
  340. }
  341. static int elan_smbus_write_fw_block(struct i2c_client *client,
  342. const u8 *page, u16 checksum, int idx)
  343. {
  344. struct device *dev = &client->dev;
  345. int error;
  346. u16 result;
  347. u8 val[I2C_SMBUS_BLOCK_MAX] = {0};
  348. /*
  349. * Due to the limitation of smbus protocol limiting
  350. * transfer to 32 bytes at a time, we must split block
  351. * in 2 transfers.
  352. */
  353. error = i2c_smbus_write_block_data(client,
  354. ETP_SMBUS_WRITE_FW_BLOCK,
  355. ETP_FW_PAGE_SIZE / 2,
  356. page);
  357. if (error) {
  358. dev_err(dev, "Failed to write page %d (part %d): %d\n",
  359. idx, 1, error);
  360. return error;
  361. }
  362. error = i2c_smbus_write_block_data(client,
  363. ETP_SMBUS_WRITE_FW_BLOCK,
  364. ETP_FW_PAGE_SIZE / 2,
  365. page + ETP_FW_PAGE_SIZE / 2);
  366. if (error) {
  367. dev_err(dev, "Failed to write page %d (part %d): %d\n",
  368. idx, 2, error);
  369. return error;
  370. }
  371. /* Wait for F/W to update one page ROM data. */
  372. usleep_range(8000, 10000);
  373. error = i2c_smbus_read_block_data(client,
  374. ETP_SMBUS_IAP_CTRL_CMD, val);
  375. if (error < 0) {
  376. dev_err(dev, "Failed to read IAP write result: %d\n",
  377. error);
  378. return error;
  379. }
  380. result = be16_to_cpup((__be16 *)val);
  381. if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
  382. dev_err(dev, "IAP reports failed write: %04hx\n",
  383. result);
  384. return -EIO;
  385. }
  386. return 0;
  387. }
  388. static int elan_smbus_get_report(struct i2c_client *client, u8 *report)
  389. {
  390. int len;
  391. BUILD_BUG_ON(I2C_SMBUS_BLOCK_MAX > ETP_SMBUS_REPORT_LEN);
  392. len = i2c_smbus_read_block_data(client,
  393. ETP_SMBUS_PACKET_QUERY,
  394. &report[ETP_SMBUS_REPORT_OFFSET]);
  395. if (len < 0) {
  396. dev_err(&client->dev, "failed to read report data: %d\n", len);
  397. return len;
  398. }
  399. if (len != ETP_SMBUS_REPORT_LEN) {
  400. dev_err(&client->dev,
  401. "wrong report length (%d vs %d expected)\n",
  402. len, ETP_SMBUS_REPORT_LEN);
  403. return -EIO;
  404. }
  405. return 0;
  406. }
  407. static int elan_smbus_finish_fw_update(struct i2c_client *client,
  408. struct completion *fw_completion)
  409. {
  410. /* No special handling unlike I2C transport */
  411. return 0;
  412. }
  413. const struct elan_transport_ops elan_smbus_ops = {
  414. .initialize = elan_smbus_initialize,
  415. .sleep_control = elan_smbus_sleep_control,
  416. .power_control = elan_smbus_power_control,
  417. .set_mode = elan_smbus_set_mode,
  418. .calibrate = elan_smbus_calibrate,
  419. .calibrate_result = elan_smbus_calibrate_result,
  420. .get_baseline_data = elan_smbus_get_baseline_data,
  421. .get_version = elan_smbus_get_version,
  422. .get_sm_version = elan_smbus_get_sm_version,
  423. .get_product_id = elan_smbus_get_product_id,
  424. .get_checksum = elan_smbus_get_checksum,
  425. .get_pressure_adjustment = elan_smbus_get_pressure_adjustment,
  426. .get_max = elan_smbus_get_max,
  427. .get_resolution = elan_smbus_get_resolution,
  428. .get_num_traces = elan_smbus_get_num_traces,
  429. .iap_get_mode = elan_smbus_iap_get_mode,
  430. .iap_reset = elan_smbus_iap_reset,
  431. .prepare_fw_update = elan_smbus_prepare_fw_update,
  432. .write_fw_block = elan_smbus_write_fw_block,
  433. .finish_fw_update = elan_smbus_finish_fw_update,
  434. .get_report = elan_smbus_get_report,
  435. };