From 6bf531fc1c115b24e2148fc4e040081ef354cdf6 Mon Sep 17 00:00:00 2001 From: jeffrey Lin Date: Wed, 8 Mar 2023 16:20:12 +0800 Subject: zephyr/test: add ps8743 usb mux emulator Implemented ps8743 usb mux emulator BUG=b:250798561 TEST=none BRANCH=none Change-Id: I8c9f5845d43de8b23f0cb25ad98f8a27eac9cfaf Signed-off-by: jeffrey Lin Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4318613 Reviewed-by: Ting Shen Tested-by: Jeffrey Lin Commit-Queue: Jeffrey Lin Reviewed-by: Jeffrey Lin --- zephyr/Kconfig.usb_mux | 2 +- zephyr/dts/bindings/emul/zephyr,ps8743-emul.yaml | 9 ++ zephyr/emul/CMakeLists.txt | 1 + zephyr/emul/Kconfig | 9 ++ zephyr/emul/emul_ps8743.c | 105 +++++++++++++++++++++++ zephyr/include/emul/emul_ps8743.h | 13 +++ zephyr/shim/include/usbc/ps8743_usb_mux.h | 1 + zephyr/shim/include/usbc/usb_muxes.h | 1 + 8 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 zephyr/dts/bindings/emul/zephyr,ps8743-emul.yaml create mode 100644 zephyr/emul/emul_ps8743.c create mode 100644 zephyr/include/emul/emul_ps8743.h diff --git a/zephyr/Kconfig.usb_mux b/zephyr/Kconfig.usb_mux index d014b85dd0..08715e8d6b 100644 --- a/zephyr/Kconfig.usb_mux +++ b/zephyr/Kconfig.usb_mux @@ -57,7 +57,7 @@ config PLATFORM_EC_USB_MUX_IT5205 config PLATFORM_EC_USB_MUX_PS8743 bool "Parade PS8743 USB-C Host Switch with redriver" default y - depends on DT_HAS_PARADE_PS8743_ENABLED + depends on DT_HAS_PARADE_PS8743_ENABLED || DT_HAS_ZEPHYR_PS8743_EMUL_ENABLED help This is a Parade USB 3.1 Gen 1 / DisplayPort (DP) Alt Mode High-Bit-Rate 2 (HBR2) redriver. It provides control of switching diff --git a/zephyr/dts/bindings/emul/zephyr,ps8743-emul.yaml b/zephyr/dts/bindings/emul/zephyr,ps8743-emul.yaml new file mode 100644 index 0000000000..80ba56896e --- /dev/null +++ b/zephyr/dts/bindings/emul/zephyr,ps8743-emul.yaml @@ -0,0 +1,9 @@ +# Copyright 2023 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +# +description: Zephyr PS8743 Emulator + +compatible: "zephyr,ps8743-emul" + +include: "parade,ps8743.yaml" diff --git a/zephyr/emul/CMakeLists.txt b/zephyr/emul/CMakeLists.txt index 6404fad247..e94b7e7d37 100644 --- a/zephyr/emul/CMakeLists.txt +++ b/zephyr/emul/CMakeLists.txt @@ -21,6 +21,7 @@ zephyr_library_sources_ifdef(CONFIG_EMUL_LIS2DW12 emul_lis2dw12.c) zephyr_library_sources_ifdef(CONFIG_EMUL_LN9310 emul_ln9310.c) zephyr_library_sources_ifdef(CONFIG_EMUL_PCT2075 emul_pct2075.c) zephyr_library_sources_ifdef(CONFIG_EMUL_PPC_SYV682X emul_syv682x.c) +zephyr_library_sources_ifdef(CONFIG_EMUL_PS8743 emul_ps8743.c) zephyr_library_sources_ifdef(CONFIG_EMUL_RT9490 emul_rt9490.c) zephyr_library_sources_ifdef(CONFIG_EMUL_RTC emul_rtc.c) zephyr_library_sources_ifdef(CONFIG_EMUL_SMART_BATTERY emul_smart_battery.c) diff --git a/zephyr/emul/Kconfig b/zephyr/emul/Kconfig index cae2972858..a7a0591c09 100644 --- a/zephyr/emul/Kconfig +++ b/zephyr/emul/Kconfig @@ -149,6 +149,15 @@ config EMUL_PCT2075 Enable the PCT2075 temperature sensor emulator. It uses emulated I2C bus. Emulator API is available in zephyr/include/emul/emul_pct2075.h. +config EMUL_PS8743 + bool "PS8743 emulator" + default y + depends on DT_HAS_ZEPHYR_PS8743_EMUL_ENABLED + select EMUL_COMMON_I2C + help + Enable the PS8743 usb mux. This driver use emulated I2C bus. + Emulator API is available in zephyr/include/emul/emul_ps8743.h. + rsource "Kconfig.ln9310" rsource "Kconfig.lis2dw12" rsource "Kconfig.i2c_mock" diff --git a/zephyr/emul/emul_ps8743.c b/zephyr/emul/emul_ps8743.c new file mode 100644 index 0000000000..181519644f --- /dev/null +++ b/zephyr/emul/emul_ps8743.c @@ -0,0 +1,105 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "driver/usb_mux/ps8743.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_stub_device.h" +#include "util.h" + +#include + +#define DT_DRV_COMPAT zephyr_ps8743_emul + +#define PS8743_REG_MAX 255 + +struct ps8743_data { + struct i2c_common_emul_data common; + uint8_t regs[PS8743_REG_MAX + 1]; +}; + +static const uint8_t default_values[PS8743_REG_MAX + 1] = { + [PS8743_REG_USB_EQ_RX] = 0x00, +}; + +void ps8743_emul_reset_regs(const struct emul *emul) +{ + struct ps8743_data *data = (struct ps8743_data *)emul->data; + + memcpy(data->regs, default_values, PS8743_REG_MAX + 1); +} + +int ps8743_emul_peek_reg(const struct emul *emul, int reg) +{ + struct ps8743_data *data = (struct ps8743_data *)emul->data; + uint8_t *regs = data->regs; + + if (!IN_RANGE(reg, 0, PS8743_REG_MAX)) { + return -1; + } + return regs[reg]; +} + +static int ps8743_emul_read(const struct emul *emul, int reg, uint8_t *val, + int bytes, void *unused_data) +{ + struct ps8743_data *data = (struct ps8743_data *)emul->data; + uint8_t *regs = data->regs; + int pos = reg + bytes; + + if (!IN_RANGE(pos, 0, PS8743_REG_MAX)) { + return -1; + } + *val = regs[pos]; + + return 0; +} + +static int ps8743_emul_write(const struct emul *emul, int reg, uint8_t val, + int bytes, void *unused_data) +{ + struct ps8743_data *data = (struct ps8743_data *)emul->data; + uint8_t *regs = data->regs; + int pos = reg + bytes - 1; + + if (!IN_RANGE(pos, 0, PS8743_REG_MAX) || !IN_RANGE(val, 0, UINT8_MAX)) { + return -1; + } + regs[pos] = val; + + return 0; +} + +static int ps8743_emul_init(const struct emul *emul, + const struct device *parent) +{ + struct ps8743_data *data = (struct ps8743_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, ps8743_emul_read, NULL); + i2c_common_emul_set_write_func(common_data, ps8743_emul_write, NULL); + + ps8743_emul_reset_regs(emul); + + return 0; +} + +#define INIT_PS8743_EMUL(n) \ + static struct i2c_common_emul_cfg common_cfg_##n; \ + static struct ps8743_data ps8743_data##n; \ + static struct i2c_common_emul_cfg common_cfg_##n = { \ + .dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)), \ + .data = &ps8743_data##n.common, \ + .addr = DT_INST_REG_ADDR(n) \ + }; \ + static struct ps8743_data ps8743_data##n = { \ + .common = { .cfg = &common_cfg_##n } \ + }; \ + EMUL_DT_INST_DEFINE(n, ps8743_emul_init, &ps8743_data##n, \ + &common_cfg_##n, &i2c_common_emul_api, NULL) + +DT_INST_FOREACH_STATUS_OKAY(INIT_PS8743_EMUL) + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); diff --git a/zephyr/include/emul/emul_ps8743.h b/zephyr/include/emul/emul_ps8743.h new file mode 100644 index 0000000000..503d014317 --- /dev/null +++ b/zephyr/include/emul/emul_ps8743.h @@ -0,0 +1,13 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef EMUL_PS8743_H +#define EMUL_PS8743_H + +#include + +int ps8743_emul_peek_reg(const struct emul *emul, int reg); + +#endif diff --git a/zephyr/shim/include/usbc/ps8743_usb_mux.h b/zephyr/shim/include/usbc/ps8743_usb_mux.h index 8cb54f4c17..7f9c5e2139 100644 --- a/zephyr/shim/include/usbc/ps8743_usb_mux.h +++ b/zephyr/shim/include/usbc/ps8743_usb_mux.h @@ -9,6 +9,7 @@ #include "usb_mux/ps8743_public.h" #define PS8743_USB_MUX_COMPAT parade_ps8743 +#define PS8743_EMUL_COMPAT zephyr_ps8743_emul /* clang-format off */ #define USB_MUX_CONFIG_PS8743(mux_id) \ diff --git a/zephyr/shim/include/usbc/usb_muxes.h b/zephyr/shim/include/usbc/usb_muxes.h index c7aee09011..f08d897ba3 100644 --- a/zephyr/shim/include/usbc/usb_muxes.h +++ b/zephyr/shim/include/usbc/usb_muxes.h @@ -34,6 +34,7 @@ (BB_RETIMER_USB_MUX_COMPAT, USB_MUX_CONFIG_BB_RETIMER), \ (IT5205_USB_MUX_COMPAT, USB_MUX_CONFIG_IT5205), \ (PS8743_USB_MUX_COMPAT, USB_MUX_CONFIG_PS8743), \ + (PS8743_EMUL_COMPAT, USB_MUX_CONFIG_PS8743), \ (PS8818_USB_MUX_COMPAT, USB_MUX_CONFIG_PS8818), \ (PS8XXX_USB_MUX_COMPAT, USB_MUX_CONFIG_TCPCI_TCPM), \ (TCPCI_TCPM_USB_MUX_COMPAT, USB_MUX_CONFIG_TCPCI_TCPM), \ -- cgit v1.2.1