Deleted Added
full compact
gpiobus.c (286845) gpiobus.c (286909)
1/*-
2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/gpio/gpiobus.c 286845 2015-08-17 16:51:37Z loos $");
28__FBSDID("$FreeBSD: head/sys/dev/gpio/gpiobus.c 286909 2015-08-18 21:37:14Z loos $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/gpio.h>
34#include <sys/kernel.h>
35#include <sys/malloc.h>
36#include <sys/module.h>
37
38#include <dev/gpio/gpiobusvar.h>
39
40#include "gpiobus_if.h"
41
42#undef GPIOBUS_DEBUG
43#ifdef GPIOBUS_DEBUG
44#define dprintf printf
45#else
46#define dprintf(x, arg...)
47#endif
48
49static void gpiobus_print_pins(struct gpiobus_ivar *, char *, size_t);
50static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int);
51static int gpiobus_probe(device_t);
52static int gpiobus_attach(device_t);
53static int gpiobus_detach(device_t);
54static int gpiobus_suspend(device_t);
55static int gpiobus_resume(device_t);
56static void gpiobus_probe_nomatch(device_t, device_t);
57static int gpiobus_print_child(device_t, device_t);
58static int gpiobus_child_location_str(device_t, device_t, char *, size_t);
59static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t);
60static device_t gpiobus_add_child(device_t, u_int, const char *, int);
61static void gpiobus_hinted_child(device_t, const char *, int);
62
63/*
64 * GPIOBUS interface
65 */
66static int gpiobus_acquire_bus(device_t, device_t, int);
67static void gpiobus_release_bus(device_t, device_t);
68static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t);
69static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*);
70static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*);
71static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int);
72static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*);
73static int gpiobus_pin_toggle(device_t, device_t, uint32_t);
74
75int
76gpio_check_flags(uint32_t caps, uint32_t flags)
77{
78
79 /* Check for unwanted flags. */
80 if ((flags & caps) == 0 || (flags & caps) != flags)
81 return (EINVAL);
82 /* Cannot mix input/output together. */
83 if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT)
84 return (EINVAL);
85 /* Cannot mix pull-up/pull-down together. */
86 if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN)
87 return (EINVAL);
88
89 return (0);
90}
91
92static void
93gpiobus_print_pins(struct gpiobus_ivar *devi, char *buf, size_t buflen)
94{
95 char tmp[128];
96 int i, range_start, range_stop, need_coma;
97
98 if (devi->npins == 0)
99 return;
100
101 need_coma = 0;
102 range_start = range_stop = devi->pins[0];
103 for (i = 1; i < devi->npins; i++) {
104 if (devi->pins[i] != (range_stop + 1)) {
105 if (need_coma)
106 strlcat(buf, ",", buflen);
107 memset(tmp, 0, sizeof(tmp));
108 if (range_start != range_stop)
109 snprintf(tmp, sizeof(tmp) - 1, "%d-%d",
110 range_start, range_stop);
111 else
112 snprintf(tmp, sizeof(tmp) - 1, "%d",
113 range_start);
114 strlcat(buf, tmp, buflen);
115
116 range_start = range_stop = devi->pins[i];
117 need_coma = 1;
118 }
119 else
120 range_stop++;
121 }
122
123 if (need_coma)
124 strlcat(buf, ",", buflen);
125 memset(tmp, 0, sizeof(tmp));
126 if (range_start != range_stop)
127 snprintf(tmp, sizeof(tmp) - 1, "%d-%d",
128 range_start, range_stop);
129 else
130 snprintf(tmp, sizeof(tmp) - 1, "%d",
131 range_start);
132 strlcat(buf, tmp, buflen);
133}
134
135device_t
136gpiobus_attach_bus(device_t dev)
137{
138 device_t busdev;
139
140 busdev = device_add_child(dev, "gpiobus", -1);
141 if (busdev == NULL)
142 return (NULL);
143 if (device_add_child(dev, "gpioc", -1) == NULL) {
144 device_delete_child(dev, busdev);
145 return (NULL);
146 }
147#ifdef FDT
148 ofw_gpiobus_register_provider(dev);
149#endif
150 bus_generic_attach(dev);
151
152 return (busdev);
153}
154
155int
156gpiobus_detach_bus(device_t dev)
157{
158 int err;
159
160#ifdef FDT
161 ofw_gpiobus_unregister_provider(dev);
162#endif
163 err = bus_generic_detach(dev);
164 if (err != 0)
165 return (err);
166
167 return (device_delete_children(dev));
168}
169
170int
171gpiobus_init_softc(device_t dev)
172{
173 struct gpiobus_softc *sc;
174
175 sc = GPIOBUS_SOFTC(dev);
176 sc->sc_busdev = dev;
177 sc->sc_dev = device_get_parent(dev);
178 sc->sc_intr_rman.rm_type = RMAN_ARRAY;
179 sc->sc_intr_rman.rm_descr = "GPIO Interrupts";
180 if (rman_init(&sc->sc_intr_rman) != 0 ||
181 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0)
182 panic("%s: failed to set up rman.", __func__);
183
184 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
185 return (ENXIO);
186
187 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins"));
188
189 /* Pins = GPIO_PIN_MAX() + 1 */
190 sc->sc_npins++;
191
192 sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF,
193 M_NOWAIT | M_ZERO);
194 if (sc->sc_pins == NULL)
195 return (ENOMEM);
196
197 /* Initialize the bus lock. */
198 GPIOBUS_LOCK_INIT(sc);
199
200 return (0);
201}
202
203int
204gpiobus_alloc_ivars(struct gpiobus_ivar *devi)
205{
206
207 /* Allocate pins and flags memory. */
208 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
209 M_NOWAIT | M_ZERO);
210 if (devi->pins == NULL)
211 return (ENOMEM);
212 devi->flags = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
213 M_NOWAIT | M_ZERO);
214 if (devi->flags == NULL) {
215 free(devi->pins, M_DEVBUF);
216 return (ENOMEM);
217 }
218
219 return (0);
220}
221
222void
223gpiobus_free_ivars(struct gpiobus_ivar *devi)
224{
225
226 if (devi->flags) {
227 free(devi->flags, M_DEVBUF);
228 devi->flags = NULL;
229 }
230 if (devi->pins) {
231 free(devi->pins, M_DEVBUF);
232 devi->pins = NULL;
233 }
234}
235
236int
237gpiobus_map_pin(device_t bus, uint32_t pin)
238{
239 struct gpiobus_softc *sc;
240
241 sc = device_get_softc(bus);
242 /* Consistency check. */
243 if (pin >= sc->sc_npins) {
244 device_printf(bus,
245 "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1);
246 return (-1);
247 }
248 /* Mark pin as mapped and give warning if it's already mapped. */
249 if (sc->sc_pins[pin].mapped) {
250 device_printf(bus, "warning: pin %d is already mapped\n", pin);
251 return (-1);
252 }
253 sc->sc_pins[pin].mapped = 1;
254
255 return (0);
256}
257
258static int
259gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
260{
261 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
262 int i, npins;
263
264 npins = 0;
265 for (i = 0; i < 32; i++) {
266 if (mask & (1 << i))
267 npins++;
268 }
269 if (npins == 0) {
270 device_printf(child, "empty pin mask\n");
271 return (EINVAL);
272 }
273 devi->npins = npins;
274 if (gpiobus_alloc_ivars(devi) != 0) {
275 device_printf(child, "cannot allocate device ivars\n");
276 return (EINVAL);
277 }
278 npins = 0;
279 for (i = 0; i < 32; i++) {
280 if ((mask & (1 << i)) == 0)
281 continue;
282 /* Reserve the GPIO pin. */
283 if (gpiobus_map_pin(sc->sc_busdev, i) != 0) {
284 gpiobus_free_ivars(devi);
285 return (EINVAL);
286 }
287 devi->pins[npins++] = i;
288 /* Use the child name as pin name. */
289 GPIOBUS_PIN_SETNAME(sc->sc_busdev, i,
290 device_get_nameunit(child));
291 }
292
293 return (0);
294}
295
296static int
297gpiobus_probe(device_t dev)
298{
299 device_set_desc(dev, "GPIO bus");
300
301 return (BUS_PROBE_GENERIC);
302}
303
304static int
305gpiobus_attach(device_t dev)
306{
307 int err;
308
309 err = gpiobus_init_softc(dev);
310 if (err != 0)
311 return (err);
312
313 /*
314 * Get parent's pins and mark them as unmapped
315 */
316 bus_generic_probe(dev);
317 bus_enumerate_hinted_children(dev);
318
319 return (bus_generic_attach(dev));
320}
321
322/*
323 * Since this is not a self-enumerating bus, and since we always add
324 * children in attach, we have to always delete children here.
325 */
326static int
327gpiobus_detach(device_t dev)
328{
329 struct gpiobus_softc *sc;
330 struct gpiobus_ivar *devi;
331 device_t *devlist;
332 int i, err, ndevs;
333
334 sc = GPIOBUS_SOFTC(dev);
335 KASSERT(mtx_initialized(&sc->sc_mtx),
336 ("gpiobus mutex not initialized"));
337 GPIOBUS_LOCK_DESTROY(sc);
338
339 if ((err = bus_generic_detach(dev)) != 0)
340 return (err);
341
342 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
343 return (err);
344 for (i = 0; i < ndevs; i++) {
345 devi = GPIOBUS_IVAR(devlist[i]);
346 gpiobus_free_ivars(devi);
347 resource_list_free(&devi->rl);
348 free(devi, M_DEVBUF);
349 device_delete_child(dev, devlist[i]);
350 }
351 free(devlist, M_TEMP);
352 rman_fini(&sc->sc_intr_rman);
353 if (sc->sc_pins) {
354 for (i = 0; i < sc->sc_npins; i++) {
355 if (sc->sc_pins[i].name != NULL)
356 free(sc->sc_pins[i].name, M_DEVBUF);
357 sc->sc_pins[i].name = NULL;
358 }
359 free(sc->sc_pins, M_DEVBUF);
360 sc->sc_pins = NULL;
361 }
362
363 return (0);
364}
365
366static int
367gpiobus_suspend(device_t dev)
368{
369
370 return (bus_generic_suspend(dev));
371}
372
373static int
374gpiobus_resume(device_t dev)
375{
376
377 return (bus_generic_resume(dev));
378}
379
380static void
381gpiobus_probe_nomatch(device_t dev, device_t child)
382{
383 char pins[128];
384 struct gpiobus_ivar *devi;
385
386 devi = GPIOBUS_IVAR(child);
387 memset(pins, 0, sizeof(pins));
388 gpiobus_print_pins(devi, pins, sizeof(pins));
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/gpio.h>
34#include <sys/kernel.h>
35#include <sys/malloc.h>
36#include <sys/module.h>
37
38#include <dev/gpio/gpiobusvar.h>
39
40#include "gpiobus_if.h"
41
42#undef GPIOBUS_DEBUG
43#ifdef GPIOBUS_DEBUG
44#define dprintf printf
45#else
46#define dprintf(x, arg...)
47#endif
48
49static void gpiobus_print_pins(struct gpiobus_ivar *, char *, size_t);
50static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int);
51static int gpiobus_probe(device_t);
52static int gpiobus_attach(device_t);
53static int gpiobus_detach(device_t);
54static int gpiobus_suspend(device_t);
55static int gpiobus_resume(device_t);
56static void gpiobus_probe_nomatch(device_t, device_t);
57static int gpiobus_print_child(device_t, device_t);
58static int gpiobus_child_location_str(device_t, device_t, char *, size_t);
59static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t);
60static device_t gpiobus_add_child(device_t, u_int, const char *, int);
61static void gpiobus_hinted_child(device_t, const char *, int);
62
63/*
64 * GPIOBUS interface
65 */
66static int gpiobus_acquire_bus(device_t, device_t, int);
67static void gpiobus_release_bus(device_t, device_t);
68static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t);
69static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*);
70static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*);
71static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int);
72static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*);
73static int gpiobus_pin_toggle(device_t, device_t, uint32_t);
74
75int
76gpio_check_flags(uint32_t caps, uint32_t flags)
77{
78
79 /* Check for unwanted flags. */
80 if ((flags & caps) == 0 || (flags & caps) != flags)
81 return (EINVAL);
82 /* Cannot mix input/output together. */
83 if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT)
84 return (EINVAL);
85 /* Cannot mix pull-up/pull-down together. */
86 if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN)
87 return (EINVAL);
88
89 return (0);
90}
91
92static void
93gpiobus_print_pins(struct gpiobus_ivar *devi, char *buf, size_t buflen)
94{
95 char tmp[128];
96 int i, range_start, range_stop, need_coma;
97
98 if (devi->npins == 0)
99 return;
100
101 need_coma = 0;
102 range_start = range_stop = devi->pins[0];
103 for (i = 1; i < devi->npins; i++) {
104 if (devi->pins[i] != (range_stop + 1)) {
105 if (need_coma)
106 strlcat(buf, ",", buflen);
107 memset(tmp, 0, sizeof(tmp));
108 if (range_start != range_stop)
109 snprintf(tmp, sizeof(tmp) - 1, "%d-%d",
110 range_start, range_stop);
111 else
112 snprintf(tmp, sizeof(tmp) - 1, "%d",
113 range_start);
114 strlcat(buf, tmp, buflen);
115
116 range_start = range_stop = devi->pins[i];
117 need_coma = 1;
118 }
119 else
120 range_stop++;
121 }
122
123 if (need_coma)
124 strlcat(buf, ",", buflen);
125 memset(tmp, 0, sizeof(tmp));
126 if (range_start != range_stop)
127 snprintf(tmp, sizeof(tmp) - 1, "%d-%d",
128 range_start, range_stop);
129 else
130 snprintf(tmp, sizeof(tmp) - 1, "%d",
131 range_start);
132 strlcat(buf, tmp, buflen);
133}
134
135device_t
136gpiobus_attach_bus(device_t dev)
137{
138 device_t busdev;
139
140 busdev = device_add_child(dev, "gpiobus", -1);
141 if (busdev == NULL)
142 return (NULL);
143 if (device_add_child(dev, "gpioc", -1) == NULL) {
144 device_delete_child(dev, busdev);
145 return (NULL);
146 }
147#ifdef FDT
148 ofw_gpiobus_register_provider(dev);
149#endif
150 bus_generic_attach(dev);
151
152 return (busdev);
153}
154
155int
156gpiobus_detach_bus(device_t dev)
157{
158 int err;
159
160#ifdef FDT
161 ofw_gpiobus_unregister_provider(dev);
162#endif
163 err = bus_generic_detach(dev);
164 if (err != 0)
165 return (err);
166
167 return (device_delete_children(dev));
168}
169
170int
171gpiobus_init_softc(device_t dev)
172{
173 struct gpiobus_softc *sc;
174
175 sc = GPIOBUS_SOFTC(dev);
176 sc->sc_busdev = dev;
177 sc->sc_dev = device_get_parent(dev);
178 sc->sc_intr_rman.rm_type = RMAN_ARRAY;
179 sc->sc_intr_rman.rm_descr = "GPIO Interrupts";
180 if (rman_init(&sc->sc_intr_rman) != 0 ||
181 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0)
182 panic("%s: failed to set up rman.", __func__);
183
184 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
185 return (ENXIO);
186
187 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins"));
188
189 /* Pins = GPIO_PIN_MAX() + 1 */
190 sc->sc_npins++;
191
192 sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF,
193 M_NOWAIT | M_ZERO);
194 if (sc->sc_pins == NULL)
195 return (ENOMEM);
196
197 /* Initialize the bus lock. */
198 GPIOBUS_LOCK_INIT(sc);
199
200 return (0);
201}
202
203int
204gpiobus_alloc_ivars(struct gpiobus_ivar *devi)
205{
206
207 /* Allocate pins and flags memory. */
208 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
209 M_NOWAIT | M_ZERO);
210 if (devi->pins == NULL)
211 return (ENOMEM);
212 devi->flags = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
213 M_NOWAIT | M_ZERO);
214 if (devi->flags == NULL) {
215 free(devi->pins, M_DEVBUF);
216 return (ENOMEM);
217 }
218
219 return (0);
220}
221
222void
223gpiobus_free_ivars(struct gpiobus_ivar *devi)
224{
225
226 if (devi->flags) {
227 free(devi->flags, M_DEVBUF);
228 devi->flags = NULL;
229 }
230 if (devi->pins) {
231 free(devi->pins, M_DEVBUF);
232 devi->pins = NULL;
233 }
234}
235
236int
237gpiobus_map_pin(device_t bus, uint32_t pin)
238{
239 struct gpiobus_softc *sc;
240
241 sc = device_get_softc(bus);
242 /* Consistency check. */
243 if (pin >= sc->sc_npins) {
244 device_printf(bus,
245 "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1);
246 return (-1);
247 }
248 /* Mark pin as mapped and give warning if it's already mapped. */
249 if (sc->sc_pins[pin].mapped) {
250 device_printf(bus, "warning: pin %d is already mapped\n", pin);
251 return (-1);
252 }
253 sc->sc_pins[pin].mapped = 1;
254
255 return (0);
256}
257
258static int
259gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
260{
261 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
262 int i, npins;
263
264 npins = 0;
265 for (i = 0; i < 32; i++) {
266 if (mask & (1 << i))
267 npins++;
268 }
269 if (npins == 0) {
270 device_printf(child, "empty pin mask\n");
271 return (EINVAL);
272 }
273 devi->npins = npins;
274 if (gpiobus_alloc_ivars(devi) != 0) {
275 device_printf(child, "cannot allocate device ivars\n");
276 return (EINVAL);
277 }
278 npins = 0;
279 for (i = 0; i < 32; i++) {
280 if ((mask & (1 << i)) == 0)
281 continue;
282 /* Reserve the GPIO pin. */
283 if (gpiobus_map_pin(sc->sc_busdev, i) != 0) {
284 gpiobus_free_ivars(devi);
285 return (EINVAL);
286 }
287 devi->pins[npins++] = i;
288 /* Use the child name as pin name. */
289 GPIOBUS_PIN_SETNAME(sc->sc_busdev, i,
290 device_get_nameunit(child));
291 }
292
293 return (0);
294}
295
296static int
297gpiobus_probe(device_t dev)
298{
299 device_set_desc(dev, "GPIO bus");
300
301 return (BUS_PROBE_GENERIC);
302}
303
304static int
305gpiobus_attach(device_t dev)
306{
307 int err;
308
309 err = gpiobus_init_softc(dev);
310 if (err != 0)
311 return (err);
312
313 /*
314 * Get parent's pins and mark them as unmapped
315 */
316 bus_generic_probe(dev);
317 bus_enumerate_hinted_children(dev);
318
319 return (bus_generic_attach(dev));
320}
321
322/*
323 * Since this is not a self-enumerating bus, and since we always add
324 * children in attach, we have to always delete children here.
325 */
326static int
327gpiobus_detach(device_t dev)
328{
329 struct gpiobus_softc *sc;
330 struct gpiobus_ivar *devi;
331 device_t *devlist;
332 int i, err, ndevs;
333
334 sc = GPIOBUS_SOFTC(dev);
335 KASSERT(mtx_initialized(&sc->sc_mtx),
336 ("gpiobus mutex not initialized"));
337 GPIOBUS_LOCK_DESTROY(sc);
338
339 if ((err = bus_generic_detach(dev)) != 0)
340 return (err);
341
342 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
343 return (err);
344 for (i = 0; i < ndevs; i++) {
345 devi = GPIOBUS_IVAR(devlist[i]);
346 gpiobus_free_ivars(devi);
347 resource_list_free(&devi->rl);
348 free(devi, M_DEVBUF);
349 device_delete_child(dev, devlist[i]);
350 }
351 free(devlist, M_TEMP);
352 rman_fini(&sc->sc_intr_rman);
353 if (sc->sc_pins) {
354 for (i = 0; i < sc->sc_npins; i++) {
355 if (sc->sc_pins[i].name != NULL)
356 free(sc->sc_pins[i].name, M_DEVBUF);
357 sc->sc_pins[i].name = NULL;
358 }
359 free(sc->sc_pins, M_DEVBUF);
360 sc->sc_pins = NULL;
361 }
362
363 return (0);
364}
365
366static int
367gpiobus_suspend(device_t dev)
368{
369
370 return (bus_generic_suspend(dev));
371}
372
373static int
374gpiobus_resume(device_t dev)
375{
376
377 return (bus_generic_resume(dev));
378}
379
380static void
381gpiobus_probe_nomatch(device_t dev, device_t child)
382{
383 char pins[128];
384 struct gpiobus_ivar *devi;
385
386 devi = GPIOBUS_IVAR(child);
387 memset(pins, 0, sizeof(pins));
388 gpiobus_print_pins(devi, pins, sizeof(pins));
389 device_printf(dev, "<unknown device> at pin(s) %s", pins);
389 if (devi->npins > 1)
390 device_printf(dev, "<unknown device> at pins %s", pins);
391 else
392 device_printf(dev, "<unknown device> at pin %s", pins);
390 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%ld");
391 printf("\n");
392}
393
394static int
395gpiobus_print_child(device_t dev, device_t child)
396{
397 char pins[128];
398 int retval = 0;
399 struct gpiobus_ivar *devi;
400
401 devi = GPIOBUS_IVAR(child);
402 memset(pins, 0, sizeof(pins));
403 retval += bus_print_child_header(dev, child);
404 if (devi->npins > 0) {
405 if (devi->npins > 1)
406 retval += printf(" at pins ");
407 else
408 retval += printf(" at pin ");
409 gpiobus_print_pins(devi, pins, sizeof(pins));
410 retval += printf("%s", pins);
411 }
412 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%ld");
413 retval += bus_print_child_footer(dev, child);
414
415 return (retval);
416}
417
418static int
419gpiobus_child_location_str(device_t bus, device_t child, char *buf,
420 size_t buflen)
421{
422 struct gpiobus_ivar *devi;
423
424 devi = GPIOBUS_IVAR(child);
393 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%ld");
394 printf("\n");
395}
396
397static int
398gpiobus_print_child(device_t dev, device_t child)
399{
400 char pins[128];
401 int retval = 0;
402 struct gpiobus_ivar *devi;
403
404 devi = GPIOBUS_IVAR(child);
405 memset(pins, 0, sizeof(pins));
406 retval += bus_print_child_header(dev, child);
407 if (devi->npins > 0) {
408 if (devi->npins > 1)
409 retval += printf(" at pins ");
410 else
411 retval += printf(" at pin ");
412 gpiobus_print_pins(devi, pins, sizeof(pins));
413 retval += printf("%s", pins);
414 }
415 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%ld");
416 retval += bus_print_child_footer(dev, child);
417
418 return (retval);
419}
420
421static int
422gpiobus_child_location_str(device_t bus, device_t child, char *buf,
423 size_t buflen)
424{
425 struct gpiobus_ivar *devi;
426
427 devi = GPIOBUS_IVAR(child);
425 strlcpy(buf, "pin(s)=", buflen);
428 if (devi->npins > 1)
429 strlcpy(buf, "pins=", buflen);
430 else
431 strlcpy(buf, "pin=", buflen);
426 gpiobus_print_pins(devi, buf, buflen);
427
428 return (0);
429}
430
431static int
432gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
433 size_t buflen)
434{
435
436 *buf = '\0';
437 return (0);
438}
439
440static device_t
441gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
442{
443 device_t child;
444 struct gpiobus_ivar *devi;
445
446 child = device_add_child_ordered(dev, order, name, unit);
447 if (child == NULL)
448 return (child);
449 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
450 if (devi == NULL) {
451 device_delete_child(dev, child);
452 return (NULL);
453 }
454 resource_list_init(&devi->rl);
455 device_set_ivars(child, devi);
456
457 return (child);
458}
459
460static void
461gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
462{
463 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
464 struct gpiobus_ivar *devi;
465 device_t child;
466 int irq, pins;
467
468 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
469 devi = GPIOBUS_IVAR(child);
470 resource_int_value(dname, dunit, "pins", &pins);
471 if (gpiobus_parse_pins(sc, child, pins)) {
472 resource_list_free(&devi->rl);
473 free(devi, M_DEVBUF);
474 device_delete_child(bus, child);
475 }
476 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
477 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
478 device_printf(bus,
479 "warning: bus_set_resource() failed\n");
480 }
481}
482
483static int
484gpiobus_set_resource(device_t dev, device_t child, int type, int rid,
485 u_long start, u_long count)
486{
487 struct gpiobus_ivar *devi;
488 struct resource_list_entry *rle;
489
490 dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n",
491 __func__, dev, child, type, rid, (void *)(intptr_t)start, count);
492 devi = GPIOBUS_IVAR(child);
493 rle = resource_list_add(&devi->rl, type, rid, start,
494 start + count - 1, count);
495 if (rle == NULL)
496 return (ENXIO);
497
498 return (0);
499}
500
501static struct resource *
502gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
503 u_long start, u_long end, u_long count, u_int flags)
504{
505 struct gpiobus_softc *sc;
506 struct resource *rv;
507 struct resource_list *rl;
508 struct resource_list_entry *rle;
509 int isdefault;
510
511 if (type != SYS_RES_IRQ)
512 return (NULL);
513 isdefault = (start == 0UL && end == ~0UL && count == 1);
514 rle = NULL;
515 if (isdefault) {
516 rl = BUS_GET_RESOURCE_LIST(bus, child);
517 if (rl == NULL)
518 return (NULL);
519 rle = resource_list_find(rl, type, *rid);
520 if (rle == NULL)
521 return (NULL);
522 if (rle->res != NULL)
523 panic("%s: resource entry is busy", __func__);
524 start = rle->start;
525 count = rle->count;
526 end = rle->end;
527 }
528 sc = device_get_softc(bus);
529 rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags,
530 child);
531 if (rv == NULL)
532 return (NULL);
533 rman_set_rid(rv, *rid);
534 if ((flags & RF_ACTIVE) != 0 &&
535 bus_activate_resource(child, type, *rid, rv) != 0) {
536 rman_release_resource(rv);
537 return (NULL);
538 }
539
540 return (rv);
541}
542
543static int
544gpiobus_release_resource(device_t bus __unused, device_t child, int type,
545 int rid, struct resource *r)
546{
547 int error;
548
549 if (rman_get_flags(r) & RF_ACTIVE) {
550 error = bus_deactivate_resource(child, type, rid, r);
551 if (error)
552 return (error);
553 }
554
555 return (rman_release_resource(r));
556}
557
558static struct resource_list *
559gpiobus_get_resource_list(device_t bus __unused, device_t child)
560{
561 struct gpiobus_ivar *ivar;
562
563 ivar = GPIOBUS_IVAR(child);
564
565 return (&ivar->rl);
566}
567
568static int
569gpiobus_acquire_bus(device_t busdev, device_t child, int how)
570{
571 struct gpiobus_softc *sc;
572
573 sc = device_get_softc(busdev);
574 GPIOBUS_ASSERT_UNLOCKED(sc);
575 GPIOBUS_LOCK(sc);
576 if (sc->sc_owner != NULL) {
577 if (sc->sc_owner == child)
578 panic("%s: %s still owns the bus.",
579 device_get_nameunit(busdev),
580 device_get_nameunit(child));
581 if (how == GPIOBUS_DONTWAIT) {
582 GPIOBUS_UNLOCK(sc);
583 return (EWOULDBLOCK);
584 }
585 while (sc->sc_owner != NULL)
586 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0);
587 }
588 sc->sc_owner = child;
589 GPIOBUS_UNLOCK(sc);
590
591 return (0);
592}
593
594static void
595gpiobus_release_bus(device_t busdev, device_t child)
596{
597 struct gpiobus_softc *sc;
598
599 sc = device_get_softc(busdev);
600 GPIOBUS_ASSERT_UNLOCKED(sc);
601 GPIOBUS_LOCK(sc);
602 if (sc->sc_owner == NULL)
603 panic("%s: %s releasing unowned bus.",
604 device_get_nameunit(busdev),
605 device_get_nameunit(child));
606 if (sc->sc_owner != child)
607 panic("%s: %s trying to release bus owned by %s",
608 device_get_nameunit(busdev),
609 device_get_nameunit(child),
610 device_get_nameunit(sc->sc_owner));
611 sc->sc_owner = NULL;
612 wakeup(sc);
613 GPIOBUS_UNLOCK(sc);
614}
615
616static int
617gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
618 uint32_t flags)
619{
620 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
621 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
622 uint32_t caps;
623
624 if (pin >= devi->npins)
625 return (EINVAL);
626 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0)
627 return (EINVAL);
628 if (gpio_check_flags(caps, flags) != 0)
629 return (EINVAL);
630
631 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags));
632}
633
634static int
635gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
636 uint32_t *flags)
637{
638 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
639 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
640
641 if (pin >= devi->npins)
642 return (EINVAL);
643
644 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
645}
646
647static int
648gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
649 uint32_t *caps)
650{
651 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
652 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
653
654 if (pin >= devi->npins)
655 return (EINVAL);
656
657 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
658}
659
660static int
661gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
662 unsigned int value)
663{
664 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
665 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
666
667 if (pin >= devi->npins)
668 return (EINVAL);
669
670 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
671}
672
673static int
674gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
675 unsigned int *value)
676{
677 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
678 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
679
680 if (pin >= devi->npins)
681 return (EINVAL);
682
683 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
684}
685
686static int
687gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
688{
689 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
690 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
691
692 if (pin >= devi->npins)
693 return (EINVAL);
694
695 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
696}
697
698static int
699gpiobus_pin_getname(device_t dev, uint32_t pin, char *name)
700{
701 struct gpiobus_softc *sc;
702
703 sc = GPIOBUS_SOFTC(dev);
704 if (pin > sc->sc_npins)
705 return (EINVAL);
706 /* Did we have a name for this pin ? */
707 if (sc->sc_pins[pin].name != NULL) {
708 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME);
709 return (0);
710 }
711
712 /* Return the default pin name. */
713 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name));
714}
715
716static int
717gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name)
718{
719 struct gpiobus_softc *sc;
720
721 sc = GPIOBUS_SOFTC(dev);
722 if (pin > sc->sc_npins)
723 return (EINVAL);
724 if (name == NULL)
725 return (EINVAL);
726 /* Save the pin name. */
727 if (sc->sc_pins[pin].name == NULL)
728 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF,
729 M_WAITOK | M_ZERO);
730 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME);
731
732 return (0);
733}
734
735static device_method_t gpiobus_methods[] = {
736 /* Device interface */
737 DEVMETHOD(device_probe, gpiobus_probe),
738 DEVMETHOD(device_attach, gpiobus_attach),
739 DEVMETHOD(device_detach, gpiobus_detach),
740 DEVMETHOD(device_shutdown, bus_generic_shutdown),
741 DEVMETHOD(device_suspend, gpiobus_suspend),
742 DEVMETHOD(device_resume, gpiobus_resume),
743
744 /* Bus interface */
745 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
746 DEVMETHOD(bus_config_intr, bus_generic_config_intr),
747 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
748 DEVMETHOD(bus_set_resource, gpiobus_set_resource),
749 DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource),
750 DEVMETHOD(bus_release_resource, gpiobus_release_resource),
751 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
752 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
753 DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list),
754 DEVMETHOD(bus_add_child, gpiobus_add_child),
755 DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch),
756 DEVMETHOD(bus_print_child, gpiobus_print_child),
757 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str),
758 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str),
759 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child),
760
761 /* GPIO protocol */
762 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus),
763 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus),
764 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags),
765 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps),
766 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags),
767 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get),
768 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set),
769 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle),
770 DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname),
771 DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname),
772
773 DEVMETHOD_END
774};
775
776driver_t gpiobus_driver = {
777 "gpiobus",
778 gpiobus_methods,
779 sizeof(struct gpiobus_softc)
780};
781
782devclass_t gpiobus_devclass;
783
784DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0);
785MODULE_VERSION(gpiobus, 1);
432 gpiobus_print_pins(devi, buf, buflen);
433
434 return (0);
435}
436
437static int
438gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
439 size_t buflen)
440{
441
442 *buf = '\0';
443 return (0);
444}
445
446static device_t
447gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
448{
449 device_t child;
450 struct gpiobus_ivar *devi;
451
452 child = device_add_child_ordered(dev, order, name, unit);
453 if (child == NULL)
454 return (child);
455 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
456 if (devi == NULL) {
457 device_delete_child(dev, child);
458 return (NULL);
459 }
460 resource_list_init(&devi->rl);
461 device_set_ivars(child, devi);
462
463 return (child);
464}
465
466static void
467gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
468{
469 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
470 struct gpiobus_ivar *devi;
471 device_t child;
472 int irq, pins;
473
474 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
475 devi = GPIOBUS_IVAR(child);
476 resource_int_value(dname, dunit, "pins", &pins);
477 if (gpiobus_parse_pins(sc, child, pins)) {
478 resource_list_free(&devi->rl);
479 free(devi, M_DEVBUF);
480 device_delete_child(bus, child);
481 }
482 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
483 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
484 device_printf(bus,
485 "warning: bus_set_resource() failed\n");
486 }
487}
488
489static int
490gpiobus_set_resource(device_t dev, device_t child, int type, int rid,
491 u_long start, u_long count)
492{
493 struct gpiobus_ivar *devi;
494 struct resource_list_entry *rle;
495
496 dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n",
497 __func__, dev, child, type, rid, (void *)(intptr_t)start, count);
498 devi = GPIOBUS_IVAR(child);
499 rle = resource_list_add(&devi->rl, type, rid, start,
500 start + count - 1, count);
501 if (rle == NULL)
502 return (ENXIO);
503
504 return (0);
505}
506
507static struct resource *
508gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
509 u_long start, u_long end, u_long count, u_int flags)
510{
511 struct gpiobus_softc *sc;
512 struct resource *rv;
513 struct resource_list *rl;
514 struct resource_list_entry *rle;
515 int isdefault;
516
517 if (type != SYS_RES_IRQ)
518 return (NULL);
519 isdefault = (start == 0UL && end == ~0UL && count == 1);
520 rle = NULL;
521 if (isdefault) {
522 rl = BUS_GET_RESOURCE_LIST(bus, child);
523 if (rl == NULL)
524 return (NULL);
525 rle = resource_list_find(rl, type, *rid);
526 if (rle == NULL)
527 return (NULL);
528 if (rle->res != NULL)
529 panic("%s: resource entry is busy", __func__);
530 start = rle->start;
531 count = rle->count;
532 end = rle->end;
533 }
534 sc = device_get_softc(bus);
535 rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags,
536 child);
537 if (rv == NULL)
538 return (NULL);
539 rman_set_rid(rv, *rid);
540 if ((flags & RF_ACTIVE) != 0 &&
541 bus_activate_resource(child, type, *rid, rv) != 0) {
542 rman_release_resource(rv);
543 return (NULL);
544 }
545
546 return (rv);
547}
548
549static int
550gpiobus_release_resource(device_t bus __unused, device_t child, int type,
551 int rid, struct resource *r)
552{
553 int error;
554
555 if (rman_get_flags(r) & RF_ACTIVE) {
556 error = bus_deactivate_resource(child, type, rid, r);
557 if (error)
558 return (error);
559 }
560
561 return (rman_release_resource(r));
562}
563
564static struct resource_list *
565gpiobus_get_resource_list(device_t bus __unused, device_t child)
566{
567 struct gpiobus_ivar *ivar;
568
569 ivar = GPIOBUS_IVAR(child);
570
571 return (&ivar->rl);
572}
573
574static int
575gpiobus_acquire_bus(device_t busdev, device_t child, int how)
576{
577 struct gpiobus_softc *sc;
578
579 sc = device_get_softc(busdev);
580 GPIOBUS_ASSERT_UNLOCKED(sc);
581 GPIOBUS_LOCK(sc);
582 if (sc->sc_owner != NULL) {
583 if (sc->sc_owner == child)
584 panic("%s: %s still owns the bus.",
585 device_get_nameunit(busdev),
586 device_get_nameunit(child));
587 if (how == GPIOBUS_DONTWAIT) {
588 GPIOBUS_UNLOCK(sc);
589 return (EWOULDBLOCK);
590 }
591 while (sc->sc_owner != NULL)
592 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0);
593 }
594 sc->sc_owner = child;
595 GPIOBUS_UNLOCK(sc);
596
597 return (0);
598}
599
600static void
601gpiobus_release_bus(device_t busdev, device_t child)
602{
603 struct gpiobus_softc *sc;
604
605 sc = device_get_softc(busdev);
606 GPIOBUS_ASSERT_UNLOCKED(sc);
607 GPIOBUS_LOCK(sc);
608 if (sc->sc_owner == NULL)
609 panic("%s: %s releasing unowned bus.",
610 device_get_nameunit(busdev),
611 device_get_nameunit(child));
612 if (sc->sc_owner != child)
613 panic("%s: %s trying to release bus owned by %s",
614 device_get_nameunit(busdev),
615 device_get_nameunit(child),
616 device_get_nameunit(sc->sc_owner));
617 sc->sc_owner = NULL;
618 wakeup(sc);
619 GPIOBUS_UNLOCK(sc);
620}
621
622static int
623gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
624 uint32_t flags)
625{
626 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
627 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
628 uint32_t caps;
629
630 if (pin >= devi->npins)
631 return (EINVAL);
632 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0)
633 return (EINVAL);
634 if (gpio_check_flags(caps, flags) != 0)
635 return (EINVAL);
636
637 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags));
638}
639
640static int
641gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
642 uint32_t *flags)
643{
644 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
645 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
646
647 if (pin >= devi->npins)
648 return (EINVAL);
649
650 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
651}
652
653static int
654gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
655 uint32_t *caps)
656{
657 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
658 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
659
660 if (pin >= devi->npins)
661 return (EINVAL);
662
663 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
664}
665
666static int
667gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
668 unsigned int value)
669{
670 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
671 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
672
673 if (pin >= devi->npins)
674 return (EINVAL);
675
676 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
677}
678
679static int
680gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
681 unsigned int *value)
682{
683 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
684 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
685
686 if (pin >= devi->npins)
687 return (EINVAL);
688
689 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
690}
691
692static int
693gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
694{
695 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
696 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
697
698 if (pin >= devi->npins)
699 return (EINVAL);
700
701 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
702}
703
704static int
705gpiobus_pin_getname(device_t dev, uint32_t pin, char *name)
706{
707 struct gpiobus_softc *sc;
708
709 sc = GPIOBUS_SOFTC(dev);
710 if (pin > sc->sc_npins)
711 return (EINVAL);
712 /* Did we have a name for this pin ? */
713 if (sc->sc_pins[pin].name != NULL) {
714 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME);
715 return (0);
716 }
717
718 /* Return the default pin name. */
719 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name));
720}
721
722static int
723gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name)
724{
725 struct gpiobus_softc *sc;
726
727 sc = GPIOBUS_SOFTC(dev);
728 if (pin > sc->sc_npins)
729 return (EINVAL);
730 if (name == NULL)
731 return (EINVAL);
732 /* Save the pin name. */
733 if (sc->sc_pins[pin].name == NULL)
734 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF,
735 M_WAITOK | M_ZERO);
736 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME);
737
738 return (0);
739}
740
741static device_method_t gpiobus_methods[] = {
742 /* Device interface */
743 DEVMETHOD(device_probe, gpiobus_probe),
744 DEVMETHOD(device_attach, gpiobus_attach),
745 DEVMETHOD(device_detach, gpiobus_detach),
746 DEVMETHOD(device_shutdown, bus_generic_shutdown),
747 DEVMETHOD(device_suspend, gpiobus_suspend),
748 DEVMETHOD(device_resume, gpiobus_resume),
749
750 /* Bus interface */
751 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
752 DEVMETHOD(bus_config_intr, bus_generic_config_intr),
753 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
754 DEVMETHOD(bus_set_resource, gpiobus_set_resource),
755 DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource),
756 DEVMETHOD(bus_release_resource, gpiobus_release_resource),
757 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
758 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
759 DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list),
760 DEVMETHOD(bus_add_child, gpiobus_add_child),
761 DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch),
762 DEVMETHOD(bus_print_child, gpiobus_print_child),
763 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str),
764 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str),
765 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child),
766
767 /* GPIO protocol */
768 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus),
769 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus),
770 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags),
771 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps),
772 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags),
773 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get),
774 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set),
775 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle),
776 DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname),
777 DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname),
778
779 DEVMETHOD_END
780};
781
782driver_t gpiobus_driver = {
783 "gpiobus",
784 gpiobus_methods,
785 sizeof(struct gpiobus_softc)
786};
787
788devclass_t gpiobus_devclass;
789
790DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0);
791MODULE_VERSION(gpiobus, 1);