Cover the shunt/bus/temperature/power alert thresholds, the multi-alert OR behaviour, the alert latch, the DIAG_ALRT control bits and the conversion-ready (CNVRF) flag.
Signed-off-by: Emmanuel Blot <[email protected]> --- tests/qtest/ina238-test.c | 146 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c index 693139f988..08813377c9 100644 --- a/tests/qtest/ina238-test.c +++ b/tests/qtest/ina238-test.c @@ -306,7 +306,143 @@ static void test_overflow(void *obj, void *data, QGuestAllocator *alloc) DIAG_MATHOF); } -/* Shunt over-limit alert (transparent mode) */ +/* Shunt over-limit alert */ +static void test_alert_shunt(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + + qmp_ina238_set("shunt-voltage", 5000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, ==, 0); + + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, ==, + DIAG_SHNTOL); + + qmp_ina238_set("shunt-voltage", 5000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, ==, 0); +} + +/* Bus over-limit alert */ +static void test_alert_bus(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_BOVL, 0x1000); + + qmp_ina238_set("bus-voltage", 12000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_BUSOL, ==, 0); + + qmp_ina238_set("bus-voltage", 48000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_BUSOL, ==, DIAG_BUSOL); +} + +/* Over-temperature alert */ +static void test_alert_temp(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_TEMP_LIMIT, 0x0A00); + + qmp_ina238_set("die-temperature", 10000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_TMPOL, ==, 0); + + qmp_ina238_set("die-temperature", 25000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_TMPOL, ==, DIAG_TMPOL); +} + +/* Power over-limit alert */ +static void test_alert_power(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + i2c_set16(dev, REG_SHUNT_CAL, 0x0FD2); + + i2c_set16(dev, REG_PWR_LIMIT, 0x0100); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_POL, ==, DIAG_POL); + + i2c_set16(dev, REG_PWR_LIMIT, 0x7FFF); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_POL, ==, 0); +} + +/* Two thresholds crossed at once are reported simultaneously (multi-alert) */ +static void test_alert_multi(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + uint16_t diag; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + i2c_set16(dev, REG_BOVL, 0x1000); + + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + + diag = i2c_get16(dev, REG_DIAG_ALRT); + g_assert_cmphex(diag & DIAG_SHNTOL, ==, DIAG_SHNTOL); + g_assert_cmphex(diag & DIAG_BUSOL, ==, DIAG_BUSOL); +} + +/* Latch mode holds the flag until the DIAG_ALRT register is read */ +static void test_alert_latch(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("shunt-voltage", 5000000); + + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, ==, + DIAG_SHNTOL); + + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, ==, 0); +} + +/* DIAG_ALRT control bits are writable; status bits are read-only */ +static void test_diag_control(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + uint16_t diag; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + i2c_set16(dev, REG_DIAG_ALRT, + DIAG_ALATCH | DIAG_CNVR | DIAG_SLOWALERT | DIAG_APOL); + diag = i2c_get16(dev, REG_DIAG_ALRT); + g_assert_cmphex(diag & DIAG_ALATCH, ==, DIAG_ALATCH); + g_assert_cmphex(diag & DIAG_CNVR, ==, DIAG_CNVR); + g_assert_cmphex(diag & DIAG_SLOWALERT, ==, DIAG_SLOWALERT); + g_assert_cmphex(diag & DIAG_APOL, ==, DIAG_APOL); + /* MEMSTAT stays set */ + g_assert_cmphex(diag & DIAG_MEMSTAT, ==, DIAG_MEMSTAT); +} + +/* Every DIAG_ALRT read clears CNVRF, whatever the alert-latch mode. */ +static void test_cnvrf(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev = (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, ==, DIAG_CNVRF); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, ==, 0); + + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + qmp_ina238_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, ==, DIAG_CNVRF); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, ==, 0); +} + static void ina238_register_nodes(void) { QOSGraphEdgeOptions opts = { @@ -329,5 +465,13 @@ static void ina238_register_nodes(void) qos_add_test("zero-cal", "ina238", test_zero_cal, NULL); qos_add_test("adcrange", "ina238", test_adcrange, NULL); qos_add_test("overflow", "ina238", test_overflow, NULL); + qos_add_test("alert-shunt", "ina238", test_alert_shunt, NULL); + qos_add_test("alert-bus", "ina238", test_alert_bus, NULL); + qos_add_test("alert-temp", "ina238", test_alert_temp, NULL); + qos_add_test("alert-power", "ina238", test_alert_power, NULL); + qos_add_test("alert-multi", "ina238", test_alert_multi, NULL); + qos_add_test("alert-latch", "ina238", test_alert_latch, NULL); + qos_add_test("diag-control", "ina238", test_diag_control, NULL); + qos_add_test("cnvrf", "ina238", test_cnvrf, NULL); } libqos_init(ina238_register_nodes); -- 2.50.1
