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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/input.h>
23 #include <linux/input-polldev.h>
24 #include <linux/ioport.h>
25 #include <linux/platform_device.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio_keys.h>
29 #define DRV_NAME "gpio-keys-polled"
31 struct gpio_keys_button_data
{
38 struct gpio_keys_polled_dev
{
39 struct input_polled_dev
*poll_dev
;
41 struct gpio_keys_platform_data
*pdata
;
42 struct gpio_keys_button_data data
[0];
45 static void gpio_keys_polled_check_state(struct input_dev
*input
,
46 struct gpio_keys_button
*button
,
47 struct gpio_keys_button_data
*bdata
)
52 state
= !!gpio_get_value_cansleep(button
->gpio
);
54 state
= !!gpio_get_value(button
->gpio
);
56 if (state
!= bdata
->last_state
) {
57 unsigned int type
= button
->type
?: EV_KEY
;
59 input_event(input
, type
, button
->code
,
60 !!(state
^ button
->active_low
));
63 bdata
->last_state
= state
;
67 static void gpio_keys_polled_poll(struct input_polled_dev
*dev
)
69 struct gpio_keys_polled_dev
*bdev
= dev
->private;
70 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
71 struct input_dev
*input
= dev
->input
;
74 for (i
= 0; i
< bdev
->pdata
->nbuttons
; i
++) {
75 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
77 if (bdata
->count
< bdata
->threshold
)
80 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
85 static void gpio_keys_polled_open(struct input_polled_dev
*dev
)
87 struct gpio_keys_polled_dev
*bdev
= dev
->private;
88 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
91 pdata
->enable(bdev
->dev
);
94 static void gpio_keys_polled_close(struct input_polled_dev
*dev
)
96 struct gpio_keys_polled_dev
*bdev
= dev
->private;
97 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
100 pdata
->disable(bdev
->dev
);
103 static int __devinit
gpio_keys_polled_probe(struct platform_device
*pdev
)
105 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
106 struct device
*dev
= &pdev
->dev
;
107 struct gpio_keys_polled_dev
*bdev
;
108 struct input_polled_dev
*poll_dev
;
109 struct input_dev
*input
;
113 if (!pdata
|| !pdata
->poll_interval
)
116 bdev
= kzalloc(sizeof(struct gpio_keys_polled_dev
) +
117 pdata
->nbuttons
* sizeof(struct gpio_keys_button_data
),
120 dev_err(dev
, "no memory for private data\n");
124 poll_dev
= input_allocate_polled_device();
126 dev_err(dev
, "no memory for polled device\n");
131 poll_dev
->private = bdev
;
132 poll_dev
->poll
= gpio_keys_polled_poll
;
133 poll_dev
->poll_interval
= pdata
->poll_interval
;
134 poll_dev
->open
= gpio_keys_polled_open
;
135 poll_dev
->close
= gpio_keys_polled_close
;
137 input
= poll_dev
->input
;
139 input
->evbit
[0] = BIT(EV_KEY
);
140 input
->name
= pdev
->name
;
141 input
->phys
= DRV_NAME
"/input0";
142 input
->dev
.parent
= &pdev
->dev
;
144 input
->id
.bustype
= BUS_HOST
;
145 input
->id
.vendor
= 0x0001;
146 input
->id
.product
= 0x0001;
147 input
->id
.version
= 0x0100;
149 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
150 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
151 struct gpio_keys_button_data
*bdata
= &bdev
->data
[i
];
152 unsigned int gpio
= button
->gpio
;
153 unsigned int type
= button
->type
?: EV_KEY
;
155 if (button
->wakeup
) {
156 dev_err(dev
, DRV_NAME
" does not support wakeup\n");
161 error
= gpio_request(gpio
,
162 button
->desc
? button
->desc
: DRV_NAME
);
164 dev_err(dev
, "unable to claim gpio %u, err=%d\n",
169 error
= gpio_direction_input(gpio
);
172 "unable to set direction on gpio %u, err=%d\n",
177 bdata
->can_sleep
= gpio_cansleep(gpio
);
178 bdata
->last_state
= -1;
179 bdata
->threshold
= DIV_ROUND_UP(button
->debounce_interval
,
180 pdata
->poll_interval
);
182 input_set_capability(input
, type
, button
->code
);
185 bdev
->poll_dev
= poll_dev
;
188 platform_set_drvdata(pdev
, bdev
);
190 error
= input_register_polled_device(poll_dev
);
192 dev_err(dev
, "unable to register polled device, err=%d\n",
197 /* report initial state of the buttons */
198 for (i
= 0; i
< pdata
->nbuttons
; i
++)
199 gpio_keys_polled_check_state(input
, &pdata
->buttons
[i
],
206 gpio_free(pdata
->buttons
[i
].gpio
);
208 input_free_polled_device(poll_dev
);
213 platform_set_drvdata(pdev
, NULL
);
217 static int __devexit
gpio_keys_polled_remove(struct platform_device
*pdev
)
219 struct gpio_keys_polled_dev
*bdev
= platform_get_drvdata(pdev
);
220 struct gpio_keys_platform_data
*pdata
= bdev
->pdata
;
223 input_unregister_polled_device(bdev
->poll_dev
);
225 for (i
= 0; i
< pdata
->nbuttons
; i
++)
226 gpio_free(pdata
->buttons
[i
].gpio
);
228 input_free_polled_device(bdev
->poll_dev
);
231 platform_set_drvdata(pdev
, NULL
);
236 static struct platform_driver gpio_keys_polled_driver
= {
237 .probe
= gpio_keys_polled_probe
,
238 .remove
= __devexit_p(gpio_keys_polled_remove
),
241 .owner
= THIS_MODULE
,
244 module_platform_driver(gpio_keys_polled_driver
);
246 MODULE_LICENSE("GPL v2");
247 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
248 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
249 MODULE_ALIAS("platform:" DRV_NAME
);