2 * Driver for buttons on GPIO lines not capable of generating interrupts
4 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
7 * This file was based on: /drivers/input/misc/cobalt_btns.c
8 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
10 * also was based on: /drivers/input/keyboard/gpio_keys.c
11 * Copyright 2005 Phil Blundell
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
23 #include <linux/input.h>
24 #include <linux/input-polldev.h>
25 #include <linux/ioport.h>
26 #include <linux/platform_device.h>
28 #include <linux/gpio_buttons.h>
32 #define DRV_NAME "gpio-buttons"
33 #define DRV_VERSION "0.1.2"
34 #define PFX DRV_NAME ": "
36 struct gpio_button_data
{
41 struct gpio_buttons_dev
{
42 struct input_polled_dev
*poll_dev
;
43 struct gpio_buttons_platform_data
*pdata
;
44 struct gpio_button_data
*data
;
47 static void gpio_buttons_poll(struct input_polled_dev
*dev
)
49 struct gpio_buttons_dev
*bdev
= dev
->private;
50 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
51 struct input_dev
*input
= dev
->input
;
54 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
55 struct gpio_button
*button
= &pdata
->buttons
[i
];
56 unsigned int type
= button
->type
?: EV_KEY
;
59 if (bdev
->data
[i
].count
< button
->threshold
) {
60 bdev
->data
[i
].count
++;
64 state
= gpio_get_value(button
->gpio
) ? 1 : 0;
65 if (state
!= bdev
->data
[i
].last_state
) {
66 input_event(input
, type
, button
->code
,
67 !!(state
^ button
->active_low
));
69 bdev
->data
[i
].count
= 0;
70 bdev
->data
[i
].last_state
= state
;
75 static int __devinit
gpio_buttons_probe(struct platform_device
*pdev
)
77 struct gpio_buttons_platform_data
*pdata
= pdev
->dev
.platform_data
;
78 struct gpio_buttons_dev
*bdev
;
79 struct input_polled_dev
*poll_dev
;
80 struct input_dev
*input
;
86 bdev
= kzalloc(sizeof(struct gpio_buttons_dev
) +
87 sizeof(struct gpio_button_data
) * pdata
->nbuttons
,
90 printk(KERN_ERR DRV_NAME
"no memory for device\n");
94 bdev
->data
= (struct gpio_button_data
*) &bdev
[1];
96 poll_dev
= input_allocate_polled_device();
98 printk(KERN_ERR DRV_NAME
"no memory for polled device\n");
103 poll_dev
->private = bdev
;
104 poll_dev
->poll
= gpio_buttons_poll
;
105 poll_dev
->poll_interval
= pdata
->poll_interval
;
107 input
= poll_dev
->input
;
109 input
->evbit
[0] = BIT(EV_KEY
);
110 input
->name
= pdev
->name
;
111 input
->phys
= "gpio-buttons/input0";
112 input
->dev
.parent
= &pdev
->dev
;
114 input
->id
.bustype
= BUS_HOST
;
115 input
->id
.vendor
= 0x0001;
116 input
->id
.product
= 0x0001;
117 input
->id
.version
= 0x0100;
119 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
120 struct gpio_button
*button
= &pdata
->buttons
[i
];
121 unsigned int gpio
= button
->gpio
;
122 unsigned int type
= button
->type
?: EV_KEY
;
124 error
= gpio_request(gpio
, button
->desc
?
125 button
->desc
: DRV_NAME
);
127 printk(KERN_ERR PFX
"unable to claim gpio %u, "
128 "error %d\n", gpio
, error
);
132 error
= gpio_direction_input(gpio
);
134 printk(KERN_ERR PFX
"unable to set direction on "
135 "gpio %u, error %d\n", gpio
, error
);
139 input_set_capability(input
, type
, button
->code
);
140 bdev
->data
[i
].last_state
= gpio_get_value(button
->gpio
) ? 1 : 0;
143 bdev
->poll_dev
= poll_dev
;
145 platform_set_drvdata(pdev
, bdev
);
147 error
= input_register_polled_device(poll_dev
);
149 printk(KERN_ERR PFX
"unable to register polled device, "
150 "error %d\n", error
);
157 for (i
= i
- 1; i
>= 0; i
--)
158 gpio_free(pdata
->buttons
[i
].gpio
);
160 input_free_polled_device(poll_dev
);
165 platform_set_drvdata(pdev
, NULL
);
169 static int __devexit
gpio_buttons_remove(struct platform_device
*pdev
)
171 struct gpio_buttons_dev
*bdev
= platform_get_drvdata(pdev
);
172 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
175 input_unregister_polled_device(bdev
->poll_dev
);
177 for (i
= 0; i
< pdata
->nbuttons
; i
++)
178 gpio_free(pdata
->buttons
[i
].gpio
);
180 input_free_polled_device(bdev
->poll_dev
);
183 platform_set_drvdata(pdev
, NULL
);
188 static struct platform_driver gpio_buttons_driver
= {
189 .probe
= gpio_buttons_probe
,
190 .remove
= __devexit_p(gpio_buttons_remove
),
193 .owner
= THIS_MODULE
,
197 static int __init
gpio_buttons_init(void)
199 printk(KERN_INFO DRV_NAME
" driver version " DRV_VERSION
"\n");
200 return platform_driver_register(&gpio_buttons_driver
);
203 static void __exit
gpio_buttons_exit(void)
205 platform_driver_unregister(&gpio_buttons_driver
);
208 module_init(gpio_buttons_init
);
209 module_exit(gpio_buttons_exit
);
211 MODULE_LICENSE("GPL");
212 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
213 MODULE_VERSION(DRV_VERSION
);
214 MODULE_DESCRIPTION("Polled buttons driver for CPU GPIOs");