Skip to content

Commit b59372d

Browse files
committed
battery: wire up thresholds to options api
Signed-off-by: Michał Kopeć <michal.kopec@3mdeb.com>
1 parent bad5562 commit b59372d

File tree

6 files changed

+15
-35
lines changed

6 files changed

+15
-35
lines changed

src/board/system76/common/battery.c

+9-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22

33
#include <board/battery.h>
4+
#include <board/options.h>
45
#include <board/smbus.h>
56
#include <common/debug.h>
67

@@ -13,41 +14,31 @@ uint16_t battery_charger_input_voltage = BATTERY_CHARGER_VOLTAGE_AC;
1314
#define BATTERY_START_DEFAULT 0
1415
#define BATTERY_END_DEFAULT 100
1516

16-
// Represents a battery percentage level, below which charging will begin.
17-
// Valid values are [0, 100]
18-
// A value of 0 turns off the start threshold control.
19-
static uint8_t battery_start_threshold = BATTERY_START_THRESHOLD;
20-
21-
// Represents a battery percentage level, above which charging will stop.
22-
// Valid values are [0, 100]
23-
// A value of 100 turns off the stop threshold control.
24-
static uint8_t battery_end_threshold = BATTERY_END_THRESHOLD;
25-
2617
uint8_t battery_get_start_threshold(void) {
27-
if (battery_start_threshold > 100)
18+
if (options_get(OPT_BAT_THRESHOLD_START) > 100)
2819
return BATTERY_START_DEFAULT;
29-
return battery_start_threshold;
20+
return options_get(OPT_BAT_THRESHOLD_START);
3021
}
3122

3223
bool battery_set_start_threshold(uint8_t value) {
33-
if (value > 100 || value >= battery_end_threshold)
24+
if (value > 100 || value >= options_get(OPT_BAT_THRESHOLD_STOP))
3425
return false;
3526

36-
battery_start_threshold = value;
27+
options_set(OPT_BAT_THRESHOLD_START, value);
3728
return true;
3829
}
3930

4031
uint8_t battery_get_end_threshold(void) {
41-
if (battery_end_threshold > 100)
32+
if (options_get(OPT_BAT_THRESHOLD_STOP) > 100)
4233
return BATTERY_END_DEFAULT;
43-
return battery_end_threshold;
34+
return options_get(OPT_BAT_THRESHOLD_STOP);
4435
}
4536

4637
bool battery_set_end_threshold(uint8_t value) {
47-
if (value > 100 || value <= battery_start_threshold)
38+
if (value > 100 || value <= options_get(OPT_BAT_THRESHOLD_START))
4839
return false;
4940

50-
battery_end_threshold = value;
41+
options_set(OPT_BAT_THRESHOLD_STOP, value);
5142
return true;
5243
}
5344

@@ -98,8 +89,3 @@ void battery_event(void) {
9889

9990
battery_charger_event();
10091
}
101-
102-
void battery_reset(void) {
103-
battery_start_threshold = BATTERY_START_THRESHOLD;
104-
battery_end_threshold = BATTERY_END_THRESHOLD;
105-
}

src/board/system76/common/common.mk

-8
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ SYSTEM76_COMMON_DIR=src/board/system76/common
6565
INCLUDE += $(SYSTEM76_COMMON_DIR)/common.mk
6666
CFLAGS+=-I$(SYSTEM76_COMMON_DIR)/include
6767

68-
# Set battery charging thresholds
69-
BATTERY_START_THRESHOLD?=95
70-
BATTERY_END_THRESHOLD?=98
71-
72-
CFLAGS+=\
73-
-DBATTERY_START_THRESHOLD=$(BATTERY_START_THRESHOLD) \
74-
-DBATTERY_END_THRESHOLD=$(BATTERY_END_THRESHOLD)
75-
7668
# Add charger
7769
CHARGER?=bq24780s
7870
board-common-y += charger/$(CHARGER).c

src/board/system76/common/config.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <board/battery.h>
66
#include <board/kbscan.h>
77
#include <board/keymap.h>
8+
#include <board/options.h>
89
#include <common/debug.h>
910

1011
/**
@@ -19,7 +20,7 @@ bool config_should_reset(void) {
1920
*/
2021
void config_reset(void) {
2122
INFO("Reset configuration\n");
22-
battery_reset();
23+
options_reset();
2324
keymap_erase_config();
2425
keymap_load_default();
2526
}

src/board/system76/common/include/board/battery.h

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ bool battery_set_end_threshold(uint8_t value);
4646

4747
int16_t battery_charger_configure(void);
4848
void battery_event(void);
49-
void battery_reset(void);
5049

5150
// Defined by charger/*.c
5251
int16_t battery_charger_disable(void);

src/board/system76/common/include/board/options.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
// Initialize the options
1010
void options_init(void);
11+
// Restore defaults
12+
void options_reset(void);
1113
// Save options to flash
1214
bool options_save_config(void);
1315
// Get an option

src/board/system76/common/options.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const uint32_t OPTIONS_ADDR = 0x1F800;
2222
// Signature is the size of the config
2323
const uint16_t OPTIONS_SIGNATURE = sizeof(OPTIONS);
2424

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

4949
void options_init(void) {
5050
if (!options_load_config()) {
51-
options_load_default();
51+
options_reset();
5252
}
5353
}
5454

0 commit comments

Comments
 (0)