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;
60 static gpio_list_t gpio_list = TAILQ_HEAD_INITIALIZER(gpio_list);
61 static struct mtx gpio_list_mtx;
62 MTX_SYSINIT(gpio_list_lock, &gpio_list_mtx, "Regulator GPIO lock", MTX_DEF);
64 struct regnode_fixed_sc {
65 struct regnode_std_param *param;
67 struct gpio_entry *gpio_entry;
70 static int regnode_fixed_init(struct regnode *regnode);
71 static int regnode_fixed_enable(struct regnode *regnode, bool enable,
73 static int regnode_fixed_status(struct regnode *regnode, int *status);
75 static regnode_method_t regnode_fixed_methods[] = {
76 /* Regulator interface */
77 REGNODEMETHOD(regnode_init, regnode_fixed_init),
78 REGNODEMETHOD(regnode_enable, regnode_fixed_enable),
79 REGNODEMETHOD(regnode_status, regnode_fixed_status),
82 DEFINE_CLASS_1(regnode_fixed, regnode_fixed_class, regnode_fixed_methods,
83 sizeof(struct regnode_fixed_sc), regnode_class);
86 * GPIO list functions.
87 * Two or more regulators can share single GPIO pins, so we must track all
89 * The GPIO pin is registerd and reseved for first consumer, all others share
92 static struct gpio_entry *
93 regnode_get_gpio_entry(struct gpiobus_pin *gpio_pin)
95 struct gpio_entry *entry, *tmp;
99 busdev = GPIO_GET_BUS(gpio_pin->dev);
102 entry = malloc(sizeof(struct gpio_entry), M_FIXEDREGULATOR,
105 mtx_lock(&gpio_list_mtx);
107 TAILQ_FOREACH(tmp, &gpio_list, link) {
108 if (tmp->gpio_pin.dev == gpio_pin->dev &&
109 tmp->gpio_pin.pin == gpio_pin->pin) {
111 mtx_unlock(&gpio_list_mtx);
112 free(entry, M_FIXEDREGULATOR);
118 /* XXX Can we call gpiobus_acquire_pin() with gpio_list_mtx held? */
119 rv = gpiobus_acquire_pin(busdev, gpio_pin->pin);
121 mtx_unlock(&gpio_list_mtx);
122 free(entry, M_FIXEDREGULATOR);
125 /* Everything is OK, build new entry and insert it to list. */
126 entry->gpio_pin = *gpio_pin;
128 TAILQ_INSERT_TAIL(&gpio_list, entry, link);
130 mtx_unlock(&gpio_list_mtx);
136 * Regulator class implementation.
139 regnode_fixed_init(struct regnode *regnode)
142 struct regnode_fixed_sc *sc;
143 struct gpiobus_pin *pin;
148 sc = regnode_get_softc(regnode);
149 dev = regnode_get_device(regnode);
150 sc->param = regnode_get_stdparam(regnode);
151 if (sc->gpio_entry == NULL)
153 pin = &sc->gpio_entry->gpio_pin;
155 flags = GPIO_PIN_OUTPUT;
156 if (sc->gpio_open_drain)
157 flags |= GPIO_PIN_OPENDRAIN;
158 enable = sc->param->boot_on || sc->param->always_on;
159 if (!sc->param->enable_active_high)
161 rv = GPIO_PIN_SET(pin->dev, pin->pin, enable);
163 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
166 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
168 device_printf(dev, "Cannot configure GPIO pin: %d\n", pin->pin);
176 * Enable/disable regulator.
177 * Take shared GPIO pins in account
180 regnode_fixed_enable(struct regnode *regnode, bool enable, int *udelay)
183 struct regnode_fixed_sc *sc;
184 struct gpiobus_pin *pin;
187 sc = regnode_get_softc(regnode);
188 dev = regnode_get_device(regnode);
191 if (sc->param->always_on && !enable)
193 if (sc->gpio_entry == NULL)
195 pin = &sc->gpio_entry->gpio_pin;
197 sc->gpio_entry->enable_cnt++;
198 if (sc->gpio_entry->enable_cnt > 1)
201 KASSERT(sc->gpio_entry->enable_cnt > 0,
202 ("Invalid enable count"));
203 sc->gpio_entry->enable_cnt--;
204 if (sc->gpio_entry->enable_cnt >= 1)
207 if (!sc->param->enable_active_high)
209 rv = GPIO_PIN_SET(pin->dev, pin->pin, enable);
211 device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
214 *udelay = sc->param->enable_delay;
219 regnode_fixed_status(struct regnode *regnode, int *status)
221 struct regnode_fixed_sc *sc;
222 struct gpiobus_pin *pin;
226 sc = regnode_get_softc(regnode);
229 if (sc->gpio_entry == NULL) {
230 *status = REGULATOR_STATUS_ENABLED;
233 pin = &sc->gpio_entry->gpio_pin;
235 rv = GPIO_PIN_GET(pin->dev, pin->pin, &val);
237 if (!sc->param->enable_active_high ^ (val != 0))
238 *status = REGULATOR_STATUS_ENABLED;
244 regnode_fixed_register(device_t dev, struct regnode_fixed_init_def *init_def)
246 struct regnode *regnode;
247 struct regnode_fixed_sc *sc;
249 regnode = regnode_create(dev, ®node_fixed_class,
250 &init_def->reg_init_def);
251 if (regnode == NULL) {
252 device_printf(dev, "Cannot create regulator.\n");
255 sc = regnode_get_softc(regnode);
256 sc->gpio_open_drain = init_def->gpio_open_drain;
257 if (init_def->gpio_pin != NULL) {
258 sc->gpio_entry = regnode_get_gpio_entry(init_def->gpio_pin);
259 if (sc->gpio_entry == NULL)
262 regnode = regnode_register(regnode);
263 if (regnode == NULL) {
264 device_printf(dev, "Cannot register regulator.\n");
271 * OFW Driver implementation.
279 struct regnode_fixed_init_def init_def;
280 phandle_t gpio_prodxref;
283 struct gpiobus_pin gpio_pin;
286 static struct ofw_compat_data compat_data[] = {
287 {"regulator-fixed", 1},
292 regfix_get_gpio(struct regfix_softc * sc)
299 if (sc->gpio_prodxref == 0)
302 node = ofw_bus_get_node(sc->dev);
304 /* Test if controller exist. */
305 sc->gpio_pin.dev = OF_device_from_xref(sc->gpio_prodxref);
306 if (sc->gpio_pin.dev == NULL)
309 /* Test if GPIO bus already exist. */
310 busdev = GPIO_GET_BUS(sc->gpio_pin.dev);
314 rv = gpio_map_gpios(sc->gpio_pin.dev, node,
315 OF_node_from_xref(sc->gpio_prodxref), sc->gpio_ncells,
316 sc->gpio_cells, &(sc->gpio_pin.pin), &(sc->gpio_pin.flags));
318 device_printf(sc->dev, "Cannot map the gpio property.\n");
321 sc->init_def.gpio_pin = &sc->gpio_pin;
326 regfix_parse_fdt(struct regfix_softc * sc)
330 struct regnode_init_def *init_def;
332 node = ofw_bus_get_node(sc->dev);
333 init_def = &sc->init_def.reg_init_def;
335 rv = regulator_parse_ofw_stdparam(sc->dev, node, init_def);
337 device_printf(sc->dev, "Cannot parse standard parameters.\n");
341 /* Fixed regulator uses 'startup-delay-us' property for enable_delay */
342 rv = OF_getencprop(node, "startup-delay-us",
343 &init_def->std_param.enable_delay,
344 sizeof(init_def->std_param.enable_delay));
346 init_def->std_param.enable_delay = 0;
348 if (OF_hasprop(node, "gpio-open-drain"))
349 sc->init_def.gpio_open_drain = true;
351 if (!OF_hasprop(node, "gpio"))
353 rv = ofw_bus_parse_xref_list_alloc(node, "gpio", "#gpio-cells", 0,
354 &sc->gpio_prodxref, &sc->gpio_ncells, &sc->gpio_cells);
356 sc->gpio_prodxref = 0;
357 device_printf(sc->dev, "Malformed gpio property\n");
364 regfix_new_pass(device_t dev)
366 struct regfix_softc * sc;
369 sc = device_get_softc(dev);
370 bus_generic_new_pass(dev);
375 /* Try to get and configure GPIO. */
376 rv = regfix_get_gpio(sc);
380 /* Register regulator. */
381 regnode_fixed_register(sc->dev, &sc->init_def);
382 sc->attach_done = true;
386 regfix_probe(device_t dev)
389 if (!ofw_bus_status_okay(dev))
392 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
395 device_set_desc(dev, "Fixed Regulator");
396 return (BUS_PROBE_DEFAULT);
400 regfix_detach(device_t dev)
403 /* This device is always present. */
408 regfix_attach(device_t dev)
410 struct regfix_softc * sc;
413 sc = device_get_softc(dev);
416 /* Parse FDT data. */
417 rv = regfix_parse_fdt(sc);
421 /* Fill reset of init. */
422 sc->init_def.reg_init_def.id = 1;
423 sc->init_def.reg_init_def.flags = REGULATOR_FLAGS_STATIC;
425 /* Try to get and configure GPIO. */
426 rv = regfix_get_gpio(sc);
428 return (bus_generic_attach(dev));
430 /* Register regulator. */
431 regnode_fixed_register(sc->dev, &sc->init_def);
432 sc->attach_done = true;
434 return (bus_generic_attach(dev));
437 static device_method_t regfix_methods[] = {
438 /* Device interface */
439 DEVMETHOD(device_probe, regfix_probe),
440 DEVMETHOD(device_attach, regfix_attach),
441 DEVMETHOD(device_detach, regfix_detach),
443 DEVMETHOD(bus_new_pass, regfix_new_pass),
444 /* Regdev interface */
445 DEVMETHOD(regdev_map, regdev_default_ofw_map),
450 static devclass_t regfix_devclass;
451 DEFINE_CLASS_0(regfix, regfix_driver, regfix_methods,
452 sizeof(struct regfix_softc));
453 EARLY_DRIVER_MODULE(regfix, simplebus, regfix_driver,
454 regfix_devclass, 0, 0, BUS_PASS_BUS);