mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] input: gpio-keys: implement debouncing
Date: Mon, 14 Dec 2015 12:46:21 +0100	[thread overview]
Message-ID: <1450093581-17714-1-git-send-email-s.hauer@pengutronix.de> (raw)

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@pengutronix.de>
---
 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@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

                 reply	other threads:[~2015-12-14 11:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1450093581-17714-1-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox