Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1439700pxb; Fri, 24 Sep 2021 04:44:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzNHcli3yKB2D9g/rEdbJBm0ElYEhOqBTclc1J5fC/Eu7ed1aA49Ts24Ith6JXzmYLi8Awk X-Received: by 2002:a50:cf0d:: with SMTP id c13mr4352903edk.269.1632483850943; Fri, 24 Sep 2021 04:44:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632483850; cv=none; d=google.com; s=arc-20160816; b=SS/CvXLhZrpC9hGapv+Bm77Zj7b2Ajca5O44+7UNLZIPL8SOD94WazezwVh02wuF+/ ohBgOQ/SEq80g228fnsXTV+tE5eW8td8G+0CG7L7br6S/7G/KXMAQg5Mj5Td/eyaTAe+ e4nWO7iFz+ldT5o01Fu0UWSTaGEchkR1mTJ5B8OVd1PABquC9yhn/6NRqOC4pV2D6Ih+ NUwHxUtYGEI7lL+/49+4SQ8S7mFdWerSDSccccEIhhNy1MjZtlWusSTI0lK1+ZYvGu3k fE3DY0P0faXjwKT0tgSjet+zlbc/5GIXaUY2zjxEZYwEs3F6etDS5s5AbzaiwVOIT/Cs LmWQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NuXMIMygOyCJoafI24dAFWyoJT94ScKf6Ijbd2Ke7LA=; b=azVFxAN+vvDqPlVJt2cnZ72Q6V81SDPaarMQ8ouH0hjuuCozBYpCHDZzg4mqX5/vk7 K1bwRphDOgc6OsZhn6xHZmf5+ENOYz/fTBkWY/Ud6SVV/6sFbGaz4GTxjdoelhAhrQ8p 2bEO2j6CzZbnPGIYWTZlMHKt2J5Z/jAjaYfU499HLukSNvWoPEAyApBruw0mVFvUl1Ec vmBkAIMID1wiL1R7VbLhe0oBjufaTCT+PoyNMBq2S0HfEGObUCwtzmRpBQHMKSKHJhkf PT9aah9QgHDLCuzP+t1x/7xCeDNizgktluXe2eTnhSzyTSSELsZ+HY/GVBndCLSwSsUF 2fnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=I12jm2Cy; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f20si8281629edj.364.2021.09.24.04.43.46; Fri, 24 Sep 2021 04:44:10 -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=@gmail.com header.s=20210112 header.b=I12jm2Cy; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245218AbhIXJcL (ORCPT + 99 others); Fri, 24 Sep 2021 05:32:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245154AbhIXJcI (ORCPT ); Fri, 24 Sep 2021 05:32:08 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6E64C061574; Fri, 24 Sep 2021 02:30:35 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id b15so36970059lfe.7; Fri, 24 Sep 2021 02:30:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NuXMIMygOyCJoafI24dAFWyoJT94ScKf6Ijbd2Ke7LA=; b=I12jm2CyFr0LlT8kXlEp5RhbD5uPEoiAXx073LisGPAj0kRZmfFnR3D97+bNZQi/Fc 8ccVzVBgMMXLlF0IJOopas10nG1MsnagIX50EOnlxCZKLpg5jzxN2Hf4N8gG+ZKAqcor 12RpB/N2pdRTYHCQYnXLRMXMW7gW7/y0IfF18ZAvdG0QE9SsshbmpoG3kWpGh46OrU+6 WOwdncwfc0sgrOsguZJH6LktmPgF4Kz99pMOejNSBZ1W4+D8MM5coz9KQbpLXptKZ134 ZDTfHjlkDfbnxo8o8kDQhWZLJzAirLhWTEHWjWjKOy4aOSoDNvDeb9T9f/y6uj8ab0X5 yUCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NuXMIMygOyCJoafI24dAFWyoJT94ScKf6Ijbd2Ke7LA=; b=2rLmXAIZk+mqXPAVaw+Z6mXfqBIkGH1o/qYJVmVr2GPzljbfIQ+B2e9E70UQI8whto Hhoooad3/e+ZffCnrmQpLjLlSkq5QEmhYINzuFZFEtRPovinoUbZNBS3hXFzxe+eTx1K ygo4/cna7QB2/3d7opBmdRdP3oxnF8xmGKaVFrpEX7d/o0d1/EUEkFZPBFi6T729rLVN bKdYpcCQzh0HZMc7QmBuA29jfgaVAJkWxtga/0C6SwjX/PcVwyXFrs+Wh8R4UwUqqM1I dXrHp1J20w/bRnlydrtBCqGF/XplkL2mFEw+q7tdncCuISnFFN3evUYICvq4lyvCvvwY P3Mg== X-Gm-Message-State: AOAM5324zzZLMYjy6HUPSjqKxLsCzdZKrUW+at3RO7mspDq8Q14ZSRx9 797CtbWinyMcV/zQCsk4Js4Nc42Agbk= X-Received: by 2002:a2e:bc27:: with SMTP id b39mr1772084ljf.78.1632475834114; Fri, 24 Sep 2021 02:30:34 -0700 (PDT) Received: from home.paul.comp (paulfertser.info. [2001:470:26:54b:226:9eff:fe70:80c2]) by smtp.gmail.com with ESMTPSA id o20sm342611lfu.29.2021.09.24.02.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 02:30:33 -0700 (PDT) Received: from home.paul.comp (home.paul.comp [IPv6:0:0:0:0:0:0:0:1]) by home.paul.comp (8.15.2/8.15.2/Debian-14~deb10u1) with ESMTP id 18O9UUv6026131; Fri, 24 Sep 2021 12:30:31 +0300 Received: (from paul@localhost) by home.paul.comp (8.15.2/8.15.2/Submit) id 18O9UUoc026130; Fri, 24 Sep 2021 12:30:30 +0300 From: Paul Fertser To: linux-hwmon@vger.kernel.org Cc: Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, Paul Fertser Subject: [PATCH v3 3/3] hwmon: tmp421: fix rounding for negative values Date: Fri, 24 Sep 2021 12:30:11 +0300 Message-Id: <20210924093011.26083-3-fercerpav@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210924093011.26083-1-fercerpav@gmail.com> References: <20210924022020.GA3032273@roeck-us.net> <20210924093011.26083-1-fercerpav@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- Changes from v2: - Add Fixes: tag Changes from v1: - Trivial rebase 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 3a789f21188c..85f5f0eb3d2e 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) @@ -175,10 +169,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.17.1