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