surface3_spi.c 9.9 KB

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