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>
23 #include <linux/input.h>
24 #include <linux/input-polldev.h>
25 #include <linux/ioport.h>
26 #include <linux/platform_device.h>
27 #include <linux/gpio.h>
28 #include <linux/gpio_buttons.h>
30 #define DRV_NAME "gpio-buttons"
32 struct gpio_button_data
{
38 struct gpio_buttons_dev
{
39 struct input_polled_dev
*poll_dev
;
40 struct gpio_buttons_platform_data
*pdata
;
41 struct gpio_button_data
*data
;
44 static void gpio_buttons_check_state(struct input_dev
*input
,
45 struct gpio_button
*button
,
46 struct gpio_button_data
*bdata
)
51 state
= !!gpio_get_value_cansleep(button
->gpio
);
53 state
= !!gpio_get_value(button
->gpio
);
55 if (state
!= bdata
->last_state
) {
56 unsigned int type
= button
->type
?: EV_KEY
;
58 input_event(input
, type
, button
->code
,
59 !!(state
^ button
->active_low
));
62 bdata
->last_state
= state
;
66 static void gpio_buttons_poll(struct input_polled_dev
*dev
)
68 struct gpio_buttons_dev
*bdev
= dev
->private;
69 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
70 struct input_dev
*input
= dev
->input
;
73 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
74 struct gpio_button
*button
= &pdata
->buttons
[i
];
75 struct gpio_button_data
*bdata
= &bdev
->data
[i
];
77 if (bdata
->count
< button
->threshold
)
80 gpio_buttons_check_state(input
, button
, bdata
);
85 static int __devinit
gpio_buttons_probe(struct platform_device
*pdev
)
87 struct gpio_buttons_platform_data
*pdata
= pdev
->dev
.platform_data
;
88 struct device
*dev
= &pdev
->dev
;
89 struct gpio_buttons_dev
*bdev
;
90 struct input_polled_dev
*poll_dev
;
91 struct input_dev
*input
;
98 bdev
= kzalloc(sizeof(struct gpio_buttons_dev
) +
99 pdata
->nbuttons
* sizeof(struct gpio_button_data
),
102 dev_err(dev
, "no memory for private data\n");
106 bdev
->data
= (struct gpio_button_data
*) &bdev
[1];
108 poll_dev
= input_allocate_polled_device();
110 dev_err(dev
, "no memory for polled device\n");
115 poll_dev
->private = bdev
;
116 poll_dev
->poll
= gpio_buttons_poll
;
117 poll_dev
->poll_interval
= pdata
->poll_interval
;
119 input
= poll_dev
->input
;
121 input
->evbit
[0] = BIT(EV_KEY
);
122 input
->name
= pdev
->name
;
123 input
->phys
= "gpio-buttons/input0";
124 input
->dev
.parent
= &pdev
->dev
;
126 input
->id
.bustype
= BUS_HOST
;
127 input
->id
.vendor
= 0x0001;
128 input
->id
.product
= 0x0001;
129 input
->id
.version
= 0x0100;
131 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
132 struct gpio_button
*button
= &pdata
->buttons
[i
];
133 unsigned int gpio
= button
->gpio
;
134 unsigned int type
= button
->type
?: EV_KEY
;
136 error
= gpio_request(gpio
,
137 button
->desc
? button
->desc
: DRV_NAME
);
139 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
144 error
= gpio_direction_input(gpio
);
147 "unable to set direction on gpio %u, err=%d\n",
152 bdev
->data
[i
].can_sleep
= gpio_cansleep(gpio
);
153 bdev
->data
[i
].last_state
= -1;
155 input_set_capability(input
, type
, button
->code
);
158 bdev
->poll_dev
= poll_dev
;
160 platform_set_drvdata(pdev
, bdev
);
162 error
= input_register_polled_device(poll_dev
);
164 dev_err(dev
, "unable to register polled device, err=%d\n",
169 /* report initial state of the buttons */
170 for (i
= 0; i
< pdata
->nbuttons
; i
++)
171 gpio_buttons_check_state(input
, &pdata
->buttons
[i
],
177 for (i
= i
- 1; i
>= 0; i
--)
178 gpio_free(pdata
->buttons
[i
].gpio
);
180 input_free_polled_device(poll_dev
);
185 platform_set_drvdata(pdev
, NULL
);
189 static int __devexit
gpio_buttons_remove(struct platform_device
*pdev
)
191 struct gpio_buttons_dev
*bdev
= platform_get_drvdata(pdev
);
192 struct gpio_buttons_platform_data
*pdata
= bdev
->pdata
;
195 input_unregister_polled_device(bdev
->poll_dev
);
197 for (i
= 0; i
< pdata
->nbuttons
; i
++)
198 gpio_free(pdata
->buttons
[i
].gpio
);
200 input_free_polled_device(bdev
->poll_dev
);
203 platform_set_drvdata(pdev
, NULL
);
208 static struct platform_driver gpio_buttons_driver
= {
209 .probe
= gpio_buttons_probe
,
210 .remove
= __devexit_p(gpio_buttons_remove
),
213 .owner
= THIS_MODULE
,
217 static int __init
gpio_buttons_init(void)
219 return platform_driver_register(&gpio_buttons_driver
);
222 static void __exit
gpio_buttons_exit(void)
224 platform_driver_unregister(&gpio_buttons_driver
);
227 module_init(gpio_buttons_init
);
228 module_exit(gpio_buttons_exit
);
230 MODULE_LICENSE("GPL v2");
231 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
232 MODULE_DESCRIPTION("Polled GPIO Buttons driver");