From 7feb008c85ead5f7e06b1460c91cc596a09400b6 Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Mon, 9 Jan 2023 11:05:38 +0100 Subject: [PATCH] drivers: regulator: common: allow to specify on by default Some regulators are enabled by default, however, such condition cannot be captured now by the regulator driver API. Refactor regulator_common_init_enable to regulator_common_init (enable removed, as it also sets mode) and add a new argument to specify such condition. With this change, regulator_disable() and regulator_is_enabled() work as expected without a first call to regulator_enable(). Signed-off-by: Gerard Marull-Paretas --- drivers/regulator/regulator_common.c | 6 ++++-- drivers/regulator/regulator_fake.c | 2 +- drivers/regulator/regulator_fixed.c | 2 +- drivers/regulator/regulator_npm6001.c | 3 +-- drivers/regulator/regulator_pca9420.c | 2 +- include/zephyr/drivers/regulator.h | 10 +++++++--- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/regulator/regulator_common.c b/drivers/regulator/regulator_common.c index 55cef0dd78f..f9f9a41911c 100644 --- a/drivers/regulator/regulator_common.c +++ b/drivers/regulator/regulator_common.c @@ -13,7 +13,7 @@ void regulator_common_data_init(const struct device *dev) data->refcnt = 0; } -int regulator_common_init_enable(const struct device *dev) +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; @@ -27,7 +27,9 @@ int regulator_common_init_enable(const struct device *dev) } } - if ((config->flags & REGULATOR_INIT_ENABLED) != 0U) { + if (is_enabled) { + data->refcnt++; + } else if ((config->flags & REGULATOR_INIT_ENABLED) != 0U) { ret = api->enable(dev); if (ret < 0) { return ret; diff --git a/drivers/regulator/regulator_fake.c b/drivers/regulator/regulator_fake.c index 6b0643a9f04..88dca9f3b25 100644 --- a/drivers/regulator/regulator_fake.c +++ b/drivers/regulator/regulator_fake.c @@ -60,7 +60,7 @@ static int regulator_fake_init(const struct device *dev) { regulator_common_data_init(dev); - return regulator_common_init_enable(dev); + return regulator_common_init(dev, false); } /* parent regulator */ diff --git a/drivers/regulator/regulator_fixed.c b/drivers/regulator/regulator_fixed.c index 75c98d19ead..3e12ee44187 100644 --- a/drivers/regulator/regulator_fixed.c +++ b/drivers/regulator/regulator_fixed.c @@ -72,7 +72,7 @@ static int regulator_fixed_init(const struct device *dev) return ret; } - ret = regulator_common_init_enable(dev); + ret = regulator_common_init(dev, false); if (ret < 0) { return ret; } diff --git a/drivers/regulator/regulator_npm6001.c b/drivers/regulator/regulator_npm6001.c index 58780426ce5..95af7a6b3de 100644 --- a/drivers/regulator/regulator_npm6001.c +++ b/drivers/regulator/regulator_npm6001.c @@ -603,8 +603,7 @@ static int regulator_npm6001_init(const struct device *dev) if (!device_is_ready(config->p)) { return -ENODEV; } - - return regulator_common_init_enable(dev); + return regulator_common_init(dev, false); } static int regulator_npm6001_common_init(const struct device *dev) diff --git a/drivers/regulator/regulator_pca9420.c b/drivers/regulator/regulator_pca9420.c index e6904a4cae8..b725b955ae4 100644 --- a/drivers/regulator/regulator_pca9420.c +++ b/drivers/regulator/regulator_pca9420.c @@ -343,7 +343,7 @@ static int regulator_pca9420_init(const struct device *dev) } } - return regulator_common_init_enable(dev); + return regulator_common_init(dev, false); } int regulator_pca9420_dvs_state_set(const struct device *dev, diff --git a/include/zephyr/drivers/regulator.h b/include/zephyr/drivers/regulator.h index dba8c6e835b..7172cb5f229 100644 --- a/include/zephyr/drivers/regulator.h +++ b/include/zephyr/drivers/regulator.h @@ -194,18 +194,22 @@ struct regulator_common_data { void regulator_common_data_init(const struct device *dev); /** - * @brief Common function to enable regulator at init time. + * @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. + * 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`. * * @param dev Regulator device instance + * @param is_enabled Indicate if the regulator is enabled by default in + * hardware. * * @retval 0 If enabled successfully. * @retval -errno Negative errno in case of failure. */ -int regulator_common_init_enable(const struct device *dev); +int regulator_common_init(const struct device *dev, bool is_enabled); /** @endcond */