drivers: regulator: improve regulator_list_voltages

- Rename to regulator_list_voltage (it is listing a single voltage)
- Function returns the value via a parameters, so that we can indicate
  wether the given index is valid or not. If a driver doesn't implement
  this call, function returns -EINVAL (as it should be).

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-12-01 11:37:10 +01:00 committed by Carles Cufí
commit c77c3ac7d6
2 changed files with 24 additions and 27 deletions

View file

@ -271,23 +271,14 @@ static unsigned int regulator_pca9420_count_voltages(const struct device *dev)
config->desc->num_ranges); config->desc->num_ranges);
} }
/** static int regulator_pca9420_list_voltage(const struct device *dev,
* Part of the extended regulator consumer API unsigned int idx, int32_t *volt_uv)
* Returns the supported voltage in uV for a given selector value
*/
static int32_t regulator_pca9420_list_voltages(const struct device *dev,
unsigned int selector)
{ {
const struct regulator_pca9420_config *config = dev->config; const struct regulator_pca9420_config *config = dev->config;
int32_t value;
if (linear_range_group_get_value(config->desc->ranges, return linear_range_group_get_value(config->desc->ranges,
config->desc->num_ranges, selector, config->desc->num_ranges, idx,
&value) < 0) { volt_uv);
return 0;
}
return value;
} }
/** /**
@ -536,7 +527,7 @@ static const struct regulator_driver_api api = {
.enable = regulator_pca9420_enable, .enable = regulator_pca9420_enable,
.disable = regulator_pca9420_disable, .disable = regulator_pca9420_disable,
.count_voltages = regulator_pca9420_count_voltages, .count_voltages = regulator_pca9420_count_voltages,
.list_voltages = regulator_pca9420_list_voltages, .list_voltage = regulator_pca9420_list_voltage,
.is_supported_voltage = regulator_pca9420_is_supported_voltage, .is_supported_voltage = regulator_pca9420_is_supported_voltage,
.set_voltage = regulator_pca9420_set_voltage, .set_voltage = regulator_pca9420_set_voltage,
.get_voltage = regulator_pca9420_get_voltage, .get_voltage = regulator_pca9420_get_voltage,

View file

@ -32,8 +32,8 @@ __subsystem struct regulator_driver_api {
int (*enable)(const struct device *dev); int (*enable)(const struct device *dev);
int (*disable)(const struct device *dev); int (*disable)(const struct device *dev);
unsigned int (*count_voltages)(const struct device *dev); unsigned int (*count_voltages)(const struct device *dev);
int32_t (*list_voltages)(const struct device *dev, int (*list_voltage)(const struct device *dev, unsigned int idx,
unsigned int selector); int32_t *volt_uv);
int (*is_supported_voltage)(const struct device *dev, int32_t min_uv, int (*is_supported_voltage)(const struct device *dev, int32_t min_uv,
int32_t max_uv); int32_t max_uv);
int (*set_voltage)(const struct device *dev, int32_t min_uv, int (*set_voltage)(const struct device *dev, int32_t min_uv,
@ -110,7 +110,7 @@ int regulator_disable(const struct device *dev);
* *
* Each voltage level supported by a regulator gets an index, starting from * Each voltage level supported by a regulator gets an index, starting from
* zero. The total number of supported voltage levels can be used together with * zero. The total number of supported voltage levels can be used together with
* regulator_list_voltages() to list all supported voltage levels. * regulator_list_voltage() to list all supported voltage levels.
* *
* @param dev Regulator device instance. * @param dev Regulator device instance.
* *
@ -129,25 +129,31 @@ static inline unsigned int regulator_count_voltages(const struct device *dev)
} }
/** /**
* @brief Obtain supported voltages. * @brief Obtain the value of a voltage given an index.
*
* Each voltage level supported by a regulator gets an index, starting from
* zero. Together with regulator_count_voltages(), this function can be used
* to iterate over all supported voltages.
* *
* @param dev Regulator device instance. * @param dev Regulator device instance.
* @param selector Voltage selector. * @param idx Voltage index.
* @param[out] volt_uv Where voltage for the given @p index will be stored, in
* microvolts.
* *
* @return voltage Voltage level in microvolts. * @retval 0 If @p index corresponds to a supported voltage.
* @retval 0 If selector code can't be used. * @retval -EINVAL If @p index does not correspond to a supported voltage.
*/ */
static inline int32_t regulator_list_voltages(const struct device *dev, static inline int regulator_list_voltage(const struct device *dev,
unsigned int selector) unsigned int idx, int32_t *volt_uv)
{ {
const struct regulator_driver_api *api = const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api; (const struct regulator_driver_api *)dev->api;
if (api->list_voltages == NULL) { if (api->list_voltage == NULL) {
return 0; return -EINVAL;
} }
return api->list_voltages(dev, selector); return api->list_voltage(dev, idx, volt_uv);
} }
/** /**