• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Maxime Ripard
4  * Maxime Ripard <maxime.ripard@free-electrons.com>
5  */
6 
7 #include <linux/clk-provider.h>
8 #include <linux/io.h>
9 
10 #include "ccu_gate.h"
11 #include "ccu_nkm.h"
12 
13 struct _ccu_nkm {
14 	unsigned long	n, min_n, max_n;
15 	unsigned long	k, min_k, max_k;
16 	unsigned long	m, min_m, max_m;
17 };
18 
ccu_nkm_find_best(unsigned long parent,u64 rate,struct _ccu_nkm * nkm)19 static void ccu_nkm_find_best(unsigned long parent, u64 rate,
20 			      struct _ccu_nkm *nkm)
21 {
22 	u64 best_rate = 0;
23 	unsigned long best_n = 0, best_k = 0, best_m = 0;
24 	unsigned long _n, _k, _m;
25 
26 	for (_k = nkm->min_k; _k <= nkm->max_k; _k++) {
27 		for (_n = nkm->min_n; _n <= nkm->max_n; _n++) {
28 			for (_m = nkm->min_m; _m <= nkm->max_m; _m++) {
29 				u64 tmp_rate;
30 				tmp_rate = parent * _n * _k;
31 				do_div(tmp_rate, _m);
32 
33 				if (tmp_rate > rate)
34 					continue;
35 				if ((rate - tmp_rate) < (rate - best_rate)) {
36 					best_rate = tmp_rate;
37 					best_n = _n;
38 					best_k = _k;
39 					best_m = _m;
40 				}
41 			}
42 		}
43 	}
44 
45 	nkm->n = best_n;
46 	nkm->k = best_k;
47 	nkm->m = best_m;
48 }
49 
ccu_nkm_disable(struct clk_hw * hw)50 static void ccu_nkm_disable(struct clk_hw *hw)
51 {
52 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
53 
54 	return ccu_gate_helper_disable(&nkm->common, nkm->enable);
55 }
56 
ccu_nkm_enable(struct clk_hw * hw)57 static int ccu_nkm_enable(struct clk_hw *hw)
58 {
59 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
60 
61 	return ccu_gate_helper_enable(&nkm->common, nkm->enable);
62 }
63 
ccu_nkm_is_enabled(struct clk_hw * hw)64 static int ccu_nkm_is_enabled(struct clk_hw *hw)
65 {
66 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
67 
68 	return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable);
69 }
70 
ccu_nkm_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)71 static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw,
72 					unsigned long parent_rate)
73 {
74 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
75 	unsigned long n, m, k, rate;
76 	u32 reg;
77 
78 	reg = readl(nkm->common.base + nkm->common.reg);
79 
80 	n = reg >> nkm->n.shift;
81 	n &= (1 << nkm->n.width) - 1;
82 	n += nkm->n.offset;
83 	if (!n)
84 		n++;
85 
86 	k = reg >> nkm->k.shift;
87 	k &= (1 << nkm->k.width) - 1;
88 	k += nkm->k.offset;
89 	if (!k)
90 		k++;
91 
92 	m = reg >> nkm->m.shift;
93 	m &= (1 << nkm->m.width) - 1;
94 	m += nkm->m.offset;
95 	if (!m)
96 		m++;
97 
98 	rate = parent_rate * n  * k / m;
99 
100 	if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
101 		rate /= nkm->fixed_post_div;
102 
103 	return rate;
104 }
105 
ccu_nkm_round_rate(struct ccu_mux_internal * mux,struct clk_hw * hw,unsigned long * parent_rate,unsigned long _rate,void * data)106 static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
107 					struct clk_hw *hw,
108 					unsigned long *parent_rate,
109 					unsigned long _rate,
110 					void *data)
111 {
112 	struct ccu_nkm *nkm = data;
113 	struct _ccu_nkm _nkm;
114 	u64 rate = _rate;
115 
116 	_nkm.min_n = nkm->n.min ?: 1;
117 	_nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
118 	_nkm.min_k = nkm->k.min ?: 1;
119 	_nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
120 	_nkm.min_m = 1;
121 	_nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
122 
123 	if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
124 		rate *= nkm->fixed_post_div;
125 
126 	ccu_nkm_find_best(*parent_rate, rate, &_nkm);
127 
128 	rate = *parent_rate * _nkm.n * _nkm.k;
129 	do_div(rate, _nkm.m);
130 
131 	if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
132 		do_div(rate, nkm->fixed_post_div);
133 
134 	return rate;
135 }
136 
ccu_nkm_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)137 static int ccu_nkm_determine_rate(struct clk_hw *hw,
138 				  struct clk_rate_request *req)
139 {
140 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
141 
142 	return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux,
143 					     req, ccu_nkm_round_rate, nkm);
144 }
145 
ccu_nkm_set_rate(struct clk_hw * hw,unsigned long _rate,unsigned long parent_rate)146 static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long _rate,
147 			   unsigned long parent_rate)
148 {
149 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
150 	struct _ccu_nkm _nkm;
151 	unsigned long flags;
152 	u64 rate = _rate;
153 	u32 reg;
154 
155 	if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
156 		rate *= nkm->fixed_post_div;
157 
158 	_nkm.min_n = nkm->n.min ?: 1;
159 	_nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
160 	_nkm.min_k = nkm->k.min ?: 1;
161 	_nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
162 	_nkm.min_m = 1;
163 	_nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
164 
165 	ccu_nkm_find_best(parent_rate, rate, &_nkm);
166 
167 	spin_lock_irqsave(nkm->common.lock, flags);
168 
169 	reg = readl(nkm->common.base + nkm->common.reg);
170 	reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
171 	reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
172 	reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
173 
174 	reg |= (_nkm.n - nkm->n.offset) << nkm->n.shift;
175 	reg |= (_nkm.k - nkm->k.offset) << nkm->k.shift;
176 	reg |= (_nkm.m - nkm->m.offset) << nkm->m.shift;
177 	writel(reg, nkm->common.base + nkm->common.reg);
178 
179 	spin_unlock_irqrestore(nkm->common.lock, flags);
180 
181 	ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
182 
183 	return 0;
184 }
185 
ccu_nkm_get_parent(struct clk_hw * hw)186 static u8 ccu_nkm_get_parent(struct clk_hw *hw)
187 {
188 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
189 
190 	return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux);
191 }
192 
ccu_nkm_set_parent(struct clk_hw * hw,u8 index)193 static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index)
194 {
195 	struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
196 
197 	return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index);
198 }
199 
200 const struct clk_ops ccu_nkm_ops = {
201 	.disable	= ccu_nkm_disable,
202 	.enable		= ccu_nkm_enable,
203 	.is_enabled	= ccu_nkm_is_enabled,
204 
205 	.get_parent	= ccu_nkm_get_parent,
206 	.set_parent	= ccu_nkm_set_parent,
207 
208 	.determine_rate	= ccu_nkm_determine_rate,
209 	.recalc_rate	= ccu_nkm_recalc_rate,
210 	.set_rate	= ccu_nkm_set_rate,
211 };
212