0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010
0011 #include <linux/delay.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/input.h>
0014 #include <linux/input/mt.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 #include <linux/spi/spi.h>
0019 #include <linux/acpi.h>
0020
0021 #include <asm/unaligned.h>
0022
0023 #define SURFACE3_PACKET_SIZE 264
0024
0025 #define SURFACE3_REPORT_TOUCH 0xd2
0026 #define SURFACE3_REPORT_PEN 0x16
0027
0028 struct surface3_ts_data {
0029 struct spi_device *spi;
0030 struct gpio_desc *gpiod_rst[2];
0031 struct input_dev *input_dev;
0032 struct input_dev *pen_input_dev;
0033 int pen_tool;
0034
0035 u8 rd_buf[SURFACE3_PACKET_SIZE] ____cacheline_aligned;
0036 };
0037
0038 struct surface3_ts_data_finger {
0039 u8 status;
0040 __le16 tracking_id;
0041 __le16 x;
0042 __le16 cx;
0043 __le16 y;
0044 __le16 cy;
0045 __le16 width;
0046 __le16 height;
0047 u32 padding;
0048 } __packed;
0049
0050 struct surface3_ts_data_pen {
0051 u8 status;
0052 __le16 x;
0053 __le16 y;
0054 __le16 pressure;
0055 u8 padding;
0056 } __packed;
0057
0058 static int surface3_spi_read(struct surface3_ts_data *ts_data)
0059 {
0060 struct spi_device *spi = ts_data->spi;
0061
0062 memset(ts_data->rd_buf, 0, sizeof(ts_data->rd_buf));
0063 return spi_read(spi, ts_data->rd_buf, sizeof(ts_data->rd_buf));
0064 }
0065
0066 static void surface3_spi_report_touch(struct surface3_ts_data *ts_data,
0067 struct surface3_ts_data_finger *finger)
0068 {
0069 int st = finger->status & 0x01;
0070 int slot;
0071
0072 slot = input_mt_get_slot_by_key(ts_data->input_dev,
0073 get_unaligned_le16(&finger->tracking_id));
0074 if (slot < 0)
0075 return;
0076
0077 input_mt_slot(ts_data->input_dev, slot);
0078 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, st);
0079 if (st) {
0080 input_report_abs(ts_data->input_dev,
0081 ABS_MT_POSITION_X,
0082 get_unaligned_le16(&finger->x));
0083 input_report_abs(ts_data->input_dev,
0084 ABS_MT_POSITION_Y,
0085 get_unaligned_le16(&finger->y));
0086 input_report_abs(ts_data->input_dev,
0087 ABS_MT_WIDTH_MAJOR,
0088 get_unaligned_le16(&finger->width));
0089 input_report_abs(ts_data->input_dev,
0090 ABS_MT_WIDTH_MINOR,
0091 get_unaligned_le16(&finger->height));
0092 }
0093 }
0094
0095 static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *data)
0096 {
0097 unsigned int i;
0098
0099 for (i = 0; i < 13; i++) {
0100 struct surface3_ts_data_finger *finger;
0101
0102 finger = (struct surface3_ts_data_finger *)&data[17 +
0103 i * sizeof(struct surface3_ts_data_finger)];
0104
0105
0106
0107
0108
0109
0110
0111 if (finger->status & 0x10)
0112 break;
0113
0114 surface3_spi_report_touch(ts_data, finger);
0115 }
0116
0117 input_mt_sync_frame(ts_data->input_dev);
0118 input_sync(ts_data->input_dev);
0119 }
0120
0121 static void surface3_spi_report_pen(struct surface3_ts_data *ts_data,
0122 struct surface3_ts_data_pen *pen)
0123 {
0124 struct input_dev *dev = ts_data->pen_input_dev;
0125 int st = pen->status;
0126 int prox = st & 0x01;
0127 int rubber = st & 0x18;
0128 int tool = (prox && rubber) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
0129
0130
0131 if (ts_data->pen_tool != tool) {
0132 input_report_key(dev, ts_data->pen_tool, 0);
0133 input_sync(dev);
0134 ts_data->pen_tool = tool;
0135 }
0136
0137 input_report_key(dev, BTN_TOUCH, st & 0x12);
0138
0139 input_report_key(dev, ts_data->pen_tool, prox);
0140
0141 if (st) {
0142 input_report_key(dev,
0143 BTN_STYLUS,
0144 st & 0x04);
0145
0146 input_report_abs(dev,
0147 ABS_X,
0148 get_unaligned_le16(&pen->x));
0149 input_report_abs(dev,
0150 ABS_Y,
0151 get_unaligned_le16(&pen->y));
0152 input_report_abs(dev,
0153 ABS_PRESSURE,
0154 get_unaligned_le16(&pen->pressure));
0155 }
0156 }
0157
0158 static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data)
0159 {
0160 struct surface3_ts_data_pen *pen;
0161
0162 pen = (struct surface3_ts_data_pen *)&data[15];
0163
0164 surface3_spi_report_pen(ts_data, pen);
0165 input_sync(ts_data->pen_input_dev);
0166 }
0167
0168 static void surface3_spi_process(struct surface3_ts_data *ts_data)
0169 {
0170 static const char header[] = {
0171 0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01
0172 };
0173 u8 *data = ts_data->rd_buf;
0174
0175 if (memcmp(header, data, sizeof(header)))
0176 dev_err(&ts_data->spi->dev,
0177 "%s header error: %*ph, ignoring...\n",
0178 __func__, (int)sizeof(header), data);
0179
0180 switch (data[9]) {
0181 case SURFACE3_REPORT_TOUCH:
0182 surface3_spi_process_touch(ts_data, data);
0183 break;
0184 case SURFACE3_REPORT_PEN:
0185 surface3_spi_process_pen(ts_data, data);
0186 break;
0187 default:
0188 dev_err(&ts_data->spi->dev,
0189 "%s unknown packet type: %x, ignoring...\n",
0190 __func__, data[9]);
0191 break;
0192 }
0193 }
0194
0195 static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
0196 {
0197 struct surface3_ts_data *data = dev_id;
0198
0199 if (surface3_spi_read(data))
0200 return IRQ_HANDLED;
0201
0202 dev_dbg(&data->spi->dev, "%s received -> %*ph\n",
0203 __func__, SURFACE3_PACKET_SIZE, data->rd_buf);
0204 surface3_spi_process(data);
0205
0206 return IRQ_HANDLED;
0207 }
0208
0209 static void surface3_spi_power(struct surface3_ts_data *data, bool on)
0210 {
0211 gpiod_set_value(data->gpiod_rst[0], on);
0212 gpiod_set_value(data->gpiod_rst[1], on);
0213
0214 msleep(20);
0215 }
0216
0217
0218
0219
0220
0221
0222 static int surface3_spi_get_gpio_config(struct surface3_ts_data *data)
0223 {
0224 int error;
0225 struct device *dev;
0226 struct gpio_desc *gpiod;
0227 int i;
0228
0229 dev = &data->spi->dev;
0230
0231
0232 for (i = 0; i < 2; i++) {
0233 gpiod = devm_gpiod_get_index(dev, NULL, i, GPIOD_OUT_LOW);
0234 if (IS_ERR(gpiod)) {
0235 error = PTR_ERR(gpiod);
0236 if (error != -EPROBE_DEFER)
0237 dev_err(dev,
0238 "Failed to get power GPIO %d: %d\n",
0239 i,
0240 error);
0241 return error;
0242 }
0243
0244 data->gpiod_rst[i] = gpiod;
0245 }
0246
0247 return 0;
0248 }
0249
0250 static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
0251 {
0252 struct input_dev *input;
0253 int error;
0254
0255 input = devm_input_allocate_device(&data->spi->dev);
0256 if (!input)
0257 return -ENOMEM;
0258
0259 data->input_dev = input;
0260
0261 input_set_abs_params(input, ABS_MT_POSITION_X, 0, 9600, 0, 0);
0262 input_abs_set_res(input, ABS_MT_POSITION_X, 40);
0263 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 7200, 0, 0);
0264 input_abs_set_res(input, ABS_MT_POSITION_Y, 48);
0265 input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 1024, 0, 0);
0266 input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 1024, 0, 0);
0267 input_mt_init_slots(input, 10, INPUT_MT_DIRECT);
0268
0269 input->name = "Surface3 SPI Capacitive TouchScreen";
0270 input->phys = "input/ts";
0271 input->id.bustype = BUS_SPI;
0272 input->id.vendor = 0x045e;
0273 input->id.product = 0x0001;
0274 input->id.version = 0x0000;
0275
0276 error = input_register_device(input);
0277 if (error) {
0278 dev_err(&data->spi->dev,
0279 "Failed to register input device: %d", error);
0280 return error;
0281 }
0282
0283 return 0;
0284 }
0285
0286 static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
0287 {
0288 struct input_dev *input;
0289 int error;
0290
0291 input = devm_input_allocate_device(&data->spi->dev);
0292 if (!input)
0293 return -ENOMEM;
0294
0295 data->pen_input_dev = input;
0296 data->pen_tool = BTN_TOOL_PEN;
0297
0298 __set_bit(INPUT_PROP_DIRECT, input->propbit);
0299 __set_bit(INPUT_PROP_POINTER, input->propbit);
0300 input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
0301 input_abs_set_res(input, ABS_X, 40);
0302 input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
0303 input_abs_set_res(input, ABS_Y, 48);
0304 input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
0305 input_set_capability(input, EV_KEY, BTN_TOUCH);
0306 input_set_capability(input, EV_KEY, BTN_STYLUS);
0307 input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
0308 input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
0309
0310 input->name = "Surface3 SPI Pen Input";
0311 input->phys = "input/ts";
0312 input->id.bustype = BUS_SPI;
0313 input->id.vendor = 0x045e;
0314 input->id.product = 0x0002;
0315 input->id.version = 0x0000;
0316
0317 error = input_register_device(input);
0318 if (error) {
0319 dev_err(&data->spi->dev,
0320 "Failed to register input device: %d", error);
0321 return error;
0322 }
0323
0324 return 0;
0325 }
0326
0327 static int surface3_spi_probe(struct spi_device *spi)
0328 {
0329 struct surface3_ts_data *data;
0330 int error;
0331
0332
0333 spi->bits_per_word = 8;
0334 spi->mode = SPI_MODE_0;
0335 error = spi_setup(spi);
0336 if (error)
0337 return error;
0338
0339 data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
0340 if (!data)
0341 return -ENOMEM;
0342
0343 data->spi = spi;
0344 spi_set_drvdata(spi, data);
0345
0346 error = surface3_spi_get_gpio_config(data);
0347 if (error)
0348 return error;
0349
0350 surface3_spi_power(data, true);
0351 surface3_spi_power(data, false);
0352 surface3_spi_power(data, true);
0353
0354 error = surface3_spi_create_touch_input(data);
0355 if (error)
0356 return error;
0357
0358 error = surface3_spi_create_pen_input(data);
0359 if (error)
0360 return error;
0361
0362 error = devm_request_threaded_irq(&spi->dev, spi->irq,
0363 NULL, surface3_spi_irq_handler,
0364 IRQF_ONESHOT,
0365 "Surface3-irq", data);
0366 if (error)
0367 return error;
0368
0369 return 0;
0370 }
0371
0372 static int __maybe_unused surface3_spi_suspend(struct device *dev)
0373 {
0374 struct spi_device *spi = to_spi_device(dev);
0375 struct surface3_ts_data *data = spi_get_drvdata(spi);
0376
0377 disable_irq(data->spi->irq);
0378
0379 surface3_spi_power(data, false);
0380
0381 return 0;
0382 }
0383
0384 static int __maybe_unused surface3_spi_resume(struct device *dev)
0385 {
0386 struct spi_device *spi = to_spi_device(dev);
0387 struct surface3_ts_data *data = spi_get_drvdata(spi);
0388
0389 surface3_spi_power(data, true);
0390
0391 enable_irq(data->spi->irq);
0392
0393 return 0;
0394 }
0395
0396 static SIMPLE_DEV_PM_OPS(surface3_spi_pm_ops,
0397 surface3_spi_suspend,
0398 surface3_spi_resume);
0399
0400 #ifdef CONFIG_ACPI
0401 static const struct acpi_device_id surface3_spi_acpi_match[] = {
0402 { "MSHW0037", 0 },
0403 { }
0404 };
0405 MODULE_DEVICE_TABLE(acpi, surface3_spi_acpi_match);
0406 #endif
0407
0408 static struct spi_driver surface3_spi_driver = {
0409 .driver = {
0410 .name = "Surface3-spi",
0411 .acpi_match_table = ACPI_PTR(surface3_spi_acpi_match),
0412 .pm = &surface3_spi_pm_ops,
0413 },
0414 .probe = surface3_spi_probe,
0415 };
0416
0417 module_spi_driver(surface3_spi_driver);
0418
0419 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
0420 MODULE_DESCRIPTION("Surface 3 SPI touchscreen driver");
0421 MODULE_LICENSE("GPL v2");