summaryrefslogtreecommitdiff
path: root/zephyr/program/skyrim/winterhold/src/thermal.c
blob: 00e162e45af85dfaeb9296d92a541bb0cacd360f (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
/* 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 "fan.h"
#include "hooks.h"
#include "host_command.h"
#include "lid_switch.h"
#include "math_util.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)

/*AMB sensor for thermal tabel control*/
#define TEMP_AMB TEMP_SENSOR_ID(DT_NODELABEL(temp_sensor_amb))
/*SOC and CPU sensor for fan tabel control*/
#define TEMP_SOC TEMP_SENSOR_ID(DT_NODELABEL(temp_sensor_soc))
#define TEMP_CPU TEMP_SENSOR_ID(DT_NODELABEL(temp_sensor_cpu))

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define THERMAL_DESKTOP_LID_OPEN \
	{                        \
		.temp_host = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(43), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(97), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(98), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(39), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(87), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(88), \
		}, \
	}
__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(97), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(98), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(39), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(87), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(88), \
		},  \
	}
__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(42), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(97), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(98), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_WARN] = C_TO_K(38), \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(87), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(88), \
		}, \
	}
__maybe_unused static const struct ec_thermal_config thermal_laptop =
	THERMAL_LAPTOP;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define FAN_SOC_DESKTOP_LID_OPEN \
	{                        \
		.temp_host = { \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(97), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(98), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(87), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(88), \
		}, \
		.temp_fan_off = C_TO_K(55), \
		.temp_fan_max = C_TO_K(72), \
	}
__maybe_unused static const struct ec_thermal_config fan_soc_desktop_lid_open =
	FAN_SOC_DESKTOP_LID_OPEN;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define FAN_SOC_DESKTOP_LID_CLOSE \
	{                         \
		.temp_host = { \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(97), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(98), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(87), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(88), \
		}, \
		.temp_fan_off = C_TO_K(55), \
		.temp_fan_max = C_TO_K(72),  \
	}
__maybe_unused static const struct ec_thermal_config fan_soc_desktop_lid_close =
	FAN_SOC_DESKTOP_LID_CLOSE;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define FAN_SOC_LAPTOP           \
	{                        \
		.temp_host = { \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(97), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(98), \
		}, \
		.temp_host_release = { \
			[EC_TEMP_THRESH_HIGH] = C_TO_K(87), \
			[EC_TEMP_THRESH_HALT] = C_TO_K(88), \
		}, \
		.temp_fan_off = C_TO_K(51), \
		.temp_fan_max = C_TO_K(68), \
	}
__maybe_unused static const struct ec_thermal_config fan_soc_laptop =
	FAN_SOC_LAPTOP;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define FAN_CPU_DESKTOP_LID_OPEN                                        \
	{                                                               \
		.temp_fan_off = C_TO_K(72), .temp_fan_max = C_TO_K(82), \
	}
__maybe_unused static const struct ec_thermal_config fan_cpu_desktop_lid_open =
	FAN_CPU_DESKTOP_LID_OPEN;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define FAN_CPU_DESKTOP_LID_CLOSE                                       \
	{                                                               \
		.temp_fan_off = C_TO_K(72), .temp_fan_max = C_TO_K(82), \
	}
__maybe_unused static const struct ec_thermal_config fan_cpu_desktop_lid_close =
	FAN_CPU_DESKTOP_LID_CLOSE;

/*
 * TODO(b/202062363): Remove when clang is fixed.
 */
#define FAN_CPU_LAPTOP                                                  \
	{                                                               \
		.temp_fan_off = C_TO_K(68), .temp_fan_max = C_TO_K(78), \
	}
__maybe_unused static const struct ec_thermal_config fan_cpu_laptop =
	FAN_CPU_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;
			thermal_params[TEMP_SOC] = fan_soc_desktop_lid_open;
			thermal_params[TEMP_CPU] = fan_cpu_desktop_lid_open;
			CPRINTS("Thermal: Desktop lid open mode");
		} else {
			thermal_params[TEMP_AMB] = thermal_desktop_lid_close;
			thermal_params[TEMP_SOC] = fan_soc_desktop_lid_close;
			thermal_params[TEMP_CPU] = fan_cpu_desktop_lid_close;
			CPRINTS("Thermal: Desktop lid close mode");
		}
	} else {
		thermal_params[TEMP_AMB] = thermal_laptop;
		thermal_params[TEMP_SOC] = fan_soc_laptop;
		thermal_params[TEMP_CPU] = fan_cpu_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);

#ifdef CONFIG_PLATFORM_EC_CUSTOM_FAN_DUTY_CONTROL

K_TIMER_DEFINE(grace_period_timer, NULL, NULL);

enum fan_status board_override_fan_control_duty(int ch)
{
	int duty, rpm_diff, deviation, duty_step;
	struct fan_data *data = &fan_data[ch];
	int rpm_actual = data->rpm_actual;
	int rpm_target = data->rpm_target;

	/* This works with one fan only. */
	if (ch != 0) {
		CPRINTS("Only FAN0 is supported!");
		return FAN_STATUS_FRUSTRATED;
	}

	/* Wait for fan RPM to catch up after its duty has been changed. */
	if (k_timer_remaining_ticks(&grace_period_timer) != 0)
		return FAN_STATUS_LOCKED;

	duty = fan_get_duty(ch);
	if (duty == 0 && rpm_target == 0)
		return FAN_STATUS_STOPPED;

	/*
	 * If the current RPM is close enough to the target just leave it.
	 * It's always going to fluctuate a bit anyway.
	 */
	deviation = fans[ch].rpm->rpm_deviation * rpm_target / 100;
	rpm_diff = rpm_target - rpm_actual;
	if (rpm_diff > deviation) {
		/* Can't set duty higher than 100%... */
		if (duty == 100)
			return FAN_STATUS_FRUSTRATED;
	} else if (rpm_diff < -deviation) {
		/* Can't set duty lower than 1%... */
		if (duty == 1 && rpm_target != 0)
			return FAN_STATUS_FRUSTRATED;
	} else {
		return FAN_STATUS_LOCKED;
	}

	/*
	 * The rpm_diff -> duty_step conversion is specific to a specific
	 * whiterun fan.
	 * It has been determined empirically.
	 */
	if (ABS(rpm_diff) >= 2500) {
		duty_step = 35;
		k_timer_start(&grace_period_timer, K_MSEC(800), K_NO_WAIT);
	} else if (ABS(rpm_diff) >= 2000) {
		duty_step = 28;
		k_timer_start(&grace_period_timer, K_MSEC(800), K_NO_WAIT);
	} else if (ABS(rpm_diff) >= 1000) {
		duty_step = 14;
		k_timer_start(&grace_period_timer, K_MSEC(800), K_NO_WAIT);
	} else if (ABS(rpm_diff) >= 500) {
		duty_step = 6;
		k_timer_start(&grace_period_timer, K_MSEC(800), K_NO_WAIT);
	} else if (ABS(rpm_diff) >= 250) {
		duty_step = 3;
		k_timer_start(&grace_period_timer, K_MSEC(600), K_NO_WAIT);
	} else {
		duty_step = 1;
		k_timer_start(&grace_period_timer, K_MSEC(600), K_NO_WAIT);
	}

	if (rpm_diff > 0)
		duty = MIN(duty + duty_step, 100);
	else
		duty = MAX(duty - duty_step, 1);

	fan_set_duty(ch, duty);

	return FAN_STATUS_CHANGING;
}
#endif