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 timer_list timer
;
33 struct work_struct work
;
36 struct gpio_keys_drvdata
{
37 struct input_dev
*input
;
38 struct gpio_button_data data
[0];
41 static void gpio_keys_report_event(struct work_struct
*work
)
43 struct gpio_button_data
*bdata
=
44 container_of(work
, struct gpio_button_data
, work
);
45 struct gpio_keys_button
*button
= bdata
->button
;
46 struct input_dev
*input
= bdata
->input
;
47 unsigned int type
= button
->type
?: EV_KEY
;
48 int state
= (gpio_get_value(button
->gpio
) ? 1 : 0) ^ button
->active_low
;
50 input_event(input
, type
, button
->code
, !!state
);
54 static void gpio_keys_timer(unsigned long _data
)
56 struct gpio_button_data
*data
= (struct gpio_button_data
*)_data
;
58 schedule_work(&data
->work
);
61 static irqreturn_t
gpio_keys_isr(int irq
, void *dev_id
)
63 struct gpio_button_data
*bdata
= dev_id
;
64 struct gpio_keys_button
*button
= bdata
->button
;
66 BUG_ON(irq
!= gpio_to_irq(button
->gpio
));
68 if (button
->debounce_interval
)
69 mod_timer(&bdata
->timer
,
70 jiffies
+ msecs_to_jiffies(button
->debounce_interval
));
72 schedule_work(&bdata
->work
);
77 static int __devinit
gpio_keys_probe(struct platform_device
*pdev
)
79 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
80 struct gpio_keys_drvdata
*ddata
;
81 struct input_dev
*input
;
85 ddata
= kzalloc(sizeof(struct gpio_keys_drvdata
) +
86 pdata
->nbuttons
* sizeof(struct gpio_button_data
),
88 input
= input_allocate_device();
89 if (!ddata
|| !input
) {
94 platform_set_drvdata(pdev
, ddata
);
96 input
->name
= pdev
->name
;
97 input
->phys
= "gpio-keys/input0";
98 input
->dev
.parent
= &pdev
->dev
;
100 input
->id
.bustype
= BUS_HOST
;
101 input
->id
.vendor
= 0x0001;
102 input
->id
.product
= 0x0001;
103 input
->id
.version
= 0x0100;
105 /* Enable auto repeat feature of Linux input subsystem */
107 __set_bit(EV_REP
, input
->evbit
);
109 ddata
->input
= input
;
111 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
112 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
113 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
115 unsigned int type
= button
->type
?: EV_KEY
;
117 bdata
->input
= input
;
118 bdata
->button
= button
;
119 setup_timer(&bdata
->timer
,
120 gpio_keys_timer
, (unsigned long)bdata
);
121 INIT_WORK(&bdata
->work
, gpio_keys_report_event
);
123 error
= gpio_request(button
->gpio
, button
->desc
?: "gpio_keys");
125 pr_err("gpio-keys: failed to request GPIO %d,"
126 " error %d\n", button
->gpio
, error
);
130 error
= gpio_direction_input(button
->gpio
);
132 pr_err("gpio-keys: failed to configure input"
133 " direction for GPIO %d, error %d\n",
134 button
->gpio
, error
);
135 gpio_free(button
->gpio
);
139 irq
= gpio_to_irq(button
->gpio
);
142 pr_err("gpio-keys: Unable to get irq number"
143 " for GPIO %d, error %d\n",
144 button
->gpio
, error
);
145 gpio_free(button
->gpio
);
149 error
= request_irq(irq
, gpio_keys_isr
,
150 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
151 button
->desc
? button
->desc
: "gpio_keys",
154 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
156 gpio_free(button
->gpio
);
163 input_set_capability(input
, type
, button
->code
);
166 error
= input_register_device(input
);
168 pr_err("gpio-keys: Unable to register input device, "
169 "error: %d\n", error
);
173 device_init_wakeup(&pdev
->dev
, wakeup
);
179 free_irq(gpio_to_irq(pdata
->buttons
[i
].gpio
), &ddata
->data
[i
]);
180 if (pdata
->buttons
[i
].debounce_interval
)
181 del_timer_sync(&ddata
->data
[i
].timer
);
182 cancel_work_sync(&ddata
->data
[i
].work
);
183 gpio_free(pdata
->buttons
[i
].gpio
);
186 platform_set_drvdata(pdev
, NULL
);
188 input_free_device(input
);
194 static int __devexit
gpio_keys_remove(struct platform_device
*pdev
)
196 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
197 struct gpio_keys_drvdata
*ddata
= platform_get_drvdata(pdev
);
198 struct input_dev
*input
= ddata
->input
;
201 device_init_wakeup(&pdev
->dev
, 0);
203 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
204 int irq
= gpio_to_irq(pdata
->buttons
[i
].gpio
);
205 free_irq(irq
, &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 input_unregister_device(input
);
219 static int gpio_keys_suspend(struct device
*dev
)
221 struct platform_device
*pdev
= to_platform_device(dev
);
222 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
225 if (device_may_wakeup(&pdev
->dev
)) {
226 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
227 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
228 if (button
->wakeup
) {
229 int irq
= gpio_to_irq(button
->gpio
);
230 enable_irq_wake(irq
);
238 static int gpio_keys_resume(struct device
*dev
)
240 struct platform_device
*pdev
= to_platform_device(dev
);
241 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
244 if (device_may_wakeup(&pdev
->dev
)) {
245 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
246 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
247 if (button
->wakeup
) {
248 int irq
= gpio_to_irq(button
->gpio
);
249 disable_irq_wake(irq
);
257 static const struct dev_pm_ops gpio_keys_pm_ops
= {
258 .suspend
= gpio_keys_suspend
,
259 .resume
= gpio_keys_resume
,
263 static struct platform_driver gpio_keys_device_driver
= {
264 .probe
= gpio_keys_probe
,
265 .remove
= __devexit_p(gpio_keys_remove
),
268 .owner
= THIS_MODULE
,
270 .pm
= &gpio_keys_pm_ops
,
275 static int __init
gpio_keys_init(void)
277 return platform_driver_register(&gpio_keys_device_driver
);
280 static void __exit
gpio_keys_exit(void)
282 platform_driver_unregister(&gpio_keys_device_driver
);
285 module_init(gpio_keys_init
);
286 module_exit(gpio_keys_exit
);
288 MODULE_LICENSE("GPL");
289 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
290 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
291 MODULE_ALIAS("platform:gpio-keys");