^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) * Hampshire 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) 2010 Adam Bennett
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Based on the dynapro driver (c) Tias Guns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * 2010/04/08 Adam Bennett <abennett72@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Copied dynapro.c and edited for Hampshire 4-byte protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define DRIVER_DESC "Hampshire serial touchscreen driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) MODULE_AUTHOR("Adam Bennett <abennett72@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Definitions & global arrays.
^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) #define HAMPSHIRE_FORMAT_TOUCH_BIT 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define HAMPSHIRE_FORMAT_LENGTH 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define HAMPSHIRE_RESPONSE_BEGIN_BYTE 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define HAMPSHIRE_MIN_XC 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define HAMPSHIRE_MAX_XC 0x1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define HAMPSHIRE_MIN_YC 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define HAMPSHIRE_MAX_YC 0x1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define HAMPSHIRE_GET_XC(data) (((data[3] & 0x0c) >> 2) | (data[1] << 2) | ((data[0] & 0x38) << 6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define HAMPSHIRE_GET_YC(data) ((data[3] & 0x03) | (data[2] << 2) | ((data[0] & 0x07) << 9))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define HAMPSHIRE_GET_TOUCHED(data) (HAMPSHIRE_FORMAT_TOUCH_BIT & data[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Per-touchscreen data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct hampshire {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct serio *serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned char data[HAMPSHIRE_FORMAT_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void hampshire_process_data(struct hampshire *phampshire)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct input_dev *dev = phampshire->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (HAMPSHIRE_FORMAT_LENGTH == ++phampshire->idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) input_report_abs(dev, ABS_X, HAMPSHIRE_GET_XC(phampshire->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) input_report_abs(dev, ABS_Y, HAMPSHIRE_GET_YC(phampshire->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) input_report_key(dev, BTN_TOUCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) HAMPSHIRE_GET_TOUCHED(phampshire->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) phampshire->idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static irqreturn_t hampshire_interrupt(struct serio *serio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned char data, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct hampshire *phampshire = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) phampshire->data[phampshire->idx] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (HAMPSHIRE_RESPONSE_BEGIN_BYTE & phampshire->data[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) hampshire_process_data(phampshire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) phampshire->data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static void hampshire_disconnect(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct hampshire *phampshire = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) input_get_device(phampshire->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) input_unregister_device(phampshire->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) input_put_device(phampshire->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) kfree(phampshire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * hampshire_connect() is the routine that is called when someone adds a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * new serio device that supports hampshire protocol and registers it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * an input device. This is usually accomplished using inputattach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int hampshire_connect(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct hampshire *phampshire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) phampshire = kzalloc(sizeof(struct hampshire), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!phampshire || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) phampshire->serio = serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) phampshire->dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) snprintf(phampshire->phys, sizeof(phampshire->phys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) "%s/input0", serio->phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) input_dev->name = "Hampshire Serial TouchScreen";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) input_dev->phys = phampshire->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) input_dev->id.bustype = BUS_RS232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) input_dev->id.vendor = SERIO_HAMPSHIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) input_dev->id.product = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) input_dev->id.version = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) input_dev->dev.parent = &serio->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) input_set_abs_params(phampshire->dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) HAMPSHIRE_MIN_XC, HAMPSHIRE_MAX_XC, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) input_set_abs_params(phampshire->dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) HAMPSHIRE_MIN_YC, HAMPSHIRE_MAX_YC, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) serio_set_drvdata(serio, phampshire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) err = serio_open(serio, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) err = input_register_device(phampshire->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) fail3: serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) fail2: serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) fail1: input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) kfree(phampshire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * The serio driver structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static const struct serio_device_id hampshire_serio_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .type = SERIO_RS232,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .proto = SERIO_HAMPSHIRE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .id = SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .extra = SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) { 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_DEVICE_TABLE(serio, hampshire_serio_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static struct serio_driver hampshire_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .name = "hampshire",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .description = DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .id_table = hampshire_serio_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .interrupt = hampshire_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .connect = hampshire_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .disconnect = hampshire_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) module_serio_driver(hampshire_drv);