Commit 97efd21c authored by Ryan McClelland's avatar Ryan McClelland Committed by Henrik Brix Andersen
Browse files

tests: drivers: regulator: add api test for active discharge



Add tests for active discharge apis not being implemented.

Signed-off-by: default avatarRyan McClelland <ryanmcclelland@meta.com>
parent 5a125a61
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -39,6 +39,10 @@ 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_set_active_discharge, const struct device *,
		       bool);
DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_active_discharge, const struct device *,
		       bool *);
DEFINE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags,
		       const struct device *, regulator_error_flags_t *);

@@ -53,6 +57,8 @@ static struct regulator_driver_api api = {
	.get_current_limit = regulator_fake_get_current_limit,
	.set_mode = regulator_fake_set_mode,
	.get_mode = regulator_fake_get_mode,
	.set_active_discharge = regulator_fake_set_active_discharge,
	.get_active_discharge = regulator_fake_get_active_discharge,
	.get_error_flags = regulator_fake_get_error_flags,
};

+4 −0
Original line number Diff line number Diff line
@@ -31,6 +31,10 @@ 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_set_active_discharge, const struct device *,
			bool);
DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_active_discharge, const struct device *,
			bool *);
DECLARE_FAKE_VALUE_FUNC(int, regulator_fake_get_error_flags,
			const struct device *, regulator_error_flags_t *);

+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
			regulator-max-microamp = <200>;
			regulator-allowed-modes = <1 10>;
			regulator-initial-mode = <1>;
			regulator-active-discharge = <1>;
		};
	};
};
+57 −3
Original line number Diff line number Diff line
@@ -108,15 +108,16 @@ ZTEST(regulator_api, test_common_config)
	zassert_equal(config->max_ua, INT32_MAX);
	zassert_equal(config->allowed_modes_cnt, 0U);
	zassert_equal(config->initial_mode, REGULATOR_INITIAL_MODE_UNKNOWN);
	zassert_equal(config->flags, 0U);
	zassert_equal(REGULATOR_ACTIVE_DISCHARGE_GET_BITS(config->flags),
	    REGULATOR_ACTIVE_DISCHARGE_DEFAULT);

	/* reg1: regulator-always-on */
	config = reg1->config;
	zassert_equal(config->flags, REGULATOR_ALWAYS_ON);
	zassert_equal(config->flags & REGULATOR_ALWAYS_ON, REGULATOR_ALWAYS_ON);

	/* reg2: regulator-boot-on */
	config = reg2->config;
	zassert_equal(config->flags, REGULATOR_BOOT_ON);
	zassert_equal(config->flags & REGULATOR_BOOT_ON, REGULATOR_BOOT_ON);

	/* reg3: regulator-min/max-microvolt/microamp */
	config = reg3->config;
@@ -127,6 +128,7 @@ ZTEST(regulator_api, test_common_config)
	zassert_equal(config->allowed_modes[0], 1U);
	zassert_equal(config->allowed_modes[1], 10U);
	zassert_equal(config->allowed_modes_cnt, 2U);
	zassert_equal(REGULATOR_ACTIVE_DISCHARGE_GET_BITS(config->flags), 1U);
}

ZTEST(regulator_api, test_common_is_init_enabled)
@@ -611,6 +613,58 @@ ZTEST(regulator_api, test_get_mode_not_implemented)
	zassert_equal(ret, -ENOSYS);
}

ZTEST(regulator_api, test_set_active_discharge_not_implemented)
{
	int ret;
	struct regulator_driver_api *api =
		(struct regulator_driver_api *)reg0->api;
	regulator_set_active_discharge_t set_active_discharge = api->set_active_discharge;

	api->set_active_discharge = NULL;
	ret = regulator_set_active_discharge(reg0, false);
	api->set_active_discharge = set_active_discharge;

	zassert_equal(ret, -ENOSYS);
}

static int get_active_discharge_ok(const struct device *dev, bool *active_discharge)
{
	ARG_UNUSED(dev);

	*active_discharge = true;

	return 0;
}

ZTEST(regulator_api, test_get_active_discharge_ok)
{
	bool active_discharge;

	RESET_FAKE(regulator_fake_get_active_discharge);

	regulator_fake_get_active_discharge_fake.custom_fake = get_active_discharge_ok;

	zassert_equal(regulator_get_active_discharge(reg0, &active_discharge), false);
	zassert_equal(active_discharge, true);
	zassert_equal(regulator_fake_get_active_discharge_fake.call_count, 1U);
	zassert_equal(regulator_fake_get_active_discharge_fake.arg0_val, reg0);
	zassert_equal(regulator_fake_get_active_discharge_fake.arg1_val, &active_discharge);
}

ZTEST(regulator_api, test_get_active_discharge_not_implemented)
{
	int ret;
	struct regulator_driver_api *api =
		(struct regulator_driver_api *)reg0->api;
	regulator_get_active_discharge_t get_active_discharge = api->get_active_discharge;

	api->get_active_discharge = NULL;
	ret = regulator_get_active_discharge(reg0, NULL);
	api->get_active_discharge = get_active_discharge;

	zassert_equal(ret, -ENOSYS);
}

static int get_mode_ok(const struct device *dev, regulator_mode_t *mode)
{
	ARG_UNUSED(dev);