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>
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.
10 * Simple multiplexer clock implementation
13 #include <linux/clk-provider.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include <linux/err.h>
20 * DOC: basic adjustable multiplexer clock that cannot gate
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
29 int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags,
32 int num_parents = clk_hw_get_num_parents(hw);
37 for (i = 0; i < num_parents; i++)
43 if (val && (flags & CLK_MUX_INDEX_BIT))
46 if (val && (flags & CLK_MUX_INDEX_ONE))
49 if (val >= num_parents)
54 EXPORT_SYMBOL_GPL(clk_mux_val_to_index);
56 unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
58 unsigned int val = index;
63 if (flags & CLK_MUX_INDEX_BIT)
66 if (flags & CLK_MUX_INDEX_ONE)
72 EXPORT_SYMBOL_GPL(clk_mux_index_to_val);
74 static u8 clk_mux_get_parent(struct clk_hw *hw)
76 struct clk_mux *mux = to_clk_mux(hw);
79 val = clk_readl(mux->reg) >> mux->shift;
82 return clk_mux_val_to_index(hw, mux->table, mux->flags, val);
85 static int clk_mux_set_parent(struct clk_hw *hw, u8 index)
87 struct clk_mux *mux = to_clk_mux(hw);
88 u32 val = clk_mux_index_to_val(mux->table, mux->flags, index);
89 unsigned long flags = 0;
93 spin_lock_irqsave(mux->lock, flags);
97 if (mux->flags & CLK_MUX_HIWORD_MASK) {
98 reg = mux->mask << (mux->shift + 16);
100 reg = clk_readl(mux->reg);
101 reg &= ~(mux->mask << mux->shift);
103 val = val << mux->shift;
105 clk_writel(reg, mux->reg);
108 spin_unlock_irqrestore(mux->lock, flags);
110 __release(mux->lock);
115 const struct clk_ops clk_mux_ops = {
116 .get_parent = clk_mux_get_parent,
117 .set_parent = clk_mux_set_parent,
118 .determine_rate = __clk_mux_determine_rate,
120 EXPORT_SYMBOL_GPL(clk_mux_ops);
122 const struct clk_ops clk_mux_ro_ops = {
123 .get_parent = clk_mux_get_parent,
125 EXPORT_SYMBOL_GPL(clk_mux_ro_ops);
127 struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name,
128 const char * const *parent_names, u8 num_parents,
130 void __iomem *reg, u8 shift, u32 mask,
131 u8 clk_mux_flags, u32 *table, spinlock_t *lock)
135 struct clk_init_data init;
139 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
140 width = fls(mask) - ffs(mask) + 1;
141 if (width + shift > 16) {
142 pr_err("mux value exceeds LOWORD field\n");
143 return ERR_PTR(-EINVAL);
147 /* allocate the mux */
148 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
150 return ERR_PTR(-ENOMEM);
153 if (clk_mux_flags & CLK_MUX_READ_ONLY)
154 init.ops = &clk_mux_ro_ops;
156 init.ops = &clk_mux_ops;
157 init.flags = flags | CLK_IS_BASIC;
158 init.parent_names = parent_names;
159 init.num_parents = num_parents;
161 /* struct clk_mux assignments */
165 mux->flags = clk_mux_flags;
168 mux->hw.init = &init;
171 ret = clk_hw_register(dev, hw);
179 EXPORT_SYMBOL_GPL(clk_hw_register_mux_table);
181 struct clk *clk_register_mux_table(struct device *dev, const char *name,
182 const char * const *parent_names, u8 num_parents,
184 void __iomem *reg, u8 shift, u32 mask,
185 u8 clk_mux_flags, u32 *table, spinlock_t *lock)
189 hw = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
190 flags, reg, shift, mask, clk_mux_flags,
196 EXPORT_SYMBOL_GPL(clk_register_mux_table);
198 struct clk *clk_register_mux(struct device *dev, const char *name,
199 const char * const *parent_names, u8 num_parents,
201 void __iomem *reg, u8 shift, u8 width,
202 u8 clk_mux_flags, spinlock_t *lock)
204 u32 mask = BIT(width) - 1;
206 return clk_register_mux_table(dev, name, parent_names, num_parents,
207 flags, reg, shift, mask, clk_mux_flags,
210 EXPORT_SYMBOL_GPL(clk_register_mux);
212 struct clk_hw *clk_hw_register_mux(struct device *dev, const char *name,
213 const char * const *parent_names, u8 num_parents,
215 void __iomem *reg, u8 shift, u8 width,
216 u8 clk_mux_flags, spinlock_t *lock)
218 u32 mask = BIT(width) - 1;
220 return clk_hw_register_mux_table(dev, name, parent_names, num_parents,
221 flags, reg, shift, mask, clk_mux_flags,
224 EXPORT_SYMBOL_GPL(clk_hw_register_mux);
226 void clk_unregister_mux(struct clk *clk)
231 hw = __clk_get_hw(clk);
235 mux = to_clk_mux(hw);
240 EXPORT_SYMBOL_GPL(clk_unregister_mux);
242 void clk_hw_unregister_mux(struct clk_hw *hw)
246 mux = to_clk_mux(hw);
248 clk_hw_unregister(hw);
251 EXPORT_SYMBOL_GPL(clk_hw_unregister_mux);