1 /* $NetBSD: gpioregulator.c,v 1.1 2017/08/13 18:27:31 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: gpioregulator.c,v 1.1 2017/08/13 18:27:31 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 #include <sys/kmem.h> 36 #include <sys/bus.h> 37 #include <sys/gpio.h> 38 39 #include <dev/fdt/fdtvar.h> 40 41 static int gpioregulator_match(device_t, cfdata_t, void *); 42 static void gpioregulator_attach(device_t, device_t, void *); 43 44 static int gpioregulator_acquire(device_t); 45 static void gpioregulator_release(device_t); 46 static int gpioregulator_enable(device_t, bool); 47 static int gpioregulator_set_voltage(device_t, u_int, u_int); 48 static int gpioregulator_get_voltage(device_t, u_int *); 49 50 static const struct fdtbus_regulator_controller_func gpioregulator_funcs = { 51 .acquire = gpioregulator_acquire, 52 .release = gpioregulator_release, 53 .enable = gpioregulator_enable, 54 .set_voltage = gpioregulator_set_voltage, 55 .get_voltage = gpioregulator_get_voltage, 56 }; 57 58 struct gpioregulator_state { 59 u_int st_val; 60 u_int st_mask; 61 }; 62 63 struct gpioregulator_softc { 64 device_t sc_dev; 65 int sc_phandle; 66 67 struct fdtbus_gpio_pin *sc_pin_enable; 68 69 struct fdtbus_gpio_pin **sc_pins; 70 u_int sc_npins; 71 72 struct gpioregulator_state *sc_states; 73 u_int sc_nstates; 74 75 bool sc_always_on; 76 bool sc_boot_on; 77 bool sc_enable_val; 78 uint32_t sc_delay; 79 80 int sc_gpioflags; 81 }; 82 83 CFATTACH_DECL_NEW(gregulator, sizeof(struct gpioregulator_softc), 84 gpioregulator_match, gpioregulator_attach, NULL, NULL); 85 86 static int 87 gpioregulator_match(device_t parent, cfdata_t cf, void *aux) 88 { 89 const char * const compatible[] = { "regulator-gpio", NULL }; 90 const struct fdt_attach_args *faa = aux; 91 92 return of_match_compatible(faa->faa_phandle, compatible); 93 } 94 95 static void 96 gpioregulator_attach(device_t parent, device_t self, void *aux) 97 { 98 struct gpioregulator_softc * const sc = device_private(self); 99 const struct fdt_attach_args *faa = aux; 100 const int phandle = faa->faa_phandle; 101 const uint32_t *pstates; 102 uint32_t mask; 103 char *name; 104 int len, n; 105 106 sc->sc_dev = self; 107 sc->sc_phandle = phandle; 108 109 aprint_naive("\n"); 110 111 len = OF_getproplen(phandle, "regulator-name"); 112 if (len > 0) { 113 name = kmem_zalloc(len, KM_SLEEP); 114 if (OF_getprop(phandle, "regulator-name", name, len) == len) { 115 aprint_normal(": %s\n", name); 116 } else { 117 aprint_normal("\n"); 118 } 119 kmem_free(name, len); 120 } else { 121 aprint_normal("\n"); 122 } 123 124 pstates = fdtbus_get_prop(phandle, "states", &len); 125 if (pstates == NULL || len < 8 || len % 8 != 0) { 126 aprint_error_dev(self, "invalid 'states' property\n"); 127 return; 128 } 129 130 mask = 0; 131 sc->sc_nstates = len / (sizeof(uint32_t) * 2); 132 sc->sc_states = kmem_zalloc( 133 sc->sc_nstates * sizeof(struct gpioregulator_state), KM_SLEEP); 134 for (n = 0; n < sc->sc_nstates; n++) { 135 sc->sc_states[n].st_val = be32toh(pstates[n * 2 + 0]); 136 sc->sc_states[n].st_mask = be32toh(pstates[n * 2 + 1]); 137 mask |= sc->sc_states[n].st_mask; 138 } 139 140 sc->sc_gpioflags = GPIO_PIN_OUTPUT; 141 if (of_getprop_bool(phandle, "gpio-open-drain")) 142 sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN; 143 144 sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on"); 145 sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on"); 146 sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high"); 147 if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0) 148 sc->sc_delay = 0; 149 150 /* "enable-gpio" property (optional) */ 151 sc->sc_pin_enable = fdtbus_gpio_acquire(phandle, "enable-gpio", 152 sc->sc_gpioflags); 153 154 /* "gpios" property */ 155 sc->sc_npins = 32 - __builtin_clz(mask); 156 sc->sc_pins = kmem_zalloc(sc->sc_npins * sizeof(sc->sc_pins), KM_SLEEP); 157 for (n = 0; n < sc->sc_npins; n++) { 158 sc->sc_pins[n] = fdtbus_gpio_acquire_index(phandle, "gpios", 159 n, sc->sc_gpioflags); 160 if (sc->sc_pins[n] == NULL) { 161 aprint_error_dev(self, "cannot get pin %d\n", n); 162 return; 163 } 164 } 165 166 fdtbus_register_regulator_controller(self, phandle, 167 &gpioregulator_funcs); 168 169 /* 170 * If the regulator is flagged as always on or enabled at boot, 171 * ensure that it is enabled 172 */ 173 if (sc->sc_always_on || sc->sc_boot_on) 174 gpioregulator_enable(self, true); 175 } 176 177 static int 178 gpioregulator_acquire(device_t dev) 179 { 180 return 0; 181 } 182 183 static void 184 gpioregulator_release(device_t dev) 185 { 186 } 187 188 static int 189 gpioregulator_enable(device_t dev, bool enable) 190 { 191 struct gpioregulator_softc * const sc = device_private(dev); 192 193 if (enable) { 194 if (sc->sc_pin_enable != NULL) 195 fdtbus_gpio_write_raw(sc->sc_pin_enable, sc->sc_enable_val); 196 if (sc->sc_delay > 0) 197 delay(sc->sc_delay); 198 } else { 199 if (sc->sc_always_on) 200 return EIO; 201 fdtbus_gpio_write_raw(sc->sc_pin_enable, !sc->sc_enable_val); 202 } 203 return 0; 204 } 205 206 static int 207 gpioregulator_set_voltage(device_t dev, u_int min_uvolt, u_int max_uvolt) 208 { 209 struct gpioregulator_softc * const sc = device_private(dev); 210 const struct gpioregulator_state *state = NULL; 211 int n; 212 213 for (n = 0; n < sc->sc_nstates; n++) 214 if (sc->sc_states[n].st_val >= min_uvolt && 215 sc->sc_states[n].st_val <= max_uvolt) { 216 state = &sc->sc_states[n]; 217 break; 218 } 219 if (state == NULL) 220 return EINVAL; 221 222 for (n = 0; n < sc->sc_npins; n++) 223 fdtbus_gpio_write(sc->sc_pins[n], (state->st_mask >> n) & 1); 224 225 if (sc->sc_delay > 0) 226 delay(sc->sc_delay); 227 228 return 0; 229 } 230 231 static int 232 gpioregulator_get_voltage(device_t dev, u_int *puvolt) 233 { 234 struct gpioregulator_softc * const sc = device_private(dev); 235 uint32_t mask = 0; 236 int n, val; 237 238 for (n = 0; n < sc->sc_npins; n++) { 239 val = fdtbus_gpio_read(sc->sc_pins[n]); 240 mask |= (val << n); 241 } 242 243 for (n = 0; n < sc->sc_nstates; n++) 244 if (sc->sc_states[n].st_mask == mask) { 245 *puvolt = sc->sc_states[n].st_val; 246 return 0; 247 } 248 249 return EIO; 250 } 251