ili210x.c 26 KB

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