Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+55
+55
drivers/regulator/mp8859.c
··· 35 35 36 36 #define MP8859_GO_BIT 0x01 37 37 38 + #define MP8859_IOUT_LIM_MASK 0x7f 39 + 38 40 #define MP8859_ENABLE_MASK 0x80 39 41 #define MP8859_DISCHG_EN_MASK 0x10 40 42 #define MP8859_MODE_MASK 0x08 ··· 131 129 132 130 return regmap_update_bits(rdev->regmap, MP8859_CTL1_REG, 133 131 MP8859_MODE_MASK, val); 132 + } 133 + 134 + static int mp8859_set_current_limit(struct regulator_dev *rdev, 135 + int min_uA, int max_uA) 136 + { 137 + unsigned int cur_val, new_val; 138 + int ret, i; 139 + 140 + /* Steps of 50mA */ 141 + new_val = max_uA / 50000; 142 + if (new_val > MP8859_IOUT_LIM_MASK) 143 + return -EINVAL; 144 + if (new_val == 0) 145 + return -EINVAL; 146 + 147 + /* 148 + * If the regulator is limiting then ramp gradually as per 149 + * datasheet, otherwise just set the value directly. 150 + */ 151 + ret = regmap_read(rdev->regmap, MP8859_STATUS_REG, &cur_val); 152 + if (ret != 0) 153 + return ret; 154 + if (!(cur_val & MP8859_CC_CV_MASK)) { 155 + return regmap_update_bits(rdev->regmap, MP8859_IOUT_LIM_REG, 156 + MP8859_IOUT_LIM_MASK, new_val); 157 + } 158 + 159 + ret = regmap_read(rdev->regmap, MP8859_IOUT_LIM_REG, &cur_val); 160 + if (ret != 0) 161 + return ret; 162 + 163 + if (cur_val >= new_val) { 164 + for (i = cur_val; i >= new_val; i--) { 165 + ret = regmap_update_bits(rdev->regmap, 166 + MP8859_IOUT_LIM_REG, 167 + MP8859_IOUT_LIM_MASK, 168 + cur_val - i); 169 + if (ret != 0) 170 + return ret; 171 + } 172 + } else { 173 + for (i = cur_val; i <= new_val; i++) { 174 + ret = regmap_update_bits(rdev->regmap, 175 + MP8859_IOUT_LIM_REG, 176 + MP8859_IOUT_LIM_MASK, 177 + cur_val + i); 178 + if (ret != 0) 179 + return ret; 180 + } 181 + } 182 + 183 + return 0; 134 184 } 135 185 136 186 static int mp8859_get_status(struct regulator_dev *rdev) ··· 295 241 .set_mode = mp8859_set_mode, 296 242 .get_mode = mp8859_get_mode, 297 243 .set_active_discharge = regulator_set_active_discharge_regmap, 244 + .set_current_limit = mp8859_set_current_limit, 298 245 .get_status = mp8859_get_status, 299 246 .get_error_flags = mp8859_get_error_flags, 300 247 };