xref: /netbsd-src/share/man/man4/gpio.4 (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1.\" $NetBSD: gpio.4,v 1.30 2015/12/06 09:38:21 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 December 6, 2015
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.It Dv GPIO_PIN_ALT0 -
172.It Dv GPIO_PIN_ALT7
173select alternate pin function 0 to 7
174.It Dv GPIO_PIN_EVENTS
175deliver events
176.It Dv GPIO_PIN_LEVEL
177trigger on pin level instead of edge
178.It Dv GPIO_PIN_FALLING
179trigger on falling instead of rising edge
180.El
181.Pp
182Note that the GPIO controller
183may not support all of these flags.
184On return the
185.Fa gp_caps
186field contains flags that are supported.
187If no flags are specified, the pin configuration stays unchanged.
188.Pp
189Only GPIO pins that have been set using
190.Ar GPIOSET
191will be accessible at securelevels greater than 0.
192.Pp
193.It Dv GPIOUNSET (struct gpio_set)
194Unset the specified pin, i.e. clear its name and make it unaccessible
195at securelevels greater than 0.
196.Pp
197.It Dv GPIOATTACH (struct gpio_attach)
198Attach the device described in the
199.Fa gpio_attach
200structure on this gpio device.
201.Bd -literal
202struct gpio_attach {
203        char ga_dvname[16];     /* device name */
204        int ga_offset;          /* pin number */
205        uint32_t ga_mask;       /* binary mask */
206        uint32_t ga_flags;      /* driver dependent */
207};
208.Ed
209.Pp
210The
211.Xr drvctl 8
212command can be used to detach a device from a gpio pin.
213.El
214.Sh FILES
215.Bl -tag -width "/dev/gpiou" -compact
216.It /dev/gpio Ns Ar u
217GPIO device unit
218.Ar u
219file.
220.El
221.Sh SEE ALSO
222.Xr ioctl 2 ,
223.Xr drvctl 8 ,
224.Xr gpioctl 8
225.Sh HISTORY
226The
227.Nm
228device first appeared in
229.Ox 3.6
230and
231.Nx 4.0 .
232.Sh AUTHORS
233.An -nosplit
234The
235.Nm
236driver was written by
237.An Alexander Yurchenko Aq Mt grange@openbsd.org .
238.Nm
239and was ported to
240.Nx
241by
242.An Jared D. McNeill Aq Mt jmcneill@NetBSD.org .
243Runtime device attachment was added by
244.An Marc Balmer Aq Mt marc@msys.ch .
245.Sh BUGS
246Event capabilities are not supported.
247