summaryrefslogtreecommitdiff
path: root/zephyr/test/krabby/src/usbc_config.c
blob: 909a8f38a7d18a30e78e0202190f2076fb8430fa (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
/* 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 "adc.h"
#include "baseboard_usbc_config.h"
#include "charge_manager.h"
#include "driver/ppc/syv682x.h"
#include "driver/ppc/syv682x_public.h"
#include "driver/tcpm/rt1718s.h"
#include "emul/emul_common_i2c.h"
#include "emul/emul_syv682x.h"
#include "emul/tcpc/emul_rt1718s.h"
#include "i2c/i2c.h"
#include "test_state.h"
#include "usb_pd.h"
#include "usbc_ppc.h"

#include <zephyr/devicetree.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/ztest.h>

static bool ppc_sink_enabled(int port)
{
	const struct emul *emul = (port == 0) ?
					  EMUL_DT_GET(DT_NODELABEL(ppc0_emul)) :
					  EMUL_DT_GET(DT_NODELABEL(ppc1_emul));
	uint8_t val = 0;

	syv682x_emul_get_reg(emul, SYV682X_CONTROL_1_REG, &val);

	return !(val & (SYV682X_CONTROL_1_PWR_ENB | SYV682X_CONTROL_1_HV_DR));
}

static bool usb_c1_source_gpio_enabled(void)
{
	const struct emul *emul = EMUL_DT_GET(DT_NODELABEL(rt1718s_emul));
	uint16_t val = 0;

	rt1718s_emul_get_reg(emul, RT1718S_GPIO_CTRL(GPIO_EN_USB_C1_SOURCE),
			     &val);

	return val & RT1718S_GPIO_CTRL_O;
}

ZTEST(usbc_config, test_set_active_charge_port)
{
	/* reset ppc state */
	zassert_ok(board_set_active_charge_port(CHARGE_PORT_NONE), NULL);
	zassert_false(ppc_sink_enabled(0), NULL);
	zassert_false(ppc_sink_enabled(1), NULL);

	/* sourcing port 0, expect port 0 not sinkable */
	zassert_ok(pd_set_power_supply_ready(0));
	zassert_not_equal(board_set_active_charge_port(0), 0, NULL);
	zassert_true(board_vbus_source_enabled(0), NULL);
	zassert_false(board_vbus_source_enabled(1), NULL);
	zassert_false(ppc_sink_enabled(0), NULL);
	zassert_false(ppc_sink_enabled(1), NULL);

	/* sinking port 1 */
	zassert_ok(board_set_active_charge_port(1), NULL);
	zassert_false(ppc_sink_enabled(0), NULL);
	zassert_true(ppc_sink_enabled(1), NULL);

	/*
	 * sinking an invalid port should return error and doesn't change
	 * any state
	 */
	zassert_not_equal(board_set_active_charge_port(2), 0, NULL);
	zassert_true(board_vbus_source_enabled(0), NULL);
	zassert_false(board_vbus_source_enabled(1), NULL);
	zassert_false(ppc_sink_enabled(0), NULL);
	zassert_true(ppc_sink_enabled(1), NULL);

	/* turn off sourcing, sinking port 0 */
	pd_power_supply_reset(0);
	zassert_ok(board_set_active_charge_port(0), NULL);
	zassert_true(ppc_sink_enabled(0), NULL);
	zassert_false(ppc_sink_enabled(1), NULL);

	/* sinking port 1 */
	zassert_ok(board_set_active_charge_port(1), NULL);
	zassert_false(ppc_sink_enabled(0), NULL);
	zassert_true(ppc_sink_enabled(1), NULL);

	/* back to port 0 */
	zassert_ok(board_set_active_charge_port(0), NULL);
	zassert_true(ppc_sink_enabled(0), NULL);
	zassert_false(ppc_sink_enabled(1), NULL);

	/* reset */
	zassert_ok(board_set_active_charge_port(CHARGE_PORT_NONE), NULL);
	zassert_false(board_vbus_source_enabled(0), NULL);
	zassert_false(board_vbus_source_enabled(1), NULL);
	zassert_false(ppc_sink_enabled(0), NULL);
	zassert_false(ppc_sink_enabled(1), NULL);
}

ZTEST(usbc_config, test_set_active_charge_port_fail)
{
	const struct emul *ppc0 = EMUL_DT_GET(DT_NODELABEL(ppc0_emul));

	/* Verify that failure on ppc0 doesn't affect ppc1 */
	i2c_common_emul_set_write_fail_reg(
		emul_syv682x_get_i2c_common_data(ppc0),
		I2C_COMMON_EMUL_FAIL_ALL_REG);

	zassert_ok(board_set_active_charge_port(1), NULL);
	zassert_true(ppc_sink_enabled(1), NULL);
	zassert_ok(board_set_active_charge_port(CHARGE_PORT_NONE), NULL);
	zassert_false(ppc_sink_enabled(1), NULL);
	zassert_ok(board_set_active_charge_port(1), NULL);
	zassert_true(ppc_sink_enabled(1), NULL);

	/* trying to enable ppc0 results in error */
	zassert_not_equal(board_set_active_charge_port(0), 0, NULL);
	zassert_false(ppc_sink_enabled(1), NULL);
}

ZTEST(usbc_config, test_rt1718s_gpio_toggle)
{
	/* toggle sourcing on port 1, expect rt1718s gpio also changes */
	zassert_false(usb_c1_source_gpio_enabled());
	zassert_ok(pd_set_power_supply_ready(1));
	zassert_true(usb_c1_source_gpio_enabled());
	pd_power_supply_reset(1);
	zassert_false(usb_c1_source_gpio_enabled());
}

ZTEST(usbc_config, test_adc_channel)
{
	zassert_equal(board_get_vbus_adc(0), ADC_VBUS_C0, NULL);
	zassert_equal(board_get_vbus_adc(1), ADC_VBUS_C1, NULL);
	zassert_equal(board_get_vbus_adc(99), ADC_VBUS_C0, NULL);
}

static void reset_ppc_state(void *fixture)
{
	const struct emul *ppc0 = EMUL_DT_GET(DT_NODELABEL(ppc0_emul));
	const struct emul *ppc1 = EMUL_DT_GET(DT_NODELABEL(ppc1_emul));

	i2c_common_emul_set_write_fail_reg(
		emul_syv682x_get_i2c_common_data(ppc0),
		I2C_COMMON_EMUL_NO_FAIL_REG);
	i2c_common_emul_set_write_fail_reg(
		emul_syv682x_get_i2c_common_data(ppc1),
		I2C_COMMON_EMUL_NO_FAIL_REG);

	ppc_vbus_source_enable(0, false);
	ppc_vbus_source_enable(1, false);
	board_set_active_charge_port(CHARGE_PORT_NONE);
}

ZTEST_SUITE(usbc_config, krabby_predicate_post_main, NULL, reset_ppc_state,
	    NULL, NULL);