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$"); 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 49 static void gpiobus_print_pins(struct gpiobus_ivar *, char *, size_t); 50 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); 51 static int gpiobus_probe(device_t); 52 static int gpiobus_attach(device_t); 53 static int gpiobus_detach(device_t); 54 static int gpiobus_suspend(device_t); 55 static int gpiobus_resume(device_t); 56 static void gpiobus_probe_nomatch(device_t, device_t); 57 static int gpiobus_print_child(device_t, device_t); 58 static int gpiobus_child_location_str(device_t, device_t, char *, size_t); 59 static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t); 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 int 76 gpio_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 92 static void 93 gpiobus_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 135 device_t 136 gpiobus_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 155 int 156 gpiobus_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 170 int 171 gpiobus_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 203 int 204 gpiobus_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 222 void 223 gpiobus_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 236 int 237 gpiobus_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 258 static int 259 gpiobus_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 296 static int 297 gpiobus_probe(device_t dev) 298 { 299 device_set_desc(dev, "GPIO bus"); 300 301 return (BUS_PROBE_GENERIC); 302 } 303 304 static int 305 gpiobus_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 */ 326 static int 327 gpiobus_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 366 static int 367 gpiobus_suspend(device_t dev) 368 { 369 370 return (bus_generic_suspend(dev)); 371 } 372 373 static int 374 gpiobus_resume(device_t dev) 375 { 376 377 return (bus_generic_resume(dev)); 378 } 379 380 static void 381 gpiobus_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, "%ld"); 394 printf("\n"); 395 } 396 397 static int 398 gpiobus_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 421 static int 422 gpiobus_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 437 static int 438 gpiobus_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 446 static device_t 447 gpiobus_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 466 static void 467 gpiobus_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 489 static int 490 gpiobus_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 507 static struct resource * 508 gpiobus_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 549 static int 550 gpiobus_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 564 static struct resource_list * 565 gpiobus_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 574 static int 575 gpiobus_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 600 static void 601 gpiobus_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 622 static int 623 gpiobus_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 640 static int 641 gpiobus_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 653 static int 654 gpiobus_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 666 static int 667 gpiobus_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 679 static int 680 gpiobus_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 692 static int 693 gpiobus_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 704 static int 705 gpiobus_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 722 static int 723 gpiobus_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 741 static 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 782 driver_t gpiobus_driver = { 783 "gpiobus", 784 gpiobus_methods, 785 sizeof(struct gpiobus_softc) 786 }; 787 788 devclass_t gpiobus_devclass; 789 790 DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0); 791 MODULE_VERSION(gpiobus, 1); 792