Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend regulator API about current limits #64516

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions drivers/regulator/regulator_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ static int cmd_vget(const struct shell *sh, size_t argc, char **argv)
return 0;
}

static int cmd_clist(const struct shell *sh, size_t argc, char **argv)
{
const struct device *dev;
unsigned int current_cnt;
int32_t last_current_ua;

ARG_UNUSED(argc);

dev = device_get_binding(argv[1]);
if (dev == NULL) {
shell_error(sh, "Regulator device %s not available", argv[1]);
return -ENODEV;
}

current_cnt = regulator_count_current_limits(dev);

for (unsigned int i = 0U; i < current_cnt; i++) {
int32_t current_ua;

(void)regulator_list_current_limit(dev, i, &current_ua);

/* do not print repeated current limits */
if ((i == 0U) || (last_current_ua != current_ua)) {
microtoshell(sh, 'A', current_ua);
}

last_current_ua = current_ua;
}

return 0;
}

static int cmd_iset(const struct shell *sh, size_t argc, char **argv)
{
const struct device *dev;
Expand Down Expand Up @@ -449,6 +481,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
"Get voltage\n"
"Usage: vget <device>",
cmd_vget, 2, 0),
SHELL_CMD_ARG(clist, &dsub_device_name,
"List all supported current limits\n"
"Usage: clist <device>",
cmd_clist, 2, 0),
SHELL_CMD_ARG(iset, &dsub_device_name,
"Set current limit\n"
"Input requires units, e.g. 200ma, 20.5ma, 10ua, 1a...\n"
Expand Down
56 changes: 56 additions & 0 deletions include/zephyr/drivers/regulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ typedef int (*regulator_set_voltage_t)(const struct device *dev, int32_t min_uv,
int32_t max_uv);
typedef int (*regulator_get_voltage_t)(const struct device *dev,
int32_t *volt_uv);
typedef unsigned int (*regulator_count_current_limits_t)(const struct device *dev);
typedef int (*regulator_list_current_limit_t)(const struct device *dev,
unsigned int idx, int32_t *current_ua);
typedef int (*regulator_set_current_limit_t)(const struct device *dev,
int32_t min_ua, int32_t max_ua);
typedef int (*regulator_get_current_limit_t)(const struct device *dev,
Expand All @@ -95,6 +98,8 @@ __subsystem struct regulator_driver_api {
regulator_list_voltage_t list_voltage;
regulator_set_voltage_t set_voltage;
regulator_get_voltage_t get_voltage;
regulator_count_current_limits_t count_current_limits;
regulator_list_current_limit_t list_current_limit;
regulator_set_current_limit_t set_current_limit;
regulator_get_current_limit_t get_current_limit;
regulator_set_mode_t set_mode;
Expand Down Expand Up @@ -485,6 +490,57 @@ static inline int regulator_get_voltage(const struct device *dev,
return api->get_voltage(dev, volt_uv);
}

/**
* @brief Obtain the number of supported current limit levels.
*
* Each current limit level supported by a regulator gets an index, starting from
* zero. The total number of supported current limit levels can be used together with
* regulator_list_current_limit() to list all supported current limit levels.
*
* @param dev Regulator device instance.
*
* @return Number of supported current limits.
*/
static inline unsigned int regulator_count_current_limits(const struct device *dev)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;

if (api->count_current_limits == NULL) {
return 0U;
}

return api->count_current_limits(dev);
}

/**
* @brief Obtain the value of a current limit given an index.
*
* Each current limit level supported by a regulator gets an index, starting from
* zero. Together with regulator_count_current_limits(), this function can be used
* to iterate over all supported current limits.
*
* @param dev Regulator device instance.
* @param idx Current index.
* @param[out] current_ua Where current for the given @p index will be stored, in
* microamps.
*
* @retval 0 If @p index corresponds to a supported current limit.
* @retval -EINVAL If @p index does not correspond to a supported current limit.
*/
static inline int regulator_list_current_limit(const struct device *dev,
unsigned int idx, int32_t *current_ua)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;

if (api->list_current_limit == NULL) {
return -EINVAL;
}

return api->list_current_limit(dev, idx, current_ua);
}

/**
* @brief Set output current limit.
*
Expand Down