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.
16 #include <linux/slab.h>
17 #include <linux/of_address.h>
18 #include <linux/clk-provider.h>
23 * Maximum input clock to the PLL before we divide it down by 2
24 * although in reality in actual systems this has never been seen to
27 #define QUADFS_NDIV_THRESHOLD 30000000
29 #define PLL_BW_GOODREF (0L)
30 #define PLL_BW_VBADREF (1L)
31 #define PLL_BW_BADREF (2L)
32 #define PLL_BW_VGOODREF (3L)
34 #define QUADFS_MAX_CHAN 4
44 static struct stm_fs fs216c65_rtbl[] = {
45 { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 312.5 Khz */
46 { .mdiv = 0x17, .pe = 0x25ed, .sdiv = 0x1, .nsdiv = 0 }, /* 27 MHz */
47 { .mdiv = 0x1a, .pe = 0x7b36, .sdiv = 0x2, .nsdiv = 1 }, /* 36.87 MHz */
48 { .mdiv = 0x13, .pe = 0x0, .sdiv = 0x2, .nsdiv = 1 }, /* 48 MHz */
49 { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x1, .nsdiv = 1 }, /* 108 MHz */
52 static struct stm_fs fs432c65_rtbl[] = {
53 { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 625 Khz */
54 { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x2, .nsdiv = 1 }, /* 108 MHz */
55 { .mdiv = 0x19, .pe = 0x121a, .sdiv = 0x0, .nsdiv = 1 }, /* 297 MHz */
58 static struct stm_fs fs660c32_rtbl[] = {
59 { .mdiv = 0x01, .pe = 0x2aaa, .sdiv = 0x8, .nsdiv = 0 }, /* 600 KHz */
60 { .mdiv = 0x02, .pe = 0x3d33, .sdiv = 0x0, .nsdiv = 0 }, /* 148.5 Mhz */
61 { .mdiv = 0x13, .pe = 0x5bcc, .sdiv = 0x0, .nsdiv = 1 }, /* 297 Mhz */
62 { .mdiv = 0x0e, .pe = 0x1025, .sdiv = 0x0, .nsdiv = 1 }, /* 333 Mhz */
63 { .mdiv = 0x0b, .pe = 0x715f, .sdiv = 0x0, .nsdiv = 1 }, /* 350 Mhz */
66 struct clkgen_quadfs_data {
68 bool bwfilter_present;
69 bool lockstatus_present;
71 struct clkgen_field ndiv;
72 struct clkgen_field ref_bw;
73 struct clkgen_field nreset;
74 struct clkgen_field npda;
75 struct clkgen_field lock_status;
77 struct clkgen_field nsb[QUADFS_MAX_CHAN];
78 struct clkgen_field en[QUADFS_MAX_CHAN];
79 struct clkgen_field mdiv[QUADFS_MAX_CHAN];
80 struct clkgen_field pe[QUADFS_MAX_CHAN];
81 struct clkgen_field sdiv[QUADFS_MAX_CHAN];
82 struct clkgen_field nsdiv[QUADFS_MAX_CHAN];
84 const struct clk_ops *pll_ops;
87 int (*get_rate)(unsigned long , struct stm_fs *,
91 static const struct clk_ops st_quadfs_pll_c65_ops;
92 static const struct clk_ops st_quadfs_pll_c32_ops;
93 static const struct clk_ops st_quadfs_fs216c65_ops;
94 static const struct clk_ops st_quadfs_fs432c65_ops;
95 static const struct clk_ops st_quadfs_fs660c32_ops;
97 static int clk_fs216c65_get_rate(unsigned long, struct stm_fs *,
99 static int clk_fs432c65_get_rate(unsigned long, struct stm_fs *,
101 static int clk_fs660c32_dig_get_rate(unsigned long, struct stm_fs *,
104 * Values for all of the standalone instances of this clock
105 * generator found in STiH415 and STiH416 SYSCFG register banks. Note
106 * that the individual channel standby control bits (nsb) are in the
107 * first register along with the PLL control bits.
109 static struct clkgen_quadfs_data st_fs216c65_416 = {
111 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
112 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
113 CLKGEN_FIELD(0x0, 0x1, 11),
114 CLKGEN_FIELD(0x0, 0x1, 12),
115 CLKGEN_FIELD(0x0, 0x1, 13) },
116 .nsdiv_present = true,
117 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
118 CLKGEN_FIELD(0x0, 0x1, 19),
119 CLKGEN_FIELD(0x0, 0x1, 20),
120 CLKGEN_FIELD(0x0, 0x1, 21) },
121 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
122 CLKGEN_FIELD(0x14, 0x1f, 0),
123 CLKGEN_FIELD(0x24, 0x1f, 0),
124 CLKGEN_FIELD(0x34, 0x1f, 0) },
125 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
126 CLKGEN_FIELD(0x20, 0x1, 0),
127 CLKGEN_FIELD(0x30, 0x1, 0),
128 CLKGEN_FIELD(0x40, 0x1, 0) },
129 .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
130 .bwfilter_present = true,
131 .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
132 .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
133 CLKGEN_FIELD(0x18, 0xffff, 0),
134 CLKGEN_FIELD(0x28, 0xffff, 0),
135 CLKGEN_FIELD(0x38, 0xffff, 0) },
136 .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
137 CLKGEN_FIELD(0x1C, 0x7, 0),
138 CLKGEN_FIELD(0x2C, 0x7, 0),
139 CLKGEN_FIELD(0x3C, 0x7, 0) },
140 .pll_ops = &st_quadfs_pll_c65_ops,
141 .rtbl = fs216c65_rtbl,
142 .rtbl_cnt = ARRAY_SIZE(fs216c65_rtbl),
143 .get_rate = clk_fs216c65_get_rate,
146 static struct clkgen_quadfs_data st_fs432c65_416 = {
147 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
148 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
149 CLKGEN_FIELD(0x0, 0x1, 11),
150 CLKGEN_FIELD(0x0, 0x1, 12),
151 CLKGEN_FIELD(0x0, 0x1, 13) },
152 .nsdiv_present = true,
153 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
154 CLKGEN_FIELD(0x0, 0x1, 19),
155 CLKGEN_FIELD(0x0, 0x1, 20),
156 CLKGEN_FIELD(0x0, 0x1, 21) },
157 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
158 CLKGEN_FIELD(0x14, 0x1f, 0),
159 CLKGEN_FIELD(0x24, 0x1f, 0),
160 CLKGEN_FIELD(0x34, 0x1f, 0) },
161 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
162 CLKGEN_FIELD(0x20, 0x1, 0),
163 CLKGEN_FIELD(0x30, 0x1, 0),
164 CLKGEN_FIELD(0x40, 0x1, 0) },
165 .ndiv = CLKGEN_FIELD(0x0, 0x1, 15),
166 .bwfilter_present = true,
167 .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16),
168 .pe = { CLKGEN_FIELD(0x8, 0xffff, 0),
169 CLKGEN_FIELD(0x18, 0xffff, 0),
170 CLKGEN_FIELD(0x28, 0xffff, 0),
171 CLKGEN_FIELD(0x38, 0xffff, 0) },
172 .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0),
173 CLKGEN_FIELD(0x1C, 0x7, 0),
174 CLKGEN_FIELD(0x2C, 0x7, 0),
175 CLKGEN_FIELD(0x3C, 0x7, 0) },
176 .pll_ops = &st_quadfs_pll_c65_ops,
177 .rtbl = fs432c65_rtbl,
178 .rtbl_cnt = ARRAY_SIZE(fs432c65_rtbl),
179 .get_rate = clk_fs432c65_get_rate,
182 static struct clkgen_quadfs_data st_fs660c32_E_416 = {
183 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
184 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
185 CLKGEN_FIELD(0x0, 0x1, 11),
186 CLKGEN_FIELD(0x0, 0x1, 12),
187 CLKGEN_FIELD(0x0, 0x1, 13) },
188 .nsdiv_present = true,
189 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
190 CLKGEN_FIELD(0x0, 0x1, 19),
191 CLKGEN_FIELD(0x0, 0x1, 20),
192 CLKGEN_FIELD(0x0, 0x1, 21) },
193 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
194 CLKGEN_FIELD(0x14, 0x1f, 0),
195 CLKGEN_FIELD(0x24, 0x1f, 0),
196 CLKGEN_FIELD(0x34, 0x1f, 0) },
197 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
198 CLKGEN_FIELD(0x20, 0x1, 0),
199 CLKGEN_FIELD(0x30, 0x1, 0),
200 CLKGEN_FIELD(0x40, 0x1, 0) },
201 .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
202 .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
203 CLKGEN_FIELD(0x18, 0x7fff, 0),
204 CLKGEN_FIELD(0x28, 0x7fff, 0),
205 CLKGEN_FIELD(0x38, 0x7fff, 0) },
206 .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
207 CLKGEN_FIELD(0x1C, 0xf, 0),
208 CLKGEN_FIELD(0x2C, 0xf, 0),
209 CLKGEN_FIELD(0x3C, 0xf, 0) },
210 .lockstatus_present = true,
211 .lock_status = CLKGEN_FIELD(0xAC, 0x1, 0),
212 .pll_ops = &st_quadfs_pll_c32_ops,
213 .rtbl = fs660c32_rtbl,
214 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
215 .get_rate = clk_fs660c32_dig_get_rate,
218 static struct clkgen_quadfs_data st_fs660c32_F_416 = {
219 .npda = CLKGEN_FIELD(0x0, 0x1, 14),
220 .nsb = { CLKGEN_FIELD(0x0, 0x1, 10),
221 CLKGEN_FIELD(0x0, 0x1, 11),
222 CLKGEN_FIELD(0x0, 0x1, 12),
223 CLKGEN_FIELD(0x0, 0x1, 13) },
224 .nsdiv_present = true,
225 .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18),
226 CLKGEN_FIELD(0x0, 0x1, 19),
227 CLKGEN_FIELD(0x0, 0x1, 20),
228 CLKGEN_FIELD(0x0, 0x1, 21) },
229 .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0),
230 CLKGEN_FIELD(0x14, 0x1f, 0),
231 CLKGEN_FIELD(0x24, 0x1f, 0),
232 CLKGEN_FIELD(0x34, 0x1f, 0) },
233 .en = { CLKGEN_FIELD(0x10, 0x1, 0),
234 CLKGEN_FIELD(0x20, 0x1, 0),
235 CLKGEN_FIELD(0x30, 0x1, 0),
236 CLKGEN_FIELD(0x40, 0x1, 0) },
237 .ndiv = CLKGEN_FIELD(0x0, 0x7, 15),
238 .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0),
239 CLKGEN_FIELD(0x18, 0x7fff, 0),
240 CLKGEN_FIELD(0x28, 0x7fff, 0),
241 CLKGEN_FIELD(0x38, 0x7fff, 0) },
242 .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0),
243 CLKGEN_FIELD(0x1C, 0xf, 0),
244 CLKGEN_FIELD(0x2C, 0xf, 0),
245 CLKGEN_FIELD(0x3C, 0xf, 0) },
246 .lockstatus_present = true,
247 .lock_status = CLKGEN_FIELD(0xEC, 0x1, 0),
248 .pll_ops = &st_quadfs_pll_c32_ops,
249 .rtbl = fs660c32_rtbl,
250 .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl),
251 .get_rate = clk_fs660c32_dig_get_rate,
255 * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
257 * Traits of this clock:
258 * prepare - clk_(un)prepare only ensures parent is (un)prepared
259 * enable - clk_enable and clk_disable are functional & control the Fsyn
260 * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
261 * parent - fixed parent. No clk_set_parent support
265 * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
266 * its parent clock, found inside a type of
267 * ST quad channel frequency synthesizer block
269 * @hw: handle between common and hardware-specific interfaces.
270 * @ndiv: regmap field for the ndiv control.
271 * @regs_base: base address of the configuration registers.
275 struct st_clk_quadfs_pll {
277 void __iomem *regs_base;
279 struct clkgen_quadfs_data *data;
283 #define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
285 static int quadfs_pll_enable(struct clk_hw *hw)
287 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
288 unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10);
291 spin_lock_irqsave(pll->lock, flags);
294 * Bring block out of reset if we have reset control.
296 if (pll->data->reset_present)
297 CLKGEN_WRITE(pll, nreset, 1);
300 * Use a fixed input clock noise bandwidth filter for the moment
302 if (pll->data->bwfilter_present)
303 CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF);
306 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
311 CLKGEN_WRITE(pll, npda, 1);
314 spin_unlock_irqrestore(pll->lock, flags);
316 if (pll->data->lockstatus_present)
317 while (!CLKGEN_READ(pll, lock_status)) {
318 if (time_after(jiffies, timeout))
326 static void quadfs_pll_disable(struct clk_hw *hw)
328 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
329 unsigned long flags = 0;
332 spin_lock_irqsave(pll->lock, flags);
335 * Powerdown the PLL and then put block into soft reset if we have
338 CLKGEN_WRITE(pll, npda, 0);
340 if (pll->data->reset_present)
341 CLKGEN_WRITE(pll, nreset, 0);
344 spin_unlock_irqrestore(pll->lock, flags);
347 static int quadfs_pll_is_enabled(struct clk_hw *hw)
349 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
350 u32 npda = CLKGEN_READ(pll, npda);
355 int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs,
358 unsigned long nd = fs->ndiv + 16; /* ndiv value */
365 static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw,
366 unsigned long parent_rate)
368 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
369 unsigned long rate = 0;
370 struct stm_fs params;
372 params.ndiv = CLKGEN_READ(pll, ndiv);
373 if (clk_fs660c32_vco_get_rate(parent_rate, ¶ms, &rate))
374 pr_err("%s:%s error calculating rate\n",
375 __clk_get_name(hw->clk), __func__);
377 pll->ndiv = params.ndiv;
382 int clk_fs660c32_vco_get_params(unsigned long input,
383 unsigned long output, struct stm_fs *fs)
386 VCO frequency = (fin x ndiv) / pdiv
387 ndiv = VCOfreq * pdiv / fin
389 unsigned long pdiv = 1, n;
391 /* Output clock range: 384Mhz to 660Mhz */
392 if (output < 384000000 || output > 660000000)
395 if (input > 40000000)
396 /* This means that PDIV would be 2 instead of 1.
397 Not supported today. */
403 n = output * pdiv / input;
406 fs->ndiv = n - 16; /* Converting formula value to reg value */
411 static long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw, unsigned long rate
412 , unsigned long *prate)
414 struct stm_fs params;
416 if (!clk_fs660c32_vco_get_params(*prate, rate, ¶ms))
417 clk_fs660c32_vco_get_rate(*prate, ¶ms, &rate);
419 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
420 __func__, __clk_get_name(hw->clk),
421 rate, (unsigned int)params.sdiv,
422 (unsigned int)params.mdiv,
423 (unsigned int)params.pe, (unsigned int)params.nsdiv);
428 static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
429 unsigned long parent_rate)
431 struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
432 struct stm_fs params;
434 unsigned long flags = 0;
436 if (!rate || !parent_rate)
439 if (!clk_fs660c32_vco_get_params(parent_rate, rate, ¶ms))
440 clk_fs660c32_vco_get_rate(parent_rate, ¶ms, &hwrate);
442 pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
443 __func__, __clk_get_name(hw->clk),
444 hwrate, (unsigned int)params.ndiv);
449 pll->ndiv = params.ndiv;
452 spin_lock_irqsave(pll->lock, flags);
454 CLKGEN_WRITE(pll, ndiv, pll->ndiv);
457 spin_unlock_irqrestore(pll->lock, flags);
462 static const struct clk_ops st_quadfs_pll_c65_ops = {
463 .enable = quadfs_pll_enable,
464 .disable = quadfs_pll_disable,
465 .is_enabled = quadfs_pll_is_enabled,
468 static const struct clk_ops st_quadfs_pll_c32_ops = {
469 .enable = quadfs_pll_enable,
470 .disable = quadfs_pll_disable,
471 .is_enabled = quadfs_pll_is_enabled,
472 .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
473 .round_rate = quadfs_pll_fs660c32_round_rate,
474 .set_rate = quadfs_pll_fs660c32_set_rate,
477 static struct clk * __init st_clk_register_quadfs_pll(
478 const char *name, const char *parent_name,
479 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
482 struct st_clk_quadfs_pll *pll;
484 struct clk_init_data init;
487 * Sanity check required pointers.
489 if (WARN_ON(!name || !parent_name))
490 return ERR_PTR(-EINVAL);
492 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
494 return ERR_PTR(-ENOMEM);
497 init.ops = quadfs->pll_ops;
498 init.flags = CLK_IS_BASIC;
499 init.parent_names = &parent_name;
500 init.num_parents = 1;
503 pll->regs_base = reg;
505 pll->hw.init = &init;
507 clk = clk_register(NULL, &pll->hw);
516 * DOC: A digital frequency synthesizer
518 * Traits of this clock:
519 * prepare - clk_(un)prepare only ensures parent is (un)prepared
520 * enable - clk_enable and clk_disable are functional
521 * rate - set rate is functional
522 * parent - fixed parent. No clk_set_parent support
526 * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
527 * frequency synthesizer (fsynth) block.
529 * @hw: handle between common and hardware-specific interfaces
531 * @nsb: regmap field in the output control register for the digital
532 * standby of this fsynth channel. This control is active low so
533 * the channel is in standby when the control bit is cleared.
535 * @nsdiv: regmap field in the output control register for
536 * for the optional divide by 3 of this fsynth channel. This control
537 * is active low so the divide by 3 is active when the control bit is
538 * cleared and the divide is bypassed when the bit is set.
540 struct st_clk_quadfs_fsynth {
542 void __iomem *regs_base;
544 struct clkgen_quadfs_data *data;
548 * Cached hardware values from set_rate so we can program the
549 * hardware in enable. There are two reasons for this:
551 * 1. The registers may not be writable until the parent has been
554 * 2. It restores the clock rate when a driver does an enable
555 * on PM restore, after a suspend to RAM has lost the hardware
564 #define to_quadfs_fsynth(_hw) \
565 container_of(_hw, struct st_clk_quadfs_fsynth, hw)
567 static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
570 * Pulse the program enable register lsb to make the hardware take
571 * notice of the new md/pe values with a glitchless transition.
573 CLKGEN_WRITE(fs, en[fs->chan], 1);
574 CLKGEN_WRITE(fs, en[fs->chan], 0);
577 static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
579 unsigned long flags = 0;
582 * Ensure the md/pe parameters are ignored while we are
583 * reprogramming them so we can get a glitchless change
584 * when fine tuning the speed of a running clock.
586 CLKGEN_WRITE(fs, en[fs->chan], 0);
588 CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
589 CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
590 CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
593 spin_lock_irqsave(fs->lock, flags);
595 if (fs->data->nsdiv_present)
596 CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
599 spin_unlock_irqrestore(fs->lock, flags);
602 static int quadfs_fsynth_enable(struct clk_hw *hw)
604 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
605 unsigned long flags = 0;
607 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
609 quadfs_fsynth_program_rate(fs);
612 spin_lock_irqsave(fs->lock, flags);
614 CLKGEN_WRITE(fs, nsb[fs->chan], 1);
617 spin_unlock_irqrestore(fs->lock, flags);
619 quadfs_fsynth_program_enable(fs);
624 static void quadfs_fsynth_disable(struct clk_hw *hw)
626 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
627 unsigned long flags = 0;
629 pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk));
632 spin_lock_irqsave(fs->lock, flags);
634 CLKGEN_WRITE(fs, nsb[fs->chan], 0);
637 spin_unlock_irqrestore(fs->lock, flags);
640 static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
642 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
643 u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
645 pr_debug("%s: %s enable bit = 0x%x\n",
646 __func__, __clk_get_name(hw->clk), nsb);
651 #define P15 (uint64_t)(1 << 15)
653 static int clk_fs216c65_get_rate(unsigned long input, struct stm_fs *fs,
658 unsigned long nd = 8; /* ndiv stuck at 0 => val = 8 */
663 s = 1 << (fs->sdiv + 1);
664 ns = (fs->nsdiv ? 1 : 3);
666 res = (uint64_t)(s * ns * P15 * (uint64_t)(m + 33));
667 res = res - (s * ns * fs->pe);
668 *rate = div64_u64(P15 * nd * input * 32, res);
673 static int clk_fs432c65_get_rate(unsigned long input, struct stm_fs *fs,
677 unsigned long nd = 16; /* ndiv value; stuck at 0 (30Mhz input) */
683 sd = 1 << (fs->sdiv + 1);
684 ns = (fs->nsdiv ? 1 : 3);
686 res = (uint64_t)(sd * ns * P15 * (uint64_t)(m + 33));
687 res = res - (sd * ns * fs->pe);
688 *rate = div64_u64(P15 * nd * input * 32, res);
693 #define P20 (uint64_t)(1 << 20)
695 static int clk_fs660c32_dig_get_rate(unsigned long input,
696 struct stm_fs *fs, unsigned long *rate)
698 unsigned long s = (1 << fs->sdiv);
703 * 'nsdiv' is a register value ('BIN') which is translated
704 * to a decimal value according to following rules.
710 ns = (fs->nsdiv == 1) ? 1 : 3;
712 res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
713 *rate = (unsigned long)div64_u64(input * P20 * 32, res);
718 static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
719 struct stm_fs *params)
722 * Get the initial hardware values for recalc_rate
724 params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
725 params->pe = CLKGEN_READ(fs, pe[fs->chan]);
726 params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
728 if (fs->data->nsdiv_present)
729 params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
734 * If All are NULL then assume no clock rate is programmed.
736 if (!params->mdiv && !params->pe && !params->sdiv)
739 fs->md = params->mdiv;
741 fs->sdiv = params->sdiv;
742 fs->nsdiv = params->nsdiv;
747 static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
748 unsigned long prate, struct stm_fs *params)
750 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
751 int (*clk_fs_get_rate)(unsigned long ,
752 struct stm_fs *, unsigned long *);
753 struct stm_fs prev_params;
754 unsigned long prev_rate, rate = 0;
755 unsigned long diff_rate, prev_diff_rate = ~0;
758 clk_fs_get_rate = fs->data->get_rate;
760 for (index = 0; index < fs->data->rtbl_cnt; index++) {
763 *params = fs->data->rtbl[index];
764 prev_params = *params;
766 clk_fs_get_rate(prate, &fs->data->rtbl[index], &rate);
768 diff_rate = abs(drate - rate);
770 if (diff_rate > prev_diff_rate) {
772 *params = prev_params;
776 prev_diff_rate = diff_rate;
783 if (index == fs->data->rtbl_cnt)
784 *params = prev_params;
789 static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
790 unsigned long parent_rate)
792 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
793 unsigned long rate = 0;
794 struct stm_fs params;
795 int (*clk_fs_get_rate)(unsigned long ,
796 struct stm_fs *, unsigned long *);
798 clk_fs_get_rate = fs->data->get_rate;
800 if (quadfs_fsynt_get_hw_value_for_recalc(fs, ¶ms))
803 if (clk_fs_get_rate(parent_rate, ¶ms, &rate)) {
804 pr_err("%s:%s error calculating rate\n",
805 __clk_get_name(hw->clk), __func__);
808 pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate);
813 static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate,
814 unsigned long *prate)
816 struct stm_fs params;
818 rate = quadfs_find_best_rate(hw, rate, *prate, ¶ms);
820 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
821 __func__, __clk_get_name(hw->clk),
822 rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv,
823 (unsigned int)params.pe, (unsigned int)params.nsdiv);
829 static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
830 struct stm_fs *params)
832 fs->md = params->mdiv;
834 fs->sdiv = params->sdiv;
835 fs->nsdiv = params->nsdiv;
838 * In some integrations you can only change the fsynth programming when
839 * the parent entity containing it is enabled.
841 quadfs_fsynth_program_rate(fs);
842 quadfs_fsynth_program_enable(fs);
845 static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
846 unsigned long parent_rate)
848 struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
849 struct stm_fs params;
851 int uninitialized_var(i);
853 if (!rate || !parent_rate)
856 memset(¶ms, 0, sizeof(struct stm_fs));
858 hwrate = quadfs_find_best_rate(hw, rate, parent_rate, ¶ms);
862 quadfs_program_and_enable(fs, ¶ms);
869 static const struct clk_ops st_quadfs_ops = {
870 .enable = quadfs_fsynth_enable,
871 .disable = quadfs_fsynth_disable,
872 .is_enabled = quadfs_fsynth_is_enabled,
873 .round_rate = quadfs_round_rate,
874 .set_rate = quadfs_set_rate,
875 .recalc_rate = quadfs_recalc_rate,
878 static struct clk * __init st_clk_register_quadfs_fsynth(
879 const char *name, const char *parent_name,
880 struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
883 struct st_clk_quadfs_fsynth *fs;
885 struct clk_init_data init;
888 * Sanity check required pointers, note that nsdiv3 is optional.
890 if (WARN_ON(!name || !parent_name))
891 return ERR_PTR(-EINVAL);
893 fs = kzalloc(sizeof(*fs), GFP_KERNEL);
895 return ERR_PTR(-ENOMEM);
898 init.ops = &st_quadfs_ops;
899 init.flags = CLK_GET_RATE_NOCACHE | CLK_IS_BASIC;
900 init.parent_names = &parent_name;
901 init.num_parents = 1;
909 clk = clk_register(NULL, &fs->hw);
917 static struct of_device_id quadfs_of_match[] = {
919 .compatible = "st,stih416-quadfs216",
920 .data = (void *)&st_fs216c65_416
923 .compatible = "st,stih416-quadfs432",
924 .data = (void *)&st_fs432c65_416
927 .compatible = "st,stih416-quadfs660-E",
928 .data = (void *)&st_fs660c32_E_416
931 .compatible = "st,stih416-quadfs660-F",
932 .data = (void *)&st_fs660c32_F_416
937 static void __init st_of_create_quadfs_fsynths(
938 struct device_node *np, const char *pll_name,
939 struct clkgen_quadfs_data *quadfs, void __iomem *reg,
942 struct clk_onecell_data *clk_data;
945 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
949 clk_data->clk_num = QUADFS_MAX_CHAN;
950 clk_data->clks = kzalloc(QUADFS_MAX_CHAN * sizeof(struct clk *),
953 if (!clk_data->clks) {
958 for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) {
960 const char *clk_name;
962 if (of_property_read_string_index(np, "clock-output-names",
963 fschan, &clk_name)) {
968 * If we read an empty clock name then the channel is unused
970 if (*clk_name == '\0')
973 clk = st_clk_register_quadfs_fsynth(clk_name, pll_name,
974 quadfs, reg, fschan, lock);
977 * If there was an error registering this clock output, clean
978 * up and move on to the next one.
981 clk_data->clks[fschan] = clk;
982 pr_debug("%s: parent %s rate %u\n",
984 __clk_get_name(clk_get_parent(clk)),
985 (unsigned int)clk_get_rate(clk));
989 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
992 static void __init st_of_quadfs_setup(struct device_node *np)
994 const struct of_device_id *match;
996 const char *pll_name, *clk_parent_name;
1000 match = of_match_node(quadfs_of_match, np);
1001 if (WARN_ON(!match))
1004 reg = of_iomap(np, 0);
1008 clk_parent_name = of_clk_get_parent_name(np, 0);
1009 if (!clk_parent_name)
1012 pll_name = kasprintf(GFP_KERNEL, "%s.pll", np->name);
1016 lock = kzalloc(sizeof(*lock), GFP_KERNEL);
1020 spin_lock_init(lock);
1022 clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name,
1023 (struct clkgen_quadfs_data *) match->data, reg, lock);
1027 pr_debug("%s: parent %s rate %u\n",
1028 __clk_get_name(clk),
1029 __clk_get_name(clk_get_parent(clk)),
1030 (unsigned int)clk_get_rate(clk));
1032 st_of_create_quadfs_fsynths(np, pll_name,
1033 (struct clkgen_quadfs_data *)match->data,
1037 kfree(pll_name); /* No longer need local copy of the PLL name */
1039 CLK_OF_DECLARE(quadfs, "st,quadfs", st_of_quadfs_setup);