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
27 * GPIO controlled regulators
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
42 #include <dev/gpio/gpiobusvar.h>
44 #include <dev/extres/regulator/regulator.h>
46 #include "regdev_if.h"
48 struct gpioregulator_state {
53 struct gpioregulator_init_def {
54 struct regnode_init_def reg_init_def;
55 struct gpiobus_pin *enable_pin;
59 struct gpioregulator_state *states;
61 struct gpiobus_pin **pins;
64 struct gpioregulator_reg_sc {
65 struct regnode *regnode;
67 struct regnode_std_param *param;
68 struct gpioregulator_init_def *def;
71 struct gpioregulator_softc {
73 struct gpioregulator_reg_sc *reg_sc;
74 struct gpioregulator_init_def init_def;
78 gpioregulator_regnode_init(struct regnode *regnode)
80 struct gpioregulator_reg_sc *sc;
83 sc = regnode_get_softc(regnode);
85 if (sc->def->enable_pin_valid == 1) {
86 error = gpio_pin_setflags(sc->def->enable_pin, GPIO_PIN_OUTPUT);
91 for (n = 0; n < sc->def->npins; n++) {
92 error = gpio_pin_setflags(sc->def->pins[n], GPIO_PIN_OUTPUT);
101 gpioregulator_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
103 struct gpioregulator_reg_sc *sc;
107 sc = regnode_get_softc(regnode);
109 if (sc->def->enable_pin_valid == 1) {
111 if (!sc->param->enable_active_high)
113 error = gpio_pin_set_active(sc->def->enable_pin, active);
118 *udelay = sc->def->startup_delay_us;
124 gpioregulator_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
125 int max_uvolt, int *udelay)
127 struct gpioregulator_reg_sc *sc;
128 const struct gpioregulator_state *state;
131 sc = regnode_get_softc(regnode);
134 for (n = 0; n < sc->def->nstates; n++) {
135 if (sc->def->states[n].val >= min_uvolt &&
136 sc->def->states[n].val <= max_uvolt) {
137 state = &sc->def->states[n];
144 for (n = 0; n < sc->def->npins; n++) {
145 error = gpio_pin_set_active(sc->def->pins[n],
146 (state->mask >> n) & 1);
151 *udelay = sc->def->startup_delay_us;
157 gpioregulator_regnode_get_voltage(struct regnode *regnode, int *uvolt)
159 struct gpioregulator_reg_sc *sc;
164 sc = regnode_get_softc(regnode);
167 for (n = 0; n < sc->def->npins; n++) {
168 error = gpio_pin_is_active(sc->def->pins[n], &active);
171 mask |= (active << n);
174 for (n = 0; n < sc->def->nstates; n++) {
175 if (sc->def->states[n].mask == mask) {
176 *uvolt = sc->def->states[n].val;
184 static regnode_method_t gpioregulator_regnode_methods[] = {
185 /* Regulator interface */
186 REGNODEMETHOD(regnode_init, gpioregulator_regnode_init),
187 REGNODEMETHOD(regnode_enable, gpioregulator_regnode_enable),
188 REGNODEMETHOD(regnode_set_voltage, gpioregulator_regnode_set_voltage),
189 REGNODEMETHOD(regnode_get_voltage, gpioregulator_regnode_get_voltage),
192 DEFINE_CLASS_1(gpioregulator_regnode, gpioregulator_regnode_class,
193 gpioregulator_regnode_methods, sizeof(struct gpioregulator_reg_sc),
197 gpioregulator_parse_fdt(struct gpioregulator_softc *sc)
199 uint32_t *pstates, mask;
204 node = ofw_bus_get_node(sc->dev);
208 error = regulator_parse_ofw_stdparam(sc->dev, node,
209 &sc->init_def.reg_init_def);
213 /* "states" property (required) */
214 len = OF_getencprop_alloc_multi(node, "states", sizeof(*pstates),
217 device_printf(sc->dev, "invalid 'states' property\n");
221 sc->init_def.nstates = len / 2;
222 sc->init_def.states = malloc(sc->init_def.nstates *
223 sizeof(*sc->init_def.states), M_DEVBUF, M_WAITOK);
224 for (n = 0; n < sc->init_def.nstates; n++) {
225 sc->init_def.states[n].val = pstates[n * 2 + 0];
226 sc->init_def.states[n].mask = pstates[n * 2 + 1];
227 mask |= sc->init_def.states[n].mask;
230 /* "startup-delay-us" property (optional) */
231 len = OF_getencprop(node, "startup-delay-us",
232 &sc->init_def.startup_delay_us,
233 sizeof(sc->init_def.startup_delay_us));
235 sc->init_def.startup_delay_us = 0;
237 /* "enable-gpio" property (optional) */
238 error = gpio_pin_get_by_ofw_property(sc->dev, node, "enable-gpio",
239 &sc->init_def.enable_pin);
241 sc->init_def.enable_pin_valid = 1;
243 /* "gpios" property */
244 sc->init_def.npins = 32 - __builtin_clz(mask);
245 sc->init_def.pins = malloc(sc->init_def.npins *
246 sizeof(sc->init_def.pins), M_DEVBUF, M_WAITOK | M_ZERO);
247 for (n = 0; n < sc->init_def.npins; n++) {
248 error = gpio_pin_get_by_ofw_idx(sc->dev, node, n,
249 &sc->init_def.pins[n]);
251 device_printf(sc->dev, "cannot get pin %d\n", n);
258 for (n = 0; n < sc->init_def.npins; n++) {
259 if (sc->init_def.pins[n] != NULL)
260 gpio_pin_release(sc->init_def.pins[n]);
263 free(sc->init_def.states, M_DEVBUF);
264 free(sc->init_def.pins, M_DEVBUF);
267 OF_prop_free(pstates);
273 gpioregulator_probe(device_t dev)
276 if (!ofw_bus_is_compatible(dev, "regulator-gpio"))
279 device_set_desc(dev, "GPIO controlled regulator");
280 return (BUS_PROBE_GENERIC);
284 gpioregulator_attach(device_t dev)
286 struct gpioregulator_softc *sc;
287 struct regnode *regnode;
291 sc = device_get_softc(dev);
293 node = ofw_bus_get_node(dev);
295 error = gpioregulator_parse_fdt(sc);
297 device_printf(dev, "cannot parse parameters\n");
300 sc->init_def.reg_init_def.id = 1;
301 sc->init_def.reg_init_def.ofw_node = node;
303 regnode = regnode_create(dev, &gpioregulator_regnode_class,
304 &sc->init_def.reg_init_def);
305 if (regnode == NULL) {
306 device_printf(dev, "cannot create regulator\n");
310 sc->reg_sc = regnode_get_softc(regnode);
311 sc->reg_sc->regnode = regnode;
312 sc->reg_sc->base_dev = dev;
313 sc->reg_sc->param = regnode_get_stdparam(regnode);
314 sc->reg_sc->def = &sc->init_def;
316 regnode_register(regnode);
322 static device_method_t gpioregulator_methods[] = {
323 /* Device interface */
324 DEVMETHOD(device_probe, gpioregulator_probe),
325 DEVMETHOD(device_attach, gpioregulator_attach),
327 /* Regdev interface */
328 DEVMETHOD(regdev_map, regdev_default_ofw_map),
333 static driver_t gpioregulator_driver = {
335 gpioregulator_methods,
336 sizeof(struct gpioregulator_softc),
339 EARLY_DRIVER_MODULE(gpioregulator, simplebus, gpioregulator_driver, 0, 0,
340 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
341 MODULE_VERSION(gpioregulator, 1);