linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
@ 2011-11-11  9:40 Patrick Combes
  2011-11-15 13:16 ` Mark Brown
  2011-11-21  7:47 ` Linus Walleij
  0 siblings, 2 replies; 8+ messages in thread
From: Patrick Combes @ 2011-11-11  9:40 UTC (permalink / raw)
  To: linux-arm-kernel

From: Hugo Dupras <h-dupras@ti.com>

By calling poll() on the /sys/class/gpio/gpioN/value sysfs file, usermode
application can take benefit of gpio interrupts.
However, depending on the power state reached, this interrupt may not wake-up
the CPU.
This patch creates a new sysfs file /sys/class/gpio/gpioN/irq_wake to enable
usermode application to set the wake properties of a gpio IRQ.
This option can be set or not for each gpio to preserve power consumption (e.g
embedded systems).

Signed-off-by: Hugo Dupras <h-dupras@ti.com>
Signed-off-by: Patrick Combes <p-combes@ti.com>
---
 drivers/gpio/gpiolib.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 54 insertions(+), 1 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index a971e3d..cf03aed 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -543,6 +543,55 @@ static ssize_t gpio_active_low_store(struct device *dev,
 static const DEVICE_ATTR(active_low, 0644,
 		gpio_active_low_show, gpio_active_low_store);
 
+static ssize_t gpio_irq_wake_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 (sysfs_streq(buf, "enable") || sysfs_streq(buf, "1"))
+		status = enable_irq_wake(gpio_to_irq(gpio));
+	else if (sysfs_streq(buf, "disable") || sysfs_streq(buf, "0"))
+		status = disable_irq_wake(gpio_to_irq(gpio));
+	else
+		status = -EINVAL;
+
+	mutex_unlock(&sysfs_lock);
+
+	return status ? : size;
+}
+
+static ssize_t gpio_irq_wake_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	const struct gpio_desc	*desc = dev_get_drvdata(dev);
+	unsigned		gpio = desc - gpio_desc;
+	int			irq = gpio_to_irq(gpio);
+	ssize_t			status;
+	struct irq_data		*gpio_irq_data = irq_get_irq_data(irq);
+
+	mutex_lock(&sysfs_lock);
+
+	if (gpio_irq_data)
+		status = sprintf(buf, " Wakeup %s\n",
+					irqd_is_wakeup_set(gpio_irq_data)
+					? " enable" : "disable");
+	else
+		status = -EINVAL;
+
+	mutex_unlock(&sysfs_lock);
+
+	return status;
+}
+
+static const DEVICE_ATTR(irq_wake, 0644,
+		gpio_irq_wake_show, gpio_irq_wake_store);
+
 static const struct attribute *gpio_attrs[] = {
 	&dev_attr_value.attr,
 	&dev_attr_active_low.attr,
@@ -744,9 +793,13 @@ int gpio_export(unsigned gpio, bool direction_may_change)
 			if (!status && gpio_to_irq(gpio) >= 0
 					&& (direction_may_change
 						|| !test_bit(FLAG_IS_OUT,
-							&desc->flags)))
+							&desc->flags))) {
 				status = device_create_file(dev,
 						&dev_attr_edge);
+				if (!status)
+					status = device_create_file(dev,
+							&dev_attr_irq_wake);
+			}
 
 			if (status != 0)
 				device_unregister(dev);
