1*fd71220bSRobert Mustacchi.\" 2*fd71220bSRobert Mustacchi.\" This file and its contents are supplied under the terms of the 3*fd71220bSRobert Mustacchi.\" Common Development and Distribution License ("CDDL"), version 1.0. 4*fd71220bSRobert Mustacchi.\" You may only use this file in accordance with the terms of version 5*fd71220bSRobert Mustacchi.\" 1.0 of the CDDL. 6*fd71220bSRobert Mustacchi.\" 7*fd71220bSRobert Mustacchi.\" A full copy of the text of the CDDL should have accompanied this 8*fd71220bSRobert Mustacchi.\" source. A copy of the CDDL is also available via the Internet at 9*fd71220bSRobert Mustacchi.\" http://www.illumos.org/license/CDDL. 10*fd71220bSRobert Mustacchi.\" 11*fd71220bSRobert Mustacchi.\" 12*fd71220bSRobert Mustacchi.\" Copyright 2022 Oxide Computer Company 13*fd71220bSRobert Mustacchi.\" 14*fd71220bSRobert Mustacchi.Dd September 17, 2022 15*fd71220bSRobert Mustacchi.Dt GPIO 7 16*fd71220bSRobert Mustacchi.Os 17*fd71220bSRobert Mustacchi.Sh NAME 18*fd71220bSRobert Mustacchi.Nm gpio , 19*fd71220bSRobert Mustacchi.Nm dpio 20*fd71220bSRobert Mustacchi.Nd General and Dedicated Purpose I/O 21*fd71220bSRobert Mustacchi.Sh DESCRIPTION 22*fd71220bSRobert MustacchiGeneral Purpose I/Os 23*fd71220bSRobert Mustacchi.Pq gpio 24*fd71220bSRobert Mustacchiare a common type of interface found in various microprocessors, ASICs, 25*fd71220bSRobert Mustacchiand other types of devices. 26*fd71220bSRobert MustacchiA GPIO is functionality that hardware exposes that allows software to 27*fd71220bSRobert Mustacchiobserve and control the state of a particular pin 28*fd71220bSRobert Mustacchi.Pq or set of pins 29*fd71220bSRobert Mustacchion the device. 30*fd71220bSRobert MustacchiThese hardware pins may be connected to any number of different things 31*fd71220bSRobert Mustacchiranging from enable and control lines, sensors, LEDs, or even a more 32*fd71220bSRobert Mustacchicomplex hardware peripheral. 33*fd71220bSRobert Mustacchi.Pp 34*fd71220bSRobert MustacchiSoftware control of a GPIO generally gives you the ability to do things 35*fd71220bSRobert Mustacchilike: 36*fd71220bSRobert Mustacchi.Bl -bullet 37*fd71220bSRobert Mustacchi.It 38*fd71220bSRobert MustacchiRead the current logic value that the hardware sees on the pin 39*fd71220bSRobert Mustacchi.It 40*fd71220bSRobert MustacchiSet the logical output value for a pin 41*fd71220bSRobert Mustacchi.It 42*fd71220bSRobert MustacchiControl internal aspects such as integrated pull-up and pull-down 43*fd71220bSRobert Mustacchiresistors, interrupt generation, and more. 44*fd71220bSRobert Mustacchi.El 45*fd71220bSRobert Mustacchi.Pp 46*fd71220bSRobert MustacchiThe OS provides a dedicated subsystem for manipulating and interfacing 47*fd71220bSRobert Mustacchiwith GPIOs in hardware. 48*fd71220bSRobert MustacchiThe rest of this manual goes into details about that subsystem, ways to 49*fd71220bSRobert Mustacchiconstrain GPIOs for safety, and what tooling is available. 50*fd71220bSRobert Mustacchi.Ss GPIO Subsystem 51*fd71220bSRobert MustacchiDevice drivers register with the kernel GPIO subsystem which the OS 52*fd71220bSRobert Mustacchiorganizes as a series of named GPIO Controllers. 53*fd71220bSRobert MustacchiEach controller itself then enumerates a series of GPIOs that have a 54*fd71220bSRobert Mustacchiname and controller-specific ID which is used to manipulate the GPIO. 55*fd71220bSRobert MustacchiIn general, while tooling requires IDs, names are what GPIO controllers 56*fd71220bSRobert Mustacchiguarantee the stability of. 57*fd71220bSRobert Mustacchi.Pp 58*fd71220bSRobert MustacchiEach GPIO has a series of attributes that describe the different 59*fd71220bSRobert Mustacchiproperties of the GPIO. 60*fd71220bSRobert MustacchiThe attributes of a GPIO are specific to the device driver that provides 61*fd71220bSRobert Mustacchiit. 62*fd71220bSRobert MustacchiWhile most GPIO controllers provide similar features, the specifics 63*fd71220bSRobert Mustacchioften vary quite particularly. 64*fd71220bSRobert MustacchiLet's take the example of control over integrated pull-up and pull-down 65*fd71220bSRobert Mustacchiresistors. 66*fd71220bSRobert MustacchiSome hardware offers a very simple option of indicating whether there is 67*fd71220bSRobert Mustacchia pull-up, pull-down, or none while others require you to specify the 68*fd71220bSRobert Mustacchistrength of the resistor, which of course varies from part to part 69*fd71220bSRobert Mustacchi.Pq and some devices only let you control the strength in one direction , 70*fd71220bSRobert Mustacchiand then some hardware actually lets you enable both the pull-up and 71*fd71220bSRobert Mustacchipull-down. 72*fd71220bSRobert Mustacchi.Pp 73*fd71220bSRobert MustacchiFor concepts which are trickier to deal with, such as the drive strength 74*fd71220bSRobert Mustacchior rise and fall times of signals, this turns into a larger mess. 75*fd71220bSRobert MustacchiAnd of course, the last thing that is important to keep in mind is that 76*fd71220bSRobert Mustacchinot all GPIOs on a given controller are necessarily the same. 77*fd71220bSRobert MustacchiAs such, the OS does not try to commonize the different attributes. 78*fd71220bSRobert MustacchiInstead, for each attribute, the following information is provided: 79*fd71220bSRobert Mustacchi.Bl -bullet 80*fd71220bSRobert Mustacchi.It 81*fd71220bSRobert MustacchiThe attribute's name. 82*fd71220bSRobert MustacchiA colon character 83*fd71220bSRobert Mustacchi.Po 84*fd71220bSRobert Mustacchi.Do 85*fd71220bSRobert Mustacchi: 86*fd71220bSRobert Mustacchi.Dc 87*fd71220bSRobert Mustacchi.Pc 88*fd71220bSRobert Mustacchiseparates the provider from the attribute itself. 89*fd71220bSRobert Mustacchi.It 90*fd71220bSRobert MustacchiThe attribute's current value. 91*fd71220bSRobert Mustacchi.It 92*fd71220bSRobert MustacchiThe set of possible values are for an attribute. 93*fd71220bSRobert MustacchiThis allows for discoverability of what is allowed. 94*fd71220bSRobert Mustacchi.It 95*fd71220bSRobert MustacchiWhether the attribute is read-only or read-write. 96*fd71220bSRobert Mustacchi.El 97*fd71220bSRobert Mustacchi.Pp 98*fd71220bSRobert MustacchiTo make this concrete, here is an example of a GPIO's attributes as 99*fd71220bSRobert Mustacchiprovided by the 100*fd71220bSRobert Mustacchi.Xr gpioadm 8 101*fd71220bSRobert Mustacchiutility: 102*fd71220bSRobert Mustacchi.Bd -literal -offset indent 103*fd71220bSRobert MustacchiATTR PERM VALUE POSSIBLE 104*fd71220bSRobert Mustacchiname r- EGPIO9_3 -- 105*fd71220bSRobert Mustacchizen:pad_name r- BP_AGPIO9_3 -- 106*fd71220bSRobert Mustacchizen:pin r- CY41 -- 107*fd71220bSRobert Mustacchizen:pad_type r- gpio -- 108*fd71220bSRobert Mustacchizen:caps r- -- -- 109*fd71220bSRobert Mustacchizen:output_driver r- push-pull push-pull 110*fd71220bSRobert Mustacchizen:voltage r- 3.3V 3.3V 111*fd71220bSRobert Mustacchizen:drive_strength rw 40R 40R,80R 112*fd71220bSRobert Mustacchizen:output rw disabled disabled,low,high 113*fd71220bSRobert Mustacchizen:input r- low low,high 114*fd71220bSRobert Mustacchizen:debounce_mode rw none none,keep-low-glitch, 115*fd71220bSRobert Mustacchi keep-high-glitch, 116*fd71220bSRobert Mustacchi remove-glitch 117*fd71220bSRobert Mustacchizen:debounce_unit rw 61us 61us,244us,15.6ms,62.5ms 118*fd71220bSRobert Mustacchizen:debounce_count rw 0x0 -- 119*fd71220bSRobert Mustacchizen:trigger_mode rw edge/high edge/high,edge/low, 120*fd71220bSRobert Mustacchi edge/both,level/high, 121*fd71220bSRobert Mustacchi level/low 122*fd71220bSRobert Mustacchizen:status r- -- -- 123*fd71220bSRobert Mustacchizen:pull rw disabled disabled,down,4k-up, 124*fd71220bSRobert Mustacchi 8k-up 125*fd71220bSRobert Mustacchizen:raw_reg r- 0x440000 -- 126*fd71220bSRobert Mustacchi.Ed 127*fd71220bSRobert Mustacchi.Pp 128*fd71220bSRobert MustacchiMultiple attributes of a GPIO may be set at once. 129*fd71220bSRobert Mustacchi.Ss Hardware Safety 130*fd71220bSRobert MustacchiIncorrect usage of GPIOs 131*fd71220bSRobert Mustacchi.Sy will damage 132*fd71220bSRobert Mustacchiyour system. 133*fd71220bSRobert MustacchiThe way that GPIOs are used will vary from product to product and just 134*fd71220bSRobert Mustacchibecause a GPIO is used in some way on one system has no bearing on 135*fd71220bSRobert Mustacchianother. 136*fd71220bSRobert MustacchiThe GPIO subsystem on its own cannot know what the hardware designer 137*fd71220bSRobert Mustacchiintended for a pin and while one may have the ability to make a pin 138*fd71220bSRobert Mustacchibegin driving an output, if it is actually expected to be an input this 139*fd71220bSRobert Mustacchican lead to two devices both trying to drive a connected signal and 140*fd71220bSRobert Mustacchiirreparably damaging both devices! 141*fd71220bSRobert Mustacchi.Ss Dedicated Purpose I/Os 142*fd71220bSRobert MustacchiTo deal with this more complicated reality and the fact that GPIO 143*fd71220bSRobert Mustacchiattributes vary wildly, the operating system has a second concept which 144*fd71220bSRobert Mustacchiare called Dedicated Purpose I/Os 145*fd71220bSRobert Mustacchi.Pq DPIO 146*fd71220bSRobert Mustacchiwhich is designed to be used more directly by both software in the 147*fd71220bSRobert Mustacchiplatform 148*fd71220bSRobert Mustacchi.Pq e.g. fault management 149*fd71220bSRobert Mustacchior by other end-user applications. 150*fd71220bSRobert Mustacchi.Pp 151*fd71220bSRobert MustacchiA DPIO creates a named character device in 152*fd71220bSRobert Mustacchi.Pa /dev/dpio 153*fd71220bSRobert Mustacchithat has the following properties: 154*fd71220bSRobert Mustacchi.Bl -bullet 155*fd71220bSRobert Mustacchi.It 156*fd71220bSRobert MustacchiReading the device 157*fd71220bSRobert Mustacchi.Pq if allowed 158*fd71220bSRobert Mustacchireturns the current pin's status. 159*fd71220bSRobert MustacchiIn addition, if the controller supports interrupts, the device can be 160*fd71220bSRobert Mustacchipolled through 161*fd71220bSRobert Mustacchi.Xr poll 2 , 162*fd71220bSRobert Mustacchi.Xr port_get 3C , 163*fd71220bSRobert Mustacchietc. for when its input changes. 164*fd71220bSRobert Mustacchi.It 165*fd71220bSRobert MustacchiWriting to the device 166*fd71220bSRobert Mustacchi.Pq if allowed 167*fd71220bSRobert Mustacchichanges the output level, allowing one to set it to one of disabled, 168*fd71220bSRobert Mustacchilow, or high. 169*fd71220bSRobert Mustacchi.It 170*fd71220bSRobert MustacchiThe device can have a semantic name that reflects its actual usage and 171*fd71220bSRobert Mustacchiabstracts away the question of which pin on which GPIO controller it is 172*fd71220bSRobert Mustacchifound on. 173*fd71220bSRobert MustacchiA DPIO's name is global to the system while a GPIO's name is scoped to a 174*fd71220bSRobert MustacchiGPIO controller. 175*fd71220bSRobert Mustacchi.It 176*fd71220bSRobert MustacchiExclusive access to the device can be granted by using the 177*fd71220bSRobert Mustacchi.Dv O_EXCL 178*fd71220bSRobert Mustacchiflag with 179*fd71220bSRobert Mustacchi.Xr open 2 . 180*fd71220bSRobert MustacchiIf a process has exclusive access, then no one else will be able to open 181*fd71220bSRobert Mustacchithe device and read or write to it. 182*fd71220bSRobert MustacchiSimilarly, if one or more processes have the device open, a request for 183*fd71220bSRobert Mustacchiexclusive access will fail. 184*fd71220bSRobert MustacchiIn both cases 185*fd71220bSRobert Mustacchi.Er EBUSY 186*fd71220bSRobert Mustacchiis returned by the underlying kgpio driver. 187*fd71220bSRobert Mustacchi.It 188*fd71220bSRobert MustacchiAll other attributes of the underlying GPIO are frozen and cannot be 189*fd71220bSRobert Mustacchimanipulated. 190*fd71220bSRobert Mustacchi.It 191*fd71220bSRobert MustacchiThe DPIO can also be constrained such that it can only be used by the 192*fd71220bSRobert Mustacchikernel and accessible by layered opens through functions such as 193*fd71220bSRobert Mustacchi.Xr ldi_open_by_name 9F . 194*fd71220bSRobert Mustacchi.El 195*fd71220bSRobert Mustacchi.Ss Utilities and Programming Interfaces 196*fd71220bSRobert MustacchiThe GPIO and DPIO subsystems are evolving interfaces in the operating 197*fd71220bSRobert Mustacchisystem. 198*fd71220bSRobert MustacchiTo discover controllers, GPIOs, and DPIOs, privileged users can use the 199*fd71220bSRobert Mustacchi.Xr gpioadm 8 200*fd71220bSRobert Mustacchiutility. 201*fd71220bSRobert MustacchiThis allows for getting and setting the attributes of GPIOs as well as 202*fd71220bSRobert Mustacchicreating and destroying DPIOs from GPIOs. 203*fd71220bSRobert Mustacchi.Pp 204*fd71220bSRobert Mustacchi.Xr gpioadm 8 205*fd71220bSRobert Mustacchiis implemented in terms of a system internal library called libxpio 206*fd71220bSRobert Mustacchiwhich can be used to manipulate and get information about GPIOs and 207*fd71220bSRobert MustacchiDPIOs. 208*fd71220bSRobert MustacchiThis is itself built on-top of private user/kernel interfaces provided by 209*fd71220bSRobert Mustacchithe 210*fd71220bSRobert Mustacchi.Xr kgpio 4D 211*fd71220bSRobert Mustacchidriver. 212*fd71220bSRobert Mustacchi.Pp 213*fd71220bSRobert MustacchiThe DPIO interface is provided in 214*fd71220bSRobert Mustacchi.In sys/gpio/dpio.h . 215*fd71220bSRobert MustacchiAll DPIOs show up under 216*fd71220bSRobert Mustacchi.Pa /dev/dpio 217*fd71220bSRobert Mustacchiin the file system and the character devices support the various 218*fd71220bSRobert Mustacchi.Xr read 2 219*fd71220bSRobert Mustacchiand 220*fd71220bSRobert Mustacchi.Xr write 2 221*fd71220bSRobert Mustacchifamilies of operations. 222*fd71220bSRobert MustacchiUnlike normal files, the DPIO character devices are not seekable. 223*fd71220bSRobert MustacchiBoth reads and writes must be exactly 4 bytes in size. 224*fd71220bSRobert MustacchiReads return the 225*fd71220bSRobert Mustacchi.Vt dpio_input_t 226*fd71220bSRobert Mustacchienumeration which currently has two values 227*fd71220bSRobert Mustacchi.Pq though more may be added in the future : 228*fd71220bSRobert Mustacchi.Bl -tag -width Dv 229*fd71220bSRobert Mustacchi.It Dv DPIO_INPUT_LOW 230*fd71220bSRobert MustacchiThis indicates that a logical low value was read in from the underlying 231*fd71220bSRobert MustacchiGPIO. 232*fd71220bSRobert Mustacchi.It Dv DPIO_INPUT_HIGH 233*fd71220bSRobert MustacchiThis indicates that a logical high value was read in from the underlying 234*fd71220bSRobert MustacchiGPIO. 235*fd71220bSRobert Mustacchi.El 236*fd71220bSRobert Mustacchi.Pp 237*fd71220bSRobert MustacchiIt's worth noting that any hardware-specific changes to the input values 238*fd71220bSRobert Mustacchisuch as polarity inversion control will have already been applied to the 239*fd71220bSRobert Mustacchivalue. 240*fd71220bSRobert MustacchiThe actual thresholds for what constitutes a logic high and low are 241*fd71220bSRobert Mustacchidevice dependent. 242*fd71220bSRobert Mustacchi.Pp 243*fd71220bSRobert MustacchiThe write interface uses the 244*fd71220bSRobert Mustacchi.Vt dpio_output_t 245*fd71220bSRobert Mustacchienumeration values which are: 246*fd71220bSRobert Mustacchi.Bl -tag -width Dv 247*fd71220bSRobert Mustacchi.It Dv DPIO_OUTPUT_LOW 248*fd71220bSRobert MustacchiThis indicates that a logical low value should be driven on the pin. 249*fd71220bSRobert Mustacchi.It Dv DPIO_OUTPUT_HIGH 250*fd71220bSRobert MustacchiThis indicates that a logical high value should be driven on the pin. 251*fd71220bSRobert Mustacchi.It Dv DPIO_OUTPUT_DISABLED 252*fd71220bSRobert MustacchiDisables the generation of any output. 253*fd71220bSRobert MustacchiThis is particularly useful for open-drain based systems. 254*fd71220bSRobert Mustacchi.El 255*fd71220bSRobert Mustacchi.Pp 256*fd71220bSRobert MustacchiLike with reads, the voltage values are device dependent. 257*fd71220bSRobert MustacchiIn addition, someone who has the device open may use the 258*fd71220bSRobert Mustacchi.Dv DPIO_IOC_CUROUT 259*fd71220bSRobert Mustacchiioctl to obtain information about what the current output value is set 260*fd71220bSRobert Mustacchito. 261*fd71220bSRobert Mustacchi.Pp 262*fd71220bSRobert MustacchiAll DPIO interfaces are still evolving and subject to change. 263*fd71220bSRobert Mustacchi.Ss I/O Multiplexing 264*fd71220bSRobert MustacchiMany complex SoCs 265*fd71220bSRobert Mustacchi.Pq system-on-chip 266*fd71220bSRobert Mustacchihave a way of switching what internal peripheral is actually using a 267*fd71220bSRobert Mustacchispecific pin. 268*fd71220bSRobert MustacchiThis peripheral may be a GPIO or it could be an entirely different 269*fd71220bSRobert Mustacchidevice like a SPI controller, serial port, I2C, or more. 270*fd71220bSRobert MustacchiAt this time, the GPIO subsystem on its own does not manipulate this 271*fd71220bSRobert Mustacchiunderlying mux and consumers of the platform will need to be aware of 272*fd71220bSRobert Mustacchithat. 273*fd71220bSRobert MustacchiIn the future, where appropriate for the controller, this subsystem will 274*fd71220bSRobert Mustacchibe integrated with this underlying mux to create a more holistic and 275*fd71220bSRobert Mustacchiuseful experience. 276*fd71220bSRobert Mustacchi.Ss GPIO Providers 277*fd71220bSRobert MustacchiGPIO providers are kernel drivers that interface with a hardware GPIO 278*fd71220bSRobert Mustacchicontroller. 279*fd71220bSRobert MustacchiThey expose the attributes, which are passed around as a series of 280*fd71220bSRobert Mustacchi.Vt nvlist_t 281*fd71220bSRobert Mustacchi.Po 282*fd71220bSRobert Mustacchi.Xr libnvpair 3LIB 283*fd71220bSRobert Mustacchi.Pc 284*fd71220bSRobert Mustacchistructures. 285*fd71220bSRobert MustacchiAttributes are either passed as an nvlist string or uint32 and then each 286*fd71220bSRobert Mustacchiattribute also contains optional metadata around the attribute's 287*fd71220bSRobert Mustacchipermissions and what values are possible for this particular GPIO. 288*fd71220bSRobert MustacchiAttributes are allowed to vary from GPIO to GPIO. 289*fd71220bSRobert Mustacchilibxpio provides logic to translate anything that isn't in a string-form 290*fd71220bSRobert Mustacchiinto something that is human-readable. 291*fd71220bSRobert MustacchiThis was done to allow for providers and programmatic interfaces to work 292*fd71220bSRobert Mustacchiin a more natural way 293*fd71220bSRobert Mustacchi.Pq e.g. using enumerations and other numeric values 294*fd71220bSRobert Mustacchiwhile still providing consumers something useful to use. 295*fd71220bSRobert Mustacchi.Pp 296*fd71220bSRobert MustacchiThe core kernel GPIO driver, 297*fd71220bSRobert Mustacchi.Xr kgpio 4D , 298*fd71220bSRobert Mustacchitakes care of dealing with the traditional character device interfaces, 299*fd71220bSRobert Mustacchiminor nodes, and all of the logic around creating, destroying, and 300*fd71220bSRobert Mustacchimanaging DPIOs. 301*fd71220bSRobert MustacchiThis in turn allows the GPIO provider drivers to be simpler and focus on 302*fd71220bSRobert Mustacchithe interface to hardware. 303*fd71220bSRobert Mustacchi.Sh SEE ALSO 304*fd71220bSRobert Mustacchi.Xr poll 2 , 305*fd71220bSRobert Mustacchi.Xr read 2 , 306*fd71220bSRobert Mustacchi.Xr write 2 , 307*fd71220bSRobert Mustacchi.Xr port_get 3C , 308*fd71220bSRobert Mustacchi.Xr libnvpair 3LIB , 309*fd71220bSRobert Mustacchi.Xr kgpio 4D , 310*fd71220bSRobert Mustacchi.Xr gpioadm 8 , 311*fd71220bSRobert Mustacchi.Xr ldi_open_by_name 9F 312