elants_i2c.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /*
  2. * Elan Microelectronics touch panels with I2C interface
  3. *
  4. * Copyright (C) 2014 Elan Microelectronics Corporation.
  5. * Scott Liu <scott.liu@emc.com.tw>
  6. *
  7. * This code is partly based on hid-multitouch.c:
  8. *
  9. * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
  10. * Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  11. * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
  12. *
  13. *
  14. * This code is partly based on i2c-hid.c:
  15. *
  16. * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  17. * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
  18. * Copyright (c) 2012 Red Hat, Inc
  19. */
  20. /*
  21. * This software is licensed under the terms of the GNU General Public
  22. * License version 2, as published by the Free Software Foundation, and
  23. * may be copied, distributed, and modified under those terms.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/input.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/async.h>
  30. #include <linux/i2c.h>
  31. #include <linux/delay.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/slab.h>
  35. #include <linux/firmware.h>
  36. #include <linux/input/mt.h>
  37. #include <linux/acpi.h>
  38. #include <linux/of.h>
  39. #include <linux/gpio/consumer.h>
  40. #include <linux/regulator/consumer.h>
  41. #include <asm/unaligned.h>
  42. /* Device, Driver information */
  43. #define DEVICE_NAME "elants_i2c"
  44. #define DRV_VERSION "1.0.9"
  45. /* Convert from rows or columns into resolution */
  46. #define ELAN_TS_RESOLUTION(n, m) (((n) - 1) * (m))
  47. /* FW header data */
  48. #define HEADER_SIZE 4
  49. #define FW_HDR_TYPE 0
  50. #define FW_HDR_COUNT 1
  51. #define FW_HDR_LENGTH 2
  52. /* Buffer mode Queue Header information */
  53. #define QUEUE_HEADER_SINGLE 0x62
  54. #define QUEUE_HEADER_NORMAL 0X63
  55. #define QUEUE_HEADER_WAIT 0x64
  56. /* Command header definition */
  57. #define CMD_HEADER_WRITE 0x54
  58. #define CMD_HEADER_READ 0x53
  59. #define CMD_HEADER_6B_READ 0x5B
  60. #define CMD_HEADER_RESP 0x52
  61. #define CMD_HEADER_6B_RESP 0x9B
  62. #define CMD_HEADER_HELLO 0x55
  63. #define CMD_HEADER_REK 0x66
  64. /* FW position data */
  65. #define PACKET_SIZE 55
  66. #define MAX_CONTACT_NUM 10
  67. #define FW_POS_HEADER 0
  68. #define FW_POS_STATE 1
  69. #define FW_POS_TOTAL 2
  70. #define FW_POS_XY 3
  71. #define FW_POS_CHECKSUM 34
  72. #define FW_POS_WIDTH 35
  73. #define FW_POS_PRESSURE 45
  74. #define HEADER_REPORT_10_FINGER 0x62
  75. /* Header (4 bytes) plus 3 fill 10-finger packets */
  76. #define MAX_PACKET_SIZE 169
  77. #define BOOT_TIME_DELAY_MS 50
  78. /* FW read command, 0x53 0x?? 0x0, 0x01 */
  79. #define E_ELAN_INFO_FW_VER 0x00
  80. #define E_ELAN_INFO_BC_VER 0x10
  81. #define E_ELAN_INFO_TEST_VER 0xE0
  82. #define E_ELAN_INFO_FW_ID 0xF0
  83. #define E_INFO_OSR 0xD6
  84. #define E_INFO_PHY_SCAN 0xD7
  85. #define E_INFO_PHY_DRIVER 0xD8
  86. #define MAX_RETRIES 3
  87. #define MAX_FW_UPDATE_RETRIES 30
  88. #define ELAN_FW_PAGESIZE 132
  89. /* calibration timeout definition */
  90. #define ELAN_CALI_TIMEOUT_MSEC 12000
  91. #define ELAN_POWERON_DELAY_USEC 500
  92. #define ELAN_RESET_DELAY_MSEC 20
  93. enum elants_state {
  94. ELAN_STATE_NORMAL,
  95. ELAN_WAIT_QUEUE_HEADER,
  96. ELAN_WAIT_RECALIBRATION,
  97. };
  98. enum elants_iap_mode {
  99. ELAN_IAP_OPERATIONAL,
  100. ELAN_IAP_RECOVERY,
  101. };
  102. /* struct elants_data - represents state of Elan touchscreen device */
  103. struct elants_data {
  104. struct i2c_client *client;
  105. struct input_dev *input;
  106. struct regulator *vcc33;
  107. struct regulator *vccio;
  108. struct gpio_desc *reset_gpio;
  109. u16 fw_version;
  110. u8 test_version;
  111. u8 solution_version;
  112. u8 bc_version;
  113. u8 iap_version;
  114. u16 hw_version;
  115. unsigned int x_res; /* resolution in units/mm */
  116. unsigned int y_res;
  117. unsigned int x_max;
  118. unsigned int y_max;
  119. enum elants_state state;
  120. enum elants_iap_mode iap_mode;
  121. /* Guards against concurrent access to the device via sysfs */
  122. struct mutex sysfs_mutex;
  123. u8 cmd_resp[HEADER_SIZE];
  124. struct completion cmd_done;
  125. u8 buf[MAX_PACKET_SIZE];
  126. bool wake_irq_enabled;
  127. bool keep_power_in_suspend;
  128. };
  129. static int elants_i2c_send(struct i2c_client *client,
  130. const void *data, size_t size)
  131. {
  132. int ret;
  133. ret = i2c_master_send(client, data, size);
  134. if (ret == size)
  135. return 0;
  136. if (ret >= 0)
  137. ret = -EIO;
  138. dev_err(&client->dev, "%s failed (%*ph): %d\n",
  139. __func__, (int)size, data, ret);
  140. return ret;
  141. }
  142. static int elants_i2c_read(struct i2c_client *client, void *data, size_t size)
  143. {
  144. int ret;
  145. ret = i2c_master_recv(client, data, size);
  146. if (ret == size)
  147. return 0;
  148. if (ret >= 0)
  149. ret = -EIO;
  150. dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
  151. return ret;
  152. }
  153. static int elants_i2c_execute_command(struct i2c_client *client,
  154. const u8 *cmd, size_t cmd_size,
  155. u8 *resp, size_t resp_size)
  156. {
  157. struct i2c_msg msgs[2];
  158. int ret;
  159. u8 expected_response;
  160. switch (cmd[0]) {
  161. case CMD_HEADER_READ:
  162. expected_response = CMD_HEADER_RESP;
  163. break;
  164. case CMD_HEADER_6B_READ:
  165. expected_response = CMD_HEADER_6B_RESP;
  166. break;
  167. default:
  168. dev_err(&client->dev, "%s: invalid command %*ph\n",
  169. __func__, (int)cmd_size, cmd);
  170. return -EINVAL;
  171. }
  172. msgs[0].addr = client->addr;
  173. msgs[0].flags = client->flags & I2C_M_TEN;
  174. msgs[0].len = cmd_size;
  175. msgs[0].buf = (u8 *)cmd;
  176. msgs[1].addr = client->addr;
  177. msgs[1].flags = client->flags & I2C_M_TEN;
  178. msgs[1].flags |= I2C_M_RD;
  179. msgs[1].len = resp_size;
  180. msgs[1].buf = resp;
  181. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  182. if (ret < 0)
  183. return ret;
  184. if (ret != ARRAY_SIZE(msgs) || resp[FW_HDR_TYPE] != expected_response)
  185. return -EIO;
  186. return 0;
  187. }
  188. static int elants_i2c_calibrate(struct elants_data *ts)
  189. {
  190. struct i2c_client *client = ts->client;
  191. int ret, error;
  192. static const u8 w_flashkey[] = { 0x54, 0xC0, 0xE1, 0x5A };
  193. static const u8 rek[] = { 0x54, 0x29, 0x00, 0x01 };
  194. static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 };
  195. disable_irq(client->irq);
  196. ts->state = ELAN_WAIT_RECALIBRATION;
  197. reinit_completion(&ts->cmd_done);
  198. elants_i2c_send(client, w_flashkey, sizeof(w_flashkey));
  199. elants_i2c_send(client, rek, sizeof(rek));
  200. enable_irq(client->irq);
  201. ret = wait_for_completion_interruptible_timeout(&ts->cmd_done,
  202. msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC));
  203. ts->state = ELAN_STATE_NORMAL;
  204. if (ret <= 0) {
  205. error = ret < 0 ? ret : -ETIMEDOUT;
  206. dev_err(&client->dev,
  207. "error while waiting for calibration to complete: %d\n",
  208. error);
  209. return error;
  210. }
  211. if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) {
  212. dev_err(&client->dev,
  213. "unexpected calibration response: %*ph\n",
  214. (int)sizeof(ts->cmd_resp), ts->cmd_resp);
  215. return -EINVAL;
  216. }
  217. return 0;
  218. }
  219. static int elants_i2c_sw_reset(struct i2c_client *client)
  220. {
  221. const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 };
  222. int error;
  223. error = elants_i2c_send(client, soft_rst_cmd,
  224. sizeof(soft_rst_cmd));
  225. if (error) {
  226. dev_err(&client->dev, "software reset failed: %d\n", error);
  227. return error;
  228. }
  229. /*
  230. * We should wait at least 10 msec (but no more than 40) before
  231. * sending fastboot or IAP command to the device.
  232. */
  233. msleep(30);
  234. return 0;
  235. }
  236. static u16 elants_i2c_parse_version(u8 *buf)
  237. {
  238. return get_unaligned_be32(buf) >> 4;
  239. }
  240. static int elants_i2c_query_fw_id(struct elants_data *ts)
  241. {
  242. struct i2c_client *client = ts->client;
  243. int error, retry_cnt;
  244. const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 };
  245. u8 resp[HEADER_SIZE];
  246. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  247. error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
  248. resp, sizeof(resp));
  249. if (!error) {
  250. ts->hw_version = elants_i2c_parse_version(resp);
  251. if (ts->hw_version != 0xffff)
  252. return 0;
  253. }
  254. dev_dbg(&client->dev, "read fw id error=%d, buf=%*phC\n",
  255. error, (int)sizeof(resp), resp);
  256. }
  257. dev_err(&client->dev,
  258. "Failed to read fw id or fw id is invalid\n");
  259. return -EINVAL;
  260. }
  261. static int elants_i2c_query_fw_version(struct elants_data *ts)
  262. {
  263. struct i2c_client *client = ts->client;
  264. int error, retry_cnt;
  265. const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 };
  266. u8 resp[HEADER_SIZE];
  267. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  268. error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
  269. resp, sizeof(resp));
  270. if (!error) {
  271. ts->fw_version = elants_i2c_parse_version(resp);
  272. if (ts->fw_version != 0x0000 &&
  273. ts->fw_version != 0xffff)
  274. return 0;
  275. }
  276. dev_dbg(&client->dev, "read fw version error=%d, buf=%*phC\n",
  277. error, (int)sizeof(resp), resp);
  278. }
  279. dev_err(&client->dev,
  280. "Failed to read fw version or fw version is invalid\n");
  281. return -EINVAL;
  282. }
  283. static int elants_i2c_query_test_version(struct elants_data *ts)
  284. {
  285. struct i2c_client *client = ts->client;
  286. int error, retry_cnt;
  287. u16 version;
  288. const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 };
  289. u8 resp[HEADER_SIZE];
  290. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  291. error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
  292. resp, sizeof(resp));
  293. if (!error) {
  294. version = elants_i2c_parse_version(resp);
  295. ts->test_version = version >> 8;
  296. ts->solution_version = version & 0xff;
  297. return 0;
  298. }
  299. dev_dbg(&client->dev,
  300. "read test version error rc=%d, buf=%*phC\n",
  301. error, (int)sizeof(resp), resp);
  302. }
  303. dev_err(&client->dev, "Failed to read test version\n");
  304. return -EINVAL;
  305. }
  306. static int elants_i2c_query_bc_version(struct elants_data *ts)
  307. {
  308. struct i2c_client *client = ts->client;
  309. const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 };
  310. u8 resp[HEADER_SIZE];
  311. u16 version;
  312. int error;
  313. error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
  314. resp, sizeof(resp));
  315. if (error) {
  316. dev_err(&client->dev,
  317. "read BC version error=%d, buf=%*phC\n",
  318. error, (int)sizeof(resp), resp);
  319. return error;
  320. }
  321. version = elants_i2c_parse_version(resp);
  322. ts->bc_version = version >> 8;
  323. ts->iap_version = version & 0xff;
  324. return 0;
  325. }
  326. static int elants_i2c_query_ts_info(struct elants_data *ts)
  327. {
  328. struct i2c_client *client = ts->client;
  329. int error;
  330. u8 resp[17];
  331. u16 phy_x, phy_y, rows, cols, osr;
  332. const u8 get_resolution_cmd[] = {
  333. CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00
  334. };
  335. const u8 get_osr_cmd[] = {
  336. CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01
  337. };
  338. const u8 get_physical_scan_cmd[] = {
  339. CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01
  340. };
  341. const u8 get_physical_drive_cmd[] = {
  342. CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01
  343. };
  344. /* Get trace number */
  345. error = elants_i2c_execute_command(client,
  346. get_resolution_cmd,
  347. sizeof(get_resolution_cmd),
  348. resp, sizeof(resp));
  349. if (error) {
  350. dev_err(&client->dev, "get resolution command failed: %d\n",
  351. error);
  352. return error;
  353. }
  354. rows = resp[2] + resp[6] + resp[10];
  355. cols = resp[3] + resp[7] + resp[11];
  356. /* Process mm_to_pixel information */
  357. error = elants_i2c_execute_command(client,
  358. get_osr_cmd, sizeof(get_osr_cmd),
  359. resp, sizeof(resp));
  360. if (error) {
  361. dev_err(&client->dev, "get osr command failed: %d\n",
  362. error);
  363. return error;
  364. }
  365. osr = resp[3];
  366. error = elants_i2c_execute_command(client,
  367. get_physical_scan_cmd,
  368. sizeof(get_physical_scan_cmd),
  369. resp, sizeof(resp));
  370. if (error) {
  371. dev_err(&client->dev, "get physical scan command failed: %d\n",
  372. error);
  373. return error;
  374. }
  375. phy_x = get_unaligned_be16(&resp[2]);
  376. error = elants_i2c_execute_command(client,
  377. get_physical_drive_cmd,
  378. sizeof(get_physical_drive_cmd),
  379. resp, sizeof(resp));
  380. if (error) {
  381. dev_err(&client->dev, "get physical drive command failed: %d\n",
  382. error);
  383. return error;
  384. }
  385. phy_y = get_unaligned_be16(&resp[2]);
  386. dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
  387. if (rows == 0 || cols == 0 || osr == 0) {
  388. dev_warn(&client->dev,
  389. "invalid trace number data: %d, %d, %d\n",
  390. rows, cols, osr);
  391. } else {
  392. /* translate trace number to TS resolution */
  393. ts->x_max = ELAN_TS_RESOLUTION(rows, osr);
  394. ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x);
  395. ts->y_max = ELAN_TS_RESOLUTION(cols, osr);
  396. ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y);
  397. }
  398. return 0;
  399. }
  400. static int elants_i2c_fastboot(struct i2c_client *client)
  401. {
  402. const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E };
  403. int error;
  404. error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd));
  405. if (error) {
  406. dev_err(&client->dev, "boot failed: %d\n", error);
  407. return error;
  408. }
  409. dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr);
  410. return 0;
  411. }
  412. static int elants_i2c_initialize(struct elants_data *ts)
  413. {
  414. struct i2c_client *client = ts->client;
  415. int error, retry_cnt;
  416. const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 };
  417. const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 };
  418. u8 buf[HEADER_SIZE];
  419. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  420. error = elants_i2c_sw_reset(client);
  421. if (error) {
  422. /* Continue initializing if it's the last try */
  423. if (retry_cnt < MAX_RETRIES - 1)
  424. continue;
  425. }
  426. error = elants_i2c_fastboot(client);
  427. if (error) {
  428. /* Continue initializing if it's the last try */
  429. if (retry_cnt < MAX_RETRIES - 1)
  430. continue;
  431. }
  432. /* Wait for Hello packet */
  433. msleep(BOOT_TIME_DELAY_MS);
  434. error = elants_i2c_read(client, buf, sizeof(buf));
  435. if (error) {
  436. dev_err(&client->dev,
  437. "failed to read 'hello' packet: %d\n", error);
  438. } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) {
  439. ts->iap_mode = ELAN_IAP_OPERATIONAL;
  440. break;
  441. } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) {
  442. /*
  443. * Setting error code will mark device
  444. * in recovery mode below.
  445. */
  446. error = -EIO;
  447. break;
  448. } else {
  449. error = -EINVAL;
  450. dev_err(&client->dev,
  451. "invalid 'hello' packet: %*ph\n",
  452. (int)sizeof(buf), buf);
  453. }
  454. }
  455. if (!error)
  456. error = elants_i2c_query_fw_id(ts);
  457. if (!error)
  458. error = elants_i2c_query_fw_version(ts);
  459. if (error) {
  460. ts->iap_mode = ELAN_IAP_RECOVERY;
  461. } else {
  462. elants_i2c_query_test_version(ts);
  463. elants_i2c_query_bc_version(ts);
  464. elants_i2c_query_ts_info(ts);
  465. }
  466. return 0;
  467. }
  468. /*
  469. * Firmware update interface.
  470. */
  471. static int elants_i2c_fw_write_page(struct i2c_client *client,
  472. const void *page)
  473. {
  474. const u8 ack_ok[] = { 0xaa, 0xaa };
  475. u8 buf[2];
  476. int retry;
  477. int error;
  478. for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) {
  479. error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE);
  480. if (error) {
  481. dev_err(&client->dev,
  482. "IAP Write Page failed: %d\n", error);
  483. continue;
  484. }
  485. error = elants_i2c_read(client, buf, 2);
  486. if (error) {
  487. dev_err(&client->dev,
  488. "IAP Ack read failed: %d\n", error);
  489. return error;
  490. }
  491. if (!memcmp(buf, ack_ok, sizeof(ack_ok)))
  492. return 0;
  493. error = -EIO;
  494. dev_err(&client->dev,
  495. "IAP Get Ack Error [%02x:%02x]\n",
  496. buf[0], buf[1]);
  497. }
  498. return error;
  499. }
  500. static int elants_i2c_do_update_firmware(struct i2c_client *client,
  501. const struct firmware *fw,
  502. bool force)
  503. {
  504. const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 };
  505. const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 };
  506. const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc };
  507. const u8 close_idle[] = {0x54, 0x2c, 0x01, 0x01};
  508. u8 buf[HEADER_SIZE];
  509. u16 send_id;
  510. int page, n_fw_pages;
  511. int error;
  512. /* Recovery mode detection! */
  513. if (force) {
  514. dev_dbg(&client->dev, "Recovery mode procedure\n");
  515. error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2));
  516. } else {
  517. /* Start IAP Procedure */
  518. dev_dbg(&client->dev, "Normal IAP procedure\n");
  519. /* Close idle mode */
  520. error = elants_i2c_send(client, close_idle, sizeof(close_idle));
  521. if (error)
  522. dev_err(&client->dev, "Failed close idle: %d\n", error);
  523. msleep(60);
  524. elants_i2c_sw_reset(client);
  525. msleep(20);
  526. error = elants_i2c_send(client, enter_iap, sizeof(enter_iap));
  527. }
  528. if (error) {
  529. dev_err(&client->dev, "failed to enter IAP mode: %d\n", error);
  530. return error;
  531. }
  532. msleep(20);
  533. /* check IAP state */
  534. error = elants_i2c_read(client, buf, 4);
  535. if (error) {
  536. dev_err(&client->dev,
  537. "failed to read IAP acknowledgement: %d\n",
  538. error);
  539. return error;
  540. }
  541. if (memcmp(buf, iap_ack, sizeof(iap_ack))) {
  542. dev_err(&client->dev,
  543. "failed to enter IAP: %*ph (expected %*ph)\n",
  544. (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack);
  545. return -EIO;
  546. }
  547. dev_info(&client->dev, "successfully entered IAP mode");
  548. send_id = client->addr;
  549. error = elants_i2c_send(client, &send_id, 1);
  550. if (error) {
  551. dev_err(&client->dev, "sending dummy byte failed: %d\n",
  552. error);
  553. return error;
  554. }
  555. /* Clear the last page of Master */
  556. error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE);
  557. if (error) {
  558. dev_err(&client->dev, "clearing of the last page failed: %d\n",
  559. error);
  560. return error;
  561. }
  562. error = elants_i2c_read(client, buf, 2);
  563. if (error) {
  564. dev_err(&client->dev,
  565. "failed to read ACK for clearing the last page: %d\n",
  566. error);
  567. return error;
  568. }
  569. n_fw_pages = fw->size / ELAN_FW_PAGESIZE;
  570. dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages);
  571. for (page = 0; page < n_fw_pages; page++) {
  572. error = elants_i2c_fw_write_page(client,
  573. fw->data + page * ELAN_FW_PAGESIZE);
  574. if (error) {
  575. dev_err(&client->dev,
  576. "failed to write FW page %d: %d\n",
  577. page, error);
  578. return error;
  579. }
  580. }
  581. /* Old iap needs to wait 200ms for WDT and rest is for hello packets */
  582. msleep(300);
  583. dev_info(&client->dev, "firmware update completed\n");
  584. return 0;
  585. }
  586. static int elants_i2c_fw_update(struct elants_data *ts)
  587. {
  588. struct i2c_client *client = ts->client;
  589. const struct firmware *fw;
  590. char *fw_name;
  591. int error;
  592. fw_name = kasprintf(GFP_KERNEL, "elants_i2c_%04x.bin", ts->hw_version);
  593. if (!fw_name)
  594. return -ENOMEM;
  595. dev_info(&client->dev, "requesting fw name = %s\n", fw_name);
  596. error = request_firmware(&fw, fw_name, &client->dev);
  597. kfree(fw_name);
  598. if (error) {
  599. dev_err(&client->dev, "failed to request firmware: %d\n",
  600. error);
  601. return error;
  602. }
  603. if (fw->size % ELAN_FW_PAGESIZE) {
  604. dev_err(&client->dev, "invalid firmware length: %zu\n",
  605. fw->size);
  606. error = -EINVAL;
  607. goto out;
  608. }
  609. disable_irq(client->irq);
  610. error = elants_i2c_do_update_firmware(client, fw,
  611. ts->iap_mode == ELAN_IAP_RECOVERY);
  612. if (error) {
  613. dev_err(&client->dev, "firmware update failed: %d\n", error);
  614. ts->iap_mode = ELAN_IAP_RECOVERY;
  615. goto out_enable_irq;
  616. }
  617. error = elants_i2c_initialize(ts);
  618. if (error) {
  619. dev_err(&client->dev,
  620. "failed to initialize device after firmware update: %d\n",
  621. error);
  622. ts->iap_mode = ELAN_IAP_RECOVERY;
  623. goto out_enable_irq;
  624. }
  625. ts->iap_mode = ELAN_IAP_OPERATIONAL;
  626. out_enable_irq:
  627. ts->state = ELAN_STATE_NORMAL;
  628. enable_irq(client->irq);
  629. msleep(100);
  630. if (!error)
  631. elants_i2c_calibrate(ts);
  632. out:
  633. release_firmware(fw);
  634. return error;
  635. }
  636. /*
  637. * Event reporting.
  638. */
  639. static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf)
  640. {
  641. struct input_dev *input = ts->input;
  642. unsigned int n_fingers;
  643. u16 finger_state;
  644. int i;
  645. n_fingers = buf[FW_POS_STATE + 1] & 0x0f;
  646. finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) |
  647. buf[FW_POS_STATE];
  648. dev_dbg(&ts->client->dev,
  649. "n_fingers: %u, state: %04x\n", n_fingers, finger_state);
  650. for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) {
  651. if (finger_state & 1) {
  652. unsigned int x, y, p, w;
  653. u8 *pos;
  654. pos = &buf[FW_POS_XY + i * 3];
  655. x = (((u16)pos[0] & 0xf0) << 4) | pos[1];
  656. y = (((u16)pos[0] & 0x0f) << 8) | pos[2];
  657. p = buf[FW_POS_PRESSURE + i];
  658. w = buf[FW_POS_WIDTH + i];
  659. dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n",
  660. i, x, y, p, w);
  661. input_mt_slot(input, i);
  662. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  663. input_event(input, EV_ABS, ABS_MT_POSITION_X, x);
  664. input_event(input, EV_ABS, ABS_MT_POSITION_Y, y);
  665. input_event(input, EV_ABS, ABS_MT_PRESSURE, p);
  666. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w);
  667. n_fingers--;
  668. }
  669. finger_state >>= 1;
  670. }
  671. input_mt_sync_frame(input);
  672. input_sync(input);
  673. }
  674. static u8 elants_i2c_calculate_checksum(u8 *buf)
  675. {
  676. u8 checksum = 0;
  677. u8 i;
  678. for (i = 0; i < FW_POS_CHECKSUM; i++)
  679. checksum += buf[i];
  680. return checksum;
  681. }
  682. static void elants_i2c_event(struct elants_data *ts, u8 *buf)
  683. {
  684. u8 checksum = elants_i2c_calculate_checksum(buf);
  685. if (unlikely(buf[FW_POS_CHECKSUM] != checksum))
  686. dev_warn(&ts->client->dev,
  687. "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
  688. __func__, buf[FW_POS_HEADER],
  689. checksum, buf[FW_POS_CHECKSUM]);
  690. else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER))
  691. dev_warn(&ts->client->dev,
  692. "%s: unknown packet type: %02x\n",
  693. __func__, buf[FW_POS_HEADER]);
  694. else
  695. elants_i2c_mt_event(ts, buf);
  696. }
  697. static irqreturn_t elants_i2c_irq(int irq, void *_dev)
  698. {
  699. const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 };
  700. struct elants_data *ts = _dev;
  701. struct i2c_client *client = ts->client;
  702. int report_count, report_len;
  703. int i;
  704. int len;
  705. len = i2c_master_recv(client, ts->buf, sizeof(ts->buf));
  706. if (len < 0) {
  707. dev_err(&client->dev, "%s: failed to read data: %d\n",
  708. __func__, len);
  709. goto out;
  710. }
  711. dev_dbg(&client->dev, "%s: packet %*ph\n",
  712. __func__, HEADER_SIZE, ts->buf);
  713. switch (ts->state) {
  714. case ELAN_WAIT_RECALIBRATION:
  715. if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) {
  716. memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp));
  717. complete(&ts->cmd_done);
  718. ts->state = ELAN_STATE_NORMAL;
  719. }
  720. break;
  721. case ELAN_WAIT_QUEUE_HEADER:
  722. if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL)
  723. break;
  724. ts->state = ELAN_STATE_NORMAL;
  725. /* fall through */
  726. case ELAN_STATE_NORMAL:
  727. switch (ts->buf[FW_HDR_TYPE]) {
  728. case CMD_HEADER_HELLO:
  729. case CMD_HEADER_RESP:
  730. case CMD_HEADER_REK:
  731. break;
  732. case QUEUE_HEADER_WAIT:
  733. if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) {
  734. dev_err(&client->dev,
  735. "invalid wait packet %*ph\n",
  736. HEADER_SIZE, ts->buf);
  737. } else {
  738. ts->state = ELAN_WAIT_QUEUE_HEADER;
  739. udelay(30);
  740. }
  741. break;
  742. case QUEUE_HEADER_SINGLE:
  743. elants_i2c_event(ts, &ts->buf[HEADER_SIZE]);
  744. break;
  745. case QUEUE_HEADER_NORMAL:
  746. report_count = ts->buf[FW_HDR_COUNT];
  747. if (report_count == 0 || report_count > 3) {
  748. dev_err(&client->dev,
  749. "bad report count: %*ph\n",
  750. HEADER_SIZE, ts->buf);
  751. break;
  752. }
  753. report_len = ts->buf[FW_HDR_LENGTH] / report_count;
  754. if (report_len != PACKET_SIZE) {
  755. dev_err(&client->dev,
  756. "mismatching report length: %*ph\n",
  757. HEADER_SIZE, ts->buf);
  758. break;
  759. }
  760. for (i = 0; i < report_count; i++) {
  761. u8 *buf = ts->buf + HEADER_SIZE +
  762. i * PACKET_SIZE;
  763. elants_i2c_event(ts, buf);
  764. }
  765. break;
  766. default:
  767. dev_err(&client->dev, "unknown packet %*ph\n",
  768. HEADER_SIZE, ts->buf);
  769. break;
  770. }
  771. break;
  772. }
  773. out:
  774. return IRQ_HANDLED;
  775. }
  776. /*
  777. * sysfs interface
  778. */
  779. static ssize_t calibrate_store(struct device *dev,
  780. struct device_attribute *attr,
  781. const char *buf, size_t count)
  782. {
  783. struct i2c_client *client = to_i2c_client(dev);
  784. struct elants_data *ts = i2c_get_clientdata(client);
  785. int error;
  786. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  787. if (error)
  788. return error;
  789. error = elants_i2c_calibrate(ts);
  790. mutex_unlock(&ts->sysfs_mutex);
  791. return error ?: count;
  792. }
  793. static ssize_t write_update_fw(struct device *dev,
  794. struct device_attribute *attr,
  795. const char *buf, size_t count)
  796. {
  797. struct i2c_client *client = to_i2c_client(dev);
  798. struct elants_data *ts = i2c_get_clientdata(client);
  799. int error;
  800. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  801. if (error)
  802. return error;
  803. error = elants_i2c_fw_update(ts);
  804. dev_dbg(dev, "firmware update result: %d\n", error);
  805. mutex_unlock(&ts->sysfs_mutex);
  806. return error ?: count;
  807. }
  808. static ssize_t show_iap_mode(struct device *dev,
  809. struct device_attribute *attr, char *buf)
  810. {
  811. struct i2c_client *client = to_i2c_client(dev);
  812. struct elants_data *ts = i2c_get_clientdata(client);
  813. return sprintf(buf, "%s\n",
  814. ts->iap_mode == ELAN_IAP_OPERATIONAL ?
  815. "Normal" : "Recovery");
  816. }
  817. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, calibrate_store);
  818. static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL);
  819. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw);
  820. struct elants_version_attribute {
  821. struct device_attribute dattr;
  822. size_t field_offset;
  823. size_t field_size;
  824. };
  825. #define __ELANTS_FIELD_SIZE(_field) \
  826. sizeof(((struct elants_data *)NULL)->_field)
  827. #define __ELANTS_VERIFY_SIZE(_field) \
  828. (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \
  829. __ELANTS_FIELD_SIZE(_field))
  830. #define ELANTS_VERSION_ATTR(_field) \
  831. struct elants_version_attribute elants_ver_attr_##_field = { \
  832. .dattr = __ATTR(_field, S_IRUGO, \
  833. elants_version_attribute_show, NULL), \
  834. .field_offset = offsetof(struct elants_data, _field), \
  835. .field_size = __ELANTS_VERIFY_SIZE(_field), \
  836. }
  837. static ssize_t elants_version_attribute_show(struct device *dev,
  838. struct device_attribute *dattr,
  839. char *buf)
  840. {
  841. struct i2c_client *client = to_i2c_client(dev);
  842. struct elants_data *ts = i2c_get_clientdata(client);
  843. struct elants_version_attribute *attr =
  844. container_of(dattr, struct elants_version_attribute, dattr);
  845. u8 *field = (u8 *)((char *)ts + attr->field_offset);
  846. unsigned int fmt_size;
  847. unsigned int val;
  848. if (attr->field_size == 1) {
  849. val = *field;
  850. fmt_size = 2; /* 2 HEX digits */
  851. } else {
  852. val = *(u16 *)field;
  853. fmt_size = 4; /* 4 HEX digits */
  854. }
  855. return sprintf(buf, "%0*x\n", fmt_size, val);
  856. }
  857. static ELANTS_VERSION_ATTR(fw_version);
  858. static ELANTS_VERSION_ATTR(hw_version);
  859. static ELANTS_VERSION_ATTR(test_version);
  860. static ELANTS_VERSION_ATTR(solution_version);
  861. static ELANTS_VERSION_ATTR(bc_version);
  862. static ELANTS_VERSION_ATTR(iap_version);
  863. static struct attribute *elants_attributes[] = {
  864. &dev_attr_calibrate.attr,
  865. &dev_attr_update_fw.attr,
  866. &dev_attr_iap_mode.attr,
  867. &elants_ver_attr_fw_version.dattr.attr,
  868. &elants_ver_attr_hw_version.dattr.attr,
  869. &elants_ver_attr_test_version.dattr.attr,
  870. &elants_ver_attr_solution_version.dattr.attr,
  871. &elants_ver_attr_bc_version.dattr.attr,
  872. &elants_ver_attr_iap_version.dattr.attr,
  873. NULL
  874. };
  875. static struct attribute_group elants_attribute_group = {
  876. .attrs = elants_attributes,
  877. };
  878. static void elants_i2c_remove_sysfs_group(void *_data)
  879. {
  880. struct elants_data *ts = _data;
  881. sysfs_remove_group(&ts->client->dev.kobj, &elants_attribute_group);
  882. }
  883. static int elants_i2c_power_on(struct elants_data *ts)
  884. {
  885. int error;
  886. /*
  887. * If we do not have reset gpio assume platform firmware
  888. * controls regulators and does power them on for us.
  889. */
  890. if (IS_ERR_OR_NULL(ts->reset_gpio))
  891. return 0;
  892. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  893. error = regulator_enable(ts->vcc33);
  894. if (error) {
  895. dev_err(&ts->client->dev,
  896. "failed to enable vcc33 regulator: %d\n",
  897. error);
  898. goto release_reset_gpio;
  899. }
  900. error = regulator_enable(ts->vccio);
  901. if (error) {
  902. dev_err(&ts->client->dev,
  903. "failed to enable vccio regulator: %d\n",
  904. error);
  905. regulator_disable(ts->vcc33);
  906. goto release_reset_gpio;
  907. }
  908. /*
  909. * We need to wait a bit after powering on controller before
  910. * we are allowed to release reset GPIO.
  911. */
  912. udelay(ELAN_POWERON_DELAY_USEC);
  913. release_reset_gpio:
  914. gpiod_set_value_cansleep(ts->reset_gpio, 0);
  915. if (error)
  916. return error;
  917. msleep(ELAN_RESET_DELAY_MSEC);
  918. return 0;
  919. }
  920. static void elants_i2c_power_off(void *_data)
  921. {
  922. struct elants_data *ts = _data;
  923. if (!IS_ERR_OR_NULL(ts->reset_gpio)) {
  924. /*
  925. * Activate reset gpio to prevent leakage through the
  926. * pin once we shut off power to the controller.
  927. */
  928. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  929. regulator_disable(ts->vccio);
  930. regulator_disable(ts->vcc33);
  931. }
  932. }
  933. static int elants_i2c_probe(struct i2c_client *client,
  934. const struct i2c_device_id *id)
  935. {
  936. union i2c_smbus_data dummy;
  937. struct elants_data *ts;
  938. unsigned long irqflags;
  939. int error;
  940. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  941. dev_err(&client->dev,
  942. "%s: i2c check functionality error\n", DEVICE_NAME);
  943. return -ENXIO;
  944. }
  945. ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL);
  946. if (!ts)
  947. return -ENOMEM;
  948. mutex_init(&ts->sysfs_mutex);
  949. init_completion(&ts->cmd_done);
  950. ts->client = client;
  951. i2c_set_clientdata(client, ts);
  952. ts->vcc33 = devm_regulator_get(&client->dev, "vcc33");
  953. if (IS_ERR(ts->vcc33)) {
  954. error = PTR_ERR(ts->vcc33);
  955. if (error != -EPROBE_DEFER)
  956. dev_err(&client->dev,
  957. "Failed to get 'vcc33' regulator: %d\n",
  958. error);
  959. return error;
  960. }
  961. ts->vccio = devm_regulator_get(&client->dev, "vccio");
  962. if (IS_ERR(ts->vccio)) {
  963. error = PTR_ERR(ts->vccio);
  964. if (error != -EPROBE_DEFER)
  965. dev_err(&client->dev,
  966. "Failed to get 'vccio' regulator: %d\n",
  967. error);
  968. return error;
  969. }
  970. ts->reset_gpio = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_LOW);
  971. if (IS_ERR(ts->reset_gpio)) {
  972. error = PTR_ERR(ts->reset_gpio);
  973. if (error == -EPROBE_DEFER)
  974. return error;
  975. if (error != -ENOENT && error != -ENOSYS) {
  976. dev_err(&client->dev,
  977. "failed to get reset gpio: %d\n",
  978. error);
  979. return error;
  980. }
  981. ts->keep_power_in_suspend = true;
  982. }
  983. error = elants_i2c_power_on(ts);
  984. if (error)
  985. return error;
  986. error = devm_add_action(&client->dev, elants_i2c_power_off, ts);
  987. if (error) {
  988. dev_err(&client->dev,
  989. "failed to install power off action: %d\n", error);
  990. elants_i2c_power_off(ts);
  991. return error;
  992. }
  993. /* Make sure there is something at this address */
  994. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  995. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
  996. dev_err(&client->dev, "nothing at this address\n");
  997. return -ENXIO;
  998. }
  999. error = elants_i2c_initialize(ts);
  1000. if (error) {
  1001. dev_err(&client->dev, "failed to initialize: %d\n", error);
  1002. return error;
  1003. }
  1004. ts->input = devm_input_allocate_device(&client->dev);
  1005. if (!ts->input) {
  1006. dev_err(&client->dev, "Failed to allocate input device\n");
  1007. return -ENOMEM;
  1008. }
  1009. ts->input->name = "Elan Touchscreen";
  1010. ts->input->id.bustype = BUS_I2C;
  1011. __set_bit(BTN_TOUCH, ts->input->keybit);
  1012. __set_bit(EV_ABS, ts->input->evbit);
  1013. __set_bit(EV_KEY, ts->input->evbit);
  1014. /* Single touch input params setup */
  1015. input_set_abs_params(ts->input, ABS_X, 0, ts->x_max, 0, 0);
  1016. input_set_abs_params(ts->input, ABS_Y, 0, ts->y_max, 0, 0);
  1017. input_set_abs_params(ts->input, ABS_PRESSURE, 0, 255, 0, 0);
  1018. input_abs_set_res(ts->input, ABS_X, ts->x_res);
  1019. input_abs_set_res(ts->input, ABS_Y, ts->y_res);
  1020. /* Multitouch input params setup */
  1021. error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM,
  1022. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  1023. if (error) {
  1024. dev_err(&client->dev,
  1025. "failed to initialize MT slots: %d\n", error);
  1026. return error;
  1027. }
  1028. input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
  1029. input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
  1030. input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  1031. input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  1032. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
  1033. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
  1034. input_set_drvdata(ts->input, ts);
  1035. error = input_register_device(ts->input);
  1036. if (error) {
  1037. dev_err(&client->dev,
  1038. "unable to register input device: %d\n", error);
  1039. return error;
  1040. }
  1041. /*
  1042. * Systems using device tree should set up interrupt via DTS,
  1043. * the rest will use the default falling edge interrupts.
  1044. */
  1045. irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
  1046. error = devm_request_threaded_irq(&client->dev, client->irq,
  1047. NULL, elants_i2c_irq,
  1048. irqflags | IRQF_ONESHOT,
  1049. client->name, ts);
  1050. if (error) {
  1051. dev_err(&client->dev, "Failed to register interrupt\n");
  1052. return error;
  1053. }
  1054. /*
  1055. * Systems using device tree should set up wakeup via DTS,
  1056. * the rest will configure device as wakeup source by default.
  1057. */
  1058. if (!client->dev.of_node)
  1059. device_init_wakeup(&client->dev, true);
  1060. error = sysfs_create_group(&client->dev.kobj, &elants_attribute_group);
  1061. if (error) {
  1062. dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
  1063. error);
  1064. return error;
  1065. }
  1066. error = devm_add_action(&client->dev,
  1067. elants_i2c_remove_sysfs_group, ts);
  1068. if (error) {
  1069. elants_i2c_remove_sysfs_group(ts);
  1070. dev_err(&client->dev,
  1071. "Failed to add sysfs cleanup action: %d\n",
  1072. error);
  1073. return error;
  1074. }
  1075. return 0;
  1076. }
  1077. static int __maybe_unused elants_i2c_suspend(struct device *dev)
  1078. {
  1079. struct i2c_client *client = to_i2c_client(dev);
  1080. struct elants_data *ts = i2c_get_clientdata(client);
  1081. const u8 set_sleep_cmd[] = { 0x54, 0x50, 0x00, 0x01 };
  1082. int retry_cnt;
  1083. int error;
  1084. /* Command not support in IAP recovery mode */
  1085. if (ts->iap_mode != ELAN_IAP_OPERATIONAL)
  1086. return -EBUSY;
  1087. disable_irq(client->irq);
  1088. if (device_may_wakeup(dev)) {
  1089. /*
  1090. * The device will automatically enter idle mode
  1091. * that has reduced power consumption.
  1092. */
  1093. ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
  1094. } else if (ts->keep_power_in_suspend) {
  1095. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  1096. error = elants_i2c_send(client, set_sleep_cmd,
  1097. sizeof(set_sleep_cmd));
  1098. if (!error)
  1099. break;
  1100. dev_err(&client->dev,
  1101. "suspend command failed: %d\n", error);
  1102. }
  1103. } else {
  1104. elants_i2c_power_off(ts);
  1105. }
  1106. return 0;
  1107. }
  1108. static int __maybe_unused elants_i2c_resume(struct device *dev)
  1109. {
  1110. struct i2c_client *client = to_i2c_client(dev);
  1111. struct elants_data *ts = i2c_get_clientdata(client);
  1112. const u8 set_active_cmd[] = { 0x54, 0x58, 0x00, 0x01 };
  1113. int retry_cnt;
  1114. int error;
  1115. if (device_may_wakeup(dev)) {
  1116. if (ts->wake_irq_enabled)
  1117. disable_irq_wake(client->irq);
  1118. elants_i2c_sw_reset(client);
  1119. } else if (ts->keep_power_in_suspend) {
  1120. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  1121. error = elants_i2c_send(client, set_active_cmd,
  1122. sizeof(set_active_cmd));
  1123. if (!error)
  1124. break;
  1125. dev_err(&client->dev,
  1126. "resume command failed: %d\n", error);
  1127. }
  1128. } else {
  1129. elants_i2c_power_on(ts);
  1130. elants_i2c_initialize(ts);
  1131. }
  1132. ts->state = ELAN_STATE_NORMAL;
  1133. enable_irq(client->irq);
  1134. return 0;
  1135. }
  1136. static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops,
  1137. elants_i2c_suspend, elants_i2c_resume);
  1138. static const struct i2c_device_id elants_i2c_id[] = {
  1139. { DEVICE_NAME, 0 },
  1140. { }
  1141. };
  1142. MODULE_DEVICE_TABLE(i2c, elants_i2c_id);
  1143. #ifdef CONFIG_ACPI
  1144. static const struct acpi_device_id elants_acpi_id[] = {
  1145. { "ELAN0001", 0 },
  1146. { }
  1147. };
  1148. MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
  1149. #endif
  1150. #ifdef CONFIG_OF
  1151. static const struct of_device_id elants_of_match[] = {
  1152. { .compatible = "elan,ekth3500" },
  1153. { /* sentinel */ }
  1154. };
  1155. MODULE_DEVICE_TABLE(of, elants_of_match);
  1156. #endif
  1157. static struct i2c_driver elants_i2c_driver = {
  1158. .probe = elants_i2c_probe,
  1159. .id_table = elants_i2c_id,
  1160. .driver = {
  1161. .name = DEVICE_NAME,
  1162. .pm = &elants_i2c_pm_ops,
  1163. .acpi_match_table = ACPI_PTR(elants_acpi_id),
  1164. .of_match_table = of_match_ptr(elants_of_match),
  1165. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1166. },
  1167. };
  1168. module_i2c_driver(elants_i2c_driver);
  1169. MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>");
  1170. MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
  1171. MODULE_VERSION(DRV_VERSION);
  1172. MODULE_LICENSE("GPL");