2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
29 * GPIO controlled regulators
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/gpio/gpiobusvar.h>
48 #include <dev/extres/regulator/regulator.h>
50 #include "regdev_if.h"
52 struct gpioregulator_state {
57 struct gpioregulator_init_def {
58 struct regnode_init_def reg_init_def;
59 struct gpiobus_pin *enable_pin;
63 struct gpioregulator_state *states;
65 struct gpiobus_pin **pins;
68 struct gpioregulator_reg_sc {
69 struct regnode *regnode;
71 struct regnode_std_param *param;
72 struct gpioregulator_init_def *def;
75 struct gpioregulator_softc {
77 struct gpioregulator_reg_sc *reg_sc;
78 struct gpioregulator_init_def init_def;
82 gpioregulator_regnode_init(struct regnode *regnode)
84 struct gpioregulator_reg_sc *sc;
87 sc = regnode_get_softc(regnode);
89 if (sc->def->enable_pin_valid == 1) {
90 error = gpio_pin_setflags(sc->def->enable_pin, GPIO_PIN_OUTPUT);
95 for (n = 0; n < sc->def->npins; n++) {
96 error = gpio_pin_setflags(sc->def->pins[n], GPIO_PIN_OUTPUT);
105 gpioregulator_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
107 struct gpioregulator_reg_sc *sc;
111 sc = regnode_get_softc(regnode);
113 if (sc->def->enable_pin_valid == 1) {
115 if (!sc->param->enable_active_high)
117 error = gpio_pin_set_active(sc->def->enable_pin, active);
122 *udelay = sc->def->startup_delay_us;
128 gpioregulator_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
129 int max_uvolt, int *udelay)
131 struct gpioregulator_reg_sc *sc;
132 const struct gpioregulator_state *state;
135 sc = regnode_get_softc(regnode);
138 for (n = 0; n < sc->def->nstates; n++) {
139 if (sc->def->states[n].val >= min_uvolt &&
140 sc->def->states[n].val <= max_uvolt) {
141 state = &sc->def->states[n];
148 for (n = 0; n < sc->def->npins; n++) {
149 error = gpio_pin_set_active(sc->def->pins[n],
150 (state->mask >> n) & 1);
155 *udelay = sc->def->startup_delay_us;
161 gpioregulator_regnode_get_voltage(struct regnode *regnode, int *uvolt)
163 struct gpioregulator_reg_sc *sc;
168 sc = regnode_get_softc(regnode);
171 for (n = 0; n < sc->def->npins; n++) {
172 error = gpio_pin_is_active(sc->def->pins[n], &active);
175 mask |= (active << n);
178 for (n = 0; n < sc->def->nstates; n++) {
179 if (sc->def->states[n].mask == mask) {
180 *uvolt = sc->def->states[n].val;
188 static regnode_method_t gpioregulator_regnode_methods[] = {
189 /* Regulator interface */
190 REGNODEMETHOD(regnode_init, gpioregulator_regnode_init),
191 REGNODEMETHOD(regnode_enable, gpioregulator_regnode_enable),
192 REGNODEMETHOD(regnode_set_voltage, gpioregulator_regnode_set_voltage),
193 REGNODEMETHOD(regnode_get_voltage, gpioregulator_regnode_get_voltage),
196 DEFINE_CLASS_1(gpioregulator_regnode, gpioregulator_regnode_class,
197 gpioregulator_regnode_methods, sizeof(struct gpioregulator_reg_sc),
201 gpioregulator_parse_fdt(struct gpioregulator_softc *sc)
203 uint32_t *pstates, mask;
208 node = ofw_bus_get_node(sc->dev);
212 error = regulator_parse_ofw_stdparam(sc->dev, node,
213 &sc->init_def.reg_init_def);
217 /* "states" property (required) */
218 len = OF_getencprop_alloc_multi(node, "states", sizeof(*pstates),
221 device_printf(sc->dev, "invalid 'states' property\n");
225 sc->init_def.nstates = len / 2;
226 sc->init_def.states = malloc(sc->init_def.nstates *
227 sizeof(*sc->init_def.states), M_DEVBUF, M_WAITOK);
228 for (n = 0; n < sc->init_def.nstates; n++) {
229 sc->init_def.states[n].val = pstates[n * 2 + 0];
230 sc->init_def.states[n].mask = pstates[n * 2 + 1];
231 mask |= sc->init_def.states[n].mask;
234 /* "startup-delay-us" property (optional) */
235 len = OF_getencprop(node, "startup-delay-us",
236 &sc->init_def.startup_delay_us,
237 sizeof(sc->init_def.startup_delay_us));
239 sc->init_def.startup_delay_us = 0;
241 /* "enable-gpio" property (optional) */
242 error = gpio_pin_get_by_ofw_property(sc->dev, node, "enable-gpio",
243 &sc->init_def.enable_pin);
245 sc->init_def.enable_pin_valid = 1;
247 /* "gpios" property */
248 sc->init_def.npins = 32 - __builtin_clz(mask);
249 sc->init_def.pins = malloc(sc->init_def.npins *
250 sizeof(sc->init_def.pins), M_DEVBUF, M_WAITOK | M_ZERO);
251 for (n = 0; n < sc->init_def.npins; n++) {
252 error = gpio_pin_get_by_ofw_idx(sc->dev, node, n,
253 &sc->init_def.pins[n]);
255 device_printf(sc->dev, "cannot get pin %d\n", n);
262 for (n = 0; n < sc->init_def.npins; n++) {
263 if (sc->init_def.pins[n] != NULL)
264 gpio_pin_release(sc->init_def.pins[n]);
267 free(sc->init_def.states, M_DEVBUF);
268 free(sc->init_def.pins, M_DEVBUF);
271 OF_prop_free(pstates);
277 gpioregulator_probe(device_t dev)
280 if (!ofw_bus_is_compatible(dev, "regulator-gpio"))
283 device_set_desc(dev, "GPIO controlled regulator");
284 return (BUS_PROBE_GENERIC);
288 gpioregulator_attach(device_t dev)
290 struct gpioregulator_softc *sc;
291 struct regnode *regnode;
295 sc = device_get_softc(dev);
297 node = ofw_bus_get_node(dev);
299 error = gpioregulator_parse_fdt(sc);
301 device_printf(dev, "cannot parse parameters\n");
304 sc->init_def.reg_init_def.id = 1;
305 sc->init_def.reg_init_def.ofw_node = node;
307 regnode = regnode_create(dev, &gpioregulator_regnode_class,
308 &sc->init_def.reg_init_def);
309 if (regnode == NULL) {
310 device_printf(dev, "cannot create regulator\n");
314 sc->reg_sc = regnode_get_softc(regnode);
315 sc->reg_sc->regnode = regnode;
316 sc->reg_sc->base_dev = dev;
317 sc->reg_sc->param = regnode_get_stdparam(regnode);
318 sc->reg_sc->def = &sc->init_def;
320 regnode_register(regnode);
326 static device_method_t gpioregulator_methods[] = {
327 /* Device interface */
328 DEVMETHOD(device_probe, gpioregulator_probe),
329 DEVMETHOD(device_attach, gpioregulator_attach),
331 /* Regdev interface */
332 DEVMETHOD(regdev_map, regdev_default_ofw_map),
337 static driver_t gpioregulator_driver = {
339 gpioregulator_methods,
340 sizeof(struct gpioregulator_softc),
343 static devclass_t gpioregulator_devclass;
345 EARLY_DRIVER_MODULE(gpioregulator, simplebus, gpioregulator_driver,
346 gpioregulator_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
347 MODULE_VERSION(gpioregulator, 1);