Input: gpio_keys - scan gpio state at probe and resume time
[linux-2.6/libata-dev.git] / drivers / input / keyboard / gpio_keys.c
blob1aff3b76effdb0b05ee4201cba2525cce5b44769
1 /*
2 * Driver for keys on GPIO lines capable of generating interrupts.
4 * Copyright 2005 Phil Blundell
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 */
11 #include <linux/module.h>
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>
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;
35 struct gpio_keys_drvdata {
36 struct input_dev *input;
37 struct gpio_button_data data[0];
40 static void gpio_keys_report_event(struct gpio_button_data *bdata)
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;
47 input_event(input, type, button->code, !!state);
48 input_sync(input);
51 static void gpio_keys_work_func(struct work_struct *work)
53 struct gpio_button_data *bdata =
54 container_of(work, struct gpio_button_data, work);
56 gpio_keys_report_event(bdata);
59 static void gpio_keys_timer(unsigned long _data)
61 struct gpio_button_data *data = (struct gpio_button_data *)_data;
63 schedule_work(&data->work);
66 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
68 struct gpio_button_data *bdata = dev_id;
69 struct gpio_keys_button *button = bdata->button;
71 BUG_ON(irq != gpio_to_irq(button->gpio));
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);
79 return IRQ_HANDLED;
82 static int __devinit gpio_keys_setup_key(struct device *dev,
83 struct gpio_button_data *bdata,
84 struct gpio_keys_button *button)
86 char *desc = button->desc ? button->desc : "gpio_keys";
87 int irq, error;
89 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
90 INIT_WORK(&bdata->work, gpio_keys_work_func);
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;
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;
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;
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;
125 return 0;
127 fail3:
128 gpio_free(button->gpio);
129 fail2:
130 return error;
133 static int __devinit gpio_keys_probe(struct platform_device *pdev)
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;
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;
152 platform_set_drvdata(pdev, ddata);
154 input->name = pdev->name;
155 input->phys = "gpio-keys/input0";
156 input->dev.parent = &pdev->dev;
158 input->id.bustype = BUS_HOST;
159 input->id.vendor = 0x0001;
160 input->id.product = 0x0001;
161 input->id.version = 0x0100;
163 /* Enable auto repeat feature of Linux input subsystem */
164 if (pdata->rep)
165 __set_bit(EV_REP, input->evbit);
167 ddata->input = input;
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;
174 bdata->input = input;
175 bdata->button = button;
177 error = gpio_keys_setup_key(dev, bdata, button);
178 if (error)
179 goto fail2;
181 if (button->wakeup)
182 wakeup = 1;
184 input_set_capability(input, type, button->code);
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;
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);
199 device_init_wakeup(&pdev->dev, wakeup);
201 return 0;
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);
212 platform_set_drvdata(pdev, NULL);
213 fail1:
214 input_free_device(input);
215 kfree(ddata);
217 return error;
220 static int __devexit gpio_keys_remove(struct platform_device *pdev)
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;
227 device_init_wakeup(&pdev->dev, 0);
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);
238 input_unregister_device(input);
240 return 0;
244 #ifdef CONFIG_PM
245 static int gpio_keys_suspend(struct device *dev)
247 struct platform_device *pdev = to_platform_device(dev);
248 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
249 int i;
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);
261 return 0;
264 static int gpio_keys_resume(struct device *dev)
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;
271 for (i = 0; i < pdata->nbuttons; i++) {
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);
279 gpio_keys_report_event(&ddata->data[i]);
281 input_sync(ddata->input);
283 return 0;
286 static const struct dev_pm_ops gpio_keys_pm_ops = {
287 .suspend = gpio_keys_suspend,
288 .resume = gpio_keys_resume,
290 #endif
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
304 static int __init gpio_keys_init(void)
306 return platform_driver_register(&gpio_keys_device_driver);
309 static void __exit gpio_keys_exit(void)
311 platform_driver_unregister(&gpio_keys_device_driver);
314 module_init(gpio_keys_init);
315 module_exit(gpio_keys_exit);
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");