Skip to content

Instantly share code, notes, and snippets.

@vuori
Created August 5, 2020 22:32
Show Gist options
  • Save vuori/9cb5ff09b059a576faed9f4366d9c0be to your computer and use it in GitHub Desktop.
Save vuori/9cb5ff09b059a576faed9f4366d9c0be to your computer and use it in GitHub Desktop.
Shell script to set battery charge limits on Thinkpads with post-2018 kernels (acpi_call kernel module not needed)
#!/bin/bash
# On post-2018 kernels the Thinkpad charge threshold mechanism has
# been included in the thinkpad-acpi module. Hence the tpacpi-bat
# tool (and the acpi_call kernel module) are no longer necessary;
# you can just set the values in sysfs.
# Stop charging at this percentage
STOP_AT=80
# Start charging at this percentage
START_AT=75
# When charging to full capacity, restart at this percentage
START_AT_FULL=95
start_dev=/sys/class/power_supply/BAT0/charge_start_threshold
stop_dev=/sys/class/power_supply/BAT0/charge_stop_threshold
# Do this in a difficult way because the order of settings matters.
# args: start-percent stop-percent
do_set() {
( echo $1 > $start_dev ) 2> /dev/null
echo $2 > $stop_dev
echo $1 > $start_dev
}
case "$1" in
limit)
do_set $START_AT $STOP_AT
;;
full)
do_set $START_AT_FULL 100
;;
status)
start=$(cat $start_dev)
# 0 = function disabled
[[ $start = 0 ]] && start=100
stop=$(cat $stop_dev)
echo "start: $start% stop: $stop%"
;;
*)
echo "usage: $0 limit|full|status"
false
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment