board.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. =============
  2. GPIO Mappings
  3. =============
  4. This document explains how GPIOs can be assigned to given devices and functions.
  5. All platforms can enable the GPIO library, but if the platform strictly
  6. requires GPIO functionality to be present, it needs to select GPIOLIB from its
  7. Kconfig. Then, how GPIOs are mapped depends on what the platform uses to
  8. describe its hardware layout. Currently, mappings can be defined through device
  9. tree, ACPI, and platform data.
  10. Device Tree
  11. -----------
  12. GPIOs can easily be mapped to devices and functions in the device tree. The
  13. exact way to do it depends on the GPIO controller providing the GPIOs, see the
  14. device tree bindings for your controller.
  15. GPIOs mappings are defined in the consumer device's node, in a property named
  16. <function>-gpios, where <function> is the function the driver will request
  17. through gpiod_get(). For example::
  18. foo_device {
  19. compatible = "acme,foo";
  20. ...
  21. led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */
  22. <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */
  23. <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */
  24. power-gpios = <&gpio 1 GPIO_ACTIVE_LOW>;
  25. };
  26. Properties named <function>-gpio are also considered valid and old bindings use
  27. it but are only supported for compatibility reasons and should not be used for
  28. newer bindings since it has been deprecated.
  29. This property will make GPIOs 15, 16 and 17 available to the driver under the
  30. "led" function, and GPIO 1 as the "power" GPIO::
  31. struct gpio_desc *red, *green, *blue, *power;
  32. red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH);
  33. green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH);
  34. blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH);
  35. power = gpiod_get(dev, "power", GPIOD_OUT_HIGH);
  36. The led GPIOs will be active high, while the power GPIO will be active low (i.e.
  37. gpiod_is_active_low(power) will be true).
  38. The second parameter of the gpiod_get() functions, the con_id string, has to be
  39. the <function>-prefix of the GPIO suffixes ("gpios" or "gpio", automatically
  40. looked up by the gpiod functions internally) used in the device tree. With above
  41. "led-gpios" example, use the prefix without the "-" as con_id parameter: "led".
  42. Internally, the GPIO subsystem prefixes the GPIO suffix ("gpios" or "gpio")
  43. with the string passed in con_id to get the resulting string
  44. (``snprintf(... "%s-%s", con_id, gpio_suffixes[]``).
  45. ACPI
  46. ----
  47. ACPI also supports function names for GPIOs in a similar fashion to DT.
  48. The above DT example can be converted to an equivalent ACPI description
  49. with the help of _DSD (Device Specific Data), introduced in ACPI 5.1::
  50. Device (FOO) {
  51. Name (_CRS, ResourceTemplate () {
  52. GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
  53. "\\_SB.GPI0", 0, ResourceConsumer) { 15 } // red
  54. GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
  55. "\\_SB.GPI0", 0, ResourceConsumer) { 16 } // green
  56. GpioIo (Exclusive, PullUp, 0, 0, IoRestrictionOutputOnly,
  57. "\\_SB.GPI0", 0, ResourceConsumer) { 17 } // blue
  58. GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionOutputOnly,
  59. "\\_SB.GPI0", 0, ResourceConsumer) { 1 } // power
  60. })
  61. Name (_DSD, Package () {
  62. ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
  63. Package () {
  64. Package () {
  65. "led-gpios",
  66. Package () {
  67. ^FOO, 0, 0, 1,
  68. ^FOO, 1, 0, 1,
  69. ^FOO, 2, 0, 1,
  70. }
  71. },
  72. Package () { "power-gpios", Package () { ^FOO, 3, 0, 0 } },
  73. }
  74. })
  75. }
  76. For more information about the ACPI GPIO bindings see
  77. Documentation/firmware-guide/acpi/gpio-properties.rst.
  78. Software Nodes
  79. --------------
  80. Software nodes allow board-specific code to construct an in-memory,
  81. device-tree-like structure using struct software_node and struct
  82. property_entry. This structure can then be associated with a platform device,
  83. allowing drivers to use the standard device properties API to query
  84. configuration, just as they would on an ACPI or device tree system.
  85. Software-node-backed GPIOs are described using the ``PROPERTY_ENTRY_GPIO()``
  86. macro, which ties a software node representing the GPIO controller with
  87. consumer device. It allows consumers to use regular gpiolib APIs, such as
  88. gpiod_get(), gpiod_get_optional().
  89. The software node representing a GPIO controller need not be attached to the
  90. GPIO controller device. The only requirement is that the node must be
  91. registered and its name must match the GPIO controller's label.
  92. For example, here is how to describe a single GPIO-connected LED. This is an
  93. alternative to using platform_data on legacy systems.
  94. .. code-block:: c
  95. #include <linux/property.h>
  96. #include <linux/gpio/machine.h>
  97. #include <linux/gpio/property.h>
  98. /*
  99. * 1. Define a node for the GPIO controller. Its .name must match the
  100. * controller's label.
  101. */
  102. static const struct software_node gpio_controller_node = {
  103. .name = "gpio-foo",
  104. };
  105. /* 2. Define the properties for the LED device. */
  106. static const struct property_entry led_device_props[] = {
  107. PROPERTY_ENTRY_STRING("label", "myboard:green:status"),
  108. PROPERTY_ENTRY_STRING("linux,default-trigger", "heartbeat"),
  109. PROPERTY_ENTRY_GPIO("gpios", &gpio_controller_node, 42, GPIO_ACTIVE_HIGH),
  110. { }
  111. };
  112. /* 3. Define the software node for the LED device. */
  113. static const struct software_node led_device_swnode = {
  114. .name = "status-led",
  115. .properties = led_device_props,
  116. };
  117. /*
  118. * 4. Register the software nodes and the platform device.
  119. */
  120. const struct software_node *swnodes[] = {
  121. &gpio_controller_node,
  122. &led_device_swnode,
  123. NULL
  124. };
  125. software_node_register_node_group(swnodes);
  126. // Then register a platform_device for "leds-gpio" and associate
  127. // it with &led_device_swnode via .fwnode.
  128. For a complete guide on converting board files to use software nodes, see
  129. Documentation/driver-api/gpio/legacy-boards.rst.
  130. Platform Data
  131. -------------
  132. Finally, GPIOs can be bound to devices and functions using platform data. Board
  133. files that desire to do so need to include the following header::
  134. #include <linux/gpio/machine.h>
  135. GPIOs are mapped by the means of tables of lookups, containing instances of the
  136. gpiod_lookup structure. Two macros are defined to help declaring such mappings::
  137. GPIO_LOOKUP(key, chip_hwnum, con_id, flags)
  138. GPIO_LOOKUP_IDX(key, chip_hwnum, con_id, idx, flags)
  139. where
  140. - key is either the label of the gpiod_chip instance providing the GPIO, or
  141. the GPIO line name
  142. - chip_hwnum is the hardware number of the GPIO within the chip, or U16_MAX
  143. to indicate that key is a GPIO line name
  144. - con_id is the name of the GPIO function from the device point of view. It
  145. can be NULL, in which case it will match any function.
  146. - idx is the index of the GPIO within the function.
  147. - flags is defined to specify the following properties:
  148. * GPIO_ACTIVE_HIGH - GPIO line is active high
  149. * GPIO_ACTIVE_LOW - GPIO line is active low
  150. * GPIO_OPEN_DRAIN - GPIO line is set up as open drain
  151. * GPIO_OPEN_SOURCE - GPIO line is set up as open source
  152. * GPIO_PERSISTENT - GPIO line is persistent during
  153. suspend/resume and maintains its value
  154. * GPIO_TRANSITORY - GPIO line is transitory and may loose its
  155. electrical state during suspend/resume
  156. In the future, these flags might be extended to support more properties.
  157. Note that:
  158. 1. GPIO line names are not guaranteed to be globally unique, so the first
  159. match found will be used.
  160. 2. GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0.
  161. A lookup table can then be defined as follows, with an empty entry defining its
  162. end. The 'dev_id' field of the table is the identifier of the device that will
  163. make use of these GPIOs. It can be NULL, in which case it will be matched for
  164. calls to gpiod_get() with a NULL device.
  165. .. code-block:: c
  166. struct gpiod_lookup_table gpios_table = {
  167. .dev_id = "foo.0",
  168. .table = {
  169. GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),
  170. GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH),
  171. GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH),
  172. GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW),
  173. { },
  174. },
  175. };
  176. And the table can be added by the board code as follows::
  177. gpiod_add_lookup_table(&gpios_table);
  178. The driver controlling "foo.0" will then be able to obtain its GPIOs as follows::
  179. struct gpio_desc *red, *green, *blue, *power;
  180. red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH);
  181. green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH);
  182. blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH);
  183. power = gpiod_get(dev, "power", GPIOD_OUT_HIGH);
  184. Since the "led" GPIOs are mapped as active-high, this example will switch their
  185. signals to 1, i.e. enabling the LEDs. And for the "power" GPIO, which is mapped
  186. as active-low, its actual signal will be 0 after this code. Contrary to the
  187. legacy integer GPIO interface, the active-low property is handled during
  188. mapping and is thus transparent to GPIO consumers.
  189. A set of functions such as gpiod_set_value() is available to work with
  190. the new descriptor-oriented interface.
  191. Boards using platform data can also hog GPIO lines by defining GPIO hog tables.
  192. .. code-block:: c
  193. struct gpiod_hog gpio_hog_table[] = {
  194. GPIO_HOG("gpio.0", 10, "foo", GPIO_ACTIVE_LOW, GPIOD_OUT_HIGH),
  195. { }
  196. };
  197. And the table can be added to the board code as follows::
  198. gpiod_add_hogs(gpio_hog_table);
  199. The line will be hogged as soon as the gpiochip is created or - in case the
  200. chip was created earlier - when the hog table is registered.
  201. Arrays of pins
  202. --------------
  203. In addition to requesting pins belonging to a function one by one, a device may
  204. also request an array of pins assigned to the function. The way those pins are
  205. mapped to the device determines if the array qualifies for fast bitmap
  206. processing. If yes, a bitmap is passed over get/set array functions directly
  207. between a caller and a respective .get/set_multiple() callback of a GPIO chip.
  208. In order to qualify for fast bitmap processing, the array must meet the
  209. following requirements:
  210. - pin hardware number of array member 0 must also be 0,
  211. - pin hardware numbers of consecutive array members which belong to the same
  212. chip as member 0 does must also match their array indexes.
  213. Otherwise fast bitmap processing path is not used in order to avoid consecutive
  214. pins which belong to the same chip but are not in hardware order being processed
  215. separately.
  216. If the array applies for fast bitmap processing path, pins which belong to
  217. different chips than member 0 does, as well as those with indexes different from
  218. their hardware pin numbers, are excluded from the fast path, both input and
  219. output. Moreover, open drain and open source pins are excluded from fast bitmap
  220. output processing.