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>
12 #include <linux/rational.h>
18 unsigned long n
, max_n
;
19 unsigned long k
, max_k
;
20 unsigned long m
, max_m
;
23 static void ccu_nkm_find_best(unsigned long parent
, unsigned long rate
,
26 unsigned long best_rate
= 0;
27 unsigned long best_n
= 0, best_k
= 0, best_m
= 0;
28 unsigned long _n
, _k
, _m
;
30 for (_k
= 1; _k
<= nkm
->max_k
; _k
++) {
31 unsigned long tmp_rate
;
33 rational_best_approximation(rate
/ _k
, parent
,
34 nkm
->max_n
, nkm
->max_m
, &_n
, &_m
);
36 tmp_rate
= parent
* _n
* _k
/ _m
;
41 if ((rate
- tmp_rate
) < (rate
- best_rate
)) {
54 static void ccu_nkm_disable(struct clk_hw
*hw
)
56 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
58 return ccu_gate_helper_disable(&nkm
->common
, nkm
->enable
);
61 static int ccu_nkm_enable(struct clk_hw
*hw
)
63 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
65 return ccu_gate_helper_enable(&nkm
->common
, nkm
->enable
);
68 static int ccu_nkm_is_enabled(struct clk_hw
*hw
)
70 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
72 return ccu_gate_helper_is_enabled(&nkm
->common
, nkm
->enable
);
75 static unsigned long ccu_nkm_recalc_rate(struct clk_hw
*hw
,
76 unsigned long parent_rate
)
78 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
79 unsigned long n
, m
, k
;
82 reg
= readl(nkm
->common
.base
+ nkm
->common
.reg
);
84 n
= reg
>> nkm
->n
.shift
;
85 n
&= (1 << nkm
->n
.width
) - 1;
87 k
= reg
>> nkm
->k
.shift
;
88 k
&= (1 << nkm
->k
.width
) - 1;
90 m
= reg
>> nkm
->m
.shift
;
91 m
&= (1 << nkm
->m
.width
) - 1;
93 return parent_rate
* (n
+ 1) * (k
+ 1) / (m
+ 1);
96 static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal
*mux
,
97 unsigned long parent_rate
,
101 struct ccu_nkm
*nkm
= data
;
102 struct _ccu_nkm _nkm
;
104 _nkm
.max_n
= 1 << nkm
->n
.width
;
105 _nkm
.max_k
= 1 << nkm
->k
.width
;
106 _nkm
.max_m
= nkm
->m
.max
?: 1 << nkm
->m
.width
;
108 ccu_nkm_find_best(parent_rate
, rate
, &_nkm
);
110 return parent_rate
* _nkm
.n
* _nkm
.k
/ _nkm
.m
;
113 static int ccu_nkm_determine_rate(struct clk_hw
*hw
,
114 struct clk_rate_request
*req
)
116 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
118 return ccu_mux_helper_determine_rate(&nkm
->common
, &nkm
->mux
,
119 req
, ccu_nkm_round_rate
, nkm
);
122 static int ccu_nkm_set_rate(struct clk_hw
*hw
, unsigned long rate
,
123 unsigned long parent_rate
)
125 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
126 struct _ccu_nkm _nkm
;
130 _nkm
.max_n
= 1 << nkm
->n
.width
;
131 _nkm
.max_k
= 1 << nkm
->k
.width
;
132 _nkm
.max_m
= nkm
->m
.max
?: 1 << nkm
->m
.width
;
134 ccu_nkm_find_best(parent_rate
, rate
, &_nkm
);
136 spin_lock_irqsave(nkm
->common
.lock
, flags
);
138 reg
= readl(nkm
->common
.base
+ nkm
->common
.reg
);
139 reg
&= ~GENMASK(nkm
->n
.width
+ nkm
->n
.shift
- 1, nkm
->n
.shift
);
140 reg
&= ~GENMASK(nkm
->k
.width
+ nkm
->k
.shift
- 1, nkm
->k
.shift
);
141 reg
&= ~GENMASK(nkm
->m
.width
+ nkm
->m
.shift
- 1, nkm
->m
.shift
);
143 reg
|= (_nkm
.n
- 1) << nkm
->n
.shift
;
144 reg
|= (_nkm
.k
- 1) << nkm
->k
.shift
;
145 reg
|= (_nkm
.m
- 1) << nkm
->m
.shift
;
147 writel(reg
, nkm
->common
.base
+ nkm
->common
.reg
);
149 spin_unlock_irqrestore(nkm
->common
.lock
, flags
);
151 ccu_helper_wait_for_lock(&nkm
->common
, nkm
->lock
);
156 static u8
ccu_nkm_get_parent(struct clk_hw
*hw
)
158 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
160 return ccu_mux_helper_get_parent(&nkm
->common
, &nkm
->mux
);
163 static int ccu_nkm_set_parent(struct clk_hw
*hw
, u8 index
)
165 struct ccu_nkm
*nkm
= hw_to_ccu_nkm(hw
);
167 return ccu_mux_helper_set_parent(&nkm
->common
, &nkm
->mux
, index
);
170 const struct clk_ops ccu_nkm_ops
= {
171 .disable
= ccu_nkm_disable
,
172 .enable
= ccu_nkm_enable
,
173 .is_enabled
= ccu_nkm_is_enabled
,
175 .get_parent
= ccu_nkm_get_parent
,
176 .set_parent
= ccu_nkm_set_parent
,
178 .determine_rate
= ccu_nkm_determine_rate
,
179 .recalc_rate
= ccu_nkm_recalc_rate
,
180 .set_rate
= ccu_nkm_set_rate
,