xref: /freebsd/sys/dev/gpio/gpioregulator.c (revision b2f0caf160993c30a0d0e3d52e8a2d03616908cd)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * GPIO controlled regulators
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/rman.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/gpio.h>
37 
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 
41 #include <dev/gpio/gpiobusvar.h>
42 
43 #include <dev/regulator/regulator.h>
44 
45 #include "regdev_if.h"
46 
47 struct gpioregulator_state {
48 	int			val;
49 	uint32_t		mask;
50 };
51 
52 struct gpioregulator_init_def {
53 	struct regnode_init_def		reg_init_def;
54 	struct gpiobus_pin		*enable_pin;
55 	int				enable_pin_valid;
56 	int				startup_delay_us;
57 	int				nstates;
58 	struct gpioregulator_state	*states;
59 	int				npins;
60 	struct gpiobus_pin		**pins;
61 };
62 
63 struct gpioregulator_reg_sc {
64 	struct regnode			*regnode;
65 	device_t			base_dev;
66 	struct regnode_std_param	*param;
67 	struct gpioregulator_init_def	*def;
68 };
69 
70 struct gpioregulator_softc {
71 	device_t			dev;
72 	struct gpioregulator_reg_sc	*reg_sc;
73 	struct gpioregulator_init_def	init_def;
74 };
75 
76 static int
gpioregulator_regnode_init(struct regnode * regnode)77 gpioregulator_regnode_init(struct regnode *regnode)
78 {
79 	struct gpioregulator_reg_sc *sc;
80 	int error, n;
81 
82 	sc = regnode_get_softc(regnode);
83 
84 	if (sc->def->enable_pin_valid == 1) {
85 		error = gpio_pin_setflags(sc->def->enable_pin, GPIO_PIN_OUTPUT);
86 		if (error != 0)
87 			return (error);
88 	}
89 
90 	for (n = 0; n < sc->def->npins; n++) {
91 		error = gpio_pin_setflags(sc->def->pins[n], GPIO_PIN_OUTPUT);
92 		if (error != 0)
93 			return (error);
94 	}
95 
96 	return (0);
97 }
98 
99 static int
gpioregulator_regnode_enable(struct regnode * regnode,bool enable,int * udelay)100 gpioregulator_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
101 {
102 	struct gpioregulator_reg_sc *sc;
103 	bool active;
104 	int error;
105 
106 	sc = regnode_get_softc(regnode);
107 
108 	if (sc->def->enable_pin_valid == 1) {
109 		active = enable;
110 		if (!sc->param->enable_active_high)
111 			active = !active;
112 		error = gpio_pin_set_active(sc->def->enable_pin, active);
113 		if (error != 0)
114 			return (error);
115 	}
116 
117 	*udelay = sc->def->startup_delay_us;
118 
119 	return (0);
120 }
121 
122 static int
gpioregulator_regnode_set_voltage(struct regnode * regnode,int min_uvolt,int max_uvolt,int * udelay)123 gpioregulator_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
124     int max_uvolt, int *udelay)
125 {
126 	struct gpioregulator_reg_sc *sc;
127 	const struct gpioregulator_state *state;
128 	int error, n;
129 
130 	sc = regnode_get_softc(regnode);
131 	state = NULL;
132 
133 	for (n = 0; n < sc->def->nstates; n++) {
134 		if (sc->def->states[n].val >= min_uvolt &&
135 		    sc->def->states[n].val <= max_uvolt) {
136 			state = &sc->def->states[n];
137 			break;
138 		}
139 	}
140 	if (state == NULL)
141 		return (EINVAL);
142 
143 	for (n = 0; n < sc->def->npins; n++) {
144 		error = gpio_pin_set_active(sc->def->pins[n],
145 		    (state->mask >> n) & 1);
146 		if (error != 0)
147 			return (error);
148 	}
149 
150 	*udelay = sc->def->startup_delay_us;
151 
152 	return (0);
153 }
154 
155 static int
gpioregulator_regnode_get_voltage(struct regnode * regnode,int * uvolt)156 gpioregulator_regnode_get_voltage(struct regnode *regnode, int *uvolt)
157 {
158 	struct gpioregulator_reg_sc *sc;
159 	uint32_t mask;
160 	int error, n;
161 	bool active;
162 
163 	sc = regnode_get_softc(regnode);
164 	mask = 0;
165 
166 	for (n = 0; n < sc->def->npins; n++) {
167 		error = gpio_pin_is_active(sc->def->pins[n], &active);
168 		if (error != 0)
169 			return (error);
170 		mask |= (active << n);
171 	}
172 
173 	for (n = 0; n < sc->def->nstates; n++) {
174 		if (sc->def->states[n].mask == mask) {
175 			*uvolt = sc->def->states[n].val;
176 			return (0);
177 		}
178 	}
179 
180 	return (EIO);
181 }
182 
183 static regnode_method_t gpioregulator_regnode_methods[] = {
184 	/* Regulator interface */
185 	REGNODEMETHOD(regnode_init,	gpioregulator_regnode_init),
186 	REGNODEMETHOD(regnode_enable,	gpioregulator_regnode_enable),
187 	REGNODEMETHOD(regnode_set_voltage, gpioregulator_regnode_set_voltage),
188 	REGNODEMETHOD(regnode_get_voltage, gpioregulator_regnode_get_voltage),
189 	REGNODEMETHOD_END
190 };
191 DEFINE_CLASS_1(gpioregulator_regnode, gpioregulator_regnode_class,
192     gpioregulator_regnode_methods, sizeof(struct gpioregulator_reg_sc),
193     regnode_class);
194 
195 static int
gpioregulator_parse_fdt(struct gpioregulator_softc * sc)196 gpioregulator_parse_fdt(struct gpioregulator_softc *sc)
197 {
198 	uint32_t *pstates, mask;
199 	phandle_t node;
200 	ssize_t len;
201 	int error, n;
202 
203 	node = ofw_bus_get_node(sc->dev);
204 	pstates = NULL;
205 	mask = 0;
206 
207 	error = regulator_parse_ofw_stdparam(sc->dev, node,
208 	    &sc->init_def.reg_init_def);
209 	if (error != 0)
210 		return (error);
211 
212 	/* "states" property (required) */
213 	len = OF_getencprop_alloc_multi(node, "states", sizeof(*pstates),
214 	    (void **)&pstates);
215 	if (len < 2) {
216 		device_printf(sc->dev, "invalid 'states' property\n");
217 		error = EINVAL;
218 		goto done;
219 	}
220 	sc->init_def.nstates = len / 2;
221 	sc->init_def.states = malloc(sc->init_def.nstates *
222 	    sizeof(*sc->init_def.states), M_DEVBUF, M_WAITOK);
223 	for (n = 0; n < sc->init_def.nstates; n++) {
224 		sc->init_def.states[n].val = pstates[n * 2 + 0];
225 		sc->init_def.states[n].mask = pstates[n * 2 + 1];
226 		mask |= sc->init_def.states[n].mask;
227 	}
228 
229 	/* "startup-delay-us" property (optional) */
230 	len = OF_getencprop(node, "startup-delay-us",
231 	    &sc->init_def.startup_delay_us,
232 	    sizeof(sc->init_def.startup_delay_us));
233 	if (len <= 0)
234 		sc->init_def.startup_delay_us = 0;
235 
236 	/* "enable-gpio" property (optional) */
237 	error = gpio_pin_get_by_ofw_property(sc->dev, node, "enable-gpio",
238 	    &sc->init_def.enable_pin);
239 	if (error == 0)
240 		sc->init_def.enable_pin_valid = 1;
241 
242 	/* "gpios" property */
243 	sc->init_def.npins = 32 - __builtin_clz(mask);
244 	sc->init_def.pins = malloc(sc->init_def.npins *
245 	    sizeof(sc->init_def.pins), M_DEVBUF, M_WAITOK | M_ZERO);
246 	for (n = 0; n < sc->init_def.npins; n++) {
247 		error = gpio_pin_get_by_ofw_idx(sc->dev, node, n,
248 		    &sc->init_def.pins[n]);
249 		if (error != 0) {
250 			device_printf(sc->dev, "cannot get pin %d\n", n);
251 			goto done;
252 		}
253 	}
254 
255 done:
256 	if (error != 0) {
257 		for (n = 0; n < sc->init_def.npins; n++) {
258 			if (sc->init_def.pins[n] != NULL)
259 				gpio_pin_release(sc->init_def.pins[n]);
260 		}
261 
262 		free(sc->init_def.states, M_DEVBUF);
263 		free(sc->init_def.pins, M_DEVBUF);
264 
265 	}
266 	OF_prop_free(pstates);
267 
268 	return (error);
269 }
270 
271 static int
gpioregulator_probe(device_t dev)272 gpioregulator_probe(device_t dev)
273 {
274 
275 	if (!ofw_bus_is_compatible(dev, "regulator-gpio"))
276 		return (ENXIO);
277 
278 	device_set_desc(dev, "GPIO controlled regulator");
279 	return (BUS_PROBE_GENERIC);
280 }
281 
282 static int
gpioregulator_attach(device_t dev)283 gpioregulator_attach(device_t dev)
284 {
285 	struct gpioregulator_softc *sc;
286 	struct regnode *regnode;
287 	phandle_t node;
288 	int error;
289 
290 	sc = device_get_softc(dev);
291 	sc->dev = dev;
292 	node = ofw_bus_get_node(dev);
293 
294 	error = gpioregulator_parse_fdt(sc);
295 	if (error != 0) {
296 		device_printf(dev, "cannot parse parameters\n");
297 		return (ENXIO);
298 	}
299 	sc->init_def.reg_init_def.id = 1;
300 	sc->init_def.reg_init_def.ofw_node = node;
301 
302 	regnode = regnode_create(dev, &gpioregulator_regnode_class,
303 	    &sc->init_def.reg_init_def);
304 	if (regnode == NULL) {
305 		device_printf(dev, "cannot create regulator\n");
306 		return (ENXIO);
307 	}
308 
309 	sc->reg_sc = regnode_get_softc(regnode);
310 	sc->reg_sc->regnode = regnode;
311 	sc->reg_sc->base_dev = dev;
312 	sc->reg_sc->param = regnode_get_stdparam(regnode);
313 	sc->reg_sc->def = &sc->init_def;
314 
315 	regnode_register(regnode);
316 
317 	return (0);
318 }
319 
320 
321 static device_method_t gpioregulator_methods[] = {
322 	/* Device interface */
323 	DEVMETHOD(device_probe,		gpioregulator_probe),
324 	DEVMETHOD(device_attach,	gpioregulator_attach),
325 
326 	/* Regdev interface */
327 	DEVMETHOD(regdev_map,		regdev_default_ofw_map),
328 
329 	DEVMETHOD_END
330 };
331 
332 static driver_t gpioregulator_driver = {
333 	"gpioregulator",
334 	gpioregulator_methods,
335 	sizeof(struct gpioregulator_softc),
336 };
337 
338 EARLY_DRIVER_MODULE(gpioregulator, simplebus, gpioregulator_driver, 0, 0,
339     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
340 MODULE_VERSION(gpioregulator, 1);
341