raydium_i2c_ts.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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 bool 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 bool 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. "failied 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. const char *fw_file = "/*(DEBLOBBED)*/";
  619. int error;
  620. error = reject_firmware(&fw, fw_file, &client->dev);
  621. if (error) {
  622. dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
  623. return error;
  624. }
  625. disable_irq(client->irq);
  626. error = raydium_i2c_do_update_firmware(ts, fw);
  627. if (error) {
  628. dev_err(&client->dev, "firmware update failed: %d\n", error);
  629. ts->boot_mode = RAYDIUM_TS_BLDR;
  630. goto out_enable_irq;
  631. }
  632. error = raydium_i2c_initialize(ts);
  633. if (error) {
  634. dev_err(&client->dev,
  635. "failed to initialize device after firmware update: %d\n",
  636. error);
  637. ts->boot_mode = RAYDIUM_TS_BLDR;
  638. goto out_enable_irq;
  639. }
  640. ts->boot_mode = RAYDIUM_TS_MAIN;
  641. out_enable_irq:
  642. enable_irq(client->irq);
  643. msleep(100);
  644. release_firmware(fw);
  645. return error;
  646. }
  647. static void raydium_mt_event(struct raydium_data *ts)
  648. {
  649. int i;
  650. for (i = 0; i < ts->report_size / ts->contact_size; i++) {
  651. u8 *contact = &ts->report_data[ts->contact_size * i];
  652. bool state = contact[RM_CONTACT_STATE_POS];
  653. u8 wx, wy;
  654. input_mt_slot(ts->input, i);
  655. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
  656. if (!state)
  657. continue;
  658. input_report_abs(ts->input, ABS_MT_POSITION_X,
  659. get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
  660. input_report_abs(ts->input, ABS_MT_POSITION_Y,
  661. get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
  662. input_report_abs(ts->input, ABS_MT_PRESSURE,
  663. contact[RM_CONTACT_PRESSURE_POS]);
  664. wx = contact[RM_CONTACT_WIDTH_X_POS];
  665. wy = contact[RM_CONTACT_WIDTH_Y_POS];
  666. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
  667. input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
  668. }
  669. input_mt_sync_frame(ts->input);
  670. input_sync(ts->input);
  671. }
  672. static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
  673. {
  674. struct raydium_data *ts = _dev;
  675. int error;
  676. u16 fw_crc;
  677. u16 calc_crc;
  678. if (ts->boot_mode != RAYDIUM_TS_MAIN)
  679. goto out;
  680. error = raydium_i2c_read_message(ts->client, ts->data_bank_addr,
  681. ts->report_data, ts->pkg_size);
  682. if (error)
  683. goto out;
  684. fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
  685. calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
  686. if (unlikely(fw_crc != calc_crc)) {
  687. dev_warn(&ts->client->dev,
  688. "%s: invalid packet crc %#04x vs %#04x\n",
  689. __func__, calc_crc, fw_crc);
  690. goto out;
  691. }
  692. raydium_mt_event(ts);
  693. out:
  694. return IRQ_HANDLED;
  695. }
  696. static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
  697. struct device_attribute *attr, char *buf)
  698. {
  699. struct i2c_client *client = to_i2c_client(dev);
  700. struct raydium_data *ts = i2c_get_clientdata(client);
  701. return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
  702. }
  703. static ssize_t raydium_i2c_hw_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, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
  709. }
  710. static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
  711. struct device_attribute *attr,
  712. 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, "%s\n",
  717. ts->boot_mode == RAYDIUM_TS_MAIN ?
  718. "Normal" : "Recovery");
  719. }
  720. static ssize_t raydium_i2c_update_fw_store(struct device *dev,
  721. struct device_attribute *attr,
  722. const char *buf, size_t count)
  723. {
  724. struct i2c_client *client = to_i2c_client(dev);
  725. struct raydium_data *ts = i2c_get_clientdata(client);
  726. int error;
  727. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  728. if (error)
  729. return error;
  730. error = raydium_i2c_fw_update(ts);
  731. mutex_unlock(&ts->sysfs_mutex);
  732. return error ?: count;
  733. }
  734. static ssize_t raydium_i2c_calibrate_store(struct device *dev,
  735. struct device_attribute *attr,
  736. const char *buf, size_t count)
  737. {
  738. struct i2c_client *client = to_i2c_client(dev);
  739. struct raydium_data *ts = i2c_get_clientdata(client);
  740. static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
  741. int error;
  742. error = mutex_lock_interruptible(&ts->sysfs_mutex);
  743. if (error)
  744. return error;
  745. error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
  746. RAYDIUM_WAIT_READY);
  747. if (error)
  748. dev_err(&client->dev, "calibrate command failed: %d\n", error);
  749. mutex_unlock(&ts->sysfs_mutex);
  750. return error ?: count;
  751. }
  752. static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
  753. static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
  754. static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
  755. static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
  756. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
  757. static struct attribute *raydium_i2c_attributes[] = {
  758. &dev_attr_update_fw.attr,
  759. &dev_attr_boot_mode.attr,
  760. &dev_attr_fw_version.attr,
  761. &dev_attr_hw_version.attr,
  762. &dev_attr_calibrate.attr,
  763. NULL
  764. };
  765. static struct attribute_group raydium_i2c_attribute_group = {
  766. .attrs = raydium_i2c_attributes,
  767. };
  768. static void raydium_i2c_remove_sysfs_group(void *_data)
  769. {
  770. struct raydium_data *ts = _data;
  771. sysfs_remove_group(&ts->client->dev.kobj, &raydium_i2c_attribute_group);
  772. }
  773. static int raydium_i2c_power_on(struct raydium_data *ts)
  774. {
  775. int error;
  776. if (!ts->reset_gpio)
  777. return 0;
  778. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  779. error = regulator_enable(ts->avdd);
  780. if (error) {
  781. dev_err(&ts->client->dev,
  782. "failed to enable avdd regulator: %d\n", error);
  783. goto release_reset_gpio;
  784. }
  785. error = regulator_enable(ts->vccio);
  786. if (error) {
  787. regulator_disable(ts->avdd);
  788. dev_err(&ts->client->dev,
  789. "failed to enable vccio regulator: %d\n", error);
  790. goto release_reset_gpio;
  791. }
  792. udelay(RM_POWERON_DELAY_USEC);
  793. release_reset_gpio:
  794. gpiod_set_value_cansleep(ts->reset_gpio, 0);
  795. if (error)
  796. return error;
  797. msleep(RM_RESET_DELAY_MSEC);
  798. return 0;
  799. }
  800. static void raydium_i2c_power_off(void *_data)
  801. {
  802. struct raydium_data *ts = _data;
  803. if (ts->reset_gpio) {
  804. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  805. regulator_disable(ts->vccio);
  806. regulator_disable(ts->avdd);
  807. }
  808. }
  809. static int raydium_i2c_probe(struct i2c_client *client,
  810. const struct i2c_device_id *id)
  811. {
  812. union i2c_smbus_data dummy;
  813. struct raydium_data *ts;
  814. int error;
  815. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  816. dev_err(&client->dev,
  817. "i2c check functionality error (need I2C_FUNC_I2C)\n");
  818. return -ENXIO;
  819. }
  820. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  821. if (!ts)
  822. return -ENOMEM;
  823. mutex_init(&ts->sysfs_mutex);
  824. ts->client = client;
  825. i2c_set_clientdata(client, ts);
  826. ts->avdd = devm_regulator_get(&client->dev, "avdd");
  827. if (IS_ERR(ts->avdd)) {
  828. error = PTR_ERR(ts->avdd);
  829. if (error != -EPROBE_DEFER)
  830. dev_err(&client->dev,
  831. "Failed to get 'avdd' regulator: %d\n", error);
  832. return error;
  833. }
  834. ts->vccio = devm_regulator_get(&client->dev, "vccio");
  835. if (IS_ERR(ts->vccio)) {
  836. error = PTR_ERR(ts->vccio);
  837. if (error != -EPROBE_DEFER)
  838. dev_err(&client->dev,
  839. "Failed to get 'vccio' regulator: %d\n", error);
  840. return error;
  841. }
  842. ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  843. GPIOD_OUT_LOW);
  844. if (IS_ERR(ts->reset_gpio)) {
  845. error = PTR_ERR(ts->reset_gpio);
  846. if (error != -EPROBE_DEFER)
  847. dev_err(&client->dev,
  848. "failed to get reset gpio: %d\n", error);
  849. return error;
  850. }
  851. error = raydium_i2c_power_on(ts);
  852. if (error)
  853. return error;
  854. error = devm_add_action(&client->dev, raydium_i2c_power_off, ts);
  855. if (error) {
  856. dev_err(&client->dev,
  857. "failed to install power off action: %d\n", error);
  858. raydium_i2c_power_off(ts);
  859. return error;
  860. }
  861. /* Make sure there is something at this address */
  862. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  863. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
  864. dev_err(&client->dev, "nothing at this address\n");
  865. return -ENXIO;
  866. }
  867. error = raydium_i2c_initialize(ts);
  868. if (error) {
  869. dev_err(&client->dev, "failed to initialize: %d\n", error);
  870. return error;
  871. }
  872. ts->report_data = devm_kmalloc(&client->dev,
  873. ts->pkg_size, GFP_KERNEL);
  874. if (!ts->report_data)
  875. return -ENOMEM;
  876. ts->input = devm_input_allocate_device(&client->dev);
  877. if (!ts->input) {
  878. dev_err(&client->dev, "Failed to allocate input device\n");
  879. return -ENOMEM;
  880. }
  881. ts->input->name = "Raydium Touchscreen";
  882. ts->input->id.bustype = BUS_I2C;
  883. input_set_drvdata(ts->input, ts);
  884. input_set_abs_params(ts->input, ABS_MT_POSITION_X,
  885. 0, le16_to_cpu(ts->info.x_max), 0, 0);
  886. input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
  887. 0, le16_to_cpu(ts->info.y_max), 0, 0);
  888. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
  889. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
  890. input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  891. input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  892. error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
  893. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  894. if (error) {
  895. dev_err(&client->dev,
  896. "failed to initialize MT slots: %d\n", error);
  897. return error;
  898. }
  899. error = input_register_device(ts->input);
  900. if (error) {
  901. dev_err(&client->dev,
  902. "unable to register input device: %d\n", error);
  903. return error;
  904. }
  905. error = devm_request_threaded_irq(&client->dev, client->irq,
  906. NULL, raydium_i2c_irq,
  907. IRQF_ONESHOT, client->name, ts);
  908. if (error) {
  909. dev_err(&client->dev, "Failed to register interrupt\n");
  910. return error;
  911. }
  912. error = sysfs_create_group(&client->dev.kobj,
  913. &raydium_i2c_attribute_group);
  914. if (error) {
  915. dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
  916. error);
  917. return error;
  918. }
  919. error = devm_add_action(&client->dev,
  920. raydium_i2c_remove_sysfs_group, ts);
  921. if (error) {
  922. raydium_i2c_remove_sysfs_group(ts);
  923. dev_err(&client->dev,
  924. "Failed to add sysfs cleanup action: %d\n", error);
  925. return error;
  926. }
  927. return 0;
  928. }
  929. static void __maybe_unused raydium_enter_sleep(struct i2c_client *client)
  930. {
  931. static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
  932. int error;
  933. error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
  934. sleep_cmd, sizeof(sleep_cmd));
  935. if (error)
  936. dev_err(&client->dev,
  937. "sleep command failed: %d\n", error);
  938. }
  939. static int __maybe_unused raydium_i2c_suspend(struct device *dev)
  940. {
  941. struct i2c_client *client = to_i2c_client(dev);
  942. struct raydium_data *ts = i2c_get_clientdata(client);
  943. /* Sleep is not available in BLDR recovery mode */
  944. if (ts->boot_mode != RAYDIUM_TS_MAIN)
  945. return -EBUSY;
  946. disable_irq(client->irq);
  947. if (device_may_wakeup(dev)) {
  948. raydium_enter_sleep(client);
  949. ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
  950. } else {
  951. raydium_i2c_power_off(ts);
  952. }
  953. return 0;
  954. }
  955. static int __maybe_unused raydium_i2c_resume(struct device *dev)
  956. {
  957. struct i2c_client *client = to_i2c_client(dev);
  958. struct raydium_data *ts = i2c_get_clientdata(client);
  959. if (device_may_wakeup(dev)) {
  960. if (ts->wake_irq_enabled)
  961. disable_irq_wake(client->irq);
  962. raydium_i2c_sw_reset(client);
  963. } else {
  964. raydium_i2c_power_on(ts);
  965. raydium_i2c_initialize(ts);
  966. }
  967. enable_irq(client->irq);
  968. return 0;
  969. }
  970. static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
  971. raydium_i2c_suspend, raydium_i2c_resume);
  972. static const struct i2c_device_id raydium_i2c_id[] = {
  973. { "raydium_i2c" , 0 },
  974. { "rm32380", 0 },
  975. { /* sentinel */ }
  976. };
  977. MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
  978. #ifdef CONFIG_ACPI
  979. static const struct acpi_device_id raydium_acpi_id[] = {
  980. { "RAYD0001", 0 },
  981. { /* sentinel */ }
  982. };
  983. MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
  984. #endif
  985. #ifdef CONFIG_OF
  986. static const struct of_device_id raydium_of_match[] = {
  987. { .compatible = "raydium,rm32380", },
  988. { /* sentinel */ }
  989. };
  990. MODULE_DEVICE_TABLE(of, raydium_of_match);
  991. #endif
  992. static struct i2c_driver raydium_i2c_driver = {
  993. .probe = raydium_i2c_probe,
  994. .id_table = raydium_i2c_id,
  995. .driver = {
  996. .name = "raydium_ts",
  997. .pm = &raydium_i2c_pm_ops,
  998. .acpi_match_table = ACPI_PTR(raydium_acpi_id),
  999. .of_match_table = of_match_ptr(raydium_of_match),
  1000. },
  1001. };
  1002. module_i2c_driver(raydium_i2c_driver);
  1003. MODULE_AUTHOR("Raydium");
  1004. MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
  1005. MODULE_LICENSE("GPL v2");