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 void ccu_nkmp_find_best(unsigned long parent
, unsigned long rate
,
24 struct _ccu_nkmp
*nkmp
)
26 unsigned long best_rate
= 0;
27 unsigned long best_n
= 0, best_k
= 0, best_m
= 0, best_p
= 0;
28 unsigned long _n
, _k
, _m
, _p
;
30 for (_k
= nkmp
->min_k
; _k
<= nkmp
->max_k
; _k
++) {
31 for (_n
= nkmp
->min_n
; _n
<= nkmp
->max_n
; _n
++) {
32 for (_m
= nkmp
->min_m
; _m
<= nkmp
->max_m
; _m
++) {
33 for (_p
= nkmp
->min_p
; _p
<= nkmp
->max_p
; _p
<<= 1) {
34 unsigned long tmp_rate
;
36 tmp_rate
= parent
* _n
* _k
/ (_m
* _p
);
41 if ((rate
- tmp_rate
) < (rate
- best_rate
)) {
59 static void ccu_nkmp_disable(struct clk_hw
*hw
)
61 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
63 return ccu_gate_helper_disable(&nkmp
->common
, nkmp
->enable
);
66 static int ccu_nkmp_enable(struct clk_hw
*hw
)
68 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
70 return ccu_gate_helper_enable(&nkmp
->common
, nkmp
->enable
);
73 static int ccu_nkmp_is_enabled(struct clk_hw
*hw
)
75 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
77 return ccu_gate_helper_is_enabled(&nkmp
->common
, nkmp
->enable
);
80 static unsigned long ccu_nkmp_recalc_rate(struct clk_hw
*hw
,
81 unsigned long parent_rate
)
83 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
84 unsigned long n
, m
, k
, p
;
87 reg
= readl(nkmp
->common
.base
+ nkmp
->common
.reg
);
89 n
= reg
>> nkmp
->n
.shift
;
90 n
&= (1 << nkmp
->n
.width
) - 1;
92 k
= reg
>> nkmp
->k
.shift
;
93 k
&= (1 << nkmp
->k
.width
) - 1;
95 m
= reg
>> nkmp
->m
.shift
;
96 m
&= (1 << nkmp
->m
.width
) - 1;
98 p
= reg
>> nkmp
->p
.shift
;
99 p
&= (1 << nkmp
->p
.width
) - 1;
101 return (parent_rate
* (n
+ 1) * (k
+ 1) >> p
) / (m
+ 1);
104 static long ccu_nkmp_round_rate(struct clk_hw
*hw
, unsigned long rate
,
105 unsigned long *parent_rate
)
107 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
108 struct _ccu_nkmp _nkmp
;
110 _nkmp
.min_n
= nkmp
->n
.min
;
111 _nkmp
.max_n
= 1 << nkmp
->n
.width
;
112 _nkmp
.min_k
= nkmp
->k
.min
;
113 _nkmp
.max_k
= 1 << nkmp
->k
.width
;
115 _nkmp
.max_m
= nkmp
->m
.max
?: 1 << nkmp
->m
.width
;
117 _nkmp
.max_p
= nkmp
->p
.max
?: 1 << ((1 << nkmp
->p
.width
) - 1);
119 ccu_nkmp_find_best(*parent_rate
, rate
, &_nkmp
);
121 return *parent_rate
* _nkmp
.n
* _nkmp
.k
/ (_nkmp
.m
* _nkmp
.p
);
124 static int ccu_nkmp_set_rate(struct clk_hw
*hw
, unsigned long rate
,
125 unsigned long parent_rate
)
127 struct ccu_nkmp
*nkmp
= hw_to_ccu_nkmp(hw
);
128 struct _ccu_nkmp _nkmp
;
133 _nkmp
.max_n
= 1 << nkmp
->n
.width
;
135 _nkmp
.max_k
= 1 << nkmp
->k
.width
;
137 _nkmp
.max_m
= nkmp
->m
.max
?: 1 << nkmp
->m
.width
;
139 _nkmp
.max_p
= nkmp
->p
.max
?: 1 << ((1 << nkmp
->p
.width
) - 1);
141 ccu_nkmp_find_best(parent_rate
, rate
, &_nkmp
);
143 spin_lock_irqsave(nkmp
->common
.lock
, flags
);
145 reg
= readl(nkmp
->common
.base
+ nkmp
->common
.reg
);
146 reg
&= ~GENMASK(nkmp
->n
.width
+ nkmp
->n
.shift
- 1, nkmp
->n
.shift
);
147 reg
&= ~GENMASK(nkmp
->k
.width
+ nkmp
->k
.shift
- 1, nkmp
->k
.shift
);
148 reg
&= ~GENMASK(nkmp
->m
.width
+ nkmp
->m
.shift
- 1, nkmp
->m
.shift
);
149 reg
&= ~GENMASK(nkmp
->p
.width
+ nkmp
->p
.shift
- 1, nkmp
->p
.shift
);
151 reg
|= (_nkmp
.n
- 1) << nkmp
->n
.shift
;
152 reg
|= (_nkmp
.k
- 1) << nkmp
->k
.shift
;
153 reg
|= (_nkmp
.m
- 1) << nkmp
->m
.shift
;
154 reg
|= ilog2(_nkmp
.p
) << nkmp
->p
.shift
;
156 writel(reg
, nkmp
->common
.base
+ nkmp
->common
.reg
);
158 spin_unlock_irqrestore(nkmp
->common
.lock
, flags
);
160 ccu_helper_wait_for_lock(&nkmp
->common
, nkmp
->lock
);
165 const struct clk_ops ccu_nkmp_ops
= {
166 .disable
= ccu_nkmp_disable
,
167 .enable
= ccu_nkmp_enable
,
168 .is_enabled
= ccu_nkmp_is_enabled
,
170 .recalc_rate
= ccu_nkmp_recalc_rate
,
171 .round_rate
= ccu_nkmp_round_rate
,
172 .set_rate
= ccu_nkmp_set_rate
,