driver.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. ================================
  2. GPIO Descriptor Driver Interface
  3. ================================
  4. This document serves as a guide for GPIO chip drivers writers. Note that it
  5. describes the new descriptor-based interface. For a description of the
  6. deprecated integer-based GPIO interface please refer to gpio-legacy.txt.
  7. Each GPIO controller driver needs to include the following header, which defines
  8. the structures used to define a GPIO driver:
  9. #include <linux/gpio/driver.h>
  10. Internal Representation of GPIOs
  11. ================================
  12. Inside a GPIO driver, individual GPIOs are identified by their hardware number,
  13. which is a unique number between 0 and n, n being the number of GPIOs managed by
  14. the chip. This number is purely internal: the hardware number of a particular
  15. GPIO descriptor is never made visible outside of the driver.
  16. On top of this internal number, each GPIO also need to have a global number in
  17. the integer GPIO namespace so that it can be used with the legacy GPIO
  18. interface. Each chip must thus have a "base" number (which can be automatically
  19. assigned), and for each GPIO the global number will be (base + hardware number).
  20. Although the integer representation is considered deprecated, it still has many
  21. users and thus needs to be maintained.
  22. So for example one platform could use numbers 32-159 for GPIOs, with a
  23. controller defining 128 GPIOs at a "base" of 32 ; while another platform uses
  24. numbers 0..63 with one set of GPIO controllers, 64-79 with another type of GPIO
  25. controller, and on one particular board 80-95 with an FPGA. The numbers need not
  26. be contiguous; either of those platforms could also use numbers 2000-2063 to
  27. identify GPIOs in a bank of I2C GPIO expanders.
  28. Controller Drivers: gpio_chip
  29. =============================
  30. In the gpiolib framework each GPIO controller is packaged as a "struct
  31. gpio_chip" (see linux/gpio/driver.h for its complete definition) with members
  32. common to each controller of that type:
  33. - methods to establish GPIO line direction
  34. - methods used to access GPIO line values
  35. - method to set electrical configuration for a given GPIO line
  36. - method to return the IRQ number associated to a given GPIO line
  37. - flag saying whether calls to its methods may sleep
  38. - optional line names array to identify lines
  39. - optional debugfs dump method (showing extra state like pullup config)
  40. - optional base number (will be automatically assigned if omitted)
  41. - optional label for diagnostics and GPIO chip mapping using platform data
  42. The code implementing a gpio_chip should support multiple instances of the
  43. controller, possibly using the driver model. That code will configure each
  44. gpio_chip and issue ``gpiochip_add[_data]()`` or ``devm_gpiochip_add_data()``.
  45. Removing a GPIO controller should be rare; use ``[devm_]gpiochip_remove()``
  46. when it is unavoidable.
  47. Often a gpio_chip is part of an instance-specific structure with states not
  48. exposed by the GPIO interfaces, such as addressing, power management, and more.
  49. Chips such as audio codecs will have complex non-GPIO states.
  50. Any debugfs dump method should normally ignore signals which haven't been
  51. requested as GPIOs. They can use gpiochip_is_requested(), which returns either
  52. NULL or the label associated with that GPIO when it was requested.
  53. RT_FULL: the GPIO driver should not use spinlock_t or any sleepable APIs
  54. (like PM runtime) in its gpio_chip implementation (.get/.set and direction
  55. control callbacks) if it is expected to call GPIO APIs from atomic context
  56. on -RT (inside hard IRQ handlers and similar contexts). Normally this should
  57. not be required.
  58. GPIO electrical configuration
  59. -----------------------------
  60. GPIOs can be configured for several electrical modes of operation by using the
  61. .set_config() callback. Currently this API supports setting debouncing and
  62. single-ended modes (open drain/open source). These settings are described
  63. below.
  64. The .set_config() callback uses the same enumerators and configuration
  65. semantics as the generic pin control drivers. This is not a coincidence: it is
  66. possible to assign the .set_config() to the function gpiochip_generic_config()
  67. which will result in pinctrl_gpio_set_config() being called and eventually
  68. ending up in the pin control back-end "behind" the GPIO controller, usually
  69. closer to the actual pins. This way the pin controller can manage the below
  70. listed GPIO configurations.
  71. If a pin controller back-end is used, the GPIO controller or hardware
  72. description needs to provide "GPIO ranges" mapping the GPIO line offsets to pin
  73. numbers on the pin controller so they can properly cross-reference each other.
  74. GPIOs with debounce support
  75. ---------------------------
  76. Debouncing is a configuration set to a pin indicating that it is connected to
  77. a mechanical switch or button, or similar that may bounce. Bouncing means the
  78. line is pulled high/low quickly at very short intervals for mechanical
  79. reasons. This can result in the value being unstable or irqs fireing repeatedly
  80. unless the line is debounced.
  81. Debouncing in practice involves setting up a timer when something happens on
  82. the line, wait a little while and then sample the line again, so see if it
  83. still has the same value (low or high). This could also be repeated by a clever
  84. state machine, waiting for a line to become stable. In either case, it sets
  85. a certain number of milliseconds for debouncing, or just "on/off" if that time
  86. is not configurable.
  87. GPIOs with open drain/source support
  88. ------------------------------------
  89. Open drain (CMOS) or open collector (TTL) means the line is not actively driven
  90. high: instead you provide the drain/collector as output, so when the transistor
  91. is not open, it will present a high-impedance (tristate) to the external rail::
  92. CMOS CONFIGURATION TTL CONFIGURATION
  93. ||--- out +--- out
  94. in ----|| |/
  95. ||--+ in ----|
  96. | |\
  97. GND GND
  98. This configuration is normally used as a way to achieve one of two things:
  99. - Level-shifting: to reach a logical level higher than that of the silicon
  100. where the output resides.
  101. - inverse wire-OR on an I/O line, for example a GPIO line, making it possible
  102. for any driving stage on the line to drive it low even if any other output
  103. to the same line is simultaneously driving it high. A special case of this
  104. is driving the SCL and SCA lines of an I2C bus, which is by definition a
  105. wire-OR bus.
  106. Both usecases require that the line be equipped with a pull-up resistor. This
  107. resistor will make the line tend to high level unless one of the transistors on
  108. the rail actively pulls it down.
  109. The level on the line will go as high as the VDD on the pull-up resistor, which
  110. may be higher than the level supported by the transistor, achieving a
  111. level-shift to the higher VDD.
  112. Integrated electronics often have an output driver stage in the form of a CMOS
  113. "totem-pole" with one N-MOS and one P-MOS transistor where one of them drives
  114. the line high and one of them drives the line low. This is called a push-pull
  115. output. The "totem-pole" looks like so::
  116. VDD
  117. |
  118. OD ||--+
  119. +--/ ---o|| P-MOS-FET
  120. | ||--+
  121. IN --+ +----- out
  122. | ||--+
  123. +--/ ----|| N-MOS-FET
  124. OS ||--+
  125. |
  126. GND
  127. The desired output signal (e.g. coming directly from some GPIO output register)
  128. arrives at IN. The switches named "OD" and "OS" are normally closed, creating
  129. a push-pull circuit.
  130. Consider the little "switches" named "OD" and "OS" that enable/disable the
  131. P-MOS or N-MOS transistor right after the split of the input. As you can see,
  132. either transistor will go totally numb if this switch is open. The totem-pole
  133. is then halved and give high impedance instead of actively driving the line
  134. high or low respectively. That is usually how software-controlled open
  135. drain/source works.
  136. Some GPIO hardware come in open drain / open source configuration. Some are
  137. hard-wired lines that will only support open drain or open source no matter
  138. what: there is only one transistor there. Some are software-configurable:
  139. by flipping a bit in a register the output can be configured as open drain
  140. or open source, in practice by flicking open the switches labeled "OD" and "OS"
  141. in the drawing above.
  142. By disabling the P-MOS transistor, the output can be driven between GND and
  143. high impedance (open drain), and by disabling the N-MOS transistor, the output
  144. can be driven between VDD and high impedance (open source). In the first case,
  145. a pull-up resistor is needed on the outgoing rail to complete the circuit, and
  146. in the second case, a pull-down resistor is needed on the rail.
  147. Hardware that supports open drain or open source or both, can implement a
  148. special callback in the gpio_chip: .set_config() that takes a generic
  149. pinconf packed value telling whether to configure the line as open drain,
  150. open source or push-pull. This will happen in response to the
  151. GPIO_OPEN_DRAIN or GPIO_OPEN_SOURCE flag set in the machine file, or coming
  152. from other hardware descriptions.
  153. If this state can not be configured in hardware, i.e. if the GPIO hardware does
  154. not support open drain/open source in hardware, the GPIO library will instead
  155. use a trick: when a line is set as output, if the line is flagged as open
  156. drain, and the IN output value is low, it will be driven low as usual. But
  157. if the IN output value is set to high, it will instead *NOT* be driven high,
  158. instead it will be switched to input, as input mode is high impedance, thus
  159. achieveing an "open drain emulation" of sorts: electrically the behaviour will
  160. be identical, with the exception of possible hardware glitches when switching
  161. the mode of the line.
  162. For open source configuration the same principle is used, just that instead
  163. of actively driving the line low, it is set to input.
  164. GPIO drivers providing IRQs
  165. ---------------------------
  166. It is custom that GPIO drivers (GPIO chips) are also providing interrupts,
  167. most often cascaded off a parent interrupt controller, and in some special
  168. cases the GPIO logic is melded with a SoC's primary interrupt controller.
  169. The IRQ portions of the GPIO block are implemented using an irqchip, using
  170. the header <linux/irq.h>. So basically such a driver is utilizing two sub-
  171. systems simultaneously: gpio and irq.
  172. RT_FULL: a realtime compliant GPIO driver should not use spinlock_t or any
  173. sleepable APIs (like PM runtime) as part of its irq_chip implementation.
  174. * spinlock_t should be replaced with raw_spinlock_t [1].
  175. * If sleepable APIs have to be used, these can be done from the .irq_bus_lock()
  176. and .irq_bus_unlock() callbacks, as these are the only slowpath callbacks
  177. on an irqchip. Create the callbacks if needed [2].
  178. GPIO irqchips usually fall in one of two categories:
  179. * CHAINED GPIO irqchips: these are usually the type that is embedded on
  180. an SoC. This means that there is a fast IRQ flow handler for the GPIOs that
  181. gets called in a chain from the parent IRQ handler, most typically the
  182. system interrupt controller. This means that the GPIO irqchip handler will
  183. be called immediately from the parent irqchip, while holding the IRQs
  184. disabled. The GPIO irqchip will then end up calling something like this
  185. sequence in its interrupt handler::
  186. static irqreturn_t foo_gpio_irq(int irq, void *data)
  187. chained_irq_enter(...);
  188. generic_handle_irq(...);
  189. chained_irq_exit(...);
  190. Chained GPIO irqchips typically can NOT set the .can_sleep flag on
  191. struct gpio_chip, as everything happens directly in the callbacks: no
  192. slow bus traffic like I2C can be used.
  193. RT_FULL: Note, chained IRQ handlers will not be forced threaded on -RT.
  194. As result, spinlock_t or any sleepable APIs (like PM runtime) can't be used
  195. in chained IRQ handler.
  196. If required (and if it can't be converted to the nested threaded GPIO irqchip)
  197. a chained IRQ handler can be converted to generic irq handler and this way
  198. it will be a threaded IRQ handler on -RT and a hard IRQ handler on non-RT
  199. (for example, see [3]).
  200. Know W/A: The generic_handle_irq() is expected to be called with IRQ disabled,
  201. so the IRQ core will complain if it is called from an IRQ handler which is
  202. forced to a thread. The "fake?" raw lock can be used to W/A this problem::
  203. raw_spinlock_t wa_lock;
  204. static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
  205. unsigned long wa_lock_flags;
  206. raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
  207. generic_handle_irq(irq_find_mapping(bank->chip.irq.domain, bit));
  208. raw_spin_unlock_irqrestore(&bank->wa_lock, wa_lock_flags);
  209. * GENERIC CHAINED GPIO irqchips: these are the same as "CHAINED GPIO irqchips",
  210. but chained IRQ handlers are not used. Instead GPIO IRQs dispatching is
  211. performed by generic IRQ handler which is configured using request_irq().
  212. The GPIO irqchip will then end up calling something like this sequence in
  213. its interrupt handler::
  214. static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
  215. for each detected GPIO IRQ
  216. generic_handle_irq(...);
  217. RT_FULL: Such kind of handlers will be forced threaded on -RT, as result IRQ
  218. core will complain that generic_handle_irq() is called with IRQ enabled and
  219. the same W/A as for "CHAINED GPIO irqchips" can be applied.
  220. * NESTED THREADED GPIO irqchips: these are off-chip GPIO expanders and any
  221. other GPIO irqchip residing on the other side of a sleeping bus. Of course
  222. such drivers that need slow bus traffic to read out IRQ status and similar,
  223. traffic which may in turn incur other IRQs to happen, cannot be handled
  224. in a quick IRQ handler with IRQs disabled. Instead they need to spawn a
  225. thread and then mask the parent IRQ line until the interrupt is handled
  226. by the driver. The hallmark of this driver is to call something like
  227. this in its interrupt handler::
  228. static irqreturn_t foo_gpio_irq(int irq, void *data)
  229. ...
  230. handle_nested_irq(irq);
  231. The hallmark of threaded GPIO irqchips is that they set the .can_sleep
  232. flag on struct gpio_chip to true, indicating that this chip may sleep
  233. when accessing the GPIOs.
  234. To help out in handling the set-up and management of GPIO irqchips and the
  235. associated irqdomain and resource allocation callbacks, the gpiolib has
  236. some helpers that can be enabled by selecting the GPIOLIB_IRQCHIP Kconfig
  237. symbol:
  238. * gpiochip_irqchip_add(): adds a chained irqchip to a gpiochip. It will pass
  239. the struct gpio_chip* for the chip to all IRQ callbacks, so the callbacks
  240. need to embed the gpio_chip in its state container and obtain a pointer
  241. to the container using container_of().
  242. (See Documentation/driver-model/design-patterns.txt)
  243. * gpiochip_irqchip_add_nested(): adds a nested irqchip to a gpiochip.
  244. Apart from that it works exactly like the chained irqchip.
  245. * gpiochip_set_chained_irqchip(): sets up a chained irq handler for a
  246. gpio_chip from a parent IRQ and passes the struct gpio_chip* as handler
  247. data. (Notice handler data, since the irqchip data is likely used by the
  248. parent irqchip!).
  249. * gpiochip_set_nested_irqchip(): sets up a nested irq handler for a
  250. gpio_chip from a parent IRQ. As the parent IRQ has usually been
  251. explicitly requested by the driver, this does very little more than
  252. mark all the child IRQs as having the other IRQ as parent.
  253. If there is a need to exclude certain GPIOs from the IRQ domain, you can
  254. set .irq.need_valid_mask of the gpiochip before gpiochip_add_data() is
  255. called. This allocates an .irq.valid_mask with as many bits set as there
  256. are GPIOs in the chip. Drivers can exclude GPIOs by clearing bits from this
  257. mask. The mask must be filled in before gpiochip_irqchip_add() or
  258. gpiochip_irqchip_add_nested() is called.
  259. To use the helpers please keep the following in mind:
  260. - Make sure to assign all relevant members of the struct gpio_chip so that
  261. the irqchip can initialize. E.g. .dev and .can_sleep shall be set up
  262. properly.
  263. - Nominally set all handlers to handle_bad_irq() in the setup call and pass
  264. handle_bad_irq() as flow handler parameter in gpiochip_irqchip_add() if it is
  265. expected for GPIO driver that irqchip .set_type() callback have to be called
  266. before using/enabling GPIO IRQ. Then set the handler to handle_level_irq()
  267. and/or handle_edge_irq() in the irqchip .set_type() callback depending on
  268. what your controller supports.
  269. It is legal for any IRQ consumer to request an IRQ from any irqchip no matter
  270. if that is a combined GPIO+IRQ driver. The basic premise is that gpio_chip and
  271. irq_chip are orthogonal, and offering their services independent of each
  272. other.
  273. gpiod_to_irq() is just a convenience function to figure out the IRQ for a
  274. certain GPIO line and should not be relied upon to have been called before
  275. the IRQ is used.
  276. So always prepare the hardware and make it ready for action in respective
  277. callbacks from the GPIO and irqchip APIs. Do not rely on gpiod_to_irq() having
  278. been called first.
  279. This orthogonality leads to ambiguities that we need to solve: if there is
  280. competition inside the subsystem which side is using the resource (a certain
  281. GPIO line and register for example) it needs to deny certain operations and
  282. keep track of usage inside of the gpiolib subsystem. This is why the API
  283. below exists.
  284. Locking IRQ usage
  285. -----------------
  286. Input GPIOs can be used as IRQ signals. When this happens, a driver is requested
  287. to mark the GPIO as being used as an IRQ::
  288. int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
  289. This will prevent the use of non-irq related GPIO APIs until the GPIO IRQ lock
  290. is released::
  291. void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
  292. When implementing an irqchip inside a GPIO driver, these two functions should
  293. typically be called in the .startup() and .shutdown() callbacks from the
  294. irqchip.
  295. When using the gpiolib irqchip helpers, these callback are automatically
  296. assigned.
  297. Real-Time compliance for GPIO IRQ chips
  298. ---------------------------------------
  299. Any provider of irqchips needs to be carefully tailored to support Real Time
  300. preemption. It is desirable that all irqchips in the GPIO subsystem keep this
  301. in mind and do the proper testing to assure they are real time-enabled.
  302. So, pay attention on above " RT_FULL:" notes, please.
  303. The following is a checklist to follow when preparing a driver for real
  304. time-compliance:
  305. - ensure spinlock_t is not used as part irq_chip implementation;
  306. - ensure that sleepable APIs are not used as part irq_chip implementation.
  307. If sleepable APIs have to be used, these can be done from the .irq_bus_lock()
  308. and .irq_bus_unlock() callbacks;
  309. - Chained GPIO irqchips: ensure spinlock_t or any sleepable APIs are not used
  310. from chained IRQ handler;
  311. - Generic chained GPIO irqchips: take care about generic_handle_irq() calls and
  312. apply corresponding W/A;
  313. - Chained GPIO irqchips: get rid of chained IRQ handler and use generic irq
  314. handler if possible :)
  315. - regmap_mmio: Sry, but you are in trouble :( if MMIO regmap is used as for
  316. GPIO IRQ chip implementation;
  317. - Test your driver with the appropriate in-kernel real time test cases for both
  318. level and edge IRQs.
  319. Requesting self-owned GPIO pins
  320. -------------------------------
  321. Sometimes it is useful to allow a GPIO chip driver to request its own GPIO
  322. descriptors through the gpiolib API. Using gpio_request() for this purpose
  323. does not help since it pins the module to the kernel forever (it calls
  324. try_module_get()). A GPIO driver can use the following functions instead
  325. to request and free descriptors without being pinned to the kernel forever::
  326. struct gpio_desc *gpiochip_request_own_desc(struct gpio_desc *desc,
  327. const char *label)
  328. void gpiochip_free_own_desc(struct gpio_desc *desc)
  329. Descriptors requested with gpiochip_request_own_desc() must be released with
  330. gpiochip_free_own_desc().
  331. These functions must be used with care since they do not affect module use
  332. count. Do not use the functions to request gpio descriptors not owned by the
  333. calling driver.
  334. * [1] http://www.spinics.net/lists/linux-omap/msg120425.html
  335. * [2] https://lkml.org/lkml/2015/9/25/494
  336. * [3] https://lkml.org/lkml/2015/9/25/495