[PATCH RFC RFT v3 13/13] clk: meson: pll: Remove the dedicated n parameter
From: Jian Hu via B4 Relay
Date: Tue Dec 01 2026 - 03:57:24 EST
From: Jian Hu <jian.hu@xxxxxxxxxxx>
The Meson PLL framework models the PLL pre-divider (N) using a dedicated
field in struct meson_clk_pll_data. Since the common clock framework
already provides a generic divider implementation, there is no need to
keep PLL-specific support for it.
Remove the dedicated n parameter from the framework and simplify the PLL
rate calculation accordingly.
The parent rate is now negotiated on every rate request: the middle of the
multiplier range is tried first, then the boundary the computed multiplier
overflowed, and the request is rejected if the multiplier still does not fit.
The PLL tables are arrays of struct pll_params_table entries, which only
carry a multiplier now that the pre-divider is gone. Replace them with
0-terminated arrays of unsigned int.
Signed-off-by: Jian Hu <jian.hu@xxxxxxxxxxx>
---
drivers/clk/meson/axg.c | 10 +-
drivers/clk/meson/clk-pll.c | 241 +++++++++++++++++++++++++-------------------
drivers/clk/meson/clk-pll.h | 14 +--
drivers/clk/meson/g12a.c | 7 +-
drivers/clk/meson/meson8b.c | 58 ++---------
5 files changed, 150 insertions(+), 180 deletions(-)
diff --git a/drivers/clk/meson/axg.c b/drivers/clk/meson/axg.c
index 8c5a2640324c..55c0f4ffb075 100644
--- a/drivers/clk/meson/axg.c
+++ b/drivers/clk/meson/axg.c
@@ -838,13 +838,7 @@ static struct clk_regmap axg_pcie_prediv = {
},
};
-static const struct pll_params_table axg_pcie_pll_params_table[] = {
- {
- .m = 200,
- .n = 3,
- },
- { /* sentinel */ },
-};
+static const unsigned int axg_pcie_pll_mult_table[] = { 200, 0 };
static const struct reg_sequence axg_pcie_pll_init_regs[] = {
{ .reg = HHI_PCIE_PLL_CNTL1, .def = 0x0084a2aa },
@@ -883,7 +877,7 @@ static struct clk_regmap axg_pcie_pll_dco = {
.shift = 29,
.width = 1,
},
- .table = axg_pcie_pll_params_table,
+ .table = axg_pcie_pll_mult_table,
.init_regs = axg_pcie_pll_init_regs,
.init_count = ARRAY_SIZE(axg_pcie_pll_init_regs),
},
diff --git a/drivers/clk/meson/clk-pll.c b/drivers/clk/meson/clk-pll.c
index 29b5222983df..cbe1da6fe636 100644
--- a/drivers/clk/meson/clk-pll.c
+++ b/drivers/clk/meson/clk-pll.c
@@ -14,7 +14,7 @@
* +--------------------------------+
* | |
* | +--+ |
- * in >>-----[ /N ]--->| | +-----+ |
+ * in >>-------------->| | +-----+ |
* | | |------| DCO |---->> out
* | +--------->| | +--v--+ |
* | | +--+ | |
@@ -23,7 +23,7 @@
* | |
* +--------------------------------+
*
- * out = in * (m + frac / frac_max) / n
+ * out = in * (m + frac / frac_max)
*/
#include <linux/clk-provider.h>
@@ -31,6 +31,7 @@
#include <linux/err.h>
#include <linux/io.h>
#include <linux/math64.h>
+#include <linux/minmax.h>
#include <linux/module.h>
#include "clk-regmap.h"
@@ -52,8 +53,7 @@ static int __pll_round_closest_mult(struct meson_clk_pll_data *pll)
}
static unsigned long __pll_params_to_rate(unsigned long parent_rate,
- unsigned int m, unsigned int n,
- unsigned int frac,
+ unsigned int m, unsigned int frac,
struct meson_clk_pll_data *pll)
{
u64 rate = (u64)parent_rate * m;
@@ -66,7 +66,7 @@ static unsigned long __pll_params_to_rate(unsigned long parent_rate,
rate += DIV_ROUND_UP_ULL(frac_rate, frac_max);
}
- return DIV_ROUND_UP_ULL(rate, n);
+ return rate;
}
static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
@@ -74,19 +74,7 @@ static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
{
struct clk_regmap *clk = to_clk_regmap(hw);
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
- unsigned int m, n, frac;
-
- n = MESON_PARM_APPLICABLE(&pll->n) ?
- meson_parm_read(clk->map, &pll->n) :
- 1;
-
- /*
- * On some HW, N is set to zero on init. This value is invalid as
- * it would result in a division by zero. The rate can't be
- * calculated in this case
- */
- if (n == 0)
- return 0;
+ unsigned int m, frac;
m = meson_parm_read(clk->map, &pll->m);
@@ -94,21 +82,20 @@ static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
meson_parm_read(clk->map, &pll->frac) :
0;
- return __pll_params_to_rate(parent_rate, m, n, frac, pll);
+ return __pll_params_to_rate(parent_rate, m, frac, pll);
}
static unsigned int __pll_params_with_frac(unsigned long rate,
unsigned long parent_rate,
unsigned int m,
- unsigned int n,
struct meson_clk_pll_data *pll)
{
unsigned int frac_max = pll->frac_max ? pll->frac_max :
(1 << pll->frac.width);
- u64 val = (u64)rate * n;
+ u64 val = (u64)rate;
/* Bail out if we are already over the requested rate */
- if (rate < parent_rate * m / n)
+ if (rate < parent_rate * m)
return 0;
if (pll->flags & CLK_MESON_PLL_ROUND_CLOSEST)
@@ -139,58 +126,77 @@ static bool meson_clk_pll_is_better(unsigned long rate,
return false;
}
-static int meson_clk_get_pll_table_index(unsigned int index,
- unsigned int *m,
- unsigned int *n,
- struct meson_clk_pll_data *pll)
+static unsigned int meson_clk_pll_min_mult(struct meson_clk_pll_data *pll)
{
- if (!pll->table[index].n)
- return -EINVAL;
+ unsigned int i, min_mult = UINT_MAX;
- *m = pll->table[index].m;
- *n = pll->table[index].n;
+ if (pll->range)
+ return pll->range->min;
- return 0;
+ for (i = 0; pll->table && pll->table[i]; i++)
+ min_mult = min(min_mult, pll->table[i]);
+
+ return min_mult == UINT_MAX ? 0 : min_mult;
}
-static unsigned int meson_clk_get_pll_range_m(unsigned long rate,
- unsigned long parent_rate,
- unsigned int n,
- struct meson_clk_pll_data *pll)
+static unsigned int meson_clk_pll_max_mult(struct meson_clk_pll_data *pll)
{
- u64 val = (u64)rate * n;
+ unsigned int i, max_mult = 0;
+
+ if (pll->range)
+ return pll->range->max;
+
+ for (i = 0; pll->table && pll->table[i]; i++)
+ max_mult = max(max_mult, pll->table[i]);
+
+ return max_mult;
+}
+static unsigned int meson_clk_pll_rate_to_mult(unsigned long rate,
+ unsigned long parent_rate,
+ struct meson_clk_pll_data *pll)
+{
if (__pll_round_closest_mult(pll))
- return DIV_ROUND_CLOSEST_ULL(val, parent_rate);
+ return DIV_ROUND_CLOSEST_ULL(rate, parent_rate);
- return div_u64(val, parent_rate);
+ return div_u64(rate, parent_rate);
}
-static int meson_clk_get_pll_range_index(unsigned long rate,
- unsigned long parent_rate,
- unsigned int index,
+/* m_hint must be non-zero */
+static int meson_clk_get_pll_parent_rate(struct clk_rate_request *req,
+ unsigned int m_hint,
+ unsigned long *parent_rate,
unsigned int *m,
- unsigned int *n,
struct meson_clk_pll_data *pll)
{
- *n = index + 1;
+ unsigned long pr;
- /* Check the predivider range */
- if (*n >= (MESON_PARM_APPLICABLE(&pll->n) ? 1 << pll->n.width : 2))
+ pr = clk_hw_round_rate(req->best_parent_hw,
+ DIV_ROUND_UP_ULL(req->rate, m_hint));
+ if (!pr)
return -EINVAL;
- if (*n == 1) {
- /* Get the boundaries out the way */
- if (rate <= pll->range->min * parent_rate) {
- *m = pll->range->min;
- return -ENODATA;
- } else if (rate >= pll->range->max * parent_rate) {
- *m = pll->range->max;
- return -ENODATA;
- }
+ *parent_rate = pr;
+ *m = meson_clk_pll_rate_to_mult(req->rate, pr, pll);
+
+ return 0;
+}
+
+static int meson_clk_get_pll_range(unsigned long rate,
+ unsigned long parent_rate,
+ unsigned int *m,
+ struct meson_clk_pll_data *pll)
+{
+ /* Get the boundaries out the way */
+ if (rate <= pll->range->min * parent_rate) {
+ *m = pll->range->min;
+ return -ENODATA;
+ } else if (rate >= pll->range->max * parent_rate) {
+ *m = pll->range->max;
+ return -ENODATA;
}
- *m = meson_clk_get_pll_range_m(rate, parent_rate, *n, pll);
+ *m = meson_clk_pll_rate_to_mult(rate, parent_rate, pll);
/* the pre-divider gives a multiplier too big - stop */
if (*m >= (1 << pll->m.width))
@@ -199,50 +205,42 @@ static int meson_clk_get_pll_range_index(unsigned long rate,
return 0;
}
-static int meson_clk_get_pll_get_index(unsigned long rate,
- unsigned long parent_rate,
- unsigned int index,
- unsigned int *m,
- unsigned int *n,
- struct meson_clk_pll_data *pll)
-{
- if (pll->range)
- return meson_clk_get_pll_range_index(rate, parent_rate,
- index, m, n, pll);
- else if (pll->table)
- return meson_clk_get_pll_table_index(index, m, n, pll);
-
- return -EINVAL;
-}
-
-static int meson_clk_get_pll_settings(unsigned long rate,
- unsigned long parent_rate,
- unsigned int *best_m,
- unsigned int *best_n,
- struct meson_clk_pll_data *pll)
+static int meson_clk_get_pll_table(unsigned long rate,
+ unsigned long parent_rate,
+ unsigned int *m,
+ struct meson_clk_pll_data *pll)
{
- unsigned long best = 0, now = 0;
- unsigned int i, m, n;
- int ret;
+ unsigned long now, best = 0;
+ unsigned int i;
+ int ret = -EINVAL;
- for (i = 0, ret = 0; !ret; i++) {
- ret = meson_clk_get_pll_get_index(rate, parent_rate,
- i, &m, &n, pll);
- if (ret == -EINVAL)
- break;
+ for (i = 0; pll->table[i]; i++) {
+ now = (unsigned long)pll->table[i] * parent_rate;
- now = __pll_params_to_rate(parent_rate, m, n, 0, pll);
if (meson_clk_pll_is_better(rate, best, now, pll)) {
best = now;
- *best_m = m;
- *best_n = n;
-
- if (now == rate)
- break;
+ *m = pll->table[i];
+ ret = 0;
}
+
+ if (now == rate)
+ break;
}
- return best ? 0 : -EINVAL;
+ return ret;
+}
+
+static int meson_clk_get_pll_mult(unsigned long rate,
+ unsigned long parent_rate,
+ unsigned int *m,
+ struct meson_clk_pll_data *pll)
+{
+ if (pll->range)
+ return meson_clk_get_pll_range(rate, parent_rate, m, pll);
+ else if (pll->table)
+ return meson_clk_get_pll_table(rate, parent_rate, m, pll);
+
+ return -EINVAL;
}
static int meson_clk_pll_determine_rate(struct clk_hw *hw,
@@ -250,16 +248,53 @@ static int meson_clk_pll_determine_rate(struct clk_hw *hw,
{
struct clk_regmap *clk = to_clk_regmap(hw);
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
- unsigned int m, n, frac;
- unsigned long round;
+ unsigned int m, m_min, m_max, frac;
+ unsigned long parent_rate, round;
int ret;
- ret = meson_clk_get_pll_settings(req->rate, req->best_parent_rate,
- &m, &n, pll);
+ if (!req->best_parent_hw)
+ return -EINVAL;
+
+ m_min = meson_clk_pll_min_mult(pll);
+ m_max = meson_clk_pll_max_mult(pll);
+ if (!m_min || !m_max)
+ return -EINVAL;
+
+ /*
+ * The pre-divider is a regular clock now, so the parent rate must be
+ * negotiated. Aim for the middle of the multiplier range first, so a
+ * divider parent, which can only round down, still leaves the
+ * multiplier within [m_min, m_max].
+ */
+ ret = meson_clk_get_pll_parent_rate(req,
+ DIV_ROUND_UP(m_min + m_max, 2),
+ &parent_rate, &m, pll);
if (ret)
return ret;
- round = __pll_params_to_rate(req->best_parent_rate, m, n, 0, pll);
+ /* Parent rounded too far: retry from the other end of the range */
+ if (m > m_max)
+ ret = meson_clk_get_pll_parent_rate(req, m_min, &parent_rate,
+ &m, pll);
+ else if (!m || m < m_min)
+ ret = meson_clk_get_pll_parent_rate(req, m_max, &parent_rate,
+ &m, pll);
+ if (ret)
+ return ret;
+
+ if (!m || m < m_min || m > m_max)
+ return -EINVAL;
+
+ /*
+ * -ENODATA only means the multiplier was clamped to one of the range
+ * boundaries, which is a usable setting.
+ */
+ ret = meson_clk_get_pll_mult(req->rate, parent_rate, &m, pll);
+ if (ret && ret != -ENODATA)
+ return ret;
+
+ req->best_parent_rate = parent_rate;
+ round = __pll_params_to_rate(parent_rate, m, 0, pll);
if (!MESON_PARM_APPLICABLE(&pll->frac) || req->rate == round) {
req->rate = round;
@@ -270,8 +305,8 @@ static int meson_clk_pll_determine_rate(struct clk_hw *hw,
* The rate provided by the setting is not an exact match, let's
* try to improve the result using the fractional parameter
*/
- frac = __pll_params_with_frac(req->rate, req->best_parent_rate, m, n, pll);
- req->rate = __pll_params_to_rate(req->best_parent_rate, m, n, frac, pll);
+ frac = __pll_params_with_frac(req->rate, parent_rate, m, pll);
+ req->rate = __pll_params_to_rate(parent_rate, m, frac, pll);
return 0;
}
@@ -440,7 +475,7 @@ static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
{
struct clk_regmap *clk = to_clk_regmap(hw);
struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
- unsigned int enabled, m, n, frac = 0;
+ unsigned int enabled, m, frac = 0;
unsigned long old_rate;
int ret;
@@ -449,20 +484,18 @@ static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
old_rate = clk_hw_get_rate(hw);
- ret = meson_clk_get_pll_settings(rate, parent_rate, &m, &n, pll);
- if (ret)
+ ret = meson_clk_get_pll_mult(rate, parent_rate, &m, pll);
+ if (ret && ret != -ENODATA)
return ret;
enabled = meson_parm_read(clk->map, &pll->en);
if (enabled)
meson_clk_pll_disable(hw);
- if (MESON_PARM_APPLICABLE(&pll->n))
- meson_parm_write(clk->map, &pll->n, n);
meson_parm_write(clk->map, &pll->m, m);
if (MESON_PARM_APPLICABLE(&pll->frac)) {
- frac = __pll_params_with_frac(rate, parent_rate, m, n, pll);
+ frac = __pll_params_with_frac(rate, parent_rate, m, pll);
meson_parm_write(clk->map, &pll->frac, frac);
}
diff --git a/drivers/clk/meson/clk-pll.h b/drivers/clk/meson/clk-pll.h
index 8384b75ae38d..1100bd795056 100644
--- a/drivers/clk/meson/clk-pll.h
+++ b/drivers/clk/meson/clk-pll.h
@@ -11,22 +11,11 @@
#include <linux/regmap.h>
#include "parm.h"
-struct pll_params_table {
- unsigned int m;
- unsigned int n;
-};
-
struct pll_mult_range {
unsigned int min;
unsigned int max;
};
-#define PLL_PARAMS(_m, _n) \
- { \
- .m = (_m), \
- .n = (_n), \
- }
-
#define CLK_MESON_PLL_ROUND_CLOSEST BIT(0)
#define CLK_MESON_PLL_NOINIT_ENABLED BIT(1)
#define CLK_MESON_PLL_RST_ACTIVE_LOW BIT(2)
@@ -34,7 +23,6 @@ struct pll_mult_range {
struct meson_clk_pll_data {
struct parm en;
struct parm m;
- struct parm n;
struct parm frac;
struct parm l;
struct parm rst;
@@ -42,7 +30,7 @@ struct meson_clk_pll_data {
struct parm l_detect;
const struct reg_sequence *init_regs;
unsigned int init_count;
- const struct pll_params_table *table;
+ const unsigned int *table;
const struct pll_mult_range *range;
unsigned int frac_max;
u8 flags;
diff --git a/drivers/clk/meson/g12a.c b/drivers/clk/meson/g12a.c
index 09eb842e0b1a..c121c6f52ae1 100644
--- a/drivers/clk/meson/g12a.c
+++ b/drivers/clk/meson/g12a.c
@@ -751,10 +751,7 @@ static const struct reg_sequence g12a_pcie_pll_init_regs[] = {
};
/* Keep a single entry table for recalc/round_rate() ops */
-static const struct pll_params_table g12a_pcie_pll_table[] = {
- PLL_PARAMS(150, 1),
- {0, 0},
-};
+static const unsigned int g12a_pcie_pll_mult_table[] = { 150, 0 };
static struct clk_regmap g12a_pcie_pll_dco = {
.data = &(struct meson_clk_pll_data){
@@ -783,7 +780,7 @@ static struct clk_regmap g12a_pcie_pll_dco = {
.shift = 29,
.width = 1,
},
- .table = g12a_pcie_pll_table,
+ .table = g12a_pcie_pll_mult_table,
.init_regs = g12a_pcie_pll_init_regs,
.init_count = ARRAY_SIZE(g12a_pcie_pll_init_regs),
},
diff --git a/drivers/clk/meson/meson8b.c b/drivers/clk/meson/meson8b.c
index ae2bab501597..9a165c8cd5b3 100644
--- a/drivers/clk/meson/meson8b.c
+++ b/drivers/clk/meson/meson8b.c
@@ -95,29 +95,8 @@ struct meson8b_clk_reset {
struct regmap *regmap;
};
-static const struct pll_params_table sys_pll_params_table[] = {
- PLL_PARAMS(50, 1),
- PLL_PARAMS(51, 1),
- PLL_PARAMS(52, 1),
- PLL_PARAMS(53, 1),
- PLL_PARAMS(54, 1),
- PLL_PARAMS(55, 1),
- PLL_PARAMS(56, 1),
- PLL_PARAMS(57, 1),
- PLL_PARAMS(58, 1),
- PLL_PARAMS(59, 1),
- PLL_PARAMS(60, 1),
- PLL_PARAMS(61, 1),
- PLL_PARAMS(62, 1),
- PLL_PARAMS(63, 1),
- PLL_PARAMS(64, 1),
- PLL_PARAMS(65, 1),
- PLL_PARAMS(66, 1),
- PLL_PARAMS(67, 1),
- PLL_PARAMS(68, 1),
- PLL_PARAMS(84, 1),
- { /* sentinel */ },
-};
+static const unsigned int sys_pll_mult_table[] = { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
+ 61, 62, 63, 64, 65, 66, 67, 68, 84, 0 };
static struct clk_regmap meson8b_fixed_prediv = {
.data = &(struct clk_regmap_div_data){
@@ -257,26 +236,8 @@ static const struct reg_sequence meson8b_hdmi_pll_init_regs[] = {
{ .reg = HHI_VID2_PLL_CNTL2, .def = 0x0430a800 },
};
-static const struct pll_params_table meson8b_hdmi_pll_params_table[] = {
- PLL_PARAMS(40, 1),
- PLL_PARAMS(42, 1),
- PLL_PARAMS(44, 1),
- PLL_PARAMS(45, 1),
- PLL_PARAMS(49, 1),
- PLL_PARAMS(52, 1),
- PLL_PARAMS(54, 1),
- PLL_PARAMS(56, 1),
- PLL_PARAMS(59, 1),
- PLL_PARAMS(60, 1),
- PLL_PARAMS(61, 1),
- PLL_PARAMS(62, 1),
- PLL_PARAMS(64, 1),
- PLL_PARAMS(66, 1),
- PLL_PARAMS(68, 1),
- PLL_PARAMS(71, 1),
- PLL_PARAMS(82, 1),
- { /* sentinel */ }
-};
+static const unsigned int meson8b_hdmi_pll_mult_table[] = { 40, 42, 44, 45, 49, 52, 54, 56, 60,
+ 61, 62, 64, 66, 68, 71, 82, 0 };
static struct clk_regmap meson8b_hdmi_pll_dco = {
.data = &(struct meson_clk_pll_data){
@@ -305,7 +266,7 @@ static struct clk_regmap meson8b_hdmi_pll_dco = {
.shift = 29,
.width = 1,
},
- .table = meson8b_hdmi_pll_params_table,
+ .table = meson8b_hdmi_pll_mult_table,
.init_regs = meson8b_hdmi_pll_init_regs,
.init_count = ARRAY_SIZE(meson8b_hdmi_pll_init_regs),
},
@@ -399,7 +360,7 @@ static struct clk_regmap meson8b_sys_pll_dco = {
.shift = 29,
.width = 1,
},
- .table = sys_pll_params_table,
+ .table = sys_pll_mult_table,
},
.hw.init = &(struct clk_init_data){
.name = "sys_pll_dco",
@@ -2121,10 +2082,7 @@ static const struct reg_sequence meson8m2_gp_pll_init_regs[] = {
{ .reg = HHI_GP_PLL_CNTL5, .def = 0x00003000 },
};
-static const struct pll_params_table meson8m2_gp_pll_params_table[] = {
- PLL_PARAMS(182, 3),
- { /* sentinel */ },
-};
+static const unsigned int meson8m2_gp_pll_mult_table[] = { 182, 0 };
static struct clk_regmap meson8m2_gp_pll_dco = {
.data = &(struct meson_clk_pll_data){
@@ -2148,7 +2106,7 @@ static struct clk_regmap meson8m2_gp_pll_dco = {
.shift = 29,
.width = 1,
},
- .table = meson8m2_gp_pll_params_table,
+ .table = meson8m2_gp_pll_mult_table,
.init_regs = meson8m2_gp_pll_init_regs,
.init_count = ARRAY_SIZE(meson8m2_gp_pll_init_regs),
},
--
2.47.1