Skip to content

Power on with duration; power on if brightness > 0; fix backlight device selection on KMS systems; handle multiple backlight sysfs nodes correctly#68

Open
p1r473 wants to merge 2 commits into
linusg:mainfrom
p1r473:main
Open

Power on with duration; power on if brightness > 0; fix backlight device selection on KMS systems; handle multiple backlight sysfs nodes correctly#68
p1r473 wants to merge 2 commits into
linusg:mainfrom
p1r473:main

Conversation

@p1r473

@p1r473 p1r473 commented Mar 19, 2025

Copy link
Copy Markdown
Contributor

Hi Linus,
On my Pi, I am able to successfully fade with a duration with rpi-backlight -b 0 -d 3. However, I cant power on/off with a duration rpi-backlight -p off -d 3. I can, however, toggle with a duration just fine rpi-backlight -p toggle -d 3
I believe we should be able to power on/off with a duration based on this line in the code, showing the intention that power setting may indeed be set with a duration.
parser.error("-p/--set-power may only be used with -d/--duration")

After checking the code, I found that powering on/off with a duration wasn't actually implemented yet
So, I added it as a feature:
-Power on/off is now supported with duration

Additionally, I found that if you set the brightness to 100 while the power is off, nothing happens. So, I added an extra 2 features:
-If setting brightness >0, turn the power on first. (This will better the user experience)
-If setting brightness to 0, turn the power off once brightness is 0. (Perhaps $ energy savings?)

I also added a quick check to the code.
-Do not try to power on if already on (This will prevent the brightness from setting to 100 if brightness is already turned up (i.e. 40) and you try to power on) which fixes #44

There is a difference between power and toggle however that needs to be reflected in the code I believe.
If its on, and you try to turn it on, nothing should happen.
If its on, and you try to toggle it, it should turn off.

Toggle does not have this problem, because if you toggle it while its already on, it will just turn off

This code prevents setting the brightness to 100 if lets say, brightness is currently 40 and you power on. Without this code, if you power on an and the screen is already, brightness will just go to 100, which is probably not what the user wanted.

@p1r473 p1r473 changed the title Update cli.py Fade with duration Mar 23, 2025
@p1r473 p1r473 changed the title Fade with duration Power on with duration; power on if brightness > 0 Mar 23, 2025
On systems exposing multiple /sys/class/backlight entries (e.g. KMS/DSI on newer Raspberry Pi models),
the previous name-based selection could pick a non-functional device and fail with EINVAL/EIO.

Select the backlight device by probing for one that accepts a brightness write, falling back to the
previous *-0045 / rpi_backlight logic if none succeed.

This makes device selection robust across different kernels, display revisions, and overlays
without hardcoding bus numbers or model checks.

Also improve CLI input validation by rejecting "on"/"off" as SYSFS_PATH and guiding users to the
proper -p flag usage.
@p1r473

p1r473 commented Jan 21, 2026

Copy link
Copy Markdown
Contributor Author

Update to PR:

Hi @linusg ,

On newer Raspberry Pi systems using KMS (e.g. Pi 5 with DSI), the kernel can expose multiple /sys/class/backlight entries:

  • One functional device
  • One or more non-functional "ghost" devices returning EIO / EINVAL
  • The legacy rpi_backlight stub

The current selection logic picks the first "*-0045" entry by name, which can resolve to a non-functional device depending on enumeration order, causing rpi-backlight to fail at startup.

This PR changes Raspberry Pi backlight selection to be capability-based instead of name-based:

  • Enumerate all /sys/class/backlight devices
  • Probe each by writing its current brightness value back
  • Select the first device that accepts the write
  • Fall back to the previous *-0045 / rpi_backlight logic if probing fails

This avoids hardcoding bus numbers or model checks and works across:

  • DSI Gen1 and Gen2
  • KMS / FKMS
  • Pi 4, Pi 5, and future revisions

Additionally, the CLI now rejects "on" and "off" as positional SYSFS_PATH values and guides users to use the proper -p flag, preventing confusing tracebacks when running:

rpi-backlight on
rpi-backlight off

@p1r473 p1r473 changed the title Power on with duration; power on if brightness > 0 Power on with duration; power on if brightness > 0; fix backlight device selection on KMS systems; handle multiple backlight sysfs nodes correctly; auto-select working backlight device on KMS / Pi 5 Jan 21, 2026
@p1r473 p1r473 changed the title Power on with duration; power on if brightness > 0; fix backlight device selection on KMS systems; handle multiple backlight sysfs nodes correctly; auto-select working backlight device on KMS / Pi 5 Power on with duration; power on if brightness > 0; fix backlight device selection on KMS systems; handle multiple backlight sysfs nodes correctly Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Toggle CLI turns brightness up to 100%

1 participant