1.\" $NetBSD: gpio.4,v 1.38 2024/12/16 16:42:22 brad Exp $ 2.\" $OpenBSD: gpio.4,v 1.5 2004/11/23 09:39:29 reyk Exp $ 3.\" 4.\" Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org> 5.\" 6.\" Permission to use, copy, modify, and distribute this software for any 7.\" purpose with or without fee is hereby granted, provided that the above 8.\" copyright notice and this permission notice appear in all copies. 9.\" 10.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17.\" 18.Dd May 4, 2021 19.Dt GPIO 4 20.Os 21.Sh NAME 22.Nm gpio 23.Nd General Purpose Input/Output 24.Sh SYNOPSIS 25.Cd "gpio* at elansc?" 26.Cd "gpio* at epgpio?" 27.Cd "gpio* at gcscpcib?" 28.Cd "gpio* at gpiosim?" 29.Cd "gpio* at gscpcib?" 30.Cd "gpio* at ichlpcib?" 31.Cd "gpio* at nsclpcsio?" 32.Cd "gpio* at soekrisgpio?" 33.Cd "gpio* at ppbus?" 34.Cd "gpio* at ptcd?" 35.Cd "gpio* at umcpmio?" 36.Cd "gpio* at wbsio?" 37.Pp 38.In sys/types.h 39.In sys/gpio.h 40.In sys/ioctl.h 41.Sh DESCRIPTION 42The 43.Nm 44device attaches to the GPIO controller and provides a uniform 45programming interface to its pins. 46.Pp 47Each GPIO controller with an attached 48.Nm 49device has an associated device file under the 50.Pa /dev 51directory, e.g.\& 52.Pa /dev/gpio0 . 53Access from userland is performed through 54.Xr ioctl 2 55calls on these devices. 56.Pp 57Whether the layout of the GPIO device can be configured is subject to 58authorization by the 59.Xr kauth 9 60framework. 61.Pp 62If for example 63.Xr secmodel_securelevel 9 64is active, the layout of the GPIO device is defined at a securelevel 65less than 1, i.e. typically during system boot, and cannot be changed later. 66GPIO pins can be configured and given a symbolic name and device drivers 67that use GPIO pins can be attached to the 68.Nm 69device at a securelevel less than 1. 70All other pins will not be accessible once the runlevel has been raised. 71.Sh IOCTL INTERFACE 72The following structures and constants are defined in the 73.In sys/gpio.h 74header file: 75.Pp 76.Bl -tag -width XXXX -compact 77.It Dv GPIOINFO ( struct gpio_info ) 78Returns information about the GPIO controller in the 79.Fa gpio_info 80structure: 81.Bd -literal 82struct gpio_info { 83 int gpio_npins; /* total number of pins available */ 84}; 85.Ed 86.Pp 87.It Dv GPIOREAD ( struct gpio_req ) 88Returns the input pin value in the 89.Fa gpio_req 90structure: 91.Bd -literal 92#define GPIOMAXNAME 64 93 94struct gpio_req { 95 char gp_name[GPIOMAXNAME]; /* pin name */ 96 int gp_pin; /* pin number */ 97 int gp_value; /* value */ 98}; 99.Ed 100.Pp 101The 102.Fa gp_name 103or 104.Fa gp_pin 105field must be set before calling. 106If both are set, gp_name takes precedence. 107On return, the 108.Fa gp_name 109field contains the current name of the pin, if set by the controller driver 110or an earlier call to 111.Dv GPIOSET . 112.Pp 113.It Dv GPIOWRITE ( struct gpio_req ) 114Writes the output value to the pin. 115The value set in the 116.Fa gp_value 117field must be either 118.Dv GPIO_PIN_LOW 119(logical 0) or 120.Dv GPIO_PIN_HIGH 121(logical 1). 122On return, the 123.Fa gp_value 124field contains the old pin state. 125.Pp 126.It Dv GPIOTOGGLE ( struct gpio_req ) 127Toggles the pin output value, i.e. changes it to the opposite. 128.Fa gp_value 129field is ignored and on return contains the old pin state. 130.Pp 131.It Dv GPIOSET ( struct gpio_set ) 132Changes pin configuration flags with the new ones provided in the 133.Fa gpio_set 134structure: 135.Bd -literal 136#define GPIOMAXNAME 64 137 138struct gpio_set { 139 char gp_name[GPIOMAXNAME]; /* pin name */ 140 int gp_pin; /* pin number */ 141 int gp_caps; /* pin capabilities (ro) */ 142 int gp_flags; /* pin configuration flags */ 143 char gp_name2[GPIOMAXNAME]; /* new name */ 144}; 145.Ed 146.Pp 147The 148.Fa gp_flags 149field is a combination of the following flags: 150.Pp 151.Bl -tag -width GPIO_PIN_OPENDRAIN -compact 152.It Dv GPIO_PIN_INPUT 153input direction 154.It Dv GPIO_PIN_OUTPUT 155output direction 156.It Dv GPIO_PIN_INOUT 157bi-directional 158.It Dv GPIO_PIN_OPENDRAIN 159open-drain output 160.It Dv GPIO_PIN_PUSHPULL 161push-pull output 162.It Dv GPIO_PIN_TRISTATE 163output disabled 164.It Dv GPIO_PIN_PULLUP 165internal pull-up enabled 166.It Dv GPIO_PIN_PULLDOWN 167internal pull-down enabled 168.It Dv GPIO_PIN_INVIN 169invert input 170.It Dv GPIO_PIN_INVOUT 171invert output 172.It Dv GPIO_PIN_PULSATE 173pulsate output 174.It Dv GPIO_PIN_ALT0 - 175.It Dv GPIO_PIN_ALT7 176select alternate pin function 0 to 7 177.El 178.Pp 179Note that the GPIO controller 180may not support all of these flags. 181On return the 182.Fa gp_caps 183field contains flags that are supported. 184If no flags are specified, the pin configuration stays unchanged. 185.Pp 186Only GPIO pins that have been set using 187.Ar GPIOSET 188will be accessible at securelevels greater than 0. 189.Pp 190.It Dv GPIOUNSET ( struct gpio_set ) 191Unset the specified pin, i.e. clear its name and make it inaccessible 192at securelevels greater than 0. 193.Pp 194.It Dv GPIOATTACH ( struct gpio_attach ) 195Attach the device described in the 196.Fa gpio_attach 197structure on this gpio device. 198.Bd -literal 199struct gpio_attach { 200 char ga_dvname[16]; /* device name */ 201 int ga_offset; /* pin number */ 202 uint32_t ga_mask; /* binary mask */ 203 uint32_t ga_flags; /* driver dependent */ 204}; 205.Ed 206.Pp 207The 208.Xr drvctl 8 209command can be used to detach a device from a gpio pin. 210.El 211.Sh FILES 212.Bl -tag -width "/dev/gpiou" -compact 213.It /dev/gpio Ns Ar u 214GPIO device unit 215.Ar u 216file. 217.El 218.Sh SEE ALSO 219.Xr ioctl 2 , 220.Xr drvctl 8 , 221.Xr gpioctl 8 222.Sh HISTORY 223The 224.Nm 225device first appeared in 226.Ox 3.6 227and 228.Nx 4.0 . 229.Sh AUTHORS 230.An -nosplit 231The 232.Nm 233driver was written by 234.An Alexander Yurchenko Aq Mt grange@openbsd.org . 235.Nm 236was ported to 237.Nx 238by 239.An Jared D. McNeill Aq Mt jmcneill@NetBSD.org . 240Runtime device attachment was added by 241.An Marc Balmer Aq Mt marc@msys.ch . 242.Sh BUGS 243Event capabilities are not supported. 244