xref: /netbsd-src/sys/dev/fdt/fixedregulator.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: fixedregulator.c,v 1.10 2021/01/27 03:10:21 thorpej 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*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: fixedregulator.c,v 1.10 2021/01/27 03:10:21 thorpej 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);
47ebac5eb9Sjmcneill static int	fixedregulator_set_voltage(device_t, u_int, u_int);
48ebac5eb9Sjmcneill static int	fixedregulator_get_voltage(device_t, u_int *);
49e2ae05f0Sjmcneill 
50e2ae05f0Sjmcneill struct fdtbus_regulator_controller_func fixedregulator_funcs = {
51e2ae05f0Sjmcneill 	.acquire = fixedregulator_acquire,
52e2ae05f0Sjmcneill 	.release = fixedregulator_release,
53ebac5eb9Sjmcneill 	.enable = fixedregulator_enable,
54ebac5eb9Sjmcneill 	.set_voltage = fixedregulator_set_voltage,
55ebac5eb9Sjmcneill 	.get_voltage = fixedregulator_get_voltage,
56e2ae05f0Sjmcneill };
57e2ae05f0Sjmcneill 
58e2ae05f0Sjmcneill struct fixedregulator_softc {
59e2ae05f0Sjmcneill 	device_t	sc_dev;
60e2ae05f0Sjmcneill 	int		sc_phandle;
61e2ae05f0Sjmcneill 
62e2ae05f0Sjmcneill 	struct fdtbus_gpio_pin *sc_pin;
63e2ae05f0Sjmcneill 	bool		sc_always_on;
6475e20526Sjmcneill 	bool		sc_boot_on;
65e2ae05f0Sjmcneill 	bool		sc_enable_val;
6675e20526Sjmcneill 	uint32_t	sc_delay;
67ebac5eb9Sjmcneill 	uint32_t	sc_min_uvol;
68ebac5eb9Sjmcneill 	uint32_t	sc_max_uvol;
69db3334b1Sjmcneill 
70db3334b1Sjmcneill 	int		sc_gpioflags;
71db3334b1Sjmcneill 	bool		sc_deferred;
72e2ae05f0Sjmcneill };
73e2ae05f0Sjmcneill 
74e2ae05f0Sjmcneill CFATTACH_DECL_NEW(fregulator, sizeof(struct fixedregulator_softc),
75e2ae05f0Sjmcneill     fixedregulator_match, fixedregulator_attach, NULL, NULL);
76e2ae05f0Sjmcneill 
77*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
78*6e54367aSthorpej 	{ .compat = "regulator-fixed" },
79*6e54367aSthorpej 	DEVICE_COMPAT_EOL
80*6e54367aSthorpej };
81*6e54367aSthorpej 
82e2ae05f0Sjmcneill static int
fixedregulator_match(device_t parent,cfdata_t cf,void * aux)83e2ae05f0Sjmcneill fixedregulator_match(device_t parent, cfdata_t cf, void *aux)
84e2ae05f0Sjmcneill {
85e2ae05f0Sjmcneill 	const struct fdt_attach_args *faa = aux;
86e2ae05f0Sjmcneill 
87*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
88e2ae05f0Sjmcneill }
89e2ae05f0Sjmcneill 
90e2ae05f0Sjmcneill static void
fixedregulator_attach(device_t parent,device_t self,void * aux)91e2ae05f0Sjmcneill fixedregulator_attach(device_t parent, device_t self, void *aux)
92e2ae05f0Sjmcneill {
93e2ae05f0Sjmcneill 	struct fixedregulator_softc * const sc = device_private(self);
94e2ae05f0Sjmcneill 	const struct fdt_attach_args *faa = aux;
95e2ae05f0Sjmcneill 	const int phandle = faa->faa_phandle;
96e2ae05f0Sjmcneill 	char *name;
97db3334b1Sjmcneill 	int len;
98e2ae05f0Sjmcneill 
99e2ae05f0Sjmcneill 	sc->sc_dev = self;
100e2ae05f0Sjmcneill 	sc->sc_phandle = phandle;
101e2ae05f0Sjmcneill 
102e2ae05f0Sjmcneill 	aprint_naive("\n");
103e2ae05f0Sjmcneill 
104e2ae05f0Sjmcneill 	len = OF_getproplen(phandle, "regulator-name");
105e2ae05f0Sjmcneill 	if (len > 0) {
106e2ae05f0Sjmcneill 		name = kmem_zalloc(len, KM_SLEEP);
107e2ae05f0Sjmcneill 		if (OF_getprop(phandle, "regulator-name", name, len) == len) {
108e2ae05f0Sjmcneill 			aprint_normal(": %s\n", name);
109e2ae05f0Sjmcneill 		} else {
110e2ae05f0Sjmcneill 			aprint_normal("\n");
111e2ae05f0Sjmcneill 		}
112e2ae05f0Sjmcneill 		kmem_free(name, len);
113e2ae05f0Sjmcneill 	} else {
114e2ae05f0Sjmcneill 		aprint_normal("\n");
115e2ae05f0Sjmcneill 	}
116e2ae05f0Sjmcneill 
117db3334b1Sjmcneill 	sc->sc_gpioflags = GPIO_PIN_OUTPUT;
11837b4dc38Sjmcneill 	if (of_getprop_bool(phandle, "gpio-open-drain"))
119db3334b1Sjmcneill 		sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN;
120e2ae05f0Sjmcneill 
12137b4dc38Sjmcneill 	sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
12275e20526Sjmcneill 	sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
12375e20526Sjmcneill 	sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
12475e20526Sjmcneill 	if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0)
12575e20526Sjmcneill 		sc->sc_delay = 0;
126ebac5eb9Sjmcneill 	of_getprop_uint32(phandle, "regulator-min-microvolt", &sc->sc_min_uvol);
127ebac5eb9Sjmcneill 	of_getprop_uint32(phandle, "regulator-max-microvolt", &sc->sc_max_uvol);
12875e20526Sjmcneill 
129db3334b1Sjmcneill 	sc->sc_pin = fdtbus_gpio_acquire(phandle, "gpio", sc->sc_gpioflags);
13037b4dc38Sjmcneill 	if (sc->sc_pin == NULL)
131db3334b1Sjmcneill 		sc->sc_always_on = OF_getproplen(phandle, "gpio") <= 0;
132e2ae05f0Sjmcneill 
133e2ae05f0Sjmcneill 	fdtbus_register_regulator_controller(self, phandle,
134e2ae05f0Sjmcneill 	    &fixedregulator_funcs);
13575e20526Sjmcneill 
13675e20526Sjmcneill 	/*
13775e20526Sjmcneill 	 * If the regulator is flagged as always on or enabled at boot,
13875e20526Sjmcneill 	 * ensure that it is enabled
13975e20526Sjmcneill 	 */
14075e20526Sjmcneill 	if (sc->sc_always_on || sc->sc_boot_on)
14175e20526Sjmcneill 		fixedregulator_enable(self, true);
142e2ae05f0Sjmcneill }
143e2ae05f0Sjmcneill 
144e2ae05f0Sjmcneill static int
fixedregulator_acquire(device_t dev)145e2ae05f0Sjmcneill fixedregulator_acquire(device_t dev)
146e2ae05f0Sjmcneill {
147db3334b1Sjmcneill 	struct fixedregulator_softc * const sc = device_private(dev);
148db3334b1Sjmcneill 
149db3334b1Sjmcneill 	if (sc->sc_pin == NULL && !sc->sc_always_on) {
150db3334b1Sjmcneill 		sc->sc_pin = fdtbus_gpio_acquire(sc->sc_phandle, "gpio",
151db3334b1Sjmcneill 		    sc->sc_gpioflags);
152db3334b1Sjmcneill 		if (sc->sc_pin == NULL)
153db3334b1Sjmcneill 			return ENXIO;
154db3334b1Sjmcneill 	}
155db3334b1Sjmcneill 
156e2ae05f0Sjmcneill 	return 0;
157e2ae05f0Sjmcneill }
158e2ae05f0Sjmcneill 
159e2ae05f0Sjmcneill static void
fixedregulator_release(device_t dev)160e2ae05f0Sjmcneill fixedregulator_release(device_t dev)
161e2ae05f0Sjmcneill {
162e2ae05f0Sjmcneill }
163e2ae05f0Sjmcneill 
164e2ae05f0Sjmcneill static int
fixedregulator_enable(device_t dev,bool enable)165e2ae05f0Sjmcneill fixedregulator_enable(device_t dev, bool enable)
166e2ae05f0Sjmcneill {
167e2ae05f0Sjmcneill 	struct fixedregulator_softc * const sc = device_private(dev);
168e2ae05f0Sjmcneill 
169e2ae05f0Sjmcneill 	if (enable) {
17075e20526Sjmcneill 		if (sc->sc_pin != NULL)
171a35fbd12Sjmcneill 			fdtbus_gpio_write_raw(sc->sc_pin, sc->sc_enable_val);
17275e20526Sjmcneill 		if (sc->sc_delay > 0)
17375e20526Sjmcneill 			delay(sc->sc_delay);
174e2ae05f0Sjmcneill 	} else {
17575e20526Sjmcneill 		if (sc->sc_always_on)
176e2ae05f0Sjmcneill 			return EIO;
177a35fbd12Sjmcneill 		fdtbus_gpio_write_raw(sc->sc_pin, !sc->sc_enable_val);
17875e20526Sjmcneill 	}
179e2ae05f0Sjmcneill 	return 0;
180e2ae05f0Sjmcneill }
181ebac5eb9Sjmcneill 
182ebac5eb9Sjmcneill static int
fixedregulator_set_voltage(device_t dev,u_int min_uvol,u_int max_uvol)183ebac5eb9Sjmcneill fixedregulator_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
184ebac5eb9Sjmcneill {
185ebac5eb9Sjmcneill 	struct fixedregulator_softc * const sc = device_private(dev);
186ebac5eb9Sjmcneill 
187ebac5eb9Sjmcneill 	if (sc->sc_min_uvol > max_uvol || sc->sc_max_uvol < min_uvol)
188ebac5eb9Sjmcneill 		return EINVAL;
189ebac5eb9Sjmcneill 
190ebac5eb9Sjmcneill 	return 0;
191ebac5eb9Sjmcneill }
192ebac5eb9Sjmcneill 
193ebac5eb9Sjmcneill static int
fixedregulator_get_voltage(device_t dev,u_int * uvol)194ebac5eb9Sjmcneill fixedregulator_get_voltage(device_t dev, u_int *uvol)
195ebac5eb9Sjmcneill {
196ebac5eb9Sjmcneill 	struct fixedregulator_softc * const sc = device_private(dev);
197ebac5eb9Sjmcneill 
198ebac5eb9Sjmcneill 	*uvol = sc->sc_min_uvol;
199ebac5eb9Sjmcneill 
200ebac5eb9Sjmcneill 	return 0;
201ebac5eb9Sjmcneill }
202