gpiosim.c revision 1.17 1 /* $NetBSD: gpiosim.c,v 1.17 2015/08/20 12:09:22 uebayasi 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
35 #define GPIOSIM_NPINS 64
36
37 struct gpiosim_softc {
38 device_t sc_dev;
39 device_t sc_gdev; /* gpio that attaches here */
40 uint64_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 __unused)
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 < NGPIOSIM; 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 }
111 sc->sc_state = 0;
112
113 /* create controller tag */
114 sc->sc_gpio_gc.gp_cookie = sc;
115 sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read;
116 sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write;
117 sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl;
118
119 /* gba.gba_name = "gpio"; */
120 gba.gba_gc = &sc->sc_gpio_gc;
121 gba.gba_pins = sc->sc_gpio_pins;
122 gba.gba_npins = GPIOSIM_NPINS;
123
124 pmf_device_register(self, NULL, NULL);
125
126 sysctl_createv(&sc->sc_log, 0, NULL, &node,
127 0,
128 CTLTYPE_NODE, device_xname(sc->sc_dev),
129 SYSCTL_DESCR("GPIO simulator"),
130 NULL, 0, NULL, 0,
131 CTL_HW, CTL_CREATE, CTL_EOL);
132
133 if (node == NULL) {
134 printf(": can't create sysctl node\n");
135 return;
136 }
137
138 sysctl_createv(&sc->sc_log, 0, &node, NULL,
139 CTLFLAG_READWRITE,
140 CTLTYPE_QUAD, "value",
141 SYSCTL_DESCR("Current GPIO simulator value"),
142 gpiosim_sysctl, 0, (void *)sc, 0,
143 CTL_CREATE, CTL_EOL);
144
145 printf(": simulating %d pins\n", GPIOSIM_NPINS);
146 sc->sc_gdev = config_found_ia(self, "gpiobus", &gba, gpiobus_print);
147 }
148
149 static int
150 gpiosim_detach(device_t self, int flags)
151 {
152 struct gpiosim_softc *sc = device_private(self);
153
154 /* Detach the gpio driver that attached here */
155 if (sc->sc_gdev != NULL)
156 config_detach(sc->sc_gdev, 0);
157
158 pmf_device_deregister(self);
159 if (sc->sc_log != NULL) {
160 sysctl_teardown(&sc->sc_log);
161 sc->sc_log = NULL;
162 }
163 return 0;
164 }
165
166 static int
167 gpiosim_sysctl(SYSCTLFN_ARGS)
168 {
169 struct sysctlnode node;
170 struct gpiosim_softc *sc;
171 uint64_t val, error;
172
173 node = *rnode;
174 sc = node.sysctl_data;
175
176 node.sysctl_data = &val;
177
178 val = sc->sc_state;
179 error = sysctl_lookup(SYSCTLFN_CALL(&node));
180 if (error || newp == NULL)
181 return error;
182
183 sc->sc_state = val;
184 return 0;
185 }
186
187 static int
188 gpiosim_pin_read(void *arg, int pin)
189 {
190 struct gpiosim_softc *sc = arg;
191
192 if (sc->sc_state & (1LL << pin))
193 return GPIO_PIN_HIGH;
194 else
195 return GPIO_PIN_LOW;
196 }
197
198 static void
199 gpiosim_pin_write(void *arg, int pin, int value)
200 {
201 struct gpiosim_softc *sc = arg;
202
203 if (value == 0)
204 sc->sc_state &= ~(1LL << pin);
205 else
206 sc->sc_state |= (1LL << pin);
207 }
208
209 static void
210 gpiosim_pin_ctl(void *arg, int pin, int flags)
211 {
212 struct gpiosim_softc *sc = arg;
213
214 sc->sc_gpio_pins[pin].pin_flags = flags;
215 }
216
217 MODULE(MODULE_CLASS_DRIVER, gpiosim, "gpio");
218
219 #ifdef _MODULE
220 static const struct cfiattrdata gpiobus_iattrdata = {
221 "gpiobus", 0, { { NULL, NULL, 0 },}
222 };
223 static const struct cfiattrdata *const gpiosim_attrs[] = {
224 &gpiobus_iattrdata, NULL
225 };
226 CFDRIVER_DECL(gpiosim, DV_DULL, gpiosim_attrs);
227 extern struct cfattach gpiosim_ca;
228 static int gpiosimloc[] = {
229 -1,
230 -1,
231 -1
232 };
233 static struct cfdata gpiosim_cfdata[] = {
234 {
235 .cf_name = "gpiosim",
236 .cf_atname = "gpiosim",
237 .cf_unit = 0,
238 .cf_fstate = FSTATE_STAR,
239 .cf_loc = gpiosimloc,
240 .cf_flags = 0,
241 .cf_pspec = NULL,
242 },
243 { NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
244 };
245 #endif
246
247 static int
248 gpiosim_modcmd(modcmd_t cmd, void *opaque)
249 {
250 #ifdef _MODULE
251 int error = 0;
252 #endif
253 switch (cmd) {
254 case MODULE_CMD_INIT:
255 #ifdef _MODULE
256 error = config_cfdriver_attach(&gpiosim_cd);
257 if (error)
258 return error;
259
260 error = config_cfattach_attach(gpiosim_cd.cd_name,
261 &gpiosim_ca);
262 if (error) {
263 config_cfdriver_detach(&gpiosim_cd);
264 aprint_error("%s: unable to register cfattach\n",
265 gpiosim_cd.cd_name);
266 return error;
267 }
268 error = config_cfdata_attach(gpiosim_cfdata, 1);
269 if (error) {
270 config_cfattach_detach(gpiosim_cd.cd_name,
271 &gpiosim_ca);
272 config_cfdriver_detach(&gpiosim_cd);
273 aprint_error("%s: unable to register cfdata\n",
274 gpiosim_cd.cd_name);
275 return error;
276 }
277 config_attach_pseudo(gpiosim_cfdata);
278 #endif
279 return 0;
280 case MODULE_CMD_FINI:
281 #ifdef _MODULE
282 error = config_cfdata_detach(gpiosim_cfdata);
283 if (error)
284 return error;
285
286 config_cfattach_detach(gpiosim_cd.cd_name, &gpiosim_ca);
287 config_cfdriver_detach(&gpiosim_cd);
288 #endif
289 return 0;
290 case MODULE_CMD_AUTOUNLOAD:
291 /* no auto-unload */
292 return EBUSY;
293 default:
294 return ENOTTY;
295 }
296 }
297