]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/input/keyboard/gpio_keys.c
Input: gpio_keys - scan gpio state at probe and resume time
[net-next-2.6.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 #include <linux/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         struct work_struct 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 gpio_button_data *bdata)
41 {
42         struct gpio_keys_button *button = bdata->button;
43         struct input_dev *input = bdata->input;
44         unsigned int type = button->type ?: EV_KEY;
45         int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
46
47         input_event(input, type, button->code, !!state);
48         input_sync(input);
49 }
50
51 static void gpio_keys_work_func(struct work_struct *work)
52 {
53         struct gpio_button_data *bdata =
54                 container_of(work, struct gpio_button_data, work);
55
56         gpio_keys_report_event(bdata);
57 }
58
59 static void gpio_keys_timer(unsigned long _data)
60 {
61         struct gpio_button_data *data = (struct gpio_button_data *)_data;
62
63         schedule_work(&data->work);
64 }
65
66 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
67 {
68         struct gpio_button_data *bdata = dev_id;
69         struct gpio_keys_button *button = bdata->button;
70
71         BUG_ON(irq != gpio_to_irq(button->gpio));
72
73         if (button->debounce_interval)
74                 mod_timer(&bdata->timer,
75                         jiffies + msecs_to_jiffies(button->debounce_interval));
76         else
77                 schedule_work(&bdata->work);
78
79         return IRQ_HANDLED;
80 }
81
82 static int __devinit gpio_keys_setup_key(struct device *dev,
83                                          struct gpio_button_data *bdata,
84                                          struct gpio_keys_button *button)
85 {
86         char *desc = button->desc ? button->desc : "gpio_keys";
87         int irq, error;
88
89         setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
90         INIT_WORK(&bdata->work, gpio_keys_work_func);
91
92         error = gpio_request(button->gpio, desc);
93         if (error < 0) {
94                 dev_err(dev, "failed to request GPIO %d, error %d\n",
95                         button->gpio, error);
96                 goto fail2;
97         }
98
99         error = gpio_direction_input(button->gpio);
100         if (error < 0) {
101                 dev_err(dev, "failed to configure"
102                         " direction for GPIO %d, error %d\n",
103                         button->gpio, error);
104                 goto fail3;
105         }
106
107         irq = gpio_to_irq(button->gpio);
108         if (irq < 0) {
109                 error = irq;
110                 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
111                         button->gpio, error);
112                 goto fail3;
113         }
114
115         error = request_irq(irq, gpio_keys_isr,
116                             IRQF_SHARED |
117                             IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
118                             desc, bdata);
119         if (error) {
120                 dev_err(dev, "Unable to claim irq %d; error %d\n",
121                         irq, error);
122                 goto fail3;
123         }
124
125         return 0;
126
127 fail3:
128         gpio_free(button->gpio);
129 fail2:
130         return error;
131 }
132
133 static int __devinit gpio_keys_probe(struct platform_device *pdev)
134 {
135         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
136         struct gpio_keys_drvdata *ddata;
137         struct device *dev = &pdev->dev;
138         struct input_dev *input;
139         int i, error;
140         int wakeup = 0;
141
142         ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
143                         pdata->nbuttons * sizeof(struct gpio_button_data),
144                         GFP_KERNEL);
145         input = input_allocate_device();
146         if (!ddata || !input) {
147                 dev_err(dev, "failed to allocate state\n");
148                 error = -ENOMEM;
149                 goto fail1;
150         }
151
152         platform_set_drvdata(pdev, ddata);
153
154         input->name = pdev->name;
155         input->phys = "gpio-keys/input0";
156         input->dev.parent = &pdev->dev;
157
158         input->id.bustype = BUS_HOST;
159         input->id.vendor = 0x0001;
160         input->id.product = 0x0001;
161         input->id.version = 0x0100;
162
163         /* Enable auto repeat feature of Linux input subsystem */
164         if (pdata->rep)
165                 __set_bit(EV_REP, input->evbit);
166
167         ddata->input = input;
168
169         for (i = 0; i < pdata->nbuttons; i++) {
170                 struct gpio_keys_button *button = &pdata->buttons[i];
171                 struct gpio_button_data *bdata = &ddata->data[i];
172                 unsigned int type = button->type ?: EV_KEY;
173
174                 bdata->input = input;
175                 bdata->button = button;
176
177                 error = gpio_keys_setup_key(dev, bdata, button);
178                 if (error)
179                         goto fail2;
180
181                 if (button->wakeup)
182                         wakeup = 1;
183
184                 input_set_capability(input, type, button->code);
185         }
186
187         error = input_register_device(input);
188         if (error) {
189                 dev_err(dev, "Unable to register input device, "
190                         "error: %d\n", error);
191                 goto fail2;
192         }
193
194         /* get current state of buttons */
195         for (i = 0; i < pdata->nbuttons; i++)
196                 gpio_keys_report_event(&ddata->data[i]);
197         input_sync(input);
198
199         device_init_wakeup(&pdev->dev, wakeup);
200
201         return 0;
202
203  fail2:
204         while (--i >= 0) {
205                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
206                 if (pdata->buttons[i].debounce_interval)
207                         del_timer_sync(&ddata->data[i].timer);
208                 cancel_work_sync(&ddata->data[i].work);
209                 gpio_free(pdata->buttons[i].gpio);
210         }
211
212         platform_set_drvdata(pdev, NULL);
213  fail1:
214         input_free_device(input);
215         kfree(ddata);
216
217         return error;
218 }
219
220 static int __devexit gpio_keys_remove(struct platform_device *pdev)
221 {
222         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
223         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
224         struct input_dev *input = ddata->input;
225         int i;
226
227         device_init_wakeup(&pdev->dev, 0);
228
229         for (i = 0; i < pdata->nbuttons; i++) {
230                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
231                 free_irq(irq, &ddata->data[i]);
232                 if (pdata->buttons[i].debounce_interval)
233                         del_timer_sync(&ddata->data[i].timer);
234                 cancel_work_sync(&ddata->data[i].work);
235                 gpio_free(pdata->buttons[i].gpio);
236         }
237
238         input_unregister_device(input);
239
240         return 0;
241 }
242
243
244 #ifdef CONFIG_PM
245 static int gpio_keys_suspend(struct device *dev)
246 {
247         struct platform_device *pdev = to_platform_device(dev);
248         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
249         int i;
250
251         if (device_may_wakeup(&pdev->dev)) {
252                 for (i = 0; i < pdata->nbuttons; i++) {
253                         struct gpio_keys_button *button = &pdata->buttons[i];
254                         if (button->wakeup) {
255                                 int irq = gpio_to_irq(button->gpio);
256                                 enable_irq_wake(irq);
257                         }
258                 }
259         }
260
261         return 0;
262 }
263
264 static int gpio_keys_resume(struct device *dev)
265 {
266         struct platform_device *pdev = to_platform_device(dev);
267         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
268         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
269         int i;
270
271         for (i = 0; i < pdata->nbuttons; i++) {
272
273                 struct gpio_keys_button *button = &pdata->buttons[i];
274                 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
275                         int irq = gpio_to_irq(button->gpio);
276                         disable_irq_wake(irq);
277                 }
278
279                 gpio_keys_report_event(&ddata->data[i]);
280         }
281         input_sync(ddata->input);
282
283         return 0;
284 }
285
286 static const struct dev_pm_ops gpio_keys_pm_ops = {
287         .suspend        = gpio_keys_suspend,
288         .resume         = gpio_keys_resume,
289 };
290 #endif
291
292 static struct platform_driver gpio_keys_device_driver = {
293         .probe          = gpio_keys_probe,
294         .remove         = __devexit_p(gpio_keys_remove),
295         .driver         = {
296                 .name   = "gpio-keys",
297                 .owner  = THIS_MODULE,
298 #ifdef CONFIG_PM
299                 .pm     = &gpio_keys_pm_ops,
300 #endif
301         }
302 };
303
304 static int __init gpio_keys_init(void)
305 {
306         return platform_driver_register(&gpio_keys_device_driver);
307 }
308
309 static void __exit gpio_keys_exit(void)
310 {
311         platform_driver_unregister(&gpio_keys_device_driver);
312 }
313
314 module_init(gpio_keys_init);
315 module_exit(gpio_keys_exit);
316
317 MODULE_LICENSE("GPL");
318 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
319 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
320 MODULE_ALIAS("platform:gpio-keys");