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>
22 #include <linux/slab.h>
24 #include <linux/input.h>
25 #include <linux/input-polldev.h>
26 #include <linux/ioport.h>
27 #include <linux/platform_device.h>
29 #include <linux/gpio_buttons.h>
33 #define DRV_NAME "gpio-buttons"
34 #define DRV_VERSION "0.1.2"
35 #define PFX DRV_NAME ": "
37 struct gpio_button_data
{
42 struct gpio_buttons_dev
{
43 struct input_polled_dev
*poll_dev
;
44 struct gpio_buttons_platform_data
*pdata
;
45 struct gpio_button_data
*data
;
48 static void gpio_buttons_poll(struct input_polled_dev
*dev
)
50 struct gpio_buttons_dev
*bdev
= dev
->private;
51 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
52 struct input_dev
*input
= dev
->input
;
55 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
56 struct gpio_button
*button
= &pdata
->buttons
[i
];
57 unsigned int type
= button
->type
?: EV_KEY
;
60 if (bdev
->data
[i
].count
< button
->threshold
) {
61 bdev
->data
[i
].count
++;
65 state
= gpio_get_value(button
->gpio
) ? 1 : 0;
66 if (state
!= bdev
->data
[i
].last_state
) {
67 input_event(input
, type
, button
->code
,
68 !!(state
^ button
->active_low
));
70 bdev
->data
[i
].count
= 0;
71 bdev
->data
[i
].last_state
= state
;
76 static int __devinit
gpio_buttons_probe(struct platform_device
*pdev
)
78 struct gpio_buttons_platform_data
*pdata
= pdev
->dev
.platform_data
;
79 struct gpio_buttons_dev
*bdev
;
80 struct input_polled_dev
*poll_dev
;
81 struct input_dev
*input
;
87 bdev
= kzalloc(sizeof(struct gpio_buttons_dev
) +
88 sizeof(struct gpio_button_data
) * pdata
->nbuttons
,
91 printk(KERN_ERR DRV_NAME
"no memory for device\n");
95 bdev
->data
= (struct gpio_button_data
*) &bdev
[1];
97 poll_dev
= input_allocate_polled_device();
99 printk(KERN_ERR DRV_NAME
"no memory for polled device\n");
104 poll_dev
->private = bdev
;
105 poll_dev
->poll
= gpio_buttons_poll
;
106 poll_dev
->poll_interval
= pdata
->poll_interval
;
108 input
= poll_dev
->input
;
110 input
->evbit
[0] = BIT(EV_KEY
);
111 input
->name
= pdev
->name
;
112 input
->phys
= "gpio-buttons/input0";
113 input
->dev
.parent
= &pdev
->dev
;
115 input
->id
.bustype
= BUS_HOST
;
116 input
->id
.vendor
= 0x0001;
117 input
->id
.product
= 0x0001;
118 input
->id
.version
= 0x0100;
120 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
121 struct gpio_button
*button
= &pdata
->buttons
[i
];
122 unsigned int gpio
= button
->gpio
;
123 unsigned int type
= button
->type
?: EV_KEY
;
125 error
= gpio_request(gpio
, button
->desc
?
126 button
->desc
: DRV_NAME
);
128 printk(KERN_ERR PFX
"unable to claim gpio %u, "
129 "error %d\n", gpio
, error
);
133 error
= gpio_direction_input(gpio
);
135 printk(KERN_ERR PFX
"unable to set direction on "
136 "gpio %u, error %d\n", gpio
, error
);
140 input_set_capability(input
, type
, button
->code
);
141 bdev
->data
[i
].last_state
= gpio_get_value(button
->gpio
) ? 1 : 0;
144 bdev
->poll_dev
= poll_dev
;
146 platform_set_drvdata(pdev
, bdev
);
148 error
= input_register_polled_device(poll_dev
);
150 printk(KERN_ERR PFX
"unable to register polled device, "
151 "error %d\n", error
);
158 for (i
= i
- 1; i
>= 0; i
--)
159 gpio_free(pdata
->buttons
[i
].gpio
);
161 input_free_polled_device(poll_dev
);
166 platform_set_drvdata(pdev
, NULL
);
170 static int __devexit
gpio_buttons_remove(struct platform_device
*pdev
)
172 struct gpio_buttons_dev
*bdev
= platform_get_drvdata(pdev
);
173 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
176 input_unregister_polled_device(bdev
->poll_dev
);
178 for (i
= 0; i
< pdata
->nbuttons
; i
++)
179 gpio_free(pdata
->buttons
[i
].gpio
);
181 input_free_polled_device(bdev
->poll_dev
);
184 platform_set_drvdata(pdev
, NULL
);
189 static struct platform_driver gpio_buttons_driver
= {
190 .probe
= gpio_buttons_probe
,
191 .remove
= __devexit_p(gpio_buttons_remove
),
194 .owner
= THIS_MODULE
,
198 static int __init
gpio_buttons_init(void)
200 printk(KERN_INFO DRV_NAME
" driver version " DRV_VERSION
"\n");
201 return platform_driver_register(&gpio_buttons_driver
);
204 static void __exit
gpio_buttons_exit(void)
206 platform_driver_unregister(&gpio_buttons_driver
);
209 module_init(gpio_buttons_init
);
210 module_exit(gpio_buttons_exit
);
212 MODULE_LICENSE("GPL");
213 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
214 MODULE_VERSION(DRV_VERSION
);
215 MODULE_DESCRIPTION("Polled buttons driver for CPU GPIOs");