Use a PWM Channel From a Consumer Driver
Drive a PWM channel from a Linux 6.12 consumer driver with devm_pwm_get, struct pwm_state and pwm_apply_might_sleep, plus the device tree binding.

Pulse-width modulation turns a digital pin into a dial. A signal that is on for 25 percent of each period dims an LED to a quarter of its brightness, sets a servo's angle, or controls a fan's speed. The hardware sits in a PWM controller, and the kernel's PWM subsystem splits the work: a provider driver for the controller, and consumer drivers that request a channel and set its period and duty cycle. This post is about the consumer side on Linux 6.12: how to get a channel, how to describe the signal with struct pwm_state, how to apply it, and how the device tree connects the two. It uses the same platform driver pattern as the LED driver and the GPIO descriptor driver.
The API is documented in the kernel's PWM subsystem documentation.
The device tree side
A PWM channel is named in the consumer's node with a pwms property, and the optional pwm-names gives each one a name. The property holds a phandle to the controller, a channel index and a period in nanoseconds:
pwm0: pwm@7e20c000 {
compatible = "brcm,bcm2835-pwm";
reg = <0x7e20c000 0x28>;
#pwm-cells = <3>;
};
backlight {
compatible = "example,pwm-fan";
pwms = <&pwm0 0 20000 0>;
pwm-names = "fan";
};The three cells after the controller are the channel number (0), the period in nanoseconds (20000, which is a 50 kHz signal) and flags (0 for normal polarity, or PWM_POLARITY_INVERTED). The period in the device tree is the board's choice of the signal frequency, because only the board designer knows what the load tolerates. A fan wants a frequency above the audible range, and a servo wants exactly 20 milliseconds. The consumer driver picks the duty cycle within that period.
Requesting the channel
struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);The con_id matches an entry of pwm-names, or is NULL to take the first one. The returned struct pwm_device is released automatically when the driver unbinds. On failure it returns an error pointer, which includes -EPROBE_DEFER when the controller has not probed yet. Use dev_err_probe() for the report so deferral stays quiet, as in the GPIO post.
Describing the signal with pwm_state
Since Linux 4.19 the whole state of a channel is one structure, and you change it as a unit:
| Field | Meaning |
|---|---|
period | Length of one cycle in nanoseconds |
duty_cycle | How long the output is active within the period, in nanoseconds |
polarity | PWM_POLARITY_NORMAL or PWM_POLARITY_INVERSED |
enabled | Whether the output is running |
The pattern is to read the current state, modify the fields you care about and write it back. This keeps the polarity and period that the device tree set:
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
struct sample_fan {
struct pwm_device *pwm;
};
static int sample_fan_set_percent(struct sample_fan *f, unsigned int percent)
{
struct pwm_state state;
if (percent > 100)
return -EINVAL;
pwm_get_state(f->pwm, &state);
state.duty_cycle = div_u64((u64)state.period * percent, 100);
state.enabled = percent != 0;
return pwm_apply_might_sleep(f->pwm, &state);
}
static int sample_fan_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct sample_fan *f;
struct pwm_state state;
int ret;
f = devm_kzalloc(dev, sizeof(*f), GFP_KERNEL);
if (!f)
return -ENOMEM;
f->pwm = devm_pwm_get(dev, "fan");
if (IS_ERR(f->pwm))
return dev_err_probe(dev, PTR_ERR(f->pwm), "no pwm channel\n");
pwm_init_state(f->pwm, &state);
state.duty_cycle = 0;
state.enabled = false;
ret = pwm_apply_might_sleep(f->pwm, &state);
if (ret)
return ret;
ret = sample_fan_set_percent(f, 25);
if (ret)
return ret;
pwm_get_state(f->pwm, &state);
dev_info(dev, "fan running, period %llu ns, duty %llu ns\n",
state.period, state.duty_cycle);
platform_set_drvdata(pdev, f);
return 0;
}
static const struct of_device_id sample_fan_of_match[] = {
{ .compatible = "example,pwm-fan" },
{ }
};
MODULE_DEVICE_TABLE(of, sample_fan_of_match);
static struct platform_driver sample_fan_driver = {
.probe = sample_fan_probe,
.driver = {
.name = "sample-fan",
.of_match_table = sample_fan_of_match,
},
};
module_platform_driver(sample_fan_driver);
MODULE_DESCRIPTION("PWM consumer example");
MODULE_LICENSE("GPL");With the device tree above, the period is 20000 ns, and a 25 percent duty cycle is 5000 ns, so the log reads:
sample-fan backlight: fan running, period 20000 ns, duty 5000 nsThe arithmetic uses div_u64() and casts to 64 bits first. On a 32-bit machine a plain division of a 64-bit value is a link error, since the kernel does not provide the 64-bit division routine. The cast protects against overflow when the period is large: 20,000,000 ns times 100 exceeds a 32-bit integer.
What each call does
pwm_init_state() loads the state from the device tree defaults: it sets the period and polarity from the pwms property and leaves the duty cycle at zero. A consumer calls it once at probe to learn what the board asked for.
pwm_get_state() returns the last state that was applied, which is the software copy of what the hardware does. It does not read hardware registers, so it never sleeps and is safe anywhere.
pwm_apply_might_sleep() is what applies a state. The name is the point. Since Linux 6.8 the old pwm_apply_state() is named pwm_apply_might_sleep() to say what it always was: the provider driver may sleep, for example if the PWM controller is on an I2C bus, so the call is legal only in process context. A consumer must therefore never call it from an interrupt handler, a timer or while holding a spinlock. When a change has to follow an event in atomic context, record the wanted value and queue a work item that applies it.
An unusual value is rejected, not silently approximated. If the period is outside what the controller can generate, pwm_apply_might_sleep() returns an error such as -EINVAL. If it can generate something close, the provider rounds to the nearest value it can do, and the state you read back may differ from what you set.
The old and the new API
Older drivers use pwm_config(pwm, duty_ns, period_ns) followed by pwm_enable(pwm), and pwm_disable(). They are the "legacy" calls and still work in 6.12, wrapped over the state interface, but they set the period every time and make it easy to lose the polarity. New drivers should use pwm_state. The legacy form also separates the duty-cycle update from the enable, so there is a moment where the output is on with the old duty, which is a visible blink on an LED. A single pwm_apply_* call changes everything at once.
Seeing it from user space
The PWM subsystem also exposes channels in sysfs for testing. A channel is exported from its controller's directory, and a consumer that already owns it blocks the export:
echo 0 | sudo tee /sys/class/pwm/pwmchip0/export
echo 20000 | sudo tee /sys/class/pwm/pwmchip0/pwm0/period
echo 5000 | sudo tee /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 | sudo tee /sys/class/pwm/pwmchip0/pwm0/enableA channel in use by a kernel driver cannot be exported, and the write fails with Device or resource busy. The sysfs interface is for bring-up. A product's control path belongs in a driver or the existing consumer drivers, such as pwm-leds, pwm-backlight and pwm-fan, which are configured entirely from the device tree and often remove the need to write any consumer code.
Picking the period and the duty cycle
The period decides what the load sees as a smooth signal. An LED is fine at a few hundred hertz, since the eye averages it, but a period of 10 ms flickers visibly when the camera or a moving eye samples it. A motor or a fan prefers a frequency above hearing, which is why the example uses 20 microseconds, and a servo expects exactly one pulse every 20 ms with a width between 1 and 2 ms, so there the duty cycle carries the information and the period is fixed by the protocol. Put the period in the device tree, where the board designer can set it, and let the driver change only the duty cycle. A driver that hardcodes both works on one board and misbehaves on the next.


