Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[pandora-kernel.git] / 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
13 #include <linux/init.h>
14 #include <linux/fs.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.h>
18 #include <linux/pm.h>
19 #include <linux/sysctl.h>
20 #include <linux/proc_fs.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/input.h>
24 #include <linux/gpio_keys.h>
25 #include <linux/workqueue.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 delayed_work work;
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 work_struct *work)
41 {
42         struct gpio_button_data *bdata =
43                 container_of(work, struct gpio_button_data, work.work);
44         struct gpio_keys_button *button = bdata->button;
45         struct input_dev *input = bdata->input;
46         unsigned int type = button->type ?: EV_KEY;
47         int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
48
49         input_event(input, type, button->code, !!state);
50         input_sync(input);
51 }
52
53 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
54 {
55         struct gpio_button_data *bdata = dev_id;
56         struct gpio_keys_button *button = bdata->button;
57         unsigned long delay;
58
59         BUG_ON(irq != gpio_to_irq(button->gpio));
60
61         delay = button->debounce_interval ?
62                         msecs_to_jiffies(button->debounce_interval) : 0;
63         schedule_delayed_work(&bdata->work, delay);
64
65         return IRQ_HANDLED;
66 }
67
68 static int __devinit gpio_keys_probe(struct platform_device *pdev)
69 {
70         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
71         struct gpio_keys_drvdata *ddata;
72         struct input_dev *input;
73         int i, error;
74         int wakeup = 0;
75
76         ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
77                         pdata->nbuttons * sizeof(struct gpio_button_data),
78                         GFP_KERNEL);
79         input = input_allocate_device();
80         if (!ddata || !input) {
81                 error = -ENOMEM;
82                 goto fail1;
83         }
84
85         platform_set_drvdata(pdev, ddata);
86
87         input->name = pdev->name;
88         input->phys = "gpio-keys/input0";
89         input->dev.parent = &pdev->dev;
90
91         input->id.bustype = BUS_HOST;
92         input->id.vendor = 0x0001;
93         input->id.product = 0x0001;
94         input->id.version = 0x0100;
95
96         /* Enable auto repeat feature of Linux input subsystem */
97         if (pdata->rep)
98                 __set_bit(EV_REP, input->evbit);
99
100         ddata->input = input;
101
102         for (i = 0; i < pdata->nbuttons; i++) {
103                 struct gpio_keys_button *button = &pdata->buttons[i];
104                 struct gpio_button_data *bdata = &ddata->data[i];
105                 int irq;
106                 unsigned int type = button->type ?: EV_KEY;
107
108                 bdata->input = input;
109                 bdata->button = button;
110                 INIT_DELAYED_WORK(&bdata->work, gpio_keys_report_event);
111
112                 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
113                 if (error < 0) {
114                         pr_err("gpio-keys: failed to request GPIO %d,"
115                                 " error %d\n", button->gpio, error);
116                         goto fail2;
117                 }
118
119                 error = gpio_direction_input(button->gpio);
120                 if (error < 0) {
121                         pr_err("gpio-keys: failed to configure input"
122                                 " direction for GPIO %d, error %d\n",
123                                 button->gpio, error);
124                         gpio_free(button->gpio);
125                         goto fail2;
126                 }
127
128                 irq = gpio_to_irq(button->gpio);
129                 if (irq < 0) {
130                         error = irq;
131                         pr_err("gpio-keys: Unable to get irq number"
132                                 " for GPIO %d, error %d\n",
133                                 button->gpio, error);
134                         gpio_free(button->gpio);
135                         goto fail2;
136                 }
137
138                 error = request_irq(irq, gpio_keys_isr,
139                                     IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
140                                     button->desc ? button->desc : "gpio_keys",
141                                     bdata);
142                 if (error) {
143                         pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
144                                 irq, error);
145                         gpio_free(button->gpio);
146                         goto fail2;
147                 }
148
149                 if (button->wakeup)
150                         wakeup = 1;
151
152                 input_set_capability(input, type, button->code);
153         }
154
155         error = input_register_device(input);
156         if (error) {
157                 pr_err("gpio-keys: Unable to register input device, "
158                         "error: %d\n", error);
159                 goto fail2;
160         }
161
162         device_init_wakeup(&pdev->dev, wakeup);
163
164         return 0;
165
166  fail2:
167         while (--i >= 0) {
168                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
169                 cancel_delayed_work_sync(&ddata->data[i].work);
170                 gpio_free(pdata->buttons[i].gpio);
171         }
172
173         platform_set_drvdata(pdev, NULL);
174  fail1:
175         input_free_device(input);
176         kfree(ddata);
177
178         return error;
179 }
180
181 static int __devexit gpio_keys_remove(struct platform_device *pdev)
182 {
183         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
184         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
185         struct input_dev *input = ddata->input;
186         int i;
187
188         device_init_wakeup(&pdev->dev, 0);
189
190         for (i = 0; i < pdata->nbuttons; i++) {
191                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
192                 free_irq(irq, &ddata->data[i]);
193                 cancel_delayed_work_sync(&ddata->data[i].work);
194                 gpio_free(pdata->buttons[i].gpio);
195         }
196
197         input_unregister_device(input);
198
199         return 0;
200 }
201
202
203 #ifdef CONFIG_PM
204 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
205 {
206         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
207         int i;
208
209         if (device_may_wakeup(&pdev->dev)) {
210                 for (i = 0; i < pdata->nbuttons; i++) {
211                         struct gpio_keys_button *button = &pdata->buttons[i];
212                         if (button->wakeup) {
213                                 int irq = gpio_to_irq(button->gpio);
214                                 enable_irq_wake(irq);
215                         }
216                 }
217         }
218
219         return 0;
220 }
221
222 static int gpio_keys_resume(struct platform_device *pdev)
223 {
224         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
225         int i;
226
227         if (device_may_wakeup(&pdev->dev)) {
228                 for (i = 0; i < pdata->nbuttons; i++) {
229                         struct gpio_keys_button *button = &pdata->buttons[i];
230                         if (button->wakeup) {
231                                 int irq = gpio_to_irq(button->gpio);
232                                 disable_irq_wake(irq);
233                         }
234                 }
235         }
236
237         return 0;
238 }
239 #else
240 #define gpio_keys_suspend       NULL
241 #define gpio_keys_resume        NULL
242 #endif
243
244 static struct platform_driver gpio_keys_device_driver = {
245         .probe          = gpio_keys_probe,
246         .remove         = __devexit_p(gpio_keys_remove),
247         .suspend        = gpio_keys_suspend,
248         .resume         = gpio_keys_resume,
249         .driver         = {
250                 .name   = "gpio-keys",
251                 .owner  = THIS_MODULE,
252         }
253 };
254
255 static int __init gpio_keys_init(void)
256 {
257         return platform_driver_register(&gpio_keys_device_driver);
258 }
259
260 static void __exit gpio_keys_exit(void)
261 {
262         platform_driver_unregister(&gpio_keys_device_driver);
263 }
264
265 module_init(gpio_keys_init);
266 module_exit(gpio_keys_exit);
267
268 MODULE_LICENSE("GPL");
269 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
270 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
271 MODULE_ALIAS("platform:gpio-keys");