]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
pwm: stm32: Fix rounding issue for requests with inverted polarity
authorUwe Kleine-König <u.kleine-koenig@baylibre.com>
Wed, 15 Apr 2026 14:50:12 +0000 (16:50 +0200)
committerUwe Kleine-König <ukleinek@kernel.org>
Thu, 16 Apr 2026 05:25:15 +0000 (07:25 +0200)
commit5d087c485b6ecf200a9ebb2a032bf8571d330250
tree9f5588f6f5f4a6d0a75bcb9b24e68105bcc2e0a5
parentaa8f35172ab66c57d4355a8c4e28d05b44c938e3
pwm: stm32: Fix rounding issue for requests with inverted polarity

The calculation of the number of pwm clk ticks from a time length in
nanoseconds involves a division and thus some rounding. That might
result in

duty_ticks + offset_ticks < period_ticks

despite

duty_length_ns + duty_offset_ns >= period_length_ns

. The stm32 PWM cannot configure offset_ticks freely, it can only select
0 or period_length_ns - duty_length_ns---that is the classic normal and
inverted polarity. The decision to select the hardware polarity must be
done using the ticks values and not the nanoseconds times to adhere to
the rounding rules by the pwm core.

With the pwm clk running at 208900 kHz on my test machine
(stm32mp135f-dk), a test case that was handled wrong is:

# pwmround -P 9999962 -O 24970 -D 9974992
period_length = 9999962
duty_length = 9974840
duty_offset = 25123

With this change applied the rounding is done correctly:

# pwmround -P 9999962 -O 24970 -D 9974992
period_length = 9999962
duty_length = 9974840
duty_offset = 0

Fixes: deaba9cff809 ("pwm: stm32: Implementation of the waveform callbacks")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Link: https://patch.msgid.link/c5e7767cee821b5f6e00f95bd14a5e13015646fb.1776264104.git.u.kleine-koenig@baylibre.com
Signed-off-by: Uwe Kleine-König <ukleinek@kernel.org>
drivers/pwm/pwm-stm32.c