1 /* 2 * Copyright (c) 2015 Linaro Ltd. 3 * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/clk-provider.h> 16 #include <linux/mfd/syscon.h> 17 #include <linux/slab.h> 18 19 #include "clk-mtk.h" 20 #include "clk-cpumux.h" 21 22 static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw) 23 { 24 return container_of(_hw, struct mtk_clk_cpumux, hw); 25 } 26 27 static u8 clk_cpumux_get_parent(struct clk_hw *hw) 28 { 29 struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw); 30 int num_parents = clk_hw_get_num_parents(hw); 31 unsigned int val; 32 33 regmap_read(mux->regmap, mux->reg, &val); 34 35 val >>= mux->shift; 36 val &= mux->mask; 37 38 if (val >= num_parents) 39 return -EINVAL; 40 41 return val; 42 } 43 44 static int clk_cpumux_set_parent(struct clk_hw *hw, u8 index) 45 { 46 struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw); 47 u32 mask, val; 48 49 val = index << mux->shift; 50 mask = mux->mask << mux->shift; 51 52 return regmap_update_bits(mux->regmap, mux->reg, mask, val); 53 } 54 55 static const struct clk_ops clk_cpumux_ops = { 56 .get_parent = clk_cpumux_get_parent, 57 .set_parent = clk_cpumux_set_parent, 58 }; 59 60 static struct clk __init * 61 mtk_clk_register_cpumux(const struct mtk_composite *mux, 62 struct regmap *regmap) 63 { 64 struct mtk_clk_cpumux *cpumux; 65 struct clk *clk; 66 struct clk_init_data init; 67 68 cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL); 69 if (!cpumux) 70 return ERR_PTR(-ENOMEM); 71 72 init.name = mux->name; 73 init.ops = &clk_cpumux_ops; 74 init.parent_names = mux->parent_names; 75 init.num_parents = mux->num_parents; 76 init.flags = mux->flags; 77 78 cpumux->reg = mux->mux_reg; 79 cpumux->shift = mux->mux_shift; 80 cpumux->mask = BIT(mux->mux_width) - 1; 81 cpumux->regmap = regmap; 82 cpumux->hw.init = &init; 83 84 clk = clk_register(NULL, &cpumux->hw); 85 if (IS_ERR(clk)) 86 kfree(cpumux); 87 88 return clk; 89 } 90 91 int __init mtk_clk_register_cpumuxes(struct device_node *node, 92 const struct mtk_composite *clks, int num, 93 struct clk_onecell_data *clk_data) 94 { 95 int i; 96 struct clk *clk; 97 struct regmap *regmap; 98 99 regmap = syscon_node_to_regmap(node); 100 if (IS_ERR(regmap)) { 101 pr_err("Cannot find regmap for %s: %ld\n", node->full_name, 102 PTR_ERR(regmap)); 103 return PTR_ERR(regmap); 104 } 105 106 for (i = 0; i < num; i++) { 107 const struct mtk_composite *mux = &clks[i]; 108 109 clk = mtk_clk_register_cpumux(mux, regmap); 110 if (IS_ERR(clk)) { 111 pr_err("Failed to register clk %s: %ld\n", 112 mux->name, PTR_ERR(clk)); 113 continue; 114 } 115 116 clk_data->clks[mux->id] = clk; 117 } 118 119 return 0; 120 } 121