Input: dm355evm_keys - use threaded IRQs
[safe/jmp/linux-2.6] / drivers / input / misc / dm355evm_keys.c
1 /*
2  * dm355evm_keys.c - support buttons and IR remote on DM355 EVM board
3  *
4  * Copyright (c) 2008 by David Brownell
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/input.h>
14 #include <linux/platform_device.h>
15 #include <linux/interrupt.h>
16
17 #include <linux/i2c/dm355evm_msp.h>
18
19
20 /*
21  * The MSP430 firmware on the DM355 EVM monitors on-board pushbuttons
22  * and an IR receptor used for the remote control.  When any key is
23  * pressed, or its autorepeat kicks in, an event is sent.  This driver
24  * read those events from the small (32 event) queue and reports them.
25  *
26  * Note that physically there can only be one of these devices.
27  *
28  * This driver was tested with firmware revision A4.
29  */
30 struct dm355evm_keys {
31         struct input_dev        *input;
32         struct device           *dev;
33         int                     irq;
34 };
35
36 /* These initial keycodes can be remapped by dm355evm_setkeycode(). */
37 static struct {
38         u16     event;
39         u16     keycode;
40 } dm355evm_keys[] = {
41
42         /*
43          * Pushbuttons on the EVM board ... note that the labels for these
44          * are SW10/SW11/etc on the PC board.  The left/right orientation
45          * comes only from the firmware's documentation, and presumes the
46          * power connector is immediately in front of you and the IR sensor
47          * is to the right.  (That is, rotate the board counter-clockwise
48          * by 90 degrees from the SW10/etc and "DM355 EVM" labels.)
49          */
50         { 0x00d8, KEY_OK, },            /* SW12 */
51         { 0x00b8, KEY_UP, },            /* SW13 */
52         { 0x00e8, KEY_DOWN, },          /* SW11 */
53         { 0x0078, KEY_LEFT, },          /* SW14 */
54         { 0x00f0, KEY_RIGHT, },         /* SW10 */
55
56         /*
57          * IR buttons ... codes assigned to match the universal remote
58          * provided with the EVM (Philips PM4S) using DVD code 0020.
59          *
60          * These event codes match firmware documentation, but other
61          * remote controls could easily send more RC5-encoded events.
62          * The PM4S manual was used in several cases to help select
63          * a keycode reflecting the intended usage.
64          *
65          * RC5 codes are 14 bits, with two start bits (0x3 prefix)
66          * and a toggle bit (masked out below).
67          */
68         { 0x300c, KEY_POWER, },         /* NOTE: docs omit this */
69         { 0x3000, KEY_NUMERIC_0, },
70         { 0x3001, KEY_NUMERIC_1, },
71         { 0x3002, KEY_NUMERIC_2, },
72         { 0x3003, KEY_NUMERIC_3, },
73         { 0x3004, KEY_NUMERIC_4, },
74         { 0x3005, KEY_NUMERIC_5, },
75         { 0x3006, KEY_NUMERIC_6, },
76         { 0x3007, KEY_NUMERIC_7, },
77         { 0x3008, KEY_NUMERIC_8, },
78         { 0x3009, KEY_NUMERIC_9, },
79         { 0x3022, KEY_ENTER, },
80         { 0x30ec, KEY_MODE, },          /* "tv/vcr/..." */
81         { 0x300f, KEY_SELECT, },        /* "info" */
82         { 0x3020, KEY_CHANNELUP, },     /* "up" */
83         { 0x302e, KEY_MENU, },          /* "in/out" */
84         { 0x3011, KEY_VOLUMEDOWN, },    /* "left" */
85         { 0x300d, KEY_MUTE, },          /* "ok" */
86         { 0x3010, KEY_VOLUMEUP, },      /* "right" */
87         { 0x301e, KEY_SUBTITLE, },      /* "cc" */
88         { 0x3021, KEY_CHANNELDOWN, },   /* "down" */
89         { 0x3022, KEY_PREVIOUS, },
90         { 0x3026, KEY_SLEEP, },
91         { 0x3172, KEY_REWIND, },        /* NOTE: docs wrongly say 0x30ca */
92         { 0x3175, KEY_PLAY, },
93         { 0x3174, KEY_FASTFORWARD, },
94         { 0x3177, KEY_RECORD, },
95         { 0x3176, KEY_STOP, },
96         { 0x3169, KEY_PAUSE, },
97 };
98
99 /* runs in an IRQ thread -- can (and will!) sleep */
100 static irqreturn_t dm355evm_keys_irq(int irq, void *_keys)
101 {
102         struct dm355evm_keys    *keys = _keys;
103         int                     status;
104
105         /* For simplicity we ignore INPUT_COUNT and just read
106          * events until we get the "queue empty" indicator.
107          * Reading INPUT_LOW decrements the count.
108          */
109         for (;;) {
110                 static u16      last_event;
111                 u16             event;
112                 int             keycode;
113                 int             i;
114
115                 status = dm355evm_msp_read(DM355EVM_MSP_INPUT_HIGH);
116                 if (status < 0) {
117                         dev_dbg(keys->dev, "input high err %d\n",
118                                         status);
119                         break;
120                 }
121                 event = status << 8;
122
123                 status = dm355evm_msp_read(DM355EVM_MSP_INPUT_LOW);
124                 if (status < 0) {
125                         dev_dbg(keys->dev, "input low err %d\n",
126                                         status);
127                         break;
128                 }
129                 event |= status;
130                 if (event == 0xdead)
131                         break;
132
133                 /* Press and release a button:  two events, same code.
134                  * Press and hold (autorepeat), then release: N events
135                  * (N > 2), same code.  For RC5 buttons the toggle bits
136                  * distinguish (for example) "1-autorepeat" from "1 1";
137                  * but PCB buttons don't support that bit.
138                  *
139                  * So we must synthesize release events.  We do that by
140                  * mapping events to a press/release event pair; then
141                  * to avoid adding extra events, skip the second event
142                  * of each pair.
143                  */
144                 if (event == last_event) {
145                         last_event = 0;
146                         continue;
147                 }
148                 last_event = event;
149
150                 /* ignore the RC5 toggle bit */
151                 event &= ~0x0800;
152
153                 /* find the key, or leave it as unknown */
154                 keycode = KEY_UNKNOWN;
155                 for (i = 0; i < ARRAY_SIZE(dm355evm_keys); i++) {
156                         if (dm355evm_keys[i].event != event)
157                                 continue;
158                         keycode = dm355evm_keys[i].keycode;
159                         break;
160                 }
161                 dev_dbg(keys->dev,
162                         "input event 0x%04x--> keycode %d\n",
163                         event, keycode);
164
165                 /* report press + release */
166                 input_report_key(keys->input, keycode, 1);
167                 input_sync(keys->input);
168                 input_report_key(keys->input, keycode, 0);
169                 input_sync(keys->input);
170         }
171         return IRQ_HANDLED;
172 }
173
174 /*
175  * Because we communicate with the MSP430 using I2C, and all I2C calls
176  * in Linux sleep, we use a threaded IRQ handler.  The IRQ itself is
177  * active low, but we go through the GPIO controller so we can trigger
178  * on falling edges and not worry about enabling/disabling the IRQ in
179  * the keypress handling path.
180  */
181 static irqreturn_t dm355evm_keys_hardirq(int irq, void *_keys)
182 {
183         return IRQ_WAKE_THREAD;
184 }
185
186 static int dm355evm_setkeycode(struct input_dev *dev, int index, int keycode)
187 {
188         u16             old_keycode;
189         unsigned        i;
190
191         if (((unsigned)index) >= ARRAY_SIZE(dm355evm_keys))
192                 return -EINVAL;
193
194         old_keycode = dm355evm_keys[index].keycode;
195         dm355evm_keys[index].keycode = keycode;
196         set_bit(keycode, dev->keybit);
197
198         for (i = 0; i < ARRAY_SIZE(dm355evm_keys); i++) {
199                 if (dm355evm_keys[index].keycode == old_keycode)
200                         goto done;
201         }
202         clear_bit(old_keycode, dev->keybit);
203 done:
204         return 0;
205 }
206
207 static int dm355evm_getkeycode(struct input_dev *dev, int index, int *keycode)
208 {
209         if (((unsigned)index) >= ARRAY_SIZE(dm355evm_keys))
210                 return -EINVAL;
211
212         return dm355evm_keys[index].keycode;
213 }
214
215 /*----------------------------------------------------------------------*/
216
217 static int __devinit dm355evm_keys_probe(struct platform_device *pdev)
218 {
219         struct dm355evm_keys    *keys;
220         struct input_dev        *input;
221         int                     status;
222         int                     i;
223
224         /* allocate instance struct and input dev */
225         keys = kzalloc(sizeof *keys, GFP_KERNEL);
226         input = input_allocate_device();
227         if (!keys || !input) {
228                 status = -ENOMEM;
229                 goto fail1;
230         }
231
232         keys->dev = &pdev->dev;
233         keys->input = input;
234
235         /* set up "threaded IRQ handler" */
236         status = platform_get_irq(pdev, 0);
237         if (status < 0)
238                 goto fail1;
239         keys->irq = status;
240
241         input_set_drvdata(input, keys);
242
243         input->name = "DM355 EVM Controls";
244         input->phys = "dm355evm/input0";
245         input->dev.parent = &pdev->dev;
246
247         input->id.bustype = BUS_I2C;
248         input->id.product = 0x0355;
249         input->id.version = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
250
251         input->evbit[0] = BIT(EV_KEY);
252         for (i = 0; i < ARRAY_SIZE(dm355evm_keys); i++)
253                 __set_bit(dm355evm_keys[i].keycode, input->keybit);
254
255         input->setkeycode = dm355evm_setkeycode;
256         input->getkeycode = dm355evm_getkeycode;
257
258         /* REVISIT:  flush the event queue? */
259
260         status = request_threaded_irq(keys->irq,
261                         dm355evm_keys_hardirq, dm355evm_keys_irq,
262                         IRQF_TRIGGER_FALLING,
263                         dev_name(&pdev->dev), keys);
264         if (status < 0)
265                 goto fail1;
266
267         /* register */
268         status = input_register_device(input);
269         if (status < 0)
270                 goto fail2;
271
272         platform_set_drvdata(pdev, keys);
273
274         return 0;
275
276 fail2:
277         free_irq(keys->irq, keys);
278 fail1:
279         input_free_device(input);
280         kfree(keys);
281         dev_err(&pdev->dev, "can't register, err %d\n", status);
282
283         return status;
284 }
285
286 static int __devexit dm355evm_keys_remove(struct platform_device *pdev)
287 {
288         struct dm355evm_keys    *keys = platform_get_drvdata(pdev);
289
290         free_irq(keys->irq, keys);
291         input_unregister_device(keys->input);
292         kfree(keys);
293
294         return 0;
295 }
296
297 /* REVISIT:  add suspend/resume when DaVinci supports it.  The IRQ should
298  * be able to wake up the system.  When device_may_wakeup(&pdev->dev), call
299  * enable_irq_wake() on suspend, and disable_irq_wake() on resume.
300  */
301
302 /*
303  * I2C is used to talk to the MSP430, but this platform device is
304  * exposed by an MFD driver that manages I2C communications.
305  */
306 static struct platform_driver dm355evm_keys_driver = {
307         .probe          = dm355evm_keys_probe,
308         .remove         = __devexit_p(dm355evm_keys_remove),
309         .driver         = {
310                 .owner  = THIS_MODULE,
311                 .name   = "dm355evm_keys",
312         },
313 };
314
315 static int __init dm355evm_keys_init(void)
316 {
317         return platform_driver_register(&dm355evm_keys_driver);
318 }
319 module_init(dm355evm_keys_init);
320
321 static void __exit dm355evm_keys_exit(void)
322 {
323         platform_driver_unregister(&dm355evm_keys_driver);
324 }
325 module_exit(dm355evm_keys_exit);
326
327 MODULE_LICENSE("GPL");