Lines Matching +full:a +full:- +full:gpio
2 Legacy GPIO Interfaces
5 This provides an overview of GPIO access conventions on Linux.
11 What is a GPIO?
13 A "General Purpose Input/Output" (GPIO) is a flexible software-controlled
15 to Linux developers working with embedded and custom hardware. Each GPIO
16 represents a bit connected to a particular pin, or "ball" on Ball Grid Array
21 System-on-Chip (SOC) processors heavily rely on GPIOs. In some cases, every
22 non-dedicated pin can be configured as a GPIO; and most chips have at least
25 often have a few such pins to help with pin scarcity on SOCs; and there are
26 also "GPIO Expander" chips that connect using the I2C or SPI serial busses.
27 Most PC southbridges have a few dozen GPIO-capable pins (with only the BIOS
32 - Output values are writable (high=1, low=0). Some chips also have
34 value might be driven ... supporting "wire-OR" and similar schemes
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.
42 - Inputs can often be used as IRQ signals, often edge triggered but
44 wakeup events, to wake the system from a low power state.
46 - Usually a GPIO will be configurable as either input or output, as needed
49 - Most GPIOs can be accessed while holding spinlocks, but those accessed
50 through a serial bus normally can't. Some systems support both types.
52 On a given board each GPIO is used for one specific purpose like monitoring
54 a LED, configuring a transceiver, bitbanging a serial bus, poking a hardware
55 watchdog, sensing a switch, and so on.
58 GPIO conventions
60 Note that this is called a "convention" because you don't need to do it this
62 is not the main issue; GPIOs are often used for the kind of board-specific
64 used on a board that's wired differently. Only least-common-denominator
65 functionality can be very portable. Other features are platform-specific,
71 used for several very different kinds of GPIO controller. (There is some
73 in this document, but drivers acting as clients to the GPIO interface must
77 use it when possible. Platforms must select GPIOLIB if GPIO functionality
79 standard GPIO calls should have Kconfig entries which depend on GPIOLIB. The
80 GPIO calls are available, either as "real code" or as optimized-away stubs,
83 #include <linux/gpio.h>
93 -----------------
100 for the GPIO lines so that board-specific setup code directly corresponds
101 to the relevant schematics. In contrast, drivers should only use GPIO
103 board-specific pin configuration data (along with other board specific
106 So for example one platform uses numbers 32-159 for GPIOs; while another
107 uses numbers 0..63 with one set of GPIO controllers, 64-79 with another
108 type of GPIO controller, and on one particular board 80-95 with an FPGA.
110 use numbers 2000-2063 to identify GPIOs in a bank of I2C GPIO expanders.
112 If you want to initialize a structure with an invalid GPIO number, use
113 some negative number (perhaps "-EINVAL"); that will never be valid. To
114 test if such number from such a structure could reference a GPIO, you
119 A number that's not valid will be rejected by calls which may request
121 example, a number might be valid but temporarily unused on a given board.
123 Whether a platform supports multiple GPIO controllers is a platform-specific
125 of GPIO numbers, and whether new controllers can be added at runtime. Such issues
126 can affect things including whether adjacent GPIO numbers are both valid.
129 -----------
130 The first thing a system should do with a GPIO is allocate it, using
133 One of the next things to do with a GPIO, often in board setup code when
134 setting up a platform_device using the GPIO, is mark its direction::
137 int gpio_direction_input(unsigned gpio);
138 int gpio_direction_output(unsigned gpio, int value);
140 The return value is zero for success, else a negative errno. It should
143 a task context. However, for spinlock-safe GPIOs it's OK to use them
150 of a GPIO implicitly requests that GPIO (see below) if it has not been
154 Setting the direction can fail if the GPIO number is invalid, or when
155 that particular GPIO can't be used in that mode. It's generally a bad
158 that board setup code probably needs to multiplex that pin as a GPIO,
162 Spinlock-Safe GPIO access
163 -------------------------
164 Most GPIO controllers can be accessed with memory read/write instructions.
171 /* GPIO INPUT: return zero or nonzero */
172 int gpio_get_value(unsigned gpio);
174 /* GPIO OUTPUT */
175 void gpio_set_value(unsigned gpio, int value);
180 issues including open-drain signaling and output latencies.
182 The get/set calls have no error returns because "invalid GPIO" should have
188 Platform-specific implementations are encouraged to optimize the two
189 calls to access the GPIO value in cases where the GPIO number (and for
190 output, value) are constant. It's normal for them to need only a couple
191 of instructions in such cases (reading or writing a hardware register),
193 applications a lot more efficient (in both space and time) than spending
197 GPIO access that may sleep
198 --------------------------
199 Some GPIO controllers must be accessed using message based busses like I2C
200 or SPI. Commands to read or write those GPIO values require waiting to
201 get to the head of a queue to transmit a command and get its response.
204 Platforms that support this type of GPIO distinguish them from other GPIOs
205 by returning nonzero from this call (which requires a valid GPIO number,
208 int gpio_cansleep(unsigned gpio);
210 To access such GPIOs, a different set of accessors is defined::
212 /* GPIO INPUT: return zero or nonzero, might sleep */
213 int gpio_get_value_cansleep(unsigned gpio);
215 /* GPIO OUTPUT, might sleep */
216 void gpio_set_value_cansleep(unsigned gpio, int value);
219 Accessing such GPIOs requires a context which may sleep, for example
220 a threaded IRQ handler, and those accessors must be used instead of
221 spinlock-safe accessors without the cansleep() name suffix.
225 the same as the spinlock-safe calls.
228 from contexts which may sleep, since they may need to access the GPIO
246 ----------------------------
249 /* request GPIO, returning 0 or negative errno.
250 * non-null labels may be useful for diagnostics.
252 int gpio_request(unsigned gpio, const char *label);
254 /* release previously-claimed GPIO */
255 void gpio_free(unsigned gpio);
257 Passing invalid GPIO numbers to gpio_request() will fail, as will requesting
260 a task context. However, for spinlock-safe GPIOs it's OK to request GPIOs
265 several hundred potential GPIOs, but often only a dozen are used on any
267 (a) two or more drivers wrongly think they have exclusive use of that
269 needed to manage a signal that's in active use. That is, requesting a
270 GPIO can serve as a kind of lock.
277 be informed of their use; a gpiolib driver's .request() operation may call
278 pinctrl_gpio_request(), and a gpiolib driver's .free() operation may call
279 pinctrl_gpio_free(). The pinctrl subsystem allows a pinctrl_gpio_request()
280 to succeed concurrently with a pin or pingroup being "owned" by a device for
284 GPIO signal to the appropriate pin should occur within a GPIO driver's
286 setup of an output GPIO's value. This allows a glitch-free migration from a
287 pin's special function to GPIO. This is sometimes required when using a GPIO
288 to implement a workaround on signals typically driven by a non-GPIO HW block.
290 Some platforms allow some or all GPIO signals to be routed to different pins.
291 Similarly, other aspects of the GPIO or pin may need to be configured, such as
294 the pinctrl subsystem's mapping table, so that GPIO users need not be aware
297 Also note that it's your responsibility to have stopped using a GPIO
303 /* request a single GPIO, with initial configuration specified by
307 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
309 /* request multiple GPIOs in a single call
311 int gpio_request_array(struct gpio *array, size_t num);
313 /* release multiple GPIOs in a single call
315 void gpio_free_array(struct gpio *array, size_t num);
319 * GPIOF_DIR_IN - to configure direction as input
320 * GPIOF_DIR_OUT - to configure direction as output
322 * GPIOF_INIT_LOW - as output, set initial level to LOW
323 * GPIOF_INIT_HIGH - as output, set initial level to HIGH
324 * GPIOF_OPEN_DRAIN - gpio pin is open drain type.
325 * GPIOF_OPEN_SOURCE - gpio pin is open source type.
327 * GPIOF_EXPORT_DIR_FIXED - export gpio to sysfs, keep direction
328 * GPIOF_EXPORT_DIR_CHANGEABLE - also export, allow changing direction
333 * GPIOF_IN - configure as input
334 * GPIOF_OUT_INIT_LOW - configured as output, initial level LOW
335 * GPIOF_OUT_INIT_HIGH - configured as output, initial level HIGH
339 require to connect pull-up on such pins. By enabling this flag, gpio lib will
345 require to connect pull-down on such pin. By enabling this flag, gpio lib will
351 Further more, to ease the claim/release of multiple GPIOs, 'struct gpio' is
354 struct gpio {
355 unsigned gpio;
360 A typical example of usage::
362 static struct gpio leds_gpios[] = {
382 --------------------
383 GPIO numbers are unsigned integers; so are IRQ numbers. These make up
384 two logically distinct namespaces (GPIO 0 need not use IRQ 0). You can
387 /* map GPIO numbers to IRQ numbers */
388 int gpio_to_irq(unsigned gpio);
390 /* map IRQ numbers to GPIO numbers (avoid using this) */
394 else a negative errno code if the mapping can't be done. (For example,
395 some GPIOs can't be used as IRQs.) It is an unchecked error to use a GPIO
399 These two mapping calls are expected to cost on the order of a single
402 Non-error values returned from gpio_to_irq() can be passed to request_irq()
404 devices, by the board-specific initialization code. Note that IRQ trigger
408 Non-error values returned from irq_to_gpio() would most commonly be used
410 when the IRQ is edge-triggered. Note that some platforms don't support
415 ----------------------------
418 "open collector" is used for TTL.) A pullup resistor causes the high signal
419 level. This is sometimes called a "wire-AND"; or more practically, from the
420 negative logic (low=true) perspective this is a "wire-OR".
422 One common example of an open drain signal is a shared active-low IRQ line.
425 Some GPIO controllers directly support open drain outputs; many don't. When
427 there's a common idiom you can use to emulate it with any GPIO pin that can
430 LOW: gpio_direction_output(gpio, 0) ... this drives the signal
433 HIGH: gpio_direction_input(gpio) ... this turns off the output,
436 If you are "driving" the signal high but gpio_get_value(gpio) reports a low
439 common example, that's how I2C clocks are stretched: a slave that needs a
444 GPIO controllers and the pinctrl subsystem
445 ------------------------------------------
447 A GPIO controller on a SOC might be tightly coupled with the pinctrl
449 together with an optional gpio feature. We have already covered the
450 case where e.g. a GPIO controller need to reserve a pin or set the
451 direction of a pin by calling any of::
458 But how does the pin control subsystem cross-correlate the GPIO
459 numbers (which are a global business) to a certain pin on a certain
463 cross-reference tables. These are described in
464 Documentation/driver-api/pinctl.rst
467 gpio (under gpiolib) is still maintained by gpio drivers. It may happen
468 that different pin ranges in a SoC is managed by different gpio drivers.
470 This makes it logical to let gpio drivers announce their pin ranges to
473 before any gpio usage.
475 For this, the gpio controller can register its pin range with pinctrl
478 For with DT support refer to Documentation/devicetree/bindings/gpio/gpio.txt.
480 For non-DT support, user can call gpiochip_add_pin_range() with appropriate
481 parameters to register a range of gpio pins with a pinctrl driver. For this
489 this is highly chip-specific and nonportable. One platform might not need
492 to route a given GPIO to any one of several pins. (Yes, those examples all
498 pullups (or pulldowns) so that the on-chip ones should not be used.
499 (When a circuit needs 5 kOhm, on-chip 100 kOhm resistors won't do.)
500 Likewise drive strength (2 mA vs 20 mA) and voltage (1.8V vs 3.3V) is a
501 platform-specific issue, as are models like (not) having a one-to-one
504 There are other system-specific mechanisms that are not specified here,
505 like the aforementioned options for input de-glitching and wire-OR output.
508 commonly grouped in banks of 16 or 32, with a given SOC having several such
514 a side effect of configuring an add-on board with some GPIO expanders.
517 GPIO implementor's framework (OPTIONAL)
520 easier for platforms to support different kinds of GPIO controller using
523 As a debugging aid, if debugfs is available a /sys/kernel/debug/gpio file
529 -----------------------------
530 In this framework each GPIO controller is packaged as a "struct gpio_chip"
533 - methods to establish GPIO direction
534 - methods used to access GPIO values
535 - flag saying whether calls to its methods may sleep
536 - optional debugfs dump method (showing extra state like pullup config)
537 - label for diagnostics
539 There is also per-instance data, which may come from device.platform_data:
540 the number of its first GPIO, and how many GPIOs it exposes.
542 The code implementing a gpio_chip should support multiple instances of the
544 gpio_chip and issue gpiochip_add(). Removing a GPIO controller should be
547 Most often a gpio_chip is part of an instance-specific structure with state
548 not exposed by the GPIO interfaces, such as addressing, power management,
549 and more. Chips such as codecs will have complex non-GPIO state.
553 either NULL or the label associated with that GPIO when it was requested.
557 ----------------
558 To force-enable this framework, a platform's Kconfig will "select" GPIOLIB,
559 else it is up to the user to configure support for GPIO.
561 It may also provide a custom value for ARCH_NR_GPIOS, so that it better
563 wasting static table space. (It should count both built-in/SoC GPIOs and
564 also ones on GPIO expanders.
567 GPIOs through GPIO-lib and the code cannot be enabled by the user.
577 logic optimizing access to specific SOC-based GPIOs. For example, if the
578 referenced GPIO is the constant "12", getting or setting its value could
581 code, costing at least a few dozen instructions. For bitbanged I/O, such
584 For SOCs, platform-specific code defines and registers gpio_chip instances
585 for each bank of on-chip GPIOs. Those GPIOs should be numbered/labeled to
587 may well start at zero and go up to a platform-specific limit. Such GPIOs
593 -------------
594 For external GPIO controllers -- such as I2C or SPI expanders, ASICs, multi
595 function devices, FPGAs or CPLDs -- most often board-specific code handles
596 registering controller devices and ensures that their drivers know what GPIO
598 platform-specific GPIOs.
601 of GPIOs that chip will expose, and passes them to each GPIO expander chip
605 Initialization order can be important. For example, when a device relies on
606 an I2C-based GPIO, its probe() routine should only be called after that GPIO
608 calls for that GPIO can work. One way to address such dependencies is for
612 the GPIO controller device becomes unavailable.
618 configure a sysfs user interface to GPIOs. This is different from the
619 debugfs interface, since it provides control over GPIO direction and
620 value instead of just showing a gpio state summary. Plus, it could be
624 know for example that GPIO #23 controls the write protect line used to
626 may need to temporarily remove that protection, first importing a GPIO,
627 then changing its output state, then updating the code before re-enabling
628 the write protection. In normal use, GPIO #23 would never be touched,
632 userspace GPIO can be used to determine system configuration data that
634 GPIO drivers could be all that the system really needs.
637 GPIO tasks: "leds-gpio" and "gpio_keys", respectively. Use those
643 --------------
644 There are three kinds of entry in /sys/class/gpio:
646 - Control interfaces used to get userspace control over GPIOs;
648 - GPIOs themselves; and
650 - GPIO controllers ("gpio_chip" instances).
654 The control interfaces are write-only:
656 /sys/class/gpio/
659 a GPIO to userspace by writing its number to this file.
661 Example: "echo 19 > export" will create a "gpio19" node
662 for GPIO #19, if that's not requested by kernel code.
666 Example: "echo 19 > unexport" will remove a "gpio19"
669 GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
672 /sys/class/gpio/gpioN/
678 configure the GPIO as an output with that initial value.
681 doesn't support changing the direction of a GPIO, or
683 allow userspace to reconfigure this GPIO's direction.
685 "value" ... reads as either 0 (low) or 1 (high). If the GPIO
689 If the pin can be configured as interrupt-generating interrupt
696 new value or close the file and re-open it to read the value.
712 GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
714 read-only attributes:
716 /sys/class/gpio/gpiochipN/
718 "base" ... same as N, the first GPIO managed by this chip
722 "ngpio" ... how many GPIOs this manges (N to N + ngpio - 1)
726 a daughtercard might be different depending on the base board being used,
729 the correct GPIO number to use for a given signal.
733 --------------------------
737 /* export the GPIO to userspace */
738 int gpio_export(unsigned gpio, bool direction_may_change);
743 /* create a sysfs link to an exported GPIO node */
745 unsigned gpio)
747 After a kernel driver requests a GPIO, it may only be made available in
753 of experiments easier), or can provide an always-there interface that's
754 suitable for documenting as part of a board support package.
756 After the GPIO has been exported, gpio_export_link() allows creating
757 symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can
759 a descriptive name.
765 The functions listed in this section are deprecated. The GPIO descriptor based
768 .. kernel-doc:: drivers/gpio/gpiolib-legacy.c