imagis.c 11 KB

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