1*37b4dc38Sjmcneill /* $NetBSD: fixedregulator.c,v 1.2 2015/12/16 19:33:55 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*37b4dc38Sjmcneill __KERNEL_RCSID(0, "$NetBSD: fixedregulator.c,v 1.2 2015/12/16 19:33:55 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; 60e2ae05f0Sjmcneill bool sc_enable_val; 61e2ae05f0Sjmcneill }; 62e2ae05f0Sjmcneill 63e2ae05f0Sjmcneill CFATTACH_DECL_NEW(fregulator, sizeof(struct fixedregulator_softc), 64e2ae05f0Sjmcneill fixedregulator_match, fixedregulator_attach, NULL, NULL); 65e2ae05f0Sjmcneill 66e2ae05f0Sjmcneill static int 67e2ae05f0Sjmcneill fixedregulator_match(device_t parent, cfdata_t cf, void *aux) 68e2ae05f0Sjmcneill { 69e2ae05f0Sjmcneill const char * const compatible[] = { "regulator-fixed", NULL }; 70e2ae05f0Sjmcneill const struct fdt_attach_args *faa = aux; 71e2ae05f0Sjmcneill 72e2ae05f0Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 73e2ae05f0Sjmcneill } 74e2ae05f0Sjmcneill 75e2ae05f0Sjmcneill static void 76e2ae05f0Sjmcneill fixedregulator_attach(device_t parent, device_t self, void *aux) 77e2ae05f0Sjmcneill { 78e2ae05f0Sjmcneill struct fixedregulator_softc * const sc = device_private(self); 79e2ae05f0Sjmcneill const struct fdt_attach_args *faa = aux; 80e2ae05f0Sjmcneill const int phandle = faa->faa_phandle; 81e2ae05f0Sjmcneill char *name; 82e2ae05f0Sjmcneill int len, gpioflags; 83e2ae05f0Sjmcneill 84e2ae05f0Sjmcneill sc->sc_dev = self; 85e2ae05f0Sjmcneill sc->sc_phandle = phandle; 86e2ae05f0Sjmcneill 87e2ae05f0Sjmcneill aprint_naive("\n"); 88e2ae05f0Sjmcneill 89e2ae05f0Sjmcneill len = OF_getproplen(phandle, "regulator-name"); 90e2ae05f0Sjmcneill if (len > 0) { 91e2ae05f0Sjmcneill name = kmem_zalloc(len, KM_SLEEP); 92e2ae05f0Sjmcneill if (OF_getprop(phandle, "regulator-name", name, len) == len) { 93e2ae05f0Sjmcneill aprint_normal(": %s\n", name); 94e2ae05f0Sjmcneill } else { 95e2ae05f0Sjmcneill aprint_normal("\n"); 96e2ae05f0Sjmcneill } 97e2ae05f0Sjmcneill kmem_free(name, len); 98e2ae05f0Sjmcneill } else { 99e2ae05f0Sjmcneill aprint_normal("\n"); 100e2ae05f0Sjmcneill } 101e2ae05f0Sjmcneill 102e2ae05f0Sjmcneill gpioflags = GPIO_PIN_OUTPUT; 103*37b4dc38Sjmcneill if (of_getprop_bool(phandle, "gpio-open-drain")) 104e2ae05f0Sjmcneill gpioflags |= GPIO_PIN_OPENDRAIN; 105e2ae05f0Sjmcneill 106*37b4dc38Sjmcneill sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on"); 107e2ae05f0Sjmcneill sc->sc_pin = fdtbus_gpio_acquire(phandle, "gpio", gpioflags); 108*37b4dc38Sjmcneill if (sc->sc_pin == NULL) 109e2ae05f0Sjmcneill sc->sc_always_on = true; 110*37b4dc38Sjmcneill sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high"); 111e2ae05f0Sjmcneill 112e2ae05f0Sjmcneill fdtbus_register_regulator_controller(self, phandle, 113e2ae05f0Sjmcneill &fixedregulator_funcs); 114e2ae05f0Sjmcneill } 115e2ae05f0Sjmcneill 116e2ae05f0Sjmcneill static int 117e2ae05f0Sjmcneill fixedregulator_acquire(device_t dev) 118e2ae05f0Sjmcneill { 119e2ae05f0Sjmcneill return 0; 120e2ae05f0Sjmcneill } 121e2ae05f0Sjmcneill 122e2ae05f0Sjmcneill static void 123e2ae05f0Sjmcneill fixedregulator_release(device_t dev) 124e2ae05f0Sjmcneill { 125e2ae05f0Sjmcneill } 126e2ae05f0Sjmcneill 127e2ae05f0Sjmcneill static int 128e2ae05f0Sjmcneill fixedregulator_enable(device_t dev, bool enable) 129e2ae05f0Sjmcneill { 130e2ae05f0Sjmcneill struct fixedregulator_softc * const sc = device_private(dev); 131e2ae05f0Sjmcneill 132e2ae05f0Sjmcneill if (enable) { 133e2ae05f0Sjmcneill if (sc->sc_always_on) { 134e2ae05f0Sjmcneill return 0; 135e2ae05f0Sjmcneill } else { 136e2ae05f0Sjmcneill fdtbus_gpio_write(sc->sc_pin, sc->sc_enable_val); 137e2ae05f0Sjmcneill return 0; 138e2ae05f0Sjmcneill } 139e2ae05f0Sjmcneill } else { 140e2ae05f0Sjmcneill if (sc->sc_always_on) { 141e2ae05f0Sjmcneill return EIO; 142e2ae05f0Sjmcneill } else { 143e2ae05f0Sjmcneill fdtbus_gpio_write(sc->sc_pin, !sc->sc_enable_val); 144e2ae05f0Sjmcneill return 0; 145e2ae05f0Sjmcneill } 146e2ae05f0Sjmcneill } 147e2ae05f0Sjmcneill } 148