summaryrefslogtreecommitdiff
path: root/zephyr/program/skyrim/winterhold/src/thermal.c
blob: dcc0562fffecc8835cf6fa905f1bd7e55c91e0a5 (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
/* 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 "body_detection.h"
#include "hooks.h"
#include "host_command.h"
#include "lid_switch.h"
#include "temp_sensor/temp_sensor.h"
#include "thermal.h"

#define CPRINTS(format, args...) cprints(CC_THERMAL, format, ##args)
#define CPRINTF(format, args...) cprintf(CC_THERMAL, format, ##args)

#define TEMP_AMB TEMP_SENSOR_ID(DT_NODELABEL(temp_sensor_amb))

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define THERMAL_DESKTOP_LID_OPEN \
	{                        \
		.temp_host = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(44), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(105), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(110), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(40), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(95), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(100), \
		}, \
	}
__maybe_unused static const struct ec_thermal_config thermal_desktop_lid_open =
	THERMAL_DESKTOP_LID_OPEN;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define THERMAL_DESKTOP_LID_CLOSE \
	{                         \
		.temp_host = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(43), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(105), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(110), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(39), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(95), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(100), \
		},  \
	}
__maybe_unused static const struct ec_thermal_config thermal_desktop_lid_close =
	THERMAL_DESKTOP_LID_CLOSE;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define THERMAL_LAPTOP           \
	{                        \
		.temp_host = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(44), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(105), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(110), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(40), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(95), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(100), \
		}, \
	}
__maybe_unused static const struct ec_thermal_config thermal_laptop =
	THERMAL_LAPTOP;

static int last_amb_temp = -1;

/* Switch thermal table when mode change */
static void thermal_table_switch(void)
{
	enum body_detect_states body_state = body_detect_get_state();

	if (body_state == BODY_DETECTION_OFF_BODY) {
		if (lid_is_open()) {
			thermal_params[TEMP_AMB] = thermal_desktop_lid_open;
			CPRINTS("Thermal: Desktop lid open mode");
		} else {
			thermal_params[TEMP_AMB] = thermal_desktop_lid_close;
			CPRINTS("Thermal: Desktop lid close mode");
		}
	} else {
		thermal_params[TEMP_AMB] = thermal_laptop;
		CPRINTS("Thermal: Laptop mode");
	}
}
DECLARE_HOOK(HOOK_INIT, thermal_table_switch, HOOK_PRIO_DEFAULT);
DECLARE_HOOK(HOOK_LID_CHANGE, thermal_table_switch, HOOK_PRIO_DEFAULT);
DECLARE_HOOK(HOOK_BODY_DETECT_CHANGE, thermal_table_switch, HOOK_PRIO_DEFAULT);

/* Set SCI event to host for temperature change */
static void detect_temp_change(void)
{
	int t, rv;

	rv = temp_sensor_read(TEMP_AMB, &t);
	if (rv == EC_SUCCESS) {
		if (last_amb_temp != t) {
			last_amb_temp = t;
			host_set_single_event(EC_HOST_EVENT_THERMAL_THRESHOLD);
		}
	} else if (rv == EC_ERROR_INVAL) {
		CPRINTS("Temp sensor: Invalid id");
	}
}
DECLARE_HOOK(HOOK_SECOND, detect_temp_change, HOOK_PRIO_TEMP_SENSOR_DONE);