Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1456179pxb; Fri, 24 Sep 2021 05:06:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYpPqNkgzfDy9GnhMFpa8x51nrhsFfVn1B8elCJDHQ2OC21DnouqkwuecqBLivmUyOnbp9 X-Received: by 2002:a50:d901:: with SMTP id t1mr4587784edj.79.1632485170200; Fri, 24 Sep 2021 05:06:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632485170; cv=none; d=google.com; s=arc-20160816; b=KWMlnVHfRWcQ1+S5c5d+4wiuXajdv+c72Ga2vcoHKLW9u9HdWftJpzh2gojkFuvRhC 7/zfB0Fut+vk3Fh7ktyd2BwlBx+6P2Jt/K1l/QFyC+qj9ldKNY8aHQ8kEoN0OatCdJ+K ctoh95CKJW1LQHLzFoaLvM4+UNZqp8VTNybI1ka5jA4oOguIzTSkBQ8wYXTGdFiqHseM k0ZBB8LGaKwkNxEcRsfP/bqrCDfAzLu3e4EEWZy+tS5KRUy3O8Vg68JwPa2fcWwyEK94 4XIoZ2s9bhn20i/Yqs3uOMU5FMImz77AJa12bscCTyZ7FtPdiHnQREcl2qu+ZJNMwxp4 Qz8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:sender:dkim-signature; bh=xe8hbcCe7Il2R3uT9h3I/mQYjYvm30reKshUti4lTYU=; b=GilQqwP3O3Wm6fChSGx06bO6/WZvrJPk87OdoQjjNbcX1bmcizCtyMNKPtQq/Q3uCK Ag5bZJaRPAIe7wB3drlNZBp4U89bJ462tQ8pDIIzHKqMa+x/v5nt/QilUJnKRzAPVH54 uDQZejW9MHFbd59X5NwdqHFAX1/h7higEkcOTFT8giXFImJ5W1DSEcgt2PwDg0GLwQp6 vaFgd9SeNH5eH1HW/qXOFL4LRuNE4yC8XVjeixj1SodP7bcbJPTHFv/JIUdMv/7pPCqq pHcE+YucctfBExwD8C3lrC3xT9ohgEXFHCI7udr/6y+OLRCBojrXIRN3sZ8tPksP0d8m Nj9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=HhsXDQ+E; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e10si12666793edj.183.2021.09.24.05.05.37; Fri, 24 Sep 2021 05:06: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=HhsXDQ+E; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245618AbhIXMC7 (ORCPT + 99 others); Fri, 24 Sep 2021 08:02:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244439AbhIXMC6 (ORCPT ); Fri, 24 Sep 2021 08:02:58 -0400 Received: from mail-ot1-x32b.google.com (mail-ot1-x32b.google.com [IPv6:2607:f8b0:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E3A3C061574; Fri, 24 Sep 2021 05:01:25 -0700 (PDT) Received: by mail-ot1-x32b.google.com with SMTP id s36-20020a05683043a400b0054d4c88353dso1932311otv.0; Fri, 24 Sep 2021 05:01:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=xe8hbcCe7Il2R3uT9h3I/mQYjYvm30reKshUti4lTYU=; b=HhsXDQ+Enaw+Ox6jaivp+qkBNL3jZBeVYJtkeOBUVX6ZI33Uk7bQlP8JuCKOE8M1cK Zqfqf5H6mofkXgAhhaxtv69r/jBZBXSO9fen+scZLl1euVLoXEZ1z0hQeuR1CeHYPnMa 8gyCJyz6U9SpXtdb6BVVC0CknxeBtKroEB2XTgV98OYCP0ymuJYY1NwmgHKllRKVtC9q pQt2RZYD9BLBDE0OXZX3qfwG12DBXxTunRbxFhHAWgFB54+nc4txs6IrzJilYmuPaXlN jdAsLLN4j6F916fXKaFfiKmUdDV3mW2c5IJ4MZwSKhIJinOKlFtM0gcO6vDCMskGRtHq 98iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=xe8hbcCe7Il2R3uT9h3I/mQYjYvm30reKshUti4lTYU=; b=1PyUR/mz+QTY47KDn2KAQEzUBblZKITdMaOpWWU/aboX8FBfvLSSx9hnNvlR/78aHv enCk+qlNvfu7ZAiUXGyubqJW4aLd+L0FD4cXJD/7/1v2ih6VYSLd4i4uztnCr9C+huRs d744nhpf6ocZO5p5JrZgPG4pG9K4gYXcQlAhx82L6IyQBHTXfkF3hvMAjlgKpYlWM3JP TGVVt76d+lckjOlcDcS+JJ/sm5dnjh6bOaJrB369tZH9Nzce3sdFdaQVZvsqP+9qeBuJ 5PQclqkzp1gSTWvG3DiBIzBWW+XNtrZ3zZoYsLfqaNOmVNpvlrimtLlGXNj6Botgl/I4 d7uw== X-Gm-Message-State: AOAM533cJAmBM6Luwf+V6kLoKDoB/5k15F50yMZawi1WUKbPzMnqWpE0 LKarKLjBlBaqwADFrA3ziRb0sj9yvTY= X-Received: by 2002:a05:6830:79c:: with SMTP id w28mr3490244ots.332.1632484883173; Fri, 24 Sep 2021 05:01:23 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id i25sm2028562oto.26.2021.09.24.05.01.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 05:01:22 -0700 (PDT) Sender: Guenter Roeck Date: Fri, 24 Sep 2021 05:01:20 -0700 From: Guenter Roeck To: Paul Fertser Cc: linux-hwmon@vger.kernel.org, Jean Delvare , linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 3/3] hwmon: tmp421: fix rounding for negative values Message-ID: <20210924120120.GA2695828@roeck-us.net> References: <20210924022020.GA3032273@roeck-us.net> <20210924093011.26083-1-fercerpav@gmail.com> <20210924093011.26083-3-fercerpav@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210924093011.26083-3-fercerpav@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 24, 2021 at 12:30:11PM +0300, Paul Fertser wrote: > 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 Applied. Thanks, Guenter > --- > > 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: > /*