ili210x.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/crc-ccitt.h>
  3. #include <linux/delay.h>
  4. #include <linux/gpio/consumer.h>
  5. #include <linux/i2c.h>
  6. #include <linux/ihex.h>
  7. #include <linux/input.h>
  8. #include <linux/input/mt.h>
  9. #include <linux/input/touchscreen.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/sizes.h>
  14. #include <linux/slab.h>
  15. #include <linux/unaligned.h>
  16. #define ILI2XXX_POLL_PERIOD 15
  17. #define ILI210X_DATA_SIZE 64
  18. #define ILI211X_DATA_SIZE 43
  19. #define ILI251X_DATA_SIZE1 31
  20. #define ILI251X_DATA_SIZE2 20
  21. /* Touchscreen commands */
  22. #define REG_TOUCHDATA 0x10
  23. #define REG_PANEL_INFO 0x20
  24. #define REG_FIRMWARE_VERSION 0x40
  25. #define REG_PROTOCOL_VERSION 0x42
  26. #define REG_KERNEL_VERSION 0x61
  27. #define REG_IC_BUSY 0x80
  28. #define REG_IC_BUSY_NOT_BUSY 0x50
  29. #define REG_GET_MODE 0xc0
  30. #define REG_GET_MODE_AP 0x5a
  31. #define REG_GET_MODE_BL 0x55
  32. #define REG_SET_MODE_AP 0xc1
  33. #define REG_SET_MODE_BL 0xc2
  34. #define REG_WRITE_DATA 0xc3
  35. #define REG_WRITE_ENABLE 0xc4
  36. #define REG_READ_DATA_CRC 0xc7
  37. #define REG_CALIBRATE 0xcc
  38. #define ILI251X_FW_FILENAME "ilitek/ili251x.bin"
  39. struct ili2xxx_chip {
  40. int (*read_reg)(struct i2c_client *client, u8 reg,
  41. void *buf, size_t len);
  42. int (*get_touch_data)(struct i2c_client *client, u8 *data);
  43. bool (*parse_touch_data)(const u8 *data, unsigned int finger,
  44. unsigned int *x, unsigned int *y,
  45. unsigned int *z);
  46. bool (*continue_polling)(const u8 *data, bool touch);
  47. unsigned int max_touches;
  48. unsigned int resolution;
  49. bool has_calibrate_reg;
  50. bool has_firmware_proto;
  51. bool has_pressure_reg;
  52. };
  53. struct ili210x {
  54. struct i2c_client *client;
  55. struct input_dev *input;
  56. struct gpio_desc *reset_gpio;
  57. struct touchscreen_properties prop;
  58. const struct ili2xxx_chip *chip;
  59. u8 version_firmware[8];
  60. u8 version_kernel[5];
  61. u8 version_proto[2];
  62. u8 ic_mode[2];
  63. bool stop;
  64. };
  65. static int ili210x_read_reg(struct i2c_client *client,
  66. u8 reg, void *buf, size_t len)
  67. {
  68. struct i2c_msg msg[] = {
  69. {
  70. .addr = client->addr,
  71. .flags = 0,
  72. .len = 1,
  73. .buf = &reg,
  74. },
  75. {
  76. .addr = client->addr,
  77. .flags = I2C_M_RD,
  78. .len = len,
  79. .buf = buf,
  80. }
  81. };
  82. int error, ret;
  83. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  84. if (ret != ARRAY_SIZE(msg)) {
  85. error = ret < 0 ? ret : -EIO;
  86. dev_err(&client->dev, "%s failed: %d\n", __func__, error);
  87. return error;
  88. }
  89. return 0;
  90. }
  91. static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
  92. {
  93. return ili210x_read_reg(client, REG_TOUCHDATA,
  94. data, ILI210X_DATA_SIZE);
  95. }
  96. static bool ili210x_touchdata_to_coords(const u8 *touchdata,
  97. unsigned int finger,
  98. unsigned int *x, unsigned int *y,
  99. unsigned int *z)
  100. {
  101. if (!(touchdata[0] & BIT(finger)))
  102. return false;
  103. *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
  104. *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
  105. return true;
  106. }
  107. static bool ili210x_check_continue_polling(const u8 *data, bool touch)
  108. {
  109. return data[0] & 0xf3;
  110. }
  111. static const struct ili2xxx_chip ili210x_chip = {
  112. .read_reg = ili210x_read_reg,
  113. .get_touch_data = ili210x_read_touch_data,
  114. .parse_touch_data = ili210x_touchdata_to_coords,
  115. .continue_polling = ili210x_check_continue_polling,
  116. .max_touches = 2,
  117. .has_calibrate_reg = true,
  118. };
  119. static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
  120. {
  121. s16 sum = 0;
  122. int error;
  123. int ret;
  124. int i;
  125. ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE);
  126. if (ret != ILI211X_DATA_SIZE) {
  127. error = ret < 0 ? ret : -EIO;
  128. dev_err(&client->dev, "%s failed: %d\n", __func__, error);
  129. return error;
  130. }
  131. /* This chip uses custom checksum at the end of data */
  132. for (i = 0; i < ILI211X_DATA_SIZE - 1; i++)
  133. sum = (sum + data[i]) & 0xff;
  134. if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) {
  135. dev_err(&client->dev,
  136. "CRC error (crc=0x%02x expected=0x%02x)\n",
  137. sum, data[ILI211X_DATA_SIZE - 1]);
  138. return -EIO;
  139. }
  140. return 0;
  141. }
  142. static bool ili211x_touchdata_to_coords(const u8 *touchdata,
  143. unsigned int finger,
  144. unsigned int *x, unsigned int *y,
  145. unsigned int *z)
  146. {
  147. u32 data;
  148. data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0);
  149. if (data == 0xffffffff) /* Finger up */
  150. return false;
  151. *x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) |
  152. touchdata[1 + (finger * 4) + 1];
  153. *y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) |
  154. touchdata[1 + (finger * 4) + 2];
  155. return true;
  156. }
  157. static bool ili211x_decline_polling(const u8 *data, bool touch)
  158. {
  159. return false;
  160. }
  161. static const struct ili2xxx_chip ili211x_chip = {
  162. .read_reg = ili210x_read_reg,
  163. .get_touch_data = ili211x_read_touch_data,
  164. .parse_touch_data = ili211x_touchdata_to_coords,
  165. .continue_polling = ili211x_decline_polling,
  166. .max_touches = 10,
  167. .resolution = 2048,
  168. };
  169. static bool ili212x_touchdata_to_coords(const u8 *touchdata,
  170. unsigned int finger,
  171. unsigned int *x, unsigned int *y,
  172. unsigned int *z)
  173. {
  174. u16 val;
  175. val = get_unaligned_be16(touchdata + 3 + (finger * 5) + 0);
  176. if (!(val & BIT(15))) /* Touch indication */
  177. return false;
  178. *x = val & 0x3fff;
  179. *y = get_unaligned_be16(touchdata + 3 + (finger * 5) + 2);
  180. return true;
  181. }
  182. static bool ili212x_check_continue_polling(const u8 *data, bool touch)
  183. {
  184. return touch;
  185. }
  186. static const struct ili2xxx_chip ili212x_chip = {
  187. .read_reg = ili210x_read_reg,
  188. .get_touch_data = ili210x_read_touch_data,
  189. .parse_touch_data = ili212x_touchdata_to_coords,
  190. .continue_polling = ili212x_check_continue_polling,
  191. .max_touches = 10,
  192. .has_calibrate_reg = true,
  193. };
  194. static int ili251x_read_reg_common(struct i2c_client *client,
  195. u8 reg, void *buf, size_t len,
  196. unsigned int delay)
  197. {
  198. int error;
  199. int ret;
  200. ret = i2c_master_send(client, &reg, 1);
  201. if (ret == 1) {
  202. if (delay)
  203. usleep_range(delay, delay + 500);
  204. ret = i2c_master_recv(client, buf, len);
  205. if (ret == len)
  206. return 0;
  207. }
  208. error = ret < 0 ? ret : -EIO;
  209. dev_err(&client->dev, "%s failed: %d\n", __func__, error);
  210. return ret;
  211. }
  212. static int ili251x_read_reg(struct i2c_client *client,
  213. u8 reg, void *buf, size_t len)
  214. {
  215. return ili251x_read_reg_common(client, reg, buf, len, 5000);
  216. }
  217. static int ili251x_read_touch_data(struct i2c_client *client, u8 *data)
  218. {
  219. int error;
  220. error = ili251x_read_reg_common(client, REG_TOUCHDATA,
  221. data, ILI251X_DATA_SIZE1, 0);
  222. if (!error && data[0] == 2) {
  223. error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1,
  224. ILI251X_DATA_SIZE2);
  225. if (error >= 0)
  226. error = error == ILI251X_DATA_SIZE2 ? 0 : -EIO;
  227. }
  228. return error;
  229. }
  230. static bool ili251x_touchdata_to_coords(const u8 *touchdata,
  231. unsigned int finger,
  232. unsigned int *x, unsigned int *y,
  233. unsigned int *z)
  234. {
  235. u16 val;
  236. val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
  237. if (!(val & BIT(15))) /* Touch indication */
  238. return false;
  239. *x = val & 0x3fff;
  240. *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
  241. *z = touchdata[1 + (finger * 5) + 4];
  242. return true;
  243. }
  244. static bool ili251x_check_continue_polling(const u8 *data, bool touch)
  245. {
  246. return touch;
  247. }
  248. static const struct ili2xxx_chip ili251x_chip = {
  249. .read_reg = ili251x_read_reg,
  250. .get_touch_data = ili251x_read_touch_data,
  251. .parse_touch_data = ili251x_touchdata_to_coords,
  252. .continue_polling = ili251x_check_continue_polling,
  253. .max_touches = 10,
  254. .has_calibrate_reg = true,
  255. .has_firmware_proto = true,
  256. .has_pressure_reg = true,
  257. };
  258. static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
  259. {
  260. struct input_dev *input = priv->input;
  261. int i;
  262. bool contact = false, touch;
  263. unsigned int x = 0, y = 0, z = 0;
  264. for (i = 0; i < priv->chip->max_touches; i++) {
  265. touch = priv->chip->parse_touch_data(touchdata, i, &x, &y, &z);
  266. input_mt_slot(input, i);
  267. if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) {
  268. touchscreen_report_pos(input, &priv->prop, x, y, true);
  269. if (priv->chip->has_pressure_reg)
  270. input_report_abs(input, ABS_MT_PRESSURE, z);
  271. contact = true;
  272. }
  273. }
  274. input_mt_report_pointer_emulation(input, false);
  275. input_sync(input);
  276. return contact;
  277. }
  278. static void ili210x_process_events(struct ili210x *priv)
  279. {
  280. struct i2c_client *client = priv->client;
  281. const struct ili2xxx_chip *chip = priv->chip;
  282. u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
  283. bool keep_polling;
  284. ktime_t time_next;
  285. s64 time_delta;
  286. bool touch;
  287. int error;
  288. do {
  289. time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
  290. error = chip->get_touch_data(client, touchdata);
  291. if (error) {
  292. dev_err(&client->dev,
  293. "Unable to get touch data: %d\n", error);
  294. break;
  295. }
  296. touch = ili210x_report_events(priv, touchdata);
  297. keep_polling = chip->continue_polling(touchdata, touch);
  298. if (keep_polling) {
  299. time_delta = ktime_us_delta(time_next, ktime_get());
  300. if (time_delta > 0)
  301. usleep_range(time_delta, time_delta + 1000);
  302. }
  303. } while (!priv->stop && keep_polling);
  304. }
  305. static irqreturn_t ili210x_irq(int irq, void *irq_data)
  306. {
  307. struct ili210x *priv = irq_data;
  308. ili210x_process_events(priv);
  309. return IRQ_HANDLED;
  310. };
  311. static void ili210x_work_i2c_poll(struct input_dev *input)
  312. {
  313. struct ili210x *priv = input_get_drvdata(input);
  314. ili210x_process_events(priv);
  315. }
  316. static int ili251x_firmware_update_resolution(struct device *dev)
  317. {
  318. struct i2c_client *client = to_i2c_client(dev);
  319. struct ili210x *priv = i2c_get_clientdata(client);
  320. u16 resx, resy;
  321. u8 rs[10];
  322. int error;
  323. /* The firmware update blob might have changed the resolution. */
  324. error = priv->chip->read_reg(client, REG_PANEL_INFO, &rs, sizeof(rs));
  325. if (!error) {
  326. resx = le16_to_cpup((__le16 *)rs);
  327. resy = le16_to_cpup((__le16 *)(rs + 2));
  328. /* The value reported by the firmware is invalid. */
  329. if (!resx || resx == 0xffff || !resy || resy == 0xffff)
  330. error = -EINVAL;
  331. }
  332. /*
  333. * In case of error, the firmware might be stuck in bootloader mode,
  334. * e.g. after a failed firmware update. Set maximum resolution, but
  335. * do not fail to probe, so the user can re-trigger the firmware
  336. * update and recover the touch controller.
  337. */
  338. if (error) {
  339. dev_warn(dev, "Invalid resolution reported by controller.\n");
  340. resx = 16384;
  341. resy = 16384;
  342. }
  343. input_abs_set_max(priv->input, ABS_X, resx - 1);
  344. input_abs_set_max(priv->input, ABS_Y, resy - 1);
  345. input_abs_set_max(priv->input, ABS_MT_POSITION_X, resx - 1);
  346. input_abs_set_max(priv->input, ABS_MT_POSITION_Y, resy - 1);
  347. return error;
  348. }
  349. static ssize_t ili251x_firmware_update_firmware_version(struct device *dev)
  350. {
  351. struct i2c_client *client = to_i2c_client(dev);
  352. struct ili210x *priv = i2c_get_clientdata(client);
  353. int error;
  354. u8 fw[8];
  355. /* Get firmware version */
  356. error = priv->chip->read_reg(client, REG_FIRMWARE_VERSION,
  357. &fw, sizeof(fw));
  358. if (!error)
  359. memcpy(priv->version_firmware, fw, sizeof(fw));
  360. return error;
  361. }
  362. static ssize_t ili251x_firmware_update_kernel_version(struct device *dev)
  363. {
  364. struct i2c_client *client = to_i2c_client(dev);
  365. struct ili210x *priv = i2c_get_clientdata(client);
  366. int error;
  367. u8 kv[5];
  368. /* Get kernel version */
  369. error = priv->chip->read_reg(client, REG_KERNEL_VERSION,
  370. &kv, sizeof(kv));
  371. if (!error)
  372. memcpy(priv->version_kernel, kv, sizeof(kv));
  373. return error;
  374. }
  375. static ssize_t ili251x_firmware_update_protocol_version(struct device *dev)
  376. {
  377. struct i2c_client *client = to_i2c_client(dev);
  378. struct ili210x *priv = i2c_get_clientdata(client);
  379. int error;
  380. u8 pv[2];
  381. /* Get protocol version */
  382. error = priv->chip->read_reg(client, REG_PROTOCOL_VERSION,
  383. &pv, sizeof(pv));
  384. if (!error)
  385. memcpy(priv->version_proto, pv, sizeof(pv));
  386. return error;
  387. }
  388. static ssize_t ili251x_firmware_update_ic_mode(struct device *dev)
  389. {
  390. struct i2c_client *client = to_i2c_client(dev);
  391. struct ili210x *priv = i2c_get_clientdata(client);
  392. int error;
  393. u8 md[2];
  394. /* Get chip boot mode */
  395. error = priv->chip->read_reg(client, REG_GET_MODE, &md, sizeof(md));
  396. if (!error)
  397. memcpy(priv->ic_mode, md, sizeof(md));
  398. return error;
  399. }
  400. static int ili251x_firmware_update_cached_state(struct device *dev)
  401. {
  402. struct i2c_client *client = to_i2c_client(dev);
  403. struct ili210x *priv = i2c_get_clientdata(client);
  404. int error;
  405. if (!priv->chip->has_firmware_proto)
  406. return 0;
  407. /* Wait for firmware to boot and stabilize itself. */
  408. msleep(200);
  409. /* Firmware does report valid information. */
  410. error = ili251x_firmware_update_resolution(dev);
  411. if (error)
  412. return error;
  413. error = ili251x_firmware_update_firmware_version(dev);
  414. if (error)
  415. return error;
  416. error = ili251x_firmware_update_kernel_version(dev);
  417. if (error)
  418. return error;
  419. error = ili251x_firmware_update_protocol_version(dev);
  420. if (error)
  421. return error;
  422. error = ili251x_firmware_update_ic_mode(dev);
  423. if (error)
  424. return error;
  425. return 0;
  426. }
  427. static ssize_t ili251x_firmware_version_show(struct device *dev,
  428. struct device_attribute *attr,
  429. char *buf)
  430. {
  431. struct i2c_client *client = to_i2c_client(dev);
  432. struct ili210x *priv = i2c_get_clientdata(client);
  433. u8 *fw = priv->version_firmware;
  434. return sysfs_emit(buf, "%02x%02x.%02x%02x.%02x%02x.%02x%02x\n",
  435. fw[0], fw[1], fw[2], fw[3],
  436. fw[4], fw[5], fw[6], fw[7]);
  437. }
  438. static DEVICE_ATTR(firmware_version, 0444, ili251x_firmware_version_show, NULL);
  439. static ssize_t ili251x_kernel_version_show(struct device *dev,
  440. struct device_attribute *attr,
  441. char *buf)
  442. {
  443. struct i2c_client *client = to_i2c_client(dev);
  444. struct ili210x *priv = i2c_get_clientdata(client);
  445. u8 *kv = priv->version_kernel;
  446. return sysfs_emit(buf, "%02x.%02x.%02x.%02x.%02x\n",
  447. kv[0], kv[1], kv[2], kv[3], kv[4]);
  448. }
  449. static DEVICE_ATTR(kernel_version, 0444, ili251x_kernel_version_show, NULL);
  450. static ssize_t ili251x_protocol_version_show(struct device *dev,
  451. struct device_attribute *attr,
  452. char *buf)
  453. {
  454. struct i2c_client *client = to_i2c_client(dev);
  455. struct ili210x *priv = i2c_get_clientdata(client);
  456. u8 *pv = priv->version_proto;
  457. return sysfs_emit(buf, "%02x.%02x\n", pv[0], pv[1]);
  458. }
  459. static DEVICE_ATTR(protocol_version, 0444, ili251x_protocol_version_show, NULL);
  460. static ssize_t ili251x_mode_show(struct device *dev,
  461. struct device_attribute *attr, char *buf)
  462. {
  463. struct i2c_client *client = to_i2c_client(dev);
  464. struct ili210x *priv = i2c_get_clientdata(client);
  465. u8 *md = priv->ic_mode;
  466. char *mode = "AP";
  467. if (md[0] == REG_GET_MODE_AP) /* Application Mode */
  468. mode = "AP";
  469. else if (md[0] == REG_GET_MODE_BL) /* BootLoader Mode */
  470. mode = "BL";
  471. else /* Unknown Mode */
  472. mode = "??";
  473. return sysfs_emit(buf, "%02x.%02x:%s\n", md[0], md[1], mode);
  474. }
  475. static DEVICE_ATTR(mode, 0444, ili251x_mode_show, NULL);
  476. static ssize_t ili210x_calibrate(struct device *dev,
  477. struct device_attribute *attr,
  478. const char *buf, size_t count)
  479. {
  480. struct i2c_client *client = to_i2c_client(dev);
  481. struct ili210x *priv = i2c_get_clientdata(client);
  482. unsigned long calibrate;
  483. int rc;
  484. u8 cmd = REG_CALIBRATE;
  485. if (kstrtoul(buf, 10, &calibrate))
  486. return -EINVAL;
  487. if (calibrate > 1)
  488. return -EINVAL;
  489. if (calibrate) {
  490. rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
  491. if (rc != sizeof(cmd))
  492. return -EIO;
  493. }
  494. return count;
  495. }
  496. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
  497. static const u8 *ili251x_firmware_to_buffer(const struct firmware *fw,
  498. u16 *ac_end, u16 *df_end)
  499. {
  500. const struct ihex_binrec *rec;
  501. u32 fw_addr, fw_last_addr = 0;
  502. u16 fw_len;
  503. /*
  504. * The firmware ihex blob can never be bigger than 64 kiB, so make this
  505. * simple -- allocate a 64 kiB buffer, iterate over the ihex blob records
  506. * once, copy them all into this buffer at the right locations, and then
  507. * do all operations on this linear buffer.
  508. */
  509. u8* fw_buf __free(kvfree) = kvmalloc(SZ_64K, GFP_KERNEL);
  510. if (!fw_buf)
  511. return ERR_PTR(-ENOMEM);
  512. rec = (const struct ihex_binrec *)fw->data;
  513. while (rec) {
  514. fw_addr = be32_to_cpu(rec->addr);
  515. fw_len = be16_to_cpu(rec->len);
  516. /* The last 32 Byte firmware block can be 0xffe0 */
  517. if (fw_addr + fw_len > SZ_64K || fw_addr > SZ_64K - 32)
  518. return ERR_PTR(-EFBIG);
  519. /* Find the last address before DF start address, that is AC end */
  520. if (fw_addr == 0xf000)
  521. *ac_end = fw_last_addr;
  522. fw_last_addr = fw_addr + fw_len;
  523. memcpy(fw_buf + fw_addr, rec->data, fw_len);
  524. rec = ihex_next_binrec(rec);
  525. }
  526. /* DF end address is the last address in the firmware blob */
  527. *df_end = fw_addr + fw_len;
  528. return_ptr(fw_buf);
  529. }
  530. /* Switch mode between Application and BootLoader */
  531. static int ili251x_switch_ic_mode(struct i2c_client *client, u8 cmd_mode)
  532. {
  533. struct ili210x *priv = i2c_get_clientdata(client);
  534. u8 cmd_wren[3] = { REG_WRITE_ENABLE, 0x5a, 0xa5 };
  535. u8 md[2];
  536. int error;
  537. error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
  538. if (error)
  539. return error;
  540. /* Mode already set */
  541. if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
  542. (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
  543. return 0;
  544. /* Unlock writes */
  545. error = i2c_master_send(client, cmd_wren, sizeof(cmd_wren));
  546. if (error != sizeof(cmd_wren))
  547. return -EINVAL;
  548. mdelay(20);
  549. /* Select mode (BootLoader or Application) */
  550. error = i2c_master_send(client, &cmd_mode, 1);
  551. if (error != 1)
  552. return -EINVAL;
  553. mdelay(200); /* Reboot into bootloader takes a lot of time ... */
  554. /* Read back mode */
  555. error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
  556. if (error)
  557. return error;
  558. /* Check if mode is correct now. */
  559. if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
  560. (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
  561. return 0;
  562. return -EINVAL;
  563. }
  564. static int ili251x_firmware_busy(struct i2c_client *client)
  565. {
  566. struct ili210x *priv = i2c_get_clientdata(client);
  567. int error, i = 0;
  568. u8 data;
  569. do {
  570. /* The read_reg already contains suitable delay */
  571. error = priv->chip->read_reg(client, REG_IC_BUSY, &data, 1);
  572. if (error)
  573. return error;
  574. if (i++ == 100000)
  575. return -ETIMEDOUT;
  576. } while (data != REG_IC_BUSY_NOT_BUSY);
  577. return 0;
  578. }
  579. static int ili251x_firmware_write_to_ic(struct device *dev, const u8 *fwbuf,
  580. u16 start, u16 end, u8 dataflash)
  581. {
  582. struct i2c_client *client = to_i2c_client(dev);
  583. struct ili210x *priv = i2c_get_clientdata(client);
  584. u8 cmd_crc = REG_READ_DATA_CRC;
  585. u8 crcrb[4] = { 0 };
  586. u8 fw_data[33];
  587. u16 fw_addr;
  588. int error;
  589. /*
  590. * The DF (dataflash) needs 2 bytes offset for unknown reasons,
  591. * the AC (application) has 2 bytes CRC16-CCITT at the end.
  592. */
  593. u16 crc = crc_ccitt(0, fwbuf + start + (dataflash ? 2 : 0),
  594. end - start - 2);
  595. /* Unlock write to either AC (application) or DF (dataflash) area */
  596. u8 cmd_wr[10] = {
  597. REG_WRITE_ENABLE, 0x5a, 0xa5, dataflash,
  598. (end >> 16) & 0xff, (end >> 8) & 0xff, end & 0xff,
  599. (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff
  600. };
  601. error = i2c_master_send(client, cmd_wr, sizeof(cmd_wr));
  602. if (error != sizeof(cmd_wr))
  603. return -EINVAL;
  604. error = ili251x_firmware_busy(client);
  605. if (error)
  606. return error;
  607. for (fw_addr = start; fw_addr < end; fw_addr += 32) {
  608. fw_data[0] = REG_WRITE_DATA;
  609. memcpy(&(fw_data[1]), fwbuf + fw_addr, 32);
  610. error = i2c_master_send(client, fw_data, 33);
  611. if (error != sizeof(fw_data))
  612. return error;
  613. error = ili251x_firmware_busy(client);
  614. if (error)
  615. return error;
  616. }
  617. error = i2c_master_send(client, &cmd_crc, 1);
  618. if (error != 1)
  619. return -EINVAL;
  620. error = ili251x_firmware_busy(client);
  621. if (error)
  622. return error;
  623. error = priv->chip->read_reg(client, REG_READ_DATA_CRC,
  624. &crcrb, sizeof(crcrb));
  625. if (error)
  626. return error;
  627. /* Check CRC readback */
  628. if ((crcrb[0] != (crc & 0xff)) || crcrb[1] != ((crc >> 8) & 0xff))
  629. return -EINVAL;
  630. return 0;
  631. }
  632. static int ili251x_firmware_reset(struct i2c_client *client)
  633. {
  634. u8 cmd_reset[2] = { 0xf2, 0x01 };
  635. int error;
  636. error = i2c_master_send(client, cmd_reset, sizeof(cmd_reset));
  637. if (error != sizeof(cmd_reset))
  638. return -EINVAL;
  639. return ili251x_firmware_busy(client);
  640. }
  641. static void ili210x_hardware_reset(struct gpio_desc *reset_gpio)
  642. {
  643. /* Reset the controller */
  644. gpiod_set_value_cansleep(reset_gpio, 1);
  645. usleep_range(12000, 15000);
  646. gpiod_set_value_cansleep(reset_gpio, 0);
  647. msleep(300);
  648. }
  649. static int ili210x_do_firmware_update(struct ili210x *priv,
  650. const u8 *fwbuf, u16 ac_end, u16 df_end)
  651. {
  652. struct i2c_client *client = priv->client;
  653. struct device *dev = &client->dev;
  654. int error;
  655. int i;
  656. error = ili251x_firmware_reset(client);
  657. if (error)
  658. return error;
  659. /* This may not succeed on first try, so re-try a few times. */
  660. for (i = 0; i < 5; i++) {
  661. error = ili251x_switch_ic_mode(client, REG_SET_MODE_BL);
  662. if (!error)
  663. break;
  664. }
  665. if (error)
  666. return error;
  667. dev_dbg(dev, "IC is now in BootLoader mode\n");
  668. msleep(200); /* The bootloader seems to need some time too. */
  669. error = ili251x_firmware_write_to_ic(dev, fwbuf, 0xf000, df_end, 1);
  670. if (error) {
  671. dev_err(dev, "DF firmware update failed, error=%d\n", error);
  672. return error;
  673. }
  674. dev_dbg(dev, "DataFlash firmware written\n");
  675. error = ili251x_firmware_write_to_ic(dev, fwbuf, 0x2000, ac_end, 0);
  676. if (error) {
  677. dev_err(dev, "AC firmware update failed, error=%d\n", error);
  678. return error;
  679. }
  680. dev_dbg(dev, "Application firmware written\n");
  681. /* This may not succeed on first try, so re-try a few times. */
  682. for (i = 0; i < 5; i++) {
  683. error = ili251x_switch_ic_mode(client, REG_SET_MODE_AP);
  684. if (!error)
  685. break;
  686. }
  687. if (error)
  688. return error;
  689. dev_dbg(dev, "IC is now in Application mode\n");
  690. error = ili251x_firmware_update_cached_state(dev);
  691. if (error)
  692. return error;
  693. return 0;
  694. }
  695. static ssize_t ili210x_firmware_update(struct device *dev, const u8 *fwbuf,
  696. u16 ac_end, u16 df_end)
  697. {
  698. struct i2c_client *client = to_i2c_client(dev);
  699. struct ili210x *priv = i2c_get_clientdata(client);
  700. const char *fwname = ILI251X_FW_FILENAME;
  701. int error;
  702. dev_dbg(dev, "Firmware update started, firmware=%s\n", fwname);
  703. ili210x_hardware_reset(priv->reset_gpio);
  704. error = ili210x_do_firmware_update(priv, fwbuf, ac_end, df_end);
  705. ili210x_hardware_reset(priv->reset_gpio);
  706. dev_dbg(dev, "Firmware update ended, error=%i\n", error);
  707. return error;
  708. }
  709. static ssize_t ili210x_firmware_update_store(struct device *dev,
  710. struct device_attribute *attr,
  711. const char *buf, size_t count)
  712. {
  713. struct i2c_client *client = to_i2c_client(dev);
  714. const char *fwname = ILI251X_FW_FILENAME;
  715. u16 ac_end, df_end;
  716. int error;
  717. const struct firmware *fw __free(firmware) = NULL;
  718. error = request_ihex_firmware(&fw, fwname, dev);
  719. if (error) {
  720. dev_err(dev, "Failed to request firmware %s, error=%d\n",
  721. fwname, error);
  722. return error;
  723. }
  724. const u8* fwbuf __free(kvfree) =
  725. ili251x_firmware_to_buffer(fw, &ac_end, &df_end);
  726. error = PTR_ERR_OR_ZERO(fwbuf);
  727. if (error)
  728. return error;
  729. /*
  730. * Disable touchscreen IRQ, so that we would not get spurious touch
  731. * interrupt during firmware update, and so that the IRQ handler won't
  732. * trigger and interfere with the firmware update. There is no bit in
  733. * the touch controller to disable the IRQs during update, so we have
  734. * to do it this way here.
  735. */
  736. if (client->irq > 0) {
  737. guard(disable_irq)(&client->irq);
  738. error = ili210x_firmware_update(dev, fwbuf, ac_end, df_end);
  739. } else {
  740. error = ili210x_firmware_update(dev, fwbuf, ac_end, df_end);
  741. }
  742. return error ?: count;
  743. }
  744. static DEVICE_ATTR(firmware_update, 0200, NULL, ili210x_firmware_update_store);
  745. static struct attribute *ili210x_attrs[] = {
  746. &dev_attr_calibrate.attr,
  747. &dev_attr_firmware_update.attr,
  748. &dev_attr_firmware_version.attr,
  749. &dev_attr_kernel_version.attr,
  750. &dev_attr_protocol_version.attr,
  751. &dev_attr_mode.attr,
  752. NULL,
  753. };
  754. static umode_t ili210x_attributes_visible(struct kobject *kobj,
  755. struct attribute *attr, int index)
  756. {
  757. struct device *dev = kobj_to_dev(kobj);
  758. struct i2c_client *client = to_i2c_client(dev);
  759. struct ili210x *priv = i2c_get_clientdata(client);
  760. /* Calibrate is present on all ILI2xxx which have calibrate register */
  761. if (attr == &dev_attr_calibrate.attr)
  762. return priv->chip->has_calibrate_reg ? attr->mode : 0;
  763. /* Firmware/Kernel/Protocol/BootMode is implemented only for ILI251x */
  764. if (!priv->chip->has_firmware_proto)
  765. return 0;
  766. return attr->mode;
  767. }
  768. static const struct attribute_group ili210x_group = {
  769. .attrs = ili210x_attrs,
  770. .is_visible = ili210x_attributes_visible,
  771. };
  772. __ATTRIBUTE_GROUPS(ili210x);
  773. static void ili210x_power_down(void *data)
  774. {
  775. struct gpio_desc *reset_gpio = data;
  776. gpiod_set_value_cansleep(reset_gpio, 1);
  777. }
  778. static void ili210x_stop(void *data)
  779. {
  780. struct ili210x *priv = data;
  781. /* Tell ISR to quit even if there is a contact. */
  782. priv->stop = true;
  783. }
  784. static int ili210x_i2c_probe(struct i2c_client *client)
  785. {
  786. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  787. struct device *dev = &client->dev;
  788. const struct ili2xxx_chip *chip;
  789. struct ili210x *priv;
  790. struct gpio_desc *reset_gpio;
  791. struct input_dev *input;
  792. int error;
  793. unsigned int max_xy;
  794. dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
  795. chip = device_get_match_data(dev);
  796. if (!chip && id)
  797. chip = (const struct ili2xxx_chip *)id->driver_data;
  798. if (!chip)
  799. return dev_err_probe(&client->dev, -ENODEV, "unknown device model\n");
  800. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  801. if (IS_ERR(reset_gpio))
  802. return PTR_ERR(reset_gpio);
  803. if (reset_gpio) {
  804. error = devm_add_action_or_reset(dev, ili210x_power_down,
  805. reset_gpio);
  806. if (error)
  807. return error;
  808. ili210x_hardware_reset(reset_gpio);
  809. }
  810. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  811. if (!priv)
  812. return -ENOMEM;
  813. input = devm_input_allocate_device(dev);
  814. if (!input)
  815. return -ENOMEM;
  816. priv->client = client;
  817. priv->input = input;
  818. priv->reset_gpio = reset_gpio;
  819. priv->chip = chip;
  820. i2c_set_clientdata(client, priv);
  821. /* Setup input device */
  822. input->name = "ILI210x Touchscreen";
  823. input->id.bustype = BUS_I2C;
  824. /* Multi touch */
  825. max_xy = (chip->resolution ?: SZ_64K) - 1;
  826. input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
  827. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
  828. if (priv->chip->has_pressure_reg)
  829. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0);
  830. error = ili251x_firmware_update_cached_state(dev);
  831. if (error)
  832. dev_warn(dev, "Unable to cache firmware information, err: %d\n",
  833. error);
  834. touchscreen_parse_properties(input, true, &priv->prop);
  835. error = input_mt_init_slots(input, priv->chip->max_touches,
  836. INPUT_MT_DIRECT);
  837. if (error)
  838. return dev_err_probe(dev, error, "Unable to set up slots\n");
  839. input_set_drvdata(input, priv);
  840. if (client->irq > 0) {
  841. error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
  842. IRQF_ONESHOT, client->name, priv);
  843. if (error)
  844. return dev_err_probe(dev, error, "Unable to request touchscreen IRQ\n");
  845. } else {
  846. error = input_setup_polling(input, ili210x_work_i2c_poll);
  847. if (error)
  848. return dev_err_probe(dev, error, "Could not set up polling mode\n");
  849. input_set_poll_interval(input, ILI2XXX_POLL_PERIOD);
  850. }
  851. error = devm_add_action_or_reset(dev, ili210x_stop, priv);
  852. if (error)
  853. return error;
  854. error = input_register_device(priv->input);
  855. if (error)
  856. return dev_err_probe(dev, error, "Cannot register input device\n");
  857. return 0;
  858. }
  859. static const struct i2c_device_id ili210x_i2c_id[] = {
  860. { "ili210x", (long)&ili210x_chip },
  861. { "ili2117", (long)&ili211x_chip },
  862. { "ili2120", (long)&ili212x_chip },
  863. { "ili251x", (long)&ili251x_chip },
  864. { }
  865. };
  866. MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
  867. static const struct of_device_id ili210x_dt_ids[] = {
  868. { .compatible = "ilitek,ili210x", .data = &ili210x_chip },
  869. { .compatible = "ilitek,ili2117", .data = &ili211x_chip },
  870. { .compatible = "ilitek,ili2120", .data = &ili212x_chip },
  871. { .compatible = "ilitek,ili251x", .data = &ili251x_chip },
  872. { }
  873. };
  874. MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
  875. static struct i2c_driver ili210x_ts_driver = {
  876. .driver = {
  877. .name = "ili210x_i2c",
  878. .dev_groups = ili210x_groups,
  879. .of_match_table = ili210x_dt_ids,
  880. },
  881. .id_table = ili210x_i2c_id,
  882. .probe = ili210x_i2c_probe,
  883. };
  884. module_i2c_driver(ili210x_ts_driver);
  885. MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
  886. MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
  887. MODULE_LICENSE("GPL");