Input: gpio-keys - optimize interrupt handler
[safe/jmp/linux-2.6] / drivers / input / keyboard / gpio_keys.c
1 /*
2  * Driver for keys on GPIO lines capable of generating interrupts.
3  *
4  * Copyright 2005 Phil Blundell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/version.h>
13
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/sched.h>
19 #include <linux/pm.h>
20 #include <linux/sysctl.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/input.h>
25 #include <linux/gpio_keys.h>
26
27 #include <asm/gpio.h>
28
29 struct gpio_button_data {
30         struct gpio_keys_button *button;
31         struct input_dev *input;
32         struct timer_list timer;
33 };
34
35 struct gpio_keys_drvdata {
36         struct input_dev *input;
37         struct gpio_button_data data[0];
38 };
39
40 static void gpio_keys_report_event(struct gpio_keys_button *button,
41                                    struct input_dev *input)
42 {
43         unsigned int type = button->type ?: EV_KEY;
44         int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
45
46         input_event(input, type, button->code, !!state);
47         input_sync(input);
48 }
49
50 static void gpio_check_button(unsigned long _data)
51 {
52         struct gpio_button_data *data = (struct gpio_button_data *)_data;
53
54         gpio_keys_report_event(data->button, data->input);
55 }
56
57 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
58 {
59         struct gpio_button_data *bdata = dev_id;
60         struct gpio_keys_button *button = bdata->button;
61
62         BUG_ON(irq != gpio_to_irq(button->gpio));
63
64         if (button->debounce_interval)
65                 mod_timer(&bdata->timer,
66                         jiffies + msecs_to_jiffies(button->debounce_interval));
67         else
68                 gpio_keys_report_event(button, bdata->input);
69
70         return IRQ_HANDLED;
71 }
72
73 static int __devinit gpio_keys_probe(struct platform_device *pdev)
74 {
75         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
76         struct gpio_keys_drvdata *ddata;
77         struct input_dev *input;
78         int i, error;
79         int wakeup = 0;
80
81         ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
82                         pdata->nbuttons * sizeof(struct gpio_button_data),
83                         GFP_KERNEL);
84         input = input_allocate_device();
85         if (!ddata || !input) {
86                 error = -ENOMEM;
87                 goto fail1;
88         }
89
90         platform_set_drvdata(pdev, ddata);
91
92         input->name = pdev->name;
93         input->phys = "gpio-keys/input0";
94         input->dev.parent = &pdev->dev;
95
96         input->id.bustype = BUS_HOST;
97         input->id.vendor = 0x0001;
98         input->id.product = 0x0001;
99         input->id.version = 0x0100;
100
101         ddata->input = input;
102
103         for (i = 0; i < pdata->nbuttons; i++) {
104                 struct gpio_keys_button *button = &pdata->buttons[i];
105                 struct gpio_button_data *bdata = &ddata->data[i];
106                 int irq;
107                 unsigned int type = button->type ?: EV_KEY;
108
109                 bdata->input = input;
110                 setup_timer(&bdata->timer,
111                             gpio_check_button, (unsigned long)bdata);
112
113                 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
114                 if (error < 0) {
115                         pr_err("gpio-keys: failed to request GPIO %d,"
116                                 " error %d\n", button->gpio, error);
117                         goto fail2;
118                 }
119
120                 error = gpio_direction_input(button->gpio);
121                 if (error < 0) {
122                         pr_err("gpio-keys: failed to configure input"
123                                 " direction for GPIO %d, error %d\n",
124                                 button->gpio, error);
125                         gpio_free(button->gpio);
126                         goto fail2;
127                 }
128
129                 irq = gpio_to_irq(button->gpio);
130                 if (irq < 0) {
131                         error = irq;
132                         pr_err("gpio-keys: Unable to get irq number"
133                                 " for GPIO %d, error %d\n",
134                                 button->gpio, error);
135                         gpio_free(button->gpio);
136                         goto fail2;
137                 }
138
139                 error = request_irq(irq, gpio_keys_isr,
140                                     IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
141                                         IRQF_TRIGGER_FALLING,
142                                     button->desc ? button->desc : "gpio_keys",
143                                     bdata);
144                 if (error) {
145                         pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
146                                 irq, error);
147                         gpio_free(button->gpio);
148                         goto fail2;
149                 }
150
151                 if (button->wakeup)
152                         wakeup = 1;
153
154                 input_set_capability(input, type, button->code);
155         }
156
157         error = input_register_device(input);
158         if (error) {
159                 pr_err("gpio-keys: Unable to register input device, "
160                         "error: %d\n", error);
161                 goto fail2;
162         }
163
164         device_init_wakeup(&pdev->dev, wakeup);
165
166         return 0;
167
168  fail2:
169         while (--i >= 0) {
170                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
171                 if (pdata->buttons[i].debounce_interval)
172                         del_timer_sync(&ddata->data[i].timer);
173                 gpio_free(pdata->buttons[i].gpio);
174         }
175
176         platform_set_drvdata(pdev, NULL);
177  fail1:
178         input_free_device(input);
179         kfree(ddata);
180
181         return error;
182 }
183
184 static int __devexit gpio_keys_remove(struct platform_device *pdev)
185 {
186         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
187         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
188         struct input_dev *input = ddata->input;
189         int i;
190
191         device_init_wakeup(&pdev->dev, 0);
192
193         for (i = 0; i < pdata->nbuttons; i++) {
194                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
195                 free_irq(irq, &ddata->data[i]);
196                 if (pdata->buttons[i].debounce_interval)
197                         del_timer_sync(&ddata->data[i].timer);
198                 gpio_free(pdata->buttons[i].gpio);
199         }
200
201         input_unregister_device(input);
202
203         return 0;
204 }
205
206
207 #ifdef CONFIG_PM
208 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
209 {
210         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
211         int i;
212
213         if (device_may_wakeup(&pdev->dev)) {
214                 for (i = 0; i < pdata->nbuttons; i++) {
215                         struct gpio_keys_button *button = &pdata->buttons[i];
216                         if (button->wakeup) {
217                                 int irq = gpio_to_irq(button->gpio);
218                                 enable_irq_wake(irq);
219                         }
220                 }
221         }
222
223         return 0;
224 }
225
226 static int gpio_keys_resume(struct platform_device *pdev)
227 {
228         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
229         int i;
230
231         if (device_may_wakeup(&pdev->dev)) {
232                 for (i = 0; i < pdata->nbuttons; i++) {
233                         struct gpio_keys_button *button = &pdata->buttons[i];
234                         if (button->wakeup) {
235                                 int irq = gpio_to_irq(button->gpio);
236                                 disable_irq_wake(irq);
237                         }
238                 }
239         }
240
241         return 0;
242 }
243 #else
244 #define gpio_keys_suspend       NULL
245 #define gpio_keys_resume        NULL
246 #endif
247
248 struct platform_driver gpio_keys_device_driver = {
249         .probe          = gpio_keys_probe,
250         .remove         = __devexit_p(gpio_keys_remove),
251         .suspend        = gpio_keys_suspend,
252         .resume         = gpio_keys_resume,
253         .driver         = {
254                 .name   = "gpio-keys",
255                 .owner  = THIS_MODULE,
256         }
257 };
258
259 static int __init gpio_keys_init(void)
260 {
261         return platform_driver_register(&gpio_keys_device_driver);
262 }
263
264 static void __exit gpio_keys_exit(void)
265 {
266         platform_driver_unregister(&gpio_keys_device_driver);
267 }
268
269 module_init(gpio_keys_init);
270 module_exit(gpio_keys_exit);
271
272 MODULE_LICENSE("GPL");
273 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
274 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
275 MODULE_ALIAS("platform:gpio-keys");