1 /* $NetBSD: gpiosim.c,v 1.7 2009/12/06 22:33:44 dyoung Exp $ */ 2 /* $OpenBSD: gpiosim.c,v 1.1 2008/11/23 18:46:49 mbalmer Exp $ */ 3 4 /* 5 * Copyright (c) 2007, 2008, 2009 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 /* 32 bit wide GPIO simulator */ 22 #include <sys/param.h> 23 #include <sys/systm.h> 24 #include <sys/device.h> 25 #include <sys/gpio.h> 26 #include <sys/malloc.h> 27 #include <sys/sysctl.h> 28 #include <sys/ioccom.h> 29 30 #include <dev/gpio/gpiovar.h> 31 32 #define GPIOSIM_NPINS 32 33 34 struct gpiosim_softc { 35 device_t sc_dev; 36 device_t sc_gdev; /* gpio that attaches here */ 37 u_int32_t sc_state; 38 struct gpio_chipset_tag sc_gpio_gc; 39 gpio_pin_t sc_gpio_pins[GPIOSIM_NPINS]; 40 41 struct sysctllog *sc_log; 42 }; 43 44 int gpiosim_match(device_t, cfdata_t, void *); 45 void gpiosimattach(int); 46 void gpiosim_attach(device_t, device_t, void *); 47 int gpiosim_detach(device_t, int); 48 int gpiosim_sysctl(SYSCTLFN_PROTO); 49 50 int gpiosim_pin_read(void *, int); 51 void gpiosim_pin_write(void *, int, int); 52 void gpiosim_pin_ctl(void *, int, int); 53 54 CFATTACH_DECL_NEW(gpiosim, sizeof(struct gpiosim_softc), gpiosim_match, 55 gpiosim_attach, gpiosim_detach, NULL); 56 57 extern struct cfdriver gpiosim_cd; 58 59 int 60 gpiosim_match(device_t parent, cfdata_t match, void *aux) 61 { 62 return 1; 63 } 64 65 void 66 gpiosimattach(int num) 67 { 68 cfdata_t cf; 69 int n, err; 70 71 err = config_cfattach_attach(gpiosim_cd.cd_name, &gpiosim_ca); 72 if (err) 73 printf("%s: unable to register cfattach\n", gpiosim_cd.cd_name); 74 75 for (n = 0; n < num; n++) { 76 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK); 77 cf->cf_name = "gpiosim"; 78 cf->cf_atname = "gpiosim"; 79 cf->cf_unit = n; 80 cf->cf_fstate = FSTATE_NOTFOUND; 81 config_attach_pseudo(cf); 82 } 83 } 84 85 void 86 gpiosim_attach(device_t parent, device_t self, void *aux) 87 { 88 struct gpiosim_softc *sc = device_private(self); 89 struct gpiobus_attach_args gba; 90 const struct sysctlnode *node; 91 int i; 92 93 sc->sc_dev = self; 94 95 printf("%s", device_xname(sc->sc_dev)); 96 97 /* initialize pin array */ 98 for (i = 0; i < GPIOSIM_NPINS; i++) { 99 sc->sc_gpio_pins[i].pin_num = i; 100 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT | 101 GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN | 102 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN | 103 GPIO_PIN_INVIN | GPIO_PIN_INVOUT; 104 105 /* read initial state */ 106 sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT; 107 sc->sc_state = 0; 108 109 /* create controller tag */ 110 sc->sc_gpio_gc.gp_cookie = sc; 111 sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read; 112 sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write; 113 sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl; 114 115 /* gba.gba_name = "gpio"; */ 116 gba.gba_gc = &sc->sc_gpio_gc; 117 gba.gba_pins = sc->sc_gpio_pins; 118 gba.gba_npins = GPIOSIM_NPINS; 119 } 120 121 pmf_device_register(self, NULL, NULL); 122 123 sysctl_createv(NULL, 0, NULL, NULL, 124 CTLFLAG_PERMANENT, 125 CTLTYPE_NODE, "hw", NULL, 126 NULL, 0, NULL, 0, 127 CTL_HW, CTL_EOL); 128 sysctl_createv(&sc->sc_log, 0, NULL, &node, 129 0, 130 CTLTYPE_NODE, device_xname(sc->sc_dev), 131 SYSCTL_DESCR("GPIO simulator"), 132 NULL, 0, NULL, 0, 133 CTL_HW, CTL_CREATE, CTL_EOL); 134 135 if (node == NULL) { 136 printf(": can't create sysctl node\n"); 137 return; 138 } 139 140 sysctl_createv(&sc->sc_log, 0, &node, NULL, 141 CTLFLAG_READWRITE, 142 CTLTYPE_INT, "value", 143 SYSCTL_DESCR("Current GPIO simulator value"), 144 gpiosim_sysctl, 0, sc, 0, 145 CTL_CREATE, CTL_EOL); 146 147 printf(": simulating %d pins\n", GPIOSIM_NPINS); 148 sc->sc_gdev = config_found_ia(self, "gpiobus", &gba, gpiobus_print); 149 } 150 151 int 152 gpiosim_detach(device_t self, int flags) 153 { 154 struct gpiosim_softc *sc = device_private(self); 155 156 /* Detach the gpio driver that attached here */ 157 if (sc->sc_gdev != NULL) 158 config_detach(sc->sc_gdev, 0); 159 160 pmf_device_deregister(self); 161 if (sc->sc_log != NULL) { 162 sysctl_teardown(&sc->sc_log); 163 sc->sc_log = NULL; 164 } 165 return 0; 166 } 167 168 int 169 gpiosim_sysctl(SYSCTLFN_ARGS) 170 { 171 struct sysctlnode node; 172 struct gpiosim_softc *sc; 173 int val, error; 174 175 node = *rnode; 176 sc = node.sysctl_data; 177 178 node.sysctl_data = &val; 179 180 val = sc->sc_state; 181 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 182 if (error || newp == NULL) 183 return error; 184 185 sc->sc_state = val; 186 return 0; 187 } 188 189 int 190 gpiosim_pin_read(void *arg, int pin) 191 { 192 struct gpiosim_softc *sc = arg; 193 194 if (sc->sc_state & (1 << pin)) 195 return GPIO_PIN_HIGH; 196 else 197 return GPIO_PIN_LOW; 198 } 199 200 void 201 gpiosim_pin_write(void *arg, int pin, int value) 202 { 203 struct gpiosim_softc *sc = arg; 204 205 if (value == 0) 206 sc->sc_state &= ~(1 << pin); 207 else 208 sc->sc_state |= (1 << pin); 209 } 210 211 void 212 gpiosim_pin_ctl(void *arg, int pin, int flags) 213 { 214 struct gpiosim_softc *sc = arg; 215 216 sc->sc_gpio_pins[pin].pin_flags = flags; 217 } 218