From 1ca1f25918c444faf90f7ef8406858306a71a6eb Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Wed, 21 Dec 2022 10:46:13 +0100 Subject: [PATCH] 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 --- drivers/regulator/regulator_fake.c | 3 ++ include/zephyr/drivers/regulator.h | 26 +++++++++++ include/zephyr/drivers/regulator/fake.h | 2 + tests/drivers/regulator/api/src/main.c | 58 +++++++++++++++++++++++++ 4 files changed, 89 insertions(+) diff --git a/drivers/regulator/regulator_fake.c b/drivers/regulator/regulator_fake.c index 0bba43ee9e..6b0643a9f0 100644 --- a/drivers/regulator/regulator_fake.c +++ b/drivers/regulator/regulator_fake.c @@ -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, }; diff --git a/include/zephyr/drivers/regulator.h b/include/zephyr/drivers/regulator.h index f346c40f9a..dba8c6e835 100644 --- a/include/zephyr/drivers/regulator.h +++ b/include/zephyr/drivers/regulator.h @@ -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. * diff --git a/include/zephyr/drivers/regulator/fake.h b/include/zephyr/drivers/regulator/fake.h index fe8b161c3c..4f97ab70e3 100644 --- a/include/zephyr/drivers/regulator/fake.h +++ b/include/zephyr/drivers/regulator/fake.h @@ -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 *); diff --git a/tests/drivers/regulator/api/src/main.c b/tests/drivers/regulator/api/src/main.c index 243e6e3e35..30cbd1f504 100644 --- a/tests/drivers/regulator/api/src/main.c +++ b/tests/drivers/regulator/api/src/main.c @@ -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;