elants_i2c.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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_hw_version(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. if (error) {
  258. dev_err(&client->dev,
  259. "Failed to read fw id: %d\n", error);
  260. return error;
  261. }
  262. dev_err(&client->dev, "Invalid fw id: %#04x\n", ts->hw_version);
  263. return -EINVAL;
  264. }
  265. static int elants_i2c_query_fw_version(struct elants_data *ts)
  266. {
  267. struct i2c_client *client = ts->client;
  268. int error, retry_cnt;
  269. const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 };
  270. u8 resp[HEADER_SIZE];
  271. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  272. error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
  273. resp, sizeof(resp));
  274. if (!error) {
  275. ts->fw_version = elants_i2c_parse_version(resp);
  276. if (ts->fw_version != 0x0000 &&
  277. ts->fw_version != 0xffff)
  278. return 0;
  279. }
  280. dev_dbg(&client->dev, "read fw version error=%d, buf=%*phC\n",
  281. error, (int)sizeof(resp), resp);
  282. }
  283. dev_err(&client->dev,
  284. "Failed to read fw version or fw version is invalid\n");
  285. return -EINVAL;
  286. }
  287. static int elants_i2c_query_test_version(struct elants_data *ts)
  288. {
  289. struct i2c_client *client = ts->client;
  290. int error, retry_cnt;
  291. u16 version;
  292. const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 };
  293. u8 resp[HEADER_SIZE];
  294. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  295. error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
  296. resp, sizeof(resp));
  297. if (!error) {
  298. version = elants_i2c_parse_version(resp);
  299. ts->test_version = version >> 8;
  300. ts->solution_version = version & 0xff;
  301. return 0;
  302. }
  303. dev_dbg(&client->dev,
  304. "read test version error rc=%d, buf=%*phC\n",
  305. error, (int)sizeof(resp), resp);
  306. }
  307. dev_err(&client->dev, "Failed to read test version\n");
  308. return -EINVAL;
  309. }
  310. static int elants_i2c_query_bc_version(struct elants_data *ts)
  311. {
  312. struct i2c_client *client = ts->client;
  313. const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 };
  314. u8 resp[HEADER_SIZE];
  315. u16 version;
  316. int error;
  317. error = elants_i2c_execute_command(client, cmd, sizeof(cmd),
  318. resp, sizeof(resp));
  319. if (error) {
  320. dev_err(&client->dev,
  321. "read BC version error=%d, buf=%*phC\n",
  322. error, (int)sizeof(resp), resp);
  323. return error;
  324. }
  325. version = elants_i2c_parse_version(resp);
  326. ts->bc_version = version >> 8;
  327. ts->iap_version = version & 0xff;
  328. return 0;
  329. }
  330. static int elants_i2c_query_ts_info(struct elants_data *ts)
  331. {
  332. struct i2c_client *client = ts->client;
  333. int error;
  334. u8 resp[17];
  335. u16 phy_x, phy_y, rows, cols, osr;
  336. const u8 get_resolution_cmd[] = {
  337. CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00
  338. };
  339. const u8 get_osr_cmd[] = {
  340. CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01
  341. };
  342. const u8 get_physical_scan_cmd[] = {
  343. CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01
  344. };
  345. const u8 get_physical_drive_cmd[] = {
  346. CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01
  347. };
  348. /* Get trace number */
  349. error = elants_i2c_execute_command(client,
  350. get_resolution_cmd,
  351. sizeof(get_resolution_cmd),
  352. resp, sizeof(resp));
  353. if (error) {
  354. dev_err(&client->dev, "get resolution command failed: %d\n",
  355. error);
  356. return error;
  357. }
  358. rows = resp[2] + resp[6] + resp[10];
  359. cols = resp[3] + resp[7] + resp[11];
  360. /* Process mm_to_pixel information */
  361. error = elants_i2c_execute_command(client,
  362. get_osr_cmd, sizeof(get_osr_cmd),
  363. resp, sizeof(resp));
  364. if (error) {
  365. dev_err(&client->dev, "get osr command failed: %d\n",
  366. error);
  367. return error;
  368. }
  369. osr = resp[3];
  370. error = elants_i2c_execute_command(client,
  371. get_physical_scan_cmd,
  372. sizeof(get_physical_scan_cmd),
  373. resp, sizeof(resp));
  374. if (error) {
  375. dev_err(&client->dev, "get physical scan command failed: %d\n",
  376. error);
  377. return error;
  378. }
  379. phy_x = get_unaligned_be16(&resp[2]);
  380. error = elants_i2c_execute_command(client,
  381. get_physical_drive_cmd,
  382. sizeof(get_physical_drive_cmd),
  383. resp, sizeof(resp));
  384. if (error) {
  385. dev_err(&client->dev, "get physical drive command failed: %d\n",
  386. error);
  387. return error;
  388. }
  389. phy_y = get_unaligned_be16(&resp[2]);
  390. dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y);
  391. if (rows == 0 || cols == 0 || osr == 0) {
  392. dev_warn(&client->dev,
  393. "invalid trace number data: %d, %d, %d\n",
  394. rows, cols, osr);
  395. } else {
  396. /* translate trace number to TS resolution */
  397. ts->x_max = ELAN_TS_RESOLUTION(rows, osr);
  398. ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x);
  399. ts->y_max = ELAN_TS_RESOLUTION(cols, osr);
  400. ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y);
  401. }
  402. return 0;
  403. }
  404. static int elants_i2c_fastboot(struct i2c_client *client)
  405. {
  406. const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E };
  407. int error;
  408. error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd));
  409. if (error) {
  410. dev_err(&client->dev, "boot failed: %d\n", error);
  411. return error;
  412. }
  413. dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr);
  414. return 0;
  415. }
  416. static int elants_i2c_initialize(struct elants_data *ts)
  417. {
  418. struct i2c_client *client = ts->client;
  419. int error, error2, retry_cnt;
  420. const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 };
  421. const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 };
  422. u8 buf[HEADER_SIZE];
  423. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  424. error = elants_i2c_sw_reset(client);
  425. if (error) {
  426. /* Continue initializing if it's the last try */
  427. if (retry_cnt < MAX_RETRIES - 1)
  428. continue;
  429. }
  430. error = elants_i2c_fastboot(client);
  431. if (error) {
  432. /* Continue initializing if it's the last try */
  433. if (retry_cnt < MAX_RETRIES - 1)
  434. continue;
  435. }
  436. /* Wait for Hello packet */
  437. msleep(BOOT_TIME_DELAY_MS);
  438. error = elants_i2c_read(client, buf, sizeof(buf));
  439. if (error) {
  440. dev_err(&client->dev,
  441. "failed to read 'hello' packet: %d\n", error);
  442. } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) {
  443. ts->iap_mode = ELAN_IAP_OPERATIONAL;
  444. break;
  445. } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) {
  446. /*
  447. * Setting error code will mark device
  448. * in recovery mode below.
  449. */
  450. error = -EIO;
  451. break;
  452. } else {
  453. error = -EINVAL;
  454. dev_err(&client->dev,
  455. "invalid 'hello' packet: %*ph\n",
  456. (int)sizeof(buf), buf);
  457. }
  458. }
  459. /* hw version is available even if device in recovery state */
  460. error2 = elants_i2c_query_hw_version(ts);
  461. if (!error)
  462. error = error2;
  463. if (!error)
  464. error = elants_i2c_query_fw_version(ts);
  465. if (!error)
  466. error = elants_i2c_query_test_version(ts);
  467. if (!error)
  468. error = elants_i2c_query_bc_version(ts);
  469. if (!error)
  470. error = elants_i2c_query_ts_info(ts);
  471. if (error)
  472. ts->iap_mode = ELAN_IAP_RECOVERY;
  473. return 0;
  474. }
  475. /*
  476. * Firmware update interface.
  477. */
  478. static int elants_i2c_fw_write_page(struct i2c_client *client,
  479. const void *page)
  480. {
  481. const u8 ack_ok[] = { 0xaa, 0xaa };
  482. u8 buf[2];
  483. int retry;
  484. int error;
  485. for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) {
  486. error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE);
  487. if (error) {
  488. dev_err(&client->dev,
  489. "IAP Write Page failed: %d\n", error);
  490. continue;
  491. }
  492. error = elants_i2c_read(client, buf, 2);
  493. if (error) {
  494. dev_err(&client->dev,
  495. "IAP Ack read failed: %d\n", error);
  496. return error;
  497. }
  498. if (!memcmp(buf, ack_ok, sizeof(ack_ok)))
  499. return 0;
  500. error = -EIO;
  501. dev_err(&client->dev,
  502. "IAP Get Ack Error [%02x:%02x]\n",
  503. buf[0], buf[1]);
  504. }
  505. return error;
  506. }
  507. static int elants_i2c_do_update_firmware(struct i2c_client *client,
  508. const struct firmware *fw,
  509. bool force)
  510. {
  511. const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 };
  512. const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 };
  513. const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc };
  514. const u8 close_idle[] = {0x54, 0x2c, 0x01, 0x01};
  515. u8 buf[HEADER_SIZE];
  516. u16 send_id;
  517. int page, n_fw_pages;
  518. int error;
  519. /* Recovery mode detection! */
  520. if (force) {
  521. dev_dbg(&client->dev, "Recovery mode procedure\n");
  522. error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2));
  523. } else {
  524. /* Start IAP Procedure */
  525. dev_dbg(&client->dev, "Normal IAP procedure\n");
  526. /* Close idle mode */
  527. error = elants_i2c_send(client, close_idle, sizeof(close_idle));
  528. if (error)
  529. dev_err(&client->dev, "Failed close idle: %d\n", error);
  530. msleep(60);
  531. elants_i2c_sw_reset(client);
  532. msleep(20);
  533. error = elants_i2c_send(client, enter_iap, sizeof(enter_iap));
  534. }
  535. if (error) {
  536. dev_err(&client->dev, "failed to enter IAP mode: %d\n", error);
  537. return error;
  538. }
  539. msleep(20);
  540. /* check IAP state */
  541. error = elants_i2c_read(client, buf, 4);
  542. if (error) {
  543. dev_err(&client->dev,
  544. "failed to read IAP acknowledgement: %d\n",
  545. error);
  546. return error;
  547. }
  548. if (memcmp(buf, iap_ack, sizeof(iap_ack))) {
  549. dev_err(&client->dev,
  550. "failed to enter IAP: %*ph (expected %*ph)\n",
  551. (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack);
  552. return -EIO;
  553. }
  554. dev_info(&client->dev, "successfully entered IAP mode");
  555. send_id = client->addr;
  556. error = elants_i2c_send(client, &send_id, 1);
  557. if (error) {
  558. dev_err(&client->dev, "sending dummy byte failed: %d\n",
  559. error);
  560. return error;
  561. }
  562. /* Clear the last page of Master */
  563. error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE);
  564. if (error) {
  565. dev_err(&client->dev, "clearing of the last page failed: %d\n",
  566. error);
  567. return error;
  568. }
  569. error = elants_i2c_read(client, buf, 2);
  570. if (error) {
  571. dev_err(&client->dev,
  572. "failed to read ACK for clearing the last page: %d\n",
  573. error);
  574. return error;
  575. }
  576. n_fw_pages = fw->size / ELAN_FW_PAGESIZE;
  577. dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages);
  578. for (page = 0; page < n_fw_pages; page++) {
  579. error = elants_i2c_fw_write_page(client,
  580. fw->data + page * ELAN_FW_PAGESIZE);
  581. if (error) {
  582. dev_err(&client->dev,
  583. "failed to write FW page %d: %d\n",
  584. page, error);
  585. return error;
  586. }
  587. }
  588. /* Old iap needs to wait 200ms for WDT and rest is for hello packets */
  589. msleep(300);
  590. dev_info(&client->dev, "firmware update completed\n");
  591. return 0;
  592. }
  593. static int elants_i2c_fw_update(struct elants_data *ts)
  594. {
  595. struct i2c_client *client = ts->client;
  596. const struct firmware *fw;
  597. char *fw_name;
  598. int error;
  599. fw_name = kasprintf(GFP_KERNEL, "/*(DEBLOBBED)*/", ts->hw_version);
  600. if (!fw_name)
  601. return -ENOMEM;
  602. dev_info(&client->dev, "requesting fw name = %s\n", fw_name);
  603. error = reject_firmware(&fw, fw_name, &client->dev);
  604. kfree(fw_name);
  605. if (error) {
  606. dev_err(&client->dev, "failed to request firmware: %d\n",
  607. error);
  608. return error;
  609. }
  610. if (fw->size % ELAN_FW_PAGESIZE) {
  611. dev_err(&client->dev, "invalid firmware length: %zu\n",
  612. fw->size);
  613. error = -EINVAL;
  614. goto out;
  615. }
  616. disable_irq(client->irq);
  617. error = elants_i2c_do_update_firmware(client, fw,
  618. ts->iap_mode == ELAN_IAP_RECOVERY);
  619. if (error) {
  620. dev_err(&client->dev, "firmware update failed: %d\n", error);
  621. ts->iap_mode = ELAN_IAP_RECOVERY;
  622. goto out_enable_irq;
  623. }
  624. error = elants_i2c_initialize(ts);
  625. if (error) {
  626. dev_err(&client->dev,
  627. "failed to initialize device after firmware update: %d\n",
  628. error);
  629. ts->iap_mode = ELAN_IAP_RECOVERY;
  630. goto out_enable_irq;
  631. }
  632. ts->iap_mode = ELAN_IAP_OPERATIONAL;
  633. out_enable_irq:
  634. ts->state = ELAN_STATE_NORMAL;
  635. enable_irq(client->irq);
  636. msleep(100);
  637. if (!error)
  638. elants_i2c_calibrate(ts);
  639. out:
  640. release_firmware(fw);
  641. return error;
  642. }
  643. /*
  644. * Event reporting.
  645. */
  646. static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf)
  647. {
  648. struct input_dev *input = ts->input;
  649. unsigned int n_fingers;
  650. u16 finger_state;
  651. int i;
  652. n_fingers = buf[FW_POS_STATE + 1] & 0x0f;
  653. finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) |
  654. buf[FW_POS_STATE];
  655. dev_dbg(&ts->client->dev,
  656. "n_fingers: %u, state: %04x\n", n_fingers, finger_state);
  657. for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) {
  658. if (finger_state & 1) {
  659. unsigned int x, y, p, w;
  660. u8 *pos;
  661. pos = &buf[FW_POS_XY + i * 3];
  662. x = (((u16)pos[0] & 0xf0) << 4) | pos[1];
  663. y = (((u16)pos[0] & 0x0f) << 8) | pos[2];
  664. p = buf[FW_POS_PRESSURE + i];
  665. w = buf[FW_POS_WIDTH + i];
  666. dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n",
  667. i, x, y, p, w);
  668. input_mt_slot(input, i);
  669. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  670. input_event(input, EV_ABS, ABS_MT_POSITION_X, x);
  671. input_event(input, EV_ABS, ABS_MT_POSITION_Y, y);
  672. input_event(input, EV_ABS, ABS_MT_PRESSURE, p);
  673. input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w);
  674. n_fingers--;
  675. }
  676. finger_state >>= 1;
  677. }
  678. input_mt_sync_frame(input);
  679. input_sync(input);
  680. }
  681. static u8 elants_i2c_calculate_checksum(u8 *buf)
  682. {
  683. u8 checksum = 0;
  684. u8 i;
  685. for (i = 0; i < FW_POS_CHECKSUM; i++)
  686. checksum += buf[i];
  687. return checksum;
  688. }
  689. static void elants_i2c_event(struct elants_data *ts, u8 *buf)
  690. {
  691. u8 checksum = elants_i2c_calculate_checksum(buf);
  692. if (unlikely(buf[FW_POS_CHECKSUM] != checksum))
  693. dev_warn(&ts->client->dev,
  694. "%s: invalid checksum for packet %02x: %02x vs. %02x\n",
  695. __func__, buf[FW_POS_HEADER],
  696. checksum, buf[FW_POS_CHECKSUM]);
  697. else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER))
  698. dev_warn(&ts->client->dev,
  699. "%s: unknown packet type: %02x\n",
  700. __func__, buf[FW_POS_HEADER]);
  701. else
  702. elants_i2c_mt_event(ts, buf);
  703. }
  704. static irqreturn_t elants_i2c_irq(int irq, void *_dev)
  705. {
  706. const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 };
  707. struct elants_data *ts = _dev;
  708. struct i2c_client *client = ts->client;
  709. int report_count, report_len;
  710. int i;
  711. int len;
  712. len = i2c_master_recv(client, ts->buf, sizeof(ts->buf));
  713. if (len < 0) {
  714. dev_err(&client->dev, "%s: failed to read data: %d\n",
  715. __func__, len);
  716. goto out;
  717. }
  718. dev_dbg(&client->dev, "%s: packet %*ph\n",
  719. __func__, HEADER_SIZE, ts->buf);
  720. switch (ts->state) {
  721. case ELAN_WAIT_RECALIBRATION:
  722. if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) {
  723. memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp));
  724. complete(&ts->cmd_done);
  725. ts->state = ELAN_STATE_NORMAL;
  726. }
  727. break;
  728. case ELAN_WAIT_QUEUE_HEADER:
  729. if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL)
  730. break;
  731. ts->state = ELAN_STATE_NORMAL;
  732. /* fall through */
  733. case ELAN_STATE_NORMAL:
  734. switch (ts->buf[FW_HDR_TYPE]) {
  735. case CMD_HEADER_HELLO:
  736. case CMD_HEADER_RESP:
  737. case CMD_HEADER_REK:
  738. break;
  739. case QUEUE_HEADER_WAIT:
  740. if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) {
  741. dev_err(&client->dev,
  742. "invalid wait packet %*ph\n",
  743. HEADER_SIZE, ts->buf);
  744. } else {
  745. ts->state = ELAN_WAIT_QUEUE_HEADER;
  746. udelay(30);
  747. }
  748. break;
  749. case QUEUE_HEADER_SINGLE:
  750. elants_i2c_event(ts, &ts->buf[HEADER_SIZE]);
  751. break;
  752. case QUEUE_HEADER_NORMAL:
  753. report_count = ts->buf[FW_HDR_COUNT];
  754. if (report_count == 0 || report_count > 3) {
  755. dev_err(&client->dev,
  756. "bad report count: %*ph\n",
  757. HEADER_SIZE, ts->buf);
  758. break;
  759. }
  760. report_len = ts->buf[FW_HDR_LENGTH] / report_count;
  761. if (report_len != PACKET_SIZE) {
  762. dev_err(&client->dev,
  763. "mismatching report length: %*ph\n",
  764. HEADER_SIZE, ts->buf);
  765. break;
  766. }
  767. for (i = 0; i < report_count; i++) {
  768. u8 *buf = ts->buf + HEADER_SIZE +
  769. i * PACKET_SIZE;
  770. elants_i2c_event(ts, buf);
  771. }
  772. break;
  773. default:
  774. dev_err(&client->dev, "unknown packet %*ph\n",
  775. HEADER_SIZE, ts->buf);
  776. break;
  777. }
  778. break;
  779. }
  780. out:
  781. return IRQ_HANDLED;
  782. }
  783. /*
  784. * sysfs interface
  785. */
  786. static ssize_t calibrate_store(struct device *dev,
  787. struct device_attribute *attr,
  788. const char *buf, size_t count)
  789. {
  790. struct i2c_client *client = to_i2c_client(dev);
  791. struct elants_data *ts = i2c_get_clientdata(client);
  792. int error;
  793. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  794. if (error)
  795. return error;
  796. error = elants_i2c_calibrate(ts);
  797. mutex_unlock(&ts->sysfs_mutex);
  798. return error ?: count;
  799. }
  800. static ssize_t write_update_fw(struct device *dev,
  801. struct device_attribute *attr,
  802. const char *buf, size_t count)
  803. {
  804. struct i2c_client *client = to_i2c_client(dev);
  805. struct elants_data *ts = i2c_get_clientdata(client);
  806. int error;
  807. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  808. if (error)
  809. return error;
  810. error = elants_i2c_fw_update(ts);
  811. dev_dbg(dev, "firmware update result: %d\n", error);
  812. mutex_unlock(&ts->sysfs_mutex);
  813. return error ?: count;
  814. }
  815. static ssize_t show_iap_mode(struct device *dev,
  816. struct device_attribute *attr, char *buf)
  817. {
  818. struct i2c_client *client = to_i2c_client(dev);
  819. struct elants_data *ts = i2c_get_clientdata(client);
  820. return sprintf(buf, "%s\n",
  821. ts->iap_mode == ELAN_IAP_OPERATIONAL ?
  822. "Normal" : "Recovery");
  823. }
  824. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, calibrate_store);
  825. static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL);
  826. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw);
  827. struct elants_version_attribute {
  828. struct device_attribute dattr;
  829. size_t field_offset;
  830. size_t field_size;
  831. };
  832. #define __ELANTS_FIELD_SIZE(_field) \
  833. sizeof(((struct elants_data *)NULL)->_field)
  834. #define __ELANTS_VERIFY_SIZE(_field) \
  835. (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \
  836. __ELANTS_FIELD_SIZE(_field))
  837. #define ELANTS_VERSION_ATTR(_field) \
  838. struct elants_version_attribute elants_ver_attr_##_field = { \
  839. .dattr = __ATTR(_field, S_IRUGO, \
  840. elants_version_attribute_show, NULL), \
  841. .field_offset = offsetof(struct elants_data, _field), \
  842. .field_size = __ELANTS_VERIFY_SIZE(_field), \
  843. }
  844. static ssize_t elants_version_attribute_show(struct device *dev,
  845. struct device_attribute *dattr,
  846. char *buf)
  847. {
  848. struct i2c_client *client = to_i2c_client(dev);
  849. struct elants_data *ts = i2c_get_clientdata(client);
  850. struct elants_version_attribute *attr =
  851. container_of(dattr, struct elants_version_attribute, dattr);
  852. u8 *field = (u8 *)((char *)ts + attr->field_offset);
  853. unsigned int fmt_size;
  854. unsigned int val;
  855. if (attr->field_size == 1) {
  856. val = *field;
  857. fmt_size = 2; /* 2 HEX digits */
  858. } else {
  859. val = *(u16 *)field;
  860. fmt_size = 4; /* 4 HEX digits */
  861. }
  862. return sprintf(buf, "%0*x\n", fmt_size, val);
  863. }
  864. static ELANTS_VERSION_ATTR(fw_version);
  865. static ELANTS_VERSION_ATTR(hw_version);
  866. static ELANTS_VERSION_ATTR(test_version);
  867. static ELANTS_VERSION_ATTR(solution_version);
  868. static ELANTS_VERSION_ATTR(bc_version);
  869. static ELANTS_VERSION_ATTR(iap_version);
  870. static struct attribute *elants_attributes[] = {
  871. &dev_attr_calibrate.attr,
  872. &dev_attr_update_fw.attr,
  873. &dev_attr_iap_mode.attr,
  874. &elants_ver_attr_fw_version.dattr.attr,
  875. &elants_ver_attr_hw_version.dattr.attr,
  876. &elants_ver_attr_test_version.dattr.attr,
  877. &elants_ver_attr_solution_version.dattr.attr,
  878. &elants_ver_attr_bc_version.dattr.attr,
  879. &elants_ver_attr_iap_version.dattr.attr,
  880. NULL
  881. };
  882. static struct attribute_group elants_attribute_group = {
  883. .attrs = elants_attributes,
  884. };
  885. static void elants_i2c_remove_sysfs_group(void *_data)
  886. {
  887. struct elants_data *ts = _data;
  888. sysfs_remove_group(&ts->client->dev.kobj, &elants_attribute_group);
  889. }
  890. static int elants_i2c_power_on(struct elants_data *ts)
  891. {
  892. int error;
  893. /*
  894. * If we do not have reset gpio assume platform firmware
  895. * controls regulators and does power them on for us.
  896. */
  897. if (IS_ERR_OR_NULL(ts->reset_gpio))
  898. return 0;
  899. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  900. error = regulator_enable(ts->vcc33);
  901. if (error) {
  902. dev_err(&ts->client->dev,
  903. "failed to enable vcc33 regulator: %d\n",
  904. error);
  905. goto release_reset_gpio;
  906. }
  907. error = regulator_enable(ts->vccio);
  908. if (error) {
  909. dev_err(&ts->client->dev,
  910. "failed to enable vccio regulator: %d\n",
  911. error);
  912. regulator_disable(ts->vcc33);
  913. goto release_reset_gpio;
  914. }
  915. /*
  916. * We need to wait a bit after powering on controller before
  917. * we are allowed to release reset GPIO.
  918. */
  919. udelay(ELAN_POWERON_DELAY_USEC);
  920. release_reset_gpio:
  921. gpiod_set_value_cansleep(ts->reset_gpio, 0);
  922. if (error)
  923. return error;
  924. msleep(ELAN_RESET_DELAY_MSEC);
  925. return 0;
  926. }
  927. static void elants_i2c_power_off(void *_data)
  928. {
  929. struct elants_data *ts = _data;
  930. if (!IS_ERR_OR_NULL(ts->reset_gpio)) {
  931. /*
  932. * Activate reset gpio to prevent leakage through the
  933. * pin once we shut off power to the controller.
  934. */
  935. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  936. regulator_disable(ts->vccio);
  937. regulator_disable(ts->vcc33);
  938. }
  939. }
  940. static int elants_i2c_probe(struct i2c_client *client,
  941. const struct i2c_device_id *id)
  942. {
  943. union i2c_smbus_data dummy;
  944. struct elants_data *ts;
  945. unsigned long irqflags;
  946. int error;
  947. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  948. dev_err(&client->dev,
  949. "%s: i2c check functionality error\n", DEVICE_NAME);
  950. return -ENXIO;
  951. }
  952. ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL);
  953. if (!ts)
  954. return -ENOMEM;
  955. mutex_init(&ts->sysfs_mutex);
  956. init_completion(&ts->cmd_done);
  957. ts->client = client;
  958. i2c_set_clientdata(client, ts);
  959. ts->vcc33 = devm_regulator_get(&client->dev, "vcc33");
  960. if (IS_ERR(ts->vcc33)) {
  961. error = PTR_ERR(ts->vcc33);
  962. if (error != -EPROBE_DEFER)
  963. dev_err(&client->dev,
  964. "Failed to get 'vcc33' regulator: %d\n",
  965. error);
  966. return error;
  967. }
  968. ts->vccio = devm_regulator_get(&client->dev, "vccio");
  969. if (IS_ERR(ts->vccio)) {
  970. error = PTR_ERR(ts->vccio);
  971. if (error != -EPROBE_DEFER)
  972. dev_err(&client->dev,
  973. "Failed to get 'vccio' regulator: %d\n",
  974. error);
  975. return error;
  976. }
  977. ts->reset_gpio = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_LOW);
  978. if (IS_ERR(ts->reset_gpio)) {
  979. error = PTR_ERR(ts->reset_gpio);
  980. if (error == -EPROBE_DEFER)
  981. return error;
  982. if (error != -ENOENT && error != -ENOSYS) {
  983. dev_err(&client->dev,
  984. "failed to get reset gpio: %d\n",
  985. error);
  986. return error;
  987. }
  988. ts->keep_power_in_suspend = true;
  989. }
  990. error = elants_i2c_power_on(ts);
  991. if (error)
  992. return error;
  993. error = devm_add_action(&client->dev, elants_i2c_power_off, ts);
  994. if (error) {
  995. dev_err(&client->dev,
  996. "failed to install power off action: %d\n", error);
  997. elants_i2c_power_off(ts);
  998. return error;
  999. }
  1000. /* Make sure there is something at this address */
  1001. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  1002. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
  1003. dev_err(&client->dev, "nothing at this address\n");
  1004. return -ENXIO;
  1005. }
  1006. error = elants_i2c_initialize(ts);
  1007. if (error) {
  1008. dev_err(&client->dev, "failed to initialize: %d\n", error);
  1009. return error;
  1010. }
  1011. ts->input = devm_input_allocate_device(&client->dev);
  1012. if (!ts->input) {
  1013. dev_err(&client->dev, "Failed to allocate input device\n");
  1014. return -ENOMEM;
  1015. }
  1016. ts->input->name = "Elan Touchscreen";
  1017. ts->input->id.bustype = BUS_I2C;
  1018. __set_bit(BTN_TOUCH, ts->input->keybit);
  1019. __set_bit(EV_ABS, ts->input->evbit);
  1020. __set_bit(EV_KEY, ts->input->evbit);
  1021. /* Single touch input params setup */
  1022. input_set_abs_params(ts->input, ABS_X, 0, ts->x_max, 0, 0);
  1023. input_set_abs_params(ts->input, ABS_Y, 0, ts->y_max, 0, 0);
  1024. input_set_abs_params(ts->input, ABS_PRESSURE, 0, 255, 0, 0);
  1025. input_abs_set_res(ts->input, ABS_X, ts->x_res);
  1026. input_abs_set_res(ts->input, ABS_Y, ts->y_res);
  1027. /* Multitouch input params setup */
  1028. error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM,
  1029. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  1030. if (error) {
  1031. dev_err(&client->dev,
  1032. "failed to initialize MT slots: %d\n", error);
  1033. return error;
  1034. }
  1035. input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0);
  1036. input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0);
  1037. input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  1038. input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  1039. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res);
  1040. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res);
  1041. input_set_drvdata(ts->input, ts);
  1042. error = input_register_device(ts->input);
  1043. if (error) {
  1044. dev_err(&client->dev,
  1045. "unable to register input device: %d\n", error);
  1046. return error;
  1047. }
  1048. /*
  1049. * Systems using device tree should set up interrupt via DTS,
  1050. * the rest will use the default falling edge interrupts.
  1051. */
  1052. irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
  1053. error = devm_request_threaded_irq(&client->dev, client->irq,
  1054. NULL, elants_i2c_irq,
  1055. irqflags | IRQF_ONESHOT,
  1056. client->name, ts);
  1057. if (error) {
  1058. dev_err(&client->dev, "Failed to register interrupt\n");
  1059. return error;
  1060. }
  1061. /*
  1062. * Systems using device tree should set up wakeup via DTS,
  1063. * the rest will configure device as wakeup source by default.
  1064. */
  1065. if (!client->dev.of_node)
  1066. device_init_wakeup(&client->dev, true);
  1067. error = sysfs_create_group(&client->dev.kobj, &elants_attribute_group);
  1068. if (error) {
  1069. dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
  1070. error);
  1071. return error;
  1072. }
  1073. error = devm_add_action(&client->dev,
  1074. elants_i2c_remove_sysfs_group, ts);
  1075. if (error) {
  1076. elants_i2c_remove_sysfs_group(ts);
  1077. dev_err(&client->dev,
  1078. "Failed to add sysfs cleanup action: %d\n",
  1079. error);
  1080. return error;
  1081. }
  1082. return 0;
  1083. }
  1084. static int __maybe_unused elants_i2c_suspend(struct device *dev)
  1085. {
  1086. struct i2c_client *client = to_i2c_client(dev);
  1087. struct elants_data *ts = i2c_get_clientdata(client);
  1088. const u8 set_sleep_cmd[] = { 0x54, 0x50, 0x00, 0x01 };
  1089. int retry_cnt;
  1090. int error;
  1091. /* Command not support in IAP recovery mode */
  1092. if (ts->iap_mode != ELAN_IAP_OPERATIONAL)
  1093. return -EBUSY;
  1094. disable_irq(client->irq);
  1095. if (device_may_wakeup(dev)) {
  1096. /*
  1097. * The device will automatically enter idle mode
  1098. * that has reduced power consumption.
  1099. */
  1100. ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
  1101. } else if (ts->keep_power_in_suspend) {
  1102. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  1103. error = elants_i2c_send(client, set_sleep_cmd,
  1104. sizeof(set_sleep_cmd));
  1105. if (!error)
  1106. break;
  1107. dev_err(&client->dev,
  1108. "suspend command failed: %d\n", error);
  1109. }
  1110. } else {
  1111. elants_i2c_power_off(ts);
  1112. }
  1113. return 0;
  1114. }
  1115. static int __maybe_unused elants_i2c_resume(struct device *dev)
  1116. {
  1117. struct i2c_client *client = to_i2c_client(dev);
  1118. struct elants_data *ts = i2c_get_clientdata(client);
  1119. const u8 set_active_cmd[] = { 0x54, 0x58, 0x00, 0x01 };
  1120. int retry_cnt;
  1121. int error;
  1122. if (device_may_wakeup(dev)) {
  1123. if (ts->wake_irq_enabled)
  1124. disable_irq_wake(client->irq);
  1125. elants_i2c_sw_reset(client);
  1126. } else if (ts->keep_power_in_suspend) {
  1127. for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
  1128. error = elants_i2c_send(client, set_active_cmd,
  1129. sizeof(set_active_cmd));
  1130. if (!error)
  1131. break;
  1132. dev_err(&client->dev,
  1133. "resume command failed: %d\n", error);
  1134. }
  1135. } else {
  1136. elants_i2c_power_on(ts);
  1137. elants_i2c_initialize(ts);
  1138. }
  1139. ts->state = ELAN_STATE_NORMAL;
  1140. enable_irq(client->irq);
  1141. return 0;
  1142. }
  1143. static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops,
  1144. elants_i2c_suspend, elants_i2c_resume);
  1145. static const struct i2c_device_id elants_i2c_id[] = {
  1146. { DEVICE_NAME, 0 },
  1147. { }
  1148. };
  1149. MODULE_DEVICE_TABLE(i2c, elants_i2c_id);
  1150. #ifdef CONFIG_ACPI
  1151. static const struct acpi_device_id elants_acpi_id[] = {
  1152. { "ELAN0001", 0 },
  1153. { }
  1154. };
  1155. MODULE_DEVICE_TABLE(acpi, elants_acpi_id);
  1156. #endif
  1157. #ifdef CONFIG_OF
  1158. static const struct of_device_id elants_of_match[] = {
  1159. { .compatible = "elan,ekth3500" },
  1160. { /* sentinel */ }
  1161. };
  1162. MODULE_DEVICE_TABLE(of, elants_of_match);
  1163. #endif
  1164. static struct i2c_driver elants_i2c_driver = {
  1165. .probe = elants_i2c_probe,
  1166. .id_table = elants_i2c_id,
  1167. .driver = {
  1168. .name = DEVICE_NAME,
  1169. .pm = &elants_i2c_pm_ops,
  1170. .acpi_match_table = ACPI_PTR(elants_acpi_id),
  1171. .of_match_table = of_match_ptr(elants_of_match),
  1172. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1173. },
  1174. };
  1175. module_i2c_driver(elants_i2c_driver);
  1176. MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>");
  1177. MODULE_DESCRIPTION("Elan I2c Touchscreen driver");
  1178. MODULE_VERSION(DRV_VERSION);
  1179. MODULE_LICENSE("GPL");