raydium_i2c_ts.c 30 KB

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