1 From: David Brownell <dbrownell@users.sourceforge.net>
2 Date: Fri, 25 Jul 2008 08:46:07 +0000 (-0700)
3 Subject: gpio: sysfs interface
4 X-Git-Tag: v2.6.27-rc1~449
5 X-Git-Url: http://git.kernel.org/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git;a=commitdiff_plain;h=d8f388d8dc8d4f36539dd37c1fff62cc404ea0fc
9 This adds a simple sysfs interface for GPIOs.
12 /export ... asks the kernel to export a GPIO to userspace
13 /unexport ... to return a GPIO to the kernel
14 /gpioN ... for each exported GPIO #N
15 /value ... always readable, writes fail for input GPIOs
16 /direction ... r/w as: in, out (default low); write high, low
17 /gpiochipN ... for each gpiochip; #N is its first GPIO
18 /base ... (r/o) same as N
19 /label ... (r/o) descriptive, not necessarily unique
20 /ngpio ... (r/o) number of GPIOs; numbered N .. N+(ngpio - 1)
22 GPIOs claimed by kernel code may be exported by its owner using a new
23 gpio_export() call, which should be most useful for driver debugging.
24 Such exports may optionally be done without a "direction" attribute.
26 Userspace may ask to take over a GPIO by writing to a sysfs control file,
27 helping to cope with incomplete board support or other "one-off"
28 requirements that don't merit full kernel support:
30 echo 23 > /sys/class/gpio/export
31 ... will gpio_request(23, "sysfs") and gpio_export(23);
32 use /sys/class/gpio/gpio-23/direction to (re)configure it,
33 when that GPIO can be used as both input and output.
34 echo 23 > /sys/class/gpio/unexport
35 ... will gpio_free(23), when it was exported as above
37 The extra D-space footprint is a few hundred bytes, except for the sysfs
38 resources associated with each exported GPIO. The additional I-space
39 footprint is about two thirds of the current size of gpiolib (!). Since
40 no /dev node creation is involved, no "udev" support is needed.
44 * This adds a device pointer to "struct gpio_chip". When GPIO
45 providers initialize that, sysfs gpio class devices become children of
46 that device instead of being "virtual" devices.
48 * The (few) gpio_chip providers which have such a device node have
51 * Some gpio_chip drivers also needed to update their module "owner"
52 field ... for which missing kerneldoc was added.
54 * Some gpio_chips don't support input GPIOs. Those GPIOs are now
55 flagged appropriately when the chip is registered.
57 Based on previous patches, and discussion both on and off LKML.
59 A Documentation/ABI/testing/sysfs-gpio update is ready to submit once this
62 [akpm@linux-foundation.org: a few maintenance build fixes]
63 Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
64 Cc: Guennadi Liakhovetski <g.liakhovetski@pengutronix.de>
65 Cc: Greg KH <greg@kroah.com>
66 Cc: Kay Sievers <kay.sievers@vrfy.org>
67 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
68 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
71 --- a/Documentation/gpio.txt
72 +++ b/Documentation/gpio.txt
74 Dynamic definition of GPIOs is not currently standard; for example, as
75 a side effect of configuring an add-on board with some GPIO expanders.
77 -These calls are purely for kernel space, but a userspace API could be built
81 GPIO implementor's framework (OPTIONAL)
82 =======================================
83 As noted earlier, there is an optional implementation framework making it
84 easier for platforms to support different kinds of GPIO controller using
85 -the same programming interface.
86 +the same programming interface. This framework is called "gpiolib".
88 As a debugging aid, if debugfs is available a /sys/kernel/debug/gpio file
89 will be found there. That will list all the controllers registered through
91 calls for that GPIO can work. One way to address such dependencies is for
92 such gpio_chip controllers to provide setup() and teardown() callbacks to
93 board specific code; those board specific callbacks would register devices
94 -once all the necessary resources are available.
95 +once all the necessary resources are available, and remove them later when
96 +the GPIO controller device becomes unavailable.
99 +Sysfs Interface for Userspace (OPTIONAL)
100 +========================================
101 +Platforms which use the "gpiolib" implementors framework may choose to
102 +configure a sysfs user interface to GPIOs. This is different from the
103 +debugfs interface, since it provides control over GPIO direction and
104 +value instead of just showing a gpio state summary. Plus, it could be
105 +present on production systems without debugging support.
107 +Given approprate hardware documentation for the system, userspace could
108 +know for example that GPIO #23 controls the write protect line used to
109 +protect boot loader segments in flash memory. System upgrade procedures
110 +may need to temporarily remove that protection, first importing a GPIO,
111 +then changing its output state, then updating the code before re-enabling
112 +the write protection. In normal use, GPIO #23 would never be touched,
113 +and the kernel would have no need to know about it.
115 +Again depending on appropriate hardware documentation, on some systems
116 +userspace GPIO can be used to determine system configuration data that
117 +standard kernels won't know about. And for some tasks, simple userspace
118 +GPIO drivers could be all that the system really needs.
120 +Note that standard kernel drivers exist for common "LEDs and Buttons"
121 +GPIO tasks: "leds-gpio" and "gpio_keys", respectively. Use those
122 +instead of talking directly to the GPIOs; they integrate with kernel
123 +frameworks better than your userspace code could.
128 +There are three kinds of entry in /sys/class/gpio:
130 + - Control interfaces used to get userspace control over GPIOs;
132 + - GPIOs themselves; and
134 + - GPIO controllers ("gpio_chip" instances).
136 +That's in addition to standard files including the "device" symlink.
138 +The control interfaces are write-only:
142 + "export" ... Userspace may ask the kernel to export control of
143 + a GPIO to userspace by writing its number to this file.
145 + Example: "echo 19 > export" will create a "gpio19" node
146 + for GPIO #19, if that's not requested by kernel code.
148 + "unexport" ... Reverses the effect of exporting to userspace.
150 + Example: "echo 19 > unexport" will remove a "gpio19"
151 + node exported using the "export" file.
153 +GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
154 +and have the following read/write attributes:
156 + /sys/class/gpio/gpioN/
158 + "direction" ... reads as either "in" or "out". This value may
159 + normally be written. Writing as "out" defaults to
160 + initializing the value as low. To ensure glitch free
161 + operation, values "low" and "high" may be written to
162 + configure the GPIO as an output with that initial value.
164 + Note that this attribute *will not exist* if the kernel
165 + doesn't support changing the direction of a GPIO, or
166 + it was exported by kernel code that didn't explicitly
167 + allow userspace to reconfigure this GPIO's direction.
169 + "value" ... reads as either 0 (low) or 1 (high). If the GPIO
170 + is configured as an output, this value may be written;
171 + any nonzero value is treated as high.
173 +GPIO controllers have paths like /sys/class/gpio/chipchip42/ (for the
174 +controller implementing GPIOs starting at #42) and have the following
175 +read-only attributes:
177 + /sys/class/gpio/gpiochipN/
179 + "base" ... same as N, the first GPIO managed by this chip
181 + "label" ... provided for diagnostics (not always unique)
183 + "ngpio" ... how many GPIOs this manges (N to N + ngpio - 1)
185 +Board documentation should in most cases cover what GPIOs are used for
186 +what purposes. However, those numbers are not always stable; GPIOs on
187 +a daughtercard might be different depending on the base board being used,
188 +or other cards in the stack. In such cases, you may need to use the
189 +gpiochip nodes (possibly in conjunction with schematics) to determine
190 +the correct GPIO number to use for a given signal.
193 +Exporting from Kernel code
194 +--------------------------
195 +Kernel code can explicitly manage exports of GPIOs which have already been
196 +requested using gpio_request():
198 + /* export the GPIO to userspace */
199 + int gpio_export(unsigned gpio, bool direction_may_change);
201 + /* reverse gpio_export() */
202 + void gpio_unexport();
204 +After a kernel driver requests a GPIO, it may only be made available in
205 +the sysfs interface by gpio_export(). The driver can control whether the
206 +signal direction may change. This helps drivers prevent userspace code
207 +from accidentally clobbering important system state.
209 +This explicit exporting can help with debugging (by making some kinds
210 +of experiments easier), or can provide an always-there interface that's
211 +suitable for documenting as part of a board support package.
212 --- a/arch/arm/plat-omap/gpio.c
213 +++ b/arch/arm/plat-omap/gpio.c
214 @@ -1488,6 +1488,9 @@
215 bank->chip.set = gpio_set;
216 if (bank_is_mpuio(bank)) {
217 bank->chip.label = "mpuio";
218 +#ifdef CONFIG_ARCH_OMAP1
219 + bank->chip.dev = &omap_mpuio_device.dev;
221 bank->chip.base = OMAP_MPUIO(0);
223 bank->chip.label = "gpio";
224 --- a/arch/avr32/mach-at32ap/pio.c
225 +++ b/arch/avr32/mach-at32ap/pio.c
227 pio->chip.label = pio->name;
228 pio->chip.base = pdev->id * 32;
229 pio->chip.ngpio = 32;
230 + pio->chip.dev = &pdev->dev;
231 + pio->chip.owner = THIS_MODULE;
233 pio->chip.direction_input = direction_input;
234 pio->chip.get = gpio_get;
235 --- a/drivers/gpio/Kconfig
236 +++ b/drivers/gpio/Kconfig
238 slower. The diagnostics help catch the type of setup errors
239 that are most common when setting up new platforms or boards.
242 + bool "/sys/class/gpio/... (sysfs interface)"
243 + depends on SYSFS && EXPERIMENTAL
245 + Say Y here to add a sysfs interface for GPIOs.
247 + This is mostly useful to work around omissions in a system's
248 + kernel support. Those are common in custom and semicustom
249 + hardware assembled using standard kernels with a minimum of
250 + custom patches. In those cases, userspace code may import
251 + a given GPIO from the kernel, if no kernel driver requested it.
253 + Kernel drivers may also request that a particular GPIO be
254 + exported to userspace; this can be useful when debugging.
256 # put expanders in the right section, in alphabetical order
258 comment "I2C GPIO expanders:"
259 --- a/drivers/gpio/gpiolib.c
260 +++ b/drivers/gpio/gpiolib.c
262 #include <linux/module.h>
263 #include <linux/irq.h>
264 #include <linux/spinlock.h>
266 -#include <asm/gpio.h>
267 +#include <linux/device.h>
268 +#include <linux/err.h>
269 +#include <linux/debugfs.h>
270 +#include <linux/seq_file.h>
271 +#include <linux/gpio.h>
274 /* Optional implementation infrastructure for GPIO interfaces.
276 #define FLAG_REQUESTED 0
277 #define FLAG_IS_OUT 1
278 #define FLAG_RESERVED 2
279 +#define FLAG_EXPORT 3 /* protected by sysfs_lock */
280 +#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */
282 #ifdef CONFIG_DEBUG_FS
284 @@ -151,6 +156,486 @@
288 +#ifdef CONFIG_GPIO_SYSFS
290 +/* lock protects against unexport_gpio() being called while
291 + * sysfs files are active.
293 +static DEFINE_MUTEX(sysfs_lock);
296 + * /sys/class/gpio/gpioN... only for GPIOs that are exported
298 + * * MAY BE OMITTED if kernel won't allow direction changes
299 + * * is read/write as "in" or "out"
300 + * * may also be written as "high" or "low", initializing
301 + * output value as specified ("out" implies "low")
303 + * * always readable, subject to hardware behavior
304 + * * may be writable, as zero/nonzero
306 + * REVISIT there will likely be an attribute for configuring async
307 + * notifications, e.g. to specify polling interval or IRQ trigger type
308 + * that would for example trigger a poll() on the "value".
311 +static ssize_t gpio_direction_show(struct device *dev,
312 + struct device_attribute *attr, char *buf)
314 + const struct gpio_desc *desc = dev_get_drvdata(dev);
317 + mutex_lock(&sysfs_lock);
319 + if (!test_bit(FLAG_EXPORT, &desc->flags))
322 + status = sprintf(buf, "%s\n",
323 + test_bit(FLAG_IS_OUT, &desc->flags)
326 + mutex_unlock(&sysfs_lock);
330 +static ssize_t gpio_direction_store(struct device *dev,
331 + struct device_attribute *attr, const char *buf, size_t size)
333 + const struct gpio_desc *desc = dev_get_drvdata(dev);
334 + unsigned gpio = desc - gpio_desc;
337 + mutex_lock(&sysfs_lock);
339 + if (!test_bit(FLAG_EXPORT, &desc->flags))
341 + else if (sysfs_streq(buf, "high"))
342 + status = gpio_direction_output(gpio, 1);
343 + else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
344 + status = gpio_direction_output(gpio, 0);
345 + else if (sysfs_streq(buf, "in"))
346 + status = gpio_direction_input(gpio);
350 + mutex_unlock(&sysfs_lock);
351 + return status ? : size;
354 +static const DEVICE_ATTR(direction, 0644,
355 + gpio_direction_show, gpio_direction_store);
357 +static ssize_t gpio_value_show(struct device *dev,
358 + struct device_attribute *attr, char *buf)
360 + const struct gpio_desc *desc = dev_get_drvdata(dev);
361 + unsigned gpio = desc - gpio_desc;
364 + mutex_lock(&sysfs_lock);
366 + if (!test_bit(FLAG_EXPORT, &desc->flags))
369 + status = sprintf(buf, "%d\n", gpio_get_value_cansleep(gpio));
371 + mutex_unlock(&sysfs_lock);
375 +static ssize_t gpio_value_store(struct device *dev,
376 + struct device_attribute *attr, const char *buf, size_t size)
378 + const struct gpio_desc *desc = dev_get_drvdata(dev);
379 + unsigned gpio = desc - gpio_desc;
382 + mutex_lock(&sysfs_lock);
384 + if (!test_bit(FLAG_EXPORT, &desc->flags))
386 + else if (!test_bit(FLAG_IS_OUT, &desc->flags))
391 + status = strict_strtol(buf, 0, &value);
393 + gpio_set_value_cansleep(gpio, value != 0);
398 + mutex_unlock(&sysfs_lock);
402 +static /*const*/ DEVICE_ATTR(value, 0644,
403 + gpio_value_show, gpio_value_store);
405 +static const struct attribute *gpio_attrs[] = {
406 + &dev_attr_direction.attr,
407 + &dev_attr_value.attr,
411 +static const struct attribute_group gpio_attr_group = {
412 + .attrs = (struct attribute **) gpio_attrs,
416 + * /sys/class/gpio/gpiochipN/
417 + * /base ... matching gpio_chip.base (N)
418 + * /label ... matching gpio_chip.label
419 + * /ngpio ... matching gpio_chip.ngpio
422 +static ssize_t chip_base_show(struct device *dev,
423 + struct device_attribute *attr, char *buf)
425 + const struct gpio_chip *chip = dev_get_drvdata(dev);
427 + return sprintf(buf, "%d\n", chip->base);
429 +static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
431 +static ssize_t chip_label_show(struct device *dev,
432 + struct device_attribute *attr, char *buf)
434 + const struct gpio_chip *chip = dev_get_drvdata(dev);
436 + return sprintf(buf, "%s\n", chip->label ? : "");
438 +static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
440 +static ssize_t chip_ngpio_show(struct device *dev,
441 + struct device_attribute *attr, char *buf)
443 + const struct gpio_chip *chip = dev_get_drvdata(dev);
445 + return sprintf(buf, "%u\n", chip->ngpio);
447 +static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
449 +static const struct attribute *gpiochip_attrs[] = {
450 + &dev_attr_base.attr,
451 + &dev_attr_label.attr,
452 + &dev_attr_ngpio.attr,
456 +static const struct attribute_group gpiochip_attr_group = {
457 + .attrs = (struct attribute **) gpiochip_attrs,
461 + * /sys/class/gpio/export ... write-only
462 + * integer N ... number of GPIO to export (full access)
463 + * /sys/class/gpio/unexport ... write-only
464 + * integer N ... number of GPIO to unexport
466 +static ssize_t export_store(struct class *class, const char *buf, size_t len)
471 + status = strict_strtol(buf, 0, &gpio);
475 + /* No extra locking here; FLAG_SYSFS just signifies that the
476 + * request and export were done by on behalf of userspace, so
477 + * they may be undone on its behalf too.
480 + status = gpio_request(gpio, "sysfs");
484 + status = gpio_export(gpio, true);
488 + set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
492 + pr_debug("%s: status %d\n", __func__, status);
493 + return status ? : len;
496 +static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
501 + status = strict_strtol(buf, 0, &gpio);
507 + /* reject bogus commands (gpio_unexport ignores them) */
508 + if (!gpio_is_valid(gpio))
511 + /* No extra locking here; FLAG_SYSFS just signifies that the
512 + * request and export were done by on behalf of userspace, so
513 + * they may be undone on its behalf too.
515 + if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
521 + pr_debug("%s: status %d\n", __func__, status);
522 + return status ? : len;
525 +static struct class_attribute gpio_class_attrs[] = {
526 + __ATTR(export, 0200, NULL, export_store),
527 + __ATTR(unexport, 0200, NULL, unexport_store),
531 +static struct class gpio_class = {
533 + .owner = THIS_MODULE,
535 + .class_attrs = gpio_class_attrs,
540 + * gpio_export - export a GPIO through sysfs
541 + * @gpio: gpio to make available, already requested
542 + * @direction_may_change: true if userspace may change gpio direction
543 + * Context: arch_initcall or later
545 + * When drivers want to make a GPIO accessible to userspace after they
546 + * have requested it -- perhaps while debugging, or as part of their
547 + * public interface -- they may use this routine. If the GPIO can
548 + * change direction (some can't) and the caller allows it, userspace
549 + * will see "direction" sysfs attribute which may be used to change
550 + * the gpio's direction. A "value" attribute will always be provided.
552 + * Returns zero on success, else an error.
554 +int gpio_export(unsigned gpio, bool direction_may_change)
556 + unsigned long flags;
557 + struct gpio_desc *desc;
558 + int status = -EINVAL;
560 + /* can't export until sysfs is available ... */
561 + if (!gpio_class.subsys.kobj.ktype) {
562 + pr_debug("%s: called too early!\n", __func__);
566 + if (!gpio_is_valid(gpio))
569 + mutex_lock(&sysfs_lock);
571 + spin_lock_irqsave(&gpio_lock, flags);
572 + desc = &gpio_desc[gpio];
573 + if (test_bit(FLAG_REQUESTED, &desc->flags)
574 + && !test_bit(FLAG_EXPORT, &desc->flags)) {
576 + if (!desc->chip->direction_input
577 + || !desc->chip->direction_output)
578 + direction_may_change = false;
580 + spin_unlock_irqrestore(&gpio_lock, flags);
583 + struct device *dev;
585 + dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
588 + dev_set_drvdata(dev, desc);
589 + if (direction_may_change)
590 + status = sysfs_create_group(&dev->kobj,
593 + status = device_create_file(dev,
596 + device_unregister(dev);
600 + set_bit(FLAG_EXPORT, &desc->flags);
603 + mutex_unlock(&sysfs_lock);
607 + pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
611 +EXPORT_SYMBOL_GPL(gpio_export);
613 +static int match_export(struct device *dev, void *data)
615 + return dev_get_drvdata(dev) == data;
619 + * gpio_unexport - reverse effect of gpio_export()
620 + * @gpio: gpio to make unavailable
622 + * This is implicit on gpio_free().
624 +void gpio_unexport(unsigned gpio)
626 + struct gpio_desc *desc;
627 + int status = -EINVAL;
629 + if (!gpio_is_valid(gpio))
632 + mutex_lock(&sysfs_lock);
634 + desc = &gpio_desc[gpio];
635 + if (test_bit(FLAG_EXPORT, &desc->flags)) {
636 + struct device *dev = NULL;
638 + dev = class_find_device(&gpio_class, desc, match_export);
640 + dev_set_drvdata(dev, NULL);
641 + clear_bit(FLAG_EXPORT, &desc->flags);
643 + device_unregister(dev);
649 + mutex_unlock(&sysfs_lock);
652 + pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
654 +EXPORT_SYMBOL_GPL(gpio_unexport);
656 +static int gpiochip_export(struct gpio_chip *chip)
659 + struct device *dev;
661 + /* Many systems register gpio chips for SOC support very early,
662 + * before driver model support is available. In those cases we
663 + * export this later, in gpiolib_sysfs_init() ... here we just
664 + * verify that _some_ field of gpio_class got initialized.
666 + if (!gpio_class.subsys.kobj.ktype)
669 + /* use chip->base for the ID; it's already known to be unique */
670 + mutex_lock(&sysfs_lock);
671 + dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0),
672 + "gpiochip%d", chip->base);
674 + dev_set_drvdata(dev, chip);
675 + status = sysfs_create_group(&dev->kobj,
676 + &gpiochip_attr_group);
679 + chip->exported = (status == 0);
680 + mutex_unlock(&sysfs_lock);
683 + unsigned long flags;
686 + spin_lock_irqsave(&gpio_lock, flags);
688 + while (gpio_desc[gpio].chip == chip)
689 + gpio_desc[gpio++].chip = NULL;
690 + spin_unlock_irqrestore(&gpio_lock, flags);
692 + pr_debug("%s: chip %s status %d\n", __func__,
693 + chip->label, status);
699 +static void gpiochip_unexport(struct gpio_chip *chip)
702 + struct device *dev;
704 + mutex_lock(&sysfs_lock);
705 + dev = class_find_device(&gpio_class, chip, match_export);
707 + dev_set_drvdata(dev, NULL);
709 + device_unregister(dev);
710 + chip->exported = 0;
714 + mutex_unlock(&sysfs_lock);
717 + pr_debug("%s: chip %s status %d\n", __func__,
718 + chip->label, status);
721 +static int __init gpiolib_sysfs_init(void)
724 + unsigned long flags;
727 + status = class_register(&gpio_class);
731 + /* Scan and register the gpio_chips which registered very
732 + * early (e.g. before the class_register above was called).
734 + * We run before arch_initcall() so chip->dev nodes can have
735 + * registered, and so arch_initcall() can always gpio_export().
737 + spin_lock_irqsave(&gpio_lock, flags);
738 + for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
739 + struct gpio_chip *chip;
741 + chip = gpio_desc[gpio].chip;
742 + if (!chip || chip->exported)
745 + spin_unlock_irqrestore(&gpio_lock, flags);
746 + status = gpiochip_export(chip);
747 + spin_lock_irqsave(&gpio_lock, flags);
749 + spin_unlock_irqrestore(&gpio_lock, flags);
754 +postcore_initcall(gpiolib_sysfs_init);
757 +static inline int gpiochip_export(struct gpio_chip *chip)
762 +static inline void gpiochip_unexport(struct gpio_chip *chip)
766 +#endif /* CONFIG_GPIO_SYSFS */
769 * gpiochip_add() - register a gpio_chip
770 * @chip: the chip to register, with chip->base initialized
772 * because the chip->base is invalid or already associated with a
773 * different chip. Otherwise it returns zero as a success code.
775 + * When gpiochip_add() is called very early during boot, so that GPIOs
776 + * can be freely used, the chip->dev device must be registered before
777 + * the gpio framework's arch_initcall(). Otherwise sysfs initialization
778 + * for GPIOs will fail rudely.
780 * If chip->base is negative, this requests dynamic assignment of
781 * a range of valid GPIOs.
784 base = gpiochip_find_base(chip->ngpio);
792 @@ -197,12 +687,23 @@
794 for (id = base; id < base + chip->ngpio; id++) {
795 gpio_desc[id].chip = chip;
796 - gpio_desc[id].flags = 0;
798 + /* REVISIT: most hardware initializes GPIOs as
799 + * inputs (often with pullups enabled) so power
800 + * usage is minimized. Linux code should set the
801 + * gpio direction first thing; but until it does,
802 + * we may expose the wrong direction in sysfs.
804 + gpio_desc[id].flags = !chip->direction_input
805 + ? (1 << FLAG_IS_OUT)
812 spin_unlock_irqrestore(&gpio_lock, flags);
814 + status = gpiochip_export(chip);
816 /* failures here can mean systems won't boot... */
821 spin_unlock_irqrestore(&gpio_lock, flags);
824 + gpiochip_unexport(chip);
828 EXPORT_SYMBOL_GPL(gpiochip_remove);
833 + gpio_unexport(gpio);
835 spin_lock_irqsave(&gpio_lock, flags);
837 desc = &gpio_desc[gpio];
838 @@ -534,10 +1041,6 @@
840 #ifdef CONFIG_DEBUG_FS
842 -#include <linux/debugfs.h>
843 -#include <linux/seq_file.h>
846 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
849 @@ -614,17 +1117,28 @@
850 /* REVISIT this isn't locked against gpio_chip removal ... */
852 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
853 + struct device *dev;
855 if (chip == gpio_desc[gpio].chip)
857 chip = gpio_desc[gpio].chip;
861 - seq_printf(s, "%sGPIOs %d-%d, %s%s:\n",
862 + seq_printf(s, "%sGPIOs %d-%d",
864 - chip->base, chip->base + chip->ngpio - 1,
865 - chip->label ? : "generic",
866 - chip->can_sleep ? ", can sleep" : "");
867 + chip->base, chip->base + chip->ngpio - 1);
870 + seq_printf(s, ", %s/%s",
871 + dev->bus ? dev->bus->name : "no-bus",
874 + seq_printf(s, ", %s", chip->label);
875 + if (chip->can_sleep)
876 + seq_printf(s, ", can sleep");
877 + seq_printf(s, ":\n");
881 chip->dbg_show(s, chip);
882 --- a/drivers/gpio/mcp23s08.c
883 +++ b/drivers/gpio/mcp23s08.c
885 mcp->chip.base = pdata->base;
887 mcp->chip.can_sleep = 1;
888 + mcp->chip.dev = &spi->dev;
889 mcp->chip.owner = THIS_MODULE;
891 spi_set_drvdata(spi, mcp);
892 --- a/drivers/gpio/pca953x.c
893 +++ b/drivers/gpio/pca953x.c
895 gc->base = chip->gpio_start;
897 gc->label = chip->client->name;
898 + gc->dev = &chip->client->dev;
899 gc->owner = THIS_MODULE;
902 --- a/drivers/gpio/pcf857x.c
903 +++ b/drivers/gpio/pcf857x.c
906 gpio->chip.base = pdata->gpio_base;
907 gpio->chip.can_sleep = 1;
908 + gpio->chip.dev = &client->dev;
909 gpio->chip.owner = THIS_MODULE;
911 /* NOTE: the OnSemi jlc1562b is also largely compatible with
912 --- a/drivers/i2c/chips/tps65010.c
913 +++ b/drivers/i2c/chips/tps65010.c
915 tps->outmask = board->outmask;
917 tps->chip.label = client->name;
918 + tps->chip.dev = &client->dev;
919 + tps->chip.owner = THIS_MODULE;
921 tps->chip.set = tps65010_gpio_set;
922 tps->chip.direction_output = tps65010_output;
923 --- a/drivers/mfd/htc-egpio.c
924 +++ b/drivers/mfd/htc-egpio.c
926 ei->chip[i].dev = &(pdev->dev);
927 chip = &(ei->chip[i].chip);
928 chip->label = "htc-egpio";
929 + chip->dev = &pdev->dev;
930 + chip->owner = THIS_MODULE;
931 chip->get = egpio_get;
932 chip->set = egpio_set;
933 chip->direction_input = egpio_direction_input;
934 --- a/include/asm-generic/gpio.h
935 +++ b/include/asm-generic/gpio.h
938 * struct gpio_chip - abstract a GPIO controller
939 * @label: for diagnostics
940 + * @dev: optional device providing the GPIOs
941 + * @owner: helps prevent removal of modules exporting active GPIOs
942 * @direction_input: configures signal "offset" as input, or returns error
943 * @get: returns value for signal "offset"; for output signals this
944 * returns either the value actually sensed, or zero
949 + struct device *dev;
950 struct module *owner;
952 int (*direction_input)(struct gpio_chip *chip,
956 unsigned can_sleep:1;
957 + unsigned exported:1;
960 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
962 extern int __gpio_cansleep(unsigned gpio);
966 +#ifdef CONFIG_GPIO_SYSFS
969 + * A sysfs interface can be exported by individual drivers if they want,
970 + * but more typically is configured entirely from userspace.
972 +extern int gpio_export(unsigned gpio, bool direction_may_change);
973 +extern void gpio_unexport(unsigned gpio);
975 +#endif /* CONFIG_GPIO_SYSFS */
977 +#else /* !CONFIG_HAVE_GPIO_LIB */
979 static inline int gpio_is_valid(int number)
982 gpio_set_value(gpio, value);
986 +#endif /* !CONFIG_HAVE_GPIO_LIB */
988 +#ifndef CONFIG_GPIO_SYSFS
990 +#include <asm/errno.h>
992 +/* sysfs support is only available with gpiolib, where it's optional */
994 +static inline int gpio_export(unsigned gpio, bool direction_may_change)
999 +static inline void gpio_unexport(unsigned gpio)
1002 +#endif /* CONFIG_GPIO_SYSFS */
1004 #endif /* _ASM_GENERIC_GPIO_H */
1005 --- a/include/linux/gpio.h
1006 +++ b/include/linux/gpio.h
1011 +static inline int gpio_export(unsigned gpio, bool direction_may_change)
1013 + /* GPIO can never have been requested or set as {in,out}put */
1018 +static inline void gpio_unexport(unsigned gpio)
1020 + /* GPIO can never have been exported */
1024 static inline int gpio_to_irq(unsigned gpio)
1026 /* GPIO can never have been requested or set as input */