surface3_spi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <asm/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. int error;
  186. struct device *dev;
  187. struct gpio_desc *gpiod;
  188. int i;
  189. dev = &data->spi->dev;
  190. /* Get the reset lines GPIO pin number */
  191. for (i = 0; i < 2; i++) {
  192. gpiod = devm_gpiod_get_index(dev, NULL, i, GPIOD_OUT_LOW);
  193. if (IS_ERR(gpiod)) {
  194. error = PTR_ERR(gpiod);
  195. if (error != -EPROBE_DEFER)
  196. dev_err(dev,
  197. "Failed to get power GPIO %d: %d\n",
  198. i,
  199. error);
  200. return error;
  201. }
  202. data->gpiod_rst[i] = gpiod;
  203. }
  204. return 0;
  205. }
  206. static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
  207. {
  208. struct input_dev *input;
  209. int error;
  210. input = devm_input_allocate_device(&data->spi->dev);
  211. if (!input)
  212. return -ENOMEM;
  213. data->input_dev = input;
  214. input_set_abs_params(input, ABS_MT_POSITION_X, 0, 9600, 0, 0);
  215. input_abs_set_res(input, ABS_MT_POSITION_X, 40);
  216. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 7200, 0, 0);
  217. input_abs_set_res(input, ABS_MT_POSITION_Y, 48);
  218. input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 1024, 0, 0);
  219. input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 1024, 0, 0);
  220. input_mt_init_slots(input, 10, INPUT_MT_DIRECT);
  221. input->name = "Surface3 SPI Capacitive TouchScreen";
  222. input->phys = "input/ts";
  223. input->id.bustype = BUS_SPI;
  224. input->id.vendor = 0x045e; /* Microsoft */
  225. input->id.product = 0x0001;
  226. input->id.version = 0x0000;
  227. error = input_register_device(input);
  228. if (error) {
  229. dev_err(&data->spi->dev,
  230. "Failed to register input device: %d", error);
  231. return error;
  232. }
  233. return 0;
  234. }
  235. static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
  236. {
  237. struct input_dev *input;
  238. int error;
  239. input = devm_input_allocate_device(&data->spi->dev);
  240. if (!input)
  241. return -ENOMEM;
  242. data->pen_input_dev = input;
  243. data->pen_tool = BTN_TOOL_PEN;
  244. __set_bit(INPUT_PROP_DIRECT, input->propbit);
  245. __set_bit(INPUT_PROP_POINTER, input->propbit);
  246. input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
  247. input_abs_set_res(input, ABS_X, 40);
  248. input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
  249. input_abs_set_res(input, ABS_Y, 48);
  250. input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
  251. input_set_capability(input, EV_KEY, BTN_TOUCH);
  252. input_set_capability(input, EV_KEY, BTN_STYLUS);
  253. input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
  254. input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
  255. input->name = "Surface3 SPI Pen Input";
  256. input->phys = "input/ts";
  257. input->id.bustype = BUS_SPI;
  258. input->id.vendor = 0x045e; /* Microsoft */
  259. input->id.product = 0x0002;
  260. input->id.version = 0x0000;
  261. error = input_register_device(input);
  262. if (error) {
  263. dev_err(&data->spi->dev,
  264. "Failed to register input device: %d", error);
  265. return error;
  266. }
  267. return 0;
  268. }
  269. static int surface3_spi_probe(struct spi_device *spi)
  270. {
  271. struct surface3_ts_data *data;
  272. int error;
  273. /* Set up SPI*/
  274. spi->bits_per_word = 8;
  275. spi->mode = SPI_MODE_0;
  276. error = spi_setup(spi);
  277. if (error)
  278. return error;
  279. data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
  280. if (!data)
  281. return -ENOMEM;
  282. data->spi = spi;
  283. spi_set_drvdata(spi, data);
  284. error = surface3_spi_get_gpio_config(data);
  285. if (error)
  286. return error;
  287. surface3_spi_power(data, true);
  288. surface3_spi_power(data, false);
  289. surface3_spi_power(data, true);
  290. error = surface3_spi_create_touch_input(data);
  291. if (error)
  292. return error;
  293. error = surface3_spi_create_pen_input(data);
  294. if (error)
  295. return error;
  296. error = devm_request_threaded_irq(&spi->dev, spi->irq,
  297. NULL, surface3_spi_irq_handler,
  298. IRQF_ONESHOT,
  299. "Surface3-irq", data);
  300. if (error)
  301. return error;
  302. return 0;
  303. }
  304. static int __maybe_unused surface3_spi_suspend(struct device *dev)
  305. {
  306. struct spi_device *spi = to_spi_device(dev);
  307. struct surface3_ts_data *data = spi_get_drvdata(spi);
  308. disable_irq(data->spi->irq);
  309. surface3_spi_power(data, false);
  310. return 0;
  311. }
  312. static int __maybe_unused surface3_spi_resume(struct device *dev)
  313. {
  314. struct spi_device *spi = to_spi_device(dev);
  315. struct surface3_ts_data *data = spi_get_drvdata(spi);
  316. surface3_spi_power(data, true);
  317. enable_irq(data->spi->irq);
  318. return 0;
  319. }
  320. static SIMPLE_DEV_PM_OPS(surface3_spi_pm_ops,
  321. surface3_spi_suspend,
  322. surface3_spi_resume);
  323. #ifdef CONFIG_ACPI
  324. static const struct acpi_device_id surface3_spi_acpi_match[] = {
  325. { "MSHW0037", 0 },
  326. { }
  327. };
  328. MODULE_DEVICE_TABLE(acpi, surface3_spi_acpi_match);
  329. #endif
  330. static struct spi_driver surface3_spi_driver = {
  331. .driver = {
  332. .name = "Surface3-spi",
  333. .acpi_match_table = ACPI_PTR(surface3_spi_acpi_match),
  334. .pm = &surface3_spi_pm_ops,
  335. },
  336. .probe = surface3_spi_probe,
  337. };
  338. module_spi_driver(surface3_spi_driver);
  339. MODULE_AUTHOR("Benjamin Tissoires <[email protected]>");
  340. MODULE_DESCRIPTION("Surface 3 SPI touchscreen driver");
  341. MODULE_LICENSE("GPL v2");