Skip to content

Commit c8e794c

Browse files
committed
regulator: mp8859: Implement set_current_limit()
The mp8859 implements support for current limiting, provide support for configuring this via the driver. The datasheet recommends that if the device has hit the current limit then any changes should be implemented via a ramp so we do so in the driver. Tested-by: Markus Reichl <m.reichl@fivetechno.de> Signed-off-by: Mark Brown <broonie@kernel.org> Link: https://msgid.link/r/20240225-regulator-mp8859-v1-8-68ee2c839ded@kernel.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 6df0921 commit c8e794c

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

drivers/regulator/mp8859.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
#define MP8859_GO_BIT 0x01
3737

38+
#define MP8859_IOUT_LIM_MASK 0x7f
39+
3840
#define MP8859_ENABLE_MASK 0x80
3941
#define MP8859_DISCHG_EN_MASK 0x10
4042
#define MP8859_MODE_MASK 0x08
@@ -131,6 +133,58 @@ static int mp8859_set_mode(struct regulator_dev *rdev, unsigned int mode)
131133
MP8859_MODE_MASK, val);
132134
}
133135

136+
static int mp8859_set_current_limit(struct regulator_dev *rdev,
137+
int min_uA, int max_uA)
138+
{
139+
unsigned int cur_val, new_val;
140+
int ret, i;
141+
142+
/* Steps of 50mA */
143+
new_val = max_uA / 50000;
144+
if (new_val > MP8859_IOUT_LIM_MASK)
145+
return -EINVAL;
146+
if (new_val == 0)
147+
return -EINVAL;
148+
149+
/*
150+
* If the regulator is limiting then ramp gradually as per
151+
* datasheet, otherwise just set the value directly.
152+
*/
153+
ret = regmap_read(rdev->regmap, MP8859_STATUS_REG, &cur_val);
154+
if (ret != 0)
155+
return ret;
156+
if (!(cur_val & MP8859_CC_CV_MASK)) {
157+
return regmap_update_bits(rdev->regmap, MP8859_IOUT_LIM_REG,
158+
MP8859_IOUT_LIM_MASK, new_val);
159+
}
160+
161+
ret = regmap_read(rdev->regmap, MP8859_IOUT_LIM_REG, &cur_val);
162+
if (ret != 0)
163+
return ret;
164+
165+
if (cur_val >= new_val) {
166+
for (i = cur_val; i >= new_val; i--) {
167+
ret = regmap_update_bits(rdev->regmap,
168+
MP8859_IOUT_LIM_REG,
169+
MP8859_IOUT_LIM_MASK,
170+
cur_val - i);
171+
if (ret != 0)
172+
return ret;
173+
}
174+
} else {
175+
for (i = cur_val; i <= new_val; i++) {
176+
ret = regmap_update_bits(rdev->regmap,
177+
MP8859_IOUT_LIM_REG,
178+
MP8859_IOUT_LIM_MASK,
179+
cur_val + i);
180+
if (ret != 0)
181+
return ret;
182+
}
183+
}
184+
185+
return 0;
186+
}
187+
134188
static int mp8859_get_status(struct regulator_dev *rdev)
135189
{
136190
unsigned int val;
@@ -241,6 +295,7 @@ static const struct regulator_ops mp8859_ops = {
241295
.set_mode = mp8859_set_mode,
242296
.get_mode = mp8859_get_mode,
243297
.set_active_discharge = regulator_set_active_discharge_regmap,
298+
.set_current_limit = mp8859_set_current_limit,
244299
.get_status = mp8859_get_status,
245300
.get_error_flags = mp8859_get_error_flags,
246301
};

0 commit comments

Comments
 (0)