diff --git a/drivers/regulator/regulator_common.c b/drivers/regulator/regulator_common.c index d00e2f647fe..cc705238a09 100644 --- a/drivers/regulator/regulator_common.c +++ b/drivers/regulator/regulator_common.c @@ -18,6 +18,7 @@ int regulator_common_init(const struct device *dev, bool is_enabled) const struct regulator_driver_api *api = dev->api; const struct regulator_common_config *config = dev->config; struct regulator_common_data *data = dev->data; + int32_t current_uv; int ret; if (config->initial_mode != REGULATOR_INITIAL_MODE_UNKNOWN) { @@ -27,13 +28,26 @@ int regulator_common_init(const struct device *dev, bool is_enabled) } } - /* regulator voltage needs to be within allowed range before enabling */ + /* If we have valid range values, we try to match them before enabling */ if ((config->min_uv > INT32_MIN) || (config->max_uv < INT32_MAX)) { - ret = regulator_set_voltage(dev, config->min_uv, - config->max_uv); - if ((ret < 0) && (ret != -ENOSYS)) { + + ret = regulator_get_voltage(dev, ¤t_uv); + if (ret < 0) { return ret; } + + /* Snap to closest interval value if out of range */ + if (current_uv < config->min_uv) { + ret = regulator_set_voltage(dev, config->min_uv, config->min_uv); + if (ret < 0) { + return ret; + } + } else if (current_uv > config->max_uv) { + ret = regulator_set_voltage(dev, config->max_uv, config->max_uv); + if (ret < 0) { + return ret; + } + } } if (is_enabled) {