gpio-properties.rst 13 KB

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