Skip to content

Commit c19fe2c

Browse files
authored
[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>
1 parent 19e9be8 commit c19fe2c

6 files changed

Lines changed: 799 additions & 148 deletions

File tree

components/drivers/regulator/Kconfig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ menuconfig RT_USING_REGULATOR
22
bool "Using Voltage and Current Regulator"
33
select RT_USING_ADT
44
select RT_USING_ADT_REF
5-
default n
65

76
config RT_REGULATOR_FAN53555
87
bool "Fairchild FAN53555 / TCS4525 Regulator"
98
depends on RT_USING_REGULATOR
109
depends on RT_USING_I2C
10+
depends on RT_USING_DM
1111
default n
1212

1313
config RT_REGULATOR_FIXED
@@ -27,6 +27,15 @@ config RT_REGULATOR_GPIO
2727
depends on RT_USING_PIN
2828
default y
2929

30+
config RT_REGULATOR_PWM
31+
bool "PWM regulator support"
32+
depends on RT_USING_REGULATOR
33+
depends on RT_USING_DM
34+
depends on RT_USING_PWM
35+
depends on RT_USING_PIN
36+
depends on RT_USING_OFW
37+
default n
38+
3039
config RT_REGULATOR_SCMI
3140
bool "SCMI regulator support"
3241
depends on RT_USING_REGULATOR

components/drivers/regulator/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ if GetDepend(['RT_REGULATOR_FIXED']):
1919
if GetDepend(['RT_REGULATOR_GPIO']):
2020
src += ['regulator-gpio.c']
2121

22+
if GetDepend(['RT_REGULATOR_PWM']):
23+
src += ['regulator-pwm.c']
24+
2225
if GetDepend(['RT_REGULATOR_SCMI']):
2326
src += ['regulator-scmi.c']
2427

components/drivers/regulator/regulator-fixed.c

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212

1313
struct regulator_fixed
1414
{
15-
struct rt_regulator_node parent;
15+
struct rt_regulator_node parent;
1616
struct rt_regulator_param param;
1717

18-
rt_base_t enable_pin;
18+
rt_base_t enable_pin;
1919
const char *input_supply;
2020
};
2121

2222
#define raw_to_regulator_fixed(raw) rt_container_of(raw, struct regulator_fixed, parent)
2323

2424
static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)
2525
{
26-
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
26+
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
2727
struct rt_regulator_param *param = &rf->param;
2828

29-
if (rf->enable_pin < 0 || param->always_on)
29+
if (rf->enable_pin < 0)
3030
{
3131
return RT_EOK;
3232
}
@@ -39,32 +39,31 @@ static rt_err_t regulator_fixed_enable(struct rt_regulator_node *reg_np)
3939

4040
static rt_err_t regulator_fixed_disable(struct rt_regulator_node *reg_np)
4141
{
42-
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
42+
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
4343
struct rt_regulator_param *param = &rf->param;
4444

45-
if (rf->enable_pin < 0 || param->always_on)
45+
if (rf->enable_pin < 0)
4646
{
4747
return RT_EOK;
4848
}
4949

5050
rt_pin_mode(rf->enable_pin, PIN_MODE_OUTPUT);
51-
rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_LOW: PIN_HIGH);
51+
rt_pin_write(rf->enable_pin, param->enable_active_high ? PIN_LOW : PIN_HIGH);
5252

5353
return RT_EOK;
5454
}
5555

5656
static rt_bool_t regulator_fixed_is_enabled(struct rt_regulator_node *reg_np)
5757
{
58-
rt_uint8_t active;
59-
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
58+
rt_uint8_t active;
59+
struct regulator_fixed *rf = raw_to_regulator_fixed(reg_np);
6060
struct rt_regulator_param *param = &rf->param;
6161

62-
if (rf->enable_pin < 0 || param->always_on)
62+
if (rf->enable_pin < 0)
6363
{
6464
return RT_TRUE;
6565
}
6666

67-
rt_pin_mode(rf->enable_pin, PIN_MODE_INPUT);
6867
active = rt_pin_read(rf->enable_pin);
6968

7069
if (param->enable_active_high)
@@ -82,20 +81,19 @@ static int regulator_fixed_get_voltage(struct rt_regulator_node *reg_np)
8281
return rf->param.min_uvolt + (rf->param.max_uvolt - rf->param.min_uvolt) / 2;
8382
}
8483

