summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2021-10-01 13:10:51 -0600
committerCommit Bot <commit-bot@chromium.org>2021-10-05 14:23:01 +0000
commit00d382845ee04c53cf44079081ba27619f1cef27 (patch)
tree78a91fb47b47066bbee0462e6b368c4f5ca7ef71
parentbd131adb7e78d11c883a8e7734dff93b87bfac8e (diff)
downloadchrome-ec-00d382845ee04c53cf44079081ba27619f1cef27.tar.gz
zephyr: test: isl923x DC 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 <peress@google.com> Change-Id: I12cd413172bfdf395a1c60ef7e487b33687202bc Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3200055 Reviewed-by: Aaron Massey <aaronmassey@google.com> Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
-rw-r--r--zephyr/emul/emul_isl923x.c20
-rw-r--r--zephyr/test/drivers/src/isl923x.c60
2 files changed, 79 insertions, 1 deletions
diff --git a/zephyr/emul/emul_isl923x.c b/zephyr/emul/emul_isl923x.c
index db2a7a654a..5ee94dfaec 100644
--- a/zephyr/emul/emul_isl923x.c
+++ b/zephyr/emul/emul_isl923x.c
@@ -46,6 +46,9 @@ LOG_MODULE_REGISTER(isl923x_emul, CONFIG_ISL923X_EMUL_LOG_LEVEL);
/** Mask used for the AC PROCHOT register */
#define REG_PROCHOT_AC_MASK GENMASK(12, 7)
+/** Mask used for the DC PROCHOT register */
+#define REG_PROCHOT_DC_MASK GENMASK(13, 8)
+
struct isl923x_emul_data {
/** Common I2C data */
struct i2c_common_emul_data common;
@@ -67,6 +70,8 @@ struct isl923x_emul_data {
uint16_t control_1_reg;
/** Emulated AC PROCHOT register */
uint16_t ac_prochot_reg;
+ /** Emulated DC PROCHOT register */
+ uint16_t dc_prochot_reg;
};
struct isl923x_emul_cfg {
@@ -198,6 +203,13 @@ static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
else
*val = (uint8_t)((data->ac_prochot_reg >> 8) & 0xff);
break;
+ case ISL923X_REG_PROCHOT_DC:
+ __ASSERT_NO_MSG(bytes == 0 || bytes == 1);
+ if (bytes == 0)
+ *val = (uint8_t)(data->dc_prochot_reg & 0xff);
+ else
+ *val = (uint8_t)((data->dc_prochot_reg >> 8) & 0xff);
+ break;
default:
return -EINVAL;
}
@@ -266,6 +278,14 @@ static int isl923x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val,
data->ac_prochot_reg |= (val << 8) &
REG_PROCHOT_AC_MASK;
break;
+ case ISL923X_REG_PROCHOT_DC:
+ __ASSERT_NO_MSG(bytes == 1 || bytes == 2);
+ if (bytes == 1)
+ data->dc_prochot_reg = val & REG_PROCHOT_DC_MASK;
+ else
+ data->dc_prochot_reg |= (val << 8) &
+ REG_PROCHOT_DC_MASK;
+ break;
default:
return -EINVAL;
}
diff --git a/zephyr/test/drivers/src/isl923x.c b/zephyr/test/drivers/src/isl923x.c
index 99f0c8d7fa..908aa18766 100644
--- a/zephyr/test/drivers/src/isl923x.c
+++ b/zephyr/test/drivers/src/isl923x.c
@@ -26,8 +26,10 @@ BUILD_ASSERT(IS_ENABLED(CONFIG_CHARGER_ISL9238),
#if CONFIG_CHARGER_SENSE_RESISTOR == 10
#define EXPECTED_CURRENT_MA(n) (n)
+#define EXPECTED_CURRENT_REG(n) (n)
#else
#define EXPECTED_CURRENT_MA(n) (n * 2)
+#define EXPECTED_CURRENT_REG(n) (n / 2)
#endif
#if CONFIG_CHARGER_SENSE_RESISTOR_AC == 20
@@ -374,6 +376,61 @@ void test_set_ac_prochot(void)
current_milli_amps);
}
}
+void test_set_dc_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_CURRENT_MA(256), EXPECTED_CURRENT_MA(512),
+ EXPECTED_CURRENT_MA(1024), EXPECTED_CURRENT_MA(2048),
+ EXPECTED_CURRENT_MA(4096), EXPECTED_CURRENT_MA(8192)
+ };
+ uint16_t current_milli_amps;
+
+ /* Test can't set current above max */
+ zassert_equal(EC_ERROR_INVAL,
+ isl923x_set_dc_prochot(
+ CHARGER_NUM, ISL923X_DC_PROCHOT_CURRENT_MAX + 1),
+ NULL);
+
+ /* Test failed I2C write to prochot register */
+ i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_PROCHOT_DC);
+ zassert_equal(EC_ERROR_INVAL, isl923x_set_dc_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_DC;
+
+ /*
+ * 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_DC_PROCHOT_CURRENT_MAX) {
+ continue;
+ }
+ zassert_ok(isl923x_set_dc_prochot(
+ CHARGER_NUM, expected_current_milli_amps[i]),
+ "Failed to set DC prochot to %dmA",
+ expected_current_milli_amps[i]);
+ zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, &reg_addr,
+ sizeof(reg_addr), &current_milli_amps,
+ sizeof(current_milli_amps)),
+ "Failed to read DC prochot register");
+ zassert_equal(
+ EXPECTED_CURRENT_REG(expected_current_milli_amps[i]),
+ current_milli_amps,
+ "AC prochot expected %dmA but got %dmA",
+ EXPECTED_CURRENT_REG(expected_current_milli_amps[i]),
+ current_milli_amps);
+ }
+}
void test_suite_isl923x(void)
{
@@ -388,6 +445,7 @@ void test_suite_isl923x(void)
ztest_unit_test(test_status),
ztest_unit_test(test_set_mode),
ztest_unit_test(test_post_init),
- ztest_unit_test(test_set_ac_prochot));
+ ztest_unit_test(test_set_ac_prochot),
+ ztest_unit_test(test_set_dc_prochot));
ztest_run_test_suite(isl923x);
}