summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2021-09-30 10:28:45 -0600
committerCommit Bot <commit-bot@chromium.org>2021-10-02 02:27:48 +0000
commit4bcdfecae714b175c1dc65549a33c71d55badb20 (patch)
tree7ba88b0b686d53a8711693bc36843e9d1edaed62
parent9aadff4307ab45b3dde6fcec26edc14136bc9dfe (diff)
downloadchrome-ec-4bcdfecae714b175c1dc65549a33c71d55badb20.tar.gz
zephyr: test: isl923x: Test input current get/set
Test all the code paths for the get/set input current of the isl923x. BRANCH=none BUG=b:201602829 TEST=zmake configure --test zephyr/test/drivers Signed-off-by: Yuval Peress <peress@google.com> Change-Id: I452690a4087c97b67e75bad23e1b82e5b3dc366a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3198235 Reviewed-by: Jeremy Bettis <jbettis@chromium.org> Reviewed-by: Aaron Massey <aaronmassey@google.com>
-rw-r--r--zephyr/emul/emul_isl923x.c55
-rw-r--r--zephyr/include/emul/emul_isl923x.h13
-rw-r--r--zephyr/test/drivers/src/isl923x.c81
3 files changed, 148 insertions, 1 deletions
diff --git a/zephyr/emul/emul_isl923x.c b/zephyr/emul/emul_isl923x.c
index 5d4ed35efa..d841d73aa6 100644
--- a/zephyr/emul/emul_isl923x.c
+++ b/zephyr/emul/emul_isl923x.c
@@ -31,11 +31,21 @@ LOG_MODULE_REGISTER(isl923x_emul, CONFIG_ISL923X_EMUL_LOG_LEVEL);
/** Mask used for the system voltage max register */
#define REG_SYS_VOLTAGE_MAX_MASK GENMASK(14, 3)
+/** Mask used for the adapter current limit 1 register */
+#define REG_ADAPTER_CURRENT_LIMIT1_MASK GENMASK(12, 2)
+
+/** Mask used for the adapter current limit 2 register */
+#define REG_ADAPTER_CURRENT_LIMIT2_MASK GENMASK(12, 2)
+
struct isl923x_emul_data {
/** Common I2C data */
struct i2c_common_emul_data common;
/** Emulated charge current limit register */
uint16_t current_limit_reg;
+ /** Emulated adapter current limit 1 register */
+ uint16_t adapter_current_limit1_reg;
+ /** Emulated adapter current limit 2 register */
+ uint16_t adapter_current_limit2_reg;
/** Emulated max voltage register */
uint16_t max_volt_reg;
};
@@ -45,6 +55,13 @@ struct isl923x_emul_cfg {
const struct i2c_common_emul_cfg common;
};
+struct i2c_emul *isl923x_emul_get_i2c_emul(const struct emul *emulator)
+{
+ struct isl923x_emul_data *data = emulator->data;
+
+ return &(data->common.emul);
+}
+
static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
int bytes)
{
@@ -65,6 +82,26 @@ static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
else
*val = (uint8_t)((data->max_volt_reg >> 8) & 0xff);
break;
+ case ISL923X_REG_ADAPTER_CURRENT_LIMIT1:
+ __ASSERT_NO_MSG(bytes == 0 || bytes == 1);
+ if (bytes == 0)
+ *val = (uint8_t)(data->adapter_current_limit1_reg &
+ 0xff);
+ else
+ *val = (uint8_t)((data->adapter_current_limit1_reg >>
+ 8) &
+ 0xff);
+ break;
+ case ISL923X_REG_ADAPTER_CURRENT_LIMIT2:
+ __ASSERT_NO_MSG(bytes == 0 || bytes == 1);
+ if (bytes == 0)
+ *val = (uint8_t)(data->adapter_current_limit2_reg &
+ 0xff);
+ else
+ *val = (uint8_t)((data->adapter_current_limit2_reg >>
+ 8) &
+ 0xff);
+ break;
default:
return -EINVAL;
}
@@ -93,6 +130,24 @@ static int isl923x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val,
data->max_volt_reg |= (val << 8) &
REG_SYS_VOLTAGE_MAX_MASK;
break;
+ case ISL923X_REG_ADAPTER_CURRENT_LIMIT1:
+ __ASSERT_NO_MSG(bytes == 1 || bytes == 2);
+ if (bytes == 1)
+ data->adapter_current_limit1_reg =
+ val & REG_ADAPTER_CURRENT_LIMIT1_MASK;
+ else
+ data->adapter_current_limit1_reg |=
+ (val << 8) & REG_ADAPTER_CURRENT_LIMIT1_MASK;
+ break;
+ case ISL923X_REG_ADAPTER_CURRENT_LIMIT2:
+ __ASSERT_NO_MSG(bytes == 1 || bytes == 2);
+ if (bytes == 1)
+ data->adapter_current_limit2_reg =
+ val & REG_ADAPTER_CURRENT_LIMIT2_MASK;
+ else
+ data->adapter_current_limit2_reg |=
+ (val << 8) & REG_ADAPTER_CURRENT_LIMIT2_MASK;
+ break;
default:
return -EINVAL;
}
diff --git a/zephyr/include/emul/emul_isl923x.h b/zephyr/include/emul/emul_isl923x.h
index 659b87d59e..ffd3d52112 100644
--- a/zephyr/include/emul/emul_isl923x.h
+++ b/zephyr/include/emul/emul_isl923x.h
@@ -6,4 +6,17 @@
#ifndef ZEPHYR_INCLUDE_EMUL_EMUL_ISL923X_H_
#define ZEPHYR_INCLUDE_EMUL_EMUL_ISL923X_H_
+#include <emul.h>
+#include <drivers/i2c_emul.h>
+
+/**
+ * @brief Get the I2C emulator struct
+ *
+ * This is generally coupled with calls to i2c_common_emul_* functions.
+ *
+ * @param emulator The emulator to look-up
+ * @return Pointer to the I2C emulator struct
+ */
+struct i2c_emul *isl923x_emul_get_i2c_emul(const struct emul *emulator);
+
#endif /* ZEPHYR_INCLUDE_EMUL_EMUL_ISL923X_H_ */
diff --git a/zephyr/test/drivers/src/isl923x.c b/zephyr/test/drivers/src/isl923x.c
index 2b0de72280..0eb261d837 100644
--- a/zephyr/test/drivers/src/isl923x.c
+++ b/zephyr/test/drivers/src/isl923x.c
@@ -8,19 +8,31 @@
#include "battery.h"
#include "charger_utils.h"
+#include "driver/charger/isl923x.h"
#include "driver/charger/isl923x_public.h"
+#include "emul/emul_common_i2c.h"
#include "emul/emul_isl923x.h"
BUILD_ASSERT(CONFIG_CHARGER_SENSE_RESISTOR == 10 ||
CONFIG_CHARGER_SENSE_RESISTOR == 5);
+BUILD_ASSERT(CONFIG_CHARGER_SENSE_RESISTOR_AC == 20 ||
+ CONFIG_CHARGER_SENSE_RESISTOR_AC == 10);
+
#if CONFIG_CHARGER_SENSE_RESISTOR == 10
#define EXPECTED_CURRENT_MA(n) (n)
#else
#define EXPECTED_CURRENT_MA(n) (n * 2)
#endif
+#if CONFIG_CHARGER_SENSE_RESISTOR_AC == 20
+#define EXPECTED_INPUT_CURRENT_MA(n) (n)
+#else
+#define EXPECTED_INPUT_CURRENT_MA(n) (n * 2)
+#endif
+
#define CHARGER_NUM get_charger_num(&isl923x_drv)
+#define ISL923X_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul)))
void test_isl923x_set_current(void)
{
@@ -80,10 +92,77 @@ void test_isl923x_set_voltage(void)
}
}
+void test_isl923x_set_input_current_limit(void)
+{
+ const struct emul *isl923x_emul = ISL923X_EMUL;
+ struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul);
+ int expected_current_milli_amps[] = {
+ EXPECTED_INPUT_CURRENT_MA(0),
+ EXPECTED_INPUT_CURRENT_MA(4),
+ EXPECTED_INPUT_CURRENT_MA(8),
+ EXPECTED_INPUT_CURRENT_MA(16),
+ EXPECTED_INPUT_CURRENT_MA(32),
+ EXPECTED_INPUT_CURRENT_MA(64),
+ 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) };
+ int current_milli_amps;
+
+ /* Test failing to write to current limit 1 reg */
+ i2c_common_emul_set_write_fail_reg(i2c_emul,
+ ISL923X_REG_ADAPTER_CURRENT_LIMIT1);
+ zassert_equal(EC_ERROR_INVAL,
+ isl923x_drv.set_input_current_limit(CHARGER_NUM, 0),
+ NULL);
+
+ /* Test failing to write to current limit 2 reg */
+ i2c_common_emul_set_write_fail_reg(i2c_emul,
+ ISL923X_REG_ADAPTER_CURRENT_LIMIT2);
+ zassert_equal(EC_ERROR_INVAL,
+ isl923x_drv.set_input_current_limit(CHARGER_NUM, 0),
+ NULL);
+
+ /* Reset fail register */
+ i2c_common_emul_set_write_fail_reg(i2c_emul,
+ I2C_COMMON_EMUL_NO_FAIL_REG);
+
+ /* Test failing to read current limit 1 reg */
+ i2c_common_emul_set_read_fail_reg(i2c_emul,
+ ISL923X_REG_ADAPTER_CURRENT_LIMIT1);
+ zassert_equal(EC_ERROR_INVAL,
+ isl923x_drv.get_input_current_limit(CHARGER_NUM,
+ &current_milli_amps),
+ NULL);
+
+ /* Reset fail register */
+ i2c_common_emul_set_read_fail_reg(i2c_emul,
+ I2C_COMMON_EMUL_NO_FAIL_REG);
+
+ /* Test normal code path */
+ for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) {
+ zassert_ok(isl923x_drv.set_input_current_limit(
+ CHARGER_NUM, expected_current_milli_amps[i]),
+ "Failed to set input current limit to %dmV",
+ expected_current_milli_amps[i]);
+ zassert_ok(isl923x_drv.get_input_current_limit(
+ CHARGER_NUM, &current_milli_amps),
+ "Failed to get input current limit");
+ zassert_equal(expected_current_milli_amps[i],
+ current_milli_amps,
+ "Expected input current %dmA but got %dmA",
+ expected_current_milli_amps[i],
+ current_milli_amps);
+ }
+}
+
void test_suite_isl923x(void)
{
ztest_test_suite(isl923x,
ztest_unit_test(test_isl923x_set_current),
- ztest_unit_test(test_isl923x_set_voltage));
+ ztest_unit_test(test_isl923x_set_voltage),
+ ztest_unit_test(test_isl923x_set_input_current_limit));
ztest_run_test_suite(isl923x);
}