From patchwork Thu Jan 22 16:01:28 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caesar Wang X-Patchwork-Id: 5686801 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E2A65C058D for ; Thu, 22 Jan 2015 16:58:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0ED2F20265 for ; Thu, 22 Jan 2015 16:58:46 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 27A28200DC for ; Thu, 22 Jan 2015 16:58:45 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YEL3k-0004ST-UL; Thu, 22 Jan 2015 16:56:44 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YEL3L-0004CO-6m; Thu, 22 Jan 2015 16:56:19 +0000 Received: from regular1.263xmail.com ([211.150.99.131]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YEKDq-0000kA-K4; Thu, 22 Jan 2015 16:03:08 +0000 Received: from caesar.wang?rock-chips.com (unknown [192.168.167.157]) by regular1.263xmail.com (Postfix) with SMTP id 7ACEC328B; Fri, 23 Jan 2015 00:02:31 +0800 (CST) X-263anti-spam: KSV:0; X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 X-KSVirus-check: 0 X-ABS-CHECKED: 4 X-ADDR-CHECKED: 0 Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.263.net (Postfix) with ESMTP id 757721A8BB; Fri, 23 Jan 2015 00:02:14 +0800 (CST) X-RL-SENDER: caesar.wang@rock-chips.com X-SENDER-IP: 172.245.48.25 X-LOGIN-NAME: caesar.wang@rock-chips.com X-UNIQUE-TAG: <6dcf5a6adfd29f9c88c14fa2699a30ce> X-ATTACHMENT-NUM: 0 X-SENDER: wxt@rock-chips.com X-DNS-TYPE: 1 Received: from unknown (host.colocrossing.com [172.245.48.25]) by smtp.263.net (Postfix) whith SMTP id 19287RW9G49; Fri, 23 Jan 2015 00:02:22 +0800 (CST) From: Caesar Wang To: Heiko Stuebner , Zhang Rui , Eduardo Valentin Subject: [PATCH v4] thermal: rockchip: make temperature reporting much more accurate Date: Fri, 23 Jan 2015 00:01:28 +0800 Message-Id: <1421942488-13764-2-git-send-email-wxt@rock-chips.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1421942488-13764-1-git-send-email-wxt@rock-chips.com> References: <1421942488-13764-1-git-send-email-wxt@rock-chips.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150122_110307_148155_65C67BA4 X-CRM114-Status: UNSURE ( 9.45 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -1.9 (-) Cc: linux-pm@vger.kernel.org, Dmitry Torokhov , linux-kernel@vger.kernel.org, Daniel Kurtz , linux-rockchip@lists.infradead.org, linux-arm-kernel@lists.infradead.org, Caesar Wang X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In general, the kernel should report temperature readings exactly as reported by the hardware. The cpu / gpu thermal driver works in 5 degree increments,but we ought to do more accurate. The temperature will do linear interpolation between the entries in the table. Test= $md5sum /dev/zero & $while true; do grep "" /sys/class/thermal/thermal_zone[1-2]/temp; sleep .5; done e.g. We can get the result as follows: /sys/class/thermal/thermal_zone1/temp:39994 /sys/class/thermal/thermal_zone2/temp:39086 /sys/class/thermal/thermal_zone1/temp:39994 /sys/class/thermal/thermal_zone2/temp:39540 /sys/class/thermal/thermal_zone1/temp:39540 /sys/class/thermal/thermal_zone2/temp:39540 /sys/class/thermal/thermal_zone1/temp:39540 /sys/class/thermal/thermal_zone2/temp:39994 Signed-off-by: Caesar Wang Reviewed-by: Dmitry Torokhov --- Changes in v4: "return -EAGAIN" instead of "return rk_tsadcv2_code_to_temp(code)" Changes in v3: Suggested-by Daniel Kurtz, the check doesn't reject "code == 0xfff" Fixed in rk_tsadcv2_code_to_temp(u32 code) Changes in v2: Reviewed-by: Dmitry Torokhov drivers/thermal/rockchip_thermal.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/drivers/thermal/rockchip_thermal.c b/drivers/thermal/rockchip_thermal.c index 1bcddfc..6267349 100644 --- a/drivers/thermal/rockchip_thermal.c +++ b/drivers/thermal/rockchip_thermal.c @@ -193,19 +193,22 @@ static u32 rk_tsadcv2_temp_to_code(long temp) static long rk_tsadcv2_code_to_temp(u32 code) { - int high, low, mid; - - low = 0; - high = ARRAY_SIZE(v2_code_table) - 1; - mid = (high + low) / 2; + unsigned int low = 0; + unsigned int high = ARRAY_SIZE(v2_code_table) - 1; + unsigned int mid = (low + high) / 2; + unsigned int num; + unsigned long denom; + /* No code available, return -EAGAIN */ if (code > v2_code_table[low].code || code < v2_code_table[high].code) - return 125000; /* No code available, return max temperature */ + return -EAGAIN; while (low <= high) { - if (code >= v2_code_table[mid].code && code < - v2_code_table[mid - 1].code) - return v2_code_table[mid].temp; + if (code >= v2_code_table[mid].code && + code < v2_code_table[mid - 1].code) + break; + else if (code == TSADCV2_DATA_MASK) + break; else if (code < v2_code_table[mid].code) low = mid + 1; else @@ -213,7 +216,16 @@ static long rk_tsadcv2_code_to_temp(u32 code) mid = (low + high) / 2; } - return 125000; + /* + * The 5C granularity provided by the table is too much. Let's + * assume that the relationship between sensor readings and + * temperature between 2 table entries is linear and interpolate + * to produce less granular result. + */ + num = v2_code_table[mid].temp - v2_code_table[mid - 1].temp; + num *= v2_code_table[mid - 1].code - code; + denom = v2_code_table[mid - 1].code - v2_code_table[mid].code; + return v2_code_table[mid - 1].temp + (num / denom); } /**