Merge 'upstream' branch of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6] / 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         struct pm_dev *pm_dev;
106         unsigned char event;    /* event ID from packet */
107         unsigned char chksum;
108         unsigned char len;
109         unsigned char idx;
110         unsigned char buf[H3600_MAX_LENGTH];
111         char phys[32];
112 };
113
114 static irqreturn_t action_button_handler(int irq, void *dev_id, struct pt_regs *regs)
115 {
116         int down = (GPLR & GPIO_BITSY_ACTION_BUTTON) ? 0 : 1;
117         struct input_dev *dev = (struct input_dev *) dev_id;
118
119         input_regs(dev, regs);
120         input_report_key(dev, KEY_ENTER, down);
121         input_sync(dev);
122
123         return IRQ_HANDLED;
124 }
125
126 static irqreturn_t npower_button_handler(int irq, void *dev_id, struct pt_regs *regs)
127 {
128         int down = (GPLR & GPIO_BITSY_NPOWER_BUTTON) ? 0 : 1;
129         struct input_dev *dev = (struct input_dev *) dev_id;
130
131         /*
132          * This interrupt is only called when we release the key. So we have
133          * to fake a key press.
134          */
135         input_regs(dev, regs);
136         input_report_key(dev, KEY_SUSPEND, 1);
137         input_report_key(dev, KEY_SUSPEND, down);
138         input_sync(dev);
139
140         return IRQ_HANDLED;
141 }
142
143 #ifdef CONFIG_PM
144
145 static int flite_brightness = 25;
146
147 enum flite_pwr {
148         FLITE_PWR_OFF = 0,
149         FLITE_PWR_ON = 1
150 };
151
152 /*
153  * h3600_flite_power: enables or disables power to frontlight, using last bright */
154 unsigned int h3600_flite_power(struct input_dev *dev, enum flite_pwr pwr)
155 {
156         unsigned char brightness = (pwr == FLITE_PWR_OFF) ? 0 : flite_brightness;
157         struct h3600_dev *ts = dev->private;
158
159         /* Must be in this order */
160         ts->serio->write(ts->serio, 1);
161         ts->serio->write(ts->serio, pwr);
162         ts->serio->write(ts->serio, brightness);
163         return 0;
164 }
165
166 static int suspended = 0;
167 static int h3600ts_pm_callback(struct pm_dev *pm_dev, pm_request_t req,
168                                 void *data)
169 {
170         struct input_dev *dev = (struct input_dev *) data;
171
172         switch (req) {
173         case PM_SUSPEND: /* enter D1-D3 */
174                 suspended = 1;
175                 h3600_flite_power(dev, FLITE_PWR_OFF);
176                 break;
177         case PM_BLANK:
178                 if (!suspended)
179                         h3600_flite_power(dev, FLITE_PWR_OFF);
180                 break;
181         case PM_RESUME:  /* enter D0 */
182                 /* same as unblank */
183         case PM_UNBLANK:
184                 if (suspended) {
185                         //initSerial();
186                         suspended = 0;
187                 }
188                 h3600_flite_power(dev, FLITE_PWR_ON);
189                 break;
190         }
191         return 0;
192 }
193 #endif
194
195 /*
196  * This function translates the native event packets to linux input event
197  * packets. Some packets coming from serial are not touchscreen related. In
198  * this case we send them off to be processed elsewhere.
199  */
200 static void h3600ts_process_packet(struct h3600_dev *ts, struct pt_regs *regs)
201 {
202         struct input_dev *dev = &ts->dev;
203         static int touched = 0;
204         int key, down = 0;
205
206         input_regs(dev, regs);
207
208         switch (ts->event) {
209                 /*
210                    Buttons - returned as a single byte
211                         7 6 5 4 3 2 1 0
212                         S x x x N N N N
213
214                    S       switch state ( 0=pressed 1=released)
215                    x       Unused.
216                    NNNN    switch number 0-15
217
218                    Note: This is true for non interrupt generated key events.
219                 */
220                 case KEYBD_ID:
221                         down = (ts->buf[0] & 0x80) ? 0 : 1;
222
223                         switch (ts->buf[0] & 0x7f) {
224                                 case H3600_SCANCODE_RECORD:
225                                         key = KEY_RECORD;
226                                         break;
227                                 case H3600_SCANCODE_CALENDAR:
228                                         key = KEY_PROG1;
229                                         break;
230                                 case H3600_SCANCODE_CONTACTS:
231                                         key = KEY_PROG2;
232                                         break;
233                                 case H3600_SCANCODE_Q:
234                                         key = KEY_Q;
235                                         break;
236                                 case H3600_SCANCODE_START:
237                                         key = KEY_PROG3;
238                                         break;
239                                 case H3600_SCANCODE_UP:
240                                         key = KEY_UP;
241                                         break;
242                                 case H3600_SCANCODE_RIGHT:
243                                         key = KEY_RIGHT;
244                                         break;
245                                 case H3600_SCANCODE_LEFT:
246                                         key = KEY_LEFT;
247                                         break;
248                                 case H3600_SCANCODE_DOWN:
249                                         key = KEY_DOWN;
250                                         break;
251                                 default:
252                                         key = 0;
253                         }
254                         if (key)
255                                 input_report_key(dev, key, down);
256                         break;
257                 /*
258                  * Native touchscreen event data is formatted as shown below:-
259                  *
260                  *      +-------+-------+-------+-------+
261                  *      | Xmsb  | Xlsb  | Ymsb  | Ylsb  |
262                  *      +-------+-------+-------+-------+
263                  *       byte 0    1       2       3
264                  */
265                 case TOUCHS_ID:
266                         if (!touched) {
267                                 input_report_key(dev, BTN_TOUCH, 1);
268                                 touched = 1;
269                         }
270
271                         if (ts->len) {
272                                 unsigned short x, y;
273
274                                 x = ts->buf[0]; x <<= 8; x += ts->buf[1];
275                                 y = ts->buf[2]; y <<= 8; y += ts->buf[3];
276
277                                 input_report_abs(dev, ABS_X, x);
278                                 input_report_abs(dev, ABS_Y, y);
279                         } else {
280                                 input_report_key(dev, BTN_TOUCH, 0);
281                                 touched = 0;
282                         }
283                         break;
284                 default:
285                         /* Send a non input event elsewhere */
286                         break;
287         }
288
289         input_sync(dev);
290 }
291
292 /*
293  * h3600ts_event() handles events from the input module.
294  */
295 static int h3600ts_event(struct input_dev *dev, unsigned int type,
296                          unsigned int code, int value)
297 {
298         struct h3600_dev *ts = dev->private;
299
300         switch (type) {
301                 case EV_LED: {
302                 //      ts->serio->write(ts->serio, SOME_CMD);
303                         return 0;
304                 }
305         }
306         return -1;
307 }
308
309 /*
310         Frame format
311   byte    1       2               3              len + 4
312         +-------+---------------+---------------+--=------------+
313         |SOF    |id     |len    | len bytes     | Chksum        |
314         +-------+---------------+---------------+--=------------+
315   bit   0     7  8    11 12   15 16
316
317         +-------+---------------+-------+
318         |SOF    |id     |0      |Chksum | - Note Chksum does not include SOF
319         +-------+---------------+-------+
320   bit   0     7  8    11 12   15 16
321
322 */
323
324 static int state;
325
326 /* decode States  */
327 #define STATE_SOF       0       /* start of FRAME */
328 #define STATE_ID        1       /* state where we decode the ID & len */
329 #define STATE_DATA      2       /* state where we decode data */
330 #define STATE_EOF       3       /* state where we decode checksum or EOF */
331
332 static irqreturn_t h3600ts_interrupt(struct serio *serio, unsigned char data,
333                                      unsigned int flags, struct pt_regs *regs)
334 {
335         struct h3600_dev *ts = serio_get_drvdata(serio);
336
337         /*
338          * We have a new frame coming in.
339          */
340         switch (state) {
341                 case STATE_SOF:
342                         if (data == CHAR_SOF)
343                                 state = STATE_ID;
344                         break;
345                 case STATE_ID:
346                         ts->event = (data & 0xf0) >> 4;
347                         ts->len = (data & 0xf);
348                         ts->idx = 0;
349                         if (ts->event >= MAX_ID) {
350                                 state = STATE_SOF;
351                                 break;
352                         }
353                         ts->chksum = data;
354                         state = (ts->len > 0) ? STATE_DATA : STATE_EOF;
355                         break;
356                 case STATE_DATA:
357                         ts->chksum += data;
358                         ts->buf[ts->idx]= data;
359                         if (++ts->idx == ts->len)
360                                 state = STATE_EOF;
361                         break;
362                 case STATE_EOF:
363                         state = STATE_SOF;
364                         if (data == CHAR_EOF || data == ts->chksum)
365                                 h3600ts_process_packet(ts, regs);
366                         break;
367                 default:
368                         printk("Error3\n");
369                         break;
370         }
371
372         return IRQ_HANDLED;
373 }
374
375 /*
376  * h3600ts_connect() is the routine that is called when someone adds a
377  * new serio device that supports H3600 protocol and registers it as
378  * an input device.
379  */
380 static int h3600ts_connect(struct serio *serio, struct serio_driver *drv)
381 {
382         struct h3600_dev *ts;
383         int err;
384
385         if (!(ts = kmalloc(sizeof(struct h3600_dev), GFP_KERNEL)))
386                 return -ENOMEM;
387
388         memset(ts, 0, sizeof(struct h3600_dev));
389
390         init_input_dev(&ts->dev);
391
392         /* Device specific stuff */
393         set_GPIO_IRQ_edge(GPIO_BITSY_ACTION_BUTTON, GPIO_BOTH_EDGES);
394         set_GPIO_IRQ_edge(GPIO_BITSY_NPOWER_BUTTON, GPIO_RISING_EDGE);
395
396         if (request_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, action_button_handler,
397                         SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
398                         "h3600_action", &ts->dev)) {
399                 printk(KERN_ERR "h3600ts.c: Could not allocate Action Button IRQ!\n");
400                 kfree(ts);
401                 return -EBUSY;
402         }
403
404         if (request_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, npower_button_handler,
405                         SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
406                         "h3600_suspend", &ts->dev)) {
407                 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
408                 printk(KERN_ERR "h3600ts.c: Could not allocate Power Button IRQ!\n");
409                 kfree(ts);
410                 return -EBUSY;
411         }
412
413         /* Now we have things going we setup our input device */
414         ts->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_LED) | BIT(EV_PWR);
415         ts->dev.ledbit[0] = BIT(LED_SLEEP);
416         input_set_abs_params(&ts->dev, ABS_X, 60, 985, 0, 0);
417         input_set_abs_params(&ts->dev, ABS_Y, 35, 1024, 0, 0);
418
419         set_bit(KEY_RECORD, ts->dev.keybit);
420         set_bit(KEY_Q, ts->dev.keybit);
421         set_bit(KEY_PROG1, ts->dev.keybit);
422         set_bit(KEY_PROG2, ts->dev.keybit);
423         set_bit(KEY_PROG3, ts->dev.keybit);
424         set_bit(KEY_UP, ts->dev.keybit);
425         set_bit(KEY_RIGHT, ts->dev.keybit);
426         set_bit(KEY_LEFT, ts->dev.keybit);
427         set_bit(KEY_DOWN, ts->dev.keybit);
428         set_bit(KEY_ENTER, ts->dev.keybit);
429         ts->dev.keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
430         ts->dev.keybit[LONG(KEY_SUSPEND)] |= BIT(KEY_SUSPEND);
431
432         ts->serio = serio;
433
434         sprintf(ts->phys, "%s/input0", serio->phys);
435
436         ts->dev.event = h3600ts_event;
437         ts->dev.private = ts;
438         ts->dev.name = h3600_name;
439         ts->dev.phys = ts->phys;
440         ts->dev.id.bustype = BUS_RS232;
441         ts->dev.id.vendor = SERIO_H3600;
442         ts->dev.id.product = 0x0666;  /* FIXME !!! We can ask the hardware */
443         ts->dev.id.version = 0x0100;
444
445         serio_set_drvdata(serio, ts);
446
447         err = serio_open(serio, drv);
448         if (err) {
449                 free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, ts);
450                 free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, ts);
451                 serio_set_drvdata(serio, NULL);
452                 kfree(ts);
453                 return err;
454         }
455
456         //h3600_flite_control(1, 25);     /* default brightness */
457 #ifdef CONFIG_PM
458         ts->pm_dev = pm_register(PM_ILLUMINATION_DEV, PM_SYS_LIGHT,
459                                 h3600ts_pm_callback);
460         printk("registered pm callback\n");
461 #endif
462         input_register_device(&ts->dev);
463
464         printk(KERN_INFO "input: %s on %s\n", h3600_name, serio->phys);
465
466         return 0;
467 }
468
469 /*
470  * h3600ts_disconnect() is the opposite of h3600ts_connect()
471  */
472
473 static void h3600ts_disconnect(struct serio *serio)
474 {
475         struct h3600_dev *ts = serio_get_drvdata(serio);
476
477         free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
478         free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, &ts->dev);
479         input_unregister_device(&ts->dev);
480         serio_close(serio);
481         serio_set_drvdata(serio, NULL);
482         kfree(ts);
483 }
484
485 /*
486  * The serio driver structure.
487  */
488
489 static struct serio_device_id h3600ts_serio_ids[] = {
490         {
491                 .type   = SERIO_RS232,
492                 .proto  = SERIO_H3600,
493                 .id     = SERIO_ANY,
494                 .extra  = SERIO_ANY,
495         },
496         { 0 }
497 };
498
499 MODULE_DEVICE_TABLE(serio, h3600ts_serio_ids);
500
501 static struct serio_driver h3600ts_drv = {
502         .driver         = {
503                 .name   = "h3600ts",
504         },
505         .description    = DRIVER_DESC,
506         .id_table       = h3600ts_serio_ids,
507         .interrupt      = h3600ts_interrupt,
508         .connect        = h3600ts_connect,
509         .disconnect     = h3600ts_disconnect,
510 };
511
512 /*
513  * The functions for inserting/removing us as a module.
514  */
515
516 static int __init h3600ts_init(void)
517 {
518         serio_register_driver(&h3600ts_drv);
519         return 0;
520 }
521
522 static void __exit h3600ts_exit(void)
523 {
524         serio_unregister_driver(&h3600ts_drv);
525 }
526
527 module_init(h3600ts_init);
528 module_exit(h3600ts_exit);