Set battery charge threshold

I was wondering whether it is possible to set a battery charge threshold for the Librem5.

Most of the time I am using my Librem5 docked to a USB-C Hub and unfortunately it does not provide enough power to run it without battery, but enough to slowly charge. So to extend battery lifespan, charging could stop when reaching 80% and kick back in when it goes below 30%.

With the Librem14 I think this is possible and I have seen in Nicole Faerber’s blog that she did it also with a Pixelbook [1]. She even posted a script to enable this but I am missing the (i2c?)commands or sysfs entries that could replace the EC commands in that script [2].

Any idea on how to achieve this?

[1]
https://www.dpin.de/nf/google-pixelbook-eve-plain-linux/

[2]

#!/bin/sh

STOP_THRESHOLD=80
START_THRESHOLD=30

while true; do
  AC_STATE=`cat /sys/class/power_supply/AC/online`
  if [ $AC_STATE == "1" ]; then
    echo "on AC"
    BAT_LEVEL=`cat /sys/class/power_supply/BAT0/capacity`
    CURRENT=`cat /sys/class/power_supply/BAT0/current_now`
    echo "current=$CURRENT"
    if [ $BAT_LEVEL -gt $STOP_THRESHOLD ]; then
      echo "bat level > $STOP_THRESHOLD%"
      if [ $CURRENT -gt 0 ]; then
        echo "charger disabling"
        sleep 10
        ectool chargecontrol idle
      fi
    elif [ $BAT_LEVEL -lt $START_THRESHOLD ]; then
      echo "bat level < $START_THRESHOLD%"
      if [ $CURRENT -lt 0 ]; then
        echo "charger enabling"
        sleep 10
        ectool chargecontrol normal
      fi
   else
    echo "charger state OK, idling..."
   fi
  else
    echo "not on AC, idling..."
  fi
sleep 10
done
3 Likes

I like your idea and I also asked this question in the matrix group, without success. Maybe a discussion here will bring up some solutions.

I already found this post battery-charge-notification which plays a sound when the battery level reaches a threshold. This solution does not de- and reactivate charging.

Regarding your [2], I modified it like that:

#!/bin/sh

# Start/stop blinking of status LED when battery is out of device
# https://forums.puri.sm/t/new-post-my-first-week-of-librem-5-convergence/12431/53
# sudo i2cset -f -y 3 0x6a 0x07 0xcd # turns STAT LED off
# sudo i2cset -f -y 3 0x6a 0x07 0x8d # turns STAT LED back on

# Charger: /sys/class/power_supply/tps6598x-source-psy-0-003f/*     
# USB-C module: /sys/class/power_supply/bq25890-charger/*
# Battery: /sys/class/power_supply/max170xx_battery/*

STOP_THRESHOLD=80
START_THRESHOLD=30

while true; do
  AC_STATE=$(cat /sys/class/power_supply/tps6598x-source-psy-0-003f/online)
  if [ $AC_STATE = "1" ]; then
    echo "on AC"
    BAT_LEVEL=$(cat /sys/class/power_supply/max170xx_battery/capacity)
    CURRENT=$(cat /sys/class/power_supply/max170xx_battery/current_now)
    echo "current=$CURRENT"
    if [ $BAT_LEVEL -gt $STOP_THRESHOLD ]; then
      echo "bat level > $STOP_THRESHOLD%"
      if [ $CURRENT -gt 0 ]; then
        echo "charger disabling"
        sleep 10
#        ectool chargecontrol idle <-- modify
      fi
    elif [ $BAT_LEVEL -lt $START_THRESHOLD ]; then
      echo "bat level < $START_THRESHOLD%"
      if [ $CURRENT -lt 0 ]; then
        echo "charger enabling"
        sleep 10
#        ectool chargecontrol normal <-- modify
      fi
   else
    echo "charger state OK, idling..."
   fi
  else
    echo "not on AC, idling..."
  fi
sleep 10
done

I changed the AC to tps6598x-source-psy-0-003f, the BAT0 to max170xx_battery and ‘cat …’ to $(cat …) . I also comment out the command ectool to check, if the script recocnizes the states. With my Librem5 it works.

