1 /*- 2 * Copyright 2016 Michal Meloun <mmel@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 #include "opt_platform.h" 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/conf.h> 33 #include <sys/gpio.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 39 #ifdef FDT 40 #include <dev/fdt/fdt_common.h> 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 #endif 44 #include <dev/gpio/gpiobusvar.h> 45 #include <dev/regulator/regulator_fixed.h> 46 47 #ifdef FDT 48 #include "regdev_if.h" 49 #endif 50 51 MALLOC_DEFINE(M_FIXEDREGULATOR, "fixedregulator", "Fixed regulator"); 52 53 /* GPIO list for shared pins. */ 54 typedef TAILQ_HEAD(gpio_list, gpio_entry) gpio_list_t; 55 struct gpio_entry { 56 TAILQ_ENTRY(gpio_entry) link; 57 struct gpiobus_pin gpio_pin; 58 int use_cnt; 59 int enable_cnt; 60 bool always_on; 61 }; 62 static gpio_list_t gpio_list = TAILQ_HEAD_INITIALIZER(gpio_list); 63 static struct mtx gpio_list_mtx; 64 MTX_SYSINIT(gpio_list_lock, &gpio_list_mtx, "Regulator GPIO lock", MTX_DEF); 65 66 struct regnode_fixed_sc { 67 struct regnode_std_param *param; 68 bool gpio_open_drain; 69 struct gpio_entry *gpio_entry; 70 }; 71 72 static int regnode_fixed_init(struct regnode *regnode); 73 static int regnode_fixed_enable(struct regnode *regnode, bool enable, 74 int *udelay); 75 static int regnode_fixed_status(struct regnode *regnode, int *status); 76 static int regnode_fixed_stop(struct regnode *regnode, int *udelay); 77 static int regnode_fixed_get_voltage(struct regnode *regnode, int *uvolt); 78 79 static regnode_method_t regnode_fixed_methods[] = { 80 /* Regulator interface */ 81 REGNODEMETHOD(regnode_init, regnode_fixed_init), 82 REGNODEMETHOD(regnode_enable, regnode_fixed_enable), 83 REGNODEMETHOD(regnode_status, regnode_fixed_status), 84 REGNODEMETHOD(regnode_stop, regnode_fixed_stop), 85 REGNODEMETHOD(regnode_get_voltage, regnode_fixed_get_voltage), 86 REGNODEMETHOD(regnode_check_voltage, regnode_method_check_voltage), 87 REGNODEMETHOD_END 88 }; 89 DEFINE_CLASS_1(regnode_fixed, regnode_fixed_class, regnode_fixed_methods, 90 sizeof(struct regnode_fixed_sc), regnode_class); 91 92 /* 93 * GPIO list functions. 94 * Two or more regulators can share single GPIO pins, so we must track all 95 * GPIOs in gpio_list. 96 * The GPIO pin is registerd and reseved for first consumer, all others share 97 * gpio_entry with it. 98 */ 99 static struct gpio_entry * 100 regnode_get_gpio_entry(struct gpiobus_pin *gpio_pin) 101 { 102 struct gpio_entry *entry, *tmp; 103 int rv; 104 105 entry = malloc(sizeof(struct gpio_entry), M_FIXEDREGULATOR, 106 M_WAITOK | M_ZERO); 107 108 mtx_lock(&gpio_list_mtx); 109 110 TAILQ_FOREACH(tmp, &gpio_list, link) { 111 if (tmp->gpio_pin.dev == gpio_pin->dev && 112 tmp->gpio_pin.pin == gpio_pin->pin) { 113 tmp->use_cnt++; 114 mtx_unlock(&gpio_list_mtx); 115 free(entry, M_FIXEDREGULATOR); 116 return (tmp); 117 } 118 } 119 120 /* Reserve pin. */ 121 /* XXX Can we call gpio_pin_acquire() with gpio_list_mtx held? */ 122 rv = gpio_pin_acquire(gpio_pin); 123 if (rv != 0) { 124 mtx_unlock(&gpio_list_mtx); 125 free(entry, M_FIXEDREGULATOR); 126 return (NULL); 127 } 128 /* Everything is OK, build new entry and insert it to list. */ 129 entry->gpio_pin = *gpio_pin; 130 entry->use_cnt = 1; 131 TAILQ_INSERT_TAIL(&gpio_list, entry, link); 132 133 mtx_unlock(&gpio_list_mtx); 134 return (entry); 135 } 136 137 138 /* 139 * Regulator class implementation. 140 */ 141 static int 142 regnode_fixed_init(struct regnode *regnode) 143 { 144 device_t dev; 145 struct regnode_fixed_sc *sc; 146 struct gpiobus_pin *pin; 147 uint32_t flags; 148 int rv; 149 150 sc = regnode_get_softc(regnode); 151 dev = regnode_get_device(regnode); 152 sc->param = regnode_get_stdparam(regnode); 153 if (sc->gpio_entry == NULL) 154 return (0); 155 pin = &sc->gpio_entry->gpio_pin; 156 157 flags = GPIO_PIN_OUTPUT; 158 if (sc->gpio_open_drain) 159 flags |= GPIO_PIN_OPENDRAIN; 160 if (sc->param->boot_on || sc->param->always_on) { 161 rv = GPIO_PIN_SET(pin->dev, pin->pin, sc->param->enable_active_high); 162 if (rv != 0) { 163 device_printf(dev, "Cannot set GPIO pin: %d\n", 164 pin->pin); 165 return (rv); 166 } 167 } 168 169 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags); 170 if (rv != 0) { 171 device_printf(dev, "Cannot configure GPIO pin: %d\n", pin->pin); 172 return (rv); 173 } 174 175 return (0); 176 } 177 178 /* 179 * Enable/disable regulator. 180 * Take shared GPIO pins in account 181 */ 182 static int 183 regnode_fixed_enable(struct regnode *regnode, bool enable, int *udelay) 184 { 185 device_t dev; 186 struct regnode_fixed_sc *sc; 187 struct gpiobus_pin *pin; 188 int rv; 189 190 sc = regnode_get_softc(regnode); 191 dev = regnode_get_device(regnode); 192 193 *udelay = 0; 194 if (sc->gpio_entry == NULL) 195 return (0); 196 pin = &sc->gpio_entry->gpio_pin; 197 if (enable) { 198 sc->gpio_entry->enable_cnt++; 199 if (sc->gpio_entry->enable_cnt > 1) 200 return (0); 201 } else { 202 KASSERT(sc->gpio_entry->enable_cnt > 0, 203 ("Invalid enable count")); 204 sc->gpio_entry->enable_cnt--; 205 if (sc->gpio_entry->enable_cnt >= 1) 206 return (0); 207 } 208 if (sc->gpio_entry->always_on && !enable) 209 return (0); 210 if (!sc->param->enable_active_high) 211 enable = !enable; 212 rv = GPIO_PIN_SET(pin->dev, pin->pin, enable); 213 if (rv != 0) { 214 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin); 215 return (rv); 216 } 217 *udelay = sc->param->enable_delay; 218 return (0); 219 } 220 221 /* 222 * Stop (physicaly shutdown) regulator. 223 * Take shared GPIO pins in account 224 */ 225 static int 226 regnode_fixed_stop(struct regnode *regnode, int *udelay) 227 { 228 device_t dev; 229 struct regnode_fixed_sc *sc; 230 struct gpiobus_pin *pin; 231 int rv; 232 233 sc = regnode_get_softc(regnode); 234 dev = regnode_get_device(regnode); 235 236 *udelay = 0; 237 if (sc->gpio_entry == NULL) 238 return (0); 239 if (sc->gpio_entry->always_on) 240 return (0); 241 pin = &sc->gpio_entry->gpio_pin; 242 if (sc->gpio_entry->enable_cnt > 0) { 243 /* Other regulator(s) are enabled. */ 244 /* XXXX Any diagnostic message? Or error? */ 245 return (0); 246 } 247 rv = GPIO_PIN_SET(pin->dev, pin->pin, 248 sc->param->enable_active_high ? false: true); 249 if (rv != 0) { 250 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin); 251 return (rv); 252 } 253 *udelay = sc->param->enable_delay; 254 return (0); 255 } 256 257 static int 258 regnode_fixed_status(struct regnode *regnode, int *status) 259 { 260 struct regnode_fixed_sc *sc; 261 struct gpiobus_pin *pin; 262 uint32_t val; 263 int rv; 264 265 sc = regnode_get_softc(regnode); 266 267 *status = 0; 268 if (sc->gpio_entry == NULL) { 269 *status = REGULATOR_STATUS_ENABLED; 270 return (0); 271 } 272 pin = &sc->gpio_entry->gpio_pin; 273 274 rv = GPIO_PIN_GET(pin->dev, pin->pin, &val); 275 if (rv == 0) { 276 if (!sc->param->enable_active_high ^ (val != 0)) 277 *status = REGULATOR_STATUS_ENABLED; 278 } 279 return (rv); 280 } 281 282 static int 283 regnode_fixed_get_voltage(struct regnode *regnode, int *uvolt) 284 { 285 struct regnode_fixed_sc *sc; 286 287 sc = regnode_get_softc(regnode); 288 *uvolt = sc->param->min_uvolt; 289 return (0); 290 } 291 292 int 293 regnode_fixed_register(device_t dev, struct regnode_fixed_init_def *init_def) 294 { 295 struct regnode *regnode; 296 struct regnode_fixed_sc *sc; 297 298 regnode = regnode_create(dev, ®node_fixed_class, 299 &init_def->reg_init_def); 300 if (regnode == NULL) { 301 device_printf(dev, "Cannot create regulator.\n"); 302 return(ENXIO); 303 } 304 sc = regnode_get_softc(regnode); 305 sc->gpio_open_drain = init_def->gpio_open_drain; 306 if (init_def->gpio_pin != NULL) { 307 sc->gpio_entry = regnode_get_gpio_entry(init_def->gpio_pin); 308 if (sc->gpio_entry == NULL) 309 return(ENXIO); 310 } 311 regnode = regnode_register(regnode); 312 if (regnode == NULL) { 313 device_printf(dev, "Cannot register regulator.\n"); 314 return(ENXIO); 315 } 316 317 if (sc->gpio_entry != NULL) 318 sc->gpio_entry->always_on |= sc->param->always_on; 319 320 return (0); 321 } 322 323 /* 324 * OFW Driver implementation. 325 */ 326 #ifdef FDT 327 328 struct regfix_softc 329 { 330 device_t dev; 331 bool attach_done; 332 struct regnode_fixed_init_def init_def; 333 phandle_t gpio_prodxref; 334 pcell_t *gpio_cells; 335 int gpio_ncells; 336 struct gpiobus_pin gpio_pin; 337 }; 338 339 static struct ofw_compat_data compat_data[] = { 340 {"regulator-fixed", 1}, 341 {NULL, 0}, 342 }; 343 344 static int 345 regfix_get_gpio(struct regfix_softc * sc) 346 { 347 device_t busdev; 348 phandle_t node; 349 350 int rv; 351 352 if (sc->gpio_prodxref == 0) 353 return (0); 354 355 node = ofw_bus_get_node(sc->dev); 356 357 /* Test if controller exist. */ 358 sc->gpio_pin.dev = OF_device_from_xref(sc->gpio_prodxref); 359 if (sc->gpio_pin.dev == NULL) 360 return (ENODEV); 361 362 /* Test if GPIO bus already exist. */ 363 busdev = GPIO_GET_BUS(sc->gpio_pin.dev); 364 if (busdev == NULL) 365 return (ENODEV); 366 367 rv = gpio_map_gpios(sc->gpio_pin.dev, node, 368 OF_node_from_xref(sc->gpio_prodxref), sc->gpio_ncells, 369 sc->gpio_cells, &(sc->gpio_pin.pin), &(sc->gpio_pin.flags)); 370 if (rv != 0) { 371 device_printf(sc->dev, "Cannot map the gpio property.\n"); 372 return (ENXIO); 373 } 374 sc->init_def.gpio_pin = &sc->gpio_pin; 375 return (0); 376 } 377 378 static int 379 regfix_parse_fdt(struct regfix_softc * sc) 380 { 381 phandle_t node; 382 int rv; 383 struct regnode_init_def *init_def; 384 385 node = ofw_bus_get_node(sc->dev); 386 init_def = &sc->init_def.reg_init_def; 387 388 rv = regulator_parse_ofw_stdparam(sc->dev, node, init_def); 389 if (rv != 0) { 390 device_printf(sc->dev, "Cannot parse standard parameters.\n"); 391 return(rv); 392 } 393 394 if (init_def->std_param.min_uvolt != init_def->std_param.max_uvolt) { 395 device_printf(sc->dev, "min_uvolt != max_uvolt\n"); 396 return (ENXIO); 397 } 398 /* Fixed regulator uses 'startup-delay-us' property for enable_delay */ 399 rv = OF_getencprop(node, "startup-delay-us", 400 &init_def->std_param.enable_delay, 401 sizeof(init_def->std_param.enable_delay)); 402 if (rv <= 0) 403 init_def->std_param.enable_delay = 0; 404 /* GPIO pin */ 405 if (OF_hasprop(node, "gpio-open-drain")) 406 sc->init_def.gpio_open_drain = true; 407 408 if (!OF_hasprop(node, "gpio")) 409 return (0); 410 rv = ofw_bus_parse_xref_list_alloc(node, "gpio", "#gpio-cells", 0, 411 &sc->gpio_prodxref, &sc->gpio_ncells, &sc->gpio_cells); 412 if (rv != 0) { 413 sc->gpio_prodxref = 0; 414 device_printf(sc->dev, "Malformed gpio property\n"); 415 return (ENXIO); 416 } 417 return (0); 418 } 419 420 static void 421 regfix_new_pass(device_t dev) 422 { 423 struct regfix_softc * sc; 424 int rv; 425 426 sc = device_get_softc(dev); 427 bus_generic_new_pass(dev); 428 429 if (sc->attach_done) 430 return; 431 432 /* Try to get and configure GPIO. */ 433 rv = regfix_get_gpio(sc); 434 if (rv != 0) 435 return; 436 437 /* Register regulator. */ 438 regnode_fixed_register(sc->dev, &sc->init_def); 439 sc->attach_done = true; 440 } 441 442 static int 443 regfix_probe(device_t dev) 444 { 445 446 if (!ofw_bus_status_okay(dev)) 447 return (ENXIO); 448 449 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 450 return (ENXIO); 451 452 device_set_desc(dev, "Fixed Regulator"); 453 return (BUS_PROBE_DEFAULT); 454 } 455 456 static int 457 regfix_detach(device_t dev) 458 { 459 460 /* This device is always present. */ 461 return (EBUSY); 462 } 463 464 static int 465 regfix_attach(device_t dev) 466 { 467 struct regfix_softc * sc; 468 int rv; 469 470 sc = device_get_softc(dev); 471 sc->dev = dev; 472 473 /* Parse FDT data. */ 474 rv = regfix_parse_fdt(sc); 475 if (rv != 0) 476 return(ENXIO); 477 478 /* Fill reset of init. */ 479 sc->init_def.reg_init_def.id = 1; 480 sc->init_def.reg_init_def.flags = REGULATOR_FLAGS_STATIC; 481 482 /* Try to get and configure GPIO. */ 483 rv = regfix_get_gpio(sc); 484 if (rv != 0) { 485 bus_attach_children(dev); 486 return (0); 487 } 488 489 /* Register regulator. */ 490 regnode_fixed_register(sc->dev, &sc->init_def); 491 sc->attach_done = true; 492 493 bus_attach_children(dev); 494 return (0); 495 } 496 497 static device_method_t regfix_methods[] = { 498 /* Device interface */ 499 DEVMETHOD(device_probe, regfix_probe), 500 DEVMETHOD(device_attach, regfix_attach), 501 DEVMETHOD(device_detach, regfix_detach), 502 /* Bus interface */ 503 DEVMETHOD(bus_new_pass, regfix_new_pass), 504 /* Regdev interface */ 505 DEVMETHOD(regdev_map, regdev_default_ofw_map), 506 507 DEVMETHOD_END 508 }; 509 510 DEFINE_CLASS_0(regfix, regfix_driver, regfix_methods, 511 sizeof(struct regfix_softc)); 512 EARLY_DRIVER_MODULE(regfix, simplebus, regfix_driver, 0, 0, BUS_PASS_BUS); 513 514 #endif /* FDT */ 515