Created
March 29, 2025 18:56
-
-
Save carlossless/867fc12315b6f09e82c09430d6eeb63f to your computer and use it in GitHub Desktop.
nix (nixos, nixos-hardware) device-tree overlay for LP5012 & oneshot systemd service to set LED color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ config, lib, ... }: | |
let | |
cfg = config.hardware.raspberry-pi."4".lp5012-leds; | |
in { | |
options.hardware = { | |
raspberry-pi."4".lp5012-leds = { | |
enable = lib.mkEnableOption '' | |
support for lp50xx activity leds. | |
''; | |
}; | |
}; | |
config = lib.mkIf cfg.enable { | |
hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = lib.mkDefault true; | |
systemd.services.lp5012-intensity = { | |
enable = true; | |
wantedBy = [ "multi-user.target" ]; | |
serviceConfig = { | |
Type = "oneshot"; | |
User = "root"; | |
Group = "root"; | |
RemainAfterExit = true; | |
}; | |
script = '' | |
if [ -d /sys/class/leds/rgb:power ]; then | |
echo "0 255 0" > /sys/class/leds/rgb:power/multi_intensity | |
# echo "255" > /sys/class/leds/rgb:power/brightness | |
fi | |
if [ -d /sys/class/leds/rgb:sd ]; then | |
echo "255 255 255" > /sys/class/leds/rgb:sd/multi_intensity | |
# echo "255" > /sys/class/leds/rgb:sd/brightness | |
fi | |
if [ -d /sys/class/leds/rgb:wlan ]; then | |
echo "255 255 0" > /sys/class/leds/rgb:wlan/multi_intensity | |
# echo "255" > /sys/class/leds/rgb:wlan/brightness | |
fi | |
if [ -d /sys/class/leds/rgb:bluetooth ]; then | |
echo "0 0 255" > /sys/class/leds/rgb:bluetooth/multi_intensity | |
# echo "255" > /sys/class/leds/rgb:bluetooth/brightness | |
fi | |
''; | |
}; | |
hardware.deviceTree = { | |
overlays = [ | |
{ | |
name = "lp5012-leds-overlay"; | |
dtsText = '' | |
/dts-v1/; | |
/plugin/; | |
#include <dt-bindings/leds/common.h> | |
/ { | |
compatible = "brcm,bcm2711"; | |
fragment@0 { | |
target = <&i2c1>; | |
i2c_bus: __overlay__ { | |
led-controller@14 { | |
compatible = "ti,lp5012"; | |
reg = <0x14>; | |
#address-cells = <1>; | |
#size-cells = <0>; | |
multi-led@0 { | |
#address-cells = <1>; | |
#size-cells = <0>; | |
reg = <0>; | |
color = <LED_COLOR_ID_RGB>; | |
function = LED_FUNCTION_POWER; | |
default-state = "off"; | |
linux,default-trigger = "default-on"; | |
max-brightness = <2>; | |
led@0 { | |
reg = <0>; | |
color = <LED_COLOR_ID_RED>; | |
}; | |
led@1 { | |
reg = <1>; | |
color = <LED_COLOR_ID_GREEN>; | |
}; | |
led@2 { | |
reg = <2>; | |
color = <LED_COLOR_ID_BLUE>; | |
}; | |
}; | |
multi-led@1 { | |
#address-cells = <1>; | |
#size-cells = <0>; | |
reg = <1>; | |
color = <LED_COLOR_ID_RGB>; | |
function = LED_FUNCTION_SD; | |
default-state = "off"; | |
linux,default-trigger = "mmc0"; | |
max-brightness = <2>; | |
led@0 { | |
reg = <0>; | |
color = <LED_COLOR_ID_RED>; | |
}; | |
led@1 { | |
reg = <1>; | |
color = <LED_COLOR_ID_GREEN>; | |
}; | |
led@2 { | |
reg = <2>; | |
color = <LED_COLOR_ID_BLUE>; | |
}; | |
}; | |
multi-led@2 { | |
#address-cells = <1>; | |
#size-cells = <0>; | |
reg = <2>; | |
color = <LED_COLOR_ID_RGB>; | |
function = LED_FUNCTION_WLAN; | |
default-state = "off"; | |
linux,default-trigger = "rfkill0"; | |
max-brightness = <2>; | |
led@0 { | |
reg = <0>; | |
color = <LED_COLOR_ID_RED>; | |
}; | |
led@1 { | |
reg = <1>; | |
color = <LED_COLOR_ID_GREEN>; | |
}; | |
led@2 { | |
reg = <2>; | |
color = <LED_COLOR_ID_BLUE>; | |
}; | |
}; | |
multi-led@3 { | |
#address-cells = <1>; | |
#size-cells = <0>; | |
reg = <3>; | |
color = <LED_COLOR_ID_RGB>; | |
function = LED_FUNCTION_BLUETOOTH; | |
default-state = "off"; | |
linux,default-trigger = "rfkill1"; | |
max-brightness = <2>; | |
led@0 { | |
reg = <0>; | |
color = <LED_COLOR_ID_RED>; | |
}; | |
led@1 { | |
reg = <1>; | |
color = <LED_COLOR_ID_GREEN>; | |
}; | |
led@2 { | |
reg = <2>; | |
color = <LED_COLOR_ID_BLUE>; | |
}; | |
}; | |
}; | |
}; | |
}; | |
}; | |
''; | |
} | |
]; | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment