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.
11 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.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>
29 struct gpio_button_data {
30 struct gpio_keys_button *button;
31 struct input_dev *input;
32 struct delayed_work 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 work_struct *work)
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;
49 input_event(input, type, button->code, !!state);
53 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
55 struct gpio_button_data *bdata = dev_id;
56 struct gpio_keys_button *button = bdata->button;
59 BUG_ON(irq != gpio_to_irq(button->gpio));
61 delay = button->debounce_interval ?
62 msecs_to_jiffies(button->debounce_interval) : 0;
63 schedule_delayed_work(&bdata->work, delay);
68 static int __devinit gpio_keys_probe(struct platform_device *pdev)
70 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
71 struct gpio_keys_drvdata *ddata;
72 struct input_dev *input;
76 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
77 pdata->nbuttons * sizeof(struct gpio_button_data),
79 input = input_allocate_device();
80 if (!ddata || !input) {
85 platform_set_drvdata(pdev, ddata);
87 input->name = pdev->name;
88 input->phys = "gpio-keys/input0";
89 input->dev.parent = &pdev->dev;
91 input->id.bustype = BUS_HOST;
92 input->id.vendor = 0x0001;
93 input->id.product = 0x0001;
94 input->id.version = 0x0100;
96 /* Enable auto repeat feature of Linux input subsystem */
98 __set_bit(EV_REP, input->evbit);
100 ddata->input = input;
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];
106 unsigned int type = button->type ?: EV_KEY;
108 bdata->input = input;
109 bdata->button = button;
110 INIT_DELAYED_WORK(&bdata->work, gpio_keys_report_event);
112 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
114 pr_err("gpio-keys: failed to request GPIO %d,"
115 " error %d\n", button->gpio, error);
119 error = gpio_direction_input(button->gpio);
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);
128 irq = gpio_to_irq(button->gpio);
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);
138 error = request_irq(irq, gpio_keys_isr,
139 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
140 button->desc ? button->desc : "gpio_keys",
143 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
145 gpio_free(button->gpio);
152 input_set_capability(input, type, button->code);
155 error = input_register_device(input);
157 pr_err("gpio-keys: Unable to register input device, "
158 "error: %d\n", error);
162 device_init_wakeup(&pdev->dev, wakeup);
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);
173 platform_set_drvdata(pdev, NULL);
175 input_free_device(input);
181 static int __devexit gpio_keys_remove(struct platform_device *pdev)
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;
188 device_init_wakeup(&pdev->dev, 0);
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);
197 input_unregister_device(input);
204 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
206 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
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);
222 static int gpio_keys_resume(struct platform_device *pdev)
224 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
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);
240 #define gpio_keys_suspend NULL
241 #define gpio_keys_resume NULL
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,
251 .owner = THIS_MODULE,
255 static int __init gpio_keys_init(void)
257 return platform_driver_register(&gpio_keys_device_driver);
260 static void __exit gpio_keys_exit(void)
262 platform_driver_unregister(&gpio_keys_device_driver);
265 module_init(gpio_keys_init);
266 module_exit(gpio_keys_exit);
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");