summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/power_common.c
blob: f96fed9f05aa533bd6ed151980c9233d08ff1eea (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

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

#include "chipset.h"
#include "common.h"
#include "extpower.h"
#include "hooks.h"
#include "host_command.h"
#include "power.h"
#include "test/drivers/stubs.h"
#include "task.h"
#include "ec_tasks.h"
#include "test/drivers/test_state.h"

#include "emul/emul_common_i2c.h"
#include "emul/emul_smart_battery.h"

#include "battery.h"
#include "battery_smart.h"
#include "test/drivers/utils.h"

#define BATTERY_NODE DT_NODELABEL(battery)

/* Description of all power states with chipset state masks */
static struct {
	/* Power state */
	enum power_state p_state;
	/*
	 * CHIPSET_STATE_* to which this state transition (the same as
	 * transition_from for static states)
	 */
	int transition_to;
	/* CHIPSET_STATE_* from which this state transition */
	int transition_from;
} test_power_state_desc[] = {
	{
		.p_state = POWER_G3,
		.transition_to = CHIPSET_STATE_HARD_OFF,
		.transition_from = CHIPSET_STATE_HARD_OFF,
	},
	{
		.p_state = POWER_G3S5,
		.transition_to = CHIPSET_STATE_SOFT_OFF,
		.transition_from = CHIPSET_STATE_HARD_OFF,
	},
	{
		.p_state = POWER_S5G3,
		.transition_to = CHIPSET_STATE_HARD_OFF,
		.transition_from = CHIPSET_STATE_SOFT_OFF,
	},
	{
		.p_state = POWER_S5,
		.transition_to = CHIPSET_STATE_SOFT_OFF,
		.transition_from = CHIPSET_STATE_SOFT_OFF,
	},
	{
		.p_state = POWER_S5S3,
		.transition_to = CHIPSET_STATE_SUSPEND,
		.transition_from = CHIPSET_STATE_SOFT_OFF,
	},
	{
		.p_state = POWER_S3S5,
		.transition_to = CHIPSET_STATE_SOFT_OFF,
		.transition_from = CHIPSET_STATE_SUSPEND,
	},
	{
		.p_state = POWER_S3,
		.transition_to = CHIPSET_STATE_SUSPEND,
		.transition_from = CHIPSET_STATE_SUSPEND,
	},
	{
		.p_state = POWER_S3S0,
		.transition_to = CHIPSET_STATE_ON,
		.transition_from = CHIPSET_STATE_SUSPEND,
	},
	{
		.p_state = POWER_S0S3,
		.transition_to = CHIPSET_STATE_SUSPEND,
		.transition_from = CHIPSET_STATE_ON,
	},
	{
		.p_state = POWER_S0,
		.transition_to = CHIPSET_STATE_ON,
		.transition_from = CHIPSET_STATE_ON,
	},
};

/*
 * Chipset state masks used by chipset_in_state and
 * chipset_in_or_transitioning_to_state tests
 */
static int in_state_test_masks[] = {
	CHIPSET_STATE_HARD_OFF,
	CHIPSET_STATE_SOFT_OFF,
	CHIPSET_STATE_SUSPEND,
	CHIPSET_STATE_ON,
	CHIPSET_STATE_STANDBY,
	CHIPSET_STATE_ANY_OFF,
	CHIPSET_STATE_ANY_SUSPEND,
	CHIPSET_STATE_ANY_SUSPEND | CHIPSET_STATE_SOFT_OFF,
};

/** Test chipset_in_state() for each state */
ZTEST(power_common_no_tasks, test_power_chipset_in_state)
{
	bool expected_in_state;
	bool transition_from;
	bool transition_to;
	bool in_state;
	int mask;

	for (int i = 0; i < ARRAY_SIZE(test_power_state_desc); i++) {
		/* Set given power state */
		power_set_state(test_power_state_desc[i].p_state);
		/* Test with selected state masks */
		for (int j = 0; j < ARRAY_SIZE(in_state_test_masks); j++) {
			mask = in_state_test_masks[j];
			/*
			 * Currently tested mask match with state if it match
			 * with transition_to and from chipset states
			 */
			transition_to = mask &
					test_power_state_desc[i].transition_to;
			transition_from =
				mask & test_power_state_desc[i].transition_from;
			expected_in_state = transition_to && transition_from;
			in_state = chipset_in_state(mask);
			zassert_equal(expected_in_state, in_state,
				      "Wrong chipset_in_state() == %d, "
				      "should be %d; mask 0x%x; power state %d "
				      "in test case %d",
				      in_state, expected_in_state, mask,
				      test_power_state_desc[i].p_state, i);
		}
	}
}

/** Test chipset_in_or_transitioning_to_state() for each state */
ZTEST(power_common_no_tasks, test_power_chipset_in_or_transitioning_to_state)
{
	bool expected_in_state;
	bool in_state;
	int mask;

	for (int i = 0; i < ARRAY_SIZE(test_power_state_desc); i++) {
		/* Set given power state */
		power_set_state(test_power_state_desc[i].p_state);
		/* Test with selected state masks */
		for (int j = 0; j < ARRAY_SIZE(in_state_test_masks); j++) {
			mask = in_state_test_masks[j];
			/*
			 * Currently tested mask match with state if it match
			 * with transition_to chipset state
			 */
			expected_in_state =
				mask & test_power_state_desc[i].transition_to;
			in_state = chipset_in_or_transitioning_to_state(mask);
			zassert_equal(expected_in_state, in_state,
				      "Wrong "
				      "chipset_in_or_transitioning_to_state() "
				      "== %d, should be %d; mask 0x%x; "
				      "power state %d in test case %d",
				      in_state, expected_in_state, mask,
				      test_power_state_desc[i].p_state, i);
		}
	}
}

/* Test using chipset_exit_hard_off() in different power states. The only
 * way to test the value of want_g3_exit is to set the power state to G3
 * and then to see if test_power_common_state() transitions to G3S5 or not.
 */
ZTEST(power_common_no_tasks, test_power_exit_hard_off)
{
	/*
	 * Every test runs in a new thread, we need to add this thread to the
	 * dynamic shimmed tasks or this test will fail.
	 */
	set_test_runner_tid();

	/* Force initial state */
	power_set_state(POWER_G3);
	test_power_common_state();
	zassert_equal(POWER_G3, power_get_state());

	/* Test after exit hard off, we reach G3S5 */
	chipset_exit_hard_off();
	test_power_common_state();
	zassert_equal(POWER_G3S5, power_get_state());

	/* Go back to G3 and check we stay there */
	power_set_state(POWER_G3);
	test_power_common_state();
	zassert_equal(POWER_G3, power_get_state());

	/* Exit G3 again */
	chipset_exit_hard_off();
	test_power_common_state();
	zassert_equal(POWER_G3S5, power_get_state());

	/* Go to S5G3 */
	power_set_state(POWER_S5G3);
	test_power_common_state();
	zassert_equal(POWER_S5G3, power_get_state());

	/* Test exit hard off in S5G3 -- should set want_g3_exit */
	chipset_exit_hard_off();
	/* Go back to G3 and check we exit it to G3S5 */
	power_set_state(POWER_G3);
	test_power_common_state();
	zassert_equal(POWER_G3S5, power_get_state());

	/* Test exit hard off is cleared on entering S5 */
	chipset_exit_hard_off();
	power_set_state(POWER_S5);
	test_power_common_state();
	zassert_equal(POWER_S5, power_get_state());

	/* Go back to G3 and check we stay in G3 */
	power_set_state(POWER_G3);
	test_power_common_state();
	zassert_equal(POWER_G3, power_get_state());

	/* Test exit hard off doesn't work on other states */
	power_set_state(POWER_S5S3);
	test_power_common_state();
	zassert_equal(POWER_S5S3, power_get_state());
	chipset_exit_hard_off();
	test_power_common_state();

	/* Go back to G3 and check we stay in G3 */
	power_set_state(POWER_G3);
	test_power_common_state();
	zassert_equal(POWER_G3, power_get_state());
}

/* Test reboot ap on g3 host command is triggering reboot */
ZTEST(power_common_no_tasks, test_power_reboot_ap_at_g3)
{
	struct ec_params_reboot_ap_on_g3_v1 params;
	struct host_cmd_handler_args args = {
		.command = EC_CMD_REBOOT_AP_ON_G3,
		.version = 0,
		.send_response = stub_send_response_callback,
		.params = &params,
		.params_size = sizeof(params),
	};
	int delay_ms;
	int64_t before_time;

	/*
	 * Every test runs in a new thread, we need to add this thread to the
	 * dynamic shimmed tasks or this test will fail.
	 */
	set_test_runner_tid();

	/* Force initial state S0 */
	power_set_state(POWER_S0);
	test_power_common_state();
	zassert_equal(POWER_S0, power_get_state());

	/* Test version 0 (no delay argument) */
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	/* Go to G3 and check if reboot is triggered */
	power_set_state(POWER_G3);
	test_power_common_state();
	zassert_equal(POWER_G3S5, power_get_state());

	/* Test version 1 (with delay argument) */
	args.version = 1;
	delay_ms = 3000;
	params.reboot_ap_at_g3_delay = delay_ms / 1000; /* in seconds */
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	/* Go to G3 and check if reboot is triggered after delay */
	power_set_state(POWER_G3);
	before_time = k_uptime_get();
	test_power_common_state();
	zassert_true(k_uptime_delta(&before_time) >= 3000);
	zassert_equal(POWER_G3S5, power_get_state());
}

/** Test setting cutoff and stay-up battery levels through host command */
ZTEST(power_common, test_power_hc_smart_discharge)
{
	struct ec_response_smart_discharge response;
	struct ec_params_smart_discharge params;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params);
	const struct emul *emul = EMUL_DT_GET(BATTERY_NODE);
	struct i2c_common_emul_data *common_data =
		emul_smart_battery_get_i2c_common_data(emul);
	int hours_to_zero;
	int hibern_drate;
	int cutoff_drate;
	int stayup_cap;
	int cutoff_cap;

	/* Set up host command parameters */
	params.flags = EC_SMART_DISCHARGE_FLAGS_SET;

	/* Test fail when battery capacity is not available */
	i2c_common_emul_set_read_fail_reg(common_data, SB_FULL_CHARGE_CAPACITY);
	zassert_equal(EC_RES_UNAVAILABLE, host_command_process(&args));
	i2c_common_emul_set_read_fail_reg(common_data,
					  I2C_COMMON_EMUL_NO_FAIL_REG);

	/* Setup discharge rates */
	params.drate.hibern = 10;
	params.drate.cutoff = 100;
	/* Test fail on higher discahrge in hibernation than cutoff */
	zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&args));

	/* Setup discharge rates */
	params.drate.hibern = 10;
	params.drate.cutoff = 0;
	/* Test fail on only one discharge rate set to 0 */
	zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&args));

	/* Setup correct parameters */
	hours_to_zero = 1000;
	hibern_drate = 100; /* uA */
	cutoff_drate = 10; /* uA */
	/* Need at least 100 mA capacity to stay 1000h using 0.1mAh */
	stayup_cap = hibern_drate * hours_to_zero / 1000;
	/* Need at least 10 mA capacity to stay 1000h using 0.01mAh */
	cutoff_cap = cutoff_drate * hours_to_zero / 1000;

	params.drate.hibern = hibern_drate;
	params.drate.cutoff = cutoff_drate;
	params.hours_to_zero = hours_to_zero;

	/* Test if correct values are set */
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));
	zassert_equal(hibern_drate, response.drate.hibern);
	zassert_equal(cutoff_drate, response.drate.cutoff);
	zassert_equal(hours_to_zero, response.hours_to_zero);
	zassert_equal(stayup_cap, response.dzone.stayup);
	zassert_equal(cutoff_cap, response.dzone.cutoff);

	/* Setup discharge rate to 0 */
	params.drate.hibern = 0;
	params.drate.cutoff = 0;
	/* Update hours to zero */
	hours_to_zero = 2000;
	params.hours_to_zero = hours_to_zero;
	/* Need at least 200 mA capacity to stay 2000h using 0.1mAh */
	stayup_cap = hibern_drate * hours_to_zero / 1000;
	/* Need at least 20 mA capacity to stay 2000h using 0.01mAh */
	cutoff_cap = cutoff_drate * hours_to_zero / 1000;

	/* Test that command doesn't change drate but apply new hours to zero */
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));
	zassert_equal(hibern_drate, response.drate.hibern);
	zassert_equal(cutoff_drate, response.drate.cutoff);
	zassert_equal(hours_to_zero, response.hours_to_zero);
	zassert_equal(stayup_cap, response.dzone.stayup);
	zassert_equal(cutoff_cap, response.dzone.cutoff);

	/* Setup any parameters != 0 */
	params.drate.hibern = 1000;
	params.drate.cutoff = 1000;
	/* Clear set flag */
	params.flags = 0;

	/* Test that command doesn't change drate and dzone */
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));
	zassert_equal(hibern_drate, response.drate.hibern);
	zassert_equal(cutoff_drate, response.drate.cutoff);
	zassert_equal(hours_to_zero, response.hours_to_zero);
	zassert_equal(stayup_cap, response.dzone.stayup);
	zassert_equal(cutoff_cap, response.dzone.cutoff);
}

