1*4882a593Smuzhiyun============= 2*4882a593SmuzhiyunGPIO Mappings 3*4882a593Smuzhiyun============= 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunThis document explains how GPIOs can be assigned to given devices and functions. 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunNote that it only applies to the new descriptor-based interface. For a 8*4882a593Smuzhiyundescription of the deprecated integer-based GPIO interface please refer to 9*4882a593Smuzhiyungpio-legacy.txt (actually, there is no real mapping possible with the old 10*4882a593Smuzhiyuninterface; you just fetch an integer from somewhere and request the 11*4882a593Smuzhiyuncorresponding GPIO). 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunAll platforms can enable the GPIO library, but if the platform strictly 14*4882a593Smuzhiyunrequires GPIO functionality to be present, it needs to select GPIOLIB from its 15*4882a593SmuzhiyunKconfig. Then, how GPIOs are mapped depends on what the platform uses to 16*4882a593Smuzhiyundescribe its hardware layout. Currently, mappings can be defined through device 17*4882a593Smuzhiyuntree, ACPI, and platform data. 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunDevice Tree 20*4882a593Smuzhiyun----------- 21*4882a593SmuzhiyunGPIOs can easily be mapped to devices and functions in the device tree. The 22*4882a593Smuzhiyunexact way to do it depends on the GPIO controller providing the GPIOs, see the 23*4882a593Smuzhiyundevice tree bindings for your controller. 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunGPIOs mappings are defined in the consumer device's node, in a property named 26*4882a593Smuzhiyun<function>-gpios, where <function> is the function the driver will request 27*4882a593Smuzhiyunthrough gpiod_get(). For example:: 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun foo_device { 30*4882a593Smuzhiyun compatible = "acme,foo"; 31*4882a593Smuzhiyun ... 32*4882a593Smuzhiyun led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */ 33*4882a593Smuzhiyun <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */ 34*4882a593Smuzhiyun <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */ 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun power-gpios = <&gpio 1 GPIO_ACTIVE_LOW>; 37*4882a593Smuzhiyun }; 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunProperties named <function>-gpio are also considered valid and old bindings use 40*4882a593Smuzhiyunit but are only supported for compatibility reasons and should not be used for 41*4882a593Smuzhiyunnewer bindings since it has been deprecated. 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunThis property will make GPIOs 15, 16 and 17 available to the driver under the 44*4882a593Smuzhiyun"led" function, and GPIO 1 as the "power" GPIO:: 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun struct gpio_desc *red, *green, *blue, *power; 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH); 49*4882a593Smuzhiyun green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH); 50*4882a593Smuzhiyun blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH); 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun power = gpiod_get(dev, "power", GPIOD_OUT_HIGH); 53*4882a593Smuzhiyun 54*4882a593SmuzhiyunThe led GPIOs will be active high, while the power GPIO will be active low (i.e. 55*4882a593Smuzhiyungpiod_is_active_low(power) will be true). 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunThe second parameter of the gpiod_get() functions, the con_id string, has to be 58*4882a593Smuzhiyunthe <function>-prefix of the GPIO suffixes ("gpios" or "gpio", automatically 59*4882a593Smuzhiyunlooked up by the gpiod functions internally) used in the device tree. With above 60*4882a593Smuzhiyun"led-gpios" example, use the prefix without the "-" as con_id parameter: "led". 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunInternally, the GPIO subsystem prefixes the GPIO suffix ("gpios" or "gpio") 63*4882a593Smuzhiyunwith the string passed in con_id to get the resulting string 64*4882a593Smuzhiyun(``snprintf(... "%s-%s", con_id, gpio_suffixes[]``). 65*4882a593Smuzhiyun 66*4882a593SmuzhiyunACPI 67*4882a593Smuzhiyun---- 68*4882a593SmuzhiyunACPI also supports function names for GPIOs in a similar fashion to DT. 69*4882a593SmuzhiyunThe above DT example can be converted to an equivalent ACPI description 70*4882a593Smuzhiyunwith the help of _DSD (Device Specific Data), introduced in ACPI 5.1:: 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun Device (FOO) { 73*4882a593Smuzhiyun Name (_CRS, ResourceTemplate () { 74*4882a593Smuzhiyun GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 75*4882a593Smuzhiyun "\\_SB.GPI0") {15} // red 76*4882a593Smuzhiyun GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 77*4882a593Smuzhiyun "\\_SB.GPI0") {16} // green 78*4882a593Smuzhiyun GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 79*4882a593Smuzhiyun "\\_SB.GPI0") {17} // blue 80*4882a593Smuzhiyun GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 81*4882a593Smuzhiyun "\\_SB.GPI0") {1} // power 82*4882a593Smuzhiyun }) 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun Name (_DSD, Package () { 85*4882a593Smuzhiyun ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), 86*4882a593Smuzhiyun Package () { 87*4882a593Smuzhiyun Package () { 88*4882a593Smuzhiyun "led-gpios", 89*4882a593Smuzhiyun Package () { 90*4882a593Smuzhiyun ^FOO, 0, 0, 1, 91*4882a593Smuzhiyun ^FOO, 1, 0, 1, 92*4882a593Smuzhiyun ^FOO, 2, 0, 1, 93*4882a593Smuzhiyun } 94*4882a593Smuzhiyun }, 95*4882a593Smuzhiyun Package () { 96*4882a593Smuzhiyun "power-gpios", 97*4882a593Smuzhiyun Package () {^FOO, 3, 0, 0}, 98*4882a593Smuzhiyun }, 99*4882a593Smuzhiyun } 100*4882a593Smuzhiyun }) 101*4882a593Smuzhiyun } 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunFor more information about the ACPI GPIO bindings see 104*4882a593SmuzhiyunDocumentation/firmware-guide/acpi/gpio-properties.rst. 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunPlatform Data 107*4882a593Smuzhiyun------------- 108*4882a593SmuzhiyunFinally, GPIOs can be bound to devices and functions using platform data. Board 109*4882a593Smuzhiyunfiles that desire to do so need to include the following header:: 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun #include <linux/gpio/machine.h> 112*4882a593Smuzhiyun 113*4882a593SmuzhiyunGPIOs are mapped by the means of tables of lookups, containing instances of the 114*4882a593Smuzhiyungpiod_lookup structure. Two macros are defined to help declaring such mappings:: 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun GPIO_LOOKUP(key, chip_hwnum, con_id, flags) 117*4882a593Smuzhiyun GPIO_LOOKUP_IDX(key, chip_hwnum, con_id, idx, flags) 118*4882a593Smuzhiyun 119*4882a593Smuzhiyunwhere 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun - key is either the label of the gpiod_chip instance providing the GPIO, or 122*4882a593Smuzhiyun the GPIO line name 123*4882a593Smuzhiyun - chip_hwnum is the hardware number of the GPIO within the chip, or U16_MAX 124*4882a593Smuzhiyun to indicate that key is a GPIO line name 125*4882a593Smuzhiyun - con_id is the name of the GPIO function from the device point of view. It 126*4882a593Smuzhiyun can be NULL, in which case it will match any function. 127*4882a593Smuzhiyun - idx is the index of the GPIO within the function. 128*4882a593Smuzhiyun - flags is defined to specify the following properties: 129*4882a593Smuzhiyun * GPIO_ACTIVE_HIGH - GPIO line is active high 130*4882a593Smuzhiyun * GPIO_ACTIVE_LOW - GPIO line is active low 131*4882a593Smuzhiyun * GPIO_OPEN_DRAIN - GPIO line is set up as open drain 132*4882a593Smuzhiyun * GPIO_OPEN_SOURCE - GPIO line is set up as open source 133*4882a593Smuzhiyun * GPIO_PERSISTENT - GPIO line is persistent during 134*4882a593Smuzhiyun suspend/resume and maintains its value 135*4882a593Smuzhiyun * GPIO_TRANSITORY - GPIO line is transitory and may loose its 136*4882a593Smuzhiyun electrical state during suspend/resume 137*4882a593Smuzhiyun 138*4882a593SmuzhiyunIn the future, these flags might be extended to support more properties. 139*4882a593Smuzhiyun 140*4882a593SmuzhiyunNote that: 141*4882a593Smuzhiyun 1. GPIO line names are not guaranteed to be globally unique, so the first 142*4882a593Smuzhiyun match found will be used. 143*4882a593Smuzhiyun 2. GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. 144*4882a593Smuzhiyun 145*4882a593SmuzhiyunA lookup table can then be defined as follows, with an empty entry defining its 146*4882a593Smuzhiyunend. The 'dev_id' field of the table is the identifier of the device that will 147*4882a593Smuzhiyunmake use of these GPIOs. It can be NULL, in which case it will be matched for 148*4882a593Smuzhiyuncalls to gpiod_get() with a NULL device. 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun.. code-block:: c 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun struct gpiod_lookup_table gpios_table = { 153*4882a593Smuzhiyun .dev_id = "foo.0", 154*4882a593Smuzhiyun .table = { 155*4882a593Smuzhiyun GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), 156*4882a593Smuzhiyun GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), 157*4882a593Smuzhiyun GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), 158*4882a593Smuzhiyun GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), 159*4882a593Smuzhiyun { }, 160*4882a593Smuzhiyun }, 161*4882a593Smuzhiyun }; 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunAnd the table can be added by the board code as follows:: 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun gpiod_add_lookup_table(&gpios_table); 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunThe driver controlling "foo.0" will then be able to obtain its GPIOs as follows:: 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun struct gpio_desc *red, *green, *blue, *power; 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH); 172*4882a593Smuzhiyun green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH); 173*4882a593Smuzhiyun blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH); 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun power = gpiod_get(dev, "power", GPIOD_OUT_HIGH); 176*4882a593Smuzhiyun 177*4882a593SmuzhiyunSince the "led" GPIOs are mapped as active-high, this example will switch their 178*4882a593Smuzhiyunsignals to 1, i.e. enabling the LEDs. And for the "power" GPIO, which is mapped 179*4882a593Smuzhiyunas active-low, its actual signal will be 0 after this code. Contrary to the 180*4882a593Smuzhiyunlegacy integer GPIO interface, the active-low property is handled during 181*4882a593Smuzhiyunmapping and is thus transparent to GPIO consumers. 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunA set of functions such as gpiod_set_value() is available to work with 184*4882a593Smuzhiyunthe new descriptor-oriented interface. 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunBoards using platform data can also hog GPIO lines by defining GPIO hog tables. 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun.. code-block:: c 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun struct gpiod_hog gpio_hog_table[] = { 191*4882a593Smuzhiyun GPIO_HOG("gpio.0", 10, "foo", GPIO_ACTIVE_LOW, GPIOD_OUT_HIGH), 192*4882a593Smuzhiyun { } 193*4882a593Smuzhiyun }; 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunAnd the table can be added to the board code as follows:: 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun gpiod_add_hogs(gpio_hog_table); 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunThe line will be hogged as soon as the gpiochip is created or - in case the 200*4882a593Smuzhiyunchip was created earlier - when the hog table is registered. 201*4882a593Smuzhiyun 202*4882a593SmuzhiyunArrays of pins 203*4882a593Smuzhiyun-------------- 204*4882a593SmuzhiyunIn addition to requesting pins belonging to a function one by one, a device may 205*4882a593Smuzhiyunalso request an array of pins assigned to the function. The way those pins are 206*4882a593Smuzhiyunmapped to the device determines if the array qualifies for fast bitmap 207*4882a593Smuzhiyunprocessing. If yes, a bitmap is passed over get/set array functions directly 208*4882a593Smuzhiyunbetween a caller and a respective .get/set_multiple() callback of a GPIO chip. 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunIn order to qualify for fast bitmap processing, the array must meet the 211*4882a593Smuzhiyunfollowing requirements: 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun- pin hardware number of array member 0 must also be 0, 214*4882a593Smuzhiyun- pin hardware numbers of consecutive array members which belong to the same 215*4882a593Smuzhiyun chip as member 0 does must also match their array indexes. 216*4882a593Smuzhiyun 217*4882a593SmuzhiyunOtherwise fast bitmap processing path is not used in order to avoid consecutive 218*4882a593Smuzhiyunpins which belong to the same chip but are not in hardware order being processed 219*4882a593Smuzhiyunseparately. 220*4882a593Smuzhiyun 221*4882a593SmuzhiyunIf the array applies for fast bitmap processing path, pins which belong to 222*4882a593Smuzhiyundifferent chips than member 0 does, as well as those with indexes different from 223*4882a593Smuzhiyuntheir hardware pin numbers, are excluded from the fast path, both input and 224*4882a593Smuzhiyunoutput. Moreover, open drain and open source pins are excluded from fast bitmap 225*4882a593Smuzhiyunoutput processing. 226