From patchwork Mon Nov 27 23:51:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Lippert X-Patchwork-Id: 10078407 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0ADEF602BD for ; Mon, 27 Nov 2017 23:52:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1AE029098 for ; Mon, 27 Nov 2017 23:52:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E6571290A2; Mon, 27 Nov 2017 23:52:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E80629098 for ; Mon, 27 Nov 2017 23:52:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751834AbdK0XwM (ORCPT ); Mon, 27 Nov 2017 18:52:12 -0500 Received: from mail-io0-f196.google.com ([209.85.223.196]:40933 "EHLO mail-io0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752652AbdK0XwK (ORCPT ); Mon, 27 Nov 2017 18:52:10 -0500 Received: by mail-io0-f196.google.com with SMTP id d21so16118483ioe.7; Mon, 27 Nov 2017 15:52:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=UmpFa9EgMcUFLd8FX9NtgZnKcugaWBN2huluLRwU6lU=; b=E3spcbuPh6hK4q7CBhfWEodPwD1zSDd/CPUHVWKyBsq+RkZF+qKWqn9RIpi+WHMg5g mW8W0tnauqQMnQ/q1/Itfv9OVVcHd8QJm6/Fm/NW1Kf3B8w6UyUxpH5V/SvrN070WNDS Hg/yser3ZTYdyyK9BMUXkJcRqL5F+FlL52RVWOJtKGqxR18XsqbFclFrPtbKk/DVdVEg FxbgauHvJaiNtrF2em/iBWqXt37bdXwPlEKE3C1bVGKAC3Is8bSPWGiQOR4UQlaqeIoA JbsFLQC/k6ka/deoT+Z9MPwJ9IEy27dzthcqRM5I4Xh+npDFHaMq6NzrAXsyMY7/tXNC jsug== 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; bh=UmpFa9EgMcUFLd8FX9NtgZnKcugaWBN2huluLRwU6lU=; b=QAa/IPlG9yQ0VYc+LWGIivG2q+YhljneNWatpSE9fWezXx5IAFXSKBwA+1Vkf95+4z IdYOz9wwsPDfo/VZ0EI4cewmpykxfZiop5hfTXiL2MY1T6fUI2BxFFdgJjZJKuRAR6Tf mUQFD6LY0STJ1eJ8N6eGlI2qQSEuHNVDo7pirOUw4nLn/NhPs5x6Oc33mEw6lDjAkVeZ +pvRuKhJ78YNo7TRxsexjrZC9WwnzXhctD0GCyRJ029y1+CKS984d9yFOYVW1zQ7GWyU zEJ+3iNuzqJDjZ6kqM+bopL9E26XH++UV2AFu6xVYmDV89YiKR4lYD9CgtWltj4qOz1I IuoQ== X-Gm-Message-State: AJaThX55Le29XG7o7ZkbsynvgOTudhvaRmEsmJqoMfScEw/k4ccEBYLN rwOufTT0XX3kULxnwJnhpgkvVs8T1r0= X-Google-Smtp-Source: AGs4zMaGGaKCZ5rlbiqipfVwEv3WYH3x7KHR8yUHA4Hz/jWu81xQdTrFoIVsmbjM81cjFWZviF2RgQ== X-Received: by 10.107.132.85 with SMTP id g82mr22609446iod.232.1511826729215; Mon, 27 Nov 2017 15:52:09 -0800 (PST) Received: from rlippert.svl.corp.google.com ([100.123.242.116]) by smtp.gmail.com with ESMTPSA id d186sm6356100itd.22.2017.11.27.15.52.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 27 Nov 2017 15:52:08 -0800 (PST) From: Robert Lippert X-Google-Original-From: Robert Lippert To: linux@roeck-us.net Cc: linux-hwmon@vger.kernel.org, jdelvare@suse.com, linux-kernel@vger.kernel.org, xow@google.com, Robert Lippert Subject: [PATCH v3] hwmon: (pmbus) Use 64bit math for DIRECT format values Date: Mon, 27 Nov 2017 15:51:55 -0800 Message-Id: <20171127235155.91764-1-rlippert@google.com> X-Mailer: git-send-email 2.15.0.417.g466bffb3ac-goog In-Reply-To: <20171122220843.18309-1-rlippert@google.com> References: <20171122220843.18309-1-rlippert@google.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Power values in the 100s of watt range can easily blow past 32bit math limits when processing everything in microwatts. Use 64bit math instead to avoid these issues on common 32bit ARM BMC platforms. Signed-off-by: Robert Lippert --- drivers/hwmon/pmbus/pmbus_core.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 52a58b8b6e1b..09977440bbea 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -499,8 +500,8 @@ static long pmbus_reg2data_linear(struct pmbus_data *data, static long pmbus_reg2data_direct(struct pmbus_data *data, struct pmbus_sensor *sensor) { - long val = (s16) sensor->data; - long m, b, R; + s64 b, val = (s16) sensor->data; + s32 m, R; m = data->info->m[sensor->class]; b = data->info->b[sensor->class]; @@ -528,11 +529,12 @@ static long pmbus_reg2data_direct(struct pmbus_data *data, R--; } while (R < 0) { - val = DIV_ROUND_CLOSEST(val, 10); + val = div_s64(val + 5LL, 10L); /* round closest */ R++; } - return (val - b) / m; + val = div_s64(val - b, m); + return clamp_val(val, LONG_MIN, LONG_MAX); } /* @@ -656,7 +658,8 @@ static u16 pmbus_data2reg_linear(struct pmbus_data *data, static u16 pmbus_data2reg_direct(struct pmbus_data *data, struct pmbus_sensor *sensor, long val) { - long m, b, R; + s64 b, val64 = val; + s32 m, R; m = data->info->m[sensor->class]; b = data->info->b[sensor->class]; @@ -673,18 +676,18 @@ static u16 pmbus_data2reg_direct(struct pmbus_data *data, R -= 3; /* Adjust R and b for data in milli-units */ b *= 1000; } - val = val * m + b; + val64 = val64 * m + b; while (R > 0) { - val *= 10; + val64 *= 10; R--; } while (R < 0) { - val = DIV_ROUND_CLOSEST(val, 10); + val64 = div_s64(val64 + 5LL, 10L); /* round closest */ R++; } - return val; + return (u16) clamp_val(val64, S16_MIN, S16_MAX); } static u16 pmbus_data2reg_vid(struct pmbus_data *data,