From f6aa1c4321b9e501a9cee1bc68ed1a3a006bb35b Mon Sep 17 00:00:00 2001 From: Andy Sinclair Date: Fri, 14 Jul 2023 13:46:23 +0100 Subject: [PATCH] drivers: regulator: Added ship mode to API Added ship mode entry function to common regulator driver Signed-off-by: Andy Sinclair --- drivers/regulator/regulator_fake.c | 4 +++ include/zephyr/drivers/regulator.h | 29 ++++++++++++++++++++ include/zephyr/drivers/regulator/fake.h | 2 ++ tests/drivers/regulator/api/src/main.c | 36 +++++++++++++++++++++++++ 4 files changed, 71 insertions(+) diff --git a/drivers/regulator/regulator_fake.c b/drivers/regulator/regulator_fake.c index e204714a66b..05b74bc56e1 100644 --- a/drivers/regulator/regulator_fake.c +++ b/drivers/regulator/regulator_fake.c @@ -68,8 +68,12 @@ static int regulator_fake_init(const struct device *dev) DEFINE_FAKE_VALUE_FUNC(int, regulator_parent_fake_dvs_state_set, const struct device *, regulator_dvs_state_t); +DEFINE_FAKE_VALUE_FUNC(int, regulator_parent_fake_ship_mode, + const struct device *); + static struct regulator_parent_driver_api parent_api = { .dvs_state_set = regulator_parent_fake_dvs_state_set, + .ship_mode = regulator_parent_fake_ship_mode, }; #define FAKE_DATA_NAME(node_id) _CONCAT(data_, DT_DEP_ORD(node_id)) diff --git a/include/zephyr/drivers/regulator.h b/include/zephyr/drivers/regulator.h index 19c5d995798..ae37b04635e 100644 --- a/include/zephyr/drivers/regulator.h +++ b/include/zephyr/drivers/regulator.h @@ -58,9 +58,12 @@ typedef uint8_t regulator_error_flags_t; typedef int (*regulator_dvs_state_set_t)(const struct device *dev, regulator_dvs_state_t state); +typedef int (*regulator_ship_mode_t)(const struct device *dev); + /** @brief Driver-specific API functions to support parent regulator control. */ __subsystem struct regulator_parent_driver_api { regulator_dvs_state_set_t dvs_state_set; + regulator_ship_mode_t ship_mode; }; typedef int (*regulator_enable_t)(const struct device *dev); @@ -279,6 +282,32 @@ static inline int regulator_parent_dvs_state_set(const struct device *dev, return api->dvs_state_set(dev, state); } +/** + * @brief Enter ship mode. + * + * Some PMICs feature a ship mode, which allows the system to save power. + * Exit from low power is normally by pin transition. + * + * This API can be used when ship mode needs to be entered. + * + * @param dev Parent regulator device instance. + * + * @retval 0 If successful. + * @retval -ENOSYS If function is not implemented. + * @retval -errno In case of any other error. + */ +static inline int regulator_parent_ship_mode(const struct device *dev) +{ + const struct regulator_parent_driver_api *api = + (const struct regulator_parent_driver_api *)dev->api; + + if (api->ship_mode == NULL) { + return -ENOSYS; + } + + return api->ship_mode(dev); +} + /** @} */ /** diff --git a/include/zephyr/drivers/regulator/fake.h b/include/zephyr/drivers/regulator/fake.h index 4f97ab70e3c..1bfffe6381b 100644 --- a/include/zephyr/drivers/regulator/fake.h +++ b/include/zephyr/drivers/regulator/fake.h @@ -36,6 +36,8 @@ DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags, DECLARE_FAKE_VALUE_FUNC(int, regulator_parent_fake_dvs_state_set, const struct device *, regulator_dvs_state_t); +DECLARE_FAKE_VALUE_FUNC(int, regulator_parent_fake_ship_mode, + const struct device *); #ifdef __cplusplus } diff --git a/tests/drivers/regulator/api/src/main.c b/tests/drivers/regulator/api/src/main.c index 47310e2889b..cf598067301 100644 --- a/tests/drivers/regulator/api/src/main.c +++ b/tests/drivers/regulator/api/src/main.c @@ -60,6 +60,42 @@ ZTEST(regulator_api, test_parent_dvs_state_set_fail) zassert_equal(regulator_parent_fake_dvs_state_set_fake.call_count, 1U); } +ZTEST(regulator_api, test_parent_ship_mode_not_implemented) +{ + int ret; + struct regulator_parent_driver_api *api = + (struct regulator_parent_driver_api *)parent->api; + regulator_ship_mode_t ship_mode = api->ship_mode; + + api->ship_mode = NULL; + ret = regulator_parent_ship_mode(parent); + api->ship_mode = ship_mode; + + zassert_equal(ret, -ENOSYS); +} + +ZTEST(regulator_api, test_parent_ship_mode_ok) +{ + RESET_FAKE(regulator_parent_fake_ship_mode); + + regulator_parent_fake_ship_mode_fake.return_val = 0; + + zassert_equal(regulator_parent_ship_mode(parent), 0); + zassert_equal(regulator_parent_fake_ship_mode_fake.arg0_val, parent); + zassert_equal(regulator_parent_fake_ship_mode_fake.call_count, 1U); +} + +ZTEST(regulator_api, test_parent_ship_mode_fail) +{ + RESET_FAKE(regulator_parent_fake_ship_mode); + + regulator_parent_fake_ship_mode_fake.return_val = -ENOTSUP; + + zassert_equal(regulator_parent_ship_mode(parent), -ENOTSUP); + zassert_equal(regulator_parent_fake_ship_mode_fake.arg0_val, parent); + zassert_equal(regulator_parent_fake_ship_mode_fake.call_count, 1U); +} + ZTEST(regulator_api, test_common_config) { const struct regulator_common_config *config;