regulator: Import from FreeBSD.

Update #3869.
This commit is contained in:
Christian Mauderer 2020-04-02 18:04:17 +02:00
parent 10aa08e580
commit 404ad60f5f
6 changed files with 2462 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,156 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DEV_EXTRES_REGULATOR_H_
#define _DEV_EXTRES_REGULATOR_H_
#include <rtems/bsd/local/opt_platform.h>
#include <sys/kobj.h>
#include <sys/sysctl.h>
#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#endif
#include <rtems/bsd/local/regnode_if.h>
SYSCTL_DECL(_hw_regulator);
#define REGULATOR_FLAGS_STATIC 0x00000001 /* Static strings */
#define REGULATOR_FLAGS_NOT_DISABLE 0x00000002 /* Cannot be disabled */
#define REGULATOR_STATUS_ENABLED 0x00000001
#define REGULATOR_STATUS_OVERCURRENT 0x00000002
typedef struct regulator *regulator_t;
/* Standard regulator parameters. */
struct regnode_std_param {
int min_uvolt; /* In uV */
int max_uvolt; /* In uV */
int min_uamp; /* In uA */
int max_uamp; /* In uA */
int ramp_delay; /* In uV/usec */
int enable_delay; /* In usec */
bool boot_on; /* Is enabled on boot */
bool always_on; /* Must be enabled */
int enable_active_high;
};
/* Initialization parameters. */
struct regnode_init_def {
char *name; /* Regulator name */
char *parent_name; /* Name of parent regulator */
struct regnode_std_param std_param; /* Standard parameters */
intptr_t id; /* Regulator ID */
int flags; /* Flags */
#ifdef FDT
phandle_t ofw_node; /* OFW node of regulator */
#endif
};
struct regulator_range {
int min_uvolt;
int step_uvolt;
uint8_t min_sel;
uint8_t max_sel;
};
#define REG_RANGE_INIT(_min_sel, _max_sel, _min_uvolt, _step_uvolt) { \
.min_sel = _min_sel, \
.max_sel = _max_sel, \
.min_uvolt = _min_uvolt, \
.step_uvolt = _step_uvolt, \
}
/*
* Shorthands for constructing method tables.
*/
#define REGNODEMETHOD KOBJMETHOD
#define REGNODEMETHOD_END KOBJMETHOD_END
#define regnode_method_t kobj_method_t
#define regnode_class_t kobj_class_t
DECLARE_CLASS(regnode_class);
/* Providers interface. */
struct regnode *regnode_create(device_t pdev, regnode_class_t regnode_class,
struct regnode_init_def *def);
struct regnode *regnode_register(struct regnode *regnode);
const char *regnode_get_name(struct regnode *regnode);
const char *regnode_get_parent_name(struct regnode *regnode);
struct regnode *regnode_get_parent(struct regnode *regnode);
int regnode_get_flags(struct regnode *regnode);
void *regnode_get_softc(struct regnode *regnode);
device_t regnode_get_device(struct regnode *regnode);
struct regnode_std_param *regnode_get_stdparam(struct regnode *regnode);
void regnode_topo_unlock(void);
void regnode_topo_xlock(void);
void regnode_topo_slock(void);
int regnode_enable(struct regnode *regnode);
int regnode_disable(struct regnode *regnode);
int regnode_stop(struct regnode *regnode, int depth);
int regnode_status(struct regnode *regnode, int *status);
int regnode_get_voltage(struct regnode *regnode, int *uvolt);
int regnode_set_voltage(struct regnode *regnode, int min_uvolt, int max_uvolt);
int regnode_set_constraint(struct regnode *regnode);
/* Standard method that aren't default */
int regnode_method_check_voltage(struct regnode *regnode, int uvolt);
#ifdef FDT
phandle_t regnode_get_ofw_node(struct regnode *regnode);
#endif
/* Consumers interface. */
int regulator_get_by_name(device_t cdev, const char *name,
regulator_t *regulator);
int regulator_get_by_id(device_t cdev, device_t pdev, intptr_t id,
regulator_t *regulator);
int regulator_release(regulator_t regulator);
const char *regulator_get_name(regulator_t regulator);
int regulator_enable(regulator_t reg);
int regulator_disable(regulator_t reg);
int regulator_stop(regulator_t reg);
int regulator_status(regulator_t reg, int *status);
int regulator_get_voltage(regulator_t reg, int *uvolt);
int regulator_set_voltage(regulator_t reg, int min_uvolt, int max_uvolt);
int regulator_check_voltage(regulator_t reg, int uvolt);
#ifdef FDT
int regulator_get_by_ofw_property(device_t dev, phandle_t node, char *name,
regulator_t *reg);
int regulator_parse_ofw_stdparam(device_t dev, phandle_t node,
struct regnode_init_def *def);
#endif
/* Utility functions */
int regulator_range_volt_to_sel8(struct regulator_range *ranges, int nranges,
int min_uvolt, int max_uvolt, uint8_t *out_sel);
int regulator_range_sel8_to_volt(struct regulator_range *ranges, int nranges,
uint8_t sel, int *volt);
#endif /* _DEV_EXTRES_REGULATOR_H_ */

