2 * Copyright (c) 2013-2014 Rui Paulo <rpaulo@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
41 gpio_open(unsigned int unit)
45 snprintf(device, sizeof(device), "/dev/gpioc%u", unit);
47 return (gpio_open_device(device));
51 gpio_open_device(const char *device)
56 fd = open(device, O_RDONLY);
58 return (GPIO_INVALID_HANDLE);
60 * Check whether a simple ioctl works.
62 if (ioctl(fd, GPIOMAXPIN, &maxpins) < 0) {
66 return (GPIO_INVALID_HANDLE);
73 gpio_close(gpio_handle_t handle)
79 gpio_pin_list(gpio_handle_t handle, gpio_config_t **pcfgs)
85 if (ioctl(handle, GPIOMAXPIN, &maxpins) < 0)
87 /* Reasonable values. */
88 if (maxpins < 0 || maxpins > 4096) {
92 cfgs = calloc(maxpins + 1, sizeof(*cfgs));
95 for (i = 0; i <= maxpins; i++) {
97 gpio_pin_config(handle, &cfgs[i]);
105 gpio_pin_config(gpio_handle_t handle, gpio_config_t *cfg)
107 struct gpio_pin gppin;
111 gppin.gp_pin = cfg->g_pin;
112 if (ioctl(handle, GPIOGETCONFIG, &gppin) < 0)
114 strlcpy(cfg->g_name, gppin.gp_name, GPIOMAXNAME);
115 cfg->g_caps = gppin.gp_caps;
116 cfg->g_flags = gppin.gp_flags;
122 gpio_pin_set_name(gpio_handle_t handle, gpio_pin_t pin, char *name)
124 struct gpio_pin gppin;
128 bzero(&gppin, sizeof(gppin));
130 strlcpy(gppin.gp_name, name, GPIOMAXNAME);
131 if (ioctl(handle, GPIOSETNAME, &gppin) < 0)
138 gpio_pin_set_flags(gpio_handle_t handle, gpio_config_t *cfg)
140 struct gpio_pin gppin;
144 gppin.gp_pin = cfg->g_pin;
145 gppin.gp_flags = cfg->g_flags;
146 if (ioctl(handle, GPIOSETCONFIG, &gppin) < 0)
153 gpio_pin_get(gpio_handle_t handle, gpio_pin_t pin)
155 struct gpio_req gpreq;
157 bzero(&gpreq, sizeof(gpreq));
159 if (ioctl(handle, GPIOGET, &gpreq) < 0)
160 return (GPIO_VALUE_INVALID);
162 return (gpreq.gp_value);
166 gpio_pin_set(gpio_handle_t handle, gpio_pin_t pin, gpio_value_t value)
168 struct gpio_req gpreq;
170 if (value == GPIO_VALUE_INVALID)
172 bzero(&gpreq, sizeof(gpreq));
174 gpreq.gp_value = value;
175 if (ioctl(handle, GPIOSET, &gpreq) < 0)
182 gpio_pin_toggle(gpio_handle_t handle, gpio_pin_t pin)
184 struct gpio_req gpreq;
186 bzero(&gpreq, sizeof(gpreq));
188 if (ioctl(handle, GPIOTOGGLE, &gpreq) < 0)
195 gpio_pin_low(gpio_handle_t handle, gpio_pin_t pin)
197 return (gpio_pin_set(handle, pin, GPIO_VALUE_LOW));
201 gpio_pin_high(gpio_handle_t handle, gpio_pin_t pin)
203 return (gpio_pin_set(handle, pin, GPIO_VALUE_HIGH));
207 gpio_pin_set_flag(gpio_handle_t handle, gpio_pin_t pin, uint32_t flag)
211 bzero(&cfg, sizeof(cfg));
213 if (gpio_pin_config(handle, &cfg) < 0)
217 return (gpio_pin_set_flags(handle, &cfg));
221 gpio_pin_input(gpio_handle_t handle, gpio_pin_t pin)
223 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_INPUT));
227 gpio_pin_output(gpio_handle_t handle, gpio_pin_t pin)
229 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_OUTPUT));
233 gpio_pin_opendrain(gpio_handle_t handle, gpio_pin_t pin)
235 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_OPENDRAIN));
239 gpio_pin_pushpull(gpio_handle_t handle, gpio_pin_t pin)
241 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_PUSHPULL));
245 gpio_pin_tristate(gpio_handle_t handle, gpio_pin_t pin)
247 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_TRISTATE));
251 gpio_pin_pullup(gpio_handle_t handle, gpio_pin_t pin)
253 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_PULLUP));
257 gpio_pin_pulldown(gpio_handle_t handle, gpio_pin_t pin)
259 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_PULLDOWN));
263 gpio_pin_invin(gpio_handle_t handle, gpio_pin_t pin)
265 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_INVIN));
269 gpio_pin_invout(gpio_handle_t handle, gpio_pin_t pin)
271 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_INVOUT));
275 gpio_pin_pulsate(gpio_handle_t handle, gpio_pin_t pin)
277 return (gpio_pin_set_flag(handle, pin, GPIO_PIN_PULSATE));