0001 =============
0002 GPIO Mappings
0003 =============
0004
0005 This document explains how GPIOs can be assigned to given devices and functions.
0006
0007 Note that it only applies to the new descriptor-based interface. For a
0008 description of the deprecated integer-based GPIO interface please refer to
0009 legacy.rst (actually, there is no real mapping possible with the old
0010 interface; you just fetch an integer from somewhere and request the
0011 corresponding GPIO).
0012
0013 All platforms can enable the GPIO library, but if the platform strictly
0014 requires GPIO functionality to be present, it needs to select GPIOLIB from its
0015 Kconfig. Then, how GPIOs are mapped depends on what the platform uses to
0016 describe its hardware layout. Currently, mappings can be defined through device
0017 tree, ACPI, and platform data.
0018
0019 Device Tree
0020 -----------
0021 GPIOs can easily be mapped to devices and functions in the device tree. The
0022 exact way to do it depends on the GPIO controller providing the GPIOs, see the
0023 device tree bindings for your controller.
0024
0025 GPIOs mappings are defined in the consumer device's node, in a property named
0026 <function>-gpios, where <function> is the function the driver will request
0027 through gpiod_get(). For example::
0028
0029 foo_device {
0030 compatible = "acme,foo";
0031 ...
0032 led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */
0033 <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */
0034 <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */
0035
0036 power-gpios = <&gpio 1 GPIO_ACTIVE_LOW>;
0037 };
0038
0039 Properties named <function>-gpio are also considered valid and old bindings use
0040 it but are only supported for compatibility reasons and should not be used for
0041 newer bindings since it has been deprecated.
0042
0043 This property will make GPIOs 15, 16 and 17 available to the driver under the
0044 "led" function, and GPIO 1 as the "power" GPIO::
0045
0046 struct gpio_desc *red, *green, *blue, *power;
0047
0048 red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH);
0049 green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH);
0050 blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH);
0051
0052 power = gpiod_get(dev, "power", GPIOD_OUT_HIGH);
0053
0054 The led GPIOs will be active high, while the power GPIO will be active low (i.e.
0055 gpiod_is_active_low(power) will be true).
0056
0057 The second parameter of the gpiod_get() functions, the con_id string, has to be
0058 the <function>-prefix of the GPIO suffixes ("gpios" or "gpio", automatically
0059 looked up by the gpiod functions internally) used in the device tree. With above
0060 "led-gpios" example, use the prefix without the "-" as con_id parameter: "led".
0061
0062 Internally, the GPIO subsystem prefixes the GPIO suffix ("gpios" or "gpio")
0063 with the string passed in con_id to get the resulting string
0064 (``snprintf(... "%s-%s", con_id, gpio_suffixes[]``).
0065
0066 ACPI
0067 ----
0068 ACPI also supports function names for GPIOs in a similar fashion to DT.
0069 The above DT example can be converted to an equivalent ACPI description
0070 with the help of _DSD (Device Specific Data), introduced in ACPI 5.1::
0071
0072 Device (FOO) {
0073 Name (_CRS, ResourceTemplate () {
0074 GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
0075 "\\_SB.GPI0", 0, ResourceConsumer) { 15 } // red
0076 GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
0077 "\\_SB.GPI0", 0, ResourceConsumer) { 16 } // green
0078 GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
0079 "\\_SB.GPI0", 0, ResourceConsumer) { 17 } // blue
0080 GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionOutputOnly,
0081 "\\_SB.GPI0", 0, ResourceConsumer) { 1 } // power
0082 })
0083
0084 Name (_DSD, Package () {
0085 ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
0086 Package () {
0087 Package () {
0088 "led-gpios",
0089 Package () {
0090 ^FOO, 0, 0, 1,
0091 ^FOO, 1, 0, 1,
0092 ^FOO, 2, 0, 1,
0093 }
0094 },
0095 Package () { "power-gpios", Package () { ^FOO, 3, 0, 0 } },
0096 }
0097 })
0098 }
0099
0100 For more information about the ACPI GPIO bindings see
0101 Documentation/firmware-guide/acpi/gpio-properties.rst.
0102
0103 Platform Data
0104 -------------
0105 Finally, GPIOs can be bound to devices and functions using platform data. Board
0106 files that desire to do so need to include the following header::
0107
0108 #include <linux/gpio/machine.h>
0109
0110 GPIOs are mapped by the means of tables of lookups, containing instances of the
0111 gpiod_lookup structure. Two macros are defined to help declaring such mappings::
0112
0113 GPIO_LOOKUP(key, chip_hwnum, con_id, flags)
0114 GPIO_LOOKUP_IDX(key, chip_hwnum, con_id, idx, flags)
0115
0116 where
0117
0118 - key is either the label of the gpiod_chip instance providing the GPIO, or
0119 the GPIO line name
0120 - chip_hwnum is the hardware number of the GPIO within the chip, or U16_MAX
0121 to indicate that key is a GPIO line name
0122 - con_id is the name of the GPIO function from the device point of view. It
0123 can be NULL, in which case it will match any function.
0124 - idx is the index of the GPIO within the function.
0125 - flags is defined to specify the following properties:
0126 * GPIO_ACTIVE_HIGH - GPIO line is active high
0127 * GPIO_ACTIVE_LOW - GPIO line is active low
0128 * GPIO_OPEN_DRAIN - GPIO line is set up as open drain
0129 * GPIO_OPEN_SOURCE - GPIO line is set up as open source
0130 * GPIO_PERSISTENT - GPIO line is persistent during
0131 suspend/resume and maintains its value
0132 * GPIO_TRANSITORY - GPIO line is transitory and may loose its
0133 electrical state during suspend/resume
0134
0135 In the future, these flags might be extended to support more properties.
0136
0137 Note that:
0138 1. GPIO line names are not guaranteed to be globally unique, so the first
0139 match found will be used.
0140 2. GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0.
0141
0142 A lookup table can then be defined as follows, with an empty entry defining its
0143 end. The 'dev_id' field of the table is the identifier of the device that will
0144 make use of these GPIOs. It can be NULL, in which case it will be matched for
0145 calls to gpiod_get() with a NULL device.
0146
0147 .. code-block:: c
0148
0149 struct gpiod_lookup_table gpios_table = {
0150 .dev_id = "foo.0",
0151 .table = {
0152 GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),
0153 GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH),
0154 GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH),
0155 GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW),
0156 { },
0157 },
0158 };
0159
0160 And the table can be added by the board code as follows::
0161
0162 gpiod_add_lookup_table(&gpios_table);
0163
0164 The driver controlling "foo.0" will then be able to obtain its GPIOs as follows::
0165
0166 struct gpio_desc *red, *green, *blue, *power;
0167
0168 red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH);
0169 green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH);
0170 blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH);
0171
0172 power = gpiod_get(dev, "power", GPIOD_OUT_HIGH);
0173
0174 Since the "led" GPIOs are mapped as active-high, this example will switch their
0175 signals to 1, i.e. enabling the LEDs. And for the "power" GPIO, which is mapped
0176 as active-low, its actual signal will be 0 after this code. Contrary to the
0177 legacy integer GPIO interface, the active-low property is handled during
0178 mapping and is thus transparent to GPIO consumers.
0179
0180 A set of functions such as gpiod_set_value() is available to work with
0181 the new descriptor-oriented interface.
0182
0183 Boards using platform data can also hog GPIO lines by defining GPIO hog tables.
0184
0185 .. code-block:: c
0186
0187 struct gpiod_hog gpio_hog_table[] = {
0188 GPIO_HOG("gpio.0", 10, "foo", GPIO_ACTIVE_LOW, GPIOD_OUT_HIGH),
0189 { }
0190 };
0191
0192 And the table can be added to the board code as follows::
0193
0194 gpiod_add_hogs(gpio_hog_table);
0195
0196 The line will be hogged as soon as the gpiochip is created or - in case the
0197 chip was created earlier - when the hog table is registered.
0198
0199 Arrays of pins
0200 --------------
0201 In addition to requesting pins belonging to a function one by one, a device may
0202 also request an array of pins assigned to the function. The way those pins are
0203 mapped to the device determines if the array qualifies for fast bitmap
0204 processing. If yes, a bitmap is passed over get/set array functions directly
0205 between a caller and a respective .get/set_multiple() callback of a GPIO chip.
0206
0207 In order to qualify for fast bitmap processing, the array must meet the
0208 following requirements:
0209
0210 - pin hardware number of array member 0 must also be 0,
0211 - pin hardware numbers of consecutive array members which belong to the same
0212 chip as member 0 does must also match their array indexes.
0213
0214 Otherwise fast bitmap processing path is not used in order to avoid consecutive
0215 pins which belong to the same chip but are not in hardware order being processed
0216 separately.
0217
0218 If the array applies for fast bitmap processing path, pins which belong to
0219 different chips than member 0 does, as well as those with indexes different from
0220 their hardware pin numbers, are excluded from the fast path, both input and
0221 output. Moreover, open drain and open source pins are excluded from fast bitmap
0222 output processing.