Input: gpio-keys - optimize interrupt handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / keyboard / gpio_keys.c
blobe2809d29d99d9f9275579d215419ced74b8da0f9
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>
12 #include <linux/version.h>
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>
27 #include <asm/gpio.h>
29 struct gpio_button_data {
30 struct gpio_keys_button *button;
31 struct input_dev *input;
32 struct timer_list timer;
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_keys_button *button,
41 struct input_dev *input)
43 unsigned int type = button->type ?: EV_KEY;
44 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
46 input_event(input, type, button->code, !!state);
47 input_sync(input);
50 static void gpio_check_button(unsigned long _data)
52 struct gpio_button_data *data = (struct gpio_button_data *)_data;
54 gpio_keys_report_event(data->button, data->input);
57 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
59 struct gpio_button_data *bdata = dev_id;
60 struct gpio_keys_button *button = bdata->button;
62 BUG_ON(irq != gpio_to_irq(button->gpio));
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);
70 return IRQ_HANDLED;
73 static int __devinit gpio_keys_probe(struct platform_device *pdev)
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;
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;
90 platform_set_drvdata(pdev, ddata);
92 input->name = pdev->name;
93 input->phys = "gpio-keys/input0";
94 input->dev.parent = &pdev->dev;
96 input->id.bustype = BUS_HOST;
97 input->id.vendor = 0x0001;
98 input->id.product = 0x0001;
99 input->id.version = 0x0100;
101 ddata->input = input;
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;
109 bdata->input = input;
110 setup_timer(&bdata->timer,
111 gpio_check_button, (unsigned long)bdata);
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;
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;
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;
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;
151 if (button->wakeup)
152 wakeup = 1;
154 input_set_capability(input, type, button->code);
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;
164 device_init_wakeup(&pdev->dev, wakeup);
166 return 0;
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);
176 platform_set_drvdata(pdev, NULL);
177 fail1:
178 input_free_device(input);
179 kfree(ddata);
181 return error;
184 static int __devexit gpio_keys_remove(struct platform_device *pdev)
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;
191 device_init_wakeup(&pdev->dev, 0);
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);
201 input_unregister_device(input);
203 return 0;
207 #ifdef CONFIG_PM
208 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
210 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
211 int i;
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);
223 return 0;
226 static int gpio_keys_resume(struct platform_device *pdev)
228 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
229 int i;
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);
241 return 0;
243 #else
244 #define gpio_keys_suspend NULL
245 #define gpio_keys_resume NULL
246 #endif
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,
259 static int __init gpio_keys_init(void)
261 return platform_driver_register(&gpio_keys_device_driver);
264 static void __exit gpio_keys_exit(void)
266 platform_driver_unregister(&gpio_keys_device_driver);
269 module_init(gpio_keys_init);
270 module_exit(gpio_keys_exit);
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");