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