ektf2127.c 9.4 KB

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