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: 6973111 Return-Path: X-Original-To: patchwork-linux-rockchip@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 78F189F373 for ; Fri, 7 Aug 2015 21:00:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 91D702061B for ; Fri, 7 Aug 2015 21:00:00 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AA69020613 for ; Fri, 7 Aug 2015 20:59:59 +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 1ZNokA-0002Yr-TM; Fri, 07 Aug 2015 20:59:58 +0000 Received: from mail-pa0-f42.google.com ([209.85.220.42]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZNojz-0002TH-W4 for linux-rockchip@lists.infradead.org; Fri, 07 Aug 2015 20:59:48 +0000 Received: by pabxd6 with SMTP id xd6so77366653pab.2 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=VvQsER/+XlKOixNGbY6uMquIgLeryu+kD6N84bwKoQwNrQds27yaNtB6V+ESsIORfm KOeUQvAYDwupEOnHObq/rviSoikTwfpvEXiuw4dITRbSrAwZcKtqZfDekfyzfV/Zskhs JDVAM3tesn/CEIToXlUDjNLnEcAkvoe7/26fx/y0T8QkK0Cr/YvSw9repH9kKrtqSl6p FyINRieaR/0PRK88b7Ud8mDM1BpjHTuO3ZxPSWmq5j9TgLfSm55ITfVbOUwBp1GjDq2d vJhp8TCmlSkRaFF3ncsXIfrt/Rt6ZjI0uYthk2X3rkxXAJXt5cXUpEBrmRoPYvqvQU32 RHLg== X-Gm-Message-State: ALoCoQkiw/sUZdc0OZc3OxOTHAigeKAR02ahcLMWAwXr/jpTeOWw7yBpeDL90zpbKb6uz+C3YZWa 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 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) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150807_135948_090343_824DE8F7 X-CRM114-Status: GOOD ( 16.65 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-pm@vger.kernel.org, dianders@chromium.org, linux-kernel@vger.kernel.org, Eduardo Valentin , linux-rockchip@lists.infradead.org, linux-arm-kernel@lists.infradead.org, Caesar Wang Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+patchwork-linux-rockchip=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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)