Arch brightness management


Background: in October 2024, after what I think was a kernel update, my AMD Thinkpad X13 started to manifest an annoying behaviour: when waking up from sleep (i.e. after opening the lid), the screen brightness was always back to 255 (maximum brightness). This is what I did to fix the issue.

NOTE: This is not a problem on my Thinkpad X1 Carbon Gen 11, only my X13.

Documentation

Where are the values?

I found the brightness values in /sys/class/backlight/amdgpu_bl1.

Files are:

The fix

I created the following script:

/usr/local/bin/amdbrightness

Note: to set the BRIGHTNESS_PCI_FILE variable, I ran lspci to find out the pci value to use.

#!/bin/bash

BRIGHTNESS_PCI_FILE="pci-0000:05:00.0:backlight:amdgpu_bl1"
SYSTEMD_DIR="/var/lib/systemd/backlight"
SYSTEMD_FILE="${SYSTEMD_DIR}/${BRIGHTNESS_PCI_FILE}"
BRIGHTNESS_FILE="/sys/class/backlight/amdgpu_bl1/brightness"

main() {
	[ -z $1 ] && usage
	case "${1}" in
		save)    check_root $1 ; save ;;
		restore) check_root $1 ; restore ;;
		status)  status ;;
		*) echo "${1}: invalid!" ; exit 42 ;;
	esac
}

check_root() {
	[ $(id -u) -eq 0 ] || {
		echo -e "\e[31;1mYou have to be root to run $1!\e[m"
		exit 42
	}
}

save() {
	cat "${BRIGHTNESS_FILE}" > "${SYSTEMD_FILE}"
	echo "Brightness saved to: $(cat ${BRIGHTNESS_FILE})"
}

restore() {
	sleep .75
	cat "${SYSTEMD_FILE}" > "${BRIGHTNESS_FILE}"
	echo "Brightness restored to: $(cat ${BRIGHTNESS_FILE})"
}

status() {
	buffer=$(mktemp)
	for file in "${SYSTEMD_FILE}" "${BRIGHTNESS_FILE}" ; do
		echo -n "${file} " >> "${buffer}"
		cat "${file}" >> "${buffer}"
	done
	cat "${buffer}" | column -t
	rm "${buffer}"
}

usage() {
	echo "$(basename $0) save|restore|status"
	exit 23
}

main ${@}
I associated this script with the following systemd services:

/etc/systemd/system/amdbrightness-save.service

[Unit]
Description=save brightness before sleeping
Before=suspend.target

[Service]
User=root
Type=oneshot
ExecStart=/usr/local/bin/amdbrightness save

[Install]
WantedBy=suspend.target

/etc/systemd/system/amdbrightness-restore.service

[Unit]
Description=restore brightness at wake up from sleep
After=suspend.target

[Service]
User=root
Type=oneshot
ExecStart=/usr/local/bin/amdbrightness restore

[Install]
WantedBy=suspend.target

I then installed the services

$ sudo systemctl daemon-reload
$ sudo systemctl enable --now amdbrightness-save.service
$ sudo systemctl enable --now amdbrightness-restore.service