18c2ecf20Sopenharmony_ci============= 28c2ecf20Sopenharmony_ciGPIO Mappings 38c2ecf20Sopenharmony_ci============= 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ciThis document explains how GPIOs can be assigned to given devices and functions. 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ciNote that it only applies to the new descriptor-based interface. For a 88c2ecf20Sopenharmony_cidescription of the deprecated integer-based GPIO interface please refer to 98c2ecf20Sopenharmony_cigpio-legacy.txt (actually, there is no real mapping possible with the old 108c2ecf20Sopenharmony_ciinterface; you just fetch an integer from somewhere and request the 118c2ecf20Sopenharmony_cicorresponding GPIO). 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciAll platforms can enable the GPIO library, but if the platform strictly 148c2ecf20Sopenharmony_cirequires GPIO functionality to be present, it needs to select GPIOLIB from its 158c2ecf20Sopenharmony_ciKconfig. Then, how GPIOs are mapped depends on what the platform uses to 168c2ecf20Sopenharmony_cidescribe its hardware layout. Currently, mappings can be defined through device 178c2ecf20Sopenharmony_citree, ACPI, and platform data. 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciDevice Tree 208c2ecf20Sopenharmony_ci----------- 218c2ecf20Sopenharmony_ciGPIOs can easily be mapped to devices and functions in the device tree. The 228c2ecf20Sopenharmony_ciexact way to do it depends on the GPIO controller providing the GPIOs, see the 238c2ecf20Sopenharmony_cidevice tree bindings for your controller. 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ciGPIOs mappings are defined in the consumer device's node, in a property named 268c2ecf20Sopenharmony_ci<function>-gpios, where <function> is the function the driver will request 278c2ecf20Sopenharmony_cithrough gpiod_get(). For example:: 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci foo_device { 308c2ecf20Sopenharmony_ci compatible = "acme,foo"; 318c2ecf20Sopenharmony_ci ... 328c2ecf20Sopenharmony_ci led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */ 338c2ecf20Sopenharmony_ci <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */ 348c2ecf20Sopenharmony_ci <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci power-gpios = <&gpio 1 GPIO_ACTIVE_LOW>; 378c2ecf20Sopenharmony_ci }; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ciProperties named <function>-gpio are also considered valid and old bindings use 408c2ecf20Sopenharmony_ciit but are only supported for compatibility reasons and should not be used for 418c2ecf20Sopenharmony_cinewer bindings since it has been deprecated. 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ciThis property will make GPIOs 15, 16 and 17 available to the driver under the 448c2ecf20Sopenharmony_ci"led" function, and GPIO 1 as the "power" GPIO:: 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci struct gpio_desc *red, *green, *blue, *power; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH); 498c2ecf20Sopenharmony_ci green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH); 508c2ecf20Sopenharmony_ci blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci power = gpiod_get(dev, "power", GPIOD_OUT_HIGH); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ciThe led GPIOs will be active high, while the power GPIO will be active low (i.e. 558c2ecf20Sopenharmony_cigpiod_is_active_low(power) will be true). 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ciThe second parameter of the gpiod_get() functions, the con_id string, has to be 588c2ecf20Sopenharmony_cithe <function>-prefix of the GPIO suffixes ("gpios" or "gpio", automatically 598c2ecf20Sopenharmony_cilooked up by the gpiod functions internally) used in the device tree. With above 608c2ecf20Sopenharmony_ci"led-gpios" example, use the prefix without the "-" as con_id parameter: "led". 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ciInternally, the GPIO subsystem prefixes the GPIO suffix ("gpios" or "gpio") 638c2ecf20Sopenharmony_ciwith the string passed in con_id to get the resulting string 648c2ecf20Sopenharmony_ci(``snprintf(... "%s-%s", con_id, gpio_suffixes[]``). 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ciACPI 678c2ecf20Sopenharmony_ci---- 688c2ecf20Sopenharmony_ciACPI also supports function names for GPIOs in a similar fashion to DT. 698c2ecf20Sopenharmony_ciThe above DT example can be converted to an equivalent ACPI description 708c2ecf20Sopenharmony_ciwith the help of _DSD (Device Specific Data), introduced in ACPI 5.1:: 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci Device (FOO) { 738c2ecf20Sopenharmony_ci Name (_CRS, ResourceTemplate () { 748c2ecf20Sopenharmony_ci GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 758c2ecf20Sopenharmony_ci "\\_SB.GPI0") {15} // red 768c2ecf20Sopenharmony_ci GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 778c2ecf20Sopenharmony_ci "\\_SB.GPI0") {16} // green 788c2ecf20Sopenharmony_ci GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 798c2ecf20Sopenharmony_ci "\\_SB.GPI0") {17} // blue 808c2ecf20Sopenharmony_ci GpioIo (Exclusive, ..., IoRestrictionOutputOnly, 818c2ecf20Sopenharmony_ci "\\_SB.GPI0") {1} // power 828c2ecf20Sopenharmony_ci }) 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci Name (_DSD, Package () { 858c2ecf20Sopenharmony_ci ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), 868c2ecf20Sopenharmony_ci Package () { 878c2ecf20Sopenharmony_ci Package () { 888c2ecf20Sopenharmony_ci "led-gpios", 898c2ecf20Sopenharmony_ci Package () { 908c2ecf20Sopenharmony_ci ^FOO, 0, 0, 1, 918c2ecf20Sopenharmony_ci ^FOO, 1, 0, 1, 928c2ecf20Sopenharmony_ci ^FOO, 2, 0, 1, 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci }, 958c2ecf20Sopenharmony_ci Package () { 968c2ecf20Sopenharmony_ci "power-gpios", 978c2ecf20Sopenharmony_ci Package () {^FOO, 3, 0, 0}, 988c2ecf20Sopenharmony_ci }, 998c2ecf20Sopenharmony_ci } 1008c2ecf20Sopenharmony_ci }) 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ciFor more information about the ACPI GPIO bindings see 1048c2ecf20Sopenharmony_ciDocumentation/firmware-guide/acpi/gpio-properties.rst. 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ciPlatform Data 1078c2ecf20Sopenharmony_ci------------- 1088c2ecf20Sopenharmony_ciFinally, GPIOs can be bound to devices and functions using platform data. Board 1098c2ecf20Sopenharmony_cifiles that desire to do so need to include the following header:: 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci #include <linux/gpio/machine.h> 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ciGPIOs are mapped by the means of tables of lookups, containing instances of the 1148c2ecf20Sopenharmony_cigpiod_lookup structure. Two macros are defined to help declaring such mappings:: 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci GPIO_LOOKUP(key, chip_hwnum, con_id, flags) 1178c2ecf20Sopenharmony_ci GPIO_LOOKUP_IDX(key, chip_hwnum, con_id, idx, flags) 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciwhere 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci - key is either the label of the gpiod_chip instance providing the GPIO, or 1228c2ecf20Sopenharmony_ci the GPIO line name 1238c2ecf20Sopenharmony_ci - chip_hwnum is the hardware number of the GPIO within the chip, or U16_MAX 1248c2ecf20Sopenharmony_ci to indicate that key is a GPIO line name 1258c2ecf20Sopenharmony_ci - con_id is the name of the GPIO function from the device point of view. It 1268c2ecf20Sopenharmony_ci can be NULL, in which case it will match any function. 1278c2ecf20Sopenharmony_ci - idx is the index of the GPIO within the function. 1288c2ecf20Sopenharmony_ci - flags is defined to specify the following properties: 1298c2ecf20Sopenharmony_ci * GPIO_ACTIVE_HIGH - GPIO line is active high 1308c2ecf20Sopenharmony_ci * GPIO_ACTIVE_LOW - GPIO line is active low 1318c2ecf20Sopenharmony_ci * GPIO_OPEN_DRAIN - GPIO line is set up as open drain 1328c2ecf20Sopenharmony_ci * GPIO_OPEN_SOURCE - GPIO line is set up as open source 1338c2ecf20Sopenharmony_ci * GPIO_PERSISTENT - GPIO line is persistent during 1348c2ecf20Sopenharmony_ci suspend/resume and maintains its value 1358c2ecf20Sopenharmony_ci * GPIO_TRANSITORY - GPIO line is transitory and may loose its 1368c2ecf20Sopenharmony_ci electrical state during suspend/resume 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ciIn the future, these flags might be extended to support more properties. 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ciNote that: 1418c2ecf20Sopenharmony_ci 1. GPIO line names are not guaranteed to be globally unique, so the first 1428c2ecf20Sopenharmony_ci match found will be used. 1438c2ecf20Sopenharmony_ci 2. GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ciA lookup table can then be defined as follows, with an empty entry defining its 1468c2ecf20Sopenharmony_ciend. The 'dev_id' field of the table is the identifier of the device that will 1478c2ecf20Sopenharmony_cimake use of these GPIOs. It can be NULL, in which case it will be matched for 1488c2ecf20Sopenharmony_cicalls to gpiod_get() with a NULL device. 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci.. code-block:: c 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci struct gpiod_lookup_table gpios_table = { 1538c2ecf20Sopenharmony_ci .dev_id = "foo.0", 1548c2ecf20Sopenharmony_ci .table = { 1558c2ecf20Sopenharmony_ci GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), 1568c2ecf20Sopenharmony_ci GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), 1578c2ecf20Sopenharmony_ci GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), 1588c2ecf20Sopenharmony_ci GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), 1598c2ecf20Sopenharmony_ci { }, 1608c2ecf20Sopenharmony_ci }, 1618c2ecf20Sopenharmony_ci }; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ciAnd the table can be added by the board code as follows:: 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci gpiod_add_lookup_table(&gpios_table); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ciThe driver controlling "foo.0" will then be able to obtain its GPIOs as follows:: 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci struct gpio_desc *red, *green, *blue, *power; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH); 1728c2ecf20Sopenharmony_ci green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH); 1738c2ecf20Sopenharmony_ci blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci power = gpiod_get(dev, "power", GPIOD_OUT_HIGH); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ciSince the "led" GPIOs are mapped as active-high, this example will switch their 1788c2ecf20Sopenharmony_cisignals to 1, i.e. enabling the LEDs. And for the "power" GPIO, which is mapped 1798c2ecf20Sopenharmony_cias active-low, its actual signal will be 0 after this code. Contrary to the 1808c2ecf20Sopenharmony_cilegacy integer GPIO interface, the active-low property is handled during 1818c2ecf20Sopenharmony_cimapping and is thus transparent to GPIO consumers. 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ciA set of functions such as gpiod_set_value() is available to work with 1848c2ecf20Sopenharmony_cithe new descriptor-oriented interface. 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ciBoards using platform data can also hog GPIO lines by defining GPIO hog tables. 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci.. code-block:: c 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci struct gpiod_hog gpio_hog_table[] = { 1918c2ecf20Sopenharmony_ci GPIO_HOG("gpio.0", 10, "foo", GPIO_ACTIVE_LOW, GPIOD_OUT_HIGH), 1928c2ecf20Sopenharmony_ci { } 1938c2ecf20Sopenharmony_ci }; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ciAnd the table can be added to the board code as follows:: 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci gpiod_add_hogs(gpio_hog_table); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ciThe line will be hogged as soon as the gpiochip is created or - in case the 2008c2ecf20Sopenharmony_cichip was created earlier - when the hog table is registered. 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ciArrays of pins 2038c2ecf20Sopenharmony_ci-------------- 2048c2ecf20Sopenharmony_ciIn addition to requesting pins belonging to a function one by one, a device may 2058c2ecf20Sopenharmony_cialso request an array of pins assigned to the function. The way those pins are 2068c2ecf20Sopenharmony_cimapped to the device determines if the array qualifies for fast bitmap 2078c2ecf20Sopenharmony_ciprocessing. If yes, a bitmap is passed over get/set array functions directly 2088c2ecf20Sopenharmony_cibetween a caller and a respective .get/set_multiple() callback of a GPIO chip. 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ciIn order to qualify for fast bitmap processing, the array must meet the 2118c2ecf20Sopenharmony_cifollowing requirements: 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci- pin hardware number of array member 0 must also be 0, 2148c2ecf20Sopenharmony_ci- pin hardware numbers of consecutive array members which belong to the same 2158c2ecf20Sopenharmony_ci chip as member 0 does must also match their array indexes. 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ciOtherwise fast bitmap processing path is not used in order to avoid consecutive 2188c2ecf20Sopenharmony_cipins which belong to the same chip but are not in hardware order being processed 2198c2ecf20Sopenharmony_ciseparately. 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ciIf the array applies for fast bitmap processing path, pins which belong to 2228c2ecf20Sopenharmony_cidifferent chips than member 0 does, as well as those with indexes different from 2238c2ecf20Sopenharmony_citheir hardware pin numbers, are excluded from the fast path, both input and 2248c2ecf20Sopenharmony_cioutput. Moreover, open drain and open source pins are excluded from fast bitmap 2258c2ecf20Sopenharmony_cioutput processing. 226