Received: by 10.223.164.202 with SMTP id h10csp2344181wrb; Mon, 27 Nov 2017 15:53:24 -0800 (PST) X-Google-Smtp-Source: AGs4zMZun7cFNkkh73/rQFU6jGputd3kr0y+r3dcFPeYRXdRNufCcP/BUbJos/iGSqWOs9L3uCkV X-Received: by 10.159.198.11 with SMTP id f11mr41029138plo.425.1511826803930; Mon, 27 Nov 2017 15:53:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511826803; cv=none; d=google.com; s=arc-20160816; b=sJP+zOe0QcPfBSs+/FlwHNHbujWb5ZGmxNF9ChXN0UFuwgTBHC+H0m112Qm7riWK8c kI1PuTzdtTUGgZZpuSarvXphO2oZRJIPky/2+eQj3EJoAeDKjpnALzXrYxHdScpvrOs8 h2GNvx0aoWZuSDyIOPPdrEmp/dvLSG+VmECA5eMYZII42LjbuFkd+iwHQ5JQIyD9wdso MAM2+YhJedGIdDq1hwjCD3mFzsKn/m8w6eyviQbF0NT06VrpZP11+Chw/PCIsniNO3Da joqMgz5fyqyjwMPKAJoOvCywG9wj0OnQD7+E8J77x/EhHxNQL4nUrLhu7cJttEfxBrqw fjdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=UmpFa9EgMcUFLd8FX9NtgZnKcugaWBN2huluLRwU6lU=; b=cXFIfjwOmh8B6tRwrhedylnFHejGa3xRffeAiDCoC5nOEjF4wEag6wTMWsgBtyzrsh 6AuV7TVkIGlYdF29iFuZvzAVbVvr7PsEZw7mA6X44pZFuJ9dIhUebKMGH3PXeFFnay6O RzCDOot7zSHVQ30gZd+v2Uan+laBJdf7VXs2oMrtqDFxkuAnfg6Vz95rCJmKnf8iaLOi fTiDIEDACQ65a3iG2+pqXpBEGzp6TSjYCyamAWRDXcuaOfed7cput0753706xsrPjkqV 5RBkTblkPuobKbxQAHzaRC+0yTac3wwFPfjfPfrz0AJOJUgtm8s2rt+ephma8CpKuvx3 WexA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=E3spcbuP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e7si25933979pfl.148.2017.11.27.15.53.12; Mon, 27 Nov 2017 15:53:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=E3spcbuP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752967AbdK0XwO (ORCPT + 78 others); Mon, 27 Nov 2017 18:52:14 -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-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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, -- 2.15.0.417.g466bffb3ac-goog From 1585258961494123628@xxx Mon Nov 27 22:12:36 +0000 2017 X-GM-THRID: 1584805833119531257 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread