Skip to content

Commit

Permalink
pwm: actually use the correct DCR registers for each fan
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Kopeć <[email protected]>
  • Loading branch information
mkopec committed Oct 17, 2024
1 parent 220e5f9 commit 582481d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
13 changes: 2 additions & 11 deletions src/board/system76/common/fan.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@

#define PWM_REG(x) concat(DCR, x)

#ifndef CPU_FAN1
#define CPU_FAN1 2
#endif

// Only V5x0TNx is different
#ifndef GPU_FAN1
#define GPU_FAN1 4
#endif

bool fan_max = false;
uint8_t last_duty_dgpu = 0;
uint8_t last_duty_peci = 0;
Expand Down Expand Up @@ -81,7 +72,7 @@ void fan_duty_set(uint8_t peci_fan_duty, uint8_t dgpu_fan_duty) __reentrant {
#endif

// set PECI fan duty
if (peci_fan_duty != DCR2) {
if (peci_fan_duty != PWM_REG(CPU_FAN1)) {
TRACE("PECI fan_duty_raw=%d\n", peci_fan_duty);
last_duty_peci = peci_fan_duty = fan_smooth(last_duty_peci, peci_fan_duty);
PWM_REG(CPU_FAN1) = fan_max ? MAX_FAN_SPEED : peci_fan_duty;
Expand All @@ -92,7 +83,7 @@ void fan_duty_set(uint8_t peci_fan_duty, uint8_t dgpu_fan_duty) __reentrant {
}

// set dGPU fan duty
if (dgpu_fan_duty != DCR4) {
if (dgpu_fan_duty != PWM_REG(GPU_FAN1)) {
TRACE("DGPU fan_duty_raw=%d\n", dgpu_fan_duty);
last_duty_dgpu = dgpu_fan_duty = fan_smooth(last_duty_dgpu, dgpu_fan_duty);
PWM_REG(GPU_FAN1) = fan_max ? MAX_FAN_SPEED : dgpu_fan_duty;
Expand Down
9 changes: 9 additions & 0 deletions src/board/system76/common/include/board/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
#define SMOOTH_FANS_MIN 0 // default to smoothing all fan speed changes
#endif

#ifndef CPU_FAN1
#define CPU_FAN1 2
#endif

// Only V5x0TNx is different
#ifndef GPU_FAN1
#define GPU_FAN1 4
#endif

struct FanPoint {
int16_t temp;
uint8_t duty;
Expand Down
8 changes: 6 additions & 2 deletions src/board/system76/common/pwm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only

#include <board/fan.h>
#include <board/pwm.h>
#include <common/macro.h>

Expand All @@ -24,9 +25,12 @@ void pwm_init(void) {
CTR0 = 255;

// Turn off CPU fan (temperature control in peci_get_fan_duty)
DCR2 = 0;
PWM_REG(CPU_FAN1) = 0;
#if HAVE_CPU_FAN2
DCR3 = 0;
PWM_REG(CPU_FAN2) = 0;
#endif
#if HAVE_DGPU
PWM_REG(GPU_FAN1) = 0;
#endif

#if CONFIG_EC_ITE_IT5570E
Expand Down
6 changes: 3 additions & 3 deletions src/board/system76/common/scratch.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ uint8_t __code __at(SCRATCH_OFFSET) scratch_rom[] = {
// Enter or exit scratch ROM
void scratch_trampoline(void) {
// Set fans to 100%
DCR2 = 0xFF;
PWM_REG(CPU_FAN1) = 0xFF;
#if HAVE_CPU_FAN2
DCR3 = 0xFF;
PWM_REG(CPU_FAN2) = 0xFF;
#endif
#if HAVE_DGPU
DCR4 = 0xFF;
PWM_REG(GPU_FAN1) = 0xFF;
#endif

//TODO: Clear keyboard presses
Expand Down
10 changes: 5 additions & 5 deletions src/board/system76/common/smfi.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ static enum Result cmd_fan_get(void) {
switch (smfi_cmd[SMFI_CMD_DATA]) {
case 0:
// Get duty of fan 0
smfi_cmd[SMFI_CMD_DATA + 1] = DCR2;
smfi_cmd[SMFI_CMD_DATA + 1] = PWM_REG(CPU_FAN1);
// TODO handle dual CPU fans
return RES_OK;
case 1:
// Get duty of fan 1
//TODO: only allow on platforms like addw2
smfi_cmd[SMFI_CMD_DATA + 1] = DCR4;
smfi_cmd[SMFI_CMD_DATA + 1] = PWM_REG(GPU_FAN1);
return RES_OK;
}

Expand All @@ -151,16 +151,16 @@ static enum Result cmd_fan_set(void) {
switch (smfi_cmd[SMFI_CMD_DATA]) {
case 0:
// Set duty cycle of fan 0
DCR2 = smfi_cmd[SMFI_CMD_DATA + 1];
PWM_REG(CPU_FAN1) = smfi_cmd[SMFI_CMD_DATA + 1];
#if HAVE_CPU_FAN2
// TODO handle CPU fan 2 separately
DCR3 = smfi_cmd[SMFI_CMD_DATA + 1];
PWM_REG(CPU_FAN2) = smfi_cmd[SMFI_CMD_DATA + 1];
#endif
return RES_OK;
case 1:
// Set duty cycle of fan 1
//TODO: only allow on platforms like addw2
DCR4 = smfi_cmd[SMFI_CMD_DATA + 1];
PWM_REG(GPU_FAN1) = smfi_cmd[SMFI_CMD_DATA + 1];
return RES_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions src/ec/ite/include/ec/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <stdint.h>

#define PWM_REG(x) concat(DCR, x)

// Channel 0 clock prescaler register
volatile uint8_t __xdata __at(0x1800) C0CPRS;
// Channel 6 clock prescaler register (low byte)
Expand Down

0 comments on commit 582481d

Please sign in to comment.