1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
|
#!/bin/sh
# 56-framework - Battery Plugin for Framework laptops:
# 1. Framework 13/16 AMD or Intel
#
# Requires the out-of-tree module framework_laptop at https://github.com/DHowett/framework-laptop-kmod
# providing the following sysfs nodes:
# * /sys/class/power_supply/BAT[01]/charge_control_end_threshold: 1..100
#
# Copyright (c) 2025 Thomas Koch <linrunner at gmx.net> and others.
# SPDX-License-Identifier: GPL-2.0-or-later
# Needs: tlp-func-base, 35-tlp-func-batt, tlp-func-stat
# --- Hardware Detection
readonly BATDRV_FRAMEWORK_DL='/sys/bus/platform/devices/framework_laptop/driver'
batdrv_is_framework () {
# check if kernel module is active i.e. owns a device
# rc: 0=Framework laptop, 1=other hardware
local drvl
drvl="$(readlink $BATDRV_FRAMEWORK_DL)"
[ "${drvl##*/}" = "framework_laptop" ]
}
# --- Plugin API functions
batdrv_init () {
# detect hardware and initialize driver
# rc: 0=matching hardware detected/1=not detected/2=no batteries detected
# retval: $_batdrv_plugin, $_batdrv_kmod
#
# 1. check for native kernel acpi (out-of-tree module framework_laptop required)
# --> retval $_natacpi:
# 0=thresholds/
# 32=disabled/
# 128=no kernel support/
# 254=laptop not supported (incompatible hard- or firmware)
#
# 2. determine method for
# reading battery data --> retval $_bm_read,
# reading/writing charging thresholds --> retval $_bm_thresh,
# reading/writing force discharge --> retval $_bm_dischg:
# none/natacpi
#
# 3. define sysfile basenames for natacpi
# stop threshold --> retval $_bn_stop,
#
# 4. determine present batteries
# list of batteries (space separated) --> retval $_batteries;
#
# 5. define charge threshold defaults
# stop threshold --> retval $_bt_def_stop;
_batdrv_plugin="framework"
_batdrv_kmod="framework_laptop" # kernel module for natacpi
# check plugin simulation override and denylist
if [ -n "$X_BAT_PLUGIN_SIMULATE" ]; then
if [ "$X_BAT_PLUGIN_SIMULATE" = "$_batdrv_plugin" ]; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.simulate"
else
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.simulate_skip"
return 1
fi
elif wordinlist "$_batdrv_plugin" "$X_BAT_PLUGIN_DENYLIST"; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.denylist"
return 1
else
# check if hardware matches
if ! batdrv_is_framework; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.no_match"
return 1
fi
fi
# presume no features at all
_natacpi=128
_bm_read="natacpi"
_bm_thresh="none"
_bm_dischg="none"
_bn_start=""
_bn_stop=""
_bn_dischg=""
_batteries=""
_bt_def_stop=100
# iterate batteries and check for native kernel ACPI
local bd bs
local done=0
for bd in "$ACPIBATDIR"/BAT[01]; do
if [ "$(read_sysf "$bd/present")" = "1" ]; then
# record detected batteries
bs=${bd##/*/}
if [ -n "$_batteries" ]; then
_batteries="$_batteries $bs"
else
_batteries="$bs"
fi
# skip natacpi detection for 2nd and subsequent batteries
[ $done -eq 1 ] && continue
done=1
if [ "$NATACPI_ENABLE" = "0" ]; then
# natacpi disabled in configuration --> skip actual detection
_natacpi=32
continue
fi
if readable_sysf "$bd/charge_control_end_threshold"; then
# threshold sysfile is actually readable
_bn_stop="charge_control_end_threshold"
_bm_thresh="natacpi"
_natacpi=0
else
# nothing detected -> unsupported model
_natacpi=254
continue
fi
fi
done
# quit if no battery detected, there is no point in activating the plugin
if [ -z "$_batteries" ]; then
echo_debug "bat" "batdrv_init.${_batdrv_plugin}.no_batteries"
return 2
fi
# shellcheck disable=SC2034
_batdrv_selected=$_batdrv_plugin
echo_debug "bat" "batdrv_init.${_batdrv_plugin}: batteries=$_batteries; natacpi=$_natacpi; thresh=$_bm_thresh; bn_start=$_bn_start; bn_stop=$_bn_stop; dischg=$_bm_dischg; bn_dischg=$_bn_dischg"
return 0
}
batdrv_select_battery () {
# determine battery acpidir and sysfiles
# $1: BAT0/BAT1/DEF
# global params: $_batdrv_plugin, $_batteries, $_bn_start, $_bn_stop, $_bn_dischg
# rc: 0=bat exists/1=bat non-existent
# retval: $_bat_str: BAT0/BAT1/<other>;
# $_bt_cfg_bat: config suffix (BAT0/BAT1);
# $_bd_read: directory with battery data sysfiles;
# $_bf_stop: sysfile for stop threshold;
# prerequisite: batdrv_init()
# defaults
_bat_str="" # no bat
_bt_cfg_bat=""
_bd_read="" # no directory
_bf_stop=""
local bat="$1"
# convert battery param to uppercase
bat="$(printf '%s' "$bat" | tr "[:lower:]" "[:upper:]")"
# validate battery param
case "$bat" in
DEF) # 1st battery is default
_bat_str="${_batteries%% *}"
;;
*)
if wordinlist "$bat" "$_batteries"; then
_bat_str="$bat"
else
# battery not present --> quit
echo_debug "bat" "batdrv.${_batdrv_plugin}.select_battery($1).not_present"
return 1
fi
;;
esac
# config suffix equals battery name
_bt_cfg_bat="$_bat_str"
# determine natacpi sysfile
_bd_read="$ACPIBATDIR/$_bat_str"
if [ "$_bm_thresh" = "natacpi" ]; then
_bf_stop="$ACPIBATDIR/$_bat_str/$_bn_stop"
fi
echo_debug "bat" "batdrv.${_batdrv_plugin}.select_battery($1): bat_str=$_bat_str; cfg=$_bt_cfg_bat; bd_read=$_bd_read; bf_stop=$_bf_stop"
return 0
}
batdrv_read_threshold () {
# read and print charge threshold (stop only)
# $1: start/stop -- unused dummy for plugin api compatibility
# $2: 0=api/1=tlp-stat output
# global params: $_batdrv_plugin, $_bm_thresh, $_bf_start, $_bf_stop
# out:
# - api: 1..100/"" on error
# - tlp-stat: 1..100/"(not available)" on error
# rc: 0=ok/4=read error/255=no api
# prerequisite: batdrv_init(), batdrv_select_battery()
local out rc=0
out="$X_THRESH_SIMULATE_STOP"
if [ -n "$out" ]; then
printf "%s" "$out"
echo_debug "bat" "batdrv.${_batdrv_plugin}.read_threshold($1, $2).simulate: bm_thresh=$_bm_thresh; bf_stop=$_bf_stop; out=$out; rc=$rc"
return 0
fi
if [ "$_bm_thresh" = "natacpi" ]; then
if ! out=$(read_sysf "$_bf_stop"); then
# not readable/non-existent
if [ "$2" != "1" ]; then
out=""
else
out="(not available)"
fi
rc=4
fi
else
# no threshold api
if [ "$2" = "1" ]; then
out="(not available)"
fi
rc=255
fi
# "return" threshold
if [ "$X_THRESH_SIMULATE_READERR" != "1" ]; then
printf "%s" "$out"
else
if [ "$2" = "1" ]; then
printf "(not available)\n"
fi
rc=4
fi
echo_debug "bat" "batdrv.${_batdrv_plugin}.read_threshold($1, $2): bm_thresh=$_bm_thresh; bf_stop=$_bf_stop; out=$out; rc=$rc"
return $rc
}
batdrv_write_thresholds () {
# write charge thresholds for a battery
# use pre-determined method and sysfiles from global parms
# $1: new start threshold -- unused dummy for plugin api compatibility
# $2: new stop threshold 1..100/DEF(default)
# $3: 0=quiet/1=output parameter errors/2=output progress and errors
# $4: non-empty string indicates thresholds stem from configuration
# global params: $_batdrv_plugin, $_bm_thresh, $_bat_str, $_bt_cfg_bat, $_bf_stop
# rc: 0=ok/
# 1=not configured/
# 2=threshold out of range or non-numeric/
# 4=threshold read error/
# 5=threshold write error
# prerequisite: batdrv_init(), batdrv_select_battery()
local new_stop=${2:-}
local verb=${3:-0}
local old_stop
# insert defaults
[ "$new_stop" = "DEF" ] && new_stop=$_bt_def_stop
# --- validate thresholds
if [ -n "$4" ] && [ -z "$new_stop" ]; then
# do nothing if unconfigured
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).not_configured: bat=$_bat_str; cfg=$_bt_cfg_bat"
return 1
fi
# stop: check for 3 digits max, ensure min 1 / max 100
if ! is_uint "$new_stop" 3 || \
! is_within_bounds "$new_stop" 1 100; then
# threshold out of range
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).invalid_stop: bat=$_bat_str; cfg=$_bt_cfg_bat"
case $verb in
1)
if [ -n "$4" ]; then
echo_message "Error in configuration at STOP_CHARGE_THRESH_${_bt_cfg_bat}=\"${new_stop}\": not specified, invalid or out of range (1..100). Battery skipped."
fi
;;
2)
if [ -n "$4" ]; then
cprintf "" "Error in configuration at STOP_CHARGE_THRESH_%s=\"%s\": not specified, invalid or out of range (1..100). Aborted.\n" "$_bt_cfg_bat" "$new_stop" 1>&2
else
cprintf "" "Error: stop charge threshold (%s) for %s is not specified, invalid or out of range (1..100). Aborted.\n" "$new_stop" "$_bat_str" 1>&2
fi
;;
esac
return 2
fi
# read active threshold value
if ! old_stop=$(batdrv_read_threshold stop 0); then
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).read_error: bat=$_bat_str; cfg=$_bt_cfg_bat"
case $verb in
1) echo_message "Error: could not read current charge threshold for $_bat_str. Battery skipped." ;;
2) cprintf "" "Error: could not read current charge threshold for %s. Aborted.\n" "$_bat_str" 1>&2 ;;
esac
return 4
fi
# write new threshold
local rc=0
if [ "$verb" = "2" ]; then
printf "Setting temporary charge threshold(s) for battery %s:\n" "$_bat_str" 1>&2
fi
if [ "$old_stop" != "$new_stop" ]; then
# new threshold differs from effective one --> write it
write_sysf "$new_stop" "$_bf_stop" || rc=5
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).write: bat=$_bat_str; cfg=$_bt_cfg_bat; old=$old_stop; new=$new_stop; rc=$rc"
case $verb in
2)
if [ $rc -eq 0 ]; then
printf " stop = %3d\n" "$new_stop" 1>&2
else
cprintf "err" " stop = %3d (Error: write failed)\n" "$new_stop" 1>&2
fi
;;
1)
if [ $rc -gt 0 ]; then
echo_message "Error: writing stop charge threshold for $_bat_str failed."
fi
;;
esac
else
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_thresholds($1, $2, $3, $4).no_change: bat=$_bat_str; cfg=$_bt_cfg_bat; old=$old_stop; new=$new_stop"
if [ "$verb" = "2" ]; then
printf " %-5s = %3d (no change)\n" "stop" "$new_stop" 1>&2
fi
fi
return $rc
}
# shellcheck disable=SC2120
batdrv_calc_soc () {
# calc and print battery charge level (rounded)
# $1: format (optional)
# global param: $_bd_read
# prerequisite: batdrv_init(), batdrv_select_battery()
# rc: 0=ok/1=charge level read error
local cf cn
if [ -f "$_bd_read/charge_full" ]; then
# real hardware
cf=$(read_sysval "$_bd_read/charge_full")
cn=$(read_sysval "$_bd_read/charge_now")
elif [ -f "$_bd_read/energy_full" ]; then
# simulation - on ThinkPad only
cf=$(read_sysval "$_bd_read/energy_full")
cn=$(read_sysval "$_bd_read/energy_now")
else
cf=0
cn=0
fi
if [ "$cf" != "0" ]; then
if [ -n "$1" ]; then
perl -e 'printf ("'"$1"'", 100.0 * '"$cn"' / '"$cf"')'
else
perl -e 'printf ("%d", int(100.0 * '"$cn"' / '"$cf"' + 0.5))'
fi
return 0
else
printf "255"
return 1
fi
}
batdrv_chargeonce () {
# function not implemented
# global param: $_batdrv_plugin
# prerequisite: batdrv_init()
echo_debug "bat" "batdrv.${_batdrv_plugin}.charge_once.not_implemented"
return 255
}
batdrv_apply_configured_thresholds () {
# apply configured stop thresholds from configuration to all batteries
# - called for bg tasks tlp init [re]start/auto and tlp start
# output parameter errors only
# prerequisite: batdrv_init()
local bat stop_thresh
for bat in BAT0 BAT1; do
if batdrv_select_battery "$bat"; then
eval stop_thresh="\$STOP_CHARGE_THRESH_${_bt_cfg_bat}"
batdrv_write_thresholds "" "$stop_thresh" 1 1
fi
done
return 0
}
batdrv_read_force_discharge () {
# function not implemented
# global param: $_batdrv_plugin
# prerequisite: batdrv_init()
echo_debug "bat" "batdrv.${_batdrv_plugin}.read_force_discharge.not_implemented"
return 255
}
batdrv_write_force_discharge () {
# function not implemented
# global param: $_batdrv_plugin
# prerequisite: batdrv_init()
echo_debug "bat" "batdrv.${_batdrv_plugin}.write_force_discharge.not_implemented"
return 255
}
batdrv_cancel_force_discharge () {
# function not implemented
# global param: $_batdrv_plugin
# prerequisite: batdrv_init()
echo_debug "bat" "batdrv.${_batdrv_plugin}.cancel_force_discharge.not_implemented"
return 255
}
batdrv_force_discharge_active () {
# function not implemented
# global param: $_batdrv_plugin
# prerequisite: batdrv_init()
echo_debug "bat" "batdrv.${_batdrv_plugin}.force_discharge_active.not_implemented"
return 255
}
batdrv_discharge_safetylock () {
# check safety lock - ThinkPads implement force-discharge sanely and do not need a safety lock
# $1: discharge/recalibrate
# rc: 0=engaged/1=disengaged
# ThinkPads implement force-discharge sanely and do not need a safety lock
return 1
}
batdrv_discharge () {
# function not implemented
# global param: $_batdrv_plugin
# prerequisite: batdrv_init()
# Important: release lock from caller
unlock_tlp tlp_discharge
echo_debug "bat" "batdrv.${_batdrv_plugin}.discharge.not_implemented"
return 255
}
batdrv_show_battery_data () {
# output battery status
# $1: 1=verbose
# global params: $_batdrv_plugin, $_batteries, $_batdrv_kmod, $_bd_read, $_bf_start, $_bf_stop, $_bf_dischg
# prerequisite: batdrv_init()
local verbose=${1:-0}
printf "+++ Battery Care\n"
printf "Plugin: %s\n" "$_batdrv_plugin"
if [ "$_bm_thresh" != "none" ]; then
cprintf "success" "Supported features: charge threshold\n"
else
cprintf "warning" "Supported features: none available\n"
fi
printf "Driver usage:\n"
# native kernel ACPI battery API
case $_natacpi in
0) cprintf "success" "* natacpi (%s) = active (charge threshold)\n" "$_batdrv_kmod" ;;
32) cprintf "notice" "* natacpi (%s) = inactive (disabled by configuration)\n" "$_batdrv_kmod" ;;
128) cprintf "err" "* natacpi (%s) = inactive (no kernel support)\n" "$_batdrv_kmod" ;;
254) cprintf "warning" "* natacpi (%s) = inactive (laptop model not supported)\n" "$_batdrv_kmod" ;;
*) cprintf "err" "* natacpi (%s) = unknown status\n" "$_batdrv_kmod" ;;
esac
if [ "$_bm_thresh" != "none" ]; then
printf "Parameter value ranges:\n"
printf "* STOP_CHARGE_THRESH_BAT0/1: 1..100(default)\n"
fi
printf "\n"
# -- show battery data
local bat
local bcnt=0
local ed ef en
local efsum=0
local ensum=0
for bat in $_batteries; do # iterate batteries
batdrv_select_battery "$bat"
printf "+++ Battery Status: %s\n" "$bat"
printparm "%-59s = ##%s##" "$_bd_read/manufacturer"
printparm "%-59s = ##%s##" "$_bd_read/model_name"
print_battery_cycle_count "$_bd_read/cycle_count" "$(read_sysf "$_bd_read/cycle_count")"
if [ -f "$_bd_read/energy_full" ]; then
printparm "%-59s = ##%6d## [mWh]" "$_bd_read/energy_full_design" "" 000
printparm "%-59s = ##%6d## [mWh]" "$_bd_read/energy_full" "" 000
printparm "%-59s = ##%6d## [mWh]" "$_bd_read/energy_now" "" 000
printparm "%-59s = ##%6d## [mW]" "$_bd_read/power_now" "" 000
# store values for charge / capacity calculation below
ed=$(read_sysval "$_bd_read/energy_full_design")
ef=$(read_sysval "$_bd_read/energy_full")
en=$(read_sysval "$_bd_read/energy_now")
efsum=$((efsum + ef))
ensum=$((ensum + en))
elif [ -f "$_bd_read/charge_full" ]; then
printparm "%-59s = ##%6d## [mAh]" "$_bd_read/charge_full_design" "" 000
printparm "%-59s = ##%6d## [mAh]" "$_bd_read/charge_full" "" 000
printparm "%-59s = ##%6d## [mAh]" "$_bd_read/charge_now" "" 000
printparm "%-59s = ##%6d## [mA]" "$_bd_read/current_now" "" 000
# store values for charge / capacity calculation below
ed=$(read_sysval "$_bd_read/charge_full_design")
ef=$(read_sysval "$_bd_read/charge_full")
en=$(read_sysval "$_bd_read/charge_now")
efsum=$((efsum + ef))
ensum=$((ensum + en))
else
ed=0
ef=0
en=0
fi
print_batstate "$_bd_read/status"
printf "\n"
if [ "$verbose" -eq 1 ]; then
printparm "%-59s = ##%6s## [mV]" "$_bd_read/voltage_min_design" "" 000
printparm "%-59s = ##%6s## [mV]" "$_bd_read/voltage_now" "" 000
printf "\n"
fi
# --- show battery features: thresholds
printf "%-59s = %6s [%%]\n" "$_bf_stop" "$(batdrv_read_threshold stop "1")"
printf "\n"
# --- show charge level (SOC) and capacity
lf=0
if [ "$ef" -ne 0 ]; then
perl -e 'printf ("%-59s = %6.1f [%%]\n", "Charge", 100.0 * '"$en"' / '"$ef"');'
lf=1
fi
if [ "$ed" -ne 0 ]; then
perl -e 'printf ("%-59s = %6.1f [%%]\n", "Capacity", 100.0 * '"$ef"' / '"$ed"');'
lf=1
fi
[ "$lf" -gt 0 ] && printf "\n"
bcnt=$((bcnt+1))
done # for bat
if [ $bcnt -gt 1 ] && [ $efsum -ne 0 ]; then
# more than one battery detected --> show charge total
perl -e 'printf ("%-59s = %6.1f [%%]\n", "+++ Charge total", 100.0 * '"$ensum"' / '"$efsum"');'
printf "\n"
fi
return 0
}
batdrv_check_soc_gt_stop () {
# check if battery charge level (SOC) is greater than the stop threshold
# rc: 0=greater/1=less or equal (or thresholds not supported)
# global params: $_bm_thresh, $_bat_str
# prerequisite: batdrv_init(), batdrv_select_battery()
local soc stop
if [ "$_bm_thresh" = "natacpi" ] && soc="$(batdrv_calc_soc)"; then
stop="$(batdrv_read_threshold stop 0)"
if [ -n "$stop" ] && [ "$soc" -gt "$stop" ]; then
return 0
fi
fi
return 1
}
batdrv_recommendations () {
# output specific recommendations
# prerequisite: batdrv_init()
soc_gt_stop_recommendation
return 0
}
|