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. error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
  345. if (error) {
  346. dev_err(&client->dev, "WRT obj command failed: %d\n",
  347. error);
  348. return error;
  349. }
  350. error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, NULL, 0);
  351. if (error) {
  352. dev_err(&client->dev, "Ack obj command failed: %d\n", error);
  353. return error;
  354. }
  355. error = raydium_i2c_bl_chk_state(client, state);
  356. if (error) {
  357. dev_err(&client->dev, "BL check state failed: %d\n", error);
  358. return error;
  359. }
  360. return 0;
  361. }
  362. static int raydium_i2c_boot_trigger(struct i2c_client *client)
  363. {
  364. static const u8 cmd[7][6] = {
  365. { 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
  366. { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
  367. { 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
  368. { 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
  369. { 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
  370. { 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
  371. { 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
  372. };
  373. int i;
  374. int error;
  375. for (i = 0; i < 7; i++) {
  376. error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
  377. RAYDIUM_WAIT_READY);
  378. if (error) {
  379. dev_err(&client->dev,
  380. "boot trigger failed at step %d: %d\n",
  381. i, error);
  382. return error;
  383. }
  384. }
  385. return 0;
  386. }
  387. static int raydium_i2c_fw_trigger(struct i2c_client *client)
  388. {
  389. static const u8 cmd[5][11] = {
  390. { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
  391. { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
  392. { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
  393. { 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
  394. { 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
  395. };
  396. int i;
  397. int error;
  398. for (i = 0; i < 5; i++) {
  399. error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
  400. RAYDIUM_ACK_NULL);
  401. if (error) {
  402. dev_err(&client->dev,
  403. "fw trigger failed at step %d: %d\n",
  404. i, error);
  405. return error;
  406. }
  407. }
  408. return 0;
  409. }
  410. static int raydium_i2c_check_path(struct i2c_client *client)
  411. {
  412. static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
  413. int error;
  414. error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
  415. RAYDIUM_PATH_READY);
  416. if (error) {
  417. dev_err(&client->dev, "check path command failed: %d\n", error);
  418. return error;
  419. }
  420. return 0;
  421. }
  422. static int raydium_i2c_enter_bl(struct i2c_client *client)
  423. {
  424. static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
  425. int error;
  426. error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
  427. RAYDIUM_ACK_NULL);
  428. if (error) {
  429. dev_err(&client->dev, "enter bl command failed: %d\n", error);
  430. return error;
  431. }
  432. msleep(RM_BOOT_DELAY_MS);
  433. return 0;
  434. }
  435. static int raydium_i2c_leave_bl(struct i2c_client *client)
  436. {
  437. static const u8 leave_cmd[] = { 0x05, 0x00 };
  438. int error;
  439. error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
  440. RAYDIUM_ACK_NULL);
  441. if (error) {
  442. dev_err(&client->dev, "leave bl command failed: %d\n", error);
  443. return error;
  444. }
  445. msleep(RM_BOOT_DELAY_MS);
  446. return 0;
  447. }
  448. static int raydium_i2c_write_checksum(struct i2c_client *client,
  449. size_t length, u16 checksum)
  450. {
  451. u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
  452. int error;
  453. put_unaligned_le16(length, &checksum_cmd[3]);
  454. put_unaligned_le16(checksum, &checksum_cmd[5]);
  455. error = raydium_i2c_write_object(client,
  456. checksum_cmd, sizeof(checksum_cmd),
  457. RAYDIUM_ACK_NULL);
  458. if (error) {
  459. dev_err(&client->dev, "failed to write checksum: %d\n",
  460. error);
  461. return error;
  462. }
  463. return 0;
  464. }
  465. static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
  466. {
  467. static const u8 cmd[] = { 0x0A, 0xAA };
  468. int error;
  469. error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
  470. RAYDIUM_WAIT_READY);
  471. if (error) {
  472. dev_err(&client->dev, "disable watchdog command failed: %d\n",
  473. error);
  474. return error;
  475. }
  476. return 0;
  477. }
  478. static int raydium_i2c_fw_write_page(struct i2c_client *client,
  479. u16 page_idx, const void *data, size_t len)
  480. {
  481. u8 buf[RM_BL_WRT_LEN];
  482. size_t xfer_len;
  483. int error;
  484. int i;
  485. BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
  486. for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
  487. buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
  488. buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
  489. buf[BL_PKG_IDX] = i + 1;
  490. xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
  491. memcpy(&buf[BL_DATA_STR], data, xfer_len);
  492. if (len < RM_BL_WRT_PKG_SIZE)
  493. memset(&buf[BL_DATA_STR + xfer_len], 0xff,
  494. RM_BL_WRT_PKG_SIZE - xfer_len);
  495. error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
  496. RAYDIUM_WAIT_READY);
  497. if (error) {
  498. dev_err(&client->dev,
  499. "page write command failed for page %d, chunk %d: %d\n",
  500. page_idx, i, error);
  501. return error;
  502. }
  503. data += xfer_len;
  504. len -= xfer_len;
  505. }
  506. return error;
  507. }
  508. static u16 raydium_calc_chksum(const u8 *buf, u16 len)
  509. {
  510. u16 checksum = 0;
  511. u16 i;
  512. for (i = 0; i < len; i++)
  513. checksum += buf[i];
  514. return checksum;
  515. }
  516. static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
  517. const struct firmware *fw)
  518. {
  519. struct i2c_client *client = ts->client;
  520. const void *data;
  521. size_t data_len;
  522. size_t len;
  523. int page_nr;
  524. int i;
  525. int error;
  526. u16 fw_checksum;
  527. if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
  528. dev_err(&client->dev, "Invalid firmware length\n");
  529. return -EINVAL;
  530. }
  531. error = raydium_i2c_check_fw_status(ts);
  532. if (error) {
  533. dev_err(&client->dev, "Unable to access IC %d\n", error);
  534. return error;
  535. }
  536. if (ts->boot_mode == RAYDIUM_TS_MAIN) {
  537. for (i = 0; i < RM_MAX_RETRIES; i++) {
  538. error = raydium_i2c_enter_bl(client);
  539. if (!error) {
  540. error = raydium_i2c_check_fw_status(ts);
  541. if (error) {
  542. dev_err(&client->dev,
  543. "unable to access IC: %d\n",
  544. error);
  545. return error;
  546. }
  547. if (ts->boot_mode == RAYDIUM_TS_BLDR)
  548. break;
  549. }
  550. }
  551. if (ts->boot_mode == RAYDIUM_TS_MAIN) {
  552. dev_err(&client->dev,
  553. "failed to jump to boot loader: %d\n",
  554. error);
  555. return -EIO;
  556. }
  557. }
  558. error = raydium_i2c_disable_watch_dog(client);
  559. if (error)
  560. return error;
  561. error = raydium_i2c_check_path(client);
  562. if (error)
  563. return error;
  564. error = raydium_i2c_boot_trigger(client);
  565. if (error) {
  566. dev_err(&client->dev, "send boot trigger fail: %d\n", error);
  567. return error;
  568. }
  569. msleep(RM_BOOT_DELAY_MS);
  570. data = fw->data;
  571. data_len = fw->size;
  572. page_nr = 0;
  573. while (data_len) {
  574. len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
  575. error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
  576. if (error)
  577. return error;
  578. msleep(20);
  579. data += len;
  580. data_len -= len;
  581. }
  582. error = raydium_i2c_leave_bl(client);
  583. if (error) {
  584. dev_err(&client->dev,
  585. "failed to leave boot loader: %d\n", error);
  586. return error;
  587. }
  588. dev_dbg(&client->dev, "left boot loader mode\n");
  589. msleep(RM_BOOT_DELAY_MS);
  590. error = raydium_i2c_check_fw_status(ts);
  591. if (error) {
  592. dev_err(&client->dev,
  593. "failed to check fw status after write: %d\n",
  594. error);
  595. return error;
  596. }
  597. if (ts->boot_mode != RAYDIUM_TS_MAIN) {
  598. dev_err(&client->dev,
  599. "failed to switch to main fw after writing firmware: %d\n",
  600. error);
  601. return -EINVAL;
  602. }
  603. error = raydium_i2c_fw_trigger(client);
  604. if (error) {
  605. dev_err(&client->dev, "failed to trigger fw: %d\n", error);
  606. return error;
  607. }
  608. fw_checksum = raydium_calc_chksum(fw->data, fw->size);
  609. error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
  610. if (error)
  611. return error;
  612. return 0;
  613. }
  614. static int raydium_i2c_fw_update(struct raydium_data *ts)
  615. {
  616. struct i2c_client *client = ts->client;
  617. const struct firmware *fw = NULL;
  618. char *fw_file;
  619. int error;
  620. fw_file = kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
  621. le32_to_cpu(ts->info.hw_ver));
  622. if (!fw_file)
  623. return -ENOMEM;
  624. dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
  625. error = request_firmware(&fw, fw_file, &client->dev);
  626. if (error) {
  627. dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
  628. goto out_free_fw_file;
  629. }
  630. disable_irq(client->irq);
  631. error = raydium_i2c_do_update_firmware(ts, fw);
  632. if (error) {
  633. dev_err(&client->dev, "firmware update failed: %d\n", error);
  634. ts->boot_mode = RAYDIUM_TS_BLDR;
  635. goto out_enable_irq;
  636. }
  637. error = raydium_i2c_initialize(ts);
  638. if (error) {
  639. dev_err(&client->dev,
  640. "failed to initialize device after firmware update: %d\n",
  641. error);
  642. ts->boot_mode = RAYDIUM_TS_BLDR;
  643. goto out_enable_irq;
  644. }
  645. ts->boot_mode = RAYDIUM_TS_MAIN;
  646. out_enable_irq:
  647. enable_irq(client->irq);
  648. msleep(100);
  649. release_firmware(fw);
  650. out_free_fw_file:
  651. kfree(fw_file);
  652. return error;
  653. }
  654. static void raydium_mt_event(struct raydium_data *ts)
  655. {
  656. int i;
  657. for (i = 0; i < ts->report_size / ts->contact_size; i++) {
  658. u8 *contact = &ts->report_data[ts->contact_size * i];
  659. bool state = contact[RM_CONTACT_STATE_POS];
  660. u8 wx, wy;
  661. input_mt_slot(ts->input, i);
  662. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
  663. if (!state)
  664. continue;
  665. input_report_abs(ts->input, ABS_MT_POSITION_X,
  666. get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
  667. input_report_abs(ts->input, ABS_MT_POSITION_Y,
  668. get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
  669. input_report_abs(ts->input, ABS_MT_PRESSURE,
  670. contact[RM_CONTACT_PRESSURE_POS]);
  671. wx = contact[RM_CONTACT_WIDTH_X_POS];
  672. wy = contact[RM_CONTACT_WIDTH_Y_POS];
  673. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
  674. input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
  675. }
  676. input_mt_sync_frame(ts->input);
  677. input_sync(ts->input);
  678. }
  679. static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
  680. {
  681. struct raydium_data *ts = _dev;
  682. int error;
  683. u16 fw_crc;
  684. u16 calc_crc;
  685. if (ts->boot_mode != RAYDIUM_TS_MAIN)
  686. goto out;
  687. error = raydium_i2c_read_message(ts->client, ts->data_bank_addr,
  688. ts->report_data, ts->pkg_size);
  689. if (error)
  690. goto out;
  691. fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
  692. calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
  693. if (unlikely(fw_crc != calc_crc)) {
  694. dev_warn(&ts->client->dev,
  695. "%s: invalid packet crc %#04x vs %#04x\n",
  696. __func__, calc_crc, fw_crc);
  697. goto out;
  698. }
  699. raydium_mt_event(ts);
  700. out:
  701. return IRQ_HANDLED;
  702. }
  703. static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
  704. struct device_attribute *attr, char *buf)
  705. {
  706. struct i2c_client *client = to_i2c_client(dev);
  707. struct raydium_data *ts = i2c_get_clientdata(client);
  708. return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
  709. }
  710. static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
  711. struct device_attribute *attr, char *buf)
  712. {
  713. struct i2c_client *client = to_i2c_client(dev);
  714. struct raydium_data *ts = i2c_get_clientdata(client);
  715. return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
  716. }
  717. static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
  718. struct device_attribute *attr,
  719. char *buf)
  720. {
  721. struct i2c_client *client = to_i2c_client(dev);
  722. struct raydium_data *ts = i2c_get_clientdata(client);
  723. return sprintf(buf, "%s\n",
  724. ts->boot_mode == RAYDIUM_TS_MAIN ?
  725. "Normal" : "Recovery");
  726. }
  727. static ssize_t raydium_i2c_update_fw_store(struct device *dev,
  728. struct device_attribute *attr,
  729. const char *buf, size_t count)
  730. {
  731. struct i2c_client *client = to_i2c_client(dev);
  732. struct raydium_data *ts = i2c_get_clientdata(client);
  733. int error;
  734. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  735. if (error)
  736. return error;
  737. error = raydium_i2c_fw_update(ts);
  738. mutex_unlock(&ts->sysfs_mutex);
  739. return error ?: count;
  740. }
  741. static ssize_t raydium_i2c_calibrate_store(struct device *dev,
  742. struct device_attribute *attr,
  743. const char *buf, size_t count)
  744. {
  745. struct i2c_client *client = to_i2c_client(dev);
  746. struct raydium_data *ts = i2c_get_clientdata(client);
  747. static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
  748. int error;
  749. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  750. if (error)
  751. return error;
  752. error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
  753. RAYDIUM_WAIT_READY);
  754. if (error)
  755. dev_err(&client->dev, "calibrate command failed: %d\n", error);
  756. mutex_unlock(&ts->sysfs_mutex);
  757. return error ?: count;
  758. }
  759. static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
  760. static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
  761. static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
  762. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
  763. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
  764. static struct attribute *raydium_i2c_attributes[] = {
  765. &dev_attr_update_fw.attr,
  766. &dev_attr_boot_mode.attr,
  767. &dev_attr_fw_version.attr,
  768. &dev_attr_hw_version.attr,
  769. &dev_attr_calibrate.attr,
  770. NULL
  771. };
  772. static const struct attribute_group raydium_i2c_attribute_group = {
  773. .attrs = raydium_i2c_attributes,
  774. };
  775. static int raydium_i2c_power_on(struct raydium_data *ts)
  776. {
  777. int error;
  778. if (!ts->reset_gpio)
  779. return 0;
  780. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  781. error = regulator_enable(ts->avdd);
  782. if (error) {
  783. dev_err(&ts->client->dev,
  784. "failed to enable avdd regulator: %d\n", error);
  785. goto release_reset_gpio;
  786. }
  787. error = regulator_enable(ts->vccio);
  788. if (error) {
  789. regulator_disable(ts->avdd);
  790. dev_err(&ts->client->dev,
  791. "failed to enable vccio regulator: %d\n", error);
  792. goto release_reset_gpio;
  793. }
  794. udelay(RM_POWERON_DELAY_USEC);
  795. release_reset_gpio:
  796. gpiod_set_value_cansleep(ts->reset_gpio, 0);
  797. if (error)
  798. return error;
  799. msleep(RM_RESET_DELAY_MSEC);
  800. return 0;
  801. }
  802. static void raydium_i2c_power_off(void *_data)
  803. {
  804. struct raydium_data *ts = _data;
  805. if (ts->reset_gpio) {
  806. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  807. regulator_disable(ts->vccio);
  808. regulator_disable(ts->avdd);
  809. }
  810. }
  811. static int raydium_i2c_probe(struct i2c_client *client,
  812. const struct i2c_device_id *id)
  813. {
  814. union i2c_smbus_data dummy;
  815. struct raydium_data *ts;
  816. int error;
  817. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  818. dev_err(&client->dev,
  819. "i2c check functionality error (need I2C_FUNC_I2C)\n");
  820. return -ENXIO;
  821. }
  822. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  823. if (!ts)
  824. return -ENOMEM;
  825. mutex_init(&ts->sysfs_mutex);
  826. ts->client = client;
  827. i2c_set_clientdata(client, ts);
  828. ts->avdd = devm_regulator_get(&client->dev, "avdd");
  829. if (IS_ERR(ts->avdd)) {
  830. error = PTR_ERR(ts->avdd);
  831. if (error != -EPROBE_DEFER)
  832. dev_err(&client->dev,
  833. "Failed to get 'avdd' regulator: %d\n", error);
  834. return error;
  835. }
  836. ts->vccio = devm_regulator_get(&client->dev, "vccio");
  837. if (IS_ERR(ts->vccio)) {
  838. error = PTR_ERR(ts->vccio);
  839. if (error != -EPROBE_DEFER)
  840. dev_err(&client->dev,
  841. "Failed to get 'vccio' regulator: %d\n", error);
  842. return error;
  843. }
  844. ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  845. GPIOD_OUT_LOW);
  846. if (IS_ERR(ts->reset_gpio)) {
  847. error = PTR_ERR(ts->reset_gpio);
  848. if (error != -EPROBE_DEFER)
  849. dev_err(&client->dev,
  850. "failed to get reset gpio: %d\n", error);
  851. return error;
  852. }
  853. error = raydium_i2c_power_on(ts);
  854. if (error)
  855. return error;
  856. error = devm_add_action(&client->dev, raydium_i2c_power_off, ts);
  857. if (error) {
  858. dev_err(&client->dev,
  859. "failed to install power off action: %d\n", error);
  860. raydium_i2c_power_off(ts);
  861. return error;
  862. }
  863. /* Make sure there is something at this address */
  864. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  865. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
  866. dev_err(&client->dev, "nothing at this address\n");
  867. return -ENXIO;
  868. }
  869. error = raydium_i2c_initialize(ts);
  870. if (error) {
  871. dev_err(&client->dev, "failed to initialize: %d\n", error);
  872. return error;
  873. }
  874. ts->report_data = devm_kmalloc(&client->dev,
  875. ts->pkg_size, GFP_KERNEL);
  876. if (!ts->report_data)
  877. return -ENOMEM;
  878. ts->input = devm_input_allocate_device(&client->dev);
  879. if (!ts->input) {
  880. dev_err(&client->dev, "Failed to allocate input device\n");
  881. return -ENOMEM;
  882. }
  883. ts->input->name = "Raydium Touchscreen";
  884. ts->input->id.bustype = BUS_I2C;
  885. input_set_abs_params(ts->input, ABS_MT_POSITION_X,
  886. 0, le16_to_cpu(ts->info.x_max), 0, 0);
  887. input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
  888. 0, le16_to_cpu(ts->info.y_max), 0, 0);
  889. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
  890. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
  891. input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  892. input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  893. error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
  894. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  895. if (error) {
  896. dev_err(&client->dev,
  897. "failed to initialize MT slots: %d\n", error);
  898. return error;
  899. }
  900. error = input_register_device(ts->input);
  901. if (error) {
  902. dev_err(&client->dev,
  903. "unable to register input device: %d\n", error);
  904. return error;
  905. }
  906. error = devm_request_threaded_irq(&client->dev, client->irq,
  907. NULL, raydium_i2c_irq,
  908. IRQF_ONESHOT, client->name, ts);
  909. if (error) {
  910. dev_err(&client->dev, "Failed to register interrupt\n");
  911. return error;
  912. }
  913. error = devm_device_add_group(&client->dev,
  914. &raydium_i2c_attribute_group);
  915. if (error) {
  916. dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
  917. error);
  918. return error;
  919. }
  920. return 0;
  921. }
  922. static void __maybe_unused raydium_enter_sleep(struct i2c_client *client)
  923. {
  924. static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
  925. int error;
  926. error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
  927. sleep_cmd, sizeof(sleep_cmd));
  928. if (error)
  929. dev_err(&client->dev,
  930. "sleep command failed: %d\n", error);
  931. }
  932. static int __maybe_unused raydium_i2c_suspend(struct device *dev)
  933. {
  934. struct i2c_client *client = to_i2c_client(dev);
  935. struct raydium_data *ts = i2c_get_clientdata(client);
  936. /* Sleep is not available in BLDR recovery mode */
  937. if (ts->boot_mode != RAYDIUM_TS_MAIN)
  938. return -EBUSY;
  939. disable_irq(client->irq);
  940. if (device_may_wakeup(dev)) {
  941. raydium_enter_sleep(client);
  942. ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
  943. } else {
  944. raydium_i2c_power_off(ts);
  945. }
  946. return 0;
  947. }
  948. static int __maybe_unused raydium_i2c_resume(struct device *dev)
  949. {
  950. struct i2c_client *client = to_i2c_client(dev);
  951. struct raydium_data *ts = i2c_get_clientdata(client);
  952. if (device_may_wakeup(dev)) {
  953. if (ts->wake_irq_enabled)
  954. disable_irq_wake(client->irq);
  955. raydium_i2c_sw_reset(client);
  956. } else {
  957. raydium_i2c_power_on(ts);
  958. raydium_i2c_initialize(ts);
  959. }
  960. enable_irq(client->irq);
  961. return 0;
  962. }
  963. static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
  964. raydium_i2c_suspend, raydium_i2c_resume);
  965. static const struct i2c_device_id raydium_i2c_id[] = {
  966. { "raydium_i2c" , 0 },
  967. { "rm32380", 0 },
  968. { /* sentinel */ }
  969. };
  970. MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
  971. #ifdef CONFIG_ACPI
  972. static const struct acpi_device_id raydium_acpi_id[] = {
  973. { "RAYD0001", 0 },
  974. { /* sentinel */ }
  975. };
  976. MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
  977. #endif
  978. #ifdef CONFIG_OF
  979. static const struct of_device_id raydium_of_match[] = {
  980. { .compatible = "raydium,rm32380", },
  981. { /* sentinel */ }
  982. };
  983. MODULE_DEVICE_TABLE(of, raydium_of_match);
  984. #endif
  985. static struct i2c_driver raydium_i2c_driver = {
  986. .probe = raydium_i2c_probe,
  987. .id_table = raydium_i2c_id,
  988. .driver = {
  989. .name = "raydium_ts",
  990. .pm = &raydium_i2c_pm_ops,
  991. .acpi_match_table = ACPI_PTR(raydium_acpi_id),
  992. .of_match_table = of_match_ptr(raydium_of_match),
  993. },
  994. };
  995. module_i2c_driver(raydium_i2c_driver);
  996. MODULE_AUTHOR("Raydium");
  997. MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
  998. MODULE_LICENSE("GPL v2");