summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/src/integration_usb.c
blob: 4205d3fd3eeab3e12db500daabd60353796b9540 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* Copyright 2021 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 <zephyr.h>
#include <ztest.h>
#include <drivers/gpio/gpio_emul.h>

#include "battery_smart.h"
#include "ec_commands.h"
#include "ec_tasks.h"
#include "emul/emul_isl923x.h"
#include "emul/emul_smart_battery.h"
#include "emul/tcpc/emul_tcpci.h"
#include "emul/tcpc/emul_tcpci_partner_drp.h"
#include "emul/tcpc/emul_tcpci_partner_snk.h"
#include "emul/tcpc/emul_tcpci_partner_src.h"
#include "host_command.h"
#include "stubs.h"
#include "tcpm/tcpci.h"
#include "test/usb_pe.h"
#include "utils.h"
#include "test_state.h"

#define TCPCI_EMUL_LABEL DT_NODELABEL(tcpci_emul)
#define BATTERY_ORD DT_DEP_ORD(DT_NODELABEL(battery))

#define GPIO_AC_OK_PATH DT_PATH(named_gpios, acok_od)
#define GPIO_AC_OK_PIN DT_GPIO_PIN(GPIO_AC_OK_PATH, gpios)

static void integration_usb_before(void *state)
{
	const struct emul *tcpci_emul =
		emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL));
	struct i2c_emul *i2c_emul;
	struct sbat_emul_bat_data *bat;
	const struct device *gpio_dev =
		DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_AC_OK_PATH, gpios));

	ARG_UNUSED(state);
	set_test_runner_tid();
	zassert_ok(tcpci_tcpm_init(0), 0);
	tcpci_emul_set_rev(tcpci_emul, TCPCI_EMUL_REV1_0_VER1_0);
	pd_set_suspend(0, 0);
	/* Reset to disconnected state. */
	zassert_ok(tcpci_emul_disconnect_partner(tcpci_emul), NULL);

	/* Battery defaults to charging, so reset to not charging. */
	i2c_emul = sbat_emul_get_ptr(BATTERY_ORD);
	bat = sbat_emul_get_bat_data(i2c_emul);
	bat->cur = -5;

	zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_AC_OK_PIN, 0), NULL);
}

static void integration_usb_after(void *state)
{
	const struct emul *tcpci_emul =
		emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL));
	ARG_UNUSED(state);

	/* TODO: This function should trigger gpios to signal there is nothing
	 * attached to the port.
	 */
	zassert_ok(tcpci_emul_disconnect_partner(tcpci_emul), NULL);
	/* Give time to actually disconnect */
	k_sleep(K_SECONDS(1));
}

ZTEST(integration_usb, test_attach_compliant_charger)
{
	const struct emul *tcpci_emul =
		emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL));
	struct i2c_emul *i2c_emul;
	uint16_t battery_status;
	struct tcpci_src_emul my_charger;
	const struct device *gpio_dev =
		DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_AC_OK_PATH, gpios));

	/* Verify battery not charging. */
	i2c_emul = sbat_emul_get_ptr(BATTERY_ORD);
	zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS,
					  &battery_status),
		   NULL);
	zassert_not_equal(battery_status & STATUS_DISCHARGING, 0,
			  "Battery is not discharging: %d", battery_status);

	/* TODO? Send host command to verify PD_ROLE_DISCONNECTED. */

	/* Attach emulated charger. */
	zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_AC_OK_PIN, 1), NULL);
	tcpci_src_emul_init(&my_charger);
	zassert_ok(tcpci_src_emul_connect_to_tcpci(&my_charger.data,
						   &my_charger.common_data,
						   &my_charger.ops, tcpci_emul),
		   NULL);

	/* Wait for current ramp. */
	k_sleep(K_SECONDS(10));

	/* Verify battery charging. */
	zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS,
					  &battery_status),
		   NULL);
	zassert_equal(battery_status & STATUS_DISCHARGING, 0,
		      "Battery is discharging: %d", battery_status);
	/* TODO: Also check voltage, current, etc. */
}

#define BATTERY_ORD	DT_DEP_ORD(DT_NODELABEL(battery))

