chipone_icn8505.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for ChipOne icn8505 i2c touchscreen controller
  4. *
  5. * Copyright (c) 2015-2018 Red Hat Inc.
  6. *
  7. * Red Hat authors:
  8. * Hans de Goede <[email protected]>
  9. */
  10. #include <asm/unaligned.h>
  11. #include <linux/acpi.h>
  12. #include <linux/crc32.h>
  13. #include <linux/delay.h>
  14. #include <linux/firmware.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/i2c.h>
  17. #include <linux/input.h>
  18. #include <linux/input/mt.h>
  19. #include <linux/input/touchscreen.h>
  20. #include <linux/module.h>
  21. /* Normal operation mode defines */
  22. #define ICN8505_REG_ADDR_WIDTH 16
  23. #define ICN8505_REG_POWER 0x0004
  24. #define ICN8505_REG_TOUCHDATA 0x1000
  25. #define ICN8505_REG_CONFIGDATA 0x8000
  26. /* ICN8505_REG_POWER commands */
  27. #define ICN8505_POWER_ACTIVE 0x00
  28. #define ICN8505_POWER_MONITOR 0x01
  29. #define ICN8505_POWER_HIBERNATE 0x02
  30. /*
  31. * The Android driver uses these to turn on/off the charger filter, but the
  32. * filter is way too aggressive making e.g. onscreen keyboards unusable.
  33. */
  34. #define ICN8505_POWER_ENA_CHARGER_MODE 0x55
  35. #define ICN8505_POWER_DIS_CHARGER_MODE 0x66
  36. #define ICN8505_MAX_TOUCHES 10
  37. /* Programming mode defines */
  38. #define ICN8505_PROG_I2C_ADDR 0x30
  39. #define ICN8505_PROG_REG_ADDR_WIDTH 24
  40. #define MAX_FW_UPLOAD_TRIES 3
  41. struct icn8505_touch {
  42. u8 slot;
  43. u8 x[2];
  44. u8 y[2];
  45. u8 pressure; /* Seems more like finger width then pressure really */
  46. u8 event;
  47. /* The difference between 2 and 3 is unclear */
  48. #define ICN8505_EVENT_NO_DATA 1 /* No finger seen yet since wakeup */
  49. #define ICN8505_EVENT_UPDATE1 2 /* New or updated coordinates */
  50. #define ICN8505_EVENT_UPDATE2 3 /* New or updated coordinates */
  51. #define ICN8505_EVENT_END 4 /* Finger lifted */
  52. } __packed;
  53. struct icn8505_touch_data {
  54. u8 softbutton;
  55. u8 touch_count;
  56. struct icn8505_touch touches[ICN8505_MAX_TOUCHES];
  57. } __packed;
  58. struct icn8505_data {
  59. struct i2c_client *client;
  60. struct input_dev *input;
  61. struct gpio_desc *wake_gpio;
  62. struct touchscreen_properties prop;
  63. char firmware_name[32];
  64. };
  65. static int icn8505_read_xfer(struct i2c_client *client, u16 i2c_addr,
  66. int reg_addr, int reg_addr_width,
  67. void *data, int len, bool silent)
  68. {
  69. u8 buf[3];
  70. int i, ret;
  71. struct i2c_msg msg[2] = {
  72. {
  73. .addr = i2c_addr,
  74. .buf = buf,
  75. .len = reg_addr_width / 8,
  76. },
  77. {
  78. .addr = i2c_addr,
  79. .flags = I2C_M_RD,
  80. .buf = data,
  81. .len = len,
  82. }
  83. };
  84. for (i = 0; i < (reg_addr_width / 8); i++)
  85. buf[i] = (reg_addr >> (reg_addr_width - (i + 1) * 8)) & 0xff;
  86. ret = i2c_transfer(client->adapter, msg, 2);
  87. if (ret != ARRAY_SIZE(msg)) {
  88. if (ret >= 0)
  89. ret = -EIO;
  90. if (!silent)
  91. dev_err(&client->dev,
  92. "Error reading addr %#x reg %#x: %d\n",
  93. i2c_addr, reg_addr, ret);
  94. return ret;
  95. }
  96. return 0;
  97. }
  98. static int icn8505_write_xfer(struct i2c_client *client, u16 i2c_addr,
  99. int reg_addr, int reg_addr_width,
  100. const void *data, int len, bool silent)
  101. {
  102. u8 buf[3 + 32]; /* 3 bytes for 24 bit reg-addr + 32 bytes max len */
  103. int i, ret;
  104. struct i2c_msg msg = {
  105. .addr = i2c_addr,
  106. .buf = buf,
  107. .len = reg_addr_width / 8 + len,
  108. };
  109. if (WARN_ON(len > 32))
  110. return -EINVAL;
  111. for (i = 0; i < (reg_addr_width / 8); i++)
  112. buf[i] = (reg_addr >> (reg_addr_width - (i + 1) * 8)) & 0xff;
  113. memcpy(buf + reg_addr_width / 8, data, len);
  114. ret = i2c_transfer(client->adapter, &msg, 1);
  115. if (ret != 1) {
  116. if (ret >= 0)
  117. ret = -EIO;
  118. if (!silent)
  119. dev_err(&client->dev,
  120. "Error writing addr %#x reg %#x: %d\n",
  121. i2c_addr, reg_addr, ret);
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. static int icn8505_read_data(struct icn8505_data *icn8505, int reg,
  127. void *buf, int len)
  128. {
  129. return icn8505_read_xfer(icn8505->client, icn8505->client->addr, reg,
  130. ICN8505_REG_ADDR_WIDTH, buf, len, false);
  131. }
  132. static int icn8505_read_reg_silent(struct icn8505_data *icn8505, int reg)
  133. {
  134. u8 buf;
  135. int error;
  136. error = icn8505_read_xfer(icn8505->client, icn8505->client->addr, reg,
  137. ICN8505_REG_ADDR_WIDTH, &buf, 1, true);
  138. if (error)
  139. return error;
  140. return buf;
  141. }
  142. static int icn8505_write_reg(struct icn8505_data *icn8505, int reg, u8 val)
  143. {
  144. return icn8505_write_xfer(icn8505->client, icn8505->client->addr, reg,
  145. ICN8505_REG_ADDR_WIDTH, &val, 1, false);
  146. }
  147. static int icn8505_read_prog_data(struct icn8505_data *icn8505, int reg,
  148. void *buf, int len)
  149. {
  150. return icn8505_read_xfer(icn8505->client, ICN8505_PROG_I2C_ADDR, reg,
  151. ICN8505_PROG_REG_ADDR_WIDTH, buf, len, false);
  152. }
  153. static int icn8505_write_prog_data(struct icn8505_data *icn8505, int reg,
  154. const void *buf, int len)
  155. {
  156. return icn8505_write_xfer(icn8505->client, ICN8505_PROG_I2C_ADDR, reg,
  157. ICN8505_PROG_REG_ADDR_WIDTH, buf, len, false);
  158. }
  159. static int icn8505_write_prog_reg(struct icn8505_data *icn8505, int reg, u8 val)
  160. {
  161. return icn8505_write_xfer(icn8505->client, ICN8505_PROG_I2C_ADDR, reg,
  162. ICN8505_PROG_REG_ADDR_WIDTH, &val, 1, false);
  163. }
  164. /*
  165. * Note this function uses a number of magic register addresses and values,
  166. * there are deliberately no defines for these because the algorithm is taken
  167. * from the icn85xx Android driver and I do not want to make up possibly wrong
  168. * names for the addresses and/or values.
  169. */
  170. static int icn8505_try_fw_upload(struct icn8505_data *icn8505,
  171. const struct firmware *fw)
  172. {
  173. struct device *dev = &icn8505->client->dev;
  174. size_t offset, count;
  175. int error;
  176. u8 buf[4];
  177. u32 crc;
  178. /* Put the controller in programming mode */
  179. error = icn8505_write_prog_reg(icn8505, 0xcc3355, 0x5a);
  180. if (error)
  181. return error;
  182. usleep_range(2000, 5000);
  183. error = icn8505_write_prog_reg(icn8505, 0x040400, 0x01);
  184. if (error)
  185. return error;
  186. usleep_range(2000, 5000);
  187. error = icn8505_read_prog_data(icn8505, 0x040002, buf, 1);
  188. if (error)
  189. return error;
  190. if (buf[0] != 0x85) {
  191. dev_err(dev, "Failed to enter programming mode\n");
  192. return -ENODEV;
  193. }
  194. usleep_range(1000, 5000);
  195. /* Enable CRC mode */
  196. error = icn8505_write_prog_reg(icn8505, 0x40028, 1);
  197. if (error)
  198. return error;
  199. /* Send the firmware to SRAM */
  200. for (offset = 0; offset < fw->size; offset += count) {
  201. count = min_t(size_t, fw->size - offset, 32);
  202. error = icn8505_write_prog_data(icn8505, offset,
  203. fw->data + offset, count);
  204. if (error)
  205. return error;
  206. }
  207. /* Disable CRC mode */
  208. error = icn8505_write_prog_reg(icn8505, 0x40028, 0);
  209. if (error)
  210. return error;
  211. /* Get and check length and CRC */
  212. error = icn8505_read_prog_data(icn8505, 0x40034, buf, 2);
  213. if (error)
  214. return error;
  215. if (get_unaligned_le16(buf) != fw->size) {
  216. dev_warn(dev, "Length mismatch after uploading fw\n");
  217. return -EIO;
  218. }
  219. error = icn8505_read_prog_data(icn8505, 0x4002c, buf, 4);
  220. if (error)
  221. return error;
  222. crc = crc32_be(0, fw->data, fw->size);
  223. if (get_unaligned_le32(buf) != crc) {
  224. dev_warn(dev, "CRC mismatch after uploading fw\n");
  225. return -EIO;
  226. }
  227. /* Boot controller from SRAM */
  228. error = icn8505_write_prog_reg(icn8505, 0x40400, 0x03);
  229. if (error)
  230. return error;
  231. usleep_range(2000, 5000);
  232. return 0;
  233. }
  234. static int icn8505_upload_fw(struct icn8505_data *icn8505)
  235. {
  236. struct device *dev = &icn8505->client->dev;
  237. const struct firmware *fw;
  238. int i, error;
  239. /*
  240. * Always load the firmware, even if we don't need it at boot, we
  241. * we may need it at resume. Having loaded it once will make the
  242. * firmware class code cache it at suspend/resume.
  243. */
  244. error = firmware_request_platform(&fw, icn8505->firmware_name, dev);
  245. if (error) {
  246. dev_err(dev, "Firmware request error %d\n", error);
  247. return error;
  248. }
  249. /* Check if the controller is not already up and running */
  250. if (icn8505_read_reg_silent(icn8505, 0x000a) == 0x85)
  251. goto success;
  252. for (i = 1; i <= MAX_FW_UPLOAD_TRIES; i++) {
  253. error = icn8505_try_fw_upload(icn8505, fw);
  254. if (!error)
  255. goto success;
  256. dev_err(dev, "Failed to upload firmware: %d (attempt %d/%d)\n",
  257. error, i, MAX_FW_UPLOAD_TRIES);
  258. usleep_range(2000, 5000);
  259. }
  260. success:
  261. release_firmware(fw);
  262. return error;
  263. }
  264. static bool icn8505_touch_active(u8 event)
  265. {
  266. return event == ICN8505_EVENT_UPDATE1 ||
  267. event == ICN8505_EVENT_UPDATE2;
  268. }
  269. static irqreturn_t icn8505_irq(int irq, void *dev_id)
  270. {
  271. struct icn8505_data *icn8505 = dev_id;
  272. struct device *dev = &icn8505->client->dev;
  273. struct icn8505_touch_data touch_data;
  274. int i, error;
  275. error = icn8505_read_data(icn8505, ICN8505_REG_TOUCHDATA,
  276. &touch_data, sizeof(touch_data));
  277. if (error) {
  278. dev_err(dev, "Error reading touch data: %d\n", error);
  279. return IRQ_HANDLED;
  280. }
  281. if (touch_data.touch_count > ICN8505_MAX_TOUCHES) {
  282. dev_warn(dev, "Too many touches %d > %d\n",
  283. touch_data.touch_count, ICN8505_MAX_TOUCHES);
  284. touch_data.touch_count = ICN8505_MAX_TOUCHES;
  285. }
  286. for (i = 0; i < touch_data.touch_count; i++) {
  287. struct icn8505_touch *touch = &touch_data.touches[i];
  288. bool act = icn8505_touch_active(touch->event);
  289. input_mt_slot(icn8505->input, touch->slot);
  290. input_mt_report_slot_state(icn8505->input, MT_TOOL_FINGER, act);
  291. if (!act)
  292. continue;
  293. touchscreen_report_pos(icn8505->input, &icn8505->prop,
  294. get_unaligned_le16(touch->x),
  295. get_unaligned_le16(touch->y),
  296. true);
  297. }
  298. input_mt_sync_frame(icn8505->input);
  299. input_report_key(icn8505->input, KEY_LEFTMETA,
  300. touch_data.softbutton == 1);
  301. input_sync(icn8505->input);
  302. return IRQ_HANDLED;
  303. }
  304. static int icn8505_probe_acpi(struct icn8505_data *icn8505, struct device *dev)
  305. {
  306. const char *subsys;
  307. int error;
  308. subsys = acpi_get_subsystem_id(ACPI_HANDLE(dev));
  309. error = PTR_ERR_OR_ZERO(subsys);
  310. if (error == -ENODATA)
  311. subsys = "unknown";
  312. else if (error)
  313. return error;
  314. snprintf(icn8505->firmware_name, sizeof(icn8505->firmware_name),
  315. "chipone/icn8505-%s.fw", subsys);
  316. kfree_const(subsys);
  317. return 0;
  318. }
  319. static int icn8505_probe(struct i2c_client *client)
  320. {
  321. struct device *dev = &client->dev;
  322. struct icn8505_data *icn8505;
  323. struct input_dev *input;
  324. __le16 resolution[2];
  325. int error;
  326. if (!client->irq) {
  327. dev_err(dev, "No irq specified\n");
  328. return -EINVAL;
  329. }
  330. icn8505 = devm_kzalloc(dev, sizeof(*icn8505), GFP_KERNEL);
  331. if (!icn8505)
  332. return -ENOMEM;
  333. input = devm_input_allocate_device(dev);
  334. if (!input)
  335. return -ENOMEM;
  336. input->name = client->name;
  337. input->id.bustype = BUS_I2C;
  338. input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
  339. input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
  340. input_set_capability(input, EV_KEY, KEY_LEFTMETA);
  341. icn8505->client = client;
  342. icn8505->input = input;
  343. input_set_drvdata(input, icn8505);
  344. error = icn8505_probe_acpi(icn8505, dev);
  345. if (error)
  346. return error;
  347. error = icn8505_upload_fw(icn8505);
  348. if (error)
  349. return error;
  350. error = icn8505_read_data(icn8505, ICN8505_REG_CONFIGDATA,
  351. resolution, sizeof(resolution));
  352. if (error) {
  353. dev_err(dev, "Error reading resolution: %d\n", error);
  354. return error;
  355. }
  356. input_set_abs_params(input, ABS_MT_POSITION_X, 0,
  357. le16_to_cpu(resolution[0]) - 1, 0, 0);
  358. input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
  359. le16_to_cpu(resolution[1]) - 1, 0, 0);
  360. touchscreen_parse_properties(input, true, &icn8505->prop);
  361. if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
  362. !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
  363. dev_err(dev, "Error touchscreen-size-x and/or -y missing\n");
  364. return -EINVAL;
  365. }
  366. error = input_mt_init_slots(input, ICN8505_MAX_TOUCHES,
  367. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  368. if (error)
  369. return error;
  370. error = devm_request_threaded_irq(dev, client->irq, NULL, icn8505_irq,
  371. IRQF_ONESHOT, client->name, icn8505);
  372. if (error) {
  373. dev_err(dev, "Error requesting irq: %d\n", error);
  374. return error;
  375. }
  376. error = input_register_device(input);
  377. if (error)
  378. return error;
  379. i2c_set_clientdata(client, icn8505);
  380. return 0;
  381. }
  382. static int __maybe_unused icn8505_suspend(struct device *dev)
  383. {
  384. struct icn8505_data *icn8505 = i2c_get_clientdata(to_i2c_client(dev));
  385. disable_irq(icn8505->client->irq);
  386. icn8505_write_reg(icn8505, ICN8505_REG_POWER, ICN8505_POWER_HIBERNATE);
  387. return 0;
  388. }
  389. static int __maybe_unused icn8505_resume(struct device *dev)
  390. {
  391. struct icn8505_data *icn8505 = i2c_get_clientdata(to_i2c_client(dev));
  392. int error;
  393. error = icn8505_upload_fw(icn8505);
  394. if (error)
  395. return error;
  396. enable_irq(icn8505->client->irq);
  397. return 0;
  398. }
  399. static SIMPLE_DEV_PM_OPS(icn8505_pm_ops, icn8505_suspend, icn8505_resume);
  400. static const struct acpi_device_id icn8505_acpi_match[] = {
  401. { "CHPN0001" },
  402. { }
  403. };
  404. MODULE_DEVICE_TABLE(acpi, icn8505_acpi_match);
  405. static struct i2c_driver icn8505_driver = {
  406. .driver = {
  407. .name = "chipone_icn8505",
  408. .pm = &icn8505_pm_ops,
  409. .acpi_match_table = icn8505_acpi_match,
  410. },
  411. .probe_new = icn8505_probe,
  412. };
  413. module_i2c_driver(icn8505_driver);
  414. MODULE_DESCRIPTION("ChipOne icn8505 I2C Touchscreen Driver");
  415. MODULE_AUTHOR("Hans de Goede <[email protected]>");
  416. MODULE_LICENSE("GPL");