sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / clk / st / clkgen-fsyn.c
blob14819d919df10d8871d8d6cb24700d06bbdb1671
1 /*
2 * Copyright (C) 2014 STMicroelectronics R&D Ltd
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 */
11 * Authors:
12 * Stephen Gallimore <stephen.gallimore@st.com>,
13 * Pankaj Dev <pankaj.dev@st.com>.
16 #include <linux/slab.h>
17 #include <linux/of_address.h>
18 #include <linux/clk.h>
19 #include <linux/clk-provider.h>
21 #include "clkgen.h"
24 * Maximum input clock to the PLL before we divide it down by 2
25 * although in reality in actual systems this has never been seen to
26 * be used.
28 #define QUADFS_NDIV_THRESHOLD 30000000
30 #define PLL_BW_GOODREF (0L)
31 #define PLL_BW_VBADREF (1L)
32 #define PLL_BW_BADREF (2L)
33 #define PLL_BW_VGOODREF (3L)
35 #define QUADFS_MAX_CHAN 4
37 struct stm_fs {
38 unsigned long ndiv;
39 unsigned long mdiv;
40 unsigned long pe;
41 unsigned long sdiv;
42 unsigned long nsdiv;
45 struct clkgen_quadfs_data {
46 bool reset_present;
47 bool bwfilter_present;
48 bool lockstatus_present;
49 bool powerup_polarity;
50 bool standby_polarity;
51 bool nsdiv_present;
52 bool nrst_present;
53 struct clkgen_field ndiv;
54 struct clkgen_field ref_bw;
55 struct clkgen_field nreset;
56 struct clkgen_field npda;
57 struct clkgen_field lock_status;
59 struct clkgen_field nrst[QUADFS_MAX_CHAN];
60 struct clkgen_field nsb[QUADFS_MAX_CHAN];
61 struct clkgen_field en[QUADFS_MAX_CHAN];
62 struct clkgen_field mdiv[QUADFS_MAX_CHAN];
63 struct clkgen_field pe[QUADFS_MAX_CHAN];
64 struct clkgen_field sdiv[QUADFS_MAX_CHAN];
65 struct clkgen_field nsdiv[QUADFS_MAX_CHAN];
67 const struct clk_ops *pll_ops;
68 int (*get_params)(unsigned long, unsigned long, struct stm_fs *);
69 int (*get_rate)(unsigned long , const struct stm_fs *,
70 unsigned long *);
73 static const struct clk_ops st_quadfs_pll_c32_ops;
74 static const struct clk_ops st_quadfs_fs660c32_ops;
76 static int clk_fs660c32_dig_get_params(unsigned long input,
77 unsigned long output, struct stm_fs *fs);
78 static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs *,
79 unsigned long *);
81 static const struct clkgen_quadfs_data st_fs660c32_C = {
82 .nrst_present = true,
83 .nrst = { CLKGEN_FIELD(0x2f0, 0x1, 0),
84 CLKGEN_FIELD(0x2f0, 0x1, 1),
85 CLKGEN_FIELD(0x2f0, 0x1, 2),
86 CLKGEN_FIELD(0x2f0, 0x1, 3) },
87 .npda = CLKGEN_FIELD(0x2f0, 0x1, 12),
88 .nsb = { CLKGEN_FIELD(0x2f0, 0x1, 8),
89 CLKGEN_FIELD(0x2f0, 0x1, 9),
90 CLKGEN_FIELD(0x2f0, 0x1, 10),
91 CLKGEN_FIELD(0x2f0, 0x1, 11) },
92 .nsdiv_present = true,
93 .nsdiv = { CLKGEN_FIELD(0x304, 0x1, 24),
94 CLKGEN_FIELD(0x308, 0x1, 24),
95 CLKGEN_FIELD(0x30c, 0x1, 24),
96 CLKGEN_FIELD(0x310, 0x1, 24) },
97 .mdiv = { CLKGEN_FIELD(0x304, 0x1f, 15),
98 CLKGEN_FIELD(0x308, 0x1f, 15),
99 CLKGEN_FIELD(0x30c, 0x1f, 15),
100 CLKGEN_FIELD(0x310, 0x1f, 15) },
101 .en = { CLKGEN_FIELD(0x2fc, 0x1, 0),
102 CLKGEN_FIELD(0x2fc, 0x1, 1),
103 CLKGEN_FIELD(0x2fc, 0x1, 2),
104 CLKGEN_FIELD(0x2fc, 0x1, 3) },
105 .ndiv = CLKGEN_FIELD(0x2f4, 0x7, 16),
106 .pe = { CLKGEN_FIELD(0x304, 0x7fff, 0),
107 CLKGEN_FIELD(0x308, 0x7fff, 0),
108 CLKGEN_FIELD(0x30c, 0x7fff, 0),
109 CLKGEN_FIELD(0x310, 0x7fff, 0) },
110 .sdiv = { CLKGEN_FIELD(0x304, 0xf, 20),
111 CLKGEN_FIELD(0x308, 0xf, 20),
112 CLKGEN_FIELD(0x30c, 0xf, 20),
113 CLKGEN_FIELD(0x310, 0xf, 20) },
114 .lockstatus_present = true,
115 .lock_status = CLKGEN_FIELD(0x2f0, 0x1, 24),
116 .powerup_polarity = 1,
117 .standby_polarity = 1,
118 .pll_ops = &st_quadfs_pll_c32_ops,
119 .get_params = clk_fs660c32_dig_get_params,
120 .get_rate = clk_fs660c32_dig_get_rate,
123 static const struct clkgen_quadfs_data st_fs660c32_D = {
124 .nrst_present = true,
125 .nrst = { CLKGEN_FIELD(0x2a0, 0x1, 0),
126 CLKGEN_FIELD(0x2a0, 0x1, 1),
127 CLKGEN_FIELD(0x2a0, 0x1, 2),
128 CLKGEN_FIELD(0x2a0, 0x1, 3) },
129 .ndiv = CLKGEN_FIELD(0x2a4, 0x7, 16),
130 .pe = { CLKGEN_FIELD(0x2b4, 0x7fff, 0),
131 CLKGEN_FIELD(0x2b8, 0x7fff, 0),
132 CLKGEN_FIELD(0x2bc, 0x7fff, 0),
133 CLKGEN_FIELD(0x2c0, 0x7fff, 0) },
134 .sdiv = { CLKGEN_FIELD(0x2b4, 0xf, 20),
135 CLKGEN_FIELD(0x2b8, 0xf, 20),
136 CLKGEN_FIELD(0x2bc, 0xf, 20),
137 CLKGEN_FIELD(0x2c0, 0xf, 20) },
138 .npda = CLKGEN_FIELD(0x2a0, 0x1, 12),
139 .nsb = { CLKGEN_FIELD(0x2a0, 0x1, 8),
140 CLKGEN_FIELD(0x2a0, 0x1, 9),
141 CLKGEN_FIELD(0x2a0, 0x1, 10),
142 CLKGEN_FIELD(0x2a0, 0x1, 11) },
143 .nsdiv_present = true,
144 .nsdiv = { CLKGEN_FIELD(0x2b4, 0x1, 24),
145 CLKGEN_FIELD(0x2b8, 0x1, 24),
146 CLKGEN_FIELD(0x2bc, 0x1, 24),
147 CLKGEN_FIELD(0x2c0, 0x1, 24) },
148 .mdiv = { CLKGEN_FIELD(0x2b4, 0x1f, 15),
149 CLKGEN_FIELD(0x2b8, 0x1f, 15),
150 CLKGEN_FIELD(0x2bc, 0x1f, 15),
151 CLKGEN_FIELD(0x2c0, 0x1f, 15) },
152 .en = { CLKGEN_FIELD(0x2ac, 0x1, 0),
153 CLKGEN_FIELD(0x2ac, 0x1, 1),
154 CLKGEN_FIELD(0x2ac, 0x1, 2),
155 CLKGEN_FIELD(0x2ac, 0x1, 3) },
156 .lockstatus_present = true,
157 .lock_status = CLKGEN_FIELD(0x2A0, 0x1, 24),
158 .powerup_polarity = 1,
159 .standby_polarity = 1,
160 .pll_ops = &st_quadfs_pll_c32_ops,
161 .get_params = clk_fs660c32_dig_get_params,
162 .get_rate = clk_fs660c32_dig_get_rate,};
165 * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
167 * Traits of this clock:
168 * prepare - clk_(un)prepare only ensures parent is (un)prepared
169 * enable - clk_enable and clk_disable are functional & control the Fsyn
170 * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
171 * parent - fixed parent. No clk_set_parent support
175 * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
176 * its parent clock, found inside a type of
177 * ST quad channel frequency synthesizer block
179 * @hw: handle between common and hardware-specific interfaces.
180 * @ndiv: regmap field for the ndiv control.
181 * @regs_base: base address of the configuration registers.
182 * @lock: spinlock.
185 struct st_clk_quadfs_pll {
186 struct clk_hw hw;
187 void __iomem *regs_base;
188 spinlock_t *lock;
189 struct clkgen_quadfs_data *data;
190 u32 ndiv;
193 #define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
195 static int quadfs_pll_enable(struct clk_hw *hw)
197 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
198 unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10);
200 if (pll->lock)
201 spin_lock_irqsave(pll->lock, flags);
204 * Bring block out of reset if we have reset control.
206 if (pll->data->reset_present)
207 CLKGEN_WRITE(pll, nreset, 1);
210 * Use a fixed input clock noise bandwidth filter for the moment
212 if (pll->data->bwfilter_present)
213 CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF);
216 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
219 * Power up the PLL
221 CLKGEN_WRITE(pll, npda, !pll->data->powerup_polarity);
223 if (pll->lock)
224 spin_unlock_irqrestore(pll->lock, flags);
226 if (pll->data->lockstatus_present)
227 while (!CLKGEN_READ(pll, lock_status)) {
228 if (time_after(jiffies, timeout))
229 return -ETIMEDOUT;
230 cpu_relax();
233 return 0;
236 static void quadfs_pll_disable(struct clk_hw *hw)
238 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
239 unsigned long flags = 0;
241 if (pll->lock)
242 spin_lock_irqsave(pll->lock, flags);
245 * Powerdown the PLL and then put block into soft reset if we have
246 * reset control.
248 CLKGEN_WRITE(pll, npda, pll->data->powerup_polarity);
250 if (pll->data->reset_present)
251 CLKGEN_WRITE(pll, nreset, 0);
253 if (pll->lock)
254 spin_unlock_irqrestore(pll->lock, flags);
257 static int quadfs_pll_is_enabled(struct clk_hw *hw)
259 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
260 u32 npda = CLKGEN_READ(pll, npda);
262 return pll->data->powerup_polarity ? !npda : !!npda;
265 static int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs,
266 unsigned long *rate)
268 unsigned long nd = fs->ndiv + 16; /* ndiv value */
270 *rate = input * nd;
272 return 0;
275 static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw,
276 unsigned long parent_rate)
278 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
279 unsigned long rate = 0;
280 struct stm_fs params;
282 params.ndiv = CLKGEN_READ(pll, ndiv);
283 if (clk_fs660c32_vco_get_rate(parent_rate, &params, &rate))
284 pr_err("%s:%s error calculating rate\n",
285 clk_hw_get_name(hw), __func__);
287 pll->ndiv = params.ndiv;
289 return rate;
292 static int clk_fs660c32_vco_get_params(unsigned long input,
293 unsigned long output, struct stm_fs *fs)
295 /* Formula
296 VCO frequency = (fin x ndiv) / pdiv
297 ndiv = VCOfreq * pdiv / fin
299 unsigned long pdiv = 1, n;
301 /* Output clock range: 384Mhz to 660Mhz */
302 if (output < 384000000 || output > 660000000)
303 return -EINVAL;
305 if (input > 40000000)
306 /* This means that PDIV would be 2 instead of 1.
307 Not supported today. */
308 return -EINVAL;
310 input /= 1000;
311 output /= 1000;
313 n = output * pdiv / input;
314 if (n < 16)
315 n = 16;
316 fs->ndiv = n - 16; /* Converting formula value to reg value */
318 return 0;
321 static long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw,
322 unsigned long rate,
323 unsigned long *prate)
325 struct stm_fs params;
327 if (clk_fs660c32_vco_get_params(*prate, rate, &params))
328 return rate;
330 clk_fs660c32_vco_get_rate(*prate, &params, &rate);
332 pr_debug("%s: %s new rate %ld [ndiv=%u]\n",
333 __func__, clk_hw_get_name(hw),
334 rate, (unsigned int)params.ndiv);
336 return rate;
339 static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
340 unsigned long parent_rate)
342 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
343 struct stm_fs params;
344 long hwrate = 0;
345 unsigned long flags = 0;
346 int ret;
348 if (!rate || !parent_rate)
349 return -EINVAL;
351 ret = clk_fs660c32_vco_get_params(parent_rate, rate, &params);
352 if (ret)
353 return ret;
355 clk_fs660c32_vco_get_rate(parent_rate, &params, &hwrate);
357 pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
358 __func__, clk_hw_get_name(hw),
359 hwrate, (unsigned int)params.ndiv);
361 if (!hwrate)
362 return -EINVAL;
364 pll->ndiv = params.ndiv;
366 if (pll->lock)
367 spin_lock_irqsave(pll->lock, flags);
369 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
371 if (pll->lock)
372 spin_unlock_irqrestore(pll->lock, flags);
374 return 0;
377 static const struct clk_ops st_quadfs_pll_c32_ops = {
378 .enable = quadfs_pll_enable,
379 .disable = quadfs_pll_disable,
380 .is_enabled = quadfs_pll_is_enabled,
381 .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
382 .round_rate = quadfs_pll_fs660c32_round_rate,
383 .set_rate = quadfs_pll_fs660c32_set_rate,
386 static struct clk * __init st_clk_register_quadfs_pll(
387 const char *name, const char *parent_name,
388 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
389 spinlock_t *lock)
391 struct st_clk_quadfs_pll *pll;
392 struct clk *clk;
393 struct clk_init_data init;
396 * Sanity check required pointers.
398 if (WARN_ON(!name || !parent_name))
399 return ERR_PTR(-EINVAL);
401 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
402 if (!pll)
403 return ERR_PTR(-ENOMEM);
405 init.name = name;
406 init.ops = quadfs->pll_ops;
407 init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE;
408 init.parent_names = &parent_name;
409 init.num_parents = 1;
411 pll->data = quadfs;
412 pll->regs_base = reg;
413 pll->lock = lock;
414 pll->hw.init = &init;
416 clk = clk_register(NULL, &pll->hw);
418 if (IS_ERR(clk))
419 kfree(pll);
421 return clk;
425 * DOC: A digital frequency synthesizer
427 * Traits of this clock:
428 * prepare - clk_(un)prepare only ensures parent is (un)prepared
429 * enable - clk_enable and clk_disable are functional
430 * rate - set rate is functional
431 * parent - fixed parent. No clk_set_parent support
435 * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
436 * frequency synthesizer (fsynth) block.
438 * @hw: handle between common and hardware-specific interfaces
440 * @nsb: regmap field in the output control register for the digital
441 * standby of this fsynth channel. This control is active low so
442 * the channel is in standby when the control bit is cleared.
444 * @nsdiv: regmap field in the output control register for
445 * for the optional divide by 3 of this fsynth channel. This control
446 * is active low so the divide by 3 is active when the control bit is
447 * cleared and the divide is bypassed when the bit is set.
449 struct st_clk_quadfs_fsynth {
450 struct clk_hw hw;
451 void __iomem *regs_base;
452 spinlock_t *lock;
453 struct clkgen_quadfs_data *data;
455 u32 chan;
457 * Cached hardware values from set_rate so we can program the
458 * hardware in enable. There are two reasons for this:
460 * 1. The registers may not be writable until the parent has been
461 * enabled.
463 * 2. It restores the clock rate when a driver does an enable
464 * on PM restore, after a suspend to RAM has lost the hardware
465 * setup.
467 u32 md;
468 u32 pe;
469 u32 sdiv;
470 u32 nsdiv;
473 #define to_quadfs_fsynth(_hw) \
474 container_of(_hw, struct st_clk_quadfs_fsynth, hw)
476 static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
479 * Pulse the program enable register lsb to make the hardware take
480 * notice of the new md/pe values with a glitchless transition.
482 CLKGEN_WRITE(fs, en[fs->chan], 1);
483 CLKGEN_WRITE(fs, en[fs->chan], 0);
486 static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
488 unsigned long flags = 0;
491 * Ensure the md/pe parameters are ignored while we are
492 * reprogramming them so we can get a glitchless change
493 * when fine tuning the speed of a running clock.
495 CLKGEN_WRITE(fs, en[fs->chan], 0);
497 CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
498 CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
499 CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
501 if (fs->lock)
502 spin_lock_irqsave(fs->lock, flags);
504 if (fs->data->nsdiv_present)
505 CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
507 if (fs->lock)
508 spin_unlock_irqrestore(fs->lock, flags);
511 static int quadfs_fsynth_enable(struct clk_hw *hw)
513 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
514 unsigned long flags = 0;
516 pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
518 quadfs_fsynth_program_rate(fs);
520 if (fs->lock)
521 spin_lock_irqsave(fs->lock, flags);
523 CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
525 if (fs->data->nrst_present)
526 CLKGEN_WRITE(fs, nrst[fs->chan], 0);
528 if (fs->lock)
529 spin_unlock_irqrestore(fs->lock, flags);
531 quadfs_fsynth_program_enable(fs);
533 return 0;
536 static void quadfs_fsynth_disable(struct clk_hw *hw)
538 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
539 unsigned long flags = 0;
541 pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
543 if (fs->lock)
544 spin_lock_irqsave(fs->lock, flags);
546 CLKGEN_WRITE(fs, nsb[fs->chan], fs->data->standby_polarity);
548 if (fs->lock)
549 spin_unlock_irqrestore(fs->lock, flags);
552 static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
554 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
555 u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
557 pr_debug("%s: %s enable bit = 0x%x\n",
558 __func__, clk_hw_get_name(hw), nsb);
560 return fs->data->standby_polarity ? !nsb : !!nsb;
563 #define P20 (uint64_t)(1 << 20)
565 static int clk_fs660c32_dig_get_rate(unsigned long input,
566 const struct stm_fs *fs, unsigned long *rate)
568 unsigned long s = (1 << fs->sdiv);
569 unsigned long ns;
570 uint64_t res;
573 * 'nsdiv' is a register value ('BIN') which is translated
574 * to a decimal value according to following rules.
576 * nsdiv ns.dec
577 * 0 3
578 * 1 1
580 ns = (fs->nsdiv == 1) ? 1 : 3;
582 res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
583 *rate = (unsigned long)div64_u64(input * P20 * 32, res);
585 return 0;
589 static int clk_fs660c32_get_pe(int m, int si, unsigned long *deviation,
590 signed long input, unsigned long output, uint64_t *p,
591 struct stm_fs *fs)
593 unsigned long new_freq, new_deviation;
594 struct stm_fs fs_tmp;
595 uint64_t val;
597 val = (uint64_t)output << si;
599 *p = (uint64_t)input * P20 - (32LL + (uint64_t)m) * val * (P20 / 32LL);
601 *p = div64_u64(*p, val);
603 if (*p > 32767LL)
604 return 1;
606 fs_tmp.mdiv = (unsigned long) m;
607 fs_tmp.pe = (unsigned long)*p;
608 fs_tmp.sdiv = si;
609 fs_tmp.nsdiv = 1;
611 clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
613 new_deviation = abs(output - new_freq);
615 if (new_deviation < *deviation) {
616 fs->mdiv = m;
617 fs->pe = (unsigned long)*p;
618 fs->sdiv = si;
619 fs->nsdiv = 1;
620 *deviation = new_deviation;
622 return 0;
625 static int clk_fs660c32_dig_get_params(unsigned long input,
626 unsigned long output, struct stm_fs *fs)
628 int si; /* sdiv_reg (8 downto 0) */
629 int m; /* md value */
630 unsigned long new_freq, new_deviation;
631 /* initial condition to say: "infinite deviation" */
632 unsigned long deviation = ~0;
633 uint64_t p, p1, p2; /* pe value */
634 int r1, r2;
636 struct stm_fs fs_tmp;
638 for (si = 0; (si <= 8) && deviation; si++) {
640 /* Boundary test to avoid useless iteration */
641 r1 = clk_fs660c32_get_pe(0, si, &deviation,
642 input, output, &p1, fs);
643 r2 = clk_fs660c32_get_pe(31, si, &deviation,
644 input, output, &p2, fs);
646 /* No solution */
647 if (r1 && r2 && (p1 > p2))
648 continue;
650 /* Try to find best deviation */
651 for (m = 1; (m < 31) && deviation; m++)
652 clk_fs660c32_get_pe(m, si, &deviation,
653 input, output, &p, fs);
657 if (deviation == ~0) /* No solution found */
658 return -1;
660 /* pe fine tuning if deviation not 0: +/- 2 around computed pe value */
661 if (deviation) {
662 fs_tmp.mdiv = fs->mdiv;
663 fs_tmp.sdiv = fs->sdiv;
664 fs_tmp.nsdiv = fs->nsdiv;
666 if (fs->pe > 2)
667 p2 = fs->pe - 2;
668 else
669 p2 = 0;
671 for (; p2 < 32768ll && (p2 <= (fs->pe + 2)); p2++) {
672 fs_tmp.pe = (unsigned long)p2;
674 clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
676 new_deviation = abs(output - new_freq);
678 /* Check if this is a better solution */
679 if (new_deviation < deviation) {
680 fs->pe = (unsigned long)p2;
681 deviation = new_deviation;
686 return 0;
689 static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
690 struct stm_fs *params)
693 * Get the initial hardware values for recalc_rate
695 params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
696 params->pe = CLKGEN_READ(fs, pe[fs->chan]);
697 params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
699 if (fs->data->nsdiv_present)
700 params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
701 else
702 params->nsdiv = 1;
705 * If All are NULL then assume no clock rate is programmed.
707 if (!params->mdiv && !params->pe && !params->sdiv)
708 return 1;
710 fs->md = params->mdiv;
711 fs->pe = params->pe;
712 fs->sdiv = params->sdiv;
713 fs->nsdiv = params->nsdiv;
715 return 0;
718 static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
719 unsigned long prate, struct stm_fs *params)
721 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
722 int (*clk_fs_get_rate)(unsigned long ,
723 const struct stm_fs *, unsigned long *);
724 int (*clk_fs_get_params)(unsigned long, unsigned long, struct stm_fs *);
725 unsigned long rate = 0;
727 clk_fs_get_rate = fs->data->get_rate;
728 clk_fs_get_params = fs->data->get_params;
730 if (!clk_fs_get_params(prate, drate, params))
731 clk_fs_get_rate(prate, params, &rate);
733 return rate;
736 static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
737 unsigned long parent_rate)
739 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
740 unsigned long rate = 0;
741 struct stm_fs params;
742 int (*clk_fs_get_rate)(unsigned long ,
743 const struct stm_fs *, unsigned long *);
745 clk_fs_get_rate = fs->data->get_rate;
747 if (quadfs_fsynt_get_hw_value_for_recalc(fs, &params))
748 return 0;
750 if (clk_fs_get_rate(parent_rate, &params, &rate)) {
751 pr_err("%s:%s error calculating rate\n",
752 clk_hw_get_name(hw), __func__);
755 pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
757 return rate;
760 static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate,
761 unsigned long *prate)
763 struct stm_fs params;
765 rate = quadfs_find_best_rate(hw, rate, *prate, &params);
767 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
768 __func__, clk_hw_get_name(hw),
769 rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv,
770 (unsigned int)params.pe, (unsigned int)params.nsdiv);
772 return rate;
776 static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
777 struct stm_fs *params)
779 fs->md = params->mdiv;
780 fs->pe = params->pe;
781 fs->sdiv = params->sdiv;
782 fs->nsdiv = params->nsdiv;
785 * In some integrations you can only change the fsynth programming when
786 * the parent entity containing it is enabled.
788 quadfs_fsynth_program_rate(fs);
789 quadfs_fsynth_program_enable(fs);
792 static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
793 unsigned long parent_rate)
795 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
796 struct stm_fs params;
797 long hwrate;
798 int uninitialized_var(i);
800 if (!rate || !parent_rate)
801 return -EINVAL;
803 memset(&params, 0, sizeof(struct stm_fs));
805 hwrate = quadfs_find_best_rate(hw, rate, parent_rate, &params);
806 if (!hwrate)
807 return -EINVAL;
809 quadfs_program_and_enable(fs, &params);
811 return 0;
816 static const struct clk_ops st_quadfs_ops = {
817 .enable = quadfs_fsynth_enable,
818 .disable = quadfs_fsynth_disable,
819 .is_enabled = quadfs_fsynth_is_enabled,
820 .round_rate = quadfs_round_rate,
821 .set_rate = quadfs_set_rate,
822 .recalc_rate = quadfs_recalc_rate,
825 static struct clk * __init st_clk_register_quadfs_fsynth(
826 const char *name, const char *parent_name,
827 struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
828 unsigned long flags, spinlock_t *lock)
830 struct st_clk_quadfs_fsynth *fs;
831 struct clk *clk;
832 struct clk_init_data init;
835 * Sanity check required pointers, note that nsdiv3 is optional.
837 if (WARN_ON(!name || !parent_name))
838 return ERR_PTR(-EINVAL);
840 fs = kzalloc(sizeof(*fs), GFP_KERNEL);
841 if (!fs)
842 return ERR_PTR(-ENOMEM);
844 init.name = name;
845 init.ops = &st_quadfs_ops;
846 init.flags = flags | CLK_GET_RATE_NOCACHE | CLK_IS_BASIC;
847 init.parent_names = &parent_name;
848 init.num_parents = 1;
850 fs->data = quadfs;
851 fs->regs_base = reg;
852 fs->chan = chan;
853 fs->lock = lock;
854 fs->hw.init = &init;
856 clk = clk_register(NULL, &fs->hw);
858 if (IS_ERR(clk))
859 kfree(fs);
861 return clk;
864 static void __init st_of_create_quadfs_fsynths(
865 struct device_node *np, const char *pll_name,
866 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
867 spinlock_t *lock)
869 struct clk_onecell_data *clk_data;
870 int fschan;
872 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
873 if (!clk_data)
874 return;
876 clk_data->clk_num = QUADFS_MAX_CHAN;
877 clk_data->clks = kzalloc(QUADFS_MAX_CHAN * sizeof(struct clk *),
878 GFP_KERNEL);
880 if (!clk_data->clks) {
881 kfree(clk_data);
882 return;
885 for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) {
886 struct clk *clk;
887 const char *clk_name;
888 unsigned long flags = 0;
890 if (of_property_read_string_index(np, "clock-output-names",
891 fschan, &clk_name)) {
892 break;
896 * If we read an empty clock name then the channel is unused
898 if (*clk_name == '\0')
899 continue;
901 of_clk_detect_critical(np, fschan, &flags);
903 clk = st_clk_register_quadfs_fsynth(clk_name, pll_name,
904 quadfs, reg, fschan,
905 flags, lock);
908 * If there was an error registering this clock output, clean
909 * up and move on to the next one.
911 if (!IS_ERR(clk)) {
912 clk_data->clks[fschan] = clk;
913 pr_debug("%s: parent %s rate %u\n",
914 __clk_get_name(clk),
915 __clk_get_name(clk_get_parent(clk)),
916 (unsigned int)clk_get_rate(clk));
920 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
923 static void __init st_of_quadfs_setup(struct device_node *np,
924 struct clkgen_quadfs_data *data)
926 struct clk *clk;
927 const char *pll_name, *clk_parent_name;
928 void __iomem *reg;
929 spinlock_t *lock;
931 reg = of_iomap(np, 0);
932 if (!reg)
933 return;
935 clk_parent_name = of_clk_get_parent_name(np, 0);
936 if (!clk_parent_name)
937 return;
939 pll_name = kasprintf(GFP_KERNEL, "%s.pll", np->name);
940 if (!pll_name)
941 return;
943 lock = kzalloc(sizeof(*lock), GFP_KERNEL);
944 if (!lock)
945 goto err_exit;
947 spin_lock_init(lock);
949 clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name, data,
950 reg, lock);
951 if (IS_ERR(clk))
952 goto err_exit;
953 else
954 pr_debug("%s: parent %s rate %u\n",
955 __clk_get_name(clk),
956 __clk_get_name(clk_get_parent(clk)),
957 (unsigned int)clk_get_rate(clk));
959 st_of_create_quadfs_fsynths(np, pll_name, data, reg, lock);
961 err_exit:
962 kfree(pll_name); /* No longer need local copy of the PLL name */
965 static void __init st_of_quadfs660C_setup(struct device_node *np)
967 st_of_quadfs_setup(np, (struct clkgen_quadfs_data *) &st_fs660c32_C);
969 CLK_OF_DECLARE(quadfs660C, "st,quadfs-pll", st_of_quadfs660C_setup);
971 static void __init st_of_quadfs660D_setup(struct device_node *np)
973 st_of_quadfs_setup(np, (struct clkgen_quadfs_data *) &st_fs660c32_D);
975 CLK_OF_DECLARE(quadfs660D, "st,quadfs", st_of_quadfs660D_setup);