surface3_spi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Ntrig/Microsoft Touchscreens over SPI
  4. *
  5. * Copyright (c) 2016 Red Hat Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/input.h>
  11. #include <linux/input/mt.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/acpi.h>
  17. #include <linux/unaligned.h>
  18. #define SURFACE3_PACKET_SIZE 264
  19. #define SURFACE3_REPORT_TOUCH 0xd2
  20. #define SURFACE3_REPORT_PEN 0x16
  21. struct surface3_ts_data {
  22. struct spi_device *spi;
  23. struct gpio_desc *gpiod_rst[2];
  24. struct input_dev *input_dev;
  25. struct input_dev *pen_input_dev;
  26. int pen_tool;
  27. u8 rd_buf[SURFACE3_PACKET_SIZE] ____cacheline_aligned;
  28. };
  29. struct surface3_ts_data_finger {
  30. u8 status;
  31. __le16 tracking_id;
  32. __le16 x;
  33. __le16 cx;
  34. __le16 y;
  35. __le16 cy;
  36. __le16 width;
  37. __le16 height;
  38. u32 padding;
  39. } __packed;
  40. struct surface3_ts_data_pen {
  41. u8 status;
  42. __le16 x;
  43. __le16 y;
  44. __le16 pressure;
  45. u8 padding;
  46. } __packed;
  47. static int surface3_spi_read(struct surface3_ts_data *ts_data)
  48. {
  49. struct spi_device *spi = ts_data->spi;
  50. memset(ts_data->rd_buf, 0, sizeof(ts_data->rd_buf));
  51. return spi_read(spi, ts_data->rd_buf, sizeof(ts_data->rd_buf));
  52. }
  53. static void surface3_spi_report_touch(struct surface3_ts_data *ts_data,
  54. struct surface3_ts_data_finger *finger)
  55. {
  56. int st = finger->status & 0x01;
  57. int slot;
  58. slot = input_mt_get_slot_by_key(ts_data->input_dev,
  59. get_unaligned_le16(&finger->tracking_id));
  60. if (slot < 0)
  61. return;
  62. input_mt_slot(ts_data->input_dev, slot);
  63. input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, st);
  64. if (st) {
  65. input_report_abs(ts_data->input_dev,
  66. ABS_MT_POSITION_X,
  67. get_unaligned_le16(&finger->x));
  68. input_report_abs(ts_data->input_dev,
  69. ABS_MT_POSITION_Y,
  70. get_unaligned_le16(&finger->y));
  71. input_report_abs(ts_data->input_dev,
  72. ABS_MT_WIDTH_MAJOR,
  73. get_unaligned_le16(&finger->width));
  74. input_report_abs(ts_data->input_dev,
  75. ABS_MT_WIDTH_MINOR,
  76. get_unaligned_le16(&finger->height));
  77. }
  78. }
  79. static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *data)
  80. {
  81. unsigned int i;
  82. for (i = 0; i < 13; i++) {
  83. struct surface3_ts_data_finger *finger;
  84. finger = (struct surface3_ts_data_finger *)&data[17 +
  85. i * sizeof(struct surface3_ts_data_finger)];
  86. /*
  87. * When bit 5 of status is 1, it marks the end of the report:
  88. * - touch present: 0xe7
  89. * - touch released: 0xe4
  90. * - nothing valuable: 0xff
  91. */
  92. if (finger->status & 0x10)
  93. break;
  94. surface3_spi_report_touch(ts_data, finger);
  95. }
  96. input_mt_sync_frame(ts_data->input_dev);
  97. input_sync(ts_data->input_dev);
  98. }
  99. static void surface3_spi_report_pen(struct surface3_ts_data *ts_data,
  100. struct surface3_ts_data_pen *pen)
  101. {
  102. struct input_dev *dev = ts_data->pen_input_dev;
  103. int st = pen->status;
  104. int prox = st & 0x01;
  105. int rubber = st & 0x18;
  106. int tool = (prox && rubber) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
  107. /* fake proximity out to switch tools */
  108. if (ts_data->pen_tool != tool) {
  109. input_report_key(dev, ts_data->pen_tool, 0);
  110. input_sync(dev);
  111. ts_data->pen_tool = tool;
  112. }
  113. input_report_key(dev, BTN_TOUCH, st & 0x12);
  114. input_report_key(dev, ts_data->pen_tool, prox);
  115. if (st) {
  116. input_report_key(dev,
  117. BTN_STYLUS,
  118. st & 0x04);
  119. input_report_abs(dev,
  120. ABS_X,
  121. get_unaligned_le16(&pen->x));
  122. input_report_abs(dev,
  123. ABS_Y,
  124. get_unaligned_le16(&pen->y));
  125. input_report_abs(dev,
  126. ABS_PRESSURE,
  127. get_unaligned_le16(&pen->pressure));
  128. }
  129. }
  130. static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data)
  131. {
  132. struct surface3_ts_data_pen *pen;
  133. pen = (struct surface3_ts_data_pen *)&data[15];
  134. surface3_spi_report_pen(ts_data, pen);
  135. input_sync(ts_data->pen_input_dev);
  136. }
  137. static void surface3_spi_process(struct surface3_ts_data *ts_data)
  138. {
  139. static const char header[] = {
  140. 0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01
  141. };
  142. u8 *data = ts_data->rd_buf;
  143. if (memcmp(header, data, sizeof(header)))
  144. dev_err(&ts_data->spi->dev,
  145. "%s header error: %*ph, ignoring...\n",
  146. __func__, (int)sizeof(header), data);
  147. switch (data[9]) {
  148. case SURFACE3_REPORT_TOUCH:
  149. surface3_spi_process_touch(ts_data, data);
  150. break;
  151. case SURFACE3_REPORT_PEN:
  152. surface3_spi_process_pen(ts_data, data);
  153. break;
  154. default:
  155. dev_err(&ts_data->spi->dev,
  156. "%s unknown packet type: %x, ignoring...\n",
  157. __func__, data[9]);
  158. break;
  159. }
  160. }
  161. static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
  162. {
  163. struct surface3_ts_data *data = dev_id;
  164. if (surface3_spi_read(data))
  165. return IRQ_HANDLED;
  166. dev_dbg(&data->spi->dev, "%s received -> %*ph\n",
  167. __func__, SURFACE3_PACKET_SIZE, data->rd_buf);
  168. surface3_spi_process(data);
  169. return IRQ_HANDLED;
  170. }
  171. static void surface3_spi_power(struct surface3_ts_data *data, bool on)
  172. {
  173. gpiod_set_value(data->gpiod_rst[0], on);
  174. gpiod_set_value(data->gpiod_rst[1], on);
  175. /* let the device settle a little */
  176. msleep(20);
  177. }
  178. /**
  179. * surface3_spi_get_gpio_config - Get GPIO config from ACPI/DT
  180. *
  181. * @data: surface3_spi_ts_data pointer
  182. */
  183. static int surface3_spi_get_gpio_config(struct surface3_ts_data *data)
  184. {
  185. struct device *dev;
  186. struct gpio_desc *gpiod;
  187. int i;
  188. dev = &data->spi->dev;
  189. /* Get the reset lines GPIO pin number */
  190. for (i = 0; i < 2; i++) {
  191. gpiod = devm_gpiod_get_index(dev, NULL, i, GPIOD_OUT_LOW);
  192. if (IS_ERR(gpiod))
  193. return dev_err_probe(dev, PTR_ERR(gpiod),
  194. "Failed to get power GPIO %d\n", i);
  195. data->gpiod_rst[i] = gpiod;
  196. }
  197. return 0;
  198. }
  199. static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
  200. {
  201. struct input_dev *input;
  202. int error;
  203. input = devm_input_allocate_device(&data->spi->dev);
  204. if (!input)
  205. return -ENOMEM;
  206. data->input_dev = input;
  207. input_set_abs_params(input, ABS_MT_POSITION_X, 0, 9600, 0, 0);
  208. input_abs_set_res(input, ABS_MT_POSITION_X, 40);
  209. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 7200, 0, 0);
  210. input_abs_set_res(input, ABS_MT_POSITION_Y, 48);
  211. input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 1024, 0, 0);
  212. input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 1024, 0, 0);
  213. input_mt_init_slots(input, 10, INPUT_MT_DIRECT);
  214. input->name = "Surface3 SPI Capacitive TouchScreen";
  215. input->phys = "input/ts";
  216. input->id.bustype = BUS_SPI;
  217. input->id.vendor = 0x045e; /* Microsoft */
  218. input->id.product = 0x0001;
  219. input->id.version = 0x0000;
  220. error = input_register_device(input);
  221. if (error) {
  222. dev_err(&data->spi->dev,
  223. "Failed to register input device: %d", error);
  224. return error;
  225. }
  226. return 0;
  227. }
  228. static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
  229. {
  230. struct input_dev *input;
  231. int error;
  232. input = devm_input_allocate_device(&data->spi->dev);
  233. if (!input)
  234. return -ENOMEM;
  235. data->pen_input_dev = input;
  236. data->pen_tool = BTN_TOOL_PEN;
  237. __set_bit(INPUT_PROP_DIRECT, input->propbit);
  238. __set_bit(INPUT_PROP_POINTER, input->propbit);
  239. input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
  240. input_abs_set_res(input, ABS_X, 40);
  241. input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
  242. input_abs_set_res(input, ABS_Y, 48);
  243. input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
  244. input_set_capability(input, EV_KEY, BTN_TOUCH);
  245. input_set_capability(input, EV_KEY, BTN_STYLUS);
  246. input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
  247. input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
  248. input->name = "Surface3 SPI Pen Input";
  249. input->phys = "input/ts";
  250. input->id.bustype = BUS_SPI;
  251. input->id.vendor = 0x045e; /* Microsoft */
  252. input->id.product = 0x0002;
  253. input->id.version = 0x0000;
  254. error = input_register_device(input);
  255. if (error) {
  256. dev_err(&data->spi->dev,
  257. "Failed to register input device: %d", error);
  258. return error;
  259. }
  260. return 0;
  261. }
  262. static int surface3_spi_probe(struct spi_device *spi)
  263. {
  264. struct surface3_ts_data *data;
  265. int error;
  266. /* Set up SPI*/
  267. spi->bits_per_word = 8;
  268. spi->mode = SPI_MODE_0;
  269. error = spi_setup(spi);
  270. if (error)
  271. return error;
  272. data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
  273. if (!data)
  274. return -ENOMEM;
  275. data->spi = spi;
  276. spi_set_drvdata(spi, data);
  277. error = surface3_spi_get_gpio_config(data);
  278. if (error)
  279. return error;
  280. surface3_spi_power(data, true);
  281. surface3_spi_power(data, false);
  282. surface3_spi_power(data, true);
  283. error = surface3_spi_create_touch_input(data);
  284. if (error)
  285. return error;
  286. error = surface3_spi_create_pen_input(data);
  287. if (error)
  288. return error;
  289. error = devm_request_threaded_irq(&spi->dev, spi->irq,
  290. NULL, surface3_spi_irq_handler,
  291. IRQF_ONESHOT,
  292. "Surface3-irq", data);
  293. if (error)
  294. return error;
  295. return 0;
  296. }
  297. static int surface3_spi_suspend(struct device *dev)
  298. {
  299. struct spi_device *spi = to_spi_device(dev);
  300. struct surface3_ts_data *data = spi_get_drvdata(spi);
  301. disable_irq(data->spi->irq);
  302. surface3_spi_power(data, false);
  303. return 0;
  304. }
  305. static int surface3_spi_resume(struct device *dev)
  306. {
  307. struct spi_device *spi = to_spi_device(dev);
  308. struct surface3_ts_data *data = spi_get_drvdata(spi);
  309. surface3_spi_power(data, true);
  310. enable_irq(data->spi->irq);
  311. return 0;
  312. }
  313. static DEFINE_SIMPLE_DEV_PM_OPS(surface3_spi_pm_ops,
  314. surface3_spi_suspend,
  315. surface3_spi_resume);
  316. #ifdef CONFIG_ACPI
  317. static const struct acpi_device_id surface3_spi_acpi_match[] = {
  318. { "MSHW0037", 0 },
  319. { }
  320. };
  321. MODULE_DEVICE_TABLE(acpi, surface3_spi_acpi_match);
  322. #endif
  323. static struct spi_driver surface3_spi_driver = {
  324. .driver = {
  325. .name = "Surface3-spi",
  326. .acpi_match_table = ACPI_PTR(surface3_spi_acpi_match),
  327. .pm = pm_sleep_ptr(&surface3_spi_pm_ops),
  328. },
  329. .probe = surface3_spi_probe,
  330. };
  331. module_spi_driver(surface3_spi_driver);
  332. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  333. MODULE_DESCRIPTION("Surface 3 SPI touchscreen driver");
  334. MODULE_LICENSE("GPL v2");