From: "Uwe Kleine-König" <ukleinek@kernel.org>
To: Stephane Lepain <stephanelepain@gmail.com>
Cc: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>,
linux-pwm@vger.kernel.org, Kenneth Kasilag <kenneth@kasilag.me>,
George Moussalem <george.moussalem@outlook.com>,
Devi Priya <quic_devipriy@quicinc.com>,
Baruch Siach <baruch.siach@siklu.com>,
linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] pwm: ipq: fix period calculation
Date: Tue, 20 Oct 2026 15:12:29 +0200 [thread overview]
Message-ID: <atdmO41XhQhbas5x@monoceros> (raw)
In-Reply-To: <20260803044748.27116-1-stephanelepain@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 9622 bytes --]
Hello,
On Mon, Aug 03, 2026 at 06:47:48AM +0200, Stephane Lepain wrote:
> From: Kenneth Kasilag <kenneth@kasilag.me>
>
> ipq_pwm_apply() fixes pwm_div at its maximum and derives only pre_div
> from the requested period. Since the period spans
> (pre_div + 1) * (pwm_div + 1) input clocks, pinning pwm_div near its
> maximum forces pre_div towards zero for short periods: once pre_div
> rounds to 0 the shortest representable period is (pwm_div + 1) / clk_rate,
> and any shorter request is rejected outright:
>
> pre_div = mul_u64_u64_div_u64(period_ns, ipq_chip->clk_rate,
> (u64)NSEC_PER_SEC * (pwm_div + 1));
> if (!pre_div)
> return -ERANGE;
>
> Four-wire fans commonly expect a ~25 kHz PWM, which is therefore
> unusable. On an IPQ6018 with the PWM block clocked at 100 MHz, a
> 40,000 ns (25 kHz) request computes floor(0.061) == 0 and returns
> -ERANGE deterministically. Where a request is not rejected outright, the
> high duration truncates to 0 and the output collapses to ~0% duty.
>
> Search for the (pre_div, pwm_div) pair whose period best approximates
"best" is very subjective. The original author valued fine grained duty
selection over the higher period range.
> the request instead of fixing pwm_div. Starting pre_div at the smallest
> value that keeps pwm_div within its field and stopping once pre_div
> exceeds pwm_div bounds the loop and keeps pwm_div as large as possible
> for fine duty resolution. For a 25 kHz request at 100 MHz this selects
> pre_div = 0, pwm_div = 3999, i.e. exactly 4000 clocks, with full 0..4000
> duty resolution.
>
> While reworking the high-duration computation, round it to nearest
> rather than truncating,
round to nearest is wrong. The official promise of the core to its
consumers is that all values are round down. There are offenders, but
I'm not willing to increase their number.
> so mid-range duty cycles are not biased low, and
> clamp it to pwm_div + 1. Rounding, or a 100% duty request, could
> otherwise push hi_dur past the period length and overflow the 16-bit
> HI_DURATION field.
That's something that the driver has to catch if this is an issue for
the hardware.
> This was first fixed downstream in OpenWrt for the qualcommbe target
> after testing on the Askey SBE1V1K, and has since been applied to
> OpenWrt's qualcommax target as well.
>
> Tested on a GL.iNet GL-AXT1800 (IPQ6018, 100 MHz PWM clock) whose DTS
> requests a 25 kHz period for its four-wire fan:
>
> pwms = <&pwm 1 40000 0>;
>
> Before, pwm-fan failed to probe on every boot:
>
> pwm-fan pwm-fan: failed to enable PWM
> pwm-fan pwm-fan: Failed to configure PWM: -34
> pwm-fan pwm-fan: probe with driver pwm-fan failed with error -34
>
> The same failure is reproducible without pwm-fan, straight from sysfs:
>
> # echo 40000 > period; echo 1 > enable -> write error (-ERANGE)
> # echo 2700000 > period; echo 1 > enable -> succeeds
>
> Because probe returns before the tachometer IRQ is requested and before
> fan-supply is claimed, the board also lost fan RPM reporting and its
> vcc_fan regulator stayed disabled, leaving the DTS cooling-maps with no
> cooling device to bind to.
The waveform API is more flexible, so the right way forward includes
converting the ipq driver to that.
> After, pwm-fan probes cleanly and the fan is confirmed spinning by its
> own tachometer:
>
> /sys/class/hwmon/hwmon7/name = pwmfan
> /sys/devices/platform/pwm-fan/hwmon/hwmon7/fan1_input = 3548
> /sys/class/regulator/regulator.3 (vcc_fan) = enabled
> /sys/class/thermal/cooling_device1 = pwm-fan
>
> with idle SoC temperature dropping from ~76 °C to ~51 °C.
>
> Fixes: c436e3e9c265 ("pwm: Driver for qualcomm ipq6018 pwm block")
I wouldn't call it a fix. The driver behaves as expected (by the core),
so what you're doing here is not a fix.
> Signed-off-by: Kenneth Kasilag <kenneth@kasilag.me>
> [Stephane: dropped the explanatory comment, the unreachable
> clk_rate > 16 GHz bound and the unrelated hi_div cast, per review]
> Tested-by: Stephane Lepain <stephanelepain@gmail.com>
> Signed-off-by: Stephane Lepain <stephanelepain@gmail.com>
> ---
> Changes in v3:
> - Resend: v2 accidentally carried a stray copy of the whole driver at
> t/drivers/pwm/pwm-ipq.c (a leftover verification directory that got
> swept into the commit). Only drivers/pwm/pwm-ipq.c is touched here.
> - No functional change from v2.
>
> Changes in v2 (per Konrad Dybcio's review):
> - Dropped the explanatory comment above the divider search; the rationale
> lives in the commit message.
> - Dropped the "clk_rate > 16 * GIGA" bound. period_ns is already clamped
> to IPQ_PWM_MAX_PERIOD_NS (1e9), so period_ns * clk_rate only overflows
> u64 above ~18.4 GHz, i.e. ~184x the 100 MHz this block runs at; the
> check was unreachable.
> - Dropped the "(u64)hi_div" cast as unrelated. It is not a live bug
> either: hi_dur and pre_div both come from 16-bit fields, so the worst
> case 65535 * 65536 = 4294901760 still fits u32 (effective_div on the
> line above genuinely does need the cast, at 65536 * 65536).
> - Behaviour is unchanged: for 40,000 ns at 100 MHz the search still
> selects pre_div = 0, pwm_div = 3999.
>
> drivers/pwm/pwm-ipq.c | 91 ++++++++++++++++++++++++++++++-------------
> 1 file changed, 65 insertions(+), 26 deletions(-)
>
> diff --git a/drivers/pwm/pwm-ipq.c b/drivers/pwm/pwm-ipq.c
> index c533739..797559d 100644
> --- a/drivers/pwm/pwm-ipq.c
> +++ b/drivers/pwm/pwm-ipq.c
> @@ -89,10 +89,10 @@ static int ipq_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> const struct pwm_state *state)
> {
> struct ipq_pwm_chip *ipq_chip = ipq_pwm_from_chip(chip);
> - unsigned int pre_div, pwm_div;
> - u64 period_ns, duty_ns;
> + unsigned int pre_div, pwm_div, best_pre_div, best_pwm_div;
> + u64 period_ns, duty_ns, period_rate, min_diff;
> unsigned long val = 0;
> - unsigned long hi_dur;
> + u64 hi_dur;
>
> if (!state->enabled) {
> /* clear IPQ_PWM_REG1_ENABLE */
> @@ -112,35 +112,74 @@ static int ipq_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> period_ns = min(state->period, IPQ_PWM_MAX_PERIOD_NS);
> duty_ns = min(state->duty_cycle, period_ns);
>
> - /*
> - * Pick the maximal value for PWM_DIV that still allows a
> - * 100% relative duty cycle. This allows a fine grained
> - * selection of duty cycles.
> - */
> - pwm_div = IPQ_PWM_MAX_DIV - 1;
> + period_rate = period_ns * ipq_chip->clk_rate;
> +
> + best_pre_div = IPQ_PWM_MAX_DIV;
> + best_pwm_div = IPQ_PWM_MAX_DIV;
> + min_diff = period_rate;
>
> /*
> - * although mul_u64_u64_div_u64 returns a u64, in practice it
> - * won't overflow due to above constraints. Take the max period
> - * of 10^9 (NSEC_PER_SEC) and the pwm_div + 1 (IPQ_PWM_MAX_DIV)
> - * 10^9 * 10^8
> - * ------------- => which fits well into a 32-bit unsigned int.
> - * 10^9 * 65,535
> + * Smaller pre_div than this cannot represent the period (pwm_div would
> + * have to exceed its field), so start the search there.
> */
> - pre_div = mul_u64_u64_div_u64(period_ns, ipq_chip->clk_rate,
> - (u64)NSEC_PER_SEC * (pwm_div + 1));
> -
> - if (!pre_div)
> - return -ERANGE;
> + pre_div = div64_u64(period_rate,
> + (u64)NSEC_PER_SEC * (IPQ_PWM_MAX_DIV + 1));
> +
> + for (; pre_div <= IPQ_PWM_MAX_DIV; pre_div++) {
> + u64 remainder;
> +
> + pwm_div = div64_u64_rem(period_rate,
> + (u64)NSEC_PER_SEC * (pre_div + 1),
> + &remainder);
> + /* pwm_div is unsigned; the swap check below catches underflow */
> + pwm_div--;
> +
> + /*
> + * Swapping pre_div and pwm_div yields the same period but a
> + * larger pwm_div gives finer duty resolution, so once pre_div
> + * exceeds pwm_div every further candidate is strictly worse.
> + */
> + if (pre_div > pwm_div)
> + break;
> +
> + /* need room for 100% duty, where hi_dur == pwm_div + 1 */
> + if (pwm_div > IPQ_PWM_MAX_DIV - 1)
> + continue;
> +
> + if (remainder < min_diff) {
> + best_pre_div = pre_div;
> + best_pwm_div = pwm_div;
> + min_diff = remainder;
> +
> + if (min_diff == 0)
> + break;
> + }
> + }
I didn't try to do the maths here, but usually you can determine the
needed values without a loop.
> - pre_div -= 1;
> + pre_div = best_pre_div;
> + pwm_div = best_pwm_div;
>
> - if (pre_div > IPQ_PWM_MAX_DIV)
> - pre_div = IPQ_PWM_MAX_DIV;
> + /*
> + * If the search found no usable candidate, best_pwm_div is left at
> + * IPQ_PWM_MAX_DIV; cap it so pwm_div + 1 still fits the 16-bit field
> + * and 100% duty remains expressible.
> + */
> + if (pwm_div > IPQ_PWM_MAX_DIV - 1)
> + pwm_div = IPQ_PWM_MAX_DIV - 1;
>
> - /* pwm duty = HI_DUR * (PRE_DIV + 1) / clk_rate */
> - hi_dur = mul_u64_u64_div_u64(duty_ns, ipq_chip->clk_rate,
> - (u64)NSEC_PER_SEC * (pre_div + 1));
> + /*
> + * high duration = duty_ratio * (pwm_div + 1)
> + * = duty_ns * clk_rate / ((pre_div + 1) * NSEC_PER_SEC)
^
missing space here -' to align the =.
> + *
> + * Round to nearest to avoid biasing every duty cycle low, then clamp
> + * to (pwm_div + 1): rounding or a 100% request can otherwise push
> + * hi_dur past the period, overflowing the 16-bit HI_DURATION field
> + * and asking the hardware to stay high beyond one period.
> + */
Best regards
Uwe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
prev parent reply other threads:[~2026-10-20 13:12 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260731070427.155221-1-stephanelepain@gmail.com>
2026-08-02 19:05 ` [PATCH v2] " Stephane Lepain
2026-08-03 4:47 ` [PATCH v3] " Stephane Lepain
2026-10-20 13:12 ` Uwe Kleine-König [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=atdmO41XhQhbas5x@monoceros \
--to=ukleinek@kernel.org \
--cc=baruch.siach@siklu.com \
--cc=george.moussalem@outlook.com \
--cc=kenneth@kasilag.me \
--cc=konrad.dybcio@oss.qualcomm.com \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=quic_devipriy@quicinc.com \
--cc=stephanelepain@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®