1f12128e4SRui Paulo /*- 2f12128e4SRui Paulo * Copyright (c) 2013-2014 Rui Paulo <rpaulo@FreeBSD.org> 3f12128e4SRui Paulo * All rights reserved. 4f12128e4SRui Paulo * 5f12128e4SRui Paulo * Redistribution and use in source and binary forms, with or without 6f12128e4SRui Paulo * modification, are permitted provided that the following conditions 7f12128e4SRui Paulo * are met: 8f12128e4SRui Paulo * 1. Redistributions of source code must retain the above copyright 9f12128e4SRui Paulo * notice, this list of conditions and the following disclaimer. 10f12128e4SRui Paulo * 2. Redistributions in binary form must reproduce the above copyright 11f12128e4SRui Paulo * notice, this list of conditions and the following disclaimer in the 12f12128e4SRui Paulo * documentation and/or other materials provided with the distribution. 13f12128e4SRui Paulo * 14f12128e4SRui Paulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15f12128e4SRui Paulo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16f12128e4SRui Paulo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17f12128e4SRui Paulo * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 18f12128e4SRui Paulo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19f12128e4SRui Paulo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20f12128e4SRui Paulo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21f12128e4SRui Paulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22f12128e4SRui Paulo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 23f12128e4SRui Paulo * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24f12128e4SRui Paulo * POSSIBILITY OF SUCH DAMAGE. 25f12128e4SRui Paulo * 26f12128e4SRui Paulo * $FreeBSD$ 27f12128e4SRui Paulo */ 28f12128e4SRui Paulo 29f12128e4SRui Paulo #ifndef _LIBGPIO_H_ 30f12128e4SRui Paulo #define _LIBGPIO_H_ 31f12128e4SRui Paulo 32f12128e4SRui Paulo #include <sys/gpio.h> 33f12128e4SRui Paulo 34f12128e4SRui Paulo __BEGIN_DECLS 35f12128e4SRui Paulo 36f12128e4SRui Paulo #define GPIO_INVALID_HANDLE -1 37f12128e4SRui Paulo typedef int gpio_handle_t; 38f12128e4SRui Paulo typedef uint32_t gpio_pin_t; 39f12128e4SRui Paulo 40f12128e4SRui Paulo /* 41f12128e4SRui Paulo * Structure describing a GPIO pin configuration. 42f12128e4SRui Paulo */ 43f12128e4SRui Paulo typedef struct { 44f12128e4SRui Paulo gpio_pin_t g_pin; 45f12128e4SRui Paulo char g_name[GPIOMAXNAME]; 46f12128e4SRui Paulo uint32_t g_caps; 47f12128e4SRui Paulo uint32_t g_flags; 48f12128e4SRui Paulo } gpio_config_t; 49f12128e4SRui Paulo 50f12128e4SRui Paulo typedef enum { 51f12128e4SRui Paulo GPIO_VALUE_INVALID = -1, 52f12128e4SRui Paulo GPIO_VALUE_LOW = GPIO_PIN_LOW, 53f12128e4SRui Paulo GPIO_VALUE_HIGH = GPIO_PIN_HIGH 54f12128e4SRui Paulo } gpio_value_t; 55f12128e4SRui Paulo 56f12128e4SRui Paulo /* 57f12128e4SRui Paulo * Open /dev/gpiocN or a specific device. 58f12128e4SRui Paulo */ 59f12128e4SRui Paulo gpio_handle_t gpio_open(unsigned int); 60f12128e4SRui Paulo gpio_handle_t gpio_open_device(const char *); 61f12128e4SRui Paulo void gpio_close(gpio_handle_t); 62f12128e4SRui Paulo /* 63f12128e4SRui Paulo * Get a list of all the GPIO pins. 64f12128e4SRui Paulo */ 65f12128e4SRui Paulo int gpio_pin_list(gpio_handle_t, gpio_config_t **); 66f12128e4SRui Paulo /* 67f12128e4SRui Paulo * GPIO pin configuration. 68f12128e4SRui Paulo * 69f12128e4SRui Paulo * Retrieve the configuration of a specific GPIO pin. The pin number is 70f12128e4SRui Paulo * passed through the gpio_config_t structure. 71f12128e4SRui Paulo */ 72f12128e4SRui Paulo int gpio_pin_config(gpio_handle_t, gpio_config_t *); 73f12128e4SRui Paulo /* 74*d752f0f6SLuiz Otavio O Souza * Sets the GPIO pin name. The pin number and pin name to be set are passed 75*d752f0f6SLuiz Otavio O Souza * as parameters. 76*d752f0f6SLuiz Otavio O Souza */ 77*d752f0f6SLuiz Otavio O Souza int gpio_pin_set_name(gpio_handle_t, gpio_pin_t, char *); 78*d752f0f6SLuiz Otavio O Souza /* 79f12128e4SRui Paulo * Sets the GPIO flags on a specific GPIO pin. The pin number and the flags 80f12128e4SRui Paulo * to be set are passed through the gpio_config_t structure. 81f12128e4SRui Paulo */ 82f12128e4SRui Paulo int gpio_pin_set_flags(gpio_handle_t, gpio_config_t *); 83f12128e4SRui Paulo /* 84f12128e4SRui Paulo * GPIO pin values. 85f12128e4SRui Paulo */ 86f12128e4SRui Paulo int gpio_pin_get(gpio_handle_t, gpio_pin_t); 87f12128e4SRui Paulo int gpio_pin_set(gpio_handle_t, gpio_pin_t, int); 88f12128e4SRui Paulo int gpio_pin_toggle(gpio_handle_t, gpio_pin_t); 89f12128e4SRui Paulo /* 90f12128e4SRui Paulo * Helper functions to set pin states. 91f12128e4SRui Paulo */ 92f12128e4SRui Paulo int gpio_pin_low(gpio_handle_t, gpio_pin_t); 93f12128e4SRui Paulo int gpio_pin_high(gpio_handle_t, gpio_pin_t); 94f12128e4SRui Paulo /* 95f12128e4SRui Paulo * Helper functions to configure pins. 96f12128e4SRui Paulo */ 97f12128e4SRui Paulo int gpio_pin_input(gpio_handle_t, gpio_pin_t); 98f12128e4SRui Paulo int gpio_pin_output(gpio_handle_t, gpio_pin_t); 99f12128e4SRui Paulo int gpio_pin_opendrain(gpio_handle_t, gpio_pin_t); 100f12128e4SRui Paulo int gpio_pin_pushpull(gpio_handle_t, gpio_pin_t); 101f12128e4SRui Paulo int gpio_pin_tristate(gpio_handle_t, gpio_pin_t); 102f12128e4SRui Paulo int gpio_pin_pullup(gpio_handle_t, gpio_pin_t); 103f12128e4SRui Paulo int gpio_pin_pulldown(gpio_handle_t, gpio_pin_t); 104f12128e4SRui Paulo int gpio_pin_invin(gpio_handle_t, gpio_pin_t); 105f12128e4SRui Paulo int gpio_pin_invout(gpio_handle_t, gpio_pin_t); 106f12128e4SRui Paulo int gpio_pin_pulsate(gpio_handle_t, gpio_pin_t); 107f12128e4SRui Paulo 108f12128e4SRui Paulo __END_DECLS 109f12128e4SRui Paulo 110f12128e4SRui Paulo #endif /* _LIBGPIO_H_ */ 111