clk: mediatek: cpumux: Implement unregister API
[linux-2.6-microblaze.git] / drivers / clk / mediatek / clk-cpumux.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 Linaro Ltd.
4  * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
5  */
6
7 #include <linux/clk-provider.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11
12 #include "clk-mtk.h"
13 #include "clk-cpumux.h"
14
15 static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw)
16 {
17         return container_of(_hw, struct mtk_clk_cpumux, hw);
18 }
19
20 static u8 clk_cpumux_get_parent(struct clk_hw *hw)
21 {
22         struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
23         unsigned int val;
24
25         regmap_read(mux->regmap, mux->reg, &val);
26
27         val >>= mux->shift;
28         val &= mux->mask;
29
30         return val;
31 }
32
33 static int clk_cpumux_set_parent(struct clk_hw *hw, u8 index)
34 {
35         struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
36         u32 mask, val;
37
38         val = index << mux->shift;
39         mask = mux->mask << mux->shift;
40
41         return regmap_update_bits(mux->regmap, mux->reg, mask, val);
42 }
43
44 static const struct clk_ops clk_cpumux_ops = {
45         .get_parent = clk_cpumux_get_parent,
46         .set_parent = clk_cpumux_set_parent,
47 };
48
49 static struct clk *
50 mtk_clk_register_cpumux(const struct mtk_composite *mux,
51                         struct regmap *regmap)
52 {
53         struct mtk_clk_cpumux *cpumux;
54         struct clk *clk;
55         struct clk_init_data init;
56
57         cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL);
58         if (!cpumux)
59                 return ERR_PTR(-ENOMEM);
60
61         init.name = mux->name;
62         init.ops = &clk_cpumux_ops;
63         init.parent_names = mux->parent_names;
64         init.num_parents = mux->num_parents;
65         init.flags = mux->flags;
66
67         cpumux->reg = mux->mux_reg;
68         cpumux->shift = mux->mux_shift;
69         cpumux->mask = BIT(mux->mux_width) - 1;
70         cpumux->regmap = regmap;
71         cpumux->hw.init = &init;
72
73         clk = clk_register(NULL, &cpumux->hw);
74         if (IS_ERR(clk))
75                 kfree(cpumux);
76
77         return clk;
78 }
79
80 static void mtk_clk_unregister_cpumux(struct clk *clk)
81 {
82         struct mtk_clk_cpumux *cpumux;
83         struct clk_hw *hw;
84
85         hw = __clk_get_hw(clk);
86         if (!hw)
87                 return;
88
89         cpumux = to_mtk_clk_cpumux(hw);
90
91         clk_unregister(clk);
92         kfree(cpumux);
93 }
94
95 int mtk_clk_register_cpumuxes(struct device_node *node,
96                               const struct mtk_composite *clks, int num,
97                               struct clk_onecell_data *clk_data)
98 {
99         int i;
100         struct clk *clk;
101         struct regmap *regmap;
102
103         regmap = device_node_to_regmap(node);
104         if (IS_ERR(regmap)) {
105                 pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
106                 return PTR_ERR(regmap);
107         }
108
109         for (i = 0; i < num; i++) {
110                 const struct mtk_composite *mux = &clks[i];
111
112                 clk = mtk_clk_register_cpumux(mux, regmap);
113                 if (IS_ERR(clk)) {
114                         pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
115                         continue;
116                 }
117
118                 clk_data->clks[mux->id] = clk;
119         }
120
121         return 0;
122 }
123
124 void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
125                                  struct clk_onecell_data *clk_data)
126 {
127         int i;
128
129         for (i = num; i > 0; i--) {
130                 const struct mtk_composite *mux = &clks[i - 1];
131
132                 if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
133                         continue;
134
135                 mtk_clk_unregister_cpumux(clk_data->clks[mux->id]);
136                 clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
137         }
138 }
139
140 MODULE_LICENSE("GPL");