consumer.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. ==================================
  2. GPIO Descriptor Consumer Interface
  3. ==================================
  4. This document describes the consumer interface of the GPIO framework. Note that
  5. it describes the new descriptor-based interface. For a description of the
  6. deprecated integer-based GPIO interface please refer to gpio-legacy.txt.
  7. Guidelines for GPIOs consumers
  8. ==============================
  9. Drivers that can't work without standard GPIO calls should have Kconfig entries
  10. that depend on GPIOLIB or select GPIOLIB. The functions that allow a driver to
  11. obtain and use GPIOs are available by including the following file:
  12. #include <linux/gpio/consumer.h>
  13. There are static inline stubs for all functions in the header file in the case
  14. where GPIOLIB is disabled. When these stubs are called they will emit
  15. warnings. These stubs are used for two use cases:
  16. - Simple compile coverage with e.g. COMPILE_TEST - it does not matter that
  17. the current platform does not enable or select GPIOLIB because we are not
  18. going to execute the system anyway.
  19. - Truly optional GPIOLIB support - where the driver does not really make use
  20. of the GPIOs on certain compile-time configurations for certain systems, but
  21. will use it under other compile-time configurations. In this case the
  22. consumer must make sure not to call into these functions, or the user will
  23. be met with console warnings that may be perceived as intimidating.
  24. All the functions that work with the descriptor-based GPIO interface are
  25. prefixed with ``gpiod_``. The ``gpio_`` prefix is used for the legacy
  26. interface. No other function in the kernel should use these prefixes. The use
  27. of the legacy functions is strongly discouraged, new code should use
  28. <linux/gpio/consumer.h> and descriptors exclusively.
  29. Obtaining and Disposing GPIOs
  30. =============================
  31. With the descriptor-based interface, GPIOs are identified with an opaque,
  32. non-forgeable handler that must be obtained through a call to one of the
  33. gpiod_get() functions. Like many other kernel subsystems, gpiod_get() takes the
  34. device that will use the GPIO and the function the requested GPIO is supposed to
  35. fulfill::
  36. struct gpio_desc *gpiod_get(struct device *dev, const char *con_id,
  37. enum gpiod_flags flags)
  38. If a function is implemented by using several GPIOs together (e.g. a simple LED
  39. device that displays digits), an additional index argument can be specified::
  40. struct gpio_desc *gpiod_get_index(struct device *dev,
  41. const char *con_id, unsigned int idx,
  42. enum gpiod_flags flags)
  43. For a more detailed description of the con_id parameter in the DeviceTree case
  44. see Documentation/driver-api/gpio/board.rst
  45. The flags parameter is used to optionally specify a direction and initial value
  46. for the GPIO. Values can be:
  47. * GPIOD_ASIS or 0 to not initialize the GPIO at all. The direction must be set
  48. later with one of the dedicated functions.
  49. * GPIOD_IN to initialize the GPIO as input.
  50. * GPIOD_OUT_LOW to initialize the GPIO as output with a value of 0.
  51. * GPIOD_OUT_HIGH to initialize the GPIO as output with a value of 1.
  52. * GPIOD_OUT_LOW_OPEN_DRAIN same as GPIOD_OUT_LOW but also enforce the line
  53. to be electrically used with open drain.
  54. * GPIOD_OUT_HIGH_OPEN_DRAIN same as GPIOD_OUT_HIGH but also enforce the line
  55. to be electrically used with open drain.
  56. The two last flags are used for use cases where open drain is mandatory, such
  57. as I2C: if the line is not already configured as open drain in the mappings
  58. (see board.txt), then open drain will be enforced anyway and a warning will be
  59. printed that the board configuration needs to be updated to match the use case.
  60. Both functions return either a valid GPIO descriptor, or an error code checkable
  61. with IS_ERR() (they will never return a NULL pointer). -ENOENT will be returned
  62. if and only if no GPIO has been assigned to the device/function/index triplet,
  63. other error codes are used for cases where a GPIO has been assigned but an error
  64. occurred while trying to acquire it. This is useful to discriminate between mere
  65. errors and an absence of GPIO for optional GPIO parameters. For the common
  66. pattern where a GPIO is optional, the gpiod_get_optional() and
  67. gpiod_get_index_optional() functions can be used. These functions return NULL
  68. instead of -ENOENT if no GPIO has been assigned to the requested function::
  69. struct gpio_desc *gpiod_get_optional(struct device *dev,
  70. const char *con_id,
  71. enum gpiod_flags flags)
  72. struct gpio_desc *gpiod_get_index_optional(struct device *dev,
  73. const char *con_id,
  74. unsigned int index,
  75. enum gpiod_flags flags)
  76. Note that gpio_get*_optional() functions (and their managed variants), unlike
  77. the rest of gpiolib API, also return NULL when gpiolib support is disabled.
  78. This is helpful to driver authors, since they do not need to special case
  79. -ENOSYS return codes. System integrators should however be careful to enable
  80. gpiolib on systems that need it.
  81. For a function using multiple GPIOs all of those can be obtained with one call::
  82. struct gpio_descs *gpiod_get_array(struct device *dev,
  83. const char *con_id,
  84. enum gpiod_flags flags)
  85. This function returns a struct gpio_descs which contains an array of
  86. descriptors. It also contains a pointer to a gpiolib private structure which,
  87. if passed back to get/set array functions, may speed up I/O proocessing::
  88. struct gpio_descs {
  89. struct gpio_array *info;
  90. unsigned int ndescs;
  91. struct gpio_desc *desc[];
  92. }
  93. The following function returns NULL instead of -ENOENT if no GPIOs have been
  94. assigned to the requested function::
  95. struct gpio_descs *gpiod_get_array_optional(struct device *dev,
  96. const char *con_id,
  97. enum gpiod_flags flags)
  98. Device-managed variants of these functions are also defined::
  99. struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id,
  100. enum gpiod_flags flags)
  101. struct gpio_desc *devm_gpiod_get_index(struct device *dev,
  102. const char *con_id,
  103. unsigned int idx,
  104. enum gpiod_flags flags)
  105. struct gpio_desc *devm_gpiod_get_optional(struct device *dev,
  106. const char *con_id,
  107. enum gpiod_flags flags)
  108. struct gpio_desc *devm_gpiod_get_index_optional(struct device *dev,
  109. const char *con_id,
  110. unsigned int index,
  111. enum gpiod_flags flags)
  112. struct gpio_descs *devm_gpiod_get_array(struct device *dev,
  113. const char *con_id,
  114. enum gpiod_flags flags)
  115. struct gpio_descs *devm_gpiod_get_array_optional(struct device *dev,
  116. const char *con_id,
  117. enum gpiod_flags flags)
  118. A GPIO descriptor can be disposed of using the gpiod_put() function::
  119. void gpiod_put(struct gpio_desc *desc)
  120. For an array of GPIOs this function can be used::
  121. void gpiod_put_array(struct gpio_descs *descs)
  122. It is strictly forbidden to use a descriptor after calling these functions.
  123. It is also not allowed to individually release descriptors (using gpiod_put())
  124. from an array acquired with gpiod_get_array().
  125. The device-managed variants are, unsurprisingly::
  126. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  127. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
  128. Using GPIOs
  129. ===========
  130. Setting Direction
  131. -----------------
  132. The first thing a driver must do with a GPIO is setting its direction. If no
  133. direction-setting flags have been given to gpiod_get*(), this is done by
  134. invoking one of the gpiod_direction_*() functions::
  135. int gpiod_direction_input(struct gpio_desc *desc)
  136. int gpiod_direction_output(struct gpio_desc *desc, int value)
  137. The return value is zero for success, else a negative errno. It should be
  138. checked, since the get/set calls don't return errors and since misconfiguration
  139. is possible. You should normally issue these calls from a task context. However,
  140. for spinlock-safe GPIOs it is OK to use them before tasking is enabled, as part
  141. of early board setup.
  142. For output GPIOs, the value provided becomes the initial output value. This
  143. helps avoid signal glitching during system startup.
  144. A driver can also query the current direction of a GPIO::
  145. int gpiod_get_direction(const struct gpio_desc *desc)
  146. This function returns 0 for output, 1 for input, or an error code in case of error.
  147. Be aware that there is no default direction for GPIOs. Therefore, **using a GPIO
  148. without setting its direction first is illegal and will result in undefined
  149. behavior!**
  150. Spinlock-Safe GPIO Access
  151. -------------------------
  152. Most GPIO controllers can be accessed with memory read/write instructions. Those
  153. don't need to sleep, and can safely be done from inside hard (non-threaded) IRQ
  154. handlers and similar contexts.
  155. Use the following calls to access GPIOs from an atomic context::
  156. int gpiod_get_value(const struct gpio_desc *desc);
  157. void gpiod_set_value(struct gpio_desc *desc, int value);
  158. The values are boolean, zero for low, nonzero for high. When reading the value
  159. of an output pin, the value returned should be what's seen on the pin. That
  160. won't always match the specified output value, because of issues including
  161. open-drain signaling and output latencies.
  162. The get/set calls do not return errors because "invalid GPIO" should have been
  163. reported earlier from gpiod_direction_*(). However, note that not all platforms
  164. can read the value of output pins; those that can't should always return zero.
  165. Also, using these calls for GPIOs that can't safely be accessed without sleeping
  166. (see below) is an error.
  167. GPIO Access That May Sleep
  168. --------------------------
  169. Some GPIO controllers must be accessed using message based buses like I2C or
  170. SPI. Commands to read or write those GPIO values require waiting to get to the
  171. head of a queue to transmit a command and get its response. This requires
  172. sleeping, which can't be done from inside IRQ handlers.
  173. Platforms that support this type of GPIO distinguish them from other GPIOs by
  174. returning nonzero from this call::
  175. int gpiod_cansleep(const struct gpio_desc *desc)
  176. To access such GPIOs, a different set of accessors is defined::
  177. int gpiod_get_value_cansleep(const struct gpio_desc *desc)
  178. void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
  179. Accessing such GPIOs requires a context which may sleep, for example a threaded
  180. IRQ handler, and those accessors must be used instead of spinlock-safe
  181. accessors without the cansleep() name suffix.
  182. Other than the fact that these accessors might sleep, and will work on GPIOs
  183. that can't be accessed from hardIRQ handlers, these calls act the same as the
  184. spinlock-safe calls.
  185. The active low and open drain semantics
  186. ---------------------------------------
  187. As a consumer should not have to care about the physical line level, all of the
  188. gpiod_set_value_xxx() or gpiod_set_array_value_xxx() functions operate with
  189. the *logical* value. With this they take the active low property into account.
  190. This means that they check whether the GPIO is configured to be active low,
  191. and if so, they manipulate the passed value before the physical line level is
  192. driven.
  193. The same is applicable for open drain or open source output lines: those do not
  194. actively drive their output high (open drain) or low (open source), they just
  195. switch their output to a high impedance value. The consumer should not need to
  196. care. (For details read about open drain in driver.txt.)
  197. With this, all the gpiod_set_(array)_value_xxx() functions interpret the
  198. parameter "value" as "asserted" ("1") or "de-asserted" ("0"). The physical line
  199. level will be driven accordingly.
  200. As an example, if the active low property for a dedicated GPIO is set, and the
  201. gpiod_set_(array)_value_xxx() passes "asserted" ("1"), the physical line level
  202. will be driven low.
  203. To summarize::
  204. Function (example) line property physical line
  205. gpiod_set_raw_value(desc, 0); don't care low
  206. gpiod_set_raw_value(desc, 1); don't care high
  207. gpiod_set_value(desc, 0); default (active high) low
  208. gpiod_set_value(desc, 1); default (active high) high
  209. gpiod_set_value(desc, 0); active low high
  210. gpiod_set_value(desc, 1); active low low
  211. gpiod_set_value(desc, 0); open drain low
  212. gpiod_set_value(desc, 1); open drain high impedance
  213. gpiod_set_value(desc, 0); open source high impedance
  214. gpiod_set_value(desc, 1); open source high
  215. It is possible to override these semantics using the set_raw/get_raw functions
  216. but it should be avoided as much as possible, especially by system-agnostic drivers
  217. which should not need to care about the actual physical line level and worry about
  218. the logical value instead.
  219. Accessing raw GPIO values
  220. -------------------------
  221. Consumers exist that need to manage the logical state of a GPIO line, i.e. the value
  222. their device will actually receive, no matter what lies between it and the GPIO
  223. line.
  224. The following set of calls ignore the active-low or open drain property of a GPIO and
  225. work on the raw line value::
  226. int gpiod_get_raw_value(const struct gpio_desc *desc)
  227. void gpiod_set_raw_value(struct gpio_desc *desc, int value)
  228. int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
  229. void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
  230. int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
  231. The active low state of a GPIO can also be queried using the following call::
  232. int gpiod_is_active_low(const struct gpio_desc *desc)
  233. Note that these functions should only be used with great moderation; a driver
  234. should not have to care about the physical line level or open drain semantics.
  235. Access multiple GPIOs with a single function call
  236. -------------------------------------------------
  237. The following functions get or set the values of an array of GPIOs::
  238. int gpiod_get_array_value(unsigned int array_size,
  239. struct gpio_desc **desc_array,
  240. struct gpio_array *array_info,
  241. unsigned long *value_bitmap);
  242. int gpiod_get_raw_array_value(unsigned int array_size,
  243. struct gpio_desc **desc_array,
  244. struct gpio_array *array_info,
  245. unsigned long *value_bitmap);
  246. int gpiod_get_array_value_cansleep(unsigned int array_size,
  247. struct gpio_desc **desc_array,
  248. struct gpio_array *array_info,
  249. unsigned long *value_bitmap);
  250. int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
  251. struct gpio_desc **desc_array,
  252. struct gpio_array *array_info,
  253. unsigned long *value_bitmap);
  254. int gpiod_set_array_value(unsigned int array_size,
  255. struct gpio_desc **desc_array,
  256. struct gpio_array *array_info,
  257. unsigned long *value_bitmap)
  258. int gpiod_set_raw_array_value(unsigned int array_size,
  259. struct gpio_desc **desc_array,
  260. struct gpio_array *array_info,
  261. unsigned long *value_bitmap)
  262. int gpiod_set_array_value_cansleep(unsigned int array_size,
  263. struct gpio_desc **desc_array,
  264. struct gpio_array *array_info,
  265. unsigned long *value_bitmap)
  266. int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
  267. struct gpio_desc **desc_array,
  268. struct gpio_array *array_info,
  269. unsigned long *value_bitmap)
  270. The array can be an arbitrary set of GPIOs. The functions will try to access
  271. GPIOs belonging to the same bank or chip simultaneously if supported by the
  272. corresponding chip driver. In that case a significantly improved performance
  273. can be expected. If simultaneous access is not possible the GPIOs will be
  274. accessed sequentially.
  275. The functions take three arguments:
  276. * array_size - the number of array elements
  277. * desc_array - an array of GPIO descriptors
  278. * array_info - optional information obtained from gpiod_get_array()
  279. * value_bitmap - a bitmap to store the GPIOs' values (get) or
  280. a bitmap of values to assign to the GPIOs (set)
  281. The descriptor array can be obtained using the gpiod_get_array() function
  282. or one of its variants. If the group of descriptors returned by that function
  283. matches the desired group of GPIOs, those GPIOs can be accessed by simply using
  284. the struct gpio_descs returned by gpiod_get_array()::
  285. struct gpio_descs *my_gpio_descs = gpiod_get_array(...);
  286. gpiod_set_array_value(my_gpio_descs->ndescs, my_gpio_descs->desc,
  287. my_gpio_descs->info, my_gpio_value_bitmap);
  288. It is also possible to access a completely arbitrary array of descriptors. The
  289. descriptors may be obtained using any combination of gpiod_get() and
  290. gpiod_get_array(). Afterwards the array of descriptors has to be setup
  291. manually before it can be passed to one of the above functions. In that case,
  292. array_info should be set to NULL.
  293. Note that for optimal performance GPIOs belonging to the same chip should be
  294. contiguous within the array of descriptors.
  295. Still better performance may be achieved if array indexes of the descriptors
  296. match hardware pin numbers of a single chip. If an array passed to a get/set
  297. array function matches the one obtained from gpiod_get_array() and array_info
  298. associated with the array is also passed, the function may take a fast bitmap
  299. processing path, passing the value_bitmap argument directly to the respective
  300. .get/set_multiple() callback of the chip. That allows for utilization of GPIO
  301. banks as data I/O ports without much loss of performance.
  302. The return value of gpiod_get_array_value() and its variants is 0 on success
  303. or negative on error. Note the difference to gpiod_get_value(), which returns
  304. 0 or 1 on success to convey the GPIO value. With the array functions, the GPIO
  305. values are stored in value_array rather than passed back as return value.
  306. GPIOs mapped to IRQs
  307. --------------------
  308. GPIO lines can quite often be used as IRQs. You can get the IRQ number
  309. corresponding to a given GPIO using the following call::
  310. int gpiod_to_irq(const struct gpio_desc *desc)
  311. It will return an IRQ number, or a negative errno code if the mapping can't be
  312. done (most likely because that particular GPIO cannot be used as IRQ). It is an
  313. unchecked error to use a GPIO that wasn't set up as an input using
  314. gpiod_direction_input(), or to use an IRQ number that didn't originally come
  315. from gpiod_to_irq(). gpiod_to_irq() is not allowed to sleep.
  316. Non-error values returned from gpiod_to_irq() can be passed to request_irq() or
  317. free_irq(). They will often be stored into IRQ resources for platform devices,
  318. by the board-specific initialization code. Note that IRQ trigger options are
  319. part of the IRQ interface, e.g. IRQF_TRIGGER_FALLING, as are system wakeup
  320. capabilities.
  321. GPIOs and ACPI
  322. ==============
  323. On ACPI systems, GPIOs are described by GpioIo()/GpioInt() resources listed by
  324. the _CRS configuration objects of devices. Those resources do not provide
  325. connection IDs (names) for GPIOs, so it is necessary to use an additional
  326. mechanism for this purpose.
  327. Systems compliant with ACPI 5.1 or newer may provide a _DSD configuration object
  328. which, among other things, may be used to provide connection IDs for specific
  329. GPIOs described by the GpioIo()/GpioInt() resources in _CRS. If that is the
  330. case, it will be handled by the GPIO subsystem automatically. However, if the
  331. _DSD is not present, the mappings between GpioIo()/GpioInt() resources and GPIO
  332. connection IDs need to be provided by device drivers.
  333. For details refer to Documentation/firmware-guide/acpi/gpio-properties.rst
  334. Interacting With the Legacy GPIO Subsystem
  335. ==========================================
  336. Many kernel subsystems still handle GPIOs using the legacy integer-based
  337. interface. Although it is strongly encouraged to upgrade them to the safer
  338. descriptor-based API, the following two functions allow you to convert a GPIO
  339. descriptor into the GPIO integer namespace and vice-versa::
  340. int desc_to_gpio(const struct gpio_desc *desc)
  341. struct gpio_desc *gpio_to_desc(unsigned gpio)
  342. The GPIO number returned by desc_to_gpio() can be safely used as long as the
  343. GPIO descriptor has not been freed. All the same, a GPIO number passed to
  344. gpio_to_desc() must have been properly acquired, and usage of the returned GPIO
  345. descriptor is only possible after the GPIO number has been released.
  346. Freeing a GPIO obtained by one API with the other API is forbidden and an
  347. unchecked error.