battery: Group battery info in a struct

Signed-off-by: Tim Crawford <tcrawford@system76.com>
This commit is contained in:
Tim Crawford
2021-12-15 10:03:35 -07:00
committed by Jeremy Soller
parent ee8ba5b72e
commit f21cc6d602
4 changed files with 38 additions and 44 deletions

View File

@ -102,32 +102,32 @@ uint8_t acpi_read(uint8_t addr) {
// AC adapter connected // AC adapter connected
data |= BIT(0); data |= BIT(0);
} }
if (battery_status & BATTERY_INITIALIZED) { if (battery_info.status & BATTERY_INITIALIZED) {
// BAT0 connected // BAT0 connected
data |= BIT(2); data |= BIT(2);
} }
break; break;
ACPI_16(0x16, battery_design_capacity); ACPI_16(0x16, battery_info.design_capacity);
ACPI_16(0x1A, battery_full_capacity); ACPI_16(0x1A, battery_info.full_capacity);
ACPI_16(0x22, battery_design_voltage); ACPI_16(0x22, battery_info.design_voltage);
case 0x26: case 0x26:
// If AC adapter connected // If AC adapter connected
if (!gpio_get(&ACIN_N)) { if (!gpio_get(&ACIN_N)) {
// And battery is not fully charged // And battery is not fully charged
if (battery_current != 0) { if (battery_info.current != 0) {
// Battery is charging // Battery is charging
data |= BIT(1); data |= BIT(1);
} }
} }
break; break;
ACPI_16(0x2A, battery_current); ACPI_16(0x2A, battery_info.current);
ACPI_16(0x2E, battery_remaining_capacity); ACPI_16(0x2E, battery_info.remaining_capacity);
ACPI_16(0x32, battery_voltage); ACPI_16(0x32, battery_info.voltage);
ACPI_16(0x42, battery_cycle_count); ACPI_16(0x42, battery_info.cycle_count);
ACPI_8(0x68, acpi_ecos); ACPI_8(0x68, acpi_ecos);

View File

@ -4,6 +4,8 @@
#include <board/smbus.h> #include <board/smbus.h>
#include <common/debug.h> #include <common/debug.h>
struct battery_info battery_info = { 0 };
// Default values to disable battery charging thresholds // Default values to disable battery charging thresholds
#define BATTERY_START_DEFAULT 0 #define BATTERY_START_DEFAULT 0
#define BATTERY_END_DEFAULT 100 #define BATTERY_END_DEFAULT 100
@ -56,7 +58,7 @@ int16_t battery_charger_configure(void) {
// Stop threshold not configured: Always charge on AC. // Stop threshold not configured: Always charge on AC.
should_charge = true; should_charge = true;
} }
else if (battery_charge >= battery_get_end_threshold()) { else if (battery_info.charge >= battery_get_end_threshold()) {
// Stop threshold configured: Stop charging at threshold. // Stop threshold configured: Stop charging at threshold.
should_charge = false; should_charge = false;
} }
@ -64,7 +66,7 @@ int16_t battery_charger_configure(void) {
// Start threshold not configured: Always charge up to stop threshold. // Start threshold not configured: Always charge up to stop threshold.
should_charge = true; should_charge = true;
} }
else if (battery_charge <= battery_get_start_threshold()) { else if (battery_info.charge <= battery_get_start_threshold()) {
// Start threshold configured: Start charging at threshold. // Start threshold configured: Start charging at threshold.
should_charge = true; should_charge = true;
} }
@ -74,17 +76,6 @@ int16_t battery_charger_configure(void) {
return battery_charger_disable(); return battery_charger_disable();
} }
uint16_t battery_temp = 0;
uint16_t battery_voltage = 0;
uint16_t battery_current = 0;
uint16_t battery_charge = 0;
uint16_t battery_remaining_capacity = 0;
uint16_t battery_full_capacity = 0;
uint16_t battery_status = 0;
uint16_t battery_cycle_count = 0;
uint16_t battery_design_capacity = 0;
uint16_t battery_design_voltage = 0;
void battery_event(void) { void battery_event(void) {
int16_t res = 0; int16_t res = 0;
@ -95,16 +86,16 @@ void battery_event(void) {
} \ } \
} }
command(battery_temp, 0x08); command(battery_info.temp, 0x08);
command(battery_voltage, 0x09); command(battery_info.voltage, 0x09);
command(battery_current, 0x0A); command(battery_info.current, 0x0A);
command(battery_charge, 0x0D); command(battery_info.charge, 0x0D);
command(battery_remaining_capacity, 0x0F); command(battery_info.remaining_capacity, 0x0F);
command(battery_full_capacity, 0x10); command(battery_info.full_capacity, 0x10);
command(battery_status, 0x16); command(battery_info.status, 0x16);
command(battery_cycle_count, 0x17); command(battery_info.cycle_count, 0x17);
command(battery_design_capacity, 0x18); command(battery_info.design_capacity, 0x18);
command(battery_design_voltage, 0x19); command(battery_info.design_voltage, 0x19);
#undef command #undef command

View File

@ -18,16 +18,19 @@
#define BATTERY_INITIALIZED BIT(7) #define BATTERY_INITIALIZED BIT(7)
extern uint16_t battery_temp; struct battery_info {
extern uint16_t battery_voltage; uint16_t temp;
extern uint16_t battery_current; uint16_t voltage;
extern uint16_t battery_charge; uint16_t current;
extern uint16_t battery_remaining_capacity; uint16_t charge;
extern uint16_t battery_full_capacity; uint16_t remaining_capacity;
extern uint16_t battery_status; uint16_t full_capacity;
extern uint16_t battery_cycle_count; uint16_t status;
extern uint16_t battery_design_capacity; uint16_t cycle_count;
extern uint16_t battery_design_voltage; uint16_t design_capacity;
uint16_t design_voltage;
};
extern struct battery_info battery_info;
uint8_t battery_get_start_threshold(void); uint8_t battery_get_start_threshold(void);
bool battery_set_start_threshold(uint8_t value); bool battery_set_start_threshold(uint8_t value);

View File

@ -578,7 +578,7 @@ void power_event(void) {
//TODO: do not require both LEDs //TODO: do not require both LEDs
#if HAVE_LED_BAT_CHG && HAVE_LED_BAT_FULL #if HAVE_LED_BAT_CHG && HAVE_LED_BAT_FULL
if (!(battery_status & BATTERY_INITIALIZED)) { if (!(battery_info.status & BATTERY_INITIALIZED)) {
// No battery connected // No battery connected
gpio_set(&LED_BAT_CHG, false); gpio_set(&LED_BAT_CHG, false);
gpio_set(&LED_BAT_FULL, false); gpio_set(&LED_BAT_FULL, false);
@ -586,7 +586,7 @@ void power_event(void) {
// Discharging (no AC adapter) // Discharging (no AC adapter)
gpio_set(&LED_BAT_CHG, false); gpio_set(&LED_BAT_CHG, false);
gpio_set(&LED_BAT_FULL, false); gpio_set(&LED_BAT_FULL, false);
} else if (battery_current == 0) { } else if (battery_info.current == 0) {
// Fully charged // Fully charged
// TODO: turn off charger // TODO: turn off charger
gpio_set(&LED_BAT_CHG, false); gpio_set(&LED_BAT_CHG, false);