Skip to content

Commit

Permalink
drivers: regulator: provide generic regulator_is_supported_voltage
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
gmarull authored and carlescufi committed Dec 14, 2022
1 parent c77c3ac commit 4e8795a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
18 changes: 18 additions & 0 deletions drivers/regulator/regulator_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
21 changes: 0 additions & 21 deletions drivers/regulator/regulator_pca9420.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 5 additions & 18 deletions include/zephyr/drivers/regulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 4e8795a

Please sign in to comment.