12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2df6561e6SMaxime Ripard /*
3df6561e6SMaxime Ripard * Copyright (C) 2016 Maxime Ripard
4df6561e6SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com>
5df6561e6SMaxime Ripard */
6df6561e6SMaxime Ripard
7df6561e6SMaxime Ripard #include <linux/clk-provider.h>
862e59c4eSStephen Boyd #include <linux/io.h>
9df6561e6SMaxime Ripard
10df6561e6SMaxime Ripard #include "ccu_gate.h"
11df6561e6SMaxime Ripard #include "ccu_nkm.h"
12df6561e6SMaxime Ripard
13df6561e6SMaxime Ripard struct _ccu_nkm {
146e0d50daSMaxime Ripard unsigned long n, min_n, max_n;
156e0d50daSMaxime Ripard unsigned long k, min_k, max_k;
166e0d50daSMaxime Ripard unsigned long m, min_m, max_m;
17df6561e6SMaxime Ripard };
18df6561e6SMaxime Ripard
ccu_nkm_is_valid_rate(struct ccu_common * common,unsigned long parent,unsigned long n,unsigned long m)19*5723879cSFrank Oltmanns static bool ccu_nkm_is_valid_rate(struct ccu_common *common, unsigned long parent,
20*5723879cSFrank Oltmanns unsigned long n, unsigned long m)
21*5723879cSFrank Oltmanns {
22*5723879cSFrank Oltmanns struct ccu_nkm *nkm = container_of(common, struct ccu_nkm, common);
23*5723879cSFrank Oltmanns
24*5723879cSFrank Oltmanns if (nkm->max_m_n_ratio && (m > nkm->max_m_n_ratio * n))
25*5723879cSFrank Oltmanns return false;
26*5723879cSFrank Oltmanns
27*5723879cSFrank Oltmanns if (nkm->min_parent_m_ratio && (parent < nkm->min_parent_m_ratio * m))
28*5723879cSFrank Oltmanns return false;
29*5723879cSFrank Oltmanns
30*5723879cSFrank Oltmanns return true;
31*5723879cSFrank Oltmanns }
32*5723879cSFrank Oltmanns
ccu_nkm_find_best_with_parent_adj(struct ccu_common * common,struct clk_hw * parent_hw,unsigned long * parent,unsigned long rate,struct _ccu_nkm * nkm)33088cefcaSFrank Oltmanns static unsigned long ccu_nkm_find_best_with_parent_adj(struct ccu_common *common,
34088cefcaSFrank Oltmanns struct clk_hw *parent_hw,
353492e4f6SFrank Oltmanns unsigned long *parent, unsigned long rate,
363492e4f6SFrank Oltmanns struct _ccu_nkm *nkm)
373492e4f6SFrank Oltmanns {
3889e00444SColin Ian King unsigned long best_rate = 0, best_parent_rate = *parent;
393492e4f6SFrank Oltmanns unsigned long best_n = 0, best_k = 0, best_m = 0;
403492e4f6SFrank Oltmanns unsigned long _n, _k, _m;
413492e4f6SFrank Oltmanns
423492e4f6SFrank Oltmanns for (_k = nkm->min_k; _k <= nkm->max_k; _k++) {
433492e4f6SFrank Oltmanns for (_n = nkm->min_n; _n <= nkm->max_n; _n++) {
443492e4f6SFrank Oltmanns for (_m = nkm->min_m; _m <= nkm->max_m; _m++) {
4589e00444SColin Ian King unsigned long tmp_rate, tmp_parent;
463492e4f6SFrank Oltmanns
473492e4f6SFrank Oltmanns tmp_parent = clk_hw_round_rate(parent_hw, rate * _m / (_n * _k));
48*5723879cSFrank Oltmanns
49*5723879cSFrank Oltmanns if (!ccu_nkm_is_valid_rate(common, tmp_parent, _n, _m))
50*5723879cSFrank Oltmanns continue;
51*5723879cSFrank Oltmanns
523492e4f6SFrank Oltmanns tmp_rate = tmp_parent * _n * _k / _m;
533492e4f6SFrank Oltmanns
54a69f9463SFrank Oltmanns if (ccu_is_better_rate(common, rate, tmp_rate, best_rate) ||
55a69f9463SFrank Oltmanns (tmp_parent == *parent && tmp_rate == best_rate)) {
563492e4f6SFrank Oltmanns best_rate = tmp_rate;
573492e4f6SFrank Oltmanns best_parent_rate = tmp_parent;
583492e4f6SFrank Oltmanns best_n = _n;
593492e4f6SFrank Oltmanns best_k = _k;
603492e4f6SFrank Oltmanns best_m = _m;
613492e4f6SFrank Oltmanns }
623492e4f6SFrank Oltmanns }
633492e4f6SFrank Oltmanns }
643492e4f6SFrank Oltmanns }
653492e4f6SFrank Oltmanns
663492e4f6SFrank Oltmanns nkm->n = best_n;
673492e4f6SFrank Oltmanns nkm->k = best_k;
683492e4f6SFrank Oltmanns nkm->m = best_m;
693492e4f6SFrank Oltmanns
703492e4f6SFrank Oltmanns *parent = best_parent_rate;
713492e4f6SFrank Oltmanns
723492e4f6SFrank Oltmanns return best_rate;
733492e4f6SFrank Oltmanns }
743492e4f6SFrank Oltmanns
ccu_nkm_find_best(unsigned long parent,unsigned long rate,struct _ccu_nkm * nkm,struct ccu_common * common)75657f477aSSamuel Holland static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate,
76088cefcaSFrank Oltmanns struct _ccu_nkm *nkm, struct ccu_common *common)
77df6561e6SMaxime Ripard {
78df6561e6SMaxime Ripard unsigned long best_rate = 0;
79df6561e6SMaxime Ripard unsigned long best_n = 0, best_k = 0, best_m = 0;
80df6561e6SMaxime Ripard unsigned long _n, _k, _m;
81df6561e6SMaxime Ripard
826e0d50daSMaxime Ripard for (_k = nkm->min_k; _k <= nkm->max_k; _k++) {
836e0d50daSMaxime Ripard for (_n = nkm->min_n; _n <= nkm->max_n; _n++) {
846e0d50daSMaxime Ripard for (_m = nkm->min_m; _m <= nkm->max_m; _m++) {
85*5723879cSFrank Oltmanns if (!ccu_nkm_is_valid_rate(common, parent, _n, _m))
86*5723879cSFrank Oltmanns continue;
87*5723879cSFrank Oltmanns
88df6561e6SMaxime Ripard unsigned long tmp_rate;
89df6561e6SMaxime Ripard
90df6561e6SMaxime Ripard tmp_rate = parent * _n * _k / _m;
91df6561e6SMaxime Ripard
92088cefcaSFrank Oltmanns if (ccu_is_better_rate(common, rate, tmp_rate, best_rate)) {
93df6561e6SMaxime Ripard best_rate = tmp_rate;
94df6561e6SMaxime Ripard best_n = _n;
95df6561e6SMaxime Ripard best_k = _k;
96df6561e6SMaxime Ripard best_m = _m;
97df6561e6SMaxime Ripard }
98df6561e6SMaxime Ripard }
99ee28648cSMaxime Ripard }
100ee28648cSMaxime Ripard }
101df6561e6SMaxime Ripard
102df6561e6SMaxime Ripard nkm->n = best_n;
103df6561e6SMaxime Ripard nkm->k = best_k;
104df6561e6SMaxime Ripard nkm->m = best_m;
105657f477aSSamuel Holland
106657f477aSSamuel Holland return best_rate;
107df6561e6SMaxime Ripard }
108df6561e6SMaxime Ripard
ccu_nkm_disable(struct clk_hw * hw)109df6561e6SMaxime Ripard static void ccu_nkm_disable(struct clk_hw *hw)
110df6561e6SMaxime Ripard {
111df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
112df6561e6SMaxime Ripard
113df6561e6SMaxime Ripard return ccu_gate_helper_disable(&nkm->common, nkm->enable);
114df6561e6SMaxime Ripard }
115df6561e6SMaxime Ripard
ccu_nkm_enable(struct clk_hw * hw)116df6561e6SMaxime Ripard static int ccu_nkm_enable(struct clk_hw *hw)
117df6561e6SMaxime Ripard {
118df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
119df6561e6SMaxime Ripard
120df6561e6SMaxime Ripard return ccu_gate_helper_enable(&nkm->common, nkm->enable);
121df6561e6SMaxime Ripard }
122df6561e6SMaxime Ripard
ccu_nkm_is_enabled(struct clk_hw * hw)123df6561e6SMaxime Ripard static int ccu_nkm_is_enabled(struct clk_hw *hw)
124df6561e6SMaxime Ripard {
125df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
126df6561e6SMaxime Ripard
127df6561e6SMaxime Ripard return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable);
128df6561e6SMaxime Ripard }
129df6561e6SMaxime Ripard
ccu_nkm_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)130df6561e6SMaxime Ripard static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw,
131df6561e6SMaxime Ripard unsigned long parent_rate)
132df6561e6SMaxime Ripard {
133df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
134a6653773SIcenowy Zheng unsigned long n, m, k, rate;
135df6561e6SMaxime Ripard u32 reg;
136df6561e6SMaxime Ripard
137df6561e6SMaxime Ripard reg = readl(nkm->common.base + nkm->common.reg);
138df6561e6SMaxime Ripard
139df6561e6SMaxime Ripard n = reg >> nkm->n.shift;
140df6561e6SMaxime Ripard n &= (1 << nkm->n.width) - 1;
141e66f81bbSMaxime Ripard n += nkm->n.offset;
142e66f81bbSMaxime Ripard if (!n)
143e66f81bbSMaxime Ripard n++;
144df6561e6SMaxime Ripard
145df6561e6SMaxime Ripard k = reg >> nkm->k.shift;
146df6561e6SMaxime Ripard k &= (1 << nkm->k.width) - 1;
147e66f81bbSMaxime Ripard k += nkm->k.offset;
148e66f81bbSMaxime Ripard if (!k)
149e66f81bbSMaxime Ripard k++;
150df6561e6SMaxime Ripard
151df6561e6SMaxime Ripard m = reg >> nkm->m.shift;
152df6561e6SMaxime Ripard m &= (1 << nkm->m.width) - 1;
153e66f81bbSMaxime Ripard m += nkm->m.offset;
154e66f81bbSMaxime Ripard if (!m)
155e66f81bbSMaxime Ripard m++;
156df6561e6SMaxime Ripard
157a6653773SIcenowy Zheng rate = parent_rate * n * k / m;
158a6653773SIcenowy Zheng
159a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
160a6653773SIcenowy Zheng rate /= nkm->fixed_post_div;
161a6653773SIcenowy Zheng
162a6653773SIcenowy Zheng return rate;
163df6561e6SMaxime Ripard }
164df6561e6SMaxime Ripard
ccu_nkm_round_rate(struct ccu_mux_internal * mux,struct clk_hw * parent_hw,unsigned long * parent_rate,unsigned long rate,void * data)165a3658359SChen-Yu Tsai static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
16680c439cdSFrank Oltmanns struct clk_hw *parent_hw,
16710a8d9b9SMaxime Ripard unsigned long *parent_rate,
168a3658359SChen-Yu Tsai unsigned long rate,
169a3658359SChen-Yu Tsai void *data)
170df6561e6SMaxime Ripard {
171a3658359SChen-Yu Tsai struct ccu_nkm *nkm = data;
172df6561e6SMaxime Ripard struct _ccu_nkm _nkm;
173df6561e6SMaxime Ripard
1744162c5ceSChen-Yu Tsai _nkm.min_n = nkm->n.min ?: 1;
1750c3c8e13SMaxime Ripard _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
1764162c5ceSChen-Yu Tsai _nkm.min_k = nkm->k.min ?: 1;
1770c3c8e13SMaxime Ripard _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
1786e0d50daSMaxime Ripard _nkm.min_m = 1;
17987ba9e59SMaxime Ripard _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
180df6561e6SMaxime Ripard
181a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
182a6653773SIcenowy Zheng rate *= nkm->fixed_post_div;
183a6653773SIcenowy Zheng
1843492e4f6SFrank Oltmanns if (!clk_hw_can_set_rate_parent(&nkm->common.hw))
185088cefcaSFrank Oltmanns rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm, &nkm->common);
1863492e4f6SFrank Oltmanns else
187088cefcaSFrank Oltmanns rate = ccu_nkm_find_best_with_parent_adj(&nkm->common, parent_hw, parent_rate, rate,
188088cefcaSFrank Oltmanns &_nkm);
189a6653773SIcenowy Zheng
190a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
191a6653773SIcenowy Zheng rate /= nkm->fixed_post_div;
192a6653773SIcenowy Zheng
193a6653773SIcenowy Zheng return rate;
194a3658359SChen-Yu Tsai }
195a3658359SChen-Yu Tsai
ccu_nkm_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)196a3658359SChen-Yu Tsai static int ccu_nkm_determine_rate(struct clk_hw *hw,
197a3658359SChen-Yu Tsai struct clk_rate_request *req)
198a3658359SChen-Yu Tsai {
199a3658359SChen-Yu Tsai struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
200a3658359SChen-Yu Tsai
201a3658359SChen-Yu Tsai return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux,
202a3658359SChen-Yu Tsai req, ccu_nkm_round_rate, nkm);
203df6561e6SMaxime Ripard }
204df6561e6SMaxime Ripard
ccu_nkm_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)205df6561e6SMaxime Ripard static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
206df6561e6SMaxime Ripard unsigned long parent_rate)
207df6561e6SMaxime Ripard {
208df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
209df6561e6SMaxime Ripard struct _ccu_nkm _nkm;
210df6561e6SMaxime Ripard unsigned long flags;
211df6561e6SMaxime Ripard u32 reg;
212df6561e6SMaxime Ripard
213a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
214a6653773SIcenowy Zheng rate *= nkm->fixed_post_div;
215a6653773SIcenowy Zheng
2164162c5ceSChen-Yu Tsai _nkm.min_n = nkm->n.min ?: 1;
2170c3c8e13SMaxime Ripard _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
2184162c5ceSChen-Yu Tsai _nkm.min_k = nkm->k.min ?: 1;
2190c3c8e13SMaxime Ripard _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
2206e0d50daSMaxime Ripard _nkm.min_m = 1;
22187ba9e59SMaxime Ripard _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
222df6561e6SMaxime Ripard
223088cefcaSFrank Oltmanns ccu_nkm_find_best(parent_rate, rate, &_nkm, &nkm->common);
224df6561e6SMaxime Ripard
225df6561e6SMaxime Ripard spin_lock_irqsave(nkm->common.lock, flags);
226df6561e6SMaxime Ripard
227df6561e6SMaxime Ripard reg = readl(nkm->common.base + nkm->common.reg);
228df6561e6SMaxime Ripard reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
229df6561e6SMaxime Ripard reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
230df6561e6SMaxime Ripard reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
231df6561e6SMaxime Ripard
232e66f81bbSMaxime Ripard reg |= (_nkm.n - nkm->n.offset) << nkm->n.shift;
233e66f81bbSMaxime Ripard reg |= (_nkm.k - nkm->k.offset) << nkm->k.shift;
234e66f81bbSMaxime Ripard reg |= (_nkm.m - nkm->m.offset) << nkm->m.shift;
235df6561e6SMaxime Ripard writel(reg, nkm->common.base + nkm->common.reg);
236df6561e6SMaxime Ripard
237df6561e6SMaxime Ripard spin_unlock_irqrestore(nkm->common.lock, flags);
238df6561e6SMaxime Ripard
239df6561e6SMaxime Ripard ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
240df6561e6SMaxime Ripard
241df6561e6SMaxime Ripard return 0;
242df6561e6SMaxime Ripard }
243df6561e6SMaxime Ripard
ccu_nkm_get_parent(struct clk_hw * hw)244a3658359SChen-Yu Tsai static u8 ccu_nkm_get_parent(struct clk_hw *hw)
245a3658359SChen-Yu Tsai {
246a3658359SChen-Yu Tsai struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
247a3658359SChen-Yu Tsai
248a3658359SChen-Yu Tsai return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux);
249a3658359SChen-Yu Tsai }
250a3658359SChen-Yu Tsai
ccu_nkm_set_parent(struct clk_hw * hw,u8 index)251a3658359SChen-Yu Tsai static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index)
252a3658359SChen-Yu Tsai {
253a3658359SChen-Yu Tsai struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
254a3658359SChen-Yu Tsai
255a3658359SChen-Yu Tsai return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index);
256a3658359SChen-Yu Tsai }
257a3658359SChen-Yu Tsai
258df6561e6SMaxime Ripard const struct clk_ops ccu_nkm_ops = {
259df6561e6SMaxime Ripard .disable = ccu_nkm_disable,
260df6561e6SMaxime Ripard .enable = ccu_nkm_enable,
261df6561e6SMaxime Ripard .is_enabled = ccu_nkm_is_enabled,
262df6561e6SMaxime Ripard
263a3658359SChen-Yu Tsai .get_parent = ccu_nkm_get_parent,
264a3658359SChen-Yu Tsai .set_parent = ccu_nkm_set_parent,
265a3658359SChen-Yu Tsai
266a3658359SChen-Yu Tsai .determine_rate = ccu_nkm_determine_rate,
267df6561e6SMaxime Ripard .recalc_rate = ccu_nkm_recalc_rate,
268df6561e6SMaxime Ripard .set_rate = ccu_nkm_set_rate,
269df6561e6SMaxime Ripard };
270551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_nkm_ops, SUNXI_CCU);
271