Linux Serial subsystem development
 help / color / mirror / Atom feed
From: Vijaya Krishna Nivarthi <quic_vnivarth@quicinc.com>
To: Doug Anderson <dianders@chromium.org>
Cc: Andy Gross <agross@kernel.org>,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jirislaby@kernel.org>,
	linux-arm-msm <linux-arm-msm@vger.kernel.org>,
	<linux-serial@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>, <quic_msavaliy@quicinc.com>,
	Matthias Kaehlcke <mka@chromium.org>,
	"Stephen Boyd" <swboyd@chromium.org>
Subject: Re: [PATCH] tty: serial: qcom-geni-serial: minor fixes to get_clk_div_rate()
Date: Mon, 6 Jun 2022 23:49:02 +0530	[thread overview]
Message-ID: <e677fd02-011f-4f4e-fa73-17dc96aea7d0@quicinc.com> (raw)
In-Reply-To: <CAD=FV=XD+LozhkJZp0C7RUO01T-XuqBA-SJ0EQeyvGk0CxC3JQ@mail.gmail.com>

Hi,


On 6/4/2022 12:10 AM, Doug Anderson wrote:
> Hi,
>
> On Fri, Jun 3, 2022 at 10:43 AM Vijaya Krishna Nivarthi
> <quic_vnivarth@quicinc.com> wrote:
>>
>> Ah, or I guess what you're saying is that the table historically
>> contained "rounded" rates but that clk_round_rate() isn't returning
>> nice round rates. OK, but if we truly want to support an inexact
>> match, you'd want to pick the rate that reduces the error, not just
>> pick the first one. In other words, something like this (untested):
>>
>> freq = clk_round_rate(clk, mult);
>> diff = abs(((long)mult - freq) / div);
>> if (diff < best_diff) {
>>     best_diff = diff;
>>     ser_clk = freq;
>>     best_div = div;
>> }
>> I am not sure if its required that freq is a multiple of best_div now
>> that we don't have a multiple of desired_clk anyway.
> How about just this (untested):
>
> freq = clk_round_rate(clk, mult);
> candidate_div = max(1, DIV_ROUND_CLOSEST(freq, desired_clk));
> candidate_freq = freq / candidate_div;
> diff = abs((long)desired_clk - candidate_freq);
> if (diff < best_diff) {
>    best_diff = diff;
>    ser_clk = freq;
>    best_div = candidate_div;
> }

I am afraid this still doesn't guarantee that ser_clk is a multiple of 
best_div

I tested it with a function simulates clk_round_rate.

static unsigned long clk_round_rate_test(struct clk *clk, unsigned long 
in_freq)
{
     unsigned long root_freq[6] = {105, 204, 303, 402, 501, 602};
     int i;

     for (i = 0; i < 6; i++) {
         if (root_freq[i] >= in_freq)
             return root_freq[i];
     }
     return root_freq[6];
}

     {
         unsigned long ser_clk;
         unsigned long desired_clk;
         unsigned long freq;
         int div_round_closest;
         unsigned long div;
         unsigned long mult;
         unsigned long candidate_div, candidate_freq;

         unsigned long diff, best_diff, best_div;
         unsigned long one;

         desired_clk = 100;
         one = 1;
         best_diff = ULONG_MAX;
         pr_err("\ndesired_clk-%d\n", desired_clk);
         for (div = 1; div <= 10; div++) {
             mult = div * desired_clk;

             freq = clk_round_rate_test(clk, mult);
             div_round_closest = DIV_ROUND_CLOSEST(freq, desired_clk);
             candidate_div = max(one, (unsigned long)div_round_closest);
             candidate_freq = freq / candidate_div;
             diff = abs((long)desired_clk - candidate_freq);
             pr_err("div-%d, mult-%d, freq-%d, div_round_closest-%d, 
candidate_div-%d, candidate_freq-%d, diff-%d\n",
                 div, mult, freq, div_round_closest, candidate_div, 
candidate_freq, diff);
             if (diff < best_diff) {
                 pr_err("This is best so far\n");
                 best_diff = diff;
                 ser_clk = freq;
                 best_div = candidate_div;
             }
         }
         pr_err("\nbest_diff-%d, ser_clk-%d, best_div-%d\n",
             best_diff, ser_clk, best_div);
     }

And here is the output

