1213237Sgonzo/*- 2213237Sgonzo * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org> 3213237Sgonzo * All rights reserved. 4213237Sgonzo * 5213237Sgonzo * Redistribution and use in source and binary forms, with or without 6213237Sgonzo * modification, are permitted provided that the following conditions 7213237Sgonzo * are met: 8213237Sgonzo * 1. Redistributions of source code must retain the above copyright 9213237Sgonzo * notice, this list of conditions and the following disclaimer. 10213237Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 11213237Sgonzo * notice, this list of conditions and the following disclaimer in the 12213237Sgonzo * documentation and/or other materials provided with the distribution. 13213237Sgonzo * 14213277Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15213277Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16213277Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17213277Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18213277Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19213277Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20213277Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21213277Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22213277Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23213277Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24213277Sgonzo * SUCH DAMAGE. 25213237Sgonzo */ 26213237Sgonzo 27213237Sgonzo#include <sys/cdefs.h> 28213237Sgonzo__FBSDID("$FreeBSD: releng/11.0/sys/dev/gpio/gpiobus.c 301539 2016-06-07 05:08:24Z mmel $"); 29213237Sgonzo 30213237Sgonzo#include <sys/param.h> 31265289Sloos#include <sys/systm.h> 32265191Sloos#include <sys/bus.h> 33274670Sloos#include <sys/gpio.h> 34298738Smmel#include <sys/intr.h> 35265191Sloos#include <sys/kernel.h> 36213237Sgonzo#include <sys/malloc.h> 37213237Sgonzo#include <sys/module.h> 38213237Sgonzo 39265191Sloos#include <dev/gpio/gpiobusvar.h> 40213237Sgonzo 41213237Sgonzo#include "gpiobus_if.h" 42213237Sgonzo 43274638Sloos#undef GPIOBUS_DEBUG 44274638Sloos#ifdef GPIOBUS_DEBUG 45274638Sloos#define dprintf printf 46274638Sloos#else 47274638Sloos#define dprintf(x, arg...) 48274638Sloos#endif 49274638Sloos 50274643Sloosstatic void gpiobus_print_pins(struct gpiobus_ivar *, char *, size_t); 51213237Sgonzostatic int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); 52213237Sgonzostatic int gpiobus_probe(device_t); 53213237Sgonzostatic int gpiobus_attach(device_t); 54213237Sgonzostatic int gpiobus_detach(device_t); 55213237Sgonzostatic int gpiobus_suspend(device_t); 56213237Sgonzostatic int gpiobus_resume(device_t); 57279620Sloosstatic void gpiobus_probe_nomatch(device_t, device_t); 58213237Sgonzostatic int gpiobus_print_child(device_t, device_t); 59213237Sgonzostatic int gpiobus_child_location_str(device_t, device_t, char *, size_t); 60213237Sgonzostatic int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t); 61213237Sgonzostatic device_t gpiobus_add_child(device_t, u_int, const char *, int); 62213237Sgonzostatic void gpiobus_hinted_child(device_t, const char *, int); 63213237Sgonzo 64213237Sgonzo/* 65213237Sgonzo * GPIOBUS interface 66213237Sgonzo */ 67273917Sloosstatic int gpiobus_acquire_bus(device_t, device_t, int); 68213237Sgonzostatic void gpiobus_release_bus(device_t, device_t); 69213237Sgonzostatic int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t); 70213237Sgonzostatic int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*); 71213237Sgonzostatic int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*); 72213237Sgonzostatic int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int); 73213237Sgonzostatic int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*); 74213237Sgonzostatic int gpiobus_pin_toggle(device_t, device_t, uint32_t); 75213237Sgonzo 76298738Smmel/* 77298738Smmel * XXX -> Move me to better place - gpio_subr.c? 78298738Smmel * Also, this function must be changed when interrupt configuration 79298738Smmel * data will be moved into struct resource. 80298738Smmel */ 81298738Smmel#ifdef INTRNG 82301539Smmelstatic void 83301539Smmelgpio_destruct_map_data(struct intr_map_data *map_data) 84301539Smmel{ 85301539Smmel 86301539Smmel KASSERT(map_data->type == INTR_MAP_DATA_GPIO, 87301539Smmel ("%s: bad map_data type %d", __func__, map_data->type)); 88301539Smmel 89301539Smmel free(map_data, M_DEVBUF); 90301539Smmel} 91301539Smmel 92298738Smmelstruct resource * 93298738Smmelgpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, 94298738Smmel gpio_pin_t pin, uint32_t intr_mode) 95298738Smmel{ 96301539Smmel int rv; 97301539Smmel u_int irq; 98301539Smmel struct intr_map_data_gpio *gpio_data; 99301539Smmel struct resource *res; 100298738Smmel 101301539Smmel gpio_data = malloc(sizeof(*gpio_data), M_DEVBUF, M_WAITOK | M_ZERO); 102301539Smmel gpio_data->hdr.type = INTR_MAP_DATA_GPIO; 103301539Smmel gpio_data->hdr.destruct = gpio_destruct_map_data; 104301539Smmel gpio_data->gpio_pin_num = pin->pin; 105301539Smmel gpio_data->gpio_pin_flags = pin->flags; 106301539Smmel gpio_data->gpio_intr_mode = intr_mode; 107301539Smmel 108301539Smmel rv = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data, 109301539Smmel &irq); 110301539Smmel if (rv != 0) { 111301539Smmel gpio_destruct_map_data((struct intr_map_data *)gpio_data); 112298738Smmel return (NULL); 113301539Smmel } 114298738Smmel 115301539Smmel res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1, 116301539Smmel alloc_flags); 117301539Smmel if (res == NULL) { 118301539Smmel gpio_destruct_map_data((struct intr_map_data *)gpio_data); 119301539Smmel return (NULL); 120301539Smmel } 121301539Smmel rman_set_virtual(res, gpio_data); 122301539Smmel return (res); 123298738Smmel} 124300871Sian#else 125300871Sianstruct resource * 126300871Siangpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, 127300871Sian gpio_pin_t pin, uint32_t intr_mode) 128300871Sian{ 129300871Sian 130300871Sian return (NULL); 131300871Sian} 132298738Smmel#endif 133298738Smmel 134274670Sloosint 135274670Sloosgpio_check_flags(uint32_t caps, uint32_t flags) 136274670Sloos{ 137274670Sloos 138274670Sloos /* Check for unwanted flags. */ 139274670Sloos if ((flags & caps) == 0 || (flags & caps) != flags) 140274670Sloos return (EINVAL); 141274670Sloos /* Cannot mix input/output together. */ 142274670Sloos if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT) 143274670Sloos return (EINVAL); 144274670Sloos /* Cannot mix pull-up/pull-down together. */ 145274670Sloos if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN) 146274670Sloos return (EINVAL); 147274670Sloos 148274670Sloos return (0); 149274670Sloos} 150274670Sloos 151274643Sloosstatic void 152274643Sloosgpiobus_print_pins(struct gpiobus_ivar *devi, char *buf, size_t buflen) 153213237Sgonzo{ 154274643Sloos char tmp[128]; 155274643Sloos int i, range_start, range_stop, need_coma; 156213237Sgonzo 157213237Sgonzo if (devi->npins == 0) 158213237Sgonzo return; 159213237Sgonzo 160213237Sgonzo need_coma = 0; 161213237Sgonzo range_start = range_stop = devi->pins[0]; 162213237Sgonzo for (i = 1; i < devi->npins; i++) { 163213237Sgonzo if (devi->pins[i] != (range_stop + 1)) { 164213237Sgonzo if (need_coma) 165274643Sloos strlcat(buf, ",", buflen); 166274643Sloos memset(tmp, 0, sizeof(tmp)); 167213237Sgonzo if (range_start != range_stop) 168274643Sloos snprintf(tmp, sizeof(tmp) - 1, "%d-%d", 169274643Sloos range_start, range_stop); 170213237Sgonzo else 171274643Sloos snprintf(tmp, sizeof(tmp) - 1, "%d", 172274643Sloos range_start); 173274643Sloos strlcat(buf, tmp, buflen); 174213237Sgonzo 175213237Sgonzo range_start = range_stop = devi->pins[i]; 176213237Sgonzo need_coma = 1; 177213237Sgonzo } 178213237Sgonzo else 179213237Sgonzo range_stop++; 180213237Sgonzo } 181213237Sgonzo 182213237Sgonzo if (need_coma) 183274643Sloos strlcat(buf, ",", buflen); 184274643Sloos memset(tmp, 0, sizeof(tmp)); 185213237Sgonzo if (range_start != range_stop) 186274643Sloos snprintf(tmp, sizeof(tmp) - 1, "%d-%d", 187274643Sloos range_start, range_stop); 188213237Sgonzo else 189274643Sloos snprintf(tmp, sizeof(tmp) - 1, "%d", 190274643Sloos range_start); 191274643Sloos strlcat(buf, tmp, buflen); 192213237Sgonzo} 193213237Sgonzo 194277996Sloosdevice_t 195277996Sloosgpiobus_attach_bus(device_t dev) 196277996Sloos{ 197277996Sloos device_t busdev; 198277996Sloos 199277996Sloos busdev = device_add_child(dev, "gpiobus", -1); 200277996Sloos if (busdev == NULL) 201277996Sloos return (NULL); 202277996Sloos if (device_add_child(dev, "gpioc", -1) == NULL) { 203277996Sloos device_delete_child(dev, busdev); 204277996Sloos return (NULL); 205277996Sloos } 206278108Sloos#ifdef FDT 207278108Sloos ofw_gpiobus_register_provider(dev); 208278108Sloos#endif 209277996Sloos bus_generic_attach(dev); 210277996Sloos 211277996Sloos return (busdev); 212277996Sloos} 213277996Sloos 214273569Sloosint 215277996Sloosgpiobus_detach_bus(device_t dev) 216277996Sloos{ 217286845Sloos int err; 218277996Sloos 219278108Sloos#ifdef FDT 220278108Sloos ofw_gpiobus_unregister_provider(dev); 221278108Sloos#endif 222286845Sloos err = bus_generic_detach(dev); 223286845Sloos if (err != 0) 224286845Sloos return (err); 225278108Sloos 226286845Sloos return (device_delete_children(dev)); 227277996Sloos} 228277996Sloos 229277996Sloosint 230273569Sloosgpiobus_init_softc(device_t dev) 231273569Sloos{ 232273569Sloos struct gpiobus_softc *sc; 233273569Sloos 234273569Sloos sc = GPIOBUS_SOFTC(dev); 235273569Sloos sc->sc_busdev = dev; 236273569Sloos sc->sc_dev = device_get_parent(dev); 237274638Sloos sc->sc_intr_rman.rm_type = RMAN_ARRAY; 238274638Sloos sc->sc_intr_rman.rm_descr = "GPIO Interrupts"; 239274638Sloos if (rman_init(&sc->sc_intr_rman) != 0 || 240274638Sloos rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0) 241274638Sloos panic("%s: failed to set up rman.", __func__); 242273569Sloos 243273569Sloos if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) 244273569Sloos return (ENXIO); 245273569Sloos 246293872Sadrian KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins")); 247273569Sloos 248273569Sloos /* Pins = GPIO_PIN_MAX() + 1 */ 249273569Sloos sc->sc_npins++; 250273569Sloos 251279761Sloos sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF, 252273569Sloos M_NOWAIT | M_ZERO); 253279761Sloos if (sc->sc_pins == NULL) 254273569Sloos return (ENOMEM); 255273569Sloos 256273569Sloos /* Initialize the bus lock. */ 257273569Sloos GPIOBUS_LOCK_INIT(sc); 258273569Sloos 259273569Sloos return (0); 260273569Sloos} 261273569Sloos 262279402Sloosint 263279402Sloosgpiobus_alloc_ivars(struct gpiobus_ivar *devi) 264279402Sloos{ 265279402Sloos 266279402Sloos /* Allocate pins and flags memory. */ 267279402Sloos devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, 268279402Sloos M_NOWAIT | M_ZERO); 269279402Sloos if (devi->pins == NULL) 270279402Sloos return (ENOMEM); 271279402Sloos devi->flags = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, 272279402Sloos M_NOWAIT | M_ZERO); 273279402Sloos if (devi->flags == NULL) { 274279402Sloos free(devi->pins, M_DEVBUF); 275279402Sloos return (ENOMEM); 276279402Sloos } 277279402Sloos 278279402Sloos return (0); 279279402Sloos} 280279402Sloos 281279402Sloosvoid 282279402Sloosgpiobus_free_ivars(struct gpiobus_ivar *devi) 283279402Sloos{ 284279402Sloos 285279402Sloos if (devi->flags) { 286279402Sloos free(devi->flags, M_DEVBUF); 287279402Sloos devi->flags = NULL; 288279402Sloos } 289279402Sloos if (devi->pins) { 290279402Sloos free(devi->pins, M_DEVBUF); 291279402Sloos devi->pins = NULL; 292279402Sloos } 293279402Sloos} 294279402Sloos 295279553Sloosint 296300750Siangpiobus_acquire_pin(device_t bus, uint32_t pin) 297279553Sloos{ 298279553Sloos struct gpiobus_softc *sc; 299279553Sloos 300279553Sloos sc = device_get_softc(bus); 301279553Sloos /* Consistency check. */ 302279553Sloos if (pin >= sc->sc_npins) { 303279622Sloos device_printf(bus, 304279553Sloos "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1); 305279553Sloos return (-1); 306279553Sloos } 307279553Sloos /* Mark pin as mapped and give warning if it's already mapped. */ 308279761Sloos if (sc->sc_pins[pin].mapped) { 309279622Sloos device_printf(bus, "warning: pin %d is already mapped\n", pin); 310279553Sloos return (-1); 311279553Sloos } 312279761Sloos sc->sc_pins[pin].mapped = 1; 313279553Sloos 314279553Sloos return (0); 315279553Sloos} 316279553Sloos 317299563Sgonzo/* Release mapped pin */ 318299563Sgonzoint 319299563Sgonzogpiobus_release_pin(device_t bus, uint32_t pin) 320299563Sgonzo{ 321299563Sgonzo struct gpiobus_softc *sc; 322299563Sgonzo 323299563Sgonzo sc = device_get_softc(bus); 324299563Sgonzo /* Consistency check. */ 325299563Sgonzo if (pin >= sc->sc_npins) { 326299563Sgonzo device_printf(bus, 327300750Sian "gpiobus_acquire_pin: invalid pin %d, max=%d\n", 328299563Sgonzo pin, sc->sc_npins - 1); 329299563Sgonzo return (-1); 330299563Sgonzo } 331299563Sgonzo 332299563Sgonzo if (!sc->sc_pins[pin].mapped) { 333300750Sian device_printf(bus, "gpiobus_acquire_pin: pin %d is not mapped\n", pin); 334299563Sgonzo return (-1); 335299563Sgonzo } 336299563Sgonzo sc->sc_pins[pin].mapped = 0; 337299563Sgonzo 338299563Sgonzo return (0); 339299563Sgonzo} 340299563Sgonzo 341213237Sgonzostatic int 342213237Sgonzogpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) 343213237Sgonzo{ 344213237Sgonzo struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 345213237Sgonzo int i, npins; 346213237Sgonzo 347213237Sgonzo npins = 0; 348213237Sgonzo for (i = 0; i < 32; i++) { 349213237Sgonzo if (mask & (1 << i)) 350213237Sgonzo npins++; 351213237Sgonzo } 352213237Sgonzo if (npins == 0) { 353255254Ssbruno device_printf(child, "empty pin mask\n"); 354213237Sgonzo return (EINVAL); 355213237Sgonzo } 356213237Sgonzo devi->npins = npins; 357279402Sloos if (gpiobus_alloc_ivars(devi) != 0) { 358279402Sloos device_printf(child, "cannot allocate device ivars\n"); 359279402Sloos return (EINVAL); 360279402Sloos } 361213237Sgonzo npins = 0; 362213237Sgonzo for (i = 0; i < 32; i++) { 363213237Sgonzo if ((mask & (1 << i)) == 0) 364213237Sgonzo continue; 365279553Sloos /* Reserve the GPIO pin. */ 366300750Sian if (gpiobus_acquire_pin(sc->sc_busdev, i) != 0) { 367279402Sloos gpiobus_free_ivars(devi); 368213237Sgonzo return (EINVAL); 369213237Sgonzo } 370213237Sgonzo devi->pins[npins++] = i; 371279761Sloos /* Use the child name as pin name. */ 372279761Sloos GPIOBUS_PIN_SETNAME(sc->sc_busdev, i, 373279761Sloos device_get_nameunit(child)); 374213237Sgonzo } 375213237Sgonzo 376213237Sgonzo return (0); 377213237Sgonzo} 378213237Sgonzo 379213237Sgonzostatic int 380213237Sgonzogpiobus_probe(device_t dev) 381213237Sgonzo{ 382213237Sgonzo device_set_desc(dev, "GPIO bus"); 383258050Sloos 384258050Sloos return (BUS_PROBE_GENERIC); 385213237Sgonzo} 386213237Sgonzo 387213237Sgonzostatic int 388213237Sgonzogpiobus_attach(device_t dev) 389213237Sgonzo{ 390273569Sloos int err; 391213237Sgonzo 392273569Sloos err = gpiobus_init_softc(dev); 393273569Sloos if (err != 0) 394273569Sloos return (err); 395213237Sgonzo 396213237Sgonzo /* 397213237Sgonzo * Get parent's pins and mark them as unmapped 398213237Sgonzo */ 399258050Sloos bus_generic_probe(dev); 400213237Sgonzo bus_enumerate_hinted_children(dev); 401258050Sloos 402213237Sgonzo return (bus_generic_attach(dev)); 403213237Sgonzo} 404213237Sgonzo 405213237Sgonzo/* 406213237Sgonzo * Since this is not a self-enumerating bus, and since we always add 407213237Sgonzo * children in attach, we have to always delete children here. 408213237Sgonzo */ 409213237Sgonzostatic int 410213237Sgonzogpiobus_detach(device_t dev) 411213237Sgonzo{ 412254988Sloos struct gpiobus_softc *sc; 413254988Sloos struct gpiobus_ivar *devi; 414254988Sloos device_t *devlist; 415254988Sloos int i, err, ndevs; 416213237Sgonzo 417254988Sloos sc = GPIOBUS_SOFTC(dev); 418213237Sgonzo KASSERT(mtx_initialized(&sc->sc_mtx), 419213237Sgonzo ("gpiobus mutex not initialized")); 420213237Sgonzo GPIOBUS_LOCK_DESTROY(sc); 421213237Sgonzo 422213237Sgonzo if ((err = bus_generic_detach(dev)) != 0) 423213237Sgonzo return (err); 424213237Sgonzo 425254988Sloos if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) 426254988Sloos return (err); 427254988Sloos for (i = 0; i < ndevs; i++) { 428254988Sloos devi = GPIOBUS_IVAR(devlist[i]); 429279402Sloos gpiobus_free_ivars(devi); 430286845Sloos resource_list_free(&devi->rl); 431286845Sloos free(devi, M_DEVBUF); 432286845Sloos device_delete_child(dev, devlist[i]); 433254988Sloos } 434254988Sloos free(devlist, M_TEMP); 435286845Sloos rman_fini(&sc->sc_intr_rman); 436279761Sloos if (sc->sc_pins) { 437279761Sloos for (i = 0; i < sc->sc_npins; i++) { 438279761Sloos if (sc->sc_pins[i].name != NULL) 439279761Sloos free(sc->sc_pins[i].name, M_DEVBUF); 440279761Sloos sc->sc_pins[i].name = NULL; 441279761Sloos } 442279761Sloos free(sc->sc_pins, M_DEVBUF); 443279761Sloos sc->sc_pins = NULL; 444213237Sgonzo } 445213237Sgonzo 446213237Sgonzo return (0); 447213237Sgonzo} 448213237Sgonzo 449213237Sgonzostatic int 450213237Sgonzogpiobus_suspend(device_t dev) 451213237Sgonzo{ 452213237Sgonzo 453213237Sgonzo return (bus_generic_suspend(dev)); 454213237Sgonzo} 455213237Sgonzo 456213237Sgonzostatic int 457213237Sgonzogpiobus_resume(device_t dev) 458213237Sgonzo{ 459213237Sgonzo 460213237Sgonzo return (bus_generic_resume(dev)); 461213237Sgonzo} 462213237Sgonzo 463279620Sloosstatic void 464279620Sloosgpiobus_probe_nomatch(device_t dev, device_t child) 465279620Sloos{ 466279620Sloos char pins[128]; 467279620Sloos struct gpiobus_ivar *devi; 468279620Sloos 469279620Sloos devi = GPIOBUS_IVAR(child); 470279620Sloos memset(pins, 0, sizeof(pins)); 471279620Sloos gpiobus_print_pins(devi, pins, sizeof(pins)); 472286909Sloos if (devi->npins > 1) 473286909Sloos device_printf(dev, "<unknown device> at pins %s", pins); 474286909Sloos else 475286909Sloos device_printf(dev, "<unknown device> at pin %s", pins); 476297199Sjhibbits resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 477279620Sloos printf("\n"); 478279620Sloos} 479279620Sloos 480213237Sgonzostatic int 481213237Sgonzogpiobus_print_child(device_t dev, device_t child) 482213237Sgonzo{ 483274643Sloos char pins[128]; 484213237Sgonzo int retval = 0; 485274643Sloos struct gpiobus_ivar *devi; 486213237Sgonzo 487274643Sloos devi = GPIOBUS_IVAR(child); 488274643Sloos memset(pins, 0, sizeof(pins)); 489213237Sgonzo retval += bus_print_child_header(dev, child); 490285784Sloos if (devi->npins > 0) { 491285784Sloos if (devi->npins > 1) 492285784Sloos retval += printf(" at pins "); 493285784Sloos else 494285784Sloos retval += printf(" at pin "); 495285784Sloos gpiobus_print_pins(devi, pins, sizeof(pins)); 496285784Sloos retval += printf("%s", pins); 497285784Sloos } 498297199Sjhibbits resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 499213237Sgonzo retval += bus_print_child_footer(dev, child); 500213237Sgonzo 501213237Sgonzo return (retval); 502213237Sgonzo} 503213237Sgonzo 504213237Sgonzostatic int 505213237Sgonzogpiobus_child_location_str(device_t bus, device_t child, char *buf, 506213237Sgonzo size_t buflen) 507213237Sgonzo{ 508274643Sloos struct gpiobus_ivar *devi; 509213237Sgonzo 510274643Sloos devi = GPIOBUS_IVAR(child); 511286909Sloos if (devi->npins > 1) 512286909Sloos strlcpy(buf, "pins=", buflen); 513286909Sloos else 514286909Sloos strlcpy(buf, "pin=", buflen); 515274643Sloos gpiobus_print_pins(devi, buf, buflen); 516274643Sloos 517213237Sgonzo return (0); 518213237Sgonzo} 519213237Sgonzo 520213237Sgonzostatic int 521213237Sgonzogpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 522213237Sgonzo size_t buflen) 523213237Sgonzo{ 524213237Sgonzo 525213237Sgonzo *buf = '\0'; 526213237Sgonzo return (0); 527213237Sgonzo} 528213237Sgonzo 529213237Sgonzostatic device_t 530213237Sgonzogpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 531213237Sgonzo{ 532213237Sgonzo device_t child; 533213237Sgonzo struct gpiobus_ivar *devi; 534213237Sgonzo 535213237Sgonzo child = device_add_child_ordered(dev, order, name, unit); 536213237Sgonzo if (child == NULL) 537213237Sgonzo return (child); 538213237Sgonzo devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 539213237Sgonzo if (devi == NULL) { 540213237Sgonzo device_delete_child(dev, child); 541286845Sloos return (NULL); 542213237Sgonzo } 543274638Sloos resource_list_init(&devi->rl); 544213237Sgonzo device_set_ivars(child, devi); 545274638Sloos 546213237Sgonzo return (child); 547213237Sgonzo} 548213237Sgonzo 549213237Sgonzostatic void 550213237Sgonzogpiobus_hinted_child(device_t bus, const char *dname, int dunit) 551213237Sgonzo{ 552213237Sgonzo struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); 553213237Sgonzo struct gpiobus_ivar *devi; 554213237Sgonzo device_t child; 555274638Sloos int irq, pins; 556213237Sgonzo 557213237Sgonzo child = BUS_ADD_CHILD(bus, 0, dname, dunit); 558213237Sgonzo devi = GPIOBUS_IVAR(child); 559213237Sgonzo resource_int_value(dname, dunit, "pins", &pins); 560286845Sloos if (gpiobus_parse_pins(sc, child, pins)) { 561286845Sloos resource_list_free(&devi->rl); 562286845Sloos free(devi, M_DEVBUF); 563213237Sgonzo device_delete_child(bus, child); 564286845Sloos } 565274638Sloos if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 566274638Sloos if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) 567274638Sloos device_printf(bus, 568274638Sloos "warning: bus_set_resource() failed\n"); 569274638Sloos } 570213237Sgonzo} 571213237Sgonzo 572273917Sloosstatic int 573274638Sloosgpiobus_set_resource(device_t dev, device_t child, int type, int rid, 574294883Sjhibbits rman_res_t start, rman_res_t count) 575274638Sloos{ 576274638Sloos struct gpiobus_ivar *devi; 577274638Sloos struct resource_list_entry *rle; 578274638Sloos 579274638Sloos dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n", 580274638Sloos __func__, dev, child, type, rid, (void *)(intptr_t)start, count); 581274638Sloos devi = GPIOBUS_IVAR(child); 582274638Sloos rle = resource_list_add(&devi->rl, type, rid, start, 583274638Sloos start + count - 1, count); 584274638Sloos if (rle == NULL) 585274638Sloos return (ENXIO); 586274638Sloos 587274638Sloos return (0); 588274638Sloos} 589274638Sloos 590274638Sloosstatic struct resource * 591274638Sloosgpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, 592294883Sjhibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 593274638Sloos{ 594274638Sloos struct gpiobus_softc *sc; 595274638Sloos struct resource *rv; 596274638Sloos struct resource_list *rl; 597274638Sloos struct resource_list_entry *rle; 598274638Sloos int isdefault; 599274638Sloos 600274638Sloos if (type != SYS_RES_IRQ) 601274638Sloos return (NULL); 602295832Sjhibbits isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1); 603274638Sloos rle = NULL; 604274638Sloos if (isdefault) { 605274638Sloos rl = BUS_GET_RESOURCE_LIST(bus, child); 606274638Sloos if (rl == NULL) 607274638Sloos return (NULL); 608274638Sloos rle = resource_list_find(rl, type, *rid); 609274638Sloos if (rle == NULL) 610274638Sloos return (NULL); 611274638Sloos if (rle->res != NULL) 612274638Sloos panic("%s: resource entry is busy", __func__); 613274638Sloos start = rle->start; 614274638Sloos count = rle->count; 615274638Sloos end = rle->end; 616274638Sloos } 617274638Sloos sc = device_get_softc(bus); 618274638Sloos rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags, 619274638Sloos child); 620274638Sloos if (rv == NULL) 621274638Sloos return (NULL); 622274638Sloos rman_set_rid(rv, *rid); 623274638Sloos if ((flags & RF_ACTIVE) != 0 && 624274638Sloos bus_activate_resource(child, type, *rid, rv) != 0) { 625274638Sloos rman_release_resource(rv); 626274638Sloos return (NULL); 627274638Sloos } 628274638Sloos 629274638Sloos return (rv); 630274638Sloos} 631274638Sloos 632274638Sloosstatic int 633274638Sloosgpiobus_release_resource(device_t bus __unused, device_t child, int type, 634274638Sloos int rid, struct resource *r) 635274638Sloos{ 636274638Sloos int error; 637274638Sloos 638274638Sloos if (rman_get_flags(r) & RF_ACTIVE) { 639274638Sloos error = bus_deactivate_resource(child, type, rid, r); 640274638Sloos if (error) 641274638Sloos return (error); 642274638Sloos } 643274638Sloos 644274638Sloos return (rman_release_resource(r)); 645274638Sloos} 646274638Sloos 647274638Sloosstatic struct resource_list * 648274638Sloosgpiobus_get_resource_list(device_t bus __unused, device_t child) 649274638Sloos{ 650274638Sloos struct gpiobus_ivar *ivar; 651274638Sloos 652274638Sloos ivar = GPIOBUS_IVAR(child); 653274638Sloos 654274638Sloos return (&ivar->rl); 655274638Sloos} 656274638Sloos 657274638Sloosstatic int 658273917Sloosgpiobus_acquire_bus(device_t busdev, device_t child, int how) 659213237Sgonzo{ 660213237Sgonzo struct gpiobus_softc *sc; 661213237Sgonzo 662213237Sgonzo sc = device_get_softc(busdev); 663213237Sgonzo GPIOBUS_ASSERT_UNLOCKED(sc); 664213237Sgonzo GPIOBUS_LOCK(sc); 665273917Sloos if (sc->sc_owner != NULL) { 666285833Simp if (sc->sc_owner == child) 667285833Simp panic("%s: %s still owns the bus.", 668285833Simp device_get_nameunit(busdev), 669285833Simp device_get_nameunit(child)); 670273917Sloos if (how == GPIOBUS_DONTWAIT) { 671273917Sloos GPIOBUS_UNLOCK(sc); 672273917Sloos return (EWOULDBLOCK); 673273917Sloos } 674273917Sloos while (sc->sc_owner != NULL) 675273917Sloos mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); 676273917Sloos } 677273917Sloos sc->sc_owner = child; 678213237Sgonzo GPIOBUS_UNLOCK(sc); 679213237Sgonzo 680273917Sloos return (0); 681213237Sgonzo} 682213237Sgonzo 683213237Sgonzostatic void 684213237Sgonzogpiobus_release_bus(device_t busdev, device_t child) 685213237Sgonzo{ 686213237Sgonzo struct gpiobus_softc *sc; 687213237Sgonzo 688213237Sgonzo sc = device_get_softc(busdev); 689273917Sloos GPIOBUS_ASSERT_UNLOCKED(sc); 690273917Sloos GPIOBUS_LOCK(sc); 691273917Sloos if (sc->sc_owner == NULL) 692285833Simp panic("%s: %s releasing unowned bus.", 693285833Simp device_get_nameunit(busdev), 694285833Simp device_get_nameunit(child)); 695213237Sgonzo if (sc->sc_owner != child) 696285833Simp panic("%s: %s trying to release bus owned by %s", 697285833Simp device_get_nameunit(busdev), 698285833Simp device_get_nameunit(child), 699285833Simp device_get_nameunit(sc->sc_owner)); 700213237Sgonzo sc->sc_owner = NULL; 701273917Sloos wakeup(sc); 702273917Sloos GPIOBUS_UNLOCK(sc); 703213237Sgonzo} 704213237Sgonzo 705213237Sgonzostatic int 706213237Sgonzogpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, 707213237Sgonzo uint32_t flags) 708213237Sgonzo{ 709213237Sgonzo struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 710213237Sgonzo struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 711274670Sloos uint32_t caps; 712213237Sgonzo 713213237Sgonzo if (pin >= devi->npins) 714213237Sgonzo return (EINVAL); 715274670Sloos if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0) 716274670Sloos return (EINVAL); 717274670Sloos if (gpio_check_flags(caps, flags) != 0) 718274670Sloos return (EINVAL); 719213237Sgonzo 720274670Sloos return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags)); 721213237Sgonzo} 722213237Sgonzo 723213237Sgonzostatic int 724213237Sgonzogpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, 725213237Sgonzo uint32_t *flags) 726213237Sgonzo{ 727213237Sgonzo struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 728213237Sgonzo struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 729213237Sgonzo 730213237Sgonzo if (pin >= devi->npins) 731213237Sgonzo return (EINVAL); 732213237Sgonzo 733213237Sgonzo return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); 734213237Sgonzo} 735213237Sgonzo 736213237Sgonzostatic int 737213237Sgonzogpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, 738213237Sgonzo uint32_t *caps) 739213237Sgonzo{ 740213237Sgonzo struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 741213237Sgonzo struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 742213237Sgonzo 743213237Sgonzo if (pin >= devi->npins) 744213237Sgonzo return (EINVAL); 745213237Sgonzo 746213237Sgonzo return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); 747213237Sgonzo} 748213237Sgonzo 749213237Sgonzostatic int 750213237Sgonzogpiobus_pin_set(device_t dev, device_t child, uint32_t pin, 751213237Sgonzo unsigned int value) 752213237Sgonzo{ 753213237Sgonzo struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 754213237Sgonzo struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 755213237Sgonzo 756213237Sgonzo if (pin >= devi->npins) 757213237Sgonzo return (EINVAL); 758213237Sgonzo 759213237Sgonzo return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); 760213237Sgonzo} 761213237Sgonzo 762213237Sgonzostatic int 763213237Sgonzogpiobus_pin_get(device_t dev, device_t child, uint32_t pin, 764213237Sgonzo unsigned int *value) 765213237Sgonzo{ 766213237Sgonzo struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 767213237Sgonzo struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 768213237Sgonzo 769213237Sgonzo if (pin >= devi->npins) 770213237Sgonzo return (EINVAL); 771213237Sgonzo 772213237Sgonzo return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); 773213237Sgonzo} 774213237Sgonzo 775213237Sgonzostatic int 776213237Sgonzogpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) 777213237Sgonzo{ 778213237Sgonzo struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 779213237Sgonzo struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 780213237Sgonzo 781213237Sgonzo if (pin >= devi->npins) 782213237Sgonzo return (EINVAL); 783213237Sgonzo 784213237Sgonzo return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); 785213237Sgonzo} 786213237Sgonzo 787279761Sloosstatic int 788279761Sloosgpiobus_pin_getname(device_t dev, uint32_t pin, char *name) 789279761Sloos{ 790279761Sloos struct gpiobus_softc *sc; 791279761Sloos 792279761Sloos sc = GPIOBUS_SOFTC(dev); 793279761Sloos if (pin > sc->sc_npins) 794279761Sloos return (EINVAL); 795279761Sloos /* Did we have a name for this pin ? */ 796279761Sloos if (sc->sc_pins[pin].name != NULL) { 797279761Sloos memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME); 798279761Sloos return (0); 799279761Sloos } 800279761Sloos 801279761Sloos /* Return the default pin name. */ 802279761Sloos return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name)); 803279761Sloos} 804279761Sloos 805279761Sloosstatic int 806279761Sloosgpiobus_pin_setname(device_t dev, uint32_t pin, const char *name) 807279761Sloos{ 808279761Sloos struct gpiobus_softc *sc; 809279761Sloos 810279761Sloos sc = GPIOBUS_SOFTC(dev); 811279761Sloos if (pin > sc->sc_npins) 812279761Sloos return (EINVAL); 813279761Sloos if (name == NULL) 814279761Sloos return (EINVAL); 815279761Sloos /* Save the pin name. */ 816279761Sloos if (sc->sc_pins[pin].name == NULL) 817279761Sloos sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF, 818279761Sloos M_WAITOK | M_ZERO); 819279761Sloos strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME); 820279761Sloos 821279761Sloos return (0); 822279761Sloos} 823279761Sloos 824213237Sgonzostatic device_method_t gpiobus_methods[] = { 825213237Sgonzo /* Device interface */ 826213237Sgonzo DEVMETHOD(device_probe, gpiobus_probe), 827213237Sgonzo DEVMETHOD(device_attach, gpiobus_attach), 828213237Sgonzo DEVMETHOD(device_detach, gpiobus_detach), 829213237Sgonzo DEVMETHOD(device_shutdown, bus_generic_shutdown), 830213237Sgonzo DEVMETHOD(device_suspend, gpiobus_suspend), 831213237Sgonzo DEVMETHOD(device_resume, gpiobus_resume), 832213237Sgonzo 833213237Sgonzo /* Bus interface */ 834274638Sloos DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 835274638Sloos DEVMETHOD(bus_config_intr, bus_generic_config_intr), 836274638Sloos DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 837274638Sloos DEVMETHOD(bus_set_resource, gpiobus_set_resource), 838274638Sloos DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource), 839274638Sloos DEVMETHOD(bus_release_resource, gpiobus_release_resource), 840274638Sloos DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 841274638Sloos DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 842274638Sloos DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list), 843213237Sgonzo DEVMETHOD(bus_add_child, gpiobus_add_child), 844279620Sloos DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch), 845213237Sgonzo DEVMETHOD(bus_print_child, gpiobus_print_child), 846213237Sgonzo DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str), 847213237Sgonzo DEVMETHOD(bus_child_location_str, gpiobus_child_location_str), 848213237Sgonzo DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), 849213237Sgonzo 850213237Sgonzo /* GPIO protocol */ 851213237Sgonzo DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), 852213237Sgonzo DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), 853213237Sgonzo DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), 854213237Sgonzo DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), 855213237Sgonzo DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), 856213237Sgonzo DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), 857213237Sgonzo DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), 858213237Sgonzo DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), 859279761Sloos DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname), 860279761Sloos DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname), 861213237Sgonzo 862227843Smarius DEVMETHOD_END 863213237Sgonzo}; 864213237Sgonzo 865215142Sthompsadriver_t gpiobus_driver = { 866213237Sgonzo "gpiobus", 867213237Sgonzo gpiobus_methods, 868213237Sgonzo sizeof(struct gpiobus_softc) 869213237Sgonzo}; 870213237Sgonzo 871213237Sgonzodevclass_t gpiobus_devclass; 872213237Sgonzo 873213237SgonzoDRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0); 874213237SgonzoMODULE_VERSION(gpiobus, 1); 875