drivers: regulator: provide generic regulator_is_supported_voltage

The function can be implemented by using regulator_count_voltages() +
regulator_list_voltage(), so there's no need to defer the job to each
driver.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-12-01 12:05:22 +01:00 committed by Carles Cufí
commit 4e8795a8ce
3 changed files with 23 additions and 39 deletions

View file

@ -63,3 +63,21 @@ int regulator_disable(const struct device *dev)
return ret;
}
bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
int32_t max_uv)
{
unsigned int volt_cnt = regulator_count_voltages(dev);
for (unsigned int idx = 0U; idx < volt_cnt; idx++) {
int32_t volt_uv;
(void)regulator_list_voltage(dev, idx, &volt_uv);
if ((volt_uv > min_uv) && (volt_uv < max_uv)) {
return true;
}
}
return false;
}

View file

@ -188,10 +188,6 @@ static const struct regulator_pca9420_desc ldo2_desc = {
.num_ranges = ARRAY_SIZE(ldo2_ranges),
};
static int regulator_pca9420_is_supported_voltage(const struct device *dev,
int32_t min_uv,
int32_t max_uv);
static bool regulator_pca9420_is_mode_allowed(const struct device *dev,
uint8_t mode)
{
@ -281,22 +277,6 @@ static int regulator_pca9420_list_voltage(const struct device *dev,
volt_uv);
}
/**
* Part of the extended regulator consumer API
* Returns true if the regulator supports a voltage in the given range.
*/
static int regulator_pca9420_is_supported_voltage(const struct device *dev,
int32_t min_uv,
int32_t max_uv)
{
const struct regulator_pca9420_config *config = dev->config;
uint16_t idx;
return linear_range_group_get_win_index(config->desc->ranges,
config->desc->num_ranges,
min_uv, max_uv, &idx);
}
/**
* Part of the extended regulator consumer API
* Sets the output voltage to the closest supported voltage value
@ -528,7 +508,6 @@ static const struct regulator_driver_api api = {
.disable = regulator_pca9420_disable,
.count_voltages = regulator_pca9420_count_voltages,
.list_voltage = regulator_pca9420_list_voltage,
.is_supported_voltage = regulator_pca9420_is_supported_voltage,
.set_voltage = regulator_pca9420_set_voltage,
.get_voltage = regulator_pca9420_get_voltage,
.get_current_limit = regulator_pca9420_get_current_limit,

View file

@ -34,8 +34,6 @@ __subsystem struct regulator_driver_api {
unsigned int (*count_voltages)(const struct device *dev);
int (*list_voltage)(const struct device *dev, unsigned int idx,
int32_t *volt_uv);
int (*is_supported_voltage)(const struct device *dev, int32_t min_uv,
int32_t max_uv);
int (*set_voltage)(const struct device *dev, int32_t min_uv,
int32_t max_uv);
int32_t (*get_voltage)(const struct device *dev);
@ -157,28 +155,17 @@ static inline int regulator_list_voltage(const struct device *dev,
}
/**
* @brief Check if a voltage range is supported.
* @brief Check if a voltage within a window is supported.
*
* @param dev Regulator device instance.
* @param min_uv Minimum voltage in microvolts.
* @param max_uv maximum voltage in microvolts.
*
* @retval 0 If successful.
* @retval -ENOSYS If function is not implemented.
* @retval -errno In case of any other error.
* @retval true If voltage is supported.
* @retval false If voltage is not supported.
*/
static inline int regulator_is_supported_voltage(const struct device *dev,
int32_t min_uv, int32_t max_uv)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->is_supported_voltage == NULL) {
return -ENOSYS;
}
return api->is_supported_voltage(dev, min_uv, max_uv);
}
bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
int32_t max_uv);
/**
* @brief Set output voltage.