generic: update gpio_buttons driver

SVN-Revision: 23984
owl
Gabor Juhos 2010-11-12 18:53:01 +00:00
parent 920a34c0ce
commit 1fa8af5e9f
1 changed files with 55 additions and 39 deletions

View File

@ -20,23 +20,19 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/input-polldev.h> #include <linux/input-polldev.h>
#include <linux/ioport.h> #include <linux/ioport.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/gpio_buttons.h> #include <linux/gpio_buttons.h>
#include <asm/gpio.h>
#define DRV_NAME "gpio-buttons" #define DRV_NAME "gpio-buttons"
#define DRV_VERSION "0.1.2"
#define PFX DRV_NAME ": "
struct gpio_button_data { struct gpio_button_data {
int last_state; int last_state;
int count; int count;
int can_sleep;
}; };
struct gpio_buttons_dev { struct gpio_buttons_dev {
@ -45,6 +41,28 @@ struct gpio_buttons_dev {
struct gpio_button_data *data; struct gpio_button_data *data;
}; };
static void gpio_buttons_check_state(struct input_dev *input,
struct gpio_button *button,
struct gpio_button_data *bdata)
{
int state;
if (bdata->can_sleep)
state = !!gpio_get_value_cansleep(button->gpio);
else
state = !!gpio_get_value(button->gpio);
if (state != bdata->last_state) {
unsigned int type = button->type ?: EV_KEY;
input_event(input, type, button->code,
!!(state ^ button->active_low));
input_sync(input);
bdata->count = 0;
bdata->last_state = state;
}
}
static void gpio_buttons_poll(struct input_polled_dev *dev) static void gpio_buttons_poll(struct input_polled_dev *dev)
{ {
struct gpio_buttons_dev *bdev = dev->private; struct gpio_buttons_dev *bdev = dev->private;
@ -54,41 +72,34 @@ static void gpio_buttons_poll(struct input_polled_dev *dev)
for (i = 0; i < bdev->pdata->nbuttons; i++) { for (i = 0; i < bdev->pdata->nbuttons; i++) {
struct gpio_button *button = &pdata->buttons[i]; struct gpio_button *button = &pdata->buttons[i];
unsigned int type = button->type ?: EV_KEY; struct gpio_button_data *bdata = &bdev->data[i];
int state;
if (bdev->data[i].count < button->threshold) { if (bdata->count < button->threshold)
bdev->data[i].count++; bdata->count++;
continue; else
} gpio_buttons_check_state(input, button, bdata);
state = gpio_get_value(button->gpio) ? 1 : 0;
if (state != bdev->data[i].last_state) {
input_event(input, type, button->code,
!!(state ^ button->active_low));
input_sync(input);
bdev->data[i].count = 0;
bdev->data[i].last_state = state;
}
} }
} }
static int __devinit gpio_buttons_probe(struct platform_device *pdev) static int __devinit gpio_buttons_probe(struct platform_device *pdev)
{ {
struct gpio_buttons_platform_data *pdata = pdev->dev.platform_data; struct gpio_buttons_platform_data *pdata = pdev->dev.platform_data;
struct device *dev = &pdev->dev;
struct gpio_buttons_dev *bdev; struct gpio_buttons_dev *bdev;
struct input_polled_dev *poll_dev; struct input_polled_dev *poll_dev;
struct input_dev *input; struct input_dev *input;
int error, i; int error;
int i;
if (!pdata) if (!pdata)
return -ENXIO; return -ENXIO;
bdev = kzalloc(sizeof(struct gpio_buttons_dev) + bdev = kzalloc(sizeof(struct gpio_buttons_dev) +
sizeof(struct gpio_button_data) * pdata->nbuttons, pdata->nbuttons * sizeof(struct gpio_button_data),
GFP_KERNEL); GFP_KERNEL);
if (!bdev) { if (!bdev) {
printk(KERN_ERR DRV_NAME "no memory for device\n"); dev_err(dev, "no memory for private data\n");
return -ENOMEM; return -ENOMEM;
} }
@ -96,7 +107,7 @@ static int __devinit gpio_buttons_probe(struct platform_device *pdev)
poll_dev = input_allocate_polled_device(); poll_dev = input_allocate_polled_device();
if (!poll_dev) { if (!poll_dev) {
printk(KERN_ERR DRV_NAME "no memory for polled device\n"); dev_err(dev, "no memory for polled device\n");
error = -ENOMEM; error = -ENOMEM;
goto err_free_bdev; goto err_free_bdev;
} }
@ -122,23 +133,26 @@ static int __devinit gpio_buttons_probe(struct platform_device *pdev)
unsigned int gpio = button->gpio; unsigned int gpio = button->gpio;
unsigned int type = button->type ?: EV_KEY; unsigned int type = button->type ?: EV_KEY;
error = gpio_request(gpio, button->desc ? error = gpio_request(gpio,
button->desc : DRV_NAME); button->desc ? button->desc : DRV_NAME);
if (error) { if (error) {
printk(KERN_ERR PFX "unable to claim gpio %u, " dev_err(dev, "unable to claim gpio %u, err=%d\n",
"error %d\n", gpio, error); gpio, error);
goto err_free_gpio; goto err_free_gpio;
} }
error = gpio_direction_input(gpio); error = gpio_direction_input(gpio);
if (error) { if (error) {
printk(KERN_ERR PFX "unable to set direction on " dev_err(dev,
"gpio %u, error %d\n", gpio, error); "unable to set direction on gpio %u, err=%d\n",
gpio, error);
goto err_free_gpio; goto err_free_gpio;
} }
bdev->data[i].can_sleep = gpio_cansleep(gpio);
bdev->data[i].last_state = -1;
input_set_capability(input, type, button->code); input_set_capability(input, type, button->code);
bdev->data[i].last_state = gpio_get_value(button->gpio) ? 1 : 0;
} }
bdev->poll_dev = poll_dev; bdev->poll_dev = poll_dev;
@ -147,11 +161,16 @@ static int __devinit gpio_buttons_probe(struct platform_device *pdev)
error = input_register_polled_device(poll_dev); error = input_register_polled_device(poll_dev);
if (error) { if (error) {
printk(KERN_ERR PFX "unable to register polled device, " dev_err(dev, "unable to register polled device, err=%d\n",
"error %d\n", error); error);
goto err_free_gpio; goto err_free_gpio;
} }
/* report initial state of the buttons */
for (i = 0; i < pdata->nbuttons; i++)
gpio_buttons_check_state(input, &pdata->buttons[i],
&bdev->data[i]);
return 0; return 0;
err_free_gpio: err_free_gpio:
@ -197,7 +216,6 @@ static struct platform_driver gpio_buttons_driver = {
static int __init gpio_buttons_init(void) static int __init gpio_buttons_init(void)
{ {
printk(KERN_INFO DRV_NAME " driver version " DRV_VERSION "\n");
return platform_driver_register(&gpio_buttons_driver); return platform_driver_register(&gpio_buttons_driver);
} }
@ -209,8 +227,6 @@ static void __exit gpio_buttons_exit(void)
module_init(gpio_buttons_init); module_init(gpio_buttons_init);
module_exit(gpio_buttons_exit); module_exit(gpio_buttons_exit);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>"); MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_VERSION(DRV_VERSION); MODULE_DESCRIPTION("Polled GPIO Buttons driver");
MODULE_DESCRIPTION("Polled buttons driver for CPU GPIOs");