From patchwork Tue Nov 10 17:54:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barker X-Patchwork-Id: 11894929 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2E196697 for ; Tue, 10 Nov 2020 17:55:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F2A2B20797 for ; Tue, 10 Nov 2020 17:55:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="SQ2XJD79" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726307AbgKJRzO (ORCPT ); Tue, 10 Nov 2020 12:55:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726152AbgKJRzO (ORCPT ); Tue, 10 Nov 2020 12:55:14 -0500 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A389CC0613CF for ; Tue, 10 Nov 2020 09:55:12 -0800 (PST) Received: by mail-wr1-x443.google.com with SMTP id b6so2340910wrt.4 for ; Tue, 10 Nov 2020 09:55:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=EQYx9zH63AvyW1sDHfoz1a/ffE3wZoAVdeE30FDrYYQ=; b=SQ2XJD79B5Sdswan6CFyzTrjbzKSW6BDBI9jqf5JNA/fHagQybM5VWi9GMSiRuz7pD cBxRINk6ucJwDDdrmoAJCH+Ogj0yForupfipWGPFBiOJsUNXXuCLqIRNP4dNFNVP98Sq X5RdVTW0vHzQrKjONrXnWWrguXpfowWFnV8qw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=EQYx9zH63AvyW1sDHfoz1a/ffE3wZoAVdeE30FDrYYQ=; b=Mj5FSllLWM8m12r6IymoeRybpTglXJB+ejcRDfncdwYHqQ5Z1BlbPfrsZGmgM7IV3w qe1k2QeY+gDB/6Q7EeCmW1psIFOL5hR4lyV2s+qxTQ6Rtk1FyYsnrXUxhQztMnzQ5089 zidBwQxYN6rXM0uelXGOBypmHAkpRKbm+7p5rVvfmC502coloeKRaS9azlWJ9j3/hd4e v+ptbYMSO6cetQvilothGC3T1yvzCKYtb1/13myXb5XKpvbWxHOl6tIUM5dxo1dbSD5k sV8vkgjUmqxrGfZusFEZh3XlOWKD3hRQl673kmDSwBSDCUtfkS+fYJqQRfEiSTDsT6Sq bs1g== X-Gm-Message-State: AOAM533ORfObi4ke1UpCILx/YgK6IYn2bLVCQLPcXcuEuhHpL1Eliqt5 A1DYhu+kURcHEEqYu0xojdkM5A== X-Google-Smtp-Source: ABdhPJwvCy7VKDFIyqrRdgvr4XID3fWRw6GdxtBJ/yEI+TTFgYcGDqV95ZZPvRtg2pRtUdMlViW33Q== X-Received: by 2002:a5d:474d:: with SMTP id o13mr26714946wrs.178.1605030911365; Tue, 10 Nov 2020 09:55:11 -0800 (PST) Received: from alpha.home.b5net.uk (cpc76132-clif11-2-0-cust249.12-4.cable.virginm.net. [80.7.160.250]) by smtp.gmail.com with ESMTPSA id k84sm3917164wmf.42.2020.11.10.09.55.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2020 09:55:10 -0800 (PST) From: Paul Barker To: Kamil Debski , Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck Cc: Paul Barker , linux-hwmon@vger.kernel.org Subject: [PATCH v4] hwmon: pwm-fan: Fix RPM calculation Date: Tue, 10 Nov 2020 17:54:34 +0000 Message-Id: <20201110175434.2591741-1-pbarker@konsulko.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org 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 --- Changes from v2: * Don't update the RPM value if delta=0. drivers/hwmon/pwm-fan.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) base-commit: f8394f232b1eab649ce2df5c5f15b0e528c92091 diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index bdba2143021a..d1fd50c32514 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -54,14 +54,15 @@ 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(); mod_timer(&ctx->rpm_timer, jiffies + HZ);