From patchwork Wed Jan 30 01:25:44 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Soren Brinkmann X-Patchwork-Id: 2064571 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id 9E8B23FDD1 for ; Wed, 30 Jan 2013 01:28:42 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1U0MRB-0003Ul-5S; Wed, 30 Jan 2013 01:26:05 +0000 Received: from va3ehsobe004.messaging.microsoft.com ([216.32.180.14] helo=va3outboundpool.messaging.microsoft.com) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1U0MR7-0003Tx-Jn for linux-arm-kernel@lists.infradead.org; Wed, 30 Jan 2013 01:26:02 +0000 Received: from mail87-va3-R.bigfish.com (10.7.14.251) by VA3EHSOBE001.bigfish.com (10.7.40.21) with Microsoft SMTP Server id 14.1.225.23; Wed, 30 Jan 2013 01:25:54 +0000 Received: from mail87-va3 (localhost [127.0.0.1]) by mail87-va3-R.bigfish.com (Postfix) with ESMTP id 4A1E63400F3; Wed, 30 Jan 2013 01:25:54 +0000 (UTC) X-Forefront-Antispam-Report: CIP:149.199.60.83; KIP:(null); UIP:(null); IPV:NLI; H:xsj-gw1; RD:unknown-60-83.xilinx.com; EFVD:NLI X-SpamScore: 1 X-BigFish: VPS1(zzc89bhzz1ee6h1de0h1202h1e76h1d1ah1d2ahzz8275bh8275dhz2fh95h668h839h93fhd24hf0ah119dh1288h12a5h12a9h12bdh137ah139eh13b6h1441h14ddh1504h1537h162dh1631h1758h1898h18e1h1946h906i1155h) Received-SPF: pass (mail87-va3: domain of xilinx.com designates 149.199.60.83 as permitted sender) client-ip=149.199.60.83; envelope-from=soren.brinkmann@xilinx.com; helo=xsj-gw1 ; helo=xsj-gw1 ; Received: from mail87-va3 (localhost.localdomain [127.0.0.1]) by mail87-va3 (MessageSwitch) id 1359509152625244_6597; Wed, 30 Jan 2013 01:25:52 +0000 (UTC) Received: from VA3EHSMHS046.bigfish.com (unknown [10.7.14.239]) by mail87-va3.bigfish.com (Postfix) with ESMTP id 92F4630004E; Wed, 30 Jan 2013 01:25:52 +0000 (UTC) Received: from xsj-gw1 (149.199.60.83) by VA3EHSMHS046.bigfish.com (10.7.99.56) with Microsoft SMTP Server id 14.1.225.23; Wed, 30 Jan 2013 01:25:49 +0000 Received: from unknown-38-66.xilinx.com ([149.199.38.66] helo=xsj-smtp1.xilinx.com) by xsj-gw1 with esmtp (Exim 4.63) (envelope-from ) id 1U0MQv-0004Gk-Jj; Tue, 29 Jan 2013 17:25:49 -0800 From: Soren Brinkmann To: Subject: [PATCH] clk: divider: Use DIV_ROUND_CLOSEST Date: Tue, 29 Jan 2013 17:25:44 -0800 X-Mailer: git-send-email 1.8.1.2 MIME-Version: 1.0 X-OriginalArrivalTime: 30 Jan 2013 01:25:48.0342 (UTC) FILETIME=[BBCD6560:01CDFE88] X-RCIS-Action: ALLOW Message-ID: <2e77dfe8-8d4f-4b77-bf1b-831ad1572c23@VA3EHSMHS046.ehs.local> X-OriginatorOrg: xilinx.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130129_202601_745305_60FB4F51 X-CRM114-Status: GOOD ( 11.73 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [216.32.180.14 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: linux-arm-kernel@lists.infradead.org, Soren Brinkmann , linux-kernel@vger.kernel.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Use the DIV_ROUND_CLOSEST macro to calculate divider values and minimize rounding errors. Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Soren Brinkmann --- Hi, I just came across this behavior: I'm using the clk-divider as cpuclock which can be scaled through cpufreq. During boot I create an opp table which in this case holds the frequencies [MHz] 666, 333 and 222. To make sure those are actually valid frequencies I call clk_round_rate(). I added a debug line in clk-divider.c:clk_divider_bestdiv() before the return in line 163 giving me: prate:1333333320, rate:333333330, div:4 for adding the 333 MHz operating point. In the running system this gives me: zynq:/sys/devices/system/cpu/cpu0/cpufreq # cat scaling_available_frequencies 222222 333333 666666 zynq:/sys/devices/system/cpu/cpu0/cpufreq # cat scaling_cur_freq 666666 So far, so good. But now, let's scale the frequency: zynq:/sys/devices/system/cpu/cpu0/cpufreq # echo 333333 > scaling_setspeed zynq:/sys/devices/system/cpu/cpu0/cpufreq # cat scaling_cur_freq 266666 And the corresponding debug line: prate:1333333320, rate:333333000, div:5 So, with DIV_ROUND_UP an actual divider of 4.00000396 becomes 5, resulting in a huge error. Regards, Sören drivers/clk/clk-divider.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c index a9204c6..32e1b6a 100644 --- a/drivers/clk/clk-divider.c +++ b/drivers/clk/clk-divider.c @@ -157,7 +157,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate, if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) { parent_rate = *best_parent_rate; - bestdiv = DIV_ROUND_UP(parent_rate, rate); + bestdiv = DIV_ROUND_CLOSEST(parent_rate, rate); bestdiv = bestdiv == 0 ? 1 : bestdiv; bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; return bestdiv; @@ -207,7 +207,7 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long flags = 0; u32 val; - div = parent_rate / rate; + div = DIV_ROUND_CLOSEST(parent_rate, rate); value = _get_val(divider, div); if (value > div_mask(divider))