Merge tag 'extcon-next-for-5.4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / clk / meson / clk-pll.c
blobddb1e563473953007288b5585359fe991e9fbffe
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2015 Endless Mobile, Inc.
4 * Author: Carlo Caione <carlo@endlessm.com>
6 * Copyright (c) 2018 Baylibre, SAS.
7 * Author: Jerome Brunet <jbrunet@baylibre.com>
8 */
11 * In the most basic form, a Meson PLL is composed as follows:
13 * PLL
14 * +--------------------------------+
15 * | |
16 * | +--+ |
17 * in >>-----[ /N ]--->| | +-----+ |
18 * | | |------| DCO |---->> out
19 * | +--------->| | +--v--+ |
20 * | | +--+ | |
21 * | | | |
22 * | +--[ *(M + (F/Fmax) ]<--+ |
23 * | |
24 * +--------------------------------+
26 * out = in * (m + frac / frac_max) / n
29 #include <linux/clk-provider.h>
30 #include <linux/delay.h>
31 #include <linux/err.h>
32 #include <linux/io.h>
33 #include <linux/math64.h>
34 #include <linux/module.h>
35 #include <linux/rational.h>
37 #include "clk-regmap.h"
38 #include "clk-pll.h"
40 static inline struct meson_clk_pll_data *
41 meson_clk_pll_data(struct clk_regmap *clk)
43 return (struct meson_clk_pll_data *)clk->data;
46 static int __pll_round_closest_mult(struct meson_clk_pll_data *pll)
48 if ((pll->flags & CLK_MESON_PLL_ROUND_CLOSEST) &&
49 !MESON_PARM_APPLICABLE(&pll->frac))
50 return 1;
52 return 0;
55 static unsigned long __pll_params_to_rate(unsigned long parent_rate,
56 unsigned int m, unsigned int n,
57 unsigned int frac,
58 struct meson_clk_pll_data *pll)
60 u64 rate = (u64)parent_rate * m;
62 if (frac && MESON_PARM_APPLICABLE(&pll->frac)) {
63 u64 frac_rate = (u64)parent_rate * frac;
65 rate += DIV_ROUND_UP_ULL(frac_rate,
66 (1 << pll->frac.width));
69 return DIV_ROUND_UP_ULL(rate, n);
72 static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
73 unsigned long parent_rate)
75 struct clk_regmap *clk = to_clk_regmap(hw);
76 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
77 unsigned int m, n, frac;
79 n = meson_parm_read(clk->map, &pll->n);
80 m = meson_parm_read(clk->map, &pll->m);
82 frac = MESON_PARM_APPLICABLE(&pll->frac) ?
83 meson_parm_read(clk->map, &pll->frac) :
86 return __pll_params_to_rate(parent_rate, m, n, frac, pll);
89 static unsigned int __pll_params_with_frac(unsigned long rate,
90 unsigned long parent_rate,
91 unsigned int m,
92 unsigned int n,
93 struct meson_clk_pll_data *pll)
95 unsigned int frac_max = (1 << pll->frac.width);
96 u64 val = (u64)rate * n;
98 /* Bail out if we are already over the requested rate */
99 if (rate < parent_rate * m / n)
100 return 0;
102 if (pll->flags & CLK_MESON_PLL_ROUND_CLOSEST)
103 val = DIV_ROUND_CLOSEST_ULL(val * frac_max, parent_rate);
104 else
105 val = div_u64(val * frac_max, parent_rate);
107 val -= m * frac_max;
109 return min((unsigned int)val, (frac_max - 1));
112 static bool meson_clk_pll_is_better(unsigned long rate,
113 unsigned long best,
114 unsigned long now,
115 struct meson_clk_pll_data *pll)
117 if (__pll_round_closest_mult(pll)) {
118 /* Round Closest */
119 if (abs(now - rate) < abs(best - rate))
120 return true;
121 } else {
122 /* Round down */
123 if (now <= rate && best < now)
124 return true;
127 return false;
130 static int meson_clk_get_pll_table_index(unsigned int index,
131 unsigned int *m,
132 unsigned int *n,
133 struct meson_clk_pll_data *pll)
135 if (!pll->table[index].n)
136 return -EINVAL;
138 *m = pll->table[index].m;
139 *n = pll->table[index].n;
141 return 0;
144 static unsigned int meson_clk_get_pll_range_m(unsigned long rate,
145 unsigned long parent_rate,
146 unsigned int n,
147 struct meson_clk_pll_data *pll)
149 u64 val = (u64)rate * n;
151 if (__pll_round_closest_mult(pll))
152 return DIV_ROUND_CLOSEST_ULL(val, parent_rate);
154 return div_u64(val, parent_rate);
157 static int meson_clk_get_pll_range_index(unsigned long rate,
158 unsigned long parent_rate,
159 unsigned int index,
160 unsigned int *m,
161 unsigned int *n,
162 struct meson_clk_pll_data *pll)
164 *n = index + 1;
166 /* Check the predivider range */
167 if (*n >= (1 << pll->n.width))
168 return -EINVAL;
170 if (*n == 1) {
171 /* Get the boundaries out the way */
172 if (rate <= pll->range->min * parent_rate) {
173 *m = pll->range->min;
174 return -ENODATA;
175 } else if (rate >= pll->range->max * parent_rate) {
176 *m = pll->range->max;
177 return -ENODATA;
181 *m = meson_clk_get_pll_range_m(rate, parent_rate, *n, pll);
183 /* the pre-divider gives a multiplier too big - stop */
184 if (*m >= (1 << pll->m.width))
185 return -EINVAL;
187 return 0;
190 static int meson_clk_get_pll_get_index(unsigned long rate,
191 unsigned long parent_rate,
192 unsigned int index,
193 unsigned int *m,
194 unsigned int *n,
195 struct meson_clk_pll_data *pll)
197 if (pll->range)
198 return meson_clk_get_pll_range_index(rate, parent_rate,
199 index, m, n, pll);
200 else if (pll->table)
201 return meson_clk_get_pll_table_index(index, m, n, pll);
203 return -EINVAL;
206 static int meson_clk_get_pll_settings(unsigned long rate,
207 unsigned long parent_rate,
208 unsigned int *best_m,
209 unsigned int *best_n,
210 struct meson_clk_pll_data *pll)
212 unsigned long best = 0, now = 0;
213 unsigned int i, m, n;
214 int ret;
216 for (i = 0, ret = 0; !ret; i++) {
217 ret = meson_clk_get_pll_get_index(rate, parent_rate,
218 i, &m, &n, pll);
219 if (ret == -EINVAL)
220 break;
222 now = __pll_params_to_rate(parent_rate, m, n, 0, pll);
223 if (meson_clk_pll_is_better(rate, best, now, pll)) {
224 best = now;
225 *best_m = m;
226 *best_n = n;
228 if (now == rate)
229 break;
233 return best ? 0 : -EINVAL;
236 static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
237 unsigned long *parent_rate)
239 struct clk_regmap *clk = to_clk_regmap(hw);
240 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
241 unsigned int m, n, frac;
242 unsigned long round;
243 int ret;
245 ret = meson_clk_get_pll_settings(rate, *parent_rate, &m, &n, pll);
246 if (ret)
247 return meson_clk_pll_recalc_rate(hw, *parent_rate);
249 round = __pll_params_to_rate(*parent_rate, m, n, 0, pll);
251 if (!MESON_PARM_APPLICABLE(&pll->frac) || rate == round)
252 return round;
255 * The rate provided by the setting is not an exact match, let's
256 * try to improve the result using the fractional parameter
258 frac = __pll_params_with_frac(rate, *parent_rate, m, n, pll);
260 return __pll_params_to_rate(*parent_rate, m, n, frac, pll);
263 static int meson_clk_pll_wait_lock(struct clk_hw *hw)
265 struct clk_regmap *clk = to_clk_regmap(hw);
266 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
267 int delay = 24000000;
269 do {
270 /* Is the clock locked now ? */
271 if (meson_parm_read(clk->map, &pll->l))
272 return 0;
274 delay--;
275 } while (delay > 0);
277 return -ETIMEDOUT;
280 static void meson_clk_pll_init(struct clk_hw *hw)
282 struct clk_regmap *clk = to_clk_regmap(hw);
283 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
285 if (pll->init_count) {
286 meson_parm_write(clk->map, &pll->rst, 1);
287 regmap_multi_reg_write(clk->map, pll->init_regs,
288 pll->init_count);
289 meson_parm_write(clk->map, &pll->rst, 0);
293 static int meson_clk_pll_is_enabled(struct clk_hw *hw)
295 struct clk_regmap *clk = to_clk_regmap(hw);
296 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
298 if (meson_parm_read(clk->map, &pll->rst) ||
299 !meson_parm_read(clk->map, &pll->en) ||
300 !meson_parm_read(clk->map, &pll->l))
301 return 0;
303 return 1;
306 static int meson_clk_pcie_pll_enable(struct clk_hw *hw)
308 meson_clk_pll_init(hw);
310 if (meson_clk_pll_wait_lock(hw))
311 return -EIO;
313 return 0;
316 static int meson_clk_pll_enable(struct clk_hw *hw)
318 struct clk_regmap *clk = to_clk_regmap(hw);
319 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
321 /* do nothing if the PLL is already enabled */
322 if (clk_hw_is_enabled(hw))
323 return 0;
325 /* Make sure the pll is in reset */
326 meson_parm_write(clk->map, &pll->rst, 1);
328 /* Enable the pll */
329 meson_parm_write(clk->map, &pll->en, 1);
331 /* Take the pll out reset */
332 meson_parm_write(clk->map, &pll->rst, 0);
334 if (meson_clk_pll_wait_lock(hw))
335 return -EIO;
337 return 0;
340 static void meson_clk_pll_disable(struct clk_hw *hw)
342 struct clk_regmap *clk = to_clk_regmap(hw);
343 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
345 /* Put the pll is in reset */
346 meson_parm_write(clk->map, &pll->rst, 1);
348 /* Disable the pll */
349 meson_parm_write(clk->map, &pll->en, 0);
352 static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
353 unsigned long parent_rate)
355 struct clk_regmap *clk = to_clk_regmap(hw);
356 struct meson_clk_pll_data *pll = meson_clk_pll_data(clk);
357 unsigned int enabled, m, n, frac = 0, ret;
358 unsigned long old_rate;
360 if (parent_rate == 0 || rate == 0)
361 return -EINVAL;
363 old_rate = rate;
365 ret = meson_clk_get_pll_settings(rate, parent_rate, &m, &n, pll);
366 if (ret)
367 return ret;
369 enabled = meson_parm_read(clk->map, &pll->en);
370 if (enabled)
371 meson_clk_pll_disable(hw);
373 meson_parm_write(clk->map, &pll->n, n);
374 meson_parm_write(clk->map, &pll->m, m);
376 if (MESON_PARM_APPLICABLE(&pll->frac)) {
377 frac = __pll_params_with_frac(rate, parent_rate, m, n, pll);
378 meson_parm_write(clk->map, &pll->frac, frac);
381 /* If the pll is stopped, bail out now */
382 if (!enabled)
383 return 0;
385 if (meson_clk_pll_enable(hw)) {
386 pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
387 __func__, old_rate);
389 * FIXME: Do we really need/want this HACK ?
390 * It looks unsafe. what happens if the clock gets into a
391 * broken state and we can't lock back on the old_rate ? Looks
392 * like an infinite recursion is possible
394 meson_clk_pll_set_rate(hw, old_rate, parent_rate);
397 return 0;
401 * The Meson G12A PCIE PLL is fined tuned to deliver a very precise
402 * 100MHz reference clock for the PCIe Analog PHY, and thus requires
403 * a strict register sequence to enable the PLL.
404 * To simplify, re-use the _init() op to enable the PLL and keep
405 * the other ops except set_rate since the rate is fixed.
407 const struct clk_ops meson_clk_pcie_pll_ops = {
408 .recalc_rate = meson_clk_pll_recalc_rate,
409 .round_rate = meson_clk_pll_round_rate,
410 .is_enabled = meson_clk_pll_is_enabled,
411 .enable = meson_clk_pcie_pll_enable,
412 .disable = meson_clk_pll_disable
414 EXPORT_SYMBOL_GPL(meson_clk_pcie_pll_ops);
416 const struct clk_ops meson_clk_pll_ops = {
417 .init = meson_clk_pll_init,
418 .recalc_rate = meson_clk_pll_recalc_rate,
419 .round_rate = meson_clk_pll_round_rate,
420 .set_rate = meson_clk_pll_set_rate,
421 .is_enabled = meson_clk_pll_is_enabled,
422 .enable = meson_clk_pll_enable,
423 .disable = meson_clk_pll_disable
425 EXPORT_SYMBOL_GPL(meson_clk_pll_ops);
427 const struct clk_ops meson_clk_pll_ro_ops = {
428 .recalc_rate = meson_clk_pll_recalc_rate,
429 .is_enabled = meson_clk_pll_is_enabled,
431 EXPORT_SYMBOL_GPL(meson_clk_pll_ro_ops);
433 MODULE_DESCRIPTION("Amlogic PLL driver");
434 MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
435 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
436 MODULE_LICENSE("GPL v2");