0001 ============
0002 Introduction
0003 ============
0004
0005
0006 GPIO Interfaces
0007 ===============
0008
0009 The documents in this directory give detailed instructions on how to access
0010 GPIOs in drivers, and how to write a driver for a device that provides GPIOs
0011 itself.
0012
0013 Due to the history of GPIO interfaces in the kernel, there are two different
0014 ways to obtain and use GPIOs:
0015
0016 - The descriptor-based interface is the preferred way to manipulate GPIOs,
0017 and is described by all the files in this directory excepted legacy.rst.
0018 - The legacy integer-based interface which is considered deprecated (but still
0019 usable for compatibility reasons) is documented in legacy.rst.
0020
0021 The remainder of this document applies to the new descriptor-based interface.
0022 legacy.rst contains the same information applied to the legacy
0023 integer-based interface.
0024
0025
0026 What is a GPIO?
0027 ===============
0028
0029 A "General Purpose Input/Output" (GPIO) is a flexible software-controlled
0030 digital signal. They are provided from many kinds of chips, and are familiar
0031 to Linux developers working with embedded and custom hardware. Each GPIO
0032 represents a bit connected to a particular pin, or "ball" on Ball Grid Array
0033 (BGA) packages. Board schematics show which external hardware connects to
0034 which GPIOs. Drivers can be written generically, so that board setup code
0035 passes such pin configuration data to drivers.
0036
0037 System-on-Chip (SOC) processors heavily rely on GPIOs. In some cases, every
0038 non-dedicated pin can be configured as a GPIO; and most chips have at least
0039 several dozen of them. Programmable logic devices (like FPGAs) can easily
0040 provide GPIOs; multifunction chips like power managers, and audio codecs
0041 often have a few such pins to help with pin scarcity on SOCs; and there are
0042 also "GPIO Expander" chips that connect using the I2C or SPI serial buses.
0043 Most PC southbridges have a few dozen GPIO-capable pins (with only the BIOS
0044 firmware knowing how they're used).
0045
0046 The exact capabilities of GPIOs vary between systems. Common options:
0047
0048 - Output values are writable (high=1, low=0). Some chips also have
0049 options about how that value is driven, so that for example only one
0050 value might be driven, supporting "wire-OR" and similar schemes for the
0051 other value (notably, "open drain" signaling).
0052
0053 - Input values are likewise readable (1, 0). Some chips support readback
0054 of pins configured as "output", which is very useful in such "wire-OR"
0055 cases (to support bidirectional signaling). GPIO controllers may have
0056 input de-glitch/debounce logic, sometimes with software controls.
0057
0058 - Inputs can often be used as IRQ signals, often edge triggered but
0059 sometimes level triggered. Such IRQs may be configurable as system
0060 wakeup events, to wake the system from a low power state.
0061
0062 - Usually a GPIO will be configurable as either input or output, as needed
0063 by different product boards; single direction ones exist too.
0064
0065 - Most GPIOs can be accessed while holding spinlocks, but those accessed
0066 through a serial bus normally can't. Some systems support both types.
0067
0068 On a given board each GPIO is used for one specific purpose like monitoring
0069 MMC/SD card insertion/removal, detecting card write-protect status, driving
0070 a LED, configuring a transceiver, bit-banging a serial bus, poking a hardware
0071 watchdog, sensing a switch, and so on.
0072
0073
0074 Common GPIO Properties
0075 ======================
0076
0077 These properties are met through all the other documents of the GPIO interface
0078 and it is useful to understand them, especially if you need to define GPIO
0079 mappings.
0080
0081 Active-High and Active-Low
0082 --------------------------
0083 It is natural to assume that a GPIO is "active" when its output signal is 1
0084 ("high"), and inactive when it is 0 ("low"). However in practice the signal of a
0085 GPIO may be inverted before is reaches its destination, or a device could decide
0086 to have different conventions about what "active" means. Such decisions should
0087 be transparent to device drivers, therefore it is possible to define a GPIO as
0088 being either active-high ("1" means "active", the default) or active-low ("0"
0089 means "active") so that drivers only need to worry about the logical signal and
0090 not about what happens at the line level.
0091
0092 Open Drain and Open Source
0093 --------------------------
0094 Sometimes shared signals need to use "open drain" (where only the low signal
0095 level is actually driven), or "open source" (where only the high signal level is
0096 driven) signaling. That term applies to CMOS transistors; "open collector" is
0097 used for TTL. A pullup or pulldown resistor causes the high or low signal level.
0098 This is sometimes called a "wire-AND"; or more practically, from the negative
0099 logic (low=true) perspective this is a "wire-OR".
0100
0101 One common example of an open drain signal is a shared active-low IRQ line.
0102 Also, bidirectional data bus signals sometimes use open drain signals.
0103
0104 Some GPIO controllers directly support open drain and open source outputs; many
0105 don't. When you need open drain signaling but your hardware doesn't directly
0106 support it, there's a common idiom you can use to emulate it with any GPIO pin
0107 that can be used as either an input or an output:
0108
0109 **LOW**: ``gpiod_direction_output(gpio, 0)`` ... this drives the signal and
0110 overrides the pullup.
0111
0112 **HIGH**: ``gpiod_direction_input(gpio)`` ... this turns off the output, so
0113 the pullup (or some other device) controls the signal.
0114
0115 The same logic can be applied to emulate open source signaling, by driving the
0116 high signal and configuring the GPIO as input for low. This open drain/open
0117 source emulation can be handled transparently by the GPIO framework.
0118
0119 If you are "driving" the signal high but gpiod_get_value(gpio) reports a low
0120 value (after the appropriate rise time passes), you know some other component is
0121 driving the shared signal low. That's not necessarily an error. As one common
0122 example, that's how I2C clocks are stretched: a slave that needs a slower clock
0123 delays the rising edge of SCK, and the I2C master adjusts its signaling rate
0124 accordingly.