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 <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2023-01-09 11:05:38 +01:00 committed by Fabio Baltieri
commit 7feb008c85
6 changed files with 15 additions and 10 deletions

View file

@ -13,7 +13,7 @@ void regulator_common_data_init(const struct device *dev)
data->refcnt = 0; 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_driver_api *api = dev->api;
const struct regulator_common_config *config = dev->config; 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); ret = api->enable(dev);
if (ret < 0) { if (ret < 0) {
return ret; return ret;

View file

@ -60,7 +60,7 @@ static int regulator_fake_init(const struct device *dev)
{ {
regulator_common_data_init(dev); regulator_common_data_init(dev);
return regulator_common_init_enable(dev); return regulator_common_init(dev, false);
} }
/* parent regulator */ /* parent regulator */

View file

@ -72,7 +72,7 @@ static int regulator_fixed_init(const struct device *dev)
return ret; return ret;
} }
ret = regulator_common_init_enable(dev); ret = regulator_common_init(dev, false);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }

View file

@ -603,8 +603,7 @@ static int regulator_npm6001_init(const struct device *dev)
if (!device_is_ready(config->p)) { if (!device_is_ready(config->p)) {
return -ENODEV; return -ENODEV;
} }
return regulator_common_init(dev, false);
return regulator_common_init_enable(dev);
} }
static int regulator_npm6001_common_init(const struct device *dev) static int regulator_npm6001_common_init(const struct device *dev)

View file

@ -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, int regulator_pca9420_dvs_state_set(const struct device *dev,

View file

@ -194,18 +194,22 @@ struct regulator_common_data {
void regulator_common_data_init(const struct device *dev); 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 * This function can be called after drivers initialize the regulator. It
* will automatically enable the regulator if it is set to `regulator-boot-on` * 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 dev Regulator device instance
* @param is_enabled Indicate if the regulator is enabled by default in
* hardware.
* *
* @retval 0 If enabled successfully. * @retval 0 If enabled successfully.
* @retval -errno Negative errno in case of failure. * @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 */ /** @endcond */