1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Linaro Ltd.
4 * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
7 #include <linux/clk-provider.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/slab.h>
12 #include "clk-cpumux.h"
14 static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw)
16 return container_of(_hw, struct mtk_clk_cpumux, hw);
19 static u8 clk_cpumux_get_parent(struct clk_hw *hw)
21 struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
24 regmap_read(mux->regmap, mux->reg, &val);
32 static int clk_cpumux_set_parent(struct clk_hw *hw, u8 index)
34 struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
37 val = index << mux->shift;
38 mask = mux->mask << mux->shift;
40 return regmap_update_bits(mux->regmap, mux->reg, mask, val);
43 static const struct clk_ops clk_cpumux_ops = {
44 .get_parent = clk_cpumux_get_parent,
45 .set_parent = clk_cpumux_set_parent,
49 mtk_clk_register_cpumux(const struct mtk_composite *mux,
50 struct regmap *regmap)
52 struct mtk_clk_cpumux *cpumux;
54 struct clk_init_data init;
56 cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL);
58 return ERR_PTR(-ENOMEM);
60 init.name = mux->name;
61 init.ops = &clk_cpumux_ops;
62 init.parent_names = mux->parent_names;
63 init.num_parents = mux->num_parents;
64 init.flags = mux->flags;
66 cpumux->reg = mux->mux_reg;
67 cpumux->shift = mux->mux_shift;
68 cpumux->mask = BIT(mux->mux_width) - 1;
69 cpumux->regmap = regmap;
70 cpumux->hw.init = &init;
72 clk = clk_register(NULL, &cpumux->hw);
79 int mtk_clk_register_cpumuxes(struct device_node *node,
80 const struct mtk_composite *clks, int num,
81 struct clk_onecell_data *clk_data)
85 struct regmap *regmap;
87 regmap = syscon_node_to_regmap(node);
89 pr_err("Cannot find regmap for %pOF: %ld\n", node,
91 return PTR_ERR(regmap);
94 for (i = 0; i < num; i++) {
95 const struct mtk_composite *mux = &clks[i];
97 clk = mtk_clk_register_cpumux(mux, regmap);
99 pr_err("Failed to register clk %s: %ld\n",
100 mux->name, PTR_ERR(clk));
104 clk_data->clks[mux->id] = clk;