clk: en7523: full clk_hw + set_rate for en7581
v8 cover clk: en7523: full clk_hw + set_rate for en7581
This simple patch convert the en7523 clk for EN7581 to full clk and adds the .set_rate OPs.
Needed mostly to configure MDC clk rate.
This will be also implemented by AN7583 since they share the same exact implementation.
Changes v8:
- Handle corner case with div by zero in en7581_find_best_rate
Changes v7:
- Address some formal improvement from Brain
- Back again to register clock returning error
- Actually round rate on determine rate
Changes v6:
- Rework to use div_table entirely
- Switch determine_rate to calculate best rate
Changes v5:
- Handle requesting 0 rate
Changes v4:
- Fix regression where base_bits is 0
- Handle divider_get_val returning -EINVAL
Changes v3:
- Rework to clock divisor API
- Return error instead of -EINVAL
- Use dev_err where possible
- Use devm_kzalloc_obj
- Revert to not fail on clk registration failure
Changes v2:
- Fix recalc_rate return value on error
Christian Marangi (2):
clk: en7523: convert to full clk_hw implementation for en7581
clk: en7523: add support for .set_rate for en7581
drivers/clk/clk-en7523.c | 444 +++++++++++++++++++++++++++++++++------ 1 file changed, 381 insertions(+), 63 deletions(-)
Signature · 1 line
2.55.0
PATCH v8 1/2 clk: en7523: convert to full clk_hw implementation for en7581
In preparation for support of .set_rate, convert the clock register logic from fixed clock implementation to full clk_hw implementation with dedicated OPs for Airoha EN7581 SoC.
While at it also rework the en7581_register_clocks() and en7523_register_pcie_clk() function to return error on clock register fail or on allocating dynamic memory.
--- drivers/clk/clk-en7523.c | 105 ++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 30 deletions(-)
drivers/clk/clk-en7523.c +75 -30
@@ -126,6 +126,12 @@ struct en_clk_gate {struct clk_hw hw; }; +struct en_clk { +struct regmap *map; +const struct en_clk_desc *desc; +struct clk_hw hw; +}; +struct en_rst_data {const u16 *bank_ofs;const u16 *idx_map;
@@ -640,10 +646,11 @@ static struct clk_hw *en7523_register_pcie_clk(struct device *dev, .ops = &soc_data->pcie_ops, };struct en_clk_gate *cg; +int ret; cg =devm_kzalloc_obj (dev, *cg);if (!cg) -return NULL ; +return ERR_PTR (-ENOMEM ); cg->base = np_base; cg->hw.init = &init;
@@ -651,8 +658,9 @@ static struct clk_hw *en7523_register_pcie_clk(struct device *dev,if (init.ops->unprepare) init.ops->unprepare (&cg->hw); -if (clk_hw_register (dev, &cg->hw)) -return NULL ; + ret =clk_hw_register (dev, &cg->hw); +if (ret) +return ERR_PTR (ret);return &cg->hw; }
@@ -718,7 +726,8 @@ static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_dat } hw =en7523_register_pcie_clk (dev, np_base); - clk_data->hws[EN7523_CLK_PCIE ] = hw; +if (!IS_ERR (hw)) + clk_data->hws[EN7523_CLK_PCIE ] = hw; } static int en7523_clk_hw_init(struct platform_device *pdev,
@@ -741,46 +750,79 @@ static int en7523_clk_hw_init(struct platform_device *pdev, en7581_rst_ofs); } -static void en7581_register_clocks (struct device *dev,struct clk_hw_onecell_data *clk_data, -struct regmap *map,void __iomem *base) +static unsigned long en7581_recalc_rate (struct clk_hw *hw, +unsigned long parent_rate) +{ + struct en_clk *c = container_of(hw, struct en_clk, hw); +const struct en_clk_desc *desc = c->desc; +struct device *dev =clk_hw_get_dev (hw); +struct regmap *map = c->map; +u32 val, reg, rate; +int err; + + err =regmap_read (map, desc->base_reg, &val); +if (err) { +dev_err (dev,"Failed reading fixed clk rate %s: %d\n" , + desc->name, err); +return 0 ; + } + rate =en7523_get_base_rate (desc, val); + + reg = desc->div_reg ? desc->div_reg : desc->base_reg; + err =regmap_read (map, reg, &val); +if (err) { +dev_err (dev,"Failed reading fixed clk div %s: %d\n" , + desc->name, err); +return 0 ; + } + +return rate /en7523_get_div (desc, val); +} + +static const struct clk_ops en7581_clk_ops = { + .recalc_rate = en7581_recalc_rate, +}; + +static int en7581_register_clocks (struct device *dev,struct clk_hw_onecell_data *clk_data, +struct regmap *map,void __iomem *base) {struct clk_hw *hw; -u32 rate;int i;for (i =0 ; i <ARRAY_SIZE (en7581_base_clks); i++) {const struct en_clk_desc *desc = &en7581_base_clks[i]; -u32 val, reg = desc->div_reg ? desc->div_reg : desc->base_reg; +struct clk_init_data init = { + .ops = &en7581_clk_ops, + }; +struct en_clk *en_clk;int err; - err =regmap_read (map, desc->base_reg, &val); -if (err) { -pr_err ("Failed reading fixed clk rate %s: %d\n" , - desc->name, err); -continue ; - } - rate =en7523_get_base_rate (desc, val); + en_clk =devm_kzalloc_obj (dev, *en_clk); +if (!en_clk) +return -ENOMEM ; - err =regmap_read (map, reg, &val); -if (err) { -pr_err ("Failed reading fixed clk div %s: %d\n" , - desc->name, err); -continue ; - } - rate /=en7523_get_div (desc, val); + init.name = desc->name; + en_clk->map = map; + en_clk->desc = desc; + en_clk->hw.init = &init; - hw =clk_hw_register_fixed_rate (dev, desc->name,NULL ,0 , rate); -if (IS_ERR (hw)) { -pr_err ("Failed to register clk %s: %ld\n" , - desc->name,PTR_ERR (hw)); -continue ; + err =devm_clk_hw_register (dev, &en_clk->hw); +if (err) { +dev_err_probe (dev, err,"Failed to register clk %s\n" , + desc->name); +return err; } - clk_data->hws[desc->id] = hw; + clk_data->hws[desc->id] = &en_clk->hw; } hw =en7523_register_pcie_clk (dev, base); +if (IS_ERR (hw)) +return PTR_ERR (hw); + clk_data->hws[EN7523_CLK_PCIE ] = hw; + +return 0 ; } static int en7523_reset_update(struct reset_controller_dev *rcdev,
@@ -1060,7 +1102,9 @@ static int en7581_clk_hw_init(struct platform_device *pdev, priv->base = base; -en7581_register_clocks (&pdev->dev, clk_data, map, base); + ret =en7581_register_clocks (&pdev->dev, clk_data, map, base); +if (ret) +return ret; val =readl (base +REG_NP_SCU_SSTR ); val &= ~(REG_PCIE_XSI0_SEL_MASK |REG_PCIE_XSI1_SEL_MASK );
@@ -1122,7 +1166,8 @@ static void en751221_register_clocks(struct device *dev,/* PCI */ hw =en7523_register_pcie_clk (dev, np_base); - clk_data->hws[EN751221_CLK_PCIE ] = hw; +if (!IS_ERR (hw)) + clk_data->hws[EN751221_CLK_PCIE ] = hw;/* SPI */ rate =EN751221_SPI_BASE ;
Signature · 1 line
2.55.0
Re: [PATCH v8 1/2] clk: en7523: convert to full clk_hw implementation for en7581
Quoted 44 lines
In preparation for support of .set_rate, convert the clock register logic from fixed clock implementation to full
clk_hwimplementation with dedicated OPs for Airoha EN7581 SoC.While at it also rework the
en7581_register_clocks()anden7523_register_pcie_clk()function to return error on clock register fail or on allocating dynamic memory.Signed-off-by Christian Marangi <[email protected]>--- drivers/clk/clk-en7523.c | 105 ++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 30 deletions(-)drivers/clk/clk-en7523.c +8
-1@@ -126,6 +126,12 @@ struct en_clk_gate {struct clk_hw hw; }; +struct en_clk { +struct regmap *map; +const struct en_clk_desc *desc; +struct clk_hw hw; +}; +struct en_rst_data {const u16 *bank_ofs;const u16 *idx_map;@@ -640,10 +646,11 @@ static struct clk_hw *en7523_register_pcie_clk(struct device *dev, .ops = &soc_data->pcie_ops, };struct en_clk_gate *cg; +int ret; cg =devm_kzalloc (dev,sizeof (*cg),GFP_KERNEL );if (!cg) -return NULL ; +return ERR_PTR (-ENOMEM ); cg->base = np_base; cg->hw.init = &init;
drivers/clk/clk-en7523.c static struct clk_hw *en7523_register_pcie_clk(struct device *dev, · line 661
Quoted 6 lines
drivers/clk/clk-en7523.c +1
-2@@ -651,8 +658,9 @@ static struct clk_hw *en7523_register_pcie_clk(struct device *dev,if (init.ops->unprepare) init.ops->unprepare (&cg->hw); -if (clk_hw_register (dev, &cg->hw)) -return NULL ; + ret =clk_hw_register (dev, &cg->hw);
Since you are making changes here, let's move this to the devm_ variant.
Quoted 24 lines
drivers/clk/clk-en7523.c +7
-3+if (ret) +return ERR_PTR (ret);return &cg->hw; }@@ -718,7 +726,8 @@ static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_dat } hw =en7523_register_pcie_clk (dev, np_base); - clk_data->hws[EN7523_CLK_PCIE ] = hw; +if (!IS_ERR (hw)) + clk_data->hws[EN7523_CLK_PCIE ] = hw; } static int en7523_clk_hw_init(struct platform_device *pdev,@@ -741,46 +750,79 @@ static int en7523_clk_hw_init(struct platform_device *pdev, en7581_rst_ofs); } -static void en7581_register_clocks (struct device *dev,struct clk_hw_onecell_data *clk_data, -struct regmap *map,void __iomem *base) +static unsigned long en7581_recalc_rate (struct clk_hw *hw, +unsigned long parent_rate) +{
drivers/clk/clk-en7523.c static int en7523_clk_hw_init(struct platform_device *pdev, · line 765
Quoted 10 lines
drivers/clk/clk-en7523.c +10
-0+ struct en_clk *c = container_of(hw, struct en_clk, hw); +const struct en_clk_desc *desc = c->desc; +struct device *dev =clk_hw_get_dev (hw); +struct regmap *map = c->map; +u32 val, reg, rate; +int err; + + err =regmap_read (map, desc->base_reg, &val); +if (err) { + dev_err(dev, "Failed reading fixed clk rate %s: %d\n",
recalc_rate can be called frequently, make this is a dev_dbg here and below. This is mmio regmap and unlikely to fail though.
Quoted 58 lines
drivers/clk/clk-en7523.c +36
-13+ desc->name, err); +return 0 ; + } + rate =en7523_get_base_rate (desc, val); + + reg = desc->div_reg ? desc->div_reg : desc->base_reg; + err =regmap_read (map, reg, &val); +if (err) { +dev_err (dev,"Failed reading fixed clk div %s: %d\n" , + desc->name, err); +return 0 ; + } + +return rate /en7523_get_div (desc, val); +} + +static const struct clk_ops en7581_clk_ops = { + .recalc_rate = en7581_recalc_rate, +}; + +static int en7581_register_clocks (struct device *dev,struct clk_hw_onecell_data *clk_data, +struct regmap *map,void __iomem *base) {struct clk_hw *hw; -u32 rate;int i;for (i =0 ; i <ARRAY_SIZE (en7581_base_clks); i++) {const struct en_clk_desc *desc = &en7581_base_clks[i]; -u32 val, reg = desc->div_reg ? desc->div_reg : desc->base_reg; +struct clk_init_data init = { + .ops = &en7581_clk_ops, + }; +struct en_clk *en_clk;int err; - err =regmap_read (map, desc->base_reg, &val); + en_clk =devm_kzalloc_obj (dev, *en_clk); +if (!en_clk) +return -ENOMEM ; + + init.name = desc->name; + en_clk->map = map; + en_clk->desc = desc; + en_clk->hw.init = &init; + + err =devm_clk_hw_register (dev, &en_clk->hw);if (err) { -pr_err ("Failed reading fixed clk rate %s: %d\n" , - desc->name, err); -continue ; - } - rate =en7523_get_base_rate (desc, val); - - err =regmap_read (map, reg, &val); -if (err) { -pr_err ("Failed reading fixed clk div %s: %d\n" , - desc->name, err);
drivers/clk/clk-en7523.c static int en7523_clk_hw_init(struct platform_device *pdev,
Quoted 10 lines
drivers/clk/clk-en7523.c +1
-9-continue ; - } - rate /=en7523_get_div (desc, val); - - hw =clk_hw_register_fixed_rate (dev, desc->name,NULL ,0 , rate); -if (IS_ERR (hw)) { -pr_err ("Failed to register clk %s: %ld\n" , - desc->name,PTR_ERR (hw)); -continue ; + dev_err_probe(dev, err, "Failed to register clk %s\n",
return dev_err_probe(...);
Brian
Signature · 1 line
Brian Masney <[email protected]>
PATCH v8 2/2 clk: en7523: add support for .set_rate for en7581
In reply to Christian MarangiAdd support for EN7523 driver to configure rate for Airoha EN7581 SoC. The SoC expose both base clock selector and clock divisor hence it's possible to change the rate.
This will be especially needed for new SoC AN7583 that require changes for the MDIO and the eMMC.
The clock were assumed correctly configured by the bootloader but this goes against the rule of "kernel should not depend on external configuration".
To support the use of standard clock divider API, clock_div_table are added where needed (BUS, SPI and SLIC) in additionl to the previous info to derive and simulate the clock from the divisor registers.
Recalc rate for Airoha EN7581 is updated to use the divider_recalc_rate API for consistency with .set_rate and .determine_rate.
en7523_get_div() is updated to use the div table to drop redundant values.
--- drivers/clk/clk-en7523.c | 341 +++++++++++++++++++++++++++++++++++---- 1 file changed, 307 insertions(+), 34 deletions(-)
drivers/clk/clk-en7523.c +307 -34
@@ -116,9 +116,7 @@ struct en_clk_desc {u16 div_reg;u8 div_bits;u8 div_shift; -u16 div_val0; -u8 div_step; -u8 div_offset; +const struct clk_div_table *div_table; };struct en_clk_gate {
@@ -161,7 +159,88 @@ struct en_clk_soc_data {static const u32 gsw_base[] = {400000000 ,500000000 };static const u32 emi_base[] = {333000000 ,400000000 };static const u32 bus_base[] = {500000000 ,540000000 }; +/* From documentation divisor 1 is n/a */ +static const struct clk_div_table bus_div[] = { + { .val =1 , .div =2 }, + { .val =2 , .div =3 }, + { .val =3 , .div =4 }, + { .val =4 , .div =5 }, + { .val =5 , .div =6 }, + { .val =6 , .div =7 }, + { .val =7 , .div =8 }, + {/* sentinel */ } +};static const u32 slic_base[] = {100000000 ,3125000 }; +static const struct clk_div_table slic_div[] = { + { .val =0 , .div =20 }, + { .val =1 , .div =2 }, + { .val =2 , .div =4 }, + { .val =3 , .div =6 }, + { .val =4 , .div =8 }, + { .val =5 , .div =10 }, + { .val =6 , .div =12 }, + { .val =7 , .div =14 }, + { .val =8 , .div =16 }, + { .val =9 , .div =18 }, + { .val =10 , .div =20 }, + { .val =11 , .div =22 }, + { .val =12 , .div =24 }, + { .val =13 , .div =26 }, + { .val =14 , .div =28 }, + { .val =15 , .div =30 }, + { .val =16 , .div =32 }, + { .val =17 , .div =34 }, + { .val =18 , .div =36 }, + { .val =19 , .div =38 }, + { .val =20 , .div =40 }, + { .val =21 , .div =42 }, + { .val =22 , .div =44 }, + { .val =23 , .div =46 }, + { .val =24 , .div =48 }, + { .val =25 , .div =50 }, + { .val =26 , .div =52 }, + { .val =27 , .div =54 }, + { .val =28 , .div =56 }, + { .val =29 , .div =58 }, + { .val =30 , .div =60 }, + { .val =31 , .div =62 }, + {/* sentinel */ } +}; +static const struct clk_div_table spi_div[] = { + { .val =0 , .div =40 }, + { .val =1 , .div =2 }, + { .val =2 , .div =4 }, + { .val =3 , .div =6 }, + { .val =4 , .div =8 }, + { .val =5 , .div =10 }, + { .val =6 , .div =12 }, + { .val =7 , .div =14 }, + { .val =8 , .div =16 }, + { .val =9 , .div =18 }, + { .val =10 , .div =20 }, + { .val =11 , .div =22 }, + { .val =12 , .div =24 }, + { .val =13 , .div =26 }, + { .val =14 , .div =28 }, + { .val =15 , .div =30 }, + { .val =16 , .div =32 }, + { .val =17 , .div =34 }, + { .val =18 , .div =36 }, + { .val =19 , .div =38 }, + { .val =20 , .div =40 }, + { .val =21 , .div =42 }, + { .val =22 , .div =44 }, + { .val =23 , .div =46 }, + { .val =24 , .div =48 }, + { .val =25 , .div =50 }, + { .val =26 , .div =52 }, + { .val =27 , .div =54 }, + { .val =28 , .div =56 }, + { .val =29 , .div =58 }, + { .val =30 , .div =60 }, + { .val =31 , .div =62 }, + {/* sentinel */ } +};static const u32 npu_base[] = {333000000 ,400000000 ,500000000 };/* EN7581 */ static const u32 emi7581_base[] = {540000000 ,480000000 ,400000000 ,300000000 };
@@ -185,8 +264,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi",
@@ -199,8 +276,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus",
@@ -213,8 +288,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic",
@@ -228,8 +302,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi",
@@ -240,8 +313,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu",
@@ -254,8 +326,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto",
@@ -281,8 +351,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi",
@@ -295,8 +363,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus",
@@ -309,8 +375,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic",
@@ -324,8 +389,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi",
@@ -336,8 +400,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu",
@@ -350,8 +413,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto",
@@ -568,16 +629,20 @@ static u32 en7523_get_base_rate(const struct en_clk_desc *desc, u32 val)static u32 en7523_get_div (const struct en_clk_desc *desc,u32 val) { -if (!desc->div_bits) -return 1 ; +const struct clk_div_table *clkt; val >>= desc->div_shift; val &= (1 << desc->div_bits) -1 ; -if (!val && desc->div_val0) -return desc->div_val0; +if (desc->div_table) { +for (clkt = desc->div_table; clkt->div; clkt++) +if (clkt->val == val) +return clkt->div; -return (val + desc->div_offset) * desc->div_step; +return 0 ; + } + +return val +1 ; }static int en7523_pci_is_enabled (struct clk_hw *hw)
@@ -710,10 +775,18 @@ static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_datconst struct en_clk_desc *desc = &en7523_base_clks[i];u32 reg = desc->div_reg ? desc->div_reg : desc->base_reg;u32 val =readl (base + desc->base_reg); +u32 div; rate =en7523_get_base_rate (desc, val); + val =readl (base + reg); - rate /=en7523_get_div (desc, val); +if (desc->div_bits) { + div =en7523_get_div (desc, val); +if (!div) +continue ; + + rate /= div; + } hw =clk_hw_register_fixed_rate (dev, desc->name,NULL ,0 , rate);if (IS_ERR (hw)) {
@@ -776,11 +849,211 @@ static unsigned long en7581_recalc_rate(struct clk_hw *hw,return 0 ; } -return rate /en7523_get_div (desc, val); +if (desc->div_bits) { + val >>= desc->div_shift; + val &= (1 << desc->div_bits) -1 ; + + rate =divider_recalc_rate (hw, rate, val, desc->div_table, +0 , desc->div_bits); + } + +return rate; +} + +/* +* Simulate rate with every divisor supported +* 1. Search the divisor for the requested rate +* 2. Simulate the final rate with the new divisor +*/ +static unsigned long en7581_get_div_for_rate (struct clk_hw *hw, +unsigned long rate, +unsigned long parent_rate, +const struct en_clk_desc *desc, +int *div_val) +{ + *div_val =divider_get_val (rate, parent_rate, desc->div_table, + desc->div_bits,0 ); +if (*div_val <0 ) +return 0 ; + +return divider_recalc_rate (hw, parent_rate, *div_val, + desc->div_table,0 , + desc->div_bits); +} + +static int en7581_get_vals_for_rate (struct clk_hw *hw, +const struct en_clk_desc *desc, +unsigned long rate, +u32 *base_val,u32 *div_val) +{ +struct device *dev =clk_hw_get_dev (hw); +int tmp_base_val =0 , tmp_div_val =0 ; +unsigned long parent_rate, tmp_rate; + +if (!rate || (!desc->base_bits && !desc->div_bits)) +return -EINVAL ; + +if (!desc->base_bits) { + parent_rate = desc->base_value; + + tmp_rate =en7581_get_div_for_rate (hw, rate, parent_rate, + desc, &tmp_div_val); + +/* Matching clock found */ +if (tmp_rate == rate) + goto exit; + } + +/* Search requested rate with every matching base rate + divisor */ +for (; tmp_base_val < desc->n_base_values; tmp_base_val++) { + parent_rate = desc->base_values[tmp_base_val]; + tmp_rate = parent_rate; + +if (desc->div_bits) + tmp_rate =en7581_get_div_for_rate (hw, rate, parent_rate, + desc, &tmp_div_val); + +/* Matching clock found */ +if (tmp_rate == rate) +break ; + } + +/* Impossible, should be validated by determine rate */ +if (tmp_base_val >= desc->n_base_values) { +dev_err (dev,"Invalid rate for clock %s\n" , + desc->name); +return -EINVAL ; + } + +exit: + *base_val = tmp_base_val; + *div_val = tmp_div_val; + +return 0 ; +} + +static void en7581_update_best_rate (struct clk_rate_request *req, +unsigned long requested_rate, +unsigned long tmp_rate, +unsigned long *best_diff) +{ +unsigned long diff; + +if (!tmp_rate || tmp_rate < req->min_rate || + tmp_rate > req->max_rate) +return ; + + diff =abs_diff (requested_rate, tmp_rate); +if (diff < *best_diff) { + *best_diff = diff; + req->rate = tmp_rate; + } +} + +static void en7581_find_best_rate (struct clk_hw *hw, +struct clk_rate_request *req, +const struct en_clk_desc *desc, +unsigned long parent_rate, +unsigned long requested_rate, +unsigned long *best_diff) +{ +struct clk_rate_request div_req = *req; +int ret; + +if (!desc->div_bits) { +en7581_update_best_rate (req, requested_rate, parent_rate, + best_diff); +return ; + } + +/* Treat each internal base rate as a fixed parent for the divider. */ + div_req.rate =clamp (requested_rate, req->min_rate, req->max_rate); + div_req.rate =min (div_req.rate, parent_rate); + div_req.best_parent_rate = parent_rate; + ret =divider_determine_rate (hw, &div_req, desc->div_table, + desc->div_bits, +CLK_DIVIDER_ROUND_CLOSEST ); +if (ret) +return ; + +en7581_update_best_rate (req, requested_rate, div_req.rate, best_diff); +} + +static int en7581_determine_rate (struct clk_hw *hw, +struct clk_rate_request *req) +{ + struct en_clk *en_clk = container_of(hw, struct en_clk, hw); +const struct en_clk_desc *desc = en_clk->desc; +unsigned long best_diff =ULONG_MAX ; +unsigned long rate = req->rate; +int tmp_base_val =0 ; + +if (!rate || (!desc->base_bits && !desc->div_bits)) +return -EINVAL ; + +if (!desc->base_bits) { +en7581_find_best_rate (hw, req, desc, desc->base_value, + rate, &best_diff); + goto exit; + } + +for (; tmp_base_val < desc->n_base_values; tmp_base_val++) +en7581_find_best_rate (hw, req, desc, + desc->base_values[tmp_base_val], + rate, &best_diff); + +exit: +return best_diff ==ULONG_MAX ? -EINVAL :0 ; +} + +static int en7581_set_rate (struct clk_hw *hw,unsigned long rate, +unsigned long parent_rate) +{ + struct en_clk *en_clk = container_of(hw, struct en_clk, hw); +const struct en_clk_desc *desc = en_clk->desc; +struct device *dev =clk_hw_get_dev (hw); +u32 base_val, div_val, reg, val, mask; +struct regmap *map = en_clk->map; +int err; + + err =en7581_get_vals_for_rate (hw, en_clk->desc, rate, + &base_val, &div_val); +if (err) +return err; + +if (desc->div_bits) { + reg = desc->div_reg ? desc->div_reg : desc->base_reg; + + mask = (BIT (desc->div_bits) -1 ) << desc->div_shift; + val = div_val << desc->div_shift; + + err =regmap_update_bits (map, reg, mask, val); +if (err) { +dev_err (dev,"Failed to update div reg for clock %s\n" , + desc->name); +return err; + } + } + +if (desc->base_bits) { + mask = (BIT (desc->base_bits) -1 ) << desc->base_shift; + val = base_val << desc->base_shift; + + err =regmap_update_bits (map, desc->base_reg, mask, val); +if (err) { +dev_err (dev,"Failed to update reg for clock %s\n" , + desc->name); +return err; + } + } + +return 0 ; }static const struct clk_ops en7581_clk_ops = { .recalc_rate = en7581_recalc_rate, + .determine_rate = en7581_determine_rate, + .set_rate = en7581_set_rate, }; static int en7581_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
Signature · 1 line
2.55.0
Re: [PATCH v8 2/2] clk: en7523: add support for .set_rate for en7581
Hi Christian,
Quoted 43 lines
Add support for EN7523 driver to configure rate for Airoha EN7581 SoC. The SoC expose both base clock selector and clock divisor hence it's possible to change the rate.
This will be especially needed for new SoC AN7583 that require changes for the MDIO and the eMMC.
The clock were assumed correctly configured by the bootloader but this goes against the rule of "kernel should not depend on external configuration".
To support the use of standard clock divider API,
clock_div_tableare added where needed (BUS, SPI and SLIC) in additionl to the previous info to derive and simulate the clock from the divisor registers.Recalc rate for Airoha EN7581 is updated to use the
divider_recalc_rateAPI for consistency with .set_rate and .determine_rate.
en7523_get_div()is updated to use the div table to drop redundant values.Signed-off-by Christian Marangi <[email protected]>--- drivers/clk/clk-en7523.c | 341 +++++++++++++++++++++++++++++++++++---- 1 file changed, 307 insertions(+), 34 deletions(-)drivers/clk/clk-en7523.c +1
-3@@ -116,9 +116,7 @@ struct en_clk_desc {u16 div_reg;u8 div_bits;u8 div_shift; -u16 div_val0; -u8 div_step; -u8 div_offset; +const struct clk_div_table *div_table; };struct en_clk_gate {@@ -161,7 +159,88 @@ struct en_clk_soc_data {static const u32 gsw_base[] = {400000000 ,500000000 };static const u32 emi_base[] = {333000000 ,400000000 };
drivers/clk/clk-en7523.c struct en_clk_soc_data { · line 170
Quoted 10 lines
drivers/clk/clk-en7523.c +9
-0static const u32 bus_base[] = {500000000 ,540000000 }; +/* From documentation divisor 1 is n/a */ +static const struct clk_div_table bus_div[] = { + { .val =1 , .div =2 }, + { .val =2 , .div =3 }, + { .val =3 , .div =4 }, + { .val =4 , .div =5 }, + { .val =5 , .div =6 }, + { .val =6 , .div =7 }, + { .val =7 , .div =8 },
Is there a case where the register can be 0? Should there be this entry as well?
{ .val = 0, .div = 1 }Quoted 220 lines
drivers/clk/clk-en7523.c +87
-29+ {/* sentinel */ } +};static const u32 slic_base[] = {100000000 ,3125000 }; +static const struct clk_div_table slic_div[] = { + { .val =0 , .div =20 }, + { .val =1 , .div =2 }, + { .val =2 , .div =4 }, + { .val =3 , .div =6 }, + { .val =4 , .div =8 }, + { .val =5 , .div =10 }, + { .val =6 , .div =12 }, + { .val =7 , .div =14 }, + { .val =8 , .div =16 }, + { .val =9 , .div =18 }, + { .val =10 , .div =20 }, + { .val =11 , .div =22 }, + { .val =12 , .div =24 }, + { .val =13 , .div =26 }, + { .val =14 , .div =28 }, + { .val =15 , .div =30 }, + { .val =16 , .div =32 }, + { .val =17 , .div =34 }, + { .val =18 , .div =36 }, + { .val =19 , .div =38 }, + { .val =20 , .div =40 }, + { .val =21 , .div =42 }, + { .val =22 , .div =44 }, + { .val =23 , .div =46 }, + { .val =24 , .div =48 }, + { .val =25 , .div =50 }, + { .val =26 , .div =52 }, + { .val =27 , .div =54 }, + { .val =28 , .div =56 }, + { .val =29 , .div =58 }, + { .val =30 , .div =60 }, + { .val =31 , .div =62 }, + {/* sentinel */ } +}; +static const struct clk_div_table spi_div[] = { + { .val =0 , .div =40 }, + { .val =1 , .div =2 }, + { .val =2 , .div =4 }, + { .val =3 , .div =6 }, + { .val =4 , .div =8 }, + { .val =5 , .div =10 }, + { .val =6 , .div =12 }, + { .val =7 , .div =14 }, + { .val =8 , .div =16 }, + { .val =9 , .div =18 }, + { .val =10 , .div =20 }, + { .val =11 , .div =22 }, + { .val =12 , .div =24 }, + { .val =13 , .div =26 }, + { .val =14 , .div =28 }, + { .val =15 , .div =30 }, + { .val =16 , .div =32 }, + { .val =17 , .div =34 }, + { .val =18 , .div =36 }, + { .val =19 , .div =38 }, + { .val =20 , .div =40 }, + { .val =21 , .div =42 }, + { .val =22 , .div =44 }, + { .val =23 , .div =46 }, + { .val =24 , .div =48 }, + { .val =25 , .div =50 }, + { .val =26 , .div =52 }, + { .val =27 , .div =54 }, + { .val =28 , .div =56 }, + { .val =29 , .div =58 }, + { .val =30 , .div =60 }, + { .val =31 , .div =62 }, + {/* sentinel */ } +};static const u32 npu_base[] = {333000000 ,400000000 ,500000000 };/* EN7581 */ static const u32 emi7581_base[] = {540000000 ,480000000 ,400000000 ,300000000 };@@ -185,8 +264,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi",@@ -199,8 +276,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus",@@ -213,8 +288,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic",@@ -228,8 +302,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi",@@ -240,8 +313,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu",@@ -254,8 +326,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto",@@ -281,8 +351,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi",@@ -295,8 +363,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus",@@ -309,8 +375,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic",@@ -324,8 +389,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi",@@ -336,8 +400,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu",@@ -350,8 +413,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto",@@ -568,16 +629,20 @@ static u32 en7523_get_base_rate(const struct en_clk_desc *desc, u32 val)static u32 en7523_get_div (const struct en_clk_desc *desc,u32 val) { -if (!desc->div_bits) -return 1 ; +const struct clk_div_table *clkt; val >>= desc->div_shift; val &= (1 << desc->div_bits) -1 ; -if (!val && desc->div_val0) -return desc->div_val0; +if (desc->div_table) { +for (clkt = desc->div_table; clkt->div; clkt++) +if (clkt->val == val) +return clkt->div; -return (val + desc->div_offset) * desc->div_step; +return 0 ; + } + +return val +1 ; }static int en7523_pci_is_enabled (struct clk_hw *hw)@@ -710,10 +775,18 @@ static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_datconst struct en_clk_desc *desc = &en7523_base_clks[i];u32 reg = desc->div_reg ? desc->div_reg : desc->base_reg;u32 val =readl (base + desc->base_reg);
drivers/clk/clk-en7523.c static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_dat · line 786
Quoted 10 lines
drivers/clk/clk-en7523.c +6
-1+u32 div; rate =en7523_get_base_rate (desc, val); + val =readl (base + reg); - rate /=en7523_get_div (desc, val); +if (desc->div_bits) { + div =en7523_get_div (desc, val); +if (!div) +continue ;
Should there be some kind of log message here?
Quoted 74 lines
drivers/clk/clk-en7523.c +66
-1+ + rate /= div; + } hw =clk_hw_register_fixed_rate (dev, desc->name,NULL ,0 , rate);if (IS_ERR (hw)) {@@ -776,11 +849,211 @@ static unsigned long en7581_recalc_rate(struct clk_hw *hw,return 0 ; } -return rate /en7523_get_div (desc, val); +if (desc->div_bits) { + val >>= desc->div_shift; + val &= (1 << desc->div_bits) -1 ; + + rate =divider_recalc_rate (hw, rate, val, desc->div_table, +0 , desc->div_bits); + } + +return rate; +} + +/* +* Simulate rate with every divisor supported +* 1. Search the divisor for the requested rate +* 2. Simulate the final rate with the new divisor +*/ +static unsigned long en7581_get_div_for_rate (struct clk_hw *hw, +unsigned long rate, +unsigned long parent_rate, +const struct en_clk_desc *desc, +int *div_val) +{ + *div_val =divider_get_val (rate, parent_rate, desc->div_table, + desc->div_bits,0 ); +if (*div_val <0 ) +return 0 ; + +return divider_recalc_rate (hw, parent_rate, *div_val, + desc->div_table,0 , + desc->div_bits); +} + +static int en7581_get_vals_for_rate (struct clk_hw *hw, +const struct en_clk_desc *desc, +unsigned long rate, +u32 *base_val,u32 *div_val) +{ +struct device *dev =clk_hw_get_dev (hw); +int tmp_base_val =0 , tmp_div_val =0 ; +unsigned long parent_rate, tmp_rate; + +if (!rate || (!desc->base_bits && !desc->div_bits)) +return -EINVAL ; + +if (!desc->base_bits) { + parent_rate = desc->base_value; + + tmp_rate =en7581_get_div_for_rate (hw, rate, parent_rate, + desc, &tmp_div_val); + +/* Matching clock found */ +if (tmp_rate == rate) + goto exit; + } + +/* Search requested rate with every matching base rate + divisor */ +for (; tmp_base_val < desc->n_base_values; tmp_base_val++) { + parent_rate = desc->base_values[tmp_base_val]; + tmp_rate = parent_rate; + +if (desc->div_bits) + tmp_rate =en7581_get_div_for_rate (hw, rate, parent_rate, + desc, &tmp_div_val);
drivers/clk/clk-en7523.c static unsigned long en7581_recalc_rate(struct clk_hw *hw, · line 924
Quoted 10 lines
drivers/clk/clk-en7523.c +10
-0+ +/* Matching clock found */ +if (tmp_rate == rate) +break ; + } + +/* Impossible, should be validated by determine rate */ +if (tmp_base_val >= desc->n_base_values) { +dev_err (dev,"Invalid rate for clock %s\n" , + desc->name);
dev_err() shouldn't be in the hot path of set_rate(). Make this a dev_dbg() as well.
Quoted 37 lines
drivers/clk/clk-en7523.c +37
-0+return -EINVAL ; + } + +exit: + *base_val = tmp_base_val; + *div_val = tmp_div_val; + +return 0 ; +} + +static void en7581_update_best_rate (struct clk_rate_request *req, +unsigned long requested_rate, +unsigned long tmp_rate, +unsigned long *best_diff) +{ +unsigned long diff; + +if (!tmp_rate || tmp_rate < req->min_rate || + tmp_rate > req->max_rate) +return ; + + diff =abs_diff (requested_rate, tmp_rate); +if (diff < *best_diff) { + *best_diff = diff; + req->rate = tmp_rate; + } +} + +static void en7581_find_best_rate (struct clk_hw *hw, +struct clk_rate_request *req, +const struct en_clk_desc *desc, +unsigned long parent_rate, +unsigned long requested_rate, +unsigned long *best_diff) +{ +struct clk_rate_request div_req = *req; +int ret;
drivers/clk/clk-en7523.c static unsigned long en7581_recalc_rate(struct clk_hw *hw, · line 971
Quoted 10 lines
drivers/clk/clk-en7523.c +10
-0+ +if (!desc->div_bits) { +en7581_update_best_rate (req, requested_rate, parent_rate, + best_diff); +return ; + } + +/* Treat each internal base rate as a fixed parent for the divider. */ + div_req.rate =clamp (requested_rate, req->min_rate, req->max_rate); + div_req.rate =min (div_req.rate, parent_rate);
The min() here shouldn't be needed. When rate >= parent_rate, the smallest table divider is returned.
Also just a heads up that I posted a guidelines file for the Common Clk Subsystem to the review prompts repo. It's not merged as of this writing, but Sashiko will start using this once it's merged. You can download the clk.md file and point your favorite $AI_AGENT at this review file before posting your next version.
https://github.com/masoncl/review-prompts/pull/123
Brian
Signature · 1 line
Brian Masney <[email protected]>
Re: [PATCH v8 2/2] clk: en7523: add support for .set_rate for en7581
Quoted 59 lines · Brian Masney
Hi Christian,
Quoted 43 lines
Add support for EN7523 driver to configure rate for Airoha EN7581 SoC. The SoC expose both base clock selector and clock divisor hence it's possible to change the rate.
This will be especially needed for new SoC AN7583 that require changes for the MDIO and the eMMC.
The clock were assumed correctly configured by the bootloader but this goes against the rule of "kernel should not depend on external configuration".
To support the use of standard clock divider API,
clock_div_tableare added where needed (BUS, SPI and SLIC) in additionl to the previous info to derive and simulate the clock from the divisor registers.Recalc rate for Airoha EN7581 is updated to use the
divider_recalc_rateAPI for consistency with .set_rate and .determine_rate.
en7523_get_div()is updated to use the div table to drop redundant values.Signed-off-by Christian Marangi <[email protected]>--- drivers/clk/clk-en7523.c | 341 +++++++++++++++++++++++++++++++++++---- 1 file changed, 307 insertions(+), 34 deletions(-)drivers/clk/clk-en7523.c +1
-3@@ -116,9 +116,7 @@ struct en_clk_desc {u16 div_reg;u8 div_bits;u8 div_shift; -u16 div_val0; -u8 div_step; -u8 div_offset; +const struct clk_div_table *div_table; };struct en_clk_gate {@@ -161,7 +159,88 @@ struct en_clk_soc_data {static const u32 gsw_base[] = {400000000 ,500000000 };static const u32 emi_base[] = {333000000 ,400000000 };drivers/clk/clk-en7523.c struct en_clk_soc_data { · line 170
Quoted 10 lines
drivers/clk/clk-en7523.c +9
-0static const u32 bus_base[] = {500000000 ,540000000 }; +/* From documentation divisor 1 is n/a */ +static const struct clk_div_table bus_div[] = { + { .val =1 , .div =2 }, + { .val =2 , .div =3 }, + { .val =3 , .div =4 }, + { .val =4 , .div =5 }, + { .val =5 , .div =6 }, + { .val =6 , .div =7 }, + { .val =7 , .div =8 },Is there a case where the register can be 0? Should there be this entry as well?
Quoted 2 lines
{ .val = 0, .div = 1 }BUS is special. val 0 is invalid and rejected by the HW. This was already handled by the pre-div-api implementation.
Quoted 222 lines
Quoted 222 lines
drivers/clk/clk-en7523.c +88
-29+ {/* sentinel */ } +};static const u32 slic_base[] = {100000000 ,3125000 }; +static const struct clk_div_table slic_div[] = { + { .val =0 , .div =20 }, + { .val =1 , .div =2 }, + { .val =2 , .div =4 }, + { .val =3 , .div =6 }, + { .val =4 , .div =8 }, + { .val =5 , .div =10 }, + { .val =6 , .div =12 }, + { .val =7 , .div =14 }, + { .val =8 , .div =16 }, + { .val =9 , .div =18 }, + { .val =10 , .div =20 }, + { .val =11 , .div =22 }, + { .val =12 , .div =24 }, + { .val =13 , .div =26 }, + { .val =14 , .div =28 }, + { .val =15 , .div =30 }, + { .val =16 , .div =32 }, + { .val =17 , .div =34 }, + { .val =18 , .div =36 }, + { .val =19 , .div =38 }, + { .val =20 , .div =40 }, + { .val =21 , .div =42 }, + { .val =22 , .div =44 }, + { .val =23 , .div =46 }, + { .val =24 , .div =48 }, + { .val =25 , .div =50 }, + { .val =26 , .div =52 }, + { .val =27 , .div =54 }, + { .val =28 , .div =56 }, + { .val =29 , .div =58 }, + { .val =30 , .div =60 }, + { .val =31 , .div =62 }, + {/* sentinel */ } +}; +static const struct clk_div_table spi_div[] = { + { .val =0 , .div =40 }, + { .val =1 , .div =2 }, + { .val =2 , .div =4 }, + { .val =3 , .div =6 }, + { .val =4 , .div =8 }, + { .val =5 , .div =10 }, + { .val =6 , .div =12 }, + { .val =7 , .div =14 }, + { .val =8 , .div =16 }, + { .val =9 , .div =18 }, + { .val =10 , .div =20 }, + { .val =11 , .div =22 }, + { .val =12 , .div =24 }, + { .val =13 , .div =26 }, + { .val =14 , .div =28 }, + { .val =15 , .div =30 }, + { .val =16 , .div =32 }, + { .val =17 , .div =34 }, + { .val =18 , .div =36 }, + { .val =19 , .div =38 }, + { .val =20 , .div =40 }, + { .val =21 , .div =42 }, + { .val =22 , .div =44 }, + { .val =23 , .div =46 }, + { .val =24 , .div =48 }, + { .val =25 , .div =50 }, + { .val =26 , .div =52 }, + { .val =27 , .div =54 }, + { .val =28 , .div =56 }, + { .val =29 , .div =58 }, + { .val =30 , .div =60 }, + { .val =31 , .div =62 }, + {/* sentinel */ } +};static const u32 npu_base[] = {333000000 ,400000000 ,500000000 };/* EN7581 */ static const u32 emi7581_base[] = {540000000 ,480000000 ,400000000 ,300000000 };@@ -185,8 +264,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi",@@ -199,8 +276,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus",@@ -213,8 +288,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic",@@ -228,8 +302,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi",@@ -240,8 +313,7 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu",@@ -254,8 +326,6 @@ static const struct en_clk_desc en7523_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto",@@ -281,8 +351,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_EMI, .name = "emi",@@ -295,8 +363,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_BUS, .name = "bus",@@ -309,8 +375,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, + .div_table = bus_div, }, { .id = EN7523_CLK_SLIC, .name = "slic",@@ -324,8 +389,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_reg = REG_SPI_CLK_DIV_SEL, .div_bits = 5, .div_shift = 24, - .div_val0 = 20, - .div_step = 2, + .div_table = slic_div, }, { .id = EN7523_CLK_SPI, .name = "spi",@@ -336,8 +400,7 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 5, .div_shift = 8, - .div_val0 = 40, - .div_step = 2, + .div_table = spi_div, }, { .id = EN7523_CLK_NPU, .name = "npu",@@ -350,8 +413,6 @@ static const struct en_clk_desc en7581_base_clks[] = { .div_bits = 3, .div_shift = 0, - .div_step = 1, - .div_offset = 1, }, { .id = EN7523_CLK_CRYPTO, .name = "crypto",@@ -568,16 +629,20 @@ static u32 en7523_get_base_rate(const struct en_clk_desc *desc, u32 val)static u32 en7523_get_div (const struct en_clk_desc *desc,u32 val) { -if (!desc->div_bits) -return 1 ; +const struct clk_div_table *clkt; val >>= desc->div_shift; val &= (1 << desc->div_bits) -1 ; -if (!val && desc->div_val0) -return desc->div_val0; +if (desc->div_table) { +for (clkt = desc->div_table; clkt->div; clkt++) +if (clkt->val == val) +return clkt->div; -return (val + desc->div_offset) * desc->div_step; +return 0 ; + } + +return val +1 ; }static int en7523_pci_is_enabled (struct clk_hw *hw)@@ -710,10 +775,18 @@ static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_datconst struct en_clk_desc *desc = &en7523_base_clks[i];u32 reg = desc->div_reg ? desc->div_reg : desc->base_reg;u32 val =readl (base + desc->base_reg); +u32 div;
Quoted 11 lines
drivers/clk/clk-en7523.c static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_dat · line 786
Quoted 8 lines
drivers/clk/clk-en7523.c +5
-1rate =en7523_get_base_rate (desc, val); + val =readl (base + reg); - rate /=en7523_get_div (desc, val); +if (desc->div_bits) { + div =en7523_get_div (desc, val); +if (!div) +continue ;Should there be some kind of log message here?
dbg or err?
Quoted 139 lines
Quoted 74 lines
drivers/clk/clk-en7523.c +66
-1+ + rate /= div; + } hw =clk_hw_register_fixed_rate (dev, desc->name,NULL ,0 , rate);if (IS_ERR (hw)) {@@ -776,11 +849,211 @@ static unsigned long en7581_recalc_rate(struct clk_hw *hw,return 0 ; } -return rate /en7523_get_div (desc, val); +if (desc->div_bits) { + val >>= desc->div_shift; + val &= (1 << desc->div_bits) -1 ; + + rate =divider_recalc_rate (hw, rate, val, desc->div_table, +0 , desc->div_bits); + } + +return rate; +} + +/* +* Simulate rate with every divisor supported +* 1. Search the divisor for the requested rate +* 2. Simulate the final rate with the new divisor +*/ +static unsigned long en7581_get_div_for_rate (struct clk_hw *hw, +unsigned long rate, +unsigned long parent_rate, +const struct en_clk_desc *desc, +int *div_val) +{ + *div_val =divider_get_val (rate, parent_rate, desc->div_table, + desc->div_bits,0 ); +if (*div_val <0 ) +return 0 ; + +return divider_recalc_rate (hw, parent_rate, *div_val, + desc->div_table,0 , + desc->div_bits); +} + +static int en7581_get_vals_for_rate (struct clk_hw *hw, +const struct en_clk_desc *desc, +unsigned long rate, +u32 *base_val,u32 *div_val) +{ +struct device *dev =clk_hw_get_dev (hw); +int tmp_base_val =0 , tmp_div_val =0 ; +unsigned long parent_rate, tmp_rate; + +if (!rate || (!desc->base_bits && !desc->div_bits)) +return -EINVAL ; + +if (!desc->base_bits) { + parent_rate = desc->base_value; + + tmp_rate =en7581_get_div_for_rate (hw, rate, parent_rate, + desc, &tmp_div_val); + +/* Matching clock found */ +if (tmp_rate == rate) + goto exit; + } + +/* Search requested rate with every matching base rate + divisor */ +for (; tmp_base_val < desc->n_base_values; tmp_base_val++) { + parent_rate = desc->base_values[tmp_base_val]; + tmp_rate = parent_rate; + +if (desc->div_bits) + tmp_rate =en7581_get_div_for_rate (hw, rate, parent_rate, + desc, &tmp_div_val);drivers/clk/clk-en7523.c static unsigned long en7581_recalc_rate(struct clk_hw *hw, · line 924
Quoted 10 lines
drivers/clk/clk-en7523.c +10
-0+ +/* Matching clock found */ +if (tmp_rate == rate) +break ; + } + +/* Impossible, should be validated by determine rate */ +if (tmp_base_val >= desc->n_base_values) { +dev_err (dev,"Invalid rate for clock %s\n" , + desc->name);
dev_err()shouldn't be in the hot path ofset_rate(). Make this adev_dbg()as well.Quoted 37 lines
drivers/clk/clk-en7523.c +37
-0+return -EINVAL ; + } + +exit: + *base_val = tmp_base_val; + *div_val = tmp_div_val; + +return 0 ; +} + +static void en7581_update_best_rate (struct clk_rate_request *req, +unsigned long requested_rate, +unsigned long tmp_rate, +unsigned long *best_diff) +{ +unsigned long diff; + +if (!tmp_rate || tmp_rate < req->min_rate || + tmp_rate > req->max_rate) +return ; + + diff =abs_diff (requested_rate, tmp_rate); +if (diff < *best_diff) { + *best_diff = diff; + req->rate = tmp_rate; + } +} + +static void en7581_find_best_rate (struct clk_hw *hw, +struct clk_rate_request *req, +const struct en_clk_desc *desc, +unsigned long parent_rate, +unsigned long requested_rate, +unsigned long *best_diff) +{ +struct clk_rate_request div_req = *req; +int ret;drivers/clk/clk-en7523.c static unsigned long en7581_recalc_rate(struct clk_hw *hw, · line 971
Quoted 10 lines
drivers/clk/clk-en7523.c +10
-0+ +if (!desc->div_bits) { +en7581_update_best_rate (req, requested_rate, parent_rate, + best_diff); +return ; + } + +/* Treat each internal base rate as a fixed parent for the divider. */ + div_req.rate =clamp (requested_rate, req->min_rate, req->max_rate); + div_req.rate =min (div_req.rate, parent_rate);The
min()here shouldn't be needed. When rate >=parent_rate, the smallest table divider is returned.
Quoted 8 lines
Also just a heads up that I posted a guidelines file for the Common Clk Subsystem to the review prompts repo. It's not merged as of this writing, but Sashiko will start using this once it's merged. You can download the clk.md file and point your favorite $AI_AGENT at this review file before posting your next version.
This was something that sashiko pointed out... the min is needed to prevent a div by zero. The previous revision pointed out this.
Since you requested these changes, I should merge these changes with the 7583 series correct?
Signature · 1 line
Ansuel
Re: [PATCH v8 2/2] clk: en7523: add support for .set_rate for en7581
Quoted 9 lines · Christian Marangi
Quoted 5 lines · Brian Masney
Is there a case where the register can be 0? Should there be this entry as well?
{ .val = 0, .div = 1 }BUS is special. val 0 is invalid and rejected by the HW. This was already handled by the pre-div-api implementation.
OK makes sense. Thanks for confirming.
Quoted 19 lines
Quoted 7 lines
Quoted 7 lines
static int
en7523_pci_is_enabled(struct clk_hw *hw)drivers/clk/clk-en7523.c +1
-0@@ -710,10 +775,18 @@ static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_datconst struct en_clk_desc *desc = &en7523_base_clks[i];u32 reg = desc->div_reg ? desc->div_reg : desc->base_reg;u32 val =readl (base + desc->base_reg); +u32 div;Quoted 11 lines
drivers/clk/clk-en7523.c static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_dat · line 786
Quoted 8 lines
drivers/clk/clk-en7523.c +5
-1rate =en7523_get_base_rate (desc, val); + val =readl (base + reg); - rate /=en7523_get_div (desc, val); +if (desc->div_bits) { + div =en7523_get_div (desc, val); +if (!div) +continue ;Should there be some kind of log message here?
Quoted 1 line
dbg or err?
Since this is called in the setup via probe, dev_err.
Quoted 29 lines
Quoted 29 lines
Quoted 9 lines
drivers/clk/clk-en7523.c +9
-0+static void en7581_find_best_rate (struct clk_hw *hw, +struct clk_rate_request *req, +const struct en_clk_desc *desc, +unsigned long parent_rate, +unsigned long requested_rate, +unsigned long *best_diff) +{ +struct clk_rate_request div_req = *req; +int ret;drivers/clk/clk-en7523.c static unsigned long en7581_recalc_rate(struct clk_hw *hw, · line 971
Quoted 10 lines
drivers/clk/clk-en7523.c +10
-0+ +if (!desc->div_bits) { +en7581_update_best_rate (req, requested_rate, parent_rate, + best_diff); +return ; + } + +/* Treat each internal base rate as a fixed parent for the divider. */ + div_req.rate =clamp (requested_rate, req->min_rate, req->max_rate); + div_req.rate =min (div_req.rate, parent_rate);The
min()here shouldn't be needed. When rate >=parent_rate, the smallest table divider is returned.Also just a heads up that I posted a guidelines file for the Common Clk Subsystem to the review prompts repo. It's not merged as of this writing, but Sashiko will start using this once it's merged. You can download the clk.md file and point your favorite $AI_AGENT at this review file before posting your next version.
Quoted 5 lines
Quoted 2 lines
This was something that sashiko pointed out... the min is needed to prevent a div by zero. The previous revision pointed out this.
OK that's fine. It's harmless.
Quoted 2 lines
Since you requested these changes, I should merge these changes with the 7583 series correct?
Yes please since it depends on the set_rate() changes.
Brian
Il giorno sab 21 nov 2026 alle ore 22:59 Christian Marangi <[email protected]> ha scritto:
Quoted 38 lines
This simple patch convert the en7523 clk for EN7581 to full clk and adds the .set_rate OPs.
Needed mostly to configure MDC clk rate.
This will be also implemented by AN7583 since they share the same exact implementation.
Changes v8:
- Handle corner case with div by zero inen7581_find_best_rateChanges v7:
- Address some formal improvement from Brain
- Back again to register clock returning error
- Actually round rate on determine rateChanges v6:
- Rework to usediv_tableentirely
- Switchdetermine_rateto calculate best rateChanges v5:
- Handle requesting 0 rateChanges v4:
- Fix regression wherebase_bitsis 0
- Handledivider_get_valreturning-EINVALChanges v3:
- Rework to clock divisor API
- Return error instead of-EINVAL
- Usedev_errwhere possible
- Usedevm_kzalloc_obj
- Revert to not fail on clk registration failureChanges v2:
- Fixrecalc_ratereturn value on error
Quoted 7 lines
Christian Marangi (2):
clk: en7523: convert to fullclk_hwimplementation for en7581
clk: en7523: add support for .set_rate for en7581drivers/clk/clk-en7523.c | 444 +++++++++++++++++++++++++++++++++------ 1 file changed, 381 insertions(+), 63 deletions(-)
Hi Brian,
sashiko seems happy now... Any chance we can pick this (or 7583) series?
It's currently blocking 7589 development.