gpiolib: Add pin change notification
Subsystems (2)
PATCH v2 1/1 gpiolib: Add pin change notification
This adds pin change notification to the gpiolib sysfs interface. It requires 16 extra bytes in gpio_desc iff CONFIG_GPIO_SYSFS which in turn means, eg, 4k of .bss usage on AVR32. Due to limitations in sysfs, this patch makes poll(2) and friends work as expected on the "value" attribute, though reads on "value" will never block and there is no facility for async reads and writes.
---
I forgot I'd been holding this stuff, v1 was posted some time back to no reviews. Since then .bss usage has been more than halved, docco's been added and IMO the user interface is nicer.
Documentation/gpio.txt | 48 +++++++++ drivers/gpio/gpiolib.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 289 insertions(+), 5 deletions(-)
Documentation/gpio.txt +48 -0
@@ -524,6 +524,21 @@ and have the following read/write attributes: is configured as an output, this value may be written; any nonzero value is treated as high. + "notify" ... Selects a method for the detection of pin change + events. This can be written or read as one of "none", + "irq" or a number. If "none", no detection will be + done, if "irq" then the change detection will be done + by registering an interrupt handler on the line given + by gpio_to_irq for that gpio. If a number is written, + the gpio will be polled for a state change every n + milliseconds where n is the number written. The + minimum interval is 10 milliseconds. + + "notify_filter" ... This can be written and read as one of + "rising", "falling" or "both". If "rising", only + rising edges will be reported, similarly for "falling" + and "both". + GPIO controllers have paths like /sys/class/gpio/chipchip42/ (for the controller implementing GPIOs starting at #42) and have the following read-only attributes:
@@ -544,6 +559,39 @@ gpiochip nodes (possibly in conjunction with schematics) to determine the correct GPIO number to use for a given signal. +Pin Change Notification +----------------------- +The "value" attribute of a gpio is pollable. For this to work, you +must have set up the notify attribute of that gpio to the type of +detection suitable for the underlying hardware. + +If the hardware supports interrupts on both edges and the gpio to +interrupt line mapping is unique, you should write "irq" to the notify +attribute. + +If the hardware doesn't support this, or you're unsure, you can write +a number to the notify attribute. This number is the delay between +successive polls of the gpio state in milliseconds. You may not poll +more often than 10ms intervals. + +You must use poll mode if communication with the gpio's chip requires +sleeping. This is the case if, for example, the gpio is part of a +I2C or SPI GPIO expander. + +By default, both rising and falling edges are reported. To filter +this, you can write one of "rising" or "falling" to the notify_filter +attribute. To go back to being notified of both edge changes, write +"both" to this attribute. + +Once the notification has been set up, you may use poll(2) and friends +on the "value" attribute. This attribute will register as having new +data ready to be read if and only if there has been a state change +since the last read(2) of the "value" attribute. + +Note that unlike a regular device file, a read on the "value" attribute +will never block whether or not there's new data to be read. + + Exporting from Kernel code -------------------------- Kernel code can explicitly manage exports of GPIOs which have already been
drivers/gpio/gpiolib.c +225 -4
264 lines not on this page · raw message
@@ -387,6 +607,17 @@ static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
status = 0;
gpio_free(gpio);
}
+
+ if (test_and_clear_bit(ASYNC_MODE_IRQ, &gpio_desc[gpio].flags))
+ free_irq(gpio_to_irq(gpio), &gpio_desc[gpio]);
+
+ if (test_and_clear_bit(ASYNC_MODE_POLL, &gpio_desc[gpio].flags)) {
+ BUG_ON(!gpio_desc[gpio].poll);
+
+ cancel_delayed_work(&gpio_desc[gpio].poll->work);
+ kfree(gpio_desc[gpio].poll);
+ }
+
done:
if (status)
pr_debug("%s: status %d\n", __func__, status);
@@ -468,6 +699,12 @@ int gpio_export(unsigned gpio, bool direction_may_change)
status = -ENODEV;
if (status == 0)
set_bit(FLAG_EXPORT, &desc->flags);
+
+ desc->dev = dev;
+ /* 100ms default poll interval */
+ desc->timeout = HZ / 10;
+ set_bit(ASYNC_RISING, &desc->flags);
+ set_bit(ASYNC_FALLING, &desc->flags);
}
mutex_unlock(&sysfs_lock);
@@ -615,7 +852,6 @@ static int __init gpiolib_sysfs_init(void)
}
spin_unlock_irqrestore(&gpio_lock, flags);
-
return status;
}
postcore_initcall(gpiolib_sysfs_init);