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