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