1.. SPDX-License-Identifier: GPL-2.0
2
3======================================
4_DSD Device Properties Related to GPIO
5======================================
6
7With the release of ACPI 5.1, the _DSD configuration object finally
8allows names to be given to GPIOs (and other things as well) returned
9by _CRS.  Previously, we were only able to use an integer index to find
10the corresponding GPIO, which is pretty error prone (it depends on
11the _CRS output ordering, for example).
12
13With _DSD we can now query GPIOs using a name instead of an integer
14index, like the ASL example below shows::
15
16  // Bluetooth device with reset and shutdown GPIOs
17  Device (BTH)
18  {
19      Name (_HID, ...)
20
21      Name (_CRS, ResourceTemplate ()
22      {
23          GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
24                  "\\_SB.GPO0", 0, ResourceConsumer) {15}
25          GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
26                  "\\_SB.GPO0", 0, ResourceConsumer) {27, 31}
27      })
28
29      Name (_DSD, Package ()
30      {
31          ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
32          Package ()
33	  {
34              Package () {"reset-gpios", Package() {^BTH, 1, 1, 0 }},
35              Package () {"shutdown-gpios", Package() {^BTH, 0, 0, 0 }},
36          }
37      })
38  }
39
40The format of the supported GPIO property is::
41
42  Package () { "name", Package () { ref, index, pin, active_low }}
43
44ref
45  The device that has _CRS containing GpioIo()/GpioInt() resources,
46  typically this is the device itself (BTH in our case).
47index
48  Index of the GpioIo()/GpioInt() resource in _CRS starting from zero.
49pin
50  Pin in the GpioIo()/GpioInt() resource. Typically this is zero.
51active_low
52  If 1, the GPIO is marked as active_low.
53
54Since ACPI GpioIo() resource does not have a field saying whether it is
55active low or high, the "active_low" argument can be used here.  Setting
56it to 1 marks the GPIO as active low.
57
58Note, active_low in _DSD does not make sense for GpioInt() resource and
59must be 0. GpioInt() resource has its own means of defining it.
60
61In our Bluetooth example the "reset-gpios" refers to the second GpioIo()
62resource, second pin in that resource with the GPIO number of 31.
63
64It is possible to leave holes in the array of GPIOs. This is useful in
65cases like with SPI host controllers where some chip selects may be
66implemented as GPIOs and some as native signals. For example a SPI host
67controller can have chip selects 0 and 2 implemented as GPIOs and 1 as
68native::
69
70  Package () {
71      "cs-gpios",
72      Package () {
73          ^GPIO, 19, 0, 0, // chip select 0: GPIO
74          0,               // chip select 1: native signal
75          ^GPIO, 20, 0, 0, // chip select 2: GPIO
76      }
77  }
78
79Other supported properties
80==========================
81
82Following Device Tree compatible device properties are also supported by
83_DSD device properties for GPIO controllers:
84
85- gpio-hog
86- output-high
87- output-low
88- input
89- line-name
90
91Example::
92
93  Name (_DSD, Package () {
94      // _DSD Hierarchical Properties Extension UUID
95      ToUUID("dbb8e3e6-5886-4ba6-8795-1319f52a966b"),
96      Package () {
97          Package () {"hog-gpio8", "G8PU"}
98      }
99  })
100
101  Name (G8PU, Package () {
102      ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
103      Package () {
104          Package () {"gpio-hog", 1},
105          Package () {"gpios", Package () {8, 0}},
106          Package () {"output-high", 1},
107          Package () {"line-name", "gpio8-pullup"},
108      }
109  })
110
111- gpio-line-names
112
113Example::
114
115  Package () {
116      "gpio-line-names",
117      Package () {
118          "SPI0_CS_N", "EXP2_INT", "MUX6_IO", "UART0_RXD",
119          "MUX7_IO", "LVL_C_A1", "MUX0_IO", "SPI1_MISO",
120      }
121  }
122
123See Documentation/devicetree/bindings/gpio/gpio.txt for more information
124about these properties.
125
126ACPI GPIO Mappings Provided by Drivers
127======================================
128
129There are systems in which the ACPI tables do not contain _DSD but provide _CRS
130with GpioIo()/GpioInt() resources and device drivers still need to work with
131them.
132
133In those cases ACPI device identification objects, _HID, _CID, _CLS, _SUB, _HRV,
134available to the driver can be used to identify the device and that is supposed
135to be sufficient to determine the meaning and purpose of all of the GPIO lines
136listed by the GpioIo()/GpioInt() resources returned by _CRS.  In other words,
137the driver is supposed to know what to use the GpioIo()/GpioInt() resources for
138once it has identified the device.  Having done that, it can simply assign names
139to the GPIO lines it is going to use and provide the GPIO subsystem with a
140mapping between those names and the ACPI GPIO resources corresponding to them.
141
142To do that, the driver needs to define a mapping table as a NULL-terminated
143array of struct acpi_gpio_mapping objects that each contains a name, a pointer
144to an array of line data (struct acpi_gpio_params) objects and the size of that
145array.  Each struct acpi_gpio_params object consists of three fields,
146crs_entry_index, line_index, active_low, representing the index of the target
147GpioIo()/GpioInt() resource in _CRS starting from zero, the index of the target
148line in that resource starting from zero, and the active-low flag for that line,
149respectively, in analogy with the _DSD GPIO property format specified above.
150
151For the example Bluetooth device discussed previously the data structures in
152question would look like this::
153
154  static const struct acpi_gpio_params reset_gpio = { 1, 1, false };
155  static const struct acpi_gpio_params shutdown_gpio = { 0, 0, false };
156
157  static const struct acpi_gpio_mapping bluetooth_acpi_gpios[] = {
158    { "reset-gpios", &reset_gpio, 1 },
159    { "shutdown-gpios", &shutdown_gpio, 1 },
160    { }
161  };
162
163Next, the mapping table needs to be passed as the second argument to
164acpi_dev_add_driver_gpios() or its managed analogue that will
165register it with the ACPI device object pointed to by its first
166argument. That should be done in the driver's .probe() routine.
167On removal, the driver should unregister its GPIO mapping table by
168calling acpi_dev_remove_driver_gpios() on the ACPI device object where that
169table was previously registered.
170
171Using the _CRS fallback
172=======================
173
174If a device does not have _DSD or the driver does not create ACPI GPIO
175mapping, the Linux GPIO framework refuses to return any GPIOs. This is
176because the driver does not know what it actually gets. For example if we
177have a device like below::
178
179  Device (BTH)
180  {
181      Name (_HID, ...)
182
183      Name (_CRS, ResourceTemplate () {
184          GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone,
185                  "\\_SB.GPO0", 0, ResourceConsumer) {15}
186          GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionNone,
187                  "\\_SB.GPO0", 0, ResourceConsumer) {27}
188      })
189  }
190
191The driver might expect to get the right GPIO when it does::
192
193  desc = gpiod_get(dev, "reset", GPIOD_OUT_LOW);
194
195but since there is no way to know the mapping between "reset" and
196the GpioIo() in _CRS desc will hold ERR_PTR(-ENOENT).
197
198The driver author can solve this by passing the mapping explicitly
199(this is the recommended way and it's documented in the above chapter).
200
201The ACPI GPIO mapping tables should not contaminate drivers that are not
202knowing about which exact device they are servicing on. It implies that
203the ACPI GPIO mapping tables are hardly linked to an ACPI ID and certain
204objects, as listed in the above chapter, of the device in question.
205
206Getting GPIO descriptor
207=======================
208
209There are two main approaches to get GPIO resource from ACPI::
210
211  desc = gpiod_get(dev, connection_id, flags);
212  desc = gpiod_get_index(dev, connection_id, index, flags);
213
214We may consider two different cases here, i.e. when connection ID is
215provided and otherwise.
216
217Case 1::
218
219  desc = gpiod_get(dev, "non-null-connection-id", flags);
220  desc = gpiod_get_index(dev, "non-null-connection-id", index, flags);
221
222Case 2::
223
224  desc = gpiod_get(dev, NULL, flags);
225  desc = gpiod_get_index(dev, NULL, index, flags);
226
227Case 1 assumes that corresponding ACPI device description must have
228defined device properties and will prevent to getting any GPIO resources
229otherwise.
230
231Case 2 explicitly tells GPIO core to look for resources in _CRS.
232
233Be aware that gpiod_get_index() in cases 1 and 2, assuming that there
234are two versions of ACPI device description provided and no mapping is
235present in the driver, will return different resources. That's why a
236certain driver has to handle them carefully as explained in the previous
237chapter.
238