2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 * Copyright (c) 2008 Marc Balmer <mbalmer@openbsd.org>
36 * Copyright (c) 2004, 2006 Alexander Yurchenko <grange@openbsd.org>
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.
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.
51 #include <sys/ioccom.h>
54 #define GPIO_PIN_LOW 0x00 /* low level (logical 0) */
55 #define GPIO_PIN_HIGH 0x01 /* high level (logical 1) */
57 /* Max name length of a pin */
58 #define GPIOPINMAXNAME 64
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 */
74 /* Consumer attach arg types */
75 #define GPIO_TYPE_INT 0x01
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 */
88 const char *driver_name;
90 int (*pin_read)(void *, int);
91 void (*pin_write)(void *, int, int);
92 void (*pin_ctl)(void *, int, int);
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;
113 struct gpio_attach_args {
114 char consumer_name[16];
124 struct gpio_pin_set_args {
130 struct gpio_mapping {
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);
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);
154 void led_switch(const char *name, int on_off);
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)