2 * mmp factor clock operation source file
4 * Copyright (C) 2012 Marvell
5 * Chao Xie <xiechao.mail@gmail.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/clk-provider.h>
13 #include <linux/slab.h>
15 #include <linux/err.h>
21 * Fout from synthesizer can be given from two equations:
22 * numerator/denominator = Fin / (Fout * factor)
25 #define to_clk_factor(hw) container_of(hw, struct mmp_clk_factor, hw)
27 static long clk_factor_round_rate(struct clk_hw *hw, unsigned long drate,
30 struct mmp_clk_factor *factor = to_clk_factor(hw);
31 u64 rate = 0, prev_rate;
34 for (i = 0; i < factor->ftbl_cnt; i++) {
37 rate *= factor->ftbl[i].den;
38 do_div(rate, factor->ftbl[i].num * factor->masks->factor);
43 if ((i == 0) || (i == factor->ftbl_cnt)) {
46 if ((drate - prev_rate) > (rate - drate))
53 static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
54 unsigned long parent_rate)
56 struct mmp_clk_factor *factor = to_clk_factor(hw);
57 struct mmp_clk_factor_masks *masks = factor->masks;
58 unsigned int val, num, den;
61 val = readl_relaxed(factor->base);
63 /* calculate numerator */
64 num = (val >> masks->num_shift) & masks->num_mask;
66 /* calculate denominator */
67 den = (val >> masks->den_shift) & masks->den_mask;
74 do_div(rate, num * factor->masks->factor);
79 /* Configures new clock rate*/
80 static int clk_factor_set_rate(struct clk_hw *hw, unsigned long drate,
83 struct mmp_clk_factor *factor = to_clk_factor(hw);
84 struct mmp_clk_factor_masks *masks = factor->masks;
87 unsigned long flags = 0;
90 for (i = 0; i < factor->ftbl_cnt; i++) {
92 rate *= factor->ftbl[i].den;
93 do_div(rate, factor->ftbl[i].num * factor->masks->factor);
102 spin_lock_irqsave(factor->lock, flags);
104 val = readl_relaxed(factor->base);
106 val &= ~(masks->num_mask << masks->num_shift);
107 val |= (factor->ftbl[i].num & masks->num_mask) << masks->num_shift;
109 val &= ~(masks->den_mask << masks->den_shift);
110 val |= (factor->ftbl[i].den & masks->den_mask) << masks->den_shift;
112 writel_relaxed(val, factor->base);
115 spin_unlock_irqrestore(factor->lock, flags);
120 static int clk_factor_init(struct clk_hw *hw)
122 struct mmp_clk_factor *factor = to_clk_factor(hw);
123 struct mmp_clk_factor_masks *masks = factor->masks;
126 unsigned long flags = 0;
129 spin_lock_irqsave(factor->lock, flags);
131 val = readl(factor->base);
133 /* calculate numerator */
134 num = (val >> masks->num_shift) & masks->num_mask;
136 /* calculate denominator */
137 den = (val >> masks->den_shift) & masks->den_mask;
139 for (i = 0; i < factor->ftbl_cnt; i++)
140 if (den == factor->ftbl[i].den && num == factor->ftbl[i].num)
143 if (i >= factor->ftbl_cnt) {
144 val &= ~(masks->num_mask << masks->num_shift);
145 val |= (factor->ftbl[0].num & masks->num_mask) <<
148 val &= ~(masks->den_mask << masks->den_shift);
149 val |= (factor->ftbl[0].den & masks->den_mask) <<
153 if (!(val & masks->enable_mask) || i >= factor->ftbl_cnt) {
154 val |= masks->enable_mask;
155 writel(val, factor->base);
159 spin_unlock_irqrestore(factor->lock, flags);
164 static const struct clk_ops clk_factor_ops = {
165 .recalc_rate = clk_factor_recalc_rate,
166 .round_rate = clk_factor_round_rate,
167 .set_rate = clk_factor_set_rate,
168 .init = clk_factor_init,
171 struct clk *mmp_clk_register_factor(const char *name, const char *parent_name,
172 unsigned long flags, void __iomem *base,
173 struct mmp_clk_factor_masks *masks,
174 struct mmp_clk_factor_tbl *ftbl,
175 unsigned int ftbl_cnt, spinlock_t *lock)
177 struct mmp_clk_factor *factor;
178 struct clk_init_data init;
182 pr_err("%s: must pass a clk_factor_mask\n", __func__);
183 return ERR_PTR(-EINVAL);
186 factor = kzalloc(sizeof(*factor), GFP_KERNEL);
188 return ERR_PTR(-ENOMEM);
190 /* struct clk_aux assignments */
192 factor->masks = masks;
194 factor->ftbl_cnt = ftbl_cnt;
195 factor->hw.init = &init;
199 init.ops = &clk_factor_ops;
201 init.parent_names = &parent_name;
202 init.num_parents = 1;
204 clk = clk_register(NULL, &factor->hw);
205 if (IS_ERR_OR_NULL(clk))