From a5a81dca4579961b9d78c7c85442d76b3c87dbbb Mon Sep 17 00:00:00 2001 From: Dawid Niedzwiecki Date: Wed, 17 Feb 2021 12:15:21 +0100 Subject: zephyr: add shim thermal support Add thermal support to Zephyr which includes handling temperature threshold for certain events. The thresholds are defined in the dts file as temperature sensor parameters - there are limits per temperature sensor. The struct ec_thermal_config thermal_params[] array with all parameters is generated automatically based on named_temp_sensors node. Enable "thermalget" and "thermalset" console commands along with EC_CMD_THERMAL_GET_THRESHOLD and EC_CMD_THERMAL_SET_THRESHOLD host commands. Also, add a few functions to utils needed to verify if an event occurred (falling/rising edge). BUG=b:179886912 BRANCH=none TEST=build Zephyr TEST=Run "thermalget" command to check current thresholds. Change the thresholds with "thermalset" to verify if AP throttling is requested. Signed-off-by: Dawid Niedzwiecki Change-Id: I5f527ceecab2427b3034fd8baa62bb8482f99ff3 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2698845 Reviewed-by: Simon Glass --- common/temp_sensor.c | 3 - common/thermal.c | 4 ++ zephyr/CMakeLists.txt | 1 + .../dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml | 2 +- .../dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml | 2 +- .../dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml | 2 +- .../dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml | 2 +- zephyr/dts/bindings/temp/temp_sensor.yaml | 79 ++++++++++++++++++++++ zephyr/dts/bindings/temp/thermistor.yaml | 8 +-- zephyr/shim/src/CMakeLists.txt | 3 +- zephyr/shim/src/thermal.c | 36 ++++++++++ zephyr/shim/src/util.c | 42 ++++++++++++ 12 files changed, 169 insertions(+), 15 deletions(-) create mode 100644 zephyr/dts/bindings/temp/temp_sensor.yaml create mode 100644 zephyr/shim/src/thermal.c diff --git a/common/temp_sensor.c b/common/temp_sensor.c index 8c72f1535c..55101a5345 100644 --- a/common/temp_sensor.c +++ b/common/temp_sensor.c @@ -117,8 +117,6 @@ int console_command_temps(int argc, char **argv) case EC_SUCCESS: ccprintf("%d K = %d C", t, K_TO_C(t)); #ifdef CONFIG_THROTTLE_AP -#ifndef CONFIG_ZEPHYR -/* TODO(b/179886912): Add thermal support */ if (thermal_params[i].temp_fan_off && thermal_params[i].temp_fan_max) ccprintf(" %d%%", @@ -126,7 +124,6 @@ int console_command_temps(int argc, char **argv) thermal_params[i].temp_fan_off, thermal_params[i].temp_fan_max, t)); -#endif /* CONFIG_ZEPHYR */ #endif ccprintf("\n"); break; diff --git a/common/thermal.c b/common/thermal.c index 821430482a..40577c0d43 100644 --- a/common/thermal.c +++ b/common/thermal.c @@ -19,6 +19,10 @@ #include "timer.h" #include "util.h" +#ifdef CONFIG_ZEPHYR +#include "temp_sensor/temp_sensor.h" +#endif + /* Console output macros */ #define CPUTS(outstr) cputs(CC_THERMAL, outstr) #define CPRINTS(format, args...) cprints(CC_THERMAL, format, ## args) diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 8a7359ec65..855bd1d753 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -256,6 +256,7 @@ zephyr_sources_ifdef(CONFIG_PLATFORM_EC_SPI_FLASH_REGS zephyr_sources_ifdef(CONFIG_PLATFORM_EC_TABLET_MODE "${PLATFORM_EC}/common/tablet_mode.c") zephyr_sources_ifdef(CONFIG_PLATFORM_EC_TEMP_SENSOR + "${PLATFORM_EC}/common/thermal.c" "${PLATFORM_EC}/common/temp_sensor.c") zephyr_sources_ifdef(CONFIG_PLATFORM_EC_THERMISTOR "${PLATFORM_EC}/driver/temp_sensor/thermistor.c") diff --git a/zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml index fb9061c33d..b2a3c4c88a 100644 --- a/zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml +++ b/zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml @@ -6,4 +6,4 @@ description: Thermistor 3v0-22k6-47k-4050b compatible: "temp-3v0-22k6-47k-4050b" -include: thermistor.yaml +include: [temp_sensor.yaml, thermistor.yaml] diff --git a/zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml index 325fcac8f1..7928fcd54a 100644 --- a/zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml +++ b/zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml @@ -6,4 +6,4 @@ description: Thermistor 3v3-13k7-47k-4050b compatible: "temp-3v3-13k7-47k-4050b" -include: thermistor.yaml +include: [temp_sensor.yaml, thermistor.yaml] diff --git a/zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml index 99ab1cc015..d8745e7abd 100644 --- a/zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml +++ b/zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml @@ -6,4 +6,4 @@ description: Thermistor 3v3-30k9-47k-4050b compatible: "temp-3v3-30k9-47k-4050b" -include: thermistor.yaml +include: [temp_sensor.yaml, thermistor.yaml] diff --git a/zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml index 6f54f98eb2..da53437c01 100644 --- a/zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml +++ b/zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml @@ -6,4 +6,4 @@ description: Thermistor 3v3-51k1-47k-4050b compatible: "temp-3v3-51k1-47k-4050b" -include: thermistor.yaml +include: [temp_sensor.yaml, thermistor.yaml] diff --git a/zephyr/dts/bindings/temp/temp_sensor.yaml b/zephyr/dts/bindings/temp/temp_sensor.yaml new file mode 100644 index 0000000000..b96e9c3262 --- /dev/null +++ b/zephyr/dts/bindings/temp/temp_sensor.yaml @@ -0,0 +1,79 @@ +# Copyright 2021 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. + +# Common properties for temperature sensors +# Zero values in degrees K(-273 in degrees C)in thermal thresholds will +# be ignored + +properties: + label: + required: true + type: string + description: + Human-readable string describing the device (used as + device_get_binding() argument) + + temp_fan_off: + required: false + type: int + default: -273 + description: + Temperature threshold in degrees C when no active cooling + is needed + + temp_fan_max: + required: false + type: int + default: -273 + description: + Temperature threshold in degrees C when max active cooling + is needed + + temp_host_warn: + required: false + type: int + default: -273 + description: + Temperature threshold in degrees C of thermal warn. + Temperatures above the thermal warn threshold generate a + request to the AP to throttle itself. + + temp_host_high: + required: false + type: int + default: -273 + description: + Temperature threshold in degrees C of thermal high. + Temperatures above the thermal high threshold cause the EC to + assert a signal (usually PROCHOT) to the AP and force the AP to + throttle. + + temp_host_halt: + required: false + type: int + default: -273 + description: + Temperature threshold in degrees C that forces AP to shutdown + due to thermal reason + + temp_host_release_warn: + required: false + type: int + default: -273 + description: + Temperature release threshold in degrees C of thermal warn + + temp_host_release_high: + required: false + type: int + default: -273 + description: + Temperature release threshold in degrees C of thermal high + + temp_host_release_halt: + required: false + type: int + default: -273 + description: + Temperature release threshold in degrees C of thermal shutdown diff --git a/zephyr/dts/bindings/temp/thermistor.yaml b/zephyr/dts/bindings/temp/thermistor.yaml index 6d53cdbff3..80503ce4df 100644 --- a/zephyr/dts/bindings/temp/thermistor.yaml +++ b/zephyr/dts/bindings/temp/thermistor.yaml @@ -2,15 +2,9 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -description: Thermistor properties +# Common properties for thermistors properties: - label: - required: true - type: string - description: - Human-readable string describing the device (used as - device_get_binding() argument) adc: required: true type: phandle diff --git a/zephyr/shim/src/CMakeLists.txt b/zephyr/shim/src/CMakeLists.txt index 38955f65d4..0311299074 100644 --- a/zephyr/shim/src/CMakeLists.txt +++ b/zephyr/shim/src/CMakeLists.txt @@ -27,7 +27,8 @@ zephyr_sources_ifdef(CONFIG_PLATFORM_EC_MPU mpu.c) zephyr_sources_ifdef(CONFIG_PLATFORM_EC_PANIC panic.c) zephyr_sources_ifdef(CONFIG_PLATFORM_EC_PWM pwm.c) zephyr_sources_ifdef(CONFIG_PLATFORM_EC_RTC rtc.c) -zephyr_sources_ifdef(CONFIG_PLATFORM_EC_TEMP_SENSOR temp_sensors.c) +zephyr_sources_ifdef(CONFIG_PLATFORM_EC_TEMP_SENSOR temp_sensors.c + thermal.c) zephyr_sources_ifdef(CONFIG_PLATFORM_EC_TIMER hwtimer.c) zephyr_sources_ifdef(CONFIG_PLATFORM_EC_I2C i2c.c) zephyr_sources_ifdef(CONFIG_SHIMMED_TASKS tasks.c) diff --git a/zephyr/shim/src/thermal.c b/zephyr/shim/src/thermal.c new file mode 100644 index 0000000000..2dd754b663 --- /dev/null +++ b/zephyr/shim/src/thermal.c @@ -0,0 +1,36 @@ +/* Copyright 2021 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 "temp_sensor.h" +#include "temp_sensor/temp_sensor.h" +#include "ec_commands.h" + +#define THERMAL_CONFIG(node_id) \ + [node_id] = { \ + .temp_host = { \ + [EC_TEMP_THRESH_WARN] = \ + C_TO_K(DT_PROP(node_id, temp_host_warn)), \ + [EC_TEMP_THRESH_HIGH] = \ + C_TO_K(DT_PROP(node_id, temp_host_high)), \ + [EC_TEMP_THRESH_HALT] = \ + C_TO_K(DT_PROP(node_id, temp_host_halt)), \ + }, \ + .temp_host_release = { \ + [EC_TEMP_THRESH_WARN] = C_TO_K( \ + DT_PROP(node_id, temp_host_release_warn)), \ + [EC_TEMP_THRESH_HIGH] = C_TO_K( \ + DT_PROP(node_id, temp_host_release_high)), \ + [EC_TEMP_THRESH_HALT] = C_TO_K( \ + DT_PROP(node_id, temp_host_release_halt)), \ + }, \ + .temp_fan_off = C_TO_K(DT_PROP(node_id, temp_fan_off)), \ + .temp_fan_max = C_TO_K(DT_PROP(node_id, temp_fan_max)), \ + }, + +struct ec_thermal_config thermal_params[] = { +#if DT_NODE_EXISTS(DT_PATH(named_temp_sensors)) + DT_FOREACH_CHILD(DT_PATH(named_temp_sensors), THERMAL_CONFIG) +#endif /* named_temp_sensors */ +}; diff --git a/zephyr/shim/src/util.c b/zephyr/shim/src/util.c index f915be9ad8..38a254bf47 100644 --- a/zephyr/shim/src/util.c +++ b/zephyr/shim/src/util.c @@ -226,3 +226,45 @@ char *strzcpy(char *dest, const char *src, int len) *d = '\0'; return dest; } + +/* stateful conditional stuff */ +enum cond_internal_bits { + COND_CURR_MASK = BIT(0), /* current value */ + COND_RISE_MASK = BIT(1), /* set if 0->1 */ + COND_FALL_MASK = BIT(2), /* set if 1->0 */ +}; + +void cond_set(cond_t *c, int val) +{ + if (val && cond_is(c, 0)) + *c |= COND_RISE_MASK; + else if (!val && cond_is(c, 1)) + *c |= COND_FALL_MASK; + if (val) + *c |= COND_CURR_MASK; + else + *c &= ~COND_CURR_MASK; +} + +int cond_went(cond_t *c, int val) +{ + int ret; + + if (val) { + ret = *c & COND_RISE_MASK; + *c &= ~COND_RISE_MASK; + } else { + ret = *c & COND_FALL_MASK; + *c &= ~COND_FALL_MASK; + } + + return ret; +} + +int cond_is(cond_t *c, int val) +{ + if (val) + return *c & COND_CURR_MASK; + else + return !(*c & COND_CURR_MASK); +} -- cgit v1.2.1