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 struct gpio_button_data
{
30 struct gpio_keys_button
*button
;
31 struct input_dev
*input
;
32 struct timer_list timer
;
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_keys_button
*button
,
41 struct input_dev
*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
->button
, data
->input
);
57 static irqreturn_t
gpio_keys_isr(int irq
, void *dev_id
)
59 struct platform_device
*pdev
= dev_id
;
60 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
61 struct gpio_keys_drvdata
*ddata
= platform_get_drvdata(pdev
);
64 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
65 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
67 if (irq
== gpio_to_irq(button
->gpio
)) {
68 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
70 if (button
->debounce_interval
)
71 mod_timer(&bdata
->timer
,
73 msecs_to_jiffies(button
->debounce_interval
));
75 gpio_keys_report_event(button
, bdata
->input
);
84 static int __devinit
gpio_keys_probe(struct platform_device
*pdev
)
86 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
87 struct gpio_keys_drvdata
*ddata
;
88 struct input_dev
*input
;
92 ddata
= kzalloc(sizeof(struct gpio_keys_drvdata
) +
93 pdata
->nbuttons
* sizeof(struct gpio_button_data
),
95 input
= input_allocate_device();
96 if (!ddata
|| !input
) {
101 platform_set_drvdata(pdev
, ddata
);
103 input
->name
= pdev
->name
;
104 input
->phys
= "gpio-keys/input0";
105 input
->dev
.parent
= &pdev
->dev
;
107 input
->id
.bustype
= BUS_HOST
;
108 input
->id
.vendor
= 0x0001;
109 input
->id
.product
= 0x0001;
110 input
->id
.version
= 0x0100;
112 ddata
->input
= input
;
114 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
115 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
116 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
118 unsigned int type
= button
->type
?: EV_KEY
;
120 bdata
->input
= input
;
121 setup_timer(&bdata
->timer
,
122 gpio_check_button
, (unsigned long)bdata
);
124 error
= gpio_request(button
->gpio
, button
->desc
?: "gpio_keys");
126 pr_err("gpio-keys: failed to request GPIO %d,"
127 " error %d\n", button
->gpio
, error
);
131 error
= gpio_direction_input(button
->gpio
);
133 pr_err("gpio-keys: failed to configure input"
134 " direction for GPIO %d, error %d\n",
135 button
->gpio
, error
);
136 gpio_free(button
->gpio
);
140 irq
= gpio_to_irq(button
->gpio
);
143 pr_err("gpio-keys: Unable to get irq number"
144 " for GPIO %d, error %d\n",
145 button
->gpio
, error
);
146 gpio_free(button
->gpio
);
150 error
= request_irq(irq
, gpio_keys_isr
,
151 IRQF_SAMPLE_RANDOM
| IRQF_TRIGGER_RISING
|
152 IRQF_TRIGGER_FALLING
,
153 button
->desc
? button
->desc
: "gpio_keys",
156 pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
158 gpio_free(button
->gpio
);
165 input_set_capability(input
, type
, button
->code
);
168 error
= input_register_device(input
);
170 pr_err("gpio-keys: Unable to register input device, "
171 "error: %d\n", error
);
175 device_init_wakeup(&pdev
->dev
, wakeup
);
181 free_irq(gpio_to_irq(pdata
->buttons
[i
].gpio
), pdev
);
182 if (pdata
->buttons
[i
].debounce_interval
)
183 del_timer_sync(&ddata
->data
[i
].timer
);
184 gpio_free(pdata
->buttons
[i
].gpio
);
187 platform_set_drvdata(pdev
, NULL
);
189 input_free_device(input
);
195 static int __devexit
gpio_keys_remove(struct platform_device
*pdev
)
197 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
198 struct gpio_keys_drvdata
*ddata
= platform_get_drvdata(pdev
);
199 struct input_dev
*input
= ddata
->input
;
202 device_init_wakeup(&pdev
->dev
, 0);
204 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
205 int irq
= gpio_to_irq(pdata
->buttons
[i
].gpio
);
207 if (pdata
->buttons
[i
].debounce_interval
)
208 del_timer_sync(&ddata
->data
[i
].timer
);
209 gpio_free(pdata
->buttons
[i
].gpio
);
212 input_unregister_device(input
);
219 static int gpio_keys_suspend(struct platform_device
*pdev
, pm_message_t state
)
221 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
224 if (device_may_wakeup(&pdev
->dev
)) {
225 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
226 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
227 if (button
->wakeup
) {
228 int irq
= gpio_to_irq(button
->gpio
);
229 enable_irq_wake(irq
);
237 static int gpio_keys_resume(struct platform_device
*pdev
)
239 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
242 if (device_may_wakeup(&pdev
->dev
)) {
243 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
244 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
245 if (button
->wakeup
) {
246 int irq
= gpio_to_irq(button
->gpio
);
247 disable_irq_wake(irq
);
255 #define gpio_keys_suspend NULL
256 #define gpio_keys_resume NULL
259 struct platform_driver gpio_keys_device_driver
= {
260 .probe
= gpio_keys_probe
,
261 .remove
= __devexit_p(gpio_keys_remove
),
262 .suspend
= gpio_keys_suspend
,
263 .resume
= gpio_keys_resume
,
266 .owner
= THIS_MODULE
,
270 static int __init
gpio_keys_init(void)
272 return platform_driver_register(&gpio_keys_device_driver
);
275 static void __exit
gpio_keys_exit(void)
277 platform_driver_unregister(&gpio_keys_device_driver
);
280 module_init(gpio_keys_init
);
281 module_exit(gpio_keys_exit
);
283 MODULE_LICENSE("GPL");
284 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
285 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
286 MODULE_ALIAS("platform:gpio-keys");