The next step is to add the command to start and stop charging while the device is plugged to a dock or power charger. Does anybody know the command for that?

1 Like

@wolloloo let me understand: you run this script on your L5 and it manages battery charging?

What is the command ‘sleep 10’? You use this for each if situation, so I’m not following how the commands work at the 2 thresholds you have?

No. The script above only detects the status of the phone and writes it to the terminal. For example:
Phone plugged to dockingstation/power charger and battery capacity is 85%, the output of the script is

on AC
current=NUMBER
bat level > 80%
charger disabling

The script runs all the time because of the while-loop. Using sleep 10 forces the script to wait.

To implement any functionality of this script it is necessary to have a command to control the charging. In the example of @Miliki this is done with ectool. In my script, I comment this command out, so it has no effect.

Question:
If I use the Librem5 connected to my docking station and charge it permanently while using it, is it better
a) to hold the battery state constantly above 100%
b) start and stop charging, e.g. between 30% and 80%, while plugged in?

Background:
a) is the standard behaviour. b) has to be implemented e.g. as a service running in the background.

For b) the script mentioned above is a first idea. What’s missing is the right command to start/stop charging while plugged in to the docking station. Does anybody know it? Is it even possbile?

If the command that we are looking for existed, I am pretty sure somebody from Purism would have told us already.

Meanwhile, I have stumbled on an article that says that what we are trying to achieve here is in fact wrong, and it is better to keep it at 100% and charging. Giving of course that the Librem5 works in the same way he describes: “when your phone’s battery is reported at 100% and charging, the battery enters idle maintenance, which ensures that the battery is not going to self-discharge, without overcharging it. In this state, the phone can remain online and be powered by off external power from USB, rather than using the battery

https://gist.githubusercontent.com/Peter-Easton/4982f66e93387e02dd2c1d677d71f4f2/raw/4edf9a41ca3a4d053c8ad7bf365f97c923f01c61/battery-management.txt

I just read the matrix chat and saw that somebody else asked the same question. The information is written down in this repo:

My intension with the script is to stop charging above the upper threshold and support the current to the phone directly by the power supply, e.g. docking station, instead of discharging the bettery.

1 Like

For the record, charging stops automatically when the battery is full and then the whole system is powered from external supply (as long as it provides enough power - if not, the rest is taken from the battery), so I don’t think you need any script for the use case you described at all. Using chg_en is useful when you want to stop charging and power the device externally even though the battery isn’t full (I use it with a power meter to easily monitor power consumption, for example).

3 Likes

Thank you for the information. It is good to know, that the phone will switch from battery supply to external power supply when I use my librem5 all the day plugged in to my docking station.

@dos: Do you think, reducing the maximum battery charge to e.g. 80% while using the phone in docked mode all the time, is better for the battery?

Maybe I will create a desktop symbol to start and stop charging manually :thinking:

1 Like

I’ve been using the following to limit the charge voltage on the battery by reprogramming the charge controller chip:

i2cset -f -y 3 0x6a 0x06 0x46

In this case, it sets the stop charging threshold to 4.112 V, which is somewhere around 90%-ish. After it stops charging, the voltage settles to around 4.06V.

If you want to do it precisely by percentage, I believe there is also a register that will allow you to simply enable/disable the charge circuit as you please, but I can’t recall for certain what it is at the moment.

Important notes: messing about with the charge controller is potentially quite dangerous, as it will allow you to do things like set the charge voltage as high as 4.608V which would likely cause the battery to “vent with flames”, i.e. explode. There are also other gotchas like using the upper 6 bits of this register to set the voltage while the lower ones control other things. So, to modify it to use the voltage you want isn’t as simple as just decreasing the value one at a time.

Other notes: This settings is temporary until the next reboot. I believe uboot resets this and some other settings on each boot.

I think I’ll write up post describing more detail and how to set other values, and confirm or deny the enable/disable charge switch some time soon.
EDIT: Link: How to limit battery charge voltage and current

3 Likes