drivers: regulator: add get_mode API

Add a new API to query the configured regulator mode. Updated fake
driver and API tests.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-12-21 10:46:13 +01:00 committed by Carles Cufí
parent fbba106a23
commit 1ca1f25918
4 changed files with 89 additions and 0 deletions

View file

@ -37,6 +37,8 @@ DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_current_limit,
const struct device *, int32_t *);
DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_set_mode, const struct device *,
regulator_mode_t);
DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_mode, const struct device *,
regulator_mode_t *);
DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags,
const struct device *, regulator_error_flags_t *);
@ -50,6 +52,7 @@ static struct regulator_driver_api api = {
.set_current_limit = regulator_fake_set_current_limit,
.get_current_limit = regulator_fake_get_current_limit,
.set_mode = regulator_fake_set_mode,
.get_mode = regulator_fake_get_mode,
.get_error_flags = regulator_fake_get_error_flags,
};

View file

@ -76,6 +76,8 @@ typedef int (*regulator_get_current_limit_t)(const struct device *dev,
int32_t *curr_ua);
typedef int (*regulator_set_mode_t)(const struct device *dev,
regulator_mode_t mode);
typedef int (*regulator_get_mode_t)(const struct device *dev,
regulator_mode_t *mode);
typedef int (*regulator_get_error_flags_t)(
const struct device *dev, regulator_error_flags_t *flags);
@ -90,6 +92,7 @@ __subsystem struct regulator_driver_api {
regulator_set_current_limit_t set_current_limit;
regulator_get_current_limit_t get_current_limit;
regulator_set_mode_t set_mode;
regulator_get_mode_t get_mode;
regulator_get_error_flags_t get_error_flags;
};
@ -457,6 +460,29 @@ static inline int regulator_get_current_limit(const struct device *dev,
*/
int regulator_set_mode(const struct device *dev, regulator_mode_t mode);
/**
* @brief Get mode.
*
* @param dev Regulator device instance.
* @param[out] mode Where mode will be stored.
*
* @retval 0 If successful.
* @retval -ENOSYS If function is not implemented.
* @retval -errno In case of any other error.
*/
static inline int regulator_get_mode(const struct device *dev,
regulator_mode_t *mode)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->get_mode == NULL) {
return -ENOSYS;
}
return api->get_mode(dev, mode);
}
/**
* @brief Get active error flags.
*

View file

@ -29,6 +29,8 @@ DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_current_limit,
const struct device *, int32_t *);
DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_set_mode, const struct device *,
regulator_mode_t);
DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_mode, const struct device *,
regulator_mode_t *);
DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags,
const struct device *, regulator_error_flags_t *);

View file

@ -553,6 +553,64 @@ ZTEST(regulator_api, test_set_mode_dt_limit)
zassert_equal(regulator_fake_set_mode_fake.call_count, 2U);
}
ZTEST(regulator_api, test_get_mode_not_implemented)
{
int ret;
struct regulator_driver_api *api =
(struct regulator_driver_api *)reg0->api;
regulator_get_mode_t get_mode = api->get_mode;
api->get_mode = NULL;
ret = regulator_get_mode(reg0, NULL);
api->get_mode = get_mode;
zassert_equal(ret, -ENOSYS);
}
static int get_mode_ok(const struct device *dev, regulator_mode_t *mode)
{
ARG_UNUSED(dev);
*mode = 10U;
return 0;
}
ZTEST(regulator_api, test_get_mode_ok)
{
regulator_mode_t mode;
RESET_FAKE(regulator_fake_get_mode);
regulator_fake_get_mode_fake.custom_fake = get_mode_ok;
zassert_equal(regulator_get_mode(reg0, &mode), 0U);
zassert_equal(mode, 10U);
zassert_equal(regulator_fake_get_mode_fake.call_count, 1U);
zassert_equal(regulator_fake_get_mode_fake.arg0_val, reg0);
zassert_equal(regulator_fake_get_mode_fake.arg1_val, &mode);
}
static int get_mode_fail(const struct device *dev, regulator_mode_t *mode)
{
ARG_UNUSED(dev);
ARG_UNUSED(mode);
return -EIO;
}
ZTEST(regulator_api, test_get_mode_error)
{
RESET_FAKE(regulator_fake_get_mode);
regulator_fake_get_mode_fake.custom_fake = get_mode_fail;
zassert_equal(regulator_get_mode(reg0, NULL), -EIO);
zassert_equal(regulator_fake_get_mode_fake.call_count, 1U);
zassert_equal(regulator_fake_get_mode_fake.arg0_val, reg0);
zassert_equal(regulator_fake_get_mode_fake.arg1_val, NULL);
}
ZTEST(regulator_api, test_get_error_flags_not_implemented)
{
int ret;