1 /*- 2 * Copyright (c) 2013-2014 Rui Paulo <rpaulo@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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. 25 * 26 * $FreeBSD$ 27 */ 28 29 #ifndef _LIBGPIO_H_ 30 #define _LIBGPIO_H_ 31 32 #include <sys/gpio.h> 33 34 __BEGIN_DECLS 35 36 #define GPIO_INVALID_HANDLE -1 37 typedef int gpio_handle_t; 38 typedef uint32_t gpio_pin_t; 39 40 /* 41 * Structure describing a GPIO pin configuration. 42 */ 43 typedef struct { 44 gpio_pin_t g_pin; 45 char g_name[GPIOMAXNAME]; 46 uint32_t g_caps; 47 uint32_t g_flags; 48 } gpio_config_t; 49 50 typedef enum { 51 GPIO_VALUE_INVALID = -1, 52 GPIO_VALUE_LOW = GPIO_PIN_LOW, 53 GPIO_VALUE_HIGH = GPIO_PIN_HIGH 54 } gpio_value_t; 55 56 /* 57 * Open /dev/gpiocN or a specific device. 58 */ 59 gpio_handle_t gpio_open(unsigned int); 60 gpio_handle_t gpio_open_device(const char *); 61 void gpio_close(gpio_handle_t); 62 /* 63 * Get a list of all the GPIO pins. 64 */ 65 int gpio_pin_list(gpio_handle_t, gpio_config_t **); 66 /* 67 * GPIO pin configuration. 68 * 69 * Retrieve the configuration of a specific GPIO pin. The pin number is 70 * passed through the gpio_config_t structure. 71 */ 72 int gpio_pin_config(gpio_handle_t, gpio_config_t *); 73 /* 74 * Sets the GPIO flags on a specific GPIO pin. The pin number and the flags 75 * to be set are passed through the gpio_config_t structure. 76 */ 77 int gpio_pin_set_flags(gpio_handle_t, gpio_config_t *); 78 /* 79 * GPIO pin values. 80 */ 81 int gpio_pin_get(gpio_handle_t, gpio_pin_t); 82 int gpio_pin_set(gpio_handle_t, gpio_pin_t, int); 83 int gpio_pin_toggle(gpio_handle_t, gpio_pin_t); 84 /* 85 * Helper functions to set pin states. 86 */ 87 int gpio_pin_low(gpio_handle_t, gpio_pin_t); 88 int gpio_pin_high(gpio_handle_t, gpio_pin_t); 89 /* 90 * Helper functions to configure pins. 91 */ 92 int gpio_pin_input(gpio_handle_t, gpio_pin_t); 93 int gpio_pin_output(gpio_handle_t, gpio_pin_t); 94 int gpio_pin_opendrain(gpio_handle_t, gpio_pin_t); 95 int gpio_pin_pushpull(gpio_handle_t, gpio_pin_t); 96 int gpio_pin_tristate(gpio_handle_t, gpio_pin_t); 97 int gpio_pin_pullup(gpio_handle_t, gpio_pin_t); 98 int gpio_pin_pulldown(gpio_handle_t, gpio_pin_t); 99 int gpio_pin_invin(gpio_handle_t, gpio_pin_t); 100 int gpio_pin_invout(gpio_handle_t, gpio_pin_t); 101 int gpio_pin_pulsate(gpio_handle_t, gpio_pin_t); 102 103 __END_DECLS 104 105 #endif /* _LIBGPIO_H_ */ 106