View File

@ -0,0 +1,89 @@
#include <machine/rtems-bsd-kernel-space.h>
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <dev/fdt/simplebus.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus_subr.h>
struct ofw_regulator_bus_softc {
struct simplebus_softc simplebus_sc;
};
static int
ofw_regulator_bus_probe(device_t dev)
{
const char *name;
name = ofw_bus_get_name(dev);
if (name == NULL || strcmp(name, "regulators") != 0)
return (ENXIO);
device_set_desc(dev, "OFW regulators bus");
return (0);
}
static int
ofw_regulator_bus_attach(device_t dev)
{
phandle_t node, child;
node = ofw_bus_get_node(dev);
simplebus_init(dev, node);
for (child = OF_child(node); child > 0; child = OF_peer(child)) {
simplebus_add_device(dev, child, 0, NULL, -1, NULL);
}
return (bus_generic_attach(dev));
}
static device_method_t ofw_regulator_bus_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, ofw_regulator_bus_probe),
DEVMETHOD(device_attach, ofw_regulator_bus_attach),
DEVMETHOD_END
};
DEFINE_CLASS_1(ofw_regulator_bus, ofw_regulator_bus_driver,
ofw_regulator_bus_methods, sizeof(struct ofw_regulator_bus_softc),
simplebus_driver);
static devclass_t ofw_regulator_bus_devclass;
EARLY_DRIVER_MODULE(ofw_regulator_bus, simplebus, ofw_regulator_bus_driver,
ofw_regulator_bus_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
MODULE_VERSION(ofw_regulator_bus, 1);

View File

@ -0,0 +1,502 @@
#include <machine/rtems-bsd-kernel-space.h>
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <rtems/bsd/local/opt_platform.h>
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/gpio.h>
#include <sys/kernel.h>
#include <sys/kobj.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/mutex.h>
#ifdef FDT
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif
#include <dev/gpio/gpiobusvar.h>
#include <dev/extres/regulator/regulator_fixed.h>
#include <rtems/bsd/local/regdev_if.h>
MALLOC_DEFINE(M_FIXEDREGULATOR, "fixedregulator", "Fixed regulator");
/* GPIO list for shared pins. */
typedef TAILQ_HEAD(gpio_list, gpio_entry) gpio_list_t;
struct gpio_entry {
TAILQ_ENTRY(gpio_entry) link;
struct gpiobus_pin gpio_pin;
int use_cnt;
int enable_cnt;
bool always_on;
};
static gpio_list_t gpio_list = TAILQ_HEAD_INITIALIZER(gpio_list);
static struct mtx gpio_list_mtx;
MTX_SYSINIT(gpio_list_lock, &gpio_list_mtx, "Regulator GPIO lock", MTX_DEF);
struct regnode_fixed_sc {
struct regnode_std_param *param;
bool gpio_open_drain;
struct gpio_entry *gpio_entry;
};
static int regnode_fixed_init(struct regnode *regnode);
static int regnode_fixed_enable(struct regnode *regnode, bool enable,
int *udelay);
static int regnode_fixed_status(struct regnode *regnode, int *status);
static int regnode_fixed_stop(struct regnode *regnode, int *udelay);
static regnode_method_t regnode_fixed_methods[] = {
/* Regulator interface */
REGNODEMETHOD(regnode_init, regnode_fixed_init),
REGNODEMETHOD(regnode_enable, regnode_fixed_enable),
REGNODEMETHOD(regnode_status, regnode_fixed_status),
REGNODEMETHOD(regnode_stop, regnode_fixed_stop),
REGNODEMETHOD(regnode_check_voltage, regnode_method_check_voltage),
REGNODEMETHOD_END
};
DEFINE_CLASS_1(regnode_fixed, regnode_fixed_class, regnode_fixed_methods,
sizeof(struct regnode_fixed_sc), regnode_class);
/*
* GPIO list functions.
* Two or more regulators can share single GPIO pins, so we must track all
* GPIOs in gpio_list.
* The GPIO pin is registerd and reseved for first consumer, all others share
* gpio_entry with it.
*/
static struct gpio_entry *
regnode_get_gpio_entry(struct gpiobus_pin *gpio_pin)
{
struct gpio_entry *entry, *tmp;
device_t busdev;
int rv;
busdev = GPIO_GET_BUS(gpio_pin->dev);
if (busdev == NULL)
return (NULL);
entry = malloc(sizeof(struct gpio_entry), M_FIXEDREGULATOR,
M_WAITOK | M_ZERO);
mtx_lock(&gpio_list_mtx);
TAILQ_FOREACH(tmp, &gpio_list, link) {
if (tmp->gpio_pin.dev == gpio_pin->dev &&
tmp->gpio_pin.pin == gpio_pin->pin) {
tmp->use_cnt++;
mtx_unlock(&gpio_list_mtx);
free(entry, M_FIXEDREGULATOR);
return (tmp);
}
}
/* Reserve pin. */
/* XXX Can we call gpiobus_acquire_pin() with gpio_list_mtx held? */
rv = gpiobus_acquire_pin(busdev, gpio_pin->pin);
if (rv != 0) {
mtx_unlock(&gpio_list_mtx);
free(entry, M_FIXEDREGULATOR);
return (NULL);
}
/* Everything is OK, build new entry and insert it to list. */
entry->gpio_pin = *gpio_pin;
entry->use_cnt = 1;
TAILQ_INSERT_TAIL(&gpio_list, entry, link);
mtx_unlock(&gpio_list_mtx);
return (entry);
}
/*
* Regulator class implementation.
*/
static int
regnode_fixed_init(struct regnode *regnode)
{
device_t dev;
struct regnode_fixed_sc *sc;
struct gpiobus_pin *pin;
uint32_t flags;
int rv;
sc = regnode_get_softc(regnode);
dev = regnode_get_device(regnode);
sc->param = regnode_get_stdparam(regnode);
if (sc->gpio_entry == NULL)
return (0);
pin = &sc->gpio_entry->gpio_pin;
flags = GPIO_PIN_OUTPUT;
if (sc->gpio_open_drain)
flags |= GPIO_PIN_OPENDRAIN;
if (sc->param->boot_on || sc->param->always_on) {
rv = GPIO_PIN_SET(pin->dev, pin->pin, sc->param->enable_active_high);
if (rv != 0) {
device_printf(dev, "Cannot set GPIO pin: %d\n",
pin->pin);
return (rv);
}
}
rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
if (rv != 0) {
device_printf(dev, "Cannot configure GPIO pin: %d\n", pin->pin);
return (rv);
}
return (0);
}
/*
* Enable/disable regulator.
* Take shared GPIO pins in account
*/
static int
regnode_fixed_enable(struct regnode *regnode, bool enable, int *udelay)
{
device_t dev;
struct regnode_fixed_sc *sc;
struct gpiobus_pin *pin;
int rv;
sc = regnode_get_softc(regnode);
dev = regnode_get_device(regnode);
*udelay = 0;
if (sc->gpio_entry == NULL)
return (0);
pin = &sc->gpio_entry->gpio_pin;
if (enable) {
sc->gpio_entry->enable_cnt++;
if (sc->gpio_entry->enable_cnt > 1)
return (0);
} else {
KASSERT(sc->gpio_entry->enable_cnt > 0,
("Invalid enable count"));
sc->gpio_entry->enable_cnt--;
if (sc->gpio_entry->enable_cnt >= 1)
return (0);
}
if (sc->gpio_entry->always_on && !enable)
return (0);
if (!sc->param->enable_active_high)
enable = !enable;
rv = GPIO_PIN_SET(pin->dev, pin->pin, enable);
if (rv != 0) {
device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
return (rv);
}
*udelay = sc->param->enable_delay;
return (0);
}
/*
* Stop (physicaly shutdown) regulator.
* Take shared GPIO pins in account
*/
static int
regnode_fixed_stop(struct regnode *regnode, int *udelay)
{
device_t dev;
struct regnode_fixed_sc *sc;
struct gpiobus_pin *pin;
int rv;
sc = regnode_get_softc(regnode);
dev = regnode_get_device(regnode);
*udelay = 0;
if (sc->gpio_entry == NULL)
return (0);
if (sc->gpio_entry->always_on)
return (0);
pin = &sc->gpio_entry->gpio_pin;
if (sc->gpio_entry->enable_cnt > 0) {
/* Other regulator(s) are enabled. */
/* XXXX Any diagnostic message? Or error? */
return (0);
}
rv = GPIO_PIN_SET(pin->dev, pin->pin,
sc->param->enable_active_high ? false: true);
if (rv != 0) {
device_printf(dev, "Cannot set GPIO pin: %d\n", pin->pin);
return (rv);
}
*udelay = sc->param->enable_delay;
return (0);
}
static int
regnode_fixed_status(struct regnode *regnode, int *status)
{
struct regnode_fixed_sc *sc;
struct gpiobus_pin *pin;
uint32_t val;
int rv;
sc = regnode_get_softc(regnode);
*status = 0;
if (sc->gpio_entry == NULL) {
*status = REGULATOR_STATUS_ENABLED;
return (0);
}
pin = &sc->gpio_entry->gpio_pin;
rv = GPIO_PIN_GET(pin->dev, pin->pin, &val);
if (rv == 0) {
if (!sc->param->enable_active_high ^ (val != 0))
*status = REGULATOR_STATUS_ENABLED;
}
return (rv);
}
int
regnode_fixed_register(device_t dev, struct regnode_fixed_init_def *init_def)
{
struct regnode *regnode;
struct regnode_fixed_sc *sc;
regnode = regnode_create(dev, &regnode_fixed_class,
&init_def->reg_init_def);
if (regnode == NULL) {
device_printf(dev, "Cannot create regulator.\n");
return(ENXIO);
}
sc = regnode_get_softc(regnode);
sc->gpio_open_drain = init_def->gpio_open_drain;
if (init_def->gpio_pin != NULL) {
sc->gpio_entry = regnode_get_gpio_entry(init_def->gpio_pin);
if (sc->gpio_entry == NULL)
return(ENXIO);
}
regnode = regnode_register(regnode);
if (regnode == NULL) {
device_printf(dev, "Cannot register regulator.\n");
return(ENXIO);
}
if (sc->gpio_entry != NULL)
sc->gpio_entry->always_on |= sc->param->always_on;
return (0);
}
/*
* OFW Driver implementation.
*/
#ifdef FDT
struct regfix_softc
{
device_t dev;
bool attach_done;
struct regnode_fixed_init_def init_def;
phandle_t gpio_prodxref;
pcell_t *gpio_cells;
int gpio_ncells;
struct gpiobus_pin gpio_pin;
};
static struct ofw_compat_data compat_data[] = {
{"regulator-fixed", 1},
{NULL, 0},
};
static int
regfix_get_gpio(struct regfix_softc * sc)
{
device_t busdev;
phandle_t node;
int rv;
if (sc->gpio_prodxref == 0)
return (0);
node = ofw_bus_get_node(sc->dev);
/* Test if controller exist. */
sc->gpio_pin.dev = OF_device_from_xref(sc->gpio_prodxref);
if (sc->gpio_pin.dev == NULL)
return (ENODEV);
/* Test if GPIO bus already exist. */
busdev = GPIO_GET_BUS(sc->gpio_pin.dev);
if (busdev == NULL)
return (ENODEV);
rv = gpio_map_gpios(sc->gpio_pin.dev, node,
OF_node_from_xref(sc->gpio_prodxref), sc->gpio_ncells,
sc->gpio_cells, &(sc->gpio_pin.pin), &(sc->gpio_pin.flags));
if (rv != 0) {
device_printf(sc->dev, "Cannot map the gpio property.\n");
return (ENXIO);
}
sc->init_def.gpio_pin = &sc->gpio_pin;
return (0);
}
static int
regfix_parse_fdt(struct regfix_softc * sc)
{
phandle_t node;
int rv;
struct regnode_init_def *init_def;
node = ofw_bus_get_node(sc->dev);
init_def = &sc->init_def.reg_init_def;
rv = regulator_parse_ofw_stdparam(sc->dev, node, init_def);
if (rv != 0) {
device_printf(sc->dev, "Cannot parse standard parameters.\n");
return(rv);
}
/* Fixed regulator uses 'startup-delay-us' property for enable_delay */
rv = OF_getencprop(node, "startup-delay-us",
&init_def->std_param.enable_delay,
sizeof(init_def->std_param.enable_delay));
if (rv <= 0)
init_def->std_param.enable_delay = 0;
/* GPIO pin */
if (OF_hasprop(node, "gpio-open-drain"))
sc->init_def.gpio_open_drain = true;
if (!OF_hasprop(node, "gpio"))
return (0);
rv = ofw_bus_parse_xref_list_alloc(node, "gpio", "#gpio-cells", 0,
&sc->gpio_prodxref, &sc->gpio_ncells, &sc->gpio_cells);
if (rv != 0) {
sc->gpio_prodxref = 0;
device_printf(sc->dev, "Malformed gpio property\n");
return (ENXIO);
}
return (0);
}
static void
regfix_new_pass(device_t dev)
{
struct regfix_softc * sc;
int rv;
sc = device_get_softc(dev);
bus_generic_new_pass(dev);
if (sc->attach_done)
return;
/* Try to get and configure GPIO. */
rv = regfix_get_gpio(sc);
if (rv != 0)
return;
/* Register regulator. */
regnode_fixed_register(sc->dev, &sc->init_def);
sc->attach_done = true;
}
static int
regfix_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
return (ENXIO);
device_set_desc(dev, "Fixed Regulator");
return (BUS_PROBE_DEFAULT);
}
static int
regfix_detach(device_t dev)
{
/* This device is always present. */
return (EBUSY);
}
static int
regfix_attach(device_t dev)
{
struct regfix_softc * sc;
int rv;
sc = device_get_softc(dev);
sc->dev = dev;
/* Parse FDT data. */
rv = regfix_parse_fdt(sc);
if (rv != 0)
return(ENXIO);
/* Fill reset of init. */
sc->init_def.reg_init_def.id = 1;
sc->init_def.reg_init_def.flags = REGULATOR_FLAGS_STATIC;
/* Try to get and configure GPIO. */
rv = regfix_get_gpio(sc);
if (rv != 0)
return (bus_generic_attach(dev));
/* Register regulator. */
regnode_fixed_register(sc->dev, &sc->init_def);
sc->attach_done = true;
return (bus_generic_attach(dev));
}
static device_method_t regfix_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, regfix_probe),
DEVMETHOD(device_attach, regfix_attach),
DEVMETHOD(device_detach, regfix_detach),
/* Bus interface */
DEVMETHOD(bus_new_pass, regfix_new_pass),
/* Regdev interface */
DEVMETHOD(regdev_map, regdev_default_ofw_map),
DEVMETHOD_END
};
static devclass_t regfix_devclass;
DEFINE_CLASS_0(regfix, regfix_driver, regfix_methods,
sizeof(struct regfix_softc));
EARLY_DRIVER_MODULE(regfix, simplebus, regfix_driver,
regfix_devclass, 0, 0, BUS_PASS_BUS);
#endif /* FDT */

