Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 1 | GPIO Mappings |
| 2 | ============= |
| 3 | |
| 4 | This document explains how GPIOs can be assigned to given devices and functions. |
Linus Walleij | adbf029 | 2018-01-18 10:43:43 +0100 | [diff] [blame] | 5 | |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 6 | Note that it only applies to the new descriptor-based interface. For a |
| 7 | description of the deprecated integer-based GPIO interface please refer to |
| 8 | gpio-legacy.txt (actually, there is no real mapping possible with the old |
| 9 | interface; you just fetch an integer from somewhere and request the |
Pavel Machek | 15e2a35 | 2016-10-03 10:43:46 +0200 | [diff] [blame] | 10 | corresponding GPIO). |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 11 | |
Linus Walleij | 65053e1 | 2016-04-19 13:40:17 +0200 | [diff] [blame] | 12 | All platforms can enable the GPIO library, but if the platform strictly |
| 13 | requires GPIO functionality to be present, it needs to select GPIOLIB from its |
| 14 | Kconfig. Then, how GPIOs are mapped depends on what the platform uses to |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 15 | describe its hardware layout. Currently, mappings can be defined through device |
| 16 | tree, ACPI, and platform data. |
| 17 | |
| 18 | Device Tree |
| 19 | ----------- |
| 20 | GPIOs can easily be mapped to devices and functions in the device tree. The |
| 21 | exact way to do it depends on the GPIO controller providing the GPIOs, see the |
| 22 | device tree bindings for your controller. |
| 23 | |
| 24 | GPIOs mappings are defined in the consumer device's node, in a property named |
Javier Martinez Canillas | 2b71920 | 2015-09-21 15:14:46 +0200 | [diff] [blame] | 25 | <function>-gpios, where <function> is the function the driver will request |
| 26 | through gpiod_get(). For example: |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 27 | |
| 28 | foo_device { |
| 29 | compatible = "acme,foo"; |
| 30 | ... |
| 31 | led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */ |
| 32 | <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */ |
| 33 | <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */ |
| 34 | |
Javier Martinez Canillas | 2b71920 | 2015-09-21 15:14:46 +0200 | [diff] [blame] | 35 | power-gpios = <&gpio 1 GPIO_ACTIVE_LOW>; |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 36 | }; |
| 37 | |
Javier Martinez Canillas | 2b71920 | 2015-09-21 15:14:46 +0200 | [diff] [blame] | 38 | Properties named <function>-gpio are also considered valid and old bindings use |
| 39 | it but are only supported for compatibility reasons and should not be used for |
| 40 | newer bindings since it has been deprecated. |
| 41 | |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 42 | This property will make GPIOs 15, 16 and 17 available to the driver under the |
| 43 | "led" function, and GPIO 1 as the "power" GPIO: |
| 44 | |
| 45 | struct gpio_desc *red, *green, *blue, *power; |
| 46 | |
Dirk Behme | 69de52b | 2015-09-02 20:07:09 +0200 | [diff] [blame] | 47 | red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH); |
| 48 | green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH); |
| 49 | blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH); |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 50 | |
Dirk Behme | 69de52b | 2015-09-02 20:07:09 +0200 | [diff] [blame] | 51 | power = gpiod_get(dev, "power", GPIOD_OUT_HIGH); |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 52 | |
Linus Walleij | adbf029 | 2018-01-18 10:43:43 +0100 | [diff] [blame] | 53 | The led GPIOs will be active high, while the power GPIO will be active low (i.e. |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 54 | gpiod_is_active_low(power) will be true). |
| 55 | |
Dirk Behme | 87e77e4 | 2015-09-02 20:07:10 +0200 | [diff] [blame] | 56 | The second parameter of the gpiod_get() functions, the con_id string, has to be |
| 57 | the <function>-prefix of the GPIO suffixes ("gpios" or "gpio", automatically |
| 58 | looked up by the gpiod functions internally) used in the device tree. With above |
| 59 | "led-gpios" example, use the prefix without the "-" as con_id parameter: "led". |
| 60 | |
| 61 | Internally, the GPIO subsystem prefixes the GPIO suffix ("gpios" or "gpio") |
| 62 | with the string passed in con_id to get the resulting string |
| 63 | (snprintf(... "%s-%s", con_id, gpio_suffixes[]). |
| 64 | |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 65 | ACPI |
| 66 | ---- |
Mika Westerberg | cfc5076 | 2015-04-01 11:13:16 +0300 | [diff] [blame] | 67 | ACPI also supports function names for GPIOs in a similar fashion to DT. |
| 68 | The above DT example can be converted to an equivalent ACPI description |
| 69 | with the help of _DSD (Device Specific Data), introduced in ACPI 5.1: |
| 70 | |
| 71 | Device (FOO) { |
| 72 | Name (_CRS, ResourceTemplate () { |
| 73 | GpioIo (Exclusive, ..., IoRestrictionOutputOnly, |
| 74 | "\\_SB.GPI0") {15} // red |
| 75 | GpioIo (Exclusive, ..., IoRestrictionOutputOnly, |
| 76 | "\\_SB.GPI0") {16} // green |
| 77 | GpioIo (Exclusive, ..., IoRestrictionOutputOnly, |
| 78 | "\\_SB.GPI0") {17} // blue |
| 79 | GpioIo (Exclusive, ..., IoRestrictionOutputOnly, |
| 80 | "\\_SB.GPI0") {1} // power |
| 81 | }) |
| 82 | |
| 83 | Name (_DSD, Package () { |
| 84 | ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), |
| 85 | Package () { |
| 86 | Package () { |
| 87 | "led-gpios", |
| 88 | Package () { |
| 89 | ^FOO, 0, 0, 1, |
| 90 | ^FOO, 1, 0, 1, |
| 91 | ^FOO, 2, 0, 1, |
| 92 | } |
| 93 | }, |
| 94 | Package () { |
| 95 | "power-gpios", |
| 96 | Package () {^FOO, 3, 0, 0}, |
| 97 | }, |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | For more information about the ACPI GPIO bindings see |
| 103 | Documentation/acpi/gpio-properties.txt. |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 104 | |
| 105 | Platform Data |
| 106 | ------------- |
| 107 | Finally, GPIOs can be bound to devices and functions using platform data. Board |
| 108 | files that desire to do so need to include the following header: |
| 109 | |
Linus Walleij | 0a6d315 | 2014-07-24 20:08:55 +0200 | [diff] [blame] | 110 | #include <linux/gpio/machine.h> |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 111 | |
| 112 | GPIOs are mapped by the means of tables of lookups, containing instances of the |
| 113 | gpiod_lookup structure. Two macros are defined to help declaring such mappings: |
| 114 | |
Gabor Juhos | cfb7428 | 2016-02-25 08:00:45 +0100 | [diff] [blame] | 115 | GPIO_LOOKUP(chip_label, chip_hwnum, con_id, flags) |
| 116 | GPIO_LOOKUP_IDX(chip_label, chip_hwnum, con_id, idx, flags) |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 117 | |
| 118 | where |
| 119 | |
| 120 | - chip_label is the label of the gpiod_chip instance providing the GPIO |
| 121 | - chip_hwnum is the hardware number of the GPIO within the chip |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 122 | - con_id is the name of the GPIO function from the device point of view. It |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 123 | can be NULL, in which case it will match any function. |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 124 | - idx is the index of the GPIO within the function. |
| 125 | - flags is defined to specify the following properties: |
Linus Walleij | adbf029 | 2018-01-18 10:43:43 +0100 | [diff] [blame] | 126 | * GPIO_ACTIVE_HIGH - GPIO line is active high |
| 127 | * GPIO_ACTIVE_LOW - GPIO line is active low |
Linus Walleij | 2046362 | 2018-01-21 16:52:31 +0100 | [diff] [blame] | 128 | * GPIO_OPEN_DRAIN - GPIO line is set up as open drain |
Linus Walleij | adbf029 | 2018-01-18 10:43:43 +0100 | [diff] [blame] | 129 | * GPIO_OPEN_SOURCE - GPIO line is set up as open source |
| 130 | * GPIO_PERSISTENT - GPIO line is persistent during |
| 131 | suspend/resume and maintains its value |
| 132 | * GPIO_TRANSITORY - GPIO line is transitory and may loose its |
| 133 | electrical state during suspend/resume |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 134 | |
| 135 | In the future, these flags might be extended to support more properties. |
| 136 | |
| 137 | Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. |
| 138 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 139 | A lookup table can then be defined as follows, with an empty entry defining its |
Gabor Juhos | cfb7428 | 2016-02-25 08:00:45 +0100 | [diff] [blame] | 140 | end. The 'dev_id' field of the table is the identifier of the device that will |
| 141 | make use of these GPIOs. It can be NULL, in which case it will be matched for |
| 142 | calls to gpiod_get() with a NULL device. |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 143 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 144 | struct gpiod_lookup_table gpios_table = { |
| 145 | .dev_id = "foo.0", |
| 146 | .table = { |
| 147 | GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), |
| 148 | GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), |
| 149 | GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), |
| 150 | GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), |
| 151 | { }, |
| 152 | }, |
| 153 | }; |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 154 | |
| 155 | And the table can be added by the board code as follows: |
| 156 | |
Alexandre Courbot | ad82478 | 2013-12-03 12:20:11 +0900 | [diff] [blame] | 157 | gpiod_add_lookup_table(&gpios_table); |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 158 | |
| 159 | The driver controlling "foo.0" will then be able to obtain its GPIOs as follows: |
| 160 | |
| 161 | struct gpio_desc *red, *green, *blue, *power; |
| 162 | |
Dirk Behme | 69de52b | 2015-09-02 20:07:09 +0200 | [diff] [blame] | 163 | red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH); |
| 164 | green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH); |
| 165 | blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH); |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 166 | |
Dirk Behme | 69de52b | 2015-09-02 20:07:09 +0200 | [diff] [blame] | 167 | power = gpiod_get(dev, "power", GPIOD_OUT_HIGH); |
Alexandre Courbot | fd8e198 | 2013-11-16 21:34:21 +0900 | [diff] [blame] | 168 | |
Dirk Behme | 69de52b | 2015-09-02 20:07:09 +0200 | [diff] [blame] | 169 | Since the "led" GPIOs are mapped as active-high, this example will switch their |
| 170 | signals to 1, i.e. enabling the LEDs. And for the "power" GPIO, which is mapped |
Pavel Machek | 15e2a35 | 2016-10-03 10:43:46 +0200 | [diff] [blame] | 171 | as active-low, its actual signal will be 0 after this code. Contrary to the |
| 172 | legacy integer GPIO interface, the active-low property is handled during |
| 173 | mapping and is thus transparent to GPIO consumers. |
| 174 | |
| 175 | A set of functions such as gpiod_set_value() is available to work with |
| 176 | the new descriptor-oriented interface. |