1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 #include <linux/clk-provider.h>
15 unsigned long n
, min_n
, max_n
;
16 unsigned long m
, min_m
, max_m
;
19 static unsigned long ccu_nm_calc_rate(unsigned long parent
,
20 unsigned long n
, unsigned long m
)
30 static void ccu_nm_find_best(unsigned long parent
, unsigned long rate
,
33 unsigned long best_rate
= 0;
34 unsigned long best_n
= 0, best_m
= 0;
37 for (_n
= nm
->min_n
; _n
<= nm
->max_n
; _n
++) {
38 for (_m
= nm
->min_m
; _m
<= nm
->max_m
; _m
++) {
39 unsigned long tmp_rate
= ccu_nm_calc_rate(parent
,
45 if ((rate
- tmp_rate
) < (rate
- best_rate
)) {
57 static void ccu_nm_disable(struct clk_hw
*hw
)
59 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
61 return ccu_gate_helper_disable(&nm
->common
, nm
->enable
);
64 static int ccu_nm_enable(struct clk_hw
*hw
)
66 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
68 return ccu_gate_helper_enable(&nm
->common
, nm
->enable
);
71 static int ccu_nm_is_enabled(struct clk_hw
*hw
)
73 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
75 return ccu_gate_helper_is_enabled(&nm
->common
, nm
->enable
);
78 static unsigned long ccu_nm_recalc_rate(struct clk_hw
*hw
,
79 unsigned long parent_rate
)
81 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
86 if (ccu_frac_helper_is_enabled(&nm
->common
, &nm
->frac
)) {
87 rate
= ccu_frac_helper_read_rate(&nm
->common
, &nm
->frac
);
89 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
90 rate
/= nm
->fixed_post_div
;
95 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
97 n
= reg
>> nm
->n
.shift
;
98 n
&= (1 << nm
->n
.width
) - 1;
103 m
= reg
>> nm
->m
.shift
;
104 m
&= (1 << nm
->m
.width
) - 1;
109 if (ccu_sdm_helper_is_enabled(&nm
->common
, &nm
->sdm
))
110 rate
= ccu_sdm_helper_read_rate(&nm
->common
, &nm
->sdm
, m
, n
);
112 rate
= ccu_nm_calc_rate(parent_rate
, n
, m
);
114 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
115 rate
/= nm
->fixed_post_div
;
120 static long ccu_nm_round_rate(struct clk_hw
*hw
, unsigned long rate
,
121 unsigned long *parent_rate
)
123 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
126 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
127 rate
*= nm
->fixed_post_div
;
129 if (rate
< nm
->min_rate
) {
131 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
132 rate
/= nm
->fixed_post_div
;
136 if (nm
->max_rate
&& rate
> nm
->max_rate
) {
138 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
139 rate
/= nm
->fixed_post_div
;
143 if (ccu_frac_helper_has_rate(&nm
->common
, &nm
->frac
, rate
)) {
144 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
145 rate
/= nm
->fixed_post_div
;
149 if (ccu_sdm_helper_has_rate(&nm
->common
, &nm
->sdm
, rate
)) {
150 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
151 rate
/= nm
->fixed_post_div
;
155 _nm
.min_n
= nm
->n
.min
?: 1;
156 _nm
.max_n
= nm
->n
.max
?: 1 << nm
->n
.width
;
158 _nm
.max_m
= nm
->m
.max
?: 1 << nm
->m
.width
;
160 ccu_nm_find_best(*parent_rate
, rate
, &_nm
);
161 rate
= ccu_nm_calc_rate(*parent_rate
, _nm
.n
, _nm
.m
);
163 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
164 rate
/= nm
->fixed_post_div
;
169 static int ccu_nm_set_rate(struct clk_hw
*hw
, unsigned long rate
,
170 unsigned long parent_rate
)
172 struct ccu_nm
*nm
= hw_to_ccu_nm(hw
);
177 /* Adjust target rate according to post-dividers */
178 if (nm
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
179 rate
= rate
* nm
->fixed_post_div
;
181 if (ccu_frac_helper_has_rate(&nm
->common
, &nm
->frac
, rate
)) {
182 spin_lock_irqsave(nm
->common
.lock
, flags
);
184 /* most SoCs require M to be 0 if fractional mode is used */
185 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
186 reg
&= ~GENMASK(nm
->m
.width
+ nm
->m
.shift
- 1, nm
->m
.shift
);
187 writel(reg
, nm
->common
.base
+ nm
->common
.reg
);
189 spin_unlock_irqrestore(nm
->common
.lock
, flags
);
191 ccu_frac_helper_enable(&nm
->common
, &nm
->frac
);
193 return ccu_frac_helper_set_rate(&nm
->common
, &nm
->frac
,
196 ccu_frac_helper_disable(&nm
->common
, &nm
->frac
);
199 _nm
.min_n
= nm
->n
.min
?: 1;
200 _nm
.max_n
= nm
->n
.max
?: 1 << nm
->n
.width
;
202 _nm
.max_m
= nm
->m
.max
?: 1 << nm
->m
.width
;
204 if (ccu_sdm_helper_has_rate(&nm
->common
, &nm
->sdm
, rate
)) {
205 ccu_sdm_helper_enable(&nm
->common
, &nm
->sdm
, rate
);
207 /* Sigma delta modulation requires specific N and M factors */
208 ccu_sdm_helper_get_factors(&nm
->common
, &nm
->sdm
, rate
,
211 ccu_sdm_helper_disable(&nm
->common
, &nm
->sdm
);
212 ccu_nm_find_best(parent_rate
, rate
, &_nm
);
215 spin_lock_irqsave(nm
->common
.lock
, flags
);
217 reg
= readl(nm
->common
.base
+ nm
->common
.reg
);
218 reg
&= ~GENMASK(nm
->n
.width
+ nm
->n
.shift
- 1, nm
->n
.shift
);
219 reg
&= ~GENMASK(nm
->m
.width
+ nm
->m
.shift
- 1, nm
->m
.shift
);
221 reg
|= (_nm
.n
- nm
->n
.offset
) << nm
->n
.shift
;
222 reg
|= (_nm
.m
- nm
->m
.offset
) << nm
->m
.shift
;
223 writel(reg
, nm
->common
.base
+ nm
->common
.reg
);
225 spin_unlock_irqrestore(nm
->common
.lock
, flags
);
227 ccu_helper_wait_for_lock(&nm
->common
, nm
->lock
);
232 const struct clk_ops ccu_nm_ops
= {
233 .disable
= ccu_nm_disable
,
234 .enable
= ccu_nm_enable
,
235 .is_enabled
= ccu_nm_is_enabled
,
237 .recalc_rate
= ccu_nm_recalc_rate
,
238 .round_rate
= ccu_nm_round_rate
,
239 .set_rate
= ccu_nm_set_rate
,