85-
static const struct rt_regulator_ops regulator_fixed_ops =
86-
{
87-
.enable = regulator_fixed_enable,
88-
.disable = regulator_fixed_disable,
89-
.is_enabled = regulator_fixed_is_enabled,
84+
static const struct rt_regulator_ops regulator_fixed_ops = {
85+
.enable = regulator_fixed_enable,
86+
.disable = regulator_fixed_disable,
87+
.is_enabled = regulator_fixed_is_enabled,
9088
.get_voltage = regulator_fixed_get_voltage,
9189
};
9290

9391
static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
9492
{
95-
rt_err_t err;
96-
rt_uint32_t val;
97-
struct rt_device *dev = &pdev->parent;
98-
struct regulator_fixed *rf = rt_calloc(1, sizeof(*rf));
93+
rt_err_t err;
94+
rt_uint32_t val;
95+
struct rt_device *dev = &pdev->parent;
96+
struct regulator_fixed *rf = rt_calloc(1, sizeof(*rf));
9997
struct rt_regulator_node *rnp;
10098

10199
if (!rf)
@@ -105,11 +103,11 @@ static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
105103

106104
regulator_ofw_parse(dev->ofw_node, &rf->param);
107105

108-
rnp = &rf->parent;
106+
rnp = &rf->parent;
109107
rnp->supply_name = rf->param.name;
110-
rnp->ops = &regulator_fixed_ops;
111-
rnp->param = &rf->param;
112-
rnp->dev = &pdev->parent;
108+
rnp->ops = &regulator_fixed_ops;
109+
rnp->param = &rf->param;
110+
rnp->dev = &pdev->parent;
113111

114112
rf->enable_pin = rt_pin_get_named_pin(dev, "enable", 0, RT_NULL, RT_NULL);
115113

@@ -148,16 +146,14 @@ static rt_err_t regulator_fixed_probe(struct rt_platform_device *pdev)
148146
return err;
149147
}
150148

151-
static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] =
152-
{
149+
static const struct rt_ofw_node_id regulator_fixed_ofw_ids[] = {
153150
{ .compatible = "regulator-fixed" },
154151
{ /* sentinel */ }
155152
};
156153

157-
static struct rt_platform_driver regulator_fixed_driver =
158-
{
154+
static struct rt_platform_driver regulator_fixed_driver = {
159155
.name = "reg-fixed-voltage",
160-
.ids = regulator_fixed_ofw_ids,
156+
.ids = regulator_fixed_ofw_ids,
161157

162158
.probe = regulator_fixed_probe,
163159
};

components/drivers/regulator/regulator-gpio.c

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct regulator_gpio_state
2020

2121
struct regulator_gpio_desc
2222
{
23-
rt_base_t pin;
23+
rt_base_t pin;
2424
rt_uint32_t flags;
2525
};
2626

@@ -30,32 +30,27 @@ struct regulator_gpio
3030

3131
rt_base_t enable_pin;
3232

33-
rt_size_t pins_nr;
33+
rt_size_t pins_nr;
3434
struct regulator_gpio_desc *pins_desc;
3535

36-
int state;
37-
rt_size_t states_nr;
36+
int state;
37+
rt_size_t states_nr;
3838
struct regulator_gpio_state *states;
3939

40-
const char *input_supply;
41-
rt_uint32_t startup_delay;
42-
rt_uint32_t off_on_delay;
43-
rt_bool_t enabled_at_boot;
40+
const char *input_supply;
41+
rt_uint32_t startup_delay;
42+
rt_uint32_t off_on_delay;
43+
rt_bool_t enabled_at_boot;
4444
struct rt_regulator_param param;
4545
};
4646

4747
#define raw_to_regulator_gpio(raw) rt_container_of(raw, struct regulator_gpio, parent)
4848

4949
static rt_err_t regulator_gpio_enable(struct rt_regulator_node *reg_np)
5050
{
51-
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
51+
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
5252
struct rt_regulator_param *param = &rg->param;
5353

54-
if (param->always_on)
55-
{
56-
return RT_EOK;
57-
}
58-
5954
if (rg->enable_pin >= 0)
6055
{
6156
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)
6762

6863
static rt_err_t regulator_gpio_disable(struct rt_regulator_node *reg_np)
6964
{
70-
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
65+
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
7166
struct rt_regulator_param *param = &rg->param;
7267

73-
if (param->always_on)
74-
{
75-
return RT_EOK;
76-
}
77-
7868
if (rg->enable_pin >= 0)
7969
{
8070
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)
8676

8777
static rt_bool_t regulator_gpio_is_enabled(struct rt_regulator_node *reg_np)
8878
{
89-
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
79+
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
9080
struct rt_regulator_param *param = &rg->param;
9181

92-
if (param->always_on)
93-
{
94-
return RT_TRUE;
95-
}
96-
9782
if (rg->enable_pin >= 0)
9883
{
9984
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)
10691
}
10792

10893
static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
109-
int min_uvolt, int max_uvolt)
94+
int min_uvolt, int max_uvolt)
11095
{
111-
int target = 0, best_val = RT_REGULATOR_UVOLT_INVALID;
96+
int target = 0, best_val = RT_REGULATOR_UVOLT_INVALID;
11297
struct regulator_gpio *rg = raw_to_regulator_gpio(reg_np);
11398

11499
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,
119104
state->value >= min_uvolt &&
120105
state->value <= max_uvolt)
121106
{
122-
target = state->gpios;
107+
target = state->gpios;
123108
best_val = state->value;
124109
}
125110
}
@@ -131,7 +116,7 @@ static rt_err_t regulator_gpio_set_voltage(struct rt_regulator_node *reg_np,
131116

132117
for (int i = 0; i < rg->pins_nr; ++i)
133118
{
134-
int state = (target >> i) & 1;
119+
int state = (target >> i) & 1;
135120
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];
136121

137122
rt_pin_mode(gpiod->pin, PIN_MODE_OUTPUT);
@@ -158,20 +143,19 @@ static int regulator_gpio_get_voltage(struct rt_regulator_node *reg_np)
158143
return -RT_EINVAL;
159144
}
160145

