summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/src/integration/usbc/usb_pd_ctrl_msg.c
blob: 7e063c71ff6e65b9c4d3d70c8bd3c7c623b5941e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright 2022 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <stdint.h>
#include <ztest.h>

#include "common.h"
#include "ec_tasks.h"
#include "emul/emul_isl923x.h"
#include "emul/tcpc/emul_tcpci_partner_drp.h"
#include "tcpm/tcpci.h"
#include "test/drivers/stubs.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"
#include "usb_pd.h"

#define SNK_PORT USBC_PORT_C0

#define TCPCI_EMUL_LABEL DT_NODELABEL(tcpci_emul)

struct usb_pd_ctrl_msg_test_fixture {
	struct tcpci_drp_emul partner_emul;
	const struct emul *tcpci_emul;
	const struct emul *charger_emul;
};

static void connect_sink_to_port(struct usb_pd_ctrl_msg_test_fixture *fixture)
{
	/*
	 * TODO(b/221439302) Updating the TCPCI emulator registers, updating the
	 *   vbus, as well as alerting should all be a part of the connect
	 *   function.
	 */
	isl923x_emul_set_adc_vbus(fixture->charger_emul, 0);
	zassume_ok(tcpci_emul_set_reg(fixture->tcpci_emul,
				      TCPC_REG_POWER_STATUS,
				      TCPC_REG_POWER_STATUS_VBUS_DET),
		   NULL);
	zassume_ok(tcpci_emul_set_reg(fixture->tcpci_emul, TCPC_REG_EXT_STATUS,
				      TCPC_REG_EXT_STATUS_SAFE0V),
		   NULL);
	zassume_ok(tcpci_drp_emul_connect_to_tcpci(
			   &fixture->partner_emul.data,
			   &fixture->partner_emul.src_data,
			   &fixture->partner_emul.snk_data,
			   &fixture->partner_emul.common_data,
			   &fixture->partner_emul.ops, fixture->tcpci_emul),
		   NULL);

	tcpci_tcpc_alert(SNK_PORT);
	/* Wait for PD negotiation and current ramp.
	 * TODO(b/213906889): Check message timing and contents.
	 */
	k_sleep(K_SECONDS(10));
}

static void
disconnect_sink_from_port(struct usb_pd_ctrl_msg_test_fixture *fixture)
{
	zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul), NULL);
	k_sleep(K_SECONDS(1));
}

static void *usb_pd_ctrl_msg_setup(void)
{
	static struct usb_pd_ctrl_msg_test_fixture fixture;

	/* Get references for the emulators */
	fixture.tcpci_emul =
		emul_get_binding(DT_LABEL(DT_NODELABEL(tcpci_emul)));
	fixture.charger_emul =
		emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul)));

	tcpci_emul_set_rev(fixture.tcpci_emul, TCPCI_EMUL_REV2_0_VER1_1);

	/* Turn TCPCI rev 2 ON */
	tcpc_config[SNK_PORT].flags |= TCPC_FLAGS_TCPCI_REV2_0;

	return &fixture;
}

static void usb_pd_ctrl_msg_before(void *data)
{
	struct usb_pd_ctrl_msg_test_fixture *fixture = data;

	set_test_runner_tid();

	test_set_chipset_to_g3();
	k_sleep(K_SECONDS(1));

	/* Set chipset to ON, this will set TCPM to DRP */
	test_set_chipset_to_s0();

	/* TODO(b/214401892): Check why need to give time TCPM to spin */
	k_sleep(K_SECONDS(1));

	/* Initialized the sink to request 5V and 3A */
	tcpci_drp_emul_init(&fixture->partner_emul);

	connect_sink_to_port(fixture);

	k_sleep(K_SECONDS(10));
}

static void usb_pd_ctrl_msg_after(void *data)
{
	struct usb_pd_ctrl_msg_test_fixture *fixture = data;

	disconnect_sink_from_port(fixture);
}

ZTEST_SUITE(usb_pd_ctrl_msg_test, drivers_predicate_post_main,
	    usb_pd_ctrl_msg_setup, NULL, usb_pd_ctrl_msg_after, NULL);

ZTEST_F(usb_pd_ctrl_msg_test, verify_vconn_swap)
{
	struct ec_response_typec_status snk_resp = { 0 };
	int rv = 0;

	/* TODO(b/228593065): Revert this once ZTEST fix before ordering
	 * is pulled in
	 */
	usb_pd_ctrl_msg_before(this);

	snk_resp = host_cmd_typec_status(SNK_PORT);
	zassert_equal(PD_ROLE_VCONN_SRC, snk_resp.vconn_role,
		      "SNK Returned vconn_role=%u", snk_resp.vconn_role);

	/* Send VCONN_SWAP request */
	rv = tcpci_partner_send_control_msg(&this->partner_emul.common_data,
					    PD_CTRL_VCONN_SWAP, 0);
	zassert_ok(rv, "Failed to send VCONN_SWAP request, rv=%d", rv);

	k_sleep(K_SECONDS(1));

	snk_resp = host_cmd_typec_status(SNK_PORT);
	zassert_equal(PD_ROLE_VCONN_OFF, snk_resp.vconn_role,
		      "SNK Returned vconn_role=%u", snk_resp.vconn_role);
}