leds: Add virtual LED group driver with priority arbitration
Subsystems (3)
v4 cover leds: Add virtual LED group driver with priority arbitration
From: Jonathan Brophy <[email protected]>
This patch series introduces a new LED driver that implements virtual LED groups with priority-based arbitration for shared physical LEDs. The driver provides a multicolor LED interface while solving the problem of multiple subsystems needing to control the same physical LEDs.
Key features:
- Winner-takes-all priority-based arbitration
- Full multicolor LED ABI compliance
- Two operating modes (multicolor and standard/fixed-color)
- Deterministic channel ordering by LED_COLOR_ID
- Comprehensive debugfs telemetry (when CONFIG_DEBUG_FS enabled)
- Optimized memory footprint (~200 bytes per LED in production builds)
Use cases:
- System status indicators with boot/error/update priority levels
- RGB lighting with coordinated control
- Multi-element LED arrays unified into single logical controls
The series includes:
1. New LED function identifier for virtual LEDs
2. Device tree bindings for virtual LED class
3. Device tree bindings for virtual LED group controller
4. ABI documentation for sysfs interface
5. Comprehensive driver documentation
6. fwnode_led_get() helper for firmware-agnostic LED resolution
7. Complete driver implementation
Changes since last v3 commit
- convert driver to pure fwnode
- Multicolor LED ABI compliance - standard multi_intensity/multi_index attributes
- Winner-takes-all arbitration - deterministic control with sequence-based tie-breaking
- Proper LED reference management - fwnode_led_get() + led_put() prevents leaks
- 30% memory reduction - conditional debug compilation
- Global ownership tracking - prevents conflicts between multiple controllers
- Hierarchical locking - documented 3-tier lock order prevents deadlocks
- Lock-free hardware I/O - concurrent vLED updates during physical LED access
- Dual operating modes - multicolor (dynamic) and standard (fixed-color) modes
- Pre-allocated arbitration buffers - zero allocations in hot path
- Comprehensive power management - suspend/resume with runtime PM support
Additional highlights:
Update batching for software PWM workloads
Gamma correction for perceptual brightness
Rate limiting for runaway updates
Extensive debugfs telemetry with stress testing
Deferred probe handling for late-probing LEDs
Removal race prevention with atomic flags
Testing:
- Tested on ARM64 platform with GPIO and PWM LEDs
- Stress tested with 10,000 iterations
- Validated suspend/resume cycles
- Memory leak detection passes
Jonathan Brophy (7):
dt-bindings: leds: Add LED_FUNCTION_VIRTUAL_STATUS identifier
dt-bindings: leds: Add virtual LED class bindings
dt-bindings: leds: Add virtual LED group controller bindings
ABI: Add sysfs documentation for leds-group-virtualcolor
leds: Add driver documentation for leds-group-virtualcolor
leds: Add fwnode_led_get() for firmware-agnostic LED resolution
leds: Add virtual LED group driver with priority arbitration
.../sysfs-class-led-driver-virtualcolor | 168 + .../leds/leds-class-virtualcolor.yaml | 197 + .../leds/leds-group-virtualcolor.yaml | 170 + .../leds/leds-group-virtualcolor.rst | 641 ++++ drivers/leds/led-class.c | 136 +- drivers/leds/leds.h | 758 +++- drivers/leds/rgb/Kconfig | 17 + drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-group-virtualcolor.c | 3360 +++++++++++++++++ include/dt-bindings/leds/common.h | 3 + 10 files changed, 5399 insertions(+), 52 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-class-led-driver-virtualcolor create mode 100644 Documentation/devicetree/bindings/leds/leds-class-virtualcolor.yaml create mode 100644 Documentation/devicetree/bindings/leds/leds-group-virtualcolor.yaml create mode 100644 Documentation/leds/leds-group-virtualcolor.rst create mode 100644 drivers/leds/rgb/leds-group-virtualcolor.c
--
2.43.0
PATCH v4 1/7 dt-bindings: leds: add function virtual_status to led common properties
From: Jonathan Brophy <[email protected]>
LED-FUNCTION-VIRTUAL-STATUS ID to the FUNCTION ID list for device tree bindings to suit virtual led drivers.
--- include/dt-bindings/leds/common.h | 3 +++ 1 file changed, 3 insertions(+)
include/dt-bindings/leds/common.h +3 -0
@@ -63,6 +63,9 @@"lp5523:{r,g,b}" (Nokia N900) */ #define LED_FUNCTION_STATUS "status" +/* Used for virtual LED groups, multifunction RGB indicators or status LEDs */ +#define LED_FUNCTION_VIRTUAL_STATUS "virtual-status" +#define LED_FUNCTION_MICMUTE "micmute"#define LED_FUNCTION_MUTE "mute"
--
2.43.0
PATCH v4 2/7 dt-bindings: leds: Add virtual LED class bindings
In reply to Jonathan BrophyFrom: Jonathan Brophy <[email protected]>
Add device tree bindings for the virtual LED class of led devices. for representation of virtual LEDs that combine multiple physical monochromatic LEDs into a group.
Usefull for representing complex system states without complex userspace scripting
Key binding properties:
- priority: Arbitration priority (0-2147483647, higher wins)
- led-mode: Operating mode (multicolor or standard/fixed-color)
- leds: Phandle array to physical LED devices
- mc-channel-multipliers: Per-channel brightness multipliers
Channel ordering is deterministic, sorted by ascending LED_COLOR_ID value. The multi_index sysfs attribute allows runtime verification.
--- .../leds/leds-class-virtualcolor.yaml | 197 ++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/leds-class-virtualcolor.yaml
Documentation/devicetree/bindings/leds/leds-class-virtualcolor.yaml new file +197 -0
@@ -0,0 +1,197 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/leds-class-virtualcolor.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Virtual LED class device with priority-based arbitration + +maintainers: + - Jonathan Brophy <[email protected]> + +description: | + Represents a single virtual LED that combines one or more physical monochromatic + LEDs. Multiple virtual LEDs can share the same physical LEDs, with priority-based + arbitration determining which virtual LED's state is displayed. + + The driver implements winner-takes-all arbitration: + - Only virtual LEDs with brightness > 0 participate in arbitration + - Highest priority wins (ties broken by sequence number - most recent wins) + - Winner controls ALL physical LEDs + - Physical LEDs not used by winner are turned off + + Two operating modes are supported: + + 1. Multicolor mode (default): + - Full multicolor LED ABI support + - Exposes multi_intensity, multi_index, multi_multipliers in sysfs + - Per-channel intensity control (0-255) + - Channel multipliers for color temperature adjustment + - Master brightness scales all channels proportionally + + 2. Standard mode: + - Fixed color ratios via mc-channel-multipliers + - Only brightness control available (no intensity changes) + - Useful for fixed-color LEDs (e.g., warm white, amber) + + Sysfs interface (multicolor mode): + - /sys/class/leds/<led>/brightness - Master brightness (0-max_brightness) + - /sys/class/leds/<led>/mc/multi_intensity - Per-channel intensities (space-separated) + - /sys/class/leds/<led>/mc/multi_index - Color IDs per channel (read-only) + - /sys/class/leds/<led>/mc/multi_multipliers - Channel multipliers (read-only) + +properties: + $nodename: + pattern: "^virtual-led(@[0-9a-f])?$" + + reg: + maxItems: 1 + description: | + Unique index of this virtual LED within the controller. Used for node addressing. + + function: + description: | + LED function identifier. recommended to use: LED_FUNCTION_VIRTUAL_STATUS, + See include/dt-bindings/leds/common.h + + priority: + $ref: /schemas/types.yaml#/definitions/uint32 + default: 0 + minimum: 0 + maximum: 2147483647 + description: | + Arbitration priority for this virtual LED (signed 32-bit integer). + When multiple virtual LEDs sharing physical LEDs are active (brightness > 0), + only the highest-priority LED's state is displayed. + + Tie-breaking: If priorities are equal, the most recently updated virtual LED wins. + + Suggested priority ranges: + 0-99: Normal operation indicators + 100-499: System state indicators (boot, shutdown) + 500-999: Warning/error indicators + 1000+: Critical alerts and emergency overrides + + led-mode: + $ref: /schemas/types.yaml#/definitions/string + enum: [multicolor, standard] + default: multicolor + description: | + Operating mode for this virtual LED: + + - "multicolor": (default) Full multicolor ABI with dynamic intensity control + * Exposes multi_intensity sysfs attribute for per-channel control + * Channels can be adjusted independently at runtime + * Suitable for RGB LEDs, color-changing indicators + + - "standard": Fixed color ratios, brightness-only control + * Channel intensities fixed by mc-channel-multipliers + * Only brightness sysfs attribute available (no multi_intensity) + * Suitable for warm white, amber, or other fixed-color LEDs + + leds: + $ref: /schemas/types.yaml#/definitions/phandle-array + minItems: 1 + maxItems: 255 + description: | + Array of phandles to physical LED devices that compose this virtual LED. + LEDs are automatically grouped by their 'color' property into channels. + + Channel ordering: Channels are ordered by ascending LED_COLOR_ID value. + Only colors present in the LED list are included (no sparse indices). + + Color ID values (from include/dt-bindings/leds/common.h): + LED_COLOR_ID_WHITE = 0 + LED_COLOR_ID_RED = 1 + LED_COLOR_ID_GREEN = 2 + LED_COLOR_ID_BLUE = 3 + LED_COLOR_ID_AMBER = 4 + LED_COLOR_ID_VIOLET = 5 + LED_COLOR_ID_YELLOW = 6 + LED_COLOR_ID_IR = 7 + ... (see header for complete list) + + The /sys/class/leds/<led>/mc/multi_index file reports the color ID for + each channel, allowing userspace to verify the ordering. + + Supported LED types: + - GPIO LEDs (gpio-leds compatible) + - PWM LEDs (pwm-leds compatible) + - I2C/SPI LED controllers (any with brightness_set or brightness_set_blocking) + + Example 1 - RGB LED: + leds = <&led_red>, <&led_green>, <&led_blue>; + Results in 3 channels: [0]=red (ID 1), [1]=green (ID 2), [2]=blue (ID 3) + + Example 2 - RGBW LED: + leds = <&led_red>, <&led_green>, <&led_blue>, <&led_white>; + Results in 4 channels: [0]=white (ID 0), [1]=red (ID 1), [2]=green (ID 2), [3]=blue (ID 3) + Note: White comes first because LED_COLOR_ID_WHITE = 0 + + Example 3 - Non-contiguous IDs: + leds = <&led_amber>, <&led_red>; + Results in 2 channels: [0]=red (ID 1), [1]=amber (ID 4) + + Userspace verification: + $ cat /sys/class/leds/status:multi/mc/multi_index + 1 2 3 + # Confirms: [0]=red, [1]=green, [2]=blue + + mc-channel-multipliers: + $ref: /schemas/types.yaml#/definitions/uint32-array + minItems: 1 + maxItems: 255 + description: | + Per-channel brightness multipliers (0-255) for color temperature adjustment + and normalization. Array must have one entry per channel. + + Channel ordering: Array indices must match the channel order, which is + determined by ascending LED_COLOR_ID value. Only colors present in the + 'leds' property are included. + + For predictable DT configuration, list multipliers in ascending color ID order. + The /sys/class/leds/<led>/mc/multi_index file can verify the ordering at runtime. + + In multicolor mode: + - Scales the intensity value before applying brightness + - Formula: final = (intensity * multiplier / 255) * brightness / max_brightness + - Useful for: Color temperature control, vendor normalization, power limiting + - Default: 255 (no scaling) for all channels if omitted + + In standard mode: + - Defines the FIXED color ratios (intensity changes not allowed) + - Formula: final = multiplier * brightness / max_brightness + - Useful for: Fixed-color LEDs (warm white, amber) + - REQUIRED in standard mode + + Example 1 - RGB with equal scaling: + leds = <&red>, <&green>, <&blue>; + mc-channel-multipliers = <255 255 255>; + # Channels: [0]=red, [1]=green, [2]=blue - all at full scale + + Example 2 - RGBW warm white (standard mode): + leds = <&red>, <&green>, <&blue>, <&white>; + mc-channel-multipliers = <180 255 200 100>; + # Channels: [0]=white:180, [1]=red:255, [2]=green:200, [3]=blue:100 + # Note: White (ID=0) comes first, then red (ID=1), green (ID=2), blue (ID=3) + + Example 3 - Color temperature adjustment: + leds = <&red>, <&green>, <&blue>; + mc-channel-multipliers = <255 180 100>; + # Creates warm white by reducing green/blue relative to red + +required: + - reg + - leds + +allOf: + - $ref: common.yaml# + - if: + properties: + led-mode: + const: standard + then: + required: + - mc-channel-multipliers + +unevaluatedProperties: false
--
2.43.0
PATCH v4 3/7 dt-bindings: leds: Add virtual LED group controller bindings
In reply to Jonathan BrophyFrom: Jonathan Brophy <[email protected]>
Add device tree bindings for the virtual LED group controller that provides priority-based arbitration for shared physical LEDs across multiple virtual LED instances.
Bindings for the virtual driver are not describing hardware LEDs they describe virtual devices made from groups of hardware LEDs created from an array of LED phandles.
Normally the device tree is used to describe hardware not virtual hardware but it is particularly useful in situations where you require an LED to be a specific color by mixing primary colors, such as multi element multi color LEDs to be operated from a device tree binding or a single trigger.
It also becomes useful with multiple LEDs operating the same indicator such as ring of light indicators, led rope where the LEDs are driven From different GPIO outputs unifying the control that can give basic indication during system startup, shutdown upgrade etc...
The controller implements winner-takes-all arbitration where only the highest-priority active virtual LED controls the hardware at any given time. This enables multiple subsystems (boot, error, status indicators) to request LED control without explicit coordination.
Binding supports:
- Multiple virtual LED children with independent priorities
- GPIO, PWM, I2C, and SPI physical LED devices
- Multicolor and standard (fixed-color) operating modes
- Global ownership tracking to prevent conflicts
Example configurations include:
- High-priority emergency/error RGB indicator
- Medium-priority system state RGBW indicator
- Low-priority warm white fixed-color indicator
--- .../leds/leds-group-virtualcolor.yaml | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/leds-group-virtualcolor.yaml
Documentation/devicetree/bindings/leds/leds-group-virtualcolor.yaml new file +170 -0
@@ -0,0 +1,170 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/leds-group-virtualcolor.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Virtual LED Group Controller with Multicolor Support + +maintainers: + - Jonathan Brophy <[email protected]> + +description: | + The virtual LED group controller provides priority-based arbitration for + shared physical LEDs across multiple virtual LED instances. Each virtual LED + combines physical monochromatic LEDs into logical units with: + + - Priority-based arbitration: Higher priority virtual LEDs take precedence + when multiple virtual LEDs compete for the same physical LEDs + - Sequence-based tie-breaking: Among equal priorities, most recent update wins + - Winner-takes-all: Only ONE virtual LED controls ALL physical LEDs at any time + - Color channel grouping: Organize LEDs by color for multicolor control + - Full multicolor ABI support: multi_intensity, multi_index, multi_multipliers + - Two operating modes: + * Multicolor mode: Dynamic per-channel intensity control (default) + * Standard mode: Fixed color ratios via multipliers (brightness only) + - Brightness scaling: Master brightness control with per-channel intensity + - Global ownership: Physical LEDs claimed exclusively per controller instance + - Update batching: Optional coalescing of rapid brightness changes + + Key features: + - Supports GPIO, PWM, I2C, and SPI LED devices + - Automatic physical LED discovery and claiming + - Lock-free arbitration with atomic sequence numbers + - Suspend/resume with state preservation + - Comprehensive debugfs telemetry (when CONFIG_DEBUG_FS enabled) + + Typical use cases: + - System status indicators with boot/update/error priority levels + - RGB lighting with priority-based overrides + - Multi-element LED arrays unified into single logical controls + - LED rings or strips with coordinated color control + +properties: + compatible: + const: leds-group-virtualcolor + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + +patternProperties: + "^virtual-led@[0-9a-f]+$": + type: object + $ref: leds-class-virtualcolor.yaml# + +required: + - compatible + - '#address-cells' + - '#size-cells' + +additionalProperties: false + +examples: + - | + #include <dt-bindings/leds/common.h> + #include <dt-bindings/gpio/gpio.h> + + /* Physical LED definitions */ + led-controller { + compatible = "gpio-leds"; + + led_red: led-red { + color = <LED_COLOR_ID_RED>; + function = LED_FUNCTION_STATUS; + gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + + led_green: led-green { + color = <LED_COLOR_ID_GREEN>; + function = LED_FUNCTION_STATUS; + gpios = <&gpio0 11 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + + led_blue: led-blue { + color = <LED_COLOR_ID_BLUE>; + function = LED_FUNCTION_STATUS; + gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + + led_white: led-white { + color = <LED_COLOR_ID_WHITE>; + function = LED_FUNCTION_STATUS; + gpios = <&gpio0 13 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + }; + + pwm-led-controller { + compatible = "pwm-leds"; + + pwm_red: led-pwm-red { + color = <LED_COLOR_ID_RED>; + function = LED_FUNCTION_STATUS; + pwms = <&pwm 0 7812500>; + max-brightness = <255>; + }; + + pwm_green: led-pwm-green { + color = <LED_COLOR_ID_GREEN>; + function = LED_FUNCTION_STATUS; + pwms = <&pwm 1 7812500>; + max-brightness = <255>; + }; + + pwm_blue: led-pwm-blue { + color = <LED_COLOR_ID_BLUE>; + function = LED_FUNCTION_STATUS; + pwms = <&pwm 2 7812500>; + max-brightness = <255>; + }; + }; + + /* virtual LED definitions */ + virtual-led-controller { + compatible = "leds-group-virtualcolor"; + #address-cells = <1>; + #size-cells = <0>; + + /* High-priority RGB virtual LED (emergency/error indicator) */ + virtual-led@0 { + reg = <0>; + color = <LED_COLOR_ID_MULTI>; + function = LED_FUNCTION_STATUS; + priority = <1000>; + led-mode = "multicolor"; + leds = <&led_red>, <&led_green>, <&led_blue>; + /* Channels ordered by color ID: [0]=red, [1]=green, [2]=blue */ + }; + + /* Medium-priority RGBW indicator (system state) */ + virtual-led@1 { + reg = <1>; + color = <LED_COLOR_ID_MULTI>; + function = LED_FUNCTION_STATUS; + priority = <500>; + led-mode = "multicolor"; + leds = <&pwm_red>, <&pwm_green>, <&pwm_blue>, <&led_white>; + /* Channels: [0]=white (ID=0), [1]=red, [2]=green, [3]=blue */ + }; + + /* Low-priority warm white (fixed color ratios, standard mode) */ + virtual-led@2 { + reg = <2>; + color = <LED_COLOR_ID_MULTI>; + function = LED_FUNCTION_STATUS; + priority = <10>; + led-mode = "standard"; + leds = <&led_red>, <&led_green>, <&led_blue>; + /* Channels: [0]=red, [1]=green, [2]=blue */ + mc-channel-multipliers = <255 180 100>; + /* Creates warm white: full red, 70% green, 40% blue */ + }; + }; + +...
--
2.43.0
Rob Herring (Arm)
maintainerRe: [PATCH v4 3/7] dt-bindings: leds: Add virtual LED group controller bindings
Quoted 35 lines · Jonathan Brophy
From: Jonathan Brophy <[email protected]>
Add device tree bindings for the virtual LED group controller that provides priority-based arbitration for shared physical LEDs across multiple virtual LED instances.
Bindings for the virtual driver are not describing hardware LEDs they describe virtual devices made from groups of hardware LEDs created from an array of LED phandles.
Normally the device tree is used to describe hardware not virtual hardware but it is particularly useful in situations where you require an LED to be a specific color by mixing primary colors, such as multi element multi color LEDs to be operated from a device tree binding or a single trigger.
It also becomes useful with multiple LEDs operating the same indicator such as ring of light indicators, led rope where the LEDs are driven From different GPIO outputs unifying the control that can give basic indication during system startup, shutdown upgrade etc...
The controller implements winner-takes-all arbitration where only the highest-priority active virtual LED controls the hardware at any given time. This enables multiple subsystems (boot, error, status indicators) to request LED control without explicit coordination.Binding supports:
- Multiple virtual LED children with independent priorities
- GPIO, PWM, I2C, and SPI physical LED devices
- Multicolor and standard (fixed-color) operating modes
- Global ownership tracking to prevent conflictsExample configurations include:
- High-priority emergency/error RGB indicator
- Medium-priority system state RGBW indicator
- Low-priority warm white fixed-color indicator
Quoted 8 lines
Co-developed-by Radoslav Tsvetkov <[email protected]>Signed-off-by Radoslav Tsvetkov <[email protected]>Signed-off-by Jonathan Brophy <[email protected]>--- .../leds/leds-group-virtualcolor.yaml | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/leds-group-virtualcolor.yaml
My bot found errors running 'make dt_binding_check' on your patch:
yamllint warnings/errors:
dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/leds/leds-group-virtualcolor.example.dtb: pwm-led-controller (pwm-leds): 'led-pwm-blue', 'led-pwm-green', 'led-pwm-red' do not match any of the regexes: '^led(-[0-9a-f]+)?$', '^pinctrl-[0-9]+$'
from schema $id: http://devicetree.org/schemas/leds/leds-pwm.yaml
doc reference errors (make refcheckdocs):
See https://patchwork.kernel.org/project/devicetree/patch/[email protected]
The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.
If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:
pip3 install dtschema --upgrade
Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.
Re: [PATCH v4 3/7] dt-bindings: leds: Add virtual LED group controller bindings
Quoted 6 lines
My bot found errors running 'make
dt_binding_check' on your patch:yamllint warnings/errors:
dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/leds/leds-group-virtualcolor.example.dtb: pwm-led-controller (pwm->leds): 'led-pwm-blue', 'led-pwm-green', 'led-pwm-red' do not match any of the regexes: '^led(-[0-9a-f]+)?$', '^pinctrl-[0-9]+$'
Thanks I have fixed this and re submitted a v5 patch series.
I have been testing on OpenWrt where the yaml and dts files are not validated and this slipped in.
Many Thanks Jonathan Brophy
Re: [PATCH v4 3/7] dt-bindings: leds: Add virtual LED group controller bindings
Quoted 4 lines · Jonathan Brophy
Quoted 3 lines
My bot found errors running 'make
dt_binding_check' on your patch:yamllint warnings/errors:
Quoted 8 lines
Quoted 2 lines
dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/leds/leds-group-virtualcolor.example.dtb: pwm-led-controller (pwm->leds): 'led-pwm-blue', 'led-pwm-green', 'led-pwm-red' do not match any of the regexes: '^led(-[0-9a-f]+)?$', '^pinctrl-[0-9]+$'Thanks I have fixed this and re submitted a v5 patch series.
I have been testing on OpenWrt where the yaml and dts files are not validated and this slipped in.
You must base and test on mainline, not OpenWRT. I understand above that v5 wasn't tested either, so I won't be reviewing it.
Best regards,
Krzysztof
PATCH v4 4/7 ABI: Add sysfs documentation for leds-group-virtualcolor
In reply to Jonathan BrophyFrom: Jonathan Brophy <[email protected]>
Document the sysfs ABI for the virtual LED group driver, including:
- mc/multi_intensity: Per-channel intensity control (0-255)
- mc/multi_index: Channel-to-color-ID mapping (read-only)
- mc/multi_multipliers: Per-channel scale factors (read-only)
- brightness: Master brightness control with arbitration trigger
- max_brightness: Maximum brightness value (mode-dependent)
Channel ordering is deterministic, sorted by ascending LED_COLOR_ID value. For RGBW LEDs, white (ID=0) appears first, followed by RGB.
The multi_intensity attribute is rate-limited to 100 updates/second per virtual LED by default, with counters visible in debugfs when CONFIG_DEBUG_FS is enabled.
--- .../sysfs-class-led-driver-virtualcolor | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-class-led-driver-virtualcolor
Documentation/ABI/testing/sysfs-class-led-driver-virtualcolor new file +168 -0
@@ -0,0 +1,168 @@ +What: /sys/class/leds/<led>/mc/multi_intensity +Date: December 2024 +KernelVersion: 6.x +Contact: Jonathan Brophy <[email protected]> +Description: + Control the intensity values for each color channel in a + virtual multicolor LED. + + Reading returns space-separated intensity values (0-255) for + each configured color channel. Channel order is deterministic, + sorted by ascending LED_COLOR_ID value. Use multi_index to + determine which color corresponds to each position. + + Writing accepts space-separated intensity values to set the + per-channel intensities. The number of values must match the + number of channels. Values must be ordered to match multi_index. + + Channel ordering examples: + RGB LED (no white): + multi_index shows "1 2 3" + Order is: red (ID 1), green (ID 2), blue (ID 3) + + RGBW LED (with white): + multi_index shows "0 1 2 3" + Order is: white (ID 0), red (ID 1), green (ID 2), blue (ID 3) + Note: White comes FIRST because LED_COLOR_ID_WHITE = 0 + + Example (RGB LED with 3 channels): + $ cat /sys/class/leds/myled/mc/multi_index + 1 2 3 + $ cat /sys/class/leds/myled/mc/multi_intensity + 255 128 0 + $ echo "128 64 200" > /sys/class/leds/myled/mc/multi_intensity + + Note: In standard mode (led-mode = "standard"), intensity + changes are rejected with -EPERM and the color is fixed by the + channel multipliers defined in the device tree. In multicolor + mode (led-mode = "multicolor", default), intensity can be + freely modified. + + This attribute is rate-limited to prevent system overload + (default: 100 updates/second per virtual LED). Excessive + updates will be silently dropped with incremented rate limit + counters (visible in debugfs when CONFIG_DEBUG_FS enabled). + +What: /sys/class/leds/<led>/mc/multi_index +Date: December 2024 +KernelVersion: 6.x +Contact: Jonathan Brophy <[email protected]> +Description: + Read-only attribute showing the LED color IDs for each channel + in the virtual LED. + + Returns space-separated LED_COLOR_ID_* values (integers) + corresponding to each channel. Channels are ordered by + ascending color ID value (0, 1, 2, 3, ...). + + See include/dt-bindings/leds/common.h for color ID definitions. + + Common color ID values: + - 0: LED_COLOR_ID_WHITE + - 1: LED_COLOR_ID_RED + - 2: LED_COLOR_ID_GREEN + - 3: LED_COLOR_ID_BLUE + - 4: LED_COLOR_ID_AMBER + - 5: LED_COLOR_ID_VIOLET + - 6: LED_COLOR_ID_YELLOW + - 7: LED_COLOR_ID_IR + - 8: LED_COLOR_ID_MULTI + - 9: LED_COLOR_ID_RGB + - 10: LED_COLOR_ID_UV + + Example (RGB LED): + $ cat /sys/class/leds/myled/mc/multi_index + 1 2 3 + (Shows: red=1, green=2, blue=3) + + Example (RGBW LED): + $ cat /sys/class/leds/myled/mc/multi_index + 0 1 2 3 + (Shows: white=0, red=1, green=2, blue=3) + + This attribute is essential for correctly indexing the + multi_intensity and mc-channel-multipliers arrays, especially + when white LEDs are present (which come first due to ID=0). + +What: /sys/class/leds/<led>/mc/multi_multipliers +Date: December 2024 +KernelVersion: 6.x +Contact: Jonathan Brophy <[email protected]> +Description: + Read-only attribute showing the scale/multiplier values (0-255) + for each color channel. + + Multipliers are defined in device tree via the + "mc-channel-multipliers" property and must be ordered to match + the channel order (sorted by LED_COLOR_ID). + + In multicolor mode, these scale the intensity values: + final = (intensity * multiplier / 255) * brightness / max_brightness + + In standard mode, these define the fixed color mix: + final = multiplier * brightness / max_brightness + + Returns space-separated values (0-255), one per channel, in the + same order as multi_index. + + Example (RGB LED): + $ cat /sys/class/leds/myled/mc/multi_index + 1 2 3 + $ cat /sys/class/leds/myled/mc/multi_multipliers + 255 200 150 + (Shows: red=255, green=200, blue=150) + + Example (RGBW warm white): + $ cat /sys/class/leds/myled/mc/multi_index + 0 1 2 3 + $ cat /sys/class/leds/myled/mc/multi_multipliers + 180 255 200 100 + (Shows: white=180, red=255, green=200, blue=100) + +What: /sys/class/leds/<led>/brightness +Date: December 2024 +KernelVersion: 6.x +Contact: Jonathan Brophy <[email protected]> +Description: + Control the overall brightness of the virtual LED. + + This is the standard LED class attribute. For virtual grouped + LEDs, this controls the master brightness that scales all + physical LEDs assigned to this virtual LED after per-channel + intensity and multipliers are applied. + + Writing brightness triggers the winner-takes-all arbitration + engine which determines which virtual LED controls the hardware + based on: + 1. Priority (higher wins) + 2. Sequence number (most recent wins on tie) + 3. Only virtual LEDs with brightness > 0 participate + + The winner controls ALL physical LEDs. Physical LEDs not used + by the winner are turned off. + + Range: 0 to max_brightness (typically 0-255) + + Reading returns the current brightness setting. + + Example: + $ echo 128 > /sys/class/leds/myled/brightness + $ cat /sys/class/leds/myled/brightness + 128 + +What: /sys/class/leds/<led>/max_brightness +Date: December 2024 +KernelVersion: 6.x +Contact: Jonathan Brophy <[email protected]> +Description: + Read-only attribute showing the maximum brightness value. + + For multicolor mode virtual LEDs, this is always 255 to provide + full 8-bit resolution for color mixing. + + For standard mode virtual LEDs, this is the minimum max_brightness + among all physical LEDs referenced by the virtual LED. + + Example: + $ cat /sys/class/leds/myled/max_brightness + 255
--
2.43.0
PATCH v4 5/7 leds: Add driver documentation for leds-group-virtualcolor
In reply to Jonathan BrophyFrom: Jonathan Brophy <[email protected]>
Add comprehensive driver documentation covering:
Architecture:
- Winner-takes-all arbitration model
- Priority-based selection with sequence number tie-breaking
- Deterministic channel ordering by LED_COLOR_ID
- Locking hierarchy to prevent deadlocks
Features:
- Two operating modes (multicolor and standard)
- Gamma correction support
- Update batching for reduced bus traffic
- Comprehensive debugfs interface (when CONFIG_DEBUG_FS enabled)
Configuration:
- Device tree binding examples (RGB, RGBW, fixed-color)
- Module parameters for tuning
- Sysfs interface usage examples
- Performance optimization guidelines
Troubleshooting:
- Common issues and solutions
- Debug logging instructions
- Known limitations
The documentation includes practical examples for channel ordering verification, priority arbitration scenarios, and debugfs monitoring.
--- .../leds/leds-group-virtualcolor.rst | 641 ++++++++++++++++++ 1 file changed, 641 insertions(+) create mode 100644 Documentation/leds/leds-group-virtualcolor.rst
Documentation/leds/leds-group-virtualcolor.rst new file +641 -0
@@ -0,0 +1,641 @@ +.. SPDX-License-Identifier: GPL-2.0 + +===================================================== +Virtual Grouped LED Driver with Multicolor ABI +===================================================== + +:Author: Jonathan Brophy <[email protected]> +:Version: 4 + +Overview +======== + +The ``leds-group-virtualcolor`` driver provides virtual LED devices that +arbitrate control over shared physical LEDs based on priority. Multiple +virtual LEDs can reference the same physical LEDs, with winner-takes-all +arbitration determining which virtual LED controls the hardware. + +This enables complex lighting scenarios where different subsystems (e.g., +notifications, indicators, effects) can request LED control without explicit +coordination. The driver handles arbitration automatically using a +priority-based system with sequence number tiebreaking. + +Key Features +============ + +* **Winner-takes-all arbitration**: Only ONE virtual LED controls hardware at any time +* **Priority-based selection**: Higher priority virtual LEDs win control +* **Sequence-based tiebreaking**: Most recent update wins among equal priorities +* **Multicolor ABI support**: Standard Linux multicolor LED interface +* **Deterministic channel ordering**: Channels sorted by LED_COLOR_ID value +* **Two operating modes**: + + - Multicolor mode (dynamic color mixing with intensity control) + - Standard mode (fixed color multipliers, brightness-only control) + +* **Gamma correction**: Optional perceptual brightness correction +* **Update batching**: Debounces rapid changes to reduce bus traffic +* **Comprehensive debugfs**: Runtime statistics and diagnostics (when CONFIG_DEBUG_FS enabled) +* **Power management**: Suspend/resume with state preservation + +Hardware Support +================ + +The driver works with any physical LED devices that expose the standard +``led_classdev`` interface. Physical LEDs are referenced via device tree +phandles and can be: + +* GPIO LEDs (gpio-leds compatible) +* PWM LEDs (pwm-leds compatible) +* I2C-connected LED controllers +* SPI-connected LED controllers +* Any device using the Linux LED subsystem + +Architecture +============ + +Winner-Takes-All Arbitration +----------------------------- + +The driver uses a winner-takes-all arbitration model: + +1. Only virtual LEDs with brightness > 0 participate in arbitration +2. The virtual LED with the highest priority wins +3. If priorities are equal, the most recently updated virtual LED wins (sequence number) +4. The winner controls **ALL** physical LEDs +5. Physical LEDs not used by the winner are turned off + +Each virtual LED has: + +* **Priority** (0 to INT_MAX): Higher values win arbitration +* **Sequence number**: Atomic counter incremented on brightness changes +* **Channel configuration**: Maps physical LEDs to color channels + +Channel Ordering +---------------- + +Physical LEDs are automatically grouped into channels by their color property. +**Channels are ordered by ascending LED_COLOR_ID value** (0, 1, 2, 3, ...). + +This ordering is deterministic and can be verified at runtime via the +``multi_index`` sysfs attribute. + +Example color ID values: + +* LED_COLOR_ID_WHITE = 0 +* LED_COLOR_ID_RED = 1 +* LED_COLOR_ID_GREEN = 2 +* LED_COLOR_ID_BLUE = 3 +* LED_COLOR_ID_AMBER = 4 +* LED_COLOR_ID_VIOLET = 5 + +For a virtual LED with ``leds = <&white>, <&red>, <&green>, <&blue>``: + +* Channel order: [0]=white (ID 0), [1]=red (ID 1), [2]=green (ID 2), [3]=blue (ID 3) +* multi_index reports: "0 1 2 3" +* multi_intensity order: white red green blue + +For a virtual LED with ``leds = <&red>, <&green>, <&blue>`` (no white): + +* Channel order: [0]=red (ID 1), [1]=green (ID 2), [2]=blue (ID 3) +* multi_index reports: "1 2 3" +* multi_intensity order: red green blue + +Brightness Calculation +---------------------- + +Final physical LED brightness is calculated as:: + + channel_value = intensity * multiplier / 255 (in multicolor mode) + multiplier (in standard mode) + + scaled_value = channel_value * vled_brightness / vled_max_brightness + + final_brightness = gamma_table[scaled_value] (if gamma enabled) + scaled_value (if gamma disabled) + +Locking Hierarchy +----------------- + +To prevent deadlocks, locks must be acquired in this order: + +1. ``vcolor_controller.lock`` (per-controller, protects arbitration state) +2. ``global_owner_rwsem`` (global, protects physical LED ownership) +3. ``virtual_led.lock`` (per-vLED, protects channel data) + +Virtual LED locks are never held during arbitration. The driver copies +channel state under lock, then releases before processing. + +Device Tree Bindings +==================== + +Controller Node +--------------- + +``compatible`` + Must be "leds-group-virtualcolor" + +``#address-cells`` + Must be 1 + +``#size-cells`` + Must be 0 + +Child Node Properties (Virtual LEDs) +------------------------------------- + +Each child node represents one virtual LED. + +``reg`` + Unique index for this virtual LED (required) + +``color`` + LED_COLOR_ID value (typically LED_COLOR_ID_MULTI for multicolor LEDs) + +``function`` + LED function identifier (e.g., LED_FUNCTION_STATUS) + +``leds`` + Phandle array referencing physical LED devices (required). + Physical LEDs are grouped by their color property into channels. + Channel order is determined by ascending LED_COLOR_ID value. + +``priority`` + Integer priority value (0 to 2147483647). Higher values win + arbitration. Default: 0 + +``led-mode`` + Operating mode, either "multicolor" or "standard". + Default: "multicolor" + + * **multicolor**: Intensity can be changed via multi_intensity sysfs + * **standard**: Color fixed by multipliers, only brightness control available + +``mc-channel-multipliers`` + Array of u32 values (0-255), one per color channel. Optional. + Must be ordered to match the channel order (sorted by color ID). + Default: 255 for all channels + + * In multicolor mode: Scales intensity values + * In standard mode: Defines fixed color mix (required) + +``linux,default-trigger`` + Default LED trigger (e.g., "heartbeat", "none") + +Example Device Tree +------------------- + +Basic RGB LED with Priority +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: dts + + #include <dt-bindings/leds/common.h> + + virtualcolor { + compatible = "leds-group-virtualcolor"; + #address-cells = <1>; + #size-cells = <0>; + + notification_led: virtual-led@0 { + reg = <0>; + color = <LED_COLOR_ID_MULTI>; + function = LED_FUNCTION_STATUS; + priority = <100>; + led-mode = "multicolor"; + leds = <&red_led>, <&green_led>, <&blue_led>; + /* Channels: [0]=red (ID 1), [1]=green (ID 2), [2]=blue (ID 3) */ + }; + + ambient_led: virtual-led@1 { + reg = <1>; + color = <LED_COLOR_ID_MULTI>; + function = LED_FUNCTION_STATUS; + priority = <10>; + led-mode = "multicolor"; + leds = <&red_led>, <&green_led>, <&blue_led>; + }; + }; + +RGBW LED with White Channel +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: dts + + virtualcolor { + compatible = "leds-group-virtualcolor"; + #address-cells = <1>; + #size-cells = <0>; + + status_led: virtual-led@0 { + reg = <0>; + color = <LED_COLOR_ID_MULTI>; + function = LED_FUNCTION_STATUS; + priority = <100>; + led-mode = "multicolor"; + leds = <&red_led>, <&green_led>, <&blue_led>, <&white_led>; + /* Channels: [0]=white (ID 0), [1]=red, [2]=green, [3]=blue */ + /* Note: White comes FIRST because LED_COLOR_ID_WHITE = 0 */ + }; + }; + +Standard Mode with Fixed Color +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: dts + + virtualcolor { + compatible = "leds-group-virtualcolor"; + #address-cells = <1>; + #size-sizes = <0>; + + warm_white: virtual-led@0 { + reg = <0>; + color = <LED_COLOR_ID_MULTI>; + function = LED_FUNCTION_STATUS; + priority = <50>; + led-mode = "standard"; + leds = <&red_led>, <&green_led>, <&blue_led>; + mc-channel-multipliers = <255 180 100>; + /* Channels: [0]=red:255, [1]=green:180, [2]=blue:100 */ + /* Creates warm white: full red, 70% green, 40% blue */ + }; + }; + +Sysfs Interface +=============== + +Each virtual LED creates a standard LED class device at:: + + /sys/class/leds/<device>:<label>/ + +Standard LED Attributes +----------------------- + +``brightness`` + RW, 0-max_brightness. Master brightness control. + + Writing brightness triggers arbitration which determines which + virtual LED controls each physical LED based on priority and + sequence numbers. + +``max_brightness`` + RO, maximum brightness value (typically 255 for multicolor mode). + +``trigger`` + RW, LED trigger name (standard LED class). + +Multicolor Attributes (mc/ subdirectory) +----------------------------------------- + +``mc/multi_intensity`` + RW (multicolor mode), RO (standard mode) + + Space-separated intensity values (0-255), one per channel. + Order matches the channel order shown in multi_index. + + Example usage:: + + # Verify channel order first + $ cat /sys/class/leds/status:multi/mc/multi_index + 1 2 3 + # Shows [0]=red (ID 1), [1]=green (ID 2), [2]=blue (ID 3) + + # Set RGB to 255, 128, 0 (orange) + $ echo "255 128 0" > /sys/class/leds/status:multi/mc/multi_intensity + +``mc/multi_index`` + RO, space-separated LED_COLOR_ID values. + Shows the color ID for each channel position. + +``mc/multi_multipliers`` + RO, space-separated multiplier values (0-255). + Shows the scale factors applied to each channel. + +Module Parameters +================= + +``enable_debugfs`` (bool, default: y if CONFIG_DEBUG_FS) + Enable debugfs diagnostics interface at /sys/kernel/debug/leds-group-virtualcolor-*/ + +``use_gamma_correction`` (bool, default: n) + Apply perceptual gamma correction (2.2) to brightness values. + +``update_delay_us`` (uint, default: 0) + Artificial delay in microseconds between physical LED updates. + Useful for rate-limiting slow buses (I2C). Max: 1000000 (1 second) + +``max_phys_leds`` (uint, default: 64) + Maximum number of unique physical LEDs per controller. Increase if you see + "Update buffer overflow" errors. Range: 1-1024 + +``enable_update_batching`` (bool, default: n) + Enable update batching/debouncing with 10ms delay. + Reduces overhead for applications doing rapid brightness changes. + +Debugfs Interface +================= + +When ``enable_debugfs=true`` and ``CONFIG_DEBUG_FS=y``, diagnostics are available at:: + + /sys/kernel/debug/leds-group-virtualcolor-<device>/ + +Available Files +--------------- + +``stats`` + RO, controller-level statistics: + + * Arbitration cycle count + * LED update count + * Last update timestamp + * Error counters (allocation failures, buffer overflows, rate limits) + * Arbitration latency (min/max/average in nanoseconds) + * Configuration (gamma, batching, delays, max_phys_leds) + * Global sequence counter + +``vled_stats`` + RO, per-virtual-LED statistics: + + * Brightness set count + * Intensity update count + * Blink requests count + * Sequence number + * Arbitration participation/win/loss counts + * Win rate percentage + * Error counters (buffer failures, parse errors, rate limit drops) + +``phys_led_states`` + RO, current state of each physical LED: + + * LED name + * Chosen brightness + * Controlling priority + * Sequence number + * Winner virtual LED name + +``claimed_leds`` + RO, total count of globally claimed physical LEDs across all controllers. + +``selftest`` + RO, driver self-test output showing configuration and status. + +``stress_test`` + WO, trigger stress test: ``echo <iterations> > stress_test`` + + Runs randomized brightness and intensity updates to test + arbitration under load. Max 10000 iterations. + +``rebuild`` + WO, trigger physical LED list rebuild: ``echo 1 > rebuild`` + + Forces re-discovery of all physical LEDs. Useful for debugging. + +Example Usage +============= + +Basic Control +------------- + +.. code-block:: bash + + # Set notification LED to red at full brightness + echo 255 > /sys/class/leds/platform:notification/brightness + echo "255 0 0" > /sys/class/leds/platform:notification/mc/multi_intensity + + # Set ambient LED to blue at 50% brightness + echo 128 > /sys/class/leds/platform:ambient/brightness + echo "0 0 255" > /sys/class/leds/platform:ambient/mc/multi_intensity + +Verifying Channel Order +------------------------ + +.. code-block:: bash + + # Check which color is which channel + $ cat /sys/class/leds/status:multi/mc/multi_index + 0 1 2 3 + # This means: [0]=white (ID 0), [1]=red (ID 1), [2]=green (ID 2), [3]=blue (ID 3) + + # Set intensities in that order: white=100, red=255, green=0, blue=0 + $ echo "100 255 0 0" > /sys/class/leds/status:multi/mc/multi_intensity + +Priority Arbitration Example +----------------------------- + +.. code-block:: bash + + # Both virtual LEDs reference the same physical red LED + # notification has priority=100, ambient has priority=10 + + # Turn on ambient (low priority) + echo 255 > /sys/class/leds/platform:ambient/brightness + echo "255 0 0" > /sys/class/leds/platform:ambient/mc/multi_intensity + # Red LED is on at 255 + + # Turn on notification (high priority) - it TAKES OVER + echo 200 > /sys/class/leds/platform:notification/brightness + echo "200 0 0" > /sys/class/leds/platform:notification/mc/multi_intensity + # Red LED changes to 200 (notification wins, ambient ignored) + + # Turn off notification + echo 0 > /sys/class/leds/platform:notification/brightness + # Red LED returns to 255 (ambient regains control) + +Monitoring with Debugfs +------------------------ + +.. code-block:: bash + + # Watch arbitration statistics + watch -n 1 cat /sys/kernel/debug/leds-group-virtualcolor-platform/stats + + # Check which vLED is winning + cat /sys/kernel/debug/leds-group-virtualcolor-platform/phys_led_states + + # Monitor per-LED stats + cat /sys/kernel/debug/leds-group-virtualcolor-platform/vled_stats + +Performance Tuning +================== + +Buffer Sizing +------------- + +If you see "Update buffer overflow" errors in dmesg: + +.. code-block:: bash + + # Count unique physical LEDs referenced in your device tree + # Add 25% headroom and set max_phys_leds + + sudo rmmod leds_group_virtualcolor + sudo modprobe leds_group_virtualcolor max_phys_leds=80 + +Rate Limiting +------------- + +For I2C LED controllers on slow buses: + +.. code-block:: bash + + # Add 500µs delay between updates + sudo modprobe leds_group_virtualcolor update_delay_us=500 + + # Or enable update batching (10ms debounce) + sudo modprobe leds_group_virtualcolor enable_update_batching=1 + +Gamma Correction +---------------- + +For LEDs used in human-visible applications: + +.. code-block:: bash + + # Enable perceptual brightness correction (gamma 2.2) + sudo modprobe leds_group_virtualcolor use_gamma_correction=1 + +Troubleshooting +=============== + +Common Issues +------------- + +**"Update buffer overflow" errors** + Physical LED count exceeds max_phys_leds. Increase the module parameter + to match or exceed the number of unique physical LEDs in your device tree. + +**"Physical LED already claimed" warnings** + Another virtualcolor controller instance is already managing this LED. + Each physical LED can only be claimed by one controller instance. + +**Intensity updates rejected in standard mode** + This is expected behavior. Standard mode locks color to the multipliers + defined in device tree. Switch to multicolor mode if dynamic color + control is needed. + +**LEDs flicker rapidly during software PWM** + Enable update batching: ``enable_update_batching=1`` + +**Slow LED response on I2C** + Add update delay: ``update_delay_us=500`` (adjust based on bus speed) + +**Linear brightness doesn't match perception** + Enable gamma correction: ``use_gamma_correction=1`` + +**Wrong color appears for RGBW LED** + Check channel order with multi_index. White (ID=0) comes before RGB (IDs 1-3). + + Example:: + + $ cat /sys/class/leds/myled/mc/multi_index + 0 1 2 3 + # Order is: white, red, green, blue + + $ echo "0 255 0 0" > /sys/class/leds/myled/mc/multi_intensity + # This sets: white=0, red=255, green=0, blue=0 + +Debug Logging +------------- + +Enable driver debug messages: + +.. code-block:: bash + + # Dynamic debug (if enabled in kernel) + echo "module leds_group_virtualcolor +p" > /sys/kernel/debug/dynamic_debug/control + + # Check kernel messages + dmesg | grep virtualcolor + +Known Limitations +================= + +* Physical LEDs cannot be shared between multiple virtualcolor controller + instances (enforced via global ownership tracking with global_owner_xa) + +* Maximum 1024 unique physical LEDs per controller (configurable via + ``max_phys_leds`` parameter) + +* Rate limiting on intensity changes may drop updates under extreme load + (>100 updates/second per vLED by default) + +* Update batching introduces 10ms latency (disable if real-time response + is critical) + +* Debugfs statistics are only available when CONFIG_DEBUG_FS is enabled + (struct sizes reduced by ~30% when disabled) + +Testing +======= + +The driver includes built-in self-tests accessible via debugfs: + +.. code-block:: bash + + # Run self-test + cat /sys/kernel/debug/leds-group-virtualcolor-platform/selftest + + # Run stress test (1000 iterations) + echo 1000 > /sys/kernel/debug/leds-group-virtualcolor-platform/stress_test + + # Watch dmesg for results + dmesg | tail -20 + +Development and Debugging +========================= + +Adding Instrumentation +---------------------- + +The driver uses conditional compilation for debug features. To add custom +telemetry: + +.. code-block:: c + + #ifdef CONFIG_DEBUG_FS + vled->my_custom_counter++; + #endif + +This ensures zero overhead in production builds when CONFIG_DEBUG_FS is +disabled. + +Memory Layout +------------- + +Key structures with memory optimization: + +* ``struct virtual_led``: ~200 bytes (non-debug), ~300 bytes (debug) +* ``struct phys_led_entry``: ~120 bytes (non-debug), ~220 bytes (debug) +* ``struct vcolor_controller``: ~400 bytes (non-debug), ~600 bytes (debug) + +Contributing +============ + +Patches should be sent to the LED subsystem maintainers: + +* Pavel Machek <[email protected]> +* Lee Jones <[email protected]> +* [email protected] + +Ensure patches: + +1. Follow kernel coding style (checkpatch.pl --strict) +2. Include appropriate Signed-off-by tags +3. Update documentation if ABI/behavior changes +4. Test with CONFIG_DEBUG_FS=y and =n +5. Run sparse/smatch static analysis +6. Test on actual hardware with multiple virtual LED configurations + +References +========== + +* LED subsystem documentation: Documentation/leds/ +* Multicolor LED class: Documentation/leds/leds-class-multicolor.rst +* Device tree bindings: Documentation/devicetree/bindings/leds/ +* LED device tree common properties: include/dt-bindings/leds/common.h + +License +======= + +This driver is licensed under GPL v2. See COPYING for details.
--
2.43.0
kernel test robot
build botRe: [PATCH v4 5/7] leds: Add driver documentation for leds-group-virtualcolor
kernel test robot noticed the following build warnings:
Hi Jonathan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on lee-leds/for-leds-next]
[also build test WARNING on robh/for-next linus/master v6.19-rc3 next-20251219] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jonathan-Brophy/dt-bindings-leds-add-function-virtual_status-to-led-common-properties/20251230-083649 base: https://git.kernel.org/pub/scm/linux/kernel/git/lee/leds.git for-leds-next patch link: https://lore.kernel.org/r/20251230003250.1197744-6-professorjonny98%40gmail.com patch subject: [PATCH v4 5/7] leds: Add driver documentation for leds-group-virtualcolor reproduce: (https://download.01.org/0day-ci/archive/20251230/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
ERROR: Cannot find file ./include/linux/i2c-atr.h
WARNING: No kernel-doc for file ./include/linux/i2c-atr.h
ERROR: Cannot find file ./include/linux/mutex.h
ERROR: Cannot find file ./include/linux/mutex.h
WARNING: No kernel-doc for file ./include/linux/mutex.h
>> Documentation/leds/leds-group-virtualcolor.rst:319: WARNING: Inline emphasis start-string without end-string. [docutils]
ERROR: Cannot find file ./include/linux/livepatch.h
WARNING: No kernel-doc for file ./include/linux/livepatch.h
ERROR: Cannot find file ./include/linux/seqlock.h
WARNING: No kernel-doc for file ./include/linux/seqlock.h
ERROR: Cannot find file ./include/linux/damon.h
--
ERROR: Cannot find file ./include/linux/mutex.h
ERROR: Cannot find file ./include/linux/mutex.h
WARNING: No kernel-doc for file ./include/linux/mutex.h
ERROR: Cannot find file ./include/linux/fwctl.h
WARNING: No kernel-doc for file ./include/linux/fwctl.h
>> Documentation/leds/leds-group-virtualcolor.rst: WARNING: document isn't included in any toctree [toc.not_included]
vim +319 Documentation/leds/leds-group-virtualcolor.rst
317
318 ``enable_debugfs`` (bool, default: y if CONFIG_DEBUG_FS)
> 319 Enable debugfs diagnostics interface at /sys/kernel/debug/leds-group-virtualcolor-*/
320
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v4 6/7 leds: Add fwnode_led_get() for firmware-agnostic LED resolution
In reply to Jonathan BrophyFrom: Jonathan Brophy <[email protected]>
Add fwnode_led_get() to resolve LED class devices from firmware node references, providing a firmware-agnostic alternative to of_led_get().
The function supports:
- Device Tree and ACPI systems
- GPIO LEDs (which may lack struct device)
- Platform LED controllers
- Deferred probing via -EPROBE_DEFER
- Reference counting via led_module_get()
Implementation details:
- Uses fwnode_property_get_reference_args() for property traversal
- Falls back to of_led_get() for Device Tree GPIO LEDs
- Returns optional parent device reference for power management
- Handles NULL parent devices gracefully (common for GPIO LEDs)
This enables LED resolution using generic firmware APIs while maintaining compatibility with existing OF-specific LED drivers. Future migration to full fwnode support in LED core will be straightforward.
--- drivers/leds/led-class.c | 136 +++++-- drivers/leds/leds.h | 758 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 842 insertions(+), 52 deletions(-)
drivers/leds/led-class.c +105 -31
@@ -25,8 +25,6 @@static DEFINE_MUTEX (leds_lookup_lock);static LIST_HEAD (leds_lookup_list); -static struct workqueue_struct *leds_wq; -static ssize_t brightness_show (struct device *dev,struct device_attribute *attr,char *buf) {
@@ -38,7 +36,7 @@ static ssize_t brightness_show(struct device *dev, brightness = led_cdev->brightness;mutex_unlock (&led_cdev->led_access); -return sysfs_emit (buf,"%u\n" , brightness); +return sprintf (buf,"%u\n" , brightness); } static ssize_t brightness_store(struct device *dev,
@@ -62,6 +60,7 @@ static ssize_t brightness_store(struct device *dev,if (state ==LED_OFF )led_trigger_remove (led_cdev);led_set_brightness (led_cdev, state); +flush_work (&led_cdev->set_brightness_work); ret = size; unlock:
@@ -80,13 +79,13 @@ static ssize_t max_brightness_show(struct device *dev, max_brightness = led_cdev->max_brightness;mutex_unlock (&led_cdev->led_access); -return sysfs_emit (buf,"%u\n" , max_brightness); +return sprintf (buf,"%u\n" , max_brightness); }static DEVICE_ATTR_RO (max_brightness);#ifdef CONFIG_LEDS_TRIGGERS -static const BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0); -static const struct bin_attribute *const led_trigger_bin_attrs[] = { +static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0); +static struct bin_attribute *led_trigger_bin_attrs[] = { &bin_attr_trigger,NULL , };
@@ -122,7 +121,7 @@ static ssize_t brightness_hw_changed_show(struct device *dev,if (led_cdev->brightness_hw_changed ==-1 )return -ENODATA ; -return sysfs_emit (buf,"%u\n" , led_cdev->brightness_hw_changed); +return sprintf (buf,"%u\n" , led_cdev->brightness_hw_changed); }static DEVICE_ATTR_RO (brightness_hw_changed);
@@ -252,23 +251,15 @@ static const struct class leds_class = {* of_led_get() - request a LED device via the LED framework * @np: device node to get the LED device from * @index: the index of the LED -* @name: the name of the LED used to map it to its function, if present * * Returns the LED device parsed from the phandle specified in the "leds" * property of a device tree node or a negative error-code on failure. */ -static struct led_classdev *of_led_get (struct device_node *np,int index, -const char *name) +struct led_classdev *of_led_get (struct device_node *np,int index) {struct device *led_dev;struct device_node *led_node; -/* -* For named LEDs, first look up the name in the "led-names" property. -* If it cannot be found, then of_parse_phandle() will propagate the error. -*/ -if (name) - index =of_property_match_string (np,"led-names" , name); led_node =of_parse_phandle (np,"leds" , index);if (!led_node)return ERR_PTR (-ENOENT );
@@ -278,6 +269,103 @@ static struct led_classdev *of_led_get(struct device_node *np, int index,return led_module_get (led_dev); } +EXPORT_SYMBOL_GPL (of_led_get); + + +/** +* fwnode_led_get() - Get LED class device from firmware node reference +* @fwnode: Firmware node containing LED phandle array property +* @index: Index within the LED array property +* @out_dev: Optional output for the LED's parent device (may be NULL) +* +* This function resolves LED class devices from firmware node references, +* providing a firmware-agnostic alternative to of_led_get(). It supports +* both Device Tree and ACPI systems. +* +* The function handles: +* - GPIO LEDs (which don't have struct device) +* - Platform LED controllers +* - Deferred probing via -EPROBE_DEFER +* - Reference counting via led_module_get() +* +* If @out_dev is non-NULL and the LED has a parent device, a reference +* to that device is returned via get_device(). The caller is responsible +* for calling put_device() when done. GPIO LEDs may not have a parent +* device, in which case @out_dev will be set to NULL. +* +* The caller must call led_put() on the returned LED class device when done. +* +* Return: LED class device pointer on success, ERR_PTR on error: +* -EPROBE_DEFER if LED provider is not yet available +* -EINVAL for invalid arguments or missing LED +* -ENODEV if LED provider returned NULL +*/ +struct led_classdev *fwnode_led_get (const struct fwnode_handle *fwnode, +int index, +struct device **out_dev) +{ +struct fwnode_reference_args args; +struct led_classdev *cdev; +struct device *led_dev =NULL ; +int ret; + +if (out_dev) + *out_dev =NULL ; + +if (!fwnode) +return ERR_PTR (-EINVAL ); + +/* Get the LED reference from the firmware node */ + ret =fwnode_property_get_reference_args (fwnode,"leds" ,NULL ,0 , + index, &args); +if (ret) +return ERR_PTR (ret); + +/* +* Try Device Tree path first if this is an OF node. +* This handles GPIO LEDs and other DT-specific LED providers. +*/ +if (is_of_node (args.fwnode)) { +struct device_node *np =to_of_node (args.fwnode); + + cdev =of_led_get (np,0 ); +fwnode_handle_put (args.fwnode); + +if (IS_ERR (cdev)) +return cdev; + +/* Get parent device if it exists */ +if (out_dev && cdev->dev) + *out_dev =get_device (cdev->dev); + +return cdev; + } + +/* +* ACPI or generic fwnode path. +* Try to find the LED class device by matching the fwnode. +*/ + led_dev =fwnode_get_next_parent_dev ((struct fwnode_handle *)args.fwnode); +fwnode_handle_put (args.fwnode); + +if (!led_dev) +return ERR_PTR (-EPROBE_DEFER ); + +/* Find the LED class device associated with this device */ + cdev =led_module_get (led_dev); +if (!cdev) { +put_device (led_dev); +return ERR_PTR (-EPROBE_DEFER ); + } + +if (out_dev) + *out_dev = led_dev; +else +put_device (led_dev); + +return cdev; +} +EXPORT_SYMBOL_GPL (fwnode_led_get);/** * led_put() - release a LED device
@@ -332,7 +420,7 @@ struct led_classdev *__must_check devm_of_led_get(struct device *dev,if (!dev)return ERR_PTR (-EINVAL ); - led =of_led_get (dev->of_node, index,NULL ); + led =of_led_get (dev->of_node, index);if (IS_ERR (led))return led;
@@ -350,14 +438,9 @@ EXPORT_SYMBOL_GPL(devm_of_led_get);struct led_classdev *led_get (struct device *dev,char *con_id) {struct led_lookup_data *lookup; -struct led_classdev *led_cdev;const char *provider =NULL ;struct device *led_dev; - led_cdev =of_led_get (dev->of_node,-1 , con_id); -if (!IS_ERR (led_cdev) ||PTR_ERR (led_cdev) != -ENOENT ) -return led_cdev; -mutex_lock (&leds_lookup_lock);list_for_each_entry (lookup, &leds_lookup_list, list) { if (!strcmp(lookup->dev_id, dev_name(dev)) &&
@@ -570,8 +653,6 @@ int led_classdev_register_ext(struct device *parent,led_update_brightness (led_cdev); - led_cdev->wq = leds_wq; -led_init_core (led_cdev);#ifdef CONFIG_LEDS_TRIGGERS
@@ -690,19 +771,12 @@ EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);static int __init leds_init (void ) { - leds_wq =alloc_ordered_workqueue ("leds" ,0 ); -if (!leds_wq) { -pr_err ("Failed to create LEDs ordered workqueue\n" ); -return -ENOMEM ; - } -return class_register (&leds_class); } static void __exit leds_exit(void) {class_unregister (&leds_class); -destroy_workqueue (leds_wq); }subsys_initcall (leds_init);
drivers/leds/leds.h +737 -21
771 lines not on this page · raw message
--
2.43.0
kernel test robot
build botRe: [PATCH v4 6/7] leds: Add fwnode_led_get() for firmware-agnostic LED resolution
kernel test robot noticed the following build errors:
Hi Jonathan,
kernel test robot noticed the following build errors:
[auto build test ERROR on lee-leds/for-leds-next]
[also build test ERROR on robh/for-next next-20251219] [cannot apply to linus/master v6.16-rc1]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jonathan-Brophy/dt-bindings-leds-add-function-virtual_status-to-led-common-properties/20251230-083649 base: https://git.kernel.org/pub/scm/linux/kernel/git/lee/leds.git for-leds-next patch link: https://lore.kernel.org/r/20251230003250.1197744-7-professorjonny98%40gmail.com patch subject: [PATCH v4 6/7] leds: Add fwnode_led_get() for firmware-agnostic LED resolution config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20251230/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251230/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All error/warnings (new ones prefixed by >>):
drivers/leds/led-core.c: In function 'led_timer_function':
>> drivers/leds/led-core.c:73:17: error: implicit declaration of function 'led_set_brightness_nosleep'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
73 | led_set_brightness_nosleep(led_cdev, LED_OFF);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_sync
>> drivers/leds/led-core.c:84:22: error: implicit declaration of function 'led_get_brightness'; did you mean 'led_set_brightness'? [-Wimplicit-function-declaration]
84 | brightness = led_get_brightness(led_cdev);
| ^~~~~~~~~~~~~~~~~~
| led_set_brightness
drivers/leds/led-core.c: In function 'set_brightness_delayed':
>> drivers/leds/led-core.c:152:17: error: implicit declaration of function 'led_stop_software_blink' [-Wimplicit-function-declaration]
152 | led_stop_software_blink(led_cdev);
| ^~~~~~~~~~~~~~~~~~~~~~~
drivers/leds/led-core.c: At top level:
drivers/leds/led-core.c:237:6: warning: no previous prototype for 'led_init_core' [-Wmissing-prototypes]
237 | void led_init_core(struct led_classdev *led_cdev)
| ^~~~~~~~~~~~~
drivers/leds/led-core.c:296:6: warning: no previous prototype for 'led_stop_software_blink' [-Wmissing-prototypes]
296 | void led_stop_software_blink(struct led_classdev *led_cdev)
| ^~~~~~~~~~~~~~~~~~~~~~~
>> drivers/leds/led-core.c:296:6: warning: conflicting types for 'led_stop_software_blink'; have 'void(struct led_classdev *)'
drivers/leds/led-core.c:152:17: note: previous implicit declaration of 'led_stop_software_blink' with type 'void(struct led_classdev *)'
152 | led_stop_software_blink(led_cdev);
| ^~~~~~~~~~~~~~~~~~~~~~~
drivers/leds/led-core.c:332:6: warning: no previous prototype for 'led_set_brightness_nopm' [-Wmissing-prototypes]
332 | void led_set_brightness_nopm(struct led_classdev *led_cdev, unsigned int value)
| ^~~~~~~~~~~~~~~~~~~~~~~
drivers/leds/led-core.c:362:6: warning: no previous prototype for 'led_set_brightness_nosleep' [-Wmissing-prototypes]
362 | void led_set_brightness_nosleep(struct led_classdev *led_cdev, unsigned int value)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/leds/led-core.c:362:6: warning: conflicting types for 'led_set_brightness_nosleep'; have 'void(struct led_classdev *, unsigned int)'
drivers/leds/led-core.c:73:17: note: previous implicit declaration of 'led_set_brightness_nosleep' with type 'void(struct led_classdev *, unsigned int)'
73 | led_set_brightness_nosleep(led_cdev, LED_OFF);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~--
In file included from include/linux/kobject.h:20,
from include/linux/energy_model.h:7, from include/linux/device.h:16, from drivers/leds/led-class.c:10:
>> drivers/leds/led-class.c:87:32: error: 'led_trigger_read' undeclared here (not in a function); did you mean 'led_trigger_set'?
87 | static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
| ^~~~~~~~~~~~~~~~
include/linux/sysfs.h:341:17: note: in definition of macro '__BIN_ATTR'
341 | .read = _read, \
| ^~~~~
drivers/leds/led-class.c:87:8: note: in expansion of macro 'BIN_ATTR'
87 | static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
| ^~~~~~~~
>> drivers/leds/led-class.c:87:50: error: 'led_trigger_write' undeclared here (not in a function); did you mean 'led_trigger_set'?
87 | static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
| ^~~~~~~~~~~~~~~~~
include/linux/sysfs.h:342:18: note: in definition of macro '__BIN_ATTR'
342 | .write = _write, \
| ^~~~~~
drivers/leds/led-class.c:87:8: note: in expansion of macro 'BIN_ATTR'
87 | static BIN_ATTR(trigger, 0644, led_trigger_read, led_trigger_write, 0);
| ^~~~~~~~
>> drivers/leds/led-class.c:93:22: error: initialization of 'const struct bin_attribute * const*' from incompatible pointer type 'struct bin_attribute **' [-Wincompatible-pointer-types]
93 | .bin_attrs = led_trigger_bin_attrs,
| ^~~~~~~~~~~~~~~~~~~~~
drivers/leds/led-class.c:93:22: note: (near initialization for 'led_trigger_group.bin_attrs')
drivers/leds/led-class.c: In function 'led_classdev_suspend':
>> drivers/leds/led-class.c:183:9: error: implicit declaration of function 'led_set_brightness_nopm'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
183 | led_set_brightness_nopm(led_cdev, 0);
| ^~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_sync
drivers/leds/led-class.c: At top level:
drivers/leds/led-class.c:258:22: warning: no previous prototype for 'of_led_get' [-Wmissing-prototypes]
258 | struct led_classdev *of_led_get(struct device_node *np, int index)
| ^~~~~~~~~~
drivers/leds/led-class.c:303:22: warning: no previous prototype for 'fwnode_led_get' [-Wmissing-prototypes]
303 | struct led_classdev *fwnode_led_get(const struct fwnode_handle *fwnode,
| ^~~~~~~~~~~~~~
drivers/leds/led-class.c: In function 'fwnode_led_get':
>> drivers/leds/led-class.c:348:19: error: implicit declaration of function 'fwnode_get_next_parent_dev'; did you mean 'fwnode_get_next_parent'? [-Wimplicit-function-declaration]
348 | led_dev = fwnode_get_next_parent_dev((struct fwnode_handle *)args.fwnode);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| fwnode_get_next_parent
>> drivers/leds/led-class.c:348:17: error: assignment to 'struct device *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
348 | led_dev = fwnode_get_next_parent_dev((struct fwnode_handle *)args.fwnode);
| ^
drivers/leds/led-class.c: In function 'led_classdev_register_ext':
>> drivers/leds/led-class.c:647:21: error: 'leds_list_lock' undeclared (first use in this function); did you mean 'leds_lookup_lock'?
647 | down_write(&leds_list_lock);
| ^~~~~~~~~~~~~~
| leds_lookup_lock
drivers/leds/led-class.c:647:21: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/leds/led-class.c:648:41: error: 'leds_list' undeclared (first use in this function); did you mean 'leds_class'?
648 | list_add_tail(&led_cdev->node, &leds_list);
| ^~~~~~~~~
| leds_class
>> drivers/leds/led-class.c:656:9: error: implicit declaration of function 'led_init_core' [-Wimplicit-function-declaration]
656 | led_init_core(led_cdev);
| ^~~~~~~~~~~~~
drivers/leds/led-class.c: In function 'led_classdev_unregister':
>> drivers/leds/led-class.c:692:9: error: implicit declaration of function 'led_stop_software_blink' [-Wimplicit-function-declaration]
692 | led_stop_software_blink(led_cdev);
| ^~~~~~~~~~~~~~~~~~~~~~~
drivers/leds/led-class.c:704:21: error: 'leds_list_lock' undeclared (first use in this function); did you mean 'leds_lookup_lock'?
704 | down_write(&leds_list_lock);
| ^~~~~~~~~~~~~~
| leds_lookup_lock--
drivers/leds/led-triggers.c:36:9: warning: no previous prototype for 'led_trigger_write' [-Wmissing-prototypes]
36 | ssize_t led_trigger_write(struct file *filp, struct kobject *kobj,
| ^~~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c:133:9: warning: no previous prototype for 'led_trigger_read' [-Wmissing-prototypes]
133 | ssize_t led_trigger_read(struct file *filp, struct kobject *kobj,
| ^~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c: In function 'led_trigger_set':
>> drivers/leds/led-triggers.c:189:17: error: implicit declaration of function 'led_stop_software_blink' [-Wimplicit-function-declaration]
189 | led_stop_software_blink(led_cdev);
| ^~~~~~~~~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c: In function 'led_trigger_register':
>> drivers/leds/led-triggers.c:341:20: error: 'leds_list_lock' undeclared (first use in this function); did you mean 'tasklist_lock'?
341 | down_read(&leds_list_lock);
| ^~~~~~~~~~~~~~
| tasklist_lock
drivers/leds/led-triggers.c:341:20: note: each undeclared identifier is reported only once for each function it appears in
In file included from include/linux/kernel.h:22,
from drivers/leds/led-triggers.c:11:
>> drivers/leds/led-triggers.c:342:40: error: 'leds_list' undeclared (first use in this function); did you mean 'pgd_list'?
342 | list_for_each_entry(led_cdev, &leds_list, node) {
| ^~~~~~~~~
include/linux/container_of.h:20:33: note: in definition of macro 'container_of'
20 | void *__mptr = (void *)(ptr); \
| ^~~
include/linux/list.h:620:9: note: in expansion of macro 'list_entry'
620 | list_entry((ptr)->next, type, member)
| ^~~~~~~~~~
include/linux/list.h:782:20: note: in expansion of macro 'list_first_entry'
782 | for (pos = list_first_entry(head, typeof(*pos), member); \
| ^~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c:342:9: note: in expansion of macro 'list_for_each_entry'
342 | list_for_each_entry(led_cdev, &leds_list, node) {
| ^~~~~~~~~~~~~~~~~~~
In file included from include/linux/container_of.h:5:
include/linux/compiler_types.h:565:27: error: expression in static assertion is not an integer
565 | #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
| ^~~~
include/linux/container_of.h:21:9: note: in expansion of macro 'static_assert'
21 | static_assert(__same_type(*(ptr), ((type *)0)->member) || \
| ^~~~~~~~~~~~~
include/linux/container_of.h:21:23: note: in expansion of macro '__same_type'
21 | static_assert(__same_type(*(ptr), ((type *)0)->member) || \
| ^~~~~~~~~~~
include/linux/list.h:609:9: note: in expansion of macro 'container_of'
609 | container_of(ptr, type, member)
| ^~~~~~~~~~~~
include/linux/list.h:620:9: note: in expansion of macro 'list_entry'
620 | list_entry((ptr)->next, type, member)
| ^~~~~~~~~~
include/linux/list.h:782:20: note: in expansion of macro 'list_first_entry'
782 | for (pos = list_first_entry(head, typeof(*pos), member); \
| ^~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c:342:9: note: in expansion of macro 'list_for_each_entry'
342 | list_for_each_entry(led_cdev, &leds_list, node) {
| ^~~~~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c: In function 'led_trigger_unregister':
drivers/leds/led-triggers.c:367:20: error: 'leds_list_lock' undeclared (first use in this function); did you mean 'tasklist_lock'?
367 | down_read(&leds_list_lock);
| ^~~~~~~~~~~~~~
| tasklist_lock
drivers/leds/led-triggers.c:368:40: error: 'leds_list' undeclared (first use in this function); did you mean 'pgd_list'?
368 | list_for_each_entry(led_cdev, &leds_list, node) {
| ^~~~~~~~~
include/linux/container_of.h:20:33: note: in definition of macro 'container_of'
20 | void *__mptr = (void *)(ptr); \
| ^~~
include/linux/list.h:620:9: note: in expansion of macro 'list_entry'
620 | list_entry((ptr)->next, type, member)
| ^~~~~~~~~~
include/linux/list.h:782:20: note: in expansion of macro 'list_first_entry'
782 | for (pos = list_first_entry(head, typeof(*pos), member); \
| ^~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c:368:9: note: in expansion of macro 'list_for_each_entry'
368 | list_for_each_entry(led_cdev, &leds_list, node) {
| ^~~~~~~~~~~~~~~~~~~
include/linux/compiler_types.h:565:27: error: expression in static assertion is not an integer
565 | #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:78:56: note: in definition of macro '__static_assert'
78 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
| ^~~~
include/linux/container_of.h:21:9: note: in expansion of macro 'static_assert'
21 | static_assert(__same_type(*(ptr), ((type *)0)->member) || \
| ^~~~~~~~~~~~~
include/linux/container_of.h:21:23: note: in expansion of macro '__same_type'
21 | static_assert(__same_type(*(ptr), ((type *)0)->member) || \
| ^~~~~~~~~~~
include/linux/list.h:609:9: note: in expansion of macro 'container_of'
609 | container_of(ptr, type, member)
| ^~~~~~~~~~~~
include/linux/list.h:620:9: note: in expansion of macro 'list_entry'
620 | list_entry((ptr)->next, type, member)
| ^~~~~~~~~~
include/linux/list.h:782:20: note: in expansion of macro 'list_first_entry'
782 | for (pos = list_first_entry(head, typeof(*pos), member); \
| ^~~~~~~~~~~~~~~~
drivers/leds/led-triggers.c:368:9: note: in expansion of macro 'list_for_each_entry'
368 | list_for_each_entry(led_cdev, &leds_list, node) {
| ^~~~~~~~~~~~~~~~~~~
--
drivers/leds/trigger/ledtrig-oneshot.c: In function 'led_invert_store':
>> drivers/leds/trigger/ledtrig-oneshot.c:61:17: error: implicit declaration of function 'led_set_brightness_nosleep'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
61 | led_set_brightness_nosleep(led_cdev, LED_FULL);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_sync
--
drivers/leds/trigger/ledtrig-heartbeat.c: In function 'led_heartbeat_function':
>> drivers/leds/trigger/ledtrig-heartbeat.c:44:17: error: implicit declaration of function 'led_set_brightness_nosleep'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
44 | led_set_brightness_nosleep(led_cdev, LED_OFF);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_sync
--
drivers/leds/trigger/ledtrig-backlight.c: In function 'ledtrig_backlight_notify_blank':
>> drivers/leds/trigger/ledtrig-backlight.c:40:17: error: implicit declaration of function 'led_set_brightness_nosleep'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
40 | led_set_brightness_nosleep(led, LED_OFF);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_sync
--
drivers/leds/trigger/ledtrig-gpio.c: In function 'gpio_trig_irq':
>> drivers/leds/trigger/ledtrig-gpio.c:33:25: error: implicit declaration of function 'led_set_brightness_nosleep'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
33 | led_set_brightness_nosleep(gpio_data->led,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_sync
--
drivers/leds/trigger/ledtrig-default-on.c: In function 'defon_trig_activate':
>> drivers/leds/trigger/ledtrig-default-on.c:18:9: error: implicit declaration of function 'led_set_brightness_nosleep'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
18 | led_set_brightness_nosleep(led_cdev, led_cdev->max_brightness);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_sync
--
drivers/leds/trigger/ledtrig-transient.c: In function 'transient_timer_function':
>> drivers/leds/trigger/ledtrig-transient.c:39:9: error: implicit declaration of function 'led_set_brightness_nosleep'; did you mean 'led_set_brightness_sync'? [-Wimplicit-function-declaration]
39 | led_set_brightness_nosleep(led_cdev, transient_data->restore_state);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
| led_set_brightness_syncvim +73 drivers/leds/led-core.c
d4887af9c2b6ab Heiner Kallweit 2016-02-16 64
49404665b93544 Kees Cook 2017-10-25 65 static void led_timer_function(struct timer_list *t)
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 66 {
41cb08555c4164 Ingo Molnar 2025-05-09 67 struct led_classdev *led_cdev = timer_container_of(led_cdev, t,
41cb08555c4164 Ingo Molnar 2025-05-09 68 blink_timer);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 69 unsigned long brightness;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 70 unsigned long delay;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 71
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 72 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
81fe8e5b73e3f4 Jacek Anaszewski 2015-10-07 @73 led_set_brightness_nosleep(led_cdev, LED_OFF);
a9c6ce57ec2f13 Hans de Goede 2016-11-08 74 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 75 return;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 76 }
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 77
a9c6ce57ec2f13 Hans de Goede 2016-11-08 78 if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
a9c6ce57ec2f13 Hans de Goede 2016-11-08 79 &led_cdev->work_flags)) {
a9c6ce57ec2f13 Hans de Goede 2016-11-08 80 clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 81 return;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 82 }
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 83
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 @84 brightness = led_get_brightness(led_cdev);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 85 if (!brightness) {
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 86 /* Time to switch the LED on. */
eb1610b4c27337 Hans de Goede 2016-10-23 87 if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
eb1610b4c27337 Hans de Goede 2016-10-23 88 &led_cdev->work_flags))
eb1610b4c27337 Hans de Goede 2016-10-23 89 brightness = led_cdev->new_blink_brightness;
eb1610b4c27337 Hans de Goede 2016-10-23 90 else
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 91 brightness = led_cdev->blink_brightness;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 92 delay = led_cdev->blink_delay_on;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 93 } else {
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 94 /* Store the current brightness value to be able
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 95 * to restore it when the delay_off period is over.
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 96 */
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 97 led_cdev->blink_brightness = brightness;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 98 brightness = LED_OFF;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 99 delay = led_cdev->blink_delay_off;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 100 }
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 101
81fe8e5b73e3f4 Jacek Anaszewski 2015-10-07 102 led_set_brightness_nosleep(led_cdev, brightness);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 103
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 104 /* Return in next iteration if led is in one-shot mode and we are in
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 105 * the final blink state so that the led is toggled each delay_on +
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 106 * delay_off milliseconds in worst case.
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 107 */
a9c6ce57ec2f13 Hans de Goede 2016-11-08 108 if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
a9c6ce57ec2f13 Hans de Goede 2016-11-08 109 if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 110 if (brightness)
a9c6ce57ec2f13 Hans de Goede 2016-11-08 111 set_bit(LED_BLINK_ONESHOT_STOP,
a9c6ce57ec2f13 Hans de Goede 2016-11-08 112 &led_cdev->work_flags);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 113 } else {
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 114 if (!brightness)
a9c6ce57ec2f13 Hans de Goede 2016-11-08 115 set_bit(LED_BLINK_ONESHOT_STOP,
a9c6ce57ec2f13 Hans de Goede 2016-11-08 116 &led_cdev->work_flags);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 117 }
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 118 }
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 119
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 120 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 121 }
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 122
fa15d8c69238b3 Hans de Goede 2023-05-10 123 static void set_brightness_delayed_set_brightness(struct led_classdev *led_cdev,
fa15d8c69238b3 Hans de Goede 2023-05-10 124 unsigned int value)
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 125 {
d33d1214a1ddf9 Lee Jones 2024-06-12 126 int ret;
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 127
fa15d8c69238b3 Hans de Goede 2023-05-10 128 ret = __led_set_brightness(led_cdev, value);
d33d1214a1ddf9 Lee Jones 2024-06-12 129 if (ret == -ENOTSUPP) {
fa15d8c69238b3 Hans de Goede 2023-05-10 130 ret = __led_set_brightness_blocking(led_cdev, value);
d33d1214a1ddf9 Lee Jones 2024-06-12 131 if (ret == -ENOTSUPP)
d33d1214a1ddf9 Lee Jones 2024-06-12 132 /* No back-end support to set a fixed brightness value */
d33d1214a1ddf9 Lee Jones 2024-06-12 133 return;
d33d1214a1ddf9 Lee Jones 2024-06-12 134 }
d33d1214a1ddf9 Lee Jones 2024-06-12 135
d84d80f38f0ff4 Heiner Kallweit 2016-01-22 136 /* LED HW might have been unplugged, therefore don't warn */
d33d1214a1ddf9 Lee Jones 2024-06-12 137 if (ret == -ENODEV && led_cdev->flags & LED_UNREGISTERING &&
d33d1214a1ddf9 Lee Jones 2024-06-12 138 led_cdev->flags & LED_HW_PLUGGABLE)
d33d1214a1ddf9 Lee Jones 2024-06-12 139 return;
d33d1214a1ddf9 Lee Jones 2024-06-12 140
d33d1214a1ddf9 Lee Jones 2024-06-12 141 if (ret < 0)
1afcadfcd184c3 Jacek Anaszewski 2015-10-19 142 dev_err(led_cdev->dev,
1afcadfcd184c3 Jacek Anaszewski 2015-10-19 143 "Setting an LED's brightness failed (%d)\n", ret);
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 144 }
757b06ae04b3b6 Jacek Anaszewski 2015-09-28 145
fa15d8c69238b3 Hans de Goede 2023-05-10 146 static void set_brightness_delayed(struct work_struct *ws)
fa15d8c69238b3 Hans de Goede 2023-05-10 147 {
fa15d8c69238b3 Hans de Goede 2023-05-10 148 struct led_classdev *led_cdev =
fa15d8c69238b3 Hans de Goede 2023-05-10 149 container_of(ws, struct led_classdev, set_brightness_work);
fa15d8c69238b3 Hans de Goede 2023-05-10 150
fa15d8c69238b3 Hans de Goede 2023-05-10 151 if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
fa15d8c69238b3 Hans de Goede 2023-05-10 @152 led_stop_software_blink(led_cdev);
fa15d8c69238b3 Hans de Goede 2023-05-10 153 set_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags);
fa15d8c69238b3 Hans de Goede 2023-05-10 154 }
fa15d8c69238b3 Hans de Goede 2023-05-10 155
fa15d8c69238b3 Hans de Goede 2023-05-10 156 /*
fa15d8c69238b3 Hans de Goede 2023-05-10 157 * Triggers may call led_set_brightness(LED_OFF),
fa15d8c69238b3 Hans de Goede 2023-05-10 158 * led_set_brightness(LED_FULL) in quick succession to disable blinking
fa15d8c69238b3 Hans de Goede 2023-05-10 159 * and turn the LED on. Both actions may have been scheduled to run
fa15d8c69238b3 Hans de Goede 2023-05-10 160 * before this work item runs once. To make sure this works properly
fa15d8c69238b3 Hans de Goede 2023-05-10 161 * handle LED_SET_BRIGHTNESS_OFF first.
fa15d8c69238b3 Hans de Goede 2023-05-10 162 */
2c70953b6f535f Remi Pommarel 2025-02-20 163 if (test_and_clear_bit(LED_SET_BRIGHTNESS_OFF, &led_cdev->work_flags)) {
fa15d8c69238b3 Hans de Goede 2023-05-10 164 set_brightness_delayed_set_brightness(led_cdev, LED_OFF);
2c70953b6f535f Remi Pommarel 2025-02-20 165 /*
2c70953b6f535f Remi Pommarel 2025-02-20 166 * The consecutives led_set_brightness(LED_OFF),
2c70953b6f535f Remi Pommarel 2025-02-20 167 * led_set_brightness(LED_FULL) could have been executed out of
2c70953b6f535f Remi Pommarel 2025-02-20 168 * order (LED_FULL first), if the work_flags has been set
2c70953b6f535f Remi Pommarel 2025-02-20 169 * between LED_SET_BRIGHTNESS_OFF and LED_SET_BRIGHTNESS of this
2c70953b6f535f Remi Pommarel 2025-02-20 170 * work. To avoid ending with the LED turned off, turn the LED
2c70953b6f535f Remi Pommarel 2025-02-20 171 * on again.
2c70953b6f535f Remi Pommarel 2025-02-20 172 */
2c70953b6f535f Remi Pommarel 2025-02-20 173 if (led_cdev->delayed_set_value != LED_OFF)
2c70953b6f535f Remi Pommarel 2025-02-20 174 set_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags);
2c70953b6f535f Remi Pommarel 2025-02-20 175 }
fa15d8c69238b3 Hans de Goede 2023-05-10 176
fa15d8c69238b3 Hans de Goede 2023-05-10 177 if (test_and_clear_bit(LED_SET_BRIGHTNESS, &led_cdev->work_flags))
fa15d8c69238b3 Hans de Goede 2023-05-10 178 set_brightness_delayed_set_brightness(led_cdev, led_cdev->delayed_set_value);
22720a87d0a966 Hans de Goede 2023-05-10 179
22720a87d0a966 Hans de Goede 2023-05-10 180 if (test_and_clear_bit(LED_SET_BLINK, &led_cdev->work_flags)) {
22720a87d0a966 Hans de Goede 2023-05-10 181 unsigned long delay_on = led_cdev->delayed_delay_on;
22720a87d0a966 Hans de Goede 2023-05-10 182 unsigned long delay_off = led_cdev->delayed_delay_off;
22720a87d0a966 Hans de Goede 2023-05-10 183
22720a87d0a966 Hans de Goede 2023-05-10 184 led_blink_set(led_cdev, &delay_on, &delay_off);
22720a87d0a966 Hans de Goede 2023-05-10 185 }
fa15d8c69238b3 Hans de Goede 2023-05-10 186 }
fa15d8c69238b3 Hans de Goede 2023-05-10 187 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v4 7/7 leds: Add virtual LED group driver with priority arbitration
In reply to Jonathan BrophyAdd a driver that implements virtual LED groups with priority-based arbitration for shared physical LEDs. The driver provides a multicolor LED interface while solving the coordination problem when multiple subsystems need to control the same physical LEDs.
Key features:
Winner-takes-all arbitration:
- Only virtual LEDs with brightness > 0 participate
- Highest priority wins (sequence number tie-breaking)
- Winner controls ALL physical LEDs
- Non-winner LEDs are turned off
Multicolor LED ABI support:
- Full compliance with standard multicolor LED interface
- Deterministic channel ordering by LED_COLOR_ID
- Two modes: multicolor (dynamic) and standard (fixed-color)
- Per-channel intensity and master brightness control
Memory optimization:
- Conditional debug compilation (~30% size reduction when disabled)
- Pre-allocated arbitration buffers
- Efficient O(1) physical LED lookup via XArray
- ~200 bytes per virtual LED in production builds
Locking design:
- Hierarchical lock acquisition order prevents deadlocks
- Lock-free arbitration with atomic sequence numbers
- Temporary lock release during hardware I/O to allow concurrency
Hardware support:
- GPIO, PWM, I2C, and SPI LED devices
- Automatic physical LED discovery and claiming
- Global ownership tracking prevents conflicts
- Power management with suspend/resume
Debugfs telemetry (CONFIG_DEBUG_FS):
- Arbitration statistics and latency metrics
- Per-LED win/loss counters
- Physical LED state inspection
- Stress testing interface
Module parameters:
- use_gamma_correction: Perceptual brightness (gamma 2.2)
- update_delay_us: Rate limiting for slow buses
- max_phys_leds: Buffer capacity (default 64)
- enable_update_batching: 10ms coalescing for rapid changes
Typical use cases:
- System status with boot/error priority levels
- RGB lighting with coordinated control
- Multi-element LED arrays (rings, strips)
--- drivers/leds/rgb/leds-group-virtualcolor.c | 3360 ++++++++++++++++++++ 1 file changed, 3360 insertions(+) create mode 100644 drivers/leds/rgb/leds-group-virtualcolor.c
drivers/leds/rgb/leds-group-virtualcolor.c new file +3360 -0
@@ -0,0 +1,3360 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* leds-group-virtualcolor.c - Virtual grouped LED driver with Multicolor ABI +* +* This driver is fully compliant with the multicolor LED ABI. +* It adds a policy layer to arbitrate shared physical LEDs, +* a problem not addressed by the LED core, without changing userspace-visible behavior. +* these additional extensions introduce new capabilities, such as: +* +* - Priority-based arbitration for shared physical LED ownership +* - Sequence/timestamp tie-breaking for deterministic conflict resolution +* - Runtime reconfiguration of shared channels for grouped LEDs +* - Atomic multi-device updates to ensure consistency +* - Group-wide brightness propagation and scaling +* - Support for arbitrated updates from multiple virtual LEDs to shared physical LEDs +* - Dynamic reallocation and resolution of conflicting virtual-to-physical mapping +* +* Priority-based arbitration resolves conflicts when multiple virtual LEDs +* reference the same physical LEDs. Arbitration rules are: +* 1. Priority value of led (higher wins) +* 2. Priority value of virtual controller (higher wins) +* 3. Sequence number for tie-breaking (most recent wins) +* 4. Winner-takes-all: ONE virtual LED controls ALL physical LEDs +* +* MC channel multipliers add advanced capabilities to LEDs, including: +* - Adjusting the relative brightness levels of different color channels +* - Normalizing output across different hardware vendors and physical configurations +* - Manipulating color temperature by changing the balance between channels +* - Avoiding overdriving specific channels unnecessarily +* - Mapping physical LEDs to application-specific color spaces and intensities +* - Emulating single fixed-color LEDs from multicolor LEDs +* - Dynamic reconfiguration of output characteristics +* - Capping brightness levels to suit specific use cases +* +* Winner-Takes-All Arbitration: +* - Only vLEDs with brightness > 0 participate +* - Highest priority wins (ties broken by sequence number) +* - Winner controls ALL physical LEDs +* - Physical LEDs not used by the winner are turned off +* +* Locking hierarchy (must be acquired in this order): +* 1. vcolor_controller.lock (per-controller) ← Controller FIRST +* 2. global_owner_rwsem (global) ← Global SECOND +* 3. virtual_led.lock (per-vLED) +* +* Never hold vLED locks during arbitration to avoid deadlock. +* Arbitration copies vLED state under the vLED lock, then releases locks +* before proceeding to core processing. +* +* Device Tree Dependency: +* This driver requires Device Tree (CONFIG_OF) due to LED phandle resolution. +* GPIO LEDs, in particular, rely on OF-specific APIs, as they lack full +* fwnode support. Minimal `CONFIG_OF` usage ensures easy migration to ACPI +* when fwnode abstraction improves. Key operations include: +* - `of_led_get()` - Called for LED phandle resolution within the single +* bridge function `vcolor_led_from_fwnode()`. +* - `device_for_each_child_node()` for child iteration +* - `fwnode_property_*()` for property access +* - `fwnode_handle_get/put()` for reference management +* +* By isolating OF dependency in the bridge function, migration to +* `fwnode_led_get()` will be straightforward when supported by the LED subsystem. +* +* Author: Jonathan Brophy <[email protected]> +*/ + +#include <dt-bindings/leds/common.h> + +#include <linux/atomic.h> +#include <linux/compiler.h> +#include <linux/debugfs.h> +#include <linux/delay.h> +#include <linux/device.h> +#include <linux/err.h> +#include <linux/kernel.h> +#include <linux/kref.h> +#include <linux/ktime.h> +#include <linux/leds.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of_platform.h> +#include <linux/platform_device.h> +#include <linux/pm.h> +#include <linux/pm_runtime.h> +#include <linux/property.h> +#ifdef CONFIG_DEBUG_FS +#include <linux/random.h> +#endif +#include <linux/ratelimit.h> +#include <linux/rwsem.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/types.h> +#include <linux/workqueue.h> +#include <linux/xarray.h> + +#define DRIVER_NAME "leds-group-virtualcolor" +#define DRIVER_VERSION "4" +#define VLED_DEBUGFS_DIR DRIVER_NAME +#define MAX_PHYS_LEDS_DEFAULT 64 +#define UPDATE_BATCH_DELAY_MS 10 +#define DEFAULT_UPDATE_RATE_LIMIT 100/* Updates per second per vLED */ +#define PRIORITY_MAX INT_MAX +#define VLED_SNAPSHOT_DEFAULT 32/* Typical system has <32 vLEDs per controller */ + +#ifdef CONFIG_DEBUG_FS +#define MAX_DEBUGFS_NAME 96/* Sized for "function:color-multicolor-##" + vLED name */ +#endif + +#ifdef CONFIG_LOCKDEP +#define assert_controller_locked (lvc) lockdep_assert_held(&(lvc)->lock) +#define assert_vled_locked (vled) lockdep_assert_held(&(vled)->lock) +#else +#define assert_controller_locked (lvc) ((void)(lvc)) +#define assert_vled_locked (vled) ((void)(vled)) +#endif + +static inline bool is_valid_led_cdev (struct led_classdev *cdev) +{ +if (!cdev) +return false; +if (!cdev->brightness_set && !cdev->brightness_set_blocking) +return false; +return true; +} + +/* Structured logging macros */ +#define ctrl_err (lvc, fmt, ...) \ + dev_err(&(lvc)->pdev->dev, "[CTRL] " fmt, ##__VA_ARGS__) + +#define ctrl_warn (lvc, fmt, ...) \ + dev_warn(&(lvc)->pdev->dev, "[CTRL] " fmt, ##__VA_ARGS__) + +#define ctrl_info (lvc, fmt, ...) \ + dev_info(&(lvc)->pdev->dev, "[CTRL] " fmt, ##__VA_ARGS__) + +#define ctrl_dbg (lvc, fmt, ...) \ + dev_dbg(&(lvc)->pdev->dev, "[CTRL] " fmt, ##__VA_ARGS__) + +#define arb_err (lvc, fmt, ...) \ + dev_err_ratelimited(&(lvc)->pdev->dev, "[ARB] " fmt, ##__VA_ARGS__) + +#define vled_err (vled, fmt, ...) \ + dev_err(&(vled)->ctrl->pdev->dev, "[vLED:%s] " fmt, (vled)->name, ##__VA_ARGS__) + +static_assert (sizeof (void *) <=sizeof (unsigned long ), +"XArray keys require pointer size <= unsigned long" ); + +/* Module parameters */ +#ifdef CONFIG_DEBUG_FS +static bool enable_debugfs = true; +#else +static bool enable_debugfs; +#endif + +static bool use_gamma_correction; +static unsigned int update_delay_us; +static unsigned int max_phys_leds =MAX_PHYS_LEDS_DEFAULT ; +static bool enable_update_batching; + +/* LED mode enumeration */ +enum vled_mode { +VLED_MODE_MULTICOLOR =0 , +VLED_MODE_STANDARD =1 , +}; + +struct vcolor_controller ; + +struct mc_channel { +u8 color_id; +u8 num_leds; +struct led_classdev **leds; +struct device **led_devs; +u8 intensity; +u8 scale; +}; + +struct phys_led_entry { +/* HOT PATH */ +struct led_classdev *cdev; +u8 chosen_brightness; +s32 chosen_priority; +u64 chosen_sequence; +struct kref refcount; +unsigned int list_index; + +/* COLD PATH */ +struct device *dev; +struct list_head list; +#ifdef CONFIG_DEBUG_FS +char winner_name[MAX_DEBUGFS_NAME ]; +#endif +}; + +struct update_buffer { +struct phys_led_entry **entries; +u8 *brightness; +unsigned int capacity; +unsigned int max_capacity; +}; + +struct arbitration_buffers { +u8 *intensities; +u8 *scales; +unsigned int capacity; +}; + +static DEFINE_XARRAY (global_owner_xa); +static DECLARE_RWSEM (global_owner_rwsem); + +struct global_phys_owner { +struct platform_device *owner_pdev; +}; + + +/* +* struct virtual_led - Virtual LED with priority-based arbitration +* @cdev: LED class device +* @priority: Arbitration priority (higher wins) +* @name: LED name (assigned by LED core) +* @channels: Array of color channels +* @num_channels: Number of color channels (max 255, but dirty tracking limited to 32) +* @lock: Protects vLED state during updates +* @list: Entry in controller's vLED list +* @fwnode: Firmware node for DT parsing +* @ctrl: Parent controller +* @cdev_registered: LED class device registration status +* @intensity_ratelimit: Rate limiter for intensity updates +* @arb_bufs: Pre-allocated buffers for arbitration +* @mode: Operating mode (MULTICOLOR or STANDARD) +* @refcount: Reference counter +* @sequence: Monotonic sequence number for tie-breaking +* All channels are processed during each arbitration cycle for simplicity. +* +*/ +struct virtual_led { +struct led_classdev cdev; +s32 priority; +const char *name; +struct mc_channel *channels; +u8 num_channels; +struct mutex lock; +struct list_head list; +struct fwnode_handle *fwnode; +struct vcolor_controller *ctrl; +bool cdev_registered; +struct ratelimit_state intensity_ratelimit; +struct arbitration_buffers arb_bufs; +enum vled_mode mode; +struct kref refcount; +u64 sequence; + +#ifdef CONFIG_DEBUG_FS +struct dentry *debugfs_dir; +u64 brightness_set_count; +u64 intensity_update_count; +u64 arbitration_wins; +u64 arbitration_losses; +u64 arbitration_participations; +u64 buffer_allocation_failures; +u64 intensity_parse_errors; +u64 ratelimit_drops; +u64 blink_requests; +#endif +}; + +struct vcolor_controller { +struct list_head leds; +struct mutex lock; +struct list_head phys_leds; +struct xarray phys_xa; +struct platform_device *pdev; +struct update_buffer update_buf; +/* Pre-allocated arbitration buffers */ +struct virtual_led **vled_snapshot; +unsigned int vled_snapshot_capacity; +struct phys_led_entry **ple_snapshot; +unsigned int ple_snapshot_capacity; +bool *ple_usage_bitmap; +unsigned int ple_usage_bitmap_capacity; +bool suspended; +atomic_t rebuilding; +bool needs_arbitration; +unsigned int phys_led_count; +atomic_t removing; +struct delayed_work update_work; +atomic_t pending_updates; +atomic64_t global_sequence; +bool first_arbitration; +#ifdef CONFIG_DEBUG_FS +struct dentry *debugfs_root; +u64 arbitration_count; +u64 update_count; +atomic64_t allocation_failures; +atomic64_t update_buffer_overflows; +atomic64_t ratelimit_hits; +u64 arb_latency_min_ns; +u64 arb_latency_max_ns; +u64 arb_latency_total_ns; +u64 arb_latency_count; +ktime_t last_update; +#endif +}; + +/* Forward declarations */ +static void controller_rebuild_phys_leds (struct vcolor_controller *lvc); +static void controller_destroy_phys_list (struct vcolor_controller *lvc); +static void controller_run_arbitration_and_update (struct vcolor_controller *lvc); +static void phys_led_entry_release (struct kref *ref); +static void virtual_led_release (struct kref *ref); + +static inline unsigned long get_stable_led_key (struct led_classdev *cdev) +{ +if (!cdev) +return 0 ; + +/* GPIO LEDs don't have dev - use cdev pointer as key */ +if (cdev->dev) +return (unsigned long )cdev->dev; +else +return (unsigned long )cdev; +} + +static inline struct virtual_led *virtual_led_get (struct virtual_led *vled) +{ +if (vled) +kref_get (&vled->refcount); +return vled; +} + +static inline void virtual_led_put (struct virtual_led *vled) +{ +if (vled) +kref_put (&vled->refcount, virtual_led_release); +} + +static inline bool controller_safe_arbitrate (struct vcolor_controller *lvc) +{ +bool ran; + +if (!lvc) +return false; + +/* Fast path: avoid lock acquisition if nothing to do */ +if (atomic_read (&lvc->removing)) +return false; + +/* FIX: Queue deferred arbitration if rebuilding */ +if (atomic_read (&lvc->rebuilding)) { + lvc->needs_arbitration = true; +return false; + } + +mutex_lock (&lvc->lock); + +/* Check suspended under lock to prevent suspend race */ + ran = false; +if (!lvc->suspended && !atomic_read (&lvc->rebuilding) && +device_is_registered (&lvc->pdev->dev)) { +controller_run_arbitration_and_update (lvc); + ran = true; + } + +/* FIX: Lock was released by controller_run_arbitration_and_update */ +return ran; +} + + +/* +* parse_leds_fwnode_array - Parse LED references using fwnode APIs +* @dev: Device for logging and memory allocation +* @fwnode: Firmware node containing LED references +* @propname: Property name (e.g., "leds") +* @out_leds: Output array of LED classdev pointers +* @out_devs: Output array of device pointers (may contain NULLs for GPIO LEDs) +* @out_count: Number of LEDs found +* +* Uses fwnode APIs for property traversal, with a single OF bridge for LED resolution. +* This pattern mirrors V4L2's approach and makes future fwnode_led_get() migration trivial. +*/ +static int parse_leds_fwnode_array (struct device *dev, +const struct fwnode_handle *fwnode, +const char *propname, +struct led_classdev ***out_leds, +struct device ***out_devs, +u8 *out_count) +{ +struct fwnode_reference_args args; +int count, idx, valid, i; +struct led_classdev **leds; +struct device **devs; +struct led_classdev *cdev; +struct device *led_dev; +int ret; + + *out_leds =NULL ; + *out_devs =NULL ; + *out_count =0 ; + +/* Count phandle references using generic fwnode API */ + count =0 ; +while (fwnode_property_get_reference_args (fwnode, propname, +NULL ,0 , count, &args) ==0 ) { +fwnode_handle_put (args.fwnode); + count++; + } + +if (count <=0 ) +return 0 ; + +/* Allocate temporary arrays for LED/device pointers */ + leds =kcalloc (count,sizeof (*leds),GFP_KERNEL ); +if (!leds) +return -ENOMEM ; + + devs =kcalloc (count,sizeof (*devs),GFP_KERNEL ); +if (!devs) { +kfree (leds); +return -ENOMEM ; + } + +/* Iterate through each LED reference and PACK valid entries */ + valid =0 ; +for (idx =0 ; idx < count; idx++) { + +/*Resolve LED from fwnode using index.*/ + cdev =fwnode_led_get (fwnode, idx, &led_dev); + +if (IS_ERR (cdev)) { + ret =PTR_ERR (cdev); + +/* Handle deferred probe - cleanup and return immediately */ +if (ret == -EPROBE_DEFER ) { +dev_info (dev,"LED %d not ready yet (EPROBE_DEFER), will retry\n" , idx); + +/* Release all previously acquired LEDs and devices */ +for (i =0 ; i < valid; i++) { +if (leds[i]) +led_put (leds[i]); +if (devs[i]) +put_device (devs[i]); + } + +kfree (leds); +kfree (devs); +return -EPROBE_DEFER ; + } + +/* Other errors - log and skip this LED */ +dev_warn (dev,"Failed to resolve LED %d: %d\n" , idx, ret); +continue ; + } + +/* Store valid LED in PACKED position */ +if (is_valid_led_cdev (cdev)) { + leds[valid] = cdev;/* Pack at 'valid' index, not 'idx' */ + devs[valid] = led_dev;/* May be NULL for GPIO LEDs */ + valid++; + }else { +dev_warn (dev,"LED %d is not valid (no brightness_set method)\n" , idx); +led_put (cdev); +if (led_dev) +put_device (led_dev); + } + } + +/* Check if we got any valid LEDs */ +if (valid ==0 ) { +dev_warn (dev,"Property '%s': none of %d LED(s) resolved\n" , + propname, count); +kfree (leds); +kfree (devs); +return -ENODEV ; + } + +/* Success - return PACKED arrays to caller */ + *out_leds = leds; + *out_devs = devs; + *out_count = (u8 )valid; + +return 0 ; +} + +static int validate_and_set_max_brightness (struct virtual_led *vled) +{ +unsigned int i, j; +enum led_brightness min_max_brightness =LED_FULL ; + +/* +* For multicolor mode, virtual LED always exposes full 8-bit range. +* Scaling happens automatically in scale_intensity_by_brightness(). +*/ +if (vled->mode ==VLED_MODE_MULTICOLOR ) { + vled->cdev.max_brightness =LED_FULL ; +return 0 ; + } + +/* +* For standard mode, use minimum of physical LEDs since color is +* fixed by multipliers. +*/ +for (i =0 ; i < vled->num_channels; i++) { +for (j =0 ; j < vled->channels[i].num_leds; j++) { +enum led_brightness max_brightness; + +if (!vled->channels[i].leds[j]) +continue ; + + max_brightness = vled->channels[i].leds[j]->max_brightness; +if (max_brightness ==0 ) + max_brightness =LED_FULL ; + +if (max_brightness < min_max_brightness) + min_max_brightness = max_brightness; + } + } + +if (min_max_brightness <1 ) + min_max_brightness =1 ; + + vled->cdev.max_brightness = min_max_brightness; +return 0 ; +} + +static void global_release_all_for_pdev (struct platform_device *pdev) +{ +unsigned long index; +unsigned long released; +struct global_phys_owner *gpo; + +down_write (&global_owner_rwsem); + + released =0 ; + +/* +* FIXED: Use xa_for_each() + xa_erase() instead of XA_STATE + xas_store(). +* +* The old code used xas_store(&xas, NULL) inside xas_for_each(), which +* corrupts the iterator state and can skip entries or cause crashes. +* +* xa_for_each() is safe to use with xa_erase() because: +* 1. xa_for_each is a simple macro that doesn't maintain complex state +* 2. xa_erase() is designed to work during iteration +* 3. The iterator naturally moves to the next valid entry +*/ +xa_for_each (&global_owner_xa, index, gpo) { +if (gpo && gpo->owner_pdev == pdev) { +xa_erase (&global_owner_xa, index); +kfree (gpo); + released++; + } + } + +up_write (&global_owner_rwsem); + +if (released) { +dev_info (&pdev->dev,"Released %lu physical LED ownership claims\n" , + released); + } +} + +static void phys_led_entry_release (struct kref *ref) +{ +struct phys_led_entry *ple; + + ple = container_of(ref, struct phys_led_entry, refcount); + +if (ple->dev) +put_device (ple->dev); + +kfree (ple); +} + +static inline struct phys_led_entry *phys_led_entry_get ( +struct phys_led_entry *ple) +{ +if (ple) +kref_get (&ple->refcount); +return ple; +} + +static inline void phys_led_entry_put (struct phys_led_entry *ple) +{ +if (ple) +kref_put (&ple->refcount, phys_led_entry_release); +} + +/* +* claim_physical_led - Claim ownership of a physical LED +* +* LOCKING: Acquires locks in this order (matching hierarchy): +* 1. lvc->lock (controller) - acquired FIRST +* 2. global_owner_rwsem (global) - acquired SECOND +* +* This order prevents AB-BA deadlock. Caller must NOT hold lvc->lock on entry. +*/ +static bool claim_physical_led (struct vcolor_controller *lvc, +struct led_classdev *cdev, +struct device *dev, +struct phys_led_entry **out_ple) +{ +struct global_phys_owner *gpo; +struct phys_led_entry *ple =NULL ; +void *ret_ptr; +bool success = false; +bool newly_claimed_global = false; +unsigned long key; + + *out_ple =NULL ; + +if (!cdev) +return false; + + key =get_stable_led_key (cdev); +if (!key) { +ctrl_err (lvc,"Failed to get stable key for LED '%s'\n" , + cdev->name ? cdev->name :"(unnamed)" ); +return false; + } + +/* +* FIXED: Acquire controller lock FIRST, then check removal flag. +* This eliminates TOCTOU race - once we hold the lock, removal +* cannot proceed past the rebuilding wait. +*/ +mutex_lock (&lvc->lock); + +/* Single authoritative check under lock */ +if (atomic_read (&lvc->removing)) { +mutex_unlock (&lvc->lock); +return false; + } + +down_write (&global_owner_rwsem); + + gpo =xa_load (&global_owner_xa, key); +if (xa_is_value (gpo)) { +ctrl_err (lvc,"Invalid XArray entry for LED '%s'\n" , cdev->name); + goto out_unlock; + } + +if (gpo && gpo->owner_pdev != lvc->pdev) { +ctrl_warn (lvc,"Physical LED '%s' already claimed by another controller\n" , + cdev->name); + goto out_unlock; + } + +if (xa_load (&lvc->phys_xa, key)) { +ctrl_dbg (lvc,"LED '%s' already claimed locally, skipping duplicate\n" , + cdev->name); + goto out_unlock; + } + + ple =kzalloc (sizeof (*ple),GFP_KERNEL ); +if (!ple) { +#ifdef CONFIG_DEBUG_FS +ctrl_err (lvc,"Failed to allocate phys_led_entry for '%s'\n" , + cdev->name); +atomic64_inc (&lvc->allocation_failures); +#endif + goto out_unlock; + } + +kref_init (&ple->refcount); + ple->cdev = cdev; + ple->dev = dev; +if (dev) +get_device (dev); + ple->chosen_brightness =0 ; + ple->chosen_priority =-1 ; + ple->chosen_sequence =0 ; +#ifdef CONFIG_DEBUG_FS + ple->winner_name[0 ] ='\0' ; +#endif + +if (!gpo) { + gpo =kzalloc (sizeof (*gpo),GFP_KERNEL ); +if (!gpo) { +#ifdef CONFIG_DEBUG_FS +ctrl_err (lvc,"Failed to allocate ownership record for LED '%s'\n" , + cdev->name); +atomic64_inc (&lvc->allocation_failures); +#endif + goto out_unlock; + } + + gpo->owner_pdev = lvc->pdev; + ret_ptr =xa_store (&global_owner_xa, key, gpo,GFP_KERNEL ); + +if (xa_is_err (ret_ptr)) { +ctrl_err (lvc,"Failed to register global ownership for LED '%s': %ld\n" , + cdev->name,PTR_ERR (ret_ptr)); +kfree (gpo); +#ifdef CONFIG_DEBUG_FS +atomic64_inc (&lvc->allocation_failures); +#endif + goto out_unlock; + } + + newly_claimed_global = true; + } + + ret_ptr =xa_store (&lvc->phys_xa, key, ple,GFP_KERNEL ); +if (xa_is_err (ret_ptr)) { +ctrl_err (lvc,"Failed to store phys_led_entry '%s' in xarray: %ld\n" , + cdev->name,PTR_ERR (ret_ptr)); + +if (newly_claimed_global) { + gpo =xa_erase (&global_owner_xa, key); +if (gpo && !xa_is_value (gpo)) { +ctrl_dbg (lvc,"Cleaned up leaked global ownership for '%s' after local store failure\n" , + cdev->name); +kfree (gpo); + } + } + +#ifdef CONFIG_DEBUG_FS +atomic64_inc (&lvc->allocation_failures); +#endif + goto out_unlock; + } + +list_add_tail (&ple->list, &lvc->phys_leds); + lvc->phys_led_count++; + *out_ple = ple; + success = true; + +out_unlock: +/* FIXED: Clean up XArray BEFORE releasing locks */ +if (!success && ple) { +void *stored =xa_load (&lvc->phys_xa, key); + +if (stored == ple) +xa_erase (&lvc->phys_xa, key); + } + +/* Release locks in reverse order */ +up_write (&global_owner_rwsem); +mutex_unlock (&lvc->lock); + +/* Safe cleanup - removed from XArray first */ +if (!success && ple) +phys_led_entry_put (ple); + +return success; +} + +static void add_led_group_to_phys_list (struct vcolor_controller *lvc, +struct led_classdev **leds, +struct device **devs, +unsigned int count) +{ +unsigned int i; +struct phys_led_entry *ple; + +for (i =0 ; i < count; i++) { +if (!leds || !leds[i]) +continue ; + +claim_physical_led (lvc, leds[i], devs ? devs[i] :NULL , &ple); + } +} + +static void controller_destroy_phys_list (struct vcolor_controller *lvc) +{ +struct phys_led_entry *ple, *tmp; +unsigned long key; + +assert_controller_locked (lvc); + +list_for_each_entry_safe (ple, tmp, &lvc->phys_leds, list) { +list_del (&ple->list); + +if (ple->cdev) { + key =get_stable_led_key (ple->cdev); +if (key) +xa_erase (&lvc->phys_xa, key); + ple->cdev =NULL ; + } + +phys_led_entry_put (ple); + } + +INIT_LIST_HEAD (&lvc->phys_leds); + lvc->phys_led_count =0 ; +} + +/* +* controller_rebuild_phys_leds - Rebuild physical LED ownership mappings +* @lvc: Controller instance +* +* LOCKING: Must be called with lvc->lock held. +* +* IMPORTANT: This function TEMPORARILY DROPS lvc->lock during LED reference +* acquisition to avoid holding the lock during potentially blocking operations. +* The atomic_rebuilding flag prevents concurrent arbitration while the lock +* is dropped. +* +* Call sequence: +* 1. Assert lvc->lock is held +* 2. Set atomic_rebuilding = 1 +* 3. Take snapshot of vLEDs with refcounts +* 4. Destroy existing physical LED mappings +* 5. DROP LOCK (intentional - see locking hierarchy comment at top of file) +* 6. Process vLED channels and claim physical LEDs +* 7. REACQUIRE LOCK +* 8. Assign list indices for O(1) arbitration lookup +* 9. Clear atomic_rebuilding = 0 +* 10. Run arbitration to apply new state +* +* The lock drop is necessary because: +* - add_led_group_to_phys_list() calls claim_physical_led() +* - claim_physical_led() acquires global_owner_rwsem (higher in hierarchy) +* - Holding lvc->lock during this would violate lock ordering +*/ +static void controller_rebuild_phys_leds (struct vcolor_controller *lvc) +{ +struct virtual_led *vled, **vled_snapshot; +unsigned int i, j, vled_count, actual_count; +bool needs_free = false; + +assert_controller_locked (lvc); + +atomic_set (&lvc->rebuilding,1 ); + +/* Count vLEDs first */ + vled_count =0 ; +list_for_each_entry (vled, &lvc->leds, list) + vled_count++; + +/* Explicitly turn off LEDs when no vLEDs exist */ +if (vled_count ==0 ) { +struct phys_led_entry *ple; + +/* Turn off all physical LEDs before destroying list */ +list_for_each_entry (ple, &lvc->phys_leds, list) { +if (ple->cdev) { +if (ple->cdev->brightness_set_blocking) + ple->cdev->brightness_set_blocking (ple->cdev,0 ); +else if (ple->cdev->brightness_set) + ple->cdev->brightness_set (ple->cdev,0 ); + ple->cdev->brightness =0 ; + } + } + +/* Safe to destroy when no vLEDs exist */ +controller_destroy_phys_list (lvc); +xa_destroy (&lvc->phys_xa); +xa_init (&lvc->phys_xa); +atomic_set (&lvc->rebuilding,0 ); +return ; + } + +/* Allocate snapshot BEFORE destroying existing state */ +if (vled_count > lvc->vled_snapshot_capacity) { +ctrl_warn (lvc, +"vLED count %u exceeds snapshot capacity %u, using dynamic allocation\n" , + vled_count, lvc->vled_snapshot_capacity); + vled_snapshot =kcalloc (vled_count,sizeof (*vled_snapshot),GFP_KERNEL ); +if (!vled_snapshot) { +ctrl_err (lvc,"Failed to allocate vled snapshot for rebuild (OOM) - keeping existing state\n" ); +#ifdef CONFIG_DEBUG_FS +atomic64_inc (&lvc->allocation_failures); +#endif +atomic_set (&lvc->rebuilding,0 ); +return ; + } + needs_free = true; + }else { + vled_snapshot = lvc->vled_snapshot; + needs_free = false; + } + +/* Copy vLED pointers with refcounts BEFORE destroying state */ + actual_count =0 ; +list_for_each_entry (vled, &lvc->leds, list) { +if (actual_count >= vled_count) { +ctrl_warn (lvc,"vLED count increased during snapshot! Expected %u\n" , vled_count); +break ; + } + vled_snapshot[actual_count++] =virtual_led_get (vled); + } + +/* Fail hard if count increased (race condition) */ +if (actual_count > vled_count) { +ctrl_err (lvc,"vLED count INCREASED during snapshot (%u -> %u) - aborting rebuild\n" , + vled_count, actual_count); +for (i =0 ; i < actual_count; i++) +virtual_led_put (vled_snapshot[i]); +if (needs_free) +kfree (vled_snapshot); +atomic_set (&lvc->rebuilding,0 ); +return ; + } + +/* Now safe to destroy existing state */ +controller_destroy_phys_list (lvc); +xa_destroy (&lvc->phys_xa); +xa_init (&lvc->phys_xa); + +/* Check removal flag BEFORE dropping lock */ +if (atomic_read (&lvc->removing)) { +atomic_set (&lvc->rebuilding,0 ); +/* Release snapshot references */ +for (i =0 ; i < actual_count; i++) { +if (vled_snapshot[i]) +virtual_led_put (vled_snapshot[i]); + } +if (needs_free) +kfree (vled_snapshot); +return ; + } + +mutex_unlock (&lvc->lock); + +/* Process all vLEDs outside lock */ +for (i =0 ; i < actual_count; i++) { + vled = vled_snapshot[i]; +if (!vled) +continue ; + +/* Double-check removal flag */ +if (atomic_read (&lvc->removing)) { +for (; i < actual_count; i++) { +if (vled_snapshot[i]) +virtual_led_put (vled_snapshot[i]); + } + goto out_cleanup; + } + +for (j =0 ; j < vled->num_channels; j++) { +add_led_group_to_phys_list (lvc, vled->channels[j].leds, + vled->channels[j].led_devs, + vled->channels[j].num_leds); + } + +virtual_led_put (vled); + vled_snapshot[i] =NULL ; + } + +out_cleanup: +if (needs_free) +kfree (vled_snapshot); + +mutex_lock (&lvc->lock); + +/* Assign list indices for O(1) lookup during arbitration */ + { +unsigned int idx =0 ; +struct phys_led_entry *ple_temp; + +list_for_each_entry (ple_temp, &lvc->phys_leds, list) { + ple_temp->list_index = idx++; + } + } + +/* FIX #4: Run arbitration BEFORE clearing rebuilding flag */ +if (!atomic_read (&lvc->removing)) +controller_run_arbitration_and_update (lvc); + +/* FIX #2: Check for deferred arbitration */ +if (lvc->needs_arbitration && !atomic_read (&lvc->removing)) { + lvc->needs_arbitration = false; +controller_run_arbitration_and_update (lvc); + } + +/* FIX #4: Clear rebuilding flag AFTER arbitration completes */ +atomic_set (&lvc->rebuilding,0 ); +} + +static const u8 gamma_table[256 ] = { +0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,4 ,4 , +4 ,4 ,5 ,5 ,5 ,5 ,6 ,6 ,6 ,7 ,7 ,7 ,8 ,8 ,8 ,9 ,9 ,9 ,10 ,10 ,11 ,11 ,11 ,12 ,12 ,13 ,13 ,14 , +14 ,15 ,15 ,16 ,16 ,17 ,17 ,18 ,18 ,19 ,19 ,20 ,20 ,21 ,22 ,22 ,23 ,23 ,24 ,25 ,25 ,26 , +26 ,27 ,28 ,28 ,29 ,30 ,30 ,31 ,32 ,32 ,33 ,34 ,34 ,35 ,36 ,37 ,37 ,38 ,39 ,40 ,40 ,41 , +42 ,43 ,44 ,44 ,45 ,46 ,47 ,48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 ,58 ,59 ,60 ,61 ,62 , +63 ,64 ,65 ,66 ,67 ,68 ,70 ,71 ,72 ,73 ,74 ,75 ,76 ,78 ,79 ,80 ,81 ,82 ,84 ,85 ,86 ,87 , +89 ,90 ,91 ,92 ,94 ,95 ,96 ,97 ,99 ,100 ,101 ,103 ,104 ,105 ,107 ,108 ,109 ,111 ,112 , +114 ,115 ,116 ,118 ,119 ,121 ,122 ,123 ,125 ,126 ,128 ,129 ,131 ,132 ,134 ,135 ,137 , +138 ,140 ,141 ,143 ,144 ,146 ,147 ,149 ,150 ,152 ,154 ,155 ,157 ,158 ,160 ,162 ,163 , +165 ,167 ,168 ,170 ,172 ,173 ,175 ,177 ,178 ,180 ,182 ,184 ,185 ,187 ,189 ,191 ,192 , +194 ,196 ,198 ,200 ,201 ,203 ,205 ,207 ,209 ,211 ,212 ,214 ,216 ,218 ,220 ,222 ,224 , +226 ,228 ,230 ,232 ,234 ,236 ,238 ,240 ,242 ,244 ,246 ,248 ,250 ,253 ,255 +}; + +static u8 scale_intensity_by_brightness (u8 intensity,u8 global_brightness, +u8 max_global_brightness) +{ +u32 scaled_val; +u8 final_intensity; + +if (max_global_brightness ==0 ) +return 0 ; + + scaled_val = (u32 )intensity * (u32 )global_brightness; + final_intensity = (u8 )clamp_val (scaled_val / max_global_brightness,0 ,255 ); + +if (use_gamma_correction) + final_intensity = gamma_table[final_intensity]; + +return final_intensity; +} + +static u8 vled_channel_get_final_intensity (enum vled_mode mode, +u8 raw_intensity, +u8 scale, +enum led_brightness vled_brightness, +enum led_brightness vled_max_brightness) +{ +u8 intensity = raw_intensity; +u16 scaled_val; + +if (mode ==VLED_MODE_MULTICOLOR ) { +if (scale <255 ) { + scaled_val = ((u16 )intensity * (u16 )scale) /255 ; + intensity = (u8 )clamp_val (scaled_val,0 ,255 ); + } + }else { + intensity = scale; + } + +return scale_intensity_by_brightness (intensity, vled_brightness, + vled_max_brightness); +} + +static void apply_winner_to_channel (struct vcolor_controller *lvc, +struct virtual_led *vled, +struct led_classdev **leds, +unsigned int count, +u8 final_intensity) +{ +unsigned int i; +struct phys_led_entry *ple; +unsigned long key; +#ifdef CONFIG_DEBUG_FS +int copy_result; +#endif + +assert_controller_locked (lvc); + +for (i =0 ; i < count; i++) { +if (!leds[i]) +continue ; + + key =get_stable_led_key (leds[i]); +if (!key) +continue ; + + ple =xa_load (&lvc->phys_xa, key); +if (!ple ||xa_is_value (ple)) +continue ; + +/* Winner takes all - no comparison needed */ + ple->chosen_brightness = final_intensity; + ple->chosen_priority = vled->priority; + ple->chosen_sequence = vled->sequence; + +#ifdef CONFIG_DEBUG_FS +if (vled->name) { + copy_result =strscpy (ple->winner_name, vled->name,MAX_DEBUGFS_NAME ); +if (copy_result <0 ) { +dev_warn_once (&lvc->pdev->dev, +"vLED name truncated in telemetry: '%.32s...'\n" , + vled->name); + } + }else { +strscpy (ple->winner_name,"(unnamed)" ,MAX_DEBUGFS_NAME ); + } +#endif + } +} + +static void mark_winner_leds (struct vcolor_controller *lvc, +struct virtual_led *winner_vled, +unsigned int channel_idx, +bool *ple_used_by_winner, +unsigned int ple_count) +{ +unsigned int i; + +for (i =0 ; i < winner_vled->channels[channel_idx].num_leds; i++) { +unsigned long key; +struct phys_led_entry *ple_temp; + +if (!winner_vled->channels[channel_idx].leds[i]) +continue ; + + key =get_stable_led_key (winner_vled->channels[channel_idx].leds[i]); +if (!key) +continue ; + + ple_temp =xa_load (&lvc->phys_xa, key); +if (!ple_temp ||xa_is_value (ple_temp)) +continue ; + +/* Direct O(1) index lookup instead of O(n) list scan */ +if (ple_temp->list_index < ple_count) + ple_used_by_winner[ple_temp->list_index] = true; + } +} + +/* +* controller_run_arbitration_and_update - Run winner-takes-all arbitration +* @lvc: Controller instance +* +* Selects the highest priority active vLED and applies its configuration +* to all physical LEDs. Non-winner vLEDs are ignored (winner-takes-all). +* +* Context: Must be called with lvc->lock held. +* +* CRITICAL LOCKING BEHAVIOR: +* - Expects lvc->lock to be held on entry +* - Temporarily RELEASES lvc->lock during hardware I/O +* - REACQUIRES lvc->lock before return +* - Lock state is PRESERVED on return, but shared state may change during unlock +* +* Rationale for temporary unlock: +* Hardware I/O can block for milliseconds. Holding lvc->lock during this +* would prevent other vLEDs from updating their state (brightness_set calls). +* By releasing the lock during I/O, we allow concurrent operations while +* preventing corruption of arbitration results. +* +* Locking: Acquires vLED locks briefly to snapshot state, then processes +* without holding any locks during hardware access. +*/ +static void controller_run_arbitration_and_update (struct vcolor_controller *lvc) +{ +struct phys_led_entry *ple; +struct virtual_led *vled; +struct virtual_led *winner_vled =NULL ; +s32 highest_priority =-1 ; +u64 latest_sequence =0 ; +u8 *intensities; +u8 *scales; +enum vled_mode mode; +unsigned int j; +u8 final_intensity; +struct phys_led_entry **local_entries; +u8 *local_brightness; +unsigned int update_count; +unsigned int i; +u64 vled_seq; +enum led_brightness brightness, max_brightness; +bool *ple_used_by_winner; +unsigned int ple_count; + +#ifdef CONFIG_DEBUG_FS +ktime_t arb_start, arb_end; +u64 arb_duration_ns; +#endif + +if (!lvc || lvc->suspended ||atomic_read (&lvc->removing)) +return ; + +assert_controller_locked (lvc); + + local_entries = lvc->update_buf.entries; + local_brightness = lvc->update_buf.brightness; + +if (!local_entries || !local_brightness) { +dev_err (&lvc->pdev->dev,"Update buffers not allocated, cannot arbitrate\n" ); +/* FIX: Unlock before returning */ +mutex_unlock (&lvc->lock); +return ; + } + +#ifdef CONFIG_DEBUG_FS + arb_start =ktime_get (); + lvc->arbitration_count++; +#endif + +/* Count physical LEDs */ + ple_count =0 ; +list_for_each_entry (ple, &lvc->phys_leds, list) + ple_count++; + +if (ple_count ==0 ) { +#ifdef CONFIG_DEBUG_FS + arb_end =ktime_get (); + arb_duration_ns =ktime_to_ns (ktime_sub (arb_end, arb_start)); +if (lvc->arb_latency_count ==0 || arb_duration_ns < lvc->arb_latency_min_ns) + lvc->arb_latency_min_ns = arb_duration_ns; +if (arb_duration_ns > lvc->arb_latency_max_ns) + lvc->arb_latency_max_ns = arb_duration_ns; + lvc->arb_latency_total_ns += arb_duration_ns; + lvc->arb_latency_count++; +#endif +/* FIX: Unlock before returning */ +mutex_unlock (&lvc->lock); +return ; + } + +/* Use pre-allocated bitmap with graceful degradation if too small */ +if (ple_count > lvc->ple_usage_bitmap_capacity) { +dev_warn_once (&lvc->pdev->dev, +"Physical LED count %u exceeds bitmap capacity %u - falling back to full LED scan (performance degraded)\n" , + ple_count, lvc->ple_usage_bitmap_capacity); + ple_used_by_winner =NULL ; +#ifdef CONFIG_DEBUG_FS +atomic64_inc (&lvc->allocation_failures); +#endif + }else { + ple_used_by_winner = lvc->ple_usage_bitmap; +memset (ple_used_by_winner,0 , ple_count *sizeof (bool )); + } + +/* Reset arbitration state */ +list_for_each_entry (ple, &lvc->phys_leds, list) { + ple->chosen_brightness =0 ; + ple->chosen_priority =-1 ; + ple->chosen_sequence =0 ; +#ifdef CONFIG_DEBUG_FS + ple->winner_name[0 ] ='\0' ; +#endif + } + +/* STEP 1: Find the ONE active vLED winner (highest priority, latest sequence) */ +list_for_each_entry (vled, &lvc->leds, list) { +#ifdef CONFIG_DEBUG_FS + vled->arbitration_participations++; +#endif +mutex_lock (&vled->lock); + + brightness = vled->cdev.brightness; + vled_seq = vled->sequence; + +mutex_unlock (&vled->lock); + +/* Only vLEDs with brightness > 0 can become winners */ +if (brightness ==0 ) +continue ; + +/* Winner-takes-all: only ONE vLED controls the controller */ +if (vled->priority > highest_priority) { +/* FIX: Release old winner ref and take new ref */ +if (winner_vled) +virtual_led_put (winner_vled); + winner_vled =virtual_led_get (vled); + highest_priority = vled->priority; + latest_sequence = vled_seq; + }else if (vled->priority == highest_priority) { +s64 seq_diff = (s64)(vled_seq - latest_sequence); + +if (seq_diff >0 ) { +/* FIX: Release old winner ref and take new ref */ +if (winner_vled) +virtual_led_put (winner_vled); + winner_vled =virtual_led_get (vled); + latest_sequence = vled_seq; + } + } + } + +/* STEP 2: If no active vLED, all physical LEDs stay at 0 (already set above) */ +if (!winner_vled) { +#ifdef CONFIG_DEBUG_FS +/* All vLEDs lost (none active) */ +list_for_each_entry (vled, &lvc->leds, list) { +mutex_lock (&vled->lock); +if (vled->cdev.brightness ==0 ) + vled->arbitration_losses++; +mutex_unlock (&vled->lock); + } +#endif +/* Physical LEDs already reset to 0 above */ + goto collect_updates; + } + +/* STEP 3: Apply winner's configuration to all its physical LEDs */ +mutex_lock (&winner_vled->lock); + + intensities = winner_vled->arb_bufs.intensities; + scales = winner_vled->arb_bufs.scales; + +if (!intensities || !scales || + winner_vled->arb_bufs.capacity < winner_vled->num_channels) { +mutex_unlock (&winner_vled->lock); +arb_err (lvc,"vLED '%s': buffer missing or undersized (cap=%u, need=%u)\n" , + winner_vled->name ? winner_vled->name :"(unnamed)" , + winner_vled->arb_bufs.capacity, + winner_vled->num_channels); +#ifdef CONFIG_DEBUG_FS +atomic64_inc (&lvc->allocation_failures); +#endif +/* FIX: Release winner ref AND unlock before returning */ +virtual_led_put (winner_vled); +mutex_unlock (&lvc->lock); +return ; + } + +/* Snapshot winner's channel data */ +for (j =0 ; j < winner_vled->num_channels; j++) { + intensities[j] = winner_vled->channels[j].intensity; + scales[j] = winner_vled->channels[j].scale; + } + + brightness = winner_vled->cdev.brightness; + max_brightness = winner_vled->cdev.max_brightness; + mode = winner_vled->mode; + vled_seq = winner_vled->sequence; + +mutex_unlock (&winner_vled->lock); + +/* Apply winner to all its channels */ +for (j =0 ; j < winner_vled->num_channels; j++) { + final_intensity =vled_channel_get_final_intensity ( + mode, intensities[j], scales[j], brightness, max_brightness + ); + +apply_winner_to_channel (lvc, winner_vled, + winner_vled->channels[j].leds, + winner_vled->channels[j].num_leds, + final_intensity); + +/* Mark physical LEDs as used by winner (only if bitmap available) */ +if (ple_used_by_winner) +mark_winner_leds (lvc, winner_vled, j, ple_used_by_winner, ple_count); + } + +#ifdef CONFIG_DEBUG_FS + winner_vled->arbitration_wins++; + +/* Mark all non-winners as losers */ +list_for_each_entry (vled, &lvc->leds, list) { +if (vled != winner_vled) { +mutex_lock (&vled->lock); +if (vled->cdev.brightness >0 ) + vled->arbitration_losses++; +mutex_unlock (&vled->lock); + } + } +#endif + +/* STEP 4: Turn off physical LEDs not used by winner */ +if (ple_used_by_winner) { +/* Fast path: Use bitmap */ + i =0 ; +list_for_each_entry (ple, &lvc->phys_leds, list) { +if (!ple_used_by_winner[i]) { + ple->chosen_brightness =0 ; + ple->chosen_priority =-1 ; + ple->chosen_sequence =0 ; +#ifdef CONFIG_DEBUG_FS + ple->winner_name[0 ] ='\0' ; +#endif + } + i++; + } + }else { +/* Slow path: Full scan when bitmap unavailable (graceful degradation) */ +list_for_each_entry (ple, &lvc->phys_leds, list) { +bool used_by_winner = false; +unsigned int chan_idx, led_idx; + +/* Check if this physical LED is in winner's channel list */ +for (chan_idx =0 ; chan_idx < winner_vled->num_channels && !used_by_winner; chan_idx++) { +for (led_idx =0 ; led_idx < winner_vled->channels[chan_idx].num_leds; led_idx++) { +if (winner_vled->channels[chan_idx].leds[led_idx] == ple->cdev) { + used_by_winner = true; +break ; + } + } + } + +if (!used_by_winner) { + ple->chosen_brightness =0 ; + ple->chosen_priority =-1 ; + ple->chosen_sequence =0 ; +#ifdef CONFIG_DEBUG_FS + ple->winner_name[0 ] ='\0' ; +#endif + } + } + } + +collect_updates: + +/* Collect LEDs that need updating */ + update_count =0 ; + +list_for_each_entry (ple, &lvc->phys_leds, list) { +if (!ple->cdev) +continue ; + +if (lvc->first_arbitration || ple->cdev->brightness != ple->chosen_brightness) { +if (update_count >= lvc->update_buf.capacity) { +dev_err_ratelimited (&lvc->pdev->dev, +"Update buffer overflow: %u LEDs need update, capacity=%u\n" , + update_count +1 , lvc->update_buf.capacity); +dev_err_ratelimited (&lvc->pdev->dev, +"CRITICAL: Increase max_phys_leds to %u and reload driver!\n" , + lvc->phys_led_count +16 ); +dev_err_ratelimited (&lvc->pdev->dev, +"Skipping remaining %u LEDs - will retry next cycle\n" , + lvc->phys_led_count - update_count); +#ifdef CONFIG_DEBUG_FS +atomic64_inc (&lvc->update_buffer_overflows); +#endif +break ; + } + + local_entries[update_count] =phys_led_entry_get (ple); + local_brightness[update_count] = ple->chosen_brightness; + update_count++; + } + } + + lvc->first_arbitration = false; + +#ifdef CONFIG_DEBUG_FS + lvc->update_count += update_count; + lvc->last_update =ktime_get (); + + arb_end =ktime_get (); + arb_duration_ns =ktime_to_ns (ktime_sub (arb_end, arb_start)); + +if (lvc->arb_latency_count ==0 || arb_duration_ns < lvc->arb_latency_min_ns) + lvc->arb_latency_min_ns = arb_duration_ns; +if (arb_duration_ns > lvc->arb_latency_max_ns) + lvc->arb_latency_max_ns = arb_duration_ns; + +if (lvc->arb_latency_total_ns > (U64_MAX - arb_duration_ns) || + lvc->arb_latency_count >= (U64_MAX -1 )) { + lvc->arb_latency_total_ns = arb_duration_ns; + lvc->arb_latency_count =1 ; + lvc->arb_latency_min_ns = arb_duration_ns; + lvc->arb_latency_max_ns = arb_duration_ns; +dev_info (&lvc->pdev->dev, +"Arbitration latency statistics reset due to overflow\n" ); + }else { + lvc->arb_latency_total_ns += arb_duration_ns; + lvc->arb_latency_count++; + } +#endif + +/* FIX: CRITICAL - Unlock BEFORE hardware I/O */ +mutex_unlock (&lvc->lock); + +/* Hardware I/O outside lock */ +for (i =0 ; i < update_count; i++) { +int pm_ret; + +/* Check removing flag and cleanup properly */ +if (atomic_read (&lvc->removing)) { +/* Release all remaining entries including current one */ +for (; i < update_count; i++) { +if (local_entries[i]) +phys_led_entry_put (local_entries[i]); + } +/* FIX: Release winner ref before returning - lock already released */ +if (winner_vled) +virtual_led_put (winner_vled); +return ; + } + + ple = local_entries[i]; +if (!ple || !ple->cdev) { +phys_led_entry_put (ple); +continue ; + } + +if (ple->dev &&pm_runtime_enabled (ple->dev)) { + pm_ret =pm_runtime_get_sync (ple->dev); +if (pm_ret <0 ) { +dev_err_ratelimited (&lvc->pdev->dev, +"Failed to power LED '%s': %d\n" , + ple->cdev->name, pm_ret); +pm_runtime_put_noidle (ple->dev); +phys_led_entry_put (ple); +continue ; + } + } + +if (ple->cdev->brightness_set_blocking) + ple->cdev->brightness_set_blocking (ple->cdev, local_brightness[i]); +else if (ple->cdev->brightness_set) + ple->cdev->brightness_set (ple->cdev, local_brightness[i]); + +/* +* Ensure the brightness value is visible to other CPUs after the +* hardware update completes. Pairs with smp_load_acquire() in +* led_get_brightness() or similar readers. +*/ +smp_store_release (&ple->cdev->brightness, local_brightness[i]); + +if (ple->dev &&pm_runtime_enabled (ple->dev)) +pm_runtime_put (ple->dev); + +phys_led_entry_put (ple); + } + +if (update_delay_us >0 && update_count >0 ) +usleep_range (update_delay_us, update_delay_us +100 ); + +/* FIX: Release winner ref at end - lock was already released above */ +if (winner_vled) +virtual_led_put (winner_vled); + +/* FIX: Function now returns with lock UNLOCKED as documented */ +} + +static int virtual_led_brightness_set (struct led_classdev *cdev, +enum led_brightness brightness) +{ +struct virtual_led *vled; +struct vcolor_controller *lvc; + +if (!cdev) +return -EINVAL ; + + vled = container_of(cdev, struct virtual_led, cdev); + +if (!vled || !vled->ctrl) +return -ENODEV ; + + lvc = vled->ctrl; + +mutex_lock (&vled->lock); + + vled->cdev.brightness = brightness; + vled->sequence =atomic64_inc_return (&lvc->global_sequence); +#ifdef CONFIG_DEBUG_FS + vled->brightness_set_count++; +#endif +mutex_unlock (&vled->lock); + +/* +* Schedule arbitration based on batching mode. +* +* When batching is enabled, defer arbitration to reduce overhead +* during rapid brightness changes (e.g., software PWM). +* +* When batching is disabled, run arbitration immediately for +* minimal latency. +*/ +if (enable_update_batching) { +atomic_inc (&lvc->pending_updates); +mod_delayed_work (system_wq, &lvc->update_work, +msecs_to_jiffies (UPDATE_BATCH_DELAY_MS )); + }else { +controller_safe_arbitrate (lvc); + } + +return 0 ; +} + +/* +* virtual_led_blink_set - Configure LED blinking +* @cdev: LED class device +* @delay_on: Pointer to ON duration in milliseconds +* @delay_off: Pointer to OFF duration in milliseconds +* +* Attempts to enable hardware blink on all physical LEDs. +* If any physical LED lacks hardware blink support, returns error +* to trigger LED core's automatic software fallback. +* +* Returns: 0 on success, -EINVAL to request software blink +*/ +static int virtual_led_blink_set (struct led_classdev *cdev, +unsigned long *delay_on, +unsigned long *delay_off) +{ +struct virtual_led *vled = container_of(cdev, struct virtual_led, cdev); + +#ifdef CONFIG_DEBUG_FS + vled->blink_requests++; +#endif + +/* +* Always return -EINVAL to request LED core's software blink. +* +* Rationale: Hardware blink on physical LEDs would bypass our +* arbitration system. When multiple vLEDs compete for the same +* physical LED, hardware blink on that LED would continue even +* when a higher-priority vLED takes control. +* +* LED core will handle software blink by calling brightness_set_blocking() +* in a timer, which properly goes through our arbitration. +*/ +return -EINVAL ; +} + +static void deferred_update_worker (struct work_struct *work) +{ +struct vcolor_controller *lvc; +int pending; + + lvc = container_of(work, struct vcolor_controller, update_work.work); + +/* FIXED: Check removal flag BEFORE doing any work */ +if (atomic_read (&lvc->removing)) +return ; + + pending =atomic_xchg (&lvc->pending_updates,0 ); + +if (pending >0 ) { +if (!controller_safe_arbitrate (lvc)) { +/* Only reschedule if not removing */ +if (!atomic_read (&lvc->removing)) { +atomic_set (&lvc->pending_updates, pending); +mod_delayed_work (system_wq, &lvc->update_work, +msecs_to_jiffies (UPDATE_BATCH_DELAY_MS )); + } + } + } +} + +static ssize_t multi_intensity_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct led_classdev *cdev; +struct virtual_led *vled; +int len; +unsigned int i; + + cdev =dev_get_drvdata (dev); +if (!cdev) +return -ENODEV ; + + vled = container_of(cdev, struct virtual_led, cdev); + +mutex_lock (&vled->lock); + + len =0 ; +for (i =0 ; i < vled->num_channels; i++) { +if (i >0 ) + len +=scnprintf (buf + len,PAGE_SIZE - len," " ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"%u" , + vled->channels[i].intensity); + } + len +=scnprintf (buf + len,PAGE_SIZE - len,"\n" ); + +mutex_unlock (&vled->lock); +return len; +} + +static int parse_intensity_values (const char *buf,u8 *values, +unsigned int expected_count) +{ +char *tmp, *cur, *end; +unsigned int count, val; +int ret; +size_t buf_len; + +if (expected_count ==0 ) +return -EINVAL ; + +/* NEW: Prevent truncation by rejecting oversized input */ + buf_len =strnlen (buf,PAGE_SIZE +1 ); +if (buf_len >PAGE_SIZE ) { +pr_warn_once ("Intensity input exceeds PAGE_SIZE (%lu bytes), rejecting\n" , +PAGE_SIZE ); +return -EINVAL ; + } + + tmp =kstrndup (buf,PAGE_SIZE ,GFP_KERNEL ); +if (!tmp) +return -ENOMEM ; + + cur = tmp; + end = tmp +strlen (tmp); + count =0 ; + ret =0 ; + +while (cur < end && count < expected_count) { +while (cur < end && (*cur ==' ' || *cur =='\t' || *cur =='\n' )) + cur++; + +if (cur >= end) +break ; + +if (kstrtouint (cur,0 , &val) || val >255 ) { + ret = -EINVAL ; + goto out; + } + + values[count++] = (u8 )val; + +while (cur < end && *cur !=' ' && *cur !='\t' && *cur !='\n' && *cur !='\0' ) + cur++; + } + +if (count != expected_count) + ret = -EINVAL ; + +out: +kfree (tmp); +return ret; +} + +static ssize_t multi_intensity_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct led_classdev *cdev; +struct virtual_led *vled; +u8 *values; +int ret; +unsigned int i; +struct vcolor_controller *lvc; + + cdev =dev_get_drvdata (dev); +if (!cdev) +return -ENODEV ; + + vled = container_of(cdev, struct virtual_led, cdev); + +/* Check ratelimit early to avoid unnecessary work */ +if (!__ratelimit (&vled->intensity_ratelimit)) { +#ifdef CONFIG_DEBUG_FS +if (vled->ctrl) +atomic64_inc (&vled->ctrl->ratelimit_hits); + vled->ratelimit_drops++; +#endif +return count; + } + +/* Allocate buffer before taking lock */ + values =kcalloc (vled->num_channels,sizeof (*values),GFP_KERNEL ); +if (!values) { +#ifdef CONFIG_DEBUG_FS +vled_err (vled,"Failed to allocate intensity buffer\n" ); + vled->buffer_allocation_failures++; +#endif +return -ENOMEM ; + } + +/* Parse values before taking lock */ + ret =parse_intensity_values (buf, values, vled->num_channels); +if (ret) { +vled_err (vled,"Invalid intensity format\n" ); +#ifdef CONFIG_DEBUG_FS + vled->intensity_parse_errors++; +#endif +kfree (values); +return ret; + } + +/* Now take lock and hold it for the entire operation */ +mutex_lock (&vled->lock); + +/* Check mode while holding lock */ +if (vled->mode ==VLED_MODE_STANDARD ) { +mutex_unlock (&vled->lock);/* Unlock before returning */ +dev_warn_ratelimited (dev, +"Cannot change intensity in standard mode\n" ); +kfree (values); +return -EPERM ; + } + +/* Apply intensity values */ +for (i =0 ; i < vled->num_channels; i++) + vled->channels[i].intensity = values[i]; + +#ifdef CONFIG_DEBUG_FS + vled->intensity_update_count++; +#endif + + lvc = vled->ctrl; +if (lvc) + vled->sequence =atomic64_inc_return (&lvc->global_sequence); + +mutex_unlock (&vled->lock);/* Unlock after all changes */ + +kfree (values); + +if (lvc) { +if (enable_update_batching) { +atomic_inc (&lvc->pending_updates); +mod_delayed_work (system_wq, &lvc->update_work, +msecs_to_jiffies (UPDATE_BATCH_DELAY_MS )); + }else { +controller_safe_arbitrate (lvc); + } + } + +return count; +} +static DEVICE_ATTR_RW (multi_intensity); + +static ssize_t multi_index_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct led_classdev *cdev; +struct virtual_led *vled; +int len; +unsigned int i; + + cdev =dev_get_drvdata (dev); +if (!cdev) +return -ENODEV ; + + vled = container_of(cdev, struct virtual_led, cdev); + +mutex_lock (&vled->lock); + + len =0 ; +for (i =0 ; i < vled->num_channels; i++) { +if (i >0 ) + len +=scnprintf (buf + len,PAGE_SIZE - len," " ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"%u" , + vled->channels[i].color_id); + } + len +=scnprintf (buf + len,PAGE_SIZE - len,"\n" ); + +mutex_unlock (&vled->lock); +return len; +} +static DEVICE_ATTR_RO (multi_index); + +static ssize_t multi_multipliers_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct led_classdev *cdev; +struct virtual_led *vled; +int len; +unsigned int i; + + cdev =dev_get_drvdata (dev); +if (!cdev) +return -ENODEV ; + + vled = container_of(cdev, struct virtual_led, cdev); + +mutex_lock (&vled->lock); + + len =0 ; +for (i =0 ; i < vled->num_channels; i++) { +if (i >0 ) + len +=scnprintf (buf + len,PAGE_SIZE - len," " ); + len +=scnprintf (buf + len,PAGE_SIZE - len,"%u" , + vled->channels[i].scale); + } + len +=scnprintf (buf + len,PAGE_SIZE - len,"\n" ); + +mutex_unlock (&vled->lock); +return len; +} +static DEVICE_ATTR_RO (multi_multipliers); + +static struct attribute *virtual_led_attrs[] = { + &dev_attr_multi_intensity.attr, + &dev_attr_multi_index.attr, + &dev_attr_multi_multipliers.attr, +NULL +}; + +static struct attribute_group virtual_led_attr_group = { + .attrs = virtual_led_attrs, + .name ="mc" , +}; + +static const struct attribute_group *virtual_led_groups[] = { + &virtual_led_attr_group, +NULL +}; + + +/* +* OPTIONAL: Even cleaner alternative using a helper structure +* +* This version is slightly longer but even more maintainable. +*/ + +struct led_transfer_tracker { +struct led_classdev **leds; +struct device **devs; +bool *led_used; +bool *dev_used; +unsigned int count; +}; + +static int led_transfer_tracker_init (struct led_transfer_tracker *tracker, +struct led_classdev **leds, +struct device **devs, +unsigned int count) +{ + tracker->leds = leds; + tracker->devs = devs; + tracker->count = count; + + tracker->led_used =kcalloc (count,sizeof (*tracker->led_used),GFP_KERNEL ); + tracker->dev_used =kcalloc (count,sizeof (*tracker->dev_used),GFP_KERNEL ); + +if (!tracker->led_used || !tracker->dev_used) { +kfree (tracker->led_used); +kfree (tracker->dev_used); + tracker->led_used =NULL ; + tracker->dev_used =NULL ; +return -ENOMEM ; + } + +return 0 ; +} + +static void led_transfer_tracker_mark (struct led_transfer_tracker *tracker, +unsigned int index) +{ +if (index >= tracker->count) +return ; + +if (tracker->led_used) + tracker->led_used[index] = true; + +if (tracker->dev_used && tracker->devs && tracker->devs[index]) + tracker->dev_used[index] = true; +} + +static void led_transfer_tracker_cleanup (struct led_transfer_tracker *tracker) +{ +unsigned int i; + +if (!tracker->led_used || !tracker->dev_used) { +/* Tracking failed, release everything */ +for (i =0 ; i < tracker->count; i++) { +if (tracker->leds && tracker->leds[i]) +led_put (tracker->leds[i]); +if (tracker->devs && tracker->devs[i]) +put_device (tracker->devs[i]); + } + }else { +/* Release only non-transferred items */ +for (i =0 ; i < tracker->count; i++) { +if (tracker->leds && tracker->leds[i] && !tracker->led_used[i]) +led_put (tracker->leds[i]); +if (tracker->devs && tracker->devs[i] && !tracker->dev_used[i]) +put_device (tracker->devs[i]); + } + } + +kfree (tracker->led_used); +kfree (tracker->dev_used); +} + +/* +* ALTERNATIVE VERSION: Using the helper structure +* +* This makes the main function cleaner and easier to understand. +*/ +static int parse_unified_led_list (struct device *dev, +struct fwnode_handle *fwnode, +const char *propname, +struct mc_channel **out_channels, +u8 *out_count) +{ +struct led_classdev **leds =NULL ; +struct device **devs =NULL ; +struct led_transfer_tracker tracker = {0 }; +u8 count, i, j; +struct mc_channel *channels =NULL ; +int ret, color_id; +unsigned int color_counts[LED_COLOR_ID_MAX ] = {0 }; +unsigned int num_channels =0 ; +unsigned int num_channels_allocated =0 ; +unsigned int led_idx; + + ret =parse_leds_fwnode_array (dev, fwnode, propname, + &leds, &devs, &count); +if (ret) +return ret; + +/* Initialize transfer tracker */ + ret =led_transfer_tracker_init (&tracker, leds, devs, count); +if (ret) { +dev_err (dev,"Failed to allocate transfer tracker\n" ); +kfree (leds); +kfree (devs); +return ret; + } + +/* Count LEDs by color */ +for (i =0 ; i < count; i++) { +if (!leds[i]) +continue ; + + color_id = leds[i]->color; + +if (color_id <0 || color_id >=LED_COLOR_ID_MAX ) { +dev_warn (dev,"LED '%s' has invalid color %d, skipping\n" , + leds[i]->name, color_id); +continue ; + } + + color_counts[color_id]++; + } + +/* Count number of unique colors */ +for (i =0 ; i <LED_COLOR_ID_MAX ; i++) { +if (color_counts[i] >0 ) + num_channels++; + } + +if (num_channels >255 ) { +dev_err (dev,"Channel count %u exceeds u8 limit (255)\n" , num_channels); + ret = -EINVAL ; + goto err_cleanup; + } + +if (num_channels ==0 ) { +dev_err (dev,"No valid LEDs found in '%s'\n" , propname); + ret = -ENODEV ; + goto err_cleanup; + } + +/* Allocate channel structures */ + channels =devm_kcalloc (dev, num_channels,sizeof (*channels),GFP_KERNEL ); +if (!channels) { + ret = -ENOMEM ; + goto err_cleanup; + } + +/* Build channels grouped by color */ + num_channels_allocated =0 ; +for (i =0 ; i <LED_COLOR_ID_MAX ; i++) { +if (color_counts[i] ==0 ) +continue ; + + channels[num_channels_allocated].leds =devm_kcalloc (dev, color_counts[i], +sizeof (*channels[num_channels_allocated].leds), +GFP_KERNEL ); + channels[num_channels_allocated].led_devs =devm_kcalloc (dev, color_counts[i], +sizeof (*channels[num_channels_allocated].led_devs), +GFP_KERNEL ); + +if (!channels[num_channels_allocated].leds || + !channels[num_channels_allocated].led_devs) { + ret = -ENOMEM ; + goto err_cleanup; + } + +/* Transfer LEDs to channel */ + led_idx =0 ; +for (j =0 ; j < count; j++) { +if (!leds[j] || leds[j]->color != i) +continue ; + + channels[num_channels_allocated].leds[led_idx] = leds[j]; + channels[num_channels_allocated].led_devs[led_idx] = devs[j]; + +/* Mark as transferred */ +led_transfer_tracker_mark (&tracker, j); + + led_idx++; + } + + channels[num_channels_allocated].color_id = i; + channels[num_channels_allocated].num_leds = color_counts[i]; + channels[num_channels_allocated].intensity =0 ; + channels[num_channels_allocated].scale =255 ; + + num_channels_allocated++; + } + +/* Success - free temporary arrays */ +kfree (leds); +kfree (devs); +kfree (tracker.led_used); +kfree (tracker.dev_used); + + *out_channels = channels; + *out_count = num_channels_allocated; + +return 0 ; + +err_cleanup: +dev_err (dev,"Failed to allocate channel arrays\n" ); + +/* Zero any partially-initialized devm channel arrays */ +if (channels) { +for (i =0 ; i < num_channels_allocated; i++) { +if (channels[i].leds) +memset (channels[i].leds,0 , + channels[i].num_leds *sizeof (*channels[i].leds)); +if (channels[i].led_devs) +memset (channels[i].led_devs,0 , + channels[i].num_leds *sizeof (*channels[i].led_devs)); + } + } + +/* Clean up using helper - handles all the complexity */ +led_transfer_tracker_cleanup (&tracker); + +kfree (leds); +kfree (devs); + +return ret; +} + +static int parse_channel_multipliers (struct device *dev, +const struct fwnode_handle *fwnode, +struct mc_channel *channels, +unsigned int num_channels) +{ +u32 *scales; +int ret, i; + + scales =kcalloc (num_channels,sizeof (*scales),GFP_KERNEL ); +if (!scales) +return -ENOMEM ; + + ret =fwnode_property_read_u32_array (fwnode,"mc-channel-multipliers" , + scales, num_channels); + +if (ret == -EINVAL || ret == -ENODATA ) { +kfree (scales); +return 0 ; + } + +if (ret) { +dev_err (dev,"Failed to read 'mc-channel-multipliers': %d\n" , ret); +kfree (scales); +return ret; + } + +for (i =0 ; i < num_channels; i++) { +if (scales[i] >255 ) { +dev_err (dev,"Invalid scale[%d]=%u (max 255)\n" , i, scales[i]); +kfree (scales); +return -EINVAL ; + } + channels[i].scale = scales[i]; + } + +kfree (scales); +return 0 ; +} + +static int allocate_vled_buffers (struct device *dev,struct virtual_led *vled) +{ + vled->arb_bufs.capacity = vled->num_channels; + + vled->arb_bufs.intensities =devm_kcalloc (dev, vled->num_channels, +sizeof (*vled->arb_bufs.intensities), +GFP_KERNEL ); +if (!vled->arb_bufs.intensities) { + vled->arb_bufs.capacity =0 ; +return -ENOMEM ; + } + + vled->arb_bufs.scales =devm_kcalloc (dev, vled->num_channels, +sizeof (*vled->arb_bufs.scales), +GFP_KERNEL ); +if (!vled->arb_bufs.scales) { + vled->arb_bufs.capacity =0 ; +return -ENOMEM ; + } + +return 0 ; +} + + +static struct virtual_led *virtual_led_init (struct device *dev, +const struct fwnode_handle *child, +struct vcolor_controller *lvc) +{ +struct virtual_led *vled; +const char *mode_str; +const char *function_str =NULL ; +const char *color_name; +const char *default_trigger =NULL ; +u32 color_id =LED_COLOR_ID_WHITE ; +u32 priority_u32; +s32 priority_signed; +int ret; +char *led_name; + + vled =kzalloc (sizeof (*vled),GFP_KERNEL ); +if (!vled) +return ERR_PTR (-ENOMEM ); + +kref_init (&vled->refcount); +mutex_init (&vled->lock); +INIT_LIST_HEAD (&vled->list); + vled->fwnode =fwnode_handle_get ((struct fwnode_handle *)child); + vled->ctrl = lvc; + +/* Parse priority as u32, then validate and convert to s32 */ + priority_u32 =0 ; + ret =fwnode_property_read_u32 (child,"priority" , &priority_u32); +if (ret) { + priority_signed =0 ; + }else { +/* Check if value fits in signed 32-bit range */ +if (priority_u32 > (u32 )INT_MAX ) { +dev_warn (dev,"Priority %u exceeds maximum %d, clamping\n" , + priority_u32,INT_MAX ); + priority_signed =INT_MAX ; + }else { + priority_signed = (s32 )priority_u32; + } + } + + vled->priority = priority_signed; + +/* Parse LED mode */ + vled->mode =VLED_MODE_MULTICOLOR ; + ret =fwnode_property_read_string (child,"led-mode" , &mode_str); +if (ret ==0 ) { +if (strcmp (mode_str,"standard" ) ==0 ) { + vled->mode =VLED_MODE_STANDARD ; + }else if (strcmp (mode_str,"multicolor" ) ==0 ) { + vled->mode =VLED_MODE_MULTICOLOR ; + }else { +dev_err (dev,"Invalid led-mode '%s'\n" , mode_str); + ret = -EINVAL ; + goto err_put_fwnode; + } + } + +/* Parse LED list */ + ret =parse_unified_led_list (dev, (struct fwnode_handle *)child,"leds" , + &vled->channels, &vled->num_channels); +if (ret) { +dev_err (dev,"Failed to parse LED list: %d\n" , ret); + goto err_put_fwnode; + } + +/* Parse channel multipliers */ + ret =parse_channel_multipliers (dev, child, vled->channels, + vled->num_channels); +if (ret) { +dev_err (dev,"Failed to parse channel multipliers: %d\n" , ret); + goto err_put_fwnode; + } + +/* Allocate arbitration buffers */ + ret =allocate_vled_buffers (dev, vled); +if (ret) { +dev_err (dev,"Failed to allocate arbitration buffers: %d\n" , ret); + goto err_put_fwnode; + } + +/* Validate and set max_brightness */ + ret =validate_and_set_max_brightness (vled); +if (ret) { +dev_err (dev,"Failed to validate max_brightness: %d\n" , ret); + goto err_put_fwnode; + } + +/* Parse function and color */ + ret =fwnode_property_read_string (child,"function" , &function_str); +if (ret || !function_str) + function_str ="status" ; + + ret =fwnode_property_read_u32 (child,"color" , &color_id); + color_name =led_get_color_name (color_id); +if (!color_name) { + color_id =LED_COLOR_ID_WHITE ; + color_name ="white" ; + } + + ret =fwnode_property_read_string (child,"linux,default-trigger" , + &default_trigger); +if (ret) + default_trigger =NULL ; + + led_name =kasprintf (GFP_KERNEL ,"%s:%s" , function_str, color_name); +if (!led_name) { + ret = -ENOMEM ; + goto err_put_fwnode; + } + + vled->cdev.name = led_name; + vled->cdev.brightness =0 ; + vled->cdev.brightness_set_blocking = virtual_led_brightness_set; + vled->cdev.blink_set = virtual_led_blink_set; + vled->cdev.groups = virtual_led_groups; + vled->cdev.default_trigger = default_trigger; + +ratelimit_state_init (&vled->intensity_ratelimit, +1 *HZ ,DEFAULT_UPDATE_RATE_LIMIT ); + +dev_info (dev,"vLED '%s': max_brightness=%u, trigger=%s\n" , + led_name, vled->cdev.max_brightness, + default_trigger ? default_trigger :"none" ); + +return vled; + +err_put_fwnode: +fwnode_handle_put (vled->fwnode); +kfree (vled); +return ERR_PTR (ret); +} + +static int virtual_led_register (struct device *dev,struct virtual_led *vled) +{ +struct led_init_data init_data = {}; +int ret; + + init_data.fwnode = vled->fwnode; + init_data.devicename =NULL ; + init_data.default_label =NULL ; + +/* Use explicit registration to match vled kzalloc/kfree lifetime */ + ret =led_classdev_register_ext (dev, &vled->cdev, &init_data); +if (ret) { +dev_err (dev,"LED registration FAILED for '%s': error %d\n" , + vled->cdev.name, ret); +return ret; + } + + vled->cdev_registered = true; +dev_info (dev,"Registered virtual LED '%s'\n" , vled->cdev.name); + +/* Verify LED core assigned name matches */ + vled->name = vled->cdev.name; + +return 0 ; +} + +static void virtual_led_release (struct kref *ref) +{ + struct virtual_led *vled = container_of(ref, struct virtual_led, refcount); + +/* +* Automatically unregister if still registered +* This ensures we never leak LED class devices even during +* abnormal teardown sequences. +*/ +if (vled->cdev_registered) { +pr_warn ("%s: Auto-unregistering LED '%s' during kref cleanup\n" , +DRIVER_NAME , vled->name ? vled->name :"(unknown)" ); +led_classdev_unregister (&vled->cdev); + vled->cdev_registered = false; + } + +/* Actually free the memory since we used kzalloc */ +kfree (vled); +} + +static void virtual_led_destroy (struct virtual_led *vled) +{ +unsigned int i, j; + +if (!vled) +return ; + + vled->cdev_registered = false; + + +if (vled->cdev.name) +kfree ((void *)vled->cdev.name); + +#ifdef CONFIG_DEBUG_FS +debugfs_remove_recursive (vled->debugfs_dir); +#endif + +for (i =0 ; i < vled->num_channels; i++) { +if (vled->channels[i].leds) { +for (j =0 ; j < vled->channels[i].num_leds; j++) { +if (vled->channels[i].leds[j]) { +led_put (vled->channels[i].leds[j]); + vled->channels[i].leds[j] =NULL ; + } + } + } + +if (vled->channels[i].led_devs) { +for (j =0 ; j < vled->channels[i].num_leds; j++) { +if (vled->channels[i].led_devs[j]) { +put_device (vled->channels[i].led_devs[j]); + vled->channels[i].led_devs[j] =NULL ; + } + } + } + } + +fwnode_handle_put (vled->fwnode); +} + +#ifdef CONFIG_DEBUG_FS + +#define SCNPRINTF_FIELD (out, len, size, name, format, value) \ + do { \ + if (len >= size) { \ + break; \ + } \ + len += scnprintf(out + len, size - len, name ": " format "\n", value); \ + } while (0) + +static int debugfs_simple_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos, +int (*format)(void *data,char *out,size_t size)) +{ +char *out; +int len, ret; + + out =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!out) +return -ENOMEM ; + + len =format (file->private_data, out,PAGE_SIZE ); + ret =simple_read_from_buffer (buf, count, ppos, out, len); +kfree (out); + +return ret; +} + +static int format_stats (void *data,char *out,size_t size) +{ +struct vcolor_controller *lvc; +s64 last_update_ms; +u64 arb_latency_avg_ns; +u64 arb_count, update_count, phys_count; +u64 alloc_failures, buf_overflows, ratelimit_hits; +int len; + + lvc = data; + + alloc_failures =atomic64_read (&lvc->allocation_failures); + buf_overflows =atomic64_read (&lvc->update_buffer_overflows); + ratelimit_hits =atomic64_read (&lvc->ratelimit_hits); + +mutex_lock (&lvc->lock); + + last_update_ms =ktime_to_ms (ktime_sub (ktime_get (), lvc->last_update)); + + arb_latency_avg_ns =0 ; +if (lvc->arb_latency_count >0 ) + arb_latency_avg_ns = lvc->arb_latency_total_ns / lvc->arb_latency_count; + + arb_count = lvc->arbitration_count; + update_count = lvc->update_count; + phys_count = lvc->phys_led_count; + +mutex_unlock (&lvc->lock); + + len =0 ; +if (len >= size) +return len; + len +=scnprintf (out + len, size - len," ===Controller Stats===\n" ); +SCNPRINTF_FIELD (out, len, size,"Arbitration cycles" ,"%llu" , arb_count); +SCNPRINTF_FIELD (out, len, size,"LED updates" ,"%llu" , update_count); +SCNPRINTF_FIELD (out, len, size,"Last update" ,"%lld ms ago" , last_update_ms); + +if (len >= size) +return len; + len +=scnprintf (out + len, size - len,"\n===Error Counters===\n" ); +SCNPRINTF_FIELD (out, len, size,"Allocation failures" ,"%llu" , alloc_failures); +SCNPRINTF_FIELD (out, len, size,"Update buffer overflows" ,"%llu" , buf_overflows); +SCNPRINTF_FIELD (out, len, size,"Rate limit hits" ,"%llu" , ratelimit_hits); +SCNPRINTF_FIELD (out, len, size,"Global sequence" ,"%llu" , +atomic64_read (&lvc->global_sequence)); + +if (len >= size) +return len; + len +=scnprintf (out + len, size - len,"\n===Arbitration Latency===\n" ); +SCNPRINTF_FIELD (out, len, size,"Min" ,"%llu ns" , lvc->arb_latency_min_ns); +SCNPRINTF_FIELD (out, len, size,"Max" ,"%llu ns" , lvc->arb_latency_max_ns); +SCNPRINTF_FIELD (out, len, size,"Avg" ,"%llu ns" , arb_latency_avg_ns); +SCNPRINTF_FIELD (out, len, size,"Count" ,"%llu" , lvc->arb_latency_count); + +if (len >= size) +return len; + len +=scnprintf (out + len, size - len,"\n===Configuration===\n" ); +SCNPRINTF_FIELD (out, len, size,"Gamma correction" ,"%s" , + use_gamma_correction ?"enabled" :"disabled" ); +SCNPRINTF_FIELD (out, len, size,"Update batching" ,"%s" , + enable_update_batching ?"enabled" :"disabled" ); +SCNPRINTF_FIELD (out, len, size,"Update delay" ,"%u us" , update_delay_us); +if (len >= size) +return len; + len +=scnprintf (out + len, size - len,"Physical LED count: %llu/%u\n" , + phys_count, lvc->update_buf.capacity); +SCNPRINTF_FIELD (out, len, size,"Removing" ,"%s" , +atomic_read (&lvc->removing) ?"yes" :"no" ); + +return len; +} + +#ifdef CONFIG_DEBUG_FS + +static int format_vled_stats (void *data,char *out,size_t size) +{ +struct vcolor_controller *lvc; +int len; +struct virtual_led *vled; +u64 win_rate; + + lvc = data; + +mutex_lock (&lvc->lock); + + len =0 ; +list_for_each_entry (vled, &lvc->leds, list) { +if (len >= size) +break ; + + win_rate =0 ; +if (vled->arbitration_participations >0 ) { + win_rate =div64_u64 (vled->arbitration_wins *100ULL , + vled->arbitration_participations); +if (win_rate >100 ) + win_rate =100 ; + } + +if (len >= size) +return len; + len +=scnprintf (out + len, size - len, +" LED: %s ===(Mode: %s, Prio: %d)===\n" , + vled->name, + vled->mode ==VLED_MODE_STANDARD ?"standard" :"multicolor" , + vled->priority); +SCNPRINTF_FIELD (out, len, size,"Max brightness" ,"%u" , + vled->cdev.max_brightness); +SCNPRINTF_FIELD (out, len, size,"Default trigger" ,"%s" , + vled->cdev.default_trigger ? vled->cdev.default_trigger :"none" ); +SCNPRINTF_FIELD (out, len, size,"Brightness sets" ,"%llu" , + vled->brightness_set_count); +SCNPRINTF_FIELD (out, len, size,"Intensity sets" ,"%llu" , + vled->intensity_update_count); +SCNPRINTF_FIELD (out, len, size,"Blink requests" ,"%llu" , + vled->blink_requests); +SCNPRINTF_FIELD (out, len, size,"Sequence" ,"%llu" , vled->sequence); +if (len >= size) +break ; + len +=scnprintf (out + len, size - len, +"Current brightness: %u/%u\n" , + vled->cdev.brightness, vled->cdev.max_brightness); +SCNPRINTF_FIELD (out, len, size,"Channels" ,"%u" , vled->num_channels); +SCNPRINTF_FIELD (out, len, size,"Arbitration participations" ,"%llu" , + vled->arbitration_participations); +SCNPRINTF_FIELD (out, len, size,"Arbitration losses" ,"%llu" , + vled->arbitration_losses); +SCNPRINTF_FIELD (out, len, size,"Win rate" ,"%llu%%\n" , win_rate); + +if (len >= size) +return len; + len +=scnprintf (out + len, size - len,"\n===vLED Error Counters===\n" ); +SCNPRINTF_FIELD (out, len, size,"Buffer allocation failures" ,"%llu" , + vled->buffer_allocation_failures); +SCNPRINTF_FIELD (out, len, size,"Intensity parse errors" ,"%llu" , + vled->intensity_parse_errors); +SCNPRINTF_FIELD (out, len, size,"Rate limit drops" ,"%llu\n" , + vled->ratelimit_drops); + } + +mutex_unlock (&lvc->lock); +return len; +} + + +static int format_phys_led_states (void *data,char *out,size_t size) +{ +struct vcolor_controller *lvc; +int len; +struct phys_led_entry *ple; + + lvc = data; + + len =0 ; + len +=scnprintf (out + len, size - len,"===Physical LED States===\n" ); +if (len >= size) +return len; + len +=scnprintf (out + len, size - len, +"Format: [LED] Brightness Priority Seq Winner\n\n" ); + +mutex_lock (&lvc->lock); + +list_for_each_entry (ple, &lvc->phys_leds, list) { +if (len >= size) +break ; +if (!ple->cdev) +continue ; + + len +=scnprintf (out + len, size - len, +"[%s] B:%u P:%d S:%llu W:%s\n" , + ple->cdev->name, + ple->chosen_brightness, + ple->chosen_priority, + ple->chosen_sequence, + ple->winner_name[0 ] ? ple->winner_name :"(none)" ); + } + +mutex_unlock (&lvc->lock); +return len; +} + +static int format_claimed_leds (void *data,char *out,size_t size) +{ +unsigned long count, index; +struct global_phys_owner *gpo; + +down_read (&global_owner_rwsem); + + count =0 ; +xa_for_each (&global_owner_xa, index, gpo) +if (gpo && !xa_is_value (gpo)) + count++; + +up_read (&global_owner_rwsem); + +return scnprintf (out, size,"%lu\n" , count); +} + +#define DEBUGFS_READ_FOP (name, formatter) \ +static ssize_t debugfs_##name##_read(struct file *file, char __user *buf, \ + size_t count, loff_t *ppos) \ + { \ + return debugfs_simple_read(file, buf, count, ppos, formatter); \ +} \ +static const struct file_operations debugfs_##name##_fops = { \ + .owner = THIS_MODULE, \ + .open = simple_open, \ + .read = debugfs_##name##_read, \ + .llseek = default_llseek, \ +} + +DEBUGFS_READ_FOP (stats, format_stats); +DEBUGFS_READ_FOP (vled_stats, format_vled_stats); +DEBUGFS_READ_FOP (phys_led_states, format_phys_led_states); +DEBUGFS_READ_FOP (claimed, format_claimed_leds); + +static ssize_t debugfs_selftest_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos) +{ +struct vcolor_controller *lvc; +char *output; +int len, ret; + + lvc = file->private_data; + +if (!lvc) +return -ENODEV ; + + output =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!output) +return -ENOMEM ; + + len =0 ; + len +=scnprintf (output + len,PAGE_SIZE - len, +"\n===%s Selftest===\n" ,DRIVER_NAME ); + len +=scnprintf (output + len,PAGE_SIZE - len, +"\nChanges in V4:\n" ); + len +=scnprintf (output + len,PAGE_SIZE - len, +"- Conditional debug compilation: IMPLEMENTED\n" ); + len +=scnprintf (output + len,PAGE_SIZE - len, +"- Reduced struct sizes (~200 bytes per LED): IMPLEMENTED\n" ); + len +=scnprintf (output + len,PAGE_SIZE - len, +"- Eliminated debug telemetry overhead: IMPLEMENTED\n" ); + len +=scnprintf (output + len,PAGE_SIZE - len, +"\nResult: PASS - Production ready (optimized)\n" ); + + ret =simple_read_from_buffer (buf, count, ppos, output, len); +kfree (output); + +return ret; +} + +static const struct file_operations debugfs_selftest_fops = { + .owner =THIS_MODULE , + .open = simple_open, + .read = debugfs_selftest_read, + .llseek = default_llseek, +}; + + +static ssize_t debugfs_stress_test_write (struct file *file, +const char __user *buf, +size_t count,loff_t *ppos) +{ +struct vcolor_controller *lvc; +unsigned int iterations, completed, i, j; +u8 random_data[4 ]; +struct virtual_led *vled, **vled_snapshot; +unsigned int vled_count; + + lvc = file->private_data; + +if (!lvc ||atomic_read (&lvc->removing)) +return -ENODEV ; + +if (kstrtouint_from_user (buf, count,0 , &iterations)) +return -EINVAL ; + +if (iterations >10000 ) { +dev_warn (&lvc->pdev->dev,"Clamping stress test to 10000 iterations\n" ); + iterations =10000 ; + } + +if (mutex_lock_interruptible (&lvc->lock)) { +dev_info (&lvc->pdev->dev,"Stress test interrupted by signal\n" ); +return -EINTR ; + } + + vled_count =0 ; +list_for_each_entry (vled, &lvc->leds, list) + vled_count++; + +if (vled_count ==0 ) { +mutex_unlock (&lvc->lock); +dev_info (&lvc->pdev->dev,"No vLEDs available for stress test\n" ); +return count; + } + + vled_snapshot =kcalloc (vled_count,sizeof (*vled_snapshot),GFP_KERNEL ); +if (!vled_snapshot) { +mutex_unlock (&lvc->lock); +dev_err (&lvc->pdev->dev,"Failed to allocate vled snapshot for stress test\n" ); +return -ENOMEM ; + } + + i =0 ; +list_for_each_entry (vled, &lvc->leds, list) { + vled_snapshot[i++] =virtual_led_get (vled); + } + +dev_info (&lvc->pdev->dev,"Starting stress test (%u iterations, %u vLEDs)\n" , + iterations, vled_count); + +/* +* Locking pattern: We hold lvc->lock across arbitration but release it +* between iterations to allow other operations. controller_run_arbitration_and_update() +* expects the lock to be held on entry and maintains that invariant on return. +*/ + completed =0 ; +for (i =0 ; i < iterations; i++) { +/* FIXED: Get random data OUTSIDE lock to avoid blocking */ +mutex_unlock (&lvc->lock); +get_random_bytes (random_data,sizeof (random_data)); + +/* Reacquire lock and check if we should abort */ +mutex_lock (&lvc->lock); +if (atomic_read (&lvc->removing)) +break ; + +for (j =0 ; j < vled_count; j++) { +unsigned int k; +u8 new_brightness; + + vled = vled_snapshot[j]; +if (!vled) +continue ; + + new_brightness = random_data[0 ] % (vled->cdev.max_brightness +1 ); + +mutex_lock (&vled->lock); +for (k =0 ; k < vled->num_channels && k <3 ; k++) + vled->channels[k].intensity = random_data[k +1 ]; + + vled->cdev.brightness = new_brightness; + vled->sequence =atomic64_inc_return (&lvc->global_sequence); +mutex_unlock (&vled->lock); + } + +controller_run_arbitration_and_update (lvc); + completed++; + +mutex_unlock (&lvc->lock); +usleep_range (100 ,200 ); +mutex_lock (&lvc->lock); +cond_resched (); + +if (atomic_read (&lvc->removing)) +break ; + } + +mutex_unlock (&lvc->lock); + +for (i =0 ; i < vled_count; i++) +virtual_led_put (vled_snapshot[i]); +kfree (vled_snapshot); + +dev_info (&lvc->pdev->dev, +"Stress test completed: %u/%u iterations, %llu total arbitrations\n" , + completed, iterations, lvc->arbitration_count); + +return count; +} + +static const struct file_operations debugfs_stress_test_fops = { + .owner =THIS_MODULE , + .open = simple_open, + .write = debugfs_stress_test_write, + .llseek = default_llseek, +}; + +static ssize_t debugfs_rebuild_write (struct file *file, +const char __user *buf, +size_t count,loff_t *ppos) +{ +struct vcolor_controller *lvc; +unsigned int phys_count; + + lvc = file->private_data; + +if (!lvc || lvc->suspended ||atomic_read (&lvc->removing)) +return -EBUSY ; + +if (mutex_lock_interruptible (&lvc->lock)) { +dev_info (&lvc->pdev->dev,"Physical LED rebuild interrupted by signal\n" ); +return -EINTR ; + } + +/* FIX: Return -EBUSY if already rebuilding */ +if (atomic_read (&lvc->rebuilding)) { +mutex_unlock (&lvc->lock); +return -EBUSY ; + } + +if (atomic_read (&lvc->removing)) { +mutex_unlock (&lvc->lock); +return -EBUSY ; + } + +dev_info (&lvc->pdev->dev,"Physical LED rebuild triggered via debugfs\n" ); +controller_rebuild_phys_leds (lvc); + + phys_count = lvc->phys_led_count; +dev_info (&lvc->pdev->dev,"Physical LED rebuild complete: %u LEDs registered\n" , + phys_count); + +/* Lock released by controller_rebuild_phys_leds -> arbitration */ + +return count; +} + +static const struct file_operations debugfs_rebuild_fops = { + .owner =THIS_MODULE , + .open = simple_open, + .write = debugfs_rebuild_write, + .llseek = default_llseek, +}; + +static void controller_setup_debugfs (struct vcolor_controller *lvc) +{ +char debugfs_dirname[64 ]; + +if (!enable_debugfs) +return ; + +snprintf (debugfs_dirname,sizeof (debugfs_dirname),"%s-%s" , +VLED_DEBUGFS_DIR ,dev_name (&lvc->pdev->dev)); + + lvc->debugfs_root =debugfs_create_dir (debugfs_dirname,NULL ); +if (IS_ERR_OR_NULL (lvc->debugfs_root)) { + lvc->debugfs_root =NULL ; +return ; + } + +debugfs_create_file ("stats" ,0444 , lvc->debugfs_root, lvc, + &debugfs_stats_fops); +debugfs_create_file ("vled_stats" ,0444 , lvc->debugfs_root, lvc, + &debugfs_vled_stats_fops); +debugfs_create_file ("phys_led_states" ,0444 , lvc->debugfs_root, lvc, + &debugfs_phys_led_states_fops); +debugfs_create_file ("claimed_leds" ,0444 , lvc->debugfs_root, lvc, + &debugfs_claimed_fops); +debugfs_create_file ("selftest" ,0444 , lvc->debugfs_root, lvc, + &debugfs_selftest_fops); +debugfs_create_file ("stress_test" ,0200 , lvc->debugfs_root, lvc, + &debugfs_stress_test_fops); +debugfs_create_file ("rebuild" ,0200 , lvc->debugfs_root, lvc, + &debugfs_rebuild_fops); +} + +static void controller_destroy_debugfs (struct vcolor_controller *lvc) +{ +debugfs_remove_recursive (lvc->debugfs_root); +} + +#else +static inline void controller_setup_debugfs (struct vcolor_controller *lvc) {} +static inline void controller_destroy_debugfs (struct vcolor_controller *lvc) {} +#endif + +#endif /* CONFIG_DEBUG_FS */ + +static int leds_virtualcolor_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vcolor_controller *lvc; +struct fwnode_handle *child_fwnode; +struct virtual_led *vled; +unsigned int phys_count; +int ret; +int initialized_count =0 ; + + dev = &pdev->dev; + + lvc =devm_kzalloc (dev,sizeof (*lvc),GFP_KERNEL ); +if (!lvc) +return -ENOMEM ; + +INIT_LIST_HEAD (&lvc->leds); +INIT_LIST_HEAD (&lvc->phys_leds); +mutex_init (&lvc->lock); + lvc->pdev = pdev; +xa_init (&lvc->phys_xa); +atomic_set (&lvc->removing,0 ); +atomic_set (&lvc->rebuilding,0 ); + lvc->needs_arbitration = false; +INIT_DELAYED_WORK (&lvc->update_work, deferred_update_worker); +atomic_set (&lvc->pending_updates,0 ); +atomic64_set (&lvc->global_sequence,0 ); + lvc->first_arbitration = true; +#ifdef CONFIG_DEBUG_FS + lvc->last_update =ktime_get (); +atomic64_set (&lvc->allocation_failures,0 ); +atomic64_set (&lvc->update_buffer_overflows,0 ); +atomic64_set (&lvc->ratelimit_hits,0 ); + lvc->arb_latency_min_ns =U64_MAX ; + lvc->arb_latency_max_ns =0 ; + lvc->arb_latency_total_ns =0 ; + lvc->arb_latency_count =0 ; +#endif +dev_set_drvdata (dev, lvc); + + lvc->update_buf.max_capacity = max_phys_leds; + lvc->update_buf.capacity = max_phys_leds; + + lvc->update_buf.entries =devm_kcalloc (dev, max_phys_leds, +sizeof (*lvc->update_buf.entries), +GFP_KERNEL ); + lvc->update_buf.brightness =devm_kcalloc (dev, max_phys_leds, +sizeof (*lvc->update_buf.brightness), +GFP_KERNEL ); +if (!lvc->update_buf.entries || !lvc->update_buf.brightness) { +#ifdef CONFIG_DEBUG_FS +dev_err (dev,"Failed to allocate update buffers (capacity=%u)\n" , + max_phys_leds); +#endif +return -ENOMEM ; + } + +/* Pre-allocate arbitration snapshot buffers */ + lvc->vled_snapshot_capacity =VLED_SNAPSHOT_DEFAULT ; + lvc->vled_snapshot =devm_kcalloc (dev, lvc->vled_snapshot_capacity, +sizeof (*lvc->vled_snapshot),GFP_KERNEL ); +if (!lvc->vled_snapshot) { +#ifdef CONFIG_DEBUG_FS +dev_err (dev,"Failed to allocate vLED snapshot buffer\n" ); +#endif +return -ENOMEM ; + } + + lvc->ple_snapshot_capacity = max_phys_leds; + lvc->ple_snapshot =devm_kcalloc (dev, lvc->ple_snapshot_capacity, +sizeof (*lvc->ple_snapshot),GFP_KERNEL ); +if (!lvc->ple_snapshot) { +#ifdef CONFIG_DEBUG_FS +dev_err (dev,"Failed to allocate PLE snapshot buffer\n" ); +#endif +return -ENOMEM ; + } + + lvc->ple_usage_bitmap_capacity = max_phys_leds; + lvc->ple_usage_bitmap =devm_kcalloc (dev, lvc->ple_usage_bitmap_capacity, +sizeof (*lvc->ple_usage_bitmap),GFP_KERNEL ); +if (!lvc->ple_usage_bitmap) { +#ifdef CONFIG_DEBUG_FS +dev_err (dev,"Failed to allocate PLE usage bitmap\n" ); +#endif +return -ENOMEM ; + } + +controller_setup_debugfs (lvc); + +/* +* PHASE 1: Initialize vLEDs and build internal list +* +* Uses generic fwnode child iteration to maintain the single OF bridge pattern. +* device_for_each_child_node() handles reference counting automatically. +*/ +device_for_each_child_node (dev, child_fwnode) { +/* +* virtual_led_init will call fwnode_handle_get() internally, +* so we pass the fwnode directly +*/ + vled =virtual_led_init (dev, child_fwnode, lvc); +if (IS_ERR (vled)) { + ret =PTR_ERR (vled); +dev_err (dev,"Failed to create LED from device node: %d\n" , ret); + +/* Handle deferred probe specially */ +if (ret == -EPROBE_DEFER ) { +dev_info (dev,"Deferring probe until LEDs are available\n" ); +fwnode_handle_put (child_fwnode); +controller_destroy_debugfs (lvc); +return -EPROBE_DEFER ; + } +/* Loop continues, macro handles fwnode_handle_put */ +continue ; + } + +mutex_lock (&lvc->lock); +list_add_tail (&vled->list, &lvc->leds); +mutex_unlock (&lvc->lock); + + initialized_count++; + } + +if (initialized_count ==0 ) { + ret =dev_err_probe (dev, -ENODEV ,"No valid LED nodes found\n" ); + goto err_cleanup; + } + +/* +* PHASE 2: Build physical LED mappings NOW.
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
--
2.43.0
Part of the diff is not shown here. View the raw message.