/**
 * Test if default board_system_is_idle() recognize cutoff and stay-up
 * levels correctly.
 */
ZTEST(power_common, test_power_board_system_is_idle)
{
	struct ec_response_smart_discharge response;
	struct ec_params_smart_discharge params;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params);
	struct sbat_emul_bat_data *bat;
	const struct emul *emul = EMUL_DT_GET(BATTERY_NODE);
	struct i2c_common_emul_data *common_data =
		emul_smart_battery_get_i2c_common_data(emul);
	uint64_t last_shutdown_time = 0;
	uint64_t target;
	uint64_t now;

	bat = sbat_emul_get_bat_data(emul);

	/* Set up host command parameters */
	params.drate.hibern = 100; /* uA */
	params.drate.cutoff = 10; /* uA */
	params.hours_to_zero = 1000; /* h */
	params.flags = EC_SMART_DISCHARGE_FLAGS_SET;
	/* Set stay-up and cutoff zones */
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	/* Test shutdown ignore is send when target time is in future */
	target = 1125;
	now = 1000;
	zassert_equal(CRITICAL_SHUTDOWN_IGNORE,
		      board_system_is_idle(last_shutdown_time, &target, now),
		      NULL);

	/* Set "now" time after target time */
	now = target + 30;

	/*
	 * Test hibernation is requested when battery remaining capacity
	 * is not available
	 */
	i2c_common_emul_set_read_fail_reg(common_data, SB_REMAINING_CAPACITY);
	zassert_equal(CRITICAL_SHUTDOWN_HIBERNATE,
		      board_system_is_idle(last_shutdown_time, &target, now),
		      NULL);
	i2c_common_emul_set_read_fail_reg(common_data,
					  I2C_COMMON_EMUL_NO_FAIL_REG);

	/* Setup remaining capacity to trigger cutoff */
	bat->cap = response.dzone.cutoff - 5;
	zassert_equal(CRITICAL_SHUTDOWN_CUTOFF,
		      board_system_is_idle(last_shutdown_time, &target, now),
		      NULL);

	/* Setup remaining capacity to trigger stay-up and ignore shutdown */
	bat->cap = response.dzone.stayup - 5;
	zassert_equal(CRITICAL_SHUTDOWN_IGNORE,
		      board_system_is_idle(last_shutdown_time, &target, now),
		      NULL);

	/* Setup remaining capacity to be in safe zone to hibernate */
	bat->cap = response.dzone.stayup + 5;
	zassert_equal(CRITICAL_SHUTDOWN_HIBERNATE,
		      board_system_is_idle(last_shutdown_time, &target, now),
		      NULL);
}

