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/intr.h> 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 50 static void gpiobus_print_pins(struct gpiobus_ivar *, char *, size_t); 51 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); 52 static int gpiobus_probe(device_t); 53 static int gpiobus_attach(device_t); 54 static int gpiobus_detach(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_str(device_t, device_t, char *, size_t); 60 static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t); 61 static device_t gpiobus_add_child(device_t, u_int, const char *, int); 62 static void gpiobus_hinted_child(device_t, const char *, int); 63 64 /* 65 * GPIOBUS interface 66 */ 67 static int gpiobus_acquire_bus(device_t, device_t, int); 68 static void gpiobus_release_bus(device_t, device_t); 69 static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t); 70 static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*); 71 static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*); 72 static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int); 73 static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*); 74 static 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 82 struct resource * 83 gpio_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 == INTR_IRQ_INVALID) 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 101 int 102 gpio_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 118 static void 119 gpiobus_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 161 device_t 162 gpiobus_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 181 int 182 gpiobus_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 196 int 197 gpiobus_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 229 int 230 gpiobus_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 248 void 249 gpiobus_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 262 int 263 gpiobus_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 284 static int 285 gpiobus_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 322 static int 323 gpiobus_probe(device_t dev) 324 { 325 device_set_desc(dev, "GPIO bus"); 326 327 return (BUS_PROBE_GENERIC); 328 } 329 330 static int 331 gpiobus_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 */ 352 static int 353 gpiobus_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 392 static int 393 gpiobus_suspend(device_t dev) 394 { 395 396 return (bus_generic_suspend(dev)); 397 } 398 399 static int 400 gpiobus_resume(device_t dev) 401 { 402 403 return (bus_generic_resume(dev)); 404 } 405 406 static void 407 gpiobus_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 423 static int 424 gpiobus_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 447 static int 448 gpiobus_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 463 static int 464 gpiobus_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 472 static device_t 473 gpiobus_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 492 static void 493 gpiobus_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 515 static int 516 gpiobus_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 533 static struct resource * 534 gpiobus_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 575 static int 576 gpiobus_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 590 static struct resource_list * 591 gpiobus_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 600 static int 601 gpiobus_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 626 static void 627 gpiobus_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 648 static int 649 gpiobus_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 666 static int 667 gpiobus_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 679 static int 680 gpiobus_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 692 static int 693 gpiobus_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 705 static int 706 gpiobus_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 718 static int 719 gpiobus_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 730 static int 731 gpiobus_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 748 static int 749 gpiobus_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 767 static 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 808 driver_t gpiobus_driver = { 809 "gpiobus", 810 gpiobus_methods, 811 sizeof(struct gpiobus_softc) 812 }; 813 814 devclass_t gpiobus_devclass; 815 816 DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0); 817 MODULE_VERSION(gpiobus, 1); 818