ZTEST(integration_usb, test_attach_pd_charger)
{
	const struct emul *tcpci_emul =
		emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL));
	const struct emul *charger_emul =
		emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul)));
	struct i2c_emul *i2c_emul;
	uint16_t battery_status;
	struct tcpci_src_emul my_charger;
	const struct device *gpio_dev =
		DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_AC_OK_PATH, gpios));
	struct ec_params_charge_state charge_params;
	struct ec_response_charge_state charge_response;
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
			EC_CMD_CHARGE_STATE, 0, charge_response, charge_params);
	struct ec_params_typec_status typec_params;
	struct ec_response_typec_status typec_response;
	struct host_cmd_handler_args typec_args =  BUILD_HOST_COMMAND(
			EC_CMD_TYPEC_STATUS, 0, typec_response, typec_params);

	/*
	 * TODO(b/209907297): Implement the steps of the test beyond USB default
	 * charging.
	 */

	/* Attach emulated charger. Send Source Capabilities that offer 20V. Set
	 * the charger input voltage to ~18V (the highest voltage it supports).
	 */
	zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_AC_OK_PIN, 1), NULL);
	tcpci_src_emul_init(&my_charger);
	my_charger.data.pdo[1] =
		PDO_FIXED(20000, 3000, PDO_FIXED_UNCONSTRAINED);
	zassert_ok(tcpci_src_emul_connect_to_tcpci(&my_charger.data,
						   &my_charger.common_data,
						   &my_charger.ops, tcpci_emul),
		   NULL);
	isl923x_emul_set_adc_vbus(charger_emul, 0x3f);

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

	/* Verify battery charging. */
	i2c_emul = sbat_emul_get_ptr(BATTERY_ORD);
	zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS,
					  &battery_status),
		   NULL);
	zassert_equal(battery_status & STATUS_DISCHARGING, 0,
		      "Battery is discharging: %d", battery_status);

	/* Check the charging voltage and current. Cross-check the PD state,
	 * the battery/charger state, and the active PDO as reported by the PD
	 * state. The charging voltage and current are not directly related to
	 * the PD charging and current, but they should be positive if the
	 * battery is charging.
	 */
	/*
	 * TODO(b/213908743): Also check the corresponding PD state and
	 * encapsulate this for use in other tests.
	 */
	charge_params.chgnum = 0;
	charge_params.cmd = CHARGE_STATE_CMD_GET_STATE;
	zassert_ok(host_command_process(&args), "Failed to get charge state");
	zassert_true(charge_response.get_state.ac, "USB default but AC absent");
	zassert_true(charge_response.get_state.chg_voltage > 0,
			"Battery charging voltage %dmV",
			charge_response.get_state.chg_voltage);
	zassert_true(charge_response.get_state.chg_current > 0,
			"Battery charging current %dmA",
			charge_response.get_state.chg_current);

	typec_params.port = 0;
	zassert_ok(host_command_process(&typec_args),
			"Failed to get Type-C state");
	zassert_true(typec_response.pd_enabled,
			"Charger attached but PD disabled");
	zassert_true(typec_response.dev_connected,
			"Charger attached but device disconnected");
	zassert_true(typec_response.sop_connected,
			"Charger attached but not SOP capable");
	zassert_equal(typec_response.source_cap_count, 2,
			"Charger has %d source PDOs",
			typec_response.source_cap_count);
	zassert_equal(typec_response.power_role, PD_ROLE_SINK,
			"Charger attached, but TCPM power role is %d",
			typec_response.power_role);

	/*
	 * 3. Wait for SenderResponseTimeout. Expect TCPM to send Request.
	 * We could verify that the Request references the expected PDO, but
	 * the voltage/current/PDO checks at the end of the test should all be
	 * wrong if the requested PDO was wrong here.
	 */

	/*
	 * 4. Send Accept and PS_RDY from partner with appropriate delay between
	 * them. Emulate supplying VBUS at the requested voltage/current before
	 * PS_RDY.
	 */

	/*
	 * 5. Check the charging voltage and current. Cross-check the PD state,
	 * the battery/charger state, and the active PDO as reported by the PD
	 * state.
	 */
}

ZTEST(integration_usb, test_attach_sink)
{
	const struct emul *tcpci_emul =
		emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL));
	struct tcpci_snk_emul my_sink;

	/*
	 * TODO: investigate why call in integration_usb_before() is not enough
	 */
	set_test_runner_tid();

	/* 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));

	/* Attach emulated sink */
	tcpci_snk_emul_init(&my_sink);
	zassert_ok(tcpci_snk_emul_connect_to_tcpci(&my_sink.data,
						   &my_sink.common_data,
						   &my_sink.ops, tcpci_emul),
		   NULL);

	/* Wait for PD negotiation */
	k_sleep(K_SECONDS(10));

	/* Test if partner believe that PD negotiation is completed */
	zassert_true(my_sink.data.pd_completed, NULL);
	/*
	 * Test that SRC ready is achieved
	 * TODO: Change it to examining EC_CMD_TYPEC_STATUS
	 */
	zassert_equal(PE_SRC_READY, get_state_pe(USBC_PORT_C0), NULL);
}

ZTEST(integration_usb, test_attach_drp)
{
	const struct emul *tcpci_emul =
		emul_get_binding(DT_LABEL(TCPCI_EMUL_LABEL));
	struct tcpci_drp_emul my_drp;

	/*
	 * TODO: investigate why call in integration_usb_before() is not enough
	 */
	set_test_runner_tid();

	/* 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));

	/* Attach emulated sink */
	tcpci_drp_emul_init(&my_drp);
	zassert_ok(tcpci_drp_emul_connect_to_tcpci(&my_drp.data,
						   &my_drp.src_data,
						   &my_drp.snk_data,
						   &my_drp.common_data,
						   &my_drp.ops, tcpci_emul),
		   NULL);

	/* Wait for PD negotiation */
	k_sleep(K_SECONDS(10));

	/*
	 * Test that SRC ready is achieved
	 * TODO: Change it to examining EC_CMD_TYPEC_STATUS
	 */
	zassert_equal(PE_SNK_READY, get_state_pe(USBC_PORT_C0), NULL);
}

ZTEST_SUITE(integration_usb, drivers_predicate_post_main, NULL,
	    integration_usb_before, integration_usb_after, NULL);