summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/nx20p348x/src/nx20p348x.c
blob: 5ec7ba5ae16b6d5a3ac64a133ef555fb8540608d (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
/* Copyright 2023 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "battery_smart.h"
#include "charger.h"
#include "console.h"
#include "driver/ppc/nx20p348x.h"
#include "driver/ppc/nx20p348x_public.h"
#include "nx20p348x_test_shared.h"
#include "test/drivers/stubs.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"
#include "usb_pd_tcpm.h"

#include <zephyr/shell/shell.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/ztest.h>

#define NX20P383X_NODE DT_NODELABEL(nx20p348x_emul)

static void *nx20p348x_driver_setup(void)
{
	static struct nx20p348x_driver_fixture fix;

	fix.nx20p348x_emul = EMUL_DT_GET(NX20P383X_NODE);

	return &fix;
}

ZTEST_SUITE(nx20p348x_driver, drivers_predicate_post_main,
	    nx20p348x_driver_setup, NULL, NULL, NULL);

struct curr_limit_pair {
	enum tcpc_rp_value rp;
	uint8_t reg;
};

/* Note: Register values are slightly higher to account for overshoot */
static struct curr_limit_pair currents[] = {
	{ .rp = TYPEC_RP_3A0, .reg = NX20P348X_ILIM_3_200 },
	{ .rp = TYPEC_RP_1A5, .reg = NX20P348X_ILIM_1_600 },
	{ .rp = TYPEC_RP_USB, .reg = NX20P348X_ILIM_0_600 },
};

ZTEST_F(nx20p348x_driver, test_source_curr_limits)
{
	for (int i = 0; i < ARRAY_SIZE(currents); i++) {
		uint8_t read;

		ppc_set_vbus_source_current_limit(TEST_PORT, currents[i].rp);
		read = nx20p348x_emul_peek(fixture->nx20p348x_emul,
					   NX20P348X_5V_SRC_OCP_THRESHOLD_REG);
		zassert_equal(
			(read & NX20P348X_ILIM_MASK), currents[i].reg,
			"Failed to see correct threshold for Rp %d (reg: 0x%02x)",
			currents[i].rp, read);
	}
}

ZTEST_F(nx20p348x_driver, test_discharge_vbus)
{
	uint8_t reg;

	zassert_ok(ppc_discharge_vbus(TEST_PORT, true));
	reg = nx20p348x_emul_peek(fixture->nx20p348x_emul,
				  NX20P348X_DEVICE_CONTROL_REG);
	zassert_equal((reg & NX20P348X_CTRL_VBUSDIS_EN),
		      NX20P348X_CTRL_VBUSDIS_EN);

	zassert_ok(ppc_discharge_vbus(TEST_PORT, false));
	reg = nx20p348x_emul_peek(fixture->nx20p348x_emul,
				  NX20P348X_DEVICE_CONTROL_REG);
	zassert_not_equal((reg & NX20P348X_CTRL_VBUSDIS_EN),
			  NX20P348X_CTRL_VBUSDIS_EN);
}

ZTEST(nx20p348x_driver, test_ppc_dump)
{
	const struct shell *shell_zephyr = get_ec_shell();
	const char *outbuffer;
	size_t buffer_size;

	shell_backend_dummy_clear_output(shell_zephyr);

	/* This chip supports PPC dump, so should return success */
	zassert_ok(shell_execute_cmd(shell_zephyr, "ppc_dump 0"));
	outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size);

	zassert_true(buffer_size > 0);

	/* Weakly verify something reasonable was output to console */
	zassert_not_null(strstr(outbuffer, "]: 0x"));
}

ZTEST_F(nx20p348x_driver, test_db_exit_err)
{
	uint8_t reg;

	/* Test an error to exit dead battery mode */
	nx20p348x_emul_set_interrupt1(fixture->nx20p348x_emul,
				      NX20P348X_INT1_DBEXIT_ERR);

	/* Give the interrupt time to process */
	k_sleep(K_MSEC(500));

	/* Interrupt should have set DB exit in the control register */
	reg = nx20p348x_emul_peek(fixture->nx20p348x_emul,
				  NX20P348X_DEVICE_CONTROL_REG);
	zassert_equal((reg & NX20P348X_CTRL_DB_EXIT), NX20P348X_CTRL_DB_EXIT);
}

