2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_platform.h"
31 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
41 #include <dev/fdt/fdt_common.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/gpio/gpiobusvar.h>
46 #include <dev/extres/regulator/regulator_fixed.h>
48 #include "regdev_if.h"
50 MALLOC_DEFINE(M_FIXEDREGULATOR, "fixedregulator", "Fixed regulator");
52 /* GPIO list for shared pins. */
53 typedef TAILQ_HEAD(gpio_list, gpio_entry) gpio_list_t;
55 TAILQ_ENTRY(gpio_entry) link;
56 struct gpiobus_pin gpio_pin;
61 static gpio_list_t gpio_list = TAILQ_HEAD_INITIALIZER(gpio_list);
62 static struct mtx gpio_list_mtx;
63 MTX_SYSINIT(gpio_list_lock, &gpio_list_mtx, "Regulator GPIO lock", MTX_DEF);
65 struct regnode_fixed_sc {
66 struct regnode_std_param *param;
68 struct gpio_entry *gpio_entry;
71 static int regnode_fixed_init(struct regnode *regnode);
72 static int regnode_fixed_enable(struct regnode *regnode, bool enable,
74 static int regnode_fixed_status(struct regnode *regnode, int *status);
75 static int regnode_fixed_stop(struct regnode *regnode, int *udelay);
77 static regnode_method_t regnode_fixed_methods[] = {
78 /* Regulator interface */
79 REGNODEMETHOD(regnode_init, regnode_fixed_init),
80 REGNODEMETHOD(regnode_enable, regnode_fixed_enable),
81 REGNODEMETHOD(regnode_status, regnode_fixed_status),
82 REGNODEMETHOD(regnode_stop, regnode_fixed_stop),
85 DEFINE_CLASS_1(regnode_fixed, regnode_fixed_class, regnode_fixed_methods,
86 sizeof(struct regnode_fixed_sc), regnode_class);
89 * GPIO list functions.
90 * Two or more regulators can share single GPIO pins, so we must track all
92 * The GPIO pin is registerd and reseved for first consumer, all others share
95 static struct gpio_entry *
96 regnode_get_gpio_entry(struct gpiobus_pin *gpio_pin)
98 struct gpio_entry *entry, *tmp;
102 busdev = GPIO_GET_BUS(gpio_pin->dev);
105 entry = malloc(sizeof(struct gpio_entry), M_FIXEDREGULATOR,
108 mtx_lock(&gpio_list_mtx);
110 TAILQ_FOREACH(tmp, &gpio_list, link) {
111 if (tmp->gpio_pin.dev == gpio_pin->dev &&
112 tmp->gpio_pin.pin == gpio_pin->pin) {
114 mtx_unlock(&gpio_list_mtx);
115 free(entry, M_FIXEDREGULATOR);
121 /* XXX Can we call gpiobus_acquire_pin() with gpio_list_mtx held? */
122 rv = gpiobus_acquire_pin(busdev, gpio_pin->pin);
124 mtx_unlock(&gpio_list_mtx);
125 free(entry, M_FIXEDREGULATOR);
128 /* Everything is OK, build new entry and insert it to list. */
129 entry->gpio_pin = *gpio_pin;
131 TAILQ_INSERT_TAIL(&gpio_list, entry, link);
133 mtx_unlock(&gpio_list_mtx);
139 * Regulator class implementation.
142 regnode_fixed_init(struct regnode *regnode)
145 struct regnode_fixed_sc *sc;
146 struct gpiobus_pin *pin;
150 sc = regnode_get_softc(regnode);
151 dev = regnode_get_device(regnode);
152 sc->param = regnode_get_stdparam(regnode);
153 if (sc->gpio_entry == NULL)
155 pin = &sc->gpio_entry->gpio_pin;
157 flags = GPIO_PIN_OUTPUT;
158 if (sc->gpio_open_drain)
159 flags |= GPIO_PIN_OPENDRAIN;
160 if (sc->param->boot_on || sc->param->always_on) {
161 rv = GPIO_PIN_SET(pin->dev, pin->pin, sc->param->enable_active_high);
163 device_printf(dev, "Cannot set GPIO pin: %d\n",
169 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
171 device_printf(dev, "Cannot configure GPIO pin: %d\n", pin->pin);
179 * Enable/disable regulator.
180 * Take shared GPIO pins in account
183 regnode_fixed_enable(struct regnode *regnode, bool enable, int *udelay)
186 struct regnode_fixed_sc *sc;
187 struct gpiobus_pin *pin;
190 sc = regnode_get_softc(regnode);
191 dev = regnode_get_device(regnode);
194 if (sc->gpio_entry == NULL)
196 pin = &sc->gpio_entry->gpio_pin;
198 sc->gpio_entry->enable_cnt++;
199 if (sc->gpio_entry->enable_cnt > 1)
202 KASSERT(sc->gpio_entry->enable_cnt > 0,
203 ("Invalid enable count"));
204 sc->gpio_entry->enable_cnt--;
205 if (sc->gpio_entry->enable_cnt >= 1)
208 if (sc->gpio_entry->always_on && !enable)
210 if (!sc->param->enable_active_high)
212 rv = GPIO_PIN_SET(pin->dev, pin->pin, enable);
214 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
217 *udelay = sc->param->enable_delay;
222 * Stop (physicaly shutdown) regulator.
223 * Take shared GPIO pins in account
226 regnode_fixed_stop(struct regnode *regnode, int *udelay)
229 struct regnode_fixed_sc *sc;
230 struct gpiobus_pin *pin;
233 sc = regnode_get_softc(regnode);
234 dev = regnode_get_device(regnode);
237 if (sc->gpio_entry == NULL)
239 if (sc->gpio_entry->always_on)
241 pin = &sc->gpio_entry->gpio_pin;
242 if (sc->gpio_entry->enable_cnt > 0) {
243 /* Other regulator(s) are enabled. */
244 /* XXXX Any diagnostic message? Or error? */
247 rv = GPIO_PIN_SET(pin->dev, pin->pin,
248 sc->param->enable_active_high ? false: true);
250 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
253 *udelay = sc->param->enable_delay;
258 regnode_fixed_status(struct regnode *regnode, int *status)
260 struct regnode_fixed_sc *sc;
261 struct gpiobus_pin *pin;
265 sc = regnode_get_softc(regnode);
268 if (sc->gpio_entry == NULL) {
269 *status = REGULATOR_STATUS_ENABLED;
272 pin = &sc->gpio_entry->gpio_pin;
274 rv = GPIO_PIN_GET(pin->dev, pin->pin, &val);
276 if (!sc->param->enable_active_high ^ (val != 0))
277 *status = REGULATOR_STATUS_ENABLED;
283 regnode_fixed_register(device_t dev, struct regnode_fixed_init_def *init_def)
285 struct regnode *regnode;
286 struct regnode_fixed_sc *sc;
288 regnode = regnode_create(dev, ®node_fixed_class,
289 &init_def->reg_init_def);
290 if (regnode == NULL) {
291 device_printf(dev, "Cannot create regulator.\n");
294 sc = regnode_get_softc(regnode);
295 sc->gpio_open_drain = init_def->gpio_open_drain;
296 if (init_def->gpio_pin != NULL) {
297 sc->gpio_entry = regnode_get_gpio_entry(init_def->gpio_pin);
298 if (sc->gpio_entry == NULL)
301 regnode = regnode_register(regnode);
302 if (regnode == NULL) {
303 device_printf(dev, "Cannot register regulator.\n");
307 if (sc->gpio_entry != NULL)
308 sc->gpio_entry->always_on |= sc->param->always_on;
314 * OFW Driver implementation.
322 struct regnode_fixed_init_def init_def;
323 phandle_t gpio_prodxref;
326 struct gpiobus_pin gpio_pin;
329 static struct ofw_compat_data compat_data[] = {
330 {"regulator-fixed", 1},
335 regfix_get_gpio(struct regfix_softc * sc)
342 if (sc->gpio_prodxref == 0)
345 node = ofw_bus_get_node(sc->dev);
347 /* Test if controller exist. */
348 sc->gpio_pin.dev = OF_device_from_xref(sc->gpio_prodxref);
349 if (sc->gpio_pin.dev == NULL)
352 /* Test if GPIO bus already exist. */
353 busdev = GPIO_GET_BUS(sc->gpio_pin.dev);
357 rv = gpio_map_gpios(sc->gpio_pin.dev, node,
358 OF_node_from_xref(sc->gpio_prodxref), sc->gpio_ncells,
359 sc->gpio_cells, &(sc->gpio_pin.pin), &(sc->gpio_pin.flags));
361 device_printf(sc->dev, "Cannot map the gpio property.\n");
364 sc->init_def.gpio_pin = &sc->gpio_pin;
369 regfix_parse_fdt(struct regfix_softc * sc)
373 struct regnode_init_def *init_def;
375 node = ofw_bus_get_node(sc->dev);
376 init_def = &sc->init_def.reg_init_def;
378 rv = regulator_parse_ofw_stdparam(sc->dev, node, init_def);
380 device_printf(sc->dev, "Cannot parse standard parameters.\n");
384 /* Fixed regulator uses 'startup-delay-us' property for enable_delay */
385 rv = OF_getencprop(node, "startup-delay-us",
386 &init_def->std_param.enable_delay,
387 sizeof(init_def->std_param.enable_delay));
389 init_def->std_param.enable_delay = 0;
391 if (OF_hasprop(node, "gpio-open-drain"))
392 sc->init_def.gpio_open_drain = true;
394 if (!OF_hasprop(node, "gpio"))
396 rv = ofw_bus_parse_xref_list_alloc(node, "gpio", "#gpio-cells", 0,
397 &sc->gpio_prodxref, &sc->gpio_ncells, &sc->gpio_cells);
399 sc->gpio_prodxref = 0;
400 device_printf(sc->dev, "Malformed gpio property\n");
407 regfix_new_pass(device_t dev)
409 struct regfix_softc * sc;
412 sc = device_get_softc(dev);
413 bus_generic_new_pass(dev);
418 /* Try to get and configure GPIO. */
419 rv = regfix_get_gpio(sc);
423 /* Register regulator. */
424 regnode_fixed_register(sc->dev, &sc->init_def);
425 sc->attach_done = true;
429 regfix_probe(device_t dev)
432 if (!ofw_bus_status_okay(dev))
435 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
438 device_set_desc(dev, "Fixed Regulator");
439 return (BUS_PROBE_DEFAULT);
443 regfix_detach(device_t dev)
446 /* This device is always present. */
451 regfix_attach(device_t dev)
453 struct regfix_softc * sc;
456 sc = device_get_softc(dev);
459 /* Parse FDT data. */
460 rv = regfix_parse_fdt(sc);
464 /* Fill reset of init. */
465 sc->init_def.reg_init_def.id = 1;
466 sc->init_def.reg_init_def.flags = REGULATOR_FLAGS_STATIC;
468 /* Try to get and configure GPIO. */
469 rv = regfix_get_gpio(sc);
471 return (bus_generic_attach(dev));
473 /* Register regulator. */
474 regnode_fixed_register(sc->dev, &sc->init_def);
475 sc->attach_done = true;
477 return (bus_generic_attach(dev));
480 static device_method_t regfix_methods[] = {
481 /* Device interface */
482 DEVMETHOD(device_probe, regfix_probe),
483 DEVMETHOD(device_attach, regfix_attach),
484 DEVMETHOD(device_detach, regfix_detach),
486 DEVMETHOD(bus_new_pass, regfix_new_pass),
487 /* Regdev interface */
488 DEVMETHOD(regdev_map, regdev_default_ofw_map),
493 static devclass_t regfix_devclass;
494 DEFINE_CLASS_0(regfix, regfix_driver, regfix_methods,
495 sizeof(struct regfix_softc));
496 EARLY_DRIVER_MODULE(regfix, simplebus, regfix_driver,
497 regfix_devclass, 0, 0, BUS_PASS_BUS);