2 * mmp factor clock operation source file
4 * Copyright (C) 2012 Marvell
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 clk_factor, hw)
29 struct clk_factor_masks *masks;
30 struct clk_factor_tbl *ftbl;
31 unsigned int ftbl_cnt;
34 static long clk_factor_round_rate(struct clk_hw *hw, unsigned long drate,
37 struct clk_factor *factor = to_clk_factor(hw);
38 unsigned long rate = 0, prev_rate;
41 for (i = 0; i < factor->ftbl_cnt; i++) {
43 rate = (((*prate / 10000) * factor->ftbl[i].den) /
44 (factor->ftbl[i].num * factor->masks->factor)) * 10000;
48 if ((i == 0) || (i == factor->ftbl_cnt)) {
51 if ((drate - prev_rate) > (rate - drate))
58 static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
59 unsigned long parent_rate)
61 struct clk_factor *factor = to_clk_factor(hw);
62 struct clk_factor_masks *masks = factor->masks;
63 unsigned int val, num, den;
65 val = readl_relaxed(factor->base);
67 /* calculate numerator */
68 num = (val >> masks->num_shift) & masks->num_mask;
70 /* calculate denominator */
71 den = (val >> masks->den_shift) & masks->den_mask;
76 return (((parent_rate / 10000) * den) /
77 (num * factor->masks->factor)) * 10000;
80 /* Configures new clock rate*/
81 static int clk_factor_set_rate(struct clk_hw *hw, unsigned long drate,
84 struct clk_factor *factor = to_clk_factor(hw);
85 struct clk_factor_masks *masks = factor->masks;
88 unsigned long prev_rate, rate = 0;
90 for (i = 0; i < factor->ftbl_cnt; i++) {
92 rate = (((prate / 10000) * factor->ftbl[i].den) /
93 (factor->ftbl[i].num * factor->masks->factor)) * 10000;
100 val = readl_relaxed(factor->base);
102 val &= ~(masks->num_mask << masks->num_shift);
103 val |= (factor->ftbl[i].num & masks->num_mask) << masks->num_shift;
105 val &= ~(masks->den_mask << masks->den_shift);
106 val |= (factor->ftbl[i].den & masks->den_mask) << masks->den_shift;
108 writel_relaxed(val, factor->base);
113 static struct clk_ops clk_factor_ops = {
114 .recalc_rate = clk_factor_recalc_rate,
115 .round_rate = clk_factor_round_rate,
116 .set_rate = clk_factor_set_rate,
119 struct clk *mmp_clk_register_factor(const char *name, const char *parent_name,
120 unsigned long flags, void __iomem *base,
121 struct clk_factor_masks *masks, struct clk_factor_tbl *ftbl,
122 unsigned int ftbl_cnt)
124 struct clk_factor *factor;
125 struct clk_init_data init;
129 pr_err("%s: must pass a clk_factor_mask\n", __func__);
130 return ERR_PTR(-EINVAL);
133 factor = kzalloc(sizeof(*factor), GFP_KERNEL);
135 pr_err("%s: could not allocate factor clk\n", __func__);
136 return ERR_PTR(-ENOMEM);
139 /* struct clk_aux assignments */
141 factor->masks = masks;
143 factor->ftbl_cnt = ftbl_cnt;
144 factor->hw.init = &init;
147 init.ops = &clk_factor_ops;
149 init.parent_names = &parent_name;
150 init.num_parents = 1;
152 clk = clk_register(NULL, &factor->hw);
153 if (IS_ERR_OR_NULL(clk))