]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/commitdiff
hwmon: (pwm-fan) Fix RPM calculation
authorPaul Barker <pbarker@konsulko.com>
Wed, 11 Nov 2020 16:46:43 +0000 (16:46 +0000)
committerGuenter Roeck <linux@roeck-us.net>
Thu, 12 Nov 2020 15:00:54 +0000 (07:00 -0800)
To convert the number of pulses counted into an RPM estimation, we need
to divide by the width of our measurement interval instead of
multiplying by it. If the width of the measurement interval is zero we
don't update the RPM value to avoid dividing by zero.

We also don't need to do 64-bit division, with 32-bits we can handle a
fan running at over 4 million RPM.

Signed-off-by: Paul Barker <pbarker@konsulko.com>
Link: https://lore.kernel.org/r/20201111164643.7087-1-pbarker@konsulko.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/pwm-fan.c

index bdba2143021a6107bdebc738f9d02c1407a38bbe..1f63807c0399eb44b4e88aaebb0d4930e2e0b69f 100644 (file)
@@ -54,16 +54,18 @@ static irqreturn_t pulse_handler(int irq, void *dev_id)
 static void sample_timer(struct timer_list *t)
 {
        struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
+       unsigned int delta = ktime_ms_delta(ktime_get(), ctx->sample_start);
        int pulses;
-       u64 tmp;
 
-       pulses = atomic_read(&ctx->pulses);
-       atomic_sub(pulses, &ctx->pulses);
-       tmp = (u64)pulses * ktime_ms_delta(ktime_get(), ctx->sample_start) * 60;
-       do_div(tmp, ctx->pulses_per_revolution * 1000);
-       ctx->rpm = tmp;
+       if (delta) {
+               pulses = atomic_read(&ctx->pulses);
+               atomic_sub(pulses, &ctx->pulses);
+               ctx->rpm = (unsigned int)(pulses * 1000 * 60) /
+                       (ctx->pulses_per_revolution * delta);
+
+               ctx->sample_start = ktime_get();
+       }
 
-       ctx->sample_start = ktime_get();
        mod_timer(&ctx->rpm_timer, jiffies + HZ);
 }