ektf2127.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for ELAN eKTF2127 i2c touchscreen controller
  4. *
  5. * For this driver the layout of the Chipone icn8318 i2c
  6. * touchscreencontroller is used.
  7. *
  8. * Author:
  9. * Michel Verlaan <[email protected]>
  10. * Siebren Vroegindeweij <[email protected]>
  11. *
  12. * Original chipone_icn8318 driver:
  13. * Hans de Goede <[email protected]>
  14. */
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/input.h>
  19. #include <linux/input/mt.h>
  20. #include <linux/input/touchscreen.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/delay.h>
  24. /* Packet header defines (first byte of data send / received) */
  25. #define EKTF2127_NOISE 0x40
  26. #define EKTF2127_RESPONSE 0x52
  27. #define EKTF2127_REQUEST 0x53
  28. #define EKTF2127_HELLO 0x55
  29. #define EKTF2127_REPORT2 0x5a
  30. #define EKTF2127_REPORT 0x5d
  31. #define EKTF2127_CALIB_DONE 0x66
  32. /* Register defines (second byte of data send / received) */
  33. #define EKTF2127_ENV_NOISY 0x41
  34. #define EKTF2127_HEIGHT 0x60
  35. #define EKTF2127_WIDTH 0x63
  36. /* 2 bytes header + 5 * 3 bytes coordinates + 3 bytes pressure info + footer */
  37. #define EKTF2127_TOUCH_REPORT_SIZE 21
  38. #define EKTF2127_MAX_TOUCHES 5
  39. struct ektf2127_ts {
  40. struct i2c_client *client;
  41. struct input_dev *input;
  42. struct gpio_desc *power_gpios;
  43. struct touchscreen_properties prop;
  44. };
  45. static void ektf2127_parse_coordinates(const u8 *buf, unsigned int touch_count,
  46. struct input_mt_pos *touches)
  47. {
  48. int index = 0;
  49. int i;
  50. for (i = 0; i < touch_count; i++) {
  51. index = 2 + i * 3;
  52. touches[i].x = (buf[index] & 0x0f);
  53. touches[i].x <<= 8;
  54. touches[i].x |= buf[index + 2];
  55. touches[i].y = (buf[index] & 0xf0);
  56. touches[i].y <<= 4;
  57. touches[i].y |= buf[index + 1];
  58. }
  59. }
  60. static void ektf2127_report_event(struct ektf2127_ts *ts, const u8 *buf)
  61. {
  62. struct input_mt_pos touches[EKTF2127_MAX_TOUCHES];
  63. int slots[EKTF2127_MAX_TOUCHES];
  64. unsigned int touch_count, i;
  65. touch_count = buf[1] & 0x07;
  66. if (touch_count > EKTF2127_MAX_TOUCHES) {
  67. dev_err(&ts->client->dev,
  68. "Too many touches %d > %d\n",
  69. touch_count, EKTF2127_MAX_TOUCHES);
  70. touch_count = EKTF2127_MAX_TOUCHES;
  71. }
  72. ektf2127_parse_coordinates(buf, touch_count, touches);
  73. input_mt_assign_slots(ts->input, slots, touches,
  74. touch_count, 0);
  75. for (i = 0; i < touch_count; i++) {
  76. input_mt_slot(ts->input, slots[i]);
  77. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
  78. touchscreen_report_pos(ts->input, &ts->prop,
  79. touches[i].x, touches[i].y, true);
  80. }
  81. input_mt_sync_frame(ts->input);
  82. input_sync(ts->input);
  83. }
  84. static void ektf2127_report2_contact(struct ektf2127_ts *ts, int slot,
  85. const u8 *buf, bool active)
  86. {
  87. input_mt_slot(ts->input, slot);
  88. input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, active);
  89. if (active) {
  90. int x = (buf[0] & 0xf0) << 4 | buf[1];
  91. int y = (buf[0] & 0x0f) << 8 | buf[2];
  92. touchscreen_report_pos(ts->input, &ts->prop, x, y, true);
  93. }
  94. }
  95. static void ektf2127_report2_event(struct ektf2127_ts *ts, const u8 *buf)
  96. {
  97. ektf2127_report2_contact(ts, 0, &buf[1], !!(buf[7] & 2));
  98. ektf2127_report2_contact(ts, 1, &buf[4], !!(buf[7] & 4));
  99. input_mt_sync_frame(ts->input);
  100. input_sync(ts->input);
  101. }
  102. static irqreturn_t ektf2127_irq(int irq, void *dev_id)
  103. {
  104. struct ektf2127_ts *ts = dev_id;
  105. struct device *dev = &ts->client->dev;
  106. char buf[EKTF2127_TOUCH_REPORT_SIZE];
  107. int ret;
  108. ret = i2c_master_recv(ts->client, buf, EKTF2127_TOUCH_REPORT_SIZE);
  109. if (ret != EKTF2127_TOUCH_REPORT_SIZE) {
  110. dev_err(dev, "Error reading touch data: %d\n", ret);
  111. goto out;
  112. }
  113. switch (buf[0]) {
  114. case EKTF2127_REPORT:
  115. ektf2127_report_event(ts, buf);
  116. break;
  117. case EKTF2127_REPORT2:
  118. ektf2127_report2_event(ts, buf);
  119. break;
  120. case EKTF2127_NOISE:
  121. if (buf[1] == EKTF2127_ENV_NOISY)
  122. dev_dbg(dev, "Environment is electrically noisy\n");
  123. break;
  124. case EKTF2127_HELLO:
  125. case EKTF2127_CALIB_DONE:
  126. break;
  127. default:
  128. dev_err(dev, "Unexpected packet header byte %#02x\n", buf[0]);
  129. break;
  130. }
  131. out:
  132. return IRQ_HANDLED;
  133. }
  134. static int ektf2127_start(struct input_dev *dev)
  135. {
  136. struct ektf2127_ts *ts = input_get_drvdata(dev);
  137. enable_irq(ts->client->irq);
  138. gpiod_set_value_cansleep(ts->power_gpios, 1);
  139. return 0;
  140. }
  141. static void ektf2127_stop(struct input_dev *dev)
  142. {
  143. struct ektf2127_ts *ts = input_get_drvdata(dev);
  144. disable_irq(ts->client->irq);
  145. gpiod_set_value_cansleep(ts->power_gpios, 0);
  146. }
  147. static int __maybe_unused ektf2127_suspend(struct device *dev)
  148. {
  149. struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
  150. mutex_lock(&ts->input->mutex);
  151. if (input_device_enabled(ts->input))
  152. ektf2127_stop(ts->input);
  153. mutex_unlock(&ts->input->mutex);
  154. return 0;
  155. }
  156. static int __maybe_unused ektf2127_resume(struct device *dev)
  157. {
  158. struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
  159. mutex_lock(&ts->input->mutex);
  160. if (input_device_enabled(ts->input))
  161. ektf2127_start(ts->input);
  162. mutex_unlock(&ts->input->mutex);
  163. return 0;
  164. }
  165. static SIMPLE_DEV_PM_OPS(ektf2127_pm_ops, ektf2127_suspend,
  166. ektf2127_resume);
  167. static int ektf2127_query_dimension(struct i2c_client *client, bool width)
  168. {
  169. struct device *dev = &client->dev;
  170. const char *what = width ? "width" : "height";
  171. u8 what_code = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
  172. u8 buf[4];
  173. int ret;
  174. int error;
  175. /* Request dimension */
  176. buf[0] = EKTF2127_REQUEST;
  177. buf[1] = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
  178. buf[2] = 0x00;
  179. buf[3] = 0x00;
  180. ret = i2c_master_send(client, buf, sizeof(buf));
  181. if (ret != sizeof(buf)) {
  182. error = ret < 0 ? ret : -EIO;
  183. dev_err(dev, "Failed to request %s: %d\n", what, error);
  184. return error;
  185. }
  186. msleep(20);
  187. /* Read response */
  188. ret = i2c_master_recv(client, buf, sizeof(buf));
  189. if (ret != sizeof(buf)) {
  190. error = ret < 0 ? ret : -EIO;
  191. dev_err(dev, "Failed to receive %s data: %d\n", what, error);
  192. return error;
  193. }
  194. if (buf[0] != EKTF2127_RESPONSE || buf[1] != what_code) {
  195. dev_err(dev, "Unexpected %s data: %#02x %#02x\n",
  196. what, buf[0], buf[1]);
  197. return -EIO;
  198. }
  199. return (((buf[3] & 0xf0) << 4) | buf[2]) - 1;
  200. }
  201. static int ektf2127_probe(struct i2c_client *client,
  202. const struct i2c_device_id *id)
  203. {
  204. struct device *dev = &client->dev;
  205. struct ektf2127_ts *ts;
  206. struct input_dev *input;
  207. u8 buf[4];
  208. int max_x, max_y;
  209. int error;
  210. if (!client->irq) {
  211. dev_err(dev, "Error no irq specified\n");
  212. return -EINVAL;
  213. }
  214. ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
  215. if (!ts)
  216. return -ENOMEM;
  217. /* This requests the gpio *and* turns on the touchscreen controller */
  218. ts->power_gpios = devm_gpiod_get(dev, "power", GPIOD_OUT_HIGH);
  219. if (IS_ERR(ts->power_gpios)) {
  220. error = PTR_ERR(ts->power_gpios);
  221. if (error != -EPROBE_DEFER)
  222. dev_err(dev, "Error getting power gpio: %d\n", error);
  223. return error;
  224. }
  225. input = devm_input_allocate_device(dev);
  226. if (!input)
  227. return -ENOMEM;
  228. input->name = client->name;
  229. input->id.bustype = BUS_I2C;
  230. input->open = ektf2127_start;
  231. input->close = ektf2127_stop;
  232. ts->client = client;
  233. /* Read hello (ignore result, depends on initial power state) */
  234. msleep(20);
  235. i2c_master_recv(ts->client, buf, sizeof(buf));
  236. /* Read resolution from chip */
  237. max_x = ektf2127_query_dimension(client, true);
  238. if (max_x < 0)
  239. return max_x;
  240. max_y = ektf2127_query_dimension(client, false);
  241. if (max_y < 0)
  242. return max_y;
  243. input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
  244. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
  245. touchscreen_parse_properties(input, true, &ts->prop);
  246. error = input_mt_init_slots(input, EKTF2127_MAX_TOUCHES,
  247. INPUT_MT_DIRECT |
  248. INPUT_MT_DROP_UNUSED |
  249. INPUT_MT_TRACK);
  250. if (error)
  251. return error;
  252. ts->input = input;
  253. input_set_drvdata(input, ts);
  254. error = devm_request_threaded_irq(dev, client->irq,
  255. NULL, ektf2127_irq,
  256. IRQF_ONESHOT, client->name, ts);
  257. if (error) {
  258. dev_err(dev, "Error requesting irq: %d\n", error);
  259. return error;
  260. }
  261. /* Stop device till opened */
  262. ektf2127_stop(ts->input);
  263. error = input_register_device(input);
  264. if (error)
  265. return error;
  266. i2c_set_clientdata(client, ts);
  267. return 0;
  268. }
  269. #ifdef CONFIG_OF
  270. static const struct of_device_id ektf2127_of_match[] = {
  271. { .compatible = "elan,ektf2127" },
  272. { .compatible = "elan,ektf2132" },
  273. {}
  274. };
  275. MODULE_DEVICE_TABLE(of, ektf2127_of_match);
  276. #endif
  277. static const struct i2c_device_id ektf2127_i2c_id[] = {
  278. { "ektf2127", 0 },
  279. { "ektf2132", 0 },
  280. {}
  281. };
  282. MODULE_DEVICE_TABLE(i2c, ektf2127_i2c_id);
  283. static struct i2c_driver ektf2127_driver = {
  284. .driver = {
  285. .name = "elan_ektf2127",
  286. .pm = &ektf2127_pm_ops,
  287. .of_match_table = of_match_ptr(ektf2127_of_match),
  288. },
  289. .probe = ektf2127_probe,
  290. .id_table = ektf2127_i2c_id,
  291. };
  292. module_i2c_driver(ektf2127_driver);
  293. MODULE_DESCRIPTION("ELAN eKTF2127/eKTF2132 I2C Touchscreen Driver");
  294. MODULE_AUTHOR("Michel Verlaan, Siebren Vroegindeweij");
  295. MODULE_LICENSE("GPL");