PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/input/mouse/elan_i2c_smbus.c

https://github.com/tklauser/linux-nios2
C | 527 lines | 398 code | 92 blank | 37 comment | 38 complexity | 4a333b142ca040dde34f8dee9c348021 MD5 | raw file
  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. u8 *ic_type, 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];
  154. *ic_type = val[1];
  155. return 0;
  156. }
  157. static int elan_smbus_get_product_id(struct i2c_client *client, u16 *id)
  158. {
  159. int error;
  160. u8 val[3];
  161. error = i2c_smbus_read_block_data(client,
  162. ETP_SMBUS_UNIQUEID_CMD, val);
  163. if (error < 0) {
  164. dev_err(&client->dev, "failed to get product ID: %d\n", error);
  165. return error;
  166. }
  167. *id = be16_to_cpup((__be16 *)val);
  168. return 0;
  169. }
  170. static int elan_smbus_get_checksum(struct i2c_client *client,
  171. bool iap, u16 *csum)
  172. {
  173. int error;
  174. u8 val[3];
  175. error = i2c_smbus_read_block_data(client,
  176. iap ? ETP_SMBUS_FW_CHECKSUM_CMD :
  177. ETP_SMBUS_IAP_CHECKSUM_CMD,
  178. val);
  179. if (error < 0) {
  180. dev_err(&client->dev, "failed to get %s checksum: %d\n",
  181. iap ? "IAP" : "FW", error);
  182. return error;
  183. }
  184. *csum = be16_to_cpup((__be16 *)val);
  185. return 0;
  186. }
  187. static int elan_smbus_get_max(struct i2c_client *client,
  188. unsigned int *max_x, unsigned int *max_y)
  189. {
  190. int ret;
  191. int error;
  192. u8 val[3];
  193. ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RANGE_CMD, val);
  194. if (ret != 3) {
  195. error = ret < 0 ? ret : -EIO;
  196. dev_err(&client->dev, "failed to get dimensions: %d\n", error);
  197. return error;
  198. }
  199. *max_x = (0x0f & val[0]) << 8 | val[1];
  200. *max_y = (0xf0 & val[0]) << 4 | val[2];
  201. return 0;
  202. }
  203. static int elan_smbus_get_resolution(struct i2c_client *client,
  204. u8 *hw_res_x, u8 *hw_res_y)
  205. {
  206. int ret;
  207. int error;
  208. u8 val[3];
  209. ret = i2c_smbus_read_block_data(client, ETP_SMBUS_RESOLUTION_CMD, val);
  210. if (ret != 3) {
  211. error = ret < 0 ? ret : -EIO;
  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 ret;
  224. int error;
  225. u8 val[3];
  226. ret = i2c_smbus_read_block_data(client, ETP_SMBUS_XY_TRACENUM_CMD, val);
  227. if (ret != 3) {
  228. error = ret < 0 ? ret : -EIO;
  229. dev_err(&client->dev, "failed to get trace info: %d\n", error);
  230. return error;
  231. }
  232. *x_traces = val[1];
  233. *y_traces = val[2];
  234. return 0;
  235. }
  236. static int elan_smbus_get_pressure_adjustment(struct i2c_client *client,
  237. int *adjustment)
  238. {
  239. *adjustment = ETP_PRESSURE_OFFSET;
  240. return 0;
  241. }
  242. static int elan_smbus_iap_get_mode(struct i2c_client *client,
  243. enum tp_mode *mode)
  244. {
  245. int error;
  246. u16 constant;
  247. u8 val[3];
  248. error = i2c_smbus_read_block_data(client, ETP_SMBUS_IAP_CTRL_CMD, val);
  249. if (error < 0) {
  250. dev_err(&client->dev, "failed to read iap ctrol register: %d\n",
  251. error);
  252. return error;
  253. }
  254. constant = be16_to_cpup((__be16 *)val);
  255. dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant);
  256. *mode = (constant & ETP_SMBUS_IAP_MODE_ON) ? IAP_MODE : MAIN_MODE;
  257. return 0;
  258. }
  259. static int elan_smbus_iap_reset(struct i2c_client *client)
  260. {
  261. int error;
  262. error = i2c_smbus_write_byte(client, ETP_SMBUS_IAP_RESET_CMD);
  263. if (error) {
  264. dev_err(&client->dev, "cannot reset IC: %d\n", error);
  265. return error;
  266. }
  267. return 0;
  268. }
  269. static int elan_smbus_set_flash_key(struct i2c_client *client)
  270. {
  271. int error;
  272. u8 cmd[4] = { 0x00, 0x0B, 0x00, 0x5A };
  273. error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  274. sizeof(cmd), cmd);
  275. if (error) {
  276. dev_err(&client->dev, "cannot set flash key: %d\n", error);
  277. return error;
  278. }
  279. return 0;
  280. }
  281. static int elan_smbus_prepare_fw_update(struct i2c_client *client)
  282. {
  283. struct device *dev = &client->dev;
  284. int len;
  285. int error;
  286. enum tp_mode mode;
  287. u8 val[3];
  288. u8 cmd[4] = {0x0F, 0x78, 0x00, 0x06};
  289. u16 password;
  290. /* Get FW in which mode (IAP_MODE/MAIN_MODE) */
  291. error = elan_smbus_iap_get_mode(client, &mode);
  292. if (error)
  293. return error;
  294. if (mode == MAIN_MODE) {
  295. /* set flash key */
  296. error = elan_smbus_set_flash_key(client);
  297. if (error)
  298. return error;
  299. /* write iap password */
  300. if (i2c_smbus_write_byte(client,
  301. ETP_SMBUS_IAP_PASSWORD_WRITE) < 0) {
  302. dev_err(dev, "cannot write iap password\n");
  303. return -EIO;
  304. }
  305. error = i2c_smbus_write_block_data(client, ETP_SMBUS_IAP_CMD,
  306. sizeof(cmd), cmd);
  307. if (error) {
  308. dev_err(dev, "failed to write iap password: %d\n",
  309. error);
  310. return error;
  311. }
  312. /*
  313. * Read back password to make sure we enabled flash
  314. * successfully.
  315. */
  316. len = i2c_smbus_read_block_data(client,
  317. ETP_SMBUS_IAP_PASSWORD_READ,
  318. val);
  319. if (len < sizeof(u16)) {
  320. error = len < 0 ? len : -EIO;
  321. dev_err(dev, "failed to read iap password: %d\n",
  322. error);
  323. return error;
  324. }
  325. password = be16_to_cpup((__be16 *)val);
  326. if (password != ETP_SMBUS_IAP_PASSWORD) {
  327. dev_err(dev, "wrong iap password = 0x%X\n", password);
  328. return -EIO;
  329. }
  330. /* Wait 30ms for MAIN_MODE change to IAP_MODE */
  331. msleep(30);
  332. }
  333. error = elan_smbus_set_flash_key(client);
  334. if (error)
  335. return error;
  336. /* Reset IC */
  337. error = elan_smbus_iap_reset(client);
  338. if (error)
  339. return error;
  340. return 0;
  341. }
  342. static int elan_smbus_write_fw_block(struct i2c_client *client,
  343. const u8 *page, u16 checksum, int idx)
  344. {
  345. struct device *dev = &client->dev;
  346. int error;
  347. u16 result;
  348. u8 val[3];
  349. /*
  350. * Due to the limitation of smbus protocol limiting
  351. * transfer to 32 bytes at a time, we must split block
  352. * in 2 transfers.
  353. */
  354. error = i2c_smbus_write_block_data(client,
  355. ETP_SMBUS_WRITE_FW_BLOCK,
  356. ETP_FW_PAGE_SIZE / 2,
  357. page);
  358. if (error) {
  359. dev_err(dev, "Failed to write page %d (part %d): %d\n",
  360. idx, 1, error);
  361. return error;
  362. }
  363. error = i2c_smbus_write_block_data(client,
  364. ETP_SMBUS_WRITE_FW_BLOCK,
  365. ETP_FW_PAGE_SIZE / 2,
  366. page + ETP_FW_PAGE_SIZE / 2);
  367. if (error) {
  368. dev_err(dev, "Failed to write page %d (part %d): %d\n",
  369. idx, 2, error);
  370. return error;
  371. }
  372. /* Wait for F/W to update one page ROM data. */
  373. usleep_range(8000, 10000);
  374. error = i2c_smbus_read_block_data(client,
  375. ETP_SMBUS_IAP_CTRL_CMD, val);
  376. if (error < 0) {
  377. dev_err(dev, "Failed to read IAP write result: %d\n",
  378. error);
  379. return error;
  380. }
  381. result = be16_to_cpup((__be16 *)val);
  382. if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) {
  383. dev_err(dev, "IAP reports failed write: %04hx\n",
  384. result);
  385. return -EIO;
  386. }
  387. return 0;
  388. }
  389. static int elan_smbus_get_report(struct i2c_client *client, u8 *report)
  390. {
  391. int 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. };