wdt87xx_i2c.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * Weida HiTech WDT87xx TouchScreen I2C driver
  3. *
  4. * Copyright (c) 2015 Weida Hi-Tech Co., Ltd.
  5. * HN Chen <hn.chen@weidahitech.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. */
  11. #include <linux/i2c.h>
  12. #include <linux/input.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/irq.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/firmware.h>
  20. #include <linux/input/mt.h>
  21. #include <linux/acpi.h>
  22. #include <asm/unaligned.h>
  23. #define WDT87XX_NAME "wdt87xx_i2c"
  24. #define WDT87XX_DRV_VER "0.9.8"
  25. #define WDT87XX_FW_NAME "/*(DEBLOBBED)*/"
  26. #define WDT87XX_CFG_NAME "/*(DEBLOBBED)*/"
  27. #define MODE_ACTIVE 0x01
  28. #define MODE_READY 0x02
  29. #define MODE_IDLE 0x03
  30. #define MODE_SLEEP 0x04
  31. #define MODE_STOP 0xFF
  32. #define WDT_MAX_FINGER 10
  33. #define WDT_RAW_BUF_COUNT 54
  34. #define WDT_V1_RAW_BUF_COUNT 74
  35. #define WDT_FIRMWARE_ID 0xa9e368f5
  36. #define PG_SIZE 0x1000
  37. #define MAX_RETRIES 3
  38. #define MAX_UNIT_AXIS 0x7FFF
  39. #define PKT_READ_SIZE 72
  40. #define PKT_WRITE_SIZE 80
  41. /* the finger definition of the report event */
  42. #define FINGER_EV_OFFSET_ID 0
  43. #define FINGER_EV_OFFSET_X 1
  44. #define FINGER_EV_OFFSET_Y 3
  45. #define FINGER_EV_SIZE 5
  46. #define FINGER_EV_V1_OFFSET_ID 0
  47. #define FINGER_EV_V1_OFFSET_W 1
  48. #define FINGER_EV_V1_OFFSET_P 2
  49. #define FINGER_EV_V1_OFFSET_X 3
  50. #define FINGER_EV_V1_OFFSET_Y 5
  51. #define FINGER_EV_V1_SIZE 7
  52. /* The definition of a report packet */
  53. #define TOUCH_PK_OFFSET_REPORT_ID 0
  54. #define TOUCH_PK_OFFSET_EVENT 1
  55. #define TOUCH_PK_OFFSET_SCAN_TIME 51
  56. #define TOUCH_PK_OFFSET_FNGR_NUM 53
  57. #define TOUCH_PK_V1_OFFSET_REPORT_ID 0
  58. #define TOUCH_PK_V1_OFFSET_EVENT 1
  59. #define TOUCH_PK_V1_OFFSET_SCAN_TIME 71
  60. #define TOUCH_PK_V1_OFFSET_FNGR_NUM 73
  61. /* The definition of the controller parameters */
  62. #define CTL_PARAM_OFFSET_FW_ID 0
  63. #define CTL_PARAM_OFFSET_PLAT_ID 2
  64. #define CTL_PARAM_OFFSET_XMLS_ID1 4
  65. #define CTL_PARAM_OFFSET_XMLS_ID2 6
  66. #define CTL_PARAM_OFFSET_PHY_CH_X 8
  67. #define CTL_PARAM_OFFSET_PHY_CH_Y 10
  68. #define CTL_PARAM_OFFSET_PHY_X0 12
  69. #define CTL_PARAM_OFFSET_PHY_X1 14
  70. #define CTL_PARAM_OFFSET_PHY_Y0 16
  71. #define CTL_PARAM_OFFSET_PHY_Y1 18
  72. #define CTL_PARAM_OFFSET_PHY_W 22
  73. #define CTL_PARAM_OFFSET_PHY_H 24
  74. #define CTL_PARAM_OFFSET_FACTOR 32
  75. /* The definition of the device descriptor */
  76. #define WDT_GD_DEVICE 1
  77. #define DEV_DESC_OFFSET_VID 8
  78. #define DEV_DESC_OFFSET_PID 10
  79. /* Communication commands */
  80. #define PACKET_SIZE 56
  81. #define VND_REQ_READ 0x06
  82. #define VND_READ_DATA 0x07
  83. #define VND_REQ_WRITE 0x08
  84. #define VND_CMD_START 0x00
  85. #define VND_CMD_STOP 0x01
  86. #define VND_CMD_RESET 0x09
  87. #define VND_CMD_ERASE 0x1A
  88. #define VND_GET_CHECKSUM 0x66
  89. #define VND_SET_DATA 0x83
  90. #define VND_SET_COMMAND_DATA 0x84
  91. #define VND_SET_CHECKSUM_CALC 0x86
  92. #define VND_SET_CHECKSUM_LENGTH 0x87
  93. #define VND_CMD_SFLCK 0xFC
  94. #define VND_CMD_SFUNL 0xFD
  95. #define CMD_SFLCK_KEY 0xC39B
  96. #define CMD_SFUNL_KEY 0x95DA
  97. #define STRIDX_PLATFORM_ID 0x80
  98. #define STRIDX_PARAMETERS 0x81
  99. #define CMD_BUF_SIZE 8
  100. #define PKT_BUF_SIZE 64
  101. /* The definition of the command packet */
  102. #define CMD_REPORT_ID_OFFSET 0x0
  103. #define CMD_TYPE_OFFSET 0x1
  104. #define CMD_INDEX_OFFSET 0x2
  105. #define CMD_KEY_OFFSET 0x3
  106. #define CMD_LENGTH_OFFSET 0x4
  107. #define CMD_DATA_OFFSET 0x8
  108. /* The definition of firmware chunk tags */
  109. #define FOURCC_ID_RIFF 0x46464952
  110. #define FOURCC_ID_WHIF 0x46494857
  111. #define FOURCC_ID_FRMT 0x544D5246
  112. #define FOURCC_ID_FRWR 0x52575246
  113. #define FOURCC_ID_CNFG 0x47464E43
  114. #define CHUNK_ID_FRMT FOURCC_ID_FRMT
  115. #define CHUNK_ID_FRWR FOURCC_ID_FRWR
  116. #define CHUNK_ID_CNFG FOURCC_ID_CNFG
  117. #define FW_FOURCC1_OFFSET 0
  118. #define FW_SIZE_OFFSET 4
  119. #define FW_FOURCC2_OFFSET 8
  120. #define FW_PAYLOAD_OFFSET 40
  121. #define FW_CHUNK_ID_OFFSET 0
  122. #define FW_CHUNK_SIZE_OFFSET 4
  123. #define FW_CHUNK_TGT_START_OFFSET 8
  124. #define FW_CHUNK_PAYLOAD_LEN_OFFSET 12
  125. #define FW_CHUNK_SRC_START_OFFSET 16
  126. #define FW_CHUNK_VERSION_OFFSET 20
  127. #define FW_CHUNK_ATTR_OFFSET 24
  128. #define FW_CHUNK_PAYLOAD_OFFSET 32
  129. /* Controller requires minimum 300us between commands */
  130. #define WDT_COMMAND_DELAY_MS 2
  131. #define WDT_FLASH_WRITE_DELAY_MS 4
  132. #define WDT_FLASH_ERASE_DELAY_MS 200
  133. #define WDT_FW_RESET_TIME 2500
  134. struct wdt87xx_sys_param {
  135. u16 fw_id;
  136. u16 plat_id;
  137. u16 xmls_id1;
  138. u16 xmls_id2;
  139. u16 phy_ch_x;
  140. u16 phy_ch_y;
  141. u16 phy_w;
  142. u16 phy_h;
  143. u16 scaling_factor;
  144. u32 max_x;
  145. u32 max_y;
  146. u16 vendor_id;
  147. u16 product_id;
  148. };
  149. struct wdt87xx_data {
  150. struct i2c_client *client;
  151. struct input_dev *input;
  152. /* Mutex for fw update to prevent concurrent access */
  153. struct mutex fw_mutex;
  154. struct wdt87xx_sys_param param;
  155. u8 phys[32];
  156. };
  157. static int wdt87xx_i2c_xfer(struct i2c_client *client,
  158. void *txdata, size_t txlen,
  159. void *rxdata, size_t rxlen)
  160. {
  161. struct i2c_msg msgs[] = {
  162. {
  163. .addr = client->addr,
  164. .flags = 0,
  165. .len = txlen,
  166. .buf = txdata,
  167. },
  168. {
  169. .addr = client->addr,
  170. .flags = I2C_M_RD,
  171. .len = rxlen,
  172. .buf = rxdata,
  173. },
  174. };
  175. int error;
  176. int ret;
  177. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  178. if (ret != ARRAY_SIZE(msgs)) {
  179. error = ret < 0 ? ret : -EIO;
  180. dev_err(&client->dev, "%s: i2c transfer failed: %d\n",
  181. __func__, error);
  182. return error;
  183. }
  184. return 0;
  185. }
  186. static int wdt87xx_get_desc(struct i2c_client *client, u8 desc_idx,
  187. u8 *buf, size_t len)
  188. {
  189. u8 tx_buf[] = { 0x22, 0x00, 0x10, 0x0E, 0x23, 0x00 };
  190. int error;
  191. tx_buf[2] |= desc_idx & 0xF;
  192. error = wdt87xx_i2c_xfer(client, tx_buf, sizeof(tx_buf),
  193. buf, len);
  194. if (error) {
  195. dev_err(&client->dev, "get desc failed: %d\n", error);
  196. return error;
  197. }
  198. if (buf[0] != len) {
  199. dev_err(&client->dev, "unexpected response to get desc: %d\n",
  200. buf[0]);
  201. return -EINVAL;
  202. }
  203. mdelay(WDT_COMMAND_DELAY_MS);
  204. return 0;
  205. }
  206. static int wdt87xx_get_string(struct i2c_client *client, u8 str_idx,
  207. u8 *buf, size_t len)
  208. {
  209. u8 tx_buf[] = { 0x22, 0x00, 0x13, 0x0E, str_idx, 0x23, 0x00 };
  210. u8 rx_buf[PKT_WRITE_SIZE];
  211. size_t rx_len = len + 2;
  212. int error;
  213. if (rx_len > sizeof(rx_buf))
  214. return -EINVAL;
  215. error = wdt87xx_i2c_xfer(client, tx_buf, sizeof(tx_buf),
  216. rx_buf, rx_len);
  217. if (error) {
  218. dev_err(&client->dev, "get string failed: %d\n", error);
  219. return error;
  220. }
  221. if (rx_buf[1] != 0x03) {
  222. dev_err(&client->dev, "unexpected response to get string: %d\n",
  223. rx_buf[1]);
  224. return -EINVAL;
  225. }
  226. rx_len = min_t(size_t, len, rx_buf[0]);
  227. memcpy(buf, &rx_buf[2], rx_len);
  228. mdelay(WDT_COMMAND_DELAY_MS);
  229. return 0;
  230. }
  231. static int wdt87xx_get_feature(struct i2c_client *client,
  232. u8 *buf, size_t buf_size)
  233. {
  234. u8 tx_buf[8];
  235. u8 rx_buf[PKT_WRITE_SIZE];
  236. size_t tx_len = 0;
  237. size_t rx_len = buf_size + 2;
  238. int error;
  239. if (rx_len > sizeof(rx_buf))
  240. return -EINVAL;
  241. /* Get feature command packet */
  242. tx_buf[tx_len++] = 0x22;
  243. tx_buf[tx_len++] = 0x00;
  244. if (buf[CMD_REPORT_ID_OFFSET] > 0xF) {
  245. tx_buf[tx_len++] = 0x30;
  246. tx_buf[tx_len++] = 0x02;
  247. tx_buf[tx_len++] = buf[CMD_REPORT_ID_OFFSET];
  248. } else {
  249. tx_buf[tx_len++] = 0x30 | buf[CMD_REPORT_ID_OFFSET];
  250. tx_buf[tx_len++] = 0x02;
  251. }
  252. tx_buf[tx_len++] = 0x23;
  253. tx_buf[tx_len++] = 0x00;
  254. error = wdt87xx_i2c_xfer(client, tx_buf, tx_len, rx_buf, rx_len);
  255. if (error) {
  256. dev_err(&client->dev, "get feature failed: %d\n", error);
  257. return error;
  258. }
  259. rx_len = min_t(size_t, buf_size, get_unaligned_le16(rx_buf));
  260. memcpy(buf, &rx_buf[2], rx_len);
  261. mdelay(WDT_COMMAND_DELAY_MS);
  262. return 0;
  263. }
  264. static int wdt87xx_set_feature(struct i2c_client *client,
  265. const u8 *buf, size_t buf_size)
  266. {
  267. u8 tx_buf[PKT_WRITE_SIZE];
  268. int tx_len = 0;
  269. int error;
  270. /* Set feature command packet */
  271. tx_buf[tx_len++] = 0x22;
  272. tx_buf[tx_len++] = 0x00;
  273. if (buf[CMD_REPORT_ID_OFFSET] > 0xF) {
  274. tx_buf[tx_len++] = 0x30;
  275. tx_buf[tx_len++] = 0x03;
  276. tx_buf[tx_len++] = buf[CMD_REPORT_ID_OFFSET];
  277. } else {
  278. tx_buf[tx_len++] = 0x30 | buf[CMD_REPORT_ID_OFFSET];
  279. tx_buf[tx_len++] = 0x03;
  280. }
  281. tx_buf[tx_len++] = 0x23;
  282. tx_buf[tx_len++] = 0x00;
  283. tx_buf[tx_len++] = (buf_size & 0xFF);
  284. tx_buf[tx_len++] = ((buf_size & 0xFF00) >> 8);
  285. if (tx_len + buf_size > sizeof(tx_buf))
  286. return -EINVAL;
  287. memcpy(&tx_buf[tx_len], buf, buf_size);
  288. tx_len += buf_size;
  289. error = i2c_master_send(client, tx_buf, tx_len);
  290. if (error < 0) {
  291. dev_err(&client->dev, "set feature failed: %d\n", error);
  292. return error;
  293. }
  294. mdelay(WDT_COMMAND_DELAY_MS);
  295. return 0;
  296. }
  297. static int wdt87xx_send_command(struct i2c_client *client, int cmd, int value)
  298. {
  299. u8 cmd_buf[CMD_BUF_SIZE];
  300. /* Set the command packet */
  301. cmd_buf[CMD_REPORT_ID_OFFSET] = VND_REQ_WRITE;
  302. cmd_buf[CMD_TYPE_OFFSET] = VND_SET_COMMAND_DATA;
  303. put_unaligned_le16((u16)cmd, &cmd_buf[CMD_INDEX_OFFSET]);
  304. switch (cmd) {
  305. case VND_CMD_START:
  306. case VND_CMD_STOP:
  307. case VND_CMD_RESET:
  308. /* Mode selector */
  309. put_unaligned_le32((value & 0xFF), &cmd_buf[CMD_LENGTH_OFFSET]);
  310. break;
  311. case VND_CMD_SFLCK:
  312. put_unaligned_le16(CMD_SFLCK_KEY, &cmd_buf[CMD_KEY_OFFSET]);
  313. break;
  314. case VND_CMD_SFUNL:
  315. put_unaligned_le16(CMD_SFUNL_KEY, &cmd_buf[CMD_KEY_OFFSET]);
  316. break;
  317. case VND_CMD_ERASE:
  318. case VND_SET_CHECKSUM_CALC:
  319. case VND_SET_CHECKSUM_LENGTH:
  320. put_unaligned_le32(value, &cmd_buf[CMD_KEY_OFFSET]);
  321. break;
  322. default:
  323. cmd_buf[CMD_REPORT_ID_OFFSET] = 0;
  324. dev_err(&client->dev, "Invalid command: %d\n", cmd);
  325. return -EINVAL;
  326. }
  327. return wdt87xx_set_feature(client, cmd_buf, sizeof(cmd_buf));
  328. }
  329. static int wdt87xx_sw_reset(struct i2c_client *client)
  330. {
  331. int error;
  332. dev_dbg(&client->dev, "resetting device now\n");
  333. error = wdt87xx_send_command(client, VND_CMD_RESET, 0);
  334. if (error) {
  335. dev_err(&client->dev, "reset failed\n");
  336. return error;
  337. }
  338. /* Wait the device to be ready */
  339. msleep(WDT_FW_RESET_TIME);
  340. return 0;
  341. }
  342. static const void *wdt87xx_get_fw_chunk(const struct firmware *fw, u32 id)
  343. {
  344. size_t pos = FW_PAYLOAD_OFFSET;
  345. u32 chunk_id, chunk_size;
  346. while (pos < fw->size) {
  347. chunk_id = get_unaligned_le32(fw->data +
  348. pos + FW_CHUNK_ID_OFFSET);
  349. if (chunk_id == id)
  350. return fw->data + pos;
  351. chunk_size = get_unaligned_le32(fw->data +
  352. pos + FW_CHUNK_SIZE_OFFSET);
  353. pos += chunk_size + 2 * sizeof(u32); /* chunk ID + size */
  354. }
  355. return NULL;
  356. }
  357. static int wdt87xx_get_sysparam(struct i2c_client *client,
  358. struct wdt87xx_sys_param *param)
  359. {
  360. u8 buf[PKT_READ_SIZE];
  361. int error;
  362. error = wdt87xx_get_desc(client, WDT_GD_DEVICE, buf, 18);
  363. if (error) {
  364. dev_err(&client->dev, "failed to get device desc\n");
  365. return error;
  366. }
  367. param->vendor_id = get_unaligned_le16(buf + DEV_DESC_OFFSET_VID);
  368. param->product_id = get_unaligned_le16(buf + DEV_DESC_OFFSET_PID);
  369. error = wdt87xx_get_string(client, STRIDX_PARAMETERS, buf, 34);
  370. if (error) {
  371. dev_err(&client->dev, "failed to get parameters\n");
  372. return error;
  373. }
  374. param->xmls_id1 = get_unaligned_le16(buf + CTL_PARAM_OFFSET_XMLS_ID1);
  375. param->xmls_id2 = get_unaligned_le16(buf + CTL_PARAM_OFFSET_XMLS_ID2);
  376. param->phy_ch_x = get_unaligned_le16(buf + CTL_PARAM_OFFSET_PHY_CH_X);
  377. param->phy_ch_y = get_unaligned_le16(buf + CTL_PARAM_OFFSET_PHY_CH_Y);
  378. param->phy_w = get_unaligned_le16(buf + CTL_PARAM_OFFSET_PHY_W) / 10;
  379. param->phy_h = get_unaligned_le16(buf + CTL_PARAM_OFFSET_PHY_H) / 10;
  380. /* Get the scaling factor of pixel to logical coordinate */
  381. param->scaling_factor =
  382. get_unaligned_le16(buf + CTL_PARAM_OFFSET_FACTOR);
  383. param->max_x = MAX_UNIT_AXIS;
  384. param->max_y = DIV_ROUND_CLOSEST(MAX_UNIT_AXIS * param->phy_h,
  385. param->phy_w);
  386. error = wdt87xx_get_string(client, STRIDX_PLATFORM_ID, buf, 8);
  387. if (error) {
  388. dev_err(&client->dev, "failed to get platform id\n");
  389. return error;
  390. }
  391. param->plat_id = buf[1];
  392. buf[0] = 0xf2;
  393. error = wdt87xx_get_feature(client, buf, 16);
  394. if (error) {
  395. dev_err(&client->dev, "failed to get firmware id\n");
  396. return error;
  397. }
  398. if (buf[0] != 0xf2) {
  399. dev_err(&client->dev, "wrong id of fw response: 0x%x\n",
  400. buf[0]);
  401. return -EINVAL;
  402. }
  403. param->fw_id = get_unaligned_le16(&buf[1]);
  404. dev_info(&client->dev,
  405. "fw_id: 0x%x, plat_id: 0x%x, xml_id1: %04x, xml_id2: %04x\n",
  406. param->fw_id, param->plat_id,
  407. param->xmls_id1, param->xmls_id2);
  408. return 0;
  409. }
  410. static int wdt87xx_validate_firmware(struct wdt87xx_data *wdt,
  411. const struct firmware *fw)
  412. {
  413. const void *fw_chunk;
  414. u32 data1, data2;
  415. u32 size;
  416. u8 fw_chip_id;
  417. u8 chip_id;
  418. data1 = get_unaligned_le32(fw->data + FW_FOURCC1_OFFSET);
  419. data2 = get_unaligned_le32(fw->data + FW_FOURCC2_OFFSET);
  420. if (data1 != FOURCC_ID_RIFF || data2 != FOURCC_ID_WHIF) {
  421. dev_err(&wdt->client->dev, "check fw tag failed\n");
  422. return -EINVAL;
  423. }
  424. size = get_unaligned_le32(fw->data + FW_SIZE_OFFSET);
  425. if (size != fw->size) {
  426. dev_err(&wdt->client->dev,
  427. "fw size mismatch: expected %d, actual %zu\n",
  428. size, fw->size);
  429. return -EINVAL;
  430. }
  431. /*
  432. * Get the chip_id from the firmware. Make sure that it is the
  433. * right controller to do the firmware and config update.
  434. */
  435. fw_chunk = wdt87xx_get_fw_chunk(fw, CHUNK_ID_FRWR);
  436. if (!fw_chunk) {
  437. dev_err(&wdt->client->dev,
  438. "unable to locate firmware chunk\n");
  439. return -EINVAL;
  440. }
  441. fw_chip_id = (get_unaligned_le32(fw_chunk +
  442. FW_CHUNK_VERSION_OFFSET) >> 12) & 0xF;
  443. chip_id = (wdt->param.fw_id >> 12) & 0xF;
  444. if (fw_chip_id != chip_id) {
  445. dev_err(&wdt->client->dev,
  446. "fw version mismatch: fw %d vs. chip %d\n",
  447. fw_chip_id, chip_id);
  448. return -ENODEV;
  449. }
  450. return 0;
  451. }
  452. static int wdt87xx_validate_fw_chunk(const void *data, int id)
  453. {
  454. if (id == CHUNK_ID_FRWR) {
  455. u32 fw_id;
  456. fw_id = get_unaligned_le32(data + FW_CHUNK_PAYLOAD_OFFSET);
  457. if (fw_id != WDT_FIRMWARE_ID)
  458. return -EINVAL;
  459. }
  460. return 0;
  461. }
  462. static int wdt87xx_write_data(struct i2c_client *client, const char *data,
  463. u32 address, int length)
  464. {
  465. u16 packet_size;
  466. int count = 0;
  467. int error;
  468. u8 pkt_buf[PKT_BUF_SIZE];
  469. /* Address and length should be 4 bytes aligned */
  470. if ((address & 0x3) != 0 || (length & 0x3) != 0) {
  471. dev_err(&client->dev,
  472. "addr & len must be 4 bytes aligned %x, %x\n",
  473. address, length);
  474. return -EINVAL;
  475. }
  476. while (length) {
  477. packet_size = min(length, PACKET_SIZE);
  478. pkt_buf[CMD_REPORT_ID_OFFSET] = VND_REQ_WRITE;
  479. pkt_buf[CMD_TYPE_OFFSET] = VND_SET_DATA;
  480. put_unaligned_le16(packet_size, &pkt_buf[CMD_INDEX_OFFSET]);
  481. put_unaligned_le32(address, &pkt_buf[CMD_LENGTH_OFFSET]);
  482. memcpy(&pkt_buf[CMD_DATA_OFFSET], data, packet_size);
  483. error = wdt87xx_set_feature(client, pkt_buf, sizeof(pkt_buf));
  484. if (error)
  485. return error;
  486. length -= packet_size;
  487. data += packet_size;
  488. address += packet_size;
  489. /* Wait for the controller to finish the write */
  490. mdelay(WDT_FLASH_WRITE_DELAY_MS);
  491. if ((++count % 32) == 0) {
  492. /* Delay for fw to clear watch dog */
  493. msleep(20);
  494. }
  495. }
  496. return 0;
  497. }
  498. static u16 misr(u16 cur_value, u8 new_value)
  499. {
  500. u32 a, b;
  501. u32 bit0;
  502. u32 y;
  503. a = cur_value;
  504. b = new_value;
  505. bit0 = a ^ (b & 1);
  506. bit0 ^= a >> 1;
  507. bit0 ^= a >> 2;
  508. bit0 ^= a >> 4;
  509. bit0 ^= a >> 5;
  510. bit0 ^= a >> 7;
  511. bit0 ^= a >> 11;
  512. bit0 ^= a >> 15;
  513. y = (a << 1) ^ b;
  514. y = (y & ~1) | (bit0 & 1);
  515. return (u16)y;
  516. }
  517. static u16 wdt87xx_calculate_checksum(const u8 *data, size_t length)
  518. {
  519. u16 checksum = 0;
  520. size_t i;
  521. for (i = 0; i < length; i++)
  522. checksum = misr(checksum, data[i]);
  523. return checksum;
  524. }
  525. static int wdt87xx_get_checksum(struct i2c_client *client, u16 *checksum,
  526. u32 address, int length)
  527. {
  528. int error;
  529. int time_delay;
  530. u8 pkt_buf[PKT_BUF_SIZE];
  531. u8 cmd_buf[CMD_BUF_SIZE];
  532. error = wdt87xx_send_command(client, VND_SET_CHECKSUM_LENGTH, length);
  533. if (error) {
  534. dev_err(&client->dev, "failed to set checksum length\n");
  535. return error;
  536. }
  537. error = wdt87xx_send_command(client, VND_SET_CHECKSUM_CALC, address);
  538. if (error) {
  539. dev_err(&client->dev, "failed to set checksum address\n");
  540. return error;
  541. }
  542. /* Wait the operation to complete */
  543. time_delay = DIV_ROUND_UP(length, 1024);
  544. msleep(time_delay * 30);
  545. memset(cmd_buf, 0, sizeof(cmd_buf));
  546. cmd_buf[CMD_REPORT_ID_OFFSET] = VND_REQ_READ;
  547. cmd_buf[CMD_TYPE_OFFSET] = VND_GET_CHECKSUM;
  548. error = wdt87xx_set_feature(client, cmd_buf, sizeof(cmd_buf));
  549. if (error) {
  550. dev_err(&client->dev, "failed to request checksum\n");
  551. return error;
  552. }
  553. memset(pkt_buf, 0, sizeof(pkt_buf));
  554. pkt_buf[CMD_REPORT_ID_OFFSET] = VND_READ_DATA;
  555. error = wdt87xx_get_feature(client, pkt_buf, sizeof(pkt_buf));
  556. if (error) {
  557. dev_err(&client->dev, "failed to read checksum\n");
  558. return error;
  559. }
  560. *checksum = get_unaligned_le16(&pkt_buf[CMD_DATA_OFFSET]);
  561. return 0;
  562. }
  563. static int wdt87xx_write_firmware(struct i2c_client *client, const void *chunk)
  564. {
  565. u32 start_addr = get_unaligned_le32(chunk + FW_CHUNK_TGT_START_OFFSET);
  566. u32 size = get_unaligned_le32(chunk + FW_CHUNK_PAYLOAD_LEN_OFFSET);
  567. const void *data = chunk + FW_CHUNK_PAYLOAD_OFFSET;
  568. int error;
  569. int err1;
  570. int page_size;
  571. int retry = 0;
  572. u16 device_checksum, firmware_checksum;
  573. dev_dbg(&client->dev, "start 4k page program\n");
  574. error = wdt87xx_send_command(client, VND_CMD_STOP, MODE_STOP);
  575. if (error) {
  576. dev_err(&client->dev, "stop report mode failed\n");
  577. return error;
  578. }
  579. error = wdt87xx_send_command(client, VND_CMD_SFUNL, 0);
  580. if (error) {
  581. dev_err(&client->dev, "unlock failed\n");
  582. goto out_enable_reporting;
  583. }
  584. mdelay(10);
  585. while (size) {
  586. dev_dbg(&client->dev, "%s: %x, %x\n", __func__,
  587. start_addr, size);
  588. page_size = min_t(u32, size, PG_SIZE);
  589. size -= page_size;
  590. for (retry = 0; retry < MAX_RETRIES; retry++) {
  591. error = wdt87xx_send_command(client, VND_CMD_ERASE,
  592. start_addr);
  593. if (error) {
  594. dev_err(&client->dev,
  595. "erase failed at %#08x\n", start_addr);
  596. break;
  597. }
  598. msleep(WDT_FLASH_ERASE_DELAY_MS);
  599. error = wdt87xx_write_data(client, data, start_addr,
  600. page_size);
  601. if (error) {
  602. dev_err(&client->dev,
  603. "write failed at %#08x (%d bytes)\n",
  604. start_addr, page_size);
  605. break;
  606. }
  607. error = wdt87xx_get_checksum(client, &device_checksum,
  608. start_addr, page_size);
  609. if (error) {
  610. dev_err(&client->dev,
  611. "failed to retrieve checksum for %#08x (len: %d)\n",
  612. start_addr, page_size);
  613. break;
  614. }
  615. firmware_checksum =
  616. wdt87xx_calculate_checksum(data, page_size);
  617. if (device_checksum == firmware_checksum)
  618. break;
  619. dev_err(&client->dev,
  620. "checksum fail: %d vs %d, retry %d\n",
  621. device_checksum, firmware_checksum, retry);
  622. }
  623. if (retry == MAX_RETRIES) {
  624. dev_err(&client->dev, "page write failed\n");
  625. error = -EIO;
  626. goto out_lock_device;
  627. }
  628. start_addr = start_addr + page_size;
  629. data = data + page_size;
  630. }
  631. out_lock_device:
  632. err1 = wdt87xx_send_command(client, VND_CMD_SFLCK, 0);
  633. if (err1)
  634. dev_err(&client->dev, "lock failed\n");
  635. mdelay(10);
  636. out_enable_reporting:
  637. err1 = wdt87xx_send_command(client, VND_CMD_START, 0);
  638. if (err1)
  639. dev_err(&client->dev, "start to report failed\n");
  640. return error ? error : err1;
  641. }
  642. static int wdt87xx_load_chunk(struct i2c_client *client,
  643. const struct firmware *fw, u32 ck_id)
  644. {
  645. const void *chunk;
  646. int error;
  647. chunk = wdt87xx_get_fw_chunk(fw, ck_id);
  648. if (!chunk) {
  649. dev_err(&client->dev, "unable to locate chunk (type %d)\n",
  650. ck_id);
  651. return -EINVAL;
  652. }
  653. error = wdt87xx_validate_fw_chunk(chunk, ck_id);
  654. if (error) {
  655. dev_err(&client->dev, "invalid chunk (type %d): %d\n",
  656. ck_id, error);
  657. return error;
  658. }
  659. error = wdt87xx_write_firmware(client, chunk);
  660. if (error) {
  661. dev_err(&client->dev,
  662. "failed to write fw chunk (type %d): %d\n",
  663. ck_id, error);
  664. return error;
  665. }
  666. return 0;
  667. }
  668. static int wdt87xx_do_update_firmware(struct i2c_client *client,
  669. const struct firmware *fw,
  670. unsigned int chunk_id)
  671. {
  672. struct wdt87xx_data *wdt = i2c_get_clientdata(client);
  673. int error;
  674. error = wdt87xx_validate_firmware(wdt, fw);
  675. if (error)
  676. return error;
  677. error = mutex_lock_interruptible(&wdt->fw_mutex);
  678. if (error)
  679. return error;
  680. disable_irq(client->irq);
  681. error = wdt87xx_load_chunk(client, fw, chunk_id);
  682. if (error) {
  683. dev_err(&client->dev,
  684. "firmware load failed (type: %d): %d\n",
  685. chunk_id, error);
  686. goto out;
  687. }
  688. error = wdt87xx_sw_reset(client);
  689. if (error) {
  690. dev_err(&client->dev, "soft reset failed: %d\n", error);
  691. goto out;
  692. }
  693. /* Refresh the parameters */
  694. error = wdt87xx_get_sysparam(client, &wdt->param);
  695. if (error)
  696. dev_err(&client->dev,
  697. "failed to refresh system parameters: %d\n", error);
  698. out:
  699. enable_irq(client->irq);
  700. mutex_unlock(&wdt->fw_mutex);
  701. return error ? error : 0;
  702. }
  703. static int wdt87xx_update_firmware(struct device *dev,
  704. const char *fw_name, unsigned int chunk_id)
  705. {
  706. struct i2c_client *client = to_i2c_client(dev);
  707. const struct firmware *fw;
  708. int error;
  709. error = reject_firmware(&fw, fw_name, dev);
  710. if (error) {
  711. dev_err(&client->dev, "unable to retrieve firmware %s: %d\n",
  712. fw_name, error);
  713. return error;
  714. }
  715. error = wdt87xx_do_update_firmware(client, fw, chunk_id);
  716. release_firmware(fw);
  717. return error ? error : 0;
  718. }
  719. static ssize_t config_csum_show(struct device *dev,
  720. struct device_attribute *attr, char *buf)
  721. {
  722. struct i2c_client *client = to_i2c_client(dev);
  723. struct wdt87xx_data *wdt = i2c_get_clientdata(client);
  724. u32 cfg_csum;
  725. cfg_csum = wdt->param.xmls_id1;
  726. cfg_csum = (cfg_csum << 16) | wdt->param.xmls_id2;
  727. return scnprintf(buf, PAGE_SIZE, "%x\n", cfg_csum);
  728. }
  729. static ssize_t fw_version_show(struct device *dev,
  730. struct device_attribute *attr, char *buf)
  731. {
  732. struct i2c_client *client = to_i2c_client(dev);
  733. struct wdt87xx_data *wdt = i2c_get_clientdata(client);
  734. return scnprintf(buf, PAGE_SIZE, "%x\n", wdt->param.fw_id);
  735. }
  736. static ssize_t plat_id_show(struct device *dev,
  737. struct device_attribute *attr, char *buf)
  738. {
  739. struct i2c_client *client = to_i2c_client(dev);
  740. struct wdt87xx_data *wdt = i2c_get_clientdata(client);
  741. return scnprintf(buf, PAGE_SIZE, "%x\n", wdt->param.plat_id);
  742. }
  743. static ssize_t update_config_store(struct device *dev,
  744. struct device_attribute *attr,
  745. const char *buf, size_t count)
  746. {
  747. int error;
  748. error = wdt87xx_update_firmware(dev, WDT87XX_CFG_NAME, CHUNK_ID_CNFG);
  749. return error ? error : count;
  750. }
  751. static ssize_t update_fw_store(struct device *dev,
  752. struct device_attribute *attr,
  753. const char *buf, size_t count)
  754. {
  755. int error;
  756. error = wdt87xx_update_firmware(dev, WDT87XX_FW_NAME, CHUNK_ID_FRWR);
  757. return error ? error : count;
  758. }
  759. static DEVICE_ATTR_RO(config_csum);
  760. static DEVICE_ATTR_RO(fw_version);
  761. static DEVICE_ATTR_RO(plat_id);
  762. static DEVICE_ATTR_WO(update_config);
  763. static DEVICE_ATTR_WO(update_fw);
  764. static struct attribute *wdt87xx_attrs[] = {
  765. &dev_attr_config_csum.attr,
  766. &dev_attr_fw_version.attr,
  767. &dev_attr_plat_id.attr,
  768. &dev_attr_update_config.attr,
  769. &dev_attr_update_fw.attr,
  770. NULL
  771. };
  772. static const struct attribute_group wdt87xx_attr_group = {
  773. .attrs = wdt87xx_attrs,
  774. };
  775. static void wdt87xx_report_contact(struct input_dev *input,
  776. struct wdt87xx_sys_param *param,
  777. u8 *buf)
  778. {
  779. int finger_id;
  780. u32 x, y, w;
  781. u8 p;
  782. finger_id = (buf[FINGER_EV_V1_OFFSET_ID] >> 3) - 1;
  783. if (finger_id < 0)
  784. return;
  785. /* Check if this is an active contact */
  786. if (!(buf[FINGER_EV_V1_OFFSET_ID] & 0x1))
  787. return;
  788. w = buf[FINGER_EV_V1_OFFSET_W];
  789. w *= param->scaling_factor;
  790. p = buf[FINGER_EV_V1_OFFSET_P];
  791. x = get_unaligned_le16(buf + FINGER_EV_V1_OFFSET_X);
  792. y = get_unaligned_le16(buf + FINGER_EV_V1_OFFSET_Y);
  793. y = DIV_ROUND_CLOSEST(y * param->phy_h, param->phy_w);
  794. /* Refuse incorrect coordinates */
  795. if (x > param->max_x || y > param->max_y)
  796. return;
  797. dev_dbg(input->dev.parent, "tip on (%d), x(%d), y(%d)\n",
  798. finger_id, x, y);
  799. input_mt_slot(input, finger_id);
  800. input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
  801. input_report_abs(input, ABS_MT_TOUCH_MAJOR, w);
  802. input_report_abs(input, ABS_MT_PRESSURE, p);
  803. input_report_abs(input, ABS_MT_POSITION_X, x);
  804. input_report_abs(input, ABS_MT_POSITION_Y, y);
  805. }
  806. static irqreturn_t wdt87xx_ts_interrupt(int irq, void *dev_id)
  807. {
  808. struct wdt87xx_data *wdt = dev_id;
  809. struct i2c_client *client = wdt->client;
  810. int i, fingers;
  811. int error;
  812. u8 raw_buf[WDT_V1_RAW_BUF_COUNT] = {0};
  813. error = i2c_master_recv(client, raw_buf, WDT_V1_RAW_BUF_COUNT);
  814. if (error < 0) {
  815. dev_err(&client->dev, "read v1 raw data failed: %d\n", error);
  816. goto irq_exit;
  817. }
  818. fingers = raw_buf[TOUCH_PK_V1_OFFSET_FNGR_NUM];
  819. if (!fingers)
  820. goto irq_exit;
  821. for (i = 0; i < WDT_MAX_FINGER; i++)
  822. wdt87xx_report_contact(wdt->input,
  823. &wdt->param,
  824. &raw_buf[TOUCH_PK_V1_OFFSET_EVENT +
  825. i * FINGER_EV_V1_SIZE]);
  826. input_mt_sync_frame(wdt->input);
  827. input_sync(wdt->input);
  828. irq_exit:
  829. return IRQ_HANDLED;
  830. }
  831. static int wdt87xx_ts_create_input_device(struct wdt87xx_data *wdt)
  832. {
  833. struct device *dev = &wdt->client->dev;
  834. struct input_dev *input;
  835. unsigned int res = DIV_ROUND_CLOSEST(MAX_UNIT_AXIS, wdt->param.phy_w);
  836. int error;
  837. input = devm_input_allocate_device(dev);
  838. if (!input) {
  839. dev_err(dev, "failed to allocate input device\n");
  840. return -ENOMEM;
  841. }
  842. wdt->input = input;
  843. input->name = "WDT87xx Touchscreen";
  844. input->id.bustype = BUS_I2C;
  845. input->id.vendor = wdt->param.vendor_id;
  846. input->id.product = wdt->param.product_id;
  847. input->phys = wdt->phys;
  848. input_set_abs_params(input, ABS_MT_POSITION_X, 0,
  849. wdt->param.max_x, 0, 0);
  850. input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
  851. wdt->param.max_y, 0, 0);
  852. input_abs_set_res(input, ABS_MT_POSITION_X, res);
  853. input_abs_set_res(input, ABS_MT_POSITION_Y, res);
  854. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
  855. 0, wdt->param.max_x, 0, 0);
  856. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xFF, 0, 0);
  857. input_mt_init_slots(input, WDT_MAX_FINGER,
  858. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  859. error = input_register_device(input);
  860. if (error) {
  861. dev_err(dev, "failed to register input device: %d\n", error);
  862. return error;
  863. }
  864. return 0;
  865. }
  866. static int wdt87xx_ts_probe(struct i2c_client *client,
  867. const struct i2c_device_id *id)
  868. {
  869. struct wdt87xx_data *wdt;
  870. int error;
  871. dev_dbg(&client->dev, "adapter=%d, client irq: %d\n",
  872. client->adapter->nr, client->irq);
  873. /* Check if the I2C function is ok in this adaptor */
  874. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  875. return -ENXIO;
  876. wdt = devm_kzalloc(&client->dev, sizeof(*wdt), GFP_KERNEL);
  877. if (!wdt)
  878. return -ENOMEM;
  879. wdt->client = client;
  880. mutex_init(&wdt->fw_mutex);
  881. i2c_set_clientdata(client, wdt);
  882. snprintf(wdt->phys, sizeof(wdt->phys), "i2c-%u-%04x/input0",
  883. client->adapter->nr, client->addr);
  884. error = wdt87xx_get_sysparam(client, &wdt->param);
  885. if (error)
  886. return error;
  887. error = wdt87xx_ts_create_input_device(wdt);
  888. if (error)
  889. return error;
  890. error = devm_request_threaded_irq(&client->dev, client->irq,
  891. NULL, wdt87xx_ts_interrupt,
  892. IRQF_ONESHOT,
  893. client->name, wdt);
  894. if (error) {
  895. dev_err(&client->dev, "request irq failed: %d\n", error);
  896. return error;
  897. }
  898. error = sysfs_create_group(&client->dev.kobj, &wdt87xx_attr_group);
  899. if (error) {
  900. dev_err(&client->dev, "create sysfs failed: %d\n", error);
  901. return error;
  902. }
  903. return 0;
  904. }
  905. static int wdt87xx_ts_remove(struct i2c_client *client)
  906. {
  907. sysfs_remove_group(&client->dev.kobj, &wdt87xx_attr_group);
  908. return 0;
  909. }
  910. static int __maybe_unused wdt87xx_suspend(struct device *dev)
  911. {
  912. struct i2c_client *client = to_i2c_client(dev);
  913. int error;
  914. disable_irq(client->irq);
  915. error = wdt87xx_send_command(client, VND_CMD_STOP, MODE_IDLE);
  916. if (error) {
  917. enable_irq(client->irq);
  918. dev_err(&client->dev,
  919. "failed to stop device when suspending: %d\n",
  920. error);
  921. return error;
  922. }
  923. return 0;
  924. }
  925. static int __maybe_unused wdt87xx_resume(struct device *dev)
  926. {
  927. struct i2c_client *client = to_i2c_client(dev);
  928. int error;
  929. /*
  930. * The chip may have been reset while system is resuming,
  931. * give it some time to settle.
  932. */
  933. mdelay(100);
  934. error = wdt87xx_send_command(client, VND_CMD_START, 0);
  935. if (error)
  936. dev_err(&client->dev,
  937. "failed to start device when resuming: %d\n",
  938. error);
  939. enable_irq(client->irq);
  940. return 0;
  941. }
  942. static SIMPLE_DEV_PM_OPS(wdt87xx_pm_ops, wdt87xx_suspend, wdt87xx_resume);
  943. static const struct i2c_device_id wdt87xx_dev_id[] = {
  944. { WDT87XX_NAME, 0 },
  945. { }
  946. };
  947. MODULE_DEVICE_TABLE(i2c, wdt87xx_dev_id);
  948. static const struct acpi_device_id wdt87xx_acpi_id[] = {
  949. { "WDHT0001", 0 },
  950. { }
  951. };
  952. MODULE_DEVICE_TABLE(acpi, wdt87xx_acpi_id);
  953. static struct i2c_driver wdt87xx_driver = {
  954. .probe = wdt87xx_ts_probe,
  955. .remove = wdt87xx_ts_remove,
  956. .id_table = wdt87xx_dev_id,
  957. .driver = {
  958. .name = WDT87XX_NAME,
  959. .pm = &wdt87xx_pm_ops,
  960. .acpi_match_table = ACPI_PTR(wdt87xx_acpi_id),
  961. },
  962. };
  963. module_i2c_driver(wdt87xx_driver);
  964. MODULE_AUTHOR("HN Chen <hn.chen@weidahitech.com>");
  965. MODULE_DESCRIPTION("WeidaHiTech WDT87XX Touchscreen driver");
  966. MODULE_VERSION(WDT87XX_DRV_VER);
  967. MODULE_LICENSE("GPL");