1 /* $NetBSD: gpiosim.c,v 1.24 2023/05/10 00:09:39 riastradh Exp $ */ 2 /* $OpenBSD: gpiosim.c,v 1.1 2008/11/23 18:46:49 mbalmer Exp $ */ 3 4 /* 5 * Copyright (c) 2007 - 2011, 2013 Marc Balmer <marc@msys.ch> 6 * All rights reserved. 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN 17 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 18 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /* 64 bit wide GPIO simulator */ 22 23 #include <sys/param.h> 24 #include <sys/systm.h> 25 #include <sys/device.h> 26 #include <sys/gpio.h> 27 #include <sys/malloc.h> 28 #include <sys/module.h> 29 #include <sys/sysctl.h> 30 #include <sys/ioccom.h> 31 #include <dev/gpio/gpiovar.h> 32 33 #include "gpiosim.h" 34 #include "ioconf.h" 35 36 #define GPIOSIM_NPINS 64 37 38 struct gpiosim_softc { 39 device_t sc_dev; 40 device_t sc_gdev; /* gpio that attaches here */ 41 uint64_t sc_state; 42 struct gpio_chipset_tag sc_gpio_gc; 43 gpio_pin_t sc_gpio_pins[GPIOSIM_NPINS]; 44 45 struct sysctllog *sc_log; 46 }; 47 48 static int gpiosim_match(device_t, cfdata_t, void *); 49 static void gpiosim_attach(device_t, device_t, void *); 50 static int gpiosim_detach(device_t, int); 51 static int gpiosim_sysctl(SYSCTLFN_PROTO); 52 53 static int gpiosim_pin_read(void *, int); 54 static void gpiosim_pin_write(void *, int, int); 55 static void gpiosim_pin_ctl(void *, int, int); 56 57 CFATTACH_DECL_NEW(gpiosim, sizeof(struct gpiosim_softc), gpiosim_match, 58 gpiosim_attach, gpiosim_detach, NULL); 59 60 static int 61 gpiosim_match(device_t parent, cfdata_t match, void *aux) 62 { 63 return 1; 64 } 65 66 void 67 gpiosimattach(int num __unused) 68 { 69 cfdata_t cf; 70 int n, err; 71 72 err = config_cfattach_attach(gpiosim_cd.cd_name, &gpiosim_ca); 73 if (err) 74 printf("%s: unable to register cfattach\n", gpiosim_cd.cd_name); 75 76 for (n = 0; n < NGPIOSIM; n++) { 77 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK); 78 cf->cf_name = "gpiosim"; 79 cf->cf_atname = "gpiosim"; 80 cf->cf_unit = n; 81 cf->cf_fstate = FSTATE_NOTFOUND; 82 config_attach_pseudo(cf); 83 } 84 } 85 86 static void 87 gpiosim_attach(device_t parent, device_t self, void *aux) 88 { 89 struct gpiosim_softc *sc = device_private(self); 90 struct gpiobus_attach_args gba; 91 const struct sysctlnode *node; 92 int i; 93 94 sc->sc_dev = self; 95 96 printf("%s", device_xname(sc->sc_dev)); 97 98 /* initialize pin array */ 99 for (i = 0; i < GPIOSIM_NPINS; i++) { 100 sc->sc_gpio_pins[i].pin_num = i; 101 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT | 102 GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN | 103 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN | 104 GPIO_PIN_INVIN | GPIO_PIN_INVOUT; 105 106 /* read initial state */ 107 sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT; 108 } 109 sc->sc_state = 0; 110 111 /* create controller tag */ 112 sc->sc_gpio_gc.gp_cookie = sc; 113 sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read; 114 sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write; 115 sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl; 116 117 /* gba.gba_name = "gpio"; */ 118 gba.gba_gc = &sc->sc_gpio_gc; 119 gba.gba_pins = sc->sc_gpio_pins; 120 gba.gba_npins = GPIOSIM_NPINS; 121 122 if (!pmf_device_register(self, NULL, NULL)) 123 aprint_error_dev(self, "couldn't establish power handler\n"); 124 125 sysctl_createv(&sc->sc_log, 0, NULL, &node, 126 0, 127 CTLTYPE_NODE, device_xname(sc->sc_dev), 128 SYSCTL_DESCR("GPIO simulator"), 129 NULL, 0, NULL, 0, 130 CTL_HW, CTL_CREATE, CTL_EOL); 131 132 if (node == NULL) { 133 aprint_error(": can't create sysctl node\n"); 134 return; 135 } 136 137 sysctl_createv(&sc->sc_log, 0, &node, NULL, 138 CTLFLAG_READWRITE, 139 CTLTYPE_QUAD, "value", 140 SYSCTL_DESCR("Current GPIO simulator value"), 141 gpiosim_sysctl, 0, (void *)sc, 0, 142 CTL_CREATE, CTL_EOL); 143 144 aprint_normal(": simulating %d pins\n", GPIOSIM_NPINS); 145 sc->sc_gdev = config_found(self, &gba, gpiobus_print, CFARGS_NONE); 146 } 147 148 static int 149 gpiosim_detach(device_t self, int flags) 150 { 151 struct gpiosim_softc *sc = device_private(self); 152 int error; 153 154 /* Detach the gpio driver that attached here */ 155 error = config_detach_children(self, flags); 156 if (error) 157 return error; 158 159 pmf_device_deregister(self); 160 if (sc->sc_log != NULL) { 161 sysctl_teardown(&sc->sc_log); 162 sc->sc_log = NULL; 163 } 164 return 0; 165 } 166 167 static int 168 gpiosim_sysctl(SYSCTLFN_ARGS) 169 { 170 struct sysctlnode node; 171 struct gpiosim_softc *sc; 172 uint64_t val, error; 173 174 node = *rnode; 175 sc = node.sysctl_data; 176 177 node.sysctl_data = &val; 178 179 val = sc->sc_state; 180 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 181 if (error || newp == NULL) 182 return error; 183 184 sc->sc_state = val; 185 return 0; 186 } 187 188 static int 189 gpiosim_pin_read(void *arg, int pin) 190 { 191 struct gpiosim_softc *sc = arg; 192 193 if (sc->sc_state & (1LL << pin)) 194 return GPIO_PIN_HIGH; 195 else 196 return GPIO_PIN_LOW; 197 } 198 199 static void 200 gpiosim_pin_write(void *arg, int pin, int value) 201 { 202 struct gpiosim_softc *sc = arg; 203 204 if (value == 0) 205 sc->sc_state &= ~(1LL << pin); 206 else 207 sc->sc_state |= (1LL << pin); 208 } 209 210 static void 211 gpiosim_pin_ctl(void *arg, int pin, int flags) 212 { 213 struct gpiosim_softc *sc = arg; 214 215 sc->sc_gpio_pins[pin].pin_flags = flags; 216 } 217 218 MODULE(MODULE_CLASS_DRIVER, gpiosim, "gpio"); 219 220 #ifdef _MODULE 221 static const struct cfiattrdata gpiobus_iattrdata = { 222 "gpiobus", 0, { { NULL, NULL, 0 },} 223 }; 224 static const struct cfiattrdata *const gpiosim_attrs[] = { 225 &gpiobus_iattrdata, NULL 226 }; 227 CFDRIVER_DECL(gpiosim, DV_DULL, gpiosim_attrs); 228 extern struct cfattach gpiosim_ca; 229 static int gpiosimloc[] = { 230 -1, 231 -1, 232 -1 233 }; 234 static struct cfdata gpiosim_cfdata[] = { 235 { 236 .cf_name = "gpiosim", 237 .cf_atname = "gpiosim", 238 .cf_unit = 0, 239 .cf_fstate = FSTATE_STAR, 240 .cf_loc = gpiosimloc, 241 .cf_flags = 0, 242 .cf_pspec = NULL, 243 }, 244 { NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL } 245 }; 246 #endif 247 248 static int 249 gpiosim_modcmd(modcmd_t cmd, void *opaque) 250 { 251 #ifdef _MODULE 252 int error = 0; 253 #endif 254 switch (cmd) { 255 case MODULE_CMD_INIT: 256 #ifdef _MODULE 257 error = config_cfdriver_attach(&gpiosim_cd); 258 if (error) 259 return error; 260 261 error = config_cfattach_attach(gpiosim_cd.cd_name, 262 &gpiosim_ca); 263 if (error) { 264 config_cfdriver_detach(&gpiosim_cd); 265 aprint_error("%s: unable to register cfattach\n", 266 gpiosim_cd.cd_name); 267 return error; 268 } 269 error = config_cfdata_attach(gpiosim_cfdata, 1); 270 if (error) { 271 config_cfattach_detach(gpiosim_cd.cd_name, 272 &gpiosim_ca); 273 config_cfdriver_detach(&gpiosim_cd); 274 aprint_error("%s: unable to register cfdata\n", 275 gpiosim_cd.cd_name); 276 return error; 277 } 278 config_attach_pseudo(gpiosim_cfdata); 279 #endif 280 return 0; 281 case MODULE_CMD_FINI: 282 #ifdef _MODULE 283 error = config_cfdata_detach(gpiosim_cfdata); 284 if (error) 285 return error; 286 287 config_cfattach_detach(gpiosim_cd.cd_name, &gpiosim_ca); 288 config_cfdriver_detach(&gpiosim_cd); 289 #endif 290 return 0; 291 case MODULE_CMD_AUTOUNLOAD: 292 /* no auto-unload */ 293 return EBUSY; 294 default: 295 return ENOTTY; 296 } 297 } 298