1
0
Fork 0

zero-sugar: Power off if battery capacity <= 5%

Verify that the battery capacity is more than 5% before
booting the device.
zero-sugar
Lars Ivar Miljeteig 2020-04-03 17:00:43 +02:00
parent 78d6deb262
commit 9cf952030c
3 changed files with 64 additions and 1 deletions

View File

@ -10,6 +10,8 @@
#define MAX77818_FG_CONFIG__FGCC_ENABLED 0x0800
#define MAX77818_FG_CONFIG__FGCC_DISABLED 0x0000
#define MAX77818_REG_FG_REPSOC 0x06
static struct udevice *fgDev = NULL;
int max77818_init_fg_device(void)
@ -124,3 +126,48 @@ int max77818_restore_fgcc(bool restore_state)
return max77818_set_fgcc_state(false, NULL);
}
}
int max77818_get_battery_capacity(u8 *capacity)
{
int ret;
u16 value;
if (!fgDev) {
ret = max77818_init_fg_device();
if (ret)
return ret;
}
ret = max77818_i2c_reg_read16(fgDev,
MAX77818_REG_FG_REPSOC,
&value);
if (ret)
return ret;
*capacity = (u8)(value >> 8);
return 0;
}
static int do_max77818_get_battery_capacity(cmd_tbl_t *cmdtp,
int flag,
int argc,
char * const argv[])
{
int ret;
u8 capacity;
ret = max77818_get_battery_capacity(&capacity);
if (ret)
return ret;
printf("Capacity: %u%%\n", capacity);
return 0;
}
U_BOOT_CMD(
max77818_get_battery_capacity, 1, 1, do_max77818_get_battery_capacity,
"Get battery state of charge",
""
);

View File

@ -8,4 +8,6 @@ int max77818_read_fgcc_state(bool *state);
int max77818_set_fgcc_state(bool enabled, bool *restore_state);
int max77818_restore_fgcc(bool restore_state);
int max77818_get_battery_capacity(u8 *capacity);
#endif /* FG_INIT_H */

View File

@ -14,6 +14,7 @@
#include "digitizer_init.h"
#include "max77818.h"
#include "max77818_charger.h"
#include "max77818_battery.h"
#include "serial_download_trap.h"
#include <asm/arch/clock.h>
@ -47,7 +48,6 @@
#include <asm/setup.h>
#include <asm/bootm.h>
#define SNVS_BASE_ADDR 0x30370000
#define SNVS_REG_LPCR SNVS_BASE_ADDR + 0x38
#define SNVS_MASK_POWEROFF (BIT(5) | BIT(6))
@ -102,6 +102,7 @@ static void power_perfs(void)
static void init_charger(void)
{
int ret;
u8 capacity;
printf("Enabling SAFEOUT1\n");
ret = max77818_enable_safeout1();
@ -116,6 +117,19 @@ static void init_charger(void)
printf("%s: Failed to set charger config: %d\n",
__func__, ret);
}
printf("Reading remaining battery capacity\n");
ret = max77818_get_battery_capacity(&capacity);
if (ret) {
printf("%s: Failed to read battery capacity: %d\n", __func__, ret);
}
else if (capacity < 5) {
printf("Battery too low (%u%% < 5%%), turning off\n", capacity);
snvs_poweroff();
}
else {
printf("Battery currently at %u%%\n", capacity);
}
}
static void save_serial(void)