ZTEST_F(nx20p348x_driver, test_db_exit_err_max)
{
	uint8_t reg;

	/* Set a DB exit error 10 times */
	for (int i = 0; i < 10; i++) {
		nx20p348x_emul_set_interrupt1(fixture->nx20p348x_emul,
					      NX20P348X_INT1_DBEXIT_ERR);
		k_sleep(K_MSEC(500));
	}

	/* Interrupt should now be masked by the driver */
	reg = nx20p348x_emul_peek(fixture->nx20p348x_emul,
				  NX20P348X_INTERRUPT1_MASK_REG);
	zassert_equal((reg & NX20P348X_INT1_DBEXIT_ERR),
		      NX20P348X_INT1_DBEXIT_ERR);
}

/* Add filler in case of event data */
#define MAX_RESPONSE_PD_LOG_ENTRY_SIZE (sizeof(struct ec_response_pd_log) + 16)

static void flush_pd_log(void)
{
	uint8_t response_buffer[MAX_RESPONSE_PD_LOG_ENTRY_SIZE];
	struct ec_response_pd_log *response =
		(struct ec_response_pd_log *)response_buffer;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_SIMPLE(EC_CMD_PD_GET_LOG_ENTRY, 0);

	args.response = response;
	args.response_max = sizeof(response_buffer);

	for (int i = 0; i < 10; i++) {
		zassert_ok(host_command_process(&args));

		if (response->type == PD_EVENT_NO_ENTRY)
			return;

		k_sleep(K_MSEC(500));
	}

	zassert_unreachable("Failed to flush PD log");
}

ZTEST_F(nx20p348x_driver, test_vbus_overcurrent)
{
	uint8_t response_buffer[MAX_RESPONSE_PD_LOG_ENTRY_SIZE];
	struct ec_response_pd_log *response =
		(struct ec_response_pd_log *)response_buffer;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_SIMPLE(EC_CMD_PD_GET_LOG_ENTRY, 0);

	flush_pd_log();

	/* Set up overcurrent */
	nx20p348x_emul_set_interrupt1(fixture->nx20p348x_emul,
				      NX20P348X_INT1_OC_5VSRC);
	k_sleep(K_MSEC(500));

	args.response = response;
	args.response_max = sizeof(response_buffer);

	zassert_ok(host_command_process(&args));
	zassert_equal(TEST_PORT, PD_LOG_PORT(response->size_port));
	zassert_equal(0, PD_LOG_SIZE(response->size_port));
	zassert_equal(PD_EVENT_PS_FAULT, response->type);
	zassert_equal(PS_FAULT_OCP, response->data);
}

ZTEST_F(nx20p348x_driver, test_vbus_reverse_current)
{
	uint8_t response_buffer[MAX_RESPONSE_PD_LOG_ENTRY_SIZE];
	struct ec_response_pd_log *response =
		(struct ec_response_pd_log *)response_buffer;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_SIMPLE(EC_CMD_PD_GET_LOG_ENTRY, 0);

	flush_pd_log();

	/* Set up reverse current */
	nx20p348x_emul_set_interrupt1(fixture->nx20p348x_emul,
				      NX20P348X_INT1_RCP_5VSRC);
	k_sleep(K_MSEC(500));

	args.response = response;
	args.response_max = sizeof(response_buffer);

	zassert_ok(host_command_process(&args));
	zassert_equal(TEST_PORT, PD_LOG_PORT(response->size_port));
	zassert_equal(0, PD_LOG_SIZE(response->size_port));
	zassert_equal(PD_EVENT_PS_FAULT, response->type);
	zassert_equal(PS_FAULT_OCP, response->data);
}

ZTEST_F(nx20p348x_driver, test_vbus_short)
{
	const struct shell *shell_zephyr = get_ec_shell();
	const char *outbuffer;
	size_t buffer_size;

	shell_backend_dummy_clear_output(shell_zephyr);

	/* Set up Vbus short, which we only report in the console */
	nx20p348x_emul_set_interrupt1(fixture->nx20p348x_emul,
				      NX20P348X_INT1_SC_5VSRC);
	k_sleep(K_MSEC(500));

	outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size);

	zassert_true(buffer_size > 0);

	/* Weakly verify something reasonable was output to console */
	zassert_not_null(strstr(outbuffer, "short"));
}