xref: /dragonfly/sys/dev/misc/gpio/gpio.h (revision dcd37f7d)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *
35  * Copyright (c) 2008 Marc Balmer <mbalmer@openbsd.org>
36  * Copyright (c) 2004, 2006 Alexander Yurchenko <grange@openbsd.org>
37  *
38  * Permission to use, copy, modify, and distribute this software for any
39  * purpose with or without fee is hereby granted, provided that the above
40  * copyright notice and this permission notice appear in all copies.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
43  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
45  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
48  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49  */
50 
51 /* GPIO pin states */
52 #define GPIO_PIN_LOW		0x00	/* low level (logical 0) */
53 #define GPIO_PIN_HIGH		0x01	/* high level (logical 1) */
54 
55 /* Max name length of a pin */
56 #define GPIOPINMAXNAME		64
57 
58 /* GPIO pin configuration flags */
59 #define GPIO_PIN_INPUT		0x0001	/* input direction */
60 #define GPIO_PIN_OUTPUT		0x0002	/* output direction */
61 #define GPIO_PIN_INOUT		0x0004	/* bi-directional */
62 #define GPIO_PIN_OPENDRAIN	0x0008	/* open-drain output */
63 #define GPIO_PIN_PUSHPULL	0x0010	/* push-pull output */
64 #define GPIO_PIN_TRISTATE	0x0020	/* output disabled */
65 #define GPIO_PIN_PULLUP		0x0040	/* internal pull-up enabled */
66 #define GPIO_PIN_PULLDOWN	0x0080	/* internal pull-down enabled */
67 #define GPIO_PIN_INVIN		0x0100	/* invert input */
68 #define GPIO_PIN_INVOUT		0x0200	/* invert output */
69 #define GPIO_PIN_USER		0x0400	/* user != 0 can access */
70 #define GPIO_PIN_SET		0x8000	/* set for securelevel access */
71 
72 /* Consumer attach arg types */
73 #define	GPIO_TYPE_INT		0x01
74 
75 typedef struct gpio_pin {
76 	int	pin_num;		/* number */
77 	int	pin_caps;		/* capabilities */
78 	int	pin_flags;		/* current configuration */
79 	int	pin_state;		/* current state */
80 	int	pin_mapped;		/* is mapped */
81 	int	pin_opened;		/* is opened */
82 	cdev_t	dev;
83 } gpio_pin_t;
84 
85 struct gpio {
86 	const char	*driver_name;
87 	void		*arg;
88 	int		(*pin_read)(void *, int);
89 	void		(*pin_write)(void *, int, int);
90 	void		(*pin_ctl)(void *, int, int);
91 	gpio_pin_t	*pins;
92 	int		npins;
93 
94 	/* Private members */
95 	int		driver_unit;
96 	cdev_t		master_dev;
97 };
98 
99 struct gpio_consumer {
100 	const char	*consumer_name;
101 	int		(*consumer_attach)(struct gpio *, void *, int, u_int32_t);
102 	int		(*consumer_detach)(struct gpio *, void *, int);
103 	LIST_ENTRY(gpio_consumer)	link;
104 };
105 
106 struct gpio_info {
107 	int		npins;
108 	gpio_pin_t	*pins;
109 };
110 
111 struct gpio_attach_args {
112 	char		consumer_name[16];
113 	int		pin_offset;
114 	u_int32_t	pin_mask;
115 	int		arg_type;
116 	union {
117 		char	string[32];
118 		long	lint;
119 	} consumer_arg;
120 };
121 
122 struct gpio_pin_set_args {
123 	int	pin;
124 	int	caps;
125 	int	flags;
126 };
127 
128 struct gpio_mapping {
129 	struct gpio	*gp;
130 	int		*map;
131 	int		size;
132 
133 	int		map_alloced;
134 };
135 
136 void	gpio_consumer_register(struct gpio_consumer *gcp);
137 void	gpio_consumer_unregister(struct gpio_consumer *gcp);
138 int	gpio_consumer_attach(const char *consumer, void *arg, struct gpio *gp,
139 	    int pin, u_int32_t mask);
140 int	gpio_consumer_detach(const char *consumer, struct gpio *gp, int pin);
141 struct gpio_mapping *gpio_map(struct gpio *gp, int *map, int offset, u_int32_t mask);
142 void	gpio_unmap(struct gpio_mapping *gmp);
143 
144 int	gpio_npins(u_int32_t mask);
145 int	gpio_pin_read(struct gpio *gp, struct gpio_mapping *map, int pin);
146 void	gpio_pin_write(struct gpio  *gp, struct gpio_mapping *map, int pin, int data);
147 void	gpio_pin_ctl(struct gpio  *gp, struct gpio_mapping *map, int pin, int flags);
148 int	gpio_pin_caps(struct gpio  *gp, struct gpio_mapping *map, int pin);
149 void	gpio_register(struct gpio *gp);
150 void	gpio_unregister(struct gpio *gp);
151 
152 void	led_switch(const char *name, int on_off);
153 
154 #define GPIOINFO		_IOWR('G', 0, struct gpio_info)
155 #define GPIOPINSET		_IOWR('G', 4, struct gpio_pin_set_args)
156 #define GPIOPINUNSET		_IOWR('G', 5, struct gpio_pin_set_args)
157 #define GPIOATTACH		_IOWR('G', 6, struct gpio_attach_args)
158 #define GPIODETACH		_IOWR('G', 7, struct gpio_attach_args)
159