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>
28 struct gpio_button_data
{
29 struct gpio_keys_button
*button
;
30 struct input_dev
*input
;
31 struct timer_list timer
;
34 struct gpio_keys_drvdata
{
35 struct input_dev
*input
;
36 struct gpio_button_data data
[0];
39 static void gpio_keys_report_event(struct gpio_button_data
*bdata
)
41 struct gpio_keys_button
*button
= bdata
->button
;
42 struct input_dev
*input
= bdata
->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
);
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
);
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
));
68 gpio_keys_report_event(bdata
);
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
;
81 ddata
= kzalloc(sizeof(struct gpio_keys_drvdata
) +
82 pdata
->nbuttons
* sizeof(struct gpio_button_data
),
84 input
= input_allocate_device();
85 if (!ddata
|| !input
) {
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 /* Enable auto repeat feature of Linux input subsystem */
103 __set_bit(EV_REP
, input
->evbit
);
105 ddata
->input
= input
;
107 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
108 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
109 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
111 unsigned int type
= button
->type
?: EV_KEY
;
113 bdata
->input
= input
;
114 bdata
->button
= button
;
115 setup_timer(&bdata
->timer
,
116 gpio_check_button
, (unsigned long)bdata
);
118 error
= gpio_request(button
->gpio
, button
->desc
?: "gpio_keys");
120 pr_err("gpio-keys: failed to request GPIO %d,"
121 " error %d\n", button
->gpio
, error
);
125 error
= gpio_direction_input(button
->gpio
);
127 pr_err("gpio-keys: failed to configure input"
128 " direction for GPIO %d, error %d\n",
129 button
->gpio
, error
);
130 gpio_free(button
->gpio
);
134 irq
= gpio_to_irq(button
->gpio
);
137 pr_err("gpio-keys: Unable to get irq number"
138 " for GPIO %d, error %d\n",
139 button
->gpio
, error
);
140 gpio_free(button
->gpio
);
144 error
= request_irq(irq
, gpio_keys_isr
,
145 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
146 button
->desc
? button
->desc
: "gpio_keys",
149 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
151 gpio_free(button
->gpio
);
158 input_set_capability(input
, type
, button
->code
);
161 error
= input_register_device(input
);
163 pr_err("gpio-keys: Unable to register input device, "
164 "error: %d\n", error
);
168 device_init_wakeup(&pdev
->dev
, wakeup
);
174 free_irq(gpio_to_irq(pdata
->buttons
[i
].gpio
), &ddata
->data
[i
]);
175 if (pdata
->buttons
[i
].debounce_interval
)
176 del_timer_sync(&ddata
->data
[i
].timer
);
177 gpio_free(pdata
->buttons
[i
].gpio
);
180 platform_set_drvdata(pdev
, NULL
);
182 input_free_device(input
);
188 static int __devexit
gpio_keys_remove(struct platform_device
*pdev
)
190 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
191 struct gpio_keys_drvdata
*ddata
= platform_get_drvdata(pdev
);
192 struct input_dev
*input
= ddata
->input
;
195 device_init_wakeup(&pdev
->dev
, 0);
197 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
198 int irq
= gpio_to_irq(pdata
->buttons
[i
].gpio
);
199 free_irq(irq
, &ddata
->data
[i
]);
200 if (pdata
->buttons
[i
].debounce_interval
)
201 del_timer_sync(&ddata
->data
[i
].timer
);
202 gpio_free(pdata
->buttons
[i
].gpio
);
205 input_unregister_device(input
);
212 static int gpio_keys_suspend(struct platform_device
*pdev
, pm_message_t state
)
214 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
217 if (device_may_wakeup(&pdev
->dev
)) {
218 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
219 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
220 if (button
->wakeup
) {
221 int irq
= gpio_to_irq(button
->gpio
);
222 enable_irq_wake(irq
);
230 static int gpio_keys_resume(struct platform_device
*pdev
)
232 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
235 if (device_may_wakeup(&pdev
->dev
)) {
236 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
237 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
238 if (button
->wakeup
) {
239 int irq
= gpio_to_irq(button
->gpio
);
240 disable_irq_wake(irq
);
248 #define gpio_keys_suspend NULL
249 #define gpio_keys_resume NULL
252 static struct platform_driver gpio_keys_device_driver
= {
253 .probe
= gpio_keys_probe
,
254 .remove
= __devexit_p(gpio_keys_remove
),
255 .suspend
= gpio_keys_suspend
,
256 .resume
= gpio_keys_resume
,
259 .owner
= THIS_MODULE
,
263 static int __init
gpio_keys_init(void)
265 return platform_driver_register(&gpio_keys_device_driver
);
268 static void __exit
gpio_keys_exit(void)
270 platform_driver_unregister(&gpio_keys_device_driver
);
273 module_init(gpio_keys_init
);
274 module_exit(gpio_keys_exit
);
276 MODULE_LICENSE("GPL");
277 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
278 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
279 MODULE_ALIAS("platform:gpio-keys");