ektf2127.c 8.1 KB

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