Skip to content

Commit

Permalink
fan: Split interval for actual/target duty
Browse files Browse the repository at this point in the history
Updating the actual duty is a detail of how the hardware control is
implemented, and requires a short interval for its implementation.

Updating the target duty is a platform policy (the fan table), and does
not require updating as frequently as the hardware control.

Signed-off-by: Tim Crawford <[email protected]>
  • Loading branch information
crawfxrd authored and jackpot51 committed Jan 7, 2025
1 parent 0e44603 commit 27f096c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
46 changes: 33 additions & 13 deletions src/board/system76/common/fan.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ static uint16_t fan_get_tach1_rpm(void) {
return rpm;
}

void fan_event(void) {
// Update the target duty of the fans based on system temps.
// Interval: 1sec
void fan_update_target(void) {
#if CONFIG_PLATFORM_INTEL
#if CONFIG_HAVE_DGPU
int16_t sys_temp = MAX(peci_temp, dgpu_temp);
Expand All @@ -209,19 +211,38 @@ void fan_event(void) {
int16_t sys_temp = 50;
#endif

// Fan update interval is 100ms (main.c). The event changes PWM duty
// by 1 every interval to give a smoothing effect.
// Set FAN1 target duty.
if (fan_max) {
fan1_pwm_target = CTR0;
} else if (power_state != POWER_STATE_S0) {
fan1_pwm_target = 0;
} else {
fan1_pwm_target = fan_get_duty(&FAN1, sys_temp);
}

// Enabling fan max toggle and exiting S0 will cause duty to immediately
// change instead of stepping to provide the desired effects.
#ifdef FAN2_PWM
// Set FAN2 target duty.
if (fan_max) {
fan2_pwm_target = CTR0;
} else if (power_state != POWER_STATE_S0) {
fan2_pwm_target = 0;
} else {
fan2_pwm_target = fan_get_duty(&FAN2, sys_temp);
}
#endif
}

// Set FAN1 duty
fan1_pwm_target = fan_get_duty(&FAN1, sys_temp);
// Update the actual duty of the fans to move towards the target duty.
// The duty is changed by 1 every interval to give a smoothing effect and to
// avoid large, sudden changes.
// Enabling fan max toggle and exiting S0 will cause duty to immediately
// change instead of stepping to provide the desired effects.
// Interval: 100ms
void fan_update_duty(void) {
// Move FAN1 duty towards target.
if (fan_max) {
fan1_pwm_target = CTR0;
fan1_pwm_actual = CTR0;
} else if (power_state != POWER_STATE_S0) {
fan1_pwm_target = 0;
fan1_pwm_actual = 0;
} else {
if (fan1_pwm_actual < fan1_pwm_target) {
Expand All @@ -242,16 +263,14 @@ void fan_event(void) {
}
TRACE("FAN1 duty=%d\n", fan1_pwm_actual);
FAN1_PWM = fan1_pwm_actual;
// Update RPM value for reporting to ACPI.
fan1_rpm = fan_get_tach0_rpm();

#ifdef FAN2_PWM
// set FAN2 duty
fan2_pwm_target = fan_get_duty(&FAN2, sys_temp);
// Move FAN2 duty towards target.
if (fan_max) {
fan2_pwm_target = CTR0;
fan2_pwm_actual = CTR0;
} else if (power_state != POWER_STATE_S0) {
fan2_pwm_target = 0;
fan2_pwm_actual = 0;
} else {
if (fan2_pwm_actual < fan2_pwm_target) {
Expand All @@ -272,6 +291,7 @@ void fan_event(void) {
}
TRACE("FAN2 duty=%d\n", fan2_pwm_actual);
FAN2_PWM = fan2_pwm_actual;
// Update RPM value for reporting to ACPI.
fan2_rpm = fan_get_tach1_rpm();
#endif
}
3 changes: 2 additions & 1 deletion src/board/system76/common/include/board/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern uint8_t fan2_pwm_target;
extern uint16_t fan2_rpm;

void fan_reset(void);
void fan_event(void);
void fan_update_duty(void);
void fan_update_target(void);

#endif // _BOARD_FAN_H
3 changes: 2 additions & 1 deletion src/board/system76/common/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void main(void) {
if ((time - last_time_100ms) >= INTERVAL_100MS) {
last_time_100ms = time;

fan_event();
fan_update_duty();
}

if ((time - last_time_250ms) >= INTERVAL_250MS) {
Expand All @@ -173,6 +173,7 @@ void main(void) {
last_time_1sec = time;

battery_event();
fan_update_target();
}

// Idle until next timer interrupt
Expand Down

0 comments on commit 27f096c

Please sign in to comment.