diff options
Diffstat (limited to 'Documentation/gpio.txt')
-rw-r--r-- | Documentation/gpio.txt | 135 |
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. | |||
347 | Dynamic definition of GPIOs is not currently standard; for example, as | 347 | Dynamic definition of GPIOs is not currently standard; for example, as |
348 | a side effect of configuring an add-on board with some GPIO expanders. | 348 | a side effect of configuring an add-on board with some GPIO expanders. |
349 | 349 | ||
350 | These calls are purely for kernel space, but a userspace API could be built | ||
351 | on top of them. | ||
352 | |||
353 | 350 | ||
354 | GPIO implementor's framework (OPTIONAL) | 351 | GPIO implementor's framework (OPTIONAL) |
355 | ======================================= | 352 | ======================================= |
356 | As noted earlier, there is an optional implementation framework making it | 353 | As noted earlier, there is an optional implementation framework making it |
357 | easier for platforms to support different kinds of GPIO controller using | 354 | easier for platforms to support different kinds of GPIO controller using |
358 | the same programming interface. | 355 | the same programming interface. This framework is called "gpiolib". |
359 | 356 | ||
360 | As a debugging aid, if debugfs is available a /sys/kernel/debug/gpio file | 357 | As a debugging aid, if debugfs is available a /sys/kernel/debug/gpio file |
361 | will be found there. That will list all the controllers registered through | 358 | will 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 | ||
393 | Platform Support | 390 | Platform Support |
394 | ---------------- | 391 | ---------------- |
395 | To support this framework, a platform's Kconfig will "select HAVE_GPIO_LIB" | 392 | To support this framework, a platform's Kconfig will "select" either |
393 | ARCH_REQUIRE_GPIOLIB or ARCH_WANT_OPTIONAL_GPIOLIB | ||
396 | and arrange that its <asm/gpio.h> includes <asm-generic/gpio.h> and defines | 394 | and arrange that its <asm/gpio.h> includes <asm-generic/gpio.h> and defines |
397 | three functions: gpio_get_value(), gpio_set_value(), and gpio_cansleep(). | 395 | three functions: gpio_get_value(), gpio_set_value(), and gpio_cansleep(). |
398 | They may also want to provide a custom value for ARCH_NR_GPIOS. | 396 | They may also want to provide a custom value for ARCH_NR_GPIOS. |
399 | 397 | ||
398 | ARCH_REQUIRE_GPIOLIB means that the gpio-lib code will always get compiled | ||
399 | into the kernel on that architecture. | ||
400 | |||
401 | ARCH_WANT_OPTIONAL_GPIOLIB means the gpio-lib code defaults to off and the user | ||
402 | can enable it and build it into the kernel optionally. | ||
403 | |||
404 | If neither of these options are selected, the platform does not support | ||
405 | GPIOs through GPIO-lib and the code cannot be enabled by the user. | ||
406 | |||
400 | Trivial implementations of those functions can directly use framework | 407 | Trivial implementations of those functions can directly use framework |
401 | code, which always dispatches through the gpio_chip: | 408 | code, 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 | |||
439 | calls for that GPIO can work. One way to address such dependencies is for | 446 | calls for that GPIO can work. One way to address such dependencies is for |
440 | such gpio_chip controllers to provide setup() and teardown() callbacks to | 447 | such gpio_chip controllers to provide setup() and teardown() callbacks to |
441 | board specific code; those board specific callbacks would register devices | 448 | board specific code; those board specific callbacks would register devices |
442 | once all the necessary resources are available. | 449 | once all the necessary resources are available, and remove them later when |
450 | the GPIO controller device becomes unavailable. | ||
451 | |||
452 | |||
453 | Sysfs Interface for Userspace (OPTIONAL) | ||
454 | ======================================== | ||
455 | Platforms which use the "gpiolib" implementors framework may choose to | ||
456 | configure a sysfs user interface to GPIOs. This is different from the | ||
457 | debugfs interface, since it provides control over GPIO direction and | ||
458 | value instead of just showing a gpio state summary. Plus, it could be | ||
459 | present on production systems without debugging support. | ||
460 | |||
461 | Given approprate hardware documentation for the system, userspace could | ||
462 | know for example that GPIO #23 controls the write protect line used to | ||
463 | protect boot loader segments in flash memory. System upgrade procedures | ||
464 | may need to temporarily remove that protection, first importing a GPIO, | ||
465 | then changing its output state, then updating the code before re-enabling | ||
466 | the write protection. In normal use, GPIO #23 would never be touched, | ||
467 | and the kernel would have no need to know about it. | ||
468 | |||
469 | Again depending on appropriate hardware documentation, on some systems | ||
470 | userspace GPIO can be used to determine system configuration data that | ||
471 | standard kernels won't know about. And for some tasks, simple userspace | ||
472 | GPIO drivers could be all that the system really needs. | ||
473 | |||
474 | Note that standard kernel drivers exist for common "LEDs and Buttons" | ||
475 | GPIO tasks: "leds-gpio" and "gpio_keys", respectively. Use those | ||
476 | instead of talking directly to the GPIOs; they integrate with kernel | ||
477 | frameworks better than your userspace code could. | ||
478 | |||
479 | |||
480 | Paths in Sysfs | ||
481 | -------------- | ||
482 | There 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 | |||
490 | That's in addition to standard files including the "device" symlink. | ||
491 | |||
492 | The 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 | |||
507 | GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) | ||
508 | and 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 | |||
527 | GPIO controllers have paths like /sys/class/gpio/chipchip42/ (for the | ||
528 | controller implementing GPIOs starting at #42) and have the following | ||
529 | read-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 | |||
539 | Board documentation should in most cases cover what GPIOs are used for | ||
540 | what purposes. However, those numbers are not always stable; GPIOs on | ||
541 | a daughtercard might be different depending on the base board being used, | ||
542 | or other cards in the stack. In such cases, you may need to use the | ||
543 | gpiochip nodes (possibly in conjunction with schematics) to determine | ||
544 | the correct GPIO number to use for a given signal. | ||
545 | |||
546 | |||
547 | Exporting from Kernel code | ||
548 | -------------------------- | ||
549 | Kernel code can explicitly manage exports of GPIOs which have already been | ||
550 | requested 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 | |||
558 | After a kernel driver requests a GPIO, it may only be made available in | ||
559 | the sysfs interface by gpio_export(). The driver can control whether the | ||
560 | signal direction may change. This helps drivers prevent userspace code | ||
561 | from accidentally clobbering important system state. | ||
562 | |||
563 | This explicit exporting can help with debugging (by making some kinds | ||
564 | of experiments easier), or can provide an always-there interface that's | ||
565 | suitable for documenting as part of a board support package. | ||