1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org> 5 * All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/gpio.h> 37 #include <sys/ioccom.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 42 #include <dev/gpio/gpiobusvar.h> 43 44 #include "gpio_if.h" 45 #include "gpiobus_if.h" 46 47 #undef GPIOC_DEBUG 48 #ifdef GPIOC_DEBUG 49 #define dprintf printf 50 #else 51 #define dprintf(x, arg...) 52 #endif 53 54 static int gpioc_probe(device_t dev); 55 static int gpioc_attach(device_t dev); 56 static int gpioc_detach(device_t dev); 57 58 static d_ioctl_t gpioc_ioctl; 59 60 static struct cdevsw gpioc_cdevsw = { 61 .d_version = D_VERSION, 62 .d_ioctl = gpioc_ioctl, 63 .d_name = "gpioc", 64 }; 65 66 struct gpioc_softc { 67 device_t sc_dev; /* gpiocX dev */ 68 device_t sc_pdev; /* gpioX dev */ 69 struct cdev *sc_ctl_dev; /* controller device */ 70 int sc_unit; 71 }; 72 73 static int 74 gpioc_probe(device_t dev) 75 { 76 device_set_desc(dev, "GPIO controller"); 77 return (0); 78 } 79 80 static int 81 gpioc_attach(device_t dev) 82 { 83 int err; 84 struct gpioc_softc *sc; 85 struct make_dev_args devargs; 86 87 sc = device_get_softc(dev); 88 sc->sc_dev = dev; 89 sc->sc_pdev = device_get_parent(dev); 90 sc->sc_unit = device_get_unit(dev); 91 make_dev_args_init(&devargs); 92 devargs.mda_devsw = &gpioc_cdevsw; 93 devargs.mda_uid = UID_ROOT; 94 devargs.mda_gid = GID_WHEEL; 95 devargs.mda_mode = 0600; 96 devargs.mda_si_drv1 = sc; 97 err = make_dev_s(&devargs, &sc->sc_ctl_dev, "gpioc%d", sc->sc_unit); 98 if (err != 0) { 99 printf("Failed to create gpioc%d", sc->sc_unit); 100 return (ENXIO); 101 } 102 103 return (0); 104 } 105 106 static int 107 gpioc_detach(device_t dev) 108 { 109 struct gpioc_softc *sc = device_get_softc(dev); 110 int err; 111 112 if (sc->sc_ctl_dev) 113 destroy_dev(sc->sc_ctl_dev); 114 115 if ((err = bus_generic_detach(dev)) != 0) 116 return (err); 117 118 return (0); 119 } 120 121 static int 122 gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag, 123 struct thread *td) 124 { 125 device_t bus; 126 int max_pin, res; 127 struct gpioc_softc *sc = cdev->si_drv1; 128 struct gpio_pin pin; 129 struct gpio_req req; 130 struct gpio_access_32 *a32; 131 struct gpio_config_32 *c32; 132 uint32_t caps; 133 134 bus = GPIO_GET_BUS(sc->sc_pdev); 135 if (bus == NULL) 136 return (EINVAL); 137 switch (cmd) { 138 case GPIOMAXPIN: 139 max_pin = -1; 140 res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin); 141 bcopy(&max_pin, arg, sizeof(max_pin)); 142 break; 143 case GPIOGETCONFIG: 144 bcopy(arg, &pin, sizeof(pin)); 145 dprintf("get config pin %d\n", pin.gp_pin); 146 res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin, 147 &pin.gp_flags); 148 /* Fail early */ 149 if (res) 150 break; 151 GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps); 152 GPIOBUS_PIN_GETNAME(bus, pin.gp_pin, pin.gp_name); 153 bcopy(&pin, arg, sizeof(pin)); 154 break; 155 case GPIOSETCONFIG: 156 bcopy(arg, &pin, sizeof(pin)); 157 dprintf("set config pin %d\n", pin.gp_pin); 158 res = GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &caps); 159 if (res == 0) 160 res = gpio_check_flags(caps, pin.gp_flags); 161 if (res == 0) 162 res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin, 163 pin.gp_flags); 164 break; 165 case GPIOGET: 166 bcopy(arg, &req, sizeof(req)); 167 res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin, 168 &req.gp_value); 169 dprintf("read pin %d -> %d\n", 170 req.gp_pin, req.gp_value); 171 bcopy(&req, arg, sizeof(req)); 172 break; 173 case GPIOSET: 174 bcopy(arg, &req, sizeof(req)); 175 res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin, 176 req.gp_value); 177 dprintf("write pin %d -> %d\n", 178 req.gp_pin, req.gp_value); 179 break; 180 case GPIOTOGGLE: 181 bcopy(arg, &req, sizeof(req)); 182 dprintf("toggle pin %d\n", 183 req.gp_pin); 184 res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin); 185 break; 186 case GPIOSETNAME: 187 bcopy(arg, &pin, sizeof(pin)); 188 dprintf("set name on pin %d\n", pin.gp_pin); 189 res = GPIOBUS_PIN_SETNAME(bus, pin.gp_pin, 190 pin.gp_name); 191 break; 192 case GPIOACCESS32: 193 a32 = (struct gpio_access_32 *)arg; 194 res = GPIO_PIN_ACCESS_32(sc->sc_pdev, a32->first_pin, 195 a32->clear_pins, a32->change_pins, &a32->orig_pins); 196 break; 197 case GPIOCONFIG32: 198 c32 = (struct gpio_config_32 *)arg; 199 res = GPIO_PIN_CONFIG_32(sc->sc_pdev, c32->first_pin, 200 c32->num_pins, c32->pin_flags); 201 break; 202 default: 203 return (ENOTTY); 204 break; 205 } 206 207 return (res); 208 } 209 210 static device_method_t gpioc_methods[] = { 211 /* Device interface */ 212 DEVMETHOD(device_probe, gpioc_probe), 213 DEVMETHOD(device_attach, gpioc_attach), 214 DEVMETHOD(device_detach, gpioc_detach), 215 DEVMETHOD(device_shutdown, bus_generic_shutdown), 216 DEVMETHOD(device_suspend, bus_generic_suspend), 217 DEVMETHOD(device_resume, bus_generic_resume), 218 219 DEVMETHOD_END 220 }; 221 222 driver_t gpioc_driver = { 223 "gpioc", 224 gpioc_methods, 225 sizeof(struct gpioc_softc) 226 }; 227 228 devclass_t gpioc_devclass; 229 230 DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0); 231 MODULE_VERSION(gpioc, 1); 232