-- 
1.7.0.4

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
  2011-11-11  9:40 [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file Patrick Combes
@ 2011-11-15 13:16 ` Mark Brown
  2011-11-15 17:20   ` Russell King - ARM Linux
  2011-11-16 14:30   ` Patrick Combes
  2011-11-21  7:47 ` Linus Walleij
  1 sibling, 2 replies; 8+ messages in thread
From: Mark Brown @ 2011-11-15 13:16 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, Nov 11, 2011 at 10:40:06AM +0100, Patrick Combes wrote:

> By calling poll() on the /sys/class/gpio/gpioN/value sysfs file, usermode
> application can take benefit of gpio interrupts.
> However, depending on the power state reached, this interrupt may not wake-up
> the CPU.
> This patch creates a new sysfs file /sys/class/gpio/gpioN/irq_wake to enable
> usermode application to set the wake properties of a gpio IRQ.
> This option can be set or not for each gpio to preserve power consumption (e.g
> embedded systems).

There's already device global control for this in sysfs via the power
framework - 

Also...

> +	else if (sysfs_streq(buf, "enable") || sysfs_streq(buf, "1"))
> +		status = enable_irq_wake(gpio_to_irq(gpio));
> +	else if (sysfs_streq(buf, "disable") || sysfs_streq(buf, "0"))
> +		status = disable_irq_wake(gpio_to_irq(gpio));
> +	else
> +		status = -EINVAL;

...this doesn't do anything to stop userspace doing multiple enables and
disables.

> +	if (gpio_irq_data)
> +		status = sprintf(buf, " Wakeup %s\n",
> +					irqd_is_wakeup_set(gpio_irq_data)
> +					? " enable" : "disable");

This is *really* non-idiomatic for sysfs output, you'd expect the sysfs
output to look like the input.  It's supposed to machine parsable...

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
  2011-11-15 13:16 ` Mark Brown
@ 2011-11-15 17:20   ` Russell King - ARM Linux
  2011-11-16 14:39     ` Patrick Combes
  2011-11-16 14:30   ` Patrick Combes
  1 sibling, 1 reply; 8+ messages in thread
From: Russell King - ARM Linux @ 2011-11-15 17:20 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Nov 15, 2011 at 01:16:37PM +0000, Mark Brown wrote:
> On Fri, Nov 11, 2011 at 10:40:06AM +0100, Patrick Combes wrote:
> > +	else if (sysfs_streq(buf, "enable") || sysfs_streq(buf, "1"))
> > +		status = enable_irq_wake(gpio_to_irq(gpio));
> > +	else if (sysfs_streq(buf, "disable") || sysfs_streq(buf, "0"))
> > +		status = disable_irq_wake(gpio_to_irq(gpio));
> > +	else
> > +		status = -EINVAL;
> 
> ...this doesn't do anything to stop userspace doing multiple enables and
> disables.

It doesn't check whether gpio_to_irq(gpio) returns something sane either.
It should be doing something like this before taking the mutex:

	irq = gpio_to_irq(gpio);
	if (irq <= 0)
		return irq < 0 ? irq : -EINVAL;

and then doing (enable|disable)_irq_wake() on irq.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
  2011-11-15 13:16 ` Mark Brown
  2011-11-15 17:20   ` Russell King - ARM Linux
@ 2011-11-16 14:30   ` Patrick Combes
  2011-11-16 14:37     ` Mark Brown
  1 sibling, 1 reply; 8+ messages in thread
From: Patrick Combes @ 2011-11-16 14:30 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Nov 15, 2011 at 01:16:37PM +0000, Mark Brown wrote:
> On Fri, Nov 11, 2011 at 10:40:06AM +0100, Patrick Combes wrote:
> 
> > By calling poll() on the /sys/class/gpio/gpioN/value sysfs file, usermode
> > application can take benefit of gpio interrupts.
> > However, depending on the power state reached, this interrupt may not wake-up
> > the CPU.
> > This patch creates a new sysfs file /sys/class/gpio/gpioN/irq_wake to enable
> > usermode application to set the wake properties of a gpio IRQ.
> > This option can be set or not for each gpio to preserve power consumption (e.g
> > embedded systems).
> 
> There's already device global control for this in sysfs via the power
> framework - 

I didn't know so I had a (quick) look at it. It seems to me that this interface
will fit better for a global GPIO wakeup capability. By the way, this is maybe
what you mean by 'global control'...
Therefore, there is still a need (at least I have) for an interface with a
deeper granularity.

> 
> Also...
> 
> > +	else if (sysfs_streq(buf, "enable") || sysfs_streq(buf, "1"))
> > +		status = enable_irq_wake(gpio_to_irq(gpio));
> > +	else if (sysfs_streq(buf, "disable") || sysfs_streq(buf, "0"))
> > +		status = disable_irq_wake(gpio_to_irq(gpio));
> > +	else
> > +		status = -EINVAL;
> 
> ...this doesn't do anything to stop userspace doing multiple enables and
> disables.

Do you mean there is a need to prevent that?
Basically the code above accepts both "1" or "enable" strings to enable the
property. I could limit that to "enable" / "disable" if it is confusing.

> 
> > +	if (gpio_irq_data)
> > +		status = sprintf(buf, " Wakeup %s\n",
> > +					irqd_is_wakeup_set(gpio_irq_data)
> > +					? " enable" : "disable");
> 
> This is *really* non-idiomatic for sysfs output, you'd expect the sysfs
> output to look like the input.  It's supposed to machine parsable...

Maybe good for the debug but not for a parser; I fix it.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
  2011-11-16 14:30   ` Patrick Combes
@ 2011-11-16 14:37     ` Mark Brown
  2011-11-16 17:23       ` Patrick Combes
  0 siblings, 1 reply; 8+ messages in thread
From: Mark Brown @ 2011-11-16 14:37 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Nov 16, 2011 at 03:30:13PM +0100, Patrick Combes wrote:

> > ...this doesn't do anything to stop userspace doing multiple enables and
> > disables.

> Do you mean there is a need to prevent that?
> Basically the code above accepts both "1" or "enable" strings to enable the
> property. I could limit that to "enable" / "disable" if it is confusing.

That's not the problem.  The problem is that if you disable it might not
actually disable the wake if it's previously been enabled multiple times.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
  2011-11-15 17:20   ` Russell King - ARM Linux
@ 2011-11-16 14:39     ` Patrick Combes
  0 siblings, 0 replies; 8+ messages in thread
From: Patrick Combes @ 2011-11-16 14:39 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Nov 15, 2011 at 05:20:53PM +0000, Russell King - ARM Linux wrote:
> On Tue, Nov 15, 2011 at 01:16:37PM +0000, Mark Brown wrote:
> > On Fri, Nov 11, 2011 at 10:40:06AM +0100, Patrick Combes wrote:
> > > +	else if (sysfs_streq(buf, "enable") || sysfs_streq(buf, "1"))
> > > +		status = enable_irq_wake(gpio_to_irq(gpio));
> > > +	else if (sysfs_streq(buf, "disable") || sysfs_streq(buf, "0"))
> > > +		status = disable_irq_wake(gpio_to_irq(gpio));
> > > +	else
> > > +		status = -EINVAL;
> > 
> > ...this doesn't do anything to stop userspace doing multiple enables and
> > disables.
> 
> It doesn't check whether gpio_to_irq(gpio) returns something sane either.
> It should be doing something like this before taking the mutex:
> 
> 	irq = gpio_to_irq(gpio);
> 	if (irq <= 0)
> 		return irq < 0 ? irq : -EINVAL;
> 
> and then doing (enable|disable)_irq_wake() on irq.

Ok I add this check

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
  2011-11-16 14:37     ` Mark Brown
@ 2011-11-16 17:23       ` Patrick Combes
  0 siblings, 0 replies; 8+ messages in thread
From: Patrick Combes @ 2011-11-16 17:23 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Nov 16, 2011 at 02:37:31PM +0000, Mark Brown wrote:
> On Wed, Nov 16, 2011 at 03:30:13PM +0100, Patrick Combes wrote:
> 
> > > ...this doesn't do anything to stop userspace doing multiple enables and
> > > disables.
> 
> > Do you mean there is a need to prevent that?
> > Basically the code above accepts both "1" or "enable" strings to enable the
> > property. I could limit that to "enable" / "disable" if it is confusing.
> 
> That's not the problem.  The problem is that if you disable it might not
> actually disable the wake if it's previously been enabled multiple times.

You're right; I however wonder if this API should work as 'enable_irq_wake' or
if it should filter multiple enables/disables. I'd have supposed it's a choice
of implementation but based on your comment I understand filtering is the
expected way. I'll modify the function accordingly. Thanks

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file
  2011-11-11  9:40 [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file Patrick Combes
  2011-11-15 13:16 ` Mark Brown
@ 2011-11-21  7:47 ` Linus Walleij
  1 sibling, 0 replies; 8+ messages in thread
From: Linus Walleij @ 2011-11-21  7:47 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, Nov 11, 2011 at 10:40 AM, Patrick Combes <p-combes@ti.com> wrote:

> This patch creates a new sysfs file /sys/class/gpio/gpioN/irq_wake to enable
> usermode application to set the wake properties of a gpio IRQ.
> This option can be set or not for each gpio to preserve power consumption (e.g
> embedded systems).
(...)
> + ? ? ? if (!test_bit(FLAG_EXPORT, &desc->flags))
> + ? ? ? ? ? ? ? status = -EIO;
> + ? ? ? else if (sysfs_streq(buf, "enable") || sysfs_streq(buf, "1"))
> + ? ? ? ? ? ? ? status = enable_irq_wake(gpio_to_irq(gpio));
> + ? ? ? else if (sysfs_streq(buf, "disable") || sysfs_streq(buf, "0"))
> + ? ? ? ? ? ? ? status = disable_irq_wake(gpio_to_irq(gpio));
> + ? ? ? else
> + ? ? ? ? ? ? ? status = -EINVAL;

As noted elsewhere, human-readable files go into debugfs.

sysfs rule is one file, one value.

Create a file ile named "irq_wakeup_enable" that works
like this:

cat wakeup_enable
0
echo 1 > wakeup_enable
cat wakeup_enable
1

Also document you new ABI in
Documentation/ABI/testing/*
and preferabley write some new words on it
in Documentation/gpio.txt too.

Thanks!
Linus Walleij

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-11-21  7:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-11  9:40 [RFC][PATCH] gpiolib: add irq_wake (power-management) sysfs file Patrick Combes
2011-11-15 13:16 ` Mark Brown
2011-11-15 17:20   ` Russell King - ARM Linux
2011-11-16 14:39     ` Patrick Combes
2011-11-16 14:30   ` Patrick Combes
2011-11-16 14:37     ` Mark Brown
2011-11-16 17:23       ` Patrick Combes
2011-11-21  7:47 ` Linus Walleij

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).