From: Guenter Roeck <linux@roeck-us.net>
To: Paul Barker <pbarker@konsulko.com>
Cc: Kamil Debski <kamil@wypas.org>,
Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>,
Jean Delvare <jdelvare@suse.com>,
linux-hwmon@vger.kernel.org
Subject: Re: [PATCH v5] hwmon: pwm-fan: Fix RPM calculation
Date: Thu, 12 Nov 2020 06:57:21 -0800 [thread overview]
Message-ID: <20201112145721.GA238476@roeck-us.net> (raw)
In-Reply-To: <20201111164643.7087-1-pbarker@konsulko.com>
On Wed, Nov 11, 2020 at 04:46:43PM +0000, Paul Barker wrote:
> 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>
Applied.
Thanks,
Guenter
> ---
>
> Changes from v4:
>
> * Don't update ctx->sample_start if delta=0.
>
> Changes from v2:
>
> * Don't update the RPM value if delta=0.
>
> drivers/hwmon/pwm-fan.c | 16 +++++++++-------
> 1 file changed, 9 insertions(+), 7 deletions(-)
>
>
> base-commit: f8394f232b1eab649ce2df5c5f15b0e528c92091
>
> diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c
> index bdba2143021a..1f63807c0399 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);
> }
>
prev parent reply other threads:[~2020-11-12 14:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-11 16:46 [PATCH v5] hwmon: pwm-fan: Fix RPM calculation Paul Barker
2020-11-12 14:57 ` Guenter Roeck [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201112145721.GA238476@roeck-us.net \
--to=linux@roeck-us.net \
--cc=b.zolnierkie@samsung.com \
--cc=jdelvare@suse.com \
--cc=kamil@wypas.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=pbarker@konsulko.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox