xref: /netbsd-src/sys/dev/fdt/fixedregulator.c (revision ed75d7a867996c84cfa88e3b8906816277e957f7)
1 /* $NetBSD: fixedregulator.c,v 1.8 2019/05/23 21:36:26 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. 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: fixedregulator.c,v 1.8 2019/05/23 21:36:26 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	fixedregulator_match(device_t, cfdata_t, void *);
42 static void	fixedregulator_attach(device_t, device_t, void *);
43 
44 static int	fixedregulator_acquire(device_t);
45 static void 	fixedregulator_release(device_t);
46 static int	fixedregulator_enable(device_t, bool);
47 static int	fixedregulator_set_voltage(device_t, u_int, u_int);
48 static int	fixedregulator_get_voltage(device_t, u_int *);
49 
50 struct fdtbus_regulator_controller_func fixedregulator_funcs = {
51 	.acquire = fixedregulator_acquire,
52 	.release = fixedregulator_release,
53 	.enable = fixedregulator_enable,
54 	.set_voltage = fixedregulator_set_voltage,
55 	.get_voltage = fixedregulator_get_voltage,
56 };
57 
58 struct fixedregulator_softc {
59 	device_t	sc_dev;
60 	int		sc_phandle;
61 
62 	struct fdtbus_gpio_pin *sc_pin;
63 	bool		sc_always_on;
64 	bool		sc_boot_on;
65 	bool		sc_enable_val;
66 	uint32_t	sc_delay;
67 	uint32_t	sc_min_uvol;
68 	uint32_t	sc_max_uvol;
69 
70 	int		sc_gpioflags;
71 	bool		sc_deferred;
72 };
73 
74 CFATTACH_DECL_NEW(fregulator, sizeof(struct fixedregulator_softc),
75     fixedregulator_match, fixedregulator_attach, NULL, NULL);
76 
77 static int
78 fixedregulator_match(device_t parent, cfdata_t cf, void *aux)
79 {
80 	const char * const compatible[] = { "regulator-fixed", NULL };
81 	const struct fdt_attach_args *faa = aux;
82 
83 	return of_match_compatible(faa->faa_phandle, compatible);
84 }
85 
86 static void
87 fixedregulator_attach(device_t parent, device_t self, void *aux)
88 {
89 	struct fixedregulator_softc * const sc = device_private(self);
90 	const struct fdt_attach_args *faa = aux;
91 	const int phandle = faa->faa_phandle;
92 	char *name;
93 	int len;
94 
95 	sc->sc_dev = self;
96 	sc->sc_phandle = phandle;
97 
98 	aprint_naive("\n");
99 
100 	len = OF_getproplen(phandle, "regulator-name");
101 	if (len > 0) {
102 		name = kmem_zalloc(len, KM_SLEEP);
103 		if (OF_getprop(phandle, "regulator-name", name, len) == len) {
104 			aprint_normal(": %s\n", name);
105 		} else {
106 			aprint_normal("\n");
107 		}
108 		kmem_free(name, len);
109 	} else {
110 		aprint_normal("\n");
111 	}
112 
113 	sc->sc_gpioflags = GPIO_PIN_OUTPUT;
114 	if (of_getprop_bool(phandle, "gpio-open-drain"))
115 		sc->sc_gpioflags |= GPIO_PIN_OPENDRAIN;
116 
117 	sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
118 	sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
119 	sc->sc_enable_val = of_getprop_bool(phandle, "enable-active-high");
120 	if (of_getprop_uint32(phandle, "startup-delay-us", &sc->sc_delay) != 0)
121 		sc->sc_delay = 0;
122 	of_getprop_uint32(phandle, "regulator-min-microvolt", &sc->sc_min_uvol);
123 	of_getprop_uint32(phandle, "regulator-max-microvolt", &sc->sc_max_uvol);
124 
125 	sc->sc_pin = fdtbus_gpio_acquire(phandle, "gpio", sc->sc_gpioflags);
126 	if (sc->sc_pin == NULL)
127 		sc->sc_always_on = OF_getproplen(phandle, "gpio") <= 0;
128 
129 	fdtbus_register_regulator_controller(self, phandle,
130 	    &fixedregulator_funcs);
131 
132 	/*
133 	 * If the regulator is flagged as always on or enabled at boot,
134 	 * ensure that it is enabled
135 	 */
136 	if (sc->sc_always_on || sc->sc_boot_on)
137 		fixedregulator_enable(self, true);
138 }
139 
140 static int
141 fixedregulator_acquire(device_t dev)
142 {
143 	struct fixedregulator_softc * const sc = device_private(dev);
144 
145 	if (sc->sc_pin == NULL && !sc->sc_always_on) {
146 		sc->sc_pin = fdtbus_gpio_acquire(sc->sc_phandle, "gpio",
147 		    sc->sc_gpioflags);
148 		if (sc->sc_pin == NULL)
149 			return ENXIO;
150 	}
151 
152 	return 0;
153 }
154 
155 static void
156 fixedregulator_release(device_t dev)
157 {
158 }
159 
160 static int
161 fixedregulator_enable(device_t dev, bool enable)
162 {
163 	struct fixedregulator_softc * const sc = device_private(dev);
164 
165 	if (enable) {
166 		if (sc->sc_pin != NULL)
167 			fdtbus_gpio_write_raw(sc->sc_pin, sc->sc_enable_val);
168 		if (sc->sc_delay > 0)
169 			delay(sc->sc_delay);
170 	} else {
171 		if (sc->sc_always_on)
172 			return EIO;
173 		fdtbus_gpio_write_raw(sc->sc_pin, !sc->sc_enable_val);
174 	}
175 	return 0;
176 }
177 
178 static int
179 fixedregulator_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
180 {
181 	struct fixedregulator_softc * const sc = device_private(dev);
182 
183 	if (sc->sc_min_uvol > max_uvol || sc->sc_max_uvol < min_uvol)
184 		return EINVAL;
185 
186 	return 0;
187 }
188 
189 static int
190 fixedregulator_get_voltage(device_t dev, u_int *uvol)
191 {
192 	struct fixedregulator_softc * const sc = device_private(dev);
193 
194 	*uvol = sc->sc_min_uvol;
195 
196 	return 0;
197 }
198