raydium_i2c_ts.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. * Raydium touchscreen I2C driver.
  3. *
  4. * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2, and only version 2, as published by the
  9. * Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Raydium reserves the right to make changes without further notice
  17. * to the materials described herein. Raydium does not assume any
  18. * liability arising out of the application described herein.
  19. *
  20. * Contact Raydium Semiconductor Corporation at www.rad-ic.com
  21. */
  22. #include <linux/acpi.h>
  23. #include <linux/delay.h>
  24. #include <linux/firmware.h>
  25. #include <linux/gpio/consumer.h>
  26. #include <linux/i2c.h>
  27. #include <linux/input.h>
  28. #include <linux/input/mt.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/regulator/consumer.h>
  33. #include <linux/slab.h>
  34. #include <asm/unaligned.h>
  35. /* Slave I2C mode */
  36. #define RM_BOOT_BLDR 0x02
  37. #define RM_BOOT_MAIN 0x03
  38. /* I2C bootoloader commands */
  39. #define RM_CMD_BOOT_PAGE_WRT 0x0B /* send bl page write */
  40. #define RM_CMD_BOOT_WRT 0x11 /* send bl write */
  41. #define RM_CMD_BOOT_ACK 0x22 /* send ack*/
  42. #define RM_CMD_BOOT_CHK 0x33 /* send data check */
  43. #define RM_CMD_BOOT_READ 0x44 /* send wait bl data ready*/
  44. #define RM_BOOT_RDY 0xFF /* bl data ready */
  45. /* I2C main commands */
  46. #define RM_CMD_QUERY_BANK 0x2B
  47. #define RM_CMD_DATA_BANK 0x4D
  48. #define RM_CMD_ENTER_SLEEP 0x4E
  49. #define RM_CMD_BANK_SWITCH 0xAA
  50. #define RM_RESET_MSG_ADDR 0x40000004
  51. #define RM_MAX_READ_SIZE 56
  52. #define RM_PACKET_CRC_SIZE 2
  53. /* Touch relative info */
  54. #define RM_MAX_RETRIES 3
  55. #define RM_MAX_TOUCH_NUM 10
  56. #define RM_BOOT_DELAY_MS 100
  57. /* Offsets in contact data */
  58. #define RM_CONTACT_STATE_POS 0
  59. #define RM_CONTACT_X_POS 1
  60. #define RM_CONTACT_Y_POS 3
  61. #define RM_CONTACT_PRESSURE_POS 5
  62. #define RM_CONTACT_WIDTH_X_POS 6
  63. #define RM_CONTACT_WIDTH_Y_POS 7
  64. /* Bootloader relative info */
  65. #define RM_BL_WRT_CMD_SIZE 3 /* bl flash wrt cmd size */
  66. #define RM_BL_WRT_PKG_SIZE 32 /* bl wrt pkg size */
  67. #define RM_BL_WRT_LEN (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
  68. #define RM_FW_PAGE_SIZE 128
  69. #define RM_MAX_FW_RETRIES 30
  70. #define RM_MAX_FW_SIZE 0xD000
  71. #define RM_POWERON_DELAY_USEC 500
  72. #define RM_RESET_DELAY_MSEC 50
  73. enum raydium_bl_cmd {
  74. BL_HEADER = 0,
  75. BL_PAGE_STR,
  76. BL_PKG_IDX,
  77. BL_DATA_STR,
  78. };
  79. enum raydium_bl_ack {
  80. RAYDIUM_ACK_NULL = 0,
  81. RAYDIUM_WAIT_READY,
  82. RAYDIUM_PATH_READY,
  83. };
  84. enum raydium_boot_mode {
  85. RAYDIUM_TS_MAIN = 0,
  86. RAYDIUM_TS_BLDR,
  87. };
  88. /* Response to RM_CMD_DATA_BANK request */
  89. struct raydium_data_info {
  90. __le32 data_bank_addr;
  91. u8 pkg_size;
  92. u8 tp_info_size;
  93. };
  94. struct raydium_info {
  95. __le32 hw_ver; /*device version */
  96. u8 main_ver;
  97. u8 sub_ver;
  98. __le16 ft_ver; /* test version */
  99. u8 x_num;
  100. u8 y_num;
  101. __le16 x_max;
  102. __le16 y_max;
  103. u8 x_res; /* units/mm */
  104. u8 y_res; /* units/mm */
  105. };
  106. /* struct raydium_data - represents state of Raydium touchscreen device */
  107. struct raydium_data {
  108. struct i2c_client *client;
  109. struct input_dev *input;
  110. struct regulator *avdd;
  111. struct regulator *vccio;
  112. struct gpio_desc *reset_gpio;
  113. struct raydium_info info;
  114. struct mutex sysfs_mutex;
  115. u8 *report_data;
  116. u32 data_bank_addr;
  117. u8 report_size;
  118. u8 contact_size;
  119. u8 pkg_size;
  120. enum raydium_boot_mode boot_mode;
  121. bool wake_irq_enabled;
  122. };
  123. static int raydium_i2c_send(struct i2c_client *client,
  124. u8 addr, const void *data, size_t len)
  125. {
  126. u8 *buf;
  127. int tries = 0;
  128. int ret;
  129. buf = kmalloc(len + 1, GFP_KERNEL);
  130. if (!buf)
  131. return -ENOMEM;
  132. buf[0] = addr;
  133. memcpy(buf + 1, data, len);
  134. do {
  135. ret = i2c_master_send(client, buf, len + 1);
  136. if (likely(ret == len + 1))
  137. break;
  138. msleep(20);
  139. } while (++tries < RM_MAX_RETRIES);
  140. kfree(buf);
  141. if (unlikely(ret != len + 1)) {
  142. if (ret >= 0)
  143. ret = -EIO;
  144. dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
  145. return ret;
  146. }
  147. return 0;
  148. }
  149. static int raydium_i2c_read(struct i2c_client *client,
  150. u8 addr, void *data, size_t len)
  151. {
  152. struct i2c_msg xfer[] = {
  153. {
  154. .addr = client->addr,
  155. .len = 1,
  156. .buf = &addr,
  157. },
  158. {
  159. .addr = client->addr,
  160. .flags = I2C_M_RD,
  161. .len = len,
  162. .buf = data,
  163. }
  164. };
  165. int ret;
  166. ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
  167. if (unlikely(ret != ARRAY_SIZE(xfer)))
  168. return ret < 0 ? ret : -EIO;
  169. return 0;
  170. }
  171. static int raydium_i2c_read_message(struct i2c_client *client,
  172. u32 addr, void *data, size_t len)
  173. {
  174. __be32 be_addr;
  175. size_t xfer_len;
  176. int error;
  177. while (len) {
  178. xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
  179. be_addr = cpu_to_be32(addr);
  180. error = raydium_i2c_send(client, RM_CMD_BANK_SWITCH,
  181. &be_addr, sizeof(be_addr));
  182. if (!error)
  183. error = raydium_i2c_read(client, addr & 0xff,
  184. data, xfer_len);
  185. if (error)
  186. return error;
  187. len -= xfer_len;
  188. data += xfer_len;
  189. addr += xfer_len;
  190. }
  191. return 0;
  192. }
  193. static int raydium_i2c_send_message(struct i2c_client *client,
  194. u32 addr, const void *data, size_t len)
  195. {
  196. __be32 be_addr = cpu_to_be32(addr);
  197. int error;
  198. error = raydium_i2c_send(client, RM_CMD_BANK_SWITCH,
  199. &be_addr, sizeof(be_addr));
  200. if (!error)
  201. error = raydium_i2c_send(client, addr & 0xff, data, len);
  202. return error;
  203. }
  204. static int raydium_i2c_sw_reset(struct i2c_client *client)
  205. {
  206. const u8 soft_rst_cmd = 0x01;
  207. int error;
  208. error = raydium_i2c_send_message(client, RM_RESET_MSG_ADDR,
  209. &soft_rst_cmd, sizeof(soft_rst_cmd));
  210. if (error) {
  211. dev_err(&client->dev, "software reset failed: %d\n", error);
  212. return error;
  213. }
  214. msleep(RM_RESET_DELAY_MSEC);
  215. return 0;
  216. }
  217. static int raydium_i2c_query_ts_info(struct raydium_data *ts)
  218. {
  219. struct i2c_client *client = ts->client;
  220. struct raydium_data_info data_info;
  221. __le32 query_bank_addr;
  222. int error, retry_cnt;
  223. for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
  224. error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
  225. &data_info, sizeof(data_info));
  226. if (error)
  227. continue;
  228. /*
  229. * Warn user if we already allocated memory for reports and
  230. * then the size changed (due to firmware update?) and keep
  231. * old size instead.
  232. */
  233. if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
  234. dev_warn(&client->dev,
  235. "report size changes, was: %d, new: %d\n",
  236. ts->pkg_size, data_info.pkg_size);
  237. } else {
  238. ts->pkg_size = data_info.pkg_size;
  239. ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
  240. }
  241. ts->contact_size = data_info.tp_info_size;
  242. ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
  243. dev_dbg(&client->dev,
  244. "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
  245. ts->data_bank_addr, ts->report_size, ts->contact_size);
  246. error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
  247. &query_bank_addr,
  248. sizeof(query_bank_addr));
  249. if (error)
  250. continue;
  251. error = raydium_i2c_read_message(client,
  252. le32_to_cpu(query_bank_addr),
  253. &ts->info, sizeof(ts->info));
  254. if (error)
  255. continue;
  256. return 0;
  257. }
  258. dev_err(&client->dev, "failed to query device parameters: %d\n", error);
  259. return error;
  260. }
  261. static int raydium_i2c_check_fw_status(struct raydium_data *ts)
  262. {
  263. struct i2c_client *client = ts->client;
  264. static const u8 bl_ack = 0x62;
  265. static const u8 main_ack = 0x66;
  266. u8 buf[4];
  267. int error;
  268. error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
  269. if (!error) {
  270. if (buf[0] == bl_ack)
  271. ts->boot_mode = RAYDIUM_TS_BLDR;
  272. else if (buf[0] == main_ack)
  273. ts->boot_mode = RAYDIUM_TS_MAIN;
  274. return 0;
  275. }
  276. return error;
  277. }
  278. static int raydium_i2c_initialize(struct raydium_data *ts)
  279. {
  280. struct i2c_client *client = ts->client;
  281. int error, retry_cnt;
  282. for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
  283. /* Wait for Hello packet */
  284. msleep(RM_BOOT_DELAY_MS);
  285. error = raydium_i2c_check_fw_status(ts);
  286. if (error) {
  287. dev_err(&client->dev,
  288. "failed to read 'hello' packet: %d\n", error);
  289. continue;
  290. }
  291. if (ts->boot_mode == RAYDIUM_TS_BLDR ||
  292. ts->boot_mode == RAYDIUM_TS_MAIN) {
  293. break;
  294. }
  295. }
  296. if (error)
  297. ts->boot_mode = RAYDIUM_TS_BLDR;
  298. if (ts->boot_mode == RAYDIUM_TS_BLDR) {
  299. ts->info.hw_ver = cpu_to_le32(0xffffffffUL);
  300. ts->info.main_ver = 0xff;
  301. ts->info.sub_ver = 0xff;
  302. } else {
  303. raydium_i2c_query_ts_info(ts);
  304. }
  305. return error;
  306. }
  307. static int raydium_i2c_bl_chk_state(struct i2c_client *client,
  308. enum raydium_bl_ack state)
  309. {
  310. static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
  311. u8 rbuf[sizeof(ack_ok)];
  312. u8 retry;
  313. int error;
  314. for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
  315. switch (state) {
  316. case RAYDIUM_ACK_NULL:
  317. return 0;
  318. case RAYDIUM_WAIT_READY:
  319. error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
  320. &rbuf[0], 1);
  321. if (!error && rbuf[0] == RM_BOOT_RDY)
  322. return 0;
  323. break;
  324. case RAYDIUM_PATH_READY:
  325. error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
  326. rbuf, sizeof(rbuf));
  327. if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
  328. return 0;
  329. break;
  330. default:
  331. dev_err(&client->dev, "%s: invalid target state %d\n",
  332. __func__, state);
  333. return -EINVAL;
  334. }
  335. msleep(20);
  336. }
  337. return -ETIMEDOUT;
  338. }
  339. static int raydium_i2c_write_object(struct i2c_client *client,
  340. const void *data, size_t len,
  341. enum raydium_bl_ack state)
  342. {
  343. int error;
  344. static const u8 cmd[] = { 0xFF, 0x39 };
  345. error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
  346. if (error) {
  347. dev_err(&client->dev, "WRT obj command failed: %d\n",
  348. error);
  349. return error;
  350. }
  351. error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
  352. if (error) {
  353. dev_err(&client->dev, "Ack obj command failed: %d\n", error);
  354. return error;
  355. }
  356. error = raydium_i2c_bl_chk_state(client, state);
  357. if (error) {
  358. dev_err(&client->dev, "BL check state failed: %d\n", error);
  359. return error;
  360. }
  361. return 0;
  362. }
  363. static int raydium_i2c_boot_trigger(struct i2c_client *client)
  364. {
  365. static const u8 cmd[7][6] = {
  366. { 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
  367. { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
  368. { 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
  369. { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
  370. { 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
  371. { 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
  372. { 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
  373. };
  374. int i;
  375. int error;
  376. for (i = 0; i < 7; i++) {
  377. error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
  378. RAYDIUM_WAIT_READY);
  379. if (error) {
  380. dev_err(&client->dev,
  381. "boot trigger failed at step %d: %d\n",
  382. i, error);
  383. return error;
  384. }
  385. }
  386. return 0;
  387. }
  388. static int raydium_i2c_fw_trigger(struct i2c_client *client)
  389. {
  390. static const u8 cmd[5][11] = {
  391. { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
  392. { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
  393. { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
  394. { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
  395. { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
  396. };
  397. int i;
  398. int error;
  399. for (i = 0; i < 5; i++) {
  400. error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
  401. RAYDIUM_ACK_NULL);
  402. if (error) {
  403. dev_err(&client->dev,
  404. "fw trigger failed at step %d: %d\n",
  405. i, error);
  406. return error;
  407. }
  408. }
  409. return 0;
  410. }
  411. static int raydium_i2c_check_path(struct i2c_client *client)
  412. {
  413. static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
  414. int error;
  415. error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
  416. RAYDIUM_PATH_READY);
  417. if (error) {
  418. dev_err(&client->dev, "check path command failed: %d\n", error);
  419. return error;
  420. }
  421. return 0;
  422. }
  423. static int raydium_i2c_enter_bl(struct i2c_client *client)
  424. {
  425. static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
  426. int error;
  427. error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
  428. RAYDIUM_ACK_NULL);
  429. if (error) {
  430. dev_err(&client->dev, "enter bl command failed: %d\n", error);
  431. return error;
  432. }
  433. msleep(RM_BOOT_DELAY_MS);
  434. return 0;
  435. }
  436. static int raydium_i2c_leave_bl(struct i2c_client *client)
  437. {
  438. static const u8 leave_cmd[] = { 0x05, 0x00 };
  439. int error;
  440. error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
  441. RAYDIUM_ACK_NULL);
  442. if (error) {
  443. dev_err(&client->dev, "leave bl command failed: %d\n", error);
  444. return error;
  445. }
  446. msleep(RM_BOOT_DELAY_MS);
  447. return 0;
  448. }
  449. static int raydium_i2c_write_checksum(struct i2c_client *client,
  450. size_t length, u16 checksum)
  451. {
  452. u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
  453. int error;
  454. put_unaligned_le16(length, &checksum_cmd[3]);
  455. put_unaligned_le16(checksum, &checksum_cmd[5]);
  456. error = raydium_i2c_write_object(client,
  457. checksum_cmd, sizeof(checksum_cmd),
  458. RAYDIUM_ACK_NULL);
  459. if (error) {
  460. dev_err(&client->dev, "failed to write checksum: %d\n",
  461. error);
  462. return error;
  463. }
  464. return 0;
  465. }
  466. static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
  467. {
  468. static const u8 cmd[] = { 0x0A, 0xAA };
  469. int error;
  470. error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
  471. RAYDIUM_WAIT_READY);
  472. if (error) {
  473. dev_err(&client->dev, "disable watchdog command failed: %d\n",
  474. error);
  475. return error;
  476. }
  477. return 0;
  478. }
  479. static int raydium_i2c_fw_write_page(struct i2c_client *client,
  480. u16 page_idx, const void *data, size_t len)
  481. {
  482. u8 buf[RM_BL_WRT_LEN];
  483. size_t xfer_len;
  484. int error;
  485. int i;
  486. BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
  487. for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
  488. buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
  489. buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
  490. buf[BL_PKG_IDX] = i + 1;
  491. xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
  492. memcpy(&buf[BL_DATA_STR], data, xfer_len);
  493. if (len < RM_BL_WRT_PKG_SIZE)
  494. memset(&buf[BL_DATA_STR + xfer_len], 0xff,
  495. RM_BL_WRT_PKG_SIZE - xfer_len);
  496. error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
  497. RAYDIUM_WAIT_READY);
  498. if (error) {
  499. dev_err(&client->dev,
  500. "page write command failed for page %d, chunk %d: %d\n",
  501. page_idx, i, error);
  502. return error;
  503. }
  504. data += xfer_len;
  505. len -= xfer_len;
  506. }
  507. return error;
  508. }
  509. static u16 raydium_calc_chksum(const u8 *buf, u16 len)
  510. {
  511. u16 checksum = 0;
  512. u16 i;
  513. for (i = 0; i < len; i++)
  514. checksum += buf[i];
  515. return checksum;
  516. }
  517. static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
  518. const struct firmware *fw)
  519. {
  520. struct i2c_client *client = ts->client;
  521. const void *data;
  522. size_t data_len;
  523. size_t len;
  524. int page_nr;
  525. int i;
  526. int error;
  527. u16 fw_checksum;
  528. if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
  529. dev_err(&client->dev, "Invalid firmware length\n");
  530. return -EINVAL;
  531. }
  532. error = raydium_i2c_check_fw_status(ts);
  533. if (error) {
  534. dev_err(&client->dev, "Unable to access IC %d\n", error);
  535. return error;
  536. }
  537. if (ts->boot_mode == RAYDIUM_TS_MAIN) {
  538. for (i = 0; i < RM_MAX_RETRIES; i++) {
  539. error = raydium_i2c_enter_bl(client);
  540. if (!error) {
  541. error = raydium_i2c_check_fw_status(ts);
  542. if (error) {
  543. dev_err(&client->dev,
  544. "unable to access IC: %d\n",
  545. error);
  546. return error;
  547. }
  548. if (ts->boot_mode == RAYDIUM_TS_BLDR)
  549. break;
  550. }
  551. }
  552. if (ts->boot_mode == RAYDIUM_TS_MAIN) {
  553. dev_err(&client->dev,
  554. "failed to jump to boot loader: %d\n",
  555. error);
  556. return -EIO;
  557. }
  558. }
  559. error = raydium_i2c_disable_watch_dog(client);
  560. if (error)
  561. return error;
  562. error = raydium_i2c_check_path(client);
  563. if (error)
  564. return error;
  565. error = raydium_i2c_boot_trigger(client);
  566. if (error) {
  567. dev_err(&client->dev, "send boot trigger fail: %d\n", error);
  568. return error;
  569. }
  570. msleep(RM_BOOT_DELAY_MS);
  571. data = fw->data;
  572. data_len = fw->size;
  573. page_nr = 0;
  574. while (data_len) {
  575. len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
  576. error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
  577. if (error)
  578. return error;
  579. msleep(20);
  580. data += len;
  581. data_len -= len;
  582. }
  583. error = raydium_i2c_leave_bl(client);
  584. if (error) {
  585. dev_err(&client->dev,
  586. "failed to leave boot loader: %d\n", error);
  587. return error;
  588. }
  589. dev_dbg(&client->dev, "left boot loader mode\n");
  590. msleep(RM_BOOT_DELAY_MS);
  591. error = raydium_i2c_check_fw_status(ts);
  592. if (error) {
  593. dev_err(&client->dev,
  594. "failed to check fw status after write: %d\n",
  595. error);
  596. return error;
  597. }
  598. if (ts->boot_mode != RAYDIUM_TS_MAIN) {
  599. dev_err(&client->dev,
  600. "failed to switch to main fw after writing firmware: %d\n",
  601. error);
  602. return -EINVAL;
  603. }
  604. error = raydium_i2c_fw_trigger(client);
  605. if (error) {
  606. dev_err(&client->dev, "failed to trigger fw: %d\n", error);
  607. return error;
  608. }
  609. fw_checksum = raydium_calc_chksum(fw->data, fw->size);
  610. error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
  611. if (error)
  612. return error;
  613. return 0;
  614. }
  615. static int raydium_i2c_fw_update(struct raydium_data *ts)
  616. {
  617. struct i2c_client *client = ts->client;
  618. const struct firmware *fw = NULL;
  619. char *fw_file;
  620. int error;
  621. fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
  622. le32_to_cpu(ts->info.hw_ver));
  623. if (!fw_file)
  624. return -ENOMEM;
  625. dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
  626. error = request_firmware(&fw, fw_file, &client->dev);
  627. if (error) {
  628. dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
  629. goto out_free_fw_file;
  630. }
  631. disable_irq(client->irq);
  632. error = raydium_i2c_do_update_firmware(ts, fw);
  633. if (error) {
  634. dev_err(&client->dev, "firmware update failed: %d\n", error);
  635. ts->boot_mode = RAYDIUM_TS_BLDR;
  636. goto out_enable_irq;
  637. }
  638. error = raydium_i2c_initialize(ts);
  639. if (error) {
  640. dev_err(&client->dev,
  641. "failed to initialize device after firmware update: %d\n",
  642. error);
  643. ts->boot_mode = RAYDIUM_TS_BLDR;
  644. goto out_enable_irq;
  645. }
  646. ts->boot_mode = RAYDIUM_TS_MAIN;
  647. out_enable_irq:
  648. enable_irq(client->irq);
  649. msleep(100);
  650. release_firmware(fw);
  651. out_free_fw_file:
  652. kfree(fw_file);
  653. return error;
  654. }
  655. static void raydium_mt_event(struct raydium_data *ts)
  656. {
  657. int i;
  658. for (i = 0; i < ts->report_size / ts->contact_size; i++) {
  659. u8 *contact = &ts->report_data[ts->contact_size * i];
  660. bool state = contact[RM_CONTACT_STATE_POS];
  661. u8 wx, wy;
  662. input_mt_slot(ts->input, i);
  663. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
  664. if (!state)
  665. continue;
  666. input_report_abs(ts->input, ABS_MT_POSITION_X,
  667. get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
  668. input_report_abs(ts->input, ABS_MT_POSITION_Y,
  669. get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
  670. input_report_abs(ts->input, ABS_MT_PRESSURE,
  671. contact[RM_CONTACT_PRESSURE_POS]);
  672. wx = contact[RM_CONTACT_WIDTH_X_POS];
  673. wy = contact[RM_CONTACT_WIDTH_Y_POS];
  674. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
  675. input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
  676. }
  677. input_mt_sync_frame(ts->input);
  678. input_sync(ts->input);
  679. }
  680. static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
  681. {
  682. struct raydium_data *ts = _dev;
  683. int error;
  684. u16 fw_crc;
  685. u16 calc_crc;
  686. if (ts->boot_mode != RAYDIUM_TS_MAIN)
  687. goto out;
  688. error = raydium_i2c_read_message(ts->client, ts->data_bank_addr,
  689. ts->report_data, ts->pkg_size);
  690. if (error)
  691. goto out;
  692. fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
  693. calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
  694. if (unlikely(fw_crc != calc_crc)) {
  695. dev_warn(&ts->client->dev,
  696. "%s: invalid packet crc %#04x vs %#04x\n",
  697. __func__, calc_crc, fw_crc);
  698. goto out;
  699. }
  700. raydium_mt_event(ts);
  701. out:
  702. return IRQ_HANDLED;
  703. }
  704. static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
  705. struct device_attribute *attr, char *buf)
  706. {
  707. struct i2c_client *client = to_i2c_client(dev);
  708. struct raydium_data *ts = i2c_get_clientdata(client);
  709. return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
  710. }
  711. static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
  712. struct device_attribute *attr, char *buf)
  713. {
  714. struct i2c_client *client = to_i2c_client(dev);
  715. struct raydium_data *ts = i2c_get_clientdata(client);
  716. return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
  717. }
  718. static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
  719. struct device_attribute *attr,
  720. char *buf)
  721. {
  722. struct i2c_client *client = to_i2c_client(dev);
  723. struct raydium_data *ts = i2c_get_clientdata(client);
  724. return sprintf(buf, "%s\n",
  725. ts->boot_mode == RAYDIUM_TS_MAIN ?
  726. "Normal" : "Recovery");
  727. }
  728. static ssize_t raydium_i2c_update_fw_store(struct device *dev,
  729. struct device_attribute *attr,
  730. const char *buf, size_t count)
  731. {
  732. struct i2c_client *client = to_i2c_client(dev);
  733. struct raydium_data *ts = i2c_get_clientdata(client);
  734. int error;
  735. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  736. if (error)
  737. return error;
  738. error = raydium_i2c_fw_update(ts);
  739. mutex_unlock(&ts->sysfs_mutex);
  740. return error ?: count;
  741. }
  742. static ssize_t raydium_i2c_calibrate_store(struct device *dev,
  743. struct device_attribute *attr,
  744. const char *buf, size_t count)
  745. {
  746. struct i2c_client *client = to_i2c_client(dev);
  747. struct raydium_data *ts = i2c_get_clientdata(client);
  748. static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
  749. int error;
  750. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  751. if (error)
  752. return error;
  753. error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
  754. RAYDIUM_WAIT_READY);
  755. if (error)
  756. dev_err(&client->dev, "calibrate command failed: %d\n", error);
  757. mutex_unlock(&ts->sysfs_mutex);
  758. return error ?: count;
  759. }
  760. static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
  761. static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
  762. static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
  763. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
  764. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
  765. static struct attribute *raydium_i2c_attributes[] = {
  766. &dev_attr_update_fw.attr,
  767. &dev_attr_boot_mode.attr,
  768. &dev_attr_fw_version.attr,
  769. &dev_attr_hw_version.attr,
  770. &dev_attr_calibrate.attr,
  771. NULL
  772. };
  773. static const struct attribute_group raydium_i2c_attribute_group = {
  774. .attrs = raydium_i2c_attributes,
  775. };
  776. static int raydium_i2c_power_on(struct raydium_data *ts)
  777. {
  778. int error;
  779. if (!ts->reset_gpio)
  780. return 0;
  781. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  782. error = regulator_enable(ts->avdd);
  783. if (error) {
  784. dev_err(&ts->client->dev,
  785. "failed to enable avdd regulator: %d\n", error);
  786. goto release_reset_gpio;
  787. }
  788. error = regulator_enable(ts->vccio);
  789. if (error) {
  790. regulator_disable(ts->avdd);
  791. dev_err(&ts->client->dev,
  792. "failed to enable vccio regulator: %d\n", error);
  793. goto release_reset_gpio;
  794. }
  795. udelay(RM_POWERON_DELAY_USEC);
  796. release_reset_gpio:
  797. gpiod_set_value_cansleep(ts->reset_gpio, 0);
  798. if (error)
  799. return error;
  800. msleep(RM_RESET_DELAY_MSEC);
  801. return 0;
  802. }
  803. static void raydium_i2c_power_off(void *_data)
  804. {
  805. struct raydium_data *ts = _data;
  806. if (ts->reset_gpio) {
  807. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  808. regulator_disable(ts->vccio);
  809. regulator_disable(ts->avdd);
  810. }
  811. }
  812. static int raydium_i2c_probe(struct i2c_client *client,
  813. const struct i2c_device_id *id)
  814. {
  815. union i2c_smbus_data dummy;
  816. struct raydium_data *ts;
  817. int error;
  818. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  819. dev_err(&client->dev,
  820. "i2c check functionality error (need I2C_FUNC_I2C)\n");
  821. return -ENXIO;
  822. }
  823. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  824. if (!ts)
  825. return -ENOMEM;
  826. mutex_init(&ts->sysfs_mutex);
  827. ts->client = client;
  828. i2c_set_clientdata(client, ts);
  829. ts->avdd = devm_regulator_get(&client->dev, "avdd");
  830. if (IS_ERR(ts->avdd)) {
  831. error = PTR_ERR(ts->avdd);
  832. if (error != -EPROBE_DEFER)
  833. dev_err(&client->dev,
  834. "Failed to get 'avdd' regulator: %d\n", error);
  835. return error;
  836. }
  837. ts->vccio = devm_regulator_get(&client->dev, "vccio");
  838. if (IS_ERR(ts->vccio)) {
  839. error = PTR_ERR(ts->vccio);
  840. if (error != -EPROBE_DEFER)
  841. dev_err(&client->dev,
  842. "Failed to get 'vccio' regulator: %d\n", error);
  843. return error;
  844. }
  845. ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  846. GPIOD_OUT_LOW);
  847. if (IS_ERR(ts->reset_gpio)) {
  848. error = PTR_ERR(ts->reset_gpio);
  849. if (error != -EPROBE_DEFER)
  850. dev_err(&client->dev,
  851. "failed to get reset gpio: %d\n", error);
  852. return error;
  853. }
  854. error = raydium_i2c_power_on(ts);
  855. if (error)
  856. return error;
  857. error = devm_add_action(&client->dev, raydium_i2c_power_off, ts);
  858. if (error) {
  859. dev_err(&client->dev,
  860. "failed to install power off action: %d\n", error);
  861. raydium_i2c_power_off(ts);
  862. return error;
  863. }
  864. /* Make sure there is something at this address */
  865. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  866. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
  867. dev_err(&client->dev, "nothing at this address\n");
  868. return -ENXIO;
  869. }
  870. error = raydium_i2c_initialize(ts);
  871. if (error) {
  872. dev_err(&client->dev, "failed to initialize: %d\n", error);
  873. return error;
  874. }
  875. ts->report_data = devm_kmalloc(&client->dev,
  876. ts->pkg_size, GFP_KERNEL);
  877. if (!ts->report_data)
  878. return -ENOMEM;
  879. ts->input = devm_input_allocate_device(&client->dev);
  880. if (!ts->input) {
  881. dev_err(&client->dev, "Failed to allocate input device\n");
  882. return -ENOMEM;
  883. }
  884. ts->input->name = "Raydium Touchscreen";
  885. ts->input->id.bustype = BUS_I2C;
  886. input_set_abs_params(ts->input, ABS_MT_POSITION_X,
  887. 0, le16_to_cpu(ts->info.x_max), 0, 0);
  888. input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
  889. 0, le16_to_cpu(ts->info.y_max), 0, 0);
  890. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
  891. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
  892. input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  893. input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  894. error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
  895. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  896. if (error) {
  897. dev_err(&client->dev,
  898. "failed to initialize MT slots: %d\n", error);
  899. return error;
  900. }
  901. error = input_register_device(ts->input);
  902. if (error) {
  903. dev_err(&client->dev,
  904. "unable to register input device: %d\n", error);
  905. return error;
  906. }
  907. error = devm_request_threaded_irq(&client->dev, client->irq,
  908. NULL, raydium_i2c_irq,
  909. IRQF_ONESHOT, client->name, ts);
  910. if (error) {
  911. dev_err(&client->dev, "Failed to register interrupt\n");
  912. return error;
  913. }
  914. error = devm_device_add_group(&client->dev,
  915. &raydium_i2c_attribute_group);
  916. if (error) {
  917. dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
  918. error);
  919. return error;
  920. }
  921. return 0;
  922. }
  923. static void __maybe_unused raydium_enter_sleep(struct i2c_client *client)
  924. {
  925. static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
  926. int error;
  927. error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
  928. sleep_cmd, sizeof(sleep_cmd));
  929. if (error)
  930. dev_err(&client->dev,
  931. "sleep command failed: %d\n", error);
  932. }
  933. static int __maybe_unused raydium_i2c_suspend(struct device *dev)
  934. {
  935. struct i2c_client *client = to_i2c_client(dev);
  936. struct raydium_data *ts = i2c_get_clientdata(client);
  937. /* Sleep is not available in BLDR recovery mode */
  938. if (ts->boot_mode != RAYDIUM_TS_MAIN)
  939. return -EBUSY;
  940. disable_irq(client->irq);
  941. if (device_may_wakeup(dev)) {
  942. raydium_enter_sleep(client);
  943. ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
  944. } else {
  945. raydium_i2c_power_off(ts);
  946. }
  947. return 0;
  948. }
  949. static int __maybe_unused raydium_i2c_resume(struct device *dev)
  950. {
  951. struct i2c_client *client = to_i2c_client(dev);
  952. struct raydium_data *ts = i2c_get_clientdata(client);
  953. if (device_may_wakeup(dev)) {
  954. if (ts->wake_irq_enabled)
  955. disable_irq_wake(client->irq);
  956. raydium_i2c_sw_reset(client);
  957. } else {
  958. raydium_i2c_power_on(ts);
  959. raydium_i2c_initialize(ts);
  960. }
  961. enable_irq(client->irq);
  962. return 0;
  963. }
  964. static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
  965. raydium_i2c_suspend, raydium_i2c_resume);
  966. static const struct i2c_device_id raydium_i2c_id[] = {
  967. { "raydium_i2c" , 0 },
  968. { "rm32380", 0 },
  969. { /* sentinel */ }
  970. };
  971. MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
  972. #ifdef CONFIG_ACPI
  973. static const struct acpi_device_id raydium_acpi_id[] = {
  974. { "RAYD0001", 0 },
  975. { /* sentinel */ }
  976. };
  977. MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
  978. #endif
  979. #ifdef CONFIG_OF
  980. static const struct of_device_id raydium_of_match[] = {
  981. { .compatible = "raydium,rm32380", },
  982. { /* sentinel */ }
  983. };
  984. MODULE_DEVICE_TABLE(of, raydium_of_match);
  985. #endif
  986. static struct i2c_driver raydium_i2c_driver = {
  987. .probe = raydium_i2c_probe,
  988. .id_table = raydium_i2c_id,
  989. .driver = {
  990. .name = "raydium_ts",
  991. .pm = &raydium_i2c_pm_ops,
  992. .acpi_match_table = ACPI_PTR(raydium_acpi_id),
  993. .of_match_table = of_match_ptr(raydium_of_match),
  994. },
  995. };
  996. module_i2c_driver(raydium_i2c_driver);
  997. MODULE_AUTHOR("Raydium");
  998. MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
  999. MODULE_LICENSE("GPL v2");