Home | History | Annotate | Line # | Download | only in gpio
gpiosim.c revision 1.20.4.1
      1 /* $NetBSD: gpiosim.c,v 1.20.4.1 2017/05/17 01:44:17 pgoyette 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 (at) 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 extern struct cfdriver gpiosim_cd;
     61 
     62 static int
     63 gpiosim_match(device_t parent, cfdata_t match, void *aux)
     64 {
     65 	return 1;
     66 }
     67 
     68 void
     69 gpiosimattach(int num __unused)
     70 {
     71 	device_t dev;
     72 	cfdata_t cf;
     73 	int n, err;
     74 
     75 	err = config_cfattach_attach(gpiosim_cd.cd_name, &gpiosim_ca);
     76 	if (err)
     77 		printf("%s: unable to register cfattach\n", gpiosim_cd.cd_name);
     78 
     79 	for (n = 0; n < NGPIOSIM; n++) {
     80 		cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
     81 		cf->cf_name = "gpiosim";
     82 		cf->cf_atname = "gpiosim";
     83 		cf->cf_unit = n;
     84 		cf->cf_fstate = FSTATE_NOTFOUND;
     85 		dev = config_attach_pseudo(cf);
     86 		device_release(dev);
     87 	}
     88 }
     89 
     90 static void
     91 gpiosim_attach(device_t parent, device_t self, void *aux)
     92 {
     93 	struct gpiosim_softc *sc = device_private(self);
     94 	struct gpiobus_attach_args gba;
     95 	const struct sysctlnode *node;
     96 	int i;
     97 
     98 	sc->sc_dev = self;
     99 
    100 	printf("%s", device_xname(sc->sc_dev));
    101 
    102 	/* initialize pin array */
    103 	for (i = 0; i < GPIOSIM_NPINS; i++) {
    104 		sc->sc_gpio_pins[i].pin_num = i;
    105 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
    106 		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
    107 		    GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
    108 		    GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
    109 
    110 		/* read initial state */
    111 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
    112 	}
    113 	sc->sc_state = 0;
    114 
    115 	/* create controller tag */
    116 	sc->sc_gpio_gc.gp_cookie = sc;
    117 	sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read;
    118 	sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write;
    119 	sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl;
    120 
    121 	/* gba.gba_name = "gpio"; */
    122 	gba.gba_gc = &sc->sc_gpio_gc;
    123 	gba.gba_pins = sc->sc_gpio_pins;
    124 	gba.gba_npins = GPIOSIM_NPINS;
    125 
    126 	if (!pmf_device_register(self, NULL, NULL))
    127 		aprint_error_dev(self, "couldn't establish power handler\n");
    128 
    129         sysctl_createv(&sc->sc_log, 0, NULL, &node,
    130             0,
    131             CTLTYPE_NODE, device_xname(sc->sc_dev),
    132             SYSCTL_DESCR("GPIO simulator"),
    133             NULL, 0, NULL, 0,
    134             CTL_HW, CTL_CREATE, CTL_EOL);
    135 
    136         if (node == NULL) {
    137 		aprint_error(": can't create sysctl node\n");
    138                 return;
    139 	}
    140 
    141         sysctl_createv(&sc->sc_log, 0, &node, NULL,
    142             CTLFLAG_READWRITE,
    143             CTLTYPE_QUAD, "value",
    144             SYSCTL_DESCR("Current GPIO simulator value"),
    145             gpiosim_sysctl, 0, (void *)sc, 0,
    146 	    CTL_CREATE, CTL_EOL);
    147 
    148 	aprint_normal(": simulating %d pins\n", GPIOSIM_NPINS);
    149 	sc->sc_gdev = config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    150 }
    151 
    152 static int
    153 gpiosim_detach(device_t self, int flags)
    154 {
    155 	struct gpiosim_softc *sc = device_private(self);
    156 
    157 	/* Detach the gpio driver that attached here */
    158 	if (sc->sc_gdev != NULL)
    159 		config_detach(sc->sc_gdev, 0);
    160 
    161 	pmf_device_deregister(self);
    162 	if (sc->sc_log != NULL) {
    163 		sysctl_teardown(&sc->sc_log);
    164 		sc->sc_log = NULL;
    165 	}
    166 	return 0;
    167 }
    168 
    169 static int
    170 gpiosim_sysctl(SYSCTLFN_ARGS)
    171 {
    172 	struct sysctlnode node;
    173 	struct gpiosim_softc *sc;
    174 	uint64_t val, error;
    175 
    176 	node = *rnode;
    177 	sc = node.sysctl_data;
    178 
    179 	node.sysctl_data = &val;
    180 
    181 	val = sc->sc_state;
    182 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    183 	if (error || newp == NULL)
    184 		return error;
    185 
    186 	sc->sc_state = val;
    187 	return 0;
    188 }
    189 
    190 static int
    191 gpiosim_pin_read(void *arg, int pin)
    192 {
    193 	struct gpiosim_softc *sc = arg;
    194 
    195 	if (sc->sc_state & (1LL << pin))
    196 		return GPIO_PIN_HIGH;
    197 	else
    198 		return GPIO_PIN_LOW;
    199 }
    200 
    201 static void
    202 gpiosim_pin_write(void *arg, int pin, int value)
    203 {
    204 	struct gpiosim_softc *sc = arg;
    205 
    206 	if (value == 0)
    207 		sc->sc_state &= ~(1LL << pin);
    208 	else
    209 		sc->sc_state |= (1LL << pin);
    210 }
    211 
    212 static void
    213 gpiosim_pin_ctl(void *arg, int pin, int flags)
    214 {
    215 	struct gpiosim_softc *sc = arg;
    216 
    217 	sc->sc_gpio_pins[pin].pin_flags = flags;
    218 }
    219 
    220 MODULE(MODULE_CLASS_DRIVER, gpiosim, "gpio");
    221 
    222 #ifdef _MODULE
    223 static const struct cfiattrdata gpiobus_iattrdata = {
    224 	"gpiobus", 0, { { NULL, NULL, 0 },}
    225 };
    226 static const struct cfiattrdata *const gpiosim_attrs[] = {
    227 	&gpiobus_iattrdata, NULL
    228 };
    229 CFDRIVER_DECL(gpiosim, DV_DULL, gpiosim_attrs);
    230 extern struct cfattach gpiosim_ca;
    231 static int gpiosimloc[] = {
    232 	-1,
    233 	-1,
    234 	-1
    235 };
    236 static struct cfdata gpiosim_cfdata[] = {
    237 	{
    238 		.cf_name = "gpiosim",
    239 		.cf_atname = "gpiosim",
    240 		.cf_unit = 0,
    241 		.cf_fstate = FSTATE_STAR,
    242 		.cf_loc = gpiosimloc,
    243 		.cf_flags = 0,
    244 		.cf_pspec = NULL,
    245 	},
    246 	{ NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
    247 };
    248 #endif
    249 
    250 static int
    251 gpiosim_modcmd(modcmd_t cmd, void *opaque)
    252 {
    253 #ifdef _MODULE
    254 	device_t dev;
    255 	int error = 0;
    256 #endif
    257 	switch (cmd) {
    258 	case MODULE_CMD_INIT:
    259 #ifdef _MODULE
    260 		error = config_cfdriver_attach(&gpiosim_cd);
    261 		if (error)
    262 			return error;
    263 
    264 		error = config_cfattach_attach(gpiosim_cd.cd_name,
    265 		    &gpiosim_ca);
    266 		if (error) {
    267 			config_cfdriver_detach(&gpiosim_cd);
    268 			aprint_error("%s: unable to register cfattach\n",
    269 			    gpiosim_cd.cd_name);
    270 			return error;
    271 		}
    272 		error = config_cfdata_attach(gpiosim_cfdata, 1);
    273 		if (error) {
    274 			config_cfattach_detach(gpiosim_cd.cd_name,
    275 			    &gpiosim_ca);
    276 			config_cfdriver_detach(&gpiosim_cd);
    277 			aprint_error("%s: unable to register cfdata\n",
    278 			    gpiosim_cd.cd_name);
    279 			return error;
    280 		}
    281 		dev = config_attach_pseudo(gpiosim_cfdata);
    282 		device_release(dev);
    283 #endif
    284 		return 0;
    285 	case MODULE_CMD_FINI:
    286 #ifdef _MODULE
    287 		error = config_cfdata_detach(gpiosim_cfdata);
    288 		if (error)
    289 			return error;
    290 
    291 		config_cfattach_detach(gpiosim_cd.cd_name, &gpiosim_ca);
    292 		config_cfdriver_detach(&gpiosim_cd);
    293 #endif
    294 		return 0;
    295 	case MODULE_CMD_AUTOUNLOAD:
    296 		/* no auto-unload */
    297 		return EBUSY;
    298 	default:
    299 		return ENOTTY;
    300 	}
    301 }
    302