Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2715628imj; Mon, 11 Feb 2019 07:24:53 -0800 (PST) X-Google-Smtp-Source: AHgI3IaQdLDImO6ybuydevDJdEAQIqXQm7K63PeBkBv1WWi/6BwAWWoIj+5lJQL3Z43X3Lk0Iqtn X-Received: by 2002:a63:2209:: with SMTP id i9mr34182678pgi.325.1549898693532; Mon, 11 Feb 2019 07:24:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549898693; cv=none; d=google.com; s=arc-20160816; b=uK5pb6fxzWS1hFoJQHcATXI9XuyCrVbUsvnfxUa1lyaNyf0iAbhHTxIjPcC8aywWj9 970MOvtFKf1HdyhUgko9+s3wjwOWRkYqk3Lq9XVe5hot9kQ6R8fYB+5UA/IAur4LtdDR ChmjtNZtz+RfjsytbhTYFoxozjGEVa77DJ4tpRUY4g0tzUb23NhjC0odYdXGQl6V8e2G 47zoaIAkdIWRGgbRVMQUeSaoXCF7VLrd3ED0FRoThpP3+/6YQO+EG7QwIkeAf+YZHBdO unSjMuQic4zmm/kI/9Wt0WafItzCzkopA7/+uJ6xI13rOFu4dUvcjBvsjENxAp6t6cMp 8Orw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Syjm1U52n5Ycl8PD0WnEoUu27eM3NnRckbzuUji9IlY=; b=QvMw1Aa5iXoIT71SlTQAKNUHiTUVCEqMOqNOqK+lzReN5UKIreIvTtk3n7eVM1Avcs gFqUrqbgtVJPea8CsL6lBrCm+maZrd+OhOCNrFvJuMTsPijyRfMdUhd/qvF+rFsuquS/ M5/SBFh5pZOoNPMyQQaQ56+RpgMymkAgY8LZWsPPPVERDuVrMjSxCfWujLhK4qrnSq3e 798Ohn4RV4ybz+9nhbhA7rY+fr//kyf4k8sTBt50+NBgyrs5vWPIF6RB6gB2zOgPsDgi iPTIYiX6eH2x0NBIT/yRmVwRLydAXZ8RYP32mmNqtkH2DN+Bn8RFVaXsbxnAmOmnI5jo +flA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Fh7mQh1F; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 4si4433362pff.161.2019.02.11.07.24.37; Mon, 11 Feb 2019 07:24:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Fh7mQh1F; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391005AbfBKPXp (ORCPT + 99 others); Mon, 11 Feb 2019 10:23:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:51506 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390564AbfBKPDJ (ORCPT ); Mon, 11 Feb 2019 10:03:09 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A67A3222A7; Mon, 11 Feb 2019 15:03:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549897389; bh=35LdIzx/7qy+0HKIZPDWAiTxYCFCQmatXMhVQ6w5Kxk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fh7mQh1FB+mj3eFQmO1guIFhs4+Xb1lnKCg0OVVdryzrgBOOSbXTHCGngE4M2ojVh XBJp2MOj63NIzki6k8do7wrVLd4RqALKOg0gbPyFPXO3NRYhrvGgn6a8FAA4T7rrEM sHhQNOrTKpvs8YzWic5M62D3FqjBloU8Q113ER1k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Laxman Dewangan , Bjorn Andersson , Eduardo Valentin , Sasha Levin Subject: [PATCH 4.14 156/205] thermal: generic-adc: Fix adc to temp interpolation Date: Mon, 11 Feb 2019 15:19:14 +0100 Message-Id: <20190211141838.565396292@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141827.214852402@linuxfoundation.org> References: <20190211141827.214852402@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 9d216211fded20fff301d0317af3238d8383634c ] First correct the edge case to return the last element if we're outside the range, rather than at the last element, so that interpolation is not omitted for points between the two last entries in the table. Then correct the formula to perform linear interpolation based the two points surrounding the read ADC value. The indices for temp are kept as "hi" and "lo" to pair with the adc indices, but there's no requirement that the temperature is provided in descendent order. mult_frac() is used to prevent issues with overflowing the int. Cc: Laxman Dewangan Signed-off-by: Bjorn Andersson Signed-off-by: Eduardo Valentin Signed-off-by: Sasha Levin --- drivers/thermal/thermal-generic-adc.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/thermal/thermal-generic-adc.c b/drivers/thermal/thermal-generic-adc.c index 73f55d6a1721..ad601e5b4175 100644 --- a/drivers/thermal/thermal-generic-adc.c +++ b/drivers/thermal/thermal-generic-adc.c @@ -26,7 +26,7 @@ struct gadc_thermal_info { static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val) { - int temp, adc_hi, adc_lo; + int temp, temp_hi, temp_lo, adc_hi, adc_lo; int i; for (i = 0; i < gti->nlookup_table; i++) { @@ -36,13 +36,17 @@ static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val) if (i == 0) { temp = gti->lookup_table[0]; - } else if (i >= (gti->nlookup_table - 1)) { + } else if (i >= gti->nlookup_table) { temp = gti->lookup_table[2 * (gti->nlookup_table - 1)]; } else { adc_hi = gti->lookup_table[2 * i - 1]; adc_lo = gti->lookup_table[2 * i + 1]; - temp = gti->lookup_table[2 * i]; - temp -= ((val - adc_lo) * 1000) / (adc_hi - adc_lo); + + temp_hi = gti->lookup_table[2 * i - 2]; + temp_lo = gti->lookup_table[2 * i]; + + temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi, + adc_lo - adc_hi); } return temp; -- 2.19.1