summaryrefslogtreecommitdiff
path: root/board/morphius/thermal.c
diff options
context:
space:
mode:
authorZick Wei <zick.wei@quanta.corp-partner.google.com>2020-07-30 12:05:50 +0800
committerCommit Bot <commit-bot@chromium.org>2020-08-03 22:16:17 +0000
commit9b818b27935d5c309067bf85108768224abd585d (patch)
treee380a537ae1863e9174f9fe3b5d413fe246baedd /board/morphius/thermal.c
parentce93a01527aa9214b6b023f4543209dd7612b80a (diff)
downloadchrome-ec-9b818b27935d5c309067bf85108768224abd585d.tar.gz
morphius: update fan curve table
This patch include: 1. Set different fan curve table by fan id and lid angle. 2. Add CPU temperature average and trigger debounce to avoid fan spin peak. BUG=b:162325433, b:147312313 BRANCH=none TEST=verify fan follow fan curve Signed-off-by: Zick Wei <zick.wei@quanta.corp-partner.google.com> Change-Id: I8c5ce458f3184aa45dbcecc5c05a419b0ce33f91 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2328564 Reviewed-by: Edward Hill <ecgh@chromium.org> Commit-Queue: Edward Hill <ecgh@chromium.org>
Diffstat (limited to 'board/morphius/thermal.c')
-rw-r--r--board/morphius/thermal.c489
1 files changed, 489 insertions, 0 deletions
diff --git a/board/morphius/thermal.c b/board/morphius/thermal.c
new file mode 100644
index 0000000000..36ff96e2ff
--- /dev/null
+++ b/board/morphius/thermal.c
@@ -0,0 +1,489 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "chipset.h"
+#include "common.h"
+#include "console.h"
+#include "extpower.h"
+#include "fan.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "lid_switch.h"
+#include "motion_lid.h"
+#include "tablet_mode.h"
+#include "temp_sensor.h"
+#include "thermal.h"
+#include "util.h"
+
+/* Console output macros */
+#define CPUTS(outstr) cputs(CC_THERMAL, outstr)
+#define CPRINTS(format, args...) cprints(CC_THERMAL, format, ## args)
+
+struct fan_step {
+ /*
+ * Sensor 1~4 trigger point, set -1 if we're not using this
+ * sensor to determine fan speed.
+ */
+ int8_t on[TEMP_SENSOR_COUNT];
+
+ /*
+ * Sensor 1~4 trigger point, set -1 if we're not using this
+ * sensor to determine fan speed.
+ */
+ int8_t off[TEMP_SENSOR_COUNT];
+
+ /* Fan 1~2 rpm */
+ uint16_t rpm[FAN_CH_COUNT];
+};
+
+static const struct fan_step *fan_step_table;
+
+static const struct fan_step fan1_table_clamshell[] = {
+ {
+ /* level 0 */
+ .on = {-1, -1, -1, -1},
+ .off = {-1, -1, -1, -1},
+ .rpm = {0},
+ },
+ {
+ /* level 1 */
+ .on = {-1, -1, 40, -1},
+ .off = {-1, -1, 31, -1},
+ .rpm = {1900},
+ },
+ {
+ /* level 2 */
+ .on = {-1, -1, 47, -1},
+ .off = {-1, -1, 45, -1},
+ .rpm = {2600},
+ },
+ {
+ /* level 3 */
+ .on = {-1, -1, 50, -1},
+ .off = {-1, -1, 48, -1},
+ .rpm = {3400},
+ },
+ {
+ /* level 4 */
+ .on = {-1, -1, 53, -1},
+ .off = {-1, -1, 51, -1},
+ .rpm = {3800},
+ },
+ {
+ /* level 5 */
+ .on = {-1, -1, 56, -1},
+ .off = {-1, -1, 54, -1},
+ .rpm = {4200},
+ },
+ {
+ /* level 6 */
+ .on = {-1, -1, 59, -1},
+ .off = {-1, -1, 57, -1},
+ .rpm = {4500},
+ },
+ {
+ /* level 7 */
+ .on = {-1, -1, 62, -1},
+ .off = {-1, -1, 60, -1},
+ .rpm = {4850},
+ },
+};
+
+static const struct fan_step fan1_table_tablet[] = {
+ {
+ /* level 0 */
+ .on = {-1, -1, -1, -1},
+ .off = {-1, -1, -1, -1},
+ .rpm = {0},
+ },
+ {
+ /* level 1 */
+ .on = {-1, -1, 45, -1},
+ .off = {-1, -1, 31, -1},
+ .rpm = {1900},
+ },
+ {
+ /* level 2 */
+ .on = {-1, -1, 48, -1},
+ .off = {-1, -1, 46, -1},
+ .rpm = {2600},
+ },
+ {
+ /* level 3 */
+ .on = {-1, -1, 52, -1},
+ .off = {-1, -1, 49, -1},
+ .rpm = {3000},
+ },
+ {
+ /* level 4 */
+ .on = {-1, -1, 56, -1},
+ .off = {-1, -1, 53, -1},
+ .rpm = {3400},
+ },
+ {
+ /* level 5 */
+ .on = {-1, -1, 60, -1},
+ .off = {-1, -1, 57, -1},
+ .rpm = {3750},
+ },
+ {
+ /* level 6 */
+ .on = {-1, -1, 64, -1},
+ .off = {-1, -1, 61, -1},
+ .rpm = {4000},
+ },
+ {
+ /* level 7 */
+ .on = {-1, -1, 68, -1},
+ .off = {-1, -1, 65, -1},
+ .rpm = {4300},
+ },
+};
+
+static const struct fan_step fan1_table_stand[] = {
+ {
+ /* level 0 */
+ .on = {-1, -1, -1, -1},
+ .off = {-1, -1, -1, -1},
+ .rpm = {0},
+ },
+ {
+ /* level 1 */
+ .on = {-1, -1, 38, -1},
+ .off = {-1, -1, 31, -1},
+ .rpm = {1850},
+ },
+ {
+ /* level 2 */
+ .on = {-1, -1, 45, -1},
+ .off = {-1, -1, 42, -1},
+ .rpm = {2550},
+ },
+ {
+ /* level 3 */
+ .on = {-1, -1, 49, -1},
+ .off = {-1, -1, 46, -1},
+ .rpm = {3000},
+ },
+ {
+ /* level 4 */
+ .on = {-1, -1, 53, -1},
+ .off = {-1, -1, 50, -1},
+ .rpm = {3450},
+ },
+ {
+ /* level 5 */
+ .on = {-1, -1, 57, -1},
+ .off = {-1, -1, 54, -1},
+ .rpm = {3800},
+ },
+ {
+ /* level 6 */
+ .on = {-1, -1, 61, -1},
+ .off = {-1, -1, 58, -1},
+ .rpm = {4050},
+ },
+ {
+ /* level 7 */
+ .on = {-1, -1, 65, -1},
+ .off = {-1, -1, 62, -1},
+ .rpm = {4400},
+ },
+};
+
+static const struct fan_step fan0_table_clamshell[] = {
+ {
+ /* level 0 */
+ .on = {-1, -1, -1, -1},
+ .off = {-1, -1, -1, -1},
+ .rpm = {0},
+ },
+ {
+ /* level 1 */
+ .on = {-1, -1, 40, -1},
+ .off = {-1, -1, 31, -1},
+ .rpm = {2200},
+ },
+ {
+ /* level 2 */
+ .on = {-1, -1, 47, -1},
+ .off = {-1, -1, 45, -1},
+ .rpm = {3000},
+ },
+ {
+ /* level 3 */
+ .on = {-1, -1, 50, -1},
+ .off = {-1, -1, 48, -1},
+ .rpm = {3350},
+ },
+ {
+ /* level 4 */
+ .on = {-1, -1, 53, -1},
+ .off = {-1, -1, 51, -1},
+ .rpm = {4000},
+ },
+ {
+ /* level 5 */
+ .on = {-1, -1, 56, -1},
+ .off = {-1, -1, 54, -1},
+ .rpm = {4450},
+ },
+ {
+ /* level 6 */
+ .on = {-1, -1, 59, -1},
+ .off = {-1, -1, 57, -1},
+ .rpm = {4750},
+ },
+ {
+ /* level 7 */
+ .on = {-1, -1, 62, -1},
+ .off = {-1, -1, 60, -1},
+ .rpm = {5000},
+ },
+};
+
+static const struct fan_step fan0_table_tablet[] = {
+ {
+ /* level 0 */
+ .on = {-1, -1, -1, -1},
+ .off = {-1, -1, -1, -1},
+ .rpm = {0},
+ },
+ {
+ /* level 1 */
+ .on = {-1, -1, 45, -1},
+ .off = {-1, -1, 31, -1},
+ .rpm = {2200},
+ },
+ {
+ /* level 2 */
+ .on = {-1, -1, 48, -1},
+ .off = {-1, -1, 46, -1},
+ .rpm = {2850},
+ },
+ {
+ /* level 3 */
+ .on = {-1, -1, 52, -1},
+ .off = {-1, -1, 49, -1},
+ .rpm = {3200},
+ },
+ {
+ /* level 4 */
+ .on = {-1, -1, 56, -1},
+ .off = {-1, -1, 53, -1},
+ .rpm = {3600},
+ },
+ {
+ /* level 5 */
+ .on = {-1, -1, 60, -1},
+ .off = {-1, -1, 57, -1},
+ .rpm = {4000},
+ },
+ {
+ /* level 6 */
+ .on = {-1, -1, 64, -1},
+ .off = {-1, -1, 61, -1},
+ .rpm = {4300},
+ },
+ {
+ /* level 7 */
+ .on = {-1, -1, 68, -1},
+ .off = {-1, -1, 65, -1},
+ .rpm = {4550},
+ },
+};
+
+static const struct fan_step fan0_table_stand[] = {
+ {
+ /* level 0 */
+ .on = {-1, -1, -1, -1},
+ .off = {-1, -1, -1, -1},
+ .rpm = {0},
+ },
+ {
+ /* level 1 */
+ .on = {-1, -1, 38, -1},
+ .off = {-1, -1, 31, -1},
+ .rpm = {2150},
+ },
+ {
+ /* level 2 */
+ .on = {-1, -1, 45, -1},
+ .off = {-1, -1, 42, -1},
+ .rpm = {2900},
+ },
+ {
+ /* level 3 */
+ .on = {-1, -1, 49, -1},
+ .off = {-1, -1, 46, -1},
+ .rpm = {3150},
+ },
+ {
+ /* level 4 */
+ .on = {-1, -1, 53, -1},
+ .off = {-1, -1, 50, -1},
+ .rpm = {3600},
+ },
+ {
+ /* level 5 */
+ .on = {-1, -1, 57, -1},
+ .off = {-1, -1, 54, -1},
+ .rpm = {3900},
+ },
+ {
+ /* level 6 */
+ .on = {-1, -1, 61, -1},
+ .off = {-1, -1, 58, -1},
+ .rpm = {4150},
+ },
+ {
+ /* level 7 */
+ .on = {-1, -1, 65, -1},
+ .off = {-1, -1, 62, -1},
+ .rpm = {4450},
+ },
+};
+
+#define NUM_FAN_LEVELS ARRAY_SIZE(fan1_table_clamshell)
+
+#define lid_angle_tablet 340
+
+BUILD_ASSERT(ARRAY_SIZE(fan1_table_clamshell) ==
+ ARRAY_SIZE(fan1_table_tablet));
+
+#define average_time 60
+int fan_table_to_rpm(int fan, int *temp)
+{
+ static int current_level;
+ static int avg_tmp[TEMP_SENSOR_COUNT];
+ static int avg_calc_tmp[TEMP_SENSOR_COUNT][average_time];
+ static int prev_tmp[TEMP_SENSOR_COUNT];
+ static int new_rpm;
+ int i, j, avg_sum = 0;
+ int lid_angle = motion_lid_get_angle();
+ static int fan_up_count, fan_down_count;
+ static int temp_count;
+
+ /*
+ * Select different fan curve table
+ * by mode: clamshell, tent/stand, tablet and fan id
+ */
+ if (tablet_get_mode()) {
+ if (gpio_get_level(GPIO_FAN_ID))
+ fan_step_table = fan1_table_stand;
+ else
+ fan_step_table = fan0_table_stand;
+
+ if (lid_angle >= lid_angle_tablet) {
+ if (gpio_get_level(GPIO_FAN_ID))
+ fan_step_table = fan1_table_tablet;
+ else
+ fan_step_table = fan0_table_tablet;
+ }
+ } else {
+ if (gpio_get_level(GPIO_FAN_ID))
+ fan_step_table = fan1_table_clamshell;
+ else
+ fan_step_table = fan0_table_clamshell;
+ }
+
+ /*
+ * Average temp 60 sec timing average
+ */
+ if (temp_count < average_time) {
+ avg_calc_tmp[TEMP_SENSOR_CPU][temp_count] =
+ temp[TEMP_SENSOR_CPU];
+ temp_count++;
+ } else
+ temp_count = 0;
+
+ for (j = 0; j < average_time; j++)
+ avg_sum = avg_sum + avg_calc_tmp[TEMP_SENSOR_CPU][j];
+
+ avg_tmp[TEMP_SENSOR_CPU] = avg_sum/average_time;
+
+ /*
+ * Compare the current and previous temperature, we have
+ * the three paths :
+ * 1. decreasing path. (check the release point)
+ * 2. increasing path. (check the trigger point)
+ * 3. invariant path. (return the current RPM)
+ */
+ if (avg_tmp[TEMP_SENSOR_CPU] < prev_tmp[TEMP_SENSOR_CPU]) {
+ for (i = current_level; i >= 0; i--) {
+ if (avg_tmp[TEMP_SENSOR_CPU] <
+ fan_step_table[i].off[TEMP_SENSOR_CPU]) {
+ /*
+ * fan step down debounce
+ */
+ if (fan_down_count < 10) {
+ fan_down_count++;
+ fan_up_count = 0;
+
+ return new_rpm;
+ }
+ fan_down_count = 0;
+ fan_up_count = 0;
+
+ current_level = i - 1;
+ } else
+ break;
+ }
+ } else if (avg_tmp[TEMP_SENSOR_CPU] > prev_tmp[TEMP_SENSOR_CPU]) {
+ for (i = current_level+1; i < NUM_FAN_LEVELS; i++) {
+ if ((avg_tmp[TEMP_SENSOR_CPU] >
+ fan_step_table[i].on[TEMP_SENSOR_CPU])) {
+ /*
+ * fan step up debounce
+ */
+ if (fan_up_count < 10) {
+ fan_up_count++;
+ fan_down_count = 0;
+
+ return new_rpm;
+ }
+ fan_down_count = 0;
+ fan_up_count = 0;
+
+ current_level = i;
+ } else
+ break;
+ }
+ } else {
+ fan_down_count = 0;
+ fan_up_count = 0;
+ }
+
+ if (current_level < 1)
+ current_level = 1;
+
+ if (current_level >= 7)
+ current_level = 7;
+
+ for (i = 0; i < TEMP_SENSOR_COUNT; ++i)
+ prev_tmp[i] = avg_tmp[i];
+
+ ASSERT(current_level < NUM_FAN_LEVELS);
+
+ switch (fan) {
+ case FAN_CH_0:
+ new_rpm = fan_step_table[current_level].rpm[FAN_CH_0];
+ break;
+ default:
+ break;
+ }
+
+ return new_rpm;
+}
+
+void board_override_fan_control(int fan, int *tmp)
+{
+ if (chipset_in_state(CHIPSET_STATE_ON |
+ CHIPSET_STATE_ANY_SUSPEND)) {
+ fan_set_rpm_mode(FAN_CH(fan), 1);
+ fan_set_rpm_target(FAN_CH(fan),
+ fan_table_to_rpm(fan, tmp));
+ }
+}