add the R_ARM_V4BX relocation to the ARM kernel module loader
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.26 / 980-backport_gpio_sysfs_support.patch
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
6
7 gpio: sysfs interface
8
9 This adds a simple sysfs interface for GPIOs.
10
11 /sys/class/gpio
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)
21
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.
25
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:
29
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
36
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.
41
42 Related changes:
43
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.
47
48 * The (few) gpio_chip providers which have such a device node have
49 been updated.
50
51 * Some gpio_chip drivers also needed to update their module "owner"
52 field ... for which missing kerneldoc was added.
53
54 * Some gpio_chips don't support input GPIOs. Those GPIOs are now
55 flagged appropriately when the chip is registered.
56
57 Based on previous patches, and discussion both on and off LKML.
58
59 A Documentation/ABI/testing/sysfs-gpio update is ready to submit once this
60 merges to mainline.
61
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>
69 ---
70
71 --- a/Documentation/gpio.txt
72 +++ b/Documentation/gpio.txt
73 @@ -347,15 +347,12 @@ necessarily be nonportable.
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.
76
77 -These calls are purely for kernel space, but a userspace API could be built
78 -on top of them.
79 -
80
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".
87
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
90 @@ -439,4 +436,120 @@ becomes available. That may mean the de
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.
97 +
98 +
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.
106 +
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.
114 +
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.
119 +
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.
124 +
125 +
126 +Paths in Sysfs
127 +--------------
128 +There are three kinds of entry in /sys/class/gpio:
129 +
130 + - Control interfaces used to get userspace control over GPIOs;
131 +
132 + - GPIOs themselves; and
133 +
134 + - GPIO controllers ("gpio_chip" instances).
135 +
136 +That's in addition to standard files including the "device" symlink.
137 +
138 +The control interfaces are write-only:
139 +
140 + /sys/class/gpio/
141 +
142 + "export" ... Userspace may ask the kernel to export control of
143 + a GPIO to userspace by writing its number to this file.
144 +
145 + Example: "echo 19 > export" will create a "gpio19" node
146 + for GPIO #19, if that's not requested by kernel code.
147 +
148 + "unexport" ... Reverses the effect of exporting to userspace.
149 +
150 + Example: "echo 19 > unexport" will remove a "gpio19"
151 + node exported using the "export" file.
152 +
153 +GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
154 +and have the following read/write attributes:
155 +
156 + /sys/class/gpio/gpioN/
157 +
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.
163 +
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.
168 +
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.
172 +
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:
176 +
177 + /sys/class/gpio/gpiochipN/
178 +
179 + "base" ... same as N, the first GPIO managed by this chip
180 +
181 + "label" ... provided for diagnostics (not always unique)
182 +
183 + "ngpio" ... how many GPIOs this manges (N to N + ngpio - 1)
184 +
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.
191 +
192 +
193 +Exporting from Kernel code
194 +--------------------------
195 +Kernel code can explicitly manage exports of GPIOs which have already been
196 +requested using gpio_request():
197 +
198 + /* export the GPIO to userspace */
199 + int gpio_export(unsigned gpio, bool direction_may_change);
200 +
201 + /* reverse gpio_export() */
202 + void gpio_unexport();
203 +
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.
208 +
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 @@ static int __init _omap_gpio_init(void)
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;
220 +#endif
221 bank->chip.base = OMAP_MPUIO(0);
222 } else {
223 bank->chip.label = "gpio";
224 --- a/arch/avr32/mach-at32ap/pio.c
225 +++ b/arch/avr32/mach-at32ap/pio.c
226 @@ -358,6 +358,8 @@ static int __init pio_probe(struct platf
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;
232
233 pio->chip.direction_input = direction_input;
234 pio->chip.get = gpio_get;
235 --- a/drivers/gpio/Kconfig
236 +++ b/drivers/gpio/Kconfig
237 @@ -23,6 +23,21 @@ config DEBUG_GPIO
238 slower. The diagnostics help catch the type of setup errors
239 that are most common when setting up new platforms or boards.
240
241 +config GPIO_SYSFS
242 + bool "/sys/class/gpio/... (sysfs interface)"
243 + depends on SYSFS && EXPERIMENTAL
244 + help
245 + Say Y here to add a sysfs interface for GPIOs.
246 +
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.
252 +
253 + Kernel drivers may also request that a particular GPIO be
254 + exported to userspace; this can be useful when debugging.
255 +
256 # put expanders in the right section, in alphabetical order
257
258 comment "I2C GPIO expanders:"
259 --- a/drivers/gpio/gpiolib.c
260 +++ b/drivers/gpio/gpiolib.c
261 @@ -2,8 +2,11 @@
262 #include <linux/module.h>
263 #include <linux/irq.h>
264 #include <linux/spinlock.h>
265 -
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>
272
273
274 /* Optional implementation infrastructure for GPIO interfaces.
275 @@ -44,6 +47,8 @@ struct gpio_desc {
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 */
281
282 #ifdef CONFIG_DEBUG_FS
283 const char *label;
284 @@ -151,6 +156,486 @@ err:
285 return ret;
286 }
287
288 +#ifdef CONFIG_GPIO_SYSFS
289 +
290 +/* lock protects against unexport_gpio() being called while
291 + * sysfs files are active.
292 + */
293 +static DEFINE_MUTEX(sysfs_lock);
294 +
295 +/*
296 + * /sys/class/gpio/gpioN... only for GPIOs that are exported
297 + * /direction
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")
302 + * /value
303 + * * always readable, subject to hardware behavior
304 + * * may be writable, as zero/nonzero
305 + *
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".
309 + */
310 +
311 +static ssize_t gpio_direction_show(struct device *dev,
312 + struct device_attribute *attr, char *buf)
313 +{
314 + const struct gpio_desc *desc = dev_get_drvdata(dev);
315 + ssize_t status;
316 +
317 + mutex_lock(&sysfs_lock);
318 +
319 + if (!test_bit(FLAG_EXPORT, &desc->flags))
320 + status = -EIO;
321 + else
322 + status = sprintf(buf, "%s\n",
323 + test_bit(FLAG_IS_OUT, &desc->flags)
324 + ? "out" : "in");
325 +
326 + mutex_unlock(&sysfs_lock);
327 + return status;
328 +}
329 +
330 +static ssize_t gpio_direction_store(struct device *dev,
331 + struct device_attribute *attr, const char *buf, size_t size)
332 +{
333 + const struct gpio_desc *desc = dev_get_drvdata(dev);
334 + unsigned gpio = desc - gpio_desc;
335 + ssize_t status;
336 +
337 + mutex_lock(&sysfs_lock);
338 +
339 + if (!test_bit(FLAG_EXPORT, &desc->flags))
340 + status = -EIO;
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);
347 + else
348 + status = -EINVAL;
349 +
350 + mutex_unlock(&sysfs_lock);
351 + return status ? : size;
352 +}
353 +
354 +static const DEVICE_ATTR(direction, 0644,
355 + gpio_direction_show, gpio_direction_store);
356 +
357 +static ssize_t gpio_value_show(struct device *dev,
358 + struct device_attribute *attr, char *buf)
359 +{
360 + const struct gpio_desc *desc = dev_get_drvdata(dev);
361 + unsigned gpio = desc - gpio_desc;
362 + ssize_t status;
363 +
364 + mutex_lock(&sysfs_lock);
365 +
366 + if (!test_bit(FLAG_EXPORT, &desc->flags))
367 + status = -EIO;
368 + else
369 + status = sprintf(buf, "%d\n", gpio_get_value_cansleep(gpio));
370 +
371 + mutex_unlock(&sysfs_lock);
372 + return status;
373 +}
374 +
375 +static ssize_t gpio_value_store(struct device *dev,
376 + struct device_attribute *attr, const char *buf, size_t size)
377 +{
378 + const struct gpio_desc *desc = dev_get_drvdata(dev);
379 + unsigned gpio = desc - gpio_desc;
380 + ssize_t status;
381 +
382 + mutex_lock(&sysfs_lock);
383 +
384 + if (!test_bit(FLAG_EXPORT, &desc->flags))
385 + status = -EIO;
386 + else if (!test_bit(FLAG_IS_OUT, &desc->flags))
387 + status = -EPERM;
388 + else {
389 + long value;
390 +
391 + status = strict_strtol(buf, 0, &value);
392 + if (status == 0) {
393 + gpio_set_value_cansleep(gpio, value != 0);
394 + status = size;
395 + }
396 + }
397 +
398 + mutex_unlock(&sysfs_lock);
399 + return status;
400 +}
401 +
402 +static /*const*/ DEVICE_ATTR(value, 0644,
403 + gpio_value_show, gpio_value_store);
404 +
405 +static const struct attribute *gpio_attrs[] = {
406 + &dev_attr_direction.attr,
407 + &dev_attr_value.attr,
408 + NULL,
409 +};
410 +
411 +static const struct attribute_group gpio_attr_group = {
412 + .attrs = (struct attribute **) gpio_attrs,
413 +};
414 +
415 +/*
416 + * /sys/class/gpio/gpiochipN/
417 + * /base ... matching gpio_chip.base (N)
418 + * /label ... matching gpio_chip.label
419 + * /ngpio ... matching gpio_chip.ngpio
420 + */
421 +
422 +static ssize_t chip_base_show(struct device *dev,
423 + struct device_attribute *attr, char *buf)
424 +{
425 + const struct gpio_chip *chip = dev_get_drvdata(dev);
426 +
427 + return sprintf(buf, "%d\n", chip->base);
428 +}
429 +static DEVICE_ATTR(base, 0444, chip_base_show, NULL);
430 +
431 +static ssize_t chip_label_show(struct device *dev,
432 + struct device_attribute *attr, char *buf)
433 +{
434 + const struct gpio_chip *chip = dev_get_drvdata(dev);
435 +
436 + return sprintf(buf, "%s\n", chip->label ? : "");
437 +}
438 +static DEVICE_ATTR(label, 0444, chip_label_show, NULL);
439 +
440 +static ssize_t chip_ngpio_show(struct device *dev,
441 + struct device_attribute *attr, char *buf)
442 +{
443 + const struct gpio_chip *chip = dev_get_drvdata(dev);
444 +
445 + return sprintf(buf, "%u\n", chip->ngpio);
446 +}
447 +static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL);
448 +
449 +static const struct attribute *gpiochip_attrs[] = {
450 + &dev_attr_base.attr,
451 + &dev_attr_label.attr,
452 + &dev_attr_ngpio.attr,
453 + NULL,
454 +};
455 +
456 +static const struct attribute_group gpiochip_attr_group = {
457 + .attrs = (struct attribute **) gpiochip_attrs,
458 +};
459 +
460 +/*
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
465 + */
466 +static ssize_t export_store(struct class *class, const char *buf, size_t len)
467 +{
468 + long gpio;
469 + int status;
470 +
471 + status = strict_strtol(buf, 0, &gpio);
472 + if (status < 0)
473 + goto done;
474 +
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.
478 + */
479 +
480 + status = gpio_request(gpio, "sysfs");
481 + if (status < 0)
482 + goto done;
483 +
484 + status = gpio_export(gpio, true);
485 + if (status < 0)
486 + gpio_free(gpio);
487 + else
488 + set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags);
489 +
490 +done:
491 + if (status)
492 + pr_debug("%s: status %d\n", __func__, status);
493 + return status ? : len;
494 +}
495 +
496 +static ssize_t unexport_store(struct class *class, const char *buf, size_t len)
497 +{
498 + long gpio;
499 + int status;
500 +
501 + status = strict_strtol(buf, 0, &gpio);
502 + if (status < 0)
503 + goto done;
504 +
505 + status = -EINVAL;
506 +
507 + /* reject bogus commands (gpio_unexport ignores them) */
508 + if (!gpio_is_valid(gpio))
509 + goto done;
510 +
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.
514 + */
515 + if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) {
516 + status = 0;
517 + gpio_free(gpio);
518 + }
519 +done:
520 + if (status)
521 + pr_debug("%s: status %d\n", __func__, status);
522 + return status ? : len;
523 +}
524 +
525 +static struct class_attribute gpio_class_attrs[] = {
526 + __ATTR(export, 0200, NULL, export_store),
527 + __ATTR(unexport, 0200, NULL, unexport_store),
528 + __ATTR_NULL,
529 +};
530 +
531 +static struct class gpio_class = {
532 + .name = "gpio",
533 + .owner = THIS_MODULE,
534 +
535 + .class_attrs = gpio_class_attrs,
536 +};
537 +
538 +
539 +/**
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
544 + *
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.
551 + *
552 + * Returns zero on success, else an error.
553 + */
554 +int gpio_export(unsigned gpio, bool direction_may_change)
555 +{
556 + unsigned long flags;
557 + struct gpio_desc *desc;
558 + int status = -EINVAL;
559 +
560 + /* can't export until sysfs is available ... */
561 + if (!gpio_class.subsys.kobj.ktype) {
562 + pr_debug("%s: called too early!\n", __func__);
563 + return -ENOENT;
564 + }
565 +
566 + if (!gpio_is_valid(gpio))
567 + goto done;
568 +
569 + mutex_lock(&sysfs_lock);
570 +
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)) {
575 + status = 0;
576 + if (!desc->chip->direction_input
577 + || !desc->chip->direction_output)
578 + direction_may_change = false;
579 + }
580 + spin_unlock_irqrestore(&gpio_lock, flags);
581 +
582 + if (status == 0) {
583 + struct device *dev;
584 +
585 + dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
586 + "gpio%d", gpio);
587 + if (dev) {
588 + dev_set_drvdata(dev, desc);
589 + if (direction_may_change)
590 + status = sysfs_create_group(&dev->kobj,
591 + &gpio_attr_group);
592 + else
593 + status = device_create_file(dev,
594 + &dev_attr_value);
595 + if (status != 0)
596 + device_unregister(dev);
597 + } else
598 + status = -ENODEV;
599 + if (status == 0)
600 + set_bit(FLAG_EXPORT, &desc->flags);
601 + }
602 +
603 + mutex_unlock(&sysfs_lock);
604 +
605 +done:
606 + if (status)
607 + pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
608 +
609 + return status;
610 +}
611 +EXPORT_SYMBOL_GPL(gpio_export);
612 +
613 +static int match_export(struct device *dev, void *data)
614 +{
615 + return dev_get_drvdata(dev) == data;
616 +}
617 +
618 +/**
619 + * gpio_unexport - reverse effect of gpio_export()
620 + * @gpio: gpio to make unavailable
621 + *
622 + * This is implicit on gpio_free().
623 + */
624 +void gpio_unexport(unsigned gpio)
625 +{
626 + struct gpio_desc *desc;
627 + int status = -EINVAL;
628 +
629 + if (!gpio_is_valid(gpio))
630 + goto done;
631 +
632 + mutex_lock(&sysfs_lock);
633 +
634 + desc = &gpio_desc[gpio];
635 + if (test_bit(FLAG_EXPORT, &desc->flags)) {
636 + struct device *dev = NULL;
637 +
638 + dev = class_find_device(&gpio_class, desc, match_export);
639 + if (dev) {
640 + dev_set_drvdata(dev, NULL);
641 + clear_bit(FLAG_EXPORT, &desc->flags);
642 + put_device(dev);
643 + device_unregister(dev);
644 + status = 0;
645 + } else
646 + status = -ENODEV;
647 + }
648 +
649 + mutex_unlock(&sysfs_lock);
650 +done:
651 + if (status)
652 + pr_debug("%s: gpio%d status %d\n", __func__, gpio, status);
653 +}
654 +EXPORT_SYMBOL_GPL(gpio_unexport);
655 +
656 +static int gpiochip_export(struct gpio_chip *chip)
657 +{
658 + int status;
659 + struct device *dev;
660 +
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.
665 + */
666 + if (!gpio_class.subsys.kobj.ktype)
667 + return 0;
668 +
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);
673 + if (dev) {
674 + dev_set_drvdata(dev, chip);
675 + status = sysfs_create_group(&dev->kobj,
676 + &gpiochip_attr_group);
677 + } else
678 + status = -ENODEV;
679 + chip->exported = (status == 0);
680 + mutex_unlock(&sysfs_lock);
681 +
682 + if (status) {
683 + unsigned long flags;
684 + unsigned gpio;
685 +
686 + spin_lock_irqsave(&gpio_lock, flags);
687 + gpio = chip->base;
688 + while (gpio_desc[gpio].chip == chip)
689 + gpio_desc[gpio++].chip = NULL;
690 + spin_unlock_irqrestore(&gpio_lock, flags);
691 +
692 + pr_debug("%s: chip %s status %d\n", __func__,
693 + chip->label, status);
694 + }
695 +
696 + return status;
697 +}
698 +
699 +static void gpiochip_unexport(struct gpio_chip *chip)
700 +{
701 + int status;
702 + struct device *dev;
703 +
704 + mutex_lock(&sysfs_lock);
705 + dev = class_find_device(&gpio_class, chip, match_export);
706 + if (dev) {
707 + dev_set_drvdata(dev, NULL);
708 + put_device(dev);
709 + device_unregister(dev);
710 + chip->exported = 0;
711 + status = 0;
712 + } else
713 + status = -ENODEV;
714 + mutex_unlock(&sysfs_lock);
715 +
716 + if (status)
717 + pr_debug("%s: chip %s status %d\n", __func__,
718 + chip->label, status);
719 +}
720 +
721 +static int __init gpiolib_sysfs_init(void)
722 +{
723 + int status;
724 + unsigned long flags;
725 + unsigned gpio;
726 +
727 + status = class_register(&gpio_class);
728 + if (status < 0)
729 + return status;
730 +
731 + /* Scan and register the gpio_chips which registered very
732 + * early (e.g. before the class_register above was called).
733 + *
734 + * We run before arch_initcall() so chip->dev nodes can have
735 + * registered, and so arch_initcall() can always gpio_export().
736 + */
737 + spin_lock_irqsave(&gpio_lock, flags);
738 + for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) {
739 + struct gpio_chip *chip;
740 +
741 + chip = gpio_desc[gpio].chip;
742 + if (!chip || chip->exported)
743 + continue;
744 +
745 + spin_unlock_irqrestore(&gpio_lock, flags);
746 + status = gpiochip_export(chip);
747 + spin_lock_irqsave(&gpio_lock, flags);
748 + }
749 + spin_unlock_irqrestore(&gpio_lock, flags);
750 +
751 +
752 + return status;
753 +}
754 +postcore_initcall(gpiolib_sysfs_init);
755 +
756 +#else
757 +static inline int gpiochip_export(struct gpio_chip *chip)
758 +{
759 + return 0;
760 +}
761 +
762 +static inline void gpiochip_unexport(struct gpio_chip *chip)
763 +{
764 +}
765 +
766 +#endif /* CONFIG_GPIO_SYSFS */
767 +
768 /**
769 * gpiochip_add() - register a gpio_chip
770 * @chip: the chip to register, with chip->base initialized
771 @@ -160,6 +645,11 @@ err:
772 * because the chip->base is invalid or already associated with a
773 * different chip. Otherwise it returns zero as a success code.
774 *
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.
779 + *
780 * If chip->base is negative, this requests dynamic assignment of
781 * a range of valid GPIOs.
782 */
783 @@ -182,7 +672,7 @@ int gpiochip_add(struct gpio_chip *chip)
784 base = gpiochip_find_base(chip->ngpio);
785 if (base < 0) {
786 status = base;
787 - goto fail_unlock;
788 + goto unlock;
789 }
790 chip->base = base;
791 }
792 @@ -197,12 +687,23 @@ int gpiochip_add(struct gpio_chip *chip)
793 if (status == 0) {
794 for (id = base; id < base + chip->ngpio; id++) {
795 gpio_desc[id].chip = chip;
796 - gpio_desc[id].flags = 0;
797 +
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.
803 + */
804 + gpio_desc[id].flags = !chip->direction_input
805 + ? (1 << FLAG_IS_OUT)
806 + : 0;
807 }
808 }
809
810 -fail_unlock:
811 +unlock:
812 spin_unlock_irqrestore(&gpio_lock, flags);
813 + if (status == 0)
814 + status = gpiochip_export(chip);
815 fail:
816 /* failures here can mean systems won't boot... */
817 if (status)
818 @@ -239,6 +740,10 @@ int gpiochip_remove(struct gpio_chip *ch
819 }
820
821 spin_unlock_irqrestore(&gpio_lock, flags);
822 +
823 + if (status == 0)
824 + gpiochip_unexport(chip);
825 +
826 return status;
827 }
828 EXPORT_SYMBOL_GPL(gpiochip_remove);
829 @@ -296,6 +801,8 @@ void gpio_free(unsigned gpio)
830 return;
831 }
832
833 + gpio_unexport(gpio);
834 +
835 spin_lock_irqsave(&gpio_lock, flags);
836
837 desc = &gpio_desc[gpio];
838 @@ -534,10 +1041,6 @@ EXPORT_SYMBOL_GPL(gpio_set_value_canslee
839
840 #ifdef CONFIG_DEBUG_FS
841
842 -#include <linux/debugfs.h>
843 -#include <linux/seq_file.h>
844 -
845 -
846 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
847 {
848 unsigned i;
849 @@ -614,17 +1117,28 @@ static int gpiolib_show(struct seq_file
850 /* REVISIT this isn't locked against gpio_chip removal ... */
851
852 for (gpio = 0; gpio_is_valid(gpio); gpio++) {
853 + struct device *dev;
854 +
855 if (chip == gpio_desc[gpio].chip)
856 continue;
857 chip = gpio_desc[gpio].chip;
858 if (!chip)
859 continue;
860
861 - seq_printf(s, "%sGPIOs %d-%d, %s%s:\n",
862 + seq_printf(s, "%sGPIOs %d-%d",
863 started ? "\n" : "",
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);
868 + dev = chip->dev;
869 + if (dev)
870 + seq_printf(s, ", %s/%s",
871 + dev->bus ? dev->bus->name : "no-bus",
872 + dev->bus_id);
873 + if (chip->label)
874 + seq_printf(s, ", %s", chip->label);
875 + if (chip->can_sleep)
876 + seq_printf(s, ", can sleep");
877 + seq_printf(s, ":\n");
878 +
879 started = 1;
880 if (chip->dbg_show)
881 chip->dbg_show(s, chip);
882 --- a/drivers/gpio/mcp23s08.c
883 +++ b/drivers/gpio/mcp23s08.c
884 @@ -239,6 +239,7 @@ static int mcp23s08_probe(struct spi_dev
885 mcp->chip.base = pdata->base;
886 mcp->chip.ngpio = 8;
887 mcp->chip.can_sleep = 1;
888 + mcp->chip.dev = &spi->dev;
889 mcp->chip.owner = THIS_MODULE;
890
891 spi_set_drvdata(spi, mcp);
892 --- a/drivers/gpio/pca953x.c
893 +++ b/drivers/gpio/pca953x.c
894 @@ -188,6 +188,7 @@ static void pca953x_setup_gpio(struct pc
895 gc->base = chip->gpio_start;
896 gc->ngpio = gpios;
897 gc->label = chip->client->name;
898 + gc->dev = &chip->client->dev;
899 gc->owner = THIS_MODULE;
900 }
901
902 --- a/drivers/gpio/pcf857x.c
903 +++ b/drivers/gpio/pcf857x.c
904 @@ -175,6 +175,7 @@ static int pcf857x_probe(struct i2c_clie
905
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;
910
911 /* NOTE: the OnSemi jlc1562b is also largely compatible with
912 --- a/drivers/i2c/chips/tps65010.c
913 +++ b/drivers/i2c/chips/tps65010.c
914 @@ -636,6 +636,8 @@ static int tps65010_probe(struct i2c_cli
915 tps->outmask = board->outmask;
916
917 tps->chip.label = client->name;
918 + tps->chip.dev = &client->dev;
919 + tps->chip.owner = THIS_MODULE;
920
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
925 @@ -318,6 +318,8 @@ static int __init egpio_probe(struct pla
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
936 @@ -32,6 +32,8 @@ struct module;
937 /**
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
945 @@ -59,6 +61,7 @@ struct module;
946 */
947 struct gpio_chip {
948 char *label;
949 + struct device *dev;
950 struct module *owner;
951
952 int (*direction_input)(struct gpio_chip *chip,
953 @@ -74,6 +77,7 @@ struct gpio_chip {
954 int base;
955 u16 ngpio;
956 unsigned can_sleep:1;
957 + unsigned exported:1;
958 };
959
960 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
961 @@ -108,7 +112,18 @@ extern void __gpio_set_value(unsigned gp
962 extern int __gpio_cansleep(unsigned gpio);
963
964
965 -#else
966 +#ifdef CONFIG_GPIO_SYSFS
967 +
968 +/*
969 + * A sysfs interface can be exported by individual drivers if they want,
970 + * but more typically is configured entirely from userspace.
971 + */
972 +extern int gpio_export(unsigned gpio, bool direction_may_change);
973 +extern void gpio_unexport(unsigned gpio);
974 +
975 +#endif /* CONFIG_GPIO_SYSFS */
976 +
977 +#else /* !CONFIG_HAVE_GPIO_LIB */
978
979 static inline int gpio_is_valid(int number)
980 {
981 @@ -137,6 +152,22 @@ static inline void gpio_set_value_cansle
982 gpio_set_value(gpio, value);
983 }
984
985 -#endif
986 +#endif /* !CONFIG_HAVE_GPIO_LIB */
987 +
988 +#ifndef CONFIG_GPIO_SYSFS
989 +
990 +#include <asm/errno.h>
991 +
992 +/* sysfs support is only available with gpiolib, where it's optional */
993 +
994 +static inline int gpio_export(unsigned gpio, bool direction_may_change)
995 +{
996 + return -ENOSYS;
997 +}
998 +
999 +static inline void gpio_unexport(unsigned gpio)
1000 +{
1001 +}
1002 +#endif /* CONFIG_GPIO_SYSFS */
1003
1004 #endif /* _ASM_GENERIC_GPIO_H */
1005 --- a/include/linux/gpio.h
1006 +++ b/include/linux/gpio.h
1007 @@ -79,6 +79,19 @@ static inline void gpio_set_value_cansle
1008 WARN_ON(1);
1009 }
1010
1011 +static inline int gpio_export(unsigned gpio, bool direction_may_change)
1012 +{
1013 + /* GPIO can never have been requested or set as {in,out}put */
1014 + WARN_ON(1);
1015 + return -EINVAL;
1016 +}
1017 +
1018 +static inline void gpio_unexport(unsigned gpio)
1019 +{
1020 + /* GPIO can never have been exported */
1021 + WARN_ON(1);
1022 +}
1023 +
1024 static inline int gpio_to_irq(unsigned gpio)
1025 {
1026 /* GPIO can never have been requested or set as input */
This page took 0.085255 seconds and 5 git commands to generate.