summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKornel Dulęba <korneld@google.com>2023-04-25 14:38:54 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-05-09 06:45:52 +0000
commit26b71ae693f63662de42d14c2d673c14b9c3e62f (patch)
tree94087d2b4fdf0f8f8c70800312f094b00c7c1371
parent4e47e1382ffa3f3f82291e62f0ba0315e6decbcf (diff)
downloadchrome-ec-26b71ae693f63662de42d14c2d673c14b9c3e62f.tar.gz
winterhold: Move fan duty logic to a separate file
This is needed for an upcoming fan test written using zephyr test suite. The test framework expects the board specific fan logic to be present in the fan.c file, move it there to make it happy. BUG=b:279493434 TEST=emerge-skyrim chromeos-zephyr TEST=./twister -i -T zephyr/test/skyrim/ Change-Id: I9d776dcf012f6c047eceebb45e4f76c40b17715f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4479371 Reviewed-by: Keith Short <keithshort@chromium.org> Reviewed-by: Diana Z <dzigterman@chromium.org> Reviewed-by: Robert Zieba <robertzieba@google.com> Commit-Queue: Kornel Dulęba <korneld@google.com> Tested-by: Kornel Dulęba <korneld@google.com>
-rw-r--r--zephyr/program/skyrim/winterhold/CMakeLists.txt3
-rw-r--r--zephyr/program/skyrim/winterhold/src/fan.c90
-rw-r--r--zephyr/program/skyrim/winterhold/src/thermal.c81
3 files changed, 93 insertions, 81 deletions
diff --git a/zephyr/program/skyrim/winterhold/CMakeLists.txt b/zephyr/program/skyrim/winterhold/CMakeLists.txt
index 0596978c05..632f11d8f1 100644
--- a/zephyr/program/skyrim/winterhold/CMakeLists.txt
+++ b/zephyr/program/skyrim/winterhold/CMakeLists.txt
@@ -12,3 +12,6 @@ zephyr_library_sources(
"src/alt_charger.c"
"src/thermal.c"
)
+
+zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_CUSTOM_FAN_DUTY_CONTROL
+ "src/fan.c")
diff --git a/zephyr/program/skyrim/winterhold/src/fan.c b/zephyr/program/skyrim/winterhold/src/fan.c
new file mode 100644
index 0000000000..a5061a660f
--- /dev/null
+++ b/zephyr/program/skyrim/winterhold/src/fan.c
@@ -0,0 +1,90 @@
+/* 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 "console.h"
+#include "fan.h"
+#include "math_util.h"
+#include "thermal.h"
+
+#include <zephyr/kernel.h>
+
+#define CPRINTS(format, args...) cprints(CC_THERMAL, format, ##args)
+#define CPRINTF(format, args...) cprintf(CC_THERMAL, format, ##args)
+
+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;
+}
diff --git a/zephyr/program/skyrim/winterhold/src/thermal.c b/zephyr/program/skyrim/winterhold/src/thermal.c
index 00e162e45a..0ccd9dd9e2 100644
--- a/zephyr/program/skyrim/winterhold/src/thermal.c
+++ b/zephyr/program/skyrim/winterhold/src/thermal.c
@@ -4,11 +4,9 @@
*/
#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"
@@ -211,82 +209,3 @@ static void detect_temp_change(void)
}
}
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