Skip to content

Commit

Permalink
fan: Put non-const fan data in a struct
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Crawford <tcrawford@system76.com>
  • Loading branch information
crawfxrd committed Aug 5, 2024
1 parent c695755 commit 17050c4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/board/system76/common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ uint8_t acpi_read(uint8_t addr) {

ACPI_8(0xCC, sci_extra);

ACPI_8(0xCE, fan1_pwm_actual);
ACPI_16(0xD0, fan1_rpm);
ACPI_8(0xCE, fan1_info.pwm_actual);
ACPI_16(0xD0, fan1_info.rpm);
#if CONFIG_HAVE_DGPU
ACPI_8(0xCD, dgpu_temp);
#endif // CONFIG_HAVE_DGPU
#ifdef FAN2_PWM
ACPI_8(0xCF, fan2_pwm_actual);
ACPI_16(0xD2, fan2_rpm);
ACPI_8(0xCF, fan2_info.pwm_actual);
ACPI_16(0xD2, fan2_info.rpm);
#endif // FAN2_PWM

#if HAVE_LED_AIRPLANE_N
Expand Down
90 changes: 48 additions & 42 deletions src/board/system76/common/fan.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@

bool fan_max = false;

uint8_t fan1_pwm_actual = 0;
uint8_t fan1_pwm_target = 0;
uint16_t fan1_rpm = 0;
struct FanInfo fan1_info = {
.pwm_actual = 0,
.pwm_target = 0,
.rpm = 0,
};

uint8_t fan2_pwm_actual = 0;
uint8_t fan2_pwm_target = 0;
uint16_t fan2_rpm = 0;
#ifdef FAN2_PWM
struct FanInfo fan2_info = {
.pwm_actual = 0,
.pwm_target = 0,
.rpm = 0,
};
#endif

#define TACH_FREQ (CONFIG_CLOCK_FREQ_KHZ * 1000UL)

Expand Down Expand Up @@ -87,58 +93,58 @@ void fan_event(void) {
// change instead of ramping to provide the desired effects.

// Set FAN1 duty
fan1_pwm_target = fan_duty(&FAN1, sys_temp);
fan1_info.pwm_target = fan_duty(&FAN1, sys_temp);
if (fan_max) {
fan1_pwm_target = CTR0;
fan1_pwm_actual = CTR0;
fan1_info.pwm_target = CTR0;
fan1_info.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) {
if (fan1_pwm_actual < CTR0) {
fan1_pwm_actual++;
if (fan1_pwm_actual < FAN1.pwm_min) {
fan1_pwm_actual = FAN1.pwm_min;
fan1_info.pwm_target = 0;
fan1_info.pwm_actual = 0;
} else if (fan1_info.pwm_actual < fan1_info.pwm_target) {
if (fan1_info.pwm_actual < CTR0) {
fan1_info.pwm_actual++;
if (fan1_info.pwm_actual < FAN1.pwm_min) {
fan1_info.pwm_actual = FAN1.pwm_min;
}
}
} else if (fan1_pwm_actual > fan1_pwm_target) {
if (fan1_pwm_actual > 0) {
fan1_pwm_actual--;
if (fan1_pwm_actual < FAN1.pwm_min) {
fan1_pwm_actual = 0;
} else if (fan1_info.pwm_actual > fan1_info.pwm_target) {
if (fan1_info.pwm_actual > 0) {
fan1_info.pwm_actual--;
if (fan1_info.pwm_actual < FAN1.pwm_min) {
fan1_info.pwm_actual = 0;
}
}
}
TRACE("FAN1 duty=%d\n", fan1_pwm_actual);
FAN1_PWM = fan1_pwm_actual;
fan1_rpm = fan_get_tach0_rpm();
TRACE("FAN1 duty=%d\n", fan1_info.pwm_actual);
FAN1_PWM = fan1_info.pwm_actual;
fan1_info.rpm = fan_get_tach0_rpm();

#ifdef FAN2_PWM
// set FAN2 duty
fan2_pwm_target = fan_duty(&FAN2, sys_temp);
fan2_info.pwm_target = fan_duty(&FAN2, sys_temp);
if (fan_max) {
fan2_pwm_target = CTR0;
fan2_pwm_actual = CTR0;
fan2_info.pwm_target = CTR0;
fan2_info.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) {
if (fan2_pwm_actual < CTR0) {
fan2_pwm_actual++;
if (fan2_pwm_actual < FAN2.pwm_min) {
fan2_pwm_actual = FAN2.pwm_min;
fan2_info.pwm_target = 0;
fan2_info.pwm_actual = 0;
} else if (fan2_info.pwm_actual < fan2_info.pwm_target) {
if (fan2_info.pwm_actual < CTR0) {
fan2_info.pwm_actual++;
if (fan2_info.pwm_actual < FAN2.pwm_min) {
fan2_info.pwm_actual = FAN2.pwm_min;
}
}
} else if (fan2_pwm_actual > fan2_pwm_target) {
if (fan2_pwm_actual > 0) {
fan2_pwm_actual--;
if (fan2_pwm_actual < FAN2.pwm_min) {
fan2_pwm_actual = 0;
} else if (fan2_info.pwm_actual > fan2_info.pwm_target) {
if (fan2_info.pwm_actual > 0) {
fan2_info.pwm_actual--;
if (fan2_info.pwm_actual < FAN2.pwm_min) {
fan2_info.pwm_actual = 0;
}
}
}
TRACE("FAN2 duty=%d\n", fan2_pwm_actual);
FAN2_PWM = fan2_pwm_actual;
fan2_rpm = fan_get_tach1_rpm();
TRACE("FAN2 duty=%d\n", fan2_info.pwm_actual);
FAN2_PWM = fan2_info.pwm_actual;
fan2_info.rpm = fan_get_tach1_rpm();
#endif
}
16 changes: 9 additions & 7 deletions src/board/system76/common/include/board/fan.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ struct Fan {
const uint8_t pwm_min;
};

struct FanInfo {
uint8_t pwm_actual;
uint8_t pwm_target;
uint16_t rpm;
};

extern const struct Fan __code FAN1;
extern struct FanInfo fan1_info;

#ifdef FAN2_PWM
extern const struct Fan __code FAN2;
extern struct FanInfo fan2_info;
#endif

extern bool fan_max;

extern uint8_t fan1_pwm_actual;
extern uint8_t fan1_pwm_target;
extern uint16_t fan1_rpm;
extern uint8_t fan2_pwm_actual;
extern uint8_t fan2_pwm_target;
extern uint16_t fan2_rpm;

void fan_reset(void);
void fan_event(void);

Expand Down
8 changes: 4 additions & 4 deletions src/board/system76/common/smfi.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ static enum Result cmd_print(void) {
static enum Result cmd_fan_get(void) {
switch (smfi_cmd[SMFI_CMD_DATA]) {
case 1:
smfi_cmd[SMFI_CMD_DATA + 1] = fan1_pwm_actual;
smfi_cmd[SMFI_CMD_DATA + 1] = fan1_info.pwm_actual;
return RES_OK;
#ifdef FAN2_PWM
case 2:
smfi_cmd[SMFI_CMD_DATA + 1] = fan2_pwm_actual;
smfi_cmd[SMFI_CMD_DATA + 1] = fan2_info.pwm_actual;
return RES_OK;
#endif
}
Expand All @@ -143,12 +143,12 @@ static enum Result cmd_fan_set(void) {
switch (smfi_cmd[SMFI_CMD_DATA]) {
case 1:
// Set duty cycle of FAN1
fan1_pwm_target = smfi_cmd[SMFI_CMD_DATA + 1];
fan1_info.pwm_target = smfi_cmd[SMFI_CMD_DATA + 1];
return RES_OK;
#ifdef FAN2_PWM
case 2:
// Set duty cycle of FAN2
fan2_pwm_target = smfi_cmd[SMFI_CMD_DATA + 1];
fan2_info.pwm_target = smfi_cmd[SMFI_CMD_DATA + 1];
return RES_OK;
#endif
}
Expand Down

0 comments on commit 17050c4

Please sign in to comment.