public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Stephen Boyd <sboyd@kernel.org>
To: Andy Gross <agross@kernel.org>,
	Bjorn Andersson <andersson@kernel.org>,
	Christian Marangi <ansuelsmth@gmail.com>,
	Konrad Dybcio <konrad.dybcio@linaro.org>,
	Michael Turquette <mturquette@baylibre.com>,
	linux-arm-msm@vger.kernel.org, linux-clk@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Christian Marangi <ansuelsmth@gmail.com>
Subject: Re: [PATCH v7 2/3] clk: qcom: clk-rcg2: add support for rcg2 freq multi ops
Date: Wed, 20 Dec 2023 12:05:39 -0800	[thread overview]
Message-ID: <ba20c2000c5c37c14c03b94f208b2961.sboyd@kernel.org> (raw)
In-Reply-To: <20231124151847.1915-3-ansuelsmth@gmail.com>

Quoting Christian Marangi (2023-11-24 07:18:46)
> Some RCG frequency can be reached by multiple configuration.
> 
> Add clk_rcg2_fm_ops ops to support these special RCG configurations.
> 
> These alternative ops will select the frequency using a CEIL policy.
> 
> When the correct frequency is found, the correct config is selected by
> calculating the final rate (by checking the defined parent and values
> in the config that is being checked) and deciding based on the one that
> is less different than the requested one.
> 
> These check are skipped if there is just on config for the requested

s/on/one/

> freq.
> 
> qcom_find_freq_multi is added to search the freq with the new struct
> freq_multi_tbl.
> __clk_rcg2_select_conf is used to select the correct conf by simulating
> the final clock.
> If a conf can't be found due to parent not reachable, a WARN is printed
> and -EINVAL is returned.
> 
> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
[...]
> diff --git a/drivers/clk/qcom/clk-rcg2.c b/drivers/clk/qcom/clk-rcg2.c
> index e22baf3a7112..2393c7df0402 100644
> --- a/drivers/clk/qcom/clk-rcg2.c
> +++ b/drivers/clk/qcom/clk-rcg2.c
> @@ -266,6 +266,115 @@ static int _freq_tbl_determine_rate(struct clk_hw *hw, const struct freq_tbl *f,
>         return 0;
>  }
>  
> +static const struct freq_conf *
> +__clk_rcg2_select_conf(struct clk_hw *hw, const struct freq_multi_tbl *f,
> +                      unsigned long req_rate)
> +{
> +       unsigned long rate_diff, best_rate_diff = ULONG_MAX;
> +       const struct freq_conf *conf, *best_conf = NULL;
> +       struct clk_rcg2 *rcg = to_clk_rcg2(hw);
> +       const char *name = clk_hw_get_name(hw);
> +       unsigned long parent_rate, rate;
> +       struct clk_hw *p;
> +       int index, i;
> +
> +       /* Exit early if only one config is defined */
> +       if (f->num_confs == 1) {
> +               best_conf = f->confs;
> +               goto exit;
> +       }
> +
> +       /* Search in each provided config the one that is near the wanted rate */
> +       for (i = 0, conf = f->confs; i < f->num_confs; i++, conf++) {
> +               index = qcom_find_src_index(hw, rcg->parent_map, conf->src);
> +               if (index < 0)
> +                       continue;
> +
> +               p = clk_hw_get_parent_by_index(hw, index);
> +               if (!p)
> +                       continue;
> +
> +               parent_rate =  clk_hw_get_rate(p);
> +               rate = calc_rate(parent_rate, conf->n, conf->m, conf->n, conf->pre_div);
> +
> +               if (rate == req_rate) {
> +                       best_conf = conf;
> +                       goto exit;
> +               }
> +
> +               rate_diff = abs(req_rate - rate);

Use abs_diff() please.

> +               if (rate_diff < best_rate_diff) {
> +                       best_rate_diff = rate_diff;
> +                       best_conf = conf;
> +               }
> +       }
> +
> +       /*
> +        * Very unlikely. Warn if we couldn't find a correct config
> +        * due to parent not found in every config.
> +        */
> +       if (unlikely(!best_conf)) {
> +               WARN(1, "%s: can't find a configuration for rate %lu.",

Needs a newline in the format string and should drop the period.

> +                    name, req_rate);
> +               return ERR_PTR(-EINVAL);
> +       }
> +
> +exit:
> +       return best_conf;
> +}
> +
[...]
> +
>  static int clk_rcg2_determine_rate(struct clk_hw *hw,
>                                    struct clk_rate_request *req)
>  {
> @@ -377,6 +494,30 @@ static int __clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
>         return clk_rcg2_configure(rcg, f);
>  }
>  
> +static int __clk_rcg2_fm_set_rate(struct clk_hw *hw, unsigned long rate)
> +{
> +       struct clk_rcg2 *rcg = to_clk_rcg2(hw);
> +       const struct freq_multi_tbl *f;
> +       const struct freq_conf *conf;
> +       struct freq_tbl f_tbl;

		f_tbl = { }

In case we want to add more fields so this isn't a time bomb.

> +
> +       f = qcom_find_freq_multi(rcg->freq_multi_tbl, rate);
> +       if (!f || !f->confs)
> +               return -EINVAL;
> +
> +       conf = __clk_rcg2_select_conf(hw, f, rate);
> +       if (IS_ERR(conf))
> +               return PTR_ERR(conf);
> +
> +       f_tbl.freq = f->freq;
> +       f_tbl.src = conf->src;
> +       f_tbl.pre_div = conf->pre_div;
> +       f_tbl.m = conf->m;
> +       f_tbl.n = conf->n;
> +
> +       return clk_rcg2_configure(rcg, &f_tbl);
> +}
> +
>  static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
>                             unsigned long parent_rate)
>  {

  parent reply	other threads:[~2023-12-20 20:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-24 15:18 [PATCH v7 0/3] clk: qcom: clk-rcg2: introduce support for multiple conf for same freq Christian Marangi
2023-11-24 15:18 ` [PATCH v7 1/3] clk: qcom: clk-rcg: " Christian Marangi
2023-12-20 20:07   ` Stephen Boyd
2023-11-24 15:18 ` [PATCH v7 2/3] clk: qcom: clk-rcg2: add support for rcg2 freq multi ops Christian Marangi
2023-12-07 11:58   ` Lei Wei
2023-12-20 20:05   ` Stephen Boyd [this message]
2023-11-24 15:18 ` [PATCH v7 3/3] clk: qcom: gcc-ipq8074: rework nss_port5/6 clock to multiple conf Christian Marangi

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=ba20c2000c5c37c14c03b94f208b2961.sboyd@kernel.org \
    --to=sboyd@kernel.org \
    --cc=agross@kernel.org \
    --cc=andersson@kernel.org \
    --cc=ansuelsmth@gmail.com \
    --cc=konrad.dybcio@linaro.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    /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