blob: 36af58eba136bac198ea3b1f33f34236be9ce5df [file] [log] [blame]
David Brownell4c203862007-02-12 00:53:11 -08001GPIO Interfaces
2
3This provides an overview of GPIO access conventions on Linux.
4
5
6What is a GPIO?
7===============
8A "General Purpose Input/Output" (GPIO) is a flexible software-controlled
9digital signal. They are provided from many kinds of chip, and are familiar
10to Linux developers working with embedded and custom hardware. Each GPIO
11represents a bit connected to a particular pin, or "ball" on Ball Grid Array
12(BGA) packages. Board schematics show which external hardware connects to
13which GPIOs. Drivers can be written generically, so that board setup code
14passes such pin configuration data to drivers.
15
16System-on-Chip (SOC) processors heavily rely on GPIOs. In some cases, every
17non-dedicated pin can be configured as a GPIO; and most chips have at least
18several dozen of them. Programmable logic devices (like FPGAs) can easily
19provide GPIOs; multifunction chips like power managers, and audio codecs
20often have a few such pins to help with pin scarcity on SOCs; and there are
21also "GPIO Expander" chips that connect using the I2C or SPI serial busses.
22Most PC southbridges have a few dozen GPIO-capable pins (with only the BIOS
23firmware knowing how they're used).
24
25The exact capabilities of GPIOs vary between systems. Common options:
26
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
David Brownell1668be72007-04-11 23:28:42 -070030 for the other value (notably, "open drain" signaling).
David Brownell4c203862007-02-12 00:53:11 -080031
32 - Input values are likewise readable (1, 0). Some chips support readback
33 of pins configured as "output", which is very useful in such "wire-OR"
34 cases (to support bidirectional signaling). GPIO controllers may have
35 input de-glitch logic, sometimes with software controls.
36
37 - Inputs can often be used as IRQ signals, often edge triggered but
38 sometimes level triggered. Such IRQs may be configurable as system
39 wakeup events, to wake the system from a low power state.
40
41 - Usually a GPIO will be configurable as either input or output, as needed
42 by different product boards; single direction ones exist too.
43
44 - Most GPIOs can be accessed while holding spinlocks, but those accessed
45 through a serial bus normally can't. Some systems support both types.
46
47On a given board each GPIO is used for one specific purpose like monitoring
48MMC/SD card insertion/removal, detecting card writeprotect status, driving
49a LED, configuring a transceiver, bitbanging a serial bus, poking a hardware
50watchdog, sensing a switch, and so on.
51
52
53GPIO conventions
54================
55Note that this is called a "convention" because you don't need to do it this
56way, and it's no crime if you don't. There **are** cases where portability
57is not the main issue; GPIOs are often used for the kind of board-specific
58glue logic that may even change between board revisions, and can't ever be
59used on a board that's wired differently. Only least-common-denominator
60functionality can be very portable. Other features are platform-specific,
61and that can be critical for glue logic.
62
63Plus, this doesn't define an implementation framework, just an interface.
64One platform might implement it as simple inline functions accessing chip
65registers; another might implement it by delegating through abstractions
66used for several very different kinds of GPIO controller.
67
68That said, if the convention is supported on their platform, drivers should
David Brownell32993b72007-05-10 22:22:17 -070069use it when possible. Platforms should declare GENERIC_GPIO support in
70Kconfig (boolean true), which multi-platform drivers can depend on when
71using the include file:
David Brownell4c203862007-02-12 00:53:11 -080072
73 #include <asm/gpio.h>
74
75If you stick to this convention then it'll be easier for other developers to
76see what your code is doing, and help maintain it.
77
78
79Identifying GPIOs
80-----------------
81GPIOs are identified by unsigned integers in the range 0..MAX_INT. That
82reserves "negative" numbers for other purposes like marking signals as
David Brownellf5de6112007-02-16 01:27:14 -080083"not available on this board", or indicating faults. Code that doesn't
84touch the underlying hardware treats these integers as opaque cookies.
David Brownell4c203862007-02-12 00:53:11 -080085
86Platforms define how they use those integers, and usually #define symbols
87for the GPIO lines so that board-specific setup code directly corresponds
88to the relevant schematics. In contrast, drivers should only use GPIO
89numbers passed to them from that setup code, using platform_data to hold
90board-specific pin configuration data (along with other board specific
91data they need). That avoids portability problems.
92
93So for example one platform uses numbers 32-159 for GPIOs; while another
94uses numbers 0..63 with one set of GPIO controllers, 64-79 with another
95type of GPIO controller, and on one particular board 80-95 with an FPGA.
96The numbers need not be contiguous; either of those platforms could also
97use numbers 2000-2063 to identify GPIOs in a bank of I2C GPIO expanders.
98
99Whether a platform supports multiple GPIO controllers is currently a
100platform-specific implementation issue.
101
102
103Using GPIOs
104-----------
105One of the first things to do with a GPIO, often in board setup code when
106setting up a platform_device using the GPIO, is mark its direction:
107
108 /* set as input or output, returning 0 or negative errno */
109 int gpio_direction_input(unsigned gpio);
David Brownell28735a72007-03-16 13:38:14 -0800110 int gpio_direction_output(unsigned gpio, int value);
David Brownell4c203862007-02-12 00:53:11 -0800111
112The return value is zero for success, else a negative errno. It should
113be checked, since the get/set calls don't have error returns and since
David Brownell83c65902007-05-16 22:11:13 -0700114misconfiguration is possible. You should normally issue these calls from
115a task context. However, for spinlock-safe GPIOs it's OK to use them
116before tasking is enabled, as part of early board setup.
David Brownell4c203862007-02-12 00:53:11 -0800117
David Brownell28735a72007-03-16 13:38:14 -0800118For output GPIOs, the value provided becomes the initial output value.
119This helps avoid signal glitching during system startup.
120
David Brownell4c203862007-02-12 00:53:11 -0800121Setting the direction can fail if the GPIO number is invalid, or when
122that particular GPIO can't be used in that mode. It's generally a bad
123idea to rely on boot firmware to have set the direction correctly, since
124it probably wasn't validated to do more than boot Linux. (Similarly,
125that board setup code probably needs to multiplex that pin as a GPIO,
126and configure pullups/pulldowns appropriately.)
127
128
129Spinlock-Safe GPIO access
130-------------------------
131Most GPIO controllers can be accessed with memory read/write instructions.
132That doesn't need to sleep, and can safely be done from inside IRQ handlers.
133
134Use these calls to access such GPIOs:
135
136 /* GPIO INPUT: return zero or nonzero */
137 int gpio_get_value(unsigned gpio);
138
139 /* GPIO OUTPUT */
140 void gpio_set_value(unsigned gpio, int value);
141
142The values are boolean, zero for low, nonzero for high. When reading the
143value of an output pin, the value returned should be what's seen on the
144pin ... that won't always match the specified output value, because of
145issues including wire-OR and output latencies.
146
147The get/set calls have no error returns because "invalid GPIO" should have
148been reported earlier in gpio_set_direction(). However, note that not all
149platforms can read the value of output pins; those that can't should always
David Brownellf5de6112007-02-16 01:27:14 -0800150return zero. Also, using these calls for GPIOs that can't safely be accessed
151without sleeping (see below) is an error.
David Brownell4c203862007-02-12 00:53:11 -0800152
David Brownellf5de6112007-02-16 01:27:14 -0800153Platform-specific implementations are encouraged to optimize the two
David Brownell4c203862007-02-12 00:53:11 -0800154calls to access the GPIO value in cases where the GPIO number (and for
155output, value) are constant. It's normal for them to need only a couple
156of instructions in such cases (reading or writing a hardware register),
157and not to need spinlocks. Such optimized calls can make bitbanging
158applications a lot more efficient (in both space and time) than spending
159dozens of instructions on subroutine calls.
160
161
162GPIO access that may sleep
163--------------------------
164Some GPIO controllers must be accessed using message based busses like I2C
165or SPI. Commands to read or write those GPIO values require waiting to
166get to the head of a queue to transmit a command and get its response.
167This requires sleeping, which can't be done from inside IRQ handlers.
168
169Platforms that support this type of GPIO distinguish them from other GPIOs
170by returning nonzero from this call:
171
172 int gpio_cansleep(unsigned gpio);
173
174To access such GPIOs, a different set of accessors is defined:
175
176 /* GPIO INPUT: return zero or nonzero, might sleep */
177 int gpio_get_value_cansleep(unsigned gpio);
178
179 /* GPIO OUTPUT, might sleep */
180 void gpio_set_value_cansleep(unsigned gpio, int value);
181
182Other than the fact that these calls might sleep, and will not be ignored
183for GPIOs that can't be accessed from IRQ handlers, these calls act the
184same as the spinlock-safe calls.
185
186
187Claiming and Releasing GPIOs (OPTIONAL)
188---------------------------------------
189To help catch system configuration errors, two calls are defined.
190However, many platforms don't currently support this mechanism.
191
192 /* request GPIO, returning 0 or negative errno.
193 * non-null labels may be useful for diagnostics.
194 */
195 int gpio_request(unsigned gpio, const char *label);
196
197 /* release previously-claimed GPIO */
198 void gpio_free(unsigned gpio);
199
200Passing invalid GPIO numbers to gpio_request() will fail, as will requesting
201GPIOs that have already been claimed with that call. The return value of
David Brownell83c65902007-05-16 22:11:13 -0700202gpio_request() must be checked. You should normally issue these calls from
203a task context. However, for spinlock-safe GPIOs it's OK to request GPIOs
204before tasking is enabled, as part of early board setup.
David Brownell4c203862007-02-12 00:53:11 -0800205
206These calls serve two basic purposes. One is marking the signals which
207are actually in use as GPIOs, for better diagnostics; systems may have
208several hundred potential GPIOs, but often only a dozen are used on any
209given board. Another is to catch conflicts between drivers, reporting
210errors when drivers wrongly think they have exclusive use of that signal.
211
212These two calls are optional because not not all current Linux platforms
213offer such functionality in their GPIO support; a valid implementation
214could return success for all gpio_request() calls. Unlike the other calls,
215the state they represent doesn't normally match anything from a hardware
216register; it's just a software bitmap which clearly is not necessary for
217correct operation of hardware or (bug free) drivers.
218
219Note that requesting a GPIO does NOT cause it to be configured in any
220way; it just marks that GPIO as in use. Separate code must handle any
221pin setup (e.g. controlling which pin the GPIO uses, pullup/pulldown).
222
223
224GPIOs mapped to IRQs
225--------------------
226GPIO numbers are unsigned integers; so are IRQ numbers. These make up
227two logically distinct namespaces (GPIO 0 need not use IRQ 0). You can
228map between them using calls like:
229
230 /* map GPIO numbers to IRQ numbers */
231 int gpio_to_irq(unsigned gpio);
232
233 /* map IRQ numbers to GPIO numbers */
234 int irq_to_gpio(unsigned irq);
235
236Those return either the corresponding number in the other namespace, or
237else a negative errno code if the mapping can't be done. (For example,
238some GPIOs can't used as IRQs.) It is an unchecked error to use a GPIO
239number that hasn't been marked as an input using gpio_set_direction(), or
240to use an IRQ number that didn't originally come from gpio_to_irq().
241
242These two mapping calls are expected to cost on the order of a single
243addition or subtraction. They're not allowed to sleep.
244
245Non-error values returned from gpio_to_irq() can be passed to request_irq()
246or free_irq(). They will often be stored into IRQ resources for platform
247devices, by the board-specific initialization code. Note that IRQ trigger
248options are part of the IRQ interface, e.g. IRQF_TRIGGER_FALLING, as are
249system wakeup capabilities.
250
251Non-error values returned from irq_to_gpio() would most commonly be used
David Brownellf5de6112007-02-16 01:27:14 -0800252with gpio_get_value(), for example to initialize or update driver state
253when the IRQ is edge-triggered.
David Brownell4c203862007-02-12 00:53:11 -0800254
255
David Brownell1668be72007-04-11 23:28:42 -0700256Emulating Open Drain Signals
257----------------------------
258Sometimes shared signals need to use "open drain" signaling, where only the
259low signal level is actually driven. (That term applies to CMOS transistors;
260"open collector" is used for TTL.) A pullup resistor causes the high signal
261level. This is sometimes called a "wire-AND"; or more practically, from the
262negative logic (low=true) perspective this is a "wire-OR".
263
264One common example of an open drain signal is a shared active-low IRQ line.
265Also, bidirectional data bus signals sometimes use open drain signals.
266
267Some GPIO controllers directly support open drain outputs; many don't. When
268you need open drain signaling but your hardware doesn't directly support it,
269there's a common idiom you can use to emulate it with any GPIO pin that can
270be used as either an input or an output:
271
272 LOW: gpio_direction_output(gpio, 0) ... this drives the signal
273 and overrides the pullup.
274
275 HIGH: gpio_direction_input(gpio) ... this turns off the output,
276 so the pullup (or some other device) controls the signal.
277
278If you are "driving" the signal high but gpio_get_value(gpio) reports a low
279value (after the appropriate rise time passes), you know some other component
280is driving the shared signal low. That's not necessarily an error. As one
281common example, that's how I2C clocks are stretched: a slave that needs a
282slower clock delays the rising edge of SCK, and the I2C master adjusts its
283signaling rate accordingly.
284
David Brownell4c203862007-02-12 00:53:11 -0800285
286What do these conventions omit?
287===============================
288One of the biggest things these conventions omit is pin multiplexing, since
289this is highly chip-specific and nonportable. One platform might not need
290explicit multiplexing; another might have just two options for use of any
291given pin; another might have eight options per pin; another might be able
292to route a given GPIO to any one of several pins. (Yes, those examples all
293come from systems that run Linux today.)
294
295Related to multiplexing is configuration and enabling of the pullups or
296pulldowns integrated on some platforms. Not all platforms support them,
297or support them in the same way; and any given board might use external
298pullups (or pulldowns) so that the on-chip ones should not be used.
299
300There are other system-specific mechanisms that are not specified here,
301like the aforementioned options for input de-glitching and wire-OR output.
302Hardware may support reading or writing GPIOs in gangs, but that's usually
David Brownellf5de6112007-02-16 01:27:14 -0800303configuration dependent: for GPIOs sharing the same bank. (GPIOs are
David Brownell4c203862007-02-12 00:53:11 -0800304commonly grouped in banks of 16 or 32, with a given SOC having several such
David Brownellf5de6112007-02-16 01:27:14 -0800305banks.) Some systems can trigger IRQs from output GPIOs. Code relying on
306such mechanisms will necessarily be nonportable.
David Brownell4c203862007-02-12 00:53:11 -0800307
308Dynamic definition of GPIOs is not currently supported; for example, as
309a side effect of configuring an add-on board with some GPIO expanders.
310
311These calls are purely for kernel space, but a userspace API could be built
312on top of it.