board.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. GPIO Mappings
  2. =============
  3. This document explains how GPIOs can be assigned to given devices and functions.
  4. Note that it only applies to the new descriptor-based interface. For a
  5. description of the deprecated integer-based GPIO interface please refer to
  6. gpio-legacy.txt (actually, there is no real mapping possible with the old
  7. interface; you just fetch an integer from somewhere and request the
  8. corresponding GPIO.
  9. Platforms that make use of GPIOs must select ARCH_REQUIRE_GPIOLIB (if GPIO usage
  10. is mandatory) or ARCH_WANT_OPTIONAL_GPIOLIB (if GPIO support can be omitted) in
  11. their Kconfig. Then, how GPIOs are mapped depends on what the platform uses to
  12. describe its hardware layout. Currently, mappings can be defined through device
  13. tree, ACPI, and platform data.
  14. Device Tree
  15. -----------
  16. GPIOs can easily be mapped to devices and functions in the device tree. The
  17. exact way to do it depends on the GPIO controller providing the GPIOs, see the
  18. device tree bindings for your controller.
  19. GPIOs mappings are defined in the consumer device's node, in a property named
  20. <function>-gpios, where <function> is the function the driver will request
  21. through gpiod_get(). For example:
  22. foo_device {
  23. compatible = "acme,foo";
  24. ...
  25. led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */
  26. <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */
  27. <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */
  28. power-gpios = <&gpio 1 GPIO_ACTIVE_LOW>;
  29. };
  30. Properties named <function>-gpio are also considered valid and old bindings use
  31. it but are only supported for compatibility reasons and should not be used for
  32. newer bindings since it has been deprecated.
  33. This property will make GPIOs 15, 16 and 17 available to the driver under the
  34. "led" function, and GPIO 1 as the "power" GPIO:
  35. struct gpio_desc *red, *green, *blue, *power;
  36. red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH);
  37. green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH);
  38. blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH);
  39. power = gpiod_get(dev, "power", GPIOD_OUT_HIGH);
  40. The led GPIOs will be active-high, while the power GPIO will be active-low (i.e.
  41. gpiod_is_active_low(power) will be true).
  42. The second parameter of the gpiod_get() functions, the con_id string, has to be
  43. the <function>-prefix of the GPIO suffixes ("gpios" or "gpio", automatically
  44. looked up by the gpiod functions internally) used in the device tree. With above
  45. "led-gpios" example, use the prefix without the "-" as con_id parameter: "led".
  46. Internally, the GPIO subsystem prefixes the GPIO suffix ("gpios" or "gpio")
  47. with the string passed in con_id to get the resulting string
  48. (snprintf(... "%s-%s", con_id, gpio_suffixes[]).
  49. ACPI
  50. ----
  51. ACPI also supports function names for GPIOs in a similar fashion to DT.
  52. The above DT example can be converted to an equivalent ACPI description
  53. with the help of _DSD (Device Specific Data), introduced in ACPI 5.1:
  54. Device (FOO) {
  55. Name (_CRS, ResourceTemplate () {
  56. GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
  57. "\\_SB.GPI0") {15} // red
  58. GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
  59. "\\_SB.GPI0") {16} // green
  60. GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
  61. "\\_SB.GPI0") {17} // blue
  62. GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
  63. "\\_SB.GPI0") {1} // power
  64. })
  65. Name (_DSD, Package () {
  66. ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
  67. Package () {
  68. Package () {
  69. "led-gpios",
  70. Package () {
  71. ^FOO, 0, 0, 1,
  72. ^FOO, 1, 0, 1,
  73. ^FOO, 2, 0, 1,
  74. }
  75. },
  76. Package () {
  77. "power-gpios",
  78. Package () {^FOO, 3, 0, 0},
  79. },
  80. }
  81. })
  82. }
  83. For more information about the ACPI GPIO bindings see
  84. Documentation/acpi/gpio-properties.txt.
  85. Platform Data
  86. -------------
  87. Finally, GPIOs can be bound to devices and functions using platform data. Board
  88. files that desire to do so need to include the following header:
  89. #include <linux/gpio/machine.h>
  90. GPIOs are mapped by the means of tables of lookups, containing instances of the
  91. gpiod_lookup structure. Two macros are defined to help declaring such mappings:
  92. GPIO_LOOKUP(chip_label, chip_hwnum, dev_id, con_id, flags)
  93. GPIO_LOOKUP_IDX(chip_label, chip_hwnum, dev_id, con_id, idx, flags)
  94. where
  95. - chip_label is the label of the gpiod_chip instance providing the GPIO
  96. - chip_hwnum is the hardware number of the GPIO within the chip
  97. - dev_id is the identifier of the device that will make use of this GPIO. It
  98. can be NULL, in which case it will be matched for calls to gpiod_get()
  99. with a NULL device.
  100. - con_id is the name of the GPIO function from the device point of view. It
  101. can be NULL, in which case it will match any function.
  102. - idx is the index of the GPIO within the function.
  103. - flags is defined to specify the following properties:
  104. * GPIOF_ACTIVE_LOW - to configure the GPIO as active-low
  105. * GPIOF_OPEN_DRAIN - GPIO pin is open drain type.
  106. * GPIOF_OPEN_SOURCE - GPIO pin is open source type.
  107. In the future, these flags might be extended to support more properties.
  108. Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0.
  109. A lookup table can then be defined as follows, with an empty entry defining its
  110. end:
  111. struct gpiod_lookup_table gpios_table = {
  112. .dev_id = "foo.0",
  113. .table = {
  114. GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),
  115. GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH),
  116. GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH),
  117. GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW),
  118. { },
  119. },
  120. };
  121. And the table can be added by the board code as follows:
  122. gpiod_add_lookup_table(&gpios_table);
  123. The driver controlling "foo.0" will then be able to obtain its GPIOs as follows:
  124. struct gpio_desc *red, *green, *blue, *power;
  125. red = gpiod_get_index(dev, "led", 0, GPIOD_OUT_HIGH);
  126. green = gpiod_get_index(dev, "led", 1, GPIOD_OUT_HIGH);
  127. blue = gpiod_get_index(dev, "led", 2, GPIOD_OUT_HIGH);
  128. power = gpiod_get(dev, "power", GPIOD_OUT_HIGH);
  129. Since the "led" GPIOs are mapped as active-high, this example will switch their
  130. signals to 1, i.e. enabling the LEDs. And for the "power" GPIO, which is mapped
  131. as active-low, its actual signal will be 0 after this code. Contrary to the legacy
  132. integer GPIO interface, the active-low property is handled during mapping and is
  133. thus transparent to GPIO consumers.