From 021f2754aa8722288cff2362c470a82ef352436b Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Sun, 3 Oct 2021 23:05:13 -0600 Subject: zephyr: test: isl923x::get_vbus_voltage Verify all the code paths in get_vbus_voltage (including calculated mV value). BRANCH=none BUG=b:201819565 TEST=zmake configure --test zephyr/test/drivers Signed-off-by: Yuval Peress Change-Id: I508880a8af30c2d546655424b289d521f3c22626 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3201915 Reviewed-by: Fabio Baltieri --- zephyr/emul/emul_isl923x.c | 18 ++++++++++++++++++ zephyr/include/emul/emul_isl923x.h | 9 +++++++++ zephyr/test/drivers/src/isl923x.c | 32 +++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/zephyr/emul/emul_isl923x.c b/zephyr/emul/emul_isl923x.c index f97af88da0..9b3aaec4f5 100644 --- a/zephyr/emul/emul_isl923x.c +++ b/zephyr/emul/emul_isl923x.c @@ -82,6 +82,8 @@ struct isl923x_emul_data { uint16_t ac_prochot_reg; /** Emulated DC PROCHOT register */ uint16_t dc_prochot_reg; + /** Emulated ADC vbus register */ + uint16_t adc_vbus_reg; }; struct isl923x_emul_cfg { @@ -137,6 +139,15 @@ void isl923x_emul_set_learn_mode_enabled(const struct emul *emulator, data->control_1_reg &= ~ISL923X_C1_LEARN_MODE_ENABLE; } +void isl923x_emul_set_adc_vbus(const struct emul *emulator, + uint16_t value) +{ + struct isl923x_emul_data *data = emulator->data; + + /* The VBUS voltage is returned in bits 13:6. The LSB is 96mV. */ + data->adc_vbus_reg = value & GENMASK(13, 6); +} + static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, int bytes) { @@ -234,6 +245,13 @@ static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, else *val = (uint8_t)((data->dc_prochot_reg >> 8) & 0xff); break; + case RAA489000_REG_ADC_VBUS: + __ASSERT_NO_MSG(bytes == 0 || bytes == 1); + if (bytes == 0) + *val = (uint8_t)(data->adc_vbus_reg & 0xff); + else + *val = (uint8_t)((data->adc_vbus_reg >> 8) & 0xff); + break; default: return -EINVAL; } diff --git a/zephyr/include/emul/emul_isl923x.h b/zephyr/include/emul/emul_isl923x.h index d43917a45b..3093b0cfe3 100644 --- a/zephyr/include/emul/emul_isl923x.h +++ b/zephyr/include/emul/emul_isl923x.h @@ -63,4 +63,13 @@ bool isl923x_emul_is_learn_mode_enabled(const struct emul *emulator); void isl923x_emul_set_learn_mode_enabled(const struct emul *emulator, bool enabled); +/** + * @brief Set the emulator's ADC vbus register + * + * @param emulator The emulator to modify + * @param value The new ADC register value + */ +void isl923x_emul_set_adc_vbus(const struct emul *emulator, + uint16_t value); + #endif /* ZEPHYR_INCLUDE_EMUL_EMUL_ISL923X_H_ */ diff --git a/zephyr/test/drivers/src/isl923x.c b/zephyr/test/drivers/src/isl923x.c index a2dd872c09..2753c12b26 100644 --- a/zephyr/test/drivers/src/isl923x.c +++ b/zephyr/test/drivers/src/isl923x.c @@ -537,6 +537,35 @@ static void test_discharge_on_ac(void) zassert_true((reg_value & ISL923X_C1_LEARN_MODE_ENABLE) == 0, NULL); } +static void test_get_vbus_voltage(void) +{ + const struct emul *isl923x_emul = ISL923X_EMUL; + struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + int reg_values[] = { BIT(6), BIT(7), BIT(8), BIT(9), + BIT(10), BIT(11), BIT(12), BIT(13) }; + int voltage; + + /* Test fail to read the ADC vbus register */ + i2c_common_emul_set_read_fail_reg(i2c_emul, RAA489000_REG_ADC_VBUS); + zassert_equal(EC_ERROR_INVAL, + isl923x_drv.get_vbus_voltage(CHARGER_NUM, 0, &voltage), + NULL); + i2c_common_emul_set_read_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + + for (int i = 0; i < ARRAY_SIZE(reg_values); ++i) { + int expected_voltage = (reg_values[i] >> 6) * 96; + + isl923x_emul_set_adc_vbus(isl923x_emul, reg_values[i]); + zassert_ok(isl923x_drv.get_vbus_voltage(CHARGER_NUM, 0, + &voltage), + NULL); + zassert_equal(expected_voltage, voltage, + "Expected %dmV but got %dmV", expected_voltage, + voltage); + } +} + void test_suite_isl923x(void) { ztest_test_suite(isl923x, @@ -553,6 +582,7 @@ void test_suite_isl923x(void) ztest_unit_test(test_set_ac_prochot), ztest_unit_test(test_set_dc_prochot), ztest_unit_test(test_comparator_inversion), - ztest_unit_test(test_discharge_on_ac)); + ztest_unit_test(test_discharge_on_ac), + ztest_unit_test(test_get_vbus_voltage)); ztest_run_test_suite(isl923x); } -- cgit v1.2.1