summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/ap_mux_control/src/ap_mux_control.c
blob: 993c6e680c3c2b7152371b1b8f3a43e224e800db (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
/* 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 "test/drivers/stubs.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"
#include "usb_mux.h"

#include <zephyr/ztest.h>

static void ap_mux_control_before(void *data)
{
	/* Set chipset on to ensure muxes are "powered" */
	test_set_chipset_to_s0();

	/*
	 * Set all muxes to NONE to begin with and give time for the USB_MUX
	 * task to process them.
	 */
	usb_mux_set(USBC_PORT_C0, USB_PD_MUX_NONE, USB_SWITCH_CONNECT, 0);
	k_sleep(K_SECONDS(1));

	/* And test the assumption that setting NONE worked */
	zassert_equal(usb_mux_get(USBC_PORT_C0), USB_PD_MUX_NONE,
		      "Failed to set mux to initial state");
}

static void ap_mux_control_after(void *data)
{
	/*
	 * Set all muxes to NONE now that we're done and give time for the
	 * USB_MUX task to process them.
	 */
	usb_mux_set(USBC_PORT_C0, USB_PD_MUX_NONE, USB_SWITCH_CONNECT, 0);
	k_sleep(K_SECONDS(1));
}

ZTEST_SUITE(ap_mux_control, drivers_predicate_post_main, NULL,
	    ap_mux_control_before, ap_mux_control_after, NULL);

ZTEST(ap_mux_control, test_feature_present)
{
	struct ec_response_get_features feat = host_cmd_get_features();

	zassert_true(feat.flags[1] &
			     EC_FEATURE_MASK_1(EC_FEATURE_TYPEC_AP_MUX_SET),
		     "Failed to see feature present");
}

ZTEST(ap_mux_control, test_set_muxes)
{
	struct ec_response_typec_status status;
	struct typec_usb_mux_set mux_set;
	uint32_t port_events;
	int index;
	uint8_t set_mode = USB_PD_MUX_DOCK;

	/* Test setting both mux indexes and receiving their events */
	/* TODO(b/239457738): Loop counter should come from device tree */
	for (index = 0; index < 2; index++) {
		mux_set.mux_index = index;
		mux_set.mux_flags = set_mode;

		host_cmd_typec_control_usb_mux_set(USBC_PORT_C0, mux_set);

		/* Give the task processing time */
		k_sleep(K_SECONDS(1));

		/*
		 * TODO(b/239460181): The "AP" should receive
		 * EC_HOST_EVENT_PD_MCU
		 */

		/* We should see the right index's event set on the port */
		status = host_cmd_typec_status(USBC_PORT_C0);
		port_events = index ? PD_STATUS_EVENT_MUX_1_SET_DONE :
				      PD_STATUS_EVENT_MUX_0_SET_DONE;
		zassert_true(status.events & port_events, "Port event missing");

		/* Clear this mux's event */
		host_cmd_typec_control_clear_events(USBC_PORT_C0, port_events);
	}

	/*
	 * Verify our mux mode is reported as set, and that our mux events are
	 * cleared out
	 */
	status = host_cmd_typec_status(USBC_PORT_C0);
	port_events = PD_STATUS_EVENT_MUX_0_SET_DONE |
		      PD_STATUS_EVENT_MUX_1_SET_DONE;
	zassert_false(status.events & port_events, "Port events still set");
	zassert_equal(status.mux_state, set_mode,
		      "Mux set to unexpected state");
}