From ade49b2f7f5c6536a8e22ba41c251cebbc91263f Mon Sep 17 00:00:00 2001 From: Diana Z Date: Fri, 31 Mar 2023 13:38:06 -0600 Subject: Zephyr test: Create NX20P383X test Create a test for the NX20P383X driver. This first test checks the APIs into the driver, but does not test the interrupt functionality. BRANCH=None BUG=b:276468569 TEST=./twister -T ./zephyr/test Change-Id: I9ffffda688c6d45b1448aac781bc4c070f06777b Signed-off-by: Diana Z Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4389618 Reviewed-by: Yuval Peress --- zephyr/test/drivers/CMakeLists.txt | 1 + zephyr/test/drivers/Kconfig | 5 ++ zephyr/test/drivers/nx20p348x/CMakeLists.txt | 5 ++ zephyr/test/drivers/nx20p348x/src/nx20p348x.c | 114 ++++++++++++++++++++++++++ zephyr/test/drivers/nx20p348x/usbc.dts | 25 ++++++ zephyr/test/drivers/testcase.yaml | 6 ++ 6 files changed, 156 insertions(+) create mode 100644 zephyr/test/drivers/nx20p348x/CMakeLists.txt create mode 100644 zephyr/test/drivers/nx20p348x/src/nx20p348x.c create mode 100644 zephyr/test/drivers/nx20p348x/usbc.dts diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt index 5153c7839c..2d7fc8a56b 100644 --- a/zephyr/test/drivers/CMakeLists.txt +++ b/zephyr/test/drivers/CMakeLists.txt @@ -33,6 +33,7 @@ add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_IT8XXX2_HW_SHA256 it8xxx2_hw_sha25 add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_KEYBOARD_SCAN keyboard_scan) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_LED_DRIVER led_driver) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_MKBP mkbp) +add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_NX20P348X nx20p348x) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_PANIC_OUTPUT panic_output) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_POWER_HOST_SLEEP power_host_sleep) add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_RT1718S rt1718s) diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig index 1bbb14aa5e..1cafa840df 100644 --- a/zephyr/test/drivers/Kconfig +++ b/zephyr/test/drivers/Kconfig @@ -97,6 +97,11 @@ config LINK_TEST_SUITE_LOCATE_CHIP_ALTS config LINK_TEST_SUITE_MKBP bool "Link and test the mkbp tests" +config LINK_TEST_SUITE_NX20P348X + bool "Link and test the nx20p348x tests" + select PLATFORM_EC_CONSOLE_CMD_PPC_DUMP + select PLATFORM_EC_USBC_PPC_NX20P3483 + config LINK_TEST_SUITE_PANIC_OUTPUT bool "Link and test the panic_output tests" diff --git a/zephyr/test/drivers/nx20p348x/CMakeLists.txt b/zephyr/test/drivers/nx20p348x/CMakeLists.txt new file mode 100644 index 0000000000..ac7c9cceb1 --- /dev/null +++ b/zephyr/test/drivers/nx20p348x/CMakeLists.txt @@ -0,0 +1,5 @@ +# 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. + +target_sources(app PRIVATE src/nx20p348x.c) diff --git a/zephyr/test/drivers/nx20p348x/src/nx20p348x.c b/zephyr/test/drivers/nx20p348x/src/nx20p348x.c new file mode 100644 index 0000000000..5f98768d06 --- /dev/null +++ b/zephyr/test/drivers/nx20p348x/src/nx20p348x.c @@ -0,0 +1,114 @@ +/* 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 "battery_smart.h" +#include "charger.h" +#include "console.h" +#include "driver/ppc/nx20p348x.h" +#include "driver/ppc/nx20p348x_public.h" +#include "emul/emul_nx20p348x.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "usb_pd_tcpm.h" + +#include +#include +#include + +#define NX20P383X_NODE DT_NODELABEL(nx20p348x_emul) + +#define TEST_PORT USBC_PORT_C0 + +struct nx20p348x_driver_fixture { + const struct emul *nx20p348x_emul; +}; + +static void *nx20p348x_driver_setup(void) +{ + static struct nx20p348x_driver_fixture fix; + + fix.nx20p348x_emul = EMUL_DT_GET(NX20P383X_NODE); + + return &fix; +} + +ZTEST_SUITE(nx20p348x_driver, drivers_predicate_post_main, + nx20p348x_driver_setup, NULL, NULL, NULL); + +struct curr_limit_pair { + enum tcpc_rp_value rp; + uint8_t reg; +}; + +/* Note: Register values are slightly higher to account for overshoot */ +static struct curr_limit_pair currents[] = { + { .rp = TYPEC_RP_3A0, .reg = NX20P348X_ILIM_3_200 }, + { .rp = TYPEC_RP_1A5, .reg = NX20P348X_ILIM_1_600 }, + { .rp = TYPEC_RP_USB, .reg = NX20P348X_ILIM_0_600 }, +}; + +ZTEST_F(nx20p348x_driver, test_source_curr_limits) +{ + for (int i = 0; i < ARRAY_SIZE(currents); i++) { + uint8_t read; + + ppc_set_vbus_source_current_limit(TEST_PORT, currents[i].rp); + read = nx20p348x_emul_peek(fixture->nx20p348x_emul, + NX20P348X_5V_SRC_OCP_THRESHOLD_REG); + zassert_equal( + (read & NX20P348X_ILIM_MASK), currents[i].reg, + "Failed to see correct threshold for Rp %d (reg: 0x%02x)", + currents[i].rp, read); + } +} + +ZTEST_F(nx20p348x_driver, test_discharge_vbus) +{ + uint8_t reg; + + zassert_ok(ppc_discharge_vbus(TEST_PORT, true)); + reg = nx20p348x_emul_peek(fixture->nx20p348x_emul, + NX20P348X_DEVICE_CONTROL_REG); + zassert_equal((reg & NX20P348X_CTRL_VBUSDIS_EN), + NX20P348X_CTRL_VBUSDIS_EN); + + zassert_ok(ppc_discharge_vbus(TEST_PORT, false)); + reg = nx20p348x_emul_peek(fixture->nx20p348x_emul, + NX20P348X_DEVICE_CONTROL_REG); + zassert_not_equal((reg & NX20P348X_CTRL_VBUSDIS_EN), + NX20P348X_CTRL_VBUSDIS_EN); +} + +ZTEST(nx20p348x_driver, test_sink_enable_timeout_failure) +{ + /* Note: PPC requires a TCPC GPIO to enable its sinking */ + zassert_equal(ppc_vbus_sink_enable(TEST_PORT, true), EC_ERROR_TIMEOUT); +} + +ZTEST(nx20p348x_driver, test_source_enable_timeout_failure) +{ + /* Note: PPC requires a TCPC GPIO to enable its sourcing */ + zassert_equal(ppc_vbus_source_enable(TEST_PORT, true), + EC_ERROR_TIMEOUT); +} + +ZTEST(nx20p348x_driver, test_ppc_dump) +{ + const struct shell *shell_zephyr = get_ec_shell(); + const char *outbuffer; + size_t buffer_size; + + shell_backend_dummy_clear_output(shell_zephyr); + + /* This chip supports PPC dump, so should return success */ + zassert_ok(shell_execute_cmd(shell_zephyr, "ppc_dump 0")); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + zassert_true(buffer_size > 0); + + /* Weakly verify something reasonable was output to console */ + zassert_not_null(strstr(outbuffer, "]: 0x")); +} diff --git a/zephyr/test/drivers/nx20p348x/usbc.dts b/zephyr/test/drivers/nx20p348x/usbc.dts new file mode 100644 index 0000000000..09650cea69 --- /dev/null +++ b/zephyr/test/drivers/nx20p348x/usbc.dts @@ -0,0 +1,25 @@ +/* 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 + +/ { + usbc { + port0@0 { + compatible = "named-usbc-port"; + ppc = <&nx20p348x_emul>; + }; + }; +}; + +&i2c2 { + status="okay"; + + nx20p348x_emul: nx20p348x_emul@9 { + compatible = "nxp,nx20p348x"; + status = "okay"; + reg = <0x71>; + }; +}; diff --git a/zephyr/test/drivers/testcase.yaml b/zephyr/test/drivers/testcase.yaml index 5da6cff9f0..773a25e361 100644 --- a/zephyr/test/drivers/testcase.yaml +++ b/zephyr/test/drivers/testcase.yaml @@ -257,6 +257,12 @@ tests: - CONFIG_PLATFORM_EC_MKBP_USE_GPIO=y - CONFIG_PLATFORM_EC_KEYBOARD_KEYPAD=y tags: common mkbp + drivers.nx20p348x: + extra_dtc_overlay_files: + - ./boards/native_posix.overlay + - ./nx20p348x/usbc.dts + extra_configs: + - CONFIG_LINK_TEST_SUITE_NX20P348X=y drivers.panic_output: extra_configs: - CONFIG_LINK_TEST_SUITE_PANIC_OUTPUT=y -- cgit v1.2.1