Skip to content

Commit e915331

Browse files
Enric Balletbo i Serrabroonie
authored andcommitted
regulator: vctrl-regulator: Avoid deadlock getting and setting the voltage
`cat /sys/kernel/debug/regulator/regulator_summary` ends on a deadlock when you have a voltage controlled regulator (vctrl). The problem is that the vctrl_get_voltage() and vctrl_set_voltage() calls the regulator_get_voltage() and regulator_set_voltage() and that will try to lock again the dependent regulators (the regulator supplying the control voltage). Fix the issue by exporting the unlocked version of the regulator_get_voltage() and regulator_set_voltage() API so drivers that need it, like the voltage controlled regulator driver can use it. Fixes: f8702f9 ("regulator: core: Use ww_mutex for regulators locking") Reported-by: Douglas Anderson <[email protected]> Signed-off-by: Enric Balletbo i Serra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 6f1ff76 commit e915331

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

drivers/regulator/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
34663466
out:
34673467
return ret;
34683468
}
3469+
EXPORT_SYMBOL(regulator_set_voltage_rdev);
34693470

34703471
static int regulator_limit_voltage_step(struct regulator_dev *rdev,
34713472
int *current_uV, int *min_uV)
@@ -4030,6 +4031,7 @@ int regulator_get_voltage_rdev(struct regulator_dev *rdev)
40304031
return ret;
40314032
return ret - rdev->constraints->uV_offset;
40324033
}
4034+
EXPORT_SYMBOL(regulator_get_voltage_rdev);
40334035

40344036
/**
40354037
* regulator_get_voltage - get regulator output voltage

drivers/regulator/vctrl-regulator.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
#include <linux/module.h>
1212
#include <linux/of.h>
1313
#include <linux/of_device.h>
14+
#include <linux/regulator/coupler.h>
1415
#include <linux/regulator/driver.h>
1516
#include <linux/regulator/of_regulator.h>
1617
#include <linux/sort.h>
1718

19+
#include "internal.h"
20+
1821
struct vctrl_voltage_range {
1922
int min_uV;
2023
int max_uV;
@@ -79,7 +82,7 @@ static int vctrl_calc_output_voltage(struct vctrl_data *vctrl, int ctrl_uV)
7982
static int vctrl_get_voltage(struct regulator_dev *rdev)
8083
{
8184
struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
82-
int ctrl_uV = regulator_get_voltage(vctrl->ctrl_reg);
85+
int ctrl_uV = regulator_get_voltage_rdev(vctrl->ctrl_reg->rdev);
8386

8487
return vctrl_calc_output_voltage(vctrl, ctrl_uV);
8588
}
@@ -90,16 +93,16 @@ static int vctrl_set_voltage(struct regulator_dev *rdev,
9093
{
9194
struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
9295
struct regulator *ctrl_reg = vctrl->ctrl_reg;
93-
int orig_ctrl_uV = regulator_get_voltage(ctrl_reg);
96+
int orig_ctrl_uV = regulator_get_voltage_rdev(ctrl_reg->rdev);
9497
int uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV);
9598
int ret;
9699

97100
if (req_min_uV >= uV || !vctrl->ovp_threshold)
98101
/* voltage rising or no OVP */
99-
return regulator_set_voltage(
100-
ctrl_reg,
102+
return regulator_set_voltage_rdev(ctrl_reg->rdev,
101103
vctrl_calc_ctrl_voltage(vctrl, req_min_uV),
102-
vctrl_calc_ctrl_voltage(vctrl, req_max_uV));
104+
vctrl_calc_ctrl_voltage(vctrl, req_max_uV),
105+
PM_SUSPEND_ON);
103106

104107
while (uV > req_min_uV) {
105108
int max_drop_uV = (uV * vctrl->ovp_threshold) / 100;
@@ -114,9 +117,10 @@ static int vctrl_set_voltage(struct regulator_dev *rdev,
114117
next_uV = max_t(int, req_min_uV, uV - max_drop_uV);
115118
next_ctrl_uV = vctrl_calc_ctrl_voltage(vctrl, next_uV);
116119

117-
ret = regulator_set_voltage(ctrl_reg,
120+
ret = regulator_set_voltage_rdev(ctrl_reg->rdev,
121+
next_ctrl_uV,
118122
next_ctrl_uV,
119-
next_ctrl_uV);
123+
PM_SUSPEND_ON);
120124
if (ret)
121125
goto err;
122126

@@ -130,7 +134,8 @@ static int vctrl_set_voltage(struct regulator_dev *rdev,
130134

131135
err:
132136
/* Try to go back to original voltage */
133-
regulator_set_voltage(ctrl_reg, orig_ctrl_uV, orig_ctrl_uV);
137+
regulator_set_voltage_rdev(ctrl_reg->rdev, orig_ctrl_uV, orig_ctrl_uV,
138+
PM_SUSPEND_ON);
134139

135140
return ret;
136141
}
@@ -155,9 +160,10 @@ static int vctrl_set_voltage_sel(struct regulator_dev *rdev,
155160

156161
if (selector >= vctrl->sel || !vctrl->ovp_threshold) {
157162
/* voltage rising or no OVP */
158-
ret = regulator_set_voltage(ctrl_reg,
163+
ret = regulator_set_voltage_rdev(ctrl_reg->rdev,
164+
vctrl->vtable[selector].ctrl,
159165
vctrl->vtable[selector].ctrl,
160-
vctrl->vtable[selector].ctrl);
166+
PM_SUSPEND_ON);
161167
if (!ret)
162168
vctrl->sel = selector;
163169

@@ -173,9 +179,10 @@ static int vctrl_set_voltage_sel(struct regulator_dev *rdev,
173179
else
174180
next_sel = vctrl->vtable[vctrl->sel].ovp_min_sel;
175181

176-
ret = regulator_set_voltage(ctrl_reg,
182+
ret = regulator_set_voltage_rdev(ctrl_reg->rdev,
177183
vctrl->vtable[next_sel].ctrl,
178-
vctrl->vtable[next_sel].ctrl);
184+
vctrl->vtable[next_sel].ctrl,
185+
PM_SUSPEND_ON);
179186
if (ret) {
180187
dev_err(&rdev->dev,
181188
"failed to set control voltage to %duV\n",
@@ -195,9 +202,10 @@ static int vctrl_set_voltage_sel(struct regulator_dev *rdev,
195202
err:
196203
if (vctrl->sel != orig_sel) {
197204
/* Try to go back to original voltage */
198-
if (!regulator_set_voltage(ctrl_reg,
205+
if (!regulator_set_voltage_rdev(ctrl_reg->rdev,
206+
vctrl->vtable[orig_sel].ctrl,
199207
vctrl->vtable[orig_sel].ctrl,
200-
vctrl->vtable[orig_sel].ctrl))
208+
PM_SUSPEND_ON))
201209
vctrl->sel = orig_sel;
202210
else
203211
dev_warn(&rdev->dev,
@@ -482,7 +490,7 @@ static int vctrl_probe(struct platform_device *pdev)
482490
if (ret)
483491
return ret;
484492

485-
ctrl_uV = regulator_get_voltage(vctrl->ctrl_reg);
493+
ctrl_uV = regulator_get_voltage_rdev(vctrl->ctrl_reg->rdev);
486494
if (ctrl_uV < 0) {
487495
dev_err(&pdev->dev, "failed to get control voltage\n");
488496
return ctrl_uV;

0 commit comments

Comments
 (0)