imagis.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/bitfield.h>
  3. #include <linux/bits.h>
  4. #include <linux/delay.h>
  5. #include <linux/i2c.h>
  6. #include <linux/input.h>
  7. #include <linux/input/mt.h>
  8. #include <linux/input/touchscreen.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/property.h>
  12. #include <linux/regulator/consumer.h>
  13. #define IST30XX_REG_STATUS 0x20
  14. #define IST30XX_REG_CHIPID (0x40000000 | IST3038C_DIRECT_ACCESS)
  15. #define IST30XX_WHOAMI 0x30003000
  16. #define IST30XXA_WHOAMI 0x300a300a
  17. #define IST30XXB_WHOAMI 0x300b300b
  18. #define IST3038_WHOAMI 0x30383038
  19. #define IST3032C_WHOAMI 0x32c
  20. #define IST3038C_WHOAMI 0x38c
  21. #define IST3038H_WHOAMI 0x38d
  22. #define IST3038B_REG_CHIPID 0x30
  23. #define IST3038B_WHOAMI 0x30380b
  24. #define IST3038C_HIB_ACCESS (0x800B << 16)
  25. #define IST3038C_DIRECT_ACCESS BIT(31)
  26. #define IST3038C_REG_CHIPID (0x40001000 | IST3038C_DIRECT_ACCESS)
  27. #define IST3038C_REG_HIB_BASE 0x30000100
  28. #define IST3038C_REG_TOUCH_STATUS (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS)
  29. #define IST3038C_REG_TOUCH_COORD (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x8)
  30. #define IST3038C_REG_INTR_MESSAGE (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x4)
  31. #define IST3038C_CHIP_ON_DELAY_MS 60
  32. #define IST3038C_I2C_RETRY_COUNT 3
  33. #define IST3038C_MAX_FINGER_NUM 10
  34. #define IST3038C_X_MASK GENMASK(23, 12)
  35. #define IST3038C_Y_MASK GENMASK(11, 0)
  36. #define IST3038C_AREA_MASK GENMASK(27, 24)
  37. #define IST3038C_FINGER_COUNT_MASK GENMASK(15, 12)
  38. #define IST3038C_FINGER_STATUS_MASK GENMASK(9, 0)
  39. #define IST3032C_KEY_STATUS_MASK GENMASK(20, 16)
  40. struct imagis_properties {
  41. unsigned int interrupt_msg_cmd;
  42. unsigned int touch_coord_cmd;
  43. unsigned int whoami_cmd;
  44. unsigned int whoami_val;
  45. bool protocol_b;
  46. bool touch_keys_supported;
  47. };
  48. struct imagis_ts {
  49. struct i2c_client *client;
  50. const struct imagis_properties *tdata;
  51. struct input_dev *input_dev;
  52. struct touchscreen_properties prop;
  53. struct regulator_bulk_data supplies[2];
  54. u32 keycodes[5];
  55. int num_keycodes;
  56. };
  57. static int imagis_i2c_read_reg(struct imagis_ts *ts,
  58. unsigned int reg, u32 *data)
  59. {
  60. __be32 ret_be;
  61. __be32 reg_be = cpu_to_be32(reg);
  62. struct i2c_msg msg[] = {
  63. {
  64. .addr = ts->client->addr,
  65. .flags = 0,
  66. .buf = (unsigned char *)&reg_be,
  67. .len = sizeof(reg_be),
  68. }, {
  69. .addr = ts->client->addr,
  70. .flags = I2C_M_RD,
  71. .buf = (unsigned char *)&ret_be,
  72. .len = sizeof(ret_be),
  73. },
  74. };
  75. int ret, error;
  76. int retry = IST3038C_I2C_RETRY_COUNT;
  77. /* Retry in case the controller fails to respond */
  78. do {
  79. ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
  80. if (ret == ARRAY_SIZE(msg)) {
  81. *data = be32_to_cpu(ret_be);
  82. return 0;
  83. }
  84. error = ret < 0 ? ret : -EIO;
  85. dev_err(&ts->client->dev,
  86. "%s - i2c_transfer failed: %d (%d)\n",
  87. __func__, error, ret);
  88. } while (--retry);
  89. return error;
  90. }
  91. static irqreturn_t imagis_interrupt(int irq, void *dev_id)
  92. {
  93. struct imagis_ts *ts = dev_id;
  94. u32 intr_message, finger_status;
  95. unsigned int finger_count, finger_pressed, key_pressed;
  96. int i;
  97. int error;
  98. error = imagis_i2c_read_reg(ts, ts->tdata->interrupt_msg_cmd, &intr_message);
  99. if (error) {
  100. dev_err(&ts->client->dev,
  101. "failed to read the interrupt message: %d\n", error);
  102. goto out;
  103. }
  104. finger_count = FIELD_GET(IST3038C_FINGER_COUNT_MASK, intr_message);
  105. if (finger_count > IST3038C_MAX_FINGER_NUM) {
  106. dev_err(&ts->client->dev,
  107. "finger count %d is more than maximum supported\n",
  108. finger_count);
  109. goto out;
  110. }
  111. finger_pressed = FIELD_GET(IST3038C_FINGER_STATUS_MASK, intr_message);
  112. for (i = 0; i < finger_count; i++) {
  113. if (ts->tdata->protocol_b)
  114. error = imagis_i2c_read_reg(ts,
  115. ts->tdata->touch_coord_cmd + (i * 4),
  116. &finger_status);
  117. else
  118. error = imagis_i2c_read_reg(ts,
  119. ts->tdata->touch_coord_cmd, &finger_status);
  120. if (error) {
  121. dev_err(&ts->client->dev,
  122. "failed to read coordinates for finger %d: %d\n",
  123. i, error);
  124. goto out;
  125. }
  126. input_mt_slot(ts->input_dev, i);
  127. input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER,
  128. finger_pressed & BIT(i));
  129. touchscreen_report_pos(ts->input_dev, &ts->prop,
  130. FIELD_GET(IST3038C_X_MASK, finger_status),
  131. FIELD_GET(IST3038C_Y_MASK, finger_status),
  132. true);
  133. input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR,
  134. FIELD_GET(IST3038C_AREA_MASK, finger_status));
  135. }
  136. key_pressed = FIELD_GET(IST3032C_KEY_STATUS_MASK, intr_message);
  137. for (int i = 0; i < ts->num_keycodes; i++)
  138. input_report_key(ts->input_dev, ts->keycodes[i],
  139. key_pressed & BIT(i));
  140. input_mt_sync_frame(ts->input_dev);
  141. input_sync(ts->input_dev);
  142. out:
  143. return IRQ_HANDLED;
  144. }
  145. static void imagis_power_off(void *_ts)
  146. {
  147. struct imagis_ts *ts = _ts;
  148. regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
  149. }
  150. static int imagis_power_on(struct imagis_ts *ts)
  151. {
  152. int error;
  153. error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
  154. if (error)
  155. return error;
  156. msleep(IST3038C_CHIP_ON_DELAY_MS);
  157. return 0;
  158. }
  159. static int imagis_start(struct imagis_ts *ts)
  160. {
  161. int error;
  162. error = imagis_power_on(ts);
  163. if (error)
  164. return error;
  165. enable_irq(ts->client->irq);
  166. return 0;
  167. }
  168. static int imagis_stop(struct imagis_ts *ts)
  169. {
  170. disable_irq(ts->client->irq);
  171. imagis_power_off(ts);
  172. return 0;
  173. }
  174. static int imagis_input_open(struct input_dev *dev)
  175. {
  176. struct imagis_ts *ts = input_get_drvdata(dev);
  177. return imagis_start(ts);
  178. }
  179. static void imagis_input_close(struct input_dev *dev)
  180. {
  181. struct imagis_ts *ts = input_get_drvdata(dev);
  182. imagis_stop(ts);
  183. }
  184. static int imagis_init_input_dev(struct imagis_ts *ts)
  185. {
  186. struct input_dev *input_dev;
  187. int error;
  188. input_dev = devm_input_allocate_device(&ts->client->dev);
  189. if (!input_dev)
  190. return -ENOMEM;
  191. ts->input_dev = input_dev;
  192. input_dev->name = "Imagis capacitive touchscreen";
  193. input_dev->phys = "input/ts";
  194. input_dev->id.bustype = BUS_I2C;
  195. input_dev->open = imagis_input_open;
  196. input_dev->close = imagis_input_close;
  197. input_set_drvdata(input_dev, ts);
  198. input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
  199. input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
  200. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 16, 0, 0);
  201. if (ts->tdata->touch_keys_supported) {
  202. ts->num_keycodes = of_property_read_variable_u32_array(
  203. ts->client->dev.of_node, "linux,keycodes",
  204. ts->keycodes, 0, ARRAY_SIZE(ts->keycodes));
  205. if (ts->num_keycodes <= 0) {
  206. ts->keycodes[0] = KEY_APPSELECT;
  207. ts->keycodes[1] = KEY_BACK;
  208. ts->num_keycodes = 2;
  209. }
  210. input_dev->keycodemax = ts->num_keycodes;
  211. input_dev->keycodesize = sizeof(ts->keycodes[0]);
  212. input_dev->keycode = ts->keycodes;
  213. }
  214. for (int i = 0; i < ts->num_keycodes; i++)
  215. input_set_capability(input_dev, EV_KEY, ts->keycodes[i]);
  216. touchscreen_parse_properties(input_dev, true, &ts->prop);
  217. if (!ts->prop.max_x || !ts->prop.max_y) {
  218. dev_err(&ts->client->dev,
  219. "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
  220. return -EINVAL;
  221. }
  222. error = input_mt_init_slots(input_dev,
  223. IST3038C_MAX_FINGER_NUM,
  224. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  225. if (error) {
  226. dev_err(&ts->client->dev,
  227. "Failed to initialize MT slots: %d", error);
  228. return error;
  229. }
  230. error = input_register_device(input_dev);
  231. if (error) {
  232. dev_err(&ts->client->dev,
  233. "Failed to register input device: %d", error);
  234. return error;
  235. }
  236. return 0;
  237. }
  238. static int imagis_init_regulators(struct imagis_ts *ts)
  239. {
  240. struct i2c_client *client = ts->client;
  241. ts->supplies[0].supply = "vdd";
  242. ts->supplies[1].supply = "vddio";
  243. return devm_regulator_bulk_get(&client->dev,
  244. ARRAY_SIZE(ts->supplies),
  245. ts->supplies);
  246. }
  247. static int imagis_probe(struct i2c_client *i2c)
  248. {
  249. struct device *dev = &i2c->dev;
  250. struct imagis_ts *ts;
  251. int chip_id, error;
  252. ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
  253. if (!ts)
  254. return -ENOMEM;
  255. ts->client = i2c;
  256. ts->tdata = device_get_match_data(dev);
  257. if (!ts->tdata) {
  258. dev_err(dev, "missing chip data\n");
  259. return -EINVAL;
  260. }
  261. error = imagis_init_regulators(ts);
  262. if (error) {
  263. dev_err(dev, "regulator init error: %d\n", error);
  264. return error;
  265. }
  266. error = imagis_power_on(ts);
  267. if (error) {
  268. dev_err(dev, "failed to enable regulators: %d\n", error);
  269. return error;
  270. }
  271. error = devm_add_action_or_reset(dev, imagis_power_off, ts);
  272. if (error) {
  273. dev_err(dev, "failed to install poweroff action: %d\n", error);
  274. return error;
  275. }
  276. error = imagis_i2c_read_reg(ts, ts->tdata->whoami_cmd, &chip_id);
  277. if (error) {
  278. dev_err(dev, "chip ID read failure: %d\n", error);
  279. return error;
  280. }
  281. if (chip_id != ts->tdata->whoami_val) {
  282. dev_err(dev, "unknown chip ID: 0x%x\n", chip_id);
  283. return -EINVAL;
  284. }
  285. error = devm_request_threaded_irq(dev, i2c->irq,
  286. NULL, imagis_interrupt,
  287. IRQF_ONESHOT | IRQF_NO_AUTOEN,
  288. "imagis-touchscreen", ts);
  289. if (error) {
  290. dev_err(dev, "IRQ %d allocation failure: %d\n",
  291. i2c->irq, error);
  292. return error;
  293. }
  294. error = imagis_init_input_dev(ts);
  295. if (error)
  296. return error;
  297. return 0;
  298. }
  299. static int imagis_suspend(struct device *dev)
  300. {
  301. struct i2c_client *client = to_i2c_client(dev);
  302. struct imagis_ts *ts = i2c_get_clientdata(client);
  303. int retval = 0;
  304. mutex_lock(&ts->input_dev->mutex);
  305. if (input_device_enabled(ts->input_dev))
  306. retval = imagis_stop(ts);
  307. mutex_unlock(&ts->input_dev->mutex);
  308. return retval;
  309. }
  310. static int imagis_resume(struct device *dev)
  311. {
  312. struct i2c_client *client = to_i2c_client(dev);
  313. struct imagis_ts *ts = i2c_get_clientdata(client);
  314. int retval = 0;
  315. mutex_lock(&ts->input_dev->mutex);
  316. if (input_device_enabled(ts->input_dev))
  317. retval = imagis_start(ts);
  318. mutex_unlock(&ts->input_dev->mutex);
  319. return retval;
  320. }
  321. static DEFINE_SIMPLE_DEV_PM_OPS(imagis_pm_ops, imagis_suspend, imagis_resume);
  322. #ifdef CONFIG_OF
  323. static const struct imagis_properties imagis_3032c_data = {
  324. .interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE,
  325. .touch_coord_cmd = IST3038C_REG_TOUCH_COORD,
  326. .whoami_cmd = IST3038C_REG_CHIPID,
  327. .whoami_val = IST3032C_WHOAMI,
  328. .touch_keys_supported = true,
  329. .protocol_b = true,
  330. };
  331. static const struct imagis_properties imagis_3038_data = {
  332. .interrupt_msg_cmd = IST30XX_REG_STATUS,
  333. .touch_coord_cmd = IST30XX_REG_STATUS,
  334. .whoami_cmd = IST30XX_REG_CHIPID,
  335. .whoami_val = IST3038_WHOAMI,
  336. .touch_keys_supported = true,
  337. };
  338. static const struct imagis_properties imagis_3038b_data = {
  339. .interrupt_msg_cmd = IST30XX_REG_STATUS,
  340. .touch_coord_cmd = IST30XX_REG_STATUS,
  341. .whoami_cmd = IST3038B_REG_CHIPID,
  342. .whoami_val = IST3038B_WHOAMI,
  343. };
  344. static const struct imagis_properties imagis_3038c_data = {
  345. .interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE,
  346. .touch_coord_cmd = IST3038C_REG_TOUCH_COORD,
  347. .whoami_cmd = IST3038C_REG_CHIPID,
  348. .whoami_val = IST3038C_WHOAMI,
  349. .protocol_b = true,
  350. };
  351. static const struct imagis_properties imagis_3038h_data = {
  352. .interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE,
  353. .touch_coord_cmd = IST3038C_REG_TOUCH_COORD,
  354. .whoami_cmd = IST3038C_REG_CHIPID,
  355. .whoami_val = IST3038H_WHOAMI,
  356. };
  357. static const struct of_device_id imagis_of_match[] = {
  358. { .compatible = "imagis,ist3032c", .data = &imagis_3032c_data },
  359. { .compatible = "imagis,ist3038", .data = &imagis_3038_data },
  360. { .compatible = "imagis,ist3038b", .data = &imagis_3038b_data },
  361. { .compatible = "imagis,ist3038c", .data = &imagis_3038c_data },
  362. { .compatible = "imagis,ist3038h", .data = &imagis_3038h_data },
  363. { },
  364. };
  365. MODULE_DEVICE_TABLE(of, imagis_of_match);
  366. #endif
  367. static struct i2c_driver imagis_ts_driver = {
  368. .driver = {
  369. .name = "imagis-touchscreen",
  370. .pm = pm_sleep_ptr(&imagis_pm_ops),
  371. .of_match_table = of_match_ptr(imagis_of_match),
  372. },
  373. .probe = imagis_probe,
  374. };
  375. module_i2c_driver(imagis_ts_driver);
  376. MODULE_DESCRIPTION("Imagis IST3038C Touchscreen Driver");
  377. MODULE_AUTHOR("Markuss Broks <markuss.broks@gmail.com>");
  378. MODULE_LICENSE("GPL");