summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/lid_switch.c
blob: 2fe7daa85a2b45cbcb32753323913e931e1b5531 (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
295
296
297
298
299
300
301
302
303
304
305
/* 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 "ec_commands.h"
#include "host_command.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"

#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/ztest.h>

#include <console.h>
#include <lid_switch.h>

#define LID_GPIO_PATH DT_PATH(named_gpios, lid_open_ec)
#define LID_GPIO_PIN DT_GPIO_PIN(LID_GPIO_PATH, gpios)

int emul_lid_open(void)
{
	const struct device *lid_gpio_dev =
		DEVICE_DT_GET(DT_GPIO_CTLR(LID_GPIO_PATH, gpios));

	return gpio_emul_input_set(lid_gpio_dev, LID_GPIO_PIN, 1);
}

int emul_lid_close(void)
{
	const struct device *lid_gpio_dev =
		DEVICE_DT_GET(DT_GPIO_CTLR(LID_GPIO_PATH, gpios));

	return gpio_emul_input_set(lid_gpio_dev, LID_GPIO_PIN, 0);
}

static void *lid_switch_setup(void)
{
	/**
	 * Set chipset to S0 as chipset power on after opening lid may disturb
	 * test
	 */
	test_set_chipset_to_s0();

	return NULL;
}

static void lid_switch_before(void *unused)
{
	/* Make sure that interrupt fire at the next lid open/close */
	zassert_ok(emul_lid_close());
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(100));
}

static void lid_switch_after(void *unused)
{
	struct ec_params_force_lid_open params = {
		.enabled = 0,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_FORCE_LID_OPEN, 0, params);
	int res;

	res = host_command_process(&args);
	if (res)
		TC_ERROR("host_command_process() failed (%d)\n", res);

	if (args.result)
		TC_ERROR("args.result != 0 (%d != 0)\n", args.result);

	res = emul_lid_open();
	if (res)
		TC_ERROR("emul_lid_open() failed (%d)\n", res);
	k_sleep(K_MSEC(100));
}

ZTEST(lid_switch, test_lid_open)
{
	/* Start closed. */
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 0);

	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 1);
}

ZTEST(lid_switch, test_lid_debounce)
{
	/* Start closed. */
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 0);

	/* Create interrupts quickly before they can be handled. */
	zassert_ok(emul_lid_open());
	zassert_ok(emul_lid_close());
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 1);
}

ZTEST(lid_switch, test_lid_close)
{
	/* Start open. */
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(100));

	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(200));
	zassert_equal(lid_is_open(), 0);
}

ZTEST(lid_switch, test_enable_lid_detect)
{
	/* Start open. */
	zassert_ok(emul_lid_open(), NULL);
	k_sleep(K_MSEC(500));
	zassert_equal(lid_is_open(), 1, NULL);

	/* Disable lid detect interrupts */
	enable_lid_detect(false);
	k_sleep(K_MSEC(100));

	/* Close lid but check if still indicates open as interrupt is
	 * disabled
	 */
	zassert_ok(emul_lid_close(), NULL);
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 1, NULL);
	zassert_ok(emul_lid_open(), NULL);
	k_sleep(K_MSEC(100));

	/* Restore lid detect interrupts, confirm interrupt is firing again */
	enable_lid_detect(true);
	zassert_ok(emul_lid_close(), NULL);
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 0, NULL);
}

ZTEST(lid_switch, test_cmd_lidopen)
{
	/* Start closed. */
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 0);

	/* Forced override lid open. */
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidopen"),
		      NULL);
	zassert_equal(lid_is_open(), 1);
	k_sleep(K_MSEC(100));

	printk("GPIO lid open/close\n");
	/* Open & close with gpio. */
	zassert_ok(emul_lid_open());
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(500));
	zassert_equal(lid_is_open(), 0);
}

ZTEST(lid_switch, test_cmd_lidopen_bounce)
{
	/* Start closed. */
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 0);

	printk("Console lid open\n");
	/* Forced override lid open. */
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidopen"),
		      NULL);
	zassert_equal(lid_is_open(), 1);
	k_sleep(K_MSEC(100));

	printk("Console lid open\n");
	/* Forced override lid open. */
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidopen"),
		      NULL);
	zassert_equal(lid_is_open(), 1);
	k_sleep(K_MSEC(100));

	printk("GPIO lid open/close\n");
	/* Open & close with gpio. */
	zassert_ok(emul_lid_open());
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(500));
	zassert_equal(lid_is_open(), 0);
}

ZTEST(lid_switch, test_cmd_lidclose)
{
	/* Start open. */
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 1);

	/* Forced override lid close. */
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidclose"),
		      NULL);
	zassert_equal(lid_is_open(), 0);
	k_sleep(K_MSEC(100));

	printk("GPIO lid close/open\n");
	/* Close & open with gpio. */
	zassert_ok(emul_lid_close());
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(500));
	zassert_equal(lid_is_open(), 1);
}

ZTEST(lid_switch, test_cmd_lidclose_bounce)
{
	/* Start open. */
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 1);

	/* Forced override lid close. */
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidclose"),
		      NULL);
	zassert_equal(lid_is_open(), 0);
	k_sleep(K_MSEC(100));

	/* Forced override lid close. */
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidclose"),
		      NULL);
	zassert_equal(lid_is_open(), 0);
	k_sleep(K_MSEC(100));

	printk("GPIO lid close/open\n");
	/* Close & open with gpio. */
	zassert_ok(emul_lid_close());
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(500));
	zassert_equal(lid_is_open(), 1);
}

#if defined(CONFIG_SHELL_BACKEND_DUMMY)
ZTEST(lid_switch, test_cmd_lidstate_open)
{
	const char *buffer;
	size_t buffer_size;

	/* Start open. */
	zassert_ok(emul_lid_open());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 1);

	/* Read the state with console. */
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidstate"),
		      NULL);
	buffer = shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
	zassert_true(strcmp(buffer, "\r\nlid state: open\r\n") == 0,
		     "Invalid console output %s", buffer);
}

ZTEST(lid_switch, test_cmd_lidstate_close)
{
	const char *buffer;
	size_t buffer_size;

	/* Start closed. */
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 0);

	/* Read the state with console. */
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "lidstate"),
		      NULL);
	buffer = shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
	zassert_true(strcmp(buffer, "\r\nlid state: closed\r\n") == 0,
		     "Invalid console output %s", buffer);
}
#else
#error This test requires CONFIG_SHELL_BACKEND_DUMMY
#endif

ZTEST(lid_switch, test_hc_force_lid_open)
{
	struct ec_params_force_lid_open params = {
		.enabled = 1,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_FORCE_LID_OPEN, 0, params);

	/* Start closed. */
	zassert_ok(emul_lid_close());
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 0);

	zassert_ok(host_command_process(&args));
	zassert_ok(args.result);
	k_sleep(K_MSEC(100));
	zassert_equal(lid_is_open(), 1);
}

ZTEST_SUITE(lid_switch, drivers_predicate_post_main, lid_switch_setup,
	    lid_switch_before, lid_switch_after, NULL);