drivers: regulator: clarify regulator_set_mode interface

Clarify the API for regulators that have the option to set mode
externally, such as PCA9420. Adjust the PCA9420 driver to comply with
the interface.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-12-01 13:46:49 +01:00 committed by Carles Cufí
commit 8db7e046c5
2 changed files with 17 additions and 2 deletions

View file

@ -327,11 +327,18 @@ static int regulator_pca9420_set_mode(const struct device *dev, uint32_t mode)
struct regulator_pca9420_common_data *cdata = config->parent->data;
int ret;
if (cconfig->enable_modesel_pins ||
!regulator_pca9420_is_mode_allowed(dev, mode)) {
if (!regulator_pca9420_is_mode_allowed(dev, mode)) {
return -ENOTSUP;
}
/* change mode, to allow configuring voltage, but return -EPERM to
* indicate we are not really changing mode, as it is managed externally
*/
if (cconfig->enable_modesel_pins) {
cdata->mode = mode;
return -EPERM;
}
ret = i2c_reg_update_byte_dt(&cconfig->i2c, PCA9420_TOP_CNTL3,
mode << PCA9420_TOP_CNTL3_MODE_I2C_POS,
PCA9420_TOP_CNTL3_MODE_I2C_MASK);

View file

@ -266,11 +266,19 @@ static inline int32_t regulator_get_current_limit(const struct device *dev)
* configuration or better power savings. This API will apply a mode for
* the regulator.
*
* Some regulators may only allow setting mode externally, but still allow
* configuring the parameters such as the output voltage. For such devices, this
* function will return -EPERM, indicating mode can't be changed. However, all
* future calls to e.g. regulator_set_voltage() will apply to the selected mode.
*
* Some regulators may apply a mode to all of its regulators simultaneously.
*
* @param dev Regulator device instance.
* @param mode Mode to select for this regulator. Only modes present in the
* `regulator-allowed-modes` devicetree property are permitted.
*
* @retval 0 If successful.
* @retval -EPERM If mode can not be changed.
* @retval -ENOSYS If function is not implemented.
* @retval -errno In case of any other error.
*/