gpiosim.c revision 1.12 1 /* $NetBSD: gpiosim.c,v 1.12 2011/08/29 15:14:04 mbalmer Exp $ */
2 /* $OpenBSD: gpiosim.c,v 1.1 2008/11/23 18:46:49 mbalmer Exp $ */
3
4 /*
5 * Copyright (c) 2007, 2008, 2009, 2010, 2011 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 /* 32 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 #ifdef _MODULE
29 #include <sys/module.h>
30 #endif
31 #include <sys/sysctl.h>
32 #include <sys/ioccom.h>
33 #include <dev/gpio/gpiovar.h>
34
35 #define GPIOSIM_NPINS 32
36
37 struct gpiosim_softc {
38 device_t sc_dev;
39 device_t sc_gdev; /* gpio that attaches here */
40 uint32_t sc_state;
41 struct gpio_chipset_tag sc_gpio_gc;
42 gpio_pin_t sc_gpio_pins[GPIOSIM_NPINS];
43
44 struct sysctllog *sc_log;
45 };
46
47 static int gpiosim_match(device_t, cfdata_t, void *);
48 void gpiosimattach(int);
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)
70 {
71 cfdata_t cf;
72 int n, err;
73
74 err = config_cfattach_attach(gpiosim_cd.cd_name, &gpiosim_ca);
75 if (err)
76 printf("%s: unable to register cfattach\n", gpiosim_cd.cd_name);
77
78 for (n = 0; n < num; n++) {
79 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
80 cf->cf_name = "gpiosim";
81 cf->cf_atname = "gpiosim";
82 cf->cf_unit = n;
83 cf->cf_fstate = FSTATE_NOTFOUND;
84 config_attach_pseudo(cf);
85 }
86 }
87
88 static void
89 gpiosim_attach(device_t parent, device_t self, void *aux)
90 {
91 struct gpiosim_softc *sc = device_private(self);
92 struct gpiobus_attach_args gba;
93 const struct sysctlnode *node;
94 int i;
95
96 sc->sc_dev = self;
97
98 printf("%s", device_xname(sc->sc_dev));
99
100 /* initialize pin array */
101 for (i = 0; i < GPIOSIM_NPINS; i++) {
102 sc->sc_gpio_pins[i].pin_num = i;
103 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
104 GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
105 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
106 GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
107
108 /* read initial state */
109 sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
110 sc->sc_state = 0;
111
112 /* create controller tag */
113 sc->sc_gpio_gc.gp_cookie = sc;
114 sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read;
115 sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write;
116 sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl;
117
118 /* gba.gba_name = "gpio"; */
119 gba.gba_gc = &sc->sc_gpio_gc;
120 gba.gba_pins = sc->sc_gpio_pins;
121 gba.gba_npins = GPIOSIM_NPINS;
122 }
123
124 pmf_device_register(self, NULL, NULL);
125
126 sysctl_createv(NULL, 0, NULL, NULL,
127 CTLFLAG_PERMANENT,
128 CTLTYPE_NODE, "hw", NULL,
129 NULL, 0, NULL, 0,
130 CTL_HW, CTL_EOL);
131 sysctl_createv(&sc->sc_log, 0, NULL, &node,
132 0,
133 CTLTYPE_NODE, device_xname(sc->sc_dev),
134 SYSCTL_DESCR("GPIO simulator"),
135 NULL, 0, NULL, 0,
136 CTL_HW, CTL_CREATE, CTL_EOL);
137
138 if (node == NULL) {
139 printf(": can't create sysctl node\n");
140 return;
141 }
142
143 sysctl_createv(&sc->sc_log, 0, &node, NULL,
144 CTLFLAG_READWRITE,
145 CTLTYPE_INT, "value",
146 SYSCTL_DESCR("Current GPIO simulator value"),
147 gpiosim_sysctl, 0, sc, 0,
148 CTL_CREATE, CTL_EOL);
149
150 printf(": simulating %d pins\n", GPIOSIM_NPINS);
151 sc->sc_gdev = config_found_ia(self, "gpiobus", &gba, gpiobus_print);
152 }
153
154 static int
155 gpiosim_detach(device_t self, int flags)
156 {
157 struct gpiosim_softc *sc = device_private(self);
158
159 /* Detach the gpio driver that attached here */
160 if (sc->sc_gdev != NULL)
161 config_detach(sc->sc_gdev, 0);
162
163 pmf_device_deregister(self);
164 if (sc->sc_log != NULL) {
165 sysctl_teardown(&sc->sc_log);
166 sc->sc_log = NULL;
167 }
168 return 0;
169 }
170
171 static int
172 gpiosim_sysctl(SYSCTLFN_ARGS)
173 {
174 struct sysctlnode node;
175 struct gpiosim_softc *sc;
176 int val, error;
177
178 node = *rnode;
179 sc = node.sysctl_data;
180
181 node.sysctl_data = &val;
182
183 val = sc->sc_state;
184 error = sysctl_lookup(SYSCTLFN_CALL(&node));
185 if (error || newp == NULL)
186 return error;
187
188 sc->sc_state = val;
189 return 0;
190 }
191
192 static int
193 gpiosim_pin_read(void *arg, int pin)
194 {
195 struct gpiosim_softc *sc = arg;
196
197 if (sc->sc_state & (1 << pin))
198 return GPIO_PIN_HIGH;
199 else
200 return GPIO_PIN_LOW;
201 }
202
203 static void
204 gpiosim_pin_write(void *arg, int pin, int value)
205 {
206 struct gpiosim_softc *sc = arg;
207
208 if (value == 0)
209 sc->sc_state &= ~(1 << pin);
210 else
211 sc->sc_state |= (1 << pin);
212 }
213
214 static void
215 gpiosim_pin_ctl(void *arg, int pin, int flags)
216 {
217 struct gpiosim_softc *sc = arg;
218
219 sc->sc_gpio_pins[pin].pin_flags = flags;
220 }
221
222 MODULE(MODULE_CLASS_DRIVER, gpiosim, "gpio");
223
224 #ifdef _MODULE
225 static const struct cfiattrdata gpiobus_iattrdata = {
226 "gpiobus", 0, { { NULL, NULL, 0 },}
227 };
228 static const struct cfiattrdata *const gpiosim_attrs[] = {
229 &gpiobus_iattrdata, NULL
230 };
231 CFDRIVER_DECL(gpiosim, DV_DULL, gpiosim_attrs);
232 extern struct cfattach gpiosim_ca;
233 static int gpiosimloc[] = {
234 -1,
235 -1,
236 -1
237 };
238 static struct cfdata gpiosim_cfdata[] = {
239 {
240 .cf_name = "gpiosim",
241 .cf_atname = "gpiosim",
242 .cf_unit = 0,
243 .cf_fstate = FSTATE_STAR,
244 .cf_loc = gpiosimloc,
245 .cf_flags = 0,
246 .cf_pspec = NULL,
247 },
248 { NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
249 };
250 #endif
251
252 static int
253 gpiosim_modcmd(modcmd_t cmd, void *opaque)
254 {
255 #ifdef _MODULE
256 int error = 0;
257 #endif
258 switch (cmd) {
259 case MODULE_CMD_INIT:
260 #ifdef _MODULE
261 error = config_cfdriver_attach(&gpiosim_cd);
262 if (error)
263 return error;
264
265 error = config_cfattach_attach(gpiosim_cd.cd_name,
266 &gpiosim_ca);
267 if (error) {
268 config_cfdriver_detach(&gpiosim_cd);
269 aprint_error("%s: unable to register cfattach\n",
270 gpiosim_cd.cd_name);
271 return error;
272 }
273 error = config_cfdata_attach(gpiosim_cfdata, 1);
274 if (error) {
275 config_cfattach_detach(gpiosim_cd.cd_name,
276 &gpiosim_ca);
277 config_cfdriver_detach(&gpiosim_cd);
278 aprint_error("%s: unable to register cfdata\n",
279 gpiosim_cd.cd_name);
280 return error;
281 }
282 config_attach_pseudo(gpiosim_cfdata);
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