Received: by 10.223.164.202 with SMTP id h10csp2220555wrb; Mon, 27 Nov 2017 13:45:23 -0800 (PST) X-Google-Smtp-Source: AGs4zMZdVJuOGv6wOydu1mBgV0405H3VdBsqvtkuQ6dPCXQ4gtCpotnwLhAr+S2WK9PmVHKMIrr2 X-Received: by 10.101.72.1 with SMTP id h1mr29469016pgs.164.1511819123871; Mon, 27 Nov 2017 13:45:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511819123; cv=none; d=google.com; s=arc-20160816; b=o7J1imhuys2OA4eeGZJ9K7Dg2l//5VGuE6xA3HcT+GrarY6cQNP6i7+Owm6raM3jjv KHG2cgcJsJVWWIFPlSCUrCdufP8fSYNxjOR1zxynHQzKTD06znKAzDvlEX0pprNWuDL7 fViBk7mWBEwTqdnlmvCOQy/HMRKSfcszOPosVPKkN9/Y5TTbfmDt+T3e+BM9vwg42rFW Q5kTViHJbP9ARt0gaj+Z72IndQcg4oIC26MMaHfnU8J78ymILkANI9Q/wbZaeAOY5UBR YLGrCGjGnoM2QegWkda07rHUpLGSTlBa2LV6RfFMU3SdpU7G2X5B0lF7CXRNDUrT767d zJnw== 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=Fkr6tj6D0pEpZVeAK1q2vSpt4nQW+E20iUHlpY+LEEk=; b=k9uw+ruCxKPHMNZp5XqiVO2fofczEhyo4TOTibUL3O332c8zCvVed/d3zs5RYPHZP1 EM0t+bShdqiyOurj6tf7ytrWeM39SS4oO0RkMKzbBsXtjlEu7p9MkSEMaFxjXjHt4qGp 8wi2TN39sy6WIVFvxRPKsWe0kzBvgWfmIUPPxbUon5QZU3tYNlIG/YQnS4shsyIm+GI9 XbR0ofPZeQDleXC1V8lIzv31E9cFqam8O+rnM/+o6Mmbt/XD0EfT4AS2Kzv51HivW9Fv 78Ele8bSR4/bJh1ahWxF+8zWUSaA20hbkV6+T8NMwkV5IDLe6ZRLK5NB7qM1ezOsDIK4 BfnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JRjGOZKG; 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 o7si7095448pgn.202.2017.11.27.13.45.11; Mon, 27 Nov 2017 13:45: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=JRjGOZKG; 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 S1753303AbdK0VnZ (ORCPT + 78 others); 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-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-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 | 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, -- 2.15.0.417.g466bffb3ac-goog From 1585257201662292940@xxx Mon Nov 27 21:44:38 +0000 2017 X-GM-THRID: 1584805833119531257 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread