1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/gpio.h> 33 #ifdef INTRNG 34 #include <sys/intr.h> 35 #endif 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/sbuf.h> 40 41 #include <dev/gpio/gpiobusvar.h> 42 43 #include "gpiobus_if.h" 44 45 #undef GPIOBUS_DEBUG 46 #ifdef GPIOBUS_DEBUG 47 #define dprintf printf 48 #else 49 #define dprintf(x, arg...) 50 #endif 51 52 static void gpiobus_print_pins(struct gpiobus_ivar *, struct sbuf *); 53 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); 54 static int gpiobus_probe(device_t); 55 static int gpiobus_suspend(device_t); 56 static int gpiobus_resume(device_t); 57 static void gpiobus_probe_nomatch(device_t, device_t); 58 static int gpiobus_print_child(device_t, device_t); 59 static int gpiobus_child_location(device_t, device_t, struct sbuf *); 60 static device_t gpiobus_add_child(device_t, u_int, const char *, int); 61 static void gpiobus_hinted_child(device_t, const char *, int); 62 63 /* 64 * GPIOBUS interface 65 */ 66 static int gpiobus_acquire_bus(device_t, device_t, int); 67 static void gpiobus_release_bus(device_t, device_t); 68 static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t); 69 static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*); 70 static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*); 71 static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int); 72 static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*); 73 static int gpiobus_pin_toggle(device_t, device_t, uint32_t); 74 75 /* 76 * gpiobus_pin flags 77 * The flags in struct gpiobus_pin are not related to the flags used by the 78 * low-level controller driver in struct gpio_pin. Currently, only pins 79 * acquired via FDT data have gpiobus_pin.flags set, sourced from the flags in 80 * the FDT properties. In theory, these flags are defined per-platform. In 81 * practice they are always the flags from the dt-bindings/gpio/gpio.h file. 82 * The only one of those flags we currently support is for handling active-low 83 * pins, so we just define that flag here instead of including a GPL'd header. 84 */ 85 #define GPIO_ACTIVE_LOW 1 86 87 /* 88 * XXX -> Move me to better place - gpio_subr.c? 89 * Also, this function must be changed when interrupt configuration 90 * data will be moved into struct resource. 91 */ 92 #ifdef INTRNG 93 94 struct resource * 95 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, 96 gpio_pin_t pin, uint32_t intr_mode) 97 { 98 u_int irq; 99 struct intr_map_data_gpio *gpio_data; 100 struct resource *res; 101 102 gpio_data = (struct intr_map_data_gpio *)intr_alloc_map_data( 103 INTR_MAP_DATA_GPIO, sizeof(*gpio_data), M_WAITOK | M_ZERO); 104 gpio_data->gpio_pin_num = pin->pin; 105 gpio_data->gpio_pin_flags = pin->flags; 106 gpio_data->gpio_intr_mode = intr_mode; 107 108 irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data); 109 res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1, 110 alloc_flags); 111 if (res == NULL) { 112 intr_free_intr_map_data((struct intr_map_data *)gpio_data); 113 return (NULL); 114 } 115 rman_set_virtual(res, gpio_data); 116 return (res); 117 } 118 #else 119 struct resource * 120 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, 121 gpio_pin_t pin, uint32_t intr_mode) 122 { 123 124 return (NULL); 125 } 126 #endif 127 128 int 129 gpio_check_flags(uint32_t caps, uint32_t flags) 130 { 131 132 /* Filter unwanted flags. */ 133 flags &= caps; 134 135 /* Cannot mix input/output together. */ 136 if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT) 137 return (EINVAL); 138 /* Cannot mix pull-up/pull-down together. */ 139 if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN) 140 return (EINVAL); 141 /* Cannot mix output and interrupt flags together */ 142 if (flags & GPIO_PIN_OUTPUT && flags & GPIO_INTR_MASK) 143 return (EINVAL); 144 /* Only one interrupt flag can be defined at once */ 145 if ((flags & GPIO_INTR_MASK) & ((flags & GPIO_INTR_MASK) - 1)) 146 return (EINVAL); 147 /* The interrupt attached flag cannot be set */ 148 if (flags & GPIO_INTR_ATTACHED) 149 return (EINVAL); 150 151 return (0); 152 } 153 154 int 155 gpio_pin_get_by_bus_pinnum(device_t busdev, uint32_t pinnum, gpio_pin_t *ppin) 156 { 157 gpio_pin_t pin; 158 int err; 159 160 err = gpiobus_acquire_pin(busdev, pinnum); 161 if (err != 0) 162 return (EBUSY); 163 164 pin = malloc(sizeof(*pin), M_DEVBUF, M_WAITOK | M_ZERO); 165 166 pin->dev = device_get_parent(busdev); 167 pin->pin = pinnum; 168 pin->flags = 0; 169 170 *ppin = pin; 171 return (0); 172 } 173 174 int 175 gpio_pin_get_by_child_index(device_t childdev, uint32_t idx, gpio_pin_t *ppin) 176 { 177 struct gpiobus_ivar *devi; 178 179 devi = GPIOBUS_IVAR(childdev); 180 if (idx >= devi->npins) 181 return (EINVAL); 182 183 return (gpio_pin_get_by_bus_pinnum(device_get_parent(childdev), 184 devi->pins[idx], ppin)); 185 } 186 187 int 188 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps) 189 { 190 191 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 192 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 193 return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps)); 194 } 195 196 int 197 gpio_pin_is_active(gpio_pin_t pin, bool *active) 198 { 199 int rv; 200 uint32_t tmp; 201 202 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 203 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 204 rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp); 205 if (rv != 0) { 206 return (rv); 207 } 208 209 if (pin->flags & GPIO_ACTIVE_LOW) 210 *active = tmp == 0; 211 else 212 *active = tmp != 0; 213 return (0); 214 } 215 216 void 217 gpio_pin_release(gpio_pin_t gpio) 218 { 219 device_t busdev; 220 221 if (gpio == NULL) 222 return; 223 224 KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL.")); 225 226 busdev = GPIO_GET_BUS(gpio->dev); 227 if (busdev != NULL) 228 gpiobus_release_pin(busdev, gpio->pin); 229 230 free(gpio, M_DEVBUF); 231 } 232 233 int 234 gpio_pin_set_active(gpio_pin_t pin, bool active) 235 { 236 int rv; 237 uint32_t tmp; 238 239 if (pin->flags & GPIO_ACTIVE_LOW) 240 tmp = active ? 0 : 1; 241 else 242 tmp = active ? 1 : 0; 243 244 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 245 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 246 rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp); 247 return (rv); 248 } 249 250 int 251 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags) 252 { 253 int rv; 254 255 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 256 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 257 258 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags); 259 return (rv); 260 } 261 262 static void 263 gpiobus_print_pins(struct gpiobus_ivar *devi, struct sbuf *sb) 264 { 265 int i, range_start, range_stop, need_coma; 266 267 if (devi->npins == 0) 268 return; 269 270 need_coma = 0; 271 range_start = range_stop = devi->pins[0]; 272 for (i = 1; i < devi->npins; i++) { 273 if (devi->pins[i] != (range_stop + 1)) { 274 if (need_coma) 275 sbuf_cat(sb, ","); 276 if (range_start != range_stop) 277 sbuf_printf(sb, "%d-%d", range_start, range_stop); 278 else 279 sbuf_printf(sb, "%d", range_start); 280 range_start = range_stop = devi->pins[i]; 281 need_coma = 1; 282 } 283 else 284 range_stop++; 285 } 286 287 if (need_coma) 288 sbuf_cat(sb, ","); 289 if (range_start != range_stop) 290 sbuf_printf(sb, "%d-%d", range_start, range_stop); 291 else 292 sbuf_printf(sb, "%d", range_start); 293 } 294 295 device_t 296 gpiobus_attach_bus(device_t dev) 297 { 298 device_t busdev; 299 300 busdev = device_add_child(dev, "gpiobus", DEVICE_UNIT_ANY); 301 if (busdev == NULL) 302 return (NULL); 303 if (device_add_child(dev, "gpioc", -1) == NULL) { 304 device_delete_child(dev, busdev); 305 return (NULL); 306 } 307 #ifdef FDT 308 ofw_gpiobus_register_provider(dev); 309 #endif 310 bus_generic_attach(dev); 311 312 return (busdev); 313 } 314 315 int 316 gpiobus_detach_bus(device_t dev) 317 { 318 int err; 319 320 #ifdef FDT 321 ofw_gpiobus_unregister_provider(dev); 322 #endif 323 err = bus_generic_detach(dev); 324 if (err != 0) 325 return (err); 326 327 return (device_delete_children(dev)); 328 } 329 330 int 331 gpiobus_init_softc(device_t dev) 332 { 333 struct gpiobus_softc *sc; 334 335 sc = GPIOBUS_SOFTC(dev); 336 sc->sc_busdev = dev; 337 sc->sc_dev = device_get_parent(dev); 338 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 339 sc->sc_intr_rman.rm_descr = "GPIO Interrupts"; 340 if (rman_init(&sc->sc_intr_rman) != 0 || 341 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0) 342 panic("%s: failed to set up rman.", __func__); 343 344 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) 345 return (ENXIO); 346 347 KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins")); 348 349 /* Pins = GPIO_PIN_MAX() + 1 */ 350 sc->sc_npins++; 351 352 sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF, 353 M_NOWAIT | M_ZERO); 354 if (sc->sc_pins == NULL) 355 return (ENOMEM); 356 357 /* Initialize the bus lock. */ 358 GPIOBUS_LOCK_INIT(sc); 359 360 return (0); 361 } 362 363 int 364 gpiobus_alloc_ivars(struct gpiobus_ivar *devi) 365 { 366 367 /* Allocate pins and flags memory. */ 368 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, 369 M_NOWAIT | M_ZERO); 370 if (devi->pins == NULL) 371 return (ENOMEM); 372 return (0); 373 } 374 375 void 376 gpiobus_free_ivars(struct gpiobus_ivar *devi) 377 { 378 379 if (devi->pins) { 380 free(devi->pins, M_DEVBUF); 381 devi->pins = NULL; 382 } 383 devi->npins = 0; 384 } 385 386 int 387 gpiobus_acquire_pin(device_t bus, uint32_t pin) 388 { 389 struct gpiobus_softc *sc; 390 391 sc = device_get_softc(bus); 392 /* Consistency check. */ 393 if (pin >= sc->sc_npins) { 394 device_printf(bus, 395 "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1); 396 return (-1); 397 } 398 /* Mark pin as mapped and give warning if it's already mapped. */ 399 if (sc->sc_pins[pin].mapped) { 400 device_printf(bus, "warning: pin %d is already mapped\n", pin); 401 return (-1); 402 } 403 sc->sc_pins[pin].mapped = 1; 404 405 return (0); 406 } 407 408 /* Release mapped pin */ 409 int 410 gpiobus_release_pin(device_t bus, uint32_t pin) 411 { 412 struct gpiobus_softc *sc; 413 414 sc = device_get_softc(bus); 415 /* Consistency check. */ 416 if (pin >= sc->sc_npins) { 417 device_printf(bus, 418 "invalid pin %d, max=%d\n", 419 pin, sc->sc_npins - 1); 420 return (-1); 421 } 422 423 if (!sc->sc_pins[pin].mapped) { 424 device_printf(bus, "pin %d is not mapped\n", pin); 425 return (-1); 426 } 427 sc->sc_pins[pin].mapped = 0; 428 429 return (0); 430 } 431 432 static int 433 gpiobus_acquire_child_pins(device_t dev, device_t child) 434 { 435 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 436 int i; 437 438 for (i = 0; i < devi->npins; i++) { 439 /* Reserve the GPIO pin. */ 440 if (gpiobus_acquire_pin(dev, devi->pins[i]) != 0) { 441 device_printf(child, "cannot acquire pin %d\n", 442 devi->pins[i]); 443 while (--i >= 0) { 444 (void)gpiobus_release_pin(dev, 445 devi->pins[i]); 446 } 447 gpiobus_free_ivars(devi); 448 return (EBUSY); 449 } 450 } 451 for (i = 0; i < devi->npins; i++) { 452 /* Use the child name as pin name. */ 453 GPIOBUS_PIN_SETNAME(dev, devi->pins[i], 454 device_get_nameunit(child)); 455 456 } 457 return (0); 458 } 459 460 static int 461 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) 462 { 463 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 464 int i, npins; 465 466 npins = 0; 467 for (i = 0; i < 32; i++) { 468 if (mask & (1 << i)) 469 npins++; 470 } 471 if (npins == 0) { 472 device_printf(child, "empty pin mask\n"); 473 return (EINVAL); 474 } 475 devi->npins = npins; 476 if (gpiobus_alloc_ivars(devi) != 0) { 477 device_printf(child, "cannot allocate device ivars\n"); 478 return (EINVAL); 479 } 480 npins = 0; 481 for (i = 0; i < 32; i++) { 482 if ((mask & (1 << i)) == 0) 483 continue; 484 devi->pins[npins++] = i; 485 } 486 487 return (0); 488 } 489 490 static int 491 gpiobus_parse_pin_list(struct gpiobus_softc *sc, device_t child, 492 const char *pins) 493 { 494 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 495 const char *p; 496 char *endp; 497 unsigned long pin; 498 int i, npins; 499 500 npins = 0; 501 p = pins; 502 for (;;) { 503 pin = strtoul(p, &endp, 0); 504 if (endp == p) 505 break; 506 npins++; 507 if (*endp == '\0') 508 break; 509 p = endp + 1; 510 } 511 512 if (*endp != '\0') { 513 device_printf(child, "garbage in the pin list: %s\n", endp); 514 return (EINVAL); 515 } 516 if (npins == 0) { 517 device_printf(child, "empty pin list\n"); 518 return (EINVAL); 519 } 520 521 devi->npins = npins; 522 if (gpiobus_alloc_ivars(devi) != 0) { 523 device_printf(child, "cannot allocate device ivars\n"); 524 return (EINVAL); 525 } 526 527 i = 0; 528 p = pins; 529 for (;;) { 530 pin = strtoul(p, &endp, 0); 531 532 devi->pins[i] = pin; 533 534 if (*endp == '\0') 535 break; 536 i++; 537 p = endp + 1; 538 } 539 540 return (0); 541 } 542 543 static int 544 gpiobus_probe(device_t dev) 545 { 546 device_set_desc(dev, "GPIO bus"); 547 548 return (BUS_PROBE_GENERIC); 549 } 550 551 int 552 gpiobus_attach(device_t dev) 553 { 554 int err; 555 556 err = gpiobus_init_softc(dev); 557 if (err != 0) 558 return (err); 559 560 /* 561 * Get parent's pins and mark them as unmapped 562 */ 563 bus_generic_probe(dev); 564 bus_enumerate_hinted_children(dev); 565 566 return (bus_generic_attach(dev)); 567 } 568 569 /* 570 * Since this is not a self-enumerating bus, and since we always add 571 * children in attach, we have to always delete children here. 572 */ 573 int 574 gpiobus_detach(device_t dev) 575 { 576 struct gpiobus_softc *sc; 577 int i, err; 578 579 sc = GPIOBUS_SOFTC(dev); 580 KASSERT(mtx_initialized(&sc->sc_mtx), 581 ("gpiobus mutex not initialized")); 582 GPIOBUS_LOCK_DESTROY(sc); 583 584 if ((err = bus_generic_detach(dev)) != 0) 585 return (err); 586 if ((err = device_delete_children(dev)) != 0) 587 return (err); 588 589 rman_fini(&sc->sc_intr_rman); 590 if (sc->sc_pins) { 591 for (i = 0; i < sc->sc_npins; i++) { 592 if (sc->sc_pins[i].name != NULL) 593 free(sc->sc_pins[i].name, M_DEVBUF); 594 sc->sc_pins[i].name = NULL; 595 } 596 free(sc->sc_pins, M_DEVBUF); 597 sc->sc_pins = NULL; 598 } 599 600 return (0); 601 } 602 603 static int 604 gpiobus_suspend(device_t dev) 605 { 606 607 return (bus_generic_suspend(dev)); 608 } 609 610 static int 611 gpiobus_resume(device_t dev) 612 { 613 614 return (bus_generic_resume(dev)); 615 } 616 617 static void 618 gpiobus_probe_nomatch(device_t dev, device_t child) 619 { 620 char pins[128]; 621 struct sbuf sb; 622 struct gpiobus_ivar *devi; 623 624 devi = GPIOBUS_IVAR(child); 625 sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN); 626 gpiobus_print_pins(devi, &sb); 627 sbuf_finish(&sb); 628 device_printf(dev, "<unknown device> at pin%s %s", 629 devi->npins > 1 ? "s" : "", sbuf_data(&sb)); 630 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 631 printf("\n"); 632 } 633 634 static int 635 gpiobus_print_child(device_t dev, device_t child) 636 { 637 char pins[128]; 638 struct sbuf sb; 639 int retval = 0; 640 struct gpiobus_ivar *devi; 641 642 devi = GPIOBUS_IVAR(child); 643 retval += bus_print_child_header(dev, child); 644 if (devi->npins > 0) { 645 if (devi->npins > 1) 646 retval += printf(" at pins "); 647 else 648 retval += printf(" at pin "); 649 sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN); 650 gpiobus_print_pins(devi, &sb); 651 sbuf_finish(&sb); 652 retval += printf("%s", sbuf_data(&sb)); 653 } 654 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 655 retval += bus_print_child_footer(dev, child); 656 657 return (retval); 658 } 659 660 static int 661 gpiobus_child_location(device_t bus, device_t child, struct sbuf *sb) 662 { 663 struct gpiobus_ivar *devi; 664 665 devi = GPIOBUS_IVAR(child); 666 sbuf_printf(sb, "pins="); 667 gpiobus_print_pins(devi, sb); 668 669 return (0); 670 } 671 672 static device_t 673 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 674 { 675 device_t child; 676 struct gpiobus_ivar *devi; 677 678 child = device_add_child_ordered(dev, order, name, unit); 679 if (child == NULL) 680 return (child); 681 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 682 if (devi == NULL) { 683 device_delete_child(dev, child); 684 return (NULL); 685 } 686 resource_list_init(&devi->rl); 687 device_set_ivars(child, devi); 688 689 return (child); 690 } 691 692 static void 693 gpiobus_child_deleted(device_t dev, device_t child) 694 { 695 struct gpiobus_ivar *devi; 696 697 devi = GPIOBUS_IVAR(child); 698 if (devi == NULL) 699 return; 700 gpiobus_free_ivars(devi); 701 resource_list_free(&devi->rl); 702 free(devi, M_DEVBUF); 703 } 704 705 static int 706 gpiobus_rescan(device_t dev) 707 { 708 709 /* 710 * Re-scan is supposed to remove and add children, but if someone has 711 * deleted the hints for a child we attached earlier, we have no easy 712 * way to handle that. So this just attaches new children for whom new 713 * hints or drivers have arrived since we last tried. 714 */ 715 bus_enumerate_hinted_children(dev); 716 bus_generic_attach(dev); 717 return (0); 718 } 719 720 static void 721 gpiobus_hinted_child(device_t bus, const char *dname, int dunit) 722 { 723 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); 724 device_t child; 725 const char *pins; 726 int irq, pinmask; 727 728 if (device_find_child(bus, dname, dunit) != NULL) { 729 return; 730 } 731 732 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 733 if (resource_int_value(dname, dunit, "pins", &pinmask) == 0) { 734 if (gpiobus_parse_pins(sc, child, pinmask)) { 735 device_delete_child(bus, child); 736 return; 737 } 738 } 739 else if (resource_string_value(dname, dunit, "pin_list", &pins) == 0) { 740 if (gpiobus_parse_pin_list(sc, child, pins)) { 741 device_delete_child(bus, child); 742 return; 743 } 744 } 745 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 746 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) 747 device_printf(bus, 748 "warning: bus_set_resource() failed\n"); 749 } 750 } 751 752 int 753 gpiobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 754 { 755 struct gpiobus_ivar *devi; 756 757 devi = GPIOBUS_IVAR(child); 758 switch (which) { 759 case GPIOBUS_IVAR_NPINS: 760 *result = devi->npins; 761 break; 762 case GPIOBUS_IVAR_PINS: 763 /* Children do not ever need to directly examine this. */ 764 return (ENOTSUP); 765 default: 766 return (ENOENT); 767 } 768 769 return (0); 770 } 771 772 static int 773 gpiobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 774 { 775 struct gpiobus_ivar *devi; 776 const uint32_t *ptr; 777 int i; 778 779 devi = GPIOBUS_IVAR(child); 780 switch (which) { 781 case GPIOBUS_IVAR_NPINS: 782 /* GPIO ivars are set once. */ 783 if (devi->npins != 0) { 784 return (EBUSY); 785 } 786 devi->npins = value; 787 if (gpiobus_alloc_ivars(devi) != 0) { 788 device_printf(child, "cannot allocate device ivars\n"); 789 devi->npins = 0; 790 return (ENOMEM); 791 } 792 break; 793 case GPIOBUS_IVAR_PINS: 794 ptr = (const uint32_t *)value; 795 for (i = 0; i < devi->npins; i++) 796 devi->pins[i] = ptr[i]; 797 if (gpiobus_acquire_child_pins(dev, child) != 0) 798 return (EBUSY); 799 break; 800 default: 801 return (ENOENT); 802 } 803 804 return (0); 805 } 806 807 static struct rman * 808 gpiobus_get_rman(device_t bus, int type, u_int flags) 809 { 810 struct gpiobus_softc *sc; 811 812 sc = device_get_softc(bus); 813 switch (type) { 814 case SYS_RES_IRQ: 815 return (&sc->sc_intr_rman); 816 default: 817 return (NULL); 818 } 819 } 820 821 static struct resource * 822 gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, 823 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 824 { 825 struct resource_list *rl; 826 struct resource_list_entry *rle; 827 int isdefault; 828 829 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1); 830 if (isdefault) { 831 rl = BUS_GET_RESOURCE_LIST(bus, child); 832 if (rl == NULL) 833 return (NULL); 834 rle = resource_list_find(rl, type, *rid); 835 if (rle == NULL) 836 return (NULL); 837 start = rle->start; 838 count = rle->count; 839 end = rle->end; 840 } 841 return (bus_generic_rman_alloc_resource(bus, child, type, rid, start, 842 end, count, flags)); 843 } 844 845 static struct resource_list * 846 gpiobus_get_resource_list(device_t bus __unused, device_t child) 847 { 848 struct gpiobus_ivar *ivar; 849 850 ivar = GPIOBUS_IVAR(child); 851 852 return (&ivar->rl); 853 } 854 855 static int 856 gpiobus_acquire_bus(device_t busdev, device_t child, int how) 857 { 858 struct gpiobus_softc *sc; 859 860 sc = device_get_softc(busdev); 861 GPIOBUS_ASSERT_UNLOCKED(sc); 862 GPIOBUS_LOCK(sc); 863 if (sc->sc_owner != NULL) { 864 if (sc->sc_owner == child) 865 panic("%s: %s still owns the bus.", 866 device_get_nameunit(busdev), 867 device_get_nameunit(child)); 868 if (how == GPIOBUS_DONTWAIT) { 869 GPIOBUS_UNLOCK(sc); 870 return (EWOULDBLOCK); 871 } 872 while (sc->sc_owner != NULL) 873 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); 874 } 875 sc->sc_owner = child; 876 GPIOBUS_UNLOCK(sc); 877 878 return (0); 879 } 880 881 static void 882 gpiobus_release_bus(device_t busdev, device_t child) 883 { 884 struct gpiobus_softc *sc; 885 886 sc = device_get_softc(busdev); 887 GPIOBUS_ASSERT_UNLOCKED(sc); 888 GPIOBUS_LOCK(sc); 889 if (sc->sc_owner == NULL) 890 panic("%s: %s releasing unowned bus.", 891 device_get_nameunit(busdev), 892 device_get_nameunit(child)); 893 if (sc->sc_owner != child) 894 panic("%s: %s trying to release bus owned by %s", 895 device_get_nameunit(busdev), 896 device_get_nameunit(child), 897 device_get_nameunit(sc->sc_owner)); 898 sc->sc_owner = NULL; 899 wakeup(sc); 900 GPIOBUS_UNLOCK(sc); 901 } 902 903 static int 904 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, 905 uint32_t flags) 906 { 907 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 908 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 909 uint32_t caps; 910 911 if (pin >= devi->npins) 912 return (EINVAL); 913 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0) 914 return (EINVAL); 915 if (gpio_check_flags(caps, flags) != 0) 916 return (EINVAL); 917 918 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags)); 919 } 920 921 static int 922 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, 923 uint32_t *flags) 924 { 925 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 926 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 927 928 if (pin >= devi->npins) 929 return (EINVAL); 930 931 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); 932 } 933 934 static int 935 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, 936 uint32_t *caps) 937 { 938 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 939 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 940 941 if (pin >= devi->npins) 942 return (EINVAL); 943 944 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); 945 } 946 947 static int 948 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, 949 unsigned int value) 950 { 951 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 952 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 953 954 if (pin >= devi->npins) 955 return (EINVAL); 956 957 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); 958 } 959 960 static int 961 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, 962 unsigned int *value) 963 { 964 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 965 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 966 967 if (pin >= devi->npins) 968 return (EINVAL); 969 970 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); 971 } 972 973 static int 974 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) 975 { 976 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 977 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 978 979 if (pin >= devi->npins) 980 return (EINVAL); 981 982 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); 983 } 984 985 static int 986 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name) 987 { 988 struct gpiobus_softc *sc; 989 990 sc = GPIOBUS_SOFTC(dev); 991 if (pin > sc->sc_npins) 992 return (EINVAL); 993 /* Did we have a name for this pin ? */ 994 if (sc->sc_pins[pin].name != NULL) { 995 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME); 996 return (0); 997 } 998 999 /* Return the default pin name. */ 1000 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name)); 1001 } 1002 1003 static int 1004 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name) 1005 { 1006 struct gpiobus_softc *sc; 1007 1008 sc = GPIOBUS_SOFTC(dev); 1009 if (pin > sc->sc_npins) 1010 return (EINVAL); 1011 if (name == NULL) 1012 return (EINVAL); 1013 /* Save the pin name. */ 1014 if (sc->sc_pins[pin].name == NULL) 1015 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF, 1016 M_WAITOK | M_ZERO); 1017 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME); 1018 1019 return (0); 1020 } 1021 1022 static device_method_t gpiobus_methods[] = { 1023 /* Device interface */ 1024 DEVMETHOD(device_probe, gpiobus_probe), 1025 DEVMETHOD(device_attach, gpiobus_attach), 1026 DEVMETHOD(device_detach, gpiobus_detach), 1027 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1028 DEVMETHOD(device_suspend, gpiobus_suspend), 1029 DEVMETHOD(device_resume, gpiobus_resume), 1030 1031 /* Bus interface */ 1032 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 1033 DEVMETHOD(bus_config_intr, bus_generic_config_intr), 1034 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1035 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 1036 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 1037 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 1038 DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource), 1039 DEVMETHOD(bus_release_resource, bus_generic_rman_release_resource), 1040 DEVMETHOD(bus_activate_resource, bus_generic_rman_activate_resource), 1041 DEVMETHOD(bus_deactivate_resource, bus_generic_rman_deactivate_resource), 1042 DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list), 1043 DEVMETHOD(bus_get_rman, gpiobus_get_rman), 1044 DEVMETHOD(bus_add_child, gpiobus_add_child), 1045 DEVMETHOD(bus_child_deleted, gpiobus_child_deleted), 1046 DEVMETHOD(bus_rescan, gpiobus_rescan), 1047 DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch), 1048 DEVMETHOD(bus_print_child, gpiobus_print_child), 1049 DEVMETHOD(bus_child_location, gpiobus_child_location), 1050 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), 1051 DEVMETHOD(bus_read_ivar, gpiobus_read_ivar), 1052 DEVMETHOD(bus_write_ivar, gpiobus_write_ivar), 1053 1054 /* GPIO protocol */ 1055 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), 1056 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), 1057 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), 1058 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), 1059 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), 1060 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), 1061 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), 1062 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), 1063 DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname), 1064 DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname), 1065 1066 DEVMETHOD_END 1067 }; 1068 1069 driver_t gpiobus_driver = { 1070 "gpiobus", 1071 gpiobus_methods, 1072 sizeof(struct gpiobus_softc) 1073 }; 1074 1075 EARLY_DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, 0, 0, 1076 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 1077 MODULE_VERSION(gpiobus, 1); 1078