gpioc.c revision 213237
1#include <sys/cdefs.h> 2__FBSDID("$FreeBSD: head/sys/dev/gpio/gpioc.c 213237 2010-09-28 03:24:53Z gonzo $"); 3 4#include <sys/param.h> 5#include <sys/systm.h> 6#include <sys/types.h> 7 8#include <sys/bus.h> 9#include <sys/conf.h> 10#include <sys/ioccom.h> 11#include <sys/kernel.h> 12#include <sys/malloc.h> 13#include <sys/module.h> 14#include <sys/queue.h> 15#include <machine/bus.h> 16#include <machine/resource.h> 17 18#include <sys/gpio.h> 19#include "gpio_if.h" 20 21#undef GPIOC_DEBUG 22#ifdef GPIOC_DEBUG 23#define dprintf printf 24#else 25#define dprintf(x, arg...) 26#endif 27 28static int gpioc_probe(device_t dev); 29static int gpioc_attach(device_t dev); 30static int gpioc_detach(device_t dev); 31 32static d_ioctl_t gpioc_ioctl; 33 34static struct cdevsw gpioc_cdevsw = { 35 .d_version = D_VERSION, 36 .d_ioctl = gpioc_ioctl, 37 .d_name = "gpioc", 38#if __FreeBSD_version >= 800039 39 .d_flags = D_PSEUDO | D_NEEDMINOR 40#endif 41}; 42 43struct gpioc_softc { 44 device_t sc_dev; /* gpiocX dev */ 45 device_t sc_pdev; /* gpioX dev */ 46 struct cdev *sc_ctl_dev; /* controller device */ 47 int sc_unit; 48}; 49 50static int 51gpioc_probe(device_t dev) 52{ 53 device_set_desc(dev, "GPIO controller"); 54 return (0); 55} 56 57static int 58gpioc_attach(device_t dev) 59{ 60 struct gpioc_softc *sc = device_get_softc(dev); 61 62 sc->sc_dev = dev; 63 sc->sc_pdev = device_get_parent(dev); 64 sc->sc_unit = device_get_unit(dev); 65 sc->sc_ctl_dev = make_dev(&gpioc_cdevsw, sc->sc_unit, 66 UID_ROOT, GID_WHEEL, 0600, "gpioc%d", sc->sc_unit); 67 if (!sc->sc_ctl_dev) { 68 printf("Failed to create gpioc%d", sc->sc_unit); 69 return (ENXIO); 70 } 71 sc->sc_ctl_dev->si_drv1 = sc; 72 73 return (0); 74} 75 76static int 77gpioc_detach(device_t dev) 78{ 79 struct gpioc_softc *sc = device_get_softc(dev); 80 int err; 81 82 if (sc->sc_ctl_dev); 83 destroy_dev(sc->sc_ctl_dev); 84 85 if ((err = bus_generic_detach(dev)) != 0) 86 return (err); 87 88 return (0); 89} 90 91static int 92gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag, 93 struct thread *td) 94{ 95 int max_pin, res; 96 struct gpioc_softc *sc = cdev->si_drv1; 97 struct gpio_pin pin; 98 struct gpio_req req; 99 100 switch (cmd) { 101 case GPIOMAXPIN: 102 max_pin = -1; 103 res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin); 104 bcopy(&max_pin, arg, sizeof(max_pin)); 105 break; 106 case GPIOGETCONFIG: 107 bcopy(arg, &pin, sizeof(pin)); 108 dprintf("get config pin %d\n", pin.gp_pin); 109 res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin, 110 &pin.gp_flags); 111 /* Fail early */ 112 if (res) 113 break; 114 GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps); 115 GPIO_PIN_GETNAME(sc->sc_pdev, pin.gp_pin, pin.gp_name); 116 bcopy(&pin, arg, sizeof(pin)); 117 break; 118 case GPIOSETCONFIG: 119 bcopy(arg, &pin, sizeof(pin)); 120 dprintf("set config pin %d\n", pin.gp_pin); 121 res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin, 122 pin.gp_flags); 123 break; 124 case GPIOGET: 125 bcopy(arg, &req, sizeof(req)); 126 res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin, 127 &req.gp_value); 128 dprintf("read pin %d -> %d\n", 129 req.gp_pin, req.gp_value); 130 bcopy(&req, arg, sizeof(req)); 131 break; 132 case GPIOSET: 133 bcopy(arg, &req, sizeof(req)); 134 res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin, 135 req.gp_value); 136 dprintf("write pin %d -> %d\n", 137 req.gp_pin, req.gp_value); 138 break; 139 case GPIOTOGGLE: 140 bcopy(arg, &req, sizeof(req)); 141 dprintf("toggle pin %d\n", 142 req.gp_pin); 143 res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin); 144 break; 145 default: 146 return (ENOTTY); 147 break; 148 } 149 150 return (res); 151} 152 153static device_method_t gpioc_methods[] = { 154 /* Device interface */ 155 DEVMETHOD(device_probe, gpioc_probe), 156 DEVMETHOD(device_attach, gpioc_attach), 157 DEVMETHOD(device_detach, gpioc_detach), 158 DEVMETHOD(device_shutdown, bus_generic_shutdown), 159 DEVMETHOD(device_suspend, bus_generic_suspend), 160 DEVMETHOD(device_resume, bus_generic_resume), 161 162 { 0, 0 } 163}; 164 165static driver_t gpioc_driver = { 166 "gpioc", 167 gpioc_methods, 168 sizeof(struct gpioc_softc) 169}; 170 171devclass_t gpioc_devclass; 172 173DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0); 174MODULE_VERSION(gpioc, 1); 175