linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: u.kleine-koenig@pengutronix.de (Uwe Kleine-König)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFC PATCH 2/5] clk: Introduce 'clk_round_rate_nearest()'
Date: Thu, 15 May 2014 09:38:16 +0200	[thread overview]
Message-ID: <20140515073816.GI16662@pengutronix.de> (raw)
In-Reply-To: <1400106655-22465-3-git-send-email-soren.brinkmann@xilinx.com>

Hello,

it's great you pick that up.

On Wed, May 14, 2014 at 03:30:52PM -0700, Soren Brinkmann wrote:
> Introduce a new API function to round a rate to the closest possible
> rate the HW clock can generate.
> In contrast to 'clk_round_rate()' which works similar, but always returns
> a frequency <= its input rate.
> 
> The code comes from Uwe and was copied from this LKML thread:
> https://lkml.org/lkml/2013/3/21/115
> 
> Signed-off-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
> ---
> 
>  drivers/clk/clk.c   | 26 ++++++++++++++++++++++++--
>  include/linux/clk.h | 14 ++++++++++++--
>  2 files changed, 36 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
> index dff0373f53c1..b715f5a9826c 100644
> --- a/drivers/clk/clk.c
> +++ b/drivers/clk/clk.c
> @@ -1011,8 +1011,9 @@ unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
>   * @rate: the rate which is to be rounded
>   *
>   * Takes in a rate as input and rounds it to a rate that the clk can actually
> - * use which is then returned.  If clk doesn't support round_rate operation
> - * then the parent rate is returned.
> + * use and does not exceed the requested frequency, which is then returned.
> + * If clk doesn't support round_rate operation then the parent rate
> + * is returned.
>   */
>  long clk_round_rate(struct clk *clk, unsigned long rate)
>  {
> @@ -1027,6 +1028,27 @@ long clk_round_rate(struct clk *clk, unsigned long rate)
>  EXPORT_SYMBOL_GPL(clk_round_rate);
>  
>  /**
> + * clk_round_rate_nearest - round the given rate for a clk
> + * @clk: the clk for which we are rounding a rate
> + * @rate: the rate which is to be rounded
> + *
> + * Takes in a rate as input and rounds it to the closest rate that the clk
> + * can actually use which is then returned. If clk doesn't support
> + * round_rate operation then the parent rate is returned.
> + */
> +long clk_round_rate_nearest(struct clk *clk, unsigned long rate)
> +{
> +	long lower_limit = clk_round_rate(clk, rate);
> +	long upper_limit = clk_round_rate(clk, rate + (rate - lower_limit));
> +
> +	if (rate - lower_limit < upper_limit - rate)
> +		return lower_limit;
> +	else
> +		return upper_limit;
I wanted to suggest to add some comment to describe why the calculation
works here. While trying to proove it, I noticed that this
implementation is buggy.
Consider a clock that can provide the following frequencies: 38000,
38401, 38600.

	clk_round_rate_nearest(clk, 38400)
	  lower_limit = clk_round_rate(clk, 38400) -> 38000
	  upper_limit = clk_round_rate(clk, 38800) -> 38600

	  return 38600

but 38401 would have been the better/correct answer. I think you cannot
implement clk_round_rate_nearest without iteration if you don't want to
add specific logic to the clock providers.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

  reply	other threads:[~2014-05-15  7:38 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-14 22:30 [RFC PATCH 0/5] Frequency resolution in CCF vs. cpufreq Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 1/5] cpufreq: stats: Allow small rounding errors Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 2/5] clk: Introduce 'clk_round_rate_nearest()' Soren Brinkmann
2014-05-15  7:38   ` Uwe Kleine-König [this message]
2014-05-15 14:10     ` Sören Brinkmann
2014-05-19  0:51     ` Sören Brinkmann
2014-05-19 16:19       ` Uwe Kleine-König
2014-05-19 16:41         ` Sören Brinkmann
2014-05-19 17:29           ` Sören Brinkmann
2014-05-20  7:51             ` Uwe Kleine-König
2014-05-20  7:33           ` Uwe Kleine-König
2014-05-20 16:01             ` Sören Brinkmann
2014-05-20 17:48               ` Stephen Boyd
2014-05-20 21:48                 ` Sören Brinkmann
2014-05-21  7:34                   ` Uwe Kleine-König
2014-05-21 15:58                     ` Sören Brinkmann
2014-05-21 18:23                       ` Uwe Kleine-König
2014-05-21 20:19                         ` Sören Brinkmann
2014-05-21 20:33                         ` Mike Turquette
2014-05-22 18:03                           ` Sören Brinkmann
2014-05-22 18:20                             ` Uwe Kleine-König
2014-05-22 20:32                               ` Sören Brinkmann
2014-05-22 21:03                                 ` Mike Turquette
2014-05-22 23:44                                   ` Sören Brinkmann
2014-05-23  1:37                                     ` Mike Turquette
2014-05-23 16:14                                       ` Sören Brinkmann
2014-05-26  6:29                                         ` Viresh Kumar
2014-05-26 11:22                                           ` Rafael J. Wysocki
2014-05-26 11:07                                             ` Viresh Kumar
2014-05-26 11:47                                               ` Rafael J. Wysocki
2014-05-26 21:52                                                 ` Sören Brinkmann
2014-05-28  2:05                                             ` Mike Turquette
2014-05-28 16:17                                               ` Rafael J. Wysocki
2014-06-07  0:44                               ` Sören Brinkmann
2014-05-14 22:30 ` [RFC PATCH 3/5] cpufreq: cpu0: Use clk_round_rate_nearest() Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 4/5] ARM: zynq: dt: Use properly rounded frequencies in OPPs Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 5/5] net: macb: Use clk_round_rate_nearest() API Soren Brinkmann
2014-05-15  6:12 ` [RFC PATCH 0/5] Frequency resolution in CCF vs. cpufreq Viresh Kumar
2014-05-15 14:05   ` Sören Brinkmann
2014-05-15  7:47 ` Uwe Kleine-König
2014-05-15 12:14   ` Nishanth Menon
2014-05-15 14:00   ` Sören Brinkmann

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=20140515073816.GI16662@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --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;
as well as URLs for NNTP newsgroup(s).