1*db3334b1Sjmcneill /* $NetBSD: fixedregulator.c,v 1.5 2017/04/24 10:55:26 jmcneill Exp $ */ 2e2ae05f0Sjmcneill 3e2ae05f0Sjmcneill /*- 4e2ae05f0Sjmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5e2ae05f0Sjmcneill * All rights reserved. 6e2ae05f0Sjmcneill * 7e2ae05f0Sjmcneill * Redistribution and use in source and binary forms, with or without 8e2ae05f0Sjmcneill * modification, are permitted provided that the following conditions 9e2ae05f0Sjmcneill * are met: 10e2ae05f0Sjmcneill * 1. Redistributions of source code must retain the above copyright 11e2ae05f0Sjmcneill * notice, this list of conditions and the following disclaimer. 12e2ae05f0Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 13e2ae05f0Sjmcneill * notice, this list of conditions and the following disclaimer in the 14e2ae05f0Sjmcneill * documentation and/or other materials provided with the distribution. 15e2ae05f0Sjmcneill * 16e2ae05f0Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17e2ae05f0Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18e2ae05f0Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19e2ae05f0Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20e2ae05f0Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21e2ae05f0Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22e2ae05f0Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23e2ae05f0Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24e2ae05f0Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25e2ae05f0Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26e2ae05f0Sjmcneill * SUCH DAMAGE. 27e2ae05f0Sjmcneill */ 28e2ae05f0Sjmcneill 29e2ae05f0Sjmcneill #include <sys/cdefs.h> 30*db3334b1Sjmcneill __KERNEL_RCSID(0, "$NetBSD: fixedregulator.c,v 1.5 2017/04/24 10:55:26 jmcneill Exp $"); 31e2ae05f0Sjmcneill 32e2ae05f0Sjmcneill #include <sys/param.h> 33e2ae05f0Sjmcneill #include <sys/systm.h> 34e2ae05f0Sjmcneill #include <sys/device.h> 35e2ae05f0Sjmcneill #include <sys/kmem.h> 36e2ae05f0Sjmcneill #include <sys/bus.h> 37e2ae05f0Sjmcneill #include <sys/gpio.h> 38e2ae05f0Sjmcneill 39e2ae05f0Sjmcneill #include <dev/fdt/fdtvar.h> 40e2ae05f0Sjmcneill 41e2ae05f0Sjmcneill static int fixedregulator_match(device_t, cfdata_t, void *); 42e2ae05f0Sjmcneill static void fixedregulator_attach(device_t, device_t, void *); 43e2ae05f0Sjmcneill 44e2ae05f0Sjmcneill static int fixedregulator_acquire(device_t); 45e2ae05f0Sjmcneill static void fixedregulator_release(device_t); 46e2ae05f0Sjmcneill static int fixedregulator_enable(device_t, bool); 47e2ae05f0Sjmcneill 48e2ae05f0Sjmcneill struct fdtbus_regulator_controller_func fixedregulator_funcs = { 49e2ae05f0Sjmcneill .acquire = fixedregulator_acquire, 50e2ae05f0Sjmcneill .release = fixedregulator_release, 51e2ae05f0Sjmcneill .enable = fixedregulator_enable 52e2ae05f0Sjmcneill }; 53e2ae05f0Sjmcneill 54e2ae05f0Sjmcneill struct fixedregulator_softc { 55e2ae05f0Sjmcneill device_t sc_dev; 56e2ae05f0Sjmcneill int sc_phandle; 57e2ae05f0Sjmcneill 58e2ae05f0Sjmcneill struct fdtbus_gpio_pin *sc_pin; 59e2ae05f0Sjmcneill bool sc_always_on; 6075e20526Sjmcneill bool sc_boot_on; 61e2ae05f0Sjmcneill bool sc_enable_val; 6275e20526Sjmcneill uint32_t sc_delay; 63*db3334b1Sjmcneill 64*db3334b1Sjmcneill int sc_gpioflags; 65*db3334b1Sjmcneill bool sc_deferred; 66e2ae05f0Sjmcneill }; 67e2ae05f0Sjmcneill 68e2ae05f0Sjmcneill CFATTACH_DECL_NEW(fregulator, sizeof(struct fixedregulator_softc), 69e2ae05f0Sjmcneill fixedregulator_match, fixedregulator_attach, NULL, NULL); 70e2ae05f0Sjmcneill 71e2ae05f0Sjmcneill static int 72e2ae05f0Sjmcneill fixedregulator_match(device_t parent, cfdata_t cf, void *aux) 73e2ae05f0Sjmcneill { 74e2ae05f0Sjmcneill const char * const compatible[] = { "regulator-fixed", NULL }; 75e2ae05f0Sjmcneill const struct fdt_attach_args *faa = aux; 76e2ae05f0Sjmcneill 77e2ae05f0Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 78e2ae05f0Sjmcneill } 79e2ae05f0Sjmcneill 80e2ae05f0Sjmcneill static void 81e2ae05f0Sjmcneill fixedregulator_attach(device_t parent, device_t self, void *aux) 82e2ae05f0Sjmcneill { 83e2ae05f0Sjmcneill struct fixedregulator_softc * const sc = device_private(self); 84e2ae05f0Sjmcneill const struct fdt_attach_args *faa = aux; 85e2ae05f0Sjmcneill const int phandle = faa->faa_phandle; 86e2ae05f0Sjmcneill char *name; 87*db3334b1Sjmcneill int len; 88e2ae05f0Sjmcneill 89e2ae05f0Sjmcneill sc->sc_dev = self; 90e2ae05f0Sjmcneill sc->sc_phandle = phandle; 91e2ae05f0Sjmcneill 92e2ae05f0Sjmcneill aprint_naive("\n"); 93e2ae05f0Sjmcneill 94e2ae05f0Sjmcneill len = OF_getproplen(phandle, "regulator-name"); 95e2ae05f0Sjmcneill if (len > 0) { 96e2ae05f0Sjmcneill name = kmem_zalloc(len, KM_SLEEP); 97e2ae05f0Sjmcneill if (OF_getprop(phandle, "regulator-name", name, len) == len) { 98e2ae05f0Sjmcneill aprint_normal(": %s\n", name); 99e2ae05f0Sjmcneill } else { 100e2ae05f0Sjmcneill aprint_normal("\n"); 101e2ae05f0Sjmcneill } 102e2ae05f0Sjmcneill kmem_free(name, len); 103e2ae05f0Sjmcneill } else { 104e2ae05f0Sjmcneill aprint_normal("\n"); 105e2ae05f0Sjmcneill } 106e2ae05f0Sjmcneill 107*db3334b1Sjmcneill sc->sc_gpioflags = GPIO_PIN_OUTPUT; 10837b4dc38Sjmcneill if (of_getprop_bool(phandle, "gpio-open-drain")) 109*db3334b1Sjmcneill sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN; 110e2ae05f0Sjmcneill 11137b4dc38Sjmcneill sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on"); 11275e20526Sjmcneill sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on"); 11375e20526Sjmcneill sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high"); 11475e20526Sjmcneill if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0) 11575e20526Sjmcneill sc->sc_delay = 0; 11675e20526Sjmcneill 117*db3334b1Sjmcneill sc->sc_pin = fdtbus_gpio_acquire(phandle, "gpio", sc->sc_gpioflags); 11837b4dc38Sjmcneill if (sc->sc_pin == NULL) 119*db3334b1Sjmcneill sc->sc_always_on = OF_getproplen(phandle, "gpio") <= 0; 120e2ae05f0Sjmcneill 121e2ae05f0Sjmcneill fdtbus_register_regulator_controller(self, phandle, 122e2ae05f0Sjmcneill &fixedregulator_funcs); 12375e20526Sjmcneill 12475e20526Sjmcneill /* 12575e20526Sjmcneill * If the regulator is flagged as always on or enabled at boot, 12675e20526Sjmcneill * ensure that it is enabled 12775e20526Sjmcneill */ 12875e20526Sjmcneill if (sc->sc_always_on || sc->sc_boot_on) 12975e20526Sjmcneill fixedregulator_enable(self, true); 130e2ae05f0Sjmcneill } 131e2ae05f0Sjmcneill 132e2ae05f0Sjmcneill static int 133e2ae05f0Sjmcneill fixedregulator_acquire(device_t dev) 134e2ae05f0Sjmcneill { 135*db3334b1Sjmcneill struct fixedregulator_softc * const sc = device_private(dev); 136*db3334b1Sjmcneill 137*db3334b1Sjmcneill if (sc->sc_pin == NULL && !sc->sc_always_on) { 138*db3334b1Sjmcneill sc->sc_pin = fdtbus_gpio_acquire(sc->sc_phandle, "gpio", 139*db3334b1Sjmcneill sc->sc_gpioflags); 140*db3334b1Sjmcneill if (sc->sc_pin == NULL) 141*db3334b1Sjmcneill return ENXIO; 142*db3334b1Sjmcneill } 143*db3334b1Sjmcneill 144e2ae05f0Sjmcneill return 0; 145e2ae05f0Sjmcneill } 146e2ae05f0Sjmcneill 147e2ae05f0Sjmcneill static void 148e2ae05f0Sjmcneill fixedregulator_release(device_t dev) 149e2ae05f0Sjmcneill { 150e2ae05f0Sjmcneill } 151e2ae05f0Sjmcneill 152e2ae05f0Sjmcneill static int 153e2ae05f0Sjmcneill fixedregulator_enable(device_t dev, bool enable) 154e2ae05f0Sjmcneill { 155e2ae05f0Sjmcneill struct fixedregulator_softc * const sc = device_private(dev); 156e2ae05f0Sjmcneill 157e2ae05f0Sjmcneill if (enable) { 15875e20526Sjmcneill if (sc->sc_pin != NULL) 1591a623fc2Sjmcneill fdtbus_gpio_write_raw(sc->sc_pin, sc->sc_enable_val); 16075e20526Sjmcneill if (sc->sc_delay > 0) 16175e20526Sjmcneill delay(sc->sc_delay); 162e2ae05f0Sjmcneill } else { 16375e20526Sjmcneill if (sc->sc_always_on) 164e2ae05f0Sjmcneill return EIO; 1651a623fc2Sjmcneill fdtbus_gpio_write_raw(sc->sc_pin, !sc->sc_enable_val); 16675e20526Sjmcneill } 167e2ae05f0Sjmcneill return 0; 168e2ae05f0Sjmcneill } 169