Index: sys/gpio.h =================================================================== --- sys/gpio.h (revision 230974) +++ sys/gpio.h (working copy) @@ -94,4 +94,26 @@ #define GPIOSET _IOW('G', 4, struct gpio_req) #define GPIOTOGGLE _IOWR('G', 5, struct gpio_req) +#ifdef _KERNEL + +/* + * Returns 0 if no GPIO device is available, otherwise - 1 + */ +int gpio_available(void); + +/* + * GPIO kobj methods exported as an API functions. Return 0 + * on success, error code otherwise. + */ +int gpio_pin_max(int unit, int *npins); +int gpio_pin_set(int unit, uint32_t pin_num, uint32_t pin_value); +int gpio_pin_get(int unit, uint32_t pin_num, uint32_t *pin_value); +int gpio_pin_toggle(int unit, uint32_t pin_num); +int gpio_pin_getcaps(int unit, uint32_t pin_num, uint32_t *caps); +int gpio_pin_getflags(int unit, uint32_t pin_num, uint32_t *flags); +int gpio_pin_getname(int unit, uint32_t pin_num, char *name); +int gpio_pin_setflags(int unit, uint32_t pin_num, uint32_t flags); + +#endif /* _KERNEL */ + #endif /* __GPIO_H__ */ Index: conf/files =================================================================== --- conf/files (revision 230974) +++ conf/files (working copy) @@ -1113,6 +1113,8 @@ dependency "gpiobus_if.h" dev/gpio/gpioc.c optional gpio \ dependency "gpio_if.h" +dev/gpio/gpio.c optional gpio \ + dependency "gpio_if.h" dev/gpio/gpioiic.c optional gpioiic dev/gpio/gpioled.c optional gpioled dev/gpio/gpio_if.m optional gpio Index: dev/gpio/gpio.c =================================================================== --- dev/gpio/gpio.c (revision 0) +++ dev/gpio/gpio.c (working copy) @@ -0,0 +1,133 @@ +/*- + * Copyright (c) 2012, Oleksandr Tymoshenko + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + * + */ + +#include +#include +#include +#include +#include +#include + +#include "gpio_if.h" + +int +gpio_available(void) +{ + + return (devclass_get_device(devclass_find("gpio"), 0) != NULL); +} + +int +gpio_pin_max(int unit, int *npins) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_MAX(gpiodev, npins); +} + +int +gpio_pin_set(int unit, uint32_t pin_num, uint32_t pin_value) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_SET(gpiodev, pin_num, pin_value); +} + +int +gpio_pin_get(int unit, uint32_t pin_num, uint32_t *pin_value) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_GET(gpiodev, pin_num, pin_value); +} + +int +gpio_pin_toggle(int unit, uint32_t pin_num) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_TOGGLE(gpiodev, pin_num); +} + +int +gpio_pin_getcaps(int unit, uint32_t pin_num, uint32_t *caps) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_GETCAPS(gpiodev, pin_num, caps); +} + +int +gpio_pin_getflags(int unit, uint32_t pin_num, uint32_t *flags) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_GETFLAGS(gpiodev, pin_num, flags); +} + +int +gpio_pin_getname(int unit, uint32_t pin_num, char *name) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_GETNAME(gpiodev, pin_num, name); +} + +int +gpio_pin_setflags(int unit, uint32_t pin_num, uint32_t flags) +{ + device_t gpiodev = devclass_get_device(devclass_find("gpio"), unit); + + if (gpiodev == NULL) + return (ENXIO); + + return GPIO_PIN_SETFLAGS(gpiodev, pin_num, flags); +}