RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / input / touchscreen / h3600_ts_input.c
blobd07c8b49c332833fa02d520ac962b096245f5763
1 /*
2 * Copyright (c) 2001 "Crazy" James Simmons jsimmons@transvirtual.com
4 * Sponsored by Transvirtual Technology.
6 * Derived from the code in h3600_ts.[ch] by Charles Flynn
7 */
9 /*
10 * Driver for the h3600 Touch Screen and other Atmel controlled devices.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Should you need to contact me, the author, you can do so by
29 * e-mail - mail your message to <jsimmons@transvirtual.com>.
32 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/slab.h>
36 #include <linux/input.h>
37 #include <linux/serio.h>
38 #include <linux/init.h>
39 #include <linux/delay.h>
41 /* SA1100 serial defines */
42 #include <mach/hardware.h>
43 #include <mach/irqs.h>
45 #define DRIVER_DESC "H3600 touchscreen driver"
47 MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
48 MODULE_DESCRIPTION(DRIVER_DESC);
49 MODULE_LICENSE("GPL");
52 * Definitions & global arrays.
55 /* The start and end of frame characters SOF and EOF */
56 #define CHAR_SOF 0x02
57 #define CHAR_EOF 0x03
58 #define FRAME_OVERHEAD 3 /* CHAR_SOF,CHAR_EOF,LENGTH = 3 */
61 Atmel events and response IDs contained in frame.
62 Programmer has no control over these numbers.
63 TODO there are holes - specifically 1,7,0x0a
65 #define VERSION_ID 0 /* Get Version (request/respose) */
66 #define KEYBD_ID 2 /* Keyboard (event) */
67 #define TOUCHS_ID 3 /* Touch Screen (event)*/
68 #define EEPROM_READ_ID 4 /* (request/response) */
69 #define EEPROM_WRITE_ID 5 /* (request/response) */
70 #define THERMAL_ID 6 /* (request/response) */
71 #define NOTIFY_LED_ID 8 /* (request/response) */
72 #define BATTERY_ID 9 /* (request/response) */
73 #define SPI_READ_ID 0x0b /* ( request/response) */
74 #define SPI_WRITE_ID 0x0c /* ( request/response) */
75 #define FLITE_ID 0x0d /* backlight ( request/response) */
76 #define STX_ID 0xa1 /* extension pack status (req/resp) */
78 #define MAX_ID 14
80 #define H3600_MAX_LENGTH 16
81 #define H3600_KEY 0xf
83 #define H3600_SCANCODE_RECORD 1 /* 1 -> record button */
84 #define H3600_SCANCODE_CALENDAR 2 /* 2 -> calendar */
85 #define H3600_SCANCODE_CONTACTS 3 /* 3 -> contact */
86 #define H3600_SCANCODE_Q 4 /* 4 -> Q button */
87 #define H3600_SCANCODE_START 5 /* 5 -> start menu */
88 #define H3600_SCANCODE_UP 6 /* 6 -> up */
89 #define H3600_SCANCODE_RIGHT 7 /* 7 -> right */
90 #define H3600_SCANCODE_LEFT 8 /* 8 -> left */
91 #define H3600_SCANCODE_DOWN 9 /* 9 -> down */
94 * Per-touchscreen data.
96 struct h3600_dev {
97 struct input_dev *dev;
98 struct serio *serio;
99 unsigned char event; /* event ID from packet */
100 unsigned char chksum;
101 unsigned char len;
102 unsigned char idx;
103 unsigned char buf[H3600_MAX_LENGTH];
104 char phys[32];
107 static irqreturn_t action_button_handler(int irq, void *dev_id)
109 int down = (GPLR & GPIO_BITSY_ACTION_BUTTON) ? 0 : 1;
110 struct input_dev *dev = dev_id;
112 input_report_key(dev, KEY_ENTER, down);
113 input_sync(dev);
115 return IRQ_HANDLED;
118 static irqreturn_t npower_button_handler(int irq, void *dev_id)
120 int down = (GPLR & GPIO_BITSY_NPOWER_BUTTON) ? 0 : 1;
121 struct input_dev *dev = dev_id;
124 * This interrupt is only called when we release the key. So we have
125 * to fake a key press.
127 input_report_key(dev, KEY_SUSPEND, 1);
128 input_report_key(dev, KEY_SUSPEND, down);
129 input_sync(dev);
131 return IRQ_HANDLED;
134 #ifdef CONFIG_PM
136 static int flite_brightness = 25;
138 enum flite_pwr {
139 FLITE_PWR_OFF = 0,
140 FLITE_PWR_ON = 1
144 * h3600_flite_power: enables or disables power to frontlight, using last bright */
145 unsigned int h3600_flite_power(struct input_dev *dev, enum flite_pwr pwr)
147 unsigned char brightness = (pwr == FLITE_PWR_OFF) ? 0 : flite_brightness;
148 struct h3600_dev *ts = input_get_drvdata(dev);
150 /* Must be in this order */
151 serio_write(ts->serio, 1);
152 serio_write(ts->serio, pwr);
153 serio_write(ts->serio, brightness);
155 return 0;
158 #endif
161 * This function translates the native event packets to linux input event
162 * packets. Some packets coming from serial are not touchscreen related. In
163 * this case we send them off to be processed elsewhere.
165 static void h3600ts_process_packet(struct h3600_dev *ts)
167 struct input_dev *dev = ts->dev;
168 static int touched = 0;
169 int key, down = 0;
171 switch (ts->event) {
173 Buttons - returned as a single byte
174 7 6 5 4 3 2 1 0
175 S x x x N N N N
177 S switch state ( 0=pressed 1=released)
178 x Unused.
179 NNNN switch number 0-15
181 Note: This is true for non interrupt generated key events.
183 case KEYBD_ID:
184 down = (ts->buf[0] & 0x80) ? 0 : 1;
186 switch (ts->buf[0] & 0x7f) {
187 case H3600_SCANCODE_RECORD:
188 key = KEY_RECORD;
189 break;
190 case H3600_SCANCODE_CALENDAR:
191 key = KEY_PROG1;
192 break;
193 case H3600_SCANCODE_CONTACTS:
194 key = KEY_PROG2;
195 break;
196 case H3600_SCANCODE_Q:
197 key = KEY_Q;
198 break;
199 case H3600_SCANCODE_START:
200 key = KEY_PROG3;
201 break;
202 case H3600_SCANCODE_UP:
203 key = KEY_UP;
204 break;
205 case H3600_SCANCODE_RIGHT:
206 key = KEY_RIGHT;
207 break;
208 case H3600_SCANCODE_LEFT:
209 key = KEY_LEFT;
210 break;
211 case H3600_SCANCODE_DOWN:
212 key = KEY_DOWN;
213 break;
214 default:
215 key = 0;
217 if (key)
218 input_report_key(dev, key, down);
219 break;
221 * Native touchscreen event data is formatted as shown below:-
223 * +-------+-------+-------+-------+
224 * | Xmsb | Xlsb | Ymsb | Ylsb |
225 * +-------+-------+-------+-------+
226 * byte 0 1 2 3
228 case TOUCHS_ID:
229 if (!touched) {
230 input_report_key(dev, BTN_TOUCH, 1);
231 touched = 1;
234 if (ts->len) {
235 unsigned short x, y;
237 x = ts->buf[0]; x <<= 8; x += ts->buf[1];
238 y = ts->buf[2]; y <<= 8; y += ts->buf[3];
240 input_report_abs(dev, ABS_X, x);
241 input_report_abs(dev, ABS_Y, y);
242 } else {
243 input_report_key(dev, BTN_TOUCH, 0);
244 touched = 0;
246 break;
247 default:
248 /* Send a non input event elsewhere */
249 break;
252 input_sync(dev);
256 * h3600ts_event() handles events from the input module.
258 static int h3600ts_event(struct input_dev *dev, unsigned int type,
259 unsigned int code, int value)
261 return 0;
265 Frame format
266 byte 1 2 3 len + 4
267 +-------+---------------+---------------+--=------------+
268 |SOF |id |len | len bytes | Chksum |
269 +-------+---------------+---------------+--=------------+
270 bit 0 7 8 11 12 15 16
272 +-------+---------------+-------+
273 |SOF |id |0 |Chksum | - Note Chksum does not include SOF
274 +-------+---------------+-------+
275 bit 0 7 8 11 12 15 16
279 static int state;
281 /* decode States */
282 #define STATE_SOF 0 /* start of FRAME */
283 #define STATE_ID 1 /* state where we decode the ID & len */
284 #define STATE_DATA 2 /* state where we decode data */
285 #define STATE_EOF 3 /* state where we decode checksum or EOF */
287 static irqreturn_t h3600ts_interrupt(struct serio *serio, unsigned char data,
288 unsigned int flags)
290 struct h3600_dev *ts = serio_get_drvdata(serio);
293 * We have a new frame coming in.
295 switch (state) {
296 case STATE_SOF:
297 if (data == CHAR_SOF)
298 state = STATE_ID;
299 break;
300 case STATE_ID:
301 ts->event = (data & 0xf0) >> 4;
302 ts->len = (data & 0xf);
303 ts->idx = 0;
304 if (ts->event >= MAX_ID) {
305 state = STATE_SOF;
306 break;
308 ts->chksum = data;
309 state = (ts->len > 0) ? STATE_DATA : STATE_EOF;
310 break;
311 case STATE_DATA:
312 ts->chksum += data;
313 ts->buf[ts->idx]= data;
314 if (++ts->idx == ts->len)
315 state = STATE_EOF;
316 break;
317 case STATE_EOF:
318 state = STATE_SOF;
319 if (data == CHAR_EOF || data == ts->chksum)
320 h3600ts_process_packet(ts);
321 break;
322 default:
323 printk("Error3\n");
324 break;
327 return IRQ_HANDLED;
331 * h3600ts_connect() is the routine that is called when someone adds a
332 * new serio device that supports H3600 protocol and registers it as
333 * an input device.
335 static int h3600ts_connect(struct serio *serio, struct serio_driver *drv)
337 struct h3600_dev *ts;
338 struct input_dev *input_dev;
339 int err;
341 ts = kzalloc(sizeof(struct h3600_dev), GFP_KERNEL);
342 input_dev = input_allocate_device();
343 if (!ts || !input_dev) {
344 err = -ENOMEM;
345 goto fail1;
348 ts->serio = serio;
349 ts->dev = input_dev;
350 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", serio->phys);
352 input_dev->name = "H3600 TouchScreen";
353 input_dev->phys = ts->phys;
354 input_dev->id.bustype = BUS_RS232;
355 input_dev->id.vendor = SERIO_H3600;
356 input_dev->id.product = 0x0666;
357 input_dev->id.version = 0x0100;
358 input_dev->dev.parent = &serio->dev;
360 input_set_drvdata(input_dev, ts);
362 input_dev->event = h3600ts_event;
364 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) |
365 BIT_MASK(EV_LED) | BIT_MASK(EV_PWR);
366 input_dev->ledbit[0] = BIT_MASK(LED_SLEEP);
367 input_set_abs_params(input_dev, ABS_X, 60, 985, 0, 0);
368 input_set_abs_params(input_dev, ABS_Y, 35, 1024, 0, 0);
370 set_bit(KEY_RECORD, input_dev->keybit);
371 set_bit(KEY_Q, input_dev->keybit);
372 set_bit(KEY_PROG1, input_dev->keybit);
373 set_bit(KEY_PROG2, input_dev->keybit);
374 set_bit(KEY_PROG3, input_dev->keybit);
375 set_bit(KEY_UP, input_dev->keybit);
376 set_bit(KEY_RIGHT, input_dev->keybit);
377 set_bit(KEY_LEFT, input_dev->keybit);
378 set_bit(KEY_DOWN, input_dev->keybit);
379 set_bit(KEY_ENTER, input_dev->keybit);
380 set_bit(KEY_SUSPEND, input_dev->keybit);
381 set_bit(BTN_TOUCH, input_dev->keybit);
383 /* Device specific stuff */
384 set_GPIO_IRQ_edge(GPIO_BITSY_ACTION_BUTTON, GPIO_BOTH_EDGES);
385 set_GPIO_IRQ_edge(GPIO_BITSY_NPOWER_BUTTON, GPIO_RISING_EDGE);
387 if (request_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, action_button_handler,
388 IRQF_SHARED | IRQF_DISABLED, "h3600_action", &ts->dev)) {
389 printk(KERN_ERR "h3600ts.c: Could not allocate Action Button IRQ!\n");
390 err = -EBUSY;
391 goto fail2;
394 if (request_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, npower_button_handler,
395 IRQF_SHARED | IRQF_DISABLED, "h3600_suspend", &ts->dev)) {
396 printk(KERN_ERR "h3600ts.c: Could not allocate Power Button IRQ!\n");
397 err = -EBUSY;
398 goto fail3;
401 serio_set_drvdata(serio, ts);
403 err = serio_open(serio, drv);
404 if (err)
405 return err;
407 //h3600_flite_control(1, 25); /* default brightness */
408 input_register_device(ts->dev);
410 return 0;
412 fail3: free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, ts->dev);
413 fail2: free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, ts->dev);
414 fail1: serio_set_drvdata(serio, NULL);
415 input_free_device(input_dev);
416 kfree(ts);
417 return err;
421 * h3600ts_disconnect() is the opposite of h3600ts_connect()
424 static void h3600ts_disconnect(struct serio *serio)
426 struct h3600_dev *ts = serio_get_drvdata(serio);
428 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
429 free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, &ts->dev);
430 input_get_device(ts->dev);
431 input_unregister_device(ts->dev);
432 serio_close(serio);
433 serio_set_drvdata(serio, NULL);
434 input_put_device(ts->dev);
435 kfree(ts);
439 * The serio driver structure.
442 static struct serio_device_id h3600ts_serio_ids[] = {
444 .type = SERIO_RS232,
445 .proto = SERIO_H3600,
446 .id = SERIO_ANY,
447 .extra = SERIO_ANY,
449 { 0 }
452 MODULE_DEVICE_TABLE(serio, h3600ts_serio_ids);
454 static struct serio_driver h3600ts_drv = {
455 .driver = {
456 .name = "h3600ts",
458 .description = DRIVER_DESC,
459 .id_table = h3600ts_serio_ids,
460 .interrupt = h3600ts_interrupt,
461 .connect = h3600ts_connect,
462 .disconnect = h3600ts_disconnect,
466 * The functions for inserting/removing us as a module.
469 static int __init h3600ts_init(void)
471 return serio_register_driver(&h3600ts_drv);
474 static void __exit h3600ts_exit(void)
476 serio_unregister_driver(&h3600ts_drv);
479 module_init(h3600ts_init);
480 module_exit(h3600ts_exit);