xref: /linux/Documentation/driver-api/gpio/intro.rst (revision c771600c6af14749609b49565ffb4cac2959710d)
102bf219dSJonathan Neuschäfer============
202bf219dSJonathan NeuschäferIntroduction
302bf219dSJonathan Neuschäfer============
402bf219dSJonathan Neuschäfer
502bf219dSJonathan Neuschäfer
6fd8e198cSAlexandre CourbotGPIO Interfaces
7fd8e198cSAlexandre Courbot===============
8fd8e198cSAlexandre Courbot
9fd8e198cSAlexandre CourbotThe documents in this directory give detailed instructions on how to access
10fd8e198cSAlexandre CourbotGPIOs in drivers, and how to write a driver for a device that provides GPIOs
11fd8e198cSAlexandre Courbotitself.
12fd8e198cSAlexandre Courbot
13fd8e198cSAlexandre Courbot
14fd8e198cSAlexandre CourbotWhat is a GPIO?
15fd8e198cSAlexandre Courbot===============
16fd8e198cSAlexandre Courbot
17fd8e198cSAlexandre CourbotA "General Purpose Input/Output" (GPIO) is a flexible software-controlled
182379d15aSBryan Brattlofdigital signal. They are provided from many kinds of chips, and are familiar
19fd8e198cSAlexandre Courbotto Linux developers working with embedded and custom hardware. Each GPIO
20fd8e198cSAlexandre Courbotrepresents a bit connected to a particular pin, or "ball" on Ball Grid Array
21fd8e198cSAlexandre Courbot(BGA) packages. Board schematics show which external hardware connects to
22fd8e198cSAlexandre Courbotwhich GPIOs. Drivers can be written generically, so that board setup code
23fd8e198cSAlexandre Courbotpasses such pin configuration data to drivers.
24fd8e198cSAlexandre Courbot
25fd8e198cSAlexandre CourbotSystem-on-Chip (SOC) processors heavily rely on GPIOs. In some cases, every
26fd8e198cSAlexandre Courbotnon-dedicated pin can be configured as a GPIO; and most chips have at least
27fd8e198cSAlexandre Courbotseveral dozen of them. Programmable logic devices (like FPGAs) can easily
28fd8e198cSAlexandre Courbotprovide GPIOs; multifunction chips like power managers, and audio codecs
29fd8e198cSAlexandre Courbotoften have a few such pins to help with pin scarcity on SOCs; and there are
30fd8e198cSAlexandre Courbotalso "GPIO Expander" chips that connect using the I2C or SPI serial buses.
31fd8e198cSAlexandre CourbotMost PC southbridges have a few dozen GPIO-capable pins (with only the BIOS
32fd8e198cSAlexandre Courbotfirmware knowing how they're used).
33fd8e198cSAlexandre Courbot
34fd8e198cSAlexandre CourbotThe exact capabilities of GPIOs vary between systems. Common options:
35fd8e198cSAlexandre Courbot
36fd8e198cSAlexandre Courbot  - Output values are writable (high=1, low=0). Some chips also have
37fd8e198cSAlexandre Courbot    options about how that value is driven, so that for example only one
38fd8e198cSAlexandre Courbot    value might be driven, supporting "wire-OR" and similar schemes for the
39fd8e198cSAlexandre Courbot    other value (notably, "open drain" signaling).
40fd8e198cSAlexandre Courbot
41fd8e198cSAlexandre Courbot  - Input values are likewise readable (1, 0). Some chips support readback
42fd8e198cSAlexandre Courbot    of pins configured as "output", which is very useful in such "wire-OR"
43fd8e198cSAlexandre Courbot    cases (to support bidirectional signaling). GPIO controllers may have
44fd8e198cSAlexandre Courbot    input de-glitch/debounce logic, sometimes with software controls.
45fd8e198cSAlexandre Courbot
46fd8e198cSAlexandre Courbot  - Inputs can often be used as IRQ signals, often edge triggered but
47fd8e198cSAlexandre Courbot    sometimes level triggered. Such IRQs may be configurable as system
48fd8e198cSAlexandre Courbot    wakeup events, to wake the system from a low power state.
49fd8e198cSAlexandre Courbot
50fd8e198cSAlexandre Courbot  - Usually a GPIO will be configurable as either input or output, as needed
51fd8e198cSAlexandre Courbot    by different product boards; single direction ones exist too.
52fd8e198cSAlexandre Courbot
53fd8e198cSAlexandre Courbot  - Most GPIOs can be accessed while holding spinlocks, but those accessed
54fd8e198cSAlexandre Courbot    through a serial bus normally can't. Some systems support both types.
55fd8e198cSAlexandre Courbot
56fd8e198cSAlexandre CourbotOn a given board each GPIO is used for one specific purpose like monitoring
57fd8e198cSAlexandre CourbotMMC/SD card insertion/removal, detecting card write-protect status, driving
58fd8e198cSAlexandre Courbota LED, configuring a transceiver, bit-banging a serial bus, poking a hardware
59fd8e198cSAlexandre Courbotwatchdog, sensing a switch, and so on.
60fd8e198cSAlexandre Courbot
61fd8e198cSAlexandre Courbot
62fd8e198cSAlexandre CourbotCommon GPIO Properties
63fd8e198cSAlexandre Courbot======================
64fd8e198cSAlexandre Courbot
65fd8e198cSAlexandre CourbotThese properties are met through all the other documents of the GPIO interface
66fd8e198cSAlexandre Courbotand it is useful to understand them, especially if you need to define GPIO
67fd8e198cSAlexandre Courbotmappings.
68fd8e198cSAlexandre Courbot
69fd8e198cSAlexandre CourbotActive-High and Active-Low
70fd8e198cSAlexandre Courbot--------------------------
71fd8e198cSAlexandre CourbotIt is natural to assume that a GPIO is "active" when its output signal is 1
72fd8e198cSAlexandre Courbot("high"), and inactive when it is 0 ("low"). However in practice the signal of a
73fd8e198cSAlexandre CourbotGPIO may be inverted before is reaches its destination, or a device could decide
74fd8e198cSAlexandre Courbotto have different conventions about what "active" means. Such decisions should
75fd8e198cSAlexandre Courbotbe transparent to device drivers, therefore it is possible to define a GPIO as
76fd8e198cSAlexandre Courbotbeing either active-high ("1" means "active", the default) or active-low ("0"
77fd8e198cSAlexandre Courbotmeans "active") so that drivers only need to worry about the logical signal and
78fd8e198cSAlexandre Courbotnot about what happens at the line level.
79fd8e198cSAlexandre Courbot
80fd8e198cSAlexandre CourbotOpen Drain and Open Source
81fd8e198cSAlexandre Courbot--------------------------
82fd8e198cSAlexandre CourbotSometimes shared signals need to use "open drain" (where only the low signal
83fd8e198cSAlexandre Courbotlevel is actually driven), or "open source" (where only the high signal level is
84fd8e198cSAlexandre Courbotdriven) signaling. That term applies to CMOS transistors; "open collector" is
85fd8e198cSAlexandre Courbotused for TTL. A pullup or pulldown resistor causes the high or low signal level.
86fd8e198cSAlexandre CourbotThis is sometimes called a "wire-AND"; or more practically, from the negative
87fd8e198cSAlexandre Courbotlogic (low=true) perspective this is a "wire-OR".
88fd8e198cSAlexandre Courbot
89fd8e198cSAlexandre CourbotOne common example of an open drain signal is a shared active-low IRQ line.
90fd8e198cSAlexandre CourbotAlso, bidirectional data bus signals sometimes use open drain signals.
91fd8e198cSAlexandre Courbot
92fd8e198cSAlexandre CourbotSome GPIO controllers directly support open drain and open source outputs; many
93fd8e198cSAlexandre Courbotdon't. When you need open drain signaling but your hardware doesn't directly
94fd8e198cSAlexandre Courbotsupport it, there's a common idiom you can use to emulate it with any GPIO pin
95fd8e198cSAlexandre Courbotthat can be used as either an input or an output:
96fd8e198cSAlexandre Courbot
97e1d4d663SJonathan Neuschäfer **LOW**: ``gpiod_direction_output(gpio, 0)`` ... this drives the signal and
98e1d4d663SJonathan Neuschäfer overrides the pullup.
99fd8e198cSAlexandre Courbot
100e1d4d663SJonathan Neuschäfer **HIGH**: ``gpiod_direction_input(gpio)`` ... this turns off the output, so
101e1d4d663SJonathan Neuschäfer the pullup (or some other device) controls the signal.
102fd8e198cSAlexandre Courbot
103fd8e198cSAlexandre CourbotThe same logic can be applied to emulate open source signaling, by driving the
104fd8e198cSAlexandre Courbothigh signal and configuring the GPIO as input for low. This open drain/open
105fd8e198cSAlexandre Courbotsource emulation can be handled transparently by the GPIO framework.
106fd8e198cSAlexandre Courbot
107fd8e198cSAlexandre CourbotIf you are "driving" the signal high but gpiod_get_value(gpio) reports a low
108fd8e198cSAlexandre Courbotvalue (after the appropriate rise time passes), you know some other component is
109fd8e198cSAlexandre Courbotdriving the shared signal low. That's not necessarily an error. As one common
110fd8e198cSAlexandre Courbotexample, that's how I2C clocks are stretched:  a slave that needs a slower clock
111fd8e198cSAlexandre Courbotdelays the rising edge of SCK, and the I2C master adjusts its signaling rate
112fd8e198cSAlexandre Courbotaccordingly.
113