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