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 159 #ifdef FDT 160 ofw_gpiobus_unregister_provider(dev); 161 #endif 162 163 return (bus_generic_detach(dev)); 164 } 165 166 int 167 gpiobus_init_softc(device_t dev) 168 { 169 struct gpiobus_softc *sc; 170 171 sc = GPIOBUS_SOFTC(dev); 172 sc->sc_busdev = dev; 173 sc->sc_dev = device_get_parent(dev); 174 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 175 sc->sc_intr_rman.rm_descr = "GPIO Interrupts"; 176 if (rman_init(&sc->sc_intr_rman) != 0 || 177 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0) 178 panic("%s: failed to set up rman.", __func__); 179 180 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) 181 return (ENXIO); 182 183 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins")); 184 185 /* Pins = GPIO_PIN_MAX() + 1 */ 186 sc->sc_npins++; 187 188 sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF, 189 M_NOWAIT | M_ZERO); 190 if (sc->sc_pins == NULL) 191 return (ENOMEM); 192 193 /* Initialize the bus lock. */ 194 GPIOBUS_LOCK_INIT(sc); 195 196 return (0); 197 } 198 199 int 200 gpiobus_alloc_ivars(struct gpiobus_ivar *devi) 201 { 202 203 /* Allocate pins and flags memory. */ 204 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, 205 M_NOWAIT | M_ZERO); 206 if (devi->pins == NULL) 207 return (ENOMEM); 208 devi->flags = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, 209 M_NOWAIT | M_ZERO); 210 if (devi->flags == NULL) { 211 free(devi->pins, M_DEVBUF); 212 return (ENOMEM); 213 } 214 215 return (0); 216 } 217 218 void 219 gpiobus_free_ivars(struct gpiobus_ivar *devi) 220 { 221 222 if (devi->flags) { 223 free(devi->flags, M_DEVBUF); 224 devi->flags = NULL; 225 } 226 if (devi->pins) { 227 free(devi->pins, M_DEVBUF); 228 devi->pins = NULL; 229 } 230 } 231 232 int 233 gpiobus_map_pin(device_t bus, uint32_t pin) 234 { 235 struct gpiobus_softc *sc; 236 237 sc = device_get_softc(bus); 238 /* Consistency check. */ 239 if (pin >= sc->sc_npins) { 240 device_printf(bus, 241 "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1); 242 return (-1); 243 } 244 /* Mark pin as mapped and give warning if it's already mapped. */ 245 if (sc->sc_pins[pin].mapped) { 246 device_printf(bus, "warning: pin %d is already mapped\n", pin); 247 return (-1); 248 } 249 sc->sc_pins[pin].mapped = 1; 250 251 return (0); 252 } 253 254 static int 255 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) 256 { 257 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 258 int i, npins; 259 260 npins = 0; 261 for (i = 0; i < 32; i++) { 262 if (mask & (1 << i)) 263 npins++; 264 } 265 if (npins == 0) { 266 device_printf(child, "empty pin mask\n"); 267 return (EINVAL); 268 } 269 devi->npins = npins; 270 if (gpiobus_alloc_ivars(devi) != 0) { 271 device_printf(child, "cannot allocate device ivars\n"); 272 return (EINVAL); 273 } 274 npins = 0; 275 for (i = 0; i < 32; i++) { 276 if ((mask & (1 << i)) == 0) 277 continue; 278 /* Reserve the GPIO pin. */ 279 if (gpiobus_map_pin(sc->sc_busdev, i) != 0) { 280 gpiobus_free_ivars(devi); 281 return (EINVAL); 282 } 283 devi->pins[npins++] = i; 284 /* Use the child name as pin name. */ 285 GPIOBUS_PIN_SETNAME(sc->sc_busdev, i, 286 device_get_nameunit(child)); 287 } 288 289 return (0); 290 } 291 292 static int 293 gpiobus_probe(device_t dev) 294 { 295 device_set_desc(dev, "GPIO bus"); 296 297 return (BUS_PROBE_GENERIC); 298 } 299 300 static int 301 gpiobus_attach(device_t dev) 302 { 303 int err; 304 305 err = gpiobus_init_softc(dev); 306 if (err != 0) 307 return (err); 308 309 /* 310 * Get parent's pins and mark them as unmapped 311 */ 312 bus_generic_probe(dev); 313 bus_enumerate_hinted_children(dev); 314 315 return (bus_generic_attach(dev)); 316 } 317 318 /* 319 * Since this is not a self-enumerating bus, and since we always add 320 * children in attach, we have to always delete children here. 321 */ 322 static int 323 gpiobus_detach(device_t dev) 324 { 325 struct gpiobus_softc *sc; 326 struct gpiobus_ivar *devi; 327 device_t *devlist; 328 int i, err, ndevs; 329 330 sc = GPIOBUS_SOFTC(dev); 331 KASSERT(mtx_initialized(&sc->sc_mtx), 332 ("gpiobus mutex not initialized")); 333 GPIOBUS_LOCK_DESTROY(sc); 334 335 if ((err = bus_generic_detach(dev)) != 0) 336 return (err); 337 338 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) 339 return (err); 340 for (i = 0; i < ndevs; i++) { 341 device_delete_child(dev, devlist[i]); 342 devi = GPIOBUS_IVAR(devlist[i]); 343 gpiobus_free_ivars(devi); 344 } 345 free(devlist, M_TEMP); 346 if (sc->sc_pins) { 347 for (i = 0; i < sc->sc_npins; i++) { 348 if (sc->sc_pins[i].name != NULL) 349 free(sc->sc_pins[i].name, M_DEVBUF); 350 sc->sc_pins[i].name = NULL; 351 } 352 free(sc->sc_pins, M_DEVBUF); 353 sc->sc_pins = NULL; 354 } 355 356 return (0); 357 } 358 359 static int 360 gpiobus_suspend(device_t dev) 361 { 362 363 return (bus_generic_suspend(dev)); 364 } 365 366 static int 367 gpiobus_resume(device_t dev) 368 { 369 370 return (bus_generic_resume(dev)); 371 } 372 373 static void 374 gpiobus_probe_nomatch(device_t dev, device_t child) 375 { 376 char pins[128]; 377 struct gpiobus_ivar *devi; 378 379 devi = GPIOBUS_IVAR(child); 380 memset(pins, 0, sizeof(pins)); 381 gpiobus_print_pins(devi, pins, sizeof(pins)); 382 device_printf(dev, "<unknown device> at pin(s) %s", pins); 383 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%ld"); 384 printf("\n"); 385 } 386 387 static int 388 gpiobus_print_child(device_t dev, device_t child) 389 { 390 char pins[128]; 391 int retval = 0; 392 struct gpiobus_ivar *devi; 393 394 devi = GPIOBUS_IVAR(child); 395 memset(pins, 0, sizeof(pins)); 396 retval += bus_print_child_header(dev, child); 397 if (devi->npins > 0) { 398 if (devi->npins > 1) 399 retval += printf(" at pins "); 400 else 401 retval += printf(" at pin "); 402 gpiobus_print_pins(devi, pins, sizeof(pins)); 403 retval += printf("%s", pins); 404 } 405 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%ld"); 406 retval += bus_print_child_footer(dev, child); 407 408 return (retval); 409 } 410 411 static int 412 gpiobus_child_location_str(device_t bus, device_t child, char *buf, 413 size_t buflen) 414 { 415 struct gpiobus_ivar *devi; 416 417 devi = GPIOBUS_IVAR(child); 418 strlcpy(buf, "pin(s)=", buflen); 419 gpiobus_print_pins(devi, buf, buflen); 420 421 return (0); 422 } 423 424 static int 425 gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 426 size_t buflen) 427 { 428 429 *buf = '\0'; 430 return (0); 431 } 432 433 static device_t 434 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 435 { 436 device_t child; 437 struct gpiobus_ivar *devi; 438 439 child = device_add_child_ordered(dev, order, name, unit); 440 if (child == NULL) 441 return (child); 442 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 443 if (devi == NULL) { 444 device_delete_child(dev, child); 445 return (0); 446 } 447 resource_list_init(&devi->rl); 448 device_set_ivars(child, devi); 449 450 return (child); 451 } 452 453 static void 454 gpiobus_hinted_child(device_t bus, const char *dname, int dunit) 455 { 456 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); 457 struct gpiobus_ivar *devi; 458 device_t child; 459 int irq, pins; 460 461 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 462 devi = GPIOBUS_IVAR(child); 463 resource_int_value(dname, dunit, "pins", &pins); 464 if (gpiobus_parse_pins(sc, child, pins)) 465 device_delete_child(bus, child); 466 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 467 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) 468 device_printf(bus, 469 "warning: bus_set_resource() failed\n"); 470 } 471 } 472 473 static int 474 gpiobus_set_resource(device_t dev, device_t child, int type, int rid, 475 u_long start, u_long count) 476 { 477 struct gpiobus_ivar *devi; 478 struct resource_list_entry *rle; 479 480 dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n", 481 __func__, dev, child, type, rid, (void *)(intptr_t)start, count); 482 devi = GPIOBUS_IVAR(child); 483 rle = resource_list_add(&devi->rl, type, rid, start, 484 start + count - 1, count); 485 if (rle == NULL) 486 return (ENXIO); 487 488 return (0); 489 } 490 491 static struct resource * 492 gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, 493 u_long start, u_long end, u_long count, u_int flags) 494 { 495 struct gpiobus_softc *sc; 496 struct resource *rv; 497 struct resource_list *rl; 498 struct resource_list_entry *rle; 499 int isdefault; 500 501 if (type != SYS_RES_IRQ) 502 return (NULL); 503 isdefault = (start == 0UL && end == ~0UL && count == 1); 504 rle = NULL; 505 if (isdefault) { 506 rl = BUS_GET_RESOURCE_LIST(bus, child); 507 if (rl == NULL) 508 return (NULL); 509 rle = resource_list_find(rl, type, *rid); 510 if (rle == NULL) 511 return (NULL); 512 if (rle->res != NULL) 513 panic("%s: resource entry is busy", __func__); 514 start = rle->start; 515 count = rle->count; 516 end = rle->end; 517 } 518 sc = device_get_softc(bus); 519 rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags, 520 child); 521 if (rv == NULL) 522 return (NULL); 523 rman_set_rid(rv, *rid); 524 if ((flags & RF_ACTIVE) != 0 && 525 bus_activate_resource(child, type, *rid, rv) != 0) { 526 rman_release_resource(rv); 527 return (NULL); 528 } 529 530 return (rv); 531 } 532 533 static int 534 gpiobus_release_resource(device_t bus __unused, device_t child, int type, 535 int rid, struct resource *r) 536 { 537 int error; 538 539 if (rman_get_flags(r) & RF_ACTIVE) { 540 error = bus_deactivate_resource(child, type, rid, r); 541 if (error) 542 return (error); 543 } 544 545 return (rman_release_resource(r)); 546 } 547 548 static struct resource_list * 549 gpiobus_get_resource_list(device_t bus __unused, device_t child) 550 { 551 struct gpiobus_ivar *ivar; 552 553 ivar = GPIOBUS_IVAR(child); 554 555 return (&ivar->rl); 556 } 557 558 static int 559 gpiobus_acquire_bus(device_t busdev, device_t child, int how) 560 { 561 struct gpiobus_softc *sc; 562 563 sc = device_get_softc(busdev); 564 GPIOBUS_ASSERT_UNLOCKED(sc); 565 GPIOBUS_LOCK(sc); 566 if (sc->sc_owner != NULL) { 567 if (sc->sc_owner == child) 568 panic("%s: %s still owns the bus.", 569 device_get_nameunit(busdev), 570 device_get_nameunit(child)); 571 if (how == GPIOBUS_DONTWAIT) { 572 GPIOBUS_UNLOCK(sc); 573 return (EWOULDBLOCK); 574 } 575 while (sc->sc_owner != NULL) 576 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); 577 } 578 sc->sc_owner = child; 579 GPIOBUS_UNLOCK(sc); 580 581 return (0); 582 } 583 584 static void 585 gpiobus_release_bus(device_t busdev, device_t child) 586 { 587 struct gpiobus_softc *sc; 588 589 sc = device_get_softc(busdev); 590 GPIOBUS_ASSERT_UNLOCKED(sc); 591 GPIOBUS_LOCK(sc); 592 if (sc->sc_owner == NULL) 593 panic("%s: %s releasing unowned bus.", 594 device_get_nameunit(busdev), 595 device_get_nameunit(child)); 596 if (sc->sc_owner != child) 597 panic("%s: %s trying to release bus owned by %s", 598 device_get_nameunit(busdev), 599 device_get_nameunit(child), 600 device_get_nameunit(sc->sc_owner)); 601 sc->sc_owner = NULL; 602 wakeup(sc); 603 GPIOBUS_UNLOCK(sc); 604 } 605 606 static int 607 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, 608 uint32_t flags) 609 { 610 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 611 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 612 uint32_t caps; 613 614 if (pin >= devi->npins) 615 return (EINVAL); 616 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0) 617 return (EINVAL); 618 if (gpio_check_flags(caps, flags) != 0) 619 return (EINVAL); 620 621 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags)); 622 } 623 624 static int 625 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, 626 uint32_t *flags) 627 { 628 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 629 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 630 631 if (pin >= devi->npins) 632 return (EINVAL); 633 634 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); 635 } 636 637 static int 638 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, 639 uint32_t *caps) 640 { 641 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 642 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 643 644 if (pin >= devi->npins) 645 return (EINVAL); 646 647 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); 648 } 649 650 static int 651 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, 652 unsigned int value) 653 { 654 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 655 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 656 657 if (pin >= devi->npins) 658 return (EINVAL); 659 660 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); 661 } 662 663 static int 664 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, 665 unsigned int *value) 666 { 667 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 668 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 669 670 if (pin >= devi->npins) 671 return (EINVAL); 672 673 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); 674 } 675 676 static int 677 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) 678 { 679 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 680 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 681 682 if (pin >= devi->npins) 683 return (EINVAL); 684 685 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); 686 } 687 688 static int 689 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name) 690 { 691 struct gpiobus_softc *sc; 692 693 sc = GPIOBUS_SOFTC(dev); 694 if (pin > sc->sc_npins) 695 return (EINVAL); 696 /* Did we have a name for this pin ? */ 697 if (sc->sc_pins[pin].name != NULL) { 698 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME); 699 return (0); 700 } 701 702 /* Return the default pin name. */ 703 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name)); 704 } 705 706 static int 707 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name) 708 { 709 struct gpiobus_softc *sc; 710 711 sc = GPIOBUS_SOFTC(dev); 712 if (pin > sc->sc_npins) 713 return (EINVAL); 714 if (name == NULL) 715 return (EINVAL); 716 /* Save the pin name. */ 717 if (sc->sc_pins[pin].name == NULL) 718 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF, 719 M_WAITOK | M_ZERO); 720 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME); 721 722 return (0); 723 } 724 725 static device_method_t gpiobus_methods[] = { 726 /* Device interface */ 727 DEVMETHOD(device_probe, gpiobus_probe), 728 DEVMETHOD(device_attach, gpiobus_attach), 729 DEVMETHOD(device_detach, gpiobus_detach), 730 DEVMETHOD(device_shutdown, bus_generic_shutdown), 731 DEVMETHOD(device_suspend, gpiobus_suspend), 732 DEVMETHOD(device_resume, gpiobus_resume), 733 734 /* Bus interface */ 735 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 736 DEVMETHOD(bus_config_intr, bus_generic_config_intr), 737 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 738 DEVMETHOD(bus_set_resource, gpiobus_set_resource), 739 DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource), 740 DEVMETHOD(bus_release_resource, gpiobus_release_resource), 741 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 742 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 743 DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list), 744 DEVMETHOD(bus_add_child, gpiobus_add_child), 745 DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch), 746 DEVMETHOD(bus_print_child, gpiobus_print_child), 747 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str), 748 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str), 749 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), 750 751 /* GPIO protocol */ 752 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), 753 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), 754 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), 755 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), 756 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), 757 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), 758 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), 759 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), 760 DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname), 761 DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname), 762 763 DEVMETHOD_END 764 }; 765 766 driver_t gpiobus_driver = { 767 "gpiobus", 768 gpiobus_methods, 769 sizeof(struct gpiobus_softc) 770 }; 771 772 devclass_t gpiobus_devclass; 773 774 DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0); 775 MODULE_VERSION(gpiobus, 1); 776