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>
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
);
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
));
77 schedule_work(&bdata
->work
);
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";
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
);
94 dev_err(dev
, "failed to request GPIO %d, error %d\n",
99 error
= gpio_direction_input(button
->gpio
);
101 dev_err(dev
, "failed to configure"
102 " direction for GPIO %d, error %d\n",
103 button
->gpio
, error
);
107 irq
= gpio_to_irq(button
->gpio
);
110 dev_err(dev
, "Unable to get irq number for GPIO %d, error %d\n",
111 button
->gpio
, error
);
115 error
= request_irq(irq
, gpio_keys_isr
,
117 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
120 dev_err(dev
, "Unable to claim irq %d; error %d\n",
128 gpio_free(button
->gpio
);
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
;
142 ddata
= kzalloc(sizeof(struct gpio_keys_drvdata
) +
143 pdata
->nbuttons
* sizeof(struct gpio_button_data
),
145 input
= input_allocate_device();
146 if (!ddata
|| !input
) {
147 dev_err(dev
, "failed to allocate state\n");
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 */
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
);
184 input_set_capability(input
, type
, button
->code
);
187 error
= input_register_device(input
);
189 dev_err(dev
, "Unable to register input device, "
190 "error: %d\n", error
);
194 /* get current state of buttons */
195 for (i
= 0; i
< pdata
->nbuttons
; i
++)
196 gpio_keys_report_event(&ddata
->data
[i
]);
199 device_init_wakeup(&pdev
->dev
, wakeup
);
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
);
214 input_free_device(input
);
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
;
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
);
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
;
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
);
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
;
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
);
286 static const struct dev_pm_ops gpio_keys_pm_ops
= {
287 .suspend
= gpio_keys_suspend
,
288 .resume
= gpio_keys_resume
,
292 static struct platform_driver gpio_keys_device_driver
= {
293 .probe
= gpio_keys_probe
,
294 .remove
= __devexit_p(gpio_keys_remove
),
297 .owner
= THIS_MODULE
,
299 .pm
= &gpio_keys_pm_ops
,
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");