drivers: regulator: Added ship mode to API

Added ship mode entry function to common regulator driver

Signed-off-by: Andy Sinclair <andy.sinclair@nordicsemi.no>
This commit is contained in:
Andy Sinclair 2023-07-14 13:46:23 +01:00 committed by Fabio Baltieri
parent d95c12848d
commit f6aa1c4321
4 changed files with 71 additions and 0 deletions

View file

@ -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))

View file

@ -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);
}
/** @} */
/**

View file

@ -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
}

View file

@ -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;