drivers: regulator: improve regulator_get_voltage

- Function now returns error, value is obtained by reference. This
  allows to propagate potential bus errors.

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

View file

@ -295,15 +295,13 @@ static int regulator_pca9420_set_voltage(const struct device *dev,
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Gets the current output voltage in uV * Gets the current output voltage in uV
*/ */
static int32_t regulator_pca9420_get_voltage(const struct device *dev) static int regulator_pca9420_get_voltage(const struct device *dev,
int32_t *volt_uv)
{ {
const struct regulator_pca9420_config *config = dev->config; const struct regulator_pca9420_config *config = dev->config;
struct regulator_pca9420_common_data *cdata = config->parent->data; struct regulator_pca9420_common_data *cdata = config->parent->data;
int32_t voltage = 0;
(void)regulator_pca9420_get_voltage_mode(dev, cdata->mode, &voltage); return regulator_pca9420_get_voltage_mode(dev, cdata->mode, volt_uv);
return voltage;
} }
/** /**

View file

@ -56,6 +56,7 @@ static int cmd_reg_dis(const struct shell *sh, size_t argc, char **argv)
static int cmd_set_vol(const struct shell *sh, size_t argc, char **argv) static int cmd_set_vol(const struct shell *sh, size_t argc, char **argv)
{ {
int lvol, uvol, ret; int lvol, uvol, ret;
int32_t volt_uv;
const struct device *reg_dev; const struct device *reg_dev;
reg_dev = device_get_binding(argv[1]); reg_dev = device_get_binding(argv[1]);
@ -72,12 +73,12 @@ static int cmd_set_vol(const struct shell *sh, size_t argc, char **argv)
shell_error(sh, "failed to set voltage, error %d", ret); shell_error(sh, "failed to set voltage, error %d", ret);
return ret; return ret;
} }
ret = regulator_get_voltage(reg_dev); ret = regulator_get_voltage(reg_dev, &volt_uv);
if (ret < 0) { if (ret < 0) {
shell_error(sh, "failed to read voltage, error %d", ret); shell_error(sh, "failed to read voltage, error %d", ret);
return ret; return ret;
} }
shell_print(sh, "set voltage to %d uV", ret); shell_print(sh, "set voltage to %d uV", volt_uv);
return 0; return 0;
} }

View file

@ -36,7 +36,7 @@ __subsystem struct regulator_driver_api {
int32_t *volt_uv); int32_t *volt_uv);
int (*set_voltage)(const struct device *dev, int32_t min_uv, int (*set_voltage)(const struct device *dev, int32_t min_uv,
int32_t max_uv); int32_t max_uv);
int32_t (*get_voltage)(const struct device *dev); int (*get_voltage)(const struct device *dev, int32_t *volt_uv);
int (*set_current_limit)(const struct device *dev, int32_t min_ua, int (*set_current_limit)(const struct device *dev, int32_t min_ua,
int32_t max_ua); int32_t max_ua);
int (*get_current_limit)(const struct device *dev); int (*get_current_limit)(const struct device *dev);
@ -195,19 +195,23 @@ static inline int regulator_set_voltage(const struct device *dev,
* @brief Obtain output voltage. * @brief Obtain output voltage.
* *
* @param dev Regulator device instance. * @param dev Regulator device instance.
* @param[out] volt_uv Where configured output voltage will be stored.
* *
* @return Voltage level in microvolts. * @retval 0 If successful
* @retval -ENOSYS If function is not implemented.
* @retval -errno In case of any other error.
*/ */
static inline int32_t regulator_get_voltage(const struct device *dev) static inline int regulator_get_voltage(const struct device *dev,
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->get_voltage == NULL) { if (api->get_voltage == NULL) {
return 0; return -ENOSYS;
} }
return api->get_voltage(dev); return api->get_voltage(dev, volt_uv);
} }
/** /**