gpiobus.c revision 266105
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: stable/10/sys/dev/gpio/gpiobus.c 266105 2014-05-15 01:27:53Z loos $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/malloc.h> 33#include <sys/module.h> 34#include <sys/kernel.h> 35#include <sys/queue.h> 36#include <sys/sysctl.h> 37#include <sys/types.h> 38 39#include <sys/bus.h> 40#include <machine/bus.h> 41#include <sys/rman.h> 42#include <machine/resource.h> 43 44#include <sys/gpio.h> 45#include <dev/gpio/gpiobusvar.h> 46#include "gpio_if.h" 47#include "gpiobus_if.h" 48 49static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); 50static int gpiobus_probe(device_t); 51static int gpiobus_attach(device_t); 52static int gpiobus_detach(device_t); 53static int gpiobus_suspend(device_t); 54static int gpiobus_resume(device_t); 55static int gpiobus_print_child(device_t, device_t); 56static int gpiobus_child_location_str(device_t, device_t, char *, size_t); 57static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t); 58static device_t gpiobus_add_child(device_t, u_int, const char *, int); 59static void gpiobus_hinted_child(device_t, const char *, int); 60 61/* 62 * GPIOBUS interface 63 */ 64static void gpiobus_lock_bus(device_t); 65static void gpiobus_unlock_bus(device_t); 66static void gpiobus_acquire_bus(device_t, device_t); 67static void gpiobus_release_bus(device_t, device_t); 68static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t); 69static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*); 70static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*); 71static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int); 72static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*); 73static int gpiobus_pin_toggle(device_t, device_t, uint32_t); 74 75void 76gpiobus_print_pins(struct gpiobus_ivar *devi) 77{ 78 int range_start, range_stop, need_coma; 79 int i; 80 81 if (devi->npins == 0) 82 return; 83 84 need_coma = 0; 85 range_start = range_stop = devi->pins[0]; 86 for (i = 1; i < devi->npins; i++) { 87 if (devi->pins[i] != (range_stop + 1)) { 88 if (need_coma) 89 printf(","); 90 if (range_start != range_stop) 91 printf("%d-%d", range_start, range_stop); 92 else 93 printf("%d", range_start); 94 95 range_start = range_stop = devi->pins[i]; 96 need_coma = 1; 97 } 98 else 99 range_stop++; 100 } 101 102 if (need_coma) 103 printf(","); 104 if (range_start != range_stop) 105 printf("%d-%d", range_start, range_stop); 106 else 107 printf("%d", range_start); 108} 109 110static int 111gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) 112{ 113 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 114 int i, npins; 115 116 npins = 0; 117 for (i = 0; i < 32; i++) { 118 if (mask & (1 << i)) 119 npins++; 120 } 121 122 if (npins == 0) { 123 device_printf(child, "empty pin mask\n"); 124 return (EINVAL); 125 } 126 127 devi->npins = npins; 128 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, 129 M_NOWAIT | M_ZERO); 130 131 if (!devi->pins) 132 return (ENOMEM); 133 134 npins = 0; 135 for (i = 0; i < 32; i++) { 136 137 if ((mask & (1 << i)) == 0) 138 continue; 139 140 if (i >= sc->sc_npins) { 141 device_printf(child, 142 "invalid pin %d, max: %d\n", i, sc->sc_npins - 1); 143 free(devi->pins, M_DEVBUF); 144 return (EINVAL); 145 } 146 147 devi->pins[npins++] = i; 148 /* 149 * Mark pin as mapped and give warning if it's already mapped 150 */ 151 if (sc->sc_pins_mapped[i]) { 152 device_printf(child, 153 "warning: pin %d is already mapped\n", i); 154 free(devi->pins, M_DEVBUF); 155 return (EINVAL); 156 } 157 sc->sc_pins_mapped[i] = 1; 158 } 159 160 return (0); 161} 162 163static int 164gpiobus_probe(device_t dev) 165{ 166 device_set_desc(dev, "GPIO bus"); 167 168 return (BUS_PROBE_GENERIC); 169} 170 171static int 172gpiobus_attach(device_t dev) 173{ 174 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 175 int res; 176 177 sc->sc_busdev = dev; 178 sc->sc_dev = device_get_parent(dev); 179 res = GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins); 180 if (res) 181 return (ENXIO); 182 183 KASSERT(sc->sc_npins != 0, ("GPIO device with no pins")); 184 185 /* 186 * Increase to get number of pins 187 */ 188 sc->sc_npins++; 189 190 sc->sc_pins_mapped = malloc(sizeof(int) * sc->sc_npins, M_DEVBUF, 191 M_NOWAIT | M_ZERO); 192 193 if (!sc->sc_pins_mapped) 194 return (ENOMEM); 195 196 /* init bus lock */ 197 GPIOBUS_LOCK_INIT(sc); 198 199 /* 200 * Get parent's pins and mark them as unmapped 201 */ 202 bus_generic_probe(dev); 203 bus_enumerate_hinted_children(dev); 204 205 return (bus_generic_attach(dev)); 206} 207 208/* 209 * Since this is not a self-enumerating bus, and since we always add 210 * children in attach, we have to always delete children here. 211 */ 212static int 213gpiobus_detach(device_t dev) 214{ 215 struct gpiobus_softc *sc; 216 struct gpiobus_ivar *devi; 217 device_t *devlist; 218 int i, err, ndevs; 219 220 sc = GPIOBUS_SOFTC(dev); 221 KASSERT(mtx_initialized(&sc->sc_mtx), 222 ("gpiobus mutex not initialized")); 223 GPIOBUS_LOCK_DESTROY(sc); 224 225 if ((err = bus_generic_detach(dev)) != 0) 226 return (err); 227 228 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) 229 return (err); 230 for (i = 0; i < ndevs; i++) { 231 device_delete_child(dev, devlist[i]); 232 devi = GPIOBUS_IVAR(devlist[i]); 233 if (devi->pins) { 234 free(devi->pins, M_DEVBUF); 235 devi->pins = NULL; 236 } 237 } 238 free(devlist, M_TEMP); 239 240 if (sc->sc_pins_mapped) { 241 free(sc->sc_pins_mapped, M_DEVBUF); 242 sc->sc_pins_mapped = NULL; 243 } 244 245 return (0); 246} 247 248static int 249gpiobus_suspend(device_t dev) 250{ 251 252 return (bus_generic_suspend(dev)); 253} 254 255static int 256gpiobus_resume(device_t dev) 257{ 258 259 return (bus_generic_resume(dev)); 260} 261 262static int 263gpiobus_print_child(device_t dev, device_t child) 264{ 265 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 266 int retval = 0; 267 268 retval += bus_print_child_header(dev, child); 269 retval += printf(" at pin(s) "); 270 gpiobus_print_pins(devi); 271 retval += bus_print_child_footer(dev, child); 272 273 return (retval); 274} 275 276static int 277gpiobus_child_location_str(device_t bus, device_t child, char *buf, 278 size_t buflen) 279{ 280 281 snprintf(buf, buflen, "pins=?"); 282 return (0); 283} 284 285static int 286gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 287 size_t buflen) 288{ 289 290 *buf = '\0'; 291 return (0); 292} 293 294static device_t 295gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 296{ 297 device_t child; 298 struct gpiobus_ivar *devi; 299 300 child = device_add_child_ordered(dev, order, name, unit); 301 if (child == NULL) 302 return (child); 303 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 304 if (devi == NULL) { 305 device_delete_child(dev, child); 306 return (0); 307 } 308 device_set_ivars(child, devi); 309 return (child); 310} 311 312static void 313gpiobus_hinted_child(device_t bus, const char *dname, int dunit) 314{ 315 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); 316 struct gpiobus_ivar *devi; 317 device_t child; 318 int pins; 319 320 321 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 322 devi = GPIOBUS_IVAR(child); 323 resource_int_value(dname, dunit, "pins", &pins); 324 if (gpiobus_parse_pins(sc, child, pins)) 325 device_delete_child(bus, child); 326} 327 328static void 329gpiobus_lock_bus(device_t busdev) 330{ 331 struct gpiobus_softc *sc; 332 333 sc = device_get_softc(busdev); 334 GPIOBUS_ASSERT_UNLOCKED(sc); 335 GPIOBUS_LOCK(sc); 336} 337 338static void 339gpiobus_unlock_bus(device_t busdev) 340{ 341 struct gpiobus_softc *sc; 342 343 sc = device_get_softc(busdev); 344 GPIOBUS_ASSERT_LOCKED(sc); 345 GPIOBUS_UNLOCK(sc); 346} 347 348static void 349gpiobus_acquire_bus(device_t busdev, device_t child) 350{ 351 struct gpiobus_softc *sc; 352 353 sc = device_get_softc(busdev); 354 GPIOBUS_ASSERT_LOCKED(sc); 355 356 if (sc->sc_owner) 357 panic("gpiobus: cannot serialize the access to device."); 358 sc->sc_owner = child; 359} 360 361static void 362gpiobus_release_bus(device_t busdev, device_t child) 363{ 364 struct gpiobus_softc *sc; 365 366 sc = device_get_softc(busdev); 367 GPIOBUS_ASSERT_LOCKED(sc); 368 369 if (!sc->sc_owner) 370 panic("gpiobus: releasing unowned bus."); 371 if (sc->sc_owner != child) 372 panic("gpiobus: you don't own the bus. game over."); 373 374 sc->sc_owner = NULL; 375} 376 377static int 378gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, 379 uint32_t flags) 380{ 381 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 382 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 383 384 if (pin >= devi->npins) 385 return (EINVAL); 386 387 return GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags); 388} 389 390static int 391gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, 392 uint32_t *flags) 393{ 394 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 395 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 396 397 if (pin >= devi->npins) 398 return (EINVAL); 399 400 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); 401} 402 403static int 404gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, 405 uint32_t *caps) 406{ 407 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 408 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 409 410 if (pin >= devi->npins) 411 return (EINVAL); 412 413 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); 414} 415 416static int 417gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, 418 unsigned int value) 419{ 420 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 421 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 422 423 if (pin >= devi->npins) 424 return (EINVAL); 425 426 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); 427} 428 429static int 430gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, 431 unsigned int *value) 432{ 433 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 434 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 435 436 if (pin >= devi->npins) 437 return (EINVAL); 438 439 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); 440} 441 442static int 443gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) 444{ 445 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 446 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 447 448 if (pin >= devi->npins) 449 return (EINVAL); 450 451 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); 452} 453 454static device_method_t gpiobus_methods[] = { 455 /* Device interface */ 456 DEVMETHOD(device_probe, gpiobus_probe), 457 DEVMETHOD(device_attach, gpiobus_attach), 458 DEVMETHOD(device_detach, gpiobus_detach), 459 DEVMETHOD(device_shutdown, bus_generic_shutdown), 460 DEVMETHOD(device_suspend, gpiobus_suspend), 461 DEVMETHOD(device_resume, gpiobus_resume), 462 463 /* Bus interface */ 464 DEVMETHOD(bus_add_child, gpiobus_add_child), 465 DEVMETHOD(bus_print_child, gpiobus_print_child), 466 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str), 467 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str), 468 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), 469 470 /* GPIO protocol */ 471 DEVMETHOD(gpiobus_lock_bus, gpiobus_lock_bus), 472 DEVMETHOD(gpiobus_unlock_bus, gpiobus_unlock_bus), 473 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), 474 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), 475 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), 476 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), 477 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), 478 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), 479 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), 480 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), 481 482 DEVMETHOD_END 483}; 484 485driver_t gpiobus_driver = { 486 "gpiobus", 487 gpiobus_methods, 488 sizeof(struct gpiobus_softc) 489}; 490 491devclass_t gpiobus_devclass; 492 493DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0); 494MODULE_VERSION(gpiobus, 1); 495