1b6dff0e1SChangbin Du.. SPDX-License-Identifier: GPL-2.0 2b6dff0e1SChangbin Du 3b6dff0e1SChangbin Du====================================== 4b6dff0e1SChangbin Du_DSD Device Properties Related to GPIO 5b6dff0e1SChangbin Du====================================== 6b6dff0e1SChangbin Du 7b6dff0e1SChangbin DuWith the release of ACPI 5.1, the _DSD configuration object finally 8b6dff0e1SChangbin Duallows names to be given to GPIOs (and other things as well) returned 9b6dff0e1SChangbin Duby _CRS. Previously, we were only able to use an integer index to find 10b6dff0e1SChangbin Duthe corresponding GPIO, which is pretty error prone (it depends on 11b6dff0e1SChangbin Duthe _CRS output ordering, for example). 12b6dff0e1SChangbin Du 13b6dff0e1SChangbin DuWith _DSD we can now query GPIOs using a name instead of an integer 14b6dff0e1SChangbin Duindex, like the ASL example below shows:: 15b6dff0e1SChangbin Du 16b6dff0e1SChangbin Du // Bluetooth device with reset and shutdown GPIOs 17b6dff0e1SChangbin Du Device (BTH) 18b6dff0e1SChangbin Du { 19b6dff0e1SChangbin Du Name (_HID, ...) 20b6dff0e1SChangbin Du 21b6dff0e1SChangbin Du Name (_CRS, ResourceTemplate () 22b6dff0e1SChangbin Du { 231bd33879SAndy Shevchenko GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly, 24b6dff0e1SChangbin Du "\\_SB.GPO0", 0, ResourceConsumer) { 15 } 251bd33879SAndy Shevchenko GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly, 26b6dff0e1SChangbin Du "\\_SB.GPO0", 0, ResourceConsumer) { 27, 31 } 27b6dff0e1SChangbin Du }) 28b6dff0e1SChangbin Du 29b6dff0e1SChangbin Du Name (_DSD, Package () 30b6dff0e1SChangbin Du { 31b6dff0e1SChangbin Du ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), 32b6dff0e1SChangbin Du Package () 33b6dff0e1SChangbin Du { 34b6dff0e1SChangbin Du Package () { "reset-gpios", Package () { ^BTH, 1, 1, 0 } }, 35b6dff0e1SChangbin Du Package () { "shutdown-gpios", Package () { ^BTH, 0, 0, 0 } }, 36b6dff0e1SChangbin Du } 37b6dff0e1SChangbin Du }) 38b6dff0e1SChangbin Du } 39b6dff0e1SChangbin Du 40b6dff0e1SChangbin DuThe format of the supported GPIO property is:: 41b6dff0e1SChangbin Du 42b6dff0e1SChangbin Du Package () { "name", Package () { ref, index, pin, active_low }} 43b6dff0e1SChangbin Du 44b6dff0e1SChangbin Duref 45b6dff0e1SChangbin Du The device that has _CRS containing GpioIo()/GpioInt() resources, 46b6dff0e1SChangbin Du typically this is the device itself (BTH in our case). 47b6dff0e1SChangbin Duindex 48b6dff0e1SChangbin Du Index of the GpioIo()/GpioInt() resource in _CRS starting from zero. 49b6dff0e1SChangbin Dupin 50b6dff0e1SChangbin Du Pin in the GpioIo()/GpioInt() resource. Typically this is zero. 51b6dff0e1SChangbin Duactive_low 521bd33879SAndy Shevchenko If 1, the GPIO is marked as active_low. 53b6dff0e1SChangbin Du 54b6dff0e1SChangbin DuSince ACPI GpioIo() resource does not have a field saying whether it is 55b6dff0e1SChangbin Duactive low or high, the "active_low" argument can be used here. Setting 56b6dff0e1SChangbin Duit to 1 marks the GPIO as active low. 57b6dff0e1SChangbin Du 580d6c41cfSAndy ShevchenkoNote, active_low in _DSD does not make sense for GpioInt() resource and 590d6c41cfSAndy Shevchenkomust be 0. GpioInt() resource has its own means of defining it. 600d6c41cfSAndy Shevchenko 61b6dff0e1SChangbin DuIn our Bluetooth example the "reset-gpios" refers to the second GpioIo() 62b6dff0e1SChangbin Duresource, second pin in that resource with the GPIO number of 31. 63b6dff0e1SChangbin Du 648b31e972SAndy ShevchenkoThe GpioIo() resource unfortunately doesn't explicitly provide an initial 658b31e972SAndy Shevchenkostate of the output pin which driver should use during its initialization. 668b31e972SAndy Shevchenko 678b31e972SAndy ShevchenkoLinux tries to use common sense here and derives the state from the bias 688b31e972SAndy Shevchenkoand polarity settings. The table below shows the expectations: 698b31e972SAndy Shevchenko 70*3082d7efSAndy Shevchenko+-------------+-------------+-----------------------------------------------+ 71*3082d7efSAndy Shevchenko| Pull Bias | Polarity | Requested... | 72*3082d7efSAndy Shevchenko+=============+=============+===============================================+ 73*3082d7efSAndy Shevchenko| Implicit | 74*3082d7efSAndy Shevchenko+-------------+-------------+-----------------------------------------------+ 75*3082d7efSAndy Shevchenko| **Default** | x | AS IS (assumed firmware configured it for us) | 76*3082d7efSAndy Shevchenko+-------------+-------------+-----------------------------------------------+ 77*3082d7efSAndy Shevchenko| Explicit | 78*3082d7efSAndy Shevchenko+-------------+-------------+-----------------------------------------------+ 79*3082d7efSAndy Shevchenko| **None** | x | AS IS (assumed firmware configured it for us) | 80*3082d7efSAndy Shevchenko| | | with no Pull Bias | 81*3082d7efSAndy Shevchenko+-------------+-------------+-----------------------------------------------+ 82*3082d7efSAndy Shevchenko| **Up** | x (no _DSD) | | 83*3082d7efSAndy Shevchenko| +-------------+ as high, assuming non-active | 84*3082d7efSAndy Shevchenko| | Low | | 85*3082d7efSAndy Shevchenko| +-------------+-----------------------------------------------+ 86*3082d7efSAndy Shevchenko| | High | as high, assuming active | 87*3082d7efSAndy Shevchenko+-------------+-------------+-----------------------------------------------+ 88*3082d7efSAndy Shevchenko| **Down** | x (no _DSD) | | 89*3082d7efSAndy Shevchenko| +-------------+ as low, assuming non-active | 90*3082d7efSAndy Shevchenko| | High | | 91*3082d7efSAndy Shevchenko| +-------------+-----------------------------------------------+ 92*3082d7efSAndy Shevchenko| | Low | as low, assuming active | 93*3082d7efSAndy Shevchenko+-------------+-------------+-----------------------------------------------+ 948b31e972SAndy Shevchenko 958b31e972SAndy ShevchenkoThat said, for our above example the both GPIOs, since the bias setting 968b31e972SAndy Shevchenkois explicit and _DSD is present, will be treated as active with a high 978b31e972SAndy Shevchenkopolarity and Linux will configure the pins in this state until a driver 988b31e972SAndy Shevchenkoreprograms them differently. 998b31e972SAndy Shevchenko 100b6dff0e1SChangbin DuIt is possible to leave holes in the array of GPIOs. This is useful in 101b6dff0e1SChangbin Ducases like with SPI host controllers where some chip selects may be 102b6dff0e1SChangbin Duimplemented as GPIOs and some as native signals. For example a SPI host 103b6dff0e1SChangbin Ducontroller can have chip selects 0 and 2 implemented as GPIOs and 1 as 104b6dff0e1SChangbin Dunative:: 105b6dff0e1SChangbin Du 106b6dff0e1SChangbin Du Package () { 107b6dff0e1SChangbin Du "cs-gpios", 108b6dff0e1SChangbin Du Package () { 109b6dff0e1SChangbin Du ^GPIO, 19, 0, 0, // chip select 0: GPIO 110b6dff0e1SChangbin Du 0, // chip select 1: native signal 111b6dff0e1SChangbin Du ^GPIO, 20, 0, 0, // chip select 2: GPIO 112b6dff0e1SChangbin Du } 113b6dff0e1SChangbin Du } 114b6dff0e1SChangbin Du 115ec3576eaSAndy ShevchenkoNote, that historically ACPI has no means of the GPIO polarity and thus 116ec3576eaSAndy Shevchenkothe SPISerialBus() resource defines it on the per-chip basis. In order 117ec3576eaSAndy Shevchenkoto avoid a chain of negations, the GPIO polarity is considered being 118ec3576eaSAndy ShevchenkoActive High. Even for the cases when _DSD() is involved (see the example 119ec3576eaSAndy Shevchenkoabove) the GPIO CS polarity must be defined Active High to avoid ambiguity. 120ec3576eaSAndy Shevchenko 121b6dff0e1SChangbin DuOther supported properties 122b6dff0e1SChangbin Du========================== 123b6dff0e1SChangbin Du 124b6dff0e1SChangbin DuFollowing Device Tree compatible device properties are also supported by 125b6dff0e1SChangbin Du_DSD device properties for GPIO controllers: 126b6dff0e1SChangbin Du 127b6dff0e1SChangbin Du- gpio-hog 128b6dff0e1SChangbin Du- output-high 129b6dff0e1SChangbin Du- output-low 130b6dff0e1SChangbin Du- input 131b6dff0e1SChangbin Du- line-name 132b6dff0e1SChangbin Du 133b6dff0e1SChangbin DuExample:: 134b6dff0e1SChangbin Du 135b6dff0e1SChangbin Du Name (_DSD, Package () { 136b6dff0e1SChangbin Du // _DSD Hierarchical Properties Extension UUID 137b6dff0e1SChangbin Du ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"), 138b6dff0e1SChangbin Du Package () { 139b6dff0e1SChangbin Du Package () { "hog-gpio8", "G8PU" } 140b6dff0e1SChangbin Du } 141b6dff0e1SChangbin Du }) 142b6dff0e1SChangbin Du 143b6dff0e1SChangbin Du Name (G8PU, Package () { 144b6dff0e1SChangbin Du ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), 145b6dff0e1SChangbin Du Package () { 146b6dff0e1SChangbin Du Package () { "gpio-hog", 1 }, 147b6dff0e1SChangbin Du Package () { "gpios", Package () { 8, 0 } }, 148b6dff0e1SChangbin Du Package () { "output-high", 1 }, 149b6dff0e1SChangbin Du Package () { "line-name", "gpio8-pullup" }, 150b6dff0e1SChangbin Du } 151b6dff0e1SChangbin Du }) 152b6dff0e1SChangbin Du 153b6dff0e1SChangbin Du- gpio-line-names 154b6dff0e1SChangbin Du 1554697958bSFlavio SuligoiThe ``gpio-line-names`` declaration is a list of strings ("names"), which 1564697958bSFlavio Suligoidescribes each line/pin of a GPIO controller/expander. This list, contained in 1574697958bSFlavio Suligoia package, must be inserted inside the GPIO controller declaration of an ACPI 1584697958bSFlavio Suligoitable (typically inside the DSDT). The ``gpio-line-names`` list must respect the 1594697958bSFlavio Suligoifollowing rules (see also the examples): 1604697958bSFlavio Suligoi 1614697958bSFlavio Suligoi - the first name in the list corresponds with the first line/pin of the GPIO 1624697958bSFlavio Suligoi controller/expander 1634697958bSFlavio Suligoi - the names inside the list must be consecutive (no "holes" are permitted) 1644697958bSFlavio Suligoi - the list can be incomplete and can end before the last GPIO line: in 1654697958bSFlavio Suligoi other words, it is not mandatory to fill all the GPIO lines 1664697958bSFlavio Suligoi - empty names are allowed (two quotation marks ``""`` correspond to an empty 1674697958bSFlavio Suligoi name) 168731e97e0SFlavio Suligoi - names inside one GPIO controller/expander must be unique 1694697958bSFlavio Suligoi 1704697958bSFlavio SuligoiExample of a GPIO controller of 16 lines, with an incomplete list with two 1714697958bSFlavio Suligoiempty names:: 1724697958bSFlavio Suligoi 1734697958bSFlavio Suligoi Package () { 1744697958bSFlavio Suligoi "gpio-line-names", 1754697958bSFlavio Suligoi Package () { 1764697958bSFlavio Suligoi "pin_0", 1774697958bSFlavio Suligoi "pin_1", 1784697958bSFlavio Suligoi "", 1794697958bSFlavio Suligoi "", 1804697958bSFlavio Suligoi "pin_3", 1814697958bSFlavio Suligoi "pin_4_push_button", 1824697958bSFlavio Suligoi } 1834697958bSFlavio Suligoi } 1844697958bSFlavio Suligoi 1854697958bSFlavio SuligoiAt runtime, the above declaration produces the following result (using the 1864697958bSFlavio Suligoi"libgpiod" tools):: 1874697958bSFlavio Suligoi 1884697958bSFlavio Suligoi root@debian:~# gpioinfo gpiochip4 1894697958bSFlavio Suligoi gpiochip4 - 16 lines: 1904697958bSFlavio Suligoi line 0: "pin_0" unused input active-high 1914697958bSFlavio Suligoi line 1: "pin_1" unused input active-high 1924697958bSFlavio Suligoi line 2: unnamed unused input active-high 1934697958bSFlavio Suligoi line 3: unnamed unused input active-high 1944697958bSFlavio Suligoi line 4: "pin_3" unused input active-high 1954697958bSFlavio Suligoi line 5: "pin_4_push_button" unused input active-high 1964697958bSFlavio Suligoi line 6: unnamed unused input active-high 1974697958bSFlavio Suligoi line 7 unnamed unused input active-high 1984697958bSFlavio Suligoi line 8: unnamed unused input active-high 1994697958bSFlavio Suligoi line 9: unnamed unused input active-high 2004697958bSFlavio Suligoi line 10: unnamed unused input active-high 2014697958bSFlavio Suligoi line 11: unnamed unused input active-high 2024697958bSFlavio Suligoi line 12: unnamed unused input active-high 2034697958bSFlavio Suligoi line 13: unnamed unused input active-high 2044697958bSFlavio Suligoi line 14: unnamed unused input active-high 2054697958bSFlavio Suligoi line 15: unnamed unused input active-high 2064697958bSFlavio Suligoi root@debian:~# gpiofind pin_4_push_button 2074697958bSFlavio Suligoi gpiochip4 5 2084697958bSFlavio Suligoi root@debian:~# 2094697958bSFlavio Suligoi 2104697958bSFlavio SuligoiAnother example:: 211b6dff0e1SChangbin Du 212b6dff0e1SChangbin Du Package () { 213b6dff0e1SChangbin Du "gpio-line-names", 214b6dff0e1SChangbin Du Package () { 2151bd33879SAndy Shevchenko "SPI0_CS_N", "EXP2_INT", "MUX6_IO", "UART0_RXD", 2161bd33879SAndy Shevchenko "MUX7_IO", "LVL_C_A1", "MUX0_IO", "SPI1_MISO", 217b6dff0e1SChangbin Du } 218b6dff0e1SChangbin Du } 219b6dff0e1SChangbin Du 220b6dff0e1SChangbin DuSee Documentation/devicetree/bindings/gpio/gpio.txt for more information 221b6dff0e1SChangbin Duabout these properties. 222b6dff0e1SChangbin Du 223b6dff0e1SChangbin DuACPI GPIO Mappings Provided by Drivers 224b6dff0e1SChangbin Du====================================== 225b6dff0e1SChangbin Du 226b6dff0e1SChangbin DuThere are systems in which the ACPI tables do not contain _DSD but provide _CRS 227b6dff0e1SChangbin Duwith GpioIo()/GpioInt() resources and device drivers still need to work with 228b6dff0e1SChangbin Duthem. 229b6dff0e1SChangbin Du 230b6dff0e1SChangbin DuIn those cases ACPI device identification objects, _HID, _CID, _CLS, _SUB, _HRV, 231b6dff0e1SChangbin Duavailable to the driver can be used to identify the device and that is supposed 232b6dff0e1SChangbin Duto be sufficient to determine the meaning and purpose of all of the GPIO lines 233b6dff0e1SChangbin Dulisted by the GpioIo()/GpioInt() resources returned by _CRS. In other words, 234b6dff0e1SChangbin Duthe driver is supposed to know what to use the GpioIo()/GpioInt() resources for 235b6dff0e1SChangbin Duonce it has identified the device. Having done that, it can simply assign names 236b6dff0e1SChangbin Duto the GPIO lines it is going to use and provide the GPIO subsystem with a 237b6dff0e1SChangbin Dumapping between those names and the ACPI GPIO resources corresponding to them. 238b6dff0e1SChangbin Du 239b6dff0e1SChangbin DuTo do that, the driver needs to define a mapping table as a NULL-terminated 2401bd33879SAndy Shevchenkoarray of struct acpi_gpio_mapping objects that each contains a name, a pointer 241b6dff0e1SChangbin Duto an array of line data (struct acpi_gpio_params) objects and the size of that 242b6dff0e1SChangbin Duarray. Each struct acpi_gpio_params object consists of three fields, 243b6dff0e1SChangbin Ducrs_entry_index, line_index, active_low, representing the index of the target 244b6dff0e1SChangbin DuGpioIo()/GpioInt() resource in _CRS starting from zero, the index of the target 245b6dff0e1SChangbin Duline in that resource starting from zero, and the active-low flag for that line, 246b6dff0e1SChangbin Durespectively, in analogy with the _DSD GPIO property format specified above. 247b6dff0e1SChangbin Du 248b6dff0e1SChangbin DuFor the example Bluetooth device discussed previously the data structures in 249b6dff0e1SChangbin Duquestion would look like this:: 250b6dff0e1SChangbin Du 251b6dff0e1SChangbin Du static const struct acpi_gpio_params reset_gpio = { 1, 1, false }; 252b6dff0e1SChangbin Du static const struct acpi_gpio_params shutdown_gpio = { 0, 0, false }; 253b6dff0e1SChangbin Du 254b6dff0e1SChangbin Du static const struct acpi_gpio_mapping bluetooth_acpi_gpios[] = { 255b6dff0e1SChangbin Du { "reset-gpios", &reset_gpio, 1 }, 256b6dff0e1SChangbin Du { "shutdown-gpios", &shutdown_gpio, 1 }, 2571bd33879SAndy Shevchenko { } 258b6dff0e1SChangbin Du }; 259b6dff0e1SChangbin Du 260b6dff0e1SChangbin DuNext, the mapping table needs to be passed as the second argument to 2611bd33879SAndy Shevchenkoacpi_dev_add_driver_gpios() or its managed analogue that will 2621bd33879SAndy Shevchenkoregister it with the ACPI device object pointed to by its first 2631bd33879SAndy Shevchenkoargument. That should be done in the driver's .probe() routine. 2641bd33879SAndy ShevchenkoOn removal, the driver should unregister its GPIO mapping table by 265b6dff0e1SChangbin Ducalling acpi_dev_remove_driver_gpios() on the ACPI device object where that 266b6dff0e1SChangbin Dutable was previously registered. 267b6dff0e1SChangbin Du 268b6dff0e1SChangbin DuUsing the _CRS fallback 269b6dff0e1SChangbin Du======================= 270b6dff0e1SChangbin Du 271b6dff0e1SChangbin DuIf a device does not have _DSD or the driver does not create ACPI GPIO 272b6dff0e1SChangbin Dumapping, the Linux GPIO framework refuses to return any GPIOs. This is 273b6dff0e1SChangbin Dubecause the driver does not know what it actually gets. For example if we 274b6dff0e1SChangbin Duhave a device like below:: 275b6dff0e1SChangbin Du 276b6dff0e1SChangbin Du Device (BTH) 277b6dff0e1SChangbin Du { 278b6dff0e1SChangbin Du Name (_HID, ...) 279b6dff0e1SChangbin Du 280b6dff0e1SChangbin Du Name (_CRS, ResourceTemplate () { 281b6dff0e1SChangbin Du GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone, 282b6dff0e1SChangbin Du "\\_SB.GPO0", 0, ResourceConsumer) { 15 } 283b6dff0e1SChangbin Du GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone, 284b6dff0e1SChangbin Du "\\_SB.GPO0", 0, ResourceConsumer) { 27 } 285b6dff0e1SChangbin Du }) 286b6dff0e1SChangbin Du } 287b6dff0e1SChangbin Du 288b6dff0e1SChangbin DuThe driver might expect to get the right GPIO when it does:: 289b6dff0e1SChangbin Du 290b6dff0e1SChangbin Du desc = gpiod_get(dev, "reset", GPIOD_OUT_LOW); 291830751d5SAndy Shevchenko if (IS_ERR(desc)) 292830751d5SAndy Shevchenko ...error handling... 293b6dff0e1SChangbin Du 294b6dff0e1SChangbin Dubut since there is no way to know the mapping between "reset" and 295b6dff0e1SChangbin Duthe GpioIo() in _CRS desc will hold ERR_PTR(-ENOENT). 296b6dff0e1SChangbin Du 2971bd33879SAndy ShevchenkoThe driver author can solve this by passing the mapping explicitly 2981bd33879SAndy Shevchenko(this is the recommended way and it's documented in the above chapter). 299b6dff0e1SChangbin Du 300b6dff0e1SChangbin DuThe ACPI GPIO mapping tables should not contaminate drivers that are not 301b6dff0e1SChangbin Duknowing about which exact device they are servicing on. It implies that 3021bd33879SAndy Shevchenkothe ACPI GPIO mapping tables are hardly linked to an ACPI ID and certain 303b6dff0e1SChangbin Duobjects, as listed in the above chapter, of the device in question. 304b6dff0e1SChangbin Du 305b6dff0e1SChangbin DuGetting GPIO descriptor 306b6dff0e1SChangbin Du======================= 307b6dff0e1SChangbin Du 308b6dff0e1SChangbin DuThere are two main approaches to get GPIO resource from ACPI:: 309b6dff0e1SChangbin Du 310b6dff0e1SChangbin Du desc = gpiod_get(dev, connection_id, flags); 311b6dff0e1SChangbin Du desc = gpiod_get_index(dev, connection_id, index, flags); 312b6dff0e1SChangbin Du 313b6dff0e1SChangbin DuWe may consider two different cases here, i.e. when connection ID is 314b6dff0e1SChangbin Duprovided and otherwise. 315b6dff0e1SChangbin Du 316b6dff0e1SChangbin DuCase 1:: 317b6dff0e1SChangbin Du 318b6dff0e1SChangbin Du desc = gpiod_get(dev, "non-null-connection-id", flags); 319b6dff0e1SChangbin Du desc = gpiod_get_index(dev, "non-null-connection-id", index, flags); 320b6dff0e1SChangbin Du 321b6dff0e1SChangbin DuCase 2:: 322b6dff0e1SChangbin Du 323b6dff0e1SChangbin Du desc = gpiod_get(dev, NULL, flags); 324b6dff0e1SChangbin Du desc = gpiod_get_index(dev, NULL, index, flags); 325b6dff0e1SChangbin Du 326b6dff0e1SChangbin DuCase 1 assumes that corresponding ACPI device description must have 327b6dff0e1SChangbin Dudefined device properties and will prevent to getting any GPIO resources 328b6dff0e1SChangbin Duotherwise. 329b6dff0e1SChangbin Du 330b6dff0e1SChangbin DuCase 2 explicitly tells GPIO core to look for resources in _CRS. 331b6dff0e1SChangbin Du 332b6dff0e1SChangbin DuBe aware that gpiod_get_index() in cases 1 and 2, assuming that there 333b6dff0e1SChangbin Duare two versions of ACPI device description provided and no mapping is 334b6dff0e1SChangbin Dupresent in the driver, will return different resources. That's why a 3351bd33879SAndy Shevchenkocertain driver has to handle them carefully as explained in the previous 336b6dff0e1SChangbin Duchapter. 337