[   17.835167] desired_clk-100
[   17.839567] div-1, mult-100, freq-105, div_round_closest-1, 
candidate_div-1, candidate_freq-105, diff-5
[   17.849220] This is best so far
[   17.852458] div-2, mult-200, freq-204, div_round_closest-2, 
candidate_div-2, candidate_freq-102, diff-2
[   17.862104] This is best so far
[   17.865345] div-3, mult-300, freq-303, div_round_closest-3, 
candidate_div-3, candidate_freq-101, diff-1
[   17.874995] This is best so far
[   17.878237] div-4, mult-400, freq-402, div_round_closest-4, 
candidate_div-4, candidate_freq-100, diff-0
[   17.887882] This is best so far
[   17.891118] div-5, mult-500, freq-501, div_round_closest-5, 
candidate_div-5, candidate_freq-100, diff-0
[   17.900770] div-6, mult-600, freq-602, div_round_closest-6, 
candidate_div-6, candidate_freq-100, diff-0
[   17.910415] div-7, mult-700, freq-602, div_round_closest-6, 
candidate_div-6, candidate_freq-100, diff-0
[   17.920057] div-8, mult-800, freq-602, div_round_closest-6, 
candidate_div-6, candidate_freq-100, diff-0
[   17.929703] div-9, mult-900, freq-602, div_round_closest-6, 
candidate_div-6, candidate_freq-100, diff-0
[   17.939353] div-10, mult-1000, freq-602, div_round_closest-6, 
candidate_div-6, candidate_freq-100, diff-0
[   17.949181]
[   17.949181] best_diff-0, ser_clk-402, best_div-4

Please note that we go past cases when we have an divider that can 
exactly divide the frequency(105/1, 204/2, 303/3) and end up with one 
that doesn't.

It seems like we need to run through all dividers to find one that can 
divide output freq from clk_round_rate and among those choose smallest 
delta.

In regular bootup 2nd loop was required only for 51200000. For others 
1843200, 153600, 1st loop worked fine.

Thank you.

>
> Here:
>
> freq: a freq we can definitely make
>
> candidate_div: the best number to divide freq by to get the desired clock.
>
> candidate_freq: the frequency we'll end up if we divide freq by
> candidate_div. We want this to be close to desired_clk.
>
> diff: how far away the candidate_freq is away from what we want.
>
> best_diff: how far away the best candidate was from what we wanted.
>
> ser_clk: What we should pass to clk_set_rate() to get the best candidate.
>
> best_div: What we should use as a divider to get the best candidate.
>
>
> -Doug

  reply	other threads:[~2022-06-06 18:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-31 18:17 [PATCH] tty: serial: qcom-geni-serial: minor fixes to get_clk_div_rate() Vijaya Krishna Nivarthi
2022-05-31 18:28 ` Greg KH
2022-05-31 19:28 ` Doug Anderson
2022-06-01 10:45   ` Vijaya Krishna Nivarthi
2022-06-01 15:33     ` Doug Anderson
2022-06-03 17:43       ` Vijaya Krishna Nivarthi
2022-06-03 18:40         ` Doug Anderson
2022-06-06 18:19           ` Vijaya Krishna Nivarthi [this message]
2022-06-06 19:59             ` Doug Anderson
2022-06-07 17:40               ` Vijaya Krishna Nivarthi
2022-06-07 19:25                 ` Doug Anderson
2022-06-08 18:33                   ` Vijaya Krishna Nivarthi
2022-06-08 22:37                     ` Doug Anderson
2022-06-09 17:48                       ` Vijaya Krishna Nivarthi
2022-06-09 18:08                       ` Vijaya Krishna Nivarthi
2022-06-10  9:33                       ` Vijaya Krishna Nivarthi (Temp) (QUIC)
2022-06-10 17:20                         ` Doug Anderson
2022-06-13 18:16                           ` Vijaya Krishna Nivarthi (Temp) (QUIC)
2022-06-21 17:58                       ` Vijaya Krishna Nivarthi (Temp)
2022-06-23 23:11                         ` Doug Anderson

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=e677fd02-011f-4f4e-fa73-17dc96aea7d0@quicinc.com \
    --to=quic_vnivarth@quicinc.com \
    --cc=agross@kernel.org \
    --cc=bjorn.andersson@linaro.org \
    --cc=dianders@chromium.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=mka@chromium.org \
    --cc=quic_msavaliy@quicinc.com \
    --cc=swboyd@chromium.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