161-
static const struct rt_regulator_ops regulator_gpio_ops =
162-
{
163-
.enable = regulator_gpio_enable,
164-
.disable = regulator_gpio_disable,
165-
.is_enabled = regulator_gpio_is_enabled,
146+
static const struct rt_regulator_ops regulator_gpio_ops = {
147+
.enable = regulator_gpio_enable,
148+
.disable = regulator_gpio_disable,
149+
.is_enabled = regulator_gpio_is_enabled,
166150
.set_voltage = regulator_gpio_set_voltage,
167151
.get_voltage = regulator_gpio_get_voltage,
168152
};
169153

170154
static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
171155
{
172-
rt_err_t err;
173-
struct rt_device *dev = &pdev->parent;
174-
struct regulator_gpio *rg = rt_calloc(1, sizeof(*rg));
156+
rt_err_t err;
157+
struct rt_device *dev = &pdev->parent;
158+
struct regulator_gpio *rg = rt_calloc(1, sizeof(*rg));
175159
struct rt_regulator_node *rgp;
176160

177161
if (!rg)
@@ -181,11 +165,11 @@ static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
181165

182166
regulator_ofw_parse(dev->ofw_node, &rg->param);
183167

184-
rgp = &rg->parent;
168+
rgp = &rg->parent;
185169
rgp->supply_name = rg->param.name;
186-
rgp->ops = &regulator_gpio_ops;
187-
rgp->param = &rg->param;
188-
rgp->dev = &pdev->parent;
170+
rgp->ops = &regulator_gpio_ops;
171+
rgp->param = &rg->param;
172+
rgp->dev = &pdev->parent;
189173

190174
rt_dm_dev_prop_read_u32(dev, "startup-delay-us", &rg->startup_delay);
191175
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)
214198

215199
for (int i = 0; i < rg->pins_nr; ++i)
216200
{
217-
rt_uint32_t val;
201+
rt_uint32_t val;
218202
struct regulator_gpio_desc *gpiod = &rg->pins_desc[i];
219203

220204
gpiod->pin = rt_pin_get_named_pin(dev, RT_NULL, i, RT_NULL, RT_NULL);
@@ -286,16 +270,14 @@ static rt_err_t regulator_gpio_probe(struct rt_platform_device *pdev)
286270
return err;
287271
}
288272

289-
static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] =
290-
{
273+
static const struct rt_ofw_node_id regulator_gpio_ofw_ids[] = {
291274
{ .compatible = "regulator-gpio" },
292275
{ /* sentinel */ }
293276
};
294277

295-
static struct rt_platform_driver regulator_gpio_driver =
296-
{
278+
static struct rt_platform_driver regulator_gpio_driver = {
297279
.name = "regulator-gpio",
298-
.ids = regulator_gpio_ofw_ids,
280+
.ids = regulator_gpio_ofw_ids,
299281

300282
.probe = regulator_gpio_probe,
301283
};

0 commit comments

Comments
 (0)