xref: /freebsd/lib/libgpio/libgpio.h (revision 0355ecace82cce8322e5fcdf48f751d253792ce3)
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 
27f12128e4SRui Paulo #ifndef _LIBGPIO_H_
28f12128e4SRui Paulo #define _LIBGPIO_H_
29f12128e4SRui Paulo 
30f12128e4SRui Paulo #include <sys/gpio.h>
31f12128e4SRui Paulo 
32f12128e4SRui Paulo __BEGIN_DECLS
33f12128e4SRui Paulo 
34f12128e4SRui Paulo #define	GPIO_INVALID_HANDLE -1
35f12128e4SRui Paulo typedef int gpio_handle_t;
36f12128e4SRui Paulo typedef uint32_t gpio_pin_t;
37f12128e4SRui Paulo 
38f12128e4SRui Paulo /*
39f12128e4SRui Paulo  * Structure describing a GPIO pin configuration.
40f12128e4SRui Paulo  */
41f12128e4SRui Paulo typedef struct {
42f12128e4SRui Paulo 	gpio_pin_t	g_pin;
43f12128e4SRui Paulo 	char 		g_name[GPIOMAXNAME];
44f12128e4SRui Paulo 	uint32_t	g_caps;
45f12128e4SRui Paulo 	uint32_t	g_flags;
46f12128e4SRui Paulo } gpio_config_t;
47f12128e4SRui Paulo 
48f12128e4SRui Paulo typedef enum {
49f12128e4SRui Paulo 	GPIO_VALUE_INVALID 	= -1,
50f12128e4SRui Paulo 	GPIO_VALUE_LOW 		= GPIO_PIN_LOW,
51f12128e4SRui Paulo 	GPIO_VALUE_HIGH 	= GPIO_PIN_HIGH
52f12128e4SRui Paulo } gpio_value_t;
53f12128e4SRui Paulo 
54f12128e4SRui Paulo /*
55f12128e4SRui Paulo  * Open /dev/gpiocN or a specific device.
56f12128e4SRui Paulo  */
57f12128e4SRui Paulo gpio_handle_t	gpio_open(unsigned int);
58f12128e4SRui Paulo gpio_handle_t	gpio_open_device(const char *);
59f12128e4SRui Paulo void		gpio_close(gpio_handle_t);
60f12128e4SRui Paulo /*
61f12128e4SRui Paulo  * Get a list of all the GPIO pins.
62f12128e4SRui Paulo  */
63f12128e4SRui Paulo int		gpio_pin_list(gpio_handle_t, gpio_config_t **);
64f12128e4SRui Paulo /*
65f12128e4SRui Paulo  * GPIO pin configuration.
66f12128e4SRui Paulo  *
67f12128e4SRui Paulo  * Retrieve the configuration of a specific GPIO pin.  The pin number is
68f12128e4SRui Paulo  * passed through the gpio_config_t structure.
69f12128e4SRui Paulo  */
70f12128e4SRui Paulo int		gpio_pin_config(gpio_handle_t, gpio_config_t *);
71f12128e4SRui Paulo /*
72d752f0f6SLuiz Otavio O Souza  * Sets the GPIO pin name.  The pin number and pin name to be set are passed
73d752f0f6SLuiz Otavio O Souza  * as parameters.
74d752f0f6SLuiz Otavio O Souza  */
75d752f0f6SLuiz Otavio O Souza int		gpio_pin_set_name(gpio_handle_t, gpio_pin_t, char *);
76d752f0f6SLuiz Otavio O Souza /*
77f12128e4SRui Paulo  * Sets the GPIO flags on a specific GPIO pin.  The pin number and the flags
78f12128e4SRui Paulo  * to be set are passed through the gpio_config_t structure.
79f12128e4SRui Paulo  */
80f12128e4SRui Paulo int		gpio_pin_set_flags(gpio_handle_t, gpio_config_t *);
81f12128e4SRui Paulo /*
82f12128e4SRui Paulo  * GPIO pin values.
83f12128e4SRui Paulo  */
84*0355ecacSJohn Baldwin gpio_value_t	gpio_pin_get(gpio_handle_t, gpio_pin_t);
85*0355ecacSJohn Baldwin int		gpio_pin_set(gpio_handle_t, gpio_pin_t, gpio_value_t);
86f12128e4SRui Paulo int		gpio_pin_toggle(gpio_handle_t, gpio_pin_t);
87f12128e4SRui Paulo /*
88f12128e4SRui Paulo  * Helper functions to set pin states.
89f12128e4SRui Paulo  */
90f12128e4SRui Paulo int		gpio_pin_low(gpio_handle_t, gpio_pin_t);
91f12128e4SRui Paulo int		gpio_pin_high(gpio_handle_t, gpio_pin_t);
92f12128e4SRui Paulo /*
93f12128e4SRui Paulo  * Helper functions to configure pins.
94f12128e4SRui Paulo  */
95f12128e4SRui Paulo int		gpio_pin_input(gpio_handle_t, gpio_pin_t);
96f12128e4SRui Paulo int		gpio_pin_output(gpio_handle_t, gpio_pin_t);
97f12128e4SRui Paulo int		gpio_pin_opendrain(gpio_handle_t, gpio_pin_t);
98f12128e4SRui Paulo int		gpio_pin_pushpull(gpio_handle_t, gpio_pin_t);
99f12128e4SRui Paulo int		gpio_pin_tristate(gpio_handle_t, gpio_pin_t);
100f12128e4SRui Paulo int		gpio_pin_pullup(gpio_handle_t, gpio_pin_t);
101f12128e4SRui Paulo int		gpio_pin_pulldown(gpio_handle_t, gpio_pin_t);
102f12128e4SRui Paulo int		gpio_pin_invin(gpio_handle_t, gpio_pin_t);
103f12128e4SRui Paulo int		gpio_pin_invout(gpio_handle_t, gpio_pin_t);
104f12128e4SRui Paulo int		gpio_pin_pulsate(gpio_handle_t, gpio_pin_t);
105f12128e4SRui Paulo 
106f12128e4SRui Paulo __END_DECLS
107f12128e4SRui Paulo 
108f12128e4SRui Paulo #endif /* _LIBGPIO_H_ */
109