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>
44 /* SA1100 serial defines */
45 #include <asm/arch/hardware.h>
46 #include <asm/arch/irqs.h>
49 * Definitions & global arrays.
52 /* The start and end of frame characters SOF and EOF */
55 #define FRAME_OVERHEAD 3 /* CHAR_SOF,CHAR_EOF,LENGTH = 3 */
58 Atmel events and response IDs contained in frame.
59 Programmer has no control over these numbers.
60 TODO there are holes - specifically 1,7,0x0a
62 #define VERSION_ID 0 /* Get Version (request/respose) */
63 #define KEYBD_ID 2 /* Keyboard (event) */
64 #define TOUCHS_ID 3 /* Touch Screen (event)*/
65 #define EEPROM_READ_ID 4 /* (request/response) */
66 #define EEPROM_WRITE_ID 5 /* (request/response) */
67 #define THERMAL_ID 6 /* (request/response) */
68 #define NOTIFY_LED_ID 8 /* (request/response) */
69 #define BATTERY_ID 9 /* (request/response) */
70 #define SPI_READ_ID 0x0b /* ( request/response) */
71 #define SPI_WRITE_ID 0x0c /* ( request/response) */
72 #define FLITE_ID 0x0d /* backlight ( request/response) */
73 #define STX_ID 0xa1 /* extension pack status (req/resp) */
77 #define H3600_MAX_LENGTH 16
80 #define H3600_SCANCODE_RECORD 1 /* 1 -> record button */
81 #define H3600_SCANCODE_CALENDAR 2 /* 2 -> calendar */
82 #define H3600_SCANCODE_CONTACTS 3 /* 3 -> contact */
83 #define H3600_SCANCODE_Q 4 /* 4 -> Q button */
84 #define H3600_SCANCODE_START 5 /* 5 -> start menu */
85 #define H3600_SCANCODE_UP 6 /* 6 -> up */
86 #define H3600_SCANCODE_RIGHT 7 /* 7 -> right */
87 #define H3600_SCANCODE_LEFT 8 /* 8 -> left */
88 #define H3600_SCANCODE_DOWN 9 /* 9 -> down */
90 static char *h3600_name = "H3600 TouchScreen";
93 * Per-touchscreen data.
98 unsigned char event; /* event ID from packet */
102 unsigned char buf[H3600_MAX_LENGTH];
106 static void action_button_handler(int irq, void *dev_id, struct pt_regs *regs)
108 int down = (GPLR & GPIO_BITSY_ACTION_BUTTON) ? 0 : 1;
109 struct input_dev *dev = (struct input_dev *) dev_id;
111 input_regs(dev, regs);
112 input_report_key(dev, KEY_ENTER, down);
116 static void npower_button_handler(int irq, void *dev_id, struct pt_regs *regs)
118 int down = (GPLR & GPIO_BITSY_NPOWER_BUTTON) ? 0 : 1;
119 struct input_dev *dev = (struct input_dev *) dev_id;
122 * This interrupt is only called when we release the key. So we have
123 * to fake a key press.
125 input_regs(dev, regs);
126 input_report_key(dev, KEY_SUSPEND, 1);
127 input_report_key(dev, KEY_SUSPEND, down);
133 static int flite_brightness = 25;
141 * h3600_flite_power: enables or disables power to frontlight, using last bright */
142 unsigned int h3600_flite_power(struct input_dev *dev, enum flite_pwr pwr)
144 unsigned char brightness = ((pwr==FLITE_PWR_OFF) ? 0:flite_brightness);
145 struct h3600_dev *ts = dev->private;
147 /* Must be in this order */
148 ts->serio->write(ts->serio, 1);
149 ts->serio->write(ts->serio, pwr);
150 ts->serio->write(ts->serio, brightness);
154 static int suspended = 0;
155 static int h3600ts_pm_callback(struct pm_dev *pm_dev, pm_request_t req,
158 struct input_dev *dev = (struct input_dev *) data;
161 case PM_SUSPEND: /* enter D1-D3 */
163 h3600_flite_power(dev, FLITE_PWR_OFF);
167 h3600_flite_power(dev, FLITE_PWR_OFF);
169 case PM_RESUME: /* enter D0 */
170 /* same as unblank */
176 h3600_flite_power(dev, FLITE_PWR_ON);
184 * This function translates the native event packets to linux input event
185 * packets. Some packets coming from serial are not touchscreen related. In
186 * this case we send them off to be processed elsewhere.
188 static void h3600ts_process_packet(struct h3600_dev *ts, struct pt_regs *regs)
190 struct input_dev *dev = &ts->dev;
191 static int touched = 0;
194 input_regs(dev, regs);
198 Buttons - returned as a single byte
202 S switch state ( 0=pressed 1=released)
204 NNNN switch number 0-15
206 Note: This is true for non interrupt generated key events.
209 down = (ts->buf[0] & 0x80) ? 0 : 1;
211 switch (ts->buf[0] & 0x7f) {
212 case H3600_SCANCODE_RECORD:
215 case H3600_SCANCODE_CALENDAR:
218 case H3600_SCANCODE_CONTACTS:
221 case H3600_SCANCODE_Q:
224 case H3600_SCANCODE_START:
227 case H3600_SCANCODE_UP:
230 case H3600_SCANCODE_RIGHT:
233 case H3600_SCANCODE_LEFT:
236 case H3600_SCANCODE_DOWN:
243 input_report_key(dev, key, down);
246 * Native touchscreen event data is formatted as shown below:-
248 * +-------+-------+-------+-------+
249 * | Xmsb | Xlsb | Ymsb | Ylsb |
250 * +-------+-------+-------+-------+
255 input_report_key(dev, BTN_TOUCH, 1);
262 x = ts->buf[0]; x <<= 8; x += ts->buf[1];
263 y = ts->buf[2]; y <<= 8; y += ts->buf[3];
265 input_report_abs(dev, ABS_X, x);
266 input_report_abs(dev, ABS_Y, y);
268 input_report_key(dev, BTN_TOUCH, 0);
273 /* Send a non input event elsewhere */
281 * h3600ts_event() handles events from the input module.
283 static int h3600ts_event(struct input_dev *dev, unsigned int type,
284 unsigned int code, int value)
286 struct h3600_dev *ts = dev->private;
290 // ts->serio->write(ts->serio, SOME_CMD);
300 +-------+---------------+---------------+--=------------+
301 |SOF |id |len | len bytes | Chksum |
302 +-------+---------------+---------------+--=------------+
303 bit 0 7 8 11 12 15 16
305 +-------+---------------+-------+
306 |SOF |id |0 |Chksum | - Note Chksum does not include SOF
307 +-------+---------------+-------+
308 bit 0 7 8 11 12 15 16
315 #define STATE_SOF 0 /* start of FRAME */
316 #define STATE_ID 1 /* state where we decode the ID & len */
317 #define STATE_DATA 2 /* state where we decode data */
318 #define STATE_EOF 3 /* state where we decode checksum or EOF */
320 static void h3600ts_interrupt(struct serio *serio, unsigned char data,
323 struct h3600_dev *ts = serio->private;
326 * We have a new frame coming in.
330 if (data == CHAR_SOF)
334 ts->event = (data & 0xf0) >> 4;
335 ts->len = (data & 0xf);
337 if (ts->event >= MAX_ID) {
342 state=(ts->len > 0 ) ? STATE_DATA : STATE_EOF;
346 ts->buf[ts->idx]= data;
347 if(++ts->idx == ts->len)
352 if (data == CHAR_EOF || data == ts->chksum )
353 h3600ts_process_packet(ts);
362 * h3600ts_connect() is the routine that is called when someone adds a
363 * new serio device. It looks whether it was registered as a H3600 touchscreen
364 * and if yes, registers it as an input device.
366 static void h3600ts_connect(struct serio *serio, struct serio_dev *dev)
368 struct h3600_dev *ts;
370 if (serio->type != (SERIO_RS232 | SERIO_H3600))
373 if (!(ts = kmalloc(sizeof(struct h3600_dev), GFP_KERNEL)))
376 memset(ts, 0, sizeof(struct h3600_dev));
378 init_input_dev(&ts->dev);
380 /* Device specific stuff */
381 set_GPIO_IRQ_edge( GPIO_BITSY_ACTION_BUTTON, GPIO_BOTH_EDGES );
382 set_GPIO_IRQ_edge( GPIO_BITSY_NPOWER_BUTTON, GPIO_RISING_EDGE );
384 if (request_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, action_button_handler,
385 SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
386 "h3600_action", &ts->dev)) {
387 printk(KERN_ERR "h3600ts.c: Could not allocate Action Button IRQ!\n");
392 if (request_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, npower_button_handler,
393 SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
394 "h3600_suspend", &ts->dev)) {
395 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
396 printk(KERN_ERR "h3600ts.c: Could not allocate Power Button IRQ!\n");
400 /* Now we have things going we setup our input device */
401 ts->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_LED) | BIT(EV_PWR);
402 ts->dev.absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
403 ts->dev.ledbit[0] = BIT(LED_SLEEP);
405 ts->dev.absmin[ABS_X] = 60; ts->dev.absmin[ABS_Y] = 35;
406 ts->dev.absmax[ABS_X] = 985; ts->dev.absmax[ABS_Y] = 1024;
407 ts->dev.absfuzz[ABS_X] = 0; ts->dev.absfuzz[ABS_Y] = 0;
412 set_bit(KEY_RECORD, ts->dev.keybit);
413 set_bit(KEY_Q, ts->dev.keybit);
414 set_bit(KEY_PROG1, ts->dev.keybit);
415 set_bit(KEY_PROG2, ts->dev.keybit);
416 set_bit(KEY_PROG3, ts->dev.keybit);
417 set_bit(KEY_UP, ts->dev.keybit);
418 set_bit(KEY_RIGHT, ts->dev.keybit);
419 set_bit(KEY_LEFT, ts->dev.keybit);
420 set_bit(KEY_DOWN, ts->dev.keybit);
421 set_bit(KEY_ENTER, ts->dev.keybit);
422 ts->dev.keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
423 ts->dev.keybit[LONG(KEY_SUSPEND)] |= BIT(KEY_SUSPEND);
425 sprintf(ts->phys, "%s/input0", serio->phys);
427 ts->dev.event = h3600ts_event;
428 ts->dev.private = ts;
429 ts->dev.name = h3600_name;
430 ts->dev.phys = ts->phys;
431 ts->dev.id.bustype = BUS_RS232;
432 ts->dev.id.vendor = SERIO_H3600;
433 ts->dev.id.product = 0x0666; /* FIXME !!! We can ask the hardware */
434 ts->dev.id.version = 0x0100;
436 if (serio_open(serio, dev)) {
437 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, ts);
438 free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, ts);
443 //h3600_flite_control(1, 25); /* default brightness */
445 ts->dev.pm_dev = pm_register(PM_ILLUMINATION_DEV, PM_SYS_LIGHT,
446 h3600ts_pm_callback);
447 printk("registered pm callback\n");
449 input_register_device(&ts->dev);
451 printk(KERN_INFO "input: %s on %s\n", h3600_name, serio->phys);
455 * h3600ts_disconnect() is the opposite of h3600ts_connect()
458 static void h3600ts_disconnect(struct serio *serio)
460 struct h3600_dev *ts = serio->private;
462 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
463 free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, &ts->dev);
464 input_unregister_device(&ts->dev);
470 * The serio device structure.
473 static struct serio_dev h3600ts_dev = {
474 .interrupt = h3600ts_interrupt,
475 .connect = h3600ts_connect,
476 .disconnect = h3600ts_disconnect,
480 * The functions for inserting/removing us as a module.
483 static int __init h3600ts_init(void)
485 serio_register_device(&h3600ts_dev);
489 static void __exit h3600ts_exit(void)
491 serio_unregister_device(&h3600ts_dev);
494 module_init(h3600ts_init);
495 module_exit(h3600ts_exit);