rtt更新
This commit is contained in:
23
rt-thread/components/drivers/regulator/Kconfig
Normal file
23
rt-thread/components/drivers/regulator/Kconfig
Normal file
@@ -0,0 +1,23 @@
|
||||
menuconfig RT_USING_REGULATOR
|
||||
bool "Using Voltage and Current Regulator"
|
||||
select RT_USING_ADT
|
||||
select RT_USING_ADT_REF
|
||||
depends on RT_USING_DM
|
||||
default n
|
||||
|
||||
config RT_REGULATOR_FIXED
|
||||
bool "Fixed regulator support"
|
||||
depends on RT_USING_REGULATOR
|
||||
depends on RT_USING_PIN
|
||||
depends on RT_USING_PINCTRL
|
||||
default y
|
||||
|
||||
config RT_REGULATOR_GPIO
|
||||
bool "GPIO regulator support"
|
||||
depends on RT_USING_REGULATOR
|
||||
depends on RT_USING_PIN
|
||||
default y
|
||||
|
||||
if RT_USING_REGULATOR
|
||||
osource "$(SOC_DM_REGULATOR_DIR)/Kconfig"
|
||||
endif
|
21
rt-thread/components/drivers/regulator/SConscript
Normal file
21
rt-thread/components/drivers/regulator/SConscript
Normal file
@@ -0,0 +1,21 @@
|
||||
from building import *
|
||||
|
||||
group = []
|
||||
|
||||
if not GetDepend(['RT_USING_REGULATOR']):
|
||||
Return('group')
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
CPPPATH = [cwd + '/../include']
|
||||
|
||||
src = ['regulator.c', 'regulator_dm.c']
|
||||
|
||||
if GetDepend(['RT_REGULATOR_FIXED']):
|
||||
src += ['regulator-fixed.c']
|
||||
|
||||
if GetDepend(['RT_REGULATOR_GPIO']):
|
||||
src += ['regulator-gpio.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
171
rt-thread/components/drivers/regulator/regulator-fixed.c
Normal file
171
rt-thread/components/drivers/regulator/regulator-fixed.c
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-09-23 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include "regulator_dm.h"
|
||||
|
||||
struct regulator_fixed
|
||||
{
|
||||
struct rt_regulator_node parent;
|
||||
struct rt_regulator_param param;
|
||||
|
||||
rt_base_t enable_pin;
|
||||
const char *input_supply;
|
||||
};
|
||||
|
||||
#define raw_to_regulator_fixed(raw) rt_container_of(raw, struct regulator_fixed, parent)
|
||||
|
||||
static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
|
||||
struct rt_regulator_param *param = &rf->param;
|
||||
|
||||
if (rf->enable_pin < 0 || param->always_on)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_pin_mode(rf->enable_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_HIGH : PIN_LOW);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t regulator_fixed_disable(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
|
||||
struct rt_regulator_param *param = &rf->param;
|
||||
|
||||
if (rf->enable_pin < 0 || param->always_on)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_pin_mode(rf->enable_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_LOW: PIN_HIGH);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_bool_t regulator_fixed_is_enabled(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
rt_uint8_t active;
|
||||
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
|
||||
struct rt_regulator_param *param = &rf->param;
|
||||
|
||||
if (rf->enable_pin < 0 || param->always_on)
|
||||
{
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
rt_pin_mode(rf->enable_pin, PIN_MODE_INPUT);
|
||||
active = rt_pin_read(rf->enable_pin);
|
||||
|
||||
if (param->enable_active_high)
|
||||
{
|
||||
return active == PIN_HIGH;
|
||||
}
|
||||
|
||||
return active == PIN_LOW;
|
||||
}
|
||||
|
||||
static int regulator_fixed_get_voltage(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
|
||||
|
||||
return rf->param.min_uvolt + (rf->param.max_uvolt - rf->param.min_uvolt) / 2;
|
||||
}
|
||||
|
||||
static const struct rt_regulator_ops regulator_fixed_ops =
|
||||
{
|
||||
.enable = regulator_fixed_enable,
|
||||
.disable = regulator_fixed_disable,
|
||||
.is_enabled = regulator_fixed_is_enabled,
|
||||
.get_voltage = regulator_fixed_get_voltage,
|
||||
};
|
||||
|
||||
static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
|
||||
{
|
||||
rt_err_t err;
|
||||
rt_uint32_t val;
|
||||
struct rt_device *dev = &pdev->parent;
|
||||
struct regulator_fixed *rf = rt_calloc(1, sizeof(*rf));
|
||||
struct rt_regulator_node *rnp;
|
||||
|
||||
if (!rf)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
regulator_ofw_parse(dev->ofw_node, &rf->param);
|
||||
|
||||
rnp = &rf->parent;
|
||||
rnp->supply_name = rf->param.name;
|
||||
rnp->ops = ®ulator_fixed_ops;
|
||||
rnp->param = &rf->param;
|
||||
rnp->dev = &pdev->parent;
|
||||
|
||||
rf->enable_pin = rt_pin_get_named_pin(dev, "enable", 0, RT_NULL, RT_NULL);
|
||||
|
||||
if (rf->enable_pin < 0)
|
||||
{
|
||||
rf->enable_pin = rt_pin_get_named_pin(dev, RT_NULL, 0, RT_NULL, RT_NULL);
|
||||
}
|
||||
|
||||
if (rf->enable_pin < 0)
|
||||
{
|
||||
rf->enable_pin = -1;
|
||||
}
|
||||
|
||||
rt_pin_ctrl_confs_apply(dev, 0);
|
||||
|
||||
if (!rt_dm_dev_prop_read_u32(dev, "startup-delay-us", &val))
|
||||
{
|
||||
rf->param.enable_delay = val;
|
||||
}
|
||||
|
||||
if (!rt_dm_dev_prop_read_u32(dev, "off-on-delay-us", &val))
|
||||
{
|
||||
rf->param.off_on_delay = val;
|
||||
}
|
||||
|
||||
if ((err = rt_regulator_register(rnp)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
|
||||
_fail:
|
||||
rt_free(rf);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] =
|
||||
{
|
||||
{ .compatible = "regulator-fixed" },
|
||||
{ /* sentinel */ }
|
||||
};
|
||||
|
||||
static struct rt_platform_driver regulator_fixed_driver =
|
||||
{
|
||||
.name = "reg-fixed-voltage",
|
||||
.ids = regulator_fixed_ofw_ids,
|
||||
|
||||
.probe = regulator_fixed_probe,
|
||||
};
|
||||
|
||||
static int regulator_fixed_register(void)
|
||||
{
|
||||
rt_platform_driver_register(®ulator_fixed_driver);
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_SUBSYS_EXPORT(regulator_fixed_register);
|
309
rt-thread/components/drivers/regulator/regulator-gpio.c
Normal file
309
rt-thread/components/drivers/regulator/regulator-gpio.c
Normal file
@@ -0,0 +1,309 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-09-23 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include <dt-bindings/pin/state.h>
|
||||
|
||||
#include "regulator_dm.h"
|
||||
|
||||
struct regulator_gpio_state
|
||||
{
|
||||
rt_uint32_t value;
|
||||
rt_uint32_t gpios;
|
||||
};
|
||||
|
||||
struct regulator_gpio_desc
|
||||
{
|
||||
rt_base_t pin;
|
||||
rt_uint32_t flags;
|
||||
};
|
||||
|
||||
struct regulator_gpio
|
||||
{
|
||||
struct rt_regulator_node parent;
|
||||
|
||||
rt_base_t enable_pin;
|
||||
|
||||
rt_size_t pins_nr;
|
||||
struct regulator_gpio_desc *pins_desc;
|
||||
|
||||
int state;
|
||||
rt_size_t states_nr;
|
||||
struct regulator_gpio_state *states;
|
||||
|
||||
const char *input_supply;
|
||||
rt_uint32_t startup_delay;
|
||||
rt_uint32_t off_on_delay;
|
||||
rt_bool_t enabled_at_boot;
|
||||
struct rt_regulator_param param;
|
||||
};
|
||||
|
||||
#define raw_to_regulator_gpio(raw) rt_container_of(raw, struct regulator_gpio, parent)
|
||||
|
||||
static rt_err_t regulator_gpio_enable(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
|
||||
struct rt_regulator_param *param = &rg->param;
|
||||
|
||||
if (param->always_on)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
if (rg->enable_pin >= 0)
|
||||
{
|
||||
rt_pin_mode(rg->enable_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_write(rg->enable_pin, param->enable_active_high ? PIN_HIGH : PIN_LOW);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t regulator_gpio_disable(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
|
||||
struct rt_regulator_param *param = &rg->param;
|
||||
|
||||
if (param->always_on)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
if (rg->enable_pin >= 0)
|
||||
{
|
||||
rt_pin_mode(rg->enable_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_write(rg->enable_pin, param->enable_active_high ? PIN_LOW : PIN_HIGH);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_bool_t regulator_gpio_is_enabled(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
|
||||
struct rt_regulator_param *param = &rg->param;
|
||||
|
||||
if (param->always_on)
|
||||
{
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
if (rg->enable_pin >= 0)
|
||||
{
|
||||
rt_uint8_t active_val = param->enable_active_high ? PIN_LOW : PIN_HIGH;
|
||||
|
||||
rt_pin_mode(rg->enable_pin, PIN_MODE_INPUT);
|
||||
return rt_pin_read(rg->enable_pin) == active_val;
|
||||
}
|
||||
|
||||
return RT_TRUE;
|
||||
}
|
||||
|
||||
static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
|
||||
int min_uvolt, int max_uvolt)
|
||||
{
|
||||
int target = 0, best_val = RT_REGULATOR_UVOLT_INVALID;
|
||||
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
|
||||
|
||||
for (int i = 0; i < rg->states_nr; ++i)
|
||||
{
|
||||
struct regulator_gpio_state *state = &rg->states[i];
|
||||
|
||||
if (state->value < best_val &&
|
||||
state->value >= min_uvolt &&
|
||||
state->value <= max_uvolt)
|
||||
{
|
||||
target = state->gpios;
|
||||
best_val = state->value;
|
||||
}
|
||||
}
|
||||
|
||||
if (best_val == RT_REGULATOR_UVOLT_INVALID)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rg->pins_nr; ++i)
|
||||
{
|
||||
int state = (target >> i) & 1;
|
||||
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];
|
||||
|
||||
rt_pin_mode(gpiod->pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_write(gpiod->pin, gpiod->flags == PIND_OUT_HIGH ? state : !state);
|
||||
}
|
||||
|
||||
rg->state = target;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int regulator_gpio_get_voltage(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
|
||||
|
||||
for (int i = 0; i < rg->states_nr; ++i)
|
||||
{
|
||||
if (rg->states[i].gpios == rg->state)
|
||||
{
|
||||
return rg->states[i].value;
|
||||
}
|
||||
}
|
||||
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
static const struct rt_regulator_ops regulator_gpio_ops =
|
||||
{
|
||||
.enable = regulator_gpio_enable,
|
||||
.disable = regulator_gpio_disable,
|
||||
.is_enabled = regulator_gpio_is_enabled,
|
||||
.set_voltage = regulator_gpio_set_voltage,
|
||||
.get_voltage = regulator_gpio_get_voltage,
|
||||
};
|
||||
|
||||
static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_device *dev = &pdev->parent;
|
||||
struct regulator_gpio *rg = rt_calloc(1, sizeof(*rg));
|
||||
struct rt_regulator_node *rgp;
|
||||
|
||||
if (!rg)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
regulator_ofw_parse(dev->ofw_node, &rg->param);
|
||||
|
||||
rgp = &rg->parent;
|
||||
rgp->supply_name = rg->param.name;
|
||||
rgp->ops = ®ulator_gpio_ops;
|
||||
rgp->param = &rg->param;
|
||||
rgp->dev = &pdev->parent;
|
||||
|
||||
rt_dm_dev_prop_read_u32(dev, "startup-delay-us", &rg->startup_delay);
|
||||
rt_dm_dev_prop_read_u32(dev, "off-on-delay-us", &rg->off_on_delay);
|
||||
|
||||
/* GPIO flags are ignored, we check by enable-active-high */
|
||||
rg->enable_pin = rt_pin_get_named_pin(dev, "enable", 0, RT_NULL, RT_NULL);
|
||||
|
||||
if (rg->enable_pin < 0 && rg->enable_pin != -RT_EEMPTY)
|
||||
{
|
||||
err = rg->enable_pin;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
rg->pins_nr = rt_pin_get_named_pin_count(dev, "gpios");
|
||||
|
||||
if (rg->pins_nr > 0)
|
||||
{
|
||||
rg->pins_desc = rt_malloc(sizeof(*rg->pins_desc) * rg->pins_nr);
|
||||
|
||||
if (!rg->pins_desc)
|
||||
{
|
||||
err = -RT_ENOMEM;
|
||||
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rg->pins_nr; ++i)
|
||||
{
|
||||
rt_uint32_t val;
|
||||
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];
|
||||
|
||||
gpiod->pin = rt_pin_get_named_pin(dev, RT_NULL, i, RT_NULL, RT_NULL);
|
||||
|
||||
if (gpiod->pin < 0)
|
||||
{
|
||||
err = gpiod->pin;
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
if (rt_dm_dev_prop_read_u32_index(dev, "gpios-states", i, &val) < 0)
|
||||
{
|
||||
gpiod->flags = PIND_OUT_HIGH;
|
||||
}
|
||||
else
|
||||
{
|
||||
gpiod->flags = val ? PIND_OUT_HIGH : PIND_OUT_LOW;
|
||||
}
|
||||
|
||||
if (gpiod->flags == PIND_OUT_HIGH)
|
||||
{
|
||||
rg->state |= 1 << i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rg->states_nr = rt_dm_dev_prop_count_of_u32(dev, "states") / 2;
|
||||
|
||||
if (rg->states_nr < 0)
|
||||
{
|
||||
err = -RT_EIO;
|
||||
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
rg->states = rt_malloc(sizeof(*rg->states) * rg->states_nr);
|
||||
|
||||
if (!rg->states)
|
||||
{
|
||||
err = -RT_ENOMEM;
|
||||
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rg->states_nr; ++i)
|
||||
{
|
||||
rt_dm_dev_prop_read_u32_index(dev, "states", i * 2, &rg->states[i].value);
|
||||
rt_dm_dev_prop_read_u32_index(dev, "states", i * 2 + 1, &rg->states[i].gpios);
|
||||
}
|
||||
|
||||
if ((err = rt_regulator_register(rgp)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
|
||||
_fail:
|
||||
if (rg->pins_desc)
|
||||
{
|
||||
rt_free(rg->pins_desc);
|
||||
}
|
||||
if (rg->states)
|
||||
{
|
||||
rt_free(rg->states);
|
||||
}
|
||||
rt_free(rg);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] =
|
||||
{
|
||||
{ .compatible = "regulator-gpio" },
|
||||
{ /* sentinel */ }
|
||||
};
|
||||
|
||||
static struct rt_platform_driver regulator_gpio_driver =
|
||||
{
|
||||
.name = "regulator-gpio",
|
||||
.ids = regulator_gpio_ofw_ids,
|
||||
|
||||
.probe = regulator_gpio_probe,
|
||||
};
|
||||
|
||||
static int regulator_gpio_register(void)
|
||||
{
|
||||
rt_platform_driver_register(®ulator_gpio_driver);
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_SUBSYS_EXPORT(regulator_gpio_register);
|
629
rt-thread/components/drivers/regulator/regulator.c
Normal file
629
rt-thread/components/drivers/regulator/regulator.c
Normal file
@@ -0,0 +1,629 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-09-23 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtservice.h>
|
||||
|
||||
#define DBG_TAG "rtdm.regulator"
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include <rtdbg.h>
|
||||
|
||||
#include <drivers/ofw.h>
|
||||
#include <drivers/platform.h>
|
||||
#include <drivers/regulator.h>
|
||||
|
||||
struct rt_regulator
|
||||
{
|
||||
struct rt_regulator_node *reg_np;
|
||||
};
|
||||
|
||||
static struct rt_spinlock _regulator_lock = { 0 };
|
||||
|
||||
static rt_err_t regulator_enable(struct rt_regulator_node *reg_np);
|
||||
static rt_err_t regulator_disable(struct rt_regulator_node *reg_np);
|
||||
|
||||
rt_err_t rt_regulator_register(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
const struct rt_regulator_param *param;
|
||||
|
||||
if (!reg_np || !reg_np->dev || !reg_np->param || !reg_np->ops)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_list_init(®_np->list);
|
||||
rt_list_init(®_np->children_nodes);
|
||||
rt_list_init(®_np->notifier_nodes);
|
||||
rt_ref_init(®_np->ref);
|
||||
rt_atomic_store(®_np->enabled_count, 0);
|
||||
|
||||
param = reg_np->param;
|
||||
|
||||
reg_np->parent = RT_NULL;
|
||||
|
||||
#ifdef RT_USING_OFW
|
||||
if (reg_np->dev->ofw_node)
|
||||
{
|
||||
rt_ofw_data(reg_np->dev->ofw_node) = reg_np;
|
||||
}
|
||||
#endif /* RT_USING_OFW */
|
||||
|
||||
if (param->boot_on || param->always_on)
|
||||
{
|
||||
regulator_enable(reg_np);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_regulator_unregister(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
if (!reg_np)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
if (rt_atomic_load(®_np->enabled_count) != 0)
|
||||
{
|
||||
err = -RT_EBUSY;
|
||||
|
||||
LOG_E("%s was enabled by consumer", reg_np->supply_name);
|
||||
|
||||
goto _unlock;
|
||||
}
|
||||
|
||||
if (!(reg_np->param->boot_on || reg_np->param->always_on))
|
||||
{
|
||||
regulator_disable(reg_np);
|
||||
}
|
||||
|
||||
if (!rt_list_isempty(®_np->children_nodes) || rt_ref_read(®_np->ref) > 1)
|
||||
{
|
||||
err = -RT_EBUSY;
|
||||
|
||||
goto _unlock;
|
||||
}
|
||||
|
||||
reg_np->parent = RT_NULL;
|
||||
rt_list_remove(®_np->list);
|
||||
|
||||
_unlock:
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t rt_regulator_notifier_register(struct rt_regulator *reg,
|
||||
struct rt_regulator_notifier *notifier)
|
||||
{
|
||||
struct rt_regulator_node *reg_np;
|
||||
|
||||
if (!reg || !notifier)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
reg_np = reg->reg_np;
|
||||
notifier->regulator = reg;
|
||||
|
||||
rt_list_init(¬ifier->list);
|
||||
rt_list_insert_after(®_np->notifier_nodes, ¬ifier->list);
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t rt_regulator_notifier_unregister(struct rt_regulator *reg,
|
||||
struct rt_regulator_notifier *notifier)
|
||||
{
|
||||
if (!reg || !notifier)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
rt_list_remove(¬ifier->list);
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t regulator_notifier_call_chain(struct rt_regulator_node *reg_np,
|
||||
rt_ubase_t msg, void *data)
|
||||
{
|
||||
rt_err_t err = RT_EOK;
|
||||
struct rt_regulator_notifier *notifier;
|
||||
rt_list_t *head = ®_np->notifier_nodes;
|
||||
|
||||
if (rt_list_isempty(head))
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_list_for_each_entry(notifier, head, list)
|
||||
{
|
||||
err = notifier->callback(notifier, msg, data);
|
||||
|
||||
if (err == -RT_EIO)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_uint32_t regulator_get_enable_time(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
if (reg_np->param->enable_delay)
|
||||
{
|
||||
return reg_np->param->enable_delay;
|
||||
}
|
||||
|
||||
if (reg_np->ops->enable_time)
|
||||
{
|
||||
return reg_np->ops->enable_time(reg_np);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void regulator_delay(rt_uint32_t delay)
|
||||
{
|
||||
rt_uint32_t ms = delay / 1000;
|
||||
rt_uint32_t us = delay % 1000;
|
||||
|
||||
if (ms > 0)
|
||||
{
|
||||
/*
|
||||
* For small enough values, handle super-millisecond
|
||||
* delays in the usleep_range() call below.
|
||||
*/
|
||||
if (ms < 20)
|
||||
{
|
||||
us += ms * 1000;
|
||||
}
|
||||
else if (rt_thread_self())
|
||||
{
|
||||
rt_thread_mdelay(ms);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_hw_us_delay(ms * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Give the scheduler some room to coalesce with any other
|
||||
* wakeup sources. For delays shorter than 10 us, don't even
|
||||
* bother setting up high-resolution timers and just busy-loop.
|
||||
*/
|
||||
if (us >= 10)
|
||||
{
|
||||
rt_hw_us_delay((us + 100) >> 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_hw_us_delay(us);
|
||||
}
|
||||
}
|
||||
|
||||
static rt_err_t regulator_enable(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
rt_err_t err = RT_EOK;
|
||||
rt_uint32_t enable_delay = regulator_get_enable_time(reg_np);
|
||||
|
||||
if (reg_np->ops->enable)
|
||||
{
|
||||
err = reg_np->ops->enable(reg_np);
|
||||
|
||||
if (!err)
|
||||
{
|
||||
if (enable_delay)
|
||||
{
|
||||
regulator_delay(enable_delay);
|
||||
}
|
||||
|
||||
rt_atomic_add(®_np->enabled_count, 1);
|
||||
err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_ENABLE, RT_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if (!err && reg_np->parent)
|
||||
{
|
||||
err = regulator_enable(reg_np->parent);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t rt_regulator_enable(struct rt_regulator *reg)
|
||||
{
|
||||
rt_err_t err;
|
||||
|
||||
if (!reg)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
if (rt_regulator_is_enabled(reg))
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
err = regulator_enable(reg->reg_np);
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t regulator_disable(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
if (reg_np->ops->disable)
|
||||
{
|
||||
err = reg_np->ops->disable(reg_np);
|
||||
|
||||
if (!err)
|
||||
{
|
||||
if (reg_np->param->off_on_delay)
|
||||
{
|
||||
regulator_delay(reg_np->param->off_on_delay);
|
||||
}
|
||||
|
||||
err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_DISABLE, RT_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if (!err && reg_np->parent)
|
||||
{
|
||||
err = regulator_disable(reg_np->parent);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t rt_regulator_disable(struct rt_regulator *reg)
|
||||
{
|
||||
rt_err_t err;
|
||||
|
||||
if (!reg)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
if (!rt_regulator_is_enabled(reg))
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
if (rt_atomic_load(®->reg_np->enabled_count) != 0)
|
||||
{
|
||||
rt_atomic_sub(®->reg_np->enabled_count, 1);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
err = regulator_disable(reg->reg_np);
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_bool_t rt_regulator_is_enabled(struct rt_regulator *reg)
|
||||
{
|
||||
if (!reg)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
if (reg->reg_np->ops->is_enabled)
|
||||
{
|
||||
return reg->reg_np->ops->is_enabled(reg->reg_np);
|
||||
}
|
||||
|
||||
return rt_atomic_load(®->reg_np->enabled_count) > 0;
|
||||
}
|
||||
|
||||
static rt_err_t regulator_set_voltage(struct rt_regulator_node *reg_np, int min_uvolt, int max_uvolt)
|
||||
{
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
if (reg_np->ops->set_voltage)
|
||||
{
|
||||
union rt_regulator_notifier_args args;
|
||||
|
||||
RT_ASSERT(reg_np->ops->get_voltage != RT_NULL);
|
||||
|
||||
args.old_uvolt = reg_np->ops->get_voltage(reg_np);
|
||||
args.min_uvolt = min_uvolt;
|
||||
args.max_uvolt = max_uvolt;
|
||||
|
||||
err = regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_VOLTAGE_CHANGE, &args);
|
||||
|
||||
if (!err)
|
||||
{
|
||||
err = reg_np->ops->set_voltage(reg_np, min_uvolt, max_uvolt);
|
||||
}
|
||||
|
||||
if (err)
|
||||
{
|
||||
regulator_notifier_call_chain(reg_np, RT_REGULATOR_MSG_VOLTAGE_CHANGE_ERR,
|
||||
(void *)(rt_base_t)args.old_uvolt);
|
||||
}
|
||||
}
|
||||
|
||||
if (!err && reg_np->parent)
|
||||
{
|
||||
err = regulator_set_voltage(reg_np->parent, min_uvolt, max_uvolt);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_bool_t rt_regulator_is_supported_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt)
|
||||
{
|
||||
const struct rt_regulator_param *param;
|
||||
|
||||
RT_ASSERT(reg != RT_NULL);
|
||||
|
||||
param = reg->reg_np->param;
|
||||
|
||||
if (!param)
|
||||
{
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
return param->min_uvolt <= min_uvolt && param->max_uvolt >= max_uvolt;
|
||||
}
|
||||
|
||||
rt_err_t rt_regulator_set_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt)
|
||||
{
|
||||
rt_err_t err;
|
||||
|
||||
if (!reg)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
err = regulator_set_voltage(reg->reg_np, min_uvolt, max_uvolt);
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int rt_regulator_get_voltage(struct rt_regulator *reg)
|
||||
{
|
||||
int uvolt = RT_REGULATOR_UVOLT_INVALID;
|
||||
struct rt_regulator_node *reg_np;
|
||||
|
||||
if (!reg)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
reg_np = reg->reg_np;
|
||||
|
||||
if (reg_np->ops->get_voltage)
|
||||
{
|
||||
uvolt = reg_np->ops->get_voltage(reg->reg_np);
|
||||
}
|
||||
else
|
||||
{
|
||||
uvolt = -RT_ENOSYS;
|
||||
}
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return uvolt;
|
||||
}
|
||||
|
||||
rt_err_t rt_regulator_set_mode(struct rt_regulator *reg, rt_uint32_t mode)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_regulator_node *reg_np;
|
||||
|
||||
if (!reg)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
reg_np = reg->reg_np;
|
||||
|
||||
if (reg_np->ops->set_mode)
|
||||
{
|
||||
err = reg_np->ops->set_mode(reg_np, mode);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = -RT_ENOSYS;
|
||||
}
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_int32_t rt_regulator_get_mode(struct rt_regulator *reg)
|
||||
{
|
||||
rt_int32_t mode;
|
||||
struct rt_regulator_node *reg_np;
|
||||
|
||||
if (!reg)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
reg_np = reg->reg_np;
|
||||
|
||||
if (reg_np->ops->get_mode)
|
||||
{
|
||||
mode = reg_np->ops->get_mode(reg_np);
|
||||
}
|
||||
else
|
||||
{
|
||||
mode = -RT_ENOSYS;
|
||||
}
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
static void regulator_check_parent(struct rt_regulator_node *reg_np)
|
||||
{
|
||||
if (reg_np->parent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef RT_USING_OFW
|
||||
rt_phandle parent_phandle = 0;
|
||||
struct rt_ofw_node *np = reg_np->dev->ofw_node;
|
||||
|
||||
while (np)
|
||||
{
|
||||
if (rt_ofw_prop_read_u32(np, "vin-supply", &parent_phandle))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(np = rt_ofw_find_node_by_phandle(parent_phandle)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(reg_np->parent = rt_ofw_data(np)))
|
||||
{
|
||||
LOG_W("%s parent ofw node = %s not init",
|
||||
reg_np->supply_name, rt_ofw_node_full_name(np));
|
||||
|
||||
rt_ofw_node_put(np);
|
||||
break;
|
||||
}
|
||||
|
||||
rt_list_insert_after(®_np->parent->children_nodes, ®_np->list);
|
||||
rt_ofw_node_put(np);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
struct rt_regulator *rt_regulator_get(struct rt_device *dev, const char *id)
|
||||
{
|
||||
struct rt_regulator *reg = RT_NULL;
|
||||
struct rt_regulator_node *reg_np = RT_NULL;
|
||||
|
||||
if (!dev || !id)
|
||||
{
|
||||
reg = rt_err_ptr(-RT_EINVAL);
|
||||
goto _end;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_OFW
|
||||
if (dev->ofw_node)
|
||||
{
|
||||
rt_phandle supply_phandle;
|
||||
struct rt_ofw_node *np = dev->ofw_node;
|
||||
char supply_name[64];
|
||||
|
||||
rt_snprintf(supply_name, sizeof(supply_name), "%s-supply", id);
|
||||
|
||||
if (rt_ofw_prop_read_u32(np, supply_name, &supply_phandle))
|
||||
{
|
||||
goto _end;
|
||||
}
|
||||
|
||||
if (!(np = rt_ofw_find_node_by_phandle(supply_phandle)))
|
||||
{
|
||||
reg = rt_err_ptr(-RT_EIO);
|
||||
goto _end;
|
||||
}
|
||||
|
||||
if (!rt_ofw_data(np))
|
||||
{
|
||||
rt_platform_ofw_request(np);
|
||||
}
|
||||
|
||||
reg_np = rt_ofw_data(np);
|
||||
rt_ofw_node_put(np);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!reg_np)
|
||||
{
|
||||
reg = rt_err_ptr(-RT_ENOSYS);
|
||||
goto _end;
|
||||
}
|
||||
|
||||
rt_hw_spin_lock(&_regulator_lock.lock);
|
||||
|
||||
regulator_check_parent(reg_np);
|
||||
|
||||
rt_hw_spin_unlock(&_regulator_lock.lock);
|
||||
|
||||
reg = rt_calloc(1, sizeof(*reg));
|
||||
|
||||
if (!reg)
|
||||
{
|
||||
reg = rt_err_ptr(-RT_ENOMEM);
|
||||
goto _end;
|
||||
}
|
||||
|
||||
reg->reg_np = reg_np;
|
||||
rt_ref_get(®_np->ref);
|
||||
|
||||
_end:
|
||||
return reg;
|
||||
}
|
||||
|
||||
static void regulator_release(struct rt_ref *r)
|
||||
{
|
||||
struct rt_regulator_node *reg_np = rt_container_of(r, struct rt_regulator_node, ref);
|
||||
|
||||
rt_regulator_unregister(reg_np);
|
||||
}
|
||||
|
||||
void rt_regulator_put(struct rt_regulator *reg)
|
||||
{
|
||||
if (!reg)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
rt_ref_put(®->reg_np->ref, ®ulator_release);
|
||||
rt_free(reg);
|
||||
}
|
59
rt-thread/components/drivers/regulator/regulator_dm.c
Normal file
59
rt-thread/components/drivers/regulator/regulator_dm.c
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-09-23 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#include "regulator_dm.h"
|
||||
|
||||
#ifdef RT_USING_OFW
|
||||
rt_err_t regulator_ofw_parse(struct rt_ofw_node *np, struct rt_regulator_param *param)
|
||||
{
|
||||
rt_uint32_t pval;
|
||||
|
||||
param->name = rt_ofw_prop_read_raw(np, "regulator-name", RT_NULL);
|
||||
|
||||
if (!rt_ofw_prop_read_u32(np, "regulator-min-microvolt", &pval))
|
||||
{
|
||||
param->min_uvolt = pval;
|
||||
}
|
||||
|
||||
if (!rt_ofw_prop_read_u32(np, "regulator-max-microvolt", &pval))
|
||||
{
|
||||
param->max_uvolt = pval;
|
||||
}
|
||||
|
||||
if (!rt_ofw_prop_read_u32(np, "regulator-min-microamp", &pval))
|
||||
{
|
||||
param->min_uamp = pval;
|
||||
}
|
||||
|
||||
if (!rt_ofw_prop_read_u32(np, "regulator-max-microamp", &pval))
|
||||
{
|
||||
param->max_uamp = pval;
|
||||
}
|
||||
|
||||
if (!rt_ofw_prop_read_u32(np, "regulator-ramp-delay", &pval))
|
||||
{
|
||||
param->ramp_delay = pval;
|
||||
}
|
||||
|
||||
if (!rt_ofw_prop_read_u32(np, "regulator-enable-ramp-delay", &pval))
|
||||
{
|
||||
param->enable_delay = pval;
|
||||
}
|
||||
|
||||
param->enable_active_high = rt_ofw_prop_read_bool(np, "enable-active-high");
|
||||
param->boot_on = rt_ofw_prop_read_bool(np, "regulator-boot-on");
|
||||
param->always_on = rt_ofw_prop_read_bool(np, "regulator-always-on");
|
||||
param->soft_start = rt_ofw_prop_read_bool(np, "regulator-soft-start");
|
||||
param->pull_down = rt_ofw_prop_read_bool(np, "regulator-pull-down");
|
||||
param->over_current_protection = rt_ofw_prop_read_bool(np, "regulator-over-current-protection");
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
#endif /* RT_USING_OFW */
|
26
rt-thread/components/drivers/regulator/regulator_dm.h
Normal file
26
rt-thread/components/drivers/regulator/regulator_dm.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-09-23 GuEe-GUI first version
|
||||
*/
|
||||
|
||||
#ifndef __REGULATOR_DM_H__
|
||||
#define __REGULATOR_DM_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_OFW
|
||||
rt_err_t regulator_ofw_parse(struct rt_ofw_node *np, struct rt_regulator_param *param);
|
||||
#else
|
||||
rt_inline rt_err_t regulator_ofw_parse(struct rt_ofw_node *np, struct rt_regulator_param *param);
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
#endif /* RT_USING_OFW */
|
||||
|
||||
#endif /* __REGULATOR_DM_H__ */
|
Reference in New Issue
Block a user