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 /* Release mapped pin */ 285 int 286 gpiobus_release_pin(device_t bus, uint32_t pin) 287 { 288 struct gpiobus_softc *sc; 289 290 sc = device_get_softc(bus); 291 /* Consistency check. */ 292 if (pin >= sc->sc_npins) { 293 device_printf(bus, 294 "gpiobus_map_pin: invalid pin %d, max=%d\n", 295 pin, sc->sc_npins - 1); 296 return (-1); 297 } 298 299 if (!sc->sc_pins[pin].mapped) { 300 device_printf(bus, "gpiobus_map_pin: pin %d is not mapped\n", pin); 301 return (-1); 302 } 303 sc->sc_pins[pin].mapped = 0; 304 305 return (0); 306 } 307 308 static int 309 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) 310 { 311 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 312 int i, npins; 313 314 npins = 0; 315 for (i = 0; i < 32; i++) { 316 if (mask & (1 << i)) 317 npins++; 318 } 319 if (npins == 0) { 320 device_printf(child, "empty pin mask\n"); 321 return (EINVAL); 322 } 323 devi->npins = npins; 324 if (gpiobus_alloc_ivars(devi) != 0) { 325 device_printf(child, "cannot allocate device ivars\n"); 326 return (EINVAL); 327 } 328 npins = 0; 329 for (i = 0; i < 32; i++) { 330 if ((mask & (1 << i)) == 0) 331 continue; 332 /* Reserve the GPIO pin. */ 333 if (gpiobus_map_pin(sc->sc_busdev, i) != 0) { 334 gpiobus_free_ivars(devi); 335 return (EINVAL); 336 } 337 devi->pins[npins++] = i; 338 /* Use the child name as pin name. */ 339 GPIOBUS_PIN_SETNAME(sc->sc_busdev, i, 340 device_get_nameunit(child)); 341 } 342 343 return (0); 344 } 345 346 static int 347 gpiobus_probe(device_t dev) 348 { 349 device_set_desc(dev, "GPIO bus"); 350 351 return (BUS_PROBE_GENERIC); 352 } 353 354 static int 355 gpiobus_attach(device_t dev) 356 { 357 int err; 358 359 err = gpiobus_init_softc(dev); 360 if (err != 0) 361 return (err); 362 363 /* 364 * Get parent's pins and mark them as unmapped 365 */ 366 bus_generic_probe(dev); 367 bus_enumerate_hinted_children(dev); 368 369 return (bus_generic_attach(dev)); 370 } 371 372 /* 373 * Since this is not a self-enumerating bus, and since we always add 374 * children in attach, we have to always delete children here. 375 */ 376 static int 377 gpiobus_detach(device_t dev) 378 { 379 struct gpiobus_softc *sc; 380 struct gpiobus_ivar *devi; 381 device_t *devlist; 382 int i, err, ndevs; 383 384 sc = GPIOBUS_SOFTC(dev); 385 KASSERT(mtx_initialized(&sc->sc_mtx), 386 ("gpiobus mutex not initialized")); 387 GPIOBUS_LOCK_DESTROY(sc); 388 389 if ((err = bus_generic_detach(dev)) != 0) 390 return (err); 391 392 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) 393 return (err); 394 for (i = 0; i < ndevs; i++) { 395 devi = GPIOBUS_IVAR(devlist[i]); 396 gpiobus_free_ivars(devi); 397 resource_list_free(&devi->rl); 398 free(devi, M_DEVBUF); 399 device_delete_child(dev, devlist[i]); 400 } 401 free(devlist, M_TEMP); 402 rman_fini(&sc->sc_intr_rman); 403 if (sc->sc_pins) { 404 for (i = 0; i < sc->sc_npins; i++) { 405 if (sc->sc_pins[i].name != NULL) 406 free(sc->sc_pins[i].name, M_DEVBUF); 407 sc->sc_pins[i].name = NULL; 408 } 409 free(sc->sc_pins, M_DEVBUF); 410 sc->sc_pins = NULL; 411 } 412 413 return (0); 414 } 415 416 static int 417 gpiobus_suspend(device_t dev) 418 { 419 420 return (bus_generic_suspend(dev)); 421 } 422 423 static int 424 gpiobus_resume(device_t dev) 425 { 426 427 return (bus_generic_resume(dev)); 428 } 429 430 static void 431 gpiobus_probe_nomatch(device_t dev, device_t child) 432 { 433 char pins[128]; 434 struct gpiobus_ivar *devi; 435 436 devi = GPIOBUS_IVAR(child); 437 memset(pins, 0, sizeof(pins)); 438 gpiobus_print_pins(devi, pins, sizeof(pins)); 439 if (devi->npins > 1) 440 device_printf(dev, "<unknown device> at pins %s", pins); 441 else 442 device_printf(dev, "<unknown device> at pin %s", pins); 443 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 444 printf("\n"); 445 } 446 447 static int 448 gpiobus_print_child(device_t dev, device_t child) 449 { 450 char pins[128]; 451 int retval = 0; 452 struct gpiobus_ivar *devi; 453 454 devi = GPIOBUS_IVAR(child); 455 memset(pins, 0, sizeof(pins)); 456 retval += bus_print_child_header(dev, child); 457 if (devi->npins > 0) { 458 if (devi->npins > 1) 459 retval += printf(" at pins "); 460 else 461 retval += printf(" at pin "); 462 gpiobus_print_pins(devi, pins, sizeof(pins)); 463 retval += printf("%s", pins); 464 } 465 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 466 retval += bus_print_child_footer(dev, child); 467 468 return (retval); 469 } 470 471 static int 472 gpiobus_child_location_str(device_t bus, device_t child, char *buf, 473 size_t buflen) 474 { 475 struct gpiobus_ivar *devi; 476 477 devi = GPIOBUS_IVAR(child); 478 if (devi->npins > 1) 479 strlcpy(buf, "pins=", buflen); 480 else 481 strlcpy(buf, "pin=", buflen); 482 gpiobus_print_pins(devi, buf, buflen); 483 484 return (0); 485 } 486 487 static int 488 gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 489 size_t buflen) 490 { 491 492 *buf = '\0'; 493 return (0); 494 } 495 496 static device_t 497 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 498 { 499 device_t child; 500 struct gpiobus_ivar *devi; 501 502 child = device_add_child_ordered(dev, order, name, unit); 503 if (child == NULL) 504 return (child); 505 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 506 if (devi == NULL) { 507 device_delete_child(dev, child); 508 return (NULL); 509 } 510 resource_list_init(&devi->rl); 511 device_set_ivars(child, devi); 512 513 return (child); 514 } 515 516 static void 517 gpiobus_hinted_child(device_t bus, const char *dname, int dunit) 518 { 519 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); 520 struct gpiobus_ivar *devi; 521 device_t child; 522 int irq, pins; 523 524 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 525 devi = GPIOBUS_IVAR(child); 526 resource_int_value(dname, dunit, "pins", &pins); 527 if (gpiobus_parse_pins(sc, child, pins)) { 528 resource_list_free(&devi->rl); 529 free(devi, M_DEVBUF); 530 device_delete_child(bus, child); 531 } 532 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 533 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) 534 device_printf(bus, 535 "warning: bus_set_resource() failed\n"); 536 } 537 } 538 539 static int 540 gpiobus_set_resource(device_t dev, device_t child, int type, int rid, 541 rman_res_t start, rman_res_t count) 542 { 543 struct gpiobus_ivar *devi; 544 struct resource_list_entry *rle; 545 546 dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n", 547 __func__, dev, child, type, rid, (void *)(intptr_t)start, count); 548 devi = GPIOBUS_IVAR(child); 549 rle = resource_list_add(&devi->rl, type, rid, start, 550 start + count - 1, count); 551 if (rle == NULL) 552 return (ENXIO); 553 554 return (0); 555 } 556 557 static struct resource * 558 gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, 559 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 560 { 561 struct gpiobus_softc *sc; 562 struct resource *rv; 563 struct resource_list *rl; 564 struct resource_list_entry *rle; 565 int isdefault; 566 567 if (type != SYS_RES_IRQ) 568 return (NULL); 569 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1); 570 rle = NULL; 571 if (isdefault) { 572 rl = BUS_GET_RESOURCE_LIST(bus, child); 573 if (rl == NULL) 574 return (NULL); 575 rle = resource_list_find(rl, type, *rid); 576 if (rle == NULL) 577 return (NULL); 578 if (rle->res != NULL) 579 panic("%s: resource entry is busy", __func__); 580 start = rle->start; 581 count = rle->count; 582 end = rle->end; 583 } 584 sc = device_get_softc(bus); 585 rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags, 586 child); 587 if (rv == NULL) 588 return (NULL); 589 rman_set_rid(rv, *rid); 590 if ((flags & RF_ACTIVE) != 0 && 591 bus_activate_resource(child, type, *rid, rv) != 0) { 592 rman_release_resource(rv); 593 return (NULL); 594 } 595 596 return (rv); 597 } 598 599 static int 600 gpiobus_release_resource(device_t bus __unused, device_t child, int type, 601 int rid, struct resource *r) 602 { 603 int error; 604 605 if (rman_get_flags(r) & RF_ACTIVE) { 606 error = bus_deactivate_resource(child, type, rid, r); 607 if (error) 608 return (error); 609 } 610 611 return (rman_release_resource(r)); 612 } 613 614 static struct resource_list * 615 gpiobus_get_resource_list(device_t bus __unused, device_t child) 616 { 617 struct gpiobus_ivar *ivar; 618 619 ivar = GPIOBUS_IVAR(child); 620 621 return (&ivar->rl); 622 } 623 624 static int 625 gpiobus_acquire_bus(device_t busdev, device_t child, int how) 626 { 627 struct gpiobus_softc *sc; 628 629 sc = device_get_softc(busdev); 630 GPIOBUS_ASSERT_UNLOCKED(sc); 631 GPIOBUS_LOCK(sc); 632 if (sc->sc_owner != NULL) { 633 if (sc->sc_owner == child) 634 panic("%s: %s still owns the bus.", 635 device_get_nameunit(busdev), 636 device_get_nameunit(child)); 637 if (how == GPIOBUS_DONTWAIT) { 638 GPIOBUS_UNLOCK(sc); 639 return (EWOULDBLOCK); 640 } 641 while (sc->sc_owner != NULL) 642 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); 643 } 644 sc->sc_owner = child; 645 GPIOBUS_UNLOCK(sc); 646 647 return (0); 648 } 649 650 static void 651 gpiobus_release_bus(device_t busdev, device_t child) 652 { 653 struct gpiobus_softc *sc; 654 655 sc = device_get_softc(busdev); 656 GPIOBUS_ASSERT_UNLOCKED(sc); 657 GPIOBUS_LOCK(sc); 658 if (sc->sc_owner == NULL) 659 panic("%s: %s releasing unowned bus.", 660 device_get_nameunit(busdev), 661 device_get_nameunit(child)); 662 if (sc->sc_owner != child) 663 panic("%s: %s trying to release bus owned by %s", 664 device_get_nameunit(busdev), 665 device_get_nameunit(child), 666 device_get_nameunit(sc->sc_owner)); 667 sc->sc_owner = NULL; 668 wakeup(sc); 669 GPIOBUS_UNLOCK(sc); 670 } 671 672 static int 673 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, 674 uint32_t flags) 675 { 676 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 677 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 678 uint32_t caps; 679 680 if (pin >= devi->npins) 681 return (EINVAL); 682 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0) 683 return (EINVAL); 684 if (gpio_check_flags(caps, flags) != 0) 685 return (EINVAL); 686 687 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags)); 688 } 689 690 static int 691 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, 692 uint32_t *flags) 693 { 694 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 695 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 696 697 if (pin >= devi->npins) 698 return (EINVAL); 699 700 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); 701 } 702 703 static int 704 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, 705 uint32_t *caps) 706 { 707 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 708 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 709 710 if (pin >= devi->npins) 711 return (EINVAL); 712 713 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); 714 } 715 716 static int 717 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, 718 unsigned int value) 719 { 720 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 721 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 722 723 if (pin >= devi->npins) 724 return (EINVAL); 725 726 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); 727 } 728 729 static int 730 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, 731 unsigned int *value) 732 { 733 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 734 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 735 736 if (pin >= devi->npins) 737 return (EINVAL); 738 739 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); 740 } 741 742 static int 743 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) 744 { 745 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 746 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 747 748 if (pin >= devi->npins) 749 return (EINVAL); 750 751 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); 752 } 753 754 static int 755 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name) 756 { 757 struct gpiobus_softc *sc; 758 759 sc = GPIOBUS_SOFTC(dev); 760 if (pin > sc->sc_npins) 761 return (EINVAL); 762 /* Did we have a name for this pin ? */ 763 if (sc->sc_pins[pin].name != NULL) { 764 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME); 765 return (0); 766 } 767 768 /* Return the default pin name. */ 769 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name)); 770 } 771 772 static int 773 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name) 774 { 775 struct gpiobus_softc *sc; 776 777 sc = GPIOBUS_SOFTC(dev); 778 if (pin > sc->sc_npins) 779 return (EINVAL); 780 if (name == NULL) 781 return (EINVAL); 782 /* Save the pin name. */ 783 if (sc->sc_pins[pin].name == NULL) 784 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF, 785 M_WAITOK | M_ZERO); 786 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME); 787 788 return (0); 789 } 790 791 static device_method_t gpiobus_methods[] = { 792 /* Device interface */ 793 DEVMETHOD(device_probe, gpiobus_probe), 794 DEVMETHOD(device_attach, gpiobus_attach), 795 DEVMETHOD(device_detach, gpiobus_detach), 796 DEVMETHOD(device_shutdown, bus_generic_shutdown), 797 DEVMETHOD(device_suspend, gpiobus_suspend), 798 DEVMETHOD(device_resume, gpiobus_resume), 799 800 /* Bus interface */ 801 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 802 DEVMETHOD(bus_config_intr, bus_generic_config_intr), 803 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 804 DEVMETHOD(bus_set_resource, gpiobus_set_resource), 805 DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource), 806 DEVMETHOD(bus_release_resource, gpiobus_release_resource), 807 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 808 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 809 DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list), 810 DEVMETHOD(bus_add_child, gpiobus_add_child), 811 DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch), 812 DEVMETHOD(bus_print_child, gpiobus_print_child), 813 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str), 814 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str), 815 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), 816 817 /* GPIO protocol */ 818 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), 819 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), 820 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), 821 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), 822 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), 823 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), 824 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), 825 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), 826 DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname), 827 DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname), 828 829 DEVMETHOD_END 830 }; 831 832 driver_t gpiobus_driver = { 833 "gpiobus", 834 gpiobus_methods, 835 sizeof(struct gpiobus_softc) 836 }; 837 838 devclass_t gpiobus_devclass; 839 840 DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0); 841 MODULE_VERSION(gpiobus, 1); 842