Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/jk/spufs into...
[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
26 #include <asm/gpio.h>
27
28 struct gpio_button_data {
29         struct gpio_keys_button *button;
30         struct input_dev *input;
31         struct timer_list timer;
32 };
33
34 struct gpio_keys_drvdata {
35         struct input_dev *input;
36         struct gpio_button_data data[0];
37 };
38
39 static void gpio_keys_report_event(struct gpio_button_data *bdata)
40 {
41         struct gpio_keys_button *button = bdata->button;
42         struct input_dev *input = bdata->input;
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);
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(bdata);
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                 bdata->button = button;
111                 setup_timer(&bdata->timer,
112                             gpio_check_button, (unsigned long)bdata);
113
114                 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
115                 if (error < 0) {
116                         pr_err("gpio-keys: failed to request GPIO %d,"
117                                 " error %d\n", button->gpio, error);
118                         goto fail2;
119                 }
120
121                 error = gpio_direction_input(button->gpio);
122                 if (error < 0) {
123                         pr_err("gpio-keys: failed to configure input"
124                                 " direction for GPIO %d, error %d\n",
125                                 button->gpio, error);
126                         gpio_free(button->gpio);
127                         goto fail2;
128                 }
129
130                 irq = gpio_to_irq(button->gpio);
131                 if (irq < 0) {
132                         error = irq;
133                         pr_err("gpio-keys: Unable to get irq number"
134                                 " for GPIO %d, error %d\n",
135                                 button->gpio, error);
136                         gpio_free(button->gpio);
137                         goto fail2;
138                 }
139
140                 error = request_irq(irq, gpio_keys_isr,
141                                     IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
142                                         IRQF_TRIGGER_FALLING,
143                                     button->desc ? button->desc : "gpio_keys",
144                                     bdata);
145                 if (error) {
146                         pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
147                                 irq, error);
148                         gpio_free(button->gpio);
149                         goto fail2;
150                 }
151
152                 if (button->wakeup)
153                         wakeup = 1;
154
155                 input_set_capability(input, type, button->code);
156         }
157
158         error = input_register_device(input);
159         if (error) {
160                 pr_err("gpio-keys: Unable to register input device, "
161                         "error: %d\n", error);
162                 goto fail2;
163         }
164
165         device_init_wakeup(&pdev->dev, wakeup);
166
167         return 0;
168
169  fail2:
170         while (--i >= 0) {
171                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
172                 if (pdata->buttons[i].debounce_interval)
173                         del_timer_sync(&ddata->data[i].timer);
174                 gpio_free(pdata->buttons[i].gpio);
175         }
176
177         platform_set_drvdata(pdev, NULL);
178  fail1:
179         input_free_device(input);
180         kfree(ddata);
181
182         return error;
183 }
184
185 static int __devexit gpio_keys_remove(struct platform_device *pdev)
186 {
187         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
188         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
189         struct input_dev *input = ddata->input;
190         int i;
191
192         device_init_wakeup(&pdev->dev, 0);
193
194         for (i = 0; i < pdata->nbuttons; i++) {
195                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
196                 free_irq(irq, &ddata->data[i]);
197                 if (pdata->buttons[i].debounce_interval)
198                         del_timer_sync(&ddata->data[i].timer);
199                 gpio_free(pdata->buttons[i].gpio);
200         }
201
202         input_unregister_device(input);
203
204         return 0;
205 }
206
207
208 #ifdef CONFIG_PM
209 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
210 {
211         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
212         int i;
213
214         if (device_may_wakeup(&pdev->dev)) {
215                 for (i = 0; i < pdata->nbuttons; i++) {
216                         struct gpio_keys_button *button = &pdata->buttons[i];
217                         if (button->wakeup) {
218                                 int irq = gpio_to_irq(button->gpio);
219                                 enable_irq_wake(irq);
220                         }
221                 }
222         }
223
224         return 0;
225 }
226
227 static int gpio_keys_resume(struct platform_device *pdev)
228 {
229         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
230         int i;
231
232         if (device_may_wakeup(&pdev->dev)) {
233                 for (i = 0; i < pdata->nbuttons; i++) {
234                         struct gpio_keys_button *button = &pdata->buttons[i];
235                         if (button->wakeup) {
236                                 int irq = gpio_to_irq(button->gpio);
237                                 disable_irq_wake(irq);
238                         }
239                 }
240         }
241
242         return 0;
243 }
244 #else
245 #define gpio_keys_suspend       NULL
246 #define gpio_keys_resume        NULL
247 #endif
248
249 static struct platform_driver gpio_keys_device_driver = {
250         .probe          = gpio_keys_probe,
251         .remove         = __devexit_p(gpio_keys_remove),
252         .suspend        = gpio_keys_suspend,
253         .resume         = gpio_keys_resume,
254         .driver         = {
255                 .name   = "gpio-keys",
256                 .owner  = THIS_MODULE,
257         }
258 };
259
260 static int __init gpio_keys_init(void)
261 {
262         return platform_driver_register(&gpio_keys_device_driver);
263 }
264
265 static void __exit gpio_keys_exit(void)
266 {
267         platform_driver_unregister(&gpio_keys_device_driver);
268 }
269
270 module_init(gpio_keys_init);
271 module_exit(gpio_keys_exit);
272
273 MODULE_LICENSE("GPL");
274 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
275 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
276 MODULE_ALIAS("platform:gpio-keys");