From: mturquette@linaro.org (Mike Turquette)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] clk: divider: Use DIV_ROUND_CLOSEST
Date: Tue, 19 Mar 2013 17:16:09 -0700 [thread overview]
Message-ID: <20130320001609.8663.21043@quantum> (raw)
In-Reply-To: <2e77dfe8-8d4f-4b77-bf1b-831ad1572c23@VA3EHSMHS046.ehs.local>
Quoting Soren Brinkmann (2013-01-29 17:25:44)
> Use the DIV_ROUND_CLOSEST macro to calculate divider values and minimize
> rounding errors.
>
> Cc: linux-arm-kernel at lists.infradead.org
> Cc: linux-kernel at vger.kernel.org
> Signed-off-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
> ---
> 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.
>
Soren,
Thanks for the patch, and apologies for it getting lost for so long. I
think that your issue is more about policy. E.g. should we round the
divider up or round the divider down? The correct answer will vary from
platform to platform and the clk.h api doesn't specify how
clk_round_rate should round, other than it must specify a rate that the
clock can actually run at.
Unless everyone can agree that DIV_ROUND_CLOSEST gives them what they
want I'm more inclined to make this behavior a flag specific to struct
clk-divider. E.g. CLK_DIVIDER_ROUND_CLOSEST.
Care to take a crack at that approach?
Regards,
Mike
> 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))
> --
> 1.8.1.2
next prev parent reply other threads:[~2013-03-20 0:16 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-30 1:25 [PATCH] clk: divider: Use DIV_ROUND_CLOSEST Soren Brinkmann
2013-02-08 2:17 ` Sören Brinkmann
2013-03-20 0:16 ` Mike Turquette [this message]
2013-03-20 16:32 ` Sören Brinkmann
2013-03-20 18:50 ` Sascha Hauer
2013-03-21 9:15 ` Uwe Kleine-König
2013-03-26 22:45 ` Sören Brinkmann
2013-03-27 1:37 ` Mike Turquette
2013-04-01 23:24 ` Sören Brinkmann
2013-04-03 0:38 ` Mike Turquette
2013-03-21 16:36 ` Sören Brinkmann
2013-03-25 10:37 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130320001609.8663.21043@quantum \
--to=mturquette@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox