xref: /dragonfly/sys/dev/misc/gpio/gpio.h (revision 07f9383ee515180efb54f5e0a106b66aabb875a5)
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 #include <sys/ioccom.h>
52 
53 /* GPIO pin states */
54 #define GPIO_PIN_LOW                    0x00      /* low level (logical 0) */
55 #define GPIO_PIN_HIGH                   0x01      /* high level (logical 1) */
56 
57 /* Max name length of a pin */
58 #define GPIOPINMAXNAME                  64
59 
60 /* GPIO pin configuration flags */
61 #define GPIO_PIN_INPUT                  0x0001    /* input direction */
62 #define GPIO_PIN_OUTPUT                 0x0002    /* output direction */
63 #define GPIO_PIN_INOUT                  0x0004    /* bi-directional */
64 #define GPIO_PIN_OPENDRAIN    0x0008    /* open-drain output */
65 #define GPIO_PIN_PUSHPULL     0x0010    /* push-pull output */
66 #define GPIO_PIN_TRISTATE     0x0020    /* output disabled */
67 #define GPIO_PIN_PULLUP                 0x0040    /* internal pull-up enabled */
68 #define GPIO_PIN_PULLDOWN     0x0080    /* internal pull-down enabled */
69 #define GPIO_PIN_INVIN                  0x0100    /* invert input */
70 #define GPIO_PIN_INVOUT                 0x0200    /* invert output */
71 #define GPIO_PIN_USER                   0x0400    /* user != 0 can access */
72 #define GPIO_PIN_SET                    0x8000    /* set for securelevel access */
73 
74 /* Consumer attach arg types */
75 #define   GPIO_TYPE_INT                 0x01
76 
77 typedef struct gpio_pin {
78           int       pin_num;            /* number */
79           int       pin_caps;           /* capabilities */
80           int       pin_flags;                    /* current configuration */
81           int       pin_state;                    /* current state */
82           int       pin_mapped;                   /* is mapped */
83           int       pin_opened;                   /* is opened */
84           cdev_t    dev;
85 } gpio_pin_t;
86 
87 struct gpio {
88           const char          *driver_name;
89           void                *arg;
90           int                 (*pin_read)(void *, int);
91           void                (*pin_write)(void *, int, int);
92           void                (*pin_ctl)(void *, int, int);
93           gpio_pin_t          *pins;
94           int                 npins;
95 
96           /* Private members */
97           int                 driver_unit;
98           cdev_t              master_dev;
99 };
100 
101 struct gpio_consumer {
102           const char          *consumer_name;
103           int                 (*consumer_attach)(struct gpio *, void *, int, u_int32_t);
104           int                 (*consumer_detach)(struct gpio *, void *, int);
105           LIST_ENTRY(gpio_consumer)     link;
106 };
107 
108 struct gpio_info {
109           int                 npins;
110           gpio_pin_t          *pins;
111 };
112 
113 struct gpio_attach_args {
114           char                consumer_name[16];
115           int                 pin_offset;
116           u_int32_t pin_mask;
117           int                 arg_type;
118           union {
119                     char      string[32];
120                     long      lint;
121           } consumer_arg;
122 };
123 
124 struct gpio_pin_set_args {
125           int       pin;
126           int       caps;
127           int       flags;
128 };
129 
130 struct gpio_mapping {
131           struct gpio         *gp;
132           int                 *map;
133           int                 size;
134 
135           int                 map_alloced;
136 };
137 
138 void      gpio_consumer_register(struct gpio_consumer *gcp);
139 void      gpio_consumer_unregister(struct gpio_consumer *gcp);
140 int       gpio_consumer_attach(const char *consumer, void *arg, struct gpio *gp,
141               int pin, u_int32_t mask);
142 int       gpio_consumer_detach(const char *consumer, struct gpio *gp, int pin);
143 struct gpio_mapping *gpio_map(struct gpio *gp, int *map, int offset, u_int32_t mask);
144 void      gpio_unmap(struct gpio_mapping *gmp);
145 
146 int       gpio_npins(u_int32_t mask);
147 int       gpio_pin_read(struct gpio *gp, struct gpio_mapping *map, int pin);
148 void      gpio_pin_write(struct gpio  *gp, struct gpio_mapping *map, int pin, int data);
149 void      gpio_pin_ctl(struct gpio  *gp, struct gpio_mapping *map, int pin, int flags);
150 int       gpio_pin_caps(struct gpio  *gp, struct gpio_mapping *map, int pin);
151 void      gpio_register(struct gpio *gp);
152 void      gpio_unregister(struct gpio *gp);
153 
154 void      led_switch(const char *name, int on_off);
155 
156 #define GPIOINFO              _IOWR('G', 0, struct gpio_info)
157 #define GPIOPINSET            _IOWR('G', 4, struct gpio_pin_set_args)
158 #define GPIOPINUNSET                    _IOWR('G', 5, struct gpio_pin_set_args)
159 #define GPIOATTACH            _IOWR('G', 6, struct gpio_attach_args)
160 #define GPIODETACH            _IOWR('G', 7, struct gpio_attach_args)
161