exc3000.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Driver for I2C connected EETI EXC3000 multiple touch controller
  3. *
  4. * Copyright (C) 2017 Ahmet Inan <inan@distec.de>
  5. *
  6. * minimal implementation based on egalax_ts.c and egalax_i2c.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/device.h>
  14. #include <linux/i2c.h>
  15. #include <linux/input.h>
  16. #include <linux/input/mt.h>
  17. #include <linux/input/touchscreen.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/timer.h>
  22. #include <asm/unaligned.h>
  23. #define EXC3000_NUM_SLOTS 10
  24. #define EXC3000_SLOTS_PER_FRAME 5
  25. #define EXC3000_LEN_FRAME 66
  26. #define EXC3000_LEN_POINT 10
  27. #define EXC3000_MT_EVENT 6
  28. #define EXC3000_TIMEOUT_MS 100
  29. struct exc3000_data {
  30. struct i2c_client *client;
  31. struct input_dev *input;
  32. struct touchscreen_properties prop;
  33. struct timer_list timer;
  34. u8 buf[2 * EXC3000_LEN_FRAME];
  35. };
  36. static void exc3000_report_slots(struct input_dev *input,
  37. struct touchscreen_properties *prop,
  38. const u8 *buf, int num)
  39. {
  40. for (; num--; buf += EXC3000_LEN_POINT) {
  41. if (buf[0] & BIT(0)) {
  42. input_mt_slot(input, buf[1]);
  43. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  44. touchscreen_report_pos(input, prop,
  45. get_unaligned_le16(buf + 2),
  46. get_unaligned_le16(buf + 4),
  47. true);
  48. }
  49. }
  50. }
  51. static void exc3000_timer(struct timer_list *t)
  52. {
  53. struct exc3000_data *data = from_timer(data, t, timer);
  54. input_mt_sync_frame(data->input);
  55. input_sync(data->input);
  56. }
  57. static int exc3000_read_frame(struct i2c_client *client, u8 *buf)
  58. {
  59. int ret;
  60. ret = i2c_master_send(client, "'", 2);
  61. if (ret < 0)
  62. return ret;
  63. if (ret != 2)
  64. return -EIO;
  65. ret = i2c_master_recv(client, buf, EXC3000_LEN_FRAME);
  66. if (ret < 0)
  67. return ret;
  68. if (ret != EXC3000_LEN_FRAME)
  69. return -EIO;
  70. if (get_unaligned_le16(buf) != EXC3000_LEN_FRAME ||
  71. buf[2] != EXC3000_MT_EVENT)
  72. return -EINVAL;
  73. return 0;
  74. }
  75. static int exc3000_read_data(struct i2c_client *client,
  76. u8 *buf, int *n_slots)
  77. {
  78. int error;
  79. error = exc3000_read_frame(client, buf);
  80. if (error)
  81. return error;
  82. *n_slots = buf[3];
  83. if (!*n_slots || *n_slots > EXC3000_NUM_SLOTS)
  84. return -EINVAL;
  85. if (*n_slots > EXC3000_SLOTS_PER_FRAME) {
  86. /* Read 2nd frame to get the rest of the contacts. */
  87. error = exc3000_read_frame(client, buf + EXC3000_LEN_FRAME);
  88. if (error)
  89. return error;
  90. /* 2nd chunk must have number of contacts set to 0. */
  91. if (buf[EXC3000_LEN_FRAME + 3] != 0)
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. static irqreturn_t exc3000_interrupt(int irq, void *dev_id)
  97. {
  98. struct exc3000_data *data = dev_id;
  99. struct input_dev *input = data->input;
  100. u8 *buf = data->buf;
  101. int slots, total_slots;
  102. int error;
  103. error = exc3000_read_data(data->client, buf, &total_slots);
  104. if (error) {
  105. /* Schedule a timer to release "stuck" contacts */
  106. mod_timer(&data->timer,
  107. jiffies + msecs_to_jiffies(EXC3000_TIMEOUT_MS));
  108. goto out;
  109. }
  110. /*
  111. * We read full state successfully, no contacts will be "stuck".
  112. */
  113. del_timer_sync(&data->timer);
  114. while (total_slots > 0) {
  115. slots = min(total_slots, EXC3000_SLOTS_PER_FRAME);
  116. exc3000_report_slots(input, &data->prop, buf + 4, slots);
  117. total_slots -= slots;
  118. buf += EXC3000_LEN_FRAME;
  119. }
  120. input_mt_sync_frame(input);
  121. input_sync(input);
  122. out:
  123. return IRQ_HANDLED;
  124. }
  125. static int exc3000_probe(struct i2c_client *client,
  126. const struct i2c_device_id *id)
  127. {
  128. struct exc3000_data *data;
  129. struct input_dev *input;
  130. int error;
  131. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  132. if (!data)
  133. return -ENOMEM;
  134. data->client = client;
  135. timer_setup(&data->timer, exc3000_timer, 0);
  136. input = devm_input_allocate_device(&client->dev);
  137. if (!input)
  138. return -ENOMEM;
  139. data->input = input;
  140. input->name = "EETI EXC3000 Touch Screen";
  141. input->id.bustype = BUS_I2C;
  142. input_set_abs_params(input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
  143. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
  144. touchscreen_parse_properties(input, true, &data->prop);
  145. error = input_mt_init_slots(input, EXC3000_NUM_SLOTS,
  146. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  147. if (error)
  148. return error;
  149. error = input_register_device(input);
  150. if (error)
  151. return error;
  152. error = devm_request_threaded_irq(&client->dev, client->irq,
  153. NULL, exc3000_interrupt, IRQF_ONESHOT,
  154. client->name, data);
  155. if (error)
  156. return error;
  157. return 0;
  158. }
  159. static const struct i2c_device_id exc3000_id[] = {
  160. { "exc3000", 0 },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(i2c, exc3000_id);
  164. #ifdef CONFIG_OF
  165. static const struct of_device_id exc3000_of_match[] = {
  166. { .compatible = "eeti,exc3000" },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(of, exc3000_of_match);
  170. #endif
  171. static struct i2c_driver exc3000_driver = {
  172. .driver = {
  173. .name = "exc3000",
  174. .of_match_table = of_match_ptr(exc3000_of_match),
  175. },
  176. .id_table = exc3000_id,
  177. .probe = exc3000_probe,
  178. };
  179. module_i2c_driver(exc3000_driver);
  180. MODULE_AUTHOR("Ahmet Inan <inan@distec.de>");
  181. MODULE_DESCRIPTION("I2C connected EETI EXC3000 multiple touch controller driver");
  182. MODULE_LICENSE("GPL v2");