View File

@ -0,0 +1,44 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DEV_EXTRES_REGULATOR_FIXED_H_
#define _DEV_EXTRES_REGULATOR_FIXED_H_
#include <dev/gpio/gpiobusvar.h>
#include <dev/extres/regulator/regulator.h>
struct regnode_fixed_init_def {
struct regnode_init_def reg_init_def;
bool gpio_open_drain;
struct gpiobus_pin *gpio_pin;
};
int regnode_fixed_register(device_t dev,
struct regnode_fixed_init_def *init_def);
#endif /*_DEV_EXTRES_REGULATOR_FIXED_H_*/

View File

@ -0,0 +1,350 @@
#include <machine/rtems-bsd-kernel-space.h>
/*-
* Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
/*
* GPIO controlled regulators
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/gpio.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/gpio/gpiobusvar.h>
#include <dev/extres/regulator/regulator.h>
#include <rtems/bsd/local/regdev_if.h>
struct gpioregulator_state {
int val;
uint32_t mask;
};
struct gpioregulator_init_def {
struct regnode_init_def reg_init_def;
struct gpiobus_pin *enable_pin;
int enable_pin_valid;
int startup_delay_us;
int nstates;
struct gpioregulator_state *states;
int npins;
struct gpiobus_pin **pins;
};
struct gpioregulator_reg_sc {
struct regnode *regnode;
device_t base_dev;
struct regnode_std_param *param;
struct gpioregulator_init_def *def;
};
struct gpioregulator_softc {
device_t dev;
struct gpioregulator_reg_sc *reg_sc;
struct gpioregulator_init_def init_def;
};
static int
gpioregulator_regnode_init(struct regnode *regnode)
{
struct gpioregulator_reg_sc *sc;
int error, n;
sc = regnode_get_softc(regnode);
if (sc->def->enable_pin_valid == 1) {
error = gpio_pin_setflags(sc->def->enable_pin, GPIO_PIN_OUTPUT);
if (error != 0)
return (error);
}
for (n = 0; n < sc->def->npins; n++) {
error = gpio_pin_setflags(sc->def->pins[n], GPIO_PIN_OUTPUT);
if (error != 0)
return (error);
}
return (0);
}
static int
gpioregulator_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
{
struct gpioregulator_reg_sc *sc;
bool active;
int error;
sc = regnode_get_softc(regnode);
if (sc->def->enable_pin_valid == 1) {
active = enable;
if (!sc->param->enable_active_high)
active = !active;
error = gpio_pin_set_active(sc->def->enable_pin, active);
if (error != 0)
return (error);
}
*udelay = sc->def->startup_delay_us;
return (0);
}
static int
gpioregulator_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
int max_uvolt, int *udelay)
{
struct gpioregulator_reg_sc *sc;
const struct gpioregulator_state *state;
int error, n;
sc = regnode_get_softc(regnode);
state = NULL;
for (n = 0; n < sc->def->nstates; n++) {
if (sc->def->states[n].val >= min_uvolt &&
sc->def->states[n].val <= max_uvolt) {
state = &sc->def->states[n];
break;
}
}
if (state == NULL)
return (EINVAL);
for (n = 0; n < sc->def->npins; n++) {
error = gpio_pin_set_active(sc->def->pins[n],
(state->mask >> n) & 1);
if (error != 0)
return (error);
}
*udelay = sc->def->startup_delay_us;
return (0);
}
static int
gpioregulator_regnode_get_voltage(struct regnode *regnode, int *uvolt)
{
struct gpioregulator_reg_sc *sc;
uint32_t mask;
int error, n;
bool active;
sc = regnode_get_softc(regnode);
mask = 0;
for (n = 0; n < sc->def->npins; n++) {
error = gpio_pin_is_active(sc->def->pins[n], &active);
if (error != 0)
return (error);
mask |= (active << n);
}
for (n = 0; n < sc->def->nstates; n++) {
if (sc->def->states[n].mask == mask) {
*uvolt = sc->def->states[n].val;
return (0);
}
}
return (EIO);
}
static regnode_method_t gpioregulator_regnode_methods[] = {
/* Regulator interface */
REGNODEMETHOD(regnode_init, gpioregulator_regnode_init),
REGNODEMETHOD(regnode_enable, gpioregulator_regnode_enable),
REGNODEMETHOD(regnode_set_voltage, gpioregulator_regnode_set_voltage),
REGNODEMETHOD(regnode_get_voltage, gpioregulator_regnode_get_voltage),
REGNODEMETHOD_END
};
DEFINE_CLASS_1(gpioregulator_regnode, gpioregulator_regnode_class,
gpioregulator_regnode_methods, sizeof(struct gpioregulator_reg_sc),
regnode_class);
static int
gpioregulator_parse_fdt(struct gpioregulator_softc *sc)
{
uint32_t *pstates, mask;
phandle_t node;
ssize_t len;
int error, n;
node = ofw_bus_get_node(sc->dev);
pstates = NULL;
mask = 0;
error = regulator_parse_ofw_stdparam(sc->dev, node,
&sc->init_def.reg_init_def);
if (error != 0)
return (error);
/* "states" property (required) */
len = OF_getencprop_alloc_multi(node, "states", sizeof(*pstates),
(void **)&pstates);
if (len < 2) {
device_printf(sc->dev, "invalid 'states' property\n");
error = EINVAL;
goto done;
}
sc->init_def.nstates = len / 2;
sc->init_def.states = malloc(sc->init_def.nstates *
sizeof(*sc->init_def.states), M_DEVBUF, M_WAITOK);
for (n = 0; n < sc->init_def.nstates; n++) {
sc->init_def.states[n].val = pstates[n * 2 + 0];
sc->init_def.states[n].mask = pstates[n * 2 + 1];
mask |= sc->init_def.states[n].mask;
}
/* "startup-delay-us" property (optional) */
len = OF_getencprop(node, "startup-delay-us",
&sc->init_def.startup_delay_us,
sizeof(sc->init_def.startup_delay_us));
if (len <= 0)
sc->init_def.startup_delay_us = 0;
/* "enable-gpio" property (optional) */
error = gpio_pin_get_by_ofw_property(sc->dev, node, "enable-gpio",
&sc->init_def.enable_pin);
if (error == 0)
sc->init_def.enable_pin_valid = 1;
/* "gpios" property */
sc->init_def.npins = 32 - __builtin_clz(mask);
sc->init_def.pins = malloc(sc->init_def.npins *
sizeof(sc->init_def.pins), M_DEVBUF, M_WAITOK);
for (n = 0; n < sc->init_def.npins; n++) {
error = gpio_pin_get_by_ofw_idx(sc->dev, node, n,
&sc->init_def.pins[n]);
if (error != 0) {
device_printf(sc->dev, "cannot get pin %d\n", n);
goto done;
}
}
done:
if (error != 0) {
for (n = 0; n < sc->init_def.npins; n++) {
if (sc->init_def.pins[n] != NULL)
gpio_pin_release(sc->init_def.pins[n]);
}
free(sc->init_def.states, M_DEVBUF);
free(sc->init_def.pins, M_DEVBUF);
}
OF_prop_free(pstates);
return (error);
}
static int
gpioregulator_probe(device_t dev)
{
if (!ofw_bus_is_compatible(dev, "regulator-gpio"))
return (ENXIO);
device_set_desc(dev, "GPIO controlled regulator");
return (BUS_PROBE_GENERIC);
}
static int
gpioregulator_attach(device_t dev)
{
struct gpioregulator_softc *sc;
struct regnode *regnode;
phandle_t node;
int error;
sc = device_get_softc(dev);
sc->dev = dev;
node = ofw_bus_get_node(dev);
error = gpioregulator_parse_fdt(sc);
if (error != 0) {
device_printf(dev, "cannot parse parameters\n");
return (ENXIO);
}
sc->init_def.reg_init_def.id = 1;
sc->init_def.reg_init_def.ofw_node = node;
regnode = regnode_create(dev, &gpioregulator_regnode_class,
&sc->init_def.reg_init_def);
if (regnode == NULL) {
device_printf(dev, "cannot create regulator\n");
return (ENXIO);
}
sc->reg_sc = regnode_get_softc(regnode);
sc->reg_sc->regnode = regnode;
sc->reg_sc->base_dev = dev;
sc->reg_sc->param = regnode_get_stdparam(regnode);
sc->reg_sc->def = &sc->init_def;
regnode_register(regnode);
return (0);
}
static device_method_t gpioregulator_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, gpioregulator_probe),
DEVMETHOD(device_attach, gpioregulator_attach),
/* Regdev interface */
DEVMETHOD(regdev_map, regdev_default_ofw_map),
DEVMETHOD_END
};
static driver_t gpioregulator_driver = {
"gpioregulator",
gpioregulator_methods,
sizeof(struct gpioregulator_softc),
};
static devclass_t gpioregulator_devclass;
EARLY_DRIVER_MODULE(gpioregulator, simplebus, gpioregulator_driver,
gpioregulator_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
MODULE_VERSION(gpioregulator, 1);