Skip to content

Commit 3028583

Browse files
Jonas Andreassonbroonie
authored andcommitted
regulator: TPS6287X: Use min/max uV to get VRANGE
Changing voltage might ignore slew rate and cause a current surge. With current implementation the driver will get the regulator to change the voltage range used during run time. According to communication I have had with Texas Instruments, this is not intended, since the Dynamic Voltage Scaling in the hardware is only designed to work within a voltage range. The current implementation will therefore ignore the slew rate that is defined in devicetree when the voltage range is changed during use. The current implementation will always select a voltage in the most accurate range that can reach that voltage even though multiple ranges are able to reach that voltage. There are 4 Voltage ranges with the following reach: 0b00: 0.4-0.71875V (1.25mV step size) 0b01: 0.4-1.0375V (2.5mV) 0b10: 0.4-1.675V (5mV) 0b11: 0.8-3.3V (10mV) This in practice means that a change from below to above 0.71875V will use the smallest range(0b00) for the values below and the second smallest range(0b01) for the voltages above (Up to 1.675V). I have timed how long it takes to go from below 0.71875V to above. The increase was 100mV which, with the slew rate set to 1250µV/µs. This in theory should take 80µs to do. With the current implementation, it takes 10µs on my hardware. Doing the same test with the slew rate set to 5000µV/µs, which should take 20µs, also only takes 10µs to do on my hardware. Not only is this not in line with the technical specification for the regulator. It also causes a current surge. Which when calculating the output current, as described in the technical specification, compared to what I could observe on my hardware the real output is ~1A higher (~1.2A) than what I calculated it to be(~0.2A). I tested also transitioning from a bigger to a smaller range, and the results were the same. Instead, let's limit the voltage range to a single one, which is in line with the intended use of the regulator. This is done by looking up the minimum and maximum requested voltage specified in devicetree. Signed-off-by: Jonas Andreasson <jonas.andreasson@axis.com> Link: https://patch.msgid.link/20250121-tps-fix-v2-1-50cc4d0f1635@axis.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 367a820 commit 3028583

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

drivers/regulator/tps6287x-regulator.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,35 @@ static const unsigned int tps6287x_voltage_range_sel[] = {
4444
0x0, 0x1, 0x2, 0x3
4545
};
4646

47+
static const unsigned int tps6287x_voltage_range_prefix[] = {
48+
0x000, 0x100, 0x200, 0x300
49+
};
50+
4751
static const unsigned int tps6287x_ramp_table[] = {
4852
10000, 5000, 1250, 500
4953
};
5054

55+
struct tps6287x_reg_data {
56+
int range;
57+
};
58+
59+
static int tps6287x_best_range(struct regulator_config *config, const struct regulator_desc *desc)
60+
{
61+
const struct linear_range *r;
62+
int i;
63+
64+
if (!config->init_data->constraints.apply_uV)
65+
return -1;
66+
67+
for (i = 0; i < desc->n_linear_ranges; i++) {
68+
r = &desc->linear_ranges[i];
69+
if (r->min <= config->init_data->constraints.min_uV &&
70+
config->init_data->constraints.max_uV <= linear_range_get_max_value(r))
71+
return i;
72+
}
73+
return -1;
74+
}
75+
5176
static int tps6287x_set_mode(struct regulator_dev *rdev, unsigned int mode)
5277
{
5378
unsigned int val;
@@ -91,6 +116,28 @@ static unsigned int tps6287x_of_map_mode(unsigned int mode)
91116
}
92117
}
93118

119+
static int tps6287x_map_voltage(struct regulator_dev *rdev, int min_uV, int max_uV)
120+
{
121+
struct tps6287x_reg_data *data = (struct tps6287x_reg_data *)rdev->reg_data;
122+
struct linear_range selected_range;
123+
int selector, voltage;
124+
125+
if (!data || data->range == -1)
126+
return regulator_map_voltage_pickable_linear_range(rdev, min_uV, max_uV);
127+
128+
selected_range = rdev->desc->linear_ranges[data->range];
129+
selector = DIV_ROUND_UP(min_uV - selected_range.min, selected_range.step);
130+
if (selector < selected_range.min_sel || selector > selected_range.max_sel)
131+
return -EINVAL;
132+
133+
selector |= tps6287x_voltage_range_prefix[data->range];
134+
voltage = rdev->desc->ops->list_voltage(rdev, selector);
135+
if (voltage < min_uV || voltage > max_uV)
136+
return -EINVAL;
137+
138+
return selector;
139+
}
140+
94141
static const struct regulator_ops tps6287x_regulator_ops = {
95142
.enable = regulator_enable_regmap,
96143
.disable = regulator_disable_regmap,
@@ -100,6 +147,7 @@ static const struct regulator_ops tps6287x_regulator_ops = {
100147
.get_voltage_sel = regulator_get_voltage_sel_pickable_regmap,
101148
.set_voltage_sel = regulator_set_voltage_sel_pickable_regmap,
102149
.list_voltage = regulator_list_voltage_pickable_linear_range,
150+
.map_voltage = tps6287x_map_voltage,
103151
.set_ramp_delay = regulator_set_ramp_delay_regmap,
104152
};
105153

@@ -130,8 +178,14 @@ static int tps6287x_i2c_probe(struct i2c_client *i2c)
130178
{
131179
struct device *dev = &i2c->dev;
132180
struct regulator_config config = {};
181+
struct tps6287x_reg_data *reg_data;
133182
struct regulator_dev *rdev;
134183

184+
reg_data = devm_kzalloc(dev, sizeof(struct tps6287x_reg_data), GFP_KERNEL);
185+
186+
if (!reg_data)
187+
return -ENOMEM;
188+
135189
config.regmap = devm_regmap_init_i2c(i2c, &tps6287x_regmap_config);
136190
if (IS_ERR(config.regmap)) {
137191
dev_err(dev, "Failed to init i2c\n");
@@ -143,12 +197,15 @@ static int tps6287x_i2c_probe(struct i2c_client *i2c)
143197
config.init_data = of_get_regulator_init_data(dev, dev->of_node,
144198
&tps6287x_reg);
145199

200+
reg_data->range = tps6287x_best_range(&config, &tps6287x_reg);
201+
146202
rdev = devm_regulator_register(dev, &tps6287x_reg, &config);
147203
if (IS_ERR(rdev)) {
148204
dev_err(dev, "Failed to register regulator\n");
149205
return PTR_ERR(rdev);
150206
}
151207

208+
rdev->reg_data = (void *)reg_data;
152209
dev_dbg(dev, "Probed regulator\n");
153210

154211
return 0;

0 commit comments

Comments
 (0)