diff options
Diffstat (limited to 'zephyr/test/drivers/common')
-rw-r--r-- | zephyr/test/drivers/common/CMakeLists.txt | 13 | ||||
-rw-r--r-- | zephyr/test/drivers/common/include/test/drivers/charger_utils.h | 30 | ||||
-rw-r--r-- | zephyr/test/drivers/common/include/test/drivers/stubs.h | 34 | ||||
-rw-r--r-- | zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h | 253 | ||||
-rw-r--r-- | zephyr/test/drivers/common/include/test/drivers/test_mocks.h | 114 | ||||
-rw-r--r-- | zephyr/test/drivers/common/include/test/drivers/test_state.h | 17 | ||||
-rw-r--r-- | zephyr/test/drivers/common/include/test/drivers/utils.h | 623 | ||||
-rw-r--r-- | zephyr/test/drivers/common/src/main.c | 63 | ||||
-rw-r--r-- | zephyr/test/drivers/common/src/stubs.c | 367 | ||||
-rw-r--r-- | zephyr/test/drivers/common/src/test_mocks.c | 46 | ||||
-rw-r--r-- | zephyr/test/drivers/common/src/test_rules.c | 38 | ||||
-rw-r--r-- | zephyr/test/drivers/common/src/utils.c | 627 |
12 files changed, 2225 insertions, 0 deletions
diff --git a/zephyr/test/drivers/common/CMakeLists.txt b/zephyr/test/drivers/common/CMakeLists.txt new file mode 100644 index 0000000000..854294ab11 --- /dev/null +++ b/zephyr/test/drivers/common/CMakeLists.txt @@ -0,0 +1,13 @@ +# Common sources +target_sources(app PRIVATE + src/main.c + src/test_mocks.c + src/test_rules.c + src/utils.c + src/stubs.c +) +target_include_directories(app PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${PLATFORM_EC}/driver/ppc/ + ${PLATFORM_EC}/zephyr/shim/src/led_driver +) diff --git a/zephyr/test/drivers/common/include/test/drivers/charger_utils.h b/zephyr/test/drivers/common/include/test/drivers/charger_utils.h new file mode 100644 index 0000000000..22331c8575 --- /dev/null +++ b/zephyr/test/drivers/common/include/test/drivers/charger_utils.h @@ -0,0 +1,30 @@ +/* Copyright 2021 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef ZEPHYR_TEST_DRIVERS_INCLUDE_CHARGER_UTILS_H_ +#define ZEPHYR_TEST_DRIVERS_INCLUDE_CHARGER_UTILS_H_ + +#include "charger.h" + +/** + * @brief Get the index of the charger in chg_chips + * + * @param charger Pointer to the charger driver. + * @return The index of the charger if found + * @return board_get_charger_chip_count() if not found + */ +static inline uint8_t get_charger_num(const struct charger_drv *charger) +{ + const uint8_t chip_count = board_get_charger_chip_count(); + uint8_t chip; + + for (chip = 0; chip < chip_count; ++chip) { + if (chg_chips[chip].drv == charger) + return chip; + } + return chip; +} + +#endif /* ZEPHYR_TEST_DRIVERS_INCLUDE_CHARGER_UTILS_H_ */ diff --git a/zephyr/test/drivers/common/include/test/drivers/stubs.h b/zephyr/test/drivers/common/include/test/drivers/stubs.h new file mode 100644 index 0000000000..98f3fa1d15 --- /dev/null +++ b/zephyr/test/drivers/common/include/test/drivers/stubs.h @@ -0,0 +1,34 @@ +/* Copyright 2021 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __TEST_DRIVERS_STUBS_H +#define __TEST_DRIVERS_STUBS_H + +#include <zephyr/fff.h> +#include "power.h" + +enum usbc_port { USBC_PORT_C0 = 0, USBC_PORT_C1, USBC_PORT_COUNT }; + +/* Structure used by usb_mux test. It is part of usb_muxes chain. */ +extern struct usb_mux usbc1_virtual_usb_mux; +extern struct usb_mux usbc0_mux0; + +/** + * @brief Set product ID that should be returned by board_get_ps8xxx_product_id + * + * @param product_id ID of PS8xxx product which is emulated + */ +void board_set_ps8xxx_product_id(uint16_t product_id); + +/* Declare fake function to allow tests to examine calls to this function */ +DECLARE_FAKE_VOID_FUNC(system_hibernate, uint32_t, uint32_t); + +DECLARE_FAKE_VOID_FUNC(board_reset_pd_mcu); + +void sys_arch_reboot(int type); + +/* Declare GPIO_TEST interrupt handler */ +void gpio_test_interrupt(enum gpio_signal signal); +#endif /* __TEST_DRIVERS_STUBS_H */ diff --git a/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h new file mode 100644 index 0000000000..08d75cccf7 --- /dev/null +++ b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h @@ -0,0 +1,253 @@ +/* Copyright 2021 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __TCPCI_TEST_COMMON_H +#define __TCPCI_TEST_COMMON_H + +#include "stubs.h" + +/** + * @brief Check TCPC register value using zassert API + * + * @param emul Pointer to TCPCI emulator + * @param reg TCPC register address to check + * @param exp_val Expected value of register + * @param line Line number to print in case of failure + */ +void check_tcpci_reg_f(const struct emul *emul, int reg, uint16_t exp_val, + int line); +#define check_tcpci_reg(emul, reg, exp_val) \ + check_tcpci_reg_f((emul), (reg), (exp_val), __LINE__) + +/** + * @brief Check TCPC register value with mask using zassert API + * + * @param emul Pointer to TCPCI emulator + * @param reg TCPC register address to check + * @param exp_val Expected value of register + * @param mask Bits that are checked + * @param line Line number to print in case of failure + */ +void check_tcpci_reg_with_mask_f(const struct emul *emul, int reg, + uint16_t exp_val, uint16_t mask, int line); +#define check_tcpci_reg_with_mask(emul, reg, exp_val, mask) \ + check_tcpci_reg_with_mask_f((emul), (reg), (exp_val), (mask), __LINE__) + +/** + * @brief Test TCPCI init and vbus level callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_init(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI release callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_release(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI get cc callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_get_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI set cc callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_set_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI set polarity callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_set_polarity(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI set vconn callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_set_vconn(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI set msg header callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_set_msg_header(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI rx and sop prime enable callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_set_rx_detect(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI get raw message from TCPC callback + * + * @param emul Pointer to TCPCI emulator + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_get_rx_message_raw(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI transmitting message from TCPC callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_transmit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI alert callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_alert(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI alert RX message callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_alert_rx_message(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI auto discharge on disconnect callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_auto_discharge(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI drp toggle callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_drp_toggle(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI get chip info callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_get_chip_info(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI enter low power mode callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_low_power_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI set bist test mode callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to argument emul's i2c_common_emul_data + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_set_bist_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +/** + * @brief Test TCPCI hard reset re-init callback + * + * @param emul Pointer to TCPCI emulator + * @param common_data Pointer to emulated I2C bus + * @param port Select USBC port that will be used to obtain tcpm_drv from + * tcpc_config + */ +void test_tcpci_hard_reset_reinit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); + +#endif /* __TCPCI_TEST_COMMON_H */ diff --git a/zephyr/test/drivers/common/include/test/drivers/test_mocks.h b/zephyr/test/drivers/common/include/test/drivers/test_mocks.h new file mode 100644 index 0000000000..8e481edef8 --- /dev/null +++ b/zephyr/test/drivers/common/include/test/drivers/test_mocks.h @@ -0,0 +1,114 @@ +/* Copyright 2021 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/fff.h> + +/* + * Convenience macros + */ + +/** + * @brief Helper macro for inspecting the argument history of a given + * fake. Counts number of times the fake was called with a given + * argument. + * @param FAKE - FFF-provided fake structure (no pointers). + * @param ARG_NUM - Zero-based index of the argument to compare. + * @param VAL - Expression the argument must equal. + * @return Returns the number of times a call was made to the fake + * where the argument `ARG_NUM` equals `VAL`. + */ +#define MOCK_COUNT_CALLS_WITH_ARG_VALUE(FAKE, ARG_NUM, VAL) \ + ({ \ + int count = 0; \ + for (int i = 0; i < (FAKE).call_count; i++) { \ + if ((FAKE).arg##ARG_NUM##_history[i] == (VAL)) { \ + count++; \ + } \ + } \ + count; \ + }) + +/** + * @brief Helper macro for asserting that a certain register write occurred. + * Used when wrapping an I2C emulator mock write function in FFF. Prints + * useful error messages when the assertion fails. + * @param FAKE - name of the fake whose arg history to insepct. Do not include + * '_fake' at the end. + * @param CALL_NUM - Index in to the call history that this write should have + * occurred at. Zero based. + * @param EXPECTED_REG - The register address that was supposed to be written. + * @param EXPECTED_VAL - The 8-bit value that was supposed to be written, or + * `MOCK_IGNORE_VALUE` to suppress this check. + */ +#define MOCK_ASSERT_I2C_WRITE(FAKE, CALL_NUM, EXPECTED_REG, EXPECTED_VAL) \ + do { \ + zassert_true((CALL_NUM) < FAKE##_fake.call_count, \ + "Call #%d did not occur (%d I2C writes total)", \ + (CALL_NUM), FAKE##_fake.call_count); \ + zassert_equal( \ + FAKE##_fake.arg1_history[(CALL_NUM)], (EXPECTED_REG), \ + "Expected I2C write #%d to register 0x%02x (" #EXPECTED_REG \ + ") but wrote to reg 0x%02x", \ + (CALL_NUM), (EXPECTED_REG), \ + FAKE##_fake.arg1_history[(CALL_NUM)]); \ + if ((EXPECTED_VAL) != MOCK_IGNORE_VALUE) { \ + zassert_equal( \ + FAKE##_fake.arg2_history[(CALL_NUM)], \ + (EXPECTED_VAL), \ + "Expected I2C write #%d to register 0x%02x (" #EXPECTED_REG \ + ") to write 0x%02x (" #EXPECTED_VAL \ + ") but wrote 0x%02x", \ + (CALL_NUM), (EXPECTED_REG), (EXPECTED_VAL), \ + FAKE##_fake.arg2_history[(CALL_NUM)]); \ + } \ + } while (0) + +/** @brief Value to pass to MOCK_ASSERT_I2C_WRITE to ignore the actual value + * written. + */ +#define MOCK_IGNORE_VALUE (-1) + +/** + * @brief Helper macro for asserting that a certain register read occurred. + * Used when wrapping an I2C emulator mock read function in FFF. Prints + * useful error messages when the assertion fails. + * @param FAKE - name of the fake whose arg history to insepct. Do not include + * '_fake' at the end. + * @param CALL_NUM - Index in to the call history that this write should have + * occurred at. Zero based. + * @param EXPECTED_REG - The register address that was supposed to be read + * from. + */ +#define MOCK_ASSERT_I2C_READ(FAKE, CALL_NUM, EXPECTED_REG) \ + do { \ + zassert_true((CALL_NUM) < FAKE##_fake.call_count, \ + "Call #%d did not occur (%d I2C reads total)", \ + (CALL_NUM), FAKE##_fake.call_count); \ + zassert_equal( \ + FAKE##_fake.arg1_history[(CALL_NUM)], (EXPECTED_REG), \ + "Expected I2C read #%d from register 0x%02x (" #EXPECTED_REG \ + ") but read from reg 0x%02x", \ + (CALL_NUM), (EXPECTED_REG), \ + FAKE##_fake.arg1_history[(CALL_NUM)]); \ + } while (0) + +/* + * Mock declarations + */ + +/* Mocks for common/init_rom.c */ +DECLARE_FAKE_VALUE_FUNC(const void *, init_rom_map, const void *, int); +DECLARE_FAKE_VOID_FUNC(init_rom_unmap, const void *, int); +DECLARE_FAKE_VALUE_FUNC(int, init_rom_copy, int, int, int); + +/* Mocks for common/system.c */ +DECLARE_FAKE_VALUE_FUNC(int, system_jumped_late); +DECLARE_FAKE_VALUE_FUNC(int, system_is_locked); +DECLARE_FAKE_VOID_FUNC(system_reset, int); +DECLARE_FAKE_VOID_FUNC(software_panic, uint32_t, uint32_t); +DECLARE_FAKE_VOID_FUNC(assert_post_action, const char *, unsigned int); + +/* Mocks for common/lid_angle.c */ +DECLARE_FAKE_VOID_FUNC(lid_angle_peripheral_enable, int); diff --git a/zephyr/test/drivers/common/include/test/drivers/test_state.h b/zephyr/test/drivers/common/include/test/drivers/test_state.h new file mode 100644 index 0000000000..bea56224fc --- /dev/null +++ b/zephyr/test/drivers/common/include/test/drivers/test_state.h @@ -0,0 +1,17 @@ +/* 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. + */ + +#ifndef ZEPHYR_TEST_DRIVERS_INCLUDE_TEST_STATE_H_ +#define ZEPHYR_TEST_DRIVERS_INCLUDE_TEST_STATE_H_ + +struct test_state { + bool ec_app_main_run; +}; + +bool drivers_predicate_pre_main(const void *state); + +bool drivers_predicate_post_main(const void *state); + +#endif /* ZEPHYR_TEST_DRIVERS_INCLUDE_TEST_STATE_H_ */ diff --git a/zephyr/test/drivers/common/include/test/drivers/utils.h b/zephyr/test/drivers/common/include/test/drivers/utils.h new file mode 100644 index 0000000000..306f2894d4 --- /dev/null +++ b/zephyr/test/drivers/common/include/test/drivers/utils.h @@ -0,0 +1,623 @@ +/* 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. + */ + +#ifndef ZEPHYR_TEST_DRIVERS_INCLUDE_UTILS_H_ +#define ZEPHYR_TEST_DRIVERS_INCLUDE_UTILS_H_ + +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> +#include <stddef.h> +#include <string.h> + +#include "charger.h" +#include "lpc.h" +#include "emul/tcpc/emul_tcpci_partner_src.h" +#include "extpower.h" +#include "host_command.h" +#include "power.h" +#include "usbc/utils.h" + +/** + * @brief Helper macro for EMUL_GET_USBC_BINDING. If @p usbc_id has the same + * port number as @p port, then struct emul* for @p chip phandle is + * returned. + * + * @param usbc_id Named usbc port ID + * @param port Port number to match with named usbc port + * @param chip Name of chip phandle property + */ +#define EMUL_GET_USBC_BINDING_IF_PORT_MATCH(usbc_id, port, chip) \ + COND_CODE_1(IS_EQ(USBC_PORT_NEW(usbc_id), port), \ + (EMUL_DT_GET(DT_PHANDLE(usbc_id, chip))), ()) + +/** + * @brief Get struct emul from phandle @p chip property of USBC @p port + * + * @param port Named usbc port number. The value has to be integer literal. + * @param chip Name of chip property that is phandle to required emulator. + */ +#define EMUL_GET_USBC_BINDING(port, chip) \ + DT_FOREACH_STATUS_OKAY_VARGS(named_usbc_port, \ + EMUL_GET_USBC_BINDING_IF_PORT_MATCH, \ + port, chip) + +/** @brief Set emulated battery level. Call all necessary hooks. */ +void test_set_battery_level(int percentage); + +/** @brief Set chipset to S0 state. Call all necessary hooks. */ +void test_set_chipset_to_s0(void); + +/** + * @brief Set the chipset to any stable state. Call all necessary hooks. + * + * Supported states are: + * <ul> + * <li>POWER_G3 (same as calling test_set_chipset_to_g3())</li> + * <li>POWER_S5</li> + * <li>POWER_S4</li> + * <li>POWER_S3</li> + * <li>POWER_S0 (same as calling test_set_chipset_to_s0()</li> + * <li>POWER_S0ix (if either CONFIG_PLATFORM_EC_POWERSEQ_S0IX or + * CONFIG_AP_PWRSEQ_S0IX are enabled)</li> + * </ul> + * + * @param new_state The new state. Must be a steady state (see above). + */ +void test_set_chipset_to_power_level(enum power_state new_state); + +/** @brief Set chipset to G3 state. Call all necessary hooks. */ +void test_set_chipset_to_g3(void); + +/* + * TODO(b/217755888): Implement ztest assume API upstream + */ + +/** + * @brief Assume that this function call won't be reached + * @param msg Optional message to print if the assumption fails + */ +#define zassume_unreachable(msg, ...) zassert_unreachable(msg, ##__VA_ARGS__) + +/** + * Run an ACPI read to the specified address. + * + * This function assumes a successful ACPI read process and will make a + * call to the zassume_* API. A failure here will skip the calling test. + * + * @param acpi_addr Address to query + * @return Byte read + */ +uint8_t acpi_read(uint8_t acpi_addr); + +/** + * Run an ACPI write to the specified address. + * + * This function assumes a successful ACPI write process and will make a + * call to the zassume_* API. A failure here will skip the calling test. + * + * @param acpi_addr Address to write + * @param write_byte Byte to write to address + */ +void acpi_write(uint8_t acpi_addr, uint8_t write_byte); + +/** + * Run the host command to get the charge state for a given charger number. + * + * This function assumes a successful host command processing and will make a + * call to the zassume_* API. A failure here will abort the calling test. + * + * @param chgnum The charger number to query. + * @return The result of the query. + */ +static inline struct ec_response_charge_state host_cmd_charge_state(int chgnum) +{ + struct ec_params_charge_state params = { + .chgnum = chgnum, + .cmd = CHARGE_STATE_CMD_GET_STATE, + }; + struct ec_response_charge_state response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_CHARGE_STATE, 0, response, params); + + zassume_ok(host_command_process(&args), + "Failed to get charge state for chgnum %d", chgnum); + return response; +} + +/** + * Run the host command to get the USB PD power info for a given port. + * + * This function assumes a successful host command processing and will make a + * call to the zassume_* API. A failure here will abort the calling test. + * + * @param port The USB port to get info from. + * @return The result of the query. + */ +static inline struct ec_response_usb_pd_power_info host_cmd_power_info(int port) +{ + struct ec_params_usb_pd_power_info params = { .port = port }; + struct ec_response_usb_pd_power_info response; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_USB_PD_POWER_INFO, 0, response, params); + + zassume_ok(host_command_process(&args), + "Failed to get power info for port %d", port); + return response; +} + +/** + * Run the host command to get the Type-C status information for a given port. + * + * This function assumes a successful host command processing and will make a + * call to the zassume_* API. A failure here will abort the calling test. + * + * @param port The USB port to get info from. + * @return The result of the query. + */ +static inline struct ec_response_typec_status host_cmd_typec_status(int port) +{ + struct ec_params_typec_status params = { .port = port }; + struct ec_response_typec_status response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_TYPEC_STATUS, 0, response, params); + + zassume_ok(host_command_process(&args), + "Failed to get Type-C state for port %d", port); + return response; +} + +static inline struct ec_response_usb_pd_control +host_cmd_usb_pd_control(int port, enum usb_pd_control_swap swap) +{ + struct ec_params_usb_pd_control params = { .port = port, .swap = swap }; + struct ec_response_usb_pd_control response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_USB_PD_CONTROL, 0, response, params); + + zassume_ok(host_command_process(&args), + "Failed to process usb_pd_control_swap for port %d, swap %d", + port, swap); + return response; +} + +/** + * Run the host command to suspend/resume PD ports + * + * This function assumes a successful host command processing and will make a + * call to the zassume_* API. A failure here will skip the calling test. + * + * @param port The USB port to operate on + * @param cmd The sub-command to run + */ +static inline void host_cmd_pd_control(int port, enum ec_pd_control_cmd cmd) +{ + struct ec_params_pd_control params = { .chip = port, .subcmd = cmd }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_PD_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to process pd_control for port %d, cmd %d", port, + cmd); +} + +/** + * Run the host command to control or query the charge state + * + * @return The result of the query. + */ +static inline struct ec_response_charge_control +host_cmd_charge_control(enum ec_charge_control_mode mode, + enum ec_charge_control_cmd cmd) +{ + struct ec_params_charge_control params = { .cmd = cmd, + .mode = mode, + .sustain_soc = { + .lower = -1, + .upper = -1, + } }; + struct ec_response_charge_control response; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_CHARGE_CONTROL, 2, response, params); + + zassume_ok(host_command_process(&args), + "Failed to get charge control values"); + + return response; +} + +/** + * @brief Call the host command HOST_EVENT with the user supplied action. + * + * @param action - HOST_EVENT action parameter. + * @param mask_type - Event mask type to apply to the HOST_EVENT action. + * @param r - Pointer to the response object to fill. + */ +enum ec_status host_cmd_host_event(enum ec_host_event_action action, + enum ec_host_event_mask_type mask_type, + struct ec_response_host_event *r); + +/** + * @brief Call the host command MOTION_SENSE with the dump sub-command + * + * Note: this function uses the zassume_ API. It will skip the test if the host + * command fails. + * + * @param max_sensor_count The maximum number of sensor data objects to populate + * in the response object. + * @param response Pointer to the response object to fill. + */ +void host_cmd_motion_sense_dump(int max_sensor_count, + struct ec_response_motion_sense *response); + +/** + * @brief Call the host command MOTION_SENSE with the data sub-command + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_data(uint8_t sensor_num, + struct ec_response_motion_sense *response); + +/** + * @brief Call the host command MOTION_SENSE with the info sub-command + * + * @param cmd_version The command version + * @param sensor_num The sensor index in the motion_sensors array to query + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_info(uint8_t cmd_version, uint8_t sensor_num, + struct ec_response_motion_sense *response); + +/** + * @brief Call the host command MOTION_SENSE with the ec_rate sub-command + * + * This function performs a read of the current rate by passing + * EC_MOTION_SENSE_NO_VALUE as the data rate. Otherwise, the data rate should be + * updated. + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param data_rate_ms The new data rate or EC_MOTION_SENSE_NO_VALUE to read + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_ec_rate(uint8_t sensor_num, int data_rate_ms, + struct ec_response_motion_sense *response); + +/** + * @brief Call the host command MOTION_SENSE with the odr sub-command + * + * This function performs a read of the current odr by passing + * EC_MOTION_SENSE_NO_VALUE as the data rate. Otherwise, the data rate should be + * updated. + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param odr The new ODR to set + * @param round_up Whether or not to round up the ODR + * @param response Pointer to the response data structure to fill on success + * @return The result code form the host command + */ +int host_cmd_motion_sense_odr(uint8_t sensor_num, int32_t odr, bool round_up, + struct ec_response_motion_sense *response); + +/** + * @brief Call the host command MOTION_SENSE with the sensor range sub-command + * + * This function attempts to set the sensor range and returns the range value. + * If the range value is EC_MOTION_SENSE_NO_VALUE, then the host command will + * not attempt to update the range. + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param range The new range to set + * @param round_up Whether or not to round up the range. + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_range(uint8_t sensor_num, int32_t range, + bool round_up, + struct ec_response_motion_sense *response); + +/** + * @brief Call the host command MOTION_SENSE with the sensor offset sub-command + * + * This function attempts to set the offset if the flags field includes + * MOTION_SENSE_SET_OFFSET. Otherwise, the temperature and offsets are ignored. + * The response field will include the current (after modification) offsets and + * temperature. + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param flags The flags to pass to the host command + * @param temperature The temperature at which the offsets were attained (set) + * @param offset_x The X offset to set + * @param offset_y The Y offset to set + * @param offset_z The Z offset to set + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_offset(uint8_t sensor_num, uint16_t flags, + int16_t temperature, int16_t offset_x, + int16_t offset_y, int16_t offset_z, + struct ec_response_motion_sense *response); + +/** + * @brief Call the host command MOTION_SENSE with the sensor scale sub-command + * + * This function attempts to set the scale if the flags field includes + * MOTION_SENSE_SET_OFFSET. Otherwise, the temperature and scales are ignored. + * The response field will include the current (after modification) scales and + * temperature. + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param flags The flags to pass to the host command + * @param temperature The temperature at which the scales were attained (set) + * @param scale_x The X scale to set + * @param scale_y The Y scale to set + * @param scale_z The Z scale to set + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_scale(uint8_t sensor_num, uint16_t flags, + int16_t temperature, int16_t scale_x, + int16_t scale_y, int16_t scale_z, + struct ec_response_motion_sense *response); + +/** + * @brief Enable/disable sensor calibration via host command + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param enable Whether to enable or disable the calibration + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_calib(uint8_t sensor_num, bool enable, + struct ec_response_motion_sense *response); + +/** + * @brief Set the sensor's fifo flush bit + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_fifo_flush(uint8_t sensor_num, + struct ec_response_motion_sense *response); + +/** + * @brief Get the current fifo info + * + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_fifo_info(struct ec_response_motion_sense *response); + +/** + * @brief Get the current fifo data + * + * @param buffer_length The number of entries available on the response pointer + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_fifo_read(uint8_t buffer_length, + struct ec_response_motion_sense *response); + +/** + * @brief Call the int_enable motionsense host command + * + * @param enable 0 for disable, 1 for enable. All others are invalid + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_int_enable(int8_t enable, + struct ec_response_motion_sense *response); + +/** + * @brief Call the spoof motion_sense subcommand + * + * @param sensor_num The sensor index in motion_sensors + * @param enable The enable field, for normal operations this will be one of + * enum motionsense_spoof_mode + * @param values0 The X value to set if using custom mode + * @param values1 The Y value to set if using custom mode + * @param values2 The Z value to set if using custom mode + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_spoof(uint8_t sensor_num, uint8_t enable, + int16_t values0, int16_t values1, + int16_t values2, + struct ec_response_motion_sense *response); + +/** + * Run the host command to get the PD discovery responses. + * + * @param port The USB-C port number + * @param partner_type SOP, SOP', or SOP'' + * @param response Destination buffer for command response; + * should hold struct ec_response_typec_discovery and + * enough struct svid_mode_info for expected response. + * @param response_size Number of bytes in response + */ +void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type, + void *response, size_t response_size); +/** + * @brief Run the host command to get the PD alternative mode response. + * + * @param port The USB-C port number + * @param response Destination for command response. + * @param response_size Destination of response size from request params. + */ +void host_cmd_usb_pd_get_amode( + uint8_t port, uint16_t svid_idx, + struct ec_params_usb_pd_get_mode_response *response, + int *response_size); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_ENTER_MODE + * + * @param port The USB-C port number + * @param mode Mode to enter + */ +void host_cmd_typec_control_enter_mode(int port, enum typec_mode mode); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_EXIT_MODES + * + * @param port The USB-C port number + */ +void host_cmd_typec_control_exit_modes(int port); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_USB_MUX_SET + * + * @param port The USB-C port number + * @param mux_set Mode and mux index to set + */ +void host_cmd_typec_control_usb_mux_set(int port, + struct typec_usb_mux_set mux_set); + +/** + * Run the host command to control PD port behavior, with the sub-command of + * TYPEC_CONTROL_COMMAND_CLEAR_EVENTS + * + * @param port The USB-C port number + * @param events Events to clear for the port (see PD_STATUS_EVENT_* + * definitions for options) + */ +void host_cmd_typec_control_clear_events(int port, uint32_t events); + +struct host_events_ctx { + host_event_t lpc_host_events; + host_event_t lpc_host_event_mask[LPC_HOST_EVENT_COUNT]; +}; + +/** + * Save all host events. This should be run as part of the "before" action for + * any test suite that manipulates the host events. + * + * @param host_events_ctx Caller allocated storage to save the host + * events. + */ +void host_events_save(struct host_events_ctx *host_events_ctx); + +/** + * Restore all host events. This should be run as part of the "after" action for + * any test suite that manipulates the host events. + * + * @param host_events_ctx Saved host events context information. + */ +void host_events_restore(struct host_events_ctx *host_events_ctx); + +#define GPIO_ACOK_OD_NODE DT_NODELABEL(gpio_acok_od) +#define GPIO_ACOK_OD_PIN DT_GPIO_PIN(GPIO_ACOK_OD_NODE, gpios) + +/** + * Set whether or not AC is enabled. + * + * If enabled, the device _should_ begin charging. + * + * This function assumes a successful gpio emulator call and will make a call + * to the zassume_* API. A failure here will abort the calling test. + * + * This function sleeps to wait for the GPIO interrupt to take place. + * + * @param enabled Whether or not to enable AC. + */ +static inline void set_ac_enabled(bool enabled) +{ + const struct device *acok_dev = + DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_ACOK_OD_NODE, gpios)); + + zassume_ok(gpio_emul_input_set(acok_dev, GPIO_ACOK_OD_PIN, enabled), + NULL); + k_sleep(K_MSEC(CONFIG_EXTPOWER_DEBOUNCE_MS + 1)); + zassume_equal(enabled, extpower_is_present(), NULL); +} + +/** + * @brief Connect a power source to a given port. + * + * Note: this is function currently only supports an ISL923X charger chip. + * + * @param partner Pointer to the emulated TCPCI partner device + * @param src Pointer to the emulated source extension + * @param pdo_index The index of the PDO object within the src to use + * @param tcpci_emul The TCPCI emulator that the source will connect to + * @param charger_emul The charger chip emulator + */ +void connect_source_to_port(struct tcpci_partner_data *partner, + struct tcpci_src_emul_data *src, int pdo_index, + const struct emul *tcpci_emul, + const struct emul *charger_emul); + +/** + * @brief Disconnect a power source from a given port. + * + * Note: this is function currently only supports an ISL923X charger chip. + * + * @param tcpci_emul The TCPCI emulator that will be disconnected + * @param charger_emul The charger chip emulator + */ +void disconnect_source_from_port(const struct emul *tcpci_emul, + const struct emul *charger_emul); + +/** + * @brief Connect a power sink to a given port. + * + * Note: this is function currently only supports an ISL923X charger chip. + * + * @param partner Pointer to the emulated TCPCI partner device + * @param tcpci_emul The TCPCI emulator that the source will connect to + * @param charger_emul The charger chip emulator + */ +void connect_sink_to_port(struct tcpci_partner_data *partner, + const struct emul *tcpci_emul, + const struct emul *charger_emul); + +/** + * @brief Disconnect a power sink from a given port. + * + * @param tcpci_emul The TCPCI emulator that will be disconnected + */ +void disconnect_sink_from_port(const struct emul *tcpci_emul); + +/** + * @brief Allocate memory for a test pourpose + * + * @param bytes Number of bytes to allocate + * + * @return Pointer to valid memory or NULL + */ +void *test_malloc(size_t bytes); + +/** + * @brief Free memory allocated by @ref test_malloc + * + * @param mem Pointer to the memory + */ +void test_free(void *mem); + +/** + * @brief Force the chipset to state G3 and then transition to S3 and finally + * S5. + * + */ +void test_set_chipset_to_g3_then_transition_to_s5(void); + +/** + * @brief Checks console command with expected console output and expected + * return value + * + */ +#define CHECK_CONSOLE_CMD(cmd, expected_output, expected_rv) \ + check_console_cmd((cmd), (expected_output), (expected_rv), __FILE__, \ + __LINE__) +void check_console_cmd(const char *cmd, const char *expected_output, + const int expected_rv, const char *file, const int line); +#endif /* ZEPHYR_TEST_DRIVERS_INCLUDE_UTILS_H_ */ diff --git a/zephyr/test/drivers/common/src/main.c b/zephyr/test/drivers/common/src/main.c new file mode 100644 index 0000000000..1c8497ab3f --- /dev/null +++ b/zephyr/test/drivers/common/src/main.c @@ -0,0 +1,63 @@ +/* Copyright 2021 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/kernel.h> +#include <zephyr/ztest.h> +#include "ec_app_main.h" +#include "hooks.h" +#include "test/drivers/test_state.h" + +/** + * @brief Semaphore that signals when hooks have completed + */ +static struct k_sem init_hooks_completed; + +/** + * @brief Hook callback function. Gets registered with the lowest priority so + * that we know all actual hooks have finished. Increments the semaphore. + */ +static void hook_completed_callback(void) +{ + /* Signal that hooks are completed */ + k_sem_give(&init_hooks_completed); +} +DECLARE_HOOK(HOOK_CHIPSET_STARTUP, hook_completed_callback, HOOK_PRIO_LAST); + +bool drivers_predicate_pre_main(const void *state) +{ + return ((struct test_state *)state)->ec_app_main_run == false; +} + +bool drivers_predicate_post_main(const void *state) +{ + return !drivers_predicate_pre_main(state); +} + +void test_main(void) +{ + k_sem_init(&init_hooks_completed, 0, 1); + + struct test_state state = { + .ec_app_main_run = false, + }; + + /* Run all the suites that depend on main not being called yet */ + ztest_run_all(&state); + + ec_app_main(); + state.ec_app_main_run = true; + +/* Delay the post-main tests until hooks finish. Allow a generous + * timeout before failing. Tests with mocked power states interfere + * with this mechanism, so proceed normally in this case. + */ +#if !IS_ENABLED(CONFIG_POWER_SEQUENCE_MOCK) + zassert_ok(k_sem_take(&init_hooks_completed, K_SECONDS(10)), + "Timed out waiting for hooks to finish"); +#endif /* !IS_ENABLED(CONFIG_POWER_SEQUENCE_MOCK) */ + + /* Run all the suites that depend on main being called */ + ztest_run_all(&state); +} diff --git a/zephyr/test/drivers/common/src/stubs.c b/zephyr/test/drivers/common/src/stubs.c new file mode 100644 index 0000000000..2683b326a8 --- /dev/null +++ b/zephyr/test/drivers/common/src/stubs.c @@ -0,0 +1,367 @@ +/* Copyright 2021 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.h" +#include "battery_fuel_gauge.h" +#include "bc12/pi3usb9201_public.h" +#include "charge_ramp.h" +#include "charger.h" +#include "charger/isl923x_public.h" +#include "charger/isl9241_public.h" +#include "config.h" +#include <zephyr/fff.h> +#include "gpio/gpio_int.h" +#include "hooks.h" +#include "i2c/i2c.h" +#include "power.h" +#include "ppc/sn5s330_public.h" +#include "ppc/syv682x_public.h" +#include "retimer/bb_retimer_public.h" +#include "test/drivers/stubs.h" +#include "tcpm/ps8xxx_public.h" +#include "tcpm/tcpci.h" +#include "usb_mux.h" +#include "usb_pd_tcpm.h" +#include "usbc_ppc.h" +#include "charge_state_v2.h" + +#include <zephyr/logging/log.h> +LOG_MODULE_REGISTER(stubs); + +#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ##args) +#define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ##args) + +/* All of these definitions are just to get the test to link. None of these + * functions are useful or behave as they should. Please remove them once the + * real code is able to be added. Most of the things here should either be + * in emulators or in the native_posix board-specific code or part of the + * device tree. + */ + +/* BC1.2 charger detect configuration */ +const struct pi3usb9201_config_t pi3usb9201_bc12_chips[] = { + [USBC_PORT_C0] = { + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = PI3USB9201_I2C_ADDR_3_FLAGS, + }, + [USBC_PORT_C1] = { + .i2c_port = I2C_PORT_USB_C1, + .i2c_addr_flags = PI3USB9201_I2C_ADDR_1_FLAGS, + }, +}; +BUILD_ASSERT(ARRAY_SIZE(pi3usb9201_bc12_chips) == USBC_PORT_COUNT); + +int board_set_active_charge_port(int port) +{ + int is_real_port = (port >= 0 && port < CONFIG_USB_PD_PORT_MAX_COUNT); + int i; + + if (!is_real_port && port != CHARGE_PORT_NONE) + return EC_ERROR_INVAL; + + if (port == CHARGE_PORT_NONE) { + CPRINTS("Disabling all charging port"); + + /* Disable all ports. */ + for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + /* + * Do not return early if one fails otherwise we can + * get into a boot loop assertion failure. + */ + if (board_vbus_sink_enable(i, 0)) + CPRINTS("Disabling p%d sink path failed.", i); + } + + return EC_SUCCESS; + } + + /* Check if the port is sourcing VBUS. */ + if (board_is_sourcing_vbus(port)) { + CPRINTS("Skip enable p%d", port); + return EC_ERROR_INVAL; + } + + CPRINTS("New charge port: p%d", port); + + /* + * Turn off the other ports' sink path FETs, before enabling the + * requested charge port. + */ + for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + if (i == port) + continue; + + if (board_vbus_sink_enable(i, 0)) + CPRINTS("p%d: sink path disable failed.", i); + } + + /* Enable requested charge port. */ + if (board_vbus_sink_enable(port, 1)) { + CPRINTS("p%d: sink path enable failed.", port); + return EC_ERROR_UNKNOWN; + } + + return EC_SUCCESS; +} + +int board_is_vbus_too_low(int port, enum chg_ramp_vbus_state ramp_state) +{ + return 0; +} + +void board_set_charge_limit(int port, int supplier, int charge_ma, int max_ma, + int charge_mv) +{ + charge_set_input_current_limit( + MAX(charge_ma, CONFIG_CHARGER_INPUT_CURRENT), charge_mv); +} + +BUILD_ASSERT(CONFIG_USB_PD_PORT_MAX_COUNT == USBC_PORT_COUNT); + +static uint16_t ps8xxx_product_id = PS8805_PRODUCT_ID; + +uint16_t board_get_ps8xxx_product_id(int port) +{ + if (port != USBC_PORT_C1) { + return 0; + } + + return ps8xxx_product_id; +} + +void board_set_ps8xxx_product_id(uint16_t product_id) +{ + ps8xxx_product_id = product_id; +} + +int board_vbus_sink_enable(int port, int enable) +{ + /* Both ports are controlled by PPC SN5S330 */ + return ppc_vbus_sink_enable(port, enable); +} + +int board_is_sourcing_vbus(int port) +{ + /* Both ports are controlled by PPC SN5S330 */ + return ppc_is_sourcing_vbus(port); +} + +/* TODO(b/239457738): Move to dts */ +struct usb_mux_chain usbc0_virtual_usb_mux_chain = { + .mux = + &(const struct usb_mux){ + .usb_port = USBC_PORT_C0, + .driver = &virtual_usb_mux_driver, + .hpd_update = &virtual_hpd_update, + }, +}; + +struct usb_mux usbc1_virtual_usb_mux = { + .usb_port = USBC_PORT_C1, + .driver = &virtual_usb_mux_driver, + .hpd_update = &virtual_hpd_update, +}; + +struct usb_mux_chain usbc1_virtual_usb_mux_chain = { + .mux = &usbc1_virtual_usb_mux, +}; + +struct usb_mux usbc0_mux0 = { + .usb_port = USBC_PORT_C0, + .driver = &tcpci_tcpm_usb_mux_driver, + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)), +}; + +struct usb_mux_chain usb_muxes[] = { + [USBC_PORT_C0] = { + .mux = &usbc0_mux0, + .next = &usbc0_virtual_usb_mux_chain, + }, + [USBC_PORT_C1] = { + .mux = &(const struct usb_mux){ + .usb_port = USBC_PORT_C1, + .driver = &bb_usb_retimer, + .hpd_update = bb_retimer_hpd_update, + .i2c_port = I2C_PORT_USB_C1, + .i2c_addr_flags = DT_REG_ADDR(DT_NODELABEL( + usb_c1_bb_retimer_emul)), + }, + .next = &usbc1_virtual_usb_mux_chain, + }, +}; +BUILD_ASSERT(ARRAY_SIZE(usb_muxes) == USBC_PORT_COUNT); + +struct bb_usb_control bb_controls[] = { + [USBC_PORT_C0] = { + /* USB-C port 0 doesn't have a retimer */ + }, + [USBC_PORT_C1] = { + .usb_ls_en_gpio = GPIO_SIGNAL(DT_NODELABEL(usb_c1_ls_en)), + .retimer_rst_gpio = + GPIO_SIGNAL(DT_NODELABEL(usb_c1_rt_rst_odl)), + }, +}; +BUILD_ASSERT(ARRAY_SIZE(bb_controls) == USBC_PORT_COUNT); + +void pd_power_supply_reset(int port) +{ +} + +int pd_check_vconn_swap(int port) +{ + return 0; +} + +int pd_set_power_supply_ready(int port) +{ + return EC_SUCCESS; +} + +/* USBC PPC configuration */ +struct ppc_config_t ppc_chips[] = { + [USBC_PORT_C0] = { + .i2c_port = I2C_PORT_USB_C0, + .i2c_addr_flags = SN5S330_ADDR0_FLAGS, + .drv = &sn5s330_drv, + }, + [USBC_PORT_C1] = { + .i2c_port = I2C_PORT_USB_C1, + .i2c_addr_flags = SYV682X_ADDR1_FLAGS, + .frs_en = GPIO_SIGNAL(DT_NODELABEL(gpio_usb_c1_frs_en)), + .drv = &syv682x_drv, + }, +}; +BUILD_ASSERT(ARRAY_SIZE(ppc_chips) == USBC_PORT_COUNT); +unsigned int ppc_cnt = ARRAY_SIZE(ppc_chips); + +DEFINE_FAKE_VOID_FUNC(system_hibernate, uint32_t, uint32_t); + +DEFINE_FAKE_VOID_FUNC(board_reset_pd_mcu); + +uint16_t tcpc_get_alert_status(void) +{ + uint16_t status = 0; + + /* + * Check which port has the ALERT line set and ignore if that TCPC has + * its reset line active. + */ + if (!gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(usb_c0_tcpc_int_odl))) { + if (gpio_pin_get_dt( + GPIO_DT_FROM_NODELABEL(usb_c0_tcpc_rst_l)) != 0) + status |= PD_STATUS_TCPC_ALERT_0; + } + + if (!gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(usb_c1_tcpc_int_odl))) { + if (gpio_pin_get_dt( + GPIO_DT_FROM_NODELABEL(usb_c1_tcpc_rst_l)) != 0) + status |= PD_STATUS_TCPC_ALERT_1; + } + + return status; +} + +void tcpc_alert_event(enum gpio_signal signal) +{ + int port; + + switch (signal) { + case GPIO_SIGNAL(DT_NODELABEL(usb_c0_tcpc_int_odl)): + port = 0; + break; + case GPIO_SIGNAL(DT_NODELABEL(usb_c1_tcpc_int_odl)): + port = 1; + break; + default: + return; + } + + schedule_deferred_pd_interrupt(port); +} + +void ppc_alert(enum gpio_signal signal) +{ + switch (signal) { + case GPIO_SIGNAL(DT_NODELABEL(gpio_usb_c0_ppc_int)): + ppc_chips[USBC_PORT_C0].drv->interrupt(USBC_PORT_C0); + break; + case GPIO_SIGNAL(DT_NODELABEL(gpio_usb_c1_ppc_int)): + ppc_chips[USBC_PORT_C1].drv->interrupt(USBC_PORT_C1); + break; + default: + return; + } +} + +/* TODO: This code should really be generic, and run based on something in + * the dts. + */ +static void stubs_interrupt_init(void) +{ + /* Enable TCPC interrupts. */ + gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c0)); + gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c1)); + + cprints(CC_USB, "Resetting TCPCs..."); + cflush(); + + /* Reset generic TCPCI on port 0. */ + gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(usb_c0_tcpc_rst_l), 0); + msleep(1); + gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(usb_c0_tcpc_rst_l), 1); + + /* Reset PS8XXX on port 1. */ + gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(usb_c1_tcpc_rst_l), 0); + msleep(PS8XXX_RESET_DELAY_MS); + gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(usb_c1_tcpc_rst_l), 1); + + /* Enable PPC interrupts. */ + gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c0_ppc)); + gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c1_ppc)); + + /* Enable SwitchCap interrupt */ + gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_switchcap_pg)); +} +DECLARE_HOOK(HOOK_INIT, stubs_interrupt_init, HOOK_PRIO_POST_I2C); + +void board_set_switchcap_power(int enable) +{ + gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(gpio_switchcap_on), enable); + /* TODO(b/217554681): So, the ln9310 emul should probably be setting + * this instead of setting it here. + */ + gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(gpio_src_vph_pwr_pg), enable); + gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(gpio_mb_power_good), enable); +} + +int board_is_switchcap_enabled(void) +{ + return gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(gpio_switchcap_on)); +} + +int board_is_switchcap_power_good(void) +{ + return gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(gpio_src_vph_pwr_pg)); +} + +void sys_arch_reboot(int type) +{ + ARG_UNUSED(type); +} + +/* GPIO TEST interrupt handler */ +bool gpio_test_interrupt_triggered; +void gpio_test_interrupt(enum gpio_signal signal) +{ + ARG_UNUSED(signal); + printk("%s called\n", __func__); + gpio_test_interrupt_triggered = true; +} + +int clock_get_freq(void) +{ + return 16000000; +} diff --git a/zephyr/test/drivers/common/src/test_mocks.c b/zephyr/test/drivers/common/src/test_mocks.c new file mode 100644 index 0000000000..11887f7cb9 --- /dev/null +++ b/zephyr/test/drivers/common/src/test_mocks.c @@ -0,0 +1,46 @@ +/* Copyright 2021 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/ztest.h> + +#include "test/drivers/test_mocks.h" + +DEFINE_FFF_GLOBALS; + +/* Mocks for common/init_rom.c */ +DEFINE_FAKE_VALUE_FUNC(const void *, init_rom_map, const void *, int); +DEFINE_FAKE_VOID_FUNC(init_rom_unmap, const void *, int); +DEFINE_FAKE_VALUE_FUNC(int, init_rom_copy, int, int, int); + +/* Mocks for common/system.c */ +DEFINE_FAKE_VALUE_FUNC(int, system_jumped_late); +DEFINE_FAKE_VALUE_FUNC(int, system_is_locked); +DEFINE_FAKE_VOID_FUNC(system_reset, int); +DEFINE_FAKE_VOID_FUNC(software_panic, uint32_t, uint32_t); +DEFINE_FAKE_VOID_FUNC(assert_post_action, const char *, unsigned int); + +/* Mocks for common/lid_angle.c */ +DEFINE_FAKE_VOID_FUNC(lid_angle_peripheral_enable, int); + +/** + * @brief Reset all the fakes before each test. + */ +static void fff_reset_rule_before(const struct ztest_unit_test *test, + void *data) +{ + ARG_UNUSED(test); + ARG_UNUSED(data); + + RESET_FAKE(init_rom_map); + RESET_FAKE(init_rom_unmap); + RESET_FAKE(init_rom_copy); + RESET_FAKE(system_jumped_late); + RESET_FAKE(system_reset); + RESET_FAKE(software_panic); + RESET_FAKE(assert_post_action); + RESET_FAKE(lid_angle_peripheral_enable); +} + +ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL); diff --git a/zephyr/test/drivers/common/src/test_rules.c b/zephyr/test/drivers/common/src/test_rules.c new file mode 100644 index 0000000000..e1b1d59480 --- /dev/null +++ b/zephyr/test/drivers/common/src/test_rules.c @@ -0,0 +1,38 @@ +/* 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/ztest.h> + +#include "emul/tcpc/emul_tcpci.h" +#include "motion_sense_fifo.h" +#include "test/drivers/stubs.h" +#include "test/drivers/utils.h" +#include "usb_pd_tcpm.h" + +static void motion_sense_fifo_reset_before(const struct ztest_unit_test *test, + void *data) +{ + ARG_UNUSED(test); + ARG_UNUSED(data); + motion_sense_fifo_reset(); +} +ZTEST_RULE(motion_sense_fifo_reset, motion_sense_fifo_reset_before, NULL); + +static void tcpci_revision_reset_before(const struct ztest_unit_test *test, + void *data) +{ + ARG_UNUSED(test); + ARG_UNUSED(data); + const struct emul *tcpc_c0_emul = EMUL_GET_USBC_BINDING(0, tcpc); + const struct emul *tcpc_c1_emul = EMUL_GET_USBC_BINDING(1, tcpc); + + /* Set TCPCI to revision 2 for both emulators */ + tcpc_config[USBC_PORT_C0].flags |= TCPC_FLAGS_TCPCI_REV2_0; + tcpci_emul_set_rev(tcpc_c0_emul, TCPCI_EMUL_REV2_0_VER1_1); + + tcpc_config[USBC_PORT_C1].flags |= TCPC_FLAGS_TCPCI_REV2_0; + tcpci_emul_set_rev(tcpc_c1_emul, TCPCI_EMUL_REV2_0_VER1_1); +} +ZTEST_RULE(tcpci_revision_reset, tcpci_revision_reset_before, NULL); diff --git a/zephyr/test/drivers/common/src/utils.c b/zephyr/test/drivers/common/src/utils.c new file mode 100644 index 0000000000..f083300886 --- /dev/null +++ b/zephyr/test/drivers/common/src/utils.c @@ -0,0 +1,627 @@ +/* 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/drivers/gpio/gpio_emul.h> +#include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_dummy.h> /* nocheck */ +#include <zephyr/shell/shell_uart.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include "acpi.h" +#include "battery.h" +#include "battery_smart.h" +#include "charge_state.h" +#include "chipset.h" +#include "lpc.h" +#include "emul/emul_isl923x.h" +#include "emul/emul_smart_battery.h" +#include "emul/emul_stub_device.h" +#include "emul/tcpc/emul_tcpci_partner_src.h" +#include "hooks.h" +#include "power.h" +#include "task.h" +#include "tcpm/tcpci.h" +#include "test/drivers/stubs.h" +#include "test/drivers/utils.h" + +#define BATTERY_NODE DT_NODELABEL(battery) +#define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) +#define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) + +void test_set_battery_level(int percentage) +{ + struct sbat_emul_bat_data *bat; + const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); + const struct device *battery_gpio_dev = + DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); + bat = sbat_emul_get_bat_data(emul); + + bat->cap = bat->full_cap * percentage / 100; + bat->volt = battery_get_info()->voltage_normal; + bat->design_mv = bat->volt; + + /* Set battery present gpio. */ + zassert_ok(gpio_emul_input_set(battery_gpio_dev, + GPIO_BATT_PRES_ODL_PORT, 0), + NULL); + + /* We need to wait for the charge task to re-read battery parameters */ + WAIT_FOR(!charge_want_shutdown(), CHARGE_MAX_SLEEP_USEC + 1, + k_sleep(K_SECONDS(1))); +} + +void test_set_chipset_to_s0(void) +{ + printk("%s: Forcing power on\n", __func__); + + task_wake(TASK_ID_CHIPSET); + k_sleep(K_SECONDS(1)); + + /* + * Make sure that battery is in good condition to + * not trigger hibernate in charge_state_v2.c + * Set battery voltage to expected value and capacity to 50%. Battery + * will not be full and accepts charging, but will not trigger + * hibernate. Charge level is set to the default value of an emulator + * (emul/emul_smart_battery.c). b/244366201. + */ + test_set_battery_level(50); + + /* The easiest way to power on seems to be the shell command. */ + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power on"), + NULL); + + k_sleep(K_SECONDS(1)); + + /* Check if chipset is in correct state */ + zassert_equal(POWER_S0, power_get_state(), "Expected S0, got %d", + power_get_state()); +} + +void test_set_chipset_to_power_level(enum power_state new_state) +{ + zassert_true(new_state == POWER_G3 || new_state == POWER_S5 || + new_state == POWER_S4 || new_state == POWER_S3 || + new_state == POWER_S0 +#ifdef CONFIG_POWER_S0IX + || new_state == POWER_S0ix +#endif + , + "Power state must be one of the steady states"); + task_wake(TASK_ID_CHIPSET); + k_sleep(K_SECONDS(1)); + + if (new_state == POWER_G3) { + test_set_chipset_to_g3(); + return; + } + + test_set_chipset_to_s0(); + + power_set_state(new_state); + + k_sleep(K_SECONDS(1)); + + /* Check if chipset is in correct state */ + zassert_equal(new_state, power_get_state(), "Expected %d, got %d", + new_state, power_get_state()); +} + +void test_set_chipset_to_g3(void) +{ + /* Let power code to settle on a particular state first. */ + task_wake(TASK_ID_CHIPSET); + k_sleep(K_SECONDS(1)); + + printk("%s: Forcing shutdown\n", __func__); + chipset_force_shutdown(CHIPSET_RESET_KB_SYSRESET); + k_sleep(K_SECONDS(20)); + /* Check if chipset is in correct state */ + zassert_equal(POWER_G3, power_get_state(), "Expected G3, got %d", + power_get_state()); +} + +void connect_source_to_port(struct tcpci_partner_data *partner, + struct tcpci_src_emul_data *src, int pdo_index, + const struct emul *tcpci_emul, + const struct emul *charger_emul) +{ + set_ac_enabled(true); + zassume_ok(tcpci_partner_connect_to_tcpci(partner, tcpci_emul), NULL); + + isl923x_emul_set_adc_vbus(charger_emul, + PDO_FIXED_GET_VOLT(src->pdo[pdo_index])); + + k_sleep(K_SECONDS(10)); +} + +void disconnect_source_from_port(const struct emul *tcpci_emul, + const struct emul *charger_emul) +{ + set_ac_enabled(false); + zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul), NULL); + isl923x_emul_set_adc_vbus(charger_emul, 0); + k_sleep(K_SECONDS(1)); +} + +void connect_sink_to_port(struct tcpci_partner_data *partner, + const struct emul *tcpci_emul, + const struct emul *charger_emul) +{ + /* + * TODO(b/221439302) Updating the TCPCI emulator registers, updating the + * vbus, as well as alerting should all be a part of the connect + * function. + */ + /* Enforce that we only support the isl923x emulator for now */ + __ASSERT_NO_MSG(EMUL_DT_GET(DT_NODELABEL(isl923x_emul)) == + charger_emul); + isl923x_emul_set_adc_vbus(charger_emul, 0); + tcpci_emul_set_reg(tcpci_emul, TCPC_REG_POWER_STATUS, + TCPC_REG_POWER_STATUS_VBUS_DET); + tcpci_emul_set_reg(tcpci_emul, TCPC_REG_EXT_STATUS, + TCPC_REG_EXT_STATUS_SAFE0V); + + tcpci_tcpc_alert(0); + k_sleep(K_SECONDS(1)); + + zassume_ok(tcpci_partner_connect_to_tcpci(partner, tcpci_emul), NULL); + + /* Wait for PD negotiation and current ramp. + * TODO(b/213906889): Check message timing and contents. + */ + k_sleep(K_SECONDS(10)); +} + +void disconnect_sink_from_port(const struct emul *tcpci_emul) +{ + zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul), NULL); + k_sleep(K_SECONDS(1)); +} + +uint8_t acpi_read(uint8_t acpi_addr) +{ + uint8_t readval; + /* + * See ec_commands.h for details on the required process + * First, send the read command, which should populate no data + */ + zassume_ok(acpi_ap_to_ec(true, EC_CMD_ACPI_READ, &readval), + "Failed to send read command"); + + /* Next, time for the address which should populate our result */ + zassume_equal(acpi_ap_to_ec(false, acpi_addr, &readval), 1, + "Failed to read value"); + return readval; +} + +void acpi_write(uint8_t acpi_addr, uint8_t write_byte) +{ + uint8_t readval; + /* + * See ec_commands.h for details on the required process + * First, send the read command, which should populate no data + */ + zassume_ok(acpi_ap_to_ec(true, EC_CMD_ACPI_WRITE, &readval), + "Failed to send read command"); + + /* Next, time for the address we want to write */ + zassume_ok(acpi_ap_to_ec(false, acpi_addr, &readval), + "Failed to write address"); + + /* Finally, time to write the data */ + zassume_ok(acpi_ap_to_ec(false, write_byte, &readval), + "Failed to write value"); +} + +enum ec_status host_cmd_host_event(enum ec_host_event_action action, + enum ec_host_event_mask_type mask_type, + struct ec_response_host_event *r) +{ + enum ec_status ret_val; + + struct ec_params_host_event params = { + .action = action, + .mask_type = mask_type, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_HOST_EVENT, 0, *r, params); + + ret_val = host_command_process(&args); + + return ret_val; +} + +void host_cmd_motion_sense_dump(int max_sensor_count, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_DUMP, + .dump = { + .max_sensor_count = max_sensor_count, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 4, *response, params); + + zassume_ok(host_command_process(&args), + "Failed to get motion_sense dump"); +} + +int host_cmd_motion_sense_data(uint8_t sensor_num, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_DATA, + .sensor_odr = { + .sensor_num = sensor_num, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 4, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_info(uint8_t cmd_version, uint8_t sensor_num, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_INFO, + .sensor_odr = { + .sensor_num = sensor_num, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, cmd_version, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_ec_rate(uint8_t sensor_num, int data_rate_ms, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_EC_RATE, + .ec_rate = { + .sensor_num = sensor_num, + .data = data_rate_ms, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_odr(uint8_t sensor_num, int32_t odr, bool round_up, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_SENSOR_ODR, + .sensor_odr = { + .sensor_num = sensor_num, + .data = odr, + .roundup = round_up, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_range(uint8_t sensor_num, int32_t range, + bool round_up, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_SENSOR_RANGE, + .sensor_range = { + .sensor_num = sensor_num, + .data = range, + .roundup = round_up, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_offset(uint8_t sensor_num, uint16_t flags, + int16_t temperature, int16_t offset_x, + int16_t offset_y, int16_t offset_z, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_SENSOR_OFFSET, + .sensor_offset = { + .sensor_num = sensor_num, + .flags = flags, + .temp = temperature, + .offset = { offset_x, offset_y, offset_z }, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_scale(uint8_t sensor_num, uint16_t flags, + int16_t temperature, int16_t scale_x, + int16_t scale_y, int16_t scale_z, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_SENSOR_SCALE, + .sensor_scale = { + .sensor_num = sensor_num, + .flags = flags, + .temp = temperature, + .scale = { scale_x, scale_y, scale_z }, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_calib(uint8_t sensor_num, bool enable, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_PERFORM_CALIB, + .perform_calib = { + .sensor_num = sensor_num, + .enable = enable, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_fifo_flush(uint8_t sensor_num, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_FIFO_FLUSH, + .sensor_odr = { + .sensor_num = sensor_num, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_fifo_info(struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_FIFO_INFO, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_fifo_read(uint8_t buffer_length, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_FIFO_READ, + .fifo_read = { + .max_data_vector = buffer_length, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_int_enable(int8_t enable, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_FIFO_INT_ENABLE, + .fifo_int_enable = { + .enable = enable, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +int host_cmd_motion_sense_spoof(uint8_t sensor_num, uint8_t enable, + int16_t values0, int16_t values1, + int16_t values2, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_SPOOF, + .spoof = { + .sensor_id = sensor_num, + .spoof_enable = enable, + .components = { values0, values1, values2 }, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + +void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type, + void *response, size_t response_size) +{ + struct ec_params_typec_discovery params = { + .port = port, .partner_type = partner_type + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_DISCOVERY, 0, params); + /* The expected response to EC_CMD_TYPEC_DISCOVERY extends beyond the + * bounds of struct ec_response_typec_discovery. + */ + args.response = response; + args.response_max = response_size; + + zassume_ok(host_command_process(&args), + "Failed to get Type-C state for port %d", port); +} + +void host_cmd_typec_control_enter_mode(int port, enum typec_mode mode) +{ + struct ec_params_typec_control params = { + .port = port, + .command = TYPEC_CONTROL_COMMAND_ENTER_MODE, + .mode_to_enter = mode + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_typec_control_exit_modes(int port) +{ + struct ec_params_typec_control params = { + .port = port, .command = TYPEC_CONTROL_COMMAND_EXIT_MODES + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_typec_control_usb_mux_set(int port, + struct typec_usb_mux_set mux_set) +{ + struct ec_params_typec_control params = { + .port = port, + .command = TYPEC_CONTROL_COMMAND_USB_MUX_SET, + .mux_params = mux_set, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_typec_control_clear_events(int port, uint32_t events) +{ + struct ec_params_typec_control params = { + .port = port, + .command = TYPEC_CONTROL_COMMAND_CLEAR_EVENTS, + .clear_events_mask = events, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params); + + zassume_ok(host_command_process(&args), + "Failed to send Type-C control for port %d", port); +} + +void host_cmd_usb_pd_get_amode( + uint8_t port, uint16_t svid_idx, + struct ec_params_usb_pd_get_mode_response *response, int *response_size) +{ + struct ec_params_usb_pd_get_mode_request params = { + .port = port, + .svid_idx = svid_idx, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_USB_PD_GET_AMODE, 0, params); + args.response = response; + + zassume_ok(host_command_process(&args), + "Failed to get alternate-mode info for port %d", port); + *response_size = args.response_size; +} + +void host_events_save(struct host_events_ctx *host_events_ctx) +{ + host_events_ctx->lpc_host_events = lpc_get_host_events(); + + for (int i = 0; i < LPC_HOST_EVENT_COUNT; i++) { + host_events_ctx->lpc_host_event_mask[i] = + lpc_get_host_events_by_type(i); + } +} + +void host_events_restore(struct host_events_ctx *host_events_ctx) +{ + lpc_set_host_event_state(host_events_ctx->lpc_host_events); + + for (int i = 0; i < LPC_HOST_EVENT_COUNT; i++) { + lpc_set_host_event_mask( + i, host_events_ctx->lpc_host_event_mask[i]); + } +} + +K_HEAP_DEFINE(test_heap, 2048); + +void *test_malloc(size_t bytes) +{ + void *mem; + + mem = k_heap_alloc(&test_heap, bytes, K_NO_WAIT); + + if (mem == NULL) { + printk("Failed to alloc %d bytes\n", bytes); + } + + return mem; +} + +void test_free(void *mem) +{ + k_heap_free(&test_heap, mem); +} + +int emul_init_stub(const struct device *dev) +{ + ARG_UNUSED(dev); + + return 0; +} + +/* These 2 lines are needed because we don't define an espi host driver */ +#define DT_DRV_COMPAT zephyr_espi_emul_espi_host +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +void check_console_cmd(const char *cmd, const char *expected_output, + const int expected_rv, const char *file, const int line) +{ + const char *buffer; + size_t buffer_size; + int rv; + + shell_backend_dummy_clear_output(get_ec_shell()); + rv = shell_execute_cmd(get_ec_shell(), cmd); + + zassert_equal(expected_rv, rv, + "%s:%u \'%s\' - Expected %d, returned %d", file, line, + cmd, expected_rv, rv); + + if (expected_output) { + buffer = shell_backend_dummy_get_output(get_ec_shell(), + &buffer_size); + zassert_true(strstr(buffer, expected_output), + "Invalid console output %s", buffer); + } +} |