Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save battery thresholds to flash #498

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/board/system76/common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ void acpi_write(uint8_t addr, uint8_t data) {

case 0xBD:
battery_set_end_threshold(data);
(void)battery_save_thresholds();
break;

#if HAVE_LED_AIRPLANE_N
Expand Down
48 changes: 48 additions & 0 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/flash.h>
#include <board/gpio.h>
#include <board/smbus.h>
#include <common/debug.h>
Expand All @@ -13,6 +14,18 @@ uint16_t battery_charger_input_current = CHARGER_INPUT_CURRENT;
#define BATTERY_START_DEFAULT 0
#define BATTERY_END_DEFAULT 100

// Flash address to save charging thresholds to
static const uint32_t BAT_CFG_ADDR = CONFIG_EC_FLASH_SIZE - (2 * 1024);
static const uint16_t BAT_CFG_MAGIC = 0x4254;

struct battery_config {
uint16_t magic;
uint8_t start_threshold;
uint8_t end_threshold;
};

static struct battery_config __xdata bat_cfg = { { 0 } };

// 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.
Expand Down Expand Up @@ -112,3 +125,38 @@ void battery_reset(void) {
battery_start_threshold = BATTERY_START_THRESHOLD;
battery_end_threshold = BATTERY_END_THRESHOLD;
}

// Read the charge thresholds from flash. Falls back to defaults if not found.
bool battery_load_thresholds(void) {
bool loaded = true;

flash_read(BAT_CFG_ADDR, (uint8_t *)&bat_cfg, sizeof(bat_cfg));

if (bat_cfg.magic != BAT_CFG_MAGIC) {
bat_cfg.magic = BAT_CFG_MAGIC;
bat_cfg.start_threshold = BATTERY_START_THRESHOLD;
bat_cfg.end_threshold = BATTERY_END_THRESHOLD;
loaded = false;
}

(void)battery_set_start_threshold(bat_cfg.start_threshold);
(void)battery_set_end_threshold(bat_cfg.end_threshold);

return loaded;
}

// Write the charge thresholds to flash.
bool battery_save_thresholds(void) {
if ((bat_cfg.start_threshold == battery_start_threshold) &&
(bat_cfg.end_threshold == battery_end_threshold)) {
return true;
}

bat_cfg.start_threshold = battery_start_threshold;
bat_cfg.end_threshold = battery_end_threshold;

flash_erase(BAT_CFG_ADDR);
flash_write(BAT_CFG_ADDR, (uint8_t *)&bat_cfg, sizeof(bat_cfg));

return flash_read_u16(BAT_CFG_ADDR) == BAT_CFG_MAGIC;
}
4 changes: 2 additions & 2 deletions src/board/system76/common/flash/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void flash_write_enable(void);
*/
void flash_entry(uint32_t addr, uint8_t *data, uint32_t length,
uint8_t command) __reentrant __critical {
// Only allow access from 64KB to 128KB.
if ((addr < 0x10000) || (length > 0x10000) || ((addr + length) > 0x20000))
// Only allow access from 64 KiB to the end of flash.
if ((addr < 0x10000) || (length > 0x10000) || ((addr + length) > CONFIG_EC_FLASH_SIZE))
return;

if (command == FLASH_COMMAND_READ) {
Expand Down
3 changes: 3 additions & 0 deletions src/board/system76/common/include/board/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ bool battery_set_start_threshold(uint8_t value);
uint8_t battery_get_end_threshold(void);
bool battery_set_end_threshold(uint8_t value);

bool battery_load_thresholds(void);
bool battery_save_thresholds(void);

int16_t battery_charger_configure(void);
void battery_event(void);
void battery_reset(void);
Expand Down
2 changes: 1 addition & 1 deletion src/board/system76/common/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bool keymap_fnlock = false;
uint16_t __xdata DYNAMIC_KEYMAP[KM_LAY][KM_OUT][KM_IN];

// Config is in the last sector of flash
const uint32_t CONFIG_ADDR = 0x1FC00;
const uint32_t CONFIG_ADDR = CONFIG_EC_FLASH_SIZE - 1024;
// Signature is the size of the keymap
const uint16_t CONFIG_SIGNATURE = sizeof(DYNAMIC_KEYMAP);

Expand Down
1 change: 1 addition & 0 deletions src/board/system76/common/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void init(void) {
// Must happen last
power_init();
board_init();
(void)battery_load_thresholds();
}

void main(void) {
Expand Down
2 changes: 2 additions & 0 deletions src/ec/ite/ec.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ CONFIG_EC_FLASH_SIZE = 262144
else
$(error flash size not specified)
endif

CFLAGS += -DCONFIG_EC_FLASH_SIZE=$(CONFIG_EC_FLASH_SIZE)
Loading