|
| 1 | +/* Copyright 2022 The Chromium OS Authors. All rights reserved. |
| 2 | + * Use of this source code is governed by a BSD-style license that can be |
| 3 | + * found in the LICENSE file. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdint.h> |
| 7 | +#include <zephyr.h> |
| 8 | +#include <ztest.h> |
| 9 | +#include <drivers/gpio/gpio_emul.h> |
| 10 | + |
| 11 | +#include "ec_commands.h" |
| 12 | +#include "ec_tasks.h" |
| 13 | +#include "emul/emul_isl923x.h" |
| 14 | +#include "emul/tcpc/emul_ps8xxx.h" |
| 15 | +#include "emul/tcpc/emul_tcpci.h" |
| 16 | +#include "emul/tcpc/emul_tcpci_partner_snk.h" |
| 17 | +#include "host_command.h" |
| 18 | +#include "stubs.h" |
| 19 | +#include "tcpm/tcpci.h" |
| 20 | +#include "utils.h" |
| 21 | +#include "test_state.h" |
| 22 | + |
| 23 | +struct usbc_alt_mode_fixture { |
| 24 | + const struct emul *tcpci_emul; |
| 25 | + const struct emul *charger_emul; |
| 26 | + struct tcpci_snk_emul partner_emul; |
| 27 | +}; |
| 28 | + |
| 29 | +static void connect_partner_to_port(struct usbc_alt_mode_fixture *fixture) |
| 30 | +{ |
| 31 | + const struct emul *tcpc_emul = fixture->tcpci_emul; |
| 32 | + struct tcpci_snk_emul *partner_emul = &fixture->partner_emul; |
| 33 | + |
| 34 | + /* Set VBUS to vSafe0V initially. */ |
| 35 | + isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); |
| 36 | + tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_POWER_STATUS, |
| 37 | + TCPC_REG_POWER_STATUS_VBUS_DET); |
| 38 | + tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_EXT_STATUS, |
| 39 | + TCPC_REG_EXT_STATUS_SAFE0V); |
| 40 | + tcpci_tcpc_alert(0); |
| 41 | + zassume_ok(tcpci_snk_emul_connect_to_tcpci( |
| 42 | + &partner_emul->data, &partner_emul->common_data, |
| 43 | + &partner_emul->ops, tcpc_emul), |
| 44 | + NULL); |
| 45 | + |
| 46 | + /* Wait for PD negotiation and current ramp. */ |
| 47 | + k_sleep(K_SECONDS(10)); |
| 48 | +} |
| 49 | + |
| 50 | +static void disconnect_partner_from_port(struct usbc_alt_mode_fixture *fixture) |
| 51 | +{ |
| 52 | + zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul), NULL); |
| 53 | + isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); |
| 54 | + k_sleep(K_SECONDS(1)); |
| 55 | +} |
| 56 | + |
| 57 | +static void *usbc_alt_mode_setup(void) |
| 58 | +{ |
| 59 | + static struct usbc_alt_mode_fixture fixture; |
| 60 | + struct tcpci_partner_data *partner_common = |
| 61 | + &fixture.partner_emul.common_data; |
| 62 | + |
| 63 | + tcpci_snk_emul_init(&fixture.partner_emul); |
| 64 | + |
| 65 | + /* Get references for the emulators */ |
| 66 | + fixture.tcpci_emul = |
| 67 | + emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul))); |
| 68 | + /* The configured TCPCI rev must match the emulator's supported rev. */ |
| 69 | + tcpc_config[0].flags |= TCPC_FLAGS_TCPCI_REV2_0; |
| 70 | + tcpci_emul_set_rev(fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1); |
| 71 | + fixture.charger_emul = |
| 72 | + emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))); |
| 73 | + |
| 74 | + /* Set up SOP discovery responses for DP adapter. */ |
| 75 | + partner_common->identity_vdm[VDO_INDEX_HDR] = |
| 76 | + VDO(USB_SID_PD, /* structured VDM */ true, |
| 77 | + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_IDENT); |
| 78 | + partner_common->identity_vdm[VDO_INDEX_IDH] = VDO_IDH( |
| 79 | + /* USB host */ false, /* USB device */ false, IDH_PTYPE_AMA, |
| 80 | + /* modal operation */ true, USB_VID_GOOGLE); |
| 81 | + partner_common->identity_vdm[VDO_INDEX_CSTAT] = 0xabcdabcd; |
| 82 | + partner_common->identity_vdm[VDO_INDEX_PRODUCT] = |
| 83 | + VDO_PRODUCT(0x1234, 0x5678); |
| 84 | + /* Hardware version 1, firmware version 2 */ |
| 85 | + partner_common->identity_vdm[VDO_INDEX_AMA] = 0x12000000; |
| 86 | + partner_common->identity_vdos = VDO_INDEX_AMA + 1; |
| 87 | + |
| 88 | + /* Support DisplayPort VID. */ |
| 89 | + partner_common->svids_vdm[VDO_INDEX_HDR] = |
| 90 | + VDO(USB_SID_PD, /* structured VDM */ true, |
| 91 | + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_SVID); |
| 92 | + partner_common->svids_vdm[VDO_INDEX_HDR + 1] = |
| 93 | + VDO_SVID(USB_SID_DISPLAYPORT, 0); |
| 94 | + partner_common->svids_vdos = VDO_INDEX_HDR + 2; |
| 95 | + |
| 96 | + /* Support one mode for DisplayPort VID. Copied from Hoho. */ |
| 97 | + partner_common->modes_vdm[VDO_INDEX_HDR] = |
| 98 | + VDO(USB_SID_DISPLAYPORT, /* structured VDM */ true, |
| 99 | + VDO_CMDT(CMDT_RSP_ACK) | CMD_DISCOVER_MODES); |
| 100 | + partner_common->modes_vdm[VDO_INDEX_HDR + 1] = VDO_MODE_DP( |
| 101 | + 0, MODE_DP_PIN_C, 1, CABLE_PLUG, MODE_DP_V13, MODE_DP_SNK); |
| 102 | + partner_common->modes_vdos = VDO_INDEX_HDR + 2; |
| 103 | + |
| 104 | + /* Sink 5V 3A. */ |
| 105 | + fixture.partner_emul.data.pdo[1] = |
| 106 | + PDO_FIXED(5000, 3000, PDO_FIXED_UNCONSTRAINED); |
| 107 | + |
| 108 | + return &fixture; |
| 109 | +} |
| 110 | + |
| 111 | +static void usbc_alt_mode_before(void *data) |
| 112 | +{ |
| 113 | + /* Set chipset to ON, this will set TCPM to DRP */ |
| 114 | + test_set_chipset_to_s0(); |
| 115 | + |
| 116 | + /* TODO(b/214401892): Check why need to give time TCPM to spin */ |
| 117 | + k_sleep(K_SECONDS(1)); |
| 118 | + |
| 119 | + connect_partner_to_port((struct usbc_alt_mode_fixture *)data); |
| 120 | +} |
| 121 | + |
| 122 | +static void usbc_alt_mode_after(void *data) |
| 123 | +{ |
| 124 | + disconnect_partner_from_port((struct usbc_alt_mode_fixture *)data); |
| 125 | +} |
| 126 | + |
| 127 | +ZTEST_F(usbc_alt_mode, verify_discovery) |
| 128 | +{ |
| 129 | + uint8_t response_buffer[EC_LPC_HOST_PACKET_SIZE]; |
| 130 | + struct ec_response_typec_discovery *discovery = |
| 131 | + (struct ec_response_typec_discovery *)response_buffer; |
| 132 | + host_cmd_typec_discovery(USBC_PORT_C0, TYPEC_PARTNER_SOP, |
| 133 | + response_buffer, sizeof(response_buffer)); |
| 134 | + |
| 135 | + /* The host command does not count the VDM header in identity_count. */ |
| 136 | + zassert_equal(discovery->identity_count, |
| 137 | + this->partner_emul.common_data.identity_vdos - 1, |
| 138 | + "Expected %d identity VDOs, got %d", |
| 139 | + this->partner_emul.common_data.identity_vdos - 1, |
| 140 | + discovery->identity_count); |
| 141 | + zassert_mem_equal(discovery->discovery_vdo, |
| 142 | + this->partner_emul.common_data.identity_vdm + 1, |
| 143 | + discovery->identity_count * |
| 144 | + sizeof(*discovery->discovery_vdo), |
| 145 | + "Discovered SOP identity ACK did not match"); |
| 146 | + zassert_equal(discovery->svid_count, 1, "Expected 1 SVID, got %d", |
| 147 | + discovery->svid_count); |
| 148 | + zassert_equal(discovery->svids[0].svid, USB_SID_DISPLAYPORT, |
| 149 | + "Expected SVID 0x%0000x, got 0x%0000x", |
| 150 | + USB_SID_DISPLAYPORT, discovery->svids[0].svid); |
| 151 | + zassert_equal(discovery->svids[0].mode_count, 1, |
| 152 | + "Expected 1 DP mode, got %d", |
| 153 | + discovery->svids[0].mode_count); |
| 154 | + zassert_equal(discovery->svids[0].mode_vdo[0], |
| 155 | + this->partner_emul.common_data.modes_vdm[1], |
| 156 | + "DP mode VDOs did not match"); |
| 157 | +} |
| 158 | + |
| 159 | +ZTEST_SUITE(usbc_alt_mode, drivers_predicate_post_main, usbc_alt_mode_setup, |
| 160 | + usbc_alt_mode_before, usbc_alt_mode_after, NULL); |
0 commit comments