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>
17 unsigned long n
, min_n
, max_n
;
18 unsigned long k
, min_k
, max_k
;
19 unsigned long m
, min_m
, max_m
;
20 unsigned long p
, min_p
, max_p
;
23 static unsigned long ccu_nkmp_calc_rate(unsigned long parent
,
24 unsigned long n
, unsigned long k
,
25 unsigned long m
, unsigned long p
)
35 static void ccu_nkmp_find_best(unsigned long parent
, unsigned long rate
,
36 struct _ccu_nkmp
*nkmp
)
38 unsigned long best_rate
= 0;
39 unsigned long best_n
= 0, best_k
= 0, best_m
= 0, best_p
= 0;
40 unsigned long _n
, _k
, _m
, _p
;
42 for (_k
= nkmp
->min_k
; _k
<= nkmp
->max_k
; _k
++) {
43 for (_n
= nkmp
->min_n
; _n
<= nkmp
->max_n
; _n
++) {
44 for (_m
= nkmp
->min_m
; _m
<= nkmp
->max_m
; _m
++) {
45 for (_p
= nkmp
->min_p
; _p
<= nkmp
->max_p
; _p
<<= 1) {
46 unsigned long tmp_rate
;
48 tmp_rate
= ccu_nkmp_calc_rate(parent
,
55 if ((rate
- tmp_rate
) < (rate
- best_rate
)) {
73 static void ccu_nkmp_disable(struct clk_hw
*hw
)
75 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
77 return ccu_gate_helper_disable(&nkmp
->common
, nkmp
->enable
);
80 static int ccu_nkmp_enable(struct clk_hw
*hw
)
82 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
84 return ccu_gate_helper_enable(&nkmp
->common
, nkmp
->enable
);
87 static int ccu_nkmp_is_enabled(struct clk_hw
*hw
)
89 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
91 return ccu_gate_helper_is_enabled(&nkmp
->common
, nkmp
->enable
);
94 static unsigned long ccu_nkmp_recalc_rate(struct clk_hw
*hw
,
95 unsigned long parent_rate
)
97 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
98 unsigned long n
, m
, k
, p
, rate
;
101 reg
= readl(nkmp
->common
.base
+ nkmp
->common
.reg
);
103 n
= reg
>> nkmp
->n
.shift
;
104 n
&= (1 << nkmp
->n
.width
) - 1;
109 k
= reg
>> nkmp
->k
.shift
;
110 k
&= (1 << nkmp
->k
.width
) - 1;
115 m
= reg
>> nkmp
->m
.shift
;
116 m
&= (1 << nkmp
->m
.width
) - 1;
121 p
= reg
>> nkmp
->p
.shift
;
122 p
&= (1 << nkmp
->p
.width
) - 1;
124 rate
= ccu_nkmp_calc_rate(parent_rate
, n
, k
, m
, 1 << p
);
125 if (nkmp
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
126 rate
/= nkmp
->fixed_post_div
;
131 static long ccu_nkmp_round_rate(struct clk_hw
*hw
, unsigned long rate
,
132 unsigned long *parent_rate
)
134 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
135 struct _ccu_nkmp _nkmp
;
137 if (nkmp
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
138 rate
*= nkmp
->fixed_post_div
;
140 _nkmp
.min_n
= nkmp
->n
.min
?: 1;
141 _nkmp
.max_n
= nkmp
->n
.max
?: 1 << nkmp
->n
.width
;
142 _nkmp
.min_k
= nkmp
->k
.min
?: 1;
143 _nkmp
.max_k
= nkmp
->k
.max
?: 1 << nkmp
->k
.width
;
145 _nkmp
.max_m
= nkmp
->m
.max
?: 1 << nkmp
->m
.width
;
147 _nkmp
.max_p
= nkmp
->p
.max
?: 1 << ((1 << nkmp
->p
.width
) - 1);
149 ccu_nkmp_find_best(*parent_rate
, rate
, &_nkmp
);
151 rate
= ccu_nkmp_calc_rate(*parent_rate
, _nkmp
.n
, _nkmp
.k
,
153 if (nkmp
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
154 rate
= rate
/ nkmp
->fixed_post_div
;
159 static int ccu_nkmp_set_rate(struct clk_hw
*hw
, unsigned long rate
,
160 unsigned long parent_rate
)
162 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
163 u32 n_mask
= 0, k_mask
= 0, m_mask
= 0, p_mask
= 0;
164 struct _ccu_nkmp _nkmp
;
168 if (nkmp
->common
.features
& CCU_FEATURE_FIXED_POSTDIV
)
169 rate
= rate
* nkmp
->fixed_post_div
;
171 _nkmp
.min_n
= nkmp
->n
.min
?: 1;
172 _nkmp
.max_n
= nkmp
->n
.max
?: 1 << nkmp
->n
.width
;
173 _nkmp
.min_k
= nkmp
->k
.min
?: 1;
174 _nkmp
.max_k
= nkmp
->k
.max
?: 1 << nkmp
->k
.width
;
176 _nkmp
.max_m
= nkmp
->m
.max
?: 1 << nkmp
->m
.width
;
178 _nkmp
.max_p
= nkmp
->p
.max
?: 1 << ((1 << nkmp
->p
.width
) - 1);
180 ccu_nkmp_find_best(parent_rate
, rate
, &_nkmp
);
183 n_mask
= GENMASK(nkmp
->n
.width
+ nkmp
->n
.shift
- 1,
186 k_mask
= GENMASK(nkmp
->k
.width
+ nkmp
->k
.shift
- 1,
189 m_mask
= GENMASK(nkmp
->m
.width
+ nkmp
->m
.shift
- 1,
192 p_mask
= GENMASK(nkmp
->p
.width
+ nkmp
->p
.shift
- 1,
195 spin_lock_irqsave(nkmp
->common
.lock
, flags
);
197 reg
= readl(nkmp
->common
.base
+ nkmp
->common
.reg
);
198 reg
&= ~(n_mask
| k_mask
| m_mask
| p_mask
);
200 reg
|= ((_nkmp
.n
- nkmp
->n
.offset
) << nkmp
->n
.shift
) & n_mask
;
201 reg
|= ((_nkmp
.k
- nkmp
->k
.offset
) << nkmp
->k
.shift
) & k_mask
;
202 reg
|= ((_nkmp
.m
- nkmp
->m
.offset
) << nkmp
->m
.shift
) & m_mask
;
203 reg
|= (ilog2(_nkmp
.p
) << nkmp
->p
.shift
) & p_mask
;
205 writel(reg
, nkmp
->common
.base
+ nkmp
->common
.reg
);
207 spin_unlock_irqrestore(nkmp
->common
.lock
, flags
);
209 ccu_helper_wait_for_lock(&nkmp
->common
, nkmp
->lock
);
214 const struct clk_ops ccu_nkmp_ops
= {
215 .disable
= ccu_nkmp_disable
,
216 .enable
= ccu_nkmp_enable
,
217 .is_enabled
= ccu_nkmp_is_enabled
,
219 .recalc_rate
= ccu_nkmp_recalc_rate
,
220 .round_rate
= ccu_nkmp_round_rate
,
221 .set_rate
= ccu_nkmp_set_rate
,