gpiosim.c revision 1.6.2.2 1 /* $NetBSD: gpiosim.c,v 1.6.2.2 2009/08/19 18:47:06 yamt 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 (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 #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_activate(device_t, enum devact);
49 int gpiosim_sysctl(SYSCTLFN_PROTO);
50
51 int gpiosim_pin_read(void *, int);
52 void gpiosim_pin_write(void *, int, int);
53 void gpiosim_pin_ctl(void *, int, int);
54
55 CFATTACH_DECL_NEW(gpiosim, sizeof(struct gpiosim_softc), gpiosim_match,
56 gpiosim_attach, gpiosim_detach, gpiosim_activate);
57
58 extern struct cfdriver gpiosim_cd;
59
60 int
61 gpiosim_match(device_t parent, cfdata_t match, void *aux)
62 {
63 return 1;
64 }
65
66 void
67 gpiosimattach(int num)
68 {
69 cfdata_t cf;
70 int n, err;
71
72 err = config_cfattach_attach(gpiosim_cd.cd_name, &gpiosim_ca);
73 if (err)
74 printf("%s: unable to register cfattach\n", gpiosim_cd.cd_name);
75
76 for (n = 0; n < num; n++) {
77 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
78 cf->cf_name = "gpiosim";
79 cf->cf_atname = "gpiosim";
80 cf->cf_unit = n;
81 cf->cf_fstate = FSTATE_NOTFOUND;
82 config_attach_pseudo(cf);
83 }
84 }
85
86 void
87 gpiosim_attach(device_t parent, device_t self, void *aux)
88 {
89 struct gpiosim_softc *sc = device_private(self);
90 struct gpiobus_attach_args gba;
91 const struct sysctlnode *node;
92 int i;
93
94 sc->sc_dev = self;
95
96 printf("%s", device_xname(sc->sc_dev));
97
98 /* initialize pin array */
99 for (i = 0; i < GPIOSIM_NPINS; i++) {
100 sc->sc_gpio_pins[i].pin_num = i;
101 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
102 GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
103 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
104 GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
105
106 /* read initial state */
107 sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
108 sc->sc_state = 0;
109
110 /* create controller tag */
111 sc->sc_gpio_gc.gp_cookie = sc;
112 sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read;
113 sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write;
114 sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl;
115
116 /* gba.gba_name = "gpio"; */
117 gba.gba_gc = &sc->sc_gpio_gc;
118 gba.gba_pins = sc->sc_gpio_pins;
119 gba.gba_npins = GPIOSIM_NPINS;
120 }
121
122 pmf_device_register(self, NULL, NULL);
123
124 sysctl_createv(NULL, 0, NULL, NULL,
125 CTLFLAG_PERMANENT,
126 CTLTYPE_NODE, "hw", NULL,
127 NULL, 0, NULL, 0,
128 CTL_HW, CTL_EOL);
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 printf(": can't create sysctl node\n");
138 return;
139 }
140
141 sysctl_createv(&sc->sc_log, 0, &node, NULL,
142 CTLFLAG_READWRITE,
143 CTLTYPE_INT, "value",
144 SYSCTL_DESCR("Current GPIO simulator value"),
145 gpiosim_sysctl, 0, sc, 0,
146 CTL_CREATE, CTL_EOL);
147
148 printf(": simulating %d pins\n", GPIOSIM_NPINS);
149 sc->sc_gdev = config_found_ia(self, "gpiobus", &gba, gpiobus_print);
150 }
151
152 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 int
170 gpiosim_activate(device_t self, enum devact act)
171 {
172 switch (act) {
173 case DVACT_ACTIVATE:
174 return EOPNOTSUPP;
175 case DVACT_DEACTIVATE:
176 break;
177 }
178 return 0;
179 }
180
181 int
182 gpiosim_sysctl(SYSCTLFN_ARGS)
183 {
184 struct sysctlnode node;
185 struct gpiosim_softc *sc;
186 int val, error;
187
188 node = *rnode;
189 sc = node.sysctl_data;
190
191 node.sysctl_data = &val;
192
193 val = sc->sc_state;
194 error = sysctl_lookup(SYSCTLFN_CALL(&node));
195 if (error || newp == NULL)
196 return error;
197
198 sc->sc_state = val;
199 return 0;
200 }
201
202 int
203 gpiosim_pin_read(void *arg, int pin)
204 {
205 struct gpiosim_softc *sc = arg;
206
207 if (sc->sc_state & (1 << pin))
208 return GPIO_PIN_HIGH;
209 else
210 return GPIO_PIN_LOW;
211 }
212
213 void
214 gpiosim_pin_write(void *arg, int pin, int value)
215 {
216 struct gpiosim_softc *sc = arg;
217
218 if (value == 0)
219 sc->sc_state &= ~(1 << pin);
220 else
221 sc->sc_state |= (1 << pin);
222 }
223
224 void
225 gpiosim_pin_ctl(void *arg, int pin, int flags)
226 {
227 struct gpiosim_softc *sc = arg;
228
229 sc->sc_gpio_pins[pin].pin_flags = flags;
230 }
231