From patchwork Tue Nov 10 11:28:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barker X-Patchwork-Id: 11894041 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 0E829138B for ; Tue, 10 Nov 2020 11:29:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D138920781 for ; Tue, 10 Nov 2020 11:29:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="bVrBNHXc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731898AbgKJL3A (ORCPT ); Tue, 10 Nov 2020 06:29:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731588AbgKJL2x (ORCPT ); Tue, 10 Nov 2020 06:28:53 -0500 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03A43C0613D1 for ; Tue, 10 Nov 2020 03:28:53 -0800 (PST) Received: by mail-wm1-x341.google.com with SMTP id c9so2638108wml.5 for ; Tue, 10 Nov 2020 03:28:52 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=Wv/1zH8BOwsn27fpscx7RB25TZ1M2ckWvcMUa7jbq8o=; b=bVrBNHXcNahdAyE52ABHm5PIKmLvHaWenQB57SQGXSncHHEqrl+C3sBijT+8Cr62Qk f4Xn+2F2qaBrroaIp5qlW+Fij+HUKjE8tew6Jy789OqU2O3aYeYFfzNmgB7Atz+5LWN1 JYvPIeAvlkqWA4NaxUAYvsDsM91tEazIzr7Eo= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=Wv/1zH8BOwsn27fpscx7RB25TZ1M2ckWvcMUa7jbq8o=; b=J7WKjFzvoRal7rnIXhXZmS1qNSBpq8pcPMHGrJ6YIOR9EC2uc6Qx7/kVvIpI8JRDK+ jJpFfvcCnaS3BdZC5k9YJL2vd/vVbZIRu9TELMvM0TiV41xmg3mpSzd+3NNE++Vy6sNu IJgji9ooYI8xsNt1dnlZj1xextrd+IKYwQCNJG7tmpH3c4CLBqvGaBJ0kCgYygSvWLak 6rXT4u1YLCGqIpIHuYUW/7roxxEu4LVQ3RBK/dfNMZhCbmroi+p0Ilzql6lkVKBWfK+p RKQsnXMGZUm0ztPx1MkOjjIaoSPxECDlX9BsCOkeM7mS5K96J6koK4JmsEVVgD2jPV9a Xgnw== X-Gm-Message-State: AOAM532s0x9jQP8qgtRQ96yUJ+Kw1YScqVI9r85/5+OdbX87UH41eV9z UV+/rgZDSfwm4jk0GYh//suqIA== X-Google-Smtp-Source: ABdhPJxKB7LIH+le7NrP+fw23qS/o+NhxWkYddkoKmIGDPt2SP9TZO6ciyto8VVC5Pl+ZPjg0WkT0A== X-Received: by 2002:a7b:c846:: with SMTP id c6mr3998539wml.143.1605007731712; Tue, 10 Nov 2020 03:28:51 -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 c64sm2575608wmd.41.2020.11.10.03.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2020 03:28:51 -0800 (PST) From: Paul Barker To: Kamil Debski , Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck Cc: Paul Barker , linux-hwmon@vger.kernel.org Subject: [PATCH v3 1/2] hwmon: pwm-fan: Store device pointer in pwm_fan_ctx Date: Tue, 10 Nov 2020 11:28:16 +0000 Message-Id: <20201110112817.52549-2-pbarker@konsulko.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201110112817.52549-1-pbarker@konsulko.com> References: <20201110112817.52549-1-pbarker@konsulko.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org This allows us to use dev_err() and friends from functions which only have a pwm_fan_ctx pointer. Signed-off-by: Paul Barker --- drivers/hwmon/pwm-fan.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index bdba2143021a..edc0453be25a 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -26,6 +26,7 @@ struct pwm_fan_ctx { struct mutex lock; struct pwm_device *pwm; struct regulator *reg_en; + struct device *dev; int irq; atomic_t pulses; @@ -290,6 +291,7 @@ static int pwm_fan_probe(struct platform_device *pdev) if (!ctx) return -ENOMEM; + ctx->dev = dev; mutex_init(&ctx->lock); ctx->pwm = devm_of_pwm_get(dev, dev->of_node, NULL); From patchwork Tue Nov 10 11:28:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Barker X-Patchwork-Id: 11894107 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 49CC615E6 for ; Tue, 10 Nov 2020 11:29:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 24CE420659 for ; Tue, 10 Nov 2020 11:29:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=konsulko.com header.i=@konsulko.com header.b="Iw85vQnK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732116AbgKJL33 (ORCPT ); Tue, 10 Nov 2020 06:29:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731824AbgKJL2z (ORCPT ); Tue, 10 Nov 2020 06:28:55 -0500 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 009A6C0613D3 for ; Tue, 10 Nov 2020 03:28:53 -0800 (PST) Received: by mail-wr1-x441.google.com with SMTP id s8so5182715wrw.10 for ; Tue, 10 Nov 2020 03:28:53 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=QcRHLaymtC6v0OYZ8BnRaI3EaRMpepE3kz0XiIhvQbI=; b=Iw85vQnKkCDY2Sd5xyZ3Io/Q6m/gzKZeMO3R7ECwEUMejlURv5fghPA073cVptWwe8 kuA9Tn2IP7k0BGTDGYepbETLQSEhAtiGjHOtuR2tusq8SPXSlAx2atlkdPWpkMQh00hy OTs0MdO/z0YkD9kKaWp/v+whzduv/Vxuq8tBI= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=QcRHLaymtC6v0OYZ8BnRaI3EaRMpepE3kz0XiIhvQbI=; b=IvIXtRB7tCmWtKrFuI5pTmmEHgzU6tjFPatM389oTyfcCl9NQr0kU+yQC92LBC2EvD aiFqOMnkzKfP57/8/DcjKqXYK+oElN7uHYIKlH7ShC4uJZwSvWQWl5M3DQPjqXdu0OiD 3F8tKiqsTaXeB5LHSwpZLIUxSvaYDMXoRMUIsCUcBPy/c46DKAHI54aEr2AwCUW72kvU otYNA8cgZpMWIT+xbGKg8f5l2S7WNoUbyi8VwFVey6xxfjxrr9S4HEFOhRiKO4kw063e LbdvzLiOvru04bGN6jI8vanGg5RkeIgdVISgjl/dQjQMv31pA3JcVBfZOr/Np2/6xobv e1eA== X-Gm-Message-State: AOAM532NBc65KNan+NXsAdVNgcv0ualedLD98G0FIqtnBomr/3WIfowQ iOJ0o8EoVJKYvmuw/YRGEeYNew== X-Google-Smtp-Source: ABdhPJycWTY30ADYTD7582nT7r2OtFLtD5BN5ulfeUTBMydHlK8nHVIFPj0dZmCssJz4KpELEb9nFw== X-Received: by 2002:a5d:534d:: with SMTP id t13mr22393922wrv.69.1605007732794; Tue, 10 Nov 2020 03:28:52 -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 c64sm2575608wmd.41.2020.11.10.03.28.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2020 03:28:52 -0800 (PST) From: Paul Barker To: Kamil Debski , Bartlomiej Zolnierkiewicz , Jean Delvare , Guenter Roeck Cc: Paul Barker , linux-hwmon@vger.kernel.org Subject: [PATCH v3 2/2] hwmon: pwm-fan: Fix RPM calculation Date: Tue, 10 Nov 2020 11:28:17 +0000 Message-Id: <20201110112817.52549-3-pbarker@konsulko.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201110112817.52549-1-pbarker@konsulko.com> References: <20201110112817.52549-1-pbarker@konsulko.com> 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 bail out instead of dividing 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 --- drivers/hwmon/pwm-fan.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c index edc0453be25a..24cfed4d625e 100644 --- a/drivers/hwmon/pwm-fan.c +++ b/drivers/hwmon/pwm-fan.c @@ -55,14 +55,19 @@ 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); + } else { + dev_err(ctx->dev, + "Cannot determine fan RPM as time delta is zero\n"); + ctx->rpm = 0; + } ctx->sample_start = ktime_get(); mod_timer(&ctx->rpm_timer, jiffies + HZ);