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