/**
 * Test power console command
 */
ZTEST(power_common, power_console_cmd)
{
	const char *buffer;
	size_t buffer_size;

	test_set_chipset_to_g3();
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power"),
		      NULL);
	buffer = shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
	zassert_true(strcmp(buffer, "\r\noff\r\n") == 0 ||
			     strcmp(buffer, "\r\nOFF\r\n") == 0,
		     "Invalid console output %s", buffer);

	test_set_chipset_to_s0();
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power"),
		      NULL);
	buffer = shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
	zassert_true(strcmp(buffer, "\r\non\r\n") == 0 ||
			     strcmp(buffer, "\r\nON\r\n") == 0,
		     "Invalid console output %s", buffer);

	zassert_equal(EC_ERROR_PARAM1,
		      shell_execute_cmd(get_ec_shell(), "power x"), NULL);

	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power on"),
		      NULL);

	zassert_equal(EC_SUCCESS,
		      shell_execute_cmd(get_ec_shell(), "power off"), NULL);
}

/**
 * Common setup for hibernation delay tests. Smart discharge zone is setup,
 * battery is set in safe zone (which trigger hibernation), power state is
 * set to G3 and AC is disabled. system_hibernate mock is reset.
 */
static void setup_hibernation_delay(void *state)
{
	struct ec_response_smart_discharge response;
	struct ec_params_smart_discharge params;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params);
	struct sbat_emul_bat_data *bat;
	const struct emul *emul = EMUL_DT_GET(BATTERY_NODE);
	ARG_UNUSED(state);

	bat = sbat_emul_get_bat_data(emul);

	/* Setup smart discharge zone and set capacity to safe zone */
	params.drate.hibern = 100; /* uA */
	params.drate.cutoff = 10; /* uA */
	params.hours_to_zero = 10000; /* h */
	params.flags = EC_SMART_DISCHARGE_FLAGS_SET;
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));
	/*
	 * Make sure that battery is in safe zone in good condition to
	 * not trigger hibernate in charge_state_v2.c
	 */
	bat->cap = response.dzone.stayup + 5;
	bat->volt = battery_get_info()->voltage_normal;

	/* Force initial state */
	test_set_chipset_to_g3();

	/* Disable AC */
	set_ac_enabled(false);

	RESET_FAKE(system_hibernate);
}

