intro.rst 5.3 KB

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