diff mbox series

[2/2] hwmon: pwm-fan: Fix RPM calculation

Message ID 20200920180943.352526-5-pbarker@konsulko.com (mailing list archive)
State Changes Requested
Headers show
Series Document multiple fan tach support in pwm-fan driver | expand

Commit Message

Paul Barker Sept. 20, 2020, 6:09 p.m. UTC
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.

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>
---
 drivers/hwmon/pwm-fan.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c
index d7f8c11b4543..2649f6bf1a26 100644
--- a/drivers/hwmon/pwm-fan.c
+++ b/drivers/hwmon/pwm-fan.c
@@ -65,19 +65,18 @@  static void sample_timer(struct timer_list *t)
 {
 	struct pwm_fan_ctx *ctx = from_timer(ctx, t, rpm_timer);
 	ktime_t now = ktime_get();
+	unsigned int delta = ktime_ms_delta(now, ctx->sample_start);
 	int i;
 
 	for (i = 0; i < ctx->tach_count; i++) {
 		struct pwm_fan_tach *tach;
-		int pulses;
-		u64 tmp;
+		unsigned int pulses;
 
 		tach = &ctx->tachs[i];
 		pulses = atomic_read(&tach->pulses);
 		atomic_sub(pulses, &tach->pulses);
-		tmp = (u64)pulses * ktime_ms_delta(now, ctx->sample_start) * 60;
-		do_div(tmp, tach->pulses_per_revolution * 1000);
-		tach->rpm = tmp;
+		tach->rpm = (pulses * 1000 * 60) /
+			(tach->pulses_per_revolution * delta);
 	}
 
 	ctx->sample_start = now;