/** Test setting hibernation delay through host command */
ZTEST(power_common_hibernation, test_power_hc_hibernation_delay)
{
	struct ec_response_hibernation_delay response;
	struct ec_params_hibernation_delay params;
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
		EC_CMD_HIBERNATION_DELAY, 0, response, params);
	uint32_t h_delay;
	int sleep_time;

	/* Ensure the lid is closed so AC connect does not boot system */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "lidclose"));

	zassert_equal(power_get_state(), POWER_G3,
		      "Power state is %d, expected G3", power_get_state());
	/* This is a no-op, but it will reset the last_shutdown_time. */
	power_set_state(POWER_G3);

	/* Set hibernate delay */
	h_delay = 9;
	params.seconds = h_delay;
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	zassert_equal(0, response.time_g3, "Time from last G3 enter %d != 0",
		      response.time_g3);
	zassert_equal(h_delay, response.time_remaining,
		      "Time to hibernation %d != %d", response.time_remaining,
		      h_delay);
	zassert_equal(h_delay, response.hibernate_delay,
		      "Hibernation delay %d != %d", h_delay,
		      response.hibernate_delay);

	/* Kick chipset task to process new hibernation delay */
	task_wake(TASK_ID_CHIPSET);
	/* Wait some arbitrary time less than hibernate delay */
	sleep_time = 6;
	k_msleep(sleep_time * 1000);

	/* Get hibernate delay */
	params.seconds = 0;
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	zassert_equal(sleep_time, response.time_g3,
		      "Time from last G3 enter %d != %d", response.time_g3,
		      sleep_time);
	zassert_equal(h_delay - sleep_time, response.time_remaining,
		      "Time to hibernation %d != %d", response.time_remaining,
		      h_delay - sleep_time);
	zassert_equal(h_delay, response.hibernate_delay,
		      "Hibernation delay %d != %d", h_delay,
		      response.hibernate_delay);
	zassert_equal(0, system_hibernate_fake.call_count,
		      "system_hibernate() shouldn't be called before delay");

	/* Wait to end of the hibenate delay */
	k_msleep((h_delay - sleep_time) * 1000);

	/* Get hibernate delay */
	params.seconds = 0;
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	zassert_equal(h_delay, response.time_g3,
		      "Time from last G3 enter %d != %d", response.time_g3,
		      h_delay);
	zassert_equal(0, response.time_remaining, "Time to hibernation %d != 0",
		      response.time_remaining);
	zassert_equal(h_delay, response.hibernate_delay,
		      "Hibernation delay %d != %d", h_delay,
		      response.hibernate_delay);
	zassert_equal(1, system_hibernate_fake.call_count,
		      "system_hibernate() should be called after delay %d",
		      system_hibernate_fake.call_count);

	/* Wait some more time */
	k_msleep(2000);

	/* Get hibernate delay */
	params.seconds = 0;
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	/* After hibernation, remaining time shouldn't be negative */
	zassert_equal(0, response.time_remaining, "Time to hibernation %d != 0",
		      response.time_remaining);

	/* Enable AC */
	set_ac_enabled(true);

	/* Reset system_hibernate fake to check that it is not called on AC */
	RESET_FAKE(system_hibernate);
	/* Allow chipset task to spin with enabled AC */
	task_wake(TASK_ID_CHIPSET);
	k_msleep(1);

	/* Get hibernate delay */
	params.seconds = 0;
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	zassert_equal(0, response.time_g3,
		      "Time from last G3 enter %d should be 0 on AC",
		      response.time_g3);
	zassert_equal(0, system_hibernate_fake.call_count,
		      "system_hibernate() shouldn't be called on AC");

	/* Disable AC */
	set_ac_enabled(false);

	/* Go to different state */
	power_set_state(POWER_G3S5);
	zassert_equal(POWER_G3S5, power_get_state());

	/* Get hibernate delay */
	params.seconds = 0;
	zassert_equal(EC_RES_SUCCESS, host_command_process(&args));

	zassert_equal(0, response.time_g3,
		      "Time from last G3 enter %d should be 0 on state != G3",
		      response.time_g3);
}

