consumer.txt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. GPIO Descriptor Consumer Interface
  2. ==================================
  3. This document describes the consumer interface of the GPIO framework. Note that
  4. it describes the new descriptor-based interface. For a description of the
  5. deprecated integer-based GPIO interface please refer to gpio-legacy.txt.
  6. Guidelines for GPIOs consumers
  7. ==============================
  8. Drivers that can't work without standard GPIO calls should have Kconfig entries
  9. that depend on GPIOLIB. The functions that allow a driver to obtain and use
  10. GPIOs are available by including the following file:
  11. #include <linux/gpio/consumer.h>
  12. All the functions that work with the descriptor-based GPIO interface are
  13. prefixed with gpiod_. The gpio_ prefix is used for the legacy interface. No
  14. other function in the kernel should use these prefixes.
  15. Obtaining and Disposing GPIOs
  16. =============================
  17. With the descriptor-based interface, GPIOs are identified with an opaque,
  18. non-forgeable handler that must be obtained through a call to one of the
  19. gpiod_get() functions. Like many other kernel subsystems, gpiod_get() takes the
  20. device that will use the GPIO and the function the requested GPIO is supposed to
  21. fulfill:
  22. struct gpio_desc *gpiod_get(struct device *dev, const char *con_id,
  23. enum gpiod_flags flags)
  24. If a function is implemented by using several GPIOs together (e.g. a simple LED
  25. device that displays digits), an additional index argument can be specified:
  26. struct gpio_desc *gpiod_get_index(struct device *dev,
  27. const char *con_id, unsigned int idx,
  28. enum gpiod_flags flags)
  29. The flags parameter is used to optionally specify a direction and initial value
  30. for the GPIO. Values can be:
  31. * GPIOD_ASIS or 0 to not initialize the GPIO at all. The direction must be set
  32. later with one of the dedicated functions.
  33. * GPIOD_IN to initialize the GPIO as input.
  34. * GPIOD_OUT_LOW to initialize the GPIO as output with a value of 0.
  35. * GPIOD_OUT_HIGH to initialize the GPIO as output with a value of 1.
  36. Both functions return either a valid GPIO descriptor, or an error code checkable
  37. with IS_ERR() (they will never return a NULL pointer). -ENOENT will be returned
  38. if and only if no GPIO has been assigned to the device/function/index triplet,
  39. other error codes are used for cases where a GPIO has been assigned but an error
  40. occurred while trying to acquire it. This is useful to discriminate between mere
  41. errors and an absence of GPIO for optional GPIO parameters. For the common
  42. pattern where a GPIO is optional, the gpiod_get_optional() and
  43. gpiod_get_index_optional() functions can be used. These functions return NULL
  44. instead of -ENOENT if no GPIO has been assigned to the requested function:
  45. struct gpio_desc *gpiod_get_optional(struct device *dev,
  46. const char *con_id,
  47. enum gpiod_flags flags)
  48. struct gpio_desc *gpiod_get_index_optional(struct device *dev,
  49. const char *con_id,
  50. unsigned int index,
  51. enum gpiod_flags flags)
  52. Device-managed variants of these functions are also defined:
  53. struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id,
  54. enum gpiod_flags flags)
  55. struct gpio_desc *devm_gpiod_get_index(struct device *dev,
  56. const char *con_id,
  57. unsigned int idx,
  58. enum gpiod_flags flags)
  59. struct gpio_desc *devm_gpiod_get_optional(struct device *dev,
  60. const char *con_id,
  61. enum gpiod_flags flags)
  62. struct gpio_desc * devm_gpiod_get_index_optional(struct device *dev,
  63. const char *con_id,
  64. unsigned int index,
  65. enum gpiod_flags flags)
  66. A GPIO descriptor can be disposed of using the gpiod_put() function:
  67. void gpiod_put(struct gpio_desc *desc)
  68. It is strictly forbidden to use a descriptor after calling this function. The
  69. device-managed variant is, unsurprisingly:
  70. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  71. Using GPIOs
  72. ===========
  73. Setting Direction
  74. -----------------
  75. The first thing a driver must do with a GPIO is setting its direction. If no
  76. direction-setting flags have been given to gpiod_get*(), this is done by
  77. invoking one of the gpiod_direction_*() functions:
  78. int gpiod_direction_input(struct gpio_desc *desc)
  79. int gpiod_direction_output(struct gpio_desc *desc, int value)
  80. The return value is zero for success, else a negative errno. It should be
  81. checked, since the get/set calls don't return errors and since misconfiguration
  82. is possible. You should normally issue these calls from a task context. However,
  83. for spinlock-safe GPIOs it is OK to use them before tasking is enabled, as part
  84. of early board setup.
  85. For output GPIOs, the value provided becomes the initial output value. This
  86. helps avoid signal glitching during system startup.
  87. A driver can also query the current direction of a GPIO:
  88. int gpiod_get_direction(const struct gpio_desc *desc)
  89. This function will return either GPIOF_DIR_IN or GPIOF_DIR_OUT.
  90. Be aware that there is no default direction for GPIOs. Therefore, **using a GPIO
  91. without setting its direction first is illegal and will result in undefined
  92. behavior!**
  93. Spinlock-Safe GPIO Access
  94. -------------------------
  95. Most GPIO controllers can be accessed with memory read/write instructions. Those
  96. don't need to sleep, and can safely be done from inside hard (non-threaded) IRQ
  97. handlers and similar contexts.
  98. Use the following calls to access GPIOs from an atomic context:
  99. int gpiod_get_value(const struct gpio_desc *desc);
  100. void gpiod_set_value(struct gpio_desc *desc, int value);
  101. The values are boolean, zero for low, nonzero for high. When reading the value
  102. of an output pin, the value returned should be what's seen on the pin. That
  103. won't always match the specified output value, because of issues including
  104. open-drain signaling and output latencies.
  105. The get/set calls do not return errors because "invalid GPIO" should have been
  106. reported earlier from gpiod_direction_*(). However, note that not all platforms
  107. can read the value of output pins; those that can't should always return zero.
  108. Also, using these calls for GPIOs that can't safely be accessed without sleeping
  109. (see below) is an error.
  110. GPIO Access That May Sleep
  111. --------------------------
  112. Some GPIO controllers must be accessed using message based buses like I2C or
  113. SPI. Commands to read or write those GPIO values require waiting to get to the
  114. head of a queue to transmit a command and get its response. This requires
  115. sleeping, which can't be done from inside IRQ handlers.
  116. Platforms that support this type of GPIO distinguish them from other GPIOs by
  117. returning nonzero from this call:
  118. int gpiod_cansleep(const struct gpio_desc *desc)
  119. To access such GPIOs, a different set of accessors is defined:
  120. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  121. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  122. Accessing such GPIOs requires a context which may sleep, for example a threaded
  123. IRQ handler, and those accessors must be used instead of spinlock-safe
  124. accessors without the cansleep() name suffix.
  125. Other than the fact that these accessors might sleep, and will work on GPIOs
  126. that can't be accessed from hardIRQ handlers, these calls act the same as the
  127. spinlock-safe calls.
  128. Active-low State and Raw GPIO Values
  129. ------------------------------------
  130. Device drivers like to manage the logical state of a GPIO, i.e. the value their
  131. device will actually receive, no matter what lies between it and the GPIO line.
  132. In some cases, it might make sense to control the actual GPIO line value. The
  133. following set of calls ignore the active-low property of a GPIO and work on the
  134. raw line value:
  135. int gpiod_get_raw_value(const struct gpio_desc *desc)
  136. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  137. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  138. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  139. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  140. The active-low state of a GPIO can also be queried using the following call:
  141. int gpiod_is_active_low(const struct gpio_desc *desc)
  142. Note that these functions should only be used with great moderation ; a driver
  143. should not have to care about the physical line level.
  144. GPIOs mapped to IRQs
  145. --------------------
  146. GPIO lines can quite often be used as IRQs. You can get the IRQ number
  147. corresponding to a given GPIO using the following call:
  148. int gpiod_to_irq(const struct gpio_desc *desc)
  149. It will return an IRQ number, or an negative errno code if the mapping can't be
  150. done (most likely because that particular GPIO cannot be used as IRQ). It is an
  151. unchecked error to use a GPIO that wasn't set up as an input using
  152. gpiod_direction_input(), or to use an IRQ number that didn't originally come
  153. from gpiod_to_irq(). gpiod_to_irq() is not allowed to sleep.
  154. Non-error values returned from gpiod_to_irq() can be passed to request_irq() or
  155. free_irq(). They will often be stored into IRQ resources for platform devices,
  156. by the board-specific initialization code. Note that IRQ trigger options are
  157. part of the IRQ interface, e.g. IRQF_TRIGGER_FALLING, as are system wakeup
  158. capabilities.
  159. Interacting With the Legacy GPIO Subsystem
  160. ==========================================
  161. Many kernel subsystems still handle GPIOs using the legacy integer-based
  162. interface. Although it is strongly encouraged to upgrade them to the safer
  163. descriptor-based API, the following two functions allow you to convert a GPIO
  164. descriptor into the GPIO integer namespace and vice-versa:
  165. int desc_to_gpio(const struct gpio_desc *desc)
  166. struct gpio_desc *gpio_to_desc(unsigned gpio)
  167. The GPIO number returned by desc_to_gpio() can be safely used as long as the
  168. GPIO descriptor has not been freed. All the same, a GPIO number passed to
  169. gpio_to_desc() must have been properly acquired, and usage of the returned GPIO
  170. descriptor is only possible after the GPIO number has been released.
  171. Freeing a GPIO obtained by one API with the other API is forbidden and an
  172. unchecked error.