拯救者R9000X 2021R无法调整亮度的workaround

r9000x model:
r7 5800h
rtx3060
linux mint 21.2 (kernel 5.15)

linux通过acpi控制背光的在/sys/class/backlight
如果是一个核显本 或者台式机,只会有一个文件夹本质上就是往brightness里写值。

通过实验,r9000x混合模式会出现amdgpu_bl0、nvidia_0和nvidia_wmi_ec_backlight,系统写的是nvidia_wmi_ec_backlight,真正起作用的是amdgpu_bl0,独显模式只有后面俩,而且俩都不起作用,所以只能用核显输出的混合模式,然后通过inotify检测/sys/class/backlight/nvidia_wmi_ec_backlight/brightness变化,同步写入/sys/class/backlight/amdgpu_bl0/brightness,注意二者max_brightness还不一样,nvidia_wmi_ec_backlight最大是100,amdgpu是255,还得做个按比例缩放。

redirect-brightness bash script:

#!/bin/bash

# NAME: redirect-brightness
# PATH: /usr/local/bin
# DESC: Redirect to correct acpi
#       /sys/class/DRIVER_NAME/brightness

WatchDriver="/sys/class/backlight/nvidia_wmi_ec_backlight"
PatchDriver="/sys/class/backlight/amdgpu_bl0"

# Must be running as sudo
if [[ $(id -u) != 0 ]]; then
    echo >&2 "Root access required. Use: 'sudo redirect-brightness'"
    exit 1
fi

# inotifywait required
type inotifywait >/dev/null 2>&1 || \
    { echo >&2 "'inotifywait' required but it's not installed.  Aborting."; \
      echo >&2 "Use 'sudo apt install inotify-tools' to install it.'"; \
      exit 1; }

# Was right watch driver directory name setup correctly?
if [[ ! -d $WatchDriver ]]; then
    echo >&2 "Watch directory: '$WatchDriver'"; \
    echo >&2 "does not exist. Did you spell it correctly? Aborting.'"; \
    exit 1;
fi

# Was right patch driver directory name setup correctly?
if [[ ! -d $PatchDriver ]]; then
    echo >&2 "Redirect to directory: '$PatchDriver'"; \
    echo >&2 "does not exist. Did you spell it correctly? Aborting.'"; \
    exit 1;
fi

# Get maximum brightness values
WatchMax=$(cat $WatchDriver/max_brightness)
PatchMax=$(cat $PatchDriver/max_brightness)

# PARM: 1="-l" or "--log-file" then write each step to log file.
fLogFile=false
if [[ $1 == "-l" ]] || [[ $1 == "--log-file" ]]; then
    fLogFile=true
    LogFile=/tmp/redirect-brightness.log
    echo redirect-brightness LOG FILE > $LogFile
    echo WatchMax: $WatchMax PatchMax: $PatchMax >> $LogFile
fi

SetBrightness () {
    # Calculate watch current percentage
    WatchAct=$(cat $WatchDriver/actual_brightness)
    WatchPer=$(( WatchAct * 100 / WatchMax ))
    [[ $fLogFile == true ]] && echo WatchAct: $WatchAct WatchPer: $WatchPer >> $LogFile
    # Reverse engineer patch brightness to set
    PatchAct=$(( PatchMax * WatchPer / 100 ))
    echo $PatchAct | sudo tee $PatchDriver/brightness
    [[ $fLogFile == true ]] && echo PatchAct: $PatchAct >> $LogFile
}

# When machine boots, set brightness to last saved value
SetBrightness

# Wait forever for user to press Fn keys adjusting brightness up/down.
while (true); do
    inotifywait --event modify $WatchDriver/actual_brightness
    [[ $fLogFile == true ]] && \
        echo "Processing modify event in $WatchDriver/actual_brightness" >> $LogFile
    SetBrightness
done

然后做个定时任务每次开机自动执行下:

# NAME: redirect-brightness-on-boot
# PATH: /etc/cron.d/
# DESC: run redirect-brightness on boot

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@reboot   root    /usr/local/bin/redirect-brightness

reference: https://askubuntu.com/questions/1045624/how-to-switch-between-options-in-sys-class-backlight-to-solve-brightness-prob#

此处评论已关闭