Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3923022pxb; Mon, 4 Oct 2021 12:44:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXW43iQY89kST0Tzrqzh52YS0IrVeoMzTHLkpqeqkiNi2DxUQ2BRJlBBNn/eMCeVwHJH6s X-Received: by 2002:a17:906:1405:: with SMTP id p5mr19320056ejc.197.1633376664637; Mon, 04 Oct 2021 12:44:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633376664; cv=none; d=google.com; s=arc-20160816; b=rfUV3dnx2SOaBOuh2BzekSqjtnh6glVcHR/asn/ucmVTYV61HfZv9NEJjwpbA3eYSR Z1vX8RIrME8kcm4eEKopYpBLpFeup80KWS3Eti+ONzKwlpcYj1PIvOOaTP5xkAFMmQ0j n5WKlz2fVnf1a1otuX8wPFTZaLD3QI7eF4/gzCVEPPj12c1juhL+aCQx46GmM4DEjT3V MDX78NW7iGny5EgE+Z7RVDA0+0UKB6+gYrMm1x15EVygOVRdCA5Ml3d2rwjiAVgNymGh zbCG6DCdRovSAdjqyIMu//vsZSzKT/XPmFVTwOVwgOgboArUvoq69DEC7wS0l+XEf1+8 2KOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=CorrRW2uk3ywfR7RJea56mI7H4lKLy9mG/8thNpwJ0c=; b=l7xfzcqwTql1X1+VI96MfmC76xL0L05WY+7ZrlLspHgIxnywn3zZ0Z6eeuT0veFIoP T/PtpEpEvwVF17Rz+Bv+pE0mQmn1h+ObuvuEeJWzfxrHYTUvCasuFoD9MAp0ljGh432L ipoW9Ub/oDt7xpJ1xZabCsxBEDEi7CsRjj9F6y1JPQBNMCm/DrGgIar4E1WFz0kVJnpz ef1FbsDUvpBhajuavH7bbbcuUPVRzArZhQuL4O/tMpORgv2pobfO3qtDTXdeNDMS0kQl 2MRICXzKIt7KnIowdMoZ1aNN4osfRdypSXoTQFfmnCP5Ek9/QMDfuzca+E74hi4IwH3q GJ/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ch5OYSKN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n6si19854665eda.103.2021.10.04.12.44.01; Mon, 04 Oct 2021 12:44:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ch5OYSKN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235199AbhJDNFs (ORCPT + 99 others); Mon, 4 Oct 2021 09:05:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:39478 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235095AbhJDNDp (ORCPT ); Mon, 4 Oct 2021 09:03:45 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1DC3D611CA; Mon, 4 Oct 2021 13:00:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633352407; bh=/jedxVR/RPuLMpMlT+S75a77nQHDpdLauJf1ZEvXxzQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ch5OYSKNT4QgbqNaj+RlvOXP2PWO8+UA+d9BBPixW1sl1NAgSlxxM+q9ZDBMtKy2X GtNJapP//TZgjkFlC5hTvGU42WzGk5zhGm5zuQmleMMZxakeyq3c1do8HwlUCkP+S1 fjdI1myR3uzKJLecgBYUk7fypetHAjVwebHH7DWc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Paul Fertser , Guenter Roeck , Sasha Levin Subject: [PATCH 4.14 50/75] hwmon: (tmp421) fix rounding for negative values Date: Mon, 4 Oct 2021 14:52:25 +0200 Message-Id: <20211004125033.202886857@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211004125031.530773667@linuxfoundation.org> References: <20211004125031.530773667@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Paul Fertser [ Upstream commit 724e8af85854c4d3401313b6dd7d79cf792d8990 ] Old code produces -24999 for 0b1110011100000000 input in standard format due to always rounding up rather than "away from zero". Use the common macro for division, unify and simplify the conversion code along the way. Fixes: 9410700b881f ("hwmon: Add driver for Texas Instruments TMP421/422/423 sensor chips") Signed-off-by: Paul Fertser Link: https://lore.kernel.org/r/20210924093011.26083-3-fercerpav@gmail.com Signed-off-by: Guenter Roeck Signed-off-by: Sasha Levin --- drivers/hwmon/tmp421.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/drivers/hwmon/tmp421.c b/drivers/hwmon/tmp421.c index ceb3db6f3fdd..c45968454110 100644 --- a/drivers/hwmon/tmp421.c +++ b/drivers/hwmon/tmp421.c @@ -109,23 +109,17 @@ struct tmp421_data { s16 temp[4]; }; -static int temp_from_s16(s16 reg) +static int temp_from_raw(u16 reg, bool extended) { /* Mask out status bits */ int temp = reg & ~0xf; - return (temp * 1000 + 128) / 256; -} - -static int temp_from_u16(u16 reg) -{ - /* Mask out status bits */ - int temp = reg & ~0xf; - - /* Add offset for extended temperature range. */ - temp -= 64 * 256; + if (extended) + temp = temp - 64 * 256; + else + temp = (s16)temp; - return (temp * 1000 + 128) / 256; + return DIV_ROUND_CLOSEST(temp * 1000, 256); } static struct tmp421_data *tmp421_update_device(struct device *dev) @@ -162,10 +156,8 @@ static int tmp421_read(struct device *dev, enum hwmon_sensor_types type, switch (attr) { case hwmon_temp_input: - if (tmp421->config & TMP421_CONFIG_RANGE) - *val = temp_from_u16(tmp421->temp[channel]); - else - *val = temp_from_s16(tmp421->temp[channel]); + *val = temp_from_raw(tmp421->temp[channel], + tmp421->config & TMP421_CONFIG_RANGE); return 0; case hwmon_temp_fault: /* -- 2.33.0