summaryrefslogtreecommitdiff
path: root/zephyr/test/skyrim/tests/common/src/fan.c
blob: 49689382988101a1c4c2747d3ea94c7a3eb30294 (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
/* 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 <zephyr/devicetree.h>
#include <zephyr/fff.h>
#include <zephyr/ztest.h>

#include <cros_board_info.h>
#include <cros_cbi.h>
#include <fan.h>
#include <hooks.h>

FAKE_VOID_FUNC(fan_set_count, int);
FAKE_VALUE_FUNC(int, cros_cbi_get_fw_config, enum cbi_fw_config_field_id,
		uint32_t *);
FAKE_VALUE_FUNC(int, cbi_get_board_version, uint32_t *);

void fan_init(void);
bool board_supports_pcore_ocp(void);

static bool fan_present;
static int board_version;

static int cros_cbi_get_fw_config_mock(enum cbi_fw_config_field_id field_id,
				       uint32_t *value)
{
	if (field_id != FW_FAN)
		return -EINVAL;

	*value = fan_present ? FW_FAN_PRESENT : FW_FAN_NOT_PRESENT;
	return 0;
}

static int cbi_get_board_version_mock(uint32_t *value)
{
	*value = board_version;
	return EC_SUCCESS;
}

static void fan_before(void *fixture)
{
	ARG_UNUSED(fixture);
	RESET_FAKE(fan_set_count);
	RESET_FAKE(cros_cbi_get_fw_config);
	RESET_FAKE(cbi_get_board_version);

	cros_cbi_get_fw_config_fake.custom_fake = cros_cbi_get_fw_config_mock;
	cbi_get_board_version_fake.custom_fake = cbi_get_board_version_mock;
}

ZTEST_SUITE(fan, NULL, NULL, fan_before, NULL, NULL);

ZTEST(fan, board_supports_pcore_ocp)
{
	/* Only supported for board version > 3. */
	board_version = 2;
	zassert_false(board_supports_pcore_ocp());
	board_version = 3;
	zassert_false(board_supports_pcore_ocp());
	board_version = 4;
	zassert_true(board_supports_pcore_ocp());
}

ZTEST(fan, fan_init)
{
	/* Only disable fans on board version >= 3. */
	fan_present = false;
	board_version = 2;
	fan_init();
	zassert_equal(fan_set_count_fake.call_count, 0);

	fan_present = true;
	board_version = 3;
	fan_init();
	zassert_equal(fan_set_count_fake.call_count, 0);

	fan_present = true;
	board_version = 4;
	fan_init();
	zassert_equal(fan_set_count_fake.call_count, 0);

	fan_present = false;
	board_version = 3;
	fan_init();
	zassert_equal(fan_set_count_fake.call_count, 1);

	fan_present = false;
	board_version = 4;
	fan_init();
	zassert_equal(fan_set_count_fake.call_count, 2);
}