surface3_spi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Driver for Ntrig/Microsoft Touchscreens over SPI
  3. *
  4. * Copyright (c) 2016 Red Hat Inc.
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/input.h>
  15. #include <linux/input/mt.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/acpi.h>
  21. #include <asm/unaligned.h>
  22. #define SURFACE3_PACKET_SIZE 264
  23. #define SURFACE3_REPORT_TOUCH 0xd2
  24. struct surface3_ts_data {
  25. struct spi_device *spi;
  26. struct gpio_desc *gpiod_rst[2];
  27. struct input_dev *input_dev;
  28. u8 rd_buf[SURFACE3_PACKET_SIZE] ____cacheline_aligned;
  29. };
  30. struct surface3_ts_data_finger {
  31. u8 status;
  32. __le16 tracking_id;
  33. __le16 x;
  34. __le16 cx;
  35. __le16 y;
  36. __le16 cy;
  37. __le16 width;
  38. __le16 height;
  39. u32 padding;
  40. } __packed;
  41. static int surface3_spi_read(struct surface3_ts_data *ts_data)
  42. {
  43. struct spi_device *spi = ts_data->spi;
  44. memset(ts_data->rd_buf, 0, sizeof(ts_data->rd_buf));
  45. return spi_read(spi, ts_data->rd_buf, sizeof(ts_data->rd_buf));
  46. }
  47. static void surface3_spi_report_touch(struct surface3_ts_data *ts_data,
  48. struct surface3_ts_data_finger *finger)
  49. {
  50. int st = finger->status & 0x01;
  51. int slot;
  52. slot = input_mt_get_slot_by_key(ts_data->input_dev,
  53. get_unaligned_le16(&finger->tracking_id));
  54. if (slot < 0)
  55. return;
  56. input_mt_slot(ts_data->input_dev, slot);
  57. input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, st);
  58. if (st) {
  59. input_report_abs(ts_data->input_dev,
  60. ABS_MT_POSITION_X,
  61. get_unaligned_le16(&finger->x));
  62. input_report_abs(ts_data->input_dev,
  63. ABS_MT_POSITION_Y,
  64. get_unaligned_le16(&finger->y));
  65. input_report_abs(ts_data->input_dev,
  66. ABS_MT_WIDTH_MAJOR,
  67. get_unaligned_le16(&finger->width));
  68. input_report_abs(ts_data->input_dev,
  69. ABS_MT_WIDTH_MINOR,
  70. get_unaligned_le16(&finger->height));
  71. }
  72. }
  73. static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *data)
  74. {
  75. u16 timestamp;
  76. unsigned int i;
  77. timestamp = get_unaligned_le16(&data[15]);
  78. for (i = 0; i < 13; i++) {
  79. struct surface3_ts_data_finger *finger;
  80. finger = (struct surface3_ts_data_finger *)&data[17 +
  81. i * sizeof(struct surface3_ts_data_finger)];
  82. /*
  83. * When bit 5 of status is 1, it marks the end of the report:
  84. * - touch present: 0xe7
  85. * - touch released: 0xe4
  86. * - nothing valuable: 0xff
  87. */
  88. if (finger->status & 0x10)
  89. break;
  90. surface3_spi_report_touch(ts_data, finger);
  91. }
  92. input_mt_sync_frame(ts_data->input_dev);
  93. input_sync(ts_data->input_dev);
  94. }
  95. static void surface3_spi_process(struct surface3_ts_data *ts_data)
  96. {
  97. const char header[] = {
  98. 0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01
  99. };
  100. u8 *data = ts_data->rd_buf;
  101. if (memcmp(header, data, sizeof(header)))
  102. dev_err(&ts_data->spi->dev,
  103. "%s header error: %*ph, ignoring...\n",
  104. __func__, (int)sizeof(header), data);
  105. if (data[9] == SURFACE3_REPORT_TOUCH)
  106. surface3_spi_process_touch(ts_data, data);
  107. else
  108. dev_err(&ts_data->spi->dev,
  109. "%s unknown packet type: %x, ignoring...\n",
  110. __func__, data[9]);
  111. }
  112. static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
  113. {
  114. struct surface3_ts_data *data = dev_id;
  115. if (surface3_spi_read(data))
  116. return IRQ_HANDLED;
  117. dev_dbg(&data->spi->dev, "%s received -> %*ph\n",
  118. __func__, SURFACE3_PACKET_SIZE, data->rd_buf);
  119. surface3_spi_process(data);
  120. return IRQ_HANDLED;
  121. }
  122. static void surface3_spi_power(struct surface3_ts_data *data, bool on)
  123. {
  124. gpiod_set_value(data->gpiod_rst[0], on);
  125. gpiod_set_value(data->gpiod_rst[1], on);
  126. /* let the device settle a little */
  127. msleep(20);
  128. }
  129. /**
  130. * surface3_spi_get_gpio_config - Get GPIO config from ACPI/DT
  131. *
  132. * @ts: surface3_spi_ts_data pointer
  133. */
  134. static int surface3_spi_get_gpio_config(struct surface3_ts_data *data)
  135. {
  136. int error;
  137. struct device *dev;
  138. struct gpio_desc *gpiod;
  139. int i;
  140. dev = &data->spi->dev;
  141. /* Get the reset lines GPIO pin number */
  142. for (i = 0; i < 2; i++) {
  143. gpiod = devm_gpiod_get_index(dev, NULL, i, GPIOD_OUT_LOW);
  144. if (IS_ERR(gpiod)) {
  145. error = PTR_ERR(gpiod);
  146. if (error != -EPROBE_DEFER)
  147. dev_err(dev,
  148. "Failed to get power GPIO %d: %d\n",
  149. i,
  150. error);
  151. return error;
  152. }
  153. data->gpiod_rst[i] = gpiod;
  154. }
  155. return 0;
  156. }
  157. static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
  158. {
  159. struct input_dev *input;
  160. int error;
  161. input = devm_input_allocate_device(&data->spi->dev);
  162. if (!input)
  163. return -ENOMEM;
  164. data->input_dev = input;
  165. input_set_abs_params(input, ABS_MT_POSITION_X, 0, 9600, 0, 0);
  166. input_abs_set_res(input, ABS_MT_POSITION_X, 40);
  167. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 7200, 0, 0);
  168. input_abs_set_res(input, ABS_MT_POSITION_Y, 48);
  169. input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 1024, 0, 0);
  170. input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 1024, 0, 0);
  171. input_mt_init_slots(input, 10, INPUT_MT_DIRECT);
  172. input->name = "Surface3 SPI Capacitive TouchScreen";
  173. input->phys = "input/ts";
  174. input->id.bustype = BUS_SPI;
  175. input->id.vendor = 0x045e; /* Microsoft */
  176. input->id.product = 0x0001;
  177. input->id.version = 0x0000;
  178. error = input_register_device(input);
  179. if (error) {
  180. dev_err(&data->spi->dev,
  181. "Failed to register input device: %d", error);
  182. return error;
  183. }
  184. return 0;
  185. }
  186. static int surface3_spi_probe(struct spi_device *spi)
  187. {
  188. struct surface3_ts_data *data;
  189. int error;
  190. /* Set up SPI*/
  191. spi->bits_per_word = 8;
  192. spi->mode = SPI_MODE_0;
  193. error = spi_setup(spi);
  194. if (error)
  195. return error;
  196. data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
  197. if (!data)
  198. return -ENOMEM;
  199. data->spi = spi;
  200. spi_set_drvdata(spi, data);
  201. error = surface3_spi_get_gpio_config(data);
  202. if (error)
  203. return error;
  204. surface3_spi_power(data, true);
  205. surface3_spi_power(data, false);
  206. surface3_spi_power(data, true);
  207. error = surface3_spi_create_touch_input(data);
  208. if (error)
  209. return error;
  210. error = devm_request_threaded_irq(&spi->dev, spi->irq,
  211. NULL, surface3_spi_irq_handler,
  212. IRQF_ONESHOT,
  213. "Surface3-irq", data);
  214. if (error)
  215. return error;
  216. return 0;
  217. }
  218. static int __maybe_unused surface3_spi_suspend(struct device *dev)
  219. {
  220. struct spi_device *spi = to_spi_device(dev);
  221. struct surface3_ts_data *data = spi_get_drvdata(spi);
  222. disable_irq(data->spi->irq);
  223. surface3_spi_power(data, false);
  224. return 0;
  225. }
  226. static int __maybe_unused surface3_spi_resume(struct device *dev)
  227. {
  228. struct spi_device *spi = to_spi_device(dev);
  229. struct surface3_ts_data *data = spi_get_drvdata(spi);
  230. surface3_spi_power(data, true);
  231. enable_irq(data->spi->irq);
  232. return 0;
  233. }
  234. static SIMPLE_DEV_PM_OPS(surface3_spi_pm_ops,
  235. surface3_spi_suspend,
  236. surface3_spi_resume);
  237. #ifdef CONFIG_ACPI
  238. static const struct acpi_device_id surface3_spi_acpi_match[] = {
  239. { "MSHW0037", 0 },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(acpi, surface3_spi_acpi_match);
  243. #endif
  244. static struct spi_driver surface3_spi_driver = {
  245. .driver = {
  246. .name = "Surface3-spi",
  247. .acpi_match_table = ACPI_PTR(surface3_spi_acpi_match),
  248. .pm = &surface3_spi_pm_ops,
  249. },
  250. .probe = surface3_spi_probe,
  251. };
  252. module_spi_driver(surface3_spi_driver);
  253. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  254. MODULE_DESCRIPTION("Surface 3 SPI touchscreen driver");
  255. MODULE_LICENSE("GPL v2");