Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3963520pxb; Mon, 4 Oct 2021 13:50:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx53XLuAbvYe2qTdskE7Q0k2XbIjVuz/LgwZStJSFGfzM1J8VjZNMu885x0+IbuIvX/Mk7I X-Received: by 2002:a17:906:c342:: with SMTP id ci2mr19860466ejb.122.1633380624712; Mon, 04 Oct 2021 13:50:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633380624; cv=none; d=google.com; s=arc-20160816; b=XvZWTsvrbpwzDXHWXH02YZs2LuzwlvOS7hfrG70H9v36NxBDtu0H83Zqho9UUmk6cZ or5fidbg11bn0YC1AtLa9F3YUvnY2TQ3sffDHGQ3hb/agax2KXnH7Cb/INeqguBAbVnk /sQtoM/WunF1PiN0J0FDFvoTEah/3wsMLLzZlkx259eLMoVRKjqJAfeSYOa8Dbxe/CGx /z/U9ThuVbiyB6vwzYjFRoCLVdNwO12uF4MaD3iEk9M7ja6b6M17/7/lkX5bRj6sbUMi Ieh3NU/Ynd0hGFtLYFQ3Q/iFOuob06kFxYy+XvnVcYr00p1uXSJgxsfnAkgHXsSwSkms jKxQ== 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=L1u/43PB44G3sEcjuM1Wr/CSFvVRe491Hd50Jl4DXfk=; b=VNgn5tlMni6RnNdLZrIdcj4XtsjmGvnNHRMCYEZka86hVBE39mbfJ3bGkf/XZ2zBmE zUHqgsxXZKvfItdSwzEifUHxoBeDUvNzIZTQ6lD1USltPct+bZy28TRXjDuW2CFS70Iu qPF5VI0aEKj3rk3QV8fDZvNmwahwC9dBklZrsE59Z4+Crblv9OxtAufiqaYRmCVQPJz0 3LzwzNZTzILZWsBftrjut751eWYtZgjDXZsii0ZwnQ1pyfWMvS5MtOCwITOtBsnew/JM XidaiEyRltej2NUgv1cAJZBPQaPcnb6zGKa7B+Wj9KrEyV+9v+1S0Mn589g8+IAeuHZ3 k88w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2QYKrpfd; 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 ah18si3785647ejc.329.2021.10.04.13.50.00; Mon, 04 Oct 2021 13:50: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=2QYKrpfd; 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 S238406AbhJDNkY (ORCPT + 99 others); Mon, 4 Oct 2021 09:40:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:51836 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238469AbhJDNif (ORCPT ); Mon, 4 Oct 2021 09:38:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9719261872; Mon, 4 Oct 2021 13:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633353451; bh=/iEj+Dy6csfBsYnHFRL20+7jzqNMnO/aEswdmbKKV8I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2QYKrpfdtZIvqE9bYPiKA9PEDVoYSj6rDvQJAADOgNx7UFP0YnknmP5oiiDriJbLS sYgcGQv7tDo7zwQsbqdMSbL9AWTEXVAHf88Jw7YXWLnXgUqk2twRDT2EqyHZwNoA4Y 1nNihIxuhSFSUgoNjcGJ0YgJ2xmQnpUh6P8LyR4c= 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 5.14 100/172] hwmon: (tmp421) fix rounding for negative values Date: Mon, 4 Oct 2021 14:52:30 +0200 Message-Id: <20211004125048.210616434@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211004125044.945314266@linuxfoundation.org> References: <20211004125044.945314266@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 c9ef83627bb7..b963a369c5ab 100644 --- a/drivers/hwmon/tmp421.c +++ b/drivers/hwmon/tmp421.c @@ -100,23 +100,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 int tmp421_update_device(struct tmp421_data *data) @@ -172,10 +166,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