[dm][regulator] Update Regulator (#11674)

* [dm][regulator] update regulator

Replace the regulator framework spinlock with a mutex
because regulator operations may sleep.
Move always-on policy into the regulator core,
keep an initial enable reference for always-on supplies,
prevent the last disable from turning them off,
and let fixed/GPIO providers report and control their actual hardware state.

Signed-off-by: GuEe-GUI <2991707448@qq.com>

* [dm][regulator] add PWM regulator support

Signed-off-by: GuEe-GUI <2991707448@qq.com>

* style: format code with clang-format

* style: format complete changed files with clang-format

---------

Signed-off-by: GuEe-GUI <2991707448@qq.com>
This commit is contained in:
GUI
2026-08-07 21:29:33 +08:00
committed by GitHub
parent 19e9be8779
commit c19fe2c823
6 changed files with 799 additions and 148 deletions
+10 -1
View File
@@ -2,12 +2,12 @@ menuconfig RT_USING_REGULATOR
bool "Using Voltage and Current Regulator"
select RT_USING_ADT
select RT_USING_ADT_REF
default n
config RT_REGULATOR_FAN53555
bool "Fairchild FAN53555 / TCS4525 Regulator"
depends on RT_USING_REGULATOR
depends on RT_USING_I2C
depends on RT_USING_DM
default n
config RT_REGULATOR_FIXED
@@ -27,6 +27,15 @@ config RT_REGULATOR_GPIO
depends on RT_USING_PIN
default y
config RT_REGULATOR_PWM
bool "PWM regulator support"
depends on RT_USING_REGULATOR
depends on RT_USING_DM
depends on RT_USING_PWM
depends on RT_USING_PIN
depends on RT_USING_OFW
default n
config RT_REGULATOR_SCMI
bool "SCMI regulator support"
depends on RT_USING_REGULATOR
+3
View File
@@ -19,6 +19,9 @@ if GetDepend(['RT_REGULATOR_FIXED']):
if GetDepend(['RT_REGULATOR_GPIO']):
src += ['regulator-gpio.c']
if GetDepend(['RT_REGULATOR_PWM']):
src += ['regulator-pwm.c']
if GetDepend(['RT_REGULATOR_SCMI']):
src += ['regulator-scmi.c']
+25 -29
View File
@@ -12,10 +12,10 @@
struct regulator_fixed
{
struct rt_regulator_node parent;
struct rt_regulator_node parent;
struct rt_regulator_param param;
rt_base_t enable_pin;
rt_base_t enable_pin;
const char *input_supply;
};
@@ -23,10 +23,10 @@ struct regulator_fixed
static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)
{
struct regulator_fixed *rf = raw_to_regulator_fixed(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)
if (rf->enable_pin < 0)
{
return RT_EOK;
}
@@ -39,32 +39,31 @@ static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)
static rt_err_t regulator_fixed_disable(struct rt_regulator_node *reg_np)
{
struct regulator_fixed *rf = raw_to_regulator_fixed(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)
if (rf->enable_pin < 0)
{
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);
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);
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)
if (rf->enable_pin < 0)
{
return RT_TRUE;
}
rt_pin_mode(rf->enable_pin, PIN_MODE_INPUT);
active = rt_pin_read(rf->enable_pin);
if (param->enable_active_high)
@@ -82,20 +81,19 @@ static int regulator_fixed_get_voltage(struct rt_regulator_node *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,
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));
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)
@@ -105,11 +103,11 @@ static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
regulator_ofw_parse(dev->ofw_node, &rf->param);
rnp = &rf->parent;
rnp = &rf->parent;
rnp->supply_name = rf->param.name;
rnp->ops = &regulator_fixed_ops;
rnp->param = &rf->param;
rnp->dev = &pdev->parent;
rnp->ops = &regulator_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);
@@ -148,16 +146,14 @@ _fail:
return err;
}
static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] =
{
static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] = {
{ .compatible = "regulator-fixed" },
{ /* sentinel */ }
};
static struct rt_platform_driver regulator_fixed_driver =
{
static struct rt_platform_driver regulator_fixed_driver = {
.name = "reg-fixed-voltage",
.ids = regulator_fixed_ofw_ids,
.ids = regulator_fixed_ofw_ids,
.probe = regulator_fixed_probe,
};
+30 -48
View File
@@ -20,7 +20,7 @@ struct regulator_gpio_state
struct regulator_gpio_desc
{
rt_base_t pin;
rt_base_t pin;
rt_uint32_t flags;
};
@@ -30,17 +30,17 @@ struct regulator_gpio
rt_base_t enable_pin;
rt_size_t pins_nr;
rt_size_t pins_nr;
struct regulator_gpio_desc *pins_desc;
int state;
rt_size_t states_nr;
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;
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;
};
@@ -48,14 +48,9 @@ struct regulator_gpio
static rt_err_t regulator_gpio_enable(struct rt_regulator_node *reg_np)
{
struct regulator_gpio *rg = raw_to_regulator_gpio(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);
@@ -67,14 +62,9 @@ static rt_err_t regulator_gpio_enable(struct rt_regulator_node *reg_np)
static rt_err_t regulator_gpio_disable(struct rt_regulator_node *reg_np)
{
struct regulator_gpio *rg = raw_to_regulator_gpio(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);
@@ -86,14 +76,9 @@ static rt_err_t regulator_gpio_disable(struct rt_regulator_node *reg_np)
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 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;
@@ -106,9 +91,9 @@ static rt_bool_t regulator_gpio_is_enabled(struct rt_regulator_node *reg_np)
}
static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
int min_uvolt, int max_uvolt)
int min_uvolt, int max_uvolt)
{
int target = 0, best_val = RT_REGULATOR_UVOLT_INVALID;
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)
@@ -119,7 +104,7 @@ static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
state->value >= min_uvolt &&
state->value <= max_uvolt)
{
target = state->gpios;
target = state->gpios;
best_val = state->value;
}
}
@@ -131,7 +116,7 @@ static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
for (int i = 0; i < rg->pins_nr; ++i)
{
int state = (target >> i) & 1;
int state = (target >> i) & 1;
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];
rt_pin_mode(gpiod->pin, PIN_MODE_OUTPUT);
@@ -158,20 +143,19 @@ static int regulator_gpio_get_voltage(struct rt_regulator_node *reg_np)
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,
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));
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)
@@ -181,11 +165,11 @@ static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
regulator_ofw_parse(dev->ofw_node, &rg->param);
rgp = &rg->parent;
rgp = &rg->parent;
rgp->supply_name = rg->param.name;
rgp->ops = &regulator_gpio_ops;
rgp->param = &rg->param;
rgp->dev = &pdev->parent;
rgp->ops = &regulator_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);
@@ -214,7 +198,7 @@ static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
for (int i = 0; i < rg->pins_nr; ++i)
{
rt_uint32_t val;
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);
@@ -286,16 +270,14 @@ _fail:
return err;
}
static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] =
{
static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] = {
{ .compatible = "regulator-gpio" },
{ /* sentinel */ }
};
static struct rt_platform_driver regulator_gpio_driver =
{
static struct rt_platform_driver regulator_gpio_driver = {
.name = "regulator-gpio",
.ids = regulator_gpio_ofw_ids,
.ids = regulator_gpio_ofw_ids,
.probe = regulator_gpio_probe,
};
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff