1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
273422392SClaudio Nieder /*
373422392SClaudio Nieder * Sahara TouchIT-213 serial touchscreen driver
473422392SClaudio Nieder *
573422392SClaudio Nieder * Copyright (c) 2007-2008 Claudio Nieder <private@claudio.ch>
673422392SClaudio Nieder *
773422392SClaudio Nieder * Based on Touchright driver (drivers/input/touchscreen/touchright.c)
873422392SClaudio Nieder * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
973422392SClaudio Nieder * Copyright (c) 2004 Vojtech Pavlik
1073422392SClaudio Nieder * and Dan Streetman <ddstreet@ieee.org>
1173422392SClaudio Nieder */
1273422392SClaudio Nieder
1373422392SClaudio Nieder
1473422392SClaudio Nieder #include <linux/errno.h>
1573422392SClaudio Nieder #include <linux/kernel.h>
1673422392SClaudio Nieder #include <linux/module.h>
1773422392SClaudio Nieder #include <linux/slab.h>
1873422392SClaudio Nieder #include <linux/input.h>
1973422392SClaudio Nieder #include <linux/serio.h>
2073422392SClaudio Nieder
2173422392SClaudio Nieder #define DRIVER_DESC "Sahara TouchIT-213 serial touchscreen driver"
2273422392SClaudio Nieder
2373422392SClaudio Nieder MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
2473422392SClaudio Nieder MODULE_DESCRIPTION(DRIVER_DESC);
2573422392SClaudio Nieder MODULE_LICENSE("GPL");
2673422392SClaudio Nieder
2773422392SClaudio Nieder /*
2873422392SClaudio Nieder * Definitions & global arrays.
2973422392SClaudio Nieder */
3073422392SClaudio Nieder
3173422392SClaudio Nieder /*
3273422392SClaudio Nieder * Data is received through COM1 at 9600bit/s,8bit,no parity in packets
3373422392SClaudio Nieder * of 5 byte each.
3473422392SClaudio Nieder *
3573422392SClaudio Nieder * +--------+ +--------+ +--------+ +--------+ +--------+
3673422392SClaudio Nieder * |1000000p| |0xxxxxxx| |0xxxxxxx| |0yyyyyyy| |0yyyyyyy|
3773422392SClaudio Nieder * +--------+ +--------+ +--------+ +--------+ +--------+
3873422392SClaudio Nieder * MSB LSB MSB LSB
3973422392SClaudio Nieder *
4073422392SClaudio Nieder * The value of p is 1 as long as the screen is touched and 0 when
4173422392SClaudio Nieder * reporting the location where touching stopped, e.g. where the pen was
4273422392SClaudio Nieder * lifted from the screen.
4373422392SClaudio Nieder *
4473422392SClaudio Nieder * When holding the screen in landscape mode as the BIOS text output is
4573422392SClaudio Nieder * presented, x is the horizontal axis with values growing from left to
4673422392SClaudio Nieder * right and y is the vertical axis with values growing from top to
4773422392SClaudio Nieder * bottom.
4873422392SClaudio Nieder *
4973422392SClaudio Nieder * When holding the screen in portrait mode with the Sahara logo in its
5073422392SClaudio Nieder * correct position, x ist the vertical axis with values growing from
5173422392SClaudio Nieder * top to bottom and y is the horizontal axis with values growing from
5273422392SClaudio Nieder * right to left.
5373422392SClaudio Nieder */
5473422392SClaudio Nieder
5573422392SClaudio Nieder #define T213_FORMAT_TOUCH_BIT 0x01
5673422392SClaudio Nieder #define T213_FORMAT_STATUS_BYTE 0x80
5773422392SClaudio Nieder #define T213_FORMAT_STATUS_MASK ~T213_FORMAT_TOUCH_BIT
5873422392SClaudio Nieder
5973422392SClaudio Nieder /*
6073422392SClaudio Nieder * On my Sahara Touch-IT 213 I have observed x values from 0 to 0x7f0
6173422392SClaudio Nieder * and y values from 0x1d to 0x7e9, so the actual measurement is
6273422392SClaudio Nieder * probably done with an 11 bit precision.
6373422392SClaudio Nieder */
6473422392SClaudio Nieder #define T213_MIN_XC 0
6573422392SClaudio Nieder #define T213_MAX_XC 0x07ff
6673422392SClaudio Nieder #define T213_MIN_YC 0
6773422392SClaudio Nieder #define T213_MAX_YC 0x07ff
6873422392SClaudio Nieder
6973422392SClaudio Nieder /*
7073422392SClaudio Nieder * Per-touchscreen data.
7173422392SClaudio Nieder */
7273422392SClaudio Nieder
7373422392SClaudio Nieder struct touchit213 {
7473422392SClaudio Nieder struct input_dev *dev;
7573422392SClaudio Nieder struct serio *serio;
7673422392SClaudio Nieder int idx;
7773422392SClaudio Nieder unsigned char csum;
7873422392SClaudio Nieder unsigned char data[5];
7973422392SClaudio Nieder char phys[32];
8073422392SClaudio Nieder };
8173422392SClaudio Nieder
touchit213_interrupt(struct serio * serio,unsigned char data,unsigned int flags)8273422392SClaudio Nieder static irqreturn_t touchit213_interrupt(struct serio *serio,
8373422392SClaudio Nieder unsigned char data, unsigned int flags)
8473422392SClaudio Nieder {
8573422392SClaudio Nieder struct touchit213 *touchit213 = serio_get_drvdata(serio);
8673422392SClaudio Nieder struct input_dev *dev = touchit213->dev;
8773422392SClaudio Nieder
8873422392SClaudio Nieder touchit213->data[touchit213->idx] = data;
8973422392SClaudio Nieder
9073422392SClaudio Nieder switch (touchit213->idx++) {
9173422392SClaudio Nieder case 0:
9273422392SClaudio Nieder if ((touchit213->data[0] & T213_FORMAT_STATUS_MASK) !=
9373422392SClaudio Nieder T213_FORMAT_STATUS_BYTE) {
9473422392SClaudio Nieder pr_debug("unsynchronized data: 0x%02x\n", data);
9573422392SClaudio Nieder touchit213->idx = 0;
9673422392SClaudio Nieder }
9773422392SClaudio Nieder break;
9873422392SClaudio Nieder
9973422392SClaudio Nieder case 4:
10073422392SClaudio Nieder touchit213->idx = 0;
10173422392SClaudio Nieder input_report_abs(dev, ABS_X,
10273422392SClaudio Nieder (touchit213->data[1] << 7) | touchit213->data[2]);
10373422392SClaudio Nieder input_report_abs(dev, ABS_Y,
10473422392SClaudio Nieder (touchit213->data[3] << 7) | touchit213->data[4]);
10573422392SClaudio Nieder input_report_key(dev, BTN_TOUCH,
10673422392SClaudio Nieder touchit213->data[0] & T213_FORMAT_TOUCH_BIT);
10773422392SClaudio Nieder input_sync(dev);
10873422392SClaudio Nieder break;
10973422392SClaudio Nieder }
11073422392SClaudio Nieder
11173422392SClaudio Nieder return IRQ_HANDLED;
11273422392SClaudio Nieder }
11373422392SClaudio Nieder
11473422392SClaudio Nieder /*
11573422392SClaudio Nieder * touchit213_disconnect() is the opposite of touchit213_connect()
11673422392SClaudio Nieder */
11773422392SClaudio Nieder
touchit213_disconnect(struct serio * serio)11873422392SClaudio Nieder static void touchit213_disconnect(struct serio *serio)
11973422392SClaudio Nieder {
12073422392SClaudio Nieder struct touchit213 *touchit213 = serio_get_drvdata(serio);
12173422392SClaudio Nieder
12273422392SClaudio Nieder input_get_device(touchit213->dev);
12373422392SClaudio Nieder input_unregister_device(touchit213->dev);
12473422392SClaudio Nieder serio_close(serio);
12573422392SClaudio Nieder serio_set_drvdata(serio, NULL);
12673422392SClaudio Nieder input_put_device(touchit213->dev);
12773422392SClaudio Nieder kfree(touchit213);
12873422392SClaudio Nieder }
12973422392SClaudio Nieder
13073422392SClaudio Nieder /*
13173422392SClaudio Nieder * touchit213_connect() is the routine that is called when someone adds a
13273422392SClaudio Nieder * new serio device that supports the Touchright protocol and registers it as
13373422392SClaudio Nieder * an input device.
13473422392SClaudio Nieder */
13573422392SClaudio Nieder
touchit213_connect(struct serio * serio,struct serio_driver * drv)13673422392SClaudio Nieder static int touchit213_connect(struct serio *serio, struct serio_driver *drv)
13773422392SClaudio Nieder {
13873422392SClaudio Nieder struct touchit213 *touchit213;
13973422392SClaudio Nieder struct input_dev *input_dev;
14073422392SClaudio Nieder int err;
14173422392SClaudio Nieder
14273422392SClaudio Nieder touchit213 = kzalloc(sizeof(struct touchit213), GFP_KERNEL);
14373422392SClaudio Nieder input_dev = input_allocate_device();
14473422392SClaudio Nieder if (!touchit213 || !input_dev) {
14573422392SClaudio Nieder err = -ENOMEM;
14673422392SClaudio Nieder goto fail1;
14773422392SClaudio Nieder }
14873422392SClaudio Nieder
14973422392SClaudio Nieder touchit213->serio = serio;
15073422392SClaudio Nieder touchit213->dev = input_dev;
15173422392SClaudio Nieder snprintf(touchit213->phys, sizeof(touchit213->phys),
15273422392SClaudio Nieder "%s/input0", serio->phys);
15373422392SClaudio Nieder
15473422392SClaudio Nieder input_dev->name = "Sahara Touch-iT213 Serial TouchScreen";
15573422392SClaudio Nieder input_dev->phys = touchit213->phys;
15673422392SClaudio Nieder input_dev->id.bustype = BUS_RS232;
15773422392SClaudio Nieder input_dev->id.vendor = SERIO_TOUCHIT213;
15873422392SClaudio Nieder input_dev->id.product = 0;
15973422392SClaudio Nieder input_dev->id.version = 0x0100;
16073422392SClaudio Nieder input_dev->dev.parent = &serio->dev;
16173422392SClaudio Nieder input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
16273422392SClaudio Nieder input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
16373422392SClaudio Nieder input_set_abs_params(touchit213->dev, ABS_X,
16473422392SClaudio Nieder T213_MIN_XC, T213_MAX_XC, 0, 0);
16573422392SClaudio Nieder input_set_abs_params(touchit213->dev, ABS_Y,
16673422392SClaudio Nieder T213_MIN_YC, T213_MAX_YC, 0, 0);
16773422392SClaudio Nieder
16873422392SClaudio Nieder serio_set_drvdata(serio, touchit213);
16973422392SClaudio Nieder
17073422392SClaudio Nieder err = serio_open(serio, drv);
17173422392SClaudio Nieder if (err)
17273422392SClaudio Nieder goto fail2;
17373422392SClaudio Nieder
17473422392SClaudio Nieder err = input_register_device(touchit213->dev);
17573422392SClaudio Nieder if (err)
17673422392SClaudio Nieder goto fail3;
17773422392SClaudio Nieder
17873422392SClaudio Nieder return 0;
17973422392SClaudio Nieder
18073422392SClaudio Nieder fail3: serio_close(serio);
18173422392SClaudio Nieder fail2: serio_set_drvdata(serio, NULL);
18273422392SClaudio Nieder fail1: input_free_device(input_dev);
18373422392SClaudio Nieder kfree(touchit213);
18473422392SClaudio Nieder return err;
18573422392SClaudio Nieder }
18673422392SClaudio Nieder
18773422392SClaudio Nieder /*
18873422392SClaudio Nieder * The serio driver structure.
18973422392SClaudio Nieder */
19073422392SClaudio Nieder
191b84704c3SArvind Yadav static const struct serio_device_id touchit213_serio_ids[] = {
19273422392SClaudio Nieder {
19373422392SClaudio Nieder .type = SERIO_RS232,
19473422392SClaudio Nieder .proto = SERIO_TOUCHIT213,
19573422392SClaudio Nieder .id = SERIO_ANY,
19673422392SClaudio Nieder .extra = SERIO_ANY,
19773422392SClaudio Nieder },
19873422392SClaudio Nieder { 0 }
19973422392SClaudio Nieder };
20073422392SClaudio Nieder
20173422392SClaudio Nieder MODULE_DEVICE_TABLE(serio, touchit213_serio_ids);
20273422392SClaudio Nieder
20373422392SClaudio Nieder static struct serio_driver touchit213_drv = {
20473422392SClaudio Nieder .driver = {
20573422392SClaudio Nieder .name = "touchit213",
20673422392SClaudio Nieder },
20773422392SClaudio Nieder .description = DRIVER_DESC,
20873422392SClaudio Nieder .id_table = touchit213_serio_ids,
20973422392SClaudio Nieder .interrupt = touchit213_interrupt,
21073422392SClaudio Nieder .connect = touchit213_connect,
21173422392SClaudio Nieder .disconnect = touchit213_disconnect,
21273422392SClaudio Nieder };
21373422392SClaudio Nieder
21465ac9f7aSAxel Lin module_serio_driver(touchit213_drv);
215