Write a Linux LED Class Driver With Brightness Control
Write a Linux 6.12 LED class driver with led_classdev, brightness_set_blocking, max_brightness and triggers, and see it appear under /sys/class/leds.

Status lights are the simplest hardware a driver can control, and Linux has a subsystem for them: the LED class. A driver that registers a led_classdev gets a directory under /sys/class/leds/ with a brightness file, a list of triggers that blink or follow other system events, and device tree properties that set the default state, without writing any of that code. This post writes a driver on Linux 6.12 for an LED whose brightness is set through a memory-mapped register, explains the two ways to set brightness and why the choice matters, and shows the sysfs interface that results. It builds on the introduction to drivers.
The subsystem is described in the kernel's LED class documentation.
Why the LED class and not a custom interface
A driver could expose a character device and let user space write a number. The LED class is better because every part of the system already knows it. The kernel's own LED triggers, such as heartbeat, disk activity and network traffic, attach to any registered LED. Device trees can say linux,default-trigger = "heartbeat" and the right thing happens at boot. Tools that show status on a board, and web interfaces on routers, write the same standard files. The cost to the driver is one structure and one callback.
The structure and the two callbacks
struct led_classdev holds the name, the brightness range and the function that applies a value:
| Field | Purpose |
|---|---|
name | The directory name under /sys/class/leds/ |
max_brightness | The highest value the hardware accepts, 255 by default |
brightness_set | Sets brightness, must not sleep |
brightness_set_blocking | Sets brightness, may sleep, returns an error code |
default_trigger | The trigger to attach at registration |
flags | Behavior flags such as LED_CORE_SUSPENDRESUME |
Provide exactly one of the two setters. The choice depends on the hardware:
brightness_setis for hardware that can be programmed from any context, such as a memory-mapped register. It returns nothing and must not sleep, because the kernel may call it from an interrupt or a timer, for example when a trigger blinks the LED.brightness_set_blockingis for hardware behind a bus that sleeps, such as an LED controller on I2C. It returns anint, may sleep, and the core calls it from a workqueue. A setter that can sleep placed inbrightness_setcauses a warning and a possible deadlock.
If you register brightness_set_blocking, the core takes care of deferring the call to process context. If you register brightness_set the call can happen anywhere, so the register write must be atomic. Using the blocking form for a memory-mapped LED is correct but slower, since every update goes through a work item. The non-blocking one is the right match for registers.
The driver
This driver controls an LED through a duty-cycle register at offset 0 of a memory region, taken from the device tree:
#include <linux/io.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#define REG_DUTY 0x00
struct sample_led {
struct led_classdev cdev;
void __iomem *regs;
};
static void sample_led_set(struct led_classdev *cdev,
enum led_brightness value)
{
struct sample_led *led = container_of(cdev, struct sample_led, cdev);
iowrite32(value, led->regs + REG_DUTY);
}
static enum led_brightness sample_led_get(struct led_classdev *cdev)
{
struct sample_led *led = container_of(cdev, struct sample_led, cdev);
return ioread32(led->regs + REG_DUTY);
}
static int sample_led_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct sample_led *led;
led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
if (!led)
return -ENOMEM;
led->regs = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(led->regs))
return PTR_ERR(led->regs);
led->cdev.name = "sample:green:status";
led->cdev.max_brightness = 255;
led->cdev.brightness_set = sample_led_set;
led->cdev.brightness_get = sample_led_get;
led->cdev.default_trigger = "heartbeat";
return devm_led_classdev_register(dev, &led->cdev);
}
static const struct of_device_id sample_led_of_match[] = {
{ .compatible = "example,duty-led" },
{ }
};
MODULE_DEVICE_TABLE(of, sample_led_of_match);
static struct platform_driver sample_led_driver = {
.probe = sample_led_probe,
.driver = {
.name = "sample-led",
.of_match_table = sample_led_of_match,
},
};
module_platform_driver(sample_led_driver);
MODULE_DESCRIPTION("Register-backed LED class driver");
MODULE_LICENSE("GPL");devm_platform_ioremap_resource() maps the first memory resource of the device and claims it, and devm_led_classdev_register() registers the LED and removes it automatically on unbind. The driver needs no remove function.
The name follows the convention devicename:color:function, here sample:green:status. Names are how user space finds an LED, so the three parts matter: a board with several LEDs needs unique names, and tools match on the color and function fields. Newer drivers fill these from the device tree instead of hardcoding a string, using led_init_data and devm_led_classdev_register_ext(), which reads the color and function properties. The hardcoded name keeps this example short.
brightness_get is optional. If you provide it, reading the brightness file asks the hardware, which is correct for LEDs that can be changed by something other than Linux. Without it, the core returns the last value written.
The sysfs interface
Once the driver binds, the LED has a directory:
ls /sys/class/leds/sample:green:status
cat /sys/class/leds/sample:green:status/max_brightnessbrightness device max_brightness power subsystem trigger uevent
255Set a level by writing to brightness, and read it back:
echo 64 | sudo tee /sys/class/leds/sample:green:status/brightness
cat /sys/class/leds/sample:green:status/brightness64
64Writing a value above max_brightness is clamped to it. Writing to brightness while a trigger such as heartbeat is active stops the trigger's effect for the moment and applies your value. Writing 0 turns the LED off and, for most triggers, leaves it attached.
The trigger file lists the available triggers and marks the active one:
cat /sys/class/leds/sample:green:status/triggernone rfkill-any rfkill-none kbd-scrolllock kbd-numlock timer oneshot disk-activity [heartbeat] default-on mtd nand-disk cpu0The brackets mark the current trigger, and here it is heartbeat because the driver set default_trigger = "heartbeat" and that trigger is built in. To change it, write the name:
echo timer | sudo tee /sys/class/leds/sample:green:status/trigger
echo 200 | sudo tee /sys/class/leds/sample:green:status/delay_on
echo 800 | sudo tee /sys/class/leds/sample:green:status/delay_offThe timer trigger creates the delay_on and delay_off files, in milliseconds, and blinks the LED by calling your brightness_set repeatedly, with max_brightness and 0. This is the case where the non-sleeping setter matters: the trigger uses a kernel timer, which runs in interrupt context.
Hardware blinking
Some LED controllers can blink on their own, which saves the CPU and keeps blinking during suspend. A driver for such hardware implements blink_set, a callback that receives the on and off times and returns zero if the hardware took over. The timer trigger tries blink_set first and falls back to the software timer if it returns an error. The driver sets the delays through the arguments, and a value of zero in both means "pick a default", which the callback must replace with a real period.
Device tree description
The usual way to bring up the driver on a board is a node with the register range, plus the generic LED properties:
led@10000000 {
compatible = "example,duty-led";
reg = <0x10000000 0x100>;
linux,default-trigger = "heartbeat";
};If the driver sets default_trigger in code and the device tree sets it too, the device tree wins, since it is applied after registration. That lets a board override what the driver chose, which is the right layering: the driver proposes, the board decides.
When the brightness does nothing
If writes succeed but the LED does not change, check three things in order. First, that the right register is being written: log the value inside the callback while debugging. Second, that a trigger is not overwriting your value, by writing none to trigger. Third, that the hardware's own control is enabled, since some LED blocks have an enable bit in a separate register that the driver must set at probe.


