Commit 088cefca authored by Frank Oltmanns's avatar Frank Oltmanns Committed by Chen-Yu Tsai

clk: sunxi-ng: nkm: Support finding closest rate

When finding the best rate for a NKM clock, consider rates that are
higher than the requested rate, if the CCU_FEATURE_CLOSEST_RATE flag is
set by using the helper function ccu_is_better_rate().

Accommodate ccu_mux_helper_determine_rate to this change.
Acked-by: default avatarMaxime Ripard <mripard@kernel.org>
Reviewed-by: default avatarJernej Skrabec <jernej.skrabec@gmail.com>
Signed-off-by: default avatarFrank Oltmanns <frank@oltmanns.dev>
Link: https://lore.kernel.org/r/20230807-pll-mipi_set_rate_parent-v6-7-f173239a4b59@oltmanns.devSigned-off-by: default avatarChen-Yu Tsai <wens@csie.org>
parent c0380d14
...@@ -139,7 +139,7 @@ int ccu_mux_helper_determine_rate(struct ccu_common *common, ...@@ -139,7 +139,7 @@ int ccu_mux_helper_determine_rate(struct ccu_common *common,
goto out; goto out;
} }
if ((req->rate - tmp_rate) < (req->rate - best_rate)) { if (ccu_is_better_rate(common, req->rate, tmp_rate, best_rate)) {
best_rate = tmp_rate; best_rate = tmp_rate;
best_parent_rate = parent_rate; best_parent_rate = parent_rate;
best_parent = parent; best_parent = parent;
......
...@@ -16,7 +16,8 @@ struct _ccu_nkm { ...@@ -16,7 +16,8 @@ struct _ccu_nkm {
unsigned long m, min_m, max_m; unsigned long m, min_m, max_m;
}; };
static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *parent_hw, static unsigned long ccu_nkm_find_best_with_parent_adj(struct ccu_common *common,
struct clk_hw *parent_hw,
unsigned long *parent, unsigned long rate, unsigned long *parent, unsigned long rate,
struct _ccu_nkm *nkm) struct _ccu_nkm *nkm)
{ {
...@@ -32,10 +33,8 @@ static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *parent_hw, ...@@ -32,10 +33,8 @@ static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *parent_hw,
tmp_parent = clk_hw_round_rate(parent_hw, rate * _m / (_n * _k)); tmp_parent = clk_hw_round_rate(parent_hw, rate * _m / (_n * _k));
tmp_rate = tmp_parent * _n * _k / _m; tmp_rate = tmp_parent * _n * _k / _m;
if (tmp_rate > rate)
continue;
if ((rate - tmp_rate) < (rate - best_rate)) { if (ccu_is_better_rate(common, rate, tmp_rate, best_rate)) {
best_rate = tmp_rate; best_rate = tmp_rate;
best_parent_rate = tmp_parent; best_parent_rate = tmp_parent;
best_n = _n; best_n = _n;
...@@ -56,7 +55,7 @@ static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *parent_hw, ...@@ -56,7 +55,7 @@ static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *parent_hw,
} }
static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate,
struct _ccu_nkm *nkm) struct _ccu_nkm *nkm, struct ccu_common *common)
{ {
unsigned long best_rate = 0; unsigned long best_rate = 0;
unsigned long best_n = 0, best_k = 0, best_m = 0; unsigned long best_n = 0, best_k = 0, best_m = 0;
...@@ -69,9 +68,7 @@ static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, ...@@ -69,9 +68,7 @@ static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate,
tmp_rate = parent * _n * _k / _m; tmp_rate = parent * _n * _k / _m;
if (tmp_rate > rate) if (ccu_is_better_rate(common, rate, tmp_rate, best_rate)) {
continue;
if ((rate - tmp_rate) < (rate - best_rate)) {
best_rate = tmp_rate; best_rate = tmp_rate;
best_n = _n; best_n = _n;
best_k = _k; best_k = _k;
...@@ -164,9 +161,10 @@ static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux, ...@@ -164,9 +161,10 @@ static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
rate *= nkm->fixed_post_div; rate *= nkm->fixed_post_div;
if (!clk_hw_can_set_rate_parent(&nkm->common.hw)) if (!clk_hw_can_set_rate_parent(&nkm->common.hw))
rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm); rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm, &nkm->common);
else else
rate = ccu_nkm_find_best_with_parent_adj(parent_hw, parent_rate, rate, &_nkm); rate = ccu_nkm_find_best_with_parent_adj(&nkm->common, parent_hw, parent_rate, rate,
&_nkm);
if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
rate /= nkm->fixed_post_div; rate /= nkm->fixed_post_div;
...@@ -201,7 +199,7 @@ static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate, ...@@ -201,7 +199,7 @@ static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
_nkm.min_m = 1; _nkm.min_m = 1;
_nkm.max_m = nkm->m.max ?: 1 << nkm->m.width; _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
ccu_nkm_find_best(parent_rate, rate, &_nkm); ccu_nkm_find_best(parent_rate, rate, &_nkm, &nkm->common);
spin_lock_irqsave(nkm->common.lock, flags); spin_lock_irqsave(nkm->common.lock, flags);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment