aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/gpio.txt
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/gpio.txt')
-rw-r--r--Documentation/gpio.txt135
1 files changed, 129 insertions, 6 deletions
diff --git a/Documentation/gpio.txt b/Documentation/gpio.txt
index c35ca9e40d4c..18022e249c53 100644
--- a/Documentation/gpio.txt
+++ b/Documentation/gpio.txt
@@ -347,15 +347,12 @@ necessarily be nonportable.
347Dynamic definition of GPIOs is not currently standard; for example, as 347Dynamic definition of GPIOs is not currently standard; for example, as
348a side effect of configuring an add-on board with some GPIO expanders. 348a side effect of configuring an add-on board with some GPIO expanders.
349 349
350These calls are purely for kernel space, but a userspace API could be built
351on top of them.
352
353 350
354GPIO implementor's framework (OPTIONAL) 351GPIO implementor's framework (OPTIONAL)
355======================================= 352=======================================
356As noted earlier, there is an optional implementation framework making it 353As noted earlier, there is an optional implementation framework making it
357easier for platforms to support different kinds of GPIO controller using 354easier for platforms to support different kinds of GPIO controller using
358the same programming interface. 355the same programming interface. This framework is called "gpiolib".
359 356
360As a debugging aid, if debugfs is available a /sys/kernel/debug/gpio file 357As a debugging aid, if debugfs is available a /sys/kernel/debug/gpio file
361will be found there. That will list all the controllers registered through 358will be found there. That will list all the controllers registered through
@@ -392,11 +389,21 @@ either NULL or the label associated with that GPIO when it was requested.
392 389
393Platform Support 390Platform Support
394---------------- 391----------------
395To support this framework, a platform's Kconfig will "select HAVE_GPIO_LIB" 392To support this framework, a platform's Kconfig will "select" either
393ARCH_REQUIRE_GPIOLIB or ARCH_WANT_OPTIONAL_GPIOLIB
396and arrange that its <asm/gpio.h> includes <asm-generic/gpio.h> and defines 394and arrange that its <asm/gpio.h> includes <asm-generic/gpio.h> and defines
397three functions: gpio_get_value(), gpio_set_value(), and gpio_cansleep(). 395three functions: gpio_get_value(), gpio_set_value(), and gpio_cansleep().
398They may also want to provide a custom value for ARCH_NR_GPIOS. 396They may also want to provide a custom value for ARCH_NR_GPIOS.
399 397
398ARCH_REQUIRE_GPIOLIB means that the gpio-lib code will always get compiled
399into the kernel on that architecture.
400
401ARCH_WANT_OPTIONAL_GPIOLIB means the gpio-lib code defaults to off and the user
402can enable it and build it into the kernel optionally.
403
404If neither of these options are selected, the platform does not support
405GPIOs through GPIO-lib and the code cannot be enabled by the user.
406
400Trivial implementations of those functions can directly use framework 407Trivial implementations of those functions can directly use framework
401code, which always dispatches through the gpio_chip: 408code, which always dispatches through the gpio_chip:
402 409
@@ -439,4 +446,120 @@ becomes available. That may mean the device should not be registered until
439calls for that GPIO can work. One way to address such dependencies is for 446calls for that GPIO can work. One way to address such dependencies is for
440such gpio_chip controllers to provide setup() and teardown() callbacks to 447such gpio_chip controllers to provide setup() and teardown() callbacks to
441board specific code; those board specific callbacks would register devices 448board specific code; those board specific callbacks would register devices
442once all the necessary resources are available. 449once all the necessary resources are available, and remove them later when
450the GPIO controller device becomes unavailable.
451
452
453Sysfs Interface for Userspace (OPTIONAL)
454========================================
455Platforms which use the "gpiolib" implementors framework may choose to
456configure a sysfs user interface to GPIOs. This is different from the
457debugfs interface, since it provides control over GPIO direction and
458value instead of just showing a gpio state summary. Plus, it could be
459present on production systems without debugging support.
460
461Given approprate hardware documentation for the system, userspace could
462know for example that GPIO #23 controls the write protect line used to
463protect boot loader segments in flash memory. System upgrade procedures
464may need to temporarily remove that protection, first importing a GPIO,
465then changing its output state, then updating the code before re-enabling
466the write protection. In normal use, GPIO #23 would never be touched,
467and the kernel would have no need to know about it.
468
469Again depending on appropriate hardware documentation, on some systems
470userspace GPIO can be used to determine system configuration data that
471standard kernels won't know about. And for some tasks, simple userspace
472GPIO drivers could be all that the system really needs.
473
474Note that standard kernel drivers exist for common "LEDs and Buttons"
475GPIO tasks: "leds-gpio" and "gpio_keys", respectively. Use those
476instead of talking directly to the GPIOs; they integrate with kernel
477frameworks better than your userspace code could.
478
479
480Paths in Sysfs
481--------------
482There are three kinds of entry in /sys/class/gpio:
483
484 - Control interfaces used to get userspace control over GPIOs;
485
486 - GPIOs themselves; and
487
488 - GPIO controllers ("gpio_chip" instances).
489
490That's in addition to standard files including the "device" symlink.
491
492The control interfaces are write-only:
493
494 /sys/class/gpio/
495
496 "export" ... Userspace may ask the kernel to export control of
497 a GPIO to userspace by writing its number to this file.
498
499 Example: "echo 19 > export" will create a "gpio19" node
500 for GPIO #19, if that's not requested by kernel code.
501
502 "unexport" ... Reverses the effect of exporting to userspace.
503
504 Example: "echo 19 > unexport" will remove a "gpio19"
505 node exported using the "export" file.
506
507GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
508and have the following read/write attributes:
509
510 /sys/class/gpio/gpioN/
511
512 "direction" ... reads as either "in" or "out". This value may
513 normally be written. Writing as "out" defaults to
514 initializing the value as low. To ensure glitch free
515 operation, values "low" and "high" may be written to
516 configure the GPIO as an output with that initial value.
517
518 Note that this attribute *will not exist* if the kernel
519 doesn't support changing the direction of a GPIO, or
520 it was exported by kernel code that didn't explicitly
521 allow userspace to reconfigure this GPIO's direction.
522
523 "value" ... reads as either 0 (low) or 1 (high). If the GPIO
524 is configured as an output, this value may be written;
525 any nonzero value is treated as high.
526
527GPIO controllers have paths like /sys/class/gpio/chipchip42/ (for the
528controller implementing GPIOs starting at #42) and have the following
529read-only attributes:
530
531 /sys/class/gpio/gpiochipN/
532
533 "base" ... same as N, the first GPIO managed by this chip
534
535 "label" ... provided for diagnostics (not always unique)
536
537 "ngpio" ... how many GPIOs this manges (N to N + ngpio - 1)
538
539Board documentation should in most cases cover what GPIOs are used for
540what purposes. However, those numbers are not always stable; GPIOs on
541a daughtercard might be different depending on the base board being used,
542or other cards in the stack. In such cases, you may need to use the
543gpiochip nodes (possibly in conjunction with schematics) to determine
544the correct GPIO number to use for a given signal.
545
546
547Exporting from Kernel code
548--------------------------
549Kernel code can explicitly manage exports of GPIOs which have already been
550requested using gpio_request():
551
552 /* export the GPIO to userspace */
553 int gpio_export(unsigned gpio, bool direction_may_change);
554
555 /* reverse gpio_export() */
556 void gpio_unexport();
557
558After a kernel driver requests a GPIO, it may only be made available in
559the sysfs interface by gpio_export(). The driver can control whether the
560signal direction may change. This helps drivers prevent userspace code
561from accidentally clobbering important system state.
562
563This explicit exporting can help with debugging (by making some kinds
564of experiments easier), or can provide an always-there interface that's
565suitable for documenting as part of a board support package.