From 240be492fade93e52abc15822d1dc9a83fb03778 Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Thu, 12 Jan 2023 15:25:21 +0100 Subject: [PATCH] drivers: regulator: common: set regulator voltage before enabling Regulator voltage needs to be within allowed range before enabling. It could happen that regulator default voltage is out of the allowed range, so the regulator could be enabled at boot time producing a not-allowed voltage. Signed-off-by: Gerard Marull-Paretas --- drivers/regulator/regulator_common.c | 9 +++++++++ include/zephyr/drivers/regulator.h | 15 ++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/drivers/regulator/regulator_common.c b/drivers/regulator/regulator_common.c index f9f9a41911c..d00e2f647fe 100644 --- a/drivers/regulator/regulator_common.c +++ b/drivers/regulator/regulator_common.c @@ -27,6 +27,15 @@ int regulator_common_init(const struct device *dev, bool is_enabled) } } + /* regulator voltage needs to be within allowed range 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)) { + return ret; + } + } + if (is_enabled) { data->refcnt++; } else if ((config->flags & REGULATOR_INIT_ENABLED) != 0U) { diff --git a/include/zephyr/drivers/regulator.h b/include/zephyr/drivers/regulator.h index 7172cb5f229..8cda3a7e68b 100644 --- a/include/zephyr/drivers/regulator.h +++ b/include/zephyr/drivers/regulator.h @@ -196,11 +196,16 @@ void regulator_common_data_init(const struct device *dev); /** * @brief Common function to initialize the regulator at init time. * - * This function can be called after drivers initialize the regulator. It - * will automatically enable the regulator if it is set to `regulator-boot-on` - * or `regulator-always-on` and increase its usage count. It will also configure - * the regulator mode if `regulator-initial-mode` is set. Regulators that are - * enabled by default in hardware, must set @p is_enabled to `true`. + * This function needs to be called after drivers initialize the regulator. It + * will: + * + * - Automatically enable the regulator if it is set to `regulator-boot-on` + * or `regulator-always-on` and increase its usage count. + * - Configure the regulator mode if `regulator-initial-mode` is set. + * - Ensure regulator voltage is set to a valid range. + * + * Regulators that are enabled by default in hardware, must set @p is_enabled to + * `true`. * * @param dev Regulator device instance * @param is_enabled Indicate if the regulator is enabled by default in