1*4882a593Smuzhiyun===================== 2*4882a593SmuzhiyunGPIO Driver Interface 3*4882a593Smuzhiyun===================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunThis document serves as a guide for writers of GPIO chip drivers. 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunEach GPIO controller driver needs to include the following header, which defines 8*4882a593Smuzhiyunthe structures used to define a GPIO driver:: 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun #include <linux/gpio/driver.h> 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunInternal Representation of GPIOs 14*4882a593Smuzhiyun================================ 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunA GPIO chip handles one or more GPIO lines. To be considered a GPIO chip, the 17*4882a593Smuzhiyunlines must conform to the definition: General Purpose Input/Output. If the 18*4882a593Smuzhiyunline is not general purpose, it is not GPIO and should not be handled by a 19*4882a593SmuzhiyunGPIO chip. The use case is the indicative: certain lines in a system may be 20*4882a593Smuzhiyuncalled GPIO but serve a very particular purpose thus not meeting the criteria 21*4882a593Smuzhiyunof a general purpose I/O. On the other hand a LED driver line may be used as a 22*4882a593SmuzhiyunGPIO and should therefore still be handled by a GPIO chip driver. 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunInside a GPIO driver, individual GPIO lines are identified by their hardware 25*4882a593Smuzhiyunnumber, sometime also referred to as ``offset``, which is a unique number 26*4882a593Smuzhiyunbetween 0 and n-1, n being the number of GPIOs managed by the chip. 27*4882a593Smuzhiyun 28*4882a593SmuzhiyunThe hardware GPIO number should be something intuitive to the hardware, for 29*4882a593Smuzhiyunexample if a system uses a memory-mapped set of I/O-registers where 32 GPIO 30*4882a593Smuzhiyunlines are handled by one bit per line in a 32-bit register, it makes sense to 31*4882a593Smuzhiyunuse hardware offsets 0..31 for these, corresponding to bits 0..31 in the 32*4882a593Smuzhiyunregister. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunThis number is purely internal: the hardware number of a particular GPIO 35*4882a593Smuzhiyunline is never made visible outside of the driver. 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunOn top of this internal number, each GPIO line also needs to have a global 38*4882a593Smuzhiyunnumber in the integer GPIO namespace so that it can be used with the legacy GPIO 39*4882a593Smuzhiyuninterface. Each chip must thus have a "base" number (which can be automatically 40*4882a593Smuzhiyunassigned), and for each GPIO line the global number will be (base + hardware 41*4882a593Smuzhiyunnumber). Although the integer representation is considered deprecated, it still 42*4882a593Smuzhiyunhas many users and thus needs to be maintained. 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunSo for example one platform could use global numbers 32-159 for GPIOs, with a 45*4882a593Smuzhiyuncontroller defining 128 GPIOs at a "base" of 32 ; while another platform uses 46*4882a593Smuzhiyunglobal numbers 0..63 with one set of GPIO controllers, 64-79 with another type 47*4882a593Smuzhiyunof GPIO controller, and on one particular board 80-95 with an FPGA. The legacy 48*4882a593Smuzhiyunnumbers need not be contiguous; either of those platforms could also use numbers 49*4882a593Smuzhiyun2000-2063 to identify GPIO lines in a bank of I2C GPIO expanders. 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunController Drivers: gpio_chip 53*4882a593Smuzhiyun============================= 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunIn the gpiolib framework each GPIO controller is packaged as a "struct 56*4882a593Smuzhiyungpio_chip" (see <linux/gpio/driver.h> for its complete definition) with members 57*4882a593Smuzhiyuncommon to each controller of that type, these should be assigned by the 58*4882a593Smuzhiyundriver code: 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun - methods to establish GPIO line direction 61*4882a593Smuzhiyun - methods used to access GPIO line values 62*4882a593Smuzhiyun - method to set electrical configuration for a given GPIO line 63*4882a593Smuzhiyun - method to return the IRQ number associated to a given GPIO line 64*4882a593Smuzhiyun - flag saying whether calls to its methods may sleep 65*4882a593Smuzhiyun - optional line names array to identify lines 66*4882a593Smuzhiyun - optional debugfs dump method (showing extra state information) 67*4882a593Smuzhiyun - optional base number (will be automatically assigned if omitted) 68*4882a593Smuzhiyun - optional label for diagnostics and GPIO chip mapping using platform data 69*4882a593Smuzhiyun 70*4882a593SmuzhiyunThe code implementing a gpio_chip should support multiple instances of the 71*4882a593Smuzhiyuncontroller, preferably using the driver model. That code will configure each 72*4882a593Smuzhiyungpio_chip and issue gpiochip_add(), gpiochip_add_data(), or 73*4882a593Smuzhiyundevm_gpiochip_add_data(). Removing a GPIO controller should be rare; use 74*4882a593Smuzhiyungpiochip_remove() when it is unavoidable. 75*4882a593Smuzhiyun 76*4882a593SmuzhiyunOften a gpio_chip is part of an instance-specific structure with states not 77*4882a593Smuzhiyunexposed by the GPIO interfaces, such as addressing, power management, and more. 78*4882a593SmuzhiyunChips such as audio codecs will have complex non-GPIO states. 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunAny debugfs dump method should normally ignore lines which haven't been 81*4882a593Smuzhiyunrequested. They can use gpiochip_is_requested(), which returns either 82*4882a593SmuzhiyunNULL or the label associated with that GPIO line when it was requested. 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunRealtime considerations: the GPIO driver should not use spinlock_t or any 85*4882a593Smuzhiyunsleepable APIs (like PM runtime) in its gpio_chip implementation (.get/.set 86*4882a593Smuzhiyunand direction control callbacks) if it is expected to call GPIO APIs from 87*4882a593Smuzhiyunatomic context on realtime kernels (inside hard IRQ handlers and similar 88*4882a593Smuzhiyuncontexts). Normally this should not be required. 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunGPIO electrical configuration 92*4882a593Smuzhiyun----------------------------- 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunGPIO lines can be configured for several electrical modes of operation by using 95*4882a593Smuzhiyunthe .set_config() callback. Currently this API supports setting: 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun- Debouncing 98*4882a593Smuzhiyun- Single-ended modes (open drain/open source) 99*4882a593Smuzhiyun- Pull up and pull down resistor enablement 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunThese settings are described below. 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunThe .set_config() callback uses the same enumerators and configuration 104*4882a593Smuzhiyunsemantics as the generic pin control drivers. This is not a coincidence: it is 105*4882a593Smuzhiyunpossible to assign the .set_config() to the function gpiochip_generic_config() 106*4882a593Smuzhiyunwhich will result in pinctrl_gpio_set_config() being called and eventually 107*4882a593Smuzhiyunending up in the pin control back-end "behind" the GPIO controller, usually 108*4882a593Smuzhiyuncloser to the actual pins. This way the pin controller can manage the below 109*4882a593Smuzhiyunlisted GPIO configurations. 110*4882a593Smuzhiyun 111*4882a593SmuzhiyunIf a pin controller back-end is used, the GPIO controller or hardware 112*4882a593Smuzhiyundescription needs to provide "GPIO ranges" mapping the GPIO line offsets to pin 113*4882a593Smuzhiyunnumbers on the pin controller so they can properly cross-reference each other. 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun 116*4882a593SmuzhiyunGPIO lines with debounce support 117*4882a593Smuzhiyun-------------------------------- 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunDebouncing is a configuration set to a pin indicating that it is connected to 120*4882a593Smuzhiyuna mechanical switch or button, or similar that may bounce. Bouncing means the 121*4882a593Smuzhiyunline is pulled high/low quickly at very short intervals for mechanical 122*4882a593Smuzhiyunreasons. This can result in the value being unstable or irqs fireing repeatedly 123*4882a593Smuzhiyununless the line is debounced. 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunDebouncing in practice involves setting up a timer when something happens on 126*4882a593Smuzhiyunthe line, wait a little while and then sample the line again, so see if it 127*4882a593Smuzhiyunstill has the same value (low or high). This could also be repeated by a clever 128*4882a593Smuzhiyunstate machine, waiting for a line to become stable. In either case, it sets 129*4882a593Smuzhiyuna certain number of milliseconds for debouncing, or just "on/off" if that time 130*4882a593Smuzhiyunis not configurable. 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunGPIO lines with open drain/source support 134*4882a593Smuzhiyun----------------------------------------- 135*4882a593Smuzhiyun 136*4882a593SmuzhiyunOpen drain (CMOS) or open collector (TTL) means the line is not actively driven 137*4882a593Smuzhiyunhigh: instead you provide the drain/collector as output, so when the transistor 138*4882a593Smuzhiyunis not open, it will present a high-impedance (tristate) to the external rail:: 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun CMOS CONFIGURATION TTL CONFIGURATION 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun ||--- out +--- out 144*4882a593Smuzhiyun in ----|| |/ 145*4882a593Smuzhiyun ||--+ in ----| 146*4882a593Smuzhiyun | |\ 147*4882a593Smuzhiyun GND GND 148*4882a593Smuzhiyun 149*4882a593SmuzhiyunThis configuration is normally used as a way to achieve one of two things: 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun- Level-shifting: to reach a logical level higher than that of the silicon 152*4882a593Smuzhiyun where the output resides. 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun- Inverse wire-OR on an I/O line, for example a GPIO line, making it possible 155*4882a593Smuzhiyun for any driving stage on the line to drive it low even if any other output 156*4882a593Smuzhiyun to the same line is simultaneously driving it high. A special case of this 157*4882a593Smuzhiyun is driving the SCL and SDA lines of an I2C bus, which is by definition a 158*4882a593Smuzhiyun wire-OR bus. 159*4882a593Smuzhiyun 160*4882a593SmuzhiyunBoth use cases require that the line be equipped with a pull-up resistor. This 161*4882a593Smuzhiyunresistor will make the line tend to high level unless one of the transistors on 162*4882a593Smuzhiyunthe rail actively pulls it down. 163*4882a593Smuzhiyun 164*4882a593SmuzhiyunThe level on the line will go as high as the VDD on the pull-up resistor, which 165*4882a593Smuzhiyunmay be higher than the level supported by the transistor, achieving a 166*4882a593Smuzhiyunlevel-shift to the higher VDD. 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunIntegrated electronics often have an output driver stage in the form of a CMOS 169*4882a593Smuzhiyun"totem-pole" with one N-MOS and one P-MOS transistor where one of them drives 170*4882a593Smuzhiyunthe line high and one of them drives the line low. This is called a push-pull 171*4882a593Smuzhiyunoutput. The "totem-pole" looks like so:: 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun VDD 174*4882a593Smuzhiyun | 175*4882a593Smuzhiyun OD ||--+ 176*4882a593Smuzhiyun +--/ ---o|| P-MOS-FET 177*4882a593Smuzhiyun | ||--+ 178*4882a593Smuzhiyun IN --+ +----- out 179*4882a593Smuzhiyun | ||--+ 180*4882a593Smuzhiyun +--/ ----|| N-MOS-FET 181*4882a593Smuzhiyun OS ||--+ 182*4882a593Smuzhiyun | 183*4882a593Smuzhiyun GND 184*4882a593Smuzhiyun 185*4882a593SmuzhiyunThe desired output signal (e.g. coming directly from some GPIO output register) 186*4882a593Smuzhiyunarrives at IN. The switches named "OD" and "OS" are normally closed, creating 187*4882a593Smuzhiyuna push-pull circuit. 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunConsider the little "switches" named "OD" and "OS" that enable/disable the 190*4882a593SmuzhiyunP-MOS or N-MOS transistor right after the split of the input. As you can see, 191*4882a593Smuzhiyuneither transistor will go totally numb if this switch is open. The totem-pole 192*4882a593Smuzhiyunis then halved and give high impedance instead of actively driving the line 193*4882a593Smuzhiyunhigh or low respectively. That is usually how software-controlled open 194*4882a593Smuzhiyundrain/source works. 195*4882a593Smuzhiyun 196*4882a593SmuzhiyunSome GPIO hardware come in open drain / open source configuration. Some are 197*4882a593Smuzhiyunhard-wired lines that will only support open drain or open source no matter 198*4882a593Smuzhiyunwhat: there is only one transistor there. Some are software-configurable: 199*4882a593Smuzhiyunby flipping a bit in a register the output can be configured as open drain 200*4882a593Smuzhiyunor open source, in practice by flicking open the switches labeled "OD" and "OS" 201*4882a593Smuzhiyunin the drawing above. 202*4882a593Smuzhiyun 203*4882a593SmuzhiyunBy disabling the P-MOS transistor, the output can be driven between GND and 204*4882a593Smuzhiyunhigh impedance (open drain), and by disabling the N-MOS transistor, the output 205*4882a593Smuzhiyuncan be driven between VDD and high impedance (open source). In the first case, 206*4882a593Smuzhiyuna pull-up resistor is needed on the outgoing rail to complete the circuit, and 207*4882a593Smuzhiyunin the second case, a pull-down resistor is needed on the rail. 208*4882a593Smuzhiyun 209*4882a593SmuzhiyunHardware that supports open drain or open source or both, can implement a 210*4882a593Smuzhiyunspecial callback in the gpio_chip: .set_config() that takes a generic 211*4882a593Smuzhiyunpinconf packed value telling whether to configure the line as open drain, 212*4882a593Smuzhiyunopen source or push-pull. This will happen in response to the 213*4882a593SmuzhiyunGPIO_OPEN_DRAIN or GPIO_OPEN_SOURCE flag set in the machine file, or coming 214*4882a593Smuzhiyunfrom other hardware descriptions. 215*4882a593Smuzhiyun 216*4882a593SmuzhiyunIf this state can not be configured in hardware, i.e. if the GPIO hardware does 217*4882a593Smuzhiyunnot support open drain/open source in hardware, the GPIO library will instead 218*4882a593Smuzhiyunuse a trick: when a line is set as output, if the line is flagged as open 219*4882a593Smuzhiyundrain, and the IN output value is low, it will be driven low as usual. But 220*4882a593Smuzhiyunif the IN output value is set to high, it will instead *NOT* be driven high, 221*4882a593Smuzhiyuninstead it will be switched to input, as input mode is high impedance, thus 222*4882a593Smuzhiyunachieveing an "open drain emulation" of sorts: electrically the behaviour will 223*4882a593Smuzhiyunbe identical, with the exception of possible hardware glitches when switching 224*4882a593Smuzhiyunthe mode of the line. 225*4882a593Smuzhiyun 226*4882a593SmuzhiyunFor open source configuration the same principle is used, just that instead 227*4882a593Smuzhiyunof actively driving the line low, it is set to input. 228*4882a593Smuzhiyun 229*4882a593Smuzhiyun 230*4882a593SmuzhiyunGPIO lines with pull up/down resistor support 231*4882a593Smuzhiyun--------------------------------------------- 232*4882a593Smuzhiyun 233*4882a593SmuzhiyunA GPIO line can support pull-up/down using the .set_config() callback. This 234*4882a593Smuzhiyunmeans that a pull up or pull-down resistor is available on the output of the 235*4882a593SmuzhiyunGPIO line, and this resistor is software controlled. 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunIn discrete designs, a pull-up or pull-down resistor is simply soldered on 238*4882a593Smuzhiyunthe circuit board. This is not something we deal with or model in software. The 239*4882a593Smuzhiyunmost you will think about these lines is that they will very likely be 240*4882a593Smuzhiyunconfigured as open drain or open source (see the section above). 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunThe .set_config() callback can only turn pull up or down on and off, and will 243*4882a593Smuzhiyunno have any semantic knowledge about the resistance used. It will only say 244*4882a593Smuzhiyunswitch a bit in a register enabling or disabling pull-up or pull-down. 245*4882a593Smuzhiyun 246*4882a593SmuzhiyunIf the GPIO line supports shunting in different resistance values for the 247*4882a593Smuzhiyunpull-up or pull-down resistor, the GPIO chip callback .set_config() will not 248*4882a593Smuzhiyunsuffice. For these complex use cases, a combined GPIO chip and pin controller 249*4882a593Smuzhiyunneed to be implemented, as the pin config interface of a pin controller 250*4882a593Smuzhiyunsupports more versatile control over electrical properties and can handle 251*4882a593Smuzhiyundifferent pull-up or pull-down resistance values. 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun 254*4882a593SmuzhiyunGPIO drivers providing IRQs 255*4882a593Smuzhiyun=========================== 256*4882a593Smuzhiyun 257*4882a593SmuzhiyunIt is custom that GPIO drivers (GPIO chips) are also providing interrupts, 258*4882a593Smuzhiyunmost often cascaded off a parent interrupt controller, and in some special 259*4882a593Smuzhiyuncases the GPIO logic is melded with a SoC's primary interrupt controller. 260*4882a593Smuzhiyun 261*4882a593SmuzhiyunThe IRQ portions of the GPIO block are implemented using an irq_chip, using 262*4882a593Smuzhiyunthe header <linux/irq.h>. So this combined driver is utilizing two sub- 263*4882a593Smuzhiyunsystems simultaneously: gpio and irq. 264*4882a593Smuzhiyun 265*4882a593SmuzhiyunIt is legal for any IRQ consumer to request an IRQ from any irqchip even if it 266*4882a593Smuzhiyunis a combined GPIO+IRQ driver. The basic premise is that gpio_chip and 267*4882a593Smuzhiyunirq_chip are orthogonal, and offering their services independent of each 268*4882a593Smuzhiyunother. 269*4882a593Smuzhiyun 270*4882a593Smuzhiyungpiod_to_irq() is just a convenience function to figure out the IRQ for a 271*4882a593Smuzhiyuncertain GPIO line and should not be relied upon to have been called before 272*4882a593Smuzhiyunthe IRQ is used. 273*4882a593Smuzhiyun 274*4882a593SmuzhiyunAlways prepare the hardware and make it ready for action in respective 275*4882a593Smuzhiyuncallbacks from the GPIO and irq_chip APIs. Do not rely on gpiod_to_irq() having 276*4882a593Smuzhiyunbeen called first. 277*4882a593Smuzhiyun 278*4882a593SmuzhiyunWe can divide GPIO irqchips in two broad categories: 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun- CASCADED INTERRUPT CHIPS: this means that the GPIO chip has one common 281*4882a593Smuzhiyun interrupt output line, which is triggered by any enabled GPIO line on that 282*4882a593Smuzhiyun chip. The interrupt output line will then be routed to an parent interrupt 283*4882a593Smuzhiyun controller one level up, in the most simple case the systems primary 284*4882a593Smuzhiyun interrupt controller. This is modeled by an irqchip that will inspect bits 285*4882a593Smuzhiyun inside the GPIO controller to figure out which line fired it. The irqchip 286*4882a593Smuzhiyun part of the driver needs to inspect registers to figure this out and it 287*4882a593Smuzhiyun will likely also need to acknowledge that it is handling the interrupt 288*4882a593Smuzhiyun by clearing some bit (sometime implicitly, by just reading a status 289*4882a593Smuzhiyun register) and it will often need to set up the configuration such as 290*4882a593Smuzhiyun edge sensitivity (rising or falling edge, or high/low level interrupt for 291*4882a593Smuzhiyun example). 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun- HIERARCHICAL INTERRUPT CHIPS: this means that each GPIO line has a dedicated 294*4882a593Smuzhiyun irq line to a parent interrupt controller one level up. There is no need 295*4882a593Smuzhiyun to inquire the GPIO hardware to figure out which line has fired, but it 296*4882a593Smuzhiyun may still be necessary to acknowledge the interrupt and set up configuration 297*4882a593Smuzhiyun such as edge sensitivity. 298*4882a593Smuzhiyun 299*4882a593SmuzhiyunRealtime considerations: a realtime compliant GPIO driver should not use 300*4882a593Smuzhiyunspinlock_t or any sleepable APIs (like PM runtime) as part of its irqchip 301*4882a593Smuzhiyunimplementation. 302*4882a593Smuzhiyun 303*4882a593Smuzhiyun- spinlock_t should be replaced with raw_spinlock_t.[1] 304*4882a593Smuzhiyun- If sleepable APIs have to be used, these can be done from the .irq_bus_lock() 305*4882a593Smuzhiyun and .irq_bus_unlock() callbacks, as these are the only slowpath callbacks 306*4882a593Smuzhiyun on an irqchip. Create the callbacks if needed.[2] 307*4882a593Smuzhiyun 308*4882a593Smuzhiyun 309*4882a593SmuzhiyunCascaded GPIO irqchips 310*4882a593Smuzhiyun---------------------- 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunCascaded GPIO irqchips usually fall in one of three categories: 313*4882a593Smuzhiyun 314*4882a593Smuzhiyun- CHAINED CASCADED GPIO IRQCHIPS: these are usually the type that is embedded on 315*4882a593Smuzhiyun an SoC. This means that there is a fast IRQ flow handler for the GPIOs that 316*4882a593Smuzhiyun gets called in a chain from the parent IRQ handler, most typically the 317*4882a593Smuzhiyun system interrupt controller. This means that the GPIO irqchip handler will 318*4882a593Smuzhiyun be called immediately from the parent irqchip, while holding the IRQs 319*4882a593Smuzhiyun disabled. The GPIO irqchip will then end up calling something like this 320*4882a593Smuzhiyun sequence in its interrupt handler:: 321*4882a593Smuzhiyun 322*4882a593Smuzhiyun static irqreturn_t foo_gpio_irq(int irq, void *data) 323*4882a593Smuzhiyun chained_irq_enter(...); 324*4882a593Smuzhiyun generic_handle_irq(...); 325*4882a593Smuzhiyun chained_irq_exit(...); 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun Chained GPIO irqchips typically can NOT set the .can_sleep flag on 328*4882a593Smuzhiyun struct gpio_chip, as everything happens directly in the callbacks: no 329*4882a593Smuzhiyun slow bus traffic like I2C can be used. 330*4882a593Smuzhiyun 331*4882a593Smuzhiyun Realtime considerations: Note that chained IRQ handlers will not be forced 332*4882a593Smuzhiyun threaded on -RT. As a result, spinlock_t or any sleepable APIs (like PM 333*4882a593Smuzhiyun runtime) can't be used in a chained IRQ handler. 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun If required (and if it can't be converted to the nested threaded GPIO irqchip, 336*4882a593Smuzhiyun see below) a chained IRQ handler can be converted to generic irq handler and 337*4882a593Smuzhiyun this way it will become a threaded IRQ handler on -RT and a hard IRQ handler 338*4882a593Smuzhiyun on non-RT (for example, see [3]). 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun The generic_handle_irq() is expected to be called with IRQ disabled, 341*4882a593Smuzhiyun so the IRQ core will complain if it is called from an IRQ handler which is 342*4882a593Smuzhiyun forced to a thread. The "fake?" raw lock can be used to work around this 343*4882a593Smuzhiyun problem:: 344*4882a593Smuzhiyun 345*4882a593Smuzhiyun raw_spinlock_t wa_lock; 346*4882a593Smuzhiyun static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank) 347*4882a593Smuzhiyun unsigned long wa_lock_flags; 348*4882a593Smuzhiyun raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags); 349*4882a593Smuzhiyun generic_handle_irq(irq_find_mapping(bank->chip.irq.domain, bit)); 350*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&bank->wa_lock, wa_lock_flags); 351*4882a593Smuzhiyun 352*4882a593Smuzhiyun- GENERIC CHAINED GPIO IRQCHIPS: these are the same as "CHAINED GPIO irqchips", 353*4882a593Smuzhiyun but chained IRQ handlers are not used. Instead GPIO IRQs dispatching is 354*4882a593Smuzhiyun performed by generic IRQ handler which is configured using request_irq(). 355*4882a593Smuzhiyun The GPIO irqchip will then end up calling something like this sequence in 356*4882a593Smuzhiyun its interrupt handler:: 357*4882a593Smuzhiyun 358*4882a593Smuzhiyun static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id) 359*4882a593Smuzhiyun for each detected GPIO IRQ 360*4882a593Smuzhiyun generic_handle_irq(...); 361*4882a593Smuzhiyun 362*4882a593Smuzhiyun Realtime considerations: this kind of handlers will be forced threaded on -RT, 363*4882a593Smuzhiyun and as result the IRQ core will complain that generic_handle_irq() is called 364*4882a593Smuzhiyun with IRQ enabled and the same work-around as for "CHAINED GPIO irqchips" can 365*4882a593Smuzhiyun be applied. 366*4882a593Smuzhiyun 367*4882a593Smuzhiyun- NESTED THREADED GPIO IRQCHIPS: these are off-chip GPIO expanders and any 368*4882a593Smuzhiyun other GPIO irqchip residing on the other side of a sleeping bus such as I2C 369*4882a593Smuzhiyun or SPI. 370*4882a593Smuzhiyun 371*4882a593Smuzhiyun Of course such drivers that need slow bus traffic to read out IRQ status and 372*4882a593Smuzhiyun similar, traffic which may in turn incur other IRQs to happen, cannot be 373*4882a593Smuzhiyun handled in a quick IRQ handler with IRQs disabled. Instead they need to spawn 374*4882a593Smuzhiyun a thread and then mask the parent IRQ line until the interrupt is handled 375*4882a593Smuzhiyun by the driver. The hallmark of this driver is to call something like 376*4882a593Smuzhiyun this in its interrupt handler:: 377*4882a593Smuzhiyun 378*4882a593Smuzhiyun static irqreturn_t foo_gpio_irq(int irq, void *data) 379*4882a593Smuzhiyun ... 380*4882a593Smuzhiyun handle_nested_irq(irq); 381*4882a593Smuzhiyun 382*4882a593Smuzhiyun The hallmark of threaded GPIO irqchips is that they set the .can_sleep 383*4882a593Smuzhiyun flag on struct gpio_chip to true, indicating that this chip may sleep 384*4882a593Smuzhiyun when accessing the GPIOs. 385*4882a593Smuzhiyun 386*4882a593Smuzhiyun These kinds of irqchips are inherently realtime tolerant as they are 387*4882a593Smuzhiyun already set up to handle sleeping contexts. 388*4882a593Smuzhiyun 389*4882a593Smuzhiyun 390*4882a593SmuzhiyunInfrastructure helpers for GPIO irqchips 391*4882a593Smuzhiyun---------------------------------------- 392*4882a593Smuzhiyun 393*4882a593SmuzhiyunTo help out in handling the set-up and management of GPIO irqchips and the 394*4882a593Smuzhiyunassociated irqdomain and resource allocation callbacks. These are activated 395*4882a593Smuzhiyunby selecting the Kconfig symbol GPIOLIB_IRQCHIP. If the symbol 396*4882a593SmuzhiyunIRQ_DOMAIN_HIERARCHY is also selected, hierarchical helpers will also be 397*4882a593Smuzhiyunprovided. A big portion of overhead code will be managed by gpiolib, 398*4882a593Smuzhiyununder the assumption that your interrupts are 1-to-1-mapped to the 399*4882a593SmuzhiyunGPIO line index: 400*4882a593Smuzhiyun 401*4882a593Smuzhiyun.. csv-table:: 402*4882a593Smuzhiyun :header: GPIO line offset, Hardware IRQ 403*4882a593Smuzhiyun 404*4882a593Smuzhiyun 0,0 405*4882a593Smuzhiyun 1,1 406*4882a593Smuzhiyun 2,2 407*4882a593Smuzhiyun ...,... 408*4882a593Smuzhiyun ngpio-1, ngpio-1 409*4882a593Smuzhiyun 410*4882a593Smuzhiyun 411*4882a593SmuzhiyunIf some GPIO lines do not have corresponding IRQs, the bitmask valid_mask 412*4882a593Smuzhiyunand the flag need_valid_mask in gpio_irq_chip can be used to mask off some 413*4882a593Smuzhiyunlines as invalid for associating with IRQs. 414*4882a593Smuzhiyun 415*4882a593SmuzhiyunThe preferred way to set up the helpers is to fill in the 416*4882a593Smuzhiyunstruct gpio_irq_chip inside struct gpio_chip before adding the gpio_chip. 417*4882a593SmuzhiyunIf you do this, the additional irq_chip will be set up by gpiolib at the 418*4882a593Smuzhiyunsame time as setting up the rest of the GPIO functionality. The following 419*4882a593Smuzhiyunis a typical example of a cascaded interrupt handler using gpio_irq_chip: 420*4882a593Smuzhiyun 421*4882a593Smuzhiyun.. code-block:: c 422*4882a593Smuzhiyun 423*4882a593Smuzhiyun /* Typical state container with dynamic irqchip */ 424*4882a593Smuzhiyun struct my_gpio { 425*4882a593Smuzhiyun struct gpio_chip gc; 426*4882a593Smuzhiyun struct irq_chip irq; 427*4882a593Smuzhiyun }; 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun int irq; /* from platform etc */ 430*4882a593Smuzhiyun struct my_gpio *g; 431*4882a593Smuzhiyun struct gpio_irq_chip *girq; 432*4882a593Smuzhiyun 433*4882a593Smuzhiyun /* Set up the irqchip dynamically */ 434*4882a593Smuzhiyun g->irq.name = "my_gpio_irq"; 435*4882a593Smuzhiyun g->irq.irq_ack = my_gpio_ack_irq; 436*4882a593Smuzhiyun g->irq.irq_mask = my_gpio_mask_irq; 437*4882a593Smuzhiyun g->irq.irq_unmask = my_gpio_unmask_irq; 438*4882a593Smuzhiyun g->irq.irq_set_type = my_gpio_set_irq_type; 439*4882a593Smuzhiyun 440*4882a593Smuzhiyun /* Get a pointer to the gpio_irq_chip */ 441*4882a593Smuzhiyun girq = &g->gc.irq; 442*4882a593Smuzhiyun girq->chip = &g->irq; 443*4882a593Smuzhiyun girq->parent_handler = ftgpio_gpio_irq_handler; 444*4882a593Smuzhiyun girq->num_parents = 1; 445*4882a593Smuzhiyun girq->parents = devm_kcalloc(dev, 1, sizeof(*girq->parents), 446*4882a593Smuzhiyun GFP_KERNEL); 447*4882a593Smuzhiyun if (!girq->parents) 448*4882a593Smuzhiyun return -ENOMEM; 449*4882a593Smuzhiyun girq->default_type = IRQ_TYPE_NONE; 450*4882a593Smuzhiyun girq->handler = handle_bad_irq; 451*4882a593Smuzhiyun girq->parents[0] = irq; 452*4882a593Smuzhiyun 453*4882a593Smuzhiyun return devm_gpiochip_add_data(dev, &g->gc, g); 454*4882a593Smuzhiyun 455*4882a593SmuzhiyunThe helper support using hierarchical interrupt controllers as well. 456*4882a593SmuzhiyunIn this case the typical set-up will look like this: 457*4882a593Smuzhiyun 458*4882a593Smuzhiyun.. code-block:: c 459*4882a593Smuzhiyun 460*4882a593Smuzhiyun /* Typical state container with dynamic irqchip */ 461*4882a593Smuzhiyun struct my_gpio { 462*4882a593Smuzhiyun struct gpio_chip gc; 463*4882a593Smuzhiyun struct irq_chip irq; 464*4882a593Smuzhiyun struct fwnode_handle *fwnode; 465*4882a593Smuzhiyun }; 466*4882a593Smuzhiyun 467*4882a593Smuzhiyun int irq; /* from platform etc */ 468*4882a593Smuzhiyun struct my_gpio *g; 469*4882a593Smuzhiyun struct gpio_irq_chip *girq; 470*4882a593Smuzhiyun 471*4882a593Smuzhiyun /* Set up the irqchip dynamically */ 472*4882a593Smuzhiyun g->irq.name = "my_gpio_irq"; 473*4882a593Smuzhiyun g->irq.irq_ack = my_gpio_ack_irq; 474*4882a593Smuzhiyun g->irq.irq_mask = my_gpio_mask_irq; 475*4882a593Smuzhiyun g->irq.irq_unmask = my_gpio_unmask_irq; 476*4882a593Smuzhiyun g->irq.irq_set_type = my_gpio_set_irq_type; 477*4882a593Smuzhiyun 478*4882a593Smuzhiyun /* Get a pointer to the gpio_irq_chip */ 479*4882a593Smuzhiyun girq = &g->gc.irq; 480*4882a593Smuzhiyun girq->chip = &g->irq; 481*4882a593Smuzhiyun girq->default_type = IRQ_TYPE_NONE; 482*4882a593Smuzhiyun girq->handler = handle_bad_irq; 483*4882a593Smuzhiyun girq->fwnode = g->fwnode; 484*4882a593Smuzhiyun girq->parent_domain = parent; 485*4882a593Smuzhiyun girq->child_to_parent_hwirq = my_gpio_child_to_parent_hwirq; 486*4882a593Smuzhiyun 487*4882a593Smuzhiyun return devm_gpiochip_add_data(dev, &g->gc, g); 488*4882a593Smuzhiyun 489*4882a593SmuzhiyunAs you can see pretty similar, but you do not supply a parent handler for 490*4882a593Smuzhiyunthe IRQ, instead a parent irqdomain, an fwnode for the hardware and 491*4882a593Smuzhiyuna funcion .child_to_parent_hwirq() that has the purpose of looking up 492*4882a593Smuzhiyunthe parent hardware irq from a child (i.e. this gpio chip) hardware irq. 493*4882a593SmuzhiyunAs always it is good to look at examples in the kernel tree for advice 494*4882a593Smuzhiyunon how to find the required pieces. 495*4882a593Smuzhiyun 496*4882a593SmuzhiyunThe old way of adding irqchips to gpiochips after registration is also still 497*4882a593Smuzhiyunavailable but we try to move away from this: 498*4882a593Smuzhiyun 499*4882a593Smuzhiyun- DEPRECATED: gpiochip_irqchip_add(): adds a chained cascaded irqchip to a 500*4882a593Smuzhiyun gpiochip. It will pass the struct gpio_chip* for the chip to all IRQ 501*4882a593Smuzhiyun callbacks, so the callbacks need to embed the gpio_chip in its state 502*4882a593Smuzhiyun container and obtain a pointer to the container using container_of(). 503*4882a593Smuzhiyun (See Documentation/driver-api/driver-model/design-patterns.rst) 504*4882a593Smuzhiyun 505*4882a593Smuzhiyun- gpiochip_irqchip_add_nested(): adds a nested cascaded irqchip to a gpiochip, 506*4882a593Smuzhiyun as discussed above regarding different types of cascaded irqchips. The 507*4882a593Smuzhiyun cascaded irq has to be handled by a threaded interrupt handler. 508*4882a593Smuzhiyun Apart from that it works exactly like the chained irqchip. 509*4882a593Smuzhiyun 510*4882a593Smuzhiyun- gpiochip_set_nested_irqchip(): sets up a nested cascaded irq handler for a 511*4882a593Smuzhiyun gpio_chip from a parent IRQ. As the parent IRQ has usually been 512*4882a593Smuzhiyun explicitly requested by the driver, this does very little more than 513*4882a593Smuzhiyun mark all the child IRQs as having the other IRQ as parent. 514*4882a593Smuzhiyun 515*4882a593SmuzhiyunIf there is a need to exclude certain GPIO lines from the IRQ domain handled by 516*4882a593Smuzhiyunthese helpers, we can set .irq.need_valid_mask of the gpiochip before 517*4882a593Smuzhiyundevm_gpiochip_add_data() or gpiochip_add_data() is called. This allocates an 518*4882a593Smuzhiyun.irq.valid_mask with as many bits set as there are GPIO lines in the chip, each 519*4882a593Smuzhiyunbit representing line 0..n-1. Drivers can exclude GPIO lines by clearing bits 520*4882a593Smuzhiyunfrom this mask. The mask must be filled in before gpiochip_irqchip_add() or 521*4882a593Smuzhiyungpiochip_irqchip_add_nested() is called. 522*4882a593Smuzhiyun 523*4882a593SmuzhiyunTo use the helpers please keep the following in mind: 524*4882a593Smuzhiyun 525*4882a593Smuzhiyun- Make sure to assign all relevant members of the struct gpio_chip so that 526*4882a593Smuzhiyun the irqchip can initialize. E.g. .dev and .can_sleep shall be set up 527*4882a593Smuzhiyun properly. 528*4882a593Smuzhiyun 529*4882a593Smuzhiyun- Nominally set all handlers to handle_bad_irq() in the setup call and pass 530*4882a593Smuzhiyun handle_bad_irq() as flow handler parameter in gpiochip_irqchip_add() if it is 531*4882a593Smuzhiyun expected for GPIO driver that irqchip .set_type() callback will be called 532*4882a593Smuzhiyun before using/enabling each GPIO IRQ. Then set the handler to 533*4882a593Smuzhiyun handle_level_irq() and/or handle_edge_irq() in the irqchip .set_type() 534*4882a593Smuzhiyun callback depending on what your controller supports and what is requested 535*4882a593Smuzhiyun by the consumer. 536*4882a593Smuzhiyun 537*4882a593Smuzhiyun 538*4882a593SmuzhiyunLocking IRQ usage 539*4882a593Smuzhiyun----------------- 540*4882a593Smuzhiyun 541*4882a593SmuzhiyunSince GPIO and irq_chip are orthogonal, we can get conflicts between different 542*4882a593Smuzhiyunuse cases. For example a GPIO line used for IRQs should be an input line, 543*4882a593Smuzhiyunit does not make sense to fire interrupts on an output GPIO. 544*4882a593Smuzhiyun 545*4882a593SmuzhiyunIf there is competition inside the subsystem which side is using the 546*4882a593Smuzhiyunresource (a certain GPIO line and register for example) it needs to deny 547*4882a593Smuzhiyuncertain operations and keep track of usage inside of the gpiolib subsystem. 548*4882a593Smuzhiyun 549*4882a593SmuzhiyunInput GPIOs can be used as IRQ signals. When this happens, a driver is requested 550*4882a593Smuzhiyunto mark the GPIO as being used as an IRQ:: 551*4882a593Smuzhiyun 552*4882a593Smuzhiyun int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) 553*4882a593Smuzhiyun 554*4882a593SmuzhiyunThis will prevent the use of non-irq related GPIO APIs until the GPIO IRQ lock 555*4882a593Smuzhiyunis released:: 556*4882a593Smuzhiyun 557*4882a593Smuzhiyun void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) 558*4882a593Smuzhiyun 559*4882a593SmuzhiyunWhen implementing an irqchip inside a GPIO driver, these two functions should 560*4882a593Smuzhiyuntypically be called in the .startup() and .shutdown() callbacks from the 561*4882a593Smuzhiyunirqchip. 562*4882a593Smuzhiyun 563*4882a593SmuzhiyunWhen using the gpiolib irqchip helpers, these callbacks are automatically 564*4882a593Smuzhiyunassigned. 565*4882a593Smuzhiyun 566*4882a593Smuzhiyun 567*4882a593SmuzhiyunDisabling and enabling IRQs 568*4882a593Smuzhiyun--------------------------- 569*4882a593Smuzhiyun 570*4882a593SmuzhiyunIn some (fringe) use cases, a driver may be using a GPIO line as input for IRQs, 571*4882a593Smuzhiyunbut occasionally switch that line over to drive output and then back to being 572*4882a593Smuzhiyunan input with interrupts again. This happens on things like CEC (Consumer 573*4882a593SmuzhiyunElectronics Control). 574*4882a593Smuzhiyun 575*4882a593SmuzhiyunWhen a GPIO is used as an IRQ signal, then gpiolib also needs to know if 576*4882a593Smuzhiyunthe IRQ is enabled or disabled. In order to inform gpiolib about this, 577*4882a593Smuzhiyunthe irqchip driver should call:: 578*4882a593Smuzhiyun 579*4882a593Smuzhiyun void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset) 580*4882a593Smuzhiyun 581*4882a593SmuzhiyunThis allows drivers to drive the GPIO as an output while the IRQ is 582*4882a593Smuzhiyundisabled. When the IRQ is enabled again, a driver should call:: 583*4882a593Smuzhiyun 584*4882a593Smuzhiyun void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset) 585*4882a593Smuzhiyun 586*4882a593SmuzhiyunWhen implementing an irqchip inside a GPIO driver, these two functions should 587*4882a593Smuzhiyuntypically be called in the .irq_disable() and .irq_enable() callbacks from the 588*4882a593Smuzhiyunirqchip. 589*4882a593Smuzhiyun 590*4882a593SmuzhiyunWhen using the gpiolib irqchip helpers, these callbacks are automatically 591*4882a593Smuzhiyunassigned. 592*4882a593Smuzhiyun 593*4882a593Smuzhiyun 594*4882a593SmuzhiyunReal-Time compliance for GPIO IRQ chips 595*4882a593Smuzhiyun--------------------------------------- 596*4882a593Smuzhiyun 597*4882a593SmuzhiyunAny provider of irqchips needs to be carefully tailored to support Real-Time 598*4882a593Smuzhiyunpreemption. It is desirable that all irqchips in the GPIO subsystem keep this 599*4882a593Smuzhiyunin mind and do the proper testing to assure they are real time-enabled. 600*4882a593Smuzhiyun 601*4882a593SmuzhiyunSo, pay attention on above realtime considerations in the documentation. 602*4882a593Smuzhiyun 603*4882a593SmuzhiyunThe following is a checklist to follow when preparing a driver for real-time 604*4882a593Smuzhiyuncompliance: 605*4882a593Smuzhiyun 606*4882a593Smuzhiyun- ensure spinlock_t is not used as part irq_chip implementation 607*4882a593Smuzhiyun- ensure that sleepable APIs are not used as part irq_chip implementation 608*4882a593Smuzhiyun If sleepable APIs have to be used, these can be done from the .irq_bus_lock() 609*4882a593Smuzhiyun and .irq_bus_unlock() callbacks 610*4882a593Smuzhiyun- Chained GPIO irqchips: ensure spinlock_t or any sleepable APIs are not used 611*4882a593Smuzhiyun from the chained IRQ handler 612*4882a593Smuzhiyun- Generic chained GPIO irqchips: take care about generic_handle_irq() calls and 613*4882a593Smuzhiyun apply corresponding work-around 614*4882a593Smuzhiyun- Chained GPIO irqchips: get rid of the chained IRQ handler and use generic irq 615*4882a593Smuzhiyun handler if possible 616*4882a593Smuzhiyun- regmap_mmio: it is possible to disable internal locking in regmap by setting 617*4882a593Smuzhiyun .disable_locking and handling the locking in the GPIO driver 618*4882a593Smuzhiyun- Test your driver with the appropriate in-kernel real-time test cases for both 619*4882a593Smuzhiyun level and edge IRQs 620*4882a593Smuzhiyun 621*4882a593Smuzhiyun* [1] http://www.spinics.net/lists/linux-omap/msg120425.html 622*4882a593Smuzhiyun* [2] https://lkml.org/lkml/2015/9/25/494 623*4882a593Smuzhiyun* [3] https://lkml.org/lkml/2015/9/25/495 624*4882a593Smuzhiyun 625*4882a593Smuzhiyun 626*4882a593SmuzhiyunRequesting self-owned GPIO pins 627*4882a593Smuzhiyun=============================== 628*4882a593Smuzhiyun 629*4882a593SmuzhiyunSometimes it is useful to allow a GPIO chip driver to request its own GPIO 630*4882a593Smuzhiyundescriptors through the gpiolib API. A GPIO driver can use the following 631*4882a593Smuzhiyunfunctions to request and free descriptors:: 632*4882a593Smuzhiyun 633*4882a593Smuzhiyun struct gpio_desc *gpiochip_request_own_desc(struct gpio_desc *desc, 634*4882a593Smuzhiyun u16 hwnum, 635*4882a593Smuzhiyun const char *label, 636*4882a593Smuzhiyun enum gpiod_flags flags) 637*4882a593Smuzhiyun 638*4882a593Smuzhiyun void gpiochip_free_own_desc(struct gpio_desc *desc) 639*4882a593Smuzhiyun 640*4882a593SmuzhiyunDescriptors requested with gpiochip_request_own_desc() must be released with 641*4882a593Smuzhiyungpiochip_free_own_desc(). 642*4882a593Smuzhiyun 643*4882a593SmuzhiyunThese functions must be used with care since they do not affect module use 644*4882a593Smuzhiyuncount. Do not use the functions to request gpio descriptors not owned by the 645*4882a593Smuzhiyuncalling driver. 646