Re: [PATCH v5 2/3] clk: qcom: clk-rcg2: add support for rcg2 freq multi ops

From: Simon Horman
Date: Thu Jul 13 2023 - 01:32:11 EST


On Thu, Jun 01, 2023 at 12:26:53AM +0200, Christian Marangi wrote:
> 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
> 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@xxxxxxxxx>

...

> diff --git a/drivers/clk/qcom/clk-rcg2.c b/drivers/clk/qcom/clk-rcg2.c
> index 76551534f10d..9a139fc8bbfa 100644
> --- a/drivers/clk/qcom/clk-rcg2.c
> +++ b/drivers/clk/qcom/clk-rcg2.c
> @@ -266,6 +266,112 @@ 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;
> + 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)
> + return f->confs;
> +
> + /* 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;
> + break;
> + }
> +
> + rate_diff = abs(req_rate - rate);
> + 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 present.
> + */
> + if (unlikely(i == f->num_confs)) {
> + WARN(1, "%s: can't find a configuration for rate %lu.",
> + name, req_rate);
> + return ERR_PTR(-EINVAL);
> + }
> +
> + return best_conf;

Hi Christian,

It's unclear to me if this can actually occur,
but Sparse warns that best_conf may be uninitialised here.

> +}

...