^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Sahara TouchIT-213 serial touchscreen driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2007-2008 Claudio Nieder <private@claudio.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on Touchright driver (drivers/input/touchscreen/touchright.c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (c) 2004 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * and Dan Streetman <ddstreet@ieee.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DRIVER_DESC "Sahara TouchIT-213 serial touchscreen driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Definitions & global arrays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Data is received through COM1 at 9600bit/s,8bit,no parity in packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * of 5 byte each.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * +--------+ +--------+ +--------+ +--------+ +--------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * |1000000p| |0xxxxxxx| |0xxxxxxx| |0yyyyyyy| |0yyyyyyy|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * +--------+ +--------+ +--------+ +--------+ +--------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * MSB LSB MSB LSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * The value of p is 1 as long as the screen is touched and 0 when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * reporting the location where touching stopped, e.g. where the pen was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * lifted from the screen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * When holding the screen in landscape mode as the BIOS text output is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * presented, x is the horizontal axis with values growing from left to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * right and y is the vertical axis with values growing from top to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * bottom.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * When holding the screen in portrait mode with the Sahara logo in its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * correct position, x ist the vertical axis with values growing from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * top to bottom and y is the horizontal axis with values growing from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * right to left.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define T213_FORMAT_TOUCH_BIT 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define T213_FORMAT_STATUS_BYTE 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define T213_FORMAT_STATUS_MASK ~T213_FORMAT_TOUCH_BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * On my Sahara Touch-IT 213 I have observed x values from 0 to 0x7f0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * and y values from 0x1d to 0x7e9, so the actual measurement is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * probably done with an 11 bit precision.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define T213_MIN_XC 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define T213_MAX_XC 0x07ff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define T213_MIN_YC 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define T213_MAX_YC 0x07ff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Per-touchscreen data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct touchit213 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct serio *serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned char csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned char data[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static irqreturn_t touchit213_interrupt(struct serio *serio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned char data, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct touchit213 *touchit213 = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct input_dev *dev = touchit213->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) touchit213->data[touchit213->idx] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) switch (touchit213->idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if ((touchit213->data[0] & T213_FORMAT_STATUS_MASK) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) T213_FORMAT_STATUS_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_debug("unsynchronized data: 0x%02x\n", data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) touchit213->idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) touchit213->idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) input_report_abs(dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) (touchit213->data[1] << 7) | touchit213->data[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) input_report_abs(dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) (touchit213->data[3] << 7) | touchit213->data[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) input_report_key(dev, BTN_TOUCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) touchit213->data[0] & T213_FORMAT_TOUCH_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * touchit213_disconnect() is the opposite of touchit213_connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void touchit213_disconnect(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct touchit213 *touchit213 = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) input_get_device(touchit213->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) input_unregister_device(touchit213->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) input_put_device(touchit213->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) kfree(touchit213);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * touchit213_connect() is the routine that is called when someone adds a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * new serio device that supports the Touchright protocol and registers it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * an input device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int touchit213_connect(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct touchit213 *touchit213;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) touchit213 = kzalloc(sizeof(struct touchit213), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (!touchit213 || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) touchit213->serio = serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) touchit213->dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) snprintf(touchit213->phys, sizeof(touchit213->phys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) "%s/input0", serio->phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) input_dev->name = "Sahara Touch-iT213 Serial TouchScreen";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) input_dev->phys = touchit213->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) input_dev->id.bustype = BUS_RS232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) input_dev->id.vendor = SERIO_TOUCHIT213;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) input_dev->id.product = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) input_dev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) input_dev->dev.parent = &serio->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) input_set_abs_params(touchit213->dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) T213_MIN_XC, T213_MAX_XC, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) input_set_abs_params(touchit213->dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) T213_MIN_YC, T213_MAX_YC, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) serio_set_drvdata(serio, touchit213);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) err = serio_open(serio, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) err = input_register_device(touchit213->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) fail3: serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) fail2: serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) fail1: input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) kfree(touchit213);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * The serio driver structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static const struct serio_device_id touchit213_serio_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .type = SERIO_RS232,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .proto = SERIO_TOUCHIT213,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .id = SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .extra = SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) { 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) MODULE_DEVICE_TABLE(serio, touchit213_serio_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static struct serio_driver touchit213_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .name = "touchit213",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .description = DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .id_table = touchit213_serio_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) .interrupt = touchit213_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) .connect = touchit213_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) .disconnect = touchit213_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) module_serio_driver(touchit213_drv);