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: 10742053 X-Patchwork-Delegate: eduardo.valentin@ti.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 11BD06C2 for ; Mon, 24 Dec 2018 07:28:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF79728D4E for ; Mon, 24 Dec 2018 07:28:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E338228D7A; Mon, 24 Dec 2018 07:28:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B267A28D5D for ; Mon, 24 Dec 2018 07:28:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726686AbeLXH2K (ORCPT ); Mon, 24 Dec 2018 02:28:10 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:39003 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725984AbeLXH2K (ORCPT ); Mon, 24 Dec 2018 02:28:10 -0500 Received: by mail-pl1-f194.google.com with SMTP id 101so5296583pld.6 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=OwM7CEP9vlUOH5ZqaLYfYcFxrSAVdXBhse+RhfWRsEeoqZDDxB9eEQB77MH7+ztWQS vPENzPDztKnS/YZqKLT+IjNj5xj5E3oKS5/haHa11ymwCCjN7cZN2ST7LeU7DDX9gpkO EnRUm42RTzHAUh1Eqbt1YcNkwX25656+MFoWKKGVAUoW1gA2BM78g3Mt8Mt9a+vqAWEY 95DG/1P4tI00q5DlaCHiHnWb0Vz4eIOikk0h0078K9ic+mT66BbW1ouaH0HhDqS5xY22 HXmYKjhbkN6emJUhl1/XAkr1dHfLyK3zN1kPM0MJKh4neEI8BtiwaVwDSIokeADCb5H7 Umkw== X-Gm-Message-State: AJcUukfQrTu3E6VFhBsr7VLkoGIyyTDWVjO1S5NCHTbddfBLrjN+F3TL 8pNpsBANO3bNnH1clPdswA/cng== X-Google-Smtp-Source: ALg8bN4Sy8XoXqa80NbukSbjw7CjjQka/bf64I5tkIBmmsLoA7M2qR+3Zq6ZEnlH/Yz9WcjwwP5OCQ== 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-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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(-) 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;