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