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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

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;
-- 
2.28.0




[Index of Archives]     [LM Sensors]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux