Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ======================================
0004 _DSD Device Properties Related to GPIO
0005 ======================================
0006 
0007 With the release of ACPI 5.1, the _DSD configuration object finally
0008 allows names to be given to GPIOs (and other things as well) returned
0009 by _CRS.  Previously, we were only able to use an integer index to find
0010 the corresponding GPIO, which is pretty error prone (it depends on
0011 the _CRS output ordering, for example).
0012 
0013 With _DSD we can now query GPIOs using a name instead of an integer
0014 index, like the ASL example below shows::
0015 
0016   // Bluetooth device with reset and shutdown GPIOs
0017   Device (BTH)
0018   {
0019       Name (_HID, ...)
0020 
0021       Name (_CRS, ResourceTemplate ()
0022       {
0023           GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
0024                   "\\_SB.GPO0", 0, ResourceConsumer) { 15 }
0025           GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
0026                   "\\_SB.GPO0", 0, ResourceConsumer) { 27, 31 }
0027       })
0028 
0029       Name (_DSD, Package ()
0030       {
0031           ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
0032           Package ()
0033           {
0034               Package () { "reset-gpios", Package () { ^BTH, 1, 1, 0 } },
0035               Package () { "shutdown-gpios", Package () { ^BTH, 0, 0, 0 } },
0036           }
0037       })
0038   }
0039 
0040 The format of the supported GPIO property is::
0041 
0042   Package () { "name", Package () { ref, index, pin, active_low }}
0043 
0044 ref
0045   The device that has _CRS containing GpioIo()/GpioInt() resources,
0046   typically this is the device itself (BTH in our case).
0047 index
0048   Index of the GpioIo()/GpioInt() resource in _CRS starting from zero.
0049 pin
0050   Pin in the GpioIo()/GpioInt() resource. Typically this is zero.
0051 active_low
0052   If 1, the GPIO is marked as active_low.
0053 
0054 Since ACPI GpioIo() resource does not have a field saying whether it is
0055 active low or high, the "active_low" argument can be used here.  Setting
0056 it to 1 marks the GPIO as active low.
0057 
0058 Note, active_low in _DSD does not make sense for GpioInt() resource and
0059 must be 0. GpioInt() resource has its own means of defining it.
0060 
0061 In our Bluetooth example the "reset-gpios" refers to the second GpioIo()
0062 resource, second pin in that resource with the GPIO number of 31.
0063 
0064 The GpioIo() resource unfortunately doesn't explicitly provide an initial
0065 state of the output pin which driver should use during its initialization.
0066 
0067 Linux tries to use common sense here and derives the state from the bias
0068 and polarity settings. The table below shows the expectations:
0069 
0070 =========  =============  ==============
0071 Pull Bias     Polarity     Requested...
0072 =========  =============  ==============
0073 Implicit     x            AS IS (assumed firmware configured for us)
0074 Explicit     x (no _DSD)  as Pull Bias (Up == High, Down == Low),
0075                           assuming non-active (Polarity = !Pull Bias)
0076 Down         Low          as low, assuming active
0077 Down         High         as low, assuming non-active
0078 Up           Low          as high, assuming non-active
0079 Up           High         as high, assuming active
0080 =========  =============  ==============
0081 
0082 That said, for our above example the both GPIOs, since the bias setting
0083 is explicit and _DSD is present, will be treated as active with a high
0084 polarity and Linux will configure the pins in this state until a driver
0085 reprograms them differently.
0086 
0087 It is possible to leave holes in the array of GPIOs. This is useful in
0088 cases like with SPI host controllers where some chip selects may be
0089 implemented as GPIOs and some as native signals. For example a SPI host
0090 controller can have chip selects 0 and 2 implemented as GPIOs and 1 as
0091 native::
0092 
0093   Package () {
0094       "cs-gpios",
0095       Package () {
0096           ^GPIO, 19, 0, 0, // chip select 0: GPIO
0097           0,               // chip select 1: native signal
0098           ^GPIO, 20, 0, 0, // chip select 2: GPIO
0099       }
0100   }
0101 
0102 Note, that historically ACPI has no means of the GPIO polarity and thus
0103 the SPISerialBus() resource defines it on the per-chip basis. In order
0104 to avoid a chain of negations, the GPIO polarity is considered being
0105 Active High. Even for the cases when _DSD() is involved (see the example
0106 above) the GPIO CS polarity must be defined Active High to avoid ambiguity.
0107 
0108 Other supported properties
0109 ==========================
0110 
0111 Following Device Tree compatible device properties are also supported by
0112 _DSD device properties for GPIO controllers:
0113 
0114 - gpio-hog
0115 - output-high
0116 - output-low
0117 - input
0118 - line-name
0119 
0120 Example::
0121 
0122   Name (_DSD, Package () {
0123       // _DSD Hierarchical Properties Extension UUID
0124       ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
0125       Package () {
0126           Package () { "hog-gpio8", "G8PU" }
0127       }
0128   })
0129 
0130   Name (G8PU, Package () {
0131       ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
0132       Package () {
0133           Package () { "gpio-hog", 1 },
0134           Package () { "gpios", Package () { 8, 0 } },
0135           Package () { "output-high", 1 },
0136           Package () { "line-name", "gpio8-pullup" },
0137       }
0138   })
0139 
0140 - gpio-line-names
0141 
0142 The ``gpio-line-names`` declaration is a list of strings ("names"), which
0143 describes each line/pin of a GPIO controller/expander. This list, contained in
0144 a package, must be inserted inside the GPIO controller declaration of an ACPI
0145 table (typically inside the DSDT). The ``gpio-line-names`` list must respect the
0146 following rules (see also the examples):
0147 
0148   - the first name in the list corresponds with the first line/pin of the GPIO
0149     controller/expander
0150   - the names inside the list must be consecutive (no "holes" are permitted)
0151   - the list can be incomplete and can end before the last GPIO line: in
0152     other words, it is not mandatory to fill all the GPIO lines
0153   - empty names are allowed (two quotation marks ``""`` correspond to an empty
0154     name)
0155   - names inside one GPIO controller/expander must be unique
0156 
0157 Example of a GPIO controller of 16 lines, with an incomplete list with two
0158 empty names::
0159 
0160   Package () {
0161       "gpio-line-names",
0162       Package () {
0163           "pin_0",
0164           "pin_1",
0165           "",
0166           "",
0167           "pin_3",
0168           "pin_4_push_button",
0169       }
0170   }
0171 
0172 At runtime, the above declaration produces the following result (using the
0173 "libgpiod" tools)::
0174 
0175   root@debian:~# gpioinfo gpiochip4
0176   gpiochip4 - 16 lines:
0177           line   0:      "pin_0"       unused   input  active-high
0178           line   1:      "pin_1"       unused   input  active-high
0179           line   2:      unnamed       unused   input  active-high
0180           line   3:      unnamed       unused   input  active-high
0181           line   4:      "pin_3"       unused   input  active-high
0182           line   5: "pin_4_push_button" unused input active-high
0183           line   6:      unnamed       unused   input  active-high
0184           line   7       unnamed       unused   input  active-high
0185           line   8:      unnamed       unused   input  active-high
0186           line   9:      unnamed       unused   input  active-high
0187           line  10:      unnamed       unused   input  active-high
0188           line  11:      unnamed       unused   input  active-high
0189           line  12:      unnamed       unused   input  active-high
0190           line  13:      unnamed       unused   input  active-high
0191           line  14:      unnamed       unused   input  active-high
0192           line  15:      unnamed       unused   input  active-high
0193   root@debian:~# gpiofind pin_4_push_button
0194   gpiochip4 5
0195   root@debian:~#
0196 
0197 Another example::
0198 
0199   Package () {
0200       "gpio-line-names",
0201       Package () {
0202           "SPI0_CS_N", "EXP2_INT", "MUX6_IO", "UART0_RXD",
0203           "MUX7_IO", "LVL_C_A1", "MUX0_IO", "SPI1_MISO",
0204       }
0205   }
0206 
0207 See Documentation/devicetree/bindings/gpio/gpio.txt for more information
0208 about these properties.
0209 
0210 ACPI GPIO Mappings Provided by Drivers
0211 ======================================
0212 
0213 There are systems in which the ACPI tables do not contain _DSD but provide _CRS
0214 with GpioIo()/GpioInt() resources and device drivers still need to work with
0215 them.
0216 
0217 In those cases ACPI device identification objects, _HID, _CID, _CLS, _SUB, _HRV,
0218 available to the driver can be used to identify the device and that is supposed
0219 to be sufficient to determine the meaning and purpose of all of the GPIO lines
0220 listed by the GpioIo()/GpioInt() resources returned by _CRS.  In other words,
0221 the driver is supposed to know what to use the GpioIo()/GpioInt() resources for
0222 once it has identified the device.  Having done that, it can simply assign names
0223 to the GPIO lines it is going to use and provide the GPIO subsystem with a
0224 mapping between those names and the ACPI GPIO resources corresponding to them.
0225 
0226 To do that, the driver needs to define a mapping table as a NULL-terminated
0227 array of struct acpi_gpio_mapping objects that each contains a name, a pointer
0228 to an array of line data (struct acpi_gpio_params) objects and the size of that
0229 array.  Each struct acpi_gpio_params object consists of three fields,
0230 crs_entry_index, line_index, active_low, representing the index of the target
0231 GpioIo()/GpioInt() resource in _CRS starting from zero, the index of the target
0232 line in that resource starting from zero, and the active-low flag for that line,
0233 respectively, in analogy with the _DSD GPIO property format specified above.
0234 
0235 For the example Bluetooth device discussed previously the data structures in
0236 question would look like this::
0237 
0238   static const struct acpi_gpio_params reset_gpio = { 1, 1, false };
0239   static const struct acpi_gpio_params shutdown_gpio = { 0, 0, false };
0240 
0241   static const struct acpi_gpio_mapping bluetooth_acpi_gpios[] = {
0242     { "reset-gpios", &reset_gpio, 1 },
0243     { "shutdown-gpios", &shutdown_gpio, 1 },
0244     { }
0245   };
0246 
0247 Next, the mapping table needs to be passed as the second argument to
0248 acpi_dev_add_driver_gpios() or its managed analogue that will
0249 register it with the ACPI device object pointed to by its first
0250 argument. That should be done in the driver's .probe() routine.
0251 On removal, the driver should unregister its GPIO mapping table by
0252 calling acpi_dev_remove_driver_gpios() on the ACPI device object where that
0253 table was previously registered.
0254 
0255 Using the _CRS fallback
0256 =======================
0257 
0258 If a device does not have _DSD or the driver does not create ACPI GPIO
0259 mapping, the Linux GPIO framework refuses to return any GPIOs. This is
0260 because the driver does not know what it actually gets. For example if we
0261 have a device like below::
0262 
0263   Device (BTH)
0264   {
0265       Name (_HID, ...)
0266 
0267       Name (_CRS, ResourceTemplate () {
0268           GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone,
0269                   "\\_SB.GPO0", 0, ResourceConsumer) { 15 }
0270           GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone,
0271                   "\\_SB.GPO0", 0, ResourceConsumer) { 27 }
0272       })
0273   }
0274 
0275 The driver might expect to get the right GPIO when it does::
0276 
0277   desc = gpiod_get(dev, "reset", GPIOD_OUT_LOW);
0278   if (IS_ERR(desc))
0279         ...error handling...
0280 
0281 but since there is no way to know the mapping between "reset" and
0282 the GpioIo() in _CRS desc will hold ERR_PTR(-ENOENT).
0283 
0284 The driver author can solve this by passing the mapping explicitly
0285 (this is the recommended way and it's documented in the above chapter).
0286 
0287 The ACPI GPIO mapping tables should not contaminate drivers that are not
0288 knowing about which exact device they are servicing on. It implies that
0289 the ACPI GPIO mapping tables are hardly linked to an ACPI ID and certain
0290 objects, as listed in the above chapter, of the device in question.
0291 
0292 Getting GPIO descriptor
0293 =======================
0294 
0295 There are two main approaches to get GPIO resource from ACPI::
0296 
0297   desc = gpiod_get(dev, connection_id, flags);
0298   desc = gpiod_get_index(dev, connection_id, index, flags);
0299 
0300 We may consider two different cases here, i.e. when connection ID is
0301 provided and otherwise.
0302 
0303 Case 1::
0304 
0305   desc = gpiod_get(dev, "non-null-connection-id", flags);
0306   desc = gpiod_get_index(dev, "non-null-connection-id", index, flags);
0307 
0308 Case 2::
0309 
0310   desc = gpiod_get(dev, NULL, flags);
0311   desc = gpiod_get_index(dev, NULL, index, flags);
0312 
0313 Case 1 assumes that corresponding ACPI device description must have
0314 defined device properties and will prevent to getting any GPIO resources
0315 otherwise.
0316 
0317 Case 2 explicitly tells GPIO core to look for resources in _CRS.
0318 
0319 Be aware that gpiod_get_index() in cases 1 and 2, assuming that there
0320 are two versions of ACPI device description provided and no mapping is
0321 present in the driver, will return different resources. That's why a
0322 certain driver has to handle them carefully as explained in the previous
0323 chapter.