2 * Driver for buttons on GPIO lines not capable of generating interrupts
4 * Copyright (C) 2007,2008 Gabor Juhos <juhosg at openwrt.org>
6 * This file was based on: /drivers/input/misc/cobalt_btns.c
7 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
9 * also was based on: /drivers/input/keyboard/gpio_keys.c
10 * Copyright 2005 Phil Blundell
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
22 #include <linux/input.h>
23 #include <linux/input-polldev.h>
24 #include <linux/ioport.h>
25 #include <linux/platform_device.h>
27 #include <linux/gpio_buttons.h>
31 #define DRV_NAME "gpio-buttons"
32 #define DRV_VERSION "0.1.1"
33 #define PFX DRV_NAME ": "
35 struct gpio_buttons_dev
{
36 struct input_polled_dev
*poll_dev
;
37 struct gpio_buttons_platform_data
*pdata
;
40 static void gpio_buttons_poll(struct input_polled_dev
*dev
)
42 struct gpio_buttons_dev
*bdev
= dev
->private;
43 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
44 struct input_dev
*input
= dev
->input
;
47 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
48 struct gpio_button
*button
= &pdata
->buttons
[i
];
49 unsigned int type
= button
->type
?: EV_KEY
;
52 state
= gpio_get_value(button
->gpio
) ? 1 : 0;
53 state
^= button
->active_low
;
58 if (button
->count
>= button
->threshold
) {
59 input_event(input
, type
, button
->code
, 1);
65 if (button
->count
== button
->threshold
) {
66 input_event(input
, type
, button
->code
, 0);
72 static int __devinit
gpio_buttons_probe(struct platform_device
*pdev
)
74 struct gpio_buttons_platform_data
*pdata
= pdev
->dev
.platform_data
;
75 struct gpio_buttons_dev
*bdev
;
76 struct input_polled_dev
*poll_dev
;
77 struct input_dev
*input
;
84 bdev
= kzalloc(sizeof(*bdev
), GFP_KERNEL
);
86 printk(KERN_ERR DRV_NAME
"no memory for device\n");
90 poll_dev
= input_allocate_polled_device();
92 printk(KERN_ERR DRV_NAME
"no memory for polled device\n");
97 poll_dev
->private = bdev
;
98 poll_dev
->poll
= gpio_buttons_poll
;
99 poll_dev
->poll_interval
= pdata
->poll_interval
;
101 input
= poll_dev
->input
;
103 input
->evbit
[0] = BIT(EV_KEY
);
104 input
->name
= pdev
->name
;
105 input
->phys
= "gpio-buttons/input0";
106 input
->dev
.parent
= &pdev
->dev
;
108 input
->id
.bustype
= BUS_HOST
;
109 input
->id
.vendor
= 0x0001;
110 input
->id
.product
= 0x0001;
111 input
->id
.version
= 0x0100;
113 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
114 struct gpio_button
*button
= &pdata
->buttons
[i
];
115 unsigned int gpio
= button
->gpio
;
116 unsigned int type
= button
->type
?: EV_KEY
;
118 error
= gpio_request(gpio
, button
->desc
?
119 button
->desc
: DRV_NAME
);
121 printk(KERN_ERR PFX
"unable to claim gpio %u, "
122 "error %d\n", gpio
, error
);
126 error
= gpio_direction_input(gpio
);
128 printk(KERN_ERR PFX
"unable to set direction on "
129 "gpio %u, error %d\n", gpio
, error
);
133 input_set_capability(input
, type
, button
->code
);
137 bdev
->poll_dev
= poll_dev
;
139 platform_set_drvdata(pdev
, bdev
);
141 error
= input_register_polled_device(poll_dev
);
143 printk(KERN_ERR PFX
"unable to register polled device, "
144 "error %d\n", error
);
151 for (i
= i
- 1; i
>= 0; i
--)
152 gpio_free(pdata
->buttons
[i
].gpio
);
154 input_free_polled_device(poll_dev
);
159 platform_set_drvdata(pdev
, NULL
);
163 static int __devexit
gpio_buttons_remove(struct platform_device
*pdev
)
165 struct gpio_buttons_dev
*bdev
= platform_get_drvdata(pdev
);
166 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
169 input_unregister_polled_device(bdev
->poll_dev
);
171 for (i
= 0; i
< pdata
->nbuttons
; i
++)
172 gpio_free(pdata
->buttons
[i
].gpio
);
174 input_free_polled_device(bdev
->poll_dev
);
177 platform_set_drvdata(pdev
, NULL
);
182 static struct platform_driver gpio_buttons_driver
= {
183 .probe
= gpio_buttons_probe
,
184 .remove
= __devexit_p(gpio_buttons_remove
),
187 .owner
= THIS_MODULE
,
191 static int __init
gpio_buttons_init(void)
193 printk(KERN_INFO DRV_NAME
" driver version " DRV_VERSION
"\n");
194 return platform_driver_register(&gpio_buttons_driver
);
197 static void __exit
gpio_buttons_exit(void)
199 platform_driver_unregister(&gpio_buttons_driver
);
202 module_init(gpio_buttons_init
);
203 module_exit(gpio_buttons_exit
);
205 MODULE_LICENSE("GPL");
206 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
207 MODULE_VERSION(DRV_VERSION
);
208 MODULE_DESCRIPTION("Polled buttons driver for CPU GPIOs");