1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Ntrig/Microsoft Touchscreens over SPI
5 * Copyright (c) 2016 Red Hat Inc.
9 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/input.h>
14 #include <linux/input/mt.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/spi/spi.h>
19 #include <linux/acpi.h>
21 #include <asm/unaligned.h>
23 #define SURFACE3_PACKET_SIZE 264
25 #define SURFACE3_REPORT_TOUCH 0xd2
26 #define SURFACE3_REPORT_PEN 0x16
28 struct surface3_ts_data {
29 struct spi_device *spi;
30 struct gpio_desc *gpiod_rst[2];
31 struct input_dev *input_dev;
32 struct input_dev *pen_input_dev;
35 u8 rd_buf[SURFACE3_PACKET_SIZE] ____cacheline_aligned;
38 struct surface3_ts_data_finger {
50 struct surface3_ts_data_pen {
58 static int surface3_spi_read(struct surface3_ts_data *ts_data)
60 struct spi_device *spi = ts_data->spi;
62 memset(ts_data->rd_buf, 0, sizeof(ts_data->rd_buf));
63 return spi_read(spi, ts_data->rd_buf, sizeof(ts_data->rd_buf));
66 static void surface3_spi_report_touch(struct surface3_ts_data *ts_data,
67 struct surface3_ts_data_finger *finger)
69 int st = finger->status & 0x01;
72 slot = input_mt_get_slot_by_key(ts_data->input_dev,
73 get_unaligned_le16(&finger->tracking_id));
77 input_mt_slot(ts_data->input_dev, slot);
78 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, st);
80 input_report_abs(ts_data->input_dev,
82 get_unaligned_le16(&finger->x));
83 input_report_abs(ts_data->input_dev,
85 get_unaligned_le16(&finger->y));
86 input_report_abs(ts_data->input_dev,
88 get_unaligned_le16(&finger->width));
89 input_report_abs(ts_data->input_dev,
91 get_unaligned_le16(&finger->height));
95 static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *data)
99 for (i = 0; i < 13; i++) {
100 struct surface3_ts_data_finger *finger;
102 finger = (struct surface3_ts_data_finger *)&data[17 +
103 i * sizeof(struct surface3_ts_data_finger)];
106 * When bit 5 of status is 1, it marks the end of the report:
107 * - touch present: 0xe7
108 * - touch released: 0xe4
109 * - nothing valuable: 0xff
111 if (finger->status & 0x10)
114 surface3_spi_report_touch(ts_data, finger);
117 input_mt_sync_frame(ts_data->input_dev);
118 input_sync(ts_data->input_dev);
121 static void surface3_spi_report_pen(struct surface3_ts_data *ts_data,
122 struct surface3_ts_data_pen *pen)
124 struct input_dev *dev = ts_data->pen_input_dev;
125 int st = pen->status;
126 int prox = st & 0x01;
127 int rubber = st & 0x18;
128 int tool = (prox && rubber) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
130 /* fake proximity out to switch tools */
131 if (ts_data->pen_tool != tool) {
132 input_report_key(dev, ts_data->pen_tool, 0);
134 ts_data->pen_tool = tool;
137 input_report_key(dev, BTN_TOUCH, st & 0x12);
139 input_report_key(dev, ts_data->pen_tool, prox);
142 input_report_key(dev,
146 input_report_abs(dev,
148 get_unaligned_le16(&pen->x));
149 input_report_abs(dev,
151 get_unaligned_le16(&pen->y));
152 input_report_abs(dev,
154 get_unaligned_le16(&pen->pressure));
158 static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data)
160 struct surface3_ts_data_pen *pen;
162 pen = (struct surface3_ts_data_pen *)&data[15];
164 surface3_spi_report_pen(ts_data, pen);
165 input_sync(ts_data->pen_input_dev);
168 static void surface3_spi_process(struct surface3_ts_data *ts_data)
170 static const char header[] = {
171 0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01
173 u8 *data = ts_data->rd_buf;
175 if (memcmp(header, data, sizeof(header)))
176 dev_err(&ts_data->spi->dev,
177 "%s header error: %*ph, ignoring...\n",
178 __func__, (int)sizeof(header), data);
181 case SURFACE3_REPORT_TOUCH:
182 surface3_spi_process_touch(ts_data, data);
184 case SURFACE3_REPORT_PEN:
185 surface3_spi_process_pen(ts_data, data);
188 dev_err(&ts_data->spi->dev,
189 "%s unknown packet type: %x, ignoring...\n",
195 static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
197 struct surface3_ts_data *data = dev_id;
199 if (surface3_spi_read(data))
202 dev_dbg(&data->spi->dev, "%s received -> %*ph\n",
203 __func__, SURFACE3_PACKET_SIZE, data->rd_buf);
204 surface3_spi_process(data);
209 static void surface3_spi_power(struct surface3_ts_data *data, bool on)
211 gpiod_set_value(data->gpiod_rst[0], on);
212 gpiod_set_value(data->gpiod_rst[1], on);
213 /* let the device settle a little */
218 * surface3_spi_get_gpio_config - Get GPIO config from ACPI/DT
220 * @data: surface3_spi_ts_data pointer
222 static int surface3_spi_get_gpio_config(struct surface3_ts_data *data)
225 struct gpio_desc *gpiod;
228 dev = &data->spi->dev;
230 /* Get the reset lines GPIO pin number */
231 for (i = 0; i < 2; i++) {
232 gpiod = devm_gpiod_get_index(dev, NULL, i, GPIOD_OUT_LOW);
234 return dev_err_probe(dev, PTR_ERR(gpiod),
235 "Failed to get power GPIO %d\n", i);
237 data->gpiod_rst[i] = gpiod;
243 static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
245 struct input_dev *input;
248 input = devm_input_allocate_device(&data->spi->dev);
252 data->input_dev = input;
254 input_set_abs_params(input, ABS_MT_POSITION_X, 0, 9600, 0, 0);
255 input_abs_set_res(input, ABS_MT_POSITION_X, 40);
256 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 7200, 0, 0);
257 input_abs_set_res(input, ABS_MT_POSITION_Y, 48);
258 input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 1024, 0, 0);
259 input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 1024, 0, 0);
260 input_mt_init_slots(input, 10, INPUT_MT_DIRECT);
262 input->name = "Surface3 SPI Capacitive TouchScreen";
263 input->phys = "input/ts";
264 input->id.bustype = BUS_SPI;
265 input->id.vendor = 0x045e; /* Microsoft */
266 input->id.product = 0x0001;
267 input->id.version = 0x0000;
269 error = input_register_device(input);
271 dev_err(&data->spi->dev,
272 "Failed to register input device: %d", error);
279 static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
281 struct input_dev *input;
284 input = devm_input_allocate_device(&data->spi->dev);
288 data->pen_input_dev = input;
289 data->pen_tool = BTN_TOOL_PEN;
291 __set_bit(INPUT_PROP_DIRECT, input->propbit);
292 __set_bit(INPUT_PROP_POINTER, input->propbit);
293 input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
294 input_abs_set_res(input, ABS_X, 40);
295 input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
296 input_abs_set_res(input, ABS_Y, 48);
297 input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
298 input_set_capability(input, EV_KEY, BTN_TOUCH);
299 input_set_capability(input, EV_KEY, BTN_STYLUS);
300 input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
301 input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
303 input->name = "Surface3 SPI Pen Input";
304 input->phys = "input/ts";
305 input->id.bustype = BUS_SPI;
306 input->id.vendor = 0x045e; /* Microsoft */
307 input->id.product = 0x0002;
308 input->id.version = 0x0000;
310 error = input_register_device(input);
312 dev_err(&data->spi->dev,
313 "Failed to register input device: %d", error);
320 static int surface3_spi_probe(struct spi_device *spi)
322 struct surface3_ts_data *data;
326 spi->bits_per_word = 8;
327 spi->mode = SPI_MODE_0;
328 error = spi_setup(spi);
332 data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
337 spi_set_drvdata(spi, data);
339 error = surface3_spi_get_gpio_config(data);
343 surface3_spi_power(data, true);
344 surface3_spi_power(data, false);
345 surface3_spi_power(data, true);
347 error = surface3_spi_create_touch_input(data);
351 error = surface3_spi_create_pen_input(data);
355 error = devm_request_threaded_irq(&spi->dev, spi->irq,
356 NULL, surface3_spi_irq_handler,
358 "Surface3-irq", data);
365 static int surface3_spi_suspend(struct device *dev)
367 struct spi_device *spi = to_spi_device(dev);
368 struct surface3_ts_data *data = spi_get_drvdata(spi);
370 disable_irq(data->spi->irq);
372 surface3_spi_power(data, false);
377 static int surface3_spi_resume(struct device *dev)
379 struct spi_device *spi = to_spi_device(dev);
380 struct surface3_ts_data *data = spi_get_drvdata(spi);
382 surface3_spi_power(data, true);
384 enable_irq(data->spi->irq);
389 static DEFINE_SIMPLE_DEV_PM_OPS(surface3_spi_pm_ops,
390 surface3_spi_suspend,
391 surface3_spi_resume);
394 static const struct acpi_device_id surface3_spi_acpi_match[] = {
398 MODULE_DEVICE_TABLE(acpi, surface3_spi_acpi_match);
401 static struct spi_driver surface3_spi_driver = {
403 .name = "Surface3-spi",
404 .acpi_match_table = ACPI_PTR(surface3_spi_acpi_match),
405 .pm = pm_sleep_ptr(&surface3_spi_pm_ops),
407 .probe = surface3_spi_probe,
410 module_spi_driver(surface3_spi_driver);
413 MODULE_DESCRIPTION("Surface 3 SPI touchscreen driver");
414 MODULE_LICENSE("GPL v2");