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>
12 #include <linux/version.h>
14 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/sched.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>
29 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
32 struct platform_device *pdev = dev_id;
33 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
34 struct input_dev *input = platform_get_drvdata(pdev);
36 for (i = 0; i < pdata->nbuttons; i++) {
37 struct gpio_keys_button *button = &pdata->buttons[i];
38 int gpio = button->gpio;
40 if (irq == gpio_to_irq(gpio)) {
41 unsigned int type = button->type ?: EV_KEY;
42 int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
44 input_event(input, type, button->code, !!state);
53 static int __devinit gpio_keys_probe(struct platform_device *pdev)
55 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
56 struct input_dev *input;
60 input = input_allocate_device();
64 platform_set_drvdata(pdev, input);
66 input->evbit[0] = BIT_MASK(EV_KEY);
68 input->name = pdev->name;
69 input->phys = "gpio-keys/input0";
70 input->dev.parent = &pdev->dev;
72 input->id.bustype = BUS_HOST;
73 input->id.vendor = 0x0001;
74 input->id.product = 0x0001;
75 input->id.version = 0x0100;
77 for (i = 0; i < pdata->nbuttons; i++) {
78 struct gpio_keys_button *button = &pdata->buttons[i];
80 unsigned int type = button->type ?: EV_KEY;
82 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
84 pr_err("gpio-keys: failed to request GPIO %d,"
85 " error %d\n", button->gpio, error);
89 error = gpio_direction_input(button->gpio);
91 pr_err("gpio-keys: failed to configure input"
92 " direction for GPIO %d, error %d\n",
94 gpio_free(button->gpio);
98 irq = gpio_to_irq(button->gpio);
101 pr_err("gpio-keys: Unable to get irq number"
102 " for GPIO %d, error %d\n",
103 button->gpio, error);
104 gpio_free(button->gpio);
108 error = request_irq(irq, gpio_keys_isr,
109 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
110 IRQF_TRIGGER_FALLING,
111 button->desc ? button->desc : "gpio_keys",
114 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
116 gpio_free(button->gpio);
123 input_set_capability(input, type, button->code);
126 error = input_register_device(input);
128 pr_err("gpio-keys: Unable to register input device, "
129 "error: %d\n", error);
133 device_init_wakeup(&pdev->dev, wakeup);
139 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
140 gpio_free(pdata->buttons[i].gpio);
143 platform_set_drvdata(pdev, NULL);
144 input_free_device(input);
149 static int __devexit gpio_keys_remove(struct platform_device *pdev)
151 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
152 struct input_dev *input = platform_get_drvdata(pdev);
155 device_init_wakeup(&pdev->dev, 0);
157 for (i = 0; i < pdata->nbuttons; i++) {
158 int irq = gpio_to_irq(pdata->buttons[i].gpio);
160 gpio_free(pdata->buttons[i].gpio);
163 input_unregister_device(input);
170 static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
172 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
175 if (device_may_wakeup(&pdev->dev)) {
176 for (i = 0; i < pdata->nbuttons; i++) {
177 struct gpio_keys_button *button = &pdata->buttons[i];
178 if (button->wakeup) {
179 int irq = gpio_to_irq(button->gpio);
180 enable_irq_wake(irq);
188 static int gpio_keys_resume(struct platform_device *pdev)
190 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
193 if (device_may_wakeup(&pdev->dev)) {
194 for (i = 0; i < pdata->nbuttons; i++) {
195 struct gpio_keys_button *button = &pdata->buttons[i];
196 if (button->wakeup) {
197 int irq = gpio_to_irq(button->gpio);
198 disable_irq_wake(irq);
206 #define gpio_keys_suspend NULL
207 #define gpio_keys_resume NULL
210 struct platform_driver gpio_keys_device_driver = {
211 .probe = gpio_keys_probe,
212 .remove = __devexit_p(gpio_keys_remove),
213 .suspend = gpio_keys_suspend,
214 .resume = gpio_keys_resume,
217 .owner = THIS_MODULE,
221 static int __init gpio_keys_init(void)
223 return platform_driver_register(&gpio_keys_device_driver);
226 static void __exit gpio_keys_exit(void)
228 platform_driver_unregister(&gpio_keys_device_driver);
231 module_init(gpio_keys_init);
232 module_exit(gpio_keys_exit);
234 MODULE_LICENSE("GPL");
235 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
236 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
237 MODULE_ALIAS("platform:gpio-keys");