From bd131adb7e78d11c883a8e7734dff93b87bfac8e Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Fri, 1 Oct 2021 13:07:57 -0600 Subject: zephyr: test: isl923x AC prochot Verify setting the prochot amps as well as edge conditions BRANCH=none BUG=b:201602829 TEST=zmake configure --test zephyr/test/drivers Signed-off-by: Yuval Peress Change-Id: I306adf1d74eea65a8522c61e5ed71cf8deacfe2a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3200054 Reviewed-by: Wai-Hong Tam --- zephyr/emul/emul_isl923x.c | 27 +++++++++++++++ zephyr/include/emul/emul_isl923x.h | 11 ++++++- zephyr/test/drivers/src/isl923x.c | 67 +++++++++++++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/zephyr/emul/emul_isl923x.c b/zephyr/emul/emul_isl923x.c index b4ab16ce68..db2a7a654a 100644 --- a/zephyr/emul/emul_isl923x.c +++ b/zephyr/emul/emul_isl923x.c @@ -43,6 +43,9 @@ LOG_MODULE_REGISTER(isl923x_emul, CONFIG_ISL923X_EMUL_LOG_LEVEL); /** Mask used for the control 1 register */ #define REG_CONTROL1_MASK (GENMASK(15, 8) | GENMASK(6, 0)) +/** Mask used for the AC PROCHOT register */ +#define REG_PROCHOT_AC_MASK GENMASK(12, 7) + struct isl923x_emul_data { /** Common I2C data */ struct i2c_common_emul_data common; @@ -62,6 +65,8 @@ struct isl923x_emul_data { uint16_t control_0_reg; /** Emulated control 1 register */ uint16_t control_1_reg; + /** Emulated AC PROCHOT register */ + uint16_t ac_prochot_reg; }; struct isl923x_emul_cfg { @@ -69,6 +74,13 @@ struct isl923x_emul_cfg { const struct i2c_common_emul_cfg common; }; +const struct device *isl923x_emul_get_parent(const struct emul *emulator) +{ + struct isl923x_emul_data *data = emulator->data; + + return data->common.i2c; +} + struct i2c_emul *isl923x_emul_get_i2c_emul(const struct emul *emulator) { struct isl923x_emul_data *data = emulator->data; @@ -179,6 +191,13 @@ static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, else *val = (uint8_t)((data->control_1_reg >> 8) & 0xff); break; + case ISL923X_REG_PROCHOT_AC: + __ASSERT_NO_MSG(bytes == 0 || bytes == 1); + if (bytes == 0) + *val = (uint8_t)(data->ac_prochot_reg & 0xff); + else + *val = (uint8_t)((data->ac_prochot_reg >> 8) & 0xff); + break; default: return -EINVAL; } @@ -239,6 +258,14 @@ static int isl923x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, else data->control_1_reg |= (val << 8) & REG_CONTROL1_MASK; break; + case ISL923X_REG_PROCHOT_AC: + __ASSERT_NO_MSG(bytes == 1 || bytes == 2); + if (bytes == 1) + data->ac_prochot_reg = val & REG_PROCHOT_AC_MASK; + else + data->ac_prochot_reg |= (val << 8) & + REG_PROCHOT_AC_MASK; + break; default: return -EINVAL; } diff --git a/zephyr/include/emul/emul_isl923x.h b/zephyr/include/emul/emul_isl923x.h index 8b34f7b519..d43917a45b 100644 --- a/zephyr/include/emul/emul_isl923x.h +++ b/zephyr/include/emul/emul_isl923x.h @@ -6,8 +6,17 @@ #ifndef ZEPHYR_INCLUDE_EMUL_EMUL_ISL923X_H_ #define ZEPHYR_INCLUDE_EMUL_EMUL_ISL923X_H_ -#include +#include #include +#include + +/** + * @brief Get the emulator's parent bus device + * + * @param emulator The emulator to look-up + * @return Pointer to the bus connecting to the emulator + */ +const struct device *isl923x_emul_get_parent(const struct emul *emulator); /** * @brief Get the I2C emulator struct diff --git a/zephyr/test/drivers/src/isl923x.c b/zephyr/test/drivers/src/isl923x.c index ecb4435306..99f0c8d7fa 100644 --- a/zephyr/test/drivers/src/isl923x.c +++ b/zephyr/test/drivers/src/isl923x.c @@ -32,8 +32,10 @@ BUILD_ASSERT(IS_ENABLED(CONFIG_CHARGER_ISL9238), #if CONFIG_CHARGER_SENSE_RESISTOR_AC == 20 #define EXPECTED_INPUT_CURRENT_MA(n) (n) +#define EXPECTED_INPUT_CURRENT_REG(n) (n) #else #define EXPECTED_INPUT_CURRENT_MA(n) (n * 2) +#define EXPECTED_INPUT_CURRENT_REG(n) (n / 2) #endif #define CHARGER_NUM get_charger_num(&isl923x_drv) @@ -311,6 +313,68 @@ void test_post_init(void) zassert_ok(isl923x_drv.post_init(CHARGER_NUM), NULL); } +void test_set_ac_prochot(void) +{ + const struct emul *isl923x_emul = ISL923X_EMUL; + const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); + struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + uint16_t expected_current_milli_amps[] = { + EXPECTED_INPUT_CURRENT_MA(0), + EXPECTED_INPUT_CURRENT_MA(128), + EXPECTED_INPUT_CURRENT_MA(256), + EXPECTED_INPUT_CURRENT_MA(512), + EXPECTED_INPUT_CURRENT_MA(1024), + EXPECTED_INPUT_CURRENT_MA(2048), + EXPECTED_INPUT_CURRENT_MA(4096) + }; + uint16_t current_milli_amps; + + /* Test can't set current above max */ + zassert_equal(EC_ERROR_INVAL, + isl923x_set_ac_prochot( + CHARGER_NUM, ISL923X_AC_PROCHOT_CURRENT_MAX + 1), + NULL); + + /* Test failed I2C write to prochot register */ + i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_PROCHOT_AC); + zassert_equal(EC_ERROR_INVAL, isl923x_set_ac_prochot(CHARGER_NUM, 0), + NULL); + + /* Clear write fail reg */ + i2c_common_emul_set_write_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + + for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) { + uint8_t reg_addr = ISL923X_REG_PROCHOT_AC; + + /* + * Due to resistor multiplying the current, the upper end of the + * test data might be out of bounds (which is already tested + * above). Skip the test. + */ + if (expected_current_milli_amps[i] > + ISL923X_AC_PROCHOT_CURRENT_MAX) { + continue; + } + + zassert_ok(isl923x_set_ac_prochot( + CHARGER_NUM, expected_current_milli_amps[i]), + "Failed to set AC prochot to %dmA", + expected_current_milli_amps[i]); + zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, + sizeof(reg_addr), ¤t_milli_amps, + sizeof(current_milli_amps)), + "Failed to read AC prochot register"); + zassert_equal(EXPECTED_INPUT_CURRENT_REG( + expected_current_milli_amps[i]), + current_milli_amps, + "AC prochot expected %dmA but got %dmA", + EXPECTED_INPUT_CURRENT_REG( + expected_current_milli_amps[i]), + current_milli_amps); + } +} + void test_suite_isl923x(void) { ztest_test_suite(isl923x, @@ -323,6 +387,7 @@ void test_suite_isl923x(void) ztest_unit_test(test_get_info), ztest_unit_test(test_status), ztest_unit_test(test_set_mode), - ztest_unit_test(test_post_init)); + ztest_unit_test(test_post_init), + ztest_unit_test(test_set_ac_prochot)); ztest_run_test_suite(isl923x); } -- cgit v1.2.1