blob: 2ad2df2e890933ac11483eac7673a9e7da6b19dd [file] [log] [blame]
Stephen Boyde1bd55e2018-12-11 09:57:48 -08001// SPDX-License-Identifier: GPL-2.0
Mike Turquette9d9f78e2012-03-15 23:11:20 -07002/*
3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
4 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
5 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
6 *
Mike Turquette9d9f78e2012-03-15 23:11:20 -07007 * Simple multiplexer clock implementation
8 */
9
Mike Turquette9d9f78e2012-03-15 23:11:20 -070010#include <linux/clk-provider.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/io.h>
14#include <linux/err.h>
15
16/*
17 * DOC: basic adjustable multiplexer clock that cannot gate
18 *
19 * Traits of this clock:
20 * prepare - clk_prepare only ensures that parents are prepared
21 * enable - clk_enable only ensures that parents are enabled
22 * rate - rate is only affected by parent switching. No clk_set_rate support
23 * parent - parent is adjustable through clk_set_parent
24 */
25
Jerome Brunet77deb662018-02-14 14:43:34 +010026int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags,
27 unsigned int val)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070028{
Stephen Boyd497295a2015-06-25 16:53:23 -070029 int num_parents = clk_hw_get_num_parents(hw);
Mike Turquette9d9f78e2012-03-15 23:11:20 -070030
Jerome Brunet77deb662018-02-14 14:43:34 +010031 if (table) {
Peter De Schrijverce4f3312013-03-22 14:07:53 +020032 int i;
33
34 for (i = 0; i < num_parents; i++)
Jerome Brunet77deb662018-02-14 14:43:34 +010035 if (table[i] == val)
Peter De Schrijverce4f3312013-03-22 14:07:53 +020036 return i;
37 return -EINVAL;
38 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -070039
Jerome Brunet77deb662018-02-14 14:43:34 +010040 if (val && (flags & CLK_MUX_INDEX_BIT))
Mike Turquette9d9f78e2012-03-15 23:11:20 -070041 val = ffs(val) - 1;
42
Jerome Brunet77deb662018-02-14 14:43:34 +010043 if (val && (flags & CLK_MUX_INDEX_ONE))
Mike Turquette9d9f78e2012-03-15 23:11:20 -070044 val--;
45
Peter De Schrijverce4f3312013-03-22 14:07:53 +020046 if (val >= num_parents)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070047 return -EINVAL;
48
49 return val;
50}
Jerome Brunet77deb662018-02-14 14:43:34 +010051EXPORT_SYMBOL_GPL(clk_mux_val_to_index);
52
53unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
54{
55 unsigned int val = index;
56
57 if (table) {
58 val = table[index];
59 } else {
60 if (flags & CLK_MUX_INDEX_BIT)
61 val = 1 << index;
62
63 if (flags & CLK_MUX_INDEX_ONE)
64 val++;
65 }
66
67 return val;
68}
69EXPORT_SYMBOL_GPL(clk_mux_index_to_val);
70
71static u8 clk_mux_get_parent(struct clk_hw *hw)
72{
73 struct clk_mux *mux = to_clk_mux(hw);
74 u32 val;
75
76 val = clk_readl(mux->reg) >> mux->shift;
77 val &= mux->mask;
78
79 return clk_mux_val_to_index(hw, mux->table, mux->flags, val);
80}
Mike Turquette9d9f78e2012-03-15 23:11:20 -070081
82static int clk_mux_set_parent(struct clk_hw *hw, u8 index)
83{
84 struct clk_mux *mux = to_clk_mux(hw);
Jerome Brunet77deb662018-02-14 14:43:34 +010085 u32 val = clk_mux_index_to_val(mux->table, mux->flags, index);
Mike Turquette9d9f78e2012-03-15 23:11:20 -070086 unsigned long flags = 0;
Jerome Brunet77deb662018-02-14 14:43:34 +010087 u32 reg;
Mike Turquette9d9f78e2012-03-15 23:11:20 -070088
89 if (mux->lock)
90 spin_lock_irqsave(mux->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -070091 else
92 __acquire(mux->lock);
Mike Turquette9d9f78e2012-03-15 23:11:20 -070093
Haojian Zhuangba492e92013-06-08 22:47:17 +080094 if (mux->flags & CLK_MUX_HIWORD_MASK) {
Jerome Brunet77deb662018-02-14 14:43:34 +010095 reg = mux->mask << (mux->shift + 16);
Haojian Zhuangba492e92013-06-08 22:47:17 +080096 } else {
Jerome Brunet77deb662018-02-14 14:43:34 +010097 reg = clk_readl(mux->reg);
98 reg &= ~(mux->mask << mux->shift);
Haojian Zhuangba492e92013-06-08 22:47:17 +080099 }
Jerome Brunet77deb662018-02-14 14:43:34 +0100100 val = val << mux->shift;
101 reg |= val;
102 clk_writel(reg, mux->reg);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700103
104 if (mux->lock)
105 spin_unlock_irqrestore(mux->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -0700106 else
107 __release(mux->lock);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700108
109 return 0;
110}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700111
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200112static int clk_mux_determine_rate(struct clk_hw *hw,
113 struct clk_rate_request *req)
114{
115 struct clk_mux *mux = to_clk_mux(hw);
116
117 return clk_mux_determine_rate_flags(hw, req, mux->flags);
118}
119
Shawn Guo822c2502012-03-27 15:23:22 +0800120const struct clk_ops clk_mux_ops = {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700121 .get_parent = clk_mux_get_parent,
122 .set_parent = clk_mux_set_parent,
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200123 .determine_rate = clk_mux_determine_rate,
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700124};
125EXPORT_SYMBOL_GPL(clk_mux_ops);
126
Tomasz Figac57acd12013-07-23 01:49:18 +0200127const struct clk_ops clk_mux_ro_ops = {
128 .get_parent = clk_mux_get_parent,
129};
130EXPORT_SYMBOL_GPL(clk_mux_ro_ops);
131
Stephen Boyd264b3172016-02-07 00:05:48 -0800132struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name,
Sascha Hauer2893c372015-03-31 20:16:52 +0200133 const char * const *parent_names, u8 num_parents,
134 unsigned long flags,
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200135 void __iomem *reg, u8 shift, u32 mask,
136 u8 clk_mux_flags, u32 *table, spinlock_t *lock)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700137{
138 struct clk_mux *mux;
Stephen Boyd264b3172016-02-07 00:05:48 -0800139 struct clk_hw *hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700140 struct clk_init_data init;
Haojian Zhuangba492e92013-06-08 22:47:17 +0800141 u8 width = 0;
Stephen Boyd264b3172016-02-07 00:05:48 -0800142 int ret;
Haojian Zhuangba492e92013-06-08 22:47:17 +0800143
144 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
145 width = fls(mask) - ffs(mask) + 1;
146 if (width + shift > 16) {
147 pr_err("mux value exceeds LOWORD field\n");
148 return ERR_PTR(-EINVAL);
149 }
150 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700151
Mike Turquette27d54592012-03-26 17:51:03 -0700152 /* allocate the mux */
Markus Elfring1e287332017-09-26 17:30:06 +0200153 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
Markus Elfring0b910402017-09-26 17:23:04 +0200154 if (!mux)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700155 return ERR_PTR(-ENOMEM);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700156
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700157 init.name = name;
Tomasz Figac57acd12013-07-23 01:49:18 +0200158 if (clk_mux_flags & CLK_MUX_READ_ONLY)
159 init.ops = &clk_mux_ro_ops;
160 else
161 init.ops = &clk_mux_ops;
Rajendra Nayakf7d8caa2012-06-01 14:02:47 +0530162 init.flags = flags | CLK_IS_BASIC;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700163 init.parent_names = parent_names;
164 init.num_parents = num_parents;
165
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700166 /* struct clk_mux assignments */
167 mux->reg = reg;
168 mux->shift = shift;
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200169 mux->mask = mask;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700170 mux->flags = clk_mux_flags;
171 mux->lock = lock;
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200172 mux->table = table;
Mike Turquette31df9db2012-05-06 18:48:11 -0700173 mux->hw.init = &init;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700174
Stephen Boyd264b3172016-02-07 00:05:48 -0800175 hw = &mux->hw;
176 ret = clk_hw_register(dev, hw);
177 if (ret) {
Mike Turquette27d54592012-03-26 17:51:03 -0700178 kfree(mux);
Stephen Boyd264b3172016-02-07 00:05:48 -0800179 hw = ERR_PTR(ret);
180 }
Mike Turquette27d54592012-03-26 17:51:03 -0700181
Stephen Boyd264b3172016-02-07 00:05:48 -0800182 return hw;
183}
184EXPORT_SYMBOL_GPL(clk_hw_register_mux_table);
185
186struct clk *clk_register_mux_table(struct device *dev, const char *name,
187 const char * const *parent_names, u8 num_parents,
188 unsigned long flags,
189 void __iomem *reg, u8 shift, u32 mask,
190 u8 clk_mux_flags, u32 *table, spinlock_t *lock)
191{
192 struct clk_hw *hw;
193
194 hw = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
195 flags, reg, shift, mask, clk_mux_flags,
196 table, lock);
197 if (IS_ERR(hw))
198 return ERR_CAST(hw);
199 return hw->clk;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700200}
Mike Turquette5cfe10b2013-08-15 19:06:29 -0700201EXPORT_SYMBOL_GPL(clk_register_mux_table);
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200202
203struct clk *clk_register_mux(struct device *dev, const char *name,
Sascha Hauer2893c372015-03-31 20:16:52 +0200204 const char * const *parent_names, u8 num_parents,
205 unsigned long flags,
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200206 void __iomem *reg, u8 shift, u8 width,
207 u8 clk_mux_flags, spinlock_t *lock)
208{
209 u32 mask = BIT(width) - 1;
210
211 return clk_register_mux_table(dev, name, parent_names, num_parents,
212 flags, reg, shift, mask, clk_mux_flags,
213 NULL, lock);
214}
Mike Turquette5cfe10b2013-08-15 19:06:29 -0700215EXPORT_SYMBOL_GPL(clk_register_mux);
Krzysztof Kozlowski4e3c0212015-01-05 10:52:40 +0100216
Stephen Boyd264b3172016-02-07 00:05:48 -0800217struct clk_hw *clk_hw_register_mux(struct device *dev, const char *name,
218 const char * const *parent_names, u8 num_parents,
219 unsigned long flags,
220 void __iomem *reg, u8 shift, u8 width,
221 u8 clk_mux_flags, spinlock_t *lock)
222{
223 u32 mask = BIT(width) - 1;
224
225 return clk_hw_register_mux_table(dev, name, parent_names, num_parents,
226 flags, reg, shift, mask, clk_mux_flags,
227 NULL, lock);
228}
229EXPORT_SYMBOL_GPL(clk_hw_register_mux);
230
Krzysztof Kozlowski4e3c0212015-01-05 10:52:40 +0100231void clk_unregister_mux(struct clk *clk)
232{
233 struct clk_mux *mux;
234 struct clk_hw *hw;
235
236 hw = __clk_get_hw(clk);
237 if (!hw)
238 return;
239
240 mux = to_clk_mux(hw);
241
242 clk_unregister(clk);
243 kfree(mux);
244}
245EXPORT_SYMBOL_GPL(clk_unregister_mux);
Stephen Boyd264b3172016-02-07 00:05:48 -0800246
247void clk_hw_unregister_mux(struct clk_hw *hw)
248{
249 struct clk_mux *mux;
250
251 mux = to_clk_mux(hw);
252
253 clk_hw_unregister(hw);
254 kfree(mux);
255}
256EXPORT_SYMBOL_GPL(clk_hw_unregister_mux);