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