Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3695054imu; Mon, 28 Jan 2019 09:11:10 -0800 (PST) X-Google-Smtp-Source: ALg8bN4lqUnOanvXvj58DvXPAoAgAKnD1mS++iGpfmgPUe9V13AySp0irOOgW4C8cYJKEzm6nn3N X-Received: by 2002:a17:902:bd46:: with SMTP id b6mr22456657plx.231.1548695469942; Mon, 28 Jan 2019 09:11:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548695469; cv=none; d=google.com; s=arc-20160816; b=N2Of0aTThh27NKp0Cot3Eu3ZfYPWAp9/ZLLfqzrFuguT0PleEScOAehK+PntIPRRoP XP0osjiEa5zobOFb7HiGooXZO+7tX34ricI+4gZ3QgKhR0Y+g74ylfF2popmLtHbHrZR ATIOCsT+/JCtVnEOL+ansM08Ewu5GhKsAgCxTRLI6g3IfQYO5SxR3w4B5mb5euD1nbz5 ynx+sD+81+k1hIyS14vHbXMFJhjJDxjG1d+1F1Wzl65nzFTJPkaXGppO5YY2BbH2D41b 8WUKk5RUWPx293GvbKjv9nL0g36topGf/Y36xbsTZUFRnAGmvOf8nmb77UAUzVswHHlE 4CFQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=V9gkGhApoZi7wj+UGiIGCvhlNOdb+yQQ2qxfZwyP0vw=; b=0YBxNcI4JqikX8OT0Hfc4afQcJ7uk44jcEo6WX3imRWgcVIbw9DuX/erLl7dR4mRBY WzboHS9vefP7HGijR9wjgymR/BexBfq+LGXHnMq3uIQZqJywpR5Zo7kMzQMGxh6ZQeJa 6Gotjjxslbbdr8p2Sgg0NgRjMjMvGBAA8LcSBM/r5DShLcYODnKqYx2DwDg/6eaVEzKg yVBhhhZxmk7c33LVqT8mJi6QieFZQ5kwYO+VcW+HrK2IydWCPvSqht2sfFdFS7BXU/5R 7Vmzkqlx9TOqTqlLEDmAqBsgLrixR0XMUYMLnk+WaZ3jNPwDtN6DucS7O6nbonE/kXRG mCew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=k2aEMrt0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y6si15425932pfi.228.2019.01.28.09.10.54; Mon, 28 Jan 2019 09:11:09 -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=k2aEMrt0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733248AbfA1RJ1 (ORCPT + 99 others); Mon, 28 Jan 2019 12:09:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:38308 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732392AbfA1QLW (ORCPT ); Mon, 28 Jan 2019 11:11:22 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B37A52147A; Mon, 28 Jan 2019 16:11:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548691881; bh=qDZbz7DAUnPmFklUjGLebSGJh3C4+5RwJy3JHN1ck8w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k2aEMrt0x+uJkFztOgJnYL90DYLy0/YxAFHRa9Frf68j/1duGSgn+2iw/ZM8D1ahf Xez0eyDFoCZwXLpudSKAlpFeV4nancpwM90ZLWyjmqLYLCuo5iLXSxMo53BSvBAFuu XvL9s/XZBCdbtKP0sWgfYncduS/x0n7pLLRhNg3Q= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Bjorn Andersson , Laxman Dewangan , Eduardo Valentin , Sasha Levin , linux-pm@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 247/258] thermal: generic-adc: Fix adc to temp interpolation Date: Mon, 28 Jan 2019 10:59:13 -0500 Message-Id: <20190128155924.51521-247-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190128155924.51521-1-sashal@kernel.org> References: <20190128155924.51521-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bjorn Andersson [ 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 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; -- 2.19.1