OpenBSD manual page server

Manual Page Search Parameters

GPIO(4) Device Drivers Manual GPIO(4)

gpioGeneral Purpose Input/Output

gpio* at ath?
gpio* at bcmgpio? (arm64, armv7)
gpio* at glxpcib? (i386)
gpio* at gscpcib? (i386)
gpio* at isagpio?
gpio* at mpfgpio? (riscv64)
gpio* at nsclpcsio?
gpio* at omgpio? (armv7)
gpio* at pcagpio?
gpio* at pcaled?
gpio* at skgpio? (amd64, i386)
gpio* at sxipio? (arm64, armv7)
gpio0 at voyager? (loongson)


#include <sys/types.h>
#include <sys/gpio.h>
#include <sys/ioctl.h>

The gpio device attaches to the GPIO controller and provides a uniform programming interface to its pins.

Each GPIO controller with an attached gpio device has an associated device file under the /dev directory, e.g. /dev/gpio0. Access from userland is performed through ioctl(2) calls on these devices.

The layout of the GPIO device is defined at securelevel 0, i.e. typically during system boot, and cannot be changed later. GPIO pins can be configured and given a symbolic name and device drivers that use GPIO pins can be attached to the gpio device at securelevel 0. All other pins will not be accessible once the runlevel has been raised.

The following structures and constants are defined in the <sys/gpio.h> header file:

struct gpio_info
Returns information about the GPIO controller in the gpio_info structure:
struct gpio_info {
	int gpio_npins;		/* total number of pins available */
};
struct gpio_pin_op
Returns the input pin value in the gpio_pin_op structure:
#define GPIOPINMAXNAME		64

struct gpio_pin_op {
	char gp_name[GPIOPINMAXNAME];	/* pin name */
	int gp_pin;			/* pin number */
	int gp_value;			/* value */
};

The gp_name or gp_pin field must be set before calling.

struct gpio_pin_op
Writes the output value to the pin. The value set in the gp_value field must be either GPIO_PIN_LOW (logical 0) or GPIO_PIN_HIGH (logical 1). On return, the gp_value field contains the old pin state.
struct gpio_pin_op
Toggles the pin output value, i.e. changes it to the opposite. gp_value field is ignored and on return contains the old pin state.
struct gpio_pin_set
Changes pin configuration flags with the new ones provided in the gpio_pin_set structure:
#define GPIOPINMAXNAME		64

struct gpio_pin_set {
	char gp_name[GPIOPINMAXNAME];	/* pin name */
	int gp_pin;			/* pin number */
	int gp_caps;			/* pin capabilities (ro) */
	int gp_flags;			/* pin configuration flags */
	char gp_name2[GPIOPINMAXNAME];	/* new name */
};

The gp_flags field is a combination of the following flags:

input direction
output direction
bi-directional
open-drain output
push-pull output
output disabled
internal pull-up enabled
internal pull-down enabled
invert input
invert output

Note that the GPIO controller may not support all of these flags. On return the gp_caps field contains flags that are supported. If no flags are specified, the pin configuration stays unchanged.

Only GPIO pins that have been set using GPIOPINSET will be accessible at securelevels greater than 0.

struct gpio_pin_set
Unset the specified pin, i.e. clear its name and make it inaccessible at securelevels greater than 0.
struct gpio_attach
Attach the device described in the gpio_attach structure on this gpio device.
struct gpio_attach {
	char ga_dvname[16];     /* device name */
	int ga_offset;		/* pin number */
	u_int32_t ga_mask;	/* binary mask */
};
struct gpio_attach
Detach a device from this gpio device that was previously attached using the GPIOATTACH ioctl(2). The ga_offset and ga_mask fields of the gpio_attach structure are ignored.

/dev/gpiou
GPIO device unit u file.

ioctl(2), gpioctl(8)

The gpio device first appeared in OpenBSD 3.6.

The gpio driver was written by Alexander Yurchenko <grange@openbsd.org>. Runtime device attachment was added by Marc Balmer <mbalmer@openbsd.org>.

Event capabilities are not supported.

January 17, 2023 OpenBSD-current