linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: linux-kernel@vger.kernel.org
Cc: Grant Likely <grant.likely@secretlab.ca>,
	Linus Walleij <linus.walleij@linaro.org>,
	Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH] gpio: export 'debounce' attribute if supported by the gpio chip
Date: Wed,  5 Dec 2012 22:32:47 -0800	[thread overview]
Message-ID: <1354775567-17408-1-git-send-email-linux@roeck-us.net> (raw)

Create a 'debounce' attribute if debounce is supported by the gpio
chip and a gpio pin is exported.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/gpio/gpiolib.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index a971e3d..13ee9a7 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -67,6 +67,7 @@ struct gpio_desc {
 #ifdef CONFIG_DEBUG_FS
 	const char		*label;
 #endif
+	unsigned		debounce;
 };
 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
 
@@ -215,6 +216,10 @@ static DEFINE_MUTEX(sysfs_lock);
  *      * is read/write as zero/nonzero
  *      * also affects existing and subsequent "falling" and "rising"
  *        /edge configuration
+ *   /debounce
+ *      * configures debounce time in uS
+ *      * available only if debounce is supported by the chip
+ *      * is read/write; 0 to disable or debounce time
  */
 
 static ssize_t gpio_direction_show(struct device *dev,
@@ -320,6 +325,55 @@ static ssize_t gpio_value_store(struct device *dev,
 static const DEVICE_ATTR(value, 0644,
 		gpio_value_show, gpio_value_store);
 
+static ssize_t gpio_debounce_show(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	const struct gpio_desc	*desc = dev_get_drvdata(dev);
+	ssize_t status;
+
+	mutex_lock(&sysfs_lock);
+
+	if (!test_bit(FLAG_EXPORT, &desc->flags))
+		status = -EIO;
+	else
+		status = sprintf(buf, "%u\n", desc->debounce);
+
+	mutex_unlock(&sysfs_lock);
+	return status;
+}
+
+static ssize_t gpio_debounce_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t size)
+{
+	const struct gpio_desc	*desc = dev_get_drvdata(dev);
+	unsigned		gpio = desc - gpio_desc;
+	ssize_t			status;
+
+	mutex_lock(&sysfs_lock);
+
+	if (!test_bit(FLAG_EXPORT, &desc->flags))
+		status = -EIO;
+	else if (test_bit(FLAG_IS_OUT, &desc->flags))
+		status = -EPERM;
+	else {
+		long		value;
+
+		status = kstrtoul(buf, 0, &value);
+		if (status == 0) {
+			status = gpio_set_debounce(gpio, value);
+			if (status == 0)
+				status = size;
+		}
+	}
+
+	mutex_unlock(&sysfs_lock);
+	return status;
+}
+
+static const DEVICE_ATTR(debounce, 0644,
+			 gpio_debounce_show, gpio_debounce_store);
+
 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
 {
 	struct sysfs_dirent	*value_sd = priv;
@@ -741,6 +795,10 @@ int gpio_export(unsigned gpio, bool direction_may_change)
 				status = device_create_file(dev,
 						&dev_attr_direction);
 
+			if (!status && desc->chip->set_debounce)
+				status = device_create_file(dev,
+						&dev_attr_debounce);
+
 			if (!status && gpio_to_irq(gpio) >= 0
 					&& (direction_may_change
 						|| !test_bit(FLAG_IS_OUT,
@@ -1507,6 +1565,7 @@ int gpio_set_debounce(unsigned gpio, unsigned debounce)
 
 	might_sleep_if(chip->can_sleep);
 
+	desc->debounce = debounce;
 	return chip->set_debounce(chip, gpio, debounce);
 
 fail:
-- 
1.7.5.4


             reply	other threads:[~2012-12-06  6:32 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-06  6:32 Guenter Roeck [this message]
2012-12-07  8:07 ` [PATCH] gpio: export 'debounce' attribute if supported by the gpio chip Linus Walleij
2012-12-07 14:59   ` Guenter Roeck
2012-12-07 16:20     ` Guenter Roeck
2012-12-07 16:49     ` Alan Cox
2012-12-09  9:58       ` anish kumar
2012-12-09 11:03         ` Alan Cox
2012-12-09 17:07           ` Guenter Roeck
2012-12-09 22:36             ` Grant Likely
2012-12-10 10:11             ` Linus Walleij
2012-12-10 10:04       ` Linus Walleij
2012-12-10 18:48         ` Guenter Roeck
2012-12-10 19:37           ` anish singh
2012-12-13 17:08             ` Guenter Roeck

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=1354775567-17408-1-git-send-email-linux@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=grant.likely@secretlab.ca \
    --cc=linus.walleij@linaro.org \
    --cc=linux-kernel@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).