/** Test setting hibernation delay through UART command */
ZTEST(power_common_hibernation, test_power_cmd_hibernation_delay)
{
	uint32_t h_delay;
	int sleep_time;

	zassert_equal(power_get_state(), POWER_G3,
		      "Power state is %d, expected G3", power_get_state());
	/* This is a no-op, but it will reset the last_shutdown_time. */
	power_set_state(POWER_G3);

	/* Test success on call without argument */
	zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "hibdelay"),
		      NULL);

	/* Test error on hibernation delay argument that is not a number */
	zassert_equal(EC_ERROR_PARAM1,
		      shell_execute_cmd(get_ec_shell(), "hibdelay test1"),
		      NULL);

	/* Set hibernate delay */
	h_delay = 3;
	zassert_equal(EC_SUCCESS,
		      shell_execute_cmd(get_ec_shell(), "hibdelay 3"), NULL);

	/* Kick chipset task to process new hibernation delay */
	task_wake(TASK_ID_CHIPSET);
	/* Wait some arbitrary time less than hibernate delay */
	sleep_time = 2;
	k_msleep(sleep_time * 1000);

	zassert_equal(0, system_hibernate_fake.call_count,
		      "system_hibernate() shouldn't be called before delay");

	/* Wait to end of the hibenate delay */
	k_msleep((h_delay - sleep_time) * 1000);

	zassert_equal(1, system_hibernate_fake.call_count,
		      "system_hibernate() should be called after delay %d",
		      system_hibernate_fake.call_count);
}

ZTEST_SUITE(power_common_no_tasks, drivers_predicate_pre_main, NULL, NULL, NULL,
	    NULL);

ZTEST_SUITE(power_common, drivers_predicate_post_main, NULL, NULL, NULL, NULL);

ZTEST_SUITE(power_common_hibernation, drivers_predicate_post_main, NULL,
	    setup_hibernation_delay, NULL, NULL);