[PATCH] input: gpio-keys: implement debouncing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The gpio-keys driver often generates multiple events on a single buttong
press. Implement debouncing. The default debouncing time is 20ms and can
be configured with the "debounce-interval" device tree property.

Signed-off-by: Sascha Hauer <s.hauer@xxxxxxxxxxxxxx>
---
 drivers/input/gpio_keys.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/drivers/input/gpio_keys.c b/drivers/input/gpio_keys.c
index d017594..5b03fd7 100644
--- a/drivers/input/gpio_keys.c
+++ b/drivers/input/gpio_keys.c
@@ -21,6 +21,9 @@ struct gpio_key {
 	int active_low;
 
 	int previous_state;
+
+	int debounce_interval;
+	u64 debounce_start;
 };
 
 struct gpio_keys {
@@ -60,11 +63,17 @@ static void gpio_key_poller(struct poller_struct *poller)
 		gb = &gk->buttons[i];
 		val = gpio_get_value(gb->gpio);
 
-		if (val != gb->previous_state && val != gb->active_low) {
-			kfifo_put(gk->recv_fifo, (u_char*)&gb->code, sizeof(int));
-			debug("pressed gpio(%d) as %d\n", gb->gpio, gb->code);
+		if (!is_timeout(gb->debounce_start, gb->debounce_interval * MSECOND))
+			continue;
+
+		if (val != gb->previous_state) {
+			gb->debounce_start = get_time_ns();
+			if (val != gb->active_low) {
+				kfifo_put(gk->recv_fifo, (u_char*)&gb->code, sizeof(int));
+				debug("pressed gpio(%d) as %d\n", gb->gpio, gb->code);
+			}
+			gb->previous_state = val;
 		}
-		gb->previous_state = val;
 	}
 }
 
@@ -111,6 +120,7 @@ static int gpio_keys_probe_pdata(struct gpio_keys *gk, struct device_d *dev)
 		gk->buttons[i].gpio = pdata->buttons[i].gpio;
 		gk->buttons[i].code = pdata->buttons[i].code;
 		gk->buttons[i].active_low = pdata->buttons[i].active_low;
+		gk->buttons[i].debounce_interval = 20;
 	}
 
 	return 0;
@@ -142,6 +152,11 @@ static int gpio_keys_probe_dt(struct gpio_keys *gk, struct device_d *dev)
 		if (ret)
 			return ret;
 
+		gk->buttons[i].debounce_interval = 20;
+
+		of_property_read_u32(npkey, "debounce-interval",
+				     &gk->buttons[i].debounce_interval);
+
 		gk->buttons[i].code = keycode;
 
 		i++;
-- 
2.6.2


_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux