1 From 4c83444bd41a70fe47731e589c6575da2bfc78c8 Mon Sep 17 00:00:00 2001
2 From: Andy Green <andy@openmoko.com>
3 Date: Fri, 25 Jul 2008 23:06:05 +0100
4 Subject: [PATCH] fix-jack-debounce.patch
6 Headphone jack detection is bouncy, it can trigger multiple interrupts
7 on insertion or removal. This patch adds a workqueue that waits out the
8 interrupt spew in 100ms units, and if it sees no more interrupts for 100ms
9 only then samples and reports the jack state. I was unable to get a bounce
10 after 20 or so tries after this.
12 Signed-off-by: Andy Green <andy@openmoko.com>
14 drivers/input/keyboard/neo1973kbd.c | 63 ++++++++++++++++++++++++++++++++--
15 1 files changed, 59 insertions(+), 4 deletions(-)
17 diff --git a/drivers/input/keyboard/neo1973kbd.c b/drivers/input/keyboard/neo1973kbd.c
18 index 06fa8e0..6c96660 100644
19 --- a/drivers/input/keyboard/neo1973kbd.c
20 +++ b/drivers/input/keyboard/neo1973kbd.c
22 #include <linux/jiffies.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 +#include <linux/workqueue.h>
28 #include <asm/mach-types.h>
31 struct input_dev *input;
32 unsigned int suspended;
33 + struct work_struct work;
34 + int work_in_progress;
35 + int hp_irq_count_in_work;
40 static irqreturn_t neo1973kbd_aux_irq(int irq, void *dev_id)
41 @@ -52,14 +58,61 @@ static irqreturn_t neo1973kbd_hold_irq(int irq, void *dev_id)
46 +static void neo1973kbd_debounce_jack(struct work_struct *work)
48 + struct neo1973kbd *kbd = container_of(work, struct neo1973kbd, work);
50 + /* we wait out any multiple interrupt stuttering in 100ms lumps */
53 + kbd->hp_irq_count_in_work = kbd->hp_irq_count;
55 + } while (kbd->hp_irq_count != kbd->hp_irq_count_in_work);
57 + /* no new interrupts on jack for 100ms... ok we will report it */
59 + input_report_switch(kbd->input,
60 + SW_HEADPHONE_INSERT,
61 + gpio_get_value(irq_to_gpio(kbd->jack_irq)));
62 + input_sync(kbd->input);
64 + /* next time we get an interrupt on jack we need new work action */
65 + kbd->work_in_progress = 0;
70 static irqreturn_t neo1973kbd_headphone_irq(int irq, void *dev_id)
72 struct neo1973kbd *neo1973kbd_data = dev_id;
74 - int key_pressed = gpio_get_value(irq_to_gpio(irq));
75 - input_report_switch(neo1973kbd_data->input,
76 - SW_HEADPHONE_INSERT, key_pressed);
77 - input_sync(neo1973kbd_data->input);
79 + * this interrupt is prone to bouncing and userspace doesn't like
80 + * to have to deal with that kind of thing. So we do not accept
81 + * that a jack interrupt is equal to a jack event. Instead we fire
82 + * some work on the first interrupt, and it hangs about in 100ms units
83 + * until no more interrupts come. Then it accepts the state it finds
84 + * for jack insert and reports it once
87 + neo1973kbd_data->hp_irq_count++;
89 + * the first interrupt we see for a while, we fire the work item
90 + * and record the interrupt count when we did that. If more interrupts
91 + * come in the meanwhile, we can tell by the difference in that
92 + * stored count and hp_irq_count which increments every interrupt
94 + if (!neo1973kbd_data->work_in_progress) {
95 + neo1973kbd_data->jack_irq = irq;
96 + neo1973kbd_data->hp_irq_count_in_work =
97 + neo1973kbd_data->hp_irq_count;
98 + if (!schedule_work(&neo1973kbd_data->work))
100 + "Unable to schedule headphone debounce\n");
102 + neo1973kbd_data->work_in_progress = 1;
107 @@ -120,6 +173,8 @@ static int neo1973kbd_probe(struct platform_device *pdev)
109 neo1973kbd->input = input_dev;
111 + INIT_WORK(&neo1973kbd->work, neo1973kbd_debounce_jack);
113 input_dev->name = "Neo1973 Buttons";
114 input_dev->phys = "neo1973kbd/input0";
115 input_dev->id.bustype = BUS_HOST;