From patchwork Fri Aug 7 20:59:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 6973091 X-Patchwork-Delegate: eduardo.valentin@ti.com Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 60D299F373 for ; Fri, 7 Aug 2015 20:59:31 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 718CB20621 for ; Fri, 7 Aug 2015 20:59:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D6AAC20620 for ; Fri, 7 Aug 2015 20:59:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753897AbbHGU71 (ORCPT ); Fri, 7 Aug 2015 16:59:27 -0400 Received: from mail-pa0-f46.google.com ([209.85.220.46]:36688 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753805AbbHGU71 (ORCPT ); Fri, 7 Aug 2015 16:59:27 -0400 Received: by pacrr5 with SMTP id rr5so60029057pac.3 for ; Fri, 07 Aug 2015 13:59:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-type:content-disposition:user-agent; bh=ulPVYo2g+lMY3DqgKAZeZjufXv6kBbk/0W9STj6MrFs=; b=DatL0JEqoPcaMF80UrOkDQXBBLUABMF8T9oA2oGiT8V0NGuK5UTwvc2gguUkzqkmGU phVqn1hsfY9QH+QWm2jdRPdPxcrvMHZctBt3lLMs9kkwg8TTL/u5+/tu1PiVuszz+kzb KkQ6OgxELgpryGV4WkGTtBvZ2l/dq0Q8Tyv4AKmjeX09e9vYGXcXCGjAldUyA1Yj+GF7 wGnhkiDZC1ObsqZWmzRAVLEJ8ZGIj+wTyQRMzqkD3JZjrSfkuEHTovho8RJyMrUuVglI 7cpzEeCBFexn80sNsrl4y/pyHlh/SszLggD33c00og96HyduOSe5bIqCAJReeibZlyy9 mp9w== X-Gm-Message-State: ALoCoQmYu1U1rkKKkTj8Yd27DMWb6uicYi50rWKtmAMXjO7KblwFL5syElxBTH9NODilUDxtBAd8 X-Received: by 10.67.5.103 with SMTP id cl7mr18219779pad.75.1438981166535; Fri, 07 Aug 2015 13:59:26 -0700 (PDT) Received: from dtor-ws ([2620:0:1000:1301:fda8:35d:f03a:6572]) by smtp.gmail.com with ESMTPSA id xs13sm11183266pac.3.2015.08.07.13.59.25 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 07 Aug 2015 13:59:25 -0700 (PDT) Date: Fri, 7 Aug 2015 13:59:23 -0700 From: Dmitry Torokhov To: Heiko Stuebner Cc: Eduardo Valentin , Caesar Wang , dianders@chromium.org, linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] thermal: rockchip: fix handling of invalid readings Message-ID: <20150807205923.GA33949@dtor-ws> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 We attempted to signal invalid code by returning -EAGAIN from rk_tsadcv2_code_to_temp(), unfortunately the return value was stuffed directly into the temperature pointer, potentially confusing upper layers with temperature of -EINVAL. Let's split temperature from error/success indicator to avoid such confusion. Also change the way we scan the temperature table to start with the 2nd element so that we do not need to worry that we may reference out of bounds element while doing binary search and keep checking that we end up with 'mid' equal to 0 (since we are looking for the temperature that would fall into interval between the 'mid' and 'mid - 1') . Signed-off-by: Dmitry Torokhov Tested-by: Caesar Wang --- drivers/thermal/rockchip_thermal.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/drivers/thermal/rockchip_thermal.c b/drivers/thermal/rockchip_thermal.c index c89ffb2..93ee307 100644 --- a/drivers/thermal/rockchip_thermal.c +++ b/drivers/thermal/rockchip_thermal.c @@ -124,7 +124,7 @@ struct rockchip_thermal_data { #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */ struct tsadc_table { - unsigned long code; + u32 code; long temp; }; @@ -164,7 +164,6 @@ static const struct tsadc_table v2_code_table[] = { {3452, 115000}, {3437, 120000}, {3421, 125000}, - {0, 125000}, }; static u32 rk_tsadcv2_temp_to_code(long temp) @@ -191,19 +190,21 @@ static u32 rk_tsadcv2_temp_to_code(long temp) return 0; } -static int rk_tsadcv2_code_to_temp(u32 code) +static int rk_tsadcv2_code_to_temp(u32 code, int *temp) { - unsigned int low = 0; + unsigned int low = 1; unsigned int high = ARRAY_SIZE(v2_code_table) - 1; unsigned int mid = (low + high) / 2; unsigned int num; unsigned long denom; - /* Invalid code, return -EAGAIN */ - if (code > TSADCV2_DATA_MASK) - return -EAGAIN; + BUILD_BUG_ON(ARRAY_SIZE(v2_code_table) < 2); - while (low <= high && mid) { + code &= TSADCV2_DATA_MASK; + if (code < v2_code_table[high].code) + return -EAGAIN; /* Incorrect reading */ + + while (low <= high) { if (code >= v2_code_table[mid].code && code < v2_code_table[mid - 1].code) break; @@ -223,7 +224,9 @@ static int rk_tsadcv2_code_to_temp(u32 code) 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); + *temp = v2_code_table[mid - 1].temp + (num / denom); + + return 0; } /** @@ -281,14 +284,9 @@ static int rk_tsadcv2_get_temp(int chn, void __iomem *regs, int *temp) { u32 val; - /* the A/D value of the channel last conversion need some time */ val = readl_relaxed(regs + TSADCV2_DATA(chn)); - if (val == 0) - return -EAGAIN; - - *temp = rk_tsadcv2_code_to_temp(val); - return 0; + return rk_tsadcv2_code_to_temp(val, temp); } static void rk_tsadcv2_tshut_temp(int chn, void __iomem *regs, long temp)