blob: 1628b93655edf809ec40d1e733a0b25ee7a8b26c [file] [log] [blame]
Mike Turquette9d9f78e2012-03-15 23:11:20 -07001/*
2 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
3 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
4 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Simple multiplexer clock implementation
11 */
12
Mike Turquette9d9f78e2012-03-15 23:11:20 -070013#include <linux/clk-provider.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/io.h>
17#include <linux/err.h>
18
19/*
20 * DOC: basic adjustable multiplexer clock that cannot gate
21 *
22 * Traits of this clock:
23 * prepare - clk_prepare only ensures that parents are prepared
24 * enable - clk_enable only ensures that parents are enabled
25 * rate - rate is only affected by parent switching. No clk_set_rate support
26 * parent - parent is adjustable through clk_set_parent
27 */
28
Jerome Brunet77deb662018-02-14 14:43:34 +010029int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags,
30 unsigned int val)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070031{
Stephen Boyd497295a2015-06-25 16:53:23 -070032 int num_parents = clk_hw_get_num_parents(hw);
Mike Turquette9d9f78e2012-03-15 23:11:20 -070033
Jerome Brunet77deb662018-02-14 14:43:34 +010034 if (table) {
Peter De Schrijverce4f3312013-03-22 14:07:53 +020035 int i;
36
37 for (i = 0; i < num_parents; i++)
Jerome Brunet77deb662018-02-14 14:43:34 +010038 if (table[i] == val)
Peter De Schrijverce4f3312013-03-22 14:07:53 +020039 return i;
40 return -EINVAL;
41 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -070042
Jerome Brunet77deb662018-02-14 14:43:34 +010043 if (val && (flags & CLK_MUX_INDEX_BIT))
Mike Turquette9d9f78e2012-03-15 23:11:20 -070044 val = ffs(val) - 1;
45
Jerome Brunet77deb662018-02-14 14:43:34 +010046 if (val && (flags & CLK_MUX_INDEX_ONE))
Mike Turquette9d9f78e2012-03-15 23:11:20 -070047 val--;
48
Peter De Schrijverce4f3312013-03-22 14:07:53 +020049 if (val >= num_parents)
Mike Turquette9d9f78e2012-03-15 23:11:20 -070050 return -EINVAL;
51
52 return val;
53}
Jerome Brunet77deb662018-02-14 14:43:34 +010054EXPORT_SYMBOL_GPL(clk_mux_val_to_index);
55
56unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
57{
58 unsigned int val = index;
59
60 if (table) {
61 val = table[index];
62 } else {
63 if (flags & CLK_MUX_INDEX_BIT)
64 val = 1 << index;
65
66 if (flags & CLK_MUX_INDEX_ONE)
67 val++;
68 }
69
70 return val;
71}
72EXPORT_SYMBOL_GPL(clk_mux_index_to_val);
73
74static u8 clk_mux_get_parent(struct clk_hw *hw)
75{
76 struct clk_mux *mux = to_clk_mux(hw);
77 u32 val;
78
79 val = clk_readl(mux->reg) >> mux->shift;
80 val &= mux->mask;
81
82 return clk_mux_val_to_index(hw, mux->table, mux->flags, val);
83}
Mike Turquette9d9f78e2012-03-15 23:11:20 -070084
85static int clk_mux_set_parent(struct clk_hw *hw, u8 index)
86{
87 struct clk_mux *mux = to_clk_mux(hw);
Jerome Brunet77deb662018-02-14 14:43:34 +010088 u32 val = clk_mux_index_to_val(mux->table, mux->flags, index);
Mike Turquette9d9f78e2012-03-15 23:11:20 -070089 unsigned long flags = 0;
Jerome Brunet77deb662018-02-14 14:43:34 +010090 u32 reg;
Mike Turquette9d9f78e2012-03-15 23:11:20 -070091
92 if (mux->lock)
93 spin_lock_irqsave(mux->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -070094 else
95 __acquire(mux->lock);
Mike Turquette9d9f78e2012-03-15 23:11:20 -070096
Haojian Zhuangba492e92013-06-08 22:47:17 +080097 if (mux->flags & CLK_MUX_HIWORD_MASK) {
Jerome Brunet77deb662018-02-14 14:43:34 +010098 reg = mux->mask << (mux->shift + 16);
Haojian Zhuangba492e92013-06-08 22:47:17 +080099 } else {
Jerome Brunet77deb662018-02-14 14:43:34 +0100100 reg = clk_readl(mux->reg);
101 reg &= ~(mux->mask << mux->shift);
Haojian Zhuangba492e92013-06-08 22:47:17 +0800102 }
Jerome Brunet77deb662018-02-14 14:43:34 +0100103 val = val << mux->shift;
104 reg |= val;
105 clk_writel(reg, mux->reg);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700106
107 if (mux->lock)
108 spin_unlock_irqrestore(mux->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -0700109 else
110 __release(mux->lock);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700111
112 return 0;
113}
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700114
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200115static int clk_mux_determine_rate(struct clk_hw *hw,
116 struct clk_rate_request *req)
117{
118 struct clk_mux *mux = to_clk_mux(hw);
119
120 return clk_mux_determine_rate_flags(hw, req, mux->flags);
121}
122
Shawn Guo822c2502012-03-27 15:23:22 +0800123const struct clk_ops clk_mux_ops = {
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700124 .get_parent = clk_mux_get_parent,
125 .set_parent = clk_mux_set_parent,
Jerome Brunet4ad69b802018-04-09 15:59:20 +0200126 .determine_rate = clk_mux_determine_rate,
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700127};
128EXPORT_SYMBOL_GPL(clk_mux_ops);
129
Tomasz Figac57acd12013-07-23 01:49:18 +0200130const struct clk_ops clk_mux_ro_ops = {
131 .get_parent = clk_mux_get_parent,
132};
133EXPORT_SYMBOL_GPL(clk_mux_ro_ops);
134
Stephen Boyd264b3172016-02-07 00:05:48 -0800135struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name,
Sascha Hauer2893c372015-03-31 20:16:52 +0200136 const char * const *parent_names, u8 num_parents,
137 unsigned long flags,
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200138 void __iomem *reg, u8 shift, u32 mask,
139 u8 clk_mux_flags, u32 *table, spinlock_t *lock)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700140{
141 struct clk_mux *mux;
Stephen Boyd264b3172016-02-07 00:05:48 -0800142 struct clk_hw *hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700143 struct clk_init_data init;
Haojian Zhuangba492e92013-06-08 22:47:17 +0800144 u8 width = 0;
Stephen Boyd264b3172016-02-07 00:05:48 -0800145 int ret;
Haojian Zhuangba492e92013-06-08 22:47:17 +0800146
147 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
148 width = fls(mask) - ffs(mask) + 1;
149 if (width + shift > 16) {
150 pr_err("mux value exceeds LOWORD field\n");
151 return ERR_PTR(-EINVAL);
152 }
153 }
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700154
Mike Turquette27d54592012-03-26 17:51:03 -0700155 /* allocate the mux */
Markus Elfring1e287332017-09-26 17:30:06 +0200156 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
Markus Elfring0b910402017-09-26 17:23:04 +0200157 if (!mux)
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700158 return ERR_PTR(-ENOMEM);
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700159
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700160 init.name = name;
Tomasz Figac57acd12013-07-23 01:49:18 +0200161 if (clk_mux_flags & CLK_MUX_READ_ONLY)
162 init.ops = &clk_mux_ro_ops;
163 else
164 init.ops = &clk_mux_ops;
Rajendra Nayakf7d8caa2012-06-01 14:02:47 +0530165 init.flags = flags | CLK_IS_BASIC;
Saravana Kannan0197b3e2012-04-25 22:58:56 -0700166 init.parent_names = parent_names;
167 init.num_parents = num_parents;
168
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700169 /* struct clk_mux assignments */
170 mux->reg = reg;
171 mux->shift = shift;
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200172 mux->mask = mask;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700173 mux->flags = clk_mux_flags;
174 mux->lock = lock;
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200175 mux->table = table;
Mike Turquette31df9db2012-05-06 18:48:11 -0700176 mux->hw.init = &init;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700177
Stephen Boyd264b3172016-02-07 00:05:48 -0800178 hw = &mux->hw;
179 ret = clk_hw_register(dev, hw);
180 if (ret) {
Mike Turquette27d54592012-03-26 17:51:03 -0700181 kfree(mux);
Stephen Boyd264b3172016-02-07 00:05:48 -0800182 hw = ERR_PTR(ret);
183 }
Mike Turquette27d54592012-03-26 17:51:03 -0700184
Stephen Boyd264b3172016-02-07 00:05:48 -0800185 return hw;
186}
187EXPORT_SYMBOL_GPL(clk_hw_register_mux_table);
188
189struct clk *clk_register_mux_table(struct device *dev, const char *name,
190 const char * const *parent_names, u8 num_parents,
191 unsigned long flags,
192 void __iomem *reg, u8 shift, u32 mask,
193 u8 clk_mux_flags, u32 *table, spinlock_t *lock)
194{
195 struct clk_hw *hw;
196
197 hw = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
198 flags, reg, shift, mask, clk_mux_flags,
199 table, lock);
200 if (IS_ERR(hw))
201 return ERR_CAST(hw);
202 return hw->clk;
Mike Turquette9d9f78e2012-03-15 23:11:20 -0700203}
Mike Turquette5cfe10b2013-08-15 19:06:29 -0700204EXPORT_SYMBOL_GPL(clk_register_mux_table);
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200205
206struct clk *clk_register_mux(struct device *dev, const char *name,
Sascha Hauer2893c372015-03-31 20:16:52 +0200207 const char * const *parent_names, u8 num_parents,
208 unsigned long flags,
Peter De Schrijverce4f3312013-03-22 14:07:53 +0200209 void __iomem *reg, u8 shift, u8 width,
210 u8 clk_mux_flags, spinlock_t *lock)
211{
212 u32 mask = BIT(width) - 1;
213
214 return clk_register_mux_table(dev, name, parent_names, num_parents,
215 flags, reg, shift, mask, clk_mux_flags,
216 NULL, lock);
217}
Mike Turquette5cfe10b2013-08-15 19:06:29 -0700218EXPORT_SYMBOL_GPL(clk_register_mux);
Krzysztof Kozlowski4e3c0212015-01-05 10:52:40 +0100219
Stephen Boyd264b3172016-02-07 00:05:48 -0800220struct clk_hw *clk_hw_register_mux(struct device *dev, const char *name,
221 const char * const *parent_names, u8 num_parents,
222 unsigned long flags,
223 void __iomem *reg, u8 shift, u8 width,
224 u8 clk_mux_flags, spinlock_t *lock)
225{
226 u32 mask = BIT(width) - 1;
227
228 return clk_hw_register_mux_table(dev, name, parent_names, num_parents,
229 flags, reg, shift, mask, clk_mux_flags,
230 NULL, lock);
231}
232EXPORT_SYMBOL_GPL(clk_hw_register_mux);
233
Krzysztof Kozlowski4e3c0212015-01-05 10:52:40 +0100234void clk_unregister_mux(struct clk *clk)
235{
236 struct clk_mux *mux;
237 struct clk_hw *hw;
238
239 hw = __clk_get_hw(clk);
240 if (!hw)
241 return;
242
243 mux = to_clk_mux(hw);
244
245 clk_unregister(clk);
246 kfree(mux);
247}
248EXPORT_SYMBOL_GPL(clk_unregister_mux);
Stephen Boyd264b3172016-02-07 00:05:48 -0800249
250void clk_hw_unregister_mux(struct clk_hw *hw)
251{
252 struct clk_mux *mux;
253
254 mux = to_clk_mux(hw);
255
256 clk_hw_unregister(hw);
257 kfree(mux);
258}
259EXPORT_SYMBOL_GPL(clk_hw_unregister_mux);