diff --git a/drivers/regulator/regulator_common.c b/drivers/regulator/regulator_common.c index b6abd76ff22..841a4ef42fb 100644 --- a/drivers/regulator/regulator_common.c +++ b/drivers/regulator/regulator_common.c @@ -50,6 +50,25 @@ int regulator_enable(const struct device *dev) return ret; } +bool regulator_is_enabled(const struct device *dev) +{ + const struct regulator_common_config *config = + (struct regulator_common_config *)dev->config; + struct regulator_common_data *data = + (struct regulator_common_data *)dev->data; + bool enabled; + + if ((config->flags & REGULATOR_ALWAYS_ON) != 0U) { + enabled = true; + } else { + (void)k_mutex_lock(&data->lock, K_FOREVER); + enabled = data->refcnt != 0; + k_mutex_unlock(&data->lock); + } + + return enabled; +} + int regulator_disable(const struct device *dev) { const struct regulator_driver_api *api = diff --git a/include/zephyr/drivers/regulator.h b/include/zephyr/drivers/regulator.h index 44885331322..7b0b7c5dbdb 100644 --- a/include/zephyr/drivers/regulator.h +++ b/include/zephyr/drivers/regulator.h @@ -210,6 +210,16 @@ static inline int regulator_parent_dvs_state_set(const struct device *dev, */ int regulator_enable(const struct device *dev); +/** + * @brief Check if a regulator is enabled. + * + * @param dev Regulator device instance. + * + * @retval true If regulator is enabled. + * @retval false If regulator is disabled. + */ +bool regulator_is_enabled(const struct device *dev); + /** * @brief Disable a regulator. *