1.\" $NetBSD: gpio.4,v 1.24 2012/12/16 13:13:35 mbalmer 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 December 16, 2012 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 ppbus?" 33.Cd "gpio* at ptcd?" 34.Pp 35.In sys/types.h 36.In sys/gpio.h 37.In sys/ioctl.h 38.Sh DESCRIPTION 39The 40.Nm 41device attaches to the 42.Tn GPIO 43controller and provides a uniform programming interface to its pins. 44.Pp 45Each 46.Tn GPIO 47controller 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 79.Tn GPIO 80controller in the 81.Fa gpio_info 82structure: 83.Bd -literal 84struct gpio_info { 85 int gpio_npins; /* total number of pins available */ 86}; 87.Ed 88.Pp 89.It Dv GPIOREAD (struct gpio_req) 90Returns the input pin value in the 91.Fa gpio_pin_op 92structure: 93.Bd -literal 94#define GPIOMAXNAME 64 95 96struct gpio_req { 97 char gp_name[GPIOMAXNAME]; /* pin name */ 98 int gp_pin; /* pin number */ 99 int gp_value; /* value */ 100}; 101.Ed 102.Pp 103The 104.Fa gp_name 105or 106.Fa gp_pin 107field must be set before calling. 108.Pp 109.It Dv GPIOWRITE (struct gpio_req) 110Writes the output value to the pin. 111The value set in the 112.Fa gp_value 113field must be either 114.Dv GPIO_PIN_LOW 115(logical 0) or 116.Dv GPIO_PIN_HIGH 117(logical 1). 118On return, the 119.Fa gp_value 120field contains the old pin state. 121.Pp 122.It Dv GPIOTOGGLE (struct gpio_req) 123Toggles the pin output value, i.e. changes it to the opposite. 124.Fa gp_value 125field is ignored and on return contains the old pin state. 126.Pp 127.It Dv GPIOSET (struct gpio_set) 128Changes pin configuration flags with the new ones provided in the 129.Fa gpio_set 130structure: 131.Bd -literal 132#define GPIOMAXNAME 64 133 134struct gpio_set { 135 char gp_name[GPIOMAXNAME]; /* pin name */ 136 int gp_pin; /* pin number */ 137 int gp_caps; /* pin capabilities (ro) */ 138 int gp_flags; /* pin configuration flags */ 139 char gp_name2[GPIOMAXNAME]; /* new name */ 140}; 141.Ed 142.Pp 143The 144.Fa gp_flags 145field is a combination of the following flags: 146.Pp 147.Bl -tag -width GPIO_PIN_OPENDRAIN -compact 148.It Dv GPIO_PIN_INPUT 149input direction 150.It Dv GPIO_PIN_OUTPUT 151output direction 152.It Dv GPIO_PIN_INOUT 153bi-directional 154.It Dv GPIO_PIN_OPENDRAIN 155open-drain output 156.It Dv GPIO_PIN_PUSHPULL 157push-pull output 158.It Dv GPIO_PIN_TRISTATE 159output disabled 160.It Dv GPIO_PIN_PULLUP 161internal pull-up enabled 162.It Dv GPIO_PIN_PULLDOWN 163internal pull-down enabled 164.It Dv GPIO_PIN_INVIN 165invert input 166.It Dv GPIO_PIN_INVOUT 167invert output 168.It Dv GPIO_PIN_PULSATE 169pulsate output 170.El 171.Pp 172Note that the GPIO controller 173may not support all of these flags. 174On return the 175.Fa gp_caps 176field contains flags that are supported. 177If no flags are specified, the pin configuration stays unchanged. 178.Pp 179Only GPIO pins that have been set using 180.Ar GPIOSET 181will be accessible at securelevels greater than 0. 182.Pp 183.It Dv GPIOUNSET (struct gpio_set) 184Unset the specified pin, i.e. clear its name and make it unaccessible 185at securelevels greater than 0. 186.Pp 187.It Dv GPIOATTACH (struct gpio_attach) 188Attach the device described in the 189.Fa gpio_attach 190structure on this gpio device. 191.Bd -literal 192struct gpio_attach { 193 char ga_dvname[16]; /* device name */ 194 int ga_offset; /* pin number */ 195 uint32_t ga_mask; /* binary mask */ 196 uint32_t ga_flags; /* driver dependent */ 197}; 198.Ed 199.Pp 200The 201.Xr drvctl 8 202command can be used to detach a device from a gpio pin. 203.El 204.Sh FILES 205.Bl -tag -width "/dev/gpiou" -compact 206.It /dev/gpio Ns Ar u 207GPIO device unit 208.Ar u 209file. 210.El 211.Sh SEE ALSO 212.Xr ioctl 2 , 213.Xr drvctl 8 , 214.Xr gpioctl 8 215.Sh HISTORY 216The 217.Nm 218device first appeared in 219.Ox 3.6 220and 221.Nx 4.0 . 222.Sh AUTHORS 223.An -nosplit 224The 225.Nm 226driver was written by 227.An Alexander Yurchenko Aq grange@openbsd.org . 228.Nm 229and was ported to 230.Nx 231by 232.An Jared D. McNeill Aq jmcneill@NetBSD.org . 233Runtime device attachment was added by 234.An Marc Balmer Aq marc@msys.ch . 235.Sh BUGS 236Event capabilities are not supported. 237