raydium_i2c_ts.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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/regulator/consumer.h>
  24. #include <linux/slab.h>
  25. #include <asm/unaligned.h>
  26. /* Slave I2C mode */
  27. #define RM_BOOT_BLDR 0x02
  28. #define RM_BOOT_MAIN 0x03
  29. /* I2C bootoloader commands */
  30. #define RM_CMD_BOOT_PAGE_WRT 0x0B /* send bl page write */
  31. #define RM_CMD_BOOT_WRT 0x11 /* send bl write */
  32. #define RM_CMD_BOOT_ACK 0x22 /* send ack*/
  33. #define RM_CMD_BOOT_CHK 0x33 /* send data check */
  34. #define RM_CMD_BOOT_READ 0x44 /* send wait bl data ready*/
  35. #define RM_BOOT_RDY 0xFF /* bl data ready */
  36. #define RM_BOOT_CMD_READHWID 0x0E /* read hwid */
  37. /* I2C main commands */
  38. #define RM_CMD_QUERY_BANK 0x2B
  39. #define RM_CMD_DATA_BANK 0x4D
  40. #define RM_CMD_ENTER_SLEEP 0x4E
  41. #define RM_CMD_BANK_SWITCH 0xAA
  42. #define RM_RESET_MSG_ADDR 0x40000004
  43. #define RM_MAX_READ_SIZE 56
  44. #define RM_PACKET_CRC_SIZE 2
  45. /* Touch relative info */
  46. #define RM_MAX_RETRIES 3
  47. #define RM_RETRY_DELAY_MS 20
  48. #define RM_MAX_TOUCH_NUM 10
  49. #define RM_BOOT_DELAY_MS 100
  50. /* Offsets in contact data */
  51. #define RM_CONTACT_STATE_POS 0
  52. #define RM_CONTACT_X_POS 1
  53. #define RM_CONTACT_Y_POS 3
  54. #define RM_CONTACT_PRESSURE_POS 5
  55. #define RM_CONTACT_WIDTH_X_POS 6
  56. #define RM_CONTACT_WIDTH_Y_POS 7
  57. /* Bootloader relative info */
  58. #define RM_BL_WRT_CMD_SIZE 3 /* bl flash wrt cmd size */
  59. #define RM_BL_WRT_PKG_SIZE 32 /* bl wrt pkg size */
  60. #define RM_BL_WRT_LEN (RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
  61. #define RM_FW_PAGE_SIZE 128
  62. #define RM_MAX_FW_RETRIES 30
  63. #define RM_MAX_FW_SIZE 0xD000
  64. #define RM_POWERON_DELAY_USEC 500
  65. #define RM_RESET_DELAY_MSEC 50
  66. enum raydium_bl_cmd {
  67. BL_HEADER = 0,
  68. BL_PAGE_STR,
  69. BL_PKG_IDX,
  70. BL_DATA_STR,
  71. };
  72. enum raydium_bl_ack {
  73. RAYDIUM_ACK_NULL = 0,
  74. RAYDIUM_WAIT_READY,
  75. RAYDIUM_PATH_READY,
  76. };
  77. enum raydium_boot_mode {
  78. RAYDIUM_TS_MAIN = 0,
  79. RAYDIUM_TS_BLDR,
  80. };
  81. /* Response to RM_CMD_DATA_BANK request */
  82. struct raydium_data_info {
  83. __le32 data_bank_addr;
  84. u8 pkg_size;
  85. u8 tp_info_size;
  86. };
  87. struct raydium_info {
  88. __le32 hw_ver; /*device version */
  89. u8 main_ver;
  90. u8 sub_ver;
  91. __le16 ft_ver; /* test version */
  92. u8 x_num;
  93. u8 y_num;
  94. __le16 x_max;
  95. __le16 y_max;
  96. u8 x_res; /* units/mm */
  97. u8 y_res; /* units/mm */
  98. };
  99. /* struct raydium_data - represents state of Raydium touchscreen device */
  100. struct raydium_data {
  101. struct i2c_client *client;
  102. struct input_dev *input;
  103. struct regulator *avdd;
  104. struct regulator *vccio;
  105. struct gpio_desc *reset_gpio;
  106. struct raydium_info info;
  107. struct mutex sysfs_mutex;
  108. u8 *report_data;
  109. u32 data_bank_addr;
  110. u8 report_size;
  111. u8 contact_size;
  112. u8 pkg_size;
  113. enum raydium_boot_mode boot_mode;
  114. bool wake_irq_enabled;
  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_attributes[] = {
  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. static const struct attribute_group raydium_i2c_attribute_group = {
  834. .attrs = raydium_i2c_attributes,
  835. };
  836. static int raydium_i2c_power_on(struct raydium_data *ts)
  837. {
  838. int error;
  839. if (!ts->reset_gpio)
  840. return 0;
  841. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  842. error = regulator_enable(ts->avdd);
  843. if (error) {
  844. dev_err(&ts->client->dev,
  845. "failed to enable avdd regulator: %d\n", error);
  846. goto release_reset_gpio;
  847. }
  848. error = regulator_enable(ts->vccio);
  849. if (error) {
  850. regulator_disable(ts->avdd);
  851. dev_err(&ts->client->dev,
  852. "failed to enable vccio regulator: %d\n", error);
  853. goto release_reset_gpio;
  854. }
  855. udelay(RM_POWERON_DELAY_USEC);
  856. release_reset_gpio:
  857. gpiod_set_value_cansleep(ts->reset_gpio, 0);
  858. if (error)
  859. return error;
  860. msleep(RM_RESET_DELAY_MSEC);
  861. return 0;
  862. }
  863. static void raydium_i2c_power_off(void *_data)
  864. {
  865. struct raydium_data *ts = _data;
  866. if (ts->reset_gpio) {
  867. gpiod_set_value_cansleep(ts->reset_gpio, 1);
  868. regulator_disable(ts->vccio);
  869. regulator_disable(ts->avdd);
  870. }
  871. }
  872. static int raydium_i2c_probe(struct i2c_client *client,
  873. const struct i2c_device_id *id)
  874. {
  875. union i2c_smbus_data dummy;
  876. struct raydium_data *ts;
  877. int error;
  878. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  879. dev_err(&client->dev,
  880. "i2c check functionality error (need I2C_FUNC_I2C)\n");
  881. return -ENXIO;
  882. }
  883. ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
  884. if (!ts)
  885. return -ENOMEM;
  886. mutex_init(&ts->sysfs_mutex);
  887. ts->client = client;
  888. i2c_set_clientdata(client, ts);
  889. ts->avdd = devm_regulator_get(&client->dev, "avdd");
  890. if (IS_ERR(ts->avdd)) {
  891. error = PTR_ERR(ts->avdd);
  892. if (error != -EPROBE_DEFER)
  893. dev_err(&client->dev,
  894. "Failed to get 'avdd' regulator: %d\n", error);
  895. return error;
  896. }
  897. ts->vccio = devm_regulator_get(&client->dev, "vccio");
  898. if (IS_ERR(ts->vccio)) {
  899. error = PTR_ERR(ts->vccio);
  900. if (error != -EPROBE_DEFER)
  901. dev_err(&client->dev,
  902. "Failed to get 'vccio' regulator: %d\n", error);
  903. return error;
  904. }
  905. ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  906. GPIOD_OUT_LOW);
  907. if (IS_ERR(ts->reset_gpio)) {
  908. error = PTR_ERR(ts->reset_gpio);
  909. if (error != -EPROBE_DEFER)
  910. dev_err(&client->dev,
  911. "failed to get reset gpio: %d\n", error);
  912. return error;
  913. }
  914. error = raydium_i2c_power_on(ts);
  915. if (error)
  916. return error;
  917. error = devm_add_action_or_reset(&client->dev,
  918. raydium_i2c_power_off, ts);
  919. if (error) {
  920. dev_err(&client->dev,
  921. "failed to install power off action: %d\n", error);
  922. return error;
  923. }
  924. /* Make sure there is something at this address */
  925. if (i2c_smbus_xfer(client->adapter, client->addr, 0,
  926. I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
  927. dev_err(&client->dev, "nothing at this address\n");
  928. return -ENXIO;
  929. }
  930. error = raydium_i2c_initialize(ts);
  931. if (error) {
  932. dev_err(&client->dev, "failed to initialize: %d\n", error);
  933. return error;
  934. }
  935. ts->report_data = devm_kmalloc(&client->dev,
  936. ts->pkg_size, GFP_KERNEL);
  937. if (!ts->report_data)
  938. return -ENOMEM;
  939. ts->input = devm_input_allocate_device(&client->dev);
  940. if (!ts->input) {
  941. dev_err(&client->dev, "Failed to allocate input device\n");
  942. return -ENOMEM;
  943. }
  944. ts->input->name = "Raydium Touchscreen";
  945. ts->input->id.bustype = BUS_I2C;
  946. input_set_abs_params(ts->input, ABS_MT_POSITION_X,
  947. 0, le16_to_cpu(ts->info.x_max), 0, 0);
  948. input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
  949. 0, le16_to_cpu(ts->info.y_max), 0, 0);
  950. input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
  951. input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
  952. input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  953. input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  954. error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
  955. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  956. if (error) {
  957. dev_err(&client->dev,
  958. "failed to initialize MT slots: %d\n", error);
  959. return error;
  960. }
  961. error = input_register_device(ts->input);
  962. if (error) {
  963. dev_err(&client->dev,
  964. "unable to register input device: %d\n", error);
  965. return error;
  966. }
  967. error = devm_request_threaded_irq(&client->dev, client->irq,
  968. NULL, raydium_i2c_irq,
  969. IRQF_ONESHOT, client->name, ts);
  970. if (error) {
  971. dev_err(&client->dev, "Failed to register interrupt\n");
  972. return error;
  973. }
  974. error = devm_device_add_group(&client->dev,
  975. &raydium_i2c_attribute_group);
  976. if (error) {
  977. dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
  978. error);
  979. return error;
  980. }
  981. return 0;
  982. }
  983. static void __maybe_unused raydium_enter_sleep(struct i2c_client *client)
  984. {
  985. static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
  986. int error;
  987. error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
  988. sleep_cmd, sizeof(sleep_cmd));
  989. if (error)
  990. dev_err(&client->dev,
  991. "sleep command failed: %d\n", error);
  992. }
  993. static int __maybe_unused raydium_i2c_suspend(struct device *dev)
  994. {
  995. struct i2c_client *client = to_i2c_client(dev);
  996. struct raydium_data *ts = i2c_get_clientdata(client);
  997. /* Sleep is not available in BLDR recovery mode */
  998. if (ts->boot_mode != RAYDIUM_TS_MAIN)
  999. return -EBUSY;
  1000. disable_irq(client->irq);
  1001. if (device_may_wakeup(dev)) {
  1002. raydium_enter_sleep(client);
  1003. ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
  1004. } else {
  1005. raydium_i2c_power_off(ts);
  1006. }
  1007. return 0;
  1008. }
  1009. static int __maybe_unused raydium_i2c_resume(struct device *dev)
  1010. {
  1011. struct i2c_client *client = to_i2c_client(dev);
  1012. struct raydium_data *ts = i2c_get_clientdata(client);
  1013. if (device_may_wakeup(dev)) {
  1014. if (ts->wake_irq_enabled)
  1015. disable_irq_wake(client->irq);
  1016. raydium_i2c_sw_reset(client);
  1017. } else {
  1018. raydium_i2c_power_on(ts);
  1019. raydium_i2c_initialize(ts);
  1020. }
  1021. enable_irq(client->irq);
  1022. return 0;
  1023. }
  1024. static SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
  1025. raydium_i2c_suspend, raydium_i2c_resume);
  1026. static const struct i2c_device_id raydium_i2c_id[] = {
  1027. { "raydium_i2c", 0 },
  1028. { "rm32380", 0 },
  1029. { /* sentinel */ }
  1030. };
  1031. MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
  1032. #ifdef CONFIG_ACPI
  1033. static const struct acpi_device_id raydium_acpi_id[] = {
  1034. { "RAYD0001", 0 },
  1035. { /* sentinel */ }
  1036. };
  1037. MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
  1038. #endif
  1039. #ifdef CONFIG_OF
  1040. static const struct of_device_id raydium_of_match[] = {
  1041. { .compatible = "raydium,rm32380", },
  1042. { /* sentinel */ }
  1043. };
  1044. MODULE_DEVICE_TABLE(of, raydium_of_match);
  1045. #endif
  1046. static struct i2c_driver raydium_i2c_driver = {
  1047. .probe = raydium_i2c_probe,
  1048. .id_table = raydium_i2c_id,
  1049. .driver = {
  1050. .name = "raydium_ts",
  1051. .pm = &raydium_i2c_pm_ops,
  1052. .acpi_match_table = ACPI_PTR(raydium_acpi_id),
  1053. .of_match_table = of_match_ptr(raydium_of_match),
  1054. },
  1055. };
  1056. module_i2c_driver(raydium_i2c_driver);
  1057. MODULE_AUTHOR("Raydium");
  1058. MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
  1059. MODULE_LICENSE("GPL v2");