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