ili210x.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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 irqreturn_t ili210x_irq(int irq, void *irq_data)
  279. {
  280. struct ili210x *priv = irq_data;
  281. struct i2c_client *client = priv->client;
  282. const struct ili2xxx_chip *chip = priv->chip;
  283. u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
  284. bool keep_polling;
  285. ktime_t time_next;
  286. s64 time_delta;
  287. bool touch;
  288. int error;
  289. do {
  290. time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
  291. error = chip->get_touch_data(client, touchdata);
  292. if (error) {
  293. dev_err(&client->dev,
  294. "Unable to get touch data: %d\n", error);
  295. break;
  296. }
  297. touch = ili210x_report_events(priv, touchdata);
  298. keep_polling = chip->continue_polling(touchdata, touch);
  299. if (keep_polling) {
  300. time_delta = ktime_us_delta(time_next, ktime_get());
  301. if (time_delta > 0)
  302. usleep_range(time_delta, time_delta + 1000);
  303. }
  304. } while (!priv->stop && keep_polling);
  305. return IRQ_HANDLED;
  306. }
  307. static int ili251x_firmware_update_resolution(struct device *dev)
  308. {
  309. struct i2c_client *client = to_i2c_client(dev);
  310. struct ili210x *priv = i2c_get_clientdata(client);
  311. u16 resx, resy;
  312. u8 rs[10];
  313. int error;
  314. /* The firmware update blob might have changed the resolution. */
  315. error = priv->chip->read_reg(client, REG_PANEL_INFO, &rs, sizeof(rs));
  316. if (!error) {
  317. resx = le16_to_cpup((__le16 *)rs);
  318. resy = le16_to_cpup((__le16 *)(rs + 2));
  319. /* The value reported by the firmware is invalid. */
  320. if (!resx || resx == 0xffff || !resy || resy == 0xffff)
  321. error = -EINVAL;
  322. }
  323. /*
  324. * In case of error, the firmware might be stuck in bootloader mode,
  325. * e.g. after a failed firmware update. Set maximum resolution, but
  326. * do not fail to probe, so the user can re-trigger the firmware
  327. * update and recover the touch controller.
  328. */
  329. if (error) {
  330. dev_warn(dev, "Invalid resolution reported by controller.\n");
  331. resx = 16384;
  332. resy = 16384;
  333. }
  334. input_abs_set_max(priv->input, ABS_X, resx - 1);
  335. input_abs_set_max(priv->input, ABS_Y, resy - 1);
  336. input_abs_set_max(priv->input, ABS_MT_POSITION_X, resx - 1);
  337. input_abs_set_max(priv->input, ABS_MT_POSITION_Y, resy - 1);
  338. return error;
  339. }
  340. static ssize_t ili251x_firmware_update_firmware_version(struct device *dev)
  341. {
  342. struct i2c_client *client = to_i2c_client(dev);
  343. struct ili210x *priv = i2c_get_clientdata(client);
  344. int error;
  345. u8 fw[8];
  346. /* Get firmware version */
  347. error = priv->chip->read_reg(client, REG_FIRMWARE_VERSION,
  348. &fw, sizeof(fw));
  349. if (!error)
  350. memcpy(priv->version_firmware, fw, sizeof(fw));
  351. return error;
  352. }
  353. static ssize_t ili251x_firmware_update_kernel_version(struct device *dev)
  354. {
  355. struct i2c_client *client = to_i2c_client(dev);
  356. struct ili210x *priv = i2c_get_clientdata(client);
  357. int error;
  358. u8 kv[5];
  359. /* Get kernel version */
  360. error = priv->chip->read_reg(client, REG_KERNEL_VERSION,
  361. &kv, sizeof(kv));
  362. if (!error)
  363. memcpy(priv->version_kernel, kv, sizeof(kv));
  364. return error;
  365. }
  366. static ssize_t ili251x_firmware_update_protocol_version(struct device *dev)
  367. {
  368. struct i2c_client *client = to_i2c_client(dev);
  369. struct ili210x *priv = i2c_get_clientdata(client);
  370. int error;
  371. u8 pv[2];
  372. /* Get protocol version */
  373. error = priv->chip->read_reg(client, REG_PROTOCOL_VERSION,
  374. &pv, sizeof(pv));
  375. if (!error)
  376. memcpy(priv->version_proto, pv, sizeof(pv));
  377. return error;
  378. }
  379. static ssize_t ili251x_firmware_update_ic_mode(struct device *dev)
  380. {
  381. struct i2c_client *client = to_i2c_client(dev);
  382. struct ili210x *priv = i2c_get_clientdata(client);
  383. int error;
  384. u8 md[2];
  385. /* Get chip boot mode */
  386. error = priv->chip->read_reg(client, REG_GET_MODE, &md, sizeof(md));
  387. if (!error)
  388. memcpy(priv->ic_mode, md, sizeof(md));
  389. return error;
  390. }
  391. static int ili251x_firmware_update_cached_state(struct device *dev)
  392. {
  393. struct i2c_client *client = to_i2c_client(dev);
  394. struct ili210x *priv = i2c_get_clientdata(client);
  395. int error;
  396. if (!priv->chip->has_firmware_proto)
  397. return 0;
  398. /* Wait for firmware to boot and stabilize itself. */
  399. msleep(200);
  400. /* Firmware does report valid information. */
  401. error = ili251x_firmware_update_resolution(dev);
  402. if (error)
  403. return error;
  404. error = ili251x_firmware_update_firmware_version(dev);
  405. if (error)
  406. return error;
  407. error = ili251x_firmware_update_kernel_version(dev);
  408. if (error)
  409. return error;
  410. error = ili251x_firmware_update_protocol_version(dev);
  411. if (error)
  412. return error;
  413. error = ili251x_firmware_update_ic_mode(dev);
  414. if (error)
  415. return error;
  416. return 0;
  417. }
  418. static ssize_t ili251x_firmware_version_show(struct device *dev,
  419. struct device_attribute *attr,
  420. char *buf)
  421. {
  422. struct i2c_client *client = to_i2c_client(dev);
  423. struct ili210x *priv = i2c_get_clientdata(client);
  424. u8 *fw = priv->version_firmware;
  425. return sysfs_emit(buf, "%02x%02x.%02x%02x.%02x%02x.%02x%02x\n",
  426. fw[0], fw[1], fw[2], fw[3],
  427. fw[4], fw[5], fw[6], fw[7]);
  428. }
  429. static DEVICE_ATTR(firmware_version, 0444, ili251x_firmware_version_show, NULL);
  430. static ssize_t ili251x_kernel_version_show(struct device *dev,
  431. struct device_attribute *attr,
  432. char *buf)
  433. {
  434. struct i2c_client *client = to_i2c_client(dev);
  435. struct ili210x *priv = i2c_get_clientdata(client);
  436. u8 *kv = priv->version_kernel;
  437. return sysfs_emit(buf, "%02x.%02x.%02x.%02x.%02x\n",
  438. kv[0], kv[1], kv[2], kv[3], kv[4]);
  439. }
  440. static DEVICE_ATTR(kernel_version, 0444, ili251x_kernel_version_show, NULL);
  441. static ssize_t ili251x_protocol_version_show(struct device *dev,
  442. struct device_attribute *attr,
  443. char *buf)
  444. {
  445. struct i2c_client *client = to_i2c_client(dev);
  446. struct ili210x *priv = i2c_get_clientdata(client);
  447. u8 *pv = priv->version_proto;
  448. return sysfs_emit(buf, "%02x.%02x\n", pv[0], pv[1]);
  449. }
  450. static DEVICE_ATTR(protocol_version, 0444, ili251x_protocol_version_show, NULL);
  451. static ssize_t ili251x_mode_show(struct device *dev,
  452. struct device_attribute *attr, char *buf)
  453. {
  454. struct i2c_client *client = to_i2c_client(dev);
  455. struct ili210x *priv = i2c_get_clientdata(client);
  456. u8 *md = priv->ic_mode;
  457. char *mode = "AP";
  458. if (md[0] == REG_GET_MODE_AP) /* Application Mode */
  459. mode = "AP";
  460. else if (md[0] == REG_GET_MODE_BL) /* BootLoader Mode */
  461. mode = "BL";
  462. else /* Unknown Mode */
  463. mode = "??";
  464. return sysfs_emit(buf, "%02x.%02x:%s\n", md[0], md[1], mode);
  465. }
  466. static DEVICE_ATTR(mode, 0444, ili251x_mode_show, NULL);
  467. static ssize_t ili210x_calibrate(struct device *dev,
  468. struct device_attribute *attr,
  469. const char *buf, size_t count)
  470. {
  471. struct i2c_client *client = to_i2c_client(dev);
  472. struct ili210x *priv = i2c_get_clientdata(client);
  473. unsigned long calibrate;
  474. int rc;
  475. u8 cmd = REG_CALIBRATE;
  476. if (kstrtoul(buf, 10, &calibrate))
  477. return -EINVAL;
  478. if (calibrate > 1)
  479. return -EINVAL;
  480. if (calibrate) {
  481. rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
  482. if (rc != sizeof(cmd))
  483. return -EIO;
  484. }
  485. return count;
  486. }
  487. static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
  488. static const u8 *ili251x_firmware_to_buffer(const struct firmware *fw,
  489. u16 *ac_end, u16 *df_end)
  490. {
  491. const struct ihex_binrec *rec;
  492. u32 fw_addr, fw_last_addr = 0;
  493. u16 fw_len;
  494. /*
  495. * The firmware ihex blob can never be bigger than 64 kiB, so make this
  496. * simple -- allocate a 64 kiB buffer, iterate over the ihex blob records
  497. * once, copy them all into this buffer at the right locations, and then
  498. * do all operations on this linear buffer.
  499. */
  500. u8* fw_buf __free(kvfree) = kvmalloc(SZ_64K, GFP_KERNEL);
  501. if (!fw_buf)
  502. return ERR_PTR(-ENOMEM);
  503. rec = (const struct ihex_binrec *)fw->data;
  504. while (rec) {
  505. fw_addr = be32_to_cpu(rec->addr);
  506. fw_len = be16_to_cpu(rec->len);
  507. /* The last 32 Byte firmware block can be 0xffe0 */
  508. if (fw_addr + fw_len > SZ_64K || fw_addr > SZ_64K - 32)
  509. return ERR_PTR(-EFBIG);
  510. /* Find the last address before DF start address, that is AC end */
  511. if (fw_addr == 0xf000)
  512. *ac_end = fw_last_addr;
  513. fw_last_addr = fw_addr + fw_len;
  514. memcpy(fw_buf + fw_addr, rec->data, fw_len);
  515. rec = ihex_next_binrec(rec);
  516. }
  517. /* DF end address is the last address in the firmware blob */
  518. *df_end = fw_addr + fw_len;
  519. return_ptr(fw_buf);
  520. }
  521. /* Switch mode between Application and BootLoader */
  522. static int ili251x_switch_ic_mode(struct i2c_client *client, u8 cmd_mode)
  523. {
  524. struct ili210x *priv = i2c_get_clientdata(client);
  525. u8 cmd_wren[3] = { REG_WRITE_ENABLE, 0x5a, 0xa5 };
  526. u8 md[2];
  527. int error;
  528. error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
  529. if (error)
  530. return error;
  531. /* Mode already set */
  532. if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
  533. (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
  534. return 0;
  535. /* Unlock writes */
  536. error = i2c_master_send(client, cmd_wren, sizeof(cmd_wren));
  537. if (error != sizeof(cmd_wren))
  538. return -EINVAL;
  539. mdelay(20);
  540. /* Select mode (BootLoader or Application) */
  541. error = i2c_master_send(client, &cmd_mode, 1);
  542. if (error != 1)
  543. return -EINVAL;
  544. mdelay(200); /* Reboot into bootloader takes a lot of time ... */
  545. /* Read back mode */
  546. error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md));
  547. if (error)
  548. return error;
  549. /* Check if mode is correct now. */
  550. if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) ||
  551. (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL))
  552. return 0;
  553. return -EINVAL;
  554. }
  555. static int ili251x_firmware_busy(struct i2c_client *client)
  556. {
  557. struct ili210x *priv = i2c_get_clientdata(client);
  558. int error, i = 0;
  559. u8 data;
  560. do {
  561. /* The read_reg already contains suitable delay */
  562. error = priv->chip->read_reg(client, REG_IC_BUSY, &data, 1);
  563. if (error)
  564. return error;
  565. if (i++ == 100000)
  566. return -ETIMEDOUT;
  567. } while (data != REG_IC_BUSY_NOT_BUSY);
  568. return 0;
  569. }
  570. static int ili251x_firmware_write_to_ic(struct device *dev, const u8 *fwbuf,
  571. u16 start, u16 end, u8 dataflash)
  572. {
  573. struct i2c_client *client = to_i2c_client(dev);
  574. struct ili210x *priv = i2c_get_clientdata(client);
  575. u8 cmd_crc = REG_READ_DATA_CRC;
  576. u8 crcrb[4] = { 0 };
  577. u8 fw_data[33];
  578. u16 fw_addr;
  579. int error;
  580. /*
  581. * The DF (dataflash) needs 2 bytes offset for unknown reasons,
  582. * the AC (application) has 2 bytes CRC16-CCITT at the end.
  583. */
  584. u16 crc = crc_ccitt(0, fwbuf + start + (dataflash ? 2 : 0),
  585. end - start - 2);
  586. /* Unlock write to either AC (application) or DF (dataflash) area */
  587. u8 cmd_wr[10] = {
  588. REG_WRITE_ENABLE, 0x5a, 0xa5, dataflash,
  589. (end >> 16) & 0xff, (end >> 8) & 0xff, end & 0xff,
  590. (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff
  591. };
  592. error = i2c_master_send(client, cmd_wr, sizeof(cmd_wr));
  593. if (error != sizeof(cmd_wr))
  594. return -EINVAL;
  595. error = ili251x_firmware_busy(client);
  596. if (error)
  597. return error;
  598. for (fw_addr = start; fw_addr < end; fw_addr += 32) {
  599. fw_data[0] = REG_WRITE_DATA;
  600. memcpy(&(fw_data[1]), fwbuf + fw_addr, 32);
  601. error = i2c_master_send(client, fw_data, 33);
  602. if (error != sizeof(fw_data))
  603. return error;
  604. error = ili251x_firmware_busy(client);
  605. if (error)
  606. return error;
  607. }
  608. error = i2c_master_send(client, &cmd_crc, 1);
  609. if (error != 1)
  610. return -EINVAL;
  611. error = ili251x_firmware_busy(client);
  612. if (error)
  613. return error;
  614. error = priv->chip->read_reg(client, REG_READ_DATA_CRC,
  615. &crcrb, sizeof(crcrb));
  616. if (error)
  617. return error;
  618. /* Check CRC readback */
  619. if ((crcrb[0] != (crc & 0xff)) || crcrb[1] != ((crc >> 8) & 0xff))
  620. return -EINVAL;
  621. return 0;
  622. }
  623. static int ili251x_firmware_reset(struct i2c_client *client)
  624. {
  625. u8 cmd_reset[2] = { 0xf2, 0x01 };
  626. int error;
  627. error = i2c_master_send(client, cmd_reset, sizeof(cmd_reset));
  628. if (error != sizeof(cmd_reset))
  629. return -EINVAL;
  630. return ili251x_firmware_busy(client);
  631. }
  632. static void ili210x_hardware_reset(struct gpio_desc *reset_gpio)
  633. {
  634. /* Reset the controller */
  635. gpiod_set_value_cansleep(reset_gpio, 1);
  636. usleep_range(12000, 15000);
  637. gpiod_set_value_cansleep(reset_gpio, 0);
  638. msleep(300);
  639. }
  640. static int ili210x_do_firmware_update(struct ili210x *priv,
  641. const u8 *fwbuf, u16 ac_end, u16 df_end)
  642. {
  643. struct i2c_client *client = priv->client;
  644. struct device *dev = &client->dev;
  645. int error;
  646. int i;
  647. error = ili251x_firmware_reset(client);
  648. if (error)
  649. return error;
  650. /* This may not succeed on first try, so re-try a few times. */
  651. for (i = 0; i < 5; i++) {
  652. error = ili251x_switch_ic_mode(client, REG_SET_MODE_BL);
  653. if (!error)
  654. break;
  655. }
  656. if (error)
  657. return error;
  658. dev_dbg(dev, "IC is now in BootLoader mode\n");
  659. msleep(200); /* The bootloader seems to need some time too. */
  660. error = ili251x_firmware_write_to_ic(dev, fwbuf, 0xf000, df_end, 1);
  661. if (error) {
  662. dev_err(dev, "DF firmware update failed, error=%d\n", error);
  663. return error;
  664. }
  665. dev_dbg(dev, "DataFlash firmware written\n");
  666. error = ili251x_firmware_write_to_ic(dev, fwbuf, 0x2000, ac_end, 0);
  667. if (error) {
  668. dev_err(dev, "AC firmware update failed, error=%d\n", error);
  669. return error;
  670. }
  671. dev_dbg(dev, "Application firmware written\n");
  672. /* This may not succeed on first try, so re-try a few times. */
  673. for (i = 0; i < 5; i++) {
  674. error = ili251x_switch_ic_mode(client, REG_SET_MODE_AP);
  675. if (!error)
  676. break;
  677. }
  678. if (error)
  679. return error;
  680. dev_dbg(dev, "IC is now in Application mode\n");
  681. error = ili251x_firmware_update_cached_state(dev);
  682. if (error)
  683. return error;
  684. return 0;
  685. }
  686. static ssize_t ili210x_firmware_update_store(struct device *dev,
  687. struct device_attribute *attr,
  688. const char *buf, size_t count)
  689. {
  690. struct i2c_client *client = to_i2c_client(dev);
  691. struct ili210x *priv = i2c_get_clientdata(client);
  692. const char *fwname = ILI251X_FW_FILENAME;
  693. u16 ac_end, df_end;
  694. int error;
  695. const struct firmware *fw __free(firmware) = NULL;
  696. error = request_ihex_firmware(&fw, fwname, dev);
  697. if (error) {
  698. dev_err(dev, "Failed to request firmware %s, error=%d\n",
  699. fwname, error);
  700. return error;
  701. }
  702. const u8* fwbuf __free(kvfree) =
  703. ili251x_firmware_to_buffer(fw, &ac_end, &df_end);
  704. error = PTR_ERR_OR_ZERO(fwbuf);
  705. if (error)
  706. return error;
  707. /*
  708. * Disable touchscreen IRQ, so that we would not get spurious touch
  709. * interrupt during firmware update, and so that the IRQ handler won't
  710. * trigger and interfere with the firmware update. There is no bit in
  711. * the touch controller to disable the IRQs during update, so we have
  712. * to do it this way here.
  713. */
  714. scoped_guard(disable_irq, &client->irq) {
  715. dev_dbg(dev, "Firmware update started, firmware=%s\n", fwname);
  716. ili210x_hardware_reset(priv->reset_gpio);
  717. error = ili210x_do_firmware_update(priv, fwbuf, ac_end, df_end);
  718. ili210x_hardware_reset(priv->reset_gpio);
  719. dev_dbg(dev, "Firmware update ended, error=%i\n", error);
  720. }
  721. return error ?: count;
  722. }
  723. static DEVICE_ATTR(firmware_update, 0200, NULL, ili210x_firmware_update_store);
  724. static struct attribute *ili210x_attrs[] = {
  725. &dev_attr_calibrate.attr,
  726. &dev_attr_firmware_update.attr,
  727. &dev_attr_firmware_version.attr,
  728. &dev_attr_kernel_version.attr,
  729. &dev_attr_protocol_version.attr,
  730. &dev_attr_mode.attr,
  731. NULL,
  732. };
  733. static umode_t ili210x_attributes_visible(struct kobject *kobj,
  734. struct attribute *attr, int index)
  735. {
  736. struct device *dev = kobj_to_dev(kobj);
  737. struct i2c_client *client = to_i2c_client(dev);
  738. struct ili210x *priv = i2c_get_clientdata(client);
  739. /* Calibrate is present on all ILI2xxx which have calibrate register */
  740. if (attr == &dev_attr_calibrate.attr)
  741. return priv->chip->has_calibrate_reg ? attr->mode : 0;
  742. /* Firmware/Kernel/Protocol/BootMode is implememted only for ILI251x */
  743. if (!priv->chip->has_firmware_proto)
  744. return 0;
  745. return attr->mode;
  746. }
  747. static const struct attribute_group ili210x_group = {
  748. .attrs = ili210x_attrs,
  749. .is_visible = ili210x_attributes_visible,
  750. };
  751. __ATTRIBUTE_GROUPS(ili210x);
  752. static void ili210x_power_down(void *data)
  753. {
  754. struct gpio_desc *reset_gpio = data;
  755. gpiod_set_value_cansleep(reset_gpio, 1);
  756. }
  757. static void ili210x_stop(void *data)
  758. {
  759. struct ili210x *priv = data;
  760. /* Tell ISR to quit even if there is a contact. */
  761. priv->stop = true;
  762. }
  763. static int ili210x_i2c_probe(struct i2c_client *client)
  764. {
  765. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  766. struct device *dev = &client->dev;
  767. const struct ili2xxx_chip *chip;
  768. struct ili210x *priv;
  769. struct gpio_desc *reset_gpio;
  770. struct input_dev *input;
  771. int error;
  772. unsigned int max_xy;
  773. dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
  774. chip = device_get_match_data(dev);
  775. if (!chip && id)
  776. chip = (const struct ili2xxx_chip *)id->driver_data;
  777. if (!chip) {
  778. dev_err(&client->dev, "unknown device model\n");
  779. return -ENODEV;
  780. }
  781. if (client->irq <= 0) {
  782. dev_err(dev, "No IRQ!\n");
  783. return -EINVAL;
  784. }
  785. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  786. if (IS_ERR(reset_gpio))
  787. return PTR_ERR(reset_gpio);
  788. if (reset_gpio) {
  789. error = devm_add_action_or_reset(dev, ili210x_power_down,
  790. reset_gpio);
  791. if (error)
  792. return error;
  793. ili210x_hardware_reset(reset_gpio);
  794. }
  795. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  796. if (!priv)
  797. return -ENOMEM;
  798. input = devm_input_allocate_device(dev);
  799. if (!input)
  800. return -ENOMEM;
  801. priv->client = client;
  802. priv->input = input;
  803. priv->reset_gpio = reset_gpio;
  804. priv->chip = chip;
  805. i2c_set_clientdata(client, priv);
  806. /* Setup input device */
  807. input->name = "ILI210x Touchscreen";
  808. input->id.bustype = BUS_I2C;
  809. /* Multi touch */
  810. max_xy = (chip->resolution ?: SZ_64K) - 1;
  811. input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
  812. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
  813. if (priv->chip->has_pressure_reg)
  814. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0);
  815. error = ili251x_firmware_update_cached_state(dev);
  816. if (error)
  817. dev_warn(dev, "Unable to cache firmware information, err: %d\n",
  818. error);
  819. touchscreen_parse_properties(input, true, &priv->prop);
  820. error = input_mt_init_slots(input, priv->chip->max_touches,
  821. INPUT_MT_DIRECT);
  822. if (error) {
  823. dev_err(dev, "Unable to set up slots, err: %d\n", error);
  824. return error;
  825. }
  826. error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
  827. IRQF_ONESHOT, client->name, priv);
  828. if (error) {
  829. dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
  830. error);
  831. return error;
  832. }
  833. error = devm_add_action_or_reset(dev, ili210x_stop, priv);
  834. if (error)
  835. return error;
  836. error = input_register_device(priv->input);
  837. if (error) {
  838. dev_err(dev, "Cannot register input device, err: %d\n", error);
  839. return error;
  840. }
  841. return 0;
  842. }
  843. static const struct i2c_device_id ili210x_i2c_id[] = {
  844. { "ili210x", (long)&ili210x_chip },
  845. { "ili2117", (long)&ili211x_chip },
  846. { "ili2120", (long)&ili212x_chip },
  847. { "ili251x", (long)&ili251x_chip },
  848. { }
  849. };
  850. MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
  851. static const struct of_device_id ili210x_dt_ids[] = {
  852. { .compatible = "ilitek,ili210x", .data = &ili210x_chip },
  853. { .compatible = "ilitek,ili2117", .data = &ili211x_chip },
  854. { .compatible = "ilitek,ili2120", .data = &ili212x_chip },
  855. { .compatible = "ilitek,ili251x", .data = &ili251x_chip },
  856. { }
  857. };
  858. MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
  859. static struct i2c_driver ili210x_ts_driver = {
  860. .driver = {
  861. .name = "ili210x_i2c",
  862. .dev_groups = ili210x_groups,
  863. .of_match_table = ili210x_dt_ids,
  864. },
  865. .id_table = ili210x_i2c_id,
  866. .probe = ili210x_i2c_probe,
  867. };
  868. module_i2c_driver(ili210x_ts_driver);
  869. MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
  870. MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
  871. MODULE_LICENSE("GPL");