vserver 1.9.5.x5
[linux-2.6.git] / drivers / input / touchscreen / h3600_ts_input.c
1 /*
2  * $Id: h3600_ts_input.c,v 1.4 2002/01/23 06:39:37 jsimmons Exp $
3  *
4  *  Copyright (c) 2001 "Crazy" James Simmons jsimmons@transvirtual.com
5  *
6  *  Sponsored by Transvirtual Technology.
7  *
8  *  Derived from the code in h3600_ts.[ch] by Charles Flynn
9  */
10
11 /*
12  * Driver for the h3600 Touch Screen and other Atmel controlled devices.
13  */
14
15 /*
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.
20  *
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.
25  *
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
29  *
30  * Should you need to contact me, the author, you can do so by
31  * e-mail - mail your message to <jsimmons@transvirtual.com>.
32  */
33
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>
42 #include <linux/pm.h>
43
44 /* SA1100 serial defines */
45 #include <asm/arch/hardware.h>
46 #include <asm/arch/irqs.h>
47
48 #define DRIVER_DESC     "H3600 touchscreen driver"
49
50 MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
51 MODULE_DESCRIPTION(DRIVER_DESC);
52 MODULE_LICENSE("GPL");
53
54 /*
55  * Definitions & global arrays.
56  */
57
58 /* The start and end of frame characters SOF and EOF */
59 #define CHAR_SOF                0x02
60 #define CHAR_EOF                0x03
61 #define FRAME_OVERHEAD          3       /* CHAR_SOF,CHAR_EOF,LENGTH = 3 */
62
63 /*
64         Atmel events and response IDs contained in frame.
65         Programmer has no control over these numbers.
66         TODO there are holes - specifically  1,7,0x0a
67 */
68 #define VERSION_ID              0       /* Get Version (request/respose) */
69 #define KEYBD_ID                2       /* Keyboard (event) */
70 #define TOUCHS_ID               3       /* Touch Screen (event)*/
71 #define EEPROM_READ_ID          4       /* (request/response) */
72 #define EEPROM_WRITE_ID         5       /* (request/response) */
73 #define THERMAL_ID              6       /* (request/response) */
74 #define NOTIFY_LED_ID           8       /* (request/response) */
75 #define BATTERY_ID              9       /* (request/response) */
76 #define SPI_READ_ID             0x0b    /* ( request/response) */
77 #define SPI_WRITE_ID            0x0c    /* ( request/response) */
78 #define FLITE_ID                0x0d    /* backlight ( request/response) */
79 #define STX_ID                  0xa1    /* extension pack status (req/resp) */
80
81 #define MAX_ID                  14
82
83 #define H3600_MAX_LENGTH 16
84 #define H3600_KEY 0xf
85
86 #define H3600_SCANCODE_RECORD   1        /* 1 -> record button */
87 #define H3600_SCANCODE_CALENDAR 2        /* 2 -> calendar */
88 #define H3600_SCANCODE_CONTACTS 3        /* 3 -> contact */
89 #define H3600_SCANCODE_Q        4        /* 4 -> Q button */
90 #define H3600_SCANCODE_START    5        /* 5 -> start menu */
91 #define H3600_SCANCODE_UP       6        /* 6 -> up */
92 #define H3600_SCANCODE_RIGHT    7        /* 7 -> right */
93 #define H3600_SCANCODE_LEFT     8        /* 8 -> left */
94 #define H3600_SCANCODE_DOWN     9        /* 9 -> down */
95
96 static char *h3600_name = "H3600 TouchScreen";
97
98 /*
99  * Per-touchscreen data.
100  */
101 struct h3600_dev {
102         struct input_dev dev;
103         struct pm_dev *pm_dev;
104         struct serio *serio;
105         unsigned char event;    /* event ID from packet */
106         unsigned char chksum;
107         unsigned char len;
108         unsigned char idx;
109         unsigned char buf[H3600_MAX_LENGTH];
110         char phys[32];
111 };
112
113 static irqreturn_t action_button_handler(int irq, void *dev_id, struct pt_regs *regs)
114 {
115         int down = (GPLR & GPIO_BITSY_ACTION_BUTTON) ? 0 : 1;
116         struct input_dev *dev = (struct input_dev *) dev_id;
117
118         input_regs(dev, regs);
119         input_report_key(dev, KEY_ENTER, down);
120         input_sync(dev);
121
122         return IRQ_HANDLED;
123 }
124
125 static irqreturn_t npower_button_handler(int irq, void *dev_id, struct pt_regs *regs)
126 {
127         int down = (GPLR & GPIO_BITSY_NPOWER_BUTTON) ? 0 : 1;
128         struct input_dev *dev = (struct input_dev *) dev_id;
129
130         /*
131          * This interrupt is only called when we release the key. So we have
132          * to fake a key press.
133          */
134         input_regs(dev, regs);
135         input_report_key(dev, KEY_SUSPEND, 1);
136         input_report_key(dev, KEY_SUSPEND, down);
137         input_sync(dev);
138
139         return IRQ_HANDLED;
140 }
141
142 #ifdef CONFIG_PM
143
144 static int flite_brightness = 25;
145
146 enum flite_pwr {
147         FLITE_PWR_OFF = 0,
148         FLITE_PWR_ON = 1
149 };
150
151 /*
152  * h3600_flite_power: enables or disables power to frontlight, using last bright */
153 unsigned int h3600_flite_power(struct input_dev *dev, enum flite_pwr pwr)
154 {
155         unsigned char brightness = (pwr == FLITE_PWR_OFF) ? 0 : flite_brightness;
156         struct h3600_dev *ts = dev->private;
157
158         /* Must be in this order */
159         ts->serio->write(ts->serio, 1);
160         ts->serio->write(ts->serio, pwr);
161         ts->serio->write(ts->serio, brightness);
162         return 0;
163 }
164
165 static int suspended = 0;
166 static int h3600ts_pm_callback(struct pm_dev *pm_dev, pm_request_t req,
167                                 void *data)
168 {
169         struct input_dev *dev = (struct input_dev *) data;
170
171         switch (req) {
172         case PM_SUSPEND: /* enter D1-D3 */
173                 suspended = 1;
174                 h3600_flite_power(dev, FLITE_PWR_OFF);
175                 break;
176         case PM_BLANK:
177                 if (!suspended)
178                         h3600_flite_power(dev, FLITE_PWR_OFF);
179                 break;
180         case PM_RESUME:  /* enter D0 */
181                 /* same as unblank */
182         case PM_UNBLANK:
183                 if (suspended) {
184                         //initSerial();
185                         suspended = 0;
186                 }
187                 h3600_flite_power(dev, FLITE_PWR_ON);
188                 break;
189         }
190         return 0;
191 }
192 #endif
193
194 /*
195  * This function translates the native event packets to linux input event
196  * packets. Some packets coming from serial are not touchscreen related. In
197  * this case we send them off to be processed elsewhere.
198  */
199 static void h3600ts_process_packet(struct h3600_dev *ts, struct pt_regs *regs)
200 {
201         struct input_dev *dev = &ts->dev;
202         static int touched = 0;
203         int key, down = 0;
204
205         input_regs(dev, regs);
206
207         switch (ts->event) {
208                 /*
209                    Buttons - returned as a single byte
210                         7 6 5 4 3 2 1 0
211                         S x x x N N N N
212
213                    S       switch state ( 0=pressed 1=released)
214                    x       Unused.
215                    NNNN    switch number 0-15
216
217                 Note: This is true for non interrupt generated key events.
218                 */
219                 case KEYBD_ID:
220                         down = (ts->buf[0] & 0x80) ? 0 : 1;
221
222                         switch (ts->buf[0] & 0x7f) {
223                                 case H3600_SCANCODE_RECORD:
224                                         key = KEY_RECORD;
225                                         break;
226                                 case H3600_SCANCODE_CALENDAR:
227                                         key = KEY_PROG1;
228                                         break;
229                                 case H3600_SCANCODE_CONTACTS:
230                                         key = KEY_PROG2;
231                                         break;
232                                 case H3600_SCANCODE_Q:
233                                         key = KEY_Q;
234                                         break;
235                                 case H3600_SCANCODE_START:
236                                         key = KEY_PROG3;
237                                         break;
238                                 case H3600_SCANCODE_UP:
239                                         key = KEY_UP;
240                                         break;
241                                 case H3600_SCANCODE_RIGHT:
242                                         key = KEY_RIGHT;
243                                         break;
244                                 case H3600_SCANCODE_LEFT:
245                                         key = KEY_LEFT;
246                                         break;
247                                 case H3600_SCANCODE_DOWN:
248                                         key = KEY_DOWN;
249                                         break;
250                                 default:
251                                         key = 0;
252                         }
253                         if (key)
254                                 input_report_key(dev, key, down);
255                         break;
256                 /*
257                  * Native touchscreen event data is formatted as shown below:-
258                  *
259                  *      +-------+-------+-------+-------+
260                  *      | Xmsb  | Xlsb  | Ymsb  | Ylsb  |
261                  *      +-------+-------+-------+-------+
262                  *       byte 0    1       2       3
263                  */
264                 case TOUCHS_ID:
265                         if (!touched) {
266                                 input_report_key(dev, BTN_TOUCH, 1);
267                                 touched = 1;
268                         }
269
270                         if (ts->len) {
271                                 unsigned short x, y;
272
273                                 x = ts->buf[0]; x <<= 8; x += ts->buf[1];
274                                 y = ts->buf[2]; y <<= 8; y += ts->buf[3];
275
276                                 input_report_abs(dev, ABS_X, x);
277                                 input_report_abs(dev, ABS_Y, y);
278                         } else {
279                                 input_report_key(dev, BTN_TOUCH, 0);
280                                 touched = 0;
281                         }
282                         break;
283                 default:
284                         /* Send a non input event elsewhere */
285                         break;
286         }
287
288         input_sync(dev);
289 }
290
291 /*
292  * h3600ts_event() handles events from the input module.
293  */
294 static int h3600ts_event(struct input_dev *dev, unsigned int type,
295                          unsigned int code, int value)
296 {
297         struct h3600_dev *ts = dev->private;
298
299         switch (type) {
300                 case EV_LED: {
301                 //      ts->serio->write(ts->serio, SOME_CMD);
302                         return 0;
303                 }
304         }
305         return -1;
306 }
307
308 /*
309         Frame format
310   byte    1       2               3              len + 4
311         +-------+---------------+---------------+--=------------+
312         |SOF    |id     |len    | len bytes     | Chksum        |
313         +-------+---------------+---------------+--=------------+
314   bit   0     7  8    11 12   15 16
315
316         +-------+---------------+-------+
317         |SOF    |id     |0      |Chksum | - Note Chksum does not include SOF
318         +-------+---------------+-------+
319   bit   0     7  8    11 12   15 16
320
321 */
322
323 static int state;
324
325 /* decode States  */
326 #define STATE_SOF       0       /* start of FRAME */
327 #define STATE_ID        1       /* state where we decode the ID & len */
328 #define STATE_DATA      2       /* state where we decode data */
329 #define STATE_EOF       3       /* state where we decode checksum or EOF */
330
331 static irqreturn_t h3600ts_interrupt(struct serio *serio, unsigned char data,
332                                      unsigned int flags, struct pt_regs *regs)
333 {
334         struct h3600_dev *ts = serio->private;
335
336         /*
337          * We have a new frame coming in.
338          */
339         switch (state) {
340                 case STATE_SOF:
341                         if (data == CHAR_SOF)
342                                 state = STATE_ID;
343                         break;
344                 case STATE_ID:
345                         ts->event = (data & 0xf0) >> 4;
346                         ts->len = (data & 0xf);
347                         ts->idx = 0;
348                         if (ts->event >= MAX_ID) {
349                                 state = STATE_SOF;
350                                 break;
351                         }
352                         ts->chksum = data;
353                         state = (ts->len > 0) ? STATE_DATA : STATE_EOF;
354                         break;
355                 case STATE_DATA:
356                         ts->chksum += data;
357                         ts->buf[ts->idx]= data;
358                         if(++ts->idx == ts->len)
359                                 state = STATE_EOF;
360                         break;
361                 case STATE_EOF:
362                         state = STATE_SOF;
363                         if (data == CHAR_EOF || data == ts->chksum)
364                                 h3600ts_process_packet(ts, regs);
365                         break;
366                 default:
367                         printk("Error3\n");
368                         break;
369         }
370
371         return IRQ_HANDLED;
372 }
373
374 /*
375  * h3600ts_connect() is the routine that is called when someone adds a
376  * new serio device. It looks whether it was registered as a H3600 touchscreen
377  * and if yes, registers it as an input device.
378  */
379 static void h3600ts_connect(struct serio *serio, struct serio_driver *drv)
380 {
381         struct h3600_dev *ts;
382
383         if (serio->type != (SERIO_RS232 | SERIO_H3600))
384                 return;
385
386         if (!(ts = kmalloc(sizeof(struct h3600_dev), GFP_KERNEL)))
387                 return;
388
389         memset(ts, 0, sizeof(struct h3600_dev));
390
391         init_input_dev(&ts->dev);
392
393         /* Device specific stuff */
394         set_GPIO_IRQ_edge(GPIO_BITSY_ACTION_BUTTON, GPIO_BOTH_EDGES);
395         set_GPIO_IRQ_edge(GPIO_BITSY_NPOWER_BUTTON, GPIO_RISING_EDGE);
396
397         if (request_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, action_button_handler,
398                         SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
399                         "h3600_action", &ts->dev)) {
400                 printk(KERN_ERR "h3600ts.c: Could not allocate Action Button IRQ!\n");
401                 kfree(ts);
402                 return;
403         }
404
405         if (request_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, npower_button_handler,
406                         SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
407                         "h3600_suspend", &ts->dev)) {
408                 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
409                 printk(KERN_ERR "h3600ts.c: Could not allocate Power Button IRQ!\n");
410                 kfree(ts);
411                 return;
412         }
413
414         /* Now we have things going we setup our input device */
415         ts->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_LED) | BIT(EV_PWR);
416         ts->dev.ledbit[0] = BIT(LED_SLEEP);
417         input_set_abs_params(&ts->dev, ABS_X, 60, 985, 0, 0);
418         input_set_abs_params(&ts->dev, ABS_Y, 35, 1024, 0, 0);
419
420         set_bit(KEY_RECORD, ts->dev.keybit);
421         set_bit(KEY_Q, ts->dev.keybit);
422         set_bit(KEY_PROG1, ts->dev.keybit);
423         set_bit(KEY_PROG2, ts->dev.keybit);
424         set_bit(KEY_PROG3, ts->dev.keybit);
425         set_bit(KEY_UP, ts->dev.keybit);
426         set_bit(KEY_RIGHT, ts->dev.keybit);
427         set_bit(KEY_LEFT, ts->dev.keybit);
428         set_bit(KEY_DOWN, ts->dev.keybit);
429         set_bit(KEY_ENTER, ts->dev.keybit);
430         ts->dev.keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
431         ts->dev.keybit[LONG(KEY_SUSPEND)] |= BIT(KEY_SUSPEND);
432
433         ts->serio = serio;
434         serio->private = ts;
435
436         sprintf(ts->phys, "%s/input0", serio->phys);
437
438         ts->dev.event = h3600ts_event;
439         ts->dev.private = ts;
440         ts->dev.name = h3600_name;
441         ts->dev.phys = ts->phys;
442         ts->dev.id.bustype = BUS_RS232;
443         ts->dev.id.vendor = SERIO_H3600;
444         ts->dev.id.product = 0x0666;  /* FIXME !!! We can ask the hardware */
445         ts->dev.id.version = 0x0100;
446
447         if (serio_open(serio, drv)) {
448                 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, ts);
449                 free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, ts);
450                 kfree(ts);
451                 return;
452         }
453
454         //h3600_flite_control(1, 25);     /* default brightness */
455 #ifdef CONFIG_PM
456         ts->pm_dev = pm_register(PM_ILLUMINATION_DEV, PM_SYS_LIGHT,
457                                 h3600ts_pm_callback);
458         printk("registered pm callback\n");
459 #endif
460         input_register_device(&ts->dev);
461
462         printk(KERN_INFO "input: %s on %s\n", h3600_name, serio->phys);
463 }
464
465 /*
466  * h3600ts_disconnect() is the opposite of h3600ts_connect()
467  */
468
469 static void h3600ts_disconnect(struct serio *serio)
470 {
471         struct h3600_dev *ts = serio->private;
472
473         free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
474         free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, &ts->dev);
475         input_unregister_device(&ts->dev);
476         serio_close(serio);
477         kfree(ts);
478 }
479
480 /*
481  * The serio device structure.
482  */
483
484 static struct serio_driver h3600ts_drv = {
485         .driver         = {
486                 .name   = "h3600ts",
487         },
488         .description    = DRIVER_DESC,
489         .interrupt      = h3600ts_interrupt,
490         .connect        = h3600ts_connect,
491         .disconnect     = h3600ts_disconnect,
492 };
493
494 /*
495  * The functions for inserting/removing us as a module.
496  */
497
498 static int __init h3600ts_init(void)
499 {
500         serio_register_driver(&h3600ts_drv);
501         return 0;
502 }
503
504 static void __exit h3600ts_exit(void)
505 {
506         serio_unregister_driver(&h3600ts_drv);
507 }
508
509 module_init(h3600ts_init);
510 module_exit(h3600ts_exit);