Home | History | Annotate | Line # | Download | only in fdt
gpioregulator.c revision 1.1.6.1
      1 /* $NetBSD: gpioregulator.c,v 1.1.6.1 2019/01/26 22:00:06 pgoyette Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) 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.6.1 2019/01/26 22:00:06 pgoyette 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 	u_int gpios_states;
    104 	char *name;
    105 	int len, n;
    106 
    107 	sc->sc_dev = self;
    108 	sc->sc_phandle = phandle;
    109 
    110 	aprint_naive("\n");
    111 
    112 	len = OF_getproplen(phandle, "regulator-name");
    113 	if (len > 0) {
    114 		name = kmem_zalloc(len, KM_SLEEP);
    115 		if (OF_getprop(phandle, "regulator-name", name, len) == len) {
    116 			aprint_normal(": %s\n", name);
    117 		} else {
    118 			aprint_normal("\n");
    119 		}
    120 		kmem_free(name, len);
    121 	} else {
    122 		aprint_normal("\n");
    123 	}
    124 
    125 	pstates = fdtbus_get_prop(phandle, "states", &len);
    126 	if (pstates == NULL || len < 8 || len % 8 != 0) {
    127 		aprint_error_dev(self, "invalid 'states' property\n");
    128 		return;
    129 	}
    130 
    131 	mask = 0;
    132 	sc->sc_nstates = len / (sizeof(uint32_t) * 2);
    133 	sc->sc_states = kmem_zalloc(
    134 	    sc->sc_nstates * sizeof(struct gpioregulator_state), KM_SLEEP);
    135 	for (n = 0; n < sc->sc_nstates; n++) {
    136 		sc->sc_states[n].st_val = be32toh(pstates[n * 2 + 0]);
    137 		sc->sc_states[n].st_mask = be32toh(pstates[n * 2 + 1]);
    138 		mask |= sc->sc_states[n].st_mask;
    139 	}
    140 
    141 	sc->sc_gpioflags = GPIO_PIN_OUTPUT;
    142 	if (of_getprop_bool(phandle, "gpio-open-drain"))
    143 		sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN;
    144 
    145 	sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
    146 	sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
    147 	sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
    148 	if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0)
    149 		sc->sc_delay = 0;
    150 
    151 	/* "enable-gpio" property (optional) */
    152 	sc->sc_pin_enable = fdtbus_gpio_acquire(phandle, "enable-gpio",
    153 	    sc->sc_gpioflags);
    154 
    155 	/* "gpios" property */
    156 	sc->sc_npins = 32 - __builtin_clz(mask);
    157 	sc->sc_pins = kmem_zalloc(sc->sc_npins * sizeof(sc->sc_pins), KM_SLEEP);
    158 	for (n = 0; n < sc->sc_npins; n++) {
    159 		sc->sc_pins[n] = fdtbus_gpio_acquire_index(phandle, "gpios",
    160 		    n, sc->sc_gpioflags);
    161 		if (sc->sc_pins[n] == NULL) {
    162 			aprint_error_dev(self, "cannot get pin %d\n", n);
    163 			return;
    164 		}
    165 	}
    166 
    167 	/* "gpios-states" property */
    168 	if (of_getprop_uint32(phandle, "gpios-states", &gpios_states) != 0)
    169 		gpios_states = 0;
    170 
    171 	/* Set initial state */
    172 	for (n = 0; n < sc->sc_npins; n++)
    173 		fdtbus_gpio_write(sc->sc_pins[n], (gpios_states >> n) & 1);
    174 
    175 	fdtbus_register_regulator_controller(self, phandle,
    176 	    &gpioregulator_funcs);
    177 
    178 	/*
    179 	 * If the regulator is flagged as always on or enabled at boot,
    180 	 * ensure that it is enabled
    181 	 */
    182 	if (sc->sc_always_on || sc->sc_boot_on)
    183 		gpioregulator_enable(self, true);
    184 }
    185 
    186 static int
    187 gpioregulator_acquire(device_t dev)
    188 {
    189 	return 0;
    190 }
    191 
    192 static void
    193 gpioregulator_release(device_t dev)
    194 {
    195 }
    196 
    197 static int
    198 gpioregulator_enable(device_t dev, bool enable)
    199 {
    200 	struct gpioregulator_softc * const sc = device_private(dev);
    201 
    202 	if (enable) {
    203 		if (sc->sc_pin_enable != NULL)
    204 			fdtbus_gpio_write_raw(sc->sc_pin_enable, sc->sc_enable_val);
    205 		if (sc->sc_delay > 0)
    206 			delay(sc->sc_delay);
    207 	} else {
    208 		if (sc->sc_always_on)
    209 			return EIO;
    210 		fdtbus_gpio_write_raw(sc->sc_pin_enable, !sc->sc_enable_val);
    211 	}
    212 	return 0;
    213 }
    214 
    215 static int
    216 gpioregulator_set_voltage(device_t dev, u_int min_uvolt, u_int max_uvolt)
    217 {
    218 	struct gpioregulator_softc * const sc = device_private(dev);
    219 	const struct gpioregulator_state *state = NULL;
    220 	int n;
    221 
    222 	for (n = 0; n < sc->sc_nstates; n++)
    223 		if (sc->sc_states[n].st_val >= min_uvolt &&
    224 		    sc->sc_states[n].st_val <= max_uvolt) {
    225 			state = &sc->sc_states[n];
    226 			break;
    227 		}
    228 	if (state == NULL)
    229 		return EINVAL;
    230 
    231 	for (n = 0; n < sc->sc_npins; n++)
    232 		fdtbus_gpio_write(sc->sc_pins[n], (state->st_mask >> n) & 1);
    233 
    234 	if (sc->sc_delay > 0)
    235 		delay(sc->sc_delay);
    236 
    237 	return 0;
    238 }
    239 
    240 static int
    241 gpioregulator_get_voltage(device_t dev, u_int *puvolt)
    242 {
    243 	struct gpioregulator_softc * const sc = device_private(dev);
    244 	uint32_t mask = 0;
    245 	int n, val;
    246 
    247 	for (n = 0; n < sc->sc_npins; n++) {
    248 		val = fdtbus_gpio_read(sc->sc_pins[n]);
    249 		mask |= (val << n);
    250 	}
    251 
    252 	for (n = 0; n < sc->sc_nstates; n++)
    253 		if (sc->sc_states[n].st_mask == mask) {
    254 			*puvolt = sc->sc_states[n].st_val;
    255 			return 0;
    256 		}
    257 
    258 	return EIO;
    259 }
    260