2 * $Id: h3600_ts_input.c,v 1.4 2002/01/23 06:39:37 jsimmons Exp $
4 * Copyright (c) 2001 "Crazy" James Simmons jsimmons@transvirtual.com
6 * Sponsored by Transvirtual Technology.
8 * Derived from the code in h3600_ts.[ch] by Charles Flynn
12 * Driver for the h3600 Touch Screen and other Atmel controlled devices.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Should you need to contact me, the author, you can do so by
31 * e-mail - mail your message to <jsimmons@transvirtual.com>.
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/input.h>
39 #include <linux/serio.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
43 /* SA1100 serial defines */
44 #include <asm/arch/hardware.h>
45 #include <asm/arch/irqs.h>
47 #define DRIVER_DESC "H3600 touchscreen driver"
49 MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
50 MODULE_DESCRIPTION(DRIVER_DESC);
51 MODULE_LICENSE("GPL");
54 * Definitions & global arrays.
57 /* The start and end of frame characters SOF and EOF */
60 #define FRAME_OVERHEAD 3 /* CHAR_SOF,CHAR_EOF,LENGTH = 3 */
63 Atmel events and response IDs contained in frame.
64 Programmer has no control over these numbers.
65 TODO there are holes - specifically 1,7,0x0a
67 #define VERSION_ID 0 /* Get Version (request/respose) */
68 #define KEYBD_ID 2 /* Keyboard (event) */
69 #define TOUCHS_ID 3 /* Touch Screen (event)*/
70 #define EEPROM_READ_ID 4 /* (request/response) */
71 #define EEPROM_WRITE_ID 5 /* (request/response) */
72 #define THERMAL_ID 6 /* (request/response) */
73 #define NOTIFY_LED_ID 8 /* (request/response) */
74 #define BATTERY_ID 9 /* (request/response) */
75 #define SPI_READ_ID 0x0b /* ( request/response) */
76 #define SPI_WRITE_ID 0x0c /* ( request/response) */
77 #define FLITE_ID 0x0d /* backlight ( request/response) */
78 #define STX_ID 0xa1 /* extension pack status (req/resp) */
82 #define H3600_MAX_LENGTH 16
85 #define H3600_SCANCODE_RECORD 1 /* 1 -> record button */
86 #define H3600_SCANCODE_CALENDAR 2 /* 2 -> calendar */
87 #define H3600_SCANCODE_CONTACTS 3 /* 3 -> contact */
88 #define H3600_SCANCODE_Q 4 /* 4 -> Q button */
89 #define H3600_SCANCODE_START 5 /* 5 -> start menu */
90 #define H3600_SCANCODE_UP 6 /* 6 -> up */
91 #define H3600_SCANCODE_RIGHT 7 /* 7 -> right */
92 #define H3600_SCANCODE_LEFT 8 /* 8 -> left */
93 #define H3600_SCANCODE_DOWN 9 /* 9 -> down */
96 * Per-touchscreen data.
99 struct input_dev *dev;
101 unsigned char event; /* event ID from packet */
102 unsigned char chksum;
105 unsigned char buf[H3600_MAX_LENGTH];
109 static irqreturn_t action_button_handler(int irq, void *dev_id)
111 int down = (GPLR & GPIO_BITSY_ACTION_BUTTON) ? 0 : 1;
112 struct input_dev *dev = (struct input_dev *) dev_id;
114 input_report_key(dev, KEY_ENTER, down);
120 static irqreturn_t npower_button_handler(int irq, void *dev_id)
122 int down = (GPLR & GPIO_BITSY_NPOWER_BUTTON) ? 0 : 1;
123 struct input_dev *dev = (struct input_dev *) dev_id;
126 * This interrupt is only called when we release the key. So we have
127 * to fake a key press.
129 input_report_key(dev, KEY_SUSPEND, 1);
130 input_report_key(dev, KEY_SUSPEND, down);
138 static int flite_brightness = 25;
146 * h3600_flite_power: enables or disables power to frontlight, using last bright */
147 unsigned int h3600_flite_power(struct input_dev *dev, enum flite_pwr pwr)
149 unsigned char brightness = (pwr == FLITE_PWR_OFF) ? 0 : flite_brightness;
150 struct h3600_dev *ts = input_get_drvdata(dev);
152 /* Must be in this order */
153 ts->serio->write(ts->serio, 1);
154 ts->serio->write(ts->serio, pwr);
155 ts->serio->write(ts->serio, brightness);
162 * This function translates the native event packets to linux input event
163 * packets. Some packets coming from serial are not touchscreen related. In
164 * this case we send them off to be processed elsewhere.
166 static void h3600ts_process_packet(struct h3600_dev *ts)
168 struct input_dev *dev = ts->dev;
169 static int touched = 0;
174 Buttons - returned as a single byte
178 S switch state ( 0=pressed 1=released)
180 NNNN switch number 0-15
182 Note: This is true for non interrupt generated key events.
185 down = (ts->buf[0] & 0x80) ? 0 : 1;
187 switch (ts->buf[0] & 0x7f) {
188 case H3600_SCANCODE_RECORD:
191 case H3600_SCANCODE_CALENDAR:
194 case H3600_SCANCODE_CONTACTS:
197 case H3600_SCANCODE_Q:
200 case H3600_SCANCODE_START:
203 case H3600_SCANCODE_UP:
206 case H3600_SCANCODE_RIGHT:
209 case H3600_SCANCODE_LEFT:
212 case H3600_SCANCODE_DOWN:
219 input_report_key(dev, key, down);
222 * Native touchscreen event data is formatted as shown below:-
224 * +-------+-------+-------+-------+
225 * | Xmsb | Xlsb | Ymsb | Ylsb |
226 * +-------+-------+-------+-------+
231 input_report_key(dev, BTN_TOUCH, 1);
238 x = ts->buf[0]; x <<= 8; x += ts->buf[1];
239 y = ts->buf[2]; y <<= 8; y += ts->buf[3];
241 input_report_abs(dev, ABS_X, x);
242 input_report_abs(dev, ABS_Y, y);
244 input_report_key(dev, BTN_TOUCH, 0);
249 /* Send a non input event elsewhere */
257 * h3600ts_event() handles events from the input module.
259 static int h3600ts_event(struct input_dev *dev, unsigned int type,
260 unsigned int code, int value)
263 struct h3600_dev *ts = input_get_drvdata(dev);
267 // ts->serio->write(ts->serio, SOME_CMD);
279 +-------+---------------+---------------+--=------------+
280 |SOF |id |len | len bytes | Chksum |
281 +-------+---------------+---------------+--=------------+
282 bit 0 7 8 11 12 15 16
284 +-------+---------------+-------+
285 |SOF |id |0 |Chksum | - Note Chksum does not include SOF
286 +-------+---------------+-------+
287 bit 0 7 8 11 12 15 16
294 #define STATE_SOF 0 /* start of FRAME */
295 #define STATE_ID 1 /* state where we decode the ID & len */
296 #define STATE_DATA 2 /* state where we decode data */
297 #define STATE_EOF 3 /* state where we decode checksum or EOF */
299 static irqreturn_t h3600ts_interrupt(struct serio *serio, unsigned char data,
302 struct h3600_dev *ts = serio_get_drvdata(serio);
305 * We have a new frame coming in.
309 if (data == CHAR_SOF)
313 ts->event = (data & 0xf0) >> 4;
314 ts->len = (data & 0xf);
316 if (ts->event >= MAX_ID) {
321 state = (ts->len > 0) ? STATE_DATA : STATE_EOF;
325 ts->buf[ts->idx]= data;
326 if (++ts->idx == ts->len)
331 if (data == CHAR_EOF || data == ts->chksum)
332 h3600ts_process_packet(ts);
343 * h3600ts_connect() is the routine that is called when someone adds a
344 * new serio device that supports H3600 protocol and registers it as
347 static int h3600ts_connect(struct serio *serio, struct serio_driver *drv)
349 struct h3600_dev *ts;
350 struct input_dev *input_dev;
353 ts = kzalloc(sizeof(struct h3600_dev), GFP_KERNEL);
354 input_dev = input_allocate_device();
355 if (!ts || !input_dev) {
362 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", serio->phys);
364 input_dev->name = "H3600 TouchScreen";
365 input_dev->phys = ts->phys;
366 input_dev->id.bustype = BUS_RS232;
367 input_dev->id.vendor = SERIO_H3600;
368 input_dev->id.product = 0x0666; /* FIXME !!! We can ask the hardware */
369 input_dev->id.version = 0x0100;
370 input_dev->dev.parent = &serio->dev;
372 input_set_drvdata(input_dev, ts);
374 input_dev->event = h3600ts_event;
376 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) |
377 BIT_MASK(EV_LED) | BIT_MASK(EV_PWR);
378 input_dev->ledbit[0] = BIT_MASK(LED_SLEEP);
379 input_set_abs_params(input_dev, ABS_X, 60, 985, 0, 0);
380 input_set_abs_params(input_dev, ABS_Y, 35, 1024, 0, 0);
382 set_bit(KEY_RECORD, input_dev->keybit);
383 set_bit(KEY_Q, input_dev->keybit);
384 set_bit(KEY_PROG1, input_dev->keybit);
385 set_bit(KEY_PROG2, input_dev->keybit);
386 set_bit(KEY_PROG3, input_dev->keybit);
387 set_bit(KEY_UP, input_dev->keybit);
388 set_bit(KEY_RIGHT, input_dev->keybit);
389 set_bit(KEY_LEFT, input_dev->keybit);
390 set_bit(KEY_DOWN, input_dev->keybit);
391 set_bit(KEY_ENTER, input_dev->keybit);
392 set_bit(KEY_SUSPEND, input_dev->keybit);
393 set_bit(BTN_TOUCH, input_dev->keybit);
395 /* Device specific stuff */
396 set_GPIO_IRQ_edge(GPIO_BITSY_ACTION_BUTTON, GPIO_BOTH_EDGES);
397 set_GPIO_IRQ_edge(GPIO_BITSY_NPOWER_BUTTON, GPIO_RISING_EDGE);
399 if (request_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, action_button_handler,
400 IRQF_SHARED | IRQF_DISABLED, "h3600_action", &ts->dev)) {
401 printk(KERN_ERR "h3600ts.c: Could not allocate Action Button IRQ!\n");
406 if (request_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, npower_button_handler,
407 IRQF_SHARED | IRQF_DISABLED, "h3600_suspend", &ts->dev)) {
408 printk(KERN_ERR "h3600ts.c: Could not allocate Power Button IRQ!\n");
413 serio_set_drvdata(serio, ts);
415 err = serio_open(serio, drv);
419 //h3600_flite_control(1, 25); /* default brightness */
420 input_register_device(ts->dev);
424 fail3: free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, ts->dev);
425 fail2: free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, ts->dev);
426 fail1: serio_set_drvdata(serio, NULL);
427 input_free_device(input_dev);
433 * h3600ts_disconnect() is the opposite of h3600ts_connect()
436 static void h3600ts_disconnect(struct serio *serio)
438 struct h3600_dev *ts = serio_get_drvdata(serio);
440 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
441 free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, &ts->dev);
442 input_get_device(ts->dev);
443 input_unregister_device(ts->dev);
445 serio_set_drvdata(serio, NULL);
446 input_put_device(ts->dev);
451 * The serio driver structure.
454 static struct serio_device_id h3600ts_serio_ids[] = {
457 .proto = SERIO_H3600,
464 MODULE_DEVICE_TABLE(serio, h3600ts_serio_ids);
466 static struct serio_driver h3600ts_drv = {
470 .description = DRIVER_DESC,
471 .id_table = h3600ts_serio_ids,
472 .interrupt = h3600ts_interrupt,
473 .connect = h3600ts_connect,
474 .disconnect = h3600ts_disconnect,
478 * The functions for inserting/removing us as a module.
481 static int __init h3600ts_init(void)
483 return serio_register_driver(&h3600ts_drv);
486 static void __exit h3600ts_exit(void)
488 serio_unregister_driver(&h3600ts_drv);
491 module_init(h3600ts_init);
492 module_exit(h3600ts_exit);