From patchwork Wed Nov 22 22:08:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Lippert X-Patchwork-Id: 10071353 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 99D8E60353 for ; Wed, 22 Nov 2017 22:08:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C0D929945 for ; Wed, 22 Nov 2017 22:08:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7F8D029E20; Wed, 22 Nov 2017 22:08:54 +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=ham 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 22E5F29945 for ; Wed, 22 Nov 2017 22:08:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751611AbdKVWIx (ORCPT ); Wed, 22 Nov 2017 17:08:53 -0500 Received: from mail-io0-f194.google.com ([209.85.223.194]:40699 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751588AbdKVWIw (ORCPT ); Wed, 22 Nov 2017 17:08:52 -0500 Received: by mail-io0-f194.google.com with SMTP id d21so2542914ioe.7; Wed, 22 Nov 2017 14:08:52 -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; bh=AtIs5eFJUf/uF1awTl4YftpeF32+t5uMNW27Y6a/r3A=; b=i9mzodS4B+BHZ4dI4F0e2KBKZErkYASZm90ImQCr5LzxE/yT1B7gYl9SA0+jFvFBwD 5144kXLr7wzFqucSdGtmGmkCqxqLYSWMal7RTigG/KcB+psgOrLEg14OYveKEGdFyVFs f762/bdZlqNMaoKrZhtg9olhifL+mXZph4G7ScKdLU1EvM6u+PqBJpDTDyNp9jLAKT8F ls72cZZoVYoXfQqf9vCB4MVqzIRMDj6Mx711YtDYGRgOxSkp+0RyO0sk83dp4vVqinTv Q5C7W7jIbWjsGioATMMflFH1YVcMF12m8ceArEXiVXmaE8phP6gA8sQjBVdkZXq9PzZ/ k4LQ== 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; bh=AtIs5eFJUf/uF1awTl4YftpeF32+t5uMNW27Y6a/r3A=; b=i5cHBib5MyXQ10wNe2XijlpK/kbzBqLi+INiqzowHFbQHpSqFrqv7R/x5JIDP855Cl +TqKZSn317XwwT5UjGGgTPSIOR0omBh3s0gsffp5FOyo/QfgqPdSbu72q6ht7crgegOl 8lNOB0O7NbzAPekg0lYPxCBSoDvLdKQTA1Vx2lnJIgHyw0cWr8sB6CFcydHdN7kCOSVt PTkShMGun9jesGXjuC8cx9Jw3xaGiJpcw4ikSbPEWhcqjuvk5zT4TqatzzLZWXPIWtNY p3B2hnWFsLX4MeWI6UUk+kPxzzi8kKcgsrSJaWF5p+rRUAYwpKiQeSAlDFEBRxXzfFvR Aerw== X-Gm-Message-State: AJaThX5Bl1b8VlxZFpmi8w5+9nXxgUp2QdB78XCVQvJMp5ST0qLMSLDk UW/zAnCERxR/JzhwcysOGg8= X-Google-Smtp-Source: AGs4zMYsuhCCX8Y3iUlbwMR28q2GHwE0jv6Dxh1zybYb9njYbC1FOAYKbokfrvJffGyxQ7zY4/sSRQ== X-Received: by 10.107.59.199 with SMTP id i190mr23953427ioa.97.1511388532199; Wed, 22 Nov 2017 14:08:52 -0800 (PST) Received: from rlippert.svl.corp.google.com ([100.123.242.116]) by smtp.gmail.com with ESMTPSA id x137sm2531010itb.37.2017.11.22.14.08.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 22 Nov 2017 14:08:51 -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] hwmon: (pmbus) Use 64bit math for DIRECT format values Date: Wed, 22 Nov 2017 14:08:43 -0800 Message-Id: <20171122220843.18309-1-rlippert@google.com> X-Mailer: git-send-email 2.15.0.448.gf294e3d99a-goog 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..f4efea9f282e 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -499,8 +499,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 val = (s16) sensor->data; + s64 m, b, R; m = data->info->m[sensor->class]; b = data->info->b[sensor->class]; @@ -528,11 +528,13 @@ static long pmbus_reg2data_direct(struct pmbus_data *data, R--; } while (R < 0) { - val = DIV_ROUND_CLOSEST(val, 10); + do_div(val, 10); R++; } - return (val - b) / m; + val = val - b; + do_div(val, m); + return val; } /* @@ -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 m, b, R; + s64 val64 = val; 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); + do_div(val64, 10); R++; } - return val; + return (u16) val64; } static u16 pmbus_data2reg_vid(struct pmbus_data *data,