Handle active-low cover backlight and restore internal panel

This commit is contained in:
Luxferre committed 2026-11-11 07:57:38 +02:00
1 parent b389120328
commit d61f2848db
11 files changed
+412 -106

No files matched your search

@@ -7,19 +7,26 @@
# GC9305s share CS0, gpio110 D/C and one PMIC PWM backlight; gpio13 selects
# the panel, gpio23 is the external reset, and gpio21 is TE. While the flip is
# closed the daemon claims gpio13/gpio23, pulses reset, replays the stock
# 240x240 init through the kernel driver's debugfs command hook, enables the
# shared backlight and flushes a 240x240 /dev/fb0 damage rect. The keypad
# stays dark. Opening the flip releases the bus and re-initialises the main
# panel. auto = /dev/fb1 with the opt-in two-panel DT, otherwise spidev;
# fb/spidev force those legacy paths; none keeps the bus untouched.
# 240x240 init through the kernel driver's debugfs command hook, and flushes a
# 240x240 /dev/fb0 damage rect. Live probing showed the shared PWM is
# active-low for the cover and active-high for the main panel: duty 0 lights
# the cover, duty 14 lights the internal panel. The marker tells
# bananarchy-display-power-sync which side owns the bus. Opening the flip
# restores the mux, normal brightness and the internal panel. auto = /dev/fb1
# with the opt-in two-panel DT, otherwise spidev; fb/spidev force those legacy
# paths; none keeps the bus untouched.
backend = kernel
main_fb_device = /dev/fb0
kernel_dpms_helper = /usr/bin/bananarchy-cover-dpms.sh
kernel_power_sync_helper = /usr/bin/bananarchy-display-power-sync
kernel_command_path = /sys/kernel/debug/dri/0/command
kernel_backlight_path = /sys/class/backlight/backlight/brightness
kernel_backlight_power_path = /sys/class/backlight/backlight/bl_power
kernel_backlight_marker = /run/bananarchy/cover-backlight
# The main and cover panels share this backlight; 14 is the normal UI level.
# 14 is the normal internal-panel level and also the external-panel OFF duty.
kernel_backlight_level = 14
# 1 = external backlight active-low (duty 0 ON), 0 = active-high.
kernel_backlight_invert = 1
fb_device = /dev/fb1
spidev_device = /dev/spidev0.1
gpiochip_device = /dev/gpiochip0
Binary file not shown.
@@ -1,54 +1,130 @@
#!/bin/sh
# Keep DRM DPMS and the schedutil rate limit in sync with the backlight state.
# Invoked by the backlight udev rule (as root) and by sxmo_wm.sh display on
# (via doas). Every blank path writes brightness=0; every wake path restores it.
# (via doas). The 2720 Flip shares one PWM backlight between both panels:
# it is active-low for the cover and active-high for the main panel.
set -u
BR_PATH=/sys/class/backlight/backlight/brightness
BL_POWER_PATH=/sys/class/backlight/backlight/bl_power
KBD_PATH=/sys/class/leds/white:kbd_backlight/brightness
RL_PATH=/sys/devices/system/cpu/cpufreq/schedutil/rate_limit_us
COVER_MARKER=/run/bananarchy/cover-backlight
FORCE_MARKER=/run/bananarchy/cover-bl-force
LID_STATE=/run/bananarchy/lid-state
CONF=/etc/bananarchy/cover-display.conf
COVER_BL_LEVEL=14
COVER_BL_INVERT=1
# A forced PWM update rewrites brightness/bl_power itself. Ignore the nested
# udev events so the two-step update cannot recurse.
if [ -f "$FORCE_MARKER" ]; then
exit 0
fi
if [ -r "$CONF" ]; then
_level=$(awk -F= '/^[[:space:]]*kernel_backlight_level[[:space:]]*=/ {gsub(/[[:space:]]/, "", $2); print $2; exit}' "$CONF" 2>/dev/null)
case "${_level:-}" in ''|*[!0-9]*) ;; *) COVER_BL_LEVEL="$_level" ;; esac
_invert=$(awk -F= '/^[[:space:]]*kernel_backlight_invert[[:space:]]*=/ {gsub(/[[:space:]]/, "", $2); print $2; exit}' "$CONF" 2>/dev/null)
case "${_invert:-}" in 0|1) COVER_BL_INVERT="$_invert" ;; esac
fi
DPMS_PATH=
for _dpms in /sys/class/drm/card*-*/dpms; do
[ -r "$_dpms" ] && DPMS_PATH="$_dpms" && break
done
br=1
[ -r "$BR_PATH" ] && { read -r br < "$BR_PATH" 2>/dev/null || br=1; }
read_value() {
_path="$1"
_value=""
[ -r "$_path" ] && { read -r _value < "$_path" 2>/dev/null || _value=""; }
printf '%s' "$_value"
}
set_if_changed() {
_path="$1"
_want="$2"
_cur="$(read_value "$_path")"
[ "$_cur" = "$_want" ] || printf '%s\n' "$_want" > "$_path" 2>/dev/null || true
}
force_backlight() {
_want_br="$1"
_want_blp="$2"
printf '1\n' > "$FORCE_MARKER" 2>/dev/null || true
set_if_changed "$BL_POWER_PATH" "4"
printf '%s\n' "$_want_br" > "$BR_PATH" 2>/dev/null || true
set_if_changed "$BL_POWER_PATH" "$_want_blp"
printf '%s\n' "$_want_br" > "$BR_PATH" 2>/dev/null || true
rm -f "$FORCE_MARKER" 2>/dev/null || true
}
force_dpms() {
_want="$1"
_cur="$(read_value "$DPMS_PATH")"
[ "$_cur" = "$_want" ] && return 0
_mode="$(printf '%s' "$_want" | tr 'A-Z' 'a-z')"
DISPLAY=:0 xset dpms force "$_mode" >/dev/null 2>&1 || true
}
br="$(read_value "$BR_PATH")"
case "$br" in ''|*[!0-9]*) br=1 ;; esac
lid="$(read_value "$LID_STATE")"
marker=""
[ -r "$COVER_MARKER" ] && marker="$(read_value "$COVER_MARKER")"
cur=On
[ -r "$DPMS_PATH" ] && { read -r cur < "$DPMS_PATH" 2>/dev/null || cur=On; }
if [ "$COVER_BL_INVERT" -eq 1 ]; then
LIT_BR=0
DARK_BR="$COVER_BL_LEVEL"
DARK_BLP=0
else
LIT_BR="$COVER_BL_LEVEL"
DARK_BR=0
DARK_BLP=4
fi
# The main and cover panels share one PWM backlight. While the cover owns the
# bus, its daemon publishes this marker before raising brightness; the closed
# lid gate must not turn that shared backlight back off.
if [ -f "$COVER_MARKER" ] &&
[ "$(cat "$LID_STATE" 2>/dev/null)" = "closed" ]; then
printf '0\n' > "$KBD_PATH" 2>/dev/null || true
[ "$cur" = "On" ] || DISPLAY=:0 xset dpms force on >/dev/null 2>&1 || true
# An open flip always wins: remove a stale cover marker and restore the
# active-high internal-panel backlight before applying normal DPMS rules.
if [ "$lid" = "open" ]; then
rm -f "$COVER_MARKER" 2>/dev/null || true
force_backlight "$COVER_BL_LEVEL" "0"
force_dpms "On"
if [ -w "$RL_PATH" ]; then
printf '40000\n' > "$RL_PATH" 2>/dev/null || true
fi
exit 0
fi
# While the cover owns the bus, the marker content selects its state:
# 1 = cover lit, 0 = cover dark. Never let the closed-lid hard gate force
# brightness 0 here, because that would turn the active-low cover backlight on.
if [ -f "$COVER_MARKER" ]; then
set_if_changed "$KBD_PATH" "0"
if [ "$marker" = "1" ]; then
force_backlight "$LIT_BR" "0"
force_dpms "On"
else
force_backlight "$DARK_BR" "$DARK_BLP"
force_dpms "On"
fi
exit 0
fi
# Hard gate: while the flip is closed no path may relight the main panel or
# the keypad backlight. This also enforces DPMS-off, which is the condition
# the cover daemon needs before it takes the shared QUP5 SPI bus.
# the keypad backlight. The cover daemon keeps gpio13 low in this state.
if ! "${BANANARCHY_LID_GATE:-/usr/bin/bananarchy-lid-gate.sh}"; then
printf '0\n' > "$KBD_PATH" 2>/dev/null || true
[ "$br" -eq 0 ] || printf '0\n' > "$BR_PATH" 2>/dev/null || true
[ "$cur" = "Off" ] || DISPLAY=:0 xset dpms force off >/dev/null 2>&1 || true
set_if_changed "$KBD_PATH" "0"
force_backlight "0" "4"
force_dpms "Off"
exit 0
fi
if [ "$br" -eq 0 ]; then
[ "$cur" = "Off" ] || DISPLAY=:0 xset dpms force off >/dev/null 2>&1 || true
force_dpms "Off"
else
[ "$cur" = "On" ] || DISPLAY=:0 xset dpms force on >/dev/null 2>&1 || true
force_dpms "On"
fi
# Switching scaling_governor away from schedutil and back recreates this
# kobject with the 6.44 s default, so re-apply the 40 ms value on every wake.
if [ "$br" -ne 0 ] && [ -w "$RL_PATH" ]; then
printf '40000\n' > "$RL_PATH" 2>/dev/null || true
fi