xref: /netbsd-src/sys/dev/gpio/gpiosim.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /* $NetBSD: gpiosim.c,v 1.4 2009/07/28 15:28:24 drochner 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 	u_int32_t		sc_state;
37 	struct gpio_chipset_tag	sc_gpio_gc;
38 	gpio_pin_t		sc_gpio_pins[GPIOSIM_NPINS];
39 
40 	const struct sysctlnode *sc_node;
41 };
42 
43 int	gpiosim_match(device_t, cfdata_t, void *);
44 void	gpiosimattach(int);
45 void	gpiosim_attach(device_t, device_t, void *);
46 int	gpiosim_detach(device_t, int);
47 int	gpiosim_activate(device_t, enum devact);
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, gpiosim_activate);
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 	int i;
91 
92 	sc->sc_dev = self;
93 
94 	printf("%s", device_xname(sc->sc_dev));
95 
96 	/* initialize pin array */
97 	for (i = 0; i < GPIOSIM_NPINS; i++) {
98 		sc->sc_gpio_pins[i].pin_num = i;
99 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
100 		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
101 		    GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
102 		    GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
103 
104 		/* read initial state */
105 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
106 		sc->sc_state = 0;
107 
108 		/* create controller tag */
109 		sc->sc_gpio_gc.gp_cookie = sc;
110 		sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read;
111 		sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write;
112 		sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl;
113 
114 		/* gba.gba_name = "gpio"; */
115 		gba.gba_gc = &sc->sc_gpio_gc;
116 		gba.gba_pins = sc->sc_gpio_pins;
117 		gba.gba_npins = GPIOSIM_NPINS;
118 	}
119 
120 	pmf_device_register(self, NULL, NULL);
121 
122 	sysctl_createv(NULL, 0, NULL, NULL,
123             CTLFLAG_PERMANENT,
124             CTLTYPE_NODE, "hw", NULL,
125             NULL, 0, NULL, 0,
126             CTL_HW, CTL_EOL);
127         sysctl_createv(NULL, 0, NULL, &sc->sc_node,
128             0,
129             CTLTYPE_NODE, device_xname(sc->sc_dev),
130             SYSCTL_DESCR("GPIO simulator"),
131             NULL, 0, NULL, 0,
132             CTL_HW, CTL_CREATE, CTL_EOL);
133 
134         if (sc->sc_node == NULL) {
135 		printf(": can't create sysctl node\n");
136                 return;
137 	}
138 
139         sysctl_createv(NULL, 0, &sc->sc_node, NULL,
140             CTLFLAG_READWRITE,
141             CTLTYPE_INT, "value",
142             SYSCTL_DESCR("Current GPIO simulator value"),
143             gpiosim_sysctl, 0, sc, 0,
144 	    CTL_CREATE, CTL_EOL);
145 
146 	printf(": simulating %d pins\n", GPIOSIM_NPINS);
147 	config_found_ia(self, "gpiobus", &gba, gpiobus_print);
148 }
149 
150 int
151 gpiosim_detach(device_t self, int flags)
152 {
153 	struct gpiosim_softc *sc = device_private(self);
154 	struct sysctlnode node;
155 
156 	if (sc->sc_node != NULL) {
157 		node = *sc->sc_node;
158 		sysctl_destroyv(&node, CTL_EOL);
159 		sc->sc_node = NULL;
160 	}
161 
162 	return 0;
163 }
164 
165 int
166 gpiosim_activate(device_t self, enum devact act)
167 {
168 	switch (act) {
169 	case DVACT_ACTIVATE:
170 		return EOPNOTSUPP;
171 	case DVACT_DEACTIVATE:
172 		break;
173 	}
174 
175 	return 0;
176 }
177 
178 int
179 gpiosim_sysctl(SYSCTLFN_ARGS)
180 {
181 	struct sysctlnode node;
182 	struct gpiosim_softc *sc;
183 	int val, error;
184 
185 	node = *rnode;
186 	sc = node.sysctl_data;
187 
188 	node.sysctl_data = &val;
189 
190 	val = sc->sc_state;
191 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
192 	if (error || newp == NULL)
193 		return error;
194 
195 	sc->sc_state = val;
196 	return 0;
197 }
198 
199 int
200 gpiosim_pin_read(void *arg, int pin)
201 {
202 	struct gpiosim_softc *sc = (struct gpiosim_softc *)arg;
203 
204 	if (sc->sc_state & (1 << pin))
205 		return GPIO_PIN_HIGH;
206 	else
207 		return GPIO_PIN_LOW;
208 }
209 
210 void
211 gpiosim_pin_write(void *arg, int pin, int value)
212 {
213 	struct gpiosim_softc *sc = (struct gpiosim_softc *)arg;
214 
215 	if (value == 0)
216 		sc->sc_state &= ~(1 << pin);
217 	else
218 		sc->sc_state |= (1 << pin);
219 }
220 
221 void
222 gpiosim_pin_ctl(void *arg, int pin, int flags)
223 {
224 	struct gpiosim_softc *sc = (struct gpiosim_softc *)arg;
225 
226 	sc->sc_gpio_pins[pin].pin_flags = flags;
227 }
228