2007-01-17 22:44:09 -07: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/version.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 01:30:22 -07:00
|
|
|
#include <linux/gpio_keys.h>
|
2007-01-17 22:44:09 -07:00
|
|
|
|
2007-02-17 23:40:46 -07:00
|
|
|
#include <asm/gpio.h>
|
2007-01-17 22:44:09 -07:00
|
|
|
|
|
|
|
static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct platform_device *pdev = dev_id;
|
|
|
|
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
struct input_dev *input = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
for (i = 0; i < pdata->nbuttons; i++) {
|
2007-04-30 22:39:13 -06:00
|
|
|
struct gpio_keys_button *button = &pdata->buttons[i];
|
|
|
|
int gpio = button->gpio;
|
|
|
|
|
2007-02-17 23:40:46 -07:00
|
|
|
if (irq == gpio_to_irq(gpio)) {
|
2007-04-30 22:39:13 -06:00
|
|
|
unsigned int type = button->type ?: EV_KEY;
|
|
|
|
int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
|
2007-01-17 22:44:09 -07:00
|
|
|
|
2007-04-30 22:39:13 -06:00
|
|
|
input_event(input, type, button->code, !!state);
|
2007-01-17 22:44:09 -07:00
|
|
|
input_sync(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __devinit gpio_keys_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
struct input_dev *input;
|
|
|
|
int i, error;
|
2007-09-25 22:01:17 -06:00
|
|
|
int wakeup = 0;
|
2007-01-17 22:44:09 -07:00
|
|
|
|
|
|
|
input = input_allocate_device();
|
|
|
|
if (!input)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, input);
|
|
|
|
|
2007-10-19 00:40:32 -06:00
|
|
|
input->evbit[0] = BIT_MASK(EV_KEY);
|
2007-01-17 22:44:09 -07:00
|
|
|
|
|
|
|
input->name = pdev->name;
|
|
|
|
input->phys = "gpio-keys/input0";
|
2007-04-11 23:34:58 -06:00
|
|
|
input->dev.parent = &pdev->dev;
|
2007-01-17 22:44:09 -07:00
|
|
|
|
|
|
|
input->id.bustype = BUS_HOST;
|
|
|
|
input->id.vendor = 0x0001;
|
|
|
|
input->id.product = 0x0001;
|
|
|
|
input->id.version = 0x0100;
|
|
|
|
|
|
|
|
for (i = 0; i < pdata->nbuttons; i++) {
|
2007-04-30 22:39:13 -06:00
|
|
|
struct gpio_keys_button *button = &pdata->buttons[i];
|
2007-11-21 12:42:33 -07:00
|
|
|
int irq;
|
2007-04-30 22:39:13 -06:00
|
|
|
unsigned int type = button->type ?: EV_KEY;
|
2007-01-17 22:44:09 -07:00
|
|
|
|
2007-11-21 12:42:33 -07:00
|
|
|
error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
|
|
|
|
if (error < 0) {
|
|
|
|
pr_err("gpio-keys: failed to request GPIO %d,"
|
|
|
|
" error %d\n", button->gpio, error);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = gpio_direction_input(button->gpio);
|
|
|
|
if (error < 0) {
|
|
|
|
pr_err("gpio-keys: failed to configure input"
|
|
|
|
" direction for GPIO %d, error %d\n",
|
|
|
|
button->gpio, error);
|
|
|
|
gpio_free(button->gpio);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
irq = gpio_to_irq(button->gpio);
|
2007-09-25 22:01:03 -06:00
|
|
|
if (irq < 0) {
|
|
|
|
error = irq;
|
2007-11-21 12:42:33 -07:00
|
|
|
pr_err("gpio-keys: Unable to get irq number"
|
|
|
|
" for GPIO %d, error %d\n",
|
2007-09-25 22:01:03 -06:00
|
|
|
button->gpio, error);
|
2007-11-21 12:42:33 -07:00
|
|
|
gpio_free(button->gpio);
|
2007-09-25 22:01:03 -06:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = request_irq(irq, gpio_keys_isr,
|
|
|
|
IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
|
|
|
|
IRQF_TRIGGER_FALLING,
|
|
|
|
button->desc ? button->desc : "gpio_keys",
|
|
|
|
pdev);
|
2007-01-17 22:44:09 -07:00
|
|
|
if (error) {
|
2007-11-21 12:42:33 -07:00
|
|
|
pr_err("gpio-keys: Unable to claim irq %d; error %d\n",
|
2007-02-17 23:40:46 -07:00
|
|
|
irq, error);
|
2007-11-21 12:42:33 -07:00
|
|
|
gpio_free(button->gpio);
|
2007-01-17 22:44:09 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
2007-04-30 22:39:13 -06:00
|
|
|
|
2007-09-25 22:01:17 -06:00
|
|
|
if (button->wakeup)
|
|
|
|
wakeup = 1;
|
|
|
|
|
2007-04-30 22:39:13 -06:00
|
|
|
input_set_capability(input, type, button->code);
|
2007-01-17 22:44:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
error = input_register_device(input);
|
|
|
|
if (error) {
|
2007-11-21 12:42:33 -07:00
|
|
|
pr_err("gpio-keys: Unable to register input device, "
|
2007-09-25 22:01:03 -06:00
|
|
|
"error: %d\n", error);
|
2007-01-17 22:44:09 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2007-09-25 22:01:17 -06:00
|
|
|
device_init_wakeup(&pdev->dev, wakeup);
|
|
|
|
|
2007-01-17 22:44:09 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2007-11-21 12:42:33 -07:00
|
|
|
while (--i >= 0) {
|
2007-02-17 23:40:46 -07:00
|
|
|
free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
|
2007-11-21 12:42:33 -07:00
|
|
|
gpio_free(pdata->buttons[i].gpio);
|
|
|
|
}
|
2007-01-17 22:44:09 -07:00
|
|
|
|
2007-09-25 22:01:03 -06:00
|
|
|
platform_set_drvdata(pdev, NULL);
|
2007-01-17 22:44:09 -07:00
|
|
|
input_free_device(input);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __devexit gpio_keys_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
struct input_dev *input = platform_get_drvdata(pdev);
|
|
|
|
int i;
|
|
|
|
|
2007-09-25 22:01:17 -06:00
|
|
|
device_init_wakeup(&pdev->dev, 0);
|
|
|
|
|
2007-01-17 22:44:09 -07:00
|
|
|
for (i = 0; i < pdata->nbuttons; i++) {
|
2007-02-17 23:40:46 -07:00
|
|
|
int irq = gpio_to_irq(pdata->buttons[i].gpio);
|
2007-01-17 22:44:09 -07:00
|
|
|
free_irq(irq, pdev);
|
2007-11-21 12:42:33 -07:00
|
|
|
gpio_free(pdata->buttons[i].gpio);
|
2007-01-17 22:44:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
input_unregister_device(input);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-25 22:01:17 -06:00
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gpio_keys_resume(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
disable_irq_wake(irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define gpio_keys_suspend NULL
|
|
|
|
#define gpio_keys_resume NULL
|
|
|
|
#endif
|
|
|
|
|
2007-01-17 22:44:09 -07:00
|
|
|
struct platform_driver gpio_keys_device_driver = {
|
|
|
|
.probe = gpio_keys_probe,
|
|
|
|
.remove = __devexit_p(gpio_keys_remove),
|
2007-09-25 22:01:17 -06:00
|
|
|
.suspend = gpio_keys_suspend,
|
|
|
|
.resume = gpio_keys_resume,
|
2007-01-17 22:44:09 -07:00
|
|
|
.driver = {
|
|
|
|
.name = "gpio-keys",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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");
|