summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2021-09-20 13:30:55 -0600
committerCommit Bot <commit-bot@chromium.org>2021-09-21 02:41:45 +0000
commit5a12a3dbca6e07882d1760b115dcbf3b7ec5931c (patch)
tree82d80374a980ec2135408075113d1c1299e7ce21
parent26e3f0f8596300e1b7f64f426c0469f9bfee669f (diff)
downloadchrome-ec-5a12a3dbca6e07882d1760b115dcbf3b7ec5931c.tar.gz
zephyr: test: add a generic I2C mock
Add a simple mock with no state to test common i2c code. BRANCH=none BUG=b:200589041 TEST=zmake testall Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: I99730361a298708278106deb58890a0ed2c9febf Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3171694 Tested-by: Yuval Peress <peress@google.com> Reviewed-by: Jeremy Bettis <jbettis@chromium.org> Commit-Queue: Yuval Peress <peress@google.com>
-rw-r--r--zephyr/dts/bindings/emul/cros,i2c-mock.yaml12
-rw-r--r--zephyr/emul/CMakeLists.txt1
-rw-r--r--zephyr/emul/Kconfig1
-rw-r--r--zephyr/emul/Kconfig.i2c_mock22
-rw-r--r--zephyr/emul/i2c_mock.c70
-rw-r--r--zephyr/include/emul/i2c_mock.h35
6 files changed, 141 insertions, 0 deletions
diff --git a/zephyr/dts/bindings/emul/cros,i2c-mock.yaml b/zephyr/dts/bindings/emul/cros,i2c-mock.yaml
new file mode 100644
index 0000000000..7da69028bd
--- /dev/null
+++ b/zephyr/dts/bindings/emul/cros,i2c-mock.yaml
@@ -0,0 +1,12 @@
+# Copyright 2021 The chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+description: A generic I2C mock
+
+compatible: "cros,i2c-mock"
+
+include: base.yaml
+properties:
+ reg:
+ required: true
diff --git a/zephyr/emul/CMakeLists.txt b/zephyr/emul/CMakeLists.txt
index b46c51c8b6..02b176b942 100644
--- a/zephyr/emul/CMakeLists.txt
+++ b/zephyr/emul/CMakeLists.txt
@@ -14,3 +14,4 @@ zephyr_library_sources_ifdef(CONFIG_EMUL_TCS3400 emul_tcs3400.c)
zephyr_library_sources_ifdef(CONFIG_EMUL_BB_RETIMER emul_bb_retimer.c)
zephyr_library_sources_ifdef(CONFIG_EMUL_LN9310 emul_ln9310.c)
zephyr_library_sources_ifdef(CONFIG_EMUL_LIS2DW12 emul_lis2dw12.c)
+zephyr_library_sources_ifdef(CONFIG_I2C_MOCK i2c_mock.c)
diff --git a/zephyr/emul/Kconfig b/zephyr/emul/Kconfig
index c108caea4b..8d9c8e42ea 100644
--- a/zephyr/emul/Kconfig
+++ b/zephyr/emul/Kconfig
@@ -69,3 +69,4 @@ config EMUL_BB_RETIMER
rsource "Kconfig.ln9310"
rsource "Kconfig.lis2dw12"
+rsource "Kconfig.i2c_mock"
diff --git a/zephyr/emul/Kconfig.i2c_mock b/zephyr/emul/Kconfig.i2c_mock
new file mode 100644
index 0000000000..6c98a32739
--- /dev/null
+++ b/zephyr/emul/Kconfig.i2c_mock
@@ -0,0 +1,22 @@
+# Copyright 2021 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+DT_COMPAT_I2C_MOCK := cros,i2c-mock
+
+menuconfig I2C_MOCK
+ bool "Mock implementation of an I2C device"
+ default $(dt_compat_enabled,$(DT_COMPAT_I2C_MOCK))
+ depends on I2C_EMUL
+ help
+ Enable the I2C mock. This driver is a pure mock and does nothing by
+ default. It is used to test common i2c code. Mock API is available in
+ zephyr/include/emul/i2c_mock.h
+
+if I2C_MOCK
+
+module = I2C_MOCK
+module-str = i2c_mock
+source "subsys/logging/Kconfig.template.log_config"
+
+endif # I2C_MOCK
diff --git a/zephyr/emul/i2c_mock.c b/zephyr/emul/i2c_mock.c
new file mode 100644
index 0000000000..7c3722ad2e
--- /dev/null
+++ b/zephyr/emul/i2c_mock.c
@@ -0,0 +1,70 @@
+/* Copyright 2021 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#define DT_DRV_COMPAT cros_i2c_mock
+
+#include <device.h>
+#include "emul/emul_common_i2c.h"
+
+#include <logging/log.h>
+LOG_MODULE_REGISTER(i2c_mock, CONFIG_I2C_MOCK_LOG_LEVEL);
+
+struct i2c_emul *i2c_mock_to_i2c_emul(const struct emul *emul)
+{
+ struct i2c_common_emul_data *data = emul->data;
+
+ return &(data->emul);
+}
+
+void i2c_mock_reset(const struct emul *emul)
+{
+ struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul);
+
+ i2c_common_emul_set_read_fail_reg(i2c_emul,
+ I2C_COMMON_EMUL_NO_FAIL_REG);
+ i2c_common_emul_set_write_fail_reg(i2c_emul,
+ I2C_COMMON_EMUL_NO_FAIL_REG);
+ i2c_common_emul_set_read_func(i2c_emul, NULL, NULL);
+ i2c_common_emul_set_write_func(i2c_emul, NULL, NULL);
+}
+
+uint16_t i2c_mock_get_addr(const struct emul *emul)
+{
+ const struct i2c_common_emul_cfg *cfg = emul->cfg;
+
+ return cfg->addr;
+}
+
+static const struct i2c_emul_api i2c_mock_api = {
+ .transfer = i2c_common_emul_transfer,
+};
+
+static int i2c_mock_init(const struct emul *emul,
+ const struct device *parent)
+{
+ const struct i2c_common_emul_cfg *cfg = emul->cfg;
+ struct i2c_common_emul_data *data = emul->data;
+
+ data->emul.api = &i2c_mock_api;
+ data->emul.addr = cfg->addr;
+ data->emul.parent = emul;
+ data->i2c = parent;
+ data->cfg = cfg;
+ i2c_common_emul_init(data);
+
+ return i2c_emul_register(parent, emul->dev_label, &data->emul);
+}
+
+#define INIT_I2C_MOCK(n) \
+ static const struct i2c_common_emul_cfg i2c_mock_cfg_##n = { \
+ .i2c_label = DT_INST_BUS_LABEL(n), \
+ .dev_label = DT_INST_LABEL(n), \
+ .addr = DT_INST_REG_ADDR(n), \
+ }; \
+ static struct i2c_common_emul_data i2c_mock_data_##n; \
+ EMUL_DEFINE(i2c_mock_init, DT_DRV_INST(n), &i2c_mock_cfg_##n, \
+ &i2c_mock_data_##n)
+
+DT_INST_FOREACH_STATUS_OKAY(INIT_I2C_MOCK)
diff --git a/zephyr/include/emul/i2c_mock.h b/zephyr/include/emul/i2c_mock.h
new file mode 100644
index 0000000000..e9e8d97252
--- /dev/null
+++ b/zephyr/include/emul/i2c_mock.h
@@ -0,0 +1,35 @@
+/* Copyright 2021 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef ZEPHYR_INCLUDE_EMUL_I2C_MOCK_H_
+#define ZEPHYR_INCLUDE_EMUL_I2C_MOCK_H_
+
+#include <emul.h>
+#include <drivers/i2c_emul.h>
+
+/**
+ * @brief reset the I2C mock.
+ *
+ * @param emul The mock device to reset.
+ */
+void i2c_mock_reset(const struct emul *emul);
+
+/**
+ * @brief Get the i2c emulator pointer from the top level mock.
+ *
+ * @param emul The mock device to query
+ * @return Pointer to the i2c emulator struct
+ */
+struct i2c_emul *i2c_mock_to_i2c_emul(const struct emul *emul);
+
+/**
+ * @brief Get the I2C address of the mock
+ *
+ * @param emul The mock device to query
+ * @return The address on the I2C bus
+ */
+uint16_t i2c_mock_get_addr(const struct emul *emul);
+
+#endif /* ZEPHYR_INCLUDE_EMUL_I2C_MOCK_H_ */