From patchwork Mon Nov 27 21:39:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Lippert X-Patchwork-Id: 10078253 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 4C6BF602BD for ; Mon, 27 Nov 2017 21:43:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3E53928D5D for ; Mon, 27 Nov 2017 21:43:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3362A2908E; Mon, 27 Nov 2017 21:43:38 +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 DE10028D5D for ; Mon, 27 Nov 2017 21:43:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752997AbdK0VnZ (ORCPT ); Mon, 27 Nov 2017 16:43:25 -0500 Received: from mail-io0-f196.google.com ([209.85.223.196]:42561 "EHLO mail-io0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752456AbdK0VnO (ORCPT ); Mon, 27 Nov 2017 16:43:14 -0500 Received: by mail-io0-f196.google.com with SMTP id u42so37988466ioi.9; Mon, 27 Nov 2017 13:43:14 -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=Fkr6tj6D0pEpZVeAK1q2vSpt4nQW+E20iUHlpY+LEEk=; b=JRjGOZKGM6QLFO4fhEKtjBNFegntG2fM4cGustYAXHx402HIDV6x0yzTDyPrfrK278 JPvSzbC8KQWLTPkjVdxNWsShFLlnSdq2GgL3PtW80RvjHh9nkcIeIZIlZoqJBAMdJcsO XCgZaNlDqWXVjDQxVSTWNam94aeTApEbKKmYUtNOznquWcqxF99vraGyZJlGDsVueMAa 9dPB7jc/lY9dpk6Y6soHQVWF2RM5GeUEVM6bL+JvQ64dSpStkQElBWF0Bcq6S8ux6HSC jRgwK2/vXAbg4Yr8ySr+1oSxrJfYbjlKx2QMVfd+5W1wAxOScBlaqp6F3HuHVrW8Rh/V 4HeA== 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=Fkr6tj6D0pEpZVeAK1q2vSpt4nQW+E20iUHlpY+LEEk=; b=i8TgDaHz6Hacmfw+qJjHUV2JNhtQNDiiWR3FGm7kxxMz9eRL9DGc2BGbcOvgFwZkZL LnX/dshYRBvlW6UzuLdpEYXM1vX13aKVnF7c3MYCxmmCU1AunmnTr3Bs/ZzPOjPOyMBb wcvGv7nts6qPtXvcbrg9QZUubDTOpglgvA5vgCUFMiH1g23dY0aOztGX8HpmcYEg+cVA vvh8Ycud93da5YaWLolpNPNbxUfMyGWTy2TEwaYn564lKCyWWZwStySaUeUdBNZs24EF ZK7Vqs4tWA+AA5JUdTPPJ64Np6mAYirZzDgwhAIAItvFJVuwYuFX7xL7DTZ905KLzYis OV/w== X-Gm-Message-State: AJaThX5ke00hL4g9FBmLPgv6EtJYY7pBHHwssltA7tVZkrGeZPbdbyeq xIGIjocTEvnNb3UxrH/v1Xa0yeTyHY8= X-Google-Smtp-Source: AGs4zMakRNQsAkosHWfYa/WuVnErkV8d8N+n3VSRNBQbb5k5QxUOsEaepGIs0a4LJr2NkFN27bJn5A== X-Received: by 10.107.142.72 with SMTP id q69mr4753464iod.205.1511818993559; Mon, 27 Nov 2017 13:43:13 -0800 (PST) Received: from rlippert.svl.corp.google.com ([100.123.242.116]) by smtp.gmail.com with ESMTPSA id o137sm2396922ito.5.2017.11.27.13.43.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 27 Nov 2017 13:43:10 -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 v2] hwmon: (pmbus) Use 64bit math for DIRECT format values Date: Mon, 27 Nov 2017 13:39:14 -0800 Message-Id: <20171127213914.141687-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 | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c index 52a58b8b6e1b..ff701502974a 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,9 @@ 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 b, R; + s32 m; m = data->info->m[sensor->class]; b = data->info->b[sensor->class]; @@ -528,11 +530,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 +659,9 @@ 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, R; + s32 m; + s64 val64 = val; m = data->info->m[sensor->class]; b = data->info->b[sensor->class]; @@ -673,18 +678,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,