summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Yilun Lin <yllin@chromium.org>2023-03-10 15:18:36 +0800
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-03-13 08:59:48 +0000
commit45ffe7ad069930957bb7e4f9e0d718dc47406e7c (patch)
tree5bed68b594f07cdb423879513468a47218c277d3
parent7434ca9a4c641813589ea93fbf446b3efbd125b5 (diff)
downloadchrome-ec-45ffe7ad069930957bb7e4f9e0d718dc47406e7c.tar.gz
tcpci: test: add get_vbus_voltage test
Add the test for TCPCI get_vbus_voltage API. BUG=b:272664811 TEST=./twister -c -i -T zephyr/test/drivers BRANCH=none Change-Id: I5b26e1e0d43225b4318694d3e76d2133905013e2 Signed-off-by: Eric Yilun Lin <yllin@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4328914 Commit-Queue: Ting Shen <phoenixshen@chromium.org> Reviewed-by: Ting Shen <phoenixshen@chromium.org> Auto-Submit: Eric Yilun Lin <yllin@google.com> Tested-by: Eric Yilun Lin <yllin@google.com>
-rw-r--r--zephyr/emul/tcpc/emul_tcpci.c23
-rw-r--r--zephyr/include/emul/tcpc/emul_tcpci.h8
-rw-r--r--zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h11
-rw-r--r--zephyr/test/drivers/default/src/tcpci.c10
-rw-r--r--zephyr/test/drivers/default/src/tcpci_test_common.c49
5 files changed, 101 insertions, 0 deletions
diff --git a/zephyr/emul/tcpc/emul_tcpci.c b/zephyr/emul/tcpc/emul_tcpci.c
index 0deb0c2727..15859afd65 100644
--- a/zephyr/emul/tcpc/emul_tcpci.c
+++ b/zephyr/emul/tcpc/emul_tcpci.c
@@ -462,6 +462,29 @@ void tcpci_emul_set_rev(const struct emul *emul, enum tcpci_emul_rev rev)
}
}
+void tcpci_emul_set_vbus_voltage(const struct emul *emul, uint32_t vbus_mv)
+{
+ uint16_t meas;
+ uint16_t scale = 0;
+
+ __ASSERT(!(vbus_mv % TCPC_REG_VBUS_VOLTAGE_LSB),
+ "vbus_mv must be divisible by %d (%d)",
+ TCPC_REG_VBUS_VOLTAGE_LSB, vbus_mv);
+
+ meas = vbus_mv / TCPC_REG_VBUS_VOLTAGE_LSB;
+
+ while (meas >= (1 << 10) && scale < 3) {
+ __ASSERT(!(meas & 1), "vbus_mv %d does not fit into the reg.",
+ vbus_mv);
+ meas >>= 1;
+ scale += 1;
+ }
+ __ASSERT(scale < 3, "scale %d, meas %d doesn't fit into the reg.",
+ scale, meas);
+
+ tcpci_emul_set_reg(emul, TCPC_REG_VBUS_VOLTAGE, (scale << 10) | meas);
+}
+
/** Check description in emul_tcpci.h */
void tcpci_emul_set_alert_callback(const struct emul *emul,
tcpci_emul_alert_state_func alert_callback,
diff --git a/zephyr/include/emul/tcpc/emul_tcpci.h b/zephyr/include/emul/tcpc/emul_tcpci.h
index 1df40b2c1a..b0c3ac08a4 100644
--- a/zephyr/include/emul/tcpc/emul_tcpci.h
+++ b/zephyr/include/emul/tcpc/emul_tcpci.h
@@ -312,6 +312,14 @@ struct tcpci_emul_msg *tcpci_emul_get_tx_msg(const struct emul *emul);
void tcpci_emul_set_rev(const struct emul *emul, enum tcpci_emul_rev rev);
/**
+ * @brief Set TCPCI VBUS Voltage in VBUS_VOLTAGE register
+ *
+ * @param emul Pointer to TCPC emulator
+ * @param vbus_mv Requested VBUS in mV
+ */
+void tcpci_emul_set_vbus_voltage(const struct emul *emul, uint32_t vbus_mv);
+
+/**
* @brief Set callback which is called when alert register is changed
*
* @param emul Pointer to TCPC emulator
diff --git a/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h
index 08d75cccf7..9abaa1be53 100644
--- a/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h
+++ b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h
@@ -250,4 +250,15 @@ void test_tcpci_hard_reset_reinit(const struct emul *emul,
struct i2c_common_emul_data *common_data,
enum usbc_port port);
+/**
+ * @brief Test TCPCI get vbus voltage callback
+ *
+ * @param emul Pointer to TCPCI emulator
+ * @param common_data Pointer to emulated I2C bus
+ * @param port Select USBC port that will be used to obtain tcpm_drv from
+ * tcpc_config
+ */
+void test_tcpci_get_vbus_voltage(const struct emul *emul,
+ struct i2c_common_emul_data *common_data,
+ enum usbc_port port);
#endif /* __TCPCI_TEST_COMMON_H */
diff --git a/zephyr/test/drivers/default/src/tcpci.c b/zephyr/test/drivers/default/src/tcpci.c
index 90d5d3fb65..9806837f07 100644
--- a/zephyr/test/drivers/default/src/tcpci.c
+++ b/zephyr/test/drivers/default/src/tcpci.c
@@ -104,6 +104,16 @@ ZTEST(tcpci, test_generic_tcpci_set_rx_detect)
test_tcpci_set_rx_detect(emul, common_data, USBC_PORT_C0);
}
+/** Test TCPCI get vbus voltage */
+ZTEST(tcpci, test_generic_tcpci_get_vbus_voltage)
+{
+ const struct emul *emul = EMUL_DT_GET(TCPCI_EMUL_NODE);
+ struct i2c_common_emul_data *common_data =
+ emul_tcpci_generic_get_i2c_common_data(emul);
+
+ test_tcpci_get_vbus_voltage(emul, common_data, USBC_PORT_C0);
+}
+
/** Test TCPCI get raw message from TCPC revision 2.0 */
ZTEST(tcpci, test_generic_tcpci_get_rx_message_raw_rev2)
{
diff --git a/zephyr/test/drivers/default/src/tcpci_test_common.c b/zephyr/test/drivers/default/src/tcpci_test_common.c
index 01f1cd944c..20332369fa 100644
--- a/zephyr/test/drivers/default/src/tcpci_test_common.c
+++ b/zephyr/test/drivers/default/src/tcpci_test_common.c
@@ -979,6 +979,55 @@ void test_tcpci_get_chip_info(const struct emul *emul,
test_tcpci_get_chip_info_mutator_fail));
}
+void test_tcpci_get_vbus_voltage(const struct emul *emul,
+ struct i2c_common_emul_data *common_data,
+ enum usbc_port port)
+{
+ const struct tcpm_drv *drv = tcpc_config[port].drv;
+ const int fake_vbus[] = { 0, 25, 1500, 5000, 12000, 16000,
+ 20000, 25575, 25600, 51150, 51200, 102300 };
+ const int fake_meas[] = { 0, 1, 2, 3, 4, 5, 128, 256, 512, 1023 };
+ const int fake_scl[] = { 0, 1 << 10, 2 << 10 };
+ int vbus = 0;
+
+ /* Test dev_cap_1 unsupported */
+ tcpci_emul_set_reg(emul, TCPC_REG_DEV_CAP_1, 0);
+ zassert_equal(EC_ERROR_UNIMPLEMENTED,
+ drv->get_vbus_voltage(port, &vbus));
+
+ tcpci_emul_set_reg(emul, TCPC_REG_DEV_CAP_1,
+ TCPC_REG_DEV_CAP_1_VBUS_MEASURE_ALARM_CAPABLE);
+
+ /* Refresh cached dev_cap_1 */
+ zassert_equal(EC_SUCCESS, drv->init(port), NULL);
+
+ /* Test error on failed command get */
+ i2c_common_emul_set_read_fail_reg(common_data,
+ I2C_COMMON_EMUL_FAIL_ALL_REG);
+ zassert_equal(EC_ERROR_INVAL, drv->get_vbus_voltage(port, &vbus));
+ i2c_common_emul_set_read_fail_reg(common_data,
+ I2C_COMMON_EMUL_NO_FAIL_REG);
+
+ /* Test by setting vbus */
+ for (int i = 0; i < ARRAY_SIZE(fake_vbus); i++) {
+ tcpci_emul_set_vbus_voltage(emul, fake_vbus[i]);
+ zassert_equal(EC_SUCCESS, drv->get_vbus_voltage(port, &vbus));
+ zassert_equal(vbus, fake_vbus[i]);
+ }
+
+ /* Test by setting the register */
+ for (int i = 0; i < ARRAY_SIZE(fake_meas); i++) {
+ for (int j = 0; j < ARRAY_SIZE(fake_scl); j++) {
+ tcpci_emul_set_reg(emul, TCPC_REG_VBUS_VOLTAGE,
+ (fake_meas[i] | fake_scl[j]));
+ zassert_equal(EC_SUCCESS,
+ drv->get_vbus_voltage(port, &vbus));
+ zassert_equal(vbus, BIT(j) * fake_meas[i] *
+ TCPC_REG_VBUS_VOLTAGE_LSB);
+ }
+ }
+}
+
/** Test TCPCI enter low power mode */
void test_tcpci_low_power_mode(const struct emul *emul,
struct i2c_common_emul_data *common_data,