Skip to content

Commit

Permalink
battery: wire up thresholds to options api
Browse files Browse the repository at this point in the history
Signed-off-by: Michał Kopeć <[email protected]>
  • Loading branch information
mkopec committed May 31, 2024
1 parent bad5562 commit b59372d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 35 deletions.
32 changes: 9 additions & 23 deletions src/board/system76/common/battery.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only

#include <board/battery.h>
#include <board/options.h>
#include <board/smbus.h>
#include <common/debug.h>

Expand All @@ -13,41 +14,31 @@ uint16_t battery_charger_input_voltage = BATTERY_CHARGER_VOLTAGE_AC;
#define BATTERY_START_DEFAULT 0
#define BATTERY_END_DEFAULT 100

// Represents a battery percentage level, below which charging will begin.
// Valid values are [0, 100]
// A value of 0 turns off the start threshold control.
static uint8_t battery_start_threshold = BATTERY_START_THRESHOLD;

// Represents a battery percentage level, above which charging will stop.
// Valid values are [0, 100]
// A value of 100 turns off the stop threshold control.
static uint8_t battery_end_threshold = BATTERY_END_THRESHOLD;

uint8_t battery_get_start_threshold(void) {
if (battery_start_threshold > 100)
if (options_get(OPT_BAT_THRESHOLD_START) > 100)
return BATTERY_START_DEFAULT;
return battery_start_threshold;
return options_get(OPT_BAT_THRESHOLD_START);
}

bool battery_set_start_threshold(uint8_t value) {
if (value > 100 || value >= battery_end_threshold)
if (value > 100 || value >= options_get(OPT_BAT_THRESHOLD_STOP))
return false;

battery_start_threshold = value;
options_set(OPT_BAT_THRESHOLD_START, value);
return true;
}

uint8_t battery_get_end_threshold(void) {
if (battery_end_threshold > 100)
if (options_get(OPT_BAT_THRESHOLD_STOP) > 100)
return BATTERY_END_DEFAULT;
return battery_end_threshold;
return options_get(OPT_BAT_THRESHOLD_STOP);
}

bool battery_set_end_threshold(uint8_t value) {
if (value > 100 || value <= battery_start_threshold)
if (value > 100 || value <= options_get(OPT_BAT_THRESHOLD_START))
return false;

battery_end_threshold = value;
options_set(OPT_BAT_THRESHOLD_STOP, value);
return true;
}

Expand Down Expand Up @@ -98,8 +89,3 @@ void battery_event(void) {

battery_charger_event();
}

void battery_reset(void) {
battery_start_threshold = BATTERY_START_THRESHOLD;
battery_end_threshold = BATTERY_END_THRESHOLD;
}
8 changes: 0 additions & 8 deletions src/board/system76/common/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ SYSTEM76_COMMON_DIR=src/board/system76/common
INCLUDE += $(SYSTEM76_COMMON_DIR)/common.mk
CFLAGS+=-I$(SYSTEM76_COMMON_DIR)/include

# Set battery charging thresholds
BATTERY_START_THRESHOLD?=95
BATTERY_END_THRESHOLD?=98

CFLAGS+=\
-DBATTERY_START_THRESHOLD=$(BATTERY_START_THRESHOLD) \
-DBATTERY_END_THRESHOLD=$(BATTERY_END_THRESHOLD)

# Add charger
CHARGER?=bq24780s
board-common-y += charger/$(CHARGER).c
Expand Down
3 changes: 2 additions & 1 deletion src/board/system76/common/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <board/battery.h>
#include <board/kbscan.h>
#include <board/keymap.h>
#include <board/options.h>
#include <common/debug.h>

/**
Expand All @@ -19,7 +20,7 @@ bool config_should_reset(void) {
*/
void config_reset(void) {
INFO("Reset configuration\n");
battery_reset();
options_reset();
keymap_erase_config();
keymap_load_default();
}
1 change: 0 additions & 1 deletion src/board/system76/common/include/board/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ bool battery_set_end_threshold(uint8_t value);

int16_t battery_charger_configure(void);
void battery_event(void);
void battery_reset(void);

// Defined by charger/*.c
int16_t battery_charger_disable(void);
Expand Down
2 changes: 2 additions & 0 deletions src/board/system76/common/include/board/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

// Initialize the options
void options_init(void);
// Restore defaults
void options_reset(void);
// Save options to flash
bool options_save_config(void);
// Get an option
Expand Down
4 changes: 2 additions & 2 deletions src/board/system76/common/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const uint32_t OPTIONS_ADDR = 0x1F800;
// Signature is the size of the config
const uint16_t OPTIONS_SIGNATURE = sizeof(OPTIONS);

static void options_load_default() {
void options_reset() {
for (uint8_t opt = 0; opt < NUM_OPTIONS; opt++) {
OPTIONS[opt] = DEFAULT_OPTIONS[opt];
}
Expand All @@ -48,7 +48,7 @@ static bool options_erase_config(void) {

void options_init(void) {
if (!options_load_config()) {
options_load_default();
options_reset();
}
}

Expand Down

0 comments on commit b59372d

Please sign in to comment.