diff options
author | lschyi <lschyi@google.com> | 2022-10-17 11:26:12 +0800 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-10-20 03:44:21 +0000 |
commit | d66a74dd8227e7f14b37134af1c4c5dfdd7d84f2 (patch) | |
tree | 36c41b23019cfae6c66a8d84a986496b240fb027 /zephyr/emul | |
parent | 87dcea3e2146e9ee0fca65fb4c1ce452646fc70b (diff) | |
download | chrome-ec-d66a74dd8227e7f14b37134af1c4c5dfdd7d84f2.tar.gz |
zephyr/test: add tusb1064 usb mux emulator
Implemented tusb1064 usb mux emulator with basic i2c read/write, and
export peek reg functions.
BUG=b:229340646
TEST=none
BRANCH=none
Signed-off-by: lschyi <lschyi@google.com>
Change-Id: Ie91c216a6d1fb9ffc1decbb07c2b082b02bf8cda
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3965888
Reviewed-by: Ting Shen <phoenixshen@chromium.org>
Tested-by: Sung-Chi Li <lschyi@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
Commit-Queue: Sung-Chi Li <lschyi@chromium.org>
Diffstat (limited to 'zephyr/emul')
-rw-r--r-- | zephyr/emul/CMakeLists.txt | 1 | ||||
-rw-r--r-- | zephyr/emul/Kconfig | 7 | ||||
-rw-r--r-- | zephyr/emul/emul_tusb1064.c | 108 |
3 files changed, 116 insertions, 0 deletions
diff --git a/zephyr/emul/CMakeLists.txt b/zephyr/emul/CMakeLists.txt index f8e4bae15b..377be11792 100644 --- a/zephyr/emul/CMakeLists.txt +++ b/zephyr/emul/CMakeLists.txt @@ -13,6 +13,7 @@ zephyr_library_sources_ifdef(CONFIG_EMUL_BMI emul_bmi.c) zephyr_library_sources_ifdef(CONFIG_EMUL_BMI emul_bmi160.c) zephyr_library_sources_ifdef(CONFIG_EMUL_BMI emul_bmi260.c) zephyr_library_sources_ifdef(CONFIG_EMUL_TCS3400 emul_tcs3400.c) +zephyr_library_sources_ifdef(CONFIG_EMUL_TUSB1064 emul_tusb1064.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) diff --git a/zephyr/emul/Kconfig b/zephyr/emul/Kconfig index 3cabd96b6f..e445e30154 100644 --- a/zephyr/emul/Kconfig +++ b/zephyr/emul/Kconfig @@ -60,6 +60,13 @@ config EMUL_TCS3400 of proper access to reserved bits. Emulators API is available in zephyr/include/emul/emul_tcs3400.h +config EMUL_TUSB1064 + bool "TCS3400 emulator" + select EMUL_COMMON_I2C + help + Enable the TUSB1064 usb mux. This driver use emulated I2C bus. + Emulator API is available in zephyr/include/emul/emul_tusb1064.h. + config EMUL_BB_RETIMER bool "BB retimer emulator" select EMUL_COMMON_I2C diff --git a/zephyr/emul/emul_tusb1064.c b/zephyr/emul/emul_tusb1064.c new file mode 100644 index 0000000000..4396cc5fc5 --- /dev/null +++ b/zephyr/emul/emul_tusb1064.c @@ -0,0 +1,108 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <zephyr/device.h> + +#include "driver/usb_mux/tusb1064.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_stub_device.h" +#include "util.h" + +#define DT_DRV_COMPAT zephyr_tusb1064_emul + +#define TUSB1064_REG_MAX 255 + +struct tusb1064_data { + struct i2c_common_emul_data common; + uint8_t regs[TUSB1064_REG_MAX + 1]; +}; + +static const uint8_t default_values[TUSB1064_REG_MAX + 1] = { + [TUSB1064_REG_GENERAL] = 0x01, + [TUSB1064_REG_DP1DP3EQ_SEL] = 0x00, + [TUSB1064_REG_DP0DP2EQ_SEL] = 0x00, +}; + +void tusb1064_emul_reset_regs(const struct emul *emul) +{ + struct tusb1064_data *data = (struct tusb1064_data *)emul->data; + + memcpy(data->regs, default_values, TUSB1064_REG_MAX + 1); +} + +int tusb1064_emul_peek_reg(const struct emul *emul, int reg) +{ + struct tusb1064_data *data = (struct tusb1064_data *)emul->data; + uint8_t *regs = data->regs; + + if (!IN_RANGE(reg, 0, TUSB1064_REG_MAX)) { + return -1; + } + return regs[reg]; +} + +static int tusb1064_emul_read(const struct emul *emul, int reg, uint8_t *val, + int bytes, void *unused_data) +{ + struct tusb1064_data *data = (struct tusb1064_data *)emul->data; + uint8_t *regs = data->regs; + int pos = reg + bytes; + + if (!IN_RANGE(pos, 0, TUSB1064_REG_MAX)) { + return -1; + } + *val = regs[pos]; + + return 0; +} + +static int tusb1064_emul_write(const struct emul *emul, int reg, uint8_t val, + int bytes, void *unused_data) +{ + struct tusb1064_data *data = (struct tusb1064_data *)emul->data; + uint8_t *regs = data->regs; + int pos = reg + bytes - 1; + + if (!IN_RANGE(pos, 0, TUSB1064_REG_MAX) || + !IN_RANGE(val, 0, UINT8_MAX)) { + return -1; + } + regs[pos] = val; + + return 0; +} + +static int tusb1064_emul_init(const struct emul *emul, + const struct device *parent) +{ + struct tusb1064_data *data = (struct tusb1064_data *)emul->data; + struct i2c_common_emul_data *common_data = &data->common; + + i2c_common_emul_init(common_data); + i2c_common_emul_set_read_func(common_data, tusb1064_emul_read, NULL); + i2c_common_emul_set_write_func(common_data, tusb1064_emul_write, NULL); + + tusb1064_emul_reset_regs(emul); + + return 0; +} + +#define INIT_TUSB1064_EMUL(n) \ + static struct i2c_common_emul_cfg common_cfg_##n; \ + static struct tusb1064_data tusb1064_data_##n; \ + static struct i2c_common_emul_cfg common_cfg_##n = { \ + .dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)), \ + .data = &tusb1064_data_##n.common, \ + .addr = DT_INST_REG_ADDR(n) \ + }; \ + static struct tusb1064_data tusb1064_data_##n = { \ + .common = { .cfg = &common_cfg_##n } \ + }; \ + EMUL_DT_INST_DEFINE(n, tusb1064_emul_init, &tusb1064_data_##n, \ + &common_cfg_##n, &i2c_common_emul_api) + +DT_INST_FOREACH_STATUS_OKAY(INIT_TUSB1064_EMUL) + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); |