1 // SPDX-License-Identifier: GPL-2.0-only
3 * HTC Shift touchscreen driver
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/input.h>
12 #include <linux/interrupt.h>
14 #include <linux/init.h>
15 #include <linux/irq.h>
16 #include <linux/isa.h>
17 #include <linux/ioport.h>
18 #include <linux/dmi.h>
21 MODULE_DESCRIPTION("HTC Shift touchscreen driver");
22 MODULE_LICENSE("GPL");
24 #define HTCPEN_PORT_IRQ_CLEAR 0x068
25 #define HTCPEN_PORT_INIT 0x06c
26 #define HTCPEN_PORT_INDEX 0x0250
27 #define HTCPEN_PORT_DATA 0x0251
30 #define DEVICE_ENABLE 0xa2
31 #define DEVICE_DISABLE 0xa3
35 #define TOUCH_INDEX 0xb
36 #define LSB_XY_INDEX 0xc
37 #define X_AXIS_MAX 2040
38 #define Y_AXIS_MAX 2040
41 module_param(invert_x, bool, 0644);
42 MODULE_PARM_DESC(invert_x, "If set, X axis is inverted");
44 module_param(invert_y, bool, 0644);
45 MODULE_PARM_DESC(invert_y, "If set, Y axis is inverted");
47 static irqreturn_t htcpen_interrupt(int irq, void *handle)
49 struct input_dev *htcpen_dev = handle;
50 unsigned short x, y, xy;
52 /* 0 = press; 1 = release */
53 outb_p(TOUCH_INDEX, HTCPEN_PORT_INDEX);
55 if (inb_p(HTCPEN_PORT_DATA)) {
56 input_report_key(htcpen_dev, BTN_TOUCH, 0);
58 outb_p(X_INDEX, HTCPEN_PORT_INDEX);
59 x = inb_p(HTCPEN_PORT_DATA);
61 outb_p(Y_INDEX, HTCPEN_PORT_INDEX);
62 y = inb_p(HTCPEN_PORT_DATA);
64 outb_p(LSB_XY_INDEX, HTCPEN_PORT_INDEX);
65 xy = inb_p(HTCPEN_PORT_DATA);
67 /* get high resolution value of X and Y using LSB */
68 x = X_AXIS_MAX - ((x * 8) + ((xy >> 4) & 0xf));
69 y = (y * 8) + (xy & 0xf);
75 if (x != X_AXIS_MAX && x != 0) {
76 input_report_key(htcpen_dev, BTN_TOUCH, 1);
77 input_report_abs(htcpen_dev, ABS_X, x);
78 input_report_abs(htcpen_dev, ABS_Y, y);
82 input_sync(htcpen_dev);
84 inb_p(HTCPEN_PORT_IRQ_CLEAR);
89 static int htcpen_open(struct input_dev *dev)
91 outb_p(DEVICE_ENABLE, HTCPEN_PORT_INIT);
96 static void htcpen_close(struct input_dev *dev)
98 outb_p(DEVICE_DISABLE, HTCPEN_PORT_INIT);
99 synchronize_irq(HTCPEN_IRQ);
102 static int htcpen_isa_probe(struct device *dev, unsigned int id)
104 struct input_dev *htcpen_dev;
107 if (!request_region(HTCPEN_PORT_IRQ_CLEAR, 1, "htcpen")) {
108 printk(KERN_ERR "htcpen: unable to get IO region 0x%x\n",
109 HTCPEN_PORT_IRQ_CLEAR);
110 goto request_region1_failed;
113 if (!request_region(HTCPEN_PORT_INIT, 1, "htcpen")) {
114 printk(KERN_ERR "htcpen: unable to get IO region 0x%x\n",
116 goto request_region2_failed;
119 if (!request_region(HTCPEN_PORT_INDEX, 2, "htcpen")) {
120 printk(KERN_ERR "htcpen: unable to get IO region 0x%x\n",
122 goto request_region3_failed;
125 htcpen_dev = input_allocate_device();
127 printk(KERN_ERR "htcpen: can't allocate device\n");
129 goto input_alloc_failed;
132 htcpen_dev->name = "HTC Shift EC TouchScreen";
133 htcpen_dev->id.bustype = BUS_ISA;
135 htcpen_dev->evbit[0] = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY);
136 htcpen_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
137 input_set_abs_params(htcpen_dev, ABS_X, 0, X_AXIS_MAX, 0, 0);
138 input_set_abs_params(htcpen_dev, ABS_Y, 0, Y_AXIS_MAX, 0, 0);
140 htcpen_dev->open = htcpen_open;
141 htcpen_dev->close = htcpen_close;
143 err = request_irq(HTCPEN_IRQ, htcpen_interrupt, 0, "htcpen",
146 printk(KERN_ERR "htcpen: irq busy\n");
147 goto request_irq_failed;
150 inb_p(HTCPEN_PORT_IRQ_CLEAR);
152 err = input_register_device(htcpen_dev);
154 goto input_register_failed;
156 dev_set_drvdata(dev, htcpen_dev);
160 input_register_failed:
161 free_irq(HTCPEN_IRQ, htcpen_dev);
163 input_free_device(htcpen_dev);
165 release_region(HTCPEN_PORT_INDEX, 2);
166 request_region3_failed:
167 release_region(HTCPEN_PORT_INIT, 1);
168 request_region2_failed:
169 release_region(HTCPEN_PORT_IRQ_CLEAR, 1);
170 request_region1_failed:
174 static void htcpen_isa_remove(struct device *dev, unsigned int id)
176 struct input_dev *htcpen_dev = dev_get_drvdata(dev);
178 input_unregister_device(htcpen_dev);
180 free_irq(HTCPEN_IRQ, htcpen_dev);
182 release_region(HTCPEN_PORT_INDEX, 2);
183 release_region(HTCPEN_PORT_INIT, 1);
184 release_region(HTCPEN_PORT_IRQ_CLEAR, 1);
188 static int htcpen_isa_suspend(struct device *dev, unsigned int n,
191 outb_p(DEVICE_DISABLE, HTCPEN_PORT_INIT);
196 static int htcpen_isa_resume(struct device *dev, unsigned int n)
198 outb_p(DEVICE_ENABLE, HTCPEN_PORT_INIT);
204 static struct isa_driver htcpen_isa_driver = {
205 .probe = htcpen_isa_probe,
206 .remove = htcpen_isa_remove,
208 .suspend = htcpen_isa_suspend,
209 .resume = htcpen_isa_resume,
212 .owner = THIS_MODULE,
217 static const struct dmi_system_id htcshift_dmi_table[] __initconst = {
221 DMI_MATCH(DMI_SYS_VENDOR, "High Tech Computer Corp"),
222 DMI_MATCH(DMI_PRODUCT_NAME, "Shift"),
227 MODULE_DEVICE_TABLE(dmi, htcshift_dmi_table);
229 static int __init htcpen_isa_init(void)
231 if (!dmi_check_system(htcshift_dmi_table))
234 return isa_register_driver(&htcpen_isa_driver, 1);
237 static void __exit htcpen_isa_exit(void)
239 isa_unregister_driver(&htcpen_isa_driver);
242 module_init(htcpen_isa_init);
243 module_exit(htcpen_isa_exit);