From 85b031d5eaea41be0754d38c05e1264462578c8b Mon Sep 17 00:00:00 2001 From: Winston Hoy Date: Sun, 17 Jan 2021 21:55:59 -0500 Subject: [PATCH 1/2] Sync dGPU and PECI fans (at highest requested duty) --- src/board/system76/common/dgpu.c | 13 +++++---- src/board/system76/common/fan.c | 28 +++++++++++++++++-- .../system76/common/include/board/dgpu.h | 2 +- src/board/system76/common/include/board/fan.h | 1 + .../system76/common/include/board/peci.h | 2 +- src/board/system76/common/main.c | 10 ++----- src/board/system76/common/peci.c | 8 ++---- src/board/system76/common/pwm.c | 2 +- src/board/system76/galp5/board.mk | 3 ++ 9 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/board/system76/common/dgpu.c b/src/board/system76/common/dgpu.c index c921f9abd..6bf727437 100644 --- a/src/board/system76/common/dgpu.c +++ b/src/board/system76/common/dgpu.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only #include +#include #if HAVE_DGPU @@ -61,7 +62,7 @@ void dgpu_init(void) { i2c_reset(&I2C_DGPU, true); } -void dgpu_event(void) { +uint8_t dgpu_get_fan_duty(void) { uint8_t duty; if (power_state == POWER_STATE_S0 && gpio_get(&DGPU_PWR_EN) && !gpio_get(&GC6_FB_EN)) { // Use I2CS if in S0 state @@ -91,16 +92,16 @@ void dgpu_event(void) { duty = fan_cooldown(&FAN, duty); } - if (duty != DCR4) { - DCR4 = duty; - DEBUG("DGPU temp=%d = %d\n", dgpu_temp, duty); - } + DEBUG("DGPU temp=%d\n", dgpu_temp); + return duty; } #else void dgpu_init(void) {} -void dgpu_event(void) {} +uint8_t dgpu_get_fan_duty(void) { + return PWM_DUTY(0); +} #endif // HAVE_DGPU diff --git a/src/board/system76/common/fan.c b/src/board/system76/common/fan.c index 8de859b72..ca8d9e7ba 100644 --- a/src/board/system76/common/fan.c +++ b/src/board/system76/common/fan.c @@ -1,9 +1,14 @@ // SPDX-License-Identifier: GPL-3.0-only #include +#include +#include bool fan_max = false; +#define max_speed PWM_DUTY(100) +#define min_speed PWM_DUTY(0) + void fan_reset(void) { // Do not manually set fans to maximum speed fan_max = false; @@ -21,7 +26,7 @@ uint8_t fan_duty(const struct Fan * fan, int16_t temp) __reentrant { } else if (temp < cur->temp) { // If lower than first temp, return 0% if (i == 0) { - return PWM_DUTY(0); + return min_speed; } else { const struct FanPoint * prev = &fan->points[i - 1]; @@ -43,7 +48,26 @@ uint8_t fan_duty(const struct Fan * fan, int16_t temp) __reentrant { } // If no point is found, return 100% - return PWM_DUTY(100); + return max_speed; +} + +void fan_duty_set(uint8_t peci_fan_duty, uint8_t dgpu_fan_duty) __reentrant { + #ifdef SYNC_FANS + peci_fan_duty = peci_fan_duty > dgpu_fan_duty ? peci_fan_duty : dgpu_fan_duty; + dgpu_fan_duty = peci_fan_duty > dgpu_fan_duty ? peci_fan_duty : dgpu_fan_duty; + #endif + + // set PECI fan duty + if (peci_fan_duty != DCR2) { + DCR2 = peci_fan_duty; + DEBUG("PECI fan_duty=%d\n", peci_fan_duty); + } + + // set dGPU fan duty + if (dgpu_fan_duty != DCR4) { + DCR4 = dgpu_fan_duty; + DEBUG("DGPU fan_duty=%d\n", dgpu_fan_duty); + } } uint8_t fan_heatup(const struct Fan * fan, uint8_t duty) __reentrant { diff --git a/src/board/system76/common/include/board/dgpu.h b/src/board/system76/common/include/board/dgpu.h index d20638877..c30574672 100644 --- a/src/board/system76/common/include/board/dgpu.h +++ b/src/board/system76/common/include/board/dgpu.h @@ -14,6 +14,6 @@ #endif // HAVE_DGPU void dgpu_init(void); -void dgpu_event(void); +uint8_t dgpu_get_fan_duty(void); #endif // _BOARD_DGPU_H diff --git a/src/board/system76/common/include/board/fan.h b/src/board/system76/common/include/board/fan.h index 9db05c8d7..c484622a3 100644 --- a/src/board/system76/common/include/board/fan.h +++ b/src/board/system76/common/include/board/fan.h @@ -28,6 +28,7 @@ extern bool fan_max; void fan_reset(void); uint8_t fan_duty(const struct Fan * fan, int16_t temp) __reentrant; +void fan_duty_set(uint8_t peci_fan_duty, uint8_t dgpu_fan_duty) __reentrant; uint8_t fan_heatup(const struct Fan * fan, uint8_t duty) __reentrant; uint8_t fan_cooldown(const struct Fan * fan, uint8_t duty) __reentrant; diff --git a/src/board/system76/common/include/board/peci.h b/src/board/system76/common/include/board/peci.h index dbef89c99..9e557535a 100644 --- a/src/board/system76/common/include/board/peci.h +++ b/src/board/system76/common/include/board/peci.h @@ -10,6 +10,6 @@ extern int16_t peci_temp; void peci_init(void); int peci_wr_pkg_config(uint8_t index, uint16_t param, uint32_t data); -void peci_event(void); +uint8_t peci_get_fan_duty(void); #endif // _BOARD_PECI_H diff --git a/src/board/system76/common/main.c b/src/board/system76/common/main.c index 80a3151d3..569a5762e 100644 --- a/src/board/system76/common/main.c +++ b/src/board/system76/common/main.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -118,13 +119,8 @@ void main(void) { if (last_time > time || (time - last_time) >= 1000) { last_time = time; - // Updates fan status and temps - peci_event(); - -#if HAVE_DGPU - // Updates discrete GPU fan status and temps - dgpu_event(); -#endif + // Update fan speeds + fan_duty_set(peci_get_fan_duty(), dgpu_get_fan_duty()); // Updates battery status battery_event(); diff --git a/src/board/system76/common/peci.c b/src/board/system76/common/peci.c index 1a47a6238..0a68a815d 100644 --- a/src/board/system76/common/peci.c +++ b/src/board/system76/common/peci.c @@ -119,7 +119,7 @@ int peci_wr_pkg_config(uint8_t index, uint16_t param, uint32_t data) { } // PECI information can be found here: https://www.intel.com/content/dam/www/public/us/en/documents/design-guides/core-i7-lga-2011-guide.pdf -void peci_event(void) { +uint8_t peci_get_fan_duty(void) { uint8_t duty; #if EC_ESPI @@ -180,8 +180,6 @@ void peci_event(void) { duty = fan_cooldown(&FAN, duty); } - if (duty != DCR2) { - DCR2 = duty; - DEBUG("PECI temp=%d = %d\n", peci_temp, duty); - } + DEBUG("PECI temp=%d\n", peci_temp); + return duty; } diff --git a/src/board/system76/common/pwm.c b/src/board/system76/common/pwm.c index 323ac3416..1e5a0fe6d 100644 --- a/src/board/system76/common/pwm.c +++ b/src/board/system76/common/pwm.c @@ -22,7 +22,7 @@ void pwm_init(void) { // Set cycle time to 255 + 1 CTR0 = 255; - // Turn off CPU fan (temperature control in peci_event) + // Turn off CPU fan (temperature control in peci_get_fan_duty) DCR2 = 0; // Enable PWM diff --git a/src/board/system76/galp5/board.mk b/src/board/system76/galp5/board.mk index 66d6ed323..d66b6206a 100644 --- a/src/board/system76/galp5/board.mk +++ b/src/board/system76/galp5/board.mk @@ -40,6 +40,9 @@ CFLAGS+=\ -DPOWER_LIMIT_AC=65 \ -DPOWER_LIMIT_DC=28 +# sync GPU fan speed to CPU fan speed (great for galp5 w/o dGPU) +CFLAGS+=-DSYNC_FANS=1 + # Custom fan curve CFLAGS+=-DBOARD_HEATUP=5 CFLAGS+=-DBOARD_COOLDOWN=20 From af6ec395b4d3594502c5b6d466edb962dd7ae863 Mon Sep 17 00:00:00 2001 From: Winston Hoy Date: Fri, 30 Apr 2021 11:24:19 -0400 Subject: [PATCH 2/2] default to fan syncing but support disabling fan syncing with SYNC_FANS=0 build flag --- src/board/system76/common/fan.c | 6 +++++- src/board/system76/galp5/board.mk | 3 --- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/board/system76/common/fan.c b/src/board/system76/common/fan.c index ca8d9e7ba..b996398c4 100644 --- a/src/board/system76/common/fan.c +++ b/src/board/system76/common/fan.c @@ -9,6 +9,10 @@ bool fan_max = false; #define max_speed PWM_DUTY(100) #define min_speed PWM_DUTY(0) +#ifndef SYNC_FANS + #define SYNC_FANS 1 +#endif + void fan_reset(void) { // Do not manually set fans to maximum speed fan_max = false; @@ -52,7 +56,7 @@ uint8_t fan_duty(const struct Fan * fan, int16_t temp) __reentrant { } void fan_duty_set(uint8_t peci_fan_duty, uint8_t dgpu_fan_duty) __reentrant { - #ifdef SYNC_FANS + #if SYNC_FANS != 0 peci_fan_duty = peci_fan_duty > dgpu_fan_duty ? peci_fan_duty : dgpu_fan_duty; dgpu_fan_duty = peci_fan_duty > dgpu_fan_duty ? peci_fan_duty : dgpu_fan_duty; #endif diff --git a/src/board/system76/galp5/board.mk b/src/board/system76/galp5/board.mk index d66b6206a..66d6ed323 100644 --- a/src/board/system76/galp5/board.mk +++ b/src/board/system76/galp5/board.mk @@ -40,9 +40,6 @@ CFLAGS+=\ -DPOWER_LIMIT_AC=65 \ -DPOWER_LIMIT_DC=28 -# sync GPU fan speed to CPU fan speed (great for galp5 w/o dGPU) -CFLAGS+=-DSYNC_FANS=1 - # Custom fan curve CFLAGS+=-DBOARD_HEATUP=5 CFLAGS+=-DBOARD_COOLDOWN=20