input: add debouncing for generic gpio input device gpio_key.c
Subsystems (2)
PATCH 1/1 input: add debouncing for generic gpio input device gpio_key.c
This patch add debouncing support for the generic gpio input device, since debouncing is most likely to happen.
Comments welcome.
---
Keith Mok
drivers/input/keyboard/gpio_keys.c +0 -0
@@ -26,26 +26,36 @@
#include <asm/gpio.h>
+static void do_gpio_keys_tasklet(unsigned long);
+static void do_gpio_keys_timer(unsigned long);
+
+static struct timer_list gpio_keys_timer; +DECLARE_TASKLET_DISABLED(gpio_keys_tasklet, do_gpio_keys_tasklet, 0); +static spinlock_t suspend_lock;
+static int suspended;
+
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++) {
- struct gpio_keys_button *button = &pdata->buttons[i];
- int gpio = button->gpio;
+ int i;
+ unsigned long flags;
- if (irq == gpio_to_irq(gpio)) {
- unsigned int type = button->type ?: EV_KEY;
- int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
+ spin_lock_irqsave(&suspend_lock, flags);
+ if (suspended) {
+ spin_unlock_irqrestore(&suspend_lock, flags);
+ return IRQ_HANDLED;
+ }
+ spin_unlock_irqrestore(&suspend_lock, flags);
- input_event(input, type, button->code, !!state);
- input_sync(input);
- }
+ /* disable keyboard interrupt and schedule for handling */
+ for (i = 0; i < pdata->nbuttons; i++) {
+ int irq = gpio_to_irq(pdata->buttons[i].gpio);
+ disable_irq(irq); }
+ tasklet_schedule(&gpio_keys_tasklet);
+
return IRQ_HANDLED; }
@@ -63,6 +73,11 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, input); input->evbit[0] = BIT_MASK(EV_KEY);
+ setup_timer(&gpio_keys_timer, do_gpio_keys_timer, (unsigned long) pdev);
+ tasklet_enable(&gpio_keys_tasklet);
+ gpio_keys_tasklet.data = (unsigned long) pdev;
+ spin_lock_init(&suspend_lock);
+ suspended = 0;
input->name = pdev->name;
input->phys = "gpio-keys/input0";
@@ -156,9 +171,14 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev)
for (i = 0; i < pdata->nbuttons; i++) {
int irq = gpio_to_irq(pdata->buttons[i].gpio);
free_irq(irq, pdev);
- gpio_free(pdata->buttons[i].gpio);
}+ del_timer_sync(&gpio_keys_timer);
+ tasklet_kill(&gpio_keys_tasklet);
+
+ for (i = 0; i < pdata->nbuttons; i++)
+ gpio_free(pdata->buttons[i].gpio);
+
input_unregister_device(input); return 0;
@@ -168,9 +188,29 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev) #ifdef CONFIG_PM
static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state) {
+ unsigned long flags; struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; int i;
+ spin_lock_irqsave(&suspend_lock, flags);
+
+ /*
+ * Re-enable the interrupt in case it has been masked by the
+ * handler and a key is still pressed. We need the interrupt
+ * to wake us up from suspended.
+ */
+
+ suspended = 1;
+
+ spin_unlock_irqrestore(&suspend_lock, flags);
+ del_timer_sync(&gpio_keys_timer);
+ tasklet_kill(&gpio_keys_tasklet);
+
+ for (i = 0; i < pdata->nbuttons; i++) {
+ int irq = gpio_to_irq(pdata->buttons[i].gpio);
+ enable_irq(irq);
+ }
+
if (device_may_wakeup(&pdev->dev)) {
for (i = 0; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];
@@ -189,6 +229,7 @@ static int gpio_keys_resume(struct platform_device *pdev)
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
int i;+ suspended = 0;
if (device_may_wakeup(&pdev->dev)) {
for (i = 0; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];@@ -206,6 +247,45 @@ static int gpio_keys_resume(struct platform_device *pdev)
#define gpio_keys_resume NULL
#endif
+static void do_gpio_keys_tasklet(unsigned long data) +{
+ int pressed;
+ int i;
+ struct platform_device *pdev = (struct platform_device*)data;
+ struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
+ struct input_dev *input = platform_get_drvdata(pdev);
+
+ /* check for any changes */
+ pressed = 0;
+ for (i = 0; i < pdata->nbuttons; i++) {
+ struct gpio_keys_button *button = &pdata->buttons[i];
+ int gpio = button->gpio;
+
+ unsigned int type = button->type ?: EV_KEY;
+ int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
+
+ input_event(input, type, button->code, !!state);
+ pressed |= !!state;
+ input_sync(input);
+ }
+ if(pressed) {
+ int delay = HZ / 20;
+ /* some key is pressed - keep irq disabled and use timer
+ * to poll */
+ mod_timer(&gpio_keys_timer, jiffies + delay);
+ } else {
+ for (i = 0; i < pdata->nbuttons; i++) {
+ int irq = gpio_to_irq(pdata->buttons[i].gpio);
+ enable_irq(irq);
+ }
+ }
+}
+
+static void do_gpio_keys_timer(unsigned long data) +{
+ tasklet_schedule(&gpio_keys_tasklet);
+}
+
struct platform_driver gpio_keys_device_driver = {
.probe = gpio_keys_probe,
.remove = __devexit_p(gpio_keys_remove),Quoted 14 lines · On Sun, 24 Feb 2008 14:07:03 +0800 Keith Mok <[email protected]> wrote:
This patch add debouncing support for the generic gpio input device, since debouncing is most likely to happen.
Comments welcome.
Signed-off-by Keith Mok <[email protected]>---
Keith Mokdrivers/input/keyboard/gpio_keys.c +0
-0
drivers/input/keyboard/gpio_keys.c line 32
Quoted 7 lines
drivers/input/keyboard/gpio_keys.c +4
-0@@ -26,26 +26,36 @@#include <asm/gpio.h> +static void do_gpio_keys_tasklet (unsigned long ); +static void do_gpio_keys_timer (unsigned long ); + +static struct timer_list gpio_keys_timer;
DEFINE_TIMER() could be used here. Then we can remove the runtime setup_timer().
drivers/input/keyboard/gpio_keys.c line 34
Quoted 2 lines
drivers/input/keyboard/gpio_keys.c +2
-0+DECLARE_TASKLET_DISABLED (gpio_keys_tasklet, do_gpio_keys_tasklet,0 ); +static spinlock_t suspend_lock;
Please use DEFINE_SPINLOCK(), then remove the runtime spin_lock_init().
Quoted 12 lines
drivers/input/keyboard/gpio_keys.c +12
-0+static void do_gpio_keys_tasklet (unsigned long data) +{ +int pressed; +int i; +struct platform_device *pdev = (struct platform_device *)data; +struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; +struct input_dev *input =platform_get_drvdata (pdev); + +/* check for any changes */ + pressed =0 ; +for (i =0 ; i < pdata->nbuttons; i++) { +struct gpio_keys_button *button = &pdata->buttons[i];
drivers/input/keyboard/gpio_keys.c line 56
Quoted 10 lines
drivers/input/keyboard/gpio_keys.c +10
-0+int gpio = button->gpio; + +unsigned int type = button->type ?:EV_KEY ; +int state = (gpio_get_value (gpio) ?1 :0 ) ^ button->active_low; + +input_event (input, type, button->code, !!state); + pressed |= !!state; +input_sync (input); + } +if (pressed) {
Please always pass patches through scripts/checkpatch.pl.