pwm_set_wave..._might_sleep(9)
π
pwm_set_waveform_might_sleep β from the Kernel API Manual (that's the pwm_set_wave..._might_sleep(9) page, bestie) β¨
π NAME π
pwm_set_waveform_might_sleep β apply a new waveform. Cannot be used in atomic context, no cap.
π¦ SYNOPSIS π¦
int pwm_set_waveform_might_sleep (struct pwm_device *pwm , const struct
pwm_waveform *wf , bool exact );
πΈ ARGUMENTS πΈ
pwmβ your PWM device, the main character here πwfβ the waveform to applyexactβ iftrue, no rounding is allowed. Like, zero. none. periodt.
β¨ DESCRIPTION β¨
okay so here's the tea: typically a requested waveform cannot be implemented exactly. why? because hardware is a diva π for example, say you asked for .period_length_ns = 100 ns, but the hardware can only set periods that are a multiple of 8.5 ns. with that hardware, passing exact = true results in pwm_set_waveform_might_sleep failing and returning -EDOM.
if exact = false you get a period of 93.5 ns β that's the biggest period not bigger than the requested value, slay π
β οΈ heads up bestie: even withexact = true, some rounding by less than1ns is possible/needed. like, it's giving "close enough" energy. in the above example, requesting.period_length_ns = 94andexact = true, you get the hardware configured with period =93.5ns.
Let C be the set of possible hardware configurations for a given PWM device, consisting of tuples (p, d, o) where p is the period length, d is the duty length and o the duty offset.
The following algorithm is implemented to pick the hardware setting (p, d, o) β C for a given request (p', d', o') with exact = false:
p = max( { αΉ | β αΈ, Θ― : (αΉ, αΈ, Θ―) β C β§ αΉ β€ p' } βͺ { min({ αΉ | β αΈ, Θ― : (αΉ, αΈ, Θ―) β C }) })
d = max( { αΈ | β Θ― : (p, αΈ, Θ―) β C β§ αΈ β€ d' } βͺ { min({ αΈ | β Θ― : (p, αΈ, Θ―) β C }) })
o = max( { Θ― | (p, d, Θ―) β C β§ Θ― β€ o' } βͺ { min({ Θ― | (p, d, Θ―) β C }) })
in words (for the girlies who don't speak math) πβ‘οΈπ
The chosen period length is the maximal possible period length not bigger than the requested period length, and if that doesn't exist, the minimal period length. the chosen duty length is the maximal possible duty length that is compatible with the chosen period length and isn't bigger than the requested duty length. again, if such a value doesn't exist, the minimal duty length compatible with the chosen period is picked. after that the duty offset compatible with the chosen period and duty length is chosen in the same way. iconic, honestly π
π₯ RETURN π₯
0on success π«Ά-EDOMif setting failed due to the exact waveform not being possible (ifexact)- or a different negative
errnoon failure π
β CONTEXT β
May sleep. literally it's allowed to nap π΄ (so, again: not atomic context, ok?)
π SEE ALSO π
Kernel file ./drivers/pwm/core.c, plus the whole friend group:
pwm_round_waveform_might_sleep(9), pwm_get_waveform_might_sleep(9), pwm_apply_might_sleep(9), pwm_apply_atomic(9), pwm_get_state_hw(9), pwm_adjust_config(9), pwm_capture(9), pwm_request_from_chip(9), of_pwm_get(9), acpi_pwm_get(9), pwm_get(9), pwm_put(9), devm_pwm_get(9), devm_fwnode_pwm_get(9), __pwmchip_add(9), pwmchip_remove(9), pwm_add_table(9), pwm_remove_table(9)
that's the whole page, bestie π§ this one lives in ./drivers/pwm, dated October 2026 β mwah, go configure some waveforms responsibly π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms