gpio.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. GPIO Interfaces
  2. ===============
  3. The documents in this directory give detailed instructions on how to access
  4. GPIOs in drivers, and how to write a driver for a device that provides GPIOs
  5. itself.
  6. Due to the history of GPIO interfaces in the kernel, there are two different
  7. ways to obtain and use GPIOs:
  8. - The descriptor-based interface is the preferred way to manipulate GPIOs,
  9. and is described by all the files in this directory excepted gpio-legacy.txt.
  10. - The legacy integer-based interface which is considered deprecated (but still
  11. usable for compatibility reasons) is documented in gpio-legacy.txt.
  12. The remainder of this document applies to the new descriptor-based interface.
  13. gpio-legacy.txt contains the same information applied to the legacy
  14. integer-based interface.
  15. What is a GPIO?
  16. ===============
  17. A "General Purpose Input/Output" (GPIO) is a flexible software-controlled
  18. digital signal. They are provided from many kinds of chip, and are familiar
  19. to Linux developers working with embedded and custom hardware. Each GPIO
  20. represents a bit connected to a particular pin, or "ball" on Ball Grid Array
  21. (BGA) packages. Board schematics show which external hardware connects to
  22. which GPIOs. Drivers can be written generically, so that board setup code
  23. passes such pin configuration data to drivers.
  24. System-on-Chip (SOC) processors heavily rely on GPIOs. In some cases, every
  25. non-dedicated pin can be configured as a GPIO; and most chips have at least
  26. several dozen of them. Programmable logic devices (like FPGAs) can easily
  27. provide GPIOs; multifunction chips like power managers, and audio codecs
  28. often have a few such pins to help with pin scarcity on SOCs; and there are
  29. also "GPIO Expander" chips that connect using the I2C or SPI serial buses.
  30. Most PC southbridges have a few dozen GPIO-capable pins (with only the BIOS
  31. firmware knowing how they're used).
  32. The exact capabilities of GPIOs vary between systems. Common options:
  33. - Output values are writable (high=1, low=0). Some chips also have
  34. options about how that value is driven, so that for example only one
  35. value might be driven, supporting "wire-OR" and similar schemes for the
  36. other value (notably, "open drain" signaling).
  37. - Input values are likewise readable (1, 0). Some chips support readback
  38. of pins configured as "output", which is very useful in such "wire-OR"
  39. cases (to support bidirectional signaling). GPIO controllers may have
  40. input de-glitch/debounce logic, sometimes with software controls.
  41. - Inputs can often be used as IRQ signals, often edge triggered but
  42. sometimes level triggered. Such IRQs may be configurable as system
  43. wakeup events, to wake the system from a low power state.
  44. - Usually a GPIO will be configurable as either input or output, as needed
  45. by different product boards; single direction ones exist too.
  46. - Most GPIOs can be accessed while holding spinlocks, but those accessed
  47. through a serial bus normally can't. Some systems support both types.
  48. On a given board each GPIO is used for one specific purpose like monitoring
  49. MMC/SD card insertion/removal, detecting card write-protect status, driving
  50. a LED, configuring a transceiver, bit-banging a serial bus, poking a hardware
  51. watchdog, sensing a switch, and so on.
  52. Common GPIO Properties
  53. ======================
  54. These properties are met through all the other documents of the GPIO interface
  55. and it is useful to understand them, especially if you need to define GPIO
  56. mappings.
  57. Active-High and Active-Low
  58. --------------------------
  59. It is natural to assume that a GPIO is "active" when its output signal is 1
  60. ("high"), and inactive when it is 0 ("low"). However in practice the signal of a
  61. GPIO may be inverted before is reaches its destination, or a device could decide
  62. to have different conventions about what "active" means. Such decisions should
  63. be transparent to device drivers, therefore it is possible to define a GPIO as
  64. being either active-high ("1" means "active", the default) or active-low ("0"
  65. means "active") so that drivers only need to worry about the logical signal and
  66. not about what happens at the line level.
  67. Open Drain and Open Source
  68. --------------------------
  69. Sometimes shared signals need to use "open drain" (where only the low signal
  70. level is actually driven), or "open source" (where only the high signal level is
  71. driven) signaling. That term applies to CMOS transistors; "open collector" is
  72. used for TTL. A pullup or pulldown resistor causes the high or low signal level.
  73. This is sometimes called a "wire-AND"; or more practically, from the negative
  74. logic (low=true) perspective this is a "wire-OR".
  75. One common example of an open drain signal is a shared active-low IRQ line.
  76. Also, bidirectional data bus signals sometimes use open drain signals.
  77. Some GPIO controllers directly support open drain and open source outputs; many
  78. don't. When you need open drain signaling but your hardware doesn't directly
  79. support it, there's a common idiom you can use to emulate it with any GPIO pin
  80. that can be used as either an input or an output:
  81. LOW: gpiod_direction_output(gpio, 0) ... this drives the signal and overrides
  82. the pullup.
  83. HIGH: gpiod_direction_input(gpio) ... this turns off the output, so the pullup
  84. (or some other device) controls the signal.
  85. The same logic can be applied to emulate open source signaling, by driving the
  86. high signal and configuring the GPIO as input for low. This open drain/open
  87. source emulation can be handled transparently by the GPIO framework.
  88. If you are "driving" the signal high but gpiod_get_value(gpio) reports a low
  89. value (after the appropriate rise time passes), you know some other component is
  90. driving the shared signal low. That's not necessarily an error. As one common
  91. example, that's how I2C clocks are stretched: a slave that needs a slower clock
  92. delays the rising edge of SCK, and the I2C master adjusts its signaling rate
  93. accordingly.