Screen Brightness Keys / i3 - Lenovo P14s Gen2 (AMD)

I’m currently migrating from my old Lenovo X230 (Ubuntu/i3 @Intel) to an new Lenovo P14s Gen2 (NixOS 21.05 /i3 @AMD Ryzen7).
NixOS Hw config is pulled from: <nixos-hardware/lenovo/thinkpad/p14s/amd/gen2>

One of the things which aren’t working out of the box are the “screen brightness” control keys (Fn+F5 & Fn+F6).

On the old Ubuntu system I had set up event handling scripts in i3 for key codes XF86BrightnessUp and XF86BrightnessDown.
But this approach doesn’t work on the P14s / NixOS setup.
xev doesn’t report those key codes on key press.

Question 1:
Does anybody has an idea why XF86BrightnessUp and XF86BrightnessDown aren’t recognised by xev?
Any packages, drivers, kernel options that I’m missing?

Question 2:
I found a solution that I was able to adapt on NixOS. In my eyes as a newbie this looks a bit ugly / hackish.
Any suggestions to improve this nix-code?

{ config, pkgs, ... }:                                                                               
                                                                                                
let                                                                                                  
  p14s-backlight-inc = pkgs.writeShellScriptBin "backlight-increase" ''                              
     bl_device=/sys/class/backlight/amdgpu_bl0/brightness                                             
     echo $(($(cat $bl_device)+10)) | tee $bl_device                                                  
  '';                                                                                                
                                                                                                      
  p14s-backlight-dec = pkgs.writeShellScriptBin "backlight-decrease" ''                              
    bl_device=/sys/class/backlight/amdgpu_bl0/brightness                                             
    echo $(($(cat $bl_device)-10)) | tee $bl_device                                                  
  '';                                                                                                      
in {                                                                                                 
  environment.systemPackages = [                                                                     
     pkgs.acpid                                                                                      
     p14s-backlight-inc                                                                              
     p14s-backlight-dec                                                                              
  ];                                                                                                 

  services.acpid.handlers = {                                                                        
    "p14bl-inc" = {                                                                                  
      action =  "${p14s-backlight-inc}/bin/backlight-increase";                                      
      event =  "video/brightnessup BRTUP 00000086 00000000";                                         
     };                                                                                              
    "p14bl-dec" = {                                                                                  
      action = "${p14s-backlight-dec}/bin/backlight-decrease";                                       
      event = "video/brightnessdown BRTDN 00000087 00000000";                                        
     };                                                                                              
  };                                                                                                                                                                                                 
} 

Looks related to amd graphics - Backlight control not working on Lenovo IdeaPad Gaming 3 with Renoir / AMDGPU - Unix & Linux Stack Exchange

These just work for me due to https://github.com/NixOS/nixos-hardware/blob/878f629005b003fe39c9e619b074e0ff7d9ed0e2/lenovo/thinkpad/p14s/amd/gen2/default.nix#L13

Can you post dmesg to a gist?

1 Like

What is the output of:

ls /sys/class/backlight/

@domenkozar
I’m using your hardware.nix. But unfortunately XF86BrightnessUp and XF86BrightnessDown keycodes are not recognised.
nix-shell -p xorg.xev --run "xev -event keyboard" | grep XF86 only report keycode 151 (= FN key is pressed)

ls /sys/class/backlight/ prints amdgpu_bl0.

dmesg output is available here:
https://gist.github.com/itc-ger/537a15c80950c4556174841f7e4ac037#file-p14s-dmesg-brightness-debug

some interesting findings are
[ 0.303902] ACPI: [Firmware Bug]: BIOS _OSI(Linux) query ignored

[    7.974158] thinkpad_acpi: This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver
[    7.974159] thinkpad_acpi: Disabling thinkpad-acpi brightness events by default...

[ 8.115285] thinkpad_acpi: Standard ACPI backlight interface available, not loading native one

I’d first make sure that it’s not a problem with backlight, rather than mapping of the keys.

Run as root to change brightness:

$ echo 10 > /sys/class/backlight/amdgpu_bl0/brightness

You should see the screen dim and confirm the value:

$ cat /sys/class/backlight/amdgpu_bl0/actual_brightness
10

Can you use illum? I’ve been using it on my thinkpads for years. It hooks in to /dev/input below X so it works even on the console. It does very rarely crash and need to be restarted.

1 Like

@domenkozar
Sorry for my misleading issue description. The nix solution I wrote in the first post is actually working fine.
But I have to use that ugly acpid-hack instead of trigger XF86BrightnessUp key events. :slight_smile:

The Problem with the acpid soltuion is, that it isn’t very portable.
Now I’ve to maintain two different approaches for backlight control in my multi machine nixos-config collection. The acpid one for the Lenovo P14s Gen 2, and XF86BrightnessUp in the i3 config for my Lenovo X230.

tried that solution, it worked great … thanks!