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