From patchwork Mon Dec 24 07:26:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bjorn Andersson X-Patchwork-Id: 154463 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp3612964ljp; Sun, 23 Dec 2018 23:28:13 -0800 (PST) X-Google-Smtp-Source: ALg8bN4iWamiXykb2PXi1ZeJegocg5U6fUQ/NXHKV5crGsxJhulmLnTZBCFShJNEfhQ6jCjoSMVm X-Received: by 2002:a17:902:7c82:: with SMTP id y2mr11896918pll.33.1545636493011; Sun, 23 Dec 2018 23:28:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545636493; cv=none; d=google.com; s=arc-20160816; b=pjWjM3NAUcQwUikueLjhpofbMDHAq3pQ0QuoGD/bjG52btFuZ9g2n84QUixNF/s3bO 69Ldd/ZUkx2R7XAR0zNTdRjp7UXmAyW4+g5rR3pK0Rxx5rQxhpRt1lPWIa0KZ5uZ7b70 6pgj8ZjpJRBtQ7oCrKD0NVMbRQot8MkW9H/kFedteXf3EmVNqnHGtJ08LX/SW/9a/Hj5 TxukDDklsroR3wMD/8AN34pJB3u0EY8EifUCBx9lTZtxCnFZFEtDTtW8jLZht84w/FkQ U/W9zlAyJjH81qxY2fwEZ9GmPkAU096RAvuhSPltyW5XxcqTaI1bbdRy//1nkYXSitlj IoXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=b5cQe/stL77QEOSniNkNAVaDWYM99u9bRka0mV9VTPU=; b=xNFQkHpNsPIg5IYrdMRYpF8fqwnQ1Qda4s0jxnBTWZE3dy/WU/9cjRFhKC0tuOibVG I7O7RxAgQpDLcp1j26zbSCbehXn/HY3SmXCLNl/375JxXBeazgUM1D5vAguBeAhIVWej SYIVqjg5f8e0CJBVyIKFovjMYSBmgG3xuJkK4XkpWo71Ht9CxwzQBBIpH3GCAXM3nM98 I+/Ia7PgIeTCLIXWHPd4ks5R6YrqpkINBqvxBfZEekKObzc4r/N1IQl6K9MDAzdO2csw 3aHW4Y2PT9qTnGZEgwkVJs1jRN+HxVIcIUc4AdD3QLVxnIjYowsMkFU0g4j1uZUVDSS9 mt+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Be3ImDAy; spf=pass (google.com: best guess record for domain of linux-arm-msm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-arm-msm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z5si26920098pgh.469.2018.12.23.23.28.12; Sun, 23 Dec 2018 23:28:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-arm-msm-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=@linaro.org header.s=google header.b=Be3ImDAy; spf=pass (google.com: best guess record for domain of linux-arm-msm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-arm-msm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726663AbeLXH2K (ORCPT + 15 others); Mon, 24 Dec 2018 02:28:10 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:38645 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726651AbeLXH2K (ORCPT ); Mon, 24 Dec 2018 02:28:10 -0500 Received: by mail-pl1-f196.google.com with SMTP id e5so5305897plb.5 for ; Sun, 23 Dec 2018 23:28:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=b5cQe/stL77QEOSniNkNAVaDWYM99u9bRka0mV9VTPU=; b=Be3ImDAyAZ4bzW8VEZeH1pW/86ufVV0xyQjXpXqFwqWwJUI9fTTTb/3PyVTSjyQWEM KCMfT50lxGhluaH6ad8TyF1C2qMa7n9foXTd2gTk3oOGGhvWAWdalT5l9BqrBgOSwuNC vnU29J6S9umX2lH1G9DtToFFtidXSVeFg97rc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=b5cQe/stL77QEOSniNkNAVaDWYM99u9bRka0mV9VTPU=; b=UJwEfrCh/xdNlTZI8JuILM717AcHq++PW0cV6E0FoYSVao5lFXtuSDrT3vNMj6Nu4P kjtall0WxCnc5k96kZ42Iy3rFXmn2q8oZcNmbdy7xX4qE0cKQ0YpuzHm73Y9mrs9qo+M KSXwlvVIbjLu0DYaHjZViwgQahy8sKjANtrTUgS+IuflAxYsV9DggaiqSxiGSNzinupD VdAEJS28z+Mmn065WdCn1r8MtVsx+3OxtEgL46wemUelfF2reRarpBcm0RZBJBgdlmSJ srD8HBaQtgiexiaSr5fitXYcZfgpVa+qP2WNTvSEXf53aJojEGbUS2vfneS60qizoV2O s3rQ== X-Gm-Message-State: AJcUukcSNWdsKLDsQLgoOMh3kQaYUJAae+ND17mgS68BhfYPpPWgwF4q ii+yBz5Zujjs3c46qZTnl8juDw== X-Received: by 2002:a17:902:22f:: with SMTP id 44mr12012427plc.137.1545636489785; Sun, 23 Dec 2018 23:28:09 -0800 (PST) Received: from localhost.localdomain (104-188-17-28.lightspeed.sndgca.sbcglobal.net. [104.188.17.28]) by smtp.gmail.com with ESMTPSA id j197sm45534640pgc.76.2018.12.23.23.28.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 23 Dec 2018 23:28:08 -0800 (PST) From: Bjorn Andersson To: Zhang Rui , Eduardo Valentin , Daniel Lezcano Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Laxman Dewangan Subject: [PATCH] thermal: generic-adc: Fix adc to temp interpolation Date: Sun, 23 Dec 2018 23:26:44 -0800 Message-Id: <20181224072644.30096-1-bjorn.andersson@linaro.org> X-Mailer: git-send-email 2.18.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org 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 --- drivers/thermal/thermal-generic-adc.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) -- 2.18.0 diff --git a/drivers/thermal/thermal-generic-adc.c b/drivers/thermal/thermal-generic-adc.c index bf1c628d4a7a..e22fc60ad36d 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;