[PATCH 1/2] clk: stm32: add support for a parent table in the muxes
From: Gabriel Fernandez
Date: Fri Nov 27 2026 - 05:36:31 EST
Some muxes have values of their selector field that are reserved and
must not be selected, so the parent index can't always be taken as the
register value.
struct stm32_mux_cfg already has a table member that is not used by the
muxes. Use it to translate between the parent index and the value of the
selector field, as the generic clk-mux does. When no table is provided,
the behavior is unchanged.
A value read from the hardware that is not in the table is reported as an
out of range index, so the clock is seen as an orphan by the framework,
and setting a parent index not in the table is refused.
Signed-off-by: Gabriel Fernandez <gabriel.fernandez@xxxxxxxxxxx>
---
drivers/clk/stm32/clk-stm32-core.c | 35 +++++++++++++++++++++++++++++------
drivers/clk/stm32/clk-stm32-core.h | 3 ++-
2 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/drivers/clk/stm32/clk-stm32-core.c b/drivers/clk/stm32/clk-stm32-core.c
index edabc160a6f2..d426e69f8b5c 100644
--- a/drivers/clk/stm32/clk-stm32-core.c
+++ b/drivers/clk/stm32/clk-stm32-core.c
@@ -9,6 +9,7 @@
#include <linux/device.h>
#include <linux/err.h>
#include <linux/io.h>
+#include <linux/limits.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/slab.h>
@@ -105,11 +106,20 @@ static u8 stm32_mux_get_parent(void __iomem *base,
const struct stm32_mux_cfg *mux = &data->muxes[mux_id];
u32 mask = BIT(mux->width) - 1;
u32 val;
+ int i;
val = readl(base + mux->offset) >> mux->shift;
val &= mask;
- return val;
+ if (!mux->table)
+ return val;
+
+ for (i = 0; i < mux->num_table; i++)
+ if (mux->table[i] == val)
+ return i;
+
+ /* u8 return type: report an invalid index (>= num_parents) */
+ return U8_MAX;
}
static int stm32_mux_set_parent(void __iomem *base,
@@ -120,10 +130,17 @@ static int stm32_mux_set_parent(void __iomem *base,
u32 mask = BIT(mux->width) - 1;
u32 reg = readl(base + mux->offset);
- u32 val = index << mux->shift;
+ u32 val = index;
+
+ if (mux->table) {
+ if (index >= mux->num_table)
+ return -EINVAL;
+
+ val = mux->table[index];
+ }
reg &= ~(mask << mux->shift);
- reg |= val;
+ reg |= val << mux->shift;
writel(reg, base + mux->offset);
@@ -266,14 +283,15 @@ static int clk_stm32_mux_set_parent(struct clk_hw *hw, u8 index)
{
struct clk_stm32_mux *mux = to_clk_stm32_mux(hw);
unsigned long flags = 0;
+ int ret;
spin_lock_irqsave(mux->lock, flags);
- stm32_mux_set_parent(mux->base, mux->clock_data, mux->mux_id, index);
+ ret = stm32_mux_set_parent(mux->base, mux->clock_data, mux->mux_id, index);
spin_unlock_irqrestore(mux->lock, flags);
- return 0;
+ return ret;
}
const struct clk_ops clk_stm32_mux_ops = {
@@ -466,13 +484,18 @@ static int clk_stm32_composite_set_parent(struct clk_hw *hw, u8 index)
{
struct clk_stm32_composite *composite = to_clk_stm32_composite(hw);
unsigned long flags = 0;
+ int ret;
spin_lock_irqsave(composite->lock, flags);
- stm32_mux_set_parent(composite->base, composite->clock_data, composite->mux_id, index);
+ ret = stm32_mux_set_parent(composite->base, composite->clock_data,
+ composite->mux_id, index);
spin_unlock_irqrestore(composite->lock, flags);
+ if (ret)
+ return ret;
+
if (composite->clock_data->is_multi_mux) {
struct clk_hw *other_mux_hw = composite->clock_data->is_multi_mux(hw);
diff --git a/drivers/clk/stm32/clk-stm32-core.h b/drivers/clk/stm32/clk-stm32-core.h
index 4be103710e12..eb07ade7f2cd 100644
--- a/drivers/clk/stm32/clk-stm32-core.h
+++ b/drivers/clk/stm32/clk-stm32-core.h
@@ -13,7 +13,8 @@ struct stm32_mux_cfg {
u8 shift;
u8 width;
u8 flags;
- u32 *table;
+ const u32 *table;
+ u8 num_table;
u8 ready;
};
--
2.43.0