wdt87xx_i2c.c 28 KB

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