Refactor battery and charging for robustness and to support future products
Librem 14 behavior is preserved except for some specifically targeted fixes.
Behavior changes for Librem 14:
- Unplugging and re-plugging AC adapter within 1 second now re-enables charging correctly
- Previously, charging stopped unexpectedly since BQ24715 automatically sets charge current to 0 on AC unplug
- Plugging in AC just as system shuts down works correctly
- Previously, the EC would lower SMC_SHUTDOWN_N but then would continue running as-is if the system didn't actually shut off
- Booting again, then unplugging AC would cause the system to shut off unexpectedly
- The EC now detects that the system didn't shut off and raises SMC_SHUTDOWN_N again
- Keyboard latency and dropped keys due to 1-second event are improved
- Trimmed and optimized periodic battery tracing to reduce time spent in 1-second event
- Staggered battery/charger events to reduce maximum latency
- At PoR, just program bq24715 registers, don't enable charge and then immediately disable it
-
battery_charger_init()
does that (if bq24715 is powered)
-
- Debug commands are now provided (via ectool) to test behaviors that are otherwise untestable
- Debug commands are never damaging but can produce behaviors that otherwise "never occur", like charger validation errors or EC freezes
bq24715 charger implementation was refactored:
- Support chargers only powered by AC (board option)
- Support enabling IDPM (board option)
- Reduce coupling to SBS battery and Librem 14 board logic
- Improve robustness
- Validate programmed parameters periodically, disable charger and reprogram if any problem is detected (battery logic can try to re-enable charger)
- Validate input values like charge current / voltage for bq24715 valid range
- Improve output for unexpected conditions like parameters out of range
- Provide debug commands to test validation
sbs_battery
(SBS battery host implementation) was extracted from Librem 14 board_battery.c