chipone_icn8505.c 12 KB

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