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 unsigned long rate
= 0, prev_rate
;
34 for (i
= 0; i
< factor
->ftbl_cnt
; i
++) {
36 rate
= (((*prate
/ 10000) * factor
->ftbl
[i
].den
) /
37 (factor
->ftbl
[i
].num
* factor
->masks
->factor
)) * 10000;
41 if ((i
== 0) || (i
== factor
->ftbl_cnt
)) {
44 if ((drate
- prev_rate
) > (rate
- drate
))
51 static unsigned long clk_factor_recalc_rate(struct clk_hw
*hw
,
52 unsigned long parent_rate
)
54 struct mmp_clk_factor
*factor
= to_clk_factor(hw
);
55 struct mmp_clk_factor_masks
*masks
= factor
->masks
;
56 unsigned int val
, num
, den
;
58 val
= readl_relaxed(factor
->base
);
60 /* calculate numerator */
61 num
= (val
>> masks
->num_shift
) & masks
->num_mask
;
63 /* calculate denominator */
64 den
= (val
>> masks
->den_shift
) & masks
->den_mask
;
69 return (((parent_rate
/ 10000) * den
) /
70 (num
* factor
->masks
->factor
)) * 10000;
73 /* Configures new clock rate*/
74 static int clk_factor_set_rate(struct clk_hw
*hw
, unsigned long drate
,
77 struct mmp_clk_factor
*factor
= to_clk_factor(hw
);
78 struct mmp_clk_factor_masks
*masks
= factor
->masks
;
81 unsigned long rate
= 0;
82 unsigned long flags
= 0;
84 for (i
= 0; i
< factor
->ftbl_cnt
; i
++) {
85 rate
= (((prate
/ 10000) * factor
->ftbl
[i
].den
) /
86 (factor
->ftbl
[i
].num
* factor
->masks
->factor
)) * 10000;
94 spin_lock_irqsave(factor
->lock
, flags
);
96 val
= readl_relaxed(factor
->base
);
98 val
&= ~(masks
->num_mask
<< masks
->num_shift
);
99 val
|= (factor
->ftbl
[i
].num
& masks
->num_mask
) << masks
->num_shift
;
101 val
&= ~(masks
->den_mask
<< masks
->den_shift
);
102 val
|= (factor
->ftbl
[i
].den
& masks
->den_mask
) << masks
->den_shift
;
104 writel_relaxed(val
, factor
->base
);
107 spin_unlock_irqrestore(factor
->lock
, flags
);
112 static int clk_factor_init(struct clk_hw
*hw
)
114 struct mmp_clk_factor
*factor
= to_clk_factor(hw
);
115 struct mmp_clk_factor_masks
*masks
= factor
->masks
;
118 unsigned long flags
= 0;
121 spin_lock_irqsave(factor
->lock
, flags
);
123 val
= readl(factor
->base
);
125 /* calculate numerator */
126 num
= (val
>> masks
->num_shift
) & masks
->num_mask
;
128 /* calculate denominator */
129 den
= (val
>> masks
->den_shift
) & masks
->den_mask
;
131 for (i
= 0; i
< factor
->ftbl_cnt
; i
++)
132 if (den
== factor
->ftbl
[i
].den
&& num
== factor
->ftbl
[i
].num
)
135 if (i
>= factor
->ftbl_cnt
) {
136 val
&= ~(masks
->num_mask
<< masks
->num_shift
);
137 val
|= (factor
->ftbl
[0].num
& masks
->num_mask
) <<
140 val
&= ~(masks
->den_mask
<< masks
->den_shift
);
141 val
|= (factor
->ftbl
[0].den
& masks
->den_mask
) <<
144 writel(val
, factor
->base
);
148 spin_unlock_irqrestore(factor
->lock
, flags
);
153 static const struct clk_ops clk_factor_ops
= {
154 .recalc_rate
= clk_factor_recalc_rate
,
155 .round_rate
= clk_factor_round_rate
,
156 .set_rate
= clk_factor_set_rate
,
157 .init
= clk_factor_init
,
160 struct clk
*mmp_clk_register_factor(const char *name
, const char *parent_name
,
161 unsigned long flags
, void __iomem
*base
,
162 struct mmp_clk_factor_masks
*masks
,
163 struct mmp_clk_factor_tbl
*ftbl
,
164 unsigned int ftbl_cnt
, spinlock_t
*lock
)
166 struct mmp_clk_factor
*factor
;
167 struct clk_init_data init
;
171 pr_err("%s: must pass a clk_factor_mask\n", __func__
);
172 return ERR_PTR(-EINVAL
);
175 factor
= kzalloc(sizeof(*factor
), GFP_KERNEL
);
177 return ERR_PTR(-ENOMEM
);
179 /* struct clk_aux assignments */
181 factor
->masks
= masks
;
183 factor
->ftbl_cnt
= ftbl_cnt
;
184 factor
->hw
.init
= &init
;
188 init
.ops
= &clk_factor_ops
;
190 init
.parent_names
= &parent_name
;
191 init
.num_parents
= 1;
193 clk
= clk_register(NULL
, &factor
->hw
);
194 if (IS_ERR_OR_NULL(clk
))