consumer.txt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. If a function is implemented by using several GPIOs together (e.g. a simple LED
  24. device that displays digits), an additional index argument can be specified:
  25. struct gpio_desc *gpiod_get_index(struct device *dev,
  26. const char *con_id, unsigned int idx)
  27. Both functions return either a valid GPIO descriptor, or an error code checkable
  28. with IS_ERR(). They will never return a NULL pointer.
  29. Device-managed variants of these functions are also defined:
  30. struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id)
  31. struct gpio_desc *devm_gpiod_get_index(struct device *dev,
  32. const char *con_id,
  33. unsigned int idx)
  34. A GPIO descriptor can be disposed of using the gpiod_put() function:
  35. void gpiod_put(struct gpio_desc *desc)
  36. It is strictly forbidden to use a descriptor after calling this function. The
  37. device-managed variant is, unsurprisingly:
  38. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  39. Using GPIOs
  40. ===========
  41. Setting Direction
  42. -----------------
  43. The first thing a driver must do with a GPIO is setting its direction. This is
  44. done by invoking one of the gpiod_direction_*() functions:
  45. int gpiod_direction_input(struct gpio_desc *desc)
  46. int gpiod_direction_output(struct gpio_desc *desc, int value)
  47. The return value is zero for success, else a negative errno. It should be
  48. checked, since the get/set calls don't return errors and since misconfiguration
  49. is possible. You should normally issue these calls from a task context. However,
  50. for spinlock-safe GPIOs it is OK to use them before tasking is enabled, as part
  51. of early board setup.
  52. For output GPIOs, the value provided becomes the initial output value. This
  53. helps avoid signal glitching during system startup.
  54. A driver can also query the current direction of a GPIO:
  55. int gpiod_get_direction(const struct gpio_desc *desc)
  56. This function will return either GPIOF_DIR_IN or GPIOF_DIR_OUT.
  57. Be aware that there is no default direction for GPIOs. Therefore, **using a GPIO
  58. without setting its direction first is illegal and will result in undefined
  59. behavior!**
  60. Spinlock-Safe GPIO Access
  61. -------------------------
  62. Most GPIO controllers can be accessed with memory read/write instructions. Those
  63. don't need to sleep, and can safely be done from inside hard (non-threaded) IRQ
  64. handlers and similar contexts.
  65. Use the following calls to access GPIOs from an atomic context:
  66. int gpiod_get_value(const struct gpio_desc *desc);
  67. void gpiod_set_value(struct gpio_desc *desc, int value);
  68. The values are boolean, zero for low, nonzero for high. When reading the value
  69. of an output pin, the value returned should be what's seen on the pin. That
  70. won't always match the specified output value, because of issues including
  71. open-drain signaling and output latencies.
  72. The get/set calls do not return errors because "invalid GPIO" should have been
  73. reported earlier from gpiod_direction_*(). However, note that not all platforms
  74. can read the value of output pins; those that can't should always return zero.
  75. Also, using these calls for GPIOs that can't safely be accessed without sleeping
  76. (see below) is an error.
  77. GPIO Access That May Sleep
  78. --------------------------
  79. Some GPIO controllers must be accessed using message based buses like I2C or
  80. SPI. Commands to read or write those GPIO values require waiting to get to the
  81. head of a queue to transmit a command and get its response. This requires
  82. sleeping, which can't be done from inside IRQ handlers.
  83. Platforms that support this type of GPIO distinguish them from other GPIOs by
  84. returning nonzero from this call:
  85. int gpiod_cansleep(const struct gpio_desc *desc)
  86. To access such GPIOs, a different set of accessors is defined:
  87. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  88. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  89. Accessing such GPIOs requires a context which may sleep, for example a threaded
  90. IRQ handler, and those accessors must be used instead of spinlock-safe
  91. accessors without the cansleep() name suffix.
  92. Other than the fact that these accessors might sleep, and will work on GPIOs
  93. that can't be accessed from hardIRQ handlers, these calls act the same as the
  94. spinlock-safe calls.
  95. Active-low State and Raw GPIO Values
  96. ------------------------------------
  97. Device drivers like to manage the logical state of a GPIO, i.e. the value their
  98. device will actually receive, no matter what lies between it and the GPIO line.
  99. In some cases, it might make sense to control the actual GPIO line value. The
  100. following set of calls ignore the active-low property of a GPIO and work on the
  101. raw line value:
  102. int gpiod_get_raw_value(const struct gpio_desc *desc)
  103. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  104. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  105. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  106. The active-low state of a GPIO can also be queried using the following call:
  107. int gpiod_is_active_low(const struct gpio_desc *desc)
  108. Note that these functions should only be used with great moderation ; a driver
  109. should not have to care about the physical line level.
  110. GPIOs mapped to IRQs
  111. --------------------
  112. GPIO lines can quite often be used as IRQs. You can get the IRQ number
  113. corresponding to a given GPIO using the following call:
  114. int gpiod_to_irq(const struct gpio_desc *desc)
  115. It will return an IRQ number, or an negative errno code if the mapping can't be
  116. done (most likely because that particular GPIO cannot be used as IRQ). It is an
  117. unchecked error to use a GPIO that wasn't set up as an input using
  118. gpiod_direction_input(), or to use an IRQ number that didn't originally come
  119. from gpiod_to_irq(). gpiod_to_irq() is not allowed to sleep.
  120. Non-error values returned from gpiod_to_irq() can be passed to request_irq() or
  121. free_irq(). They will often be stored into IRQ resources for platform devices,
  122. by the board-specific initialization code. Note that IRQ trigger options are
  123. part of the IRQ interface, e.g. IRQF_TRIGGER_FALLING, as are system wakeup
  124. capabilities.
  125. Interacting With the Legacy GPIO Subsystem
  126. ==========================================
  127. Many kernel subsystems still handle GPIOs using the legacy integer-based
  128. interface. Although it is strongly encouraged to upgrade them to the safer
  129. descriptor-based API, the following two functions allow you to convert a GPIO
  130. descriptor into the GPIO integer namespace and vice-versa:
  131. int desc_to_gpio(const struct gpio_desc *desc)
  132. struct gpio_desc *gpio_to_desc(unsigned gpio)
  133. The GPIO number returned by desc_to_gpio() can be safely used as long as the
  134. GPIO descriptor has not been freed. All the same, a GPIO number passed to
  135. gpio_to_desc() must have been properly acquired, and usage of the returned GPIO
  136. descriptor is only possible after the GPIO number has been released.
  137. Freeing a GPIO obtained by one API with the other API is forbidden and an
  138. unchecked error.