xref: /netbsd-src/share/man/man4/gpio.4 (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1.\" $NetBSD: gpio.4,v 1.28 2014/03/18 18:20:39 riastradh 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 June 10, 2013
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.Pp
36.In sys/types.h
37.In sys/gpio.h
38.In sys/ioctl.h
39.Sh DESCRIPTION
40The
41.Nm
42device attaches to the
43.Tn GPIO
44controller and provides a uniform programming interface to its pins.
45.Pp
46Each
47.Tn GPIO
48controller with an attached
49.Nm
50device has an associated device file under the
51.Pa /dev
52directory, e.g.\&
53.Pa /dev/gpio0 .
54Access from userland is performed through
55.Xr ioctl 2
56calls on these devices.
57.Pp
58Whether the layout of the GPIO device can be configured is subject to
59authorization by the
60.Xr kauth 9
61framework.
62.Pp
63If for example
64.Xr secmodel_securelevel 9
65is active, the layout of the GPIO device is defined at a securelevel
66less than 1, i.e. typically during system boot, and cannot be changed later.
67GPIO pins can be configured and given a symbolic name and device drivers
68that use GPIO pins can be attached to the
69.Nm
70device at a securelevel less than 1.
71All other pins will not be accessible once the runlevel has been raised.
72.Sh IOCTL INTERFACE
73The following structures and constants are defined in the
74.In sys/gpio.h
75header file:
76.Pp
77.Bl -tag -width XXXX -compact
78.It Dv GPIOINFO (struct gpio_info)
79Returns information about the
80.Tn GPIO
81controller in the
82.Fa gpio_info
83structure:
84.Bd -literal
85struct gpio_info {
86	int gpio_npins;		/* total number of pins available */
87};
88.Ed
89.Pp
90.It Dv GPIOREAD (struct gpio_req)
91Returns the input pin value in the
92.Fa gpio_pin_op
93structure:
94.Bd -literal
95#define GPIOMAXNAME		64
96
97struct gpio_req {
98	char gp_name[GPIOMAXNAME];	/* pin name */
99	int gp_pin;			/* pin number */
100	int gp_value;			/* value */
101};
102.Ed
103.Pp
104The
105.Fa gp_name
106or
107.Fa gp_pin
108field must be set before calling.
109.Pp
110.It Dv GPIOWRITE (struct gpio_req)
111Writes the output value to the pin.
112The value set in the
113.Fa gp_value
114field must be either
115.Dv GPIO_PIN_LOW
116(logical 0) or
117.Dv GPIO_PIN_HIGH
118(logical 1).
119On return, the
120.Fa gp_value
121field contains the old pin state.
122.Pp
123.It Dv GPIOTOGGLE (struct gpio_req)
124Toggles the pin output value, i.e. changes it to the opposite.
125.Fa gp_value
126field is ignored and on return contains the old pin state.
127.Pp
128.It Dv GPIOSET (struct gpio_set)
129Changes pin configuration flags with the new ones provided in the
130.Fa gpio_set
131structure:
132.Bd -literal
133#define GPIOMAXNAME          64
134
135struct gpio_set {
136        char gp_name[GPIOMAXNAME];   /* pin name */
137        int gp_pin;                     /* pin number */
138        int gp_caps;                    /* pin capabilities (ro) */
139        int gp_flags;                   /* pin configuration flags */
140        char gp_name2[GPIOMAXNAME];  /* new name */
141};
142.Ed
143.Pp
144The
145.Fa gp_flags
146field is a combination of the following flags:
147.Pp
148.Bl -tag -width GPIO_PIN_OPENDRAIN -compact
149.It Dv GPIO_PIN_INPUT
150input direction
151.It Dv GPIO_PIN_OUTPUT
152output direction
153.It Dv GPIO_PIN_INOUT
154bi-directional
155.It Dv GPIO_PIN_OPENDRAIN
156open-drain output
157.It Dv GPIO_PIN_PUSHPULL
158push-pull output
159.It Dv GPIO_PIN_TRISTATE
160output disabled
161.It Dv GPIO_PIN_PULLUP
162internal pull-up enabled
163.It Dv GPIO_PIN_PULLDOWN
164internal pull-down enabled
165.It Dv GPIO_PIN_INVIN
166invert input
167.It Dv GPIO_PIN_INVOUT
168invert output
169.It Dv GPIO_PIN_PULSATE
170pulsate output
171.El
172.Pp
173Note that the GPIO controller
174may not support all of these flags.
175On return the
176.Fa gp_caps
177field contains flags that are supported.
178If no flags are specified, the pin configuration stays unchanged.
179.Pp
180Only GPIO pins that have been set using
181.Ar GPIOSET
182will be accessible at securelevels greater than 0.
183.Pp
184.It Dv GPIOUNSET (struct gpio_set)
185Unset the specified pin, i.e. clear its name and make it unaccessible
186at securelevels greater than 0.
187.Pp
188.It Dv GPIOATTACH (struct gpio_attach)
189Attach the device described in the
190.Fa gpio_attach
191structure on this gpio device.
192.Bd -literal
193struct gpio_attach {
194        char ga_dvname[16];     /* device name */
195        int ga_offset;          /* pin number */
196        uint32_t ga_mask;       /* binary mask */
197        uint32_t ga_flags;      /* driver dependent */
198};
199.Ed
200.Pp
201The
202.Xr drvctl 8
203command can be used to detach a device from a gpio pin.
204.El
205.Sh FILES
206.Bl -tag -width "/dev/gpiou" -compact
207.It /dev/gpio Ns Ar u
208GPIO device unit
209.Ar u
210file.
211.El
212.Sh SEE ALSO
213.Xr ioctl 2 ,
214.Xr drvctl 8 ,
215.Xr gpioctl 8
216.Sh HISTORY
217The
218.Nm
219device first appeared in
220.Ox 3.6
221and
222.Nx 4.0 .
223.Sh AUTHORS
224.An -nosplit
225The
226.Nm
227driver was written by
228.An Alexander Yurchenko Aq Mt grange@openbsd.org .
229.Nm
230and was ported to
231.Nx
232by
233.An Jared D. McNeill Aq Mt jmcneill@NetBSD.org .
234Runtime device attachment was added by
235.An Marc Balmer Aq Mt marc@msys.ch .
236.Sh BUGS
237Event capabilities are not supported.
238