Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp490045pxb; Wed, 22 Sep 2021 06:45:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkRZocW+PDKu4nq8X4zqtjQC897Q70jIask8e5TsUELxdZiYTqGsdbGqHzDbaKq1ZhfRof X-Received: by 2002:aa7:df0a:: with SMTP id c10mr42662006edy.367.1632318300241; Wed, 22 Sep 2021 06:45:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632318300; cv=none; d=google.com; s=arc-20160816; b=krF80yoVBKOOWfr/TmMQOFFYFvmsAqr283a1PyHgERuGwbfhBxOaE0hhE5s/PUVYGe tsCXVr9nc1nyhspj6J1DNA3BcCLvyJqLc5bEFacpr53lqcQVy3Z5IIh1F1w81MODKcrG S5483ZTsepshDDBuYiq4QcK+FJ6PVFD0+3YtFyF+nK0yg0+AKVHji3+AqDyD0RQgVerV 4rkTOYQFFgX5HD96V/8QNJIRrALYGsvMVYslaWPGCgOgC0WmBhFTbRJfTg1VCc6Yucth QlAmiJfHcJv4Q58twl0X2ySPg2AzzB7Etyj6PJ70oPli5XSH0CGKKGw8pbgyj+D4+XUI LelA== 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=lOF37ErIrt4QCm9hfD32diBPJ9qYvrZDIm2Bmis27/E=; b=TgyqyHiZ0x80LMLHKmDNLR0CJvjwyggu8gyQvoncREGxEc3tKqgqedRKZNoei8Ewch 8n9lRIV/oDlqC0DSepSJRnIYa+1IU4P5osuJLK/k5lDIyJibMpfRvabrai2eTn2fvHsJ ZVw5W+P7mbLWr2xKN5v+ktpp4AmBLXWKIWGbP+FcSH5OsouHoqCp3wblZw6QXAH0k8SK uYJmz6AUIWlll4C8s8yULMgxbx2iwe/HK75vm682p81ffN80IMcAyGyUJXVeasN9EOoT /RgzvDBra68RA5GzGkSo8MR2NrsEo9xZNZw4GrbykFTzdGb1TAevBpXZ698qWGGm1rVC KCvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=REMfwssb; 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 w10si3427594ejv.754.2021.09.22.06.44.33; Wed, 22 Sep 2021 06:45:00 -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=REMfwssb; 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 S232777AbhIVNo0 (ORCPT + 99 others); Wed, 22 Sep 2021 09:44:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233003AbhIVNoX (ORCPT ); Wed, 22 Sep 2021 09:44:23 -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 82A31C061756; Wed, 22 Sep 2021 06:42:53 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id e15so11999536lfr.10; Wed, 22 Sep 2021 06:42:53 -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=lOF37ErIrt4QCm9hfD32diBPJ9qYvrZDIm2Bmis27/E=; b=REMfwssb9gkyNN1zQ063pdfB/iO5RBC6SxWxl5yWT8m2NGso+nVADmTJFQkvl1BB26 dyTNEApex+x7KZZ6JzOENT1d+mkRVTcDqC6oCZonKNdtKUKotQxW7Te3C4q3Wm9VrZXa u3OKY9+uAe5Y88jqUrgLkDKaMS+nacUhuV68glw0LaUOQyxrgCD0df5Sv6QUEYNn8D9a z7P5Qkjx4MVIJBreEj6zTauE/JerFvKR6/efBPccLImCJqQRmR+9oia4maiyGsl8s0sA RMUqNf+EmuNe1G/ch5akAp2oaD0wlczxYxRZ62jp+VTdRVwbBrcqs0Uw7bNMW6J25fVZ enVA== 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=lOF37ErIrt4QCm9hfD32diBPJ9qYvrZDIm2Bmis27/E=; b=S+AXnZfEQBiyIE5qlryuiur9tgFe90e7hWR1sFEscc84zbTlzVuehCOVsbHkOstGzt 8OAgKHxYWPmiJ+gf3UQe7eAphZFeMYQWpHh8T2oqIZw595lbZeGezetqBPfzJaaIQwhn dfr6HyoaHvq4okMZazN+SowYzMqetUYiBdgU5KFluVVkXIOEQSzgInXZxdzo6yQhQ4qs y3If/QrwwM3qguNmnqSiJmXHCtp1wP9tR72Z56RsTBF9ujokzpAzOf9e4R+zuB6e1G3m efGk6ckpIfCqeG/sFN/pXyZlXcKzoyr+BdrXzZLcuoM7qcEZU5ZW9WijKxEFSpuAKe3A NF0A== X-Gm-Message-State: AOAM532ew8+FG/XtfECnj7gHJsQ5GbJvybtJ1eXzf8W+DC77kCXnTZRi cTl6L8Fm+eSlbx1QIpF/lnuDxuOwX7Q= X-Received: by 2002:a05:651c:3c3:: with SMTP id f3mr27966863ljp.222.1632318142221; Wed, 22 Sep 2021 06:42:22 -0700 (PDT) Received: from home.paul.comp (paulfertser.info. [2001:470:26:54b:226:9eff:fe70:80c2]) by smtp.gmail.com with ESMTPSA id y13sm182538lfs.17.2021.09.22.06.42.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 06:42:21 -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 18MDgJ6j020815; Wed, 22 Sep 2021 16:42:20 +0300 Received: (from paul@localhost) by home.paul.comp (8.15.2/8.15.2/Submit) id 18MDgJSW020814; Wed, 22 Sep 2021 16:42:19 +0300 From: Paul Fertser To: linux-hwmon@vger.kernel.org Cc: Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, Paul Fertser Subject: [PATCH 3/3] hwmon: tmp421: fix rounding for negative values Date: Wed, 22 Sep 2021 16:41:54 +0300 Message-Id: <20210922134154.20766-3-fercerpav@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210922134154.20766-1-fercerpav@gmail.com> References: <20210922134154.20766-1-fercerpav@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Current 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. Signed-off-by: Paul Fertser --- 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 9ee4c61531f3..dec3aced3d8b 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 struct tmp421_data *tmp421_update_device(struct device *dev) @@ -167,10 +161,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