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