2 * Copyright (C) 2016 Maxime Ripard
3 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
11 #include <linux/clk-provider.h>
19 unsigned long n
, min_n
, max_n
;
20 unsigned long m
, min_m
, max_m
;
23 static unsigned long ccu_nm_calc_rate(unsigned long parent
,
24 unsigned long n
, unsigned long m
)
34 static void ccu_nm_find_best(unsigned long parent
, unsigned long rate
,
37 unsigned long best_rate
= 0;
38 unsigned long best_n
= 0, best_m
= 0;
41 for (_n
= nm
->min_n
; _n
<= nm
->max_n
; _n
++) {
42 for (_m
= nm
->min_m
; _m
<= nm
->max_m
; _m
++) {
43 unsigned long tmp_rate
= ccu_nm_calc_rate(parent
,
49 if ((rate
- tmp_rate
) < (rate
- best_rate
)) {
61 static void ccu_nm_disable(struct clk_hw
*hw
)
63 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
65 return ccu_gate_helper_disable(&nm
->common
, nm
->enable
);
68 static int ccu_nm_enable(struct clk_hw
*hw
)
70 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
72 return ccu_gate_helper_enable(&nm
->common
, nm
->enable
);
75 static int ccu_nm_is_enabled(struct clk_hw
*hw
)
77 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
79 return ccu_gate_helper_is_enabled(&nm
->common
, nm
->enable
);
82 static unsigned long ccu_nm_recalc_rate(struct clk_hw
*hw
,
83 unsigned long parent_rate
)
85 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
90 if (ccu_frac_helper_is_enabled(&nm
->common
, &nm
->frac
)) {
91 rate
= ccu_frac_helper_read_rate(&nm
->common
, &nm
->frac
);
93 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
94 rate
/= nm
->fixed_post_div
;
99 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
101 n
= reg
>> nm
->n
.shift
;
102 n
&= (1 << nm
->n
.width
) - 1;
107 m
= reg
>> nm
->m
.shift
;
108 m
&= (1 << nm
->m
.width
) - 1;
113 if (ccu_sdm_helper_is_enabled(&nm
->common
, &nm
->sdm
))
114 rate
= ccu_sdm_helper_read_rate(&nm
->common
, &nm
->sdm
, m
, n
);
116 rate
= ccu_nm_calc_rate(parent_rate
, n
, m
);
118 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
119 rate
/= nm
->fixed_post_div
;
124 static long ccu_nm_round_rate(struct clk_hw
*hw
, unsigned long rate
,
125 unsigned long *parent_rate
)
127 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
130 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
131 rate
*= nm
->fixed_post_div
;
133 if (rate
< nm
->min_rate
) {
135 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
136 rate
/= nm
->fixed_post_div
;
140 if (nm
->max_rate
&& rate
> nm
->max_rate
) {
142 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
143 rate
/= nm
->fixed_post_div
;
147 if (ccu_frac_helper_has_rate(&nm
->common
, &nm
->frac
, rate
)) {
148 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
149 rate
/= nm
->fixed_post_div
;
153 if (ccu_sdm_helper_has_rate(&nm
->common
, &nm
->sdm
, rate
)) {
154 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
155 rate
/= nm
->fixed_post_div
;
159 _nm
.min_n
= nm
->n
.min
?: 1;
160 _nm
.max_n
= nm
->n
.max
?: 1 << nm
->n
.width
;
162 _nm
.max_m
= nm
->m
.max
?: 1 << nm
->m
.width
;
164 ccu_nm_find_best(*parent_rate
, rate
, &_nm
);
165 rate
= ccu_nm_calc_rate(*parent_rate
, _nm
.n
, _nm
.m
);
167 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
168 rate
/= nm
->fixed_post_div
;
173 static int ccu_nm_set_rate(struct clk_hw
*hw
, unsigned long rate
,
174 unsigned long parent_rate
)
176 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
181 /* Adjust target rate according to post-dividers */
182 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
183 rate
= rate
* nm
->fixed_post_div
;
185 if (ccu_frac_helper_has_rate(&nm
->common
, &nm
->frac
, rate
)) {
186 spin_lock_irqsave(nm
->common
.lock
, flags
);
188 /* most SoCs require M to be 0 if fractional mode is used */
189 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
190 reg
&= ~GENMASK(nm
->m
.width
+ nm
->m
.shift
- 1, nm
->m
.shift
);
191 writel(reg
, nm
->common
.base
+ nm
->common
.reg
);
193 spin_unlock_irqrestore(nm
->common
.lock
, flags
);
195 ccu_frac_helper_enable(&nm
->common
, &nm
->frac
);
197 return ccu_frac_helper_set_rate(&nm
->common
, &nm
->frac
,
200 ccu_frac_helper_disable(&nm
->common
, &nm
->frac
);
203 _nm
.min_n
= nm
->n
.min
?: 1;
204 _nm
.max_n
= nm
->n
.max
?: 1 << nm
->n
.width
;
206 _nm
.max_m
= nm
->m
.max
?: 1 << nm
->m
.width
;
208 if (ccu_sdm_helper_has_rate(&nm
->common
, &nm
->sdm
, rate
)) {
209 ccu_sdm_helper_enable(&nm
->common
, &nm
->sdm
, rate
);
211 /* Sigma delta modulation requires specific N and M factors */
212 ccu_sdm_helper_get_factors(&nm
->common
, &nm
->sdm
, rate
,
215 ccu_sdm_helper_disable(&nm
->common
, &nm
->sdm
);
216 ccu_nm_find_best(parent_rate
, rate
, &_nm
);
219 spin_lock_irqsave(nm
->common
.lock
, flags
);
221 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
222 reg
&= ~GENMASK(nm
->n
.width
+ nm
->n
.shift
- 1, nm
->n
.shift
);
223 reg
&= ~GENMASK(nm
->m
.width
+ nm
->m
.shift
- 1, nm
->m
.shift
);
225 reg
|= (_nm
.n
- nm
->n
.offset
) << nm
->n
.shift
;
226 reg
|= (_nm
.m
- nm
->m
.offset
) << nm
->m
.shift
;
227 writel(reg
, nm
->common
.base
+ nm
->common
.reg
);
229 spin_unlock_irqrestore(nm
->common
.lock
, flags
);
231 ccu_helper_wait_for_lock(&nm
->common
, nm
->lock
);
236 const struct clk_ops ccu_nm_ops
= {
237 .disable
= ccu_nm_disable
,
238 .enable
= ccu_nm_enable
,
239 .is_enabled
= ccu_nm_is_enabled
,
241 .recalc_rate
= ccu_nm_recalc_rate
,
242 .round_rate
= ccu_nm_round_rate
,
243 .set_rate
= ccu_nm_set_rate
,