Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp833074pxu; Mon, 23 Nov 2020 05:27:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJwJZNHyDCEr+ARUh7tcaeBMHUVslewSAC4l0J9/wkL5WpQTHkOzy3B1/CQcnV+zajRvTeA0 X-Received: by 2002:a17:906:7ac7:: with SMTP id k7mr2811297ejo.454.1606138034105; Mon, 23 Nov 2020 05:27:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606138034; cv=none; d=google.com; s=arc-20160816; b=OQ1CGmxPlUP4anQP3udRogeASjlbb040c1H5PEXdTbHcxmT1BX3NJZXI2zLmK66W8W r7+la4eWjO9BUlA8Vd2sRAzLzVHUPclECtPEacUsklnlN1sYpSEKXtdHJqvpTpPod9bQ zmmfBM67rjF3GmDupTPMvGcaD6EGYMFE7wFKBKxmFPVTI+3mgdAUk652ie2HAHzFfH/R Bjuqlhn4FUoU4rlWUCh9QkQvKs6K+/abSxWd0MQ0DL1b91c+LvhYt6Jiii6+px5oUldA H0BRU8AjtVLNoO9UFljl5dMsV99pmMSaD5Q+R3jxQ85Ar5Pk6SKanDoVBZM5tVGy84u7 wzsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7vupvUB1SGu1QaRXK4AVOCFBspyNzZPIwXKtWpUjuT0=; b=Rvr312zcuZujGXHG8aP+a7H3ehrUjy5QfvFR1N2ckP2Fcm9dZalYyIrceAdzZMOlmg WqOzo3ZOO4AgclIfYdlblsHwQKKCToshol3F22yU2uDc4SS57x7Tpmuo9m0DbnnQG76l ly0+8xsyTaHiw0l9P4Xiu3jqSjKcQT9dvJ4DEoQ2L0BuEvSIeMHDFzMisOKLVrnmoRZ/ grN1j26o0epCf84c+YNjjxeFz+riRnUXgw1M79YlRqa6ClvnMoLszktF/W7213/XoiOb MwfWUcqrdsbHdjoAYVo50FrrakbhijG3pQiSHbwLdTjG8ObnSvU+kybPbyTeVWlQBf3K JFIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2g1WNZ8P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oo19si6638412ejb.40.2020.11.23.05.26.50; Mon, 23 Nov 2020 05:27:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2g1WNZ8P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731541AbgKWNXG (ORCPT + 99 others); Mon, 23 Nov 2020 08:23:06 -0500 Received: from mail.kernel.org ([198.145.29.99]:47438 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731542AbgKWMfb (ORCPT ); Mon, 23 Nov 2020 07:35:31 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D0A9320721; Mon, 23 Nov 2020 12:35:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1606134931; bh=stoMUMtMKbMWCjCDbMDcEIexQLblDJ1DcpgWpADEU0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2g1WNZ8Pa4LFoLy+RoNoMOXwTDZHqNAEoSO9gnQW0r+ZEVG+b/K5PCEkmGVkP0Yc7 wTgWV1Yz+mVZbLqrfPKHP4HjKBML45fj+aRNNbsg2/y2tJCc1wDfY3iBZiHK2uGkir JgKRrzVnSFg9pck42filD89/oOqTa0CqHLgZebhE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paul Barker , Guenter Roeck , Sasha Levin Subject: [PATCH 5.4 043/158] hwmon: (pwm-fan) Fix RPM calculation Date: Mon, 23 Nov 2020 13:21:11 +0100 Message-Id: <20201123121822.004279243@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201123121819.943135899@linuxfoundation.org> References: <20201123121819.943135899@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paul Barker [ Upstream commit fd8feec665fef840277515a5c2b9b7c3e3970fad ] 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 Link: https://lore.kernel.org/r/20201111164643.7087-1-pbarker@konsulko.com Signed-off-by: Guenter Roeck Signed-off-by: Sasha Levin --- drivers/hwmon/pwm-fan.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index 42ffd2e5182d5..c88ce77fe6763 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -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); } -- 2.27.0