2007-01-18 05:44:09 +00:00
|
|
|
/*
|
|
|
|
* Driver for keys on GPIO lines capable of generating interrupts.
|
|
|
|
*
|
|
|
|
* Copyright 2005 Phil Blundell
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/sysctl.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/input.h>
|
2007-03-05 08:30:22 +00:00
|
|
|
#include <linux/gpio_keys.h>
|
2009-06-29 05:38:56 +00:00
|
|
|
#include <linux/workqueue.h>
|
2009-11-11 05:01:31 +00:00
|
|
|
#include <linux/gpio.h>
|
2007-01-18 05:44:09 +00:00
|
|
|
|
2008-05-07 20:30:15 +00:00
|
|
|
struct gpio_button_data {
|
|
|
|
struct gpio_keys_button *button;
|
|
|
|
struct input_dev *input;
|
2009-06-29 05:38:44 +00:00
|
|
|
struct timer_list timer;
|
2009-06-29 05:38:56 +00:00
|
|
|
struct work_struct work;
|
2008-05-07 20:30:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gpio_keys_drvdata {
|
|
|
|
struct input_dev *input;
|
|
|
|
struct gpio_button_data data[0];
|
|
|
|
};
|
|
|
|
|
2009-11-30 08:04:02 +00:00
|
|
|
static void gpio_keys_report_event(struct gpio_button_data *bdata)
|
2008-05-07 20:30:15 +00:00
|
|
|
{
|
2008-08-08 16:14:36 +00:00
|
|
|
struct gpio_keys_button *button = bdata->button;
|
|
|
|
struct input_dev *input = bdata->input;
|
2008-05-07 20:30:15 +00:00
|
|
|
unsigned int type = button->type ?: EV_KEY;
|
|
|
|
int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
|
|
|
|
|
|
|
|
input_event(input, type, button->code, !!state);
|
|
|
|
input_sync(input);
|
|
|
|
}
|
|
|
|
|
2009-11-30 08:04:02 +00:00
|
|
|
static void gpio_keys_work_func(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct gpio_button_data *bdata =
|
|
|
|
container_of(work, struct gpio_button_data, work);
|
|
|
|
|
|
|
|
gpio_keys_report_event(bdata);
|
|
|
|
}
|
|
|
|
|
2009-06-29 05:38:56 +00:00
|
|
|
static void gpio_keys_timer(unsigned long _data)
|
2009-06-29 05:38:44 +00:00
|
|
|
{
|
|
|
|
struct gpio_button_data *data = (struct gpio_button_data *)_data;
|
|
|
|
|
2009-06-29 05:38:56 +00:00
|
|
|
schedule_work(&data->work);
|
2009-06-29 05:38:44 +00:00
|
|
|
}
|
|
|
|
|
2007-01-18 05:44:09 +00:00
|
|
|
static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
|
|
|
|
{
|
2008-08-08 16:14:34 +00:00
|
|
|
struct gpio_button_data *bdata = dev_id;
|
|
|
|
struct gpio_keys_button *button = bdata->button;
|
2007-01-18 05:44:09 +00:00
|
|
|
|
2008-08-08 16:14:34 +00:00
|
|
|
BUG_ON(irq != gpio_to_irq(button->gpio));
|
2007-05-01 04:39:13 +00:00
|
|
|
|
2009-06-29 05:38:44 +00:00
|
|
|
if (button->debounce_interval)
|
|
|
|
mod_timer(&bdata->timer,
|
|
|
|
jiffies + msecs_to_jiffies(button->debounce_interval));
|
|
|
|
else
|
2009-06-29 05:38:56 +00:00
|
|
|
schedule_work(&bdata->work);
|
2007-01-18 05:44:09 +00:00
|
|
|
|
2008-08-08 16:14:34 +00:00
|
|
|
return IRQ_HANDLED;
|
2007-01-18 05:44:09 +00:00
|
|
|
}
|
|
|
|
|
2009-11-11 05:01:31 +00:00
|
|
|
static int __devinit gpio_keys_setup_key(struct device *dev,
|
|
|
|
struct gpio_button_data *bdata,
|
|
|
|
struct gpio_keys_button *button)
|
|
|
|
{
|
|
|
|
char *desc = button->desc ? button->desc : "gpio_keys";
|
|
|
|
int irq, error;
|
|
|
|
|
|
|
|
setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
|
2009-11-30 08:04:02 +00:00
|
|
|
INIT_WORK(&bdata->work, gpio_keys_work_func);
|
2009-11-11 05:01:31 +00:00
|
|
|
|
|
|
|
error = gpio_request(button->gpio, desc);
|
|
|
|
if (error < 0) {
|
|
|
|
dev_err(dev, "failed to request GPIO %d, error %d\n",
|
|
|
|
button->gpio, error);
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpio_direction_input(button->gpio);
|
|
|
|
if (error < 0) {
|
|
|
|
dev_err(dev, "failed to configure"
|
|
|
|
" direction for GPIO %d, error %d\n",
|
|
|
|
button->gpio, error);
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
|
|
|
irq = gpio_to_irq(button->gpio);
|
|
|
|
if (irq < 0) {
|
|
|
|
error = irq;
|
|
|
|
dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
|
|
|
|
button->gpio, error);
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = request_irq(irq, gpio_keys_isr,
|
|
|
|
IRQF_SHARED |
|
|
|
|
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
|
|
|
|
desc, bdata);
|
|
|
|
if (error) {
|
|
|
|
dev_err(dev, "Unable to claim irq %d; error %d\n",
|
|
|
|
irq, error);
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail3:
|
|
|
|
gpio_free(button->gpio);
|
|
|
|
fail2:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2007-01-18 05:44:09 +00:00
|
|
|
static int __devinit gpio_keys_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
|
2008-05-07 20:30:15 +00:00
|
|
|
struct gpio_keys_drvdata *ddata;
|
2009-11-11 05:00:35 +00:00
|
|
|
struct device *dev = &pdev->dev;
|
2007-01-18 05:44:09 +00:00
|
|
|
struct input_dev *input;
|
|
|
|
int i, error;
|
2007-09-26 04:01:17 +00:00
|
|
|
int wakeup = 0;
|
2007-01-18 05:44:09 +00:00
|
|
|
|
2008-05-07 20:30:15 +00:00
|
|
|
ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
|
|
|
|
pdata->nbuttons * sizeof(struct gpio_button_data),
|
|
|
|
GFP_KERNEL);
|
2007-01-18 05:44:09 +00:00
|
|
|
input = input_allocate_device();
|
2008-05-07 20:30:15 +00:00
|
|
|
if (!ddata || !input) {
|
2009-11-11 05:00:35 +00:00
|
|
|
dev_err(dev, "failed to allocate state\n");
|
2008-05-07 20:30:15 +00:00
|
|
|
error = -ENOMEM;
|
|
|
|
goto fail1;
|
|
|
|
}
|
2007-01-18 05:44:09 +00:00
|
|
|
|
2008-05-07 20:30:15 +00:00
|
|
|
platform_set_drvdata(pdev, ddata);
|
2007-01-18 05:44:09 +00:00
|
|
|
|
|
|
|
input->name = pdev->name;
|
|
|
|
input->phys = "gpio-keys/input0";
|
2007-04-12 05:34:58 +00:00
|
|
|
input->dev.parent = &pdev->dev;
|
2007-01-18 05:44:09 +00:00
|
|
|
|
|
|
|
input->id.bustype = BUS_HOST;
|
|
|
|
input->id.vendor = 0x0001;
|
|
|
|
input->id.product = 0x0001;
|
|
|
|
input->id.version = 0x0100;
|
|
|
|
|
2008-10-28 02:30:53 +00:00
|
|
|
/* Enable auto repeat feature of Linux input subsystem */
|
|
|
|
if (pdata->rep)
|
|
|
|
__set_bit(EV_REP, input->evbit);
|
|
|
|
|
2008-05-07 20:30:15 +00:00
|
|
|
ddata->input = input;
|
|
|
|
|
2007-01-18 05:44:09 +00:00
|
|
|
for (i = 0; i < pdata->nbuttons; i++) {
|
2007-05-01 04:39:13 +00:00
|
|
|
struct gpio_keys_button *button = &pdata->buttons[i];
|
2008-05-07 20:30:15 +00:00
|
|
|
struct gpio_button_data *bdata = &ddata->data[i];
|
2007-05-01 04:39:13 +00:00
|
|
|
unsigned int type = button->type ?: EV_KEY;
|
2007-01-18 05:44:09 +00:00
|
|
|
|
2008-05-07 20:30:15 +00:00
|
|
|
bdata->input = input;
|
2008-07-30 14:33:43 +00:00
|
|
|
bdata->button = button;
|
2009-11-11 05:01:31 +00:00
|
|
|
|
|
|
|
error = gpio_keys_setup_key(dev, bdata, button);
|
|
|
|
if (error)
|
2008-05-07 20:30:15 +00:00
|
|
|
goto fail2;
|
2007-05-01 04:39:13 +00:00
|
|
|
|
2007-09-26 04:01:17 +00:00
|
|
|
if (button->wakeup)
|
|
|
|
wakeup = 1;
|
|
|
|
|
2007-05-01 04:39:13 +00:00
|
|
|
input_set_capability(input, type, button->code);
|
2007-01-18 05:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error = input_register_device(input);
|
|
|
|
if (error) {
|
2009-11-11 05:00:35 +00:00
|
|
|
dev_err(dev, "Unable to register input device, "
|
2007-09-26 04:01:03 +00:00
|
|
|
"error: %d\n", error);
|
2008-05-07 20:30:15 +00:00
|
|
|
goto fail2;
|
2007-01-18 05:44:09 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 08:04:02 +00:00
|
|
|
/* get current state of buttons */
|
|
|
|
for (i = 0; i < pdata->nbuttons; i++)
|
|
|
|
gpio_keys_report_event(&ddata->data[i]);
|
|
|
|
input_sync(input);
|
|
|
|
|
2007-09-26 04:01:17 +00:00
|
|
|
device_init_wakeup(&pdev->dev, wakeup);
|
|
|
|
|
2007-01-18 05:44:09 +00:00
|
|
|
return 0;
|
|
|
|
|
2008-05-07 20:30:15 +00:00
|
|
|
fail2:
|
2007-11-21 19:42:33 +00:00
|
|
|
while (--i >= 0) {
|
2008-08-08 16:14:34 +00:00
|
|
|
free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
|
2009-06-29 05:38:44 +00:00
|
|
|
if (pdata->buttons[i].debounce_interval)
|
|
|
|
del_timer_sync(&ddata->data[i].timer);
|
2009-06-29 05:38:56 +00:00
|
|
|
cancel_work_sync(&ddata->data[i].work);
|
2007-11-21 19:42:33 +00:00
|
|
|
gpio_free(pdata->buttons[i].gpio);
|
|
|
|
}
|
2007-01-18 05:44:09 +00:00
|
|
|
|
2007-09-26 04:01:03 +00:00
|
|
|
platform_set_drvdata(pdev, NULL);
|
2008-05-07 20:30:15 +00:00
|
|
|
fail1:
|
2007-01-18 05:44:09 +00:00
|
|
|
input_free_device(input);
|
2008-05-07 20:30:15 +00:00
|
|
|
kfree(ddata);
|
2007-01-18 05:44:09 +00:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __devexit gpio_keys_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
|
2008-05-07 20:30:15 +00:00
|
|
|
struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
|
|
|
|
struct input_dev *input = ddata->input;
|
2007-01-18 05:44:09 +00:00
|
|
|
int i;
|
|
|
|
|
2007-09-26 04:01:17 +00:00
|
|
|
device_init_wakeup(&pdev->dev, 0);
|
|
|
|
|
2007-01-18 05:44:09 +00:00
|
|
|
for (i = 0; i < pdata->nbuttons; i++) {
|
2007-02-18 06:40:46 +00:00
|
|
|
int irq = gpio_to_irq(pdata->buttons[i].gpio);
|
2008-08-08 16:14:34 +00:00
|
|
|
free_irq(irq, &ddata->data[i]);
|
2009-06-29 05:38:44 +00:00
|
|
|
if (pdata->buttons[i].debounce_interval)
|
|
|
|
del_timer_sync(&ddata->data[i].timer);
|
2009-06-29 05:38:56 +00:00
|
|
|
cancel_work_sync(&ddata->data[i].work);
|
2007-11-21 19:42:33 +00:00
|
|
|
gpio_free(pdata->buttons[i].gpio);
|
2007-01-18 05:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
input_unregister_device(input);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-26 04:01:17 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
2009-07-23 06:02:54 +00:00
|
|
|
static int gpio_keys_suspend(struct device *dev)
|
2007-09-26 04:01:17 +00:00
|
|
|
{
|
2009-07-23 06:02:54 +00:00
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
2007-09-26 04:01:17 +00:00
|
|
|
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (device_may_wakeup(&pdev->dev)) {
|
|
|
|
for (i = 0; i < pdata->nbuttons; i++) {
|
|
|
|
struct gpio_keys_button *button = &pdata->buttons[i];
|
|
|
|
if (button->wakeup) {
|
|
|
|
int irq = gpio_to_irq(button->gpio);
|
|
|
|
enable_irq_wake(irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-23 06:02:54 +00:00
|
|
|
static int gpio_keys_resume(struct device *dev)
|
2007-09-26 04:01:17 +00:00
|
|
|
{
|
2009-07-23 06:02:54 +00:00
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
2009-11-30 08:04:02 +00:00
|
|
|
struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
|
2007-09-26 04:01:17 +00:00
|
|
|
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
int i;
|
|
|
|
|
2009-11-30 08:04:02 +00:00
|
|
|
for (i = 0; i < pdata->nbuttons; i++) {
|
|
|
|
|
|
|
|
struct gpio_keys_button *button = &pdata->buttons[i];
|
|
|
|
if (button->wakeup && device_may_wakeup(&pdev->dev)) {
|
|
|
|
int irq = gpio_to_irq(button->gpio);
|
|
|
|
disable_irq_wake(irq);
|
2007-09-26 04:01:17 +00:00
|
|
|
}
|
2009-11-30 08:04:02 +00:00
|
|
|
|
|
|
|
gpio_keys_report_event(&ddata->data[i]);
|
2007-09-26 04:01:17 +00:00
|
|
|
}
|
2009-11-30 08:04:02 +00:00
|
|
|
input_sync(ddata->input);
|
2007-09-26 04:01:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-07-23 06:02:54 +00:00
|
|
|
|
|
|
|
static const struct dev_pm_ops gpio_keys_pm_ops = {
|
|
|
|
.suspend = gpio_keys_suspend,
|
|
|
|
.resume = gpio_keys_resume,
|
|
|
|
};
|
2007-09-26 04:01:17 +00:00
|
|
|
#endif
|
|
|
|
|
2008-07-30 14:34:02 +00:00
|
|
|
static struct platform_driver gpio_keys_device_driver = {
|
2007-01-18 05:44:09 +00:00
|
|
|
.probe = gpio_keys_probe,
|
|
|
|
.remove = __devexit_p(gpio_keys_remove),
|
|
|
|
.driver = {
|
|
|
|
.name = "gpio-keys",
|
2008-04-18 04:24:42 +00:00
|
|
|
.owner = THIS_MODULE,
|
2009-07-23 06:02:54 +00:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
.pm = &gpio_keys_pm_ops,
|
|
|
|
#endif
|
2007-01-18 05:44:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init gpio_keys_init(void)
|
|
|
|
{
|
|
|
|
return platform_driver_register(&gpio_keys_device_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit gpio_keys_exit(void)
|
|
|
|
{
|
|
|
|
platform_driver_unregister(&gpio_keys_device_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(gpio_keys_init);
|
|
|
|
module_exit(gpio_keys_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
|
|
|
|
MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
|
2008-04-18 04:24:42 +00:00
|
|
|
MODULE_ALIAS("platform:gpio-keys");
|