1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 STMicroelectronics R&D Ltd
8 * Stephen Gallimore <stephen.gallimore@st.com>,
9 * Pankaj Dev <pankaj.dev@st.com>.
12 #include <linux/slab.h>
13 #include <linux/of_address.h>
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
20 * Maximum input clock to the PLL before we divide it down by 2
21 * although in reality in actual systems this has never been seen to
24 #define QUADFS_NDIV_THRESHOLD 30000000
26 #define PLL_BW_GOODREF (0L)
27 #define PLL_BW_VBADREF (1L)
28 #define PLL_BW_BADREF (2L)
29 #define PLL_BW_VGOODREF (3L)
31 #define QUADFS_MAX_CHAN 4
41 struct clkgen_quadfs_data
{
43 bool bwfilter_present
;
44 bool lockstatus_present
;
45 bool powerup_polarity
;
46 bool standby_polarity
;
49 struct clkgen_field ndiv
;
50 struct clkgen_field ref_bw
;
51 struct clkgen_field nreset
;
52 struct clkgen_field npda
;
53 struct clkgen_field lock_status
;
55 struct clkgen_field nrst
[QUADFS_MAX_CHAN
];
56 struct clkgen_field nsb
[QUADFS_MAX_CHAN
];
57 struct clkgen_field en
[QUADFS_MAX_CHAN
];
58 struct clkgen_field mdiv
[QUADFS_MAX_CHAN
];
59 struct clkgen_field pe
[QUADFS_MAX_CHAN
];
60 struct clkgen_field sdiv
[QUADFS_MAX_CHAN
];
61 struct clkgen_field nsdiv
[QUADFS_MAX_CHAN
];
63 const struct clk_ops
*pll_ops
;
64 int (*get_params
)(unsigned long, unsigned long, struct stm_fs
*);
65 int (*get_rate
)(unsigned long , const struct stm_fs
*,
69 struct clkgen_clk_out
{
74 struct clkgen_quadfs_data_clks
{
75 struct clkgen_quadfs_data
*data
;
76 const struct clkgen_clk_out
*outputs
;
79 static const struct clk_ops st_quadfs_pll_c32_ops
;
81 static int clk_fs660c32_dig_get_params(unsigned long input
,
82 unsigned long output
, struct stm_fs
*fs
);
83 static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs
*,
86 static const struct clkgen_quadfs_data st_fs660c32_C
= {
88 .nrst
= { CLKGEN_FIELD(0x2f0, 0x1, 0),
89 CLKGEN_FIELD(0x2f0, 0x1, 1),
90 CLKGEN_FIELD(0x2f0, 0x1, 2),
91 CLKGEN_FIELD(0x2f0, 0x1, 3) },
92 .npda
= CLKGEN_FIELD(0x2f0, 0x1, 12),
93 .nsb
= { CLKGEN_FIELD(0x2f0, 0x1, 8),
94 CLKGEN_FIELD(0x2f0, 0x1, 9),
95 CLKGEN_FIELD(0x2f0, 0x1, 10),
96 CLKGEN_FIELD(0x2f0, 0x1, 11) },
97 .nsdiv_present
= true,
98 .nsdiv
= { CLKGEN_FIELD(0x304, 0x1, 24),
99 CLKGEN_FIELD(0x308, 0x1, 24),
100 CLKGEN_FIELD(0x30c, 0x1, 24),
101 CLKGEN_FIELD(0x310, 0x1, 24) },
102 .mdiv
= { CLKGEN_FIELD(0x304, 0x1f, 15),
103 CLKGEN_FIELD(0x308, 0x1f, 15),
104 CLKGEN_FIELD(0x30c, 0x1f, 15),
105 CLKGEN_FIELD(0x310, 0x1f, 15) },
106 .en
= { CLKGEN_FIELD(0x2fc, 0x1, 0),
107 CLKGEN_FIELD(0x2fc, 0x1, 1),
108 CLKGEN_FIELD(0x2fc, 0x1, 2),
109 CLKGEN_FIELD(0x2fc, 0x1, 3) },
110 .ndiv
= CLKGEN_FIELD(0x2f4, 0x7, 16),
111 .pe
= { CLKGEN_FIELD(0x304, 0x7fff, 0),
112 CLKGEN_FIELD(0x308, 0x7fff, 0),
113 CLKGEN_FIELD(0x30c, 0x7fff, 0),
114 CLKGEN_FIELD(0x310, 0x7fff, 0) },
115 .sdiv
= { CLKGEN_FIELD(0x304, 0xf, 20),
116 CLKGEN_FIELD(0x308, 0xf, 20),
117 CLKGEN_FIELD(0x30c, 0xf, 20),
118 CLKGEN_FIELD(0x310, 0xf, 20) },
119 .lockstatus_present
= true,
120 .lock_status
= CLKGEN_FIELD(0x2f0, 0x1, 24),
121 .powerup_polarity
= 1,
122 .standby_polarity
= 1,
123 .pll_ops
= &st_quadfs_pll_c32_ops
,
124 .get_params
= clk_fs660c32_dig_get_params
,
125 .get_rate
= clk_fs660c32_dig_get_rate
,
128 static const struct clkgen_clk_out st_fs660c32_C_clks
[] = {
129 { .name
= "clk-s-c0-fs0-ch0", },
130 { .name
= "clk-s-c0-fs0-ch1", },
131 { .name
= "clk-s-c0-fs0-ch2", },
132 { .name
= "clk-s-c0-fs0-ch3", },
135 static const struct clkgen_quadfs_data_clks st_fs660c32_C_data
= {
136 .data
= (struct clkgen_quadfs_data
*)&st_fs660c32_C
,
137 .outputs
= st_fs660c32_C_clks
,
140 static const struct clkgen_quadfs_data st_fs660c32_D
= {
141 .nrst_present
= true,
142 .nrst
= { CLKGEN_FIELD(0x2a0, 0x1, 0),
143 CLKGEN_FIELD(0x2a0, 0x1, 1),
144 CLKGEN_FIELD(0x2a0, 0x1, 2),
145 CLKGEN_FIELD(0x2a0, 0x1, 3) },
146 .ndiv
= CLKGEN_FIELD(0x2a4, 0x7, 16),
147 .pe
= { CLKGEN_FIELD(0x2b4, 0x7fff, 0),
148 CLKGEN_FIELD(0x2b8, 0x7fff, 0),
149 CLKGEN_FIELD(0x2bc, 0x7fff, 0),
150 CLKGEN_FIELD(0x2c0, 0x7fff, 0) },
151 .sdiv
= { CLKGEN_FIELD(0x2b4, 0xf, 20),
152 CLKGEN_FIELD(0x2b8, 0xf, 20),
153 CLKGEN_FIELD(0x2bc, 0xf, 20),
154 CLKGEN_FIELD(0x2c0, 0xf, 20) },
155 .npda
= CLKGEN_FIELD(0x2a0, 0x1, 12),
156 .nsb
= { CLKGEN_FIELD(0x2a0, 0x1, 8),
157 CLKGEN_FIELD(0x2a0, 0x1, 9),
158 CLKGEN_FIELD(0x2a0, 0x1, 10),
159 CLKGEN_FIELD(0x2a0, 0x1, 11) },
160 .nsdiv_present
= true,
161 .nsdiv
= { CLKGEN_FIELD(0x2b4, 0x1, 24),
162 CLKGEN_FIELD(0x2b8, 0x1, 24),
163 CLKGEN_FIELD(0x2bc, 0x1, 24),
164 CLKGEN_FIELD(0x2c0, 0x1, 24) },
165 .mdiv
= { CLKGEN_FIELD(0x2b4, 0x1f, 15),
166 CLKGEN_FIELD(0x2b8, 0x1f, 15),
167 CLKGEN_FIELD(0x2bc, 0x1f, 15),
168 CLKGEN_FIELD(0x2c0, 0x1f, 15) },
169 .en
= { CLKGEN_FIELD(0x2ac, 0x1, 0),
170 CLKGEN_FIELD(0x2ac, 0x1, 1),
171 CLKGEN_FIELD(0x2ac, 0x1, 2),
172 CLKGEN_FIELD(0x2ac, 0x1, 3) },
173 .lockstatus_present
= true,
174 .lock_status
= CLKGEN_FIELD(0x2A0, 0x1, 24),
175 .powerup_polarity
= 1,
176 .standby_polarity
= 1,
177 .pll_ops
= &st_quadfs_pll_c32_ops
,
178 .get_params
= clk_fs660c32_dig_get_params
,
179 .get_rate
= clk_fs660c32_dig_get_rate
,};
181 static const struct clkgen_quadfs_data_clks st_fs660c32_D_data
= {
182 .data
= (struct clkgen_quadfs_data
*)&st_fs660c32_D
,
185 static const struct clkgen_clk_out st_fs660c32_D0_clks
[] = {
186 { .name
= "clk-s-d0-fs0-ch0", },
187 { .name
= "clk-s-d0-fs0-ch1", },
188 { .name
= "clk-s-d0-fs0-ch2", },
189 { .name
= "clk-s-d0-fs0-ch3", },
192 static const struct clkgen_quadfs_data_clks st_fs660c32_D0_data
= {
193 .data
= (struct clkgen_quadfs_data
*)&st_fs660c32_D
,
194 .outputs
= st_fs660c32_D0_clks
,
197 static const struct clkgen_clk_out st_fs660c32_D2_clks
[] = {
198 { .name
= "clk-s-d2-fs0-ch0", },
199 { .name
= "clk-s-d2-fs0-ch1", },
200 { .name
= "clk-s-d2-fs0-ch2", },
201 { .name
= "clk-s-d2-fs0-ch3", },
204 static const struct clkgen_quadfs_data_clks st_fs660c32_D2_data
= {
205 .data
= (struct clkgen_quadfs_data
*)&st_fs660c32_D
,
206 .outputs
= st_fs660c32_D2_clks
,
209 static const struct clkgen_clk_out st_fs660c32_D3_clks
[] = {
210 { .name
= "clk-s-d3-fs0-ch0", },
211 { .name
= "clk-s-d3-fs0-ch1", },
212 { .name
= "clk-s-d3-fs0-ch2", },
213 { .name
= "clk-s-d3-fs0-ch3", },
216 static const struct clkgen_quadfs_data_clks st_fs660c32_D3_data
= {
217 .data
= (struct clkgen_quadfs_data
*)&st_fs660c32_D
,
218 .outputs
= st_fs660c32_D3_clks
,
222 * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
224 * Traits of this clock:
225 * prepare - clk_(un)prepare only ensures parent is (un)prepared
226 * enable - clk_enable and clk_disable are functional & control the Fsyn
227 * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
228 * parent - fixed parent. No clk_set_parent support
232 * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
233 * its parent clock, found inside a type of
234 * ST quad channel frequency synthesizer block
236 * @hw: handle between common and hardware-specific interfaces.
237 * @regs_base: base address of the configuration registers.
239 * @data: local driver data
240 * @ndiv: regmap field for the ndiv control.
242 struct st_clk_quadfs_pll
{
244 void __iomem
*regs_base
;
246 struct clkgen_quadfs_data
*data
;
250 #define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
252 static int quadfs_pll_enable(struct clk_hw
*hw
)
254 struct st_clk_quadfs_pll
*pll
= to_quadfs_pll(hw
);
255 unsigned long flags
= 0, timeout
= jiffies
+ msecs_to_jiffies(10);
258 spin_lock_irqsave(pll
->lock
, flags
);
261 * Bring block out of reset if we have reset control.
263 if (pll
->data
->reset_present
)
264 CLKGEN_WRITE(pll
, nreset
, 1);
267 * Use a fixed input clock noise bandwidth filter for the moment
269 if (pll
->data
->bwfilter_present
)
270 CLKGEN_WRITE(pll
, ref_bw
, PLL_BW_GOODREF
);
273 CLKGEN_WRITE(pll
, ndiv
, pll
->ndiv
);
278 CLKGEN_WRITE(pll
, npda
, !pll
->data
->powerup_polarity
);
281 spin_unlock_irqrestore(pll
->lock
, flags
);
283 if (pll
->data
->lockstatus_present
)
284 while (!CLKGEN_READ(pll
, lock_status
)) {
285 if (time_after(jiffies
, timeout
))
293 static void quadfs_pll_disable(struct clk_hw
*hw
)
295 struct st_clk_quadfs_pll
*pll
= to_quadfs_pll(hw
);
296 unsigned long flags
= 0;
299 spin_lock_irqsave(pll
->lock
, flags
);
302 * Powerdown the PLL and then put block into soft reset if we have
305 CLKGEN_WRITE(pll
, npda
, pll
->data
->powerup_polarity
);
307 if (pll
->data
->reset_present
)
308 CLKGEN_WRITE(pll
, nreset
, 0);
311 spin_unlock_irqrestore(pll
->lock
, flags
);
314 static int quadfs_pll_is_enabled(struct clk_hw
*hw
)
316 struct st_clk_quadfs_pll
*pll
= to_quadfs_pll(hw
);
317 u32 npda
= CLKGEN_READ(pll
, npda
);
319 return pll
->data
->powerup_polarity
? !npda
: !!npda
;
322 static int clk_fs660c32_vco_get_rate(unsigned long input
, struct stm_fs
*fs
,
325 unsigned long nd
= fs
->ndiv
+ 16; /* ndiv value */
332 static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw
*hw
,
333 unsigned long parent_rate
)
335 struct st_clk_quadfs_pll
*pll
= to_quadfs_pll(hw
);
336 unsigned long rate
= 0;
337 struct stm_fs params
;
339 params
.ndiv
= CLKGEN_READ(pll
, ndiv
);
340 if (clk_fs660c32_vco_get_rate(parent_rate
, ¶ms
, &rate
))
341 pr_err("%s:%s error calculating rate\n",
342 clk_hw_get_name(hw
), __func__
);
344 pll
->ndiv
= params
.ndiv
;
349 static int clk_fs660c32_vco_get_params(unsigned long input
,
350 unsigned long output
, struct stm_fs
*fs
)
353 VCO frequency = (fin x ndiv) / pdiv
354 ndiv = VCOfreq * pdiv / fin
356 unsigned long pdiv
= 1, n
;
358 /* Output clock range: 384Mhz to 660Mhz */
359 if (output
< 384000000 || output
> 660000000)
362 if (input
> 40000000)
363 /* This means that PDIV would be 2 instead of 1.
364 Not supported today. */
370 n
= output
* pdiv
/ input
;
373 fs
->ndiv
= n
- 16; /* Converting formula value to reg value */
378 static long quadfs_pll_fs660c32_round_rate(struct clk_hw
*hw
,
380 unsigned long *prate
)
382 struct stm_fs params
;
384 if (clk_fs660c32_vco_get_params(*prate
, rate
, ¶ms
))
387 clk_fs660c32_vco_get_rate(*prate
, ¶ms
, &rate
);
389 pr_debug("%s: %s new rate %ld [ndiv=%u]\n",
390 __func__
, clk_hw_get_name(hw
),
391 rate
, (unsigned int)params
.ndiv
);
396 static int quadfs_pll_fs660c32_set_rate(struct clk_hw
*hw
, unsigned long rate
,
397 unsigned long parent_rate
)
399 struct st_clk_quadfs_pll
*pll
= to_quadfs_pll(hw
);
400 struct stm_fs params
;
402 unsigned long flags
= 0;
405 if (!rate
|| !parent_rate
)
408 ret
= clk_fs660c32_vco_get_params(parent_rate
, rate
, ¶ms
);
412 clk_fs660c32_vco_get_rate(parent_rate
, ¶ms
, &hwrate
);
414 pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
415 __func__
, clk_hw_get_name(hw
),
416 hwrate
, (unsigned int)params
.ndiv
);
421 pll
->ndiv
= params
.ndiv
;
424 spin_lock_irqsave(pll
->lock
, flags
);
426 CLKGEN_WRITE(pll
, ndiv
, pll
->ndiv
);
429 spin_unlock_irqrestore(pll
->lock
, flags
);
434 static const struct clk_ops st_quadfs_pll_c32_ops
= {
435 .enable
= quadfs_pll_enable
,
436 .disable
= quadfs_pll_disable
,
437 .is_enabled
= quadfs_pll_is_enabled
,
438 .recalc_rate
= quadfs_pll_fs660c32_recalc_rate
,
439 .round_rate
= quadfs_pll_fs660c32_round_rate
,
440 .set_rate
= quadfs_pll_fs660c32_set_rate
,
443 static struct clk
* __init
st_clk_register_quadfs_pll(
444 const char *name
, const char *parent_name
,
445 struct clkgen_quadfs_data
*quadfs
, void __iomem
*reg
,
448 struct st_clk_quadfs_pll
*pll
;
450 struct clk_init_data init
;
453 * Sanity check required pointers.
455 if (WARN_ON(!name
|| !parent_name
))
456 return ERR_PTR(-EINVAL
);
458 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
460 return ERR_PTR(-ENOMEM
);
463 init
.ops
= quadfs
->pll_ops
;
464 init
.flags
= CLK_GET_RATE_NOCACHE
;
465 init
.parent_names
= &parent_name
;
466 init
.num_parents
= 1;
469 pll
->regs_base
= reg
;
471 pll
->hw
.init
= &init
;
473 clk
= clk_register(NULL
, &pll
->hw
);
482 * DOC: A digital frequency synthesizer
484 * Traits of this clock:
485 * prepare - clk_(un)prepare only ensures parent is (un)prepared
486 * enable - clk_enable and clk_disable are functional
487 * rate - set rate is functional
488 * parent - fixed parent. No clk_set_parent support
492 * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
493 * frequency synthesizer (fsynth) block.
495 * @hw: handle between common and hardware-specific interfaces
497 * @nsb: regmap field in the output control register for the digital
498 * standby of this fsynth channel. This control is active low so
499 * the channel is in standby when the control bit is cleared.
501 * @nsdiv: regmap field in the output control register for
502 * for the optional divide by 3 of this fsynth channel. This control
503 * is active low so the divide by 3 is active when the control bit is
504 * cleared and the divide is bypassed when the bit is set.
506 struct st_clk_quadfs_fsynth
{
508 void __iomem
*regs_base
;
510 struct clkgen_quadfs_data
*data
;
514 * Cached hardware values from set_rate so we can program the
515 * hardware in enable. There are two reasons for this:
517 * 1. The registers may not be writable until the parent has been
520 * 2. It restores the clock rate when a driver does an enable
521 * on PM restore, after a suspend to RAM has lost the hardware
530 #define to_quadfs_fsynth(_hw) \
531 container_of(_hw, struct st_clk_quadfs_fsynth, hw)
533 static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth
*fs
)
536 * Pulse the program enable register lsb to make the hardware take
537 * notice of the new md/pe values with a glitchless transition.
539 CLKGEN_WRITE(fs
, en
[fs
->chan
], 1);
540 CLKGEN_WRITE(fs
, en
[fs
->chan
], 0);
543 static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth
*fs
)
545 unsigned long flags
= 0;
548 * Ensure the md/pe parameters are ignored while we are
549 * reprogramming them so we can get a glitchless change
550 * when fine tuning the speed of a running clock.
552 CLKGEN_WRITE(fs
, en
[fs
->chan
], 0);
554 CLKGEN_WRITE(fs
, mdiv
[fs
->chan
], fs
->md
);
555 CLKGEN_WRITE(fs
, pe
[fs
->chan
], fs
->pe
);
556 CLKGEN_WRITE(fs
, sdiv
[fs
->chan
], fs
->sdiv
);
559 spin_lock_irqsave(fs
->lock
, flags
);
561 if (fs
->data
->nsdiv_present
)
562 CLKGEN_WRITE(fs
, nsdiv
[fs
->chan
], fs
->nsdiv
);
565 spin_unlock_irqrestore(fs
->lock
, flags
);
568 static int quadfs_fsynth_enable(struct clk_hw
*hw
)
570 struct st_clk_quadfs_fsynth
*fs
= to_quadfs_fsynth(hw
);
571 unsigned long flags
= 0;
573 pr_debug("%s: %s\n", __func__
, clk_hw_get_name(hw
));
575 quadfs_fsynth_program_rate(fs
);
578 spin_lock_irqsave(fs
->lock
, flags
);
580 CLKGEN_WRITE(fs
, nsb
[fs
->chan
], !fs
->data
->standby_polarity
);
582 if (fs
->data
->nrst_present
)
583 CLKGEN_WRITE(fs
, nrst
[fs
->chan
], 0);
586 spin_unlock_irqrestore(fs
->lock
, flags
);
588 quadfs_fsynth_program_enable(fs
);
593 static void quadfs_fsynth_disable(struct clk_hw
*hw
)
595 struct st_clk_quadfs_fsynth
*fs
= to_quadfs_fsynth(hw
);
596 unsigned long flags
= 0;
598 pr_debug("%s: %s\n", __func__
, clk_hw_get_name(hw
));
601 spin_lock_irqsave(fs
->lock
, flags
);
603 CLKGEN_WRITE(fs
, nsb
[fs
->chan
], fs
->data
->standby_polarity
);
606 spin_unlock_irqrestore(fs
->lock
, flags
);
609 static int quadfs_fsynth_is_enabled(struct clk_hw
*hw
)
611 struct st_clk_quadfs_fsynth
*fs
= to_quadfs_fsynth(hw
);
612 u32 nsb
= CLKGEN_READ(fs
, nsb
[fs
->chan
]);
614 pr_debug("%s: %s enable bit = 0x%x\n",
615 __func__
, clk_hw_get_name(hw
), nsb
);
617 return fs
->data
->standby_polarity
? !nsb
: !!nsb
;
620 #define P20 (uint64_t)(1 << 20)
622 static int clk_fs660c32_dig_get_rate(unsigned long input
,
623 const struct stm_fs
*fs
, unsigned long *rate
)
625 unsigned long s
= (1 << fs
->sdiv
);
630 * 'nsdiv' is a register value ('BIN') which is translated
631 * to a decimal value according to following rules.
637 ns
= (fs
->nsdiv
== 1) ? 1 : 3;
639 res
= (P20
* (32 + fs
->mdiv
) + 32 * fs
->pe
) * s
* ns
;
640 *rate
= (unsigned long)div64_u64(input
* P20
* 32, res
);
646 static int clk_fs660c32_get_pe(int m
, int si
, unsigned long *deviation
,
647 signed long input
, unsigned long output
, uint64_t *p
,
650 unsigned long new_freq
, new_deviation
;
651 struct stm_fs fs_tmp
;
654 val
= (uint64_t)output
<< si
;
656 *p
= (uint64_t)input
* P20
- (32LL + (uint64_t)m
) * val
* (P20
/ 32LL);
658 *p
= div64_u64(*p
, val
);
663 fs_tmp
.mdiv
= (unsigned long) m
;
664 fs_tmp
.pe
= (unsigned long)*p
;
668 clk_fs660c32_dig_get_rate(input
, &fs_tmp
, &new_freq
);
670 new_deviation
= abs(output
- new_freq
);
672 if (new_deviation
< *deviation
) {
674 fs
->pe
= (unsigned long)*p
;
677 *deviation
= new_deviation
;
682 static int clk_fs660c32_dig_get_params(unsigned long input
,
683 unsigned long output
, struct stm_fs
*fs
)
685 int si
; /* sdiv_reg (8 downto 0) */
686 int m
; /* md value */
687 unsigned long new_freq
, new_deviation
;
688 /* initial condition to say: "infinite deviation" */
689 unsigned long deviation
= ~0;
690 uint64_t p
, p1
, p2
; /* pe value */
693 struct stm_fs fs_tmp
;
695 for (si
= 0; (si
<= 8) && deviation
; si
++) {
697 /* Boundary test to avoid useless iteration */
698 r1
= clk_fs660c32_get_pe(0, si
, &deviation
,
699 input
, output
, &p1
, fs
);
700 r2
= clk_fs660c32_get_pe(31, si
, &deviation
,
701 input
, output
, &p2
, fs
);
704 if (r1
&& r2
&& (p1
> p2
))
707 /* Try to find best deviation */
708 for (m
= 1; (m
< 31) && deviation
; m
++)
709 clk_fs660c32_get_pe(m
, si
, &deviation
,
710 input
, output
, &p
, fs
);
714 if (deviation
== ~0) /* No solution found */
717 /* pe fine tuning if deviation not 0: +/- 2 around computed pe value */
719 fs_tmp
.mdiv
= fs
->mdiv
;
720 fs_tmp
.sdiv
= fs
->sdiv
;
721 fs_tmp
.nsdiv
= fs
->nsdiv
;
728 for (; p2
< 32768ll && (p2
<= (fs
->pe
+ 2)); p2
++) {
729 fs_tmp
.pe
= (unsigned long)p2
;
731 clk_fs660c32_dig_get_rate(input
, &fs_tmp
, &new_freq
);
733 new_deviation
= abs(output
- new_freq
);
735 /* Check if this is a better solution */
736 if (new_deviation
< deviation
) {
737 fs
->pe
= (unsigned long)p2
;
738 deviation
= new_deviation
;
746 static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth
*fs
,
747 struct stm_fs
*params
)
750 * Get the initial hardware values for recalc_rate
752 params
->mdiv
= CLKGEN_READ(fs
, mdiv
[fs
->chan
]);
753 params
->pe
= CLKGEN_READ(fs
, pe
[fs
->chan
]);
754 params
->sdiv
= CLKGEN_READ(fs
, sdiv
[fs
->chan
]);
756 if (fs
->data
->nsdiv_present
)
757 params
->nsdiv
= CLKGEN_READ(fs
, nsdiv
[fs
->chan
]);
762 * If All are NULL then assume no clock rate is programmed.
764 if (!params
->mdiv
&& !params
->pe
&& !params
->sdiv
)
767 fs
->md
= params
->mdiv
;
769 fs
->sdiv
= params
->sdiv
;
770 fs
->nsdiv
= params
->nsdiv
;
775 static long quadfs_find_best_rate(struct clk_hw
*hw
, unsigned long drate
,
776 unsigned long prate
, struct stm_fs
*params
)
778 struct st_clk_quadfs_fsynth
*fs
= to_quadfs_fsynth(hw
);
779 int (*clk_fs_get_rate
)(unsigned long ,
780 const struct stm_fs
*, unsigned long *);
781 int (*clk_fs_get_params
)(unsigned long, unsigned long, struct stm_fs
*);
782 unsigned long rate
= 0;
784 clk_fs_get_rate
= fs
->data
->get_rate
;
785 clk_fs_get_params
= fs
->data
->get_params
;
787 if (!clk_fs_get_params(prate
, drate
, params
))
788 clk_fs_get_rate(prate
, params
, &rate
);
793 static unsigned long quadfs_recalc_rate(struct clk_hw
*hw
,
794 unsigned long parent_rate
)
796 struct st_clk_quadfs_fsynth
*fs
= to_quadfs_fsynth(hw
);
797 unsigned long rate
= 0;
798 struct stm_fs params
;
799 int (*clk_fs_get_rate
)(unsigned long ,
800 const struct stm_fs
*, unsigned long *);
802 clk_fs_get_rate
= fs
->data
->get_rate
;
804 if (quadfs_fsynt_get_hw_value_for_recalc(fs
, ¶ms
))
807 if (clk_fs_get_rate(parent_rate
, ¶ms
, &rate
)) {
808 pr_err("%s:%s error calculating rate\n",
809 clk_hw_get_name(hw
), __func__
);
812 pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw
), __func__
, rate
);
817 static long quadfs_round_rate(struct clk_hw
*hw
, unsigned long rate
,
818 unsigned long *prate
)
820 struct stm_fs params
;
822 rate
= quadfs_find_best_rate(hw
, rate
, *prate
, ¶ms
);
824 pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
825 __func__
, clk_hw_get_name(hw
),
826 rate
, (unsigned int)params
.sdiv
, (unsigned int)params
.mdiv
,
827 (unsigned int)params
.pe
, (unsigned int)params
.nsdiv
);
833 static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth
*fs
,
834 struct stm_fs
*params
)
836 fs
->md
= params
->mdiv
;
838 fs
->sdiv
= params
->sdiv
;
839 fs
->nsdiv
= params
->nsdiv
;
842 * In some integrations you can only change the fsynth programming when
843 * the parent entity containing it is enabled.
845 quadfs_fsynth_program_rate(fs
);
846 quadfs_fsynth_program_enable(fs
);
849 static int quadfs_set_rate(struct clk_hw
*hw
, unsigned long rate
,
850 unsigned long parent_rate
)
852 struct st_clk_quadfs_fsynth
*fs
= to_quadfs_fsynth(hw
);
853 struct stm_fs params
;
856 if (!rate
|| !parent_rate
)
859 memset(¶ms
, 0, sizeof(struct stm_fs
));
861 hwrate
= quadfs_find_best_rate(hw
, rate
, parent_rate
, ¶ms
);
865 quadfs_program_and_enable(fs
, ¶ms
);
872 static const struct clk_ops st_quadfs_ops
= {
873 .enable
= quadfs_fsynth_enable
,
874 .disable
= quadfs_fsynth_disable
,
875 .is_enabled
= quadfs_fsynth_is_enabled
,
876 .round_rate
= quadfs_round_rate
,
877 .set_rate
= quadfs_set_rate
,
878 .recalc_rate
= quadfs_recalc_rate
,
881 static struct clk
* __init
st_clk_register_quadfs_fsynth(
882 const char *name
, const char *parent_name
,
883 struct clkgen_quadfs_data
*quadfs
, void __iomem
*reg
, u32 chan
,
884 unsigned long flags
, spinlock_t
*lock
)
886 struct st_clk_quadfs_fsynth
*fs
;
888 struct clk_init_data init
;
891 * Sanity check required pointers, note that nsdiv3 is optional.
893 if (WARN_ON(!name
|| !parent_name
))
894 return ERR_PTR(-EINVAL
);
896 fs
= kzalloc(sizeof(*fs
), GFP_KERNEL
);
898 return ERR_PTR(-ENOMEM
);
901 init
.ops
= &st_quadfs_ops
;
902 init
.flags
= flags
| CLK_GET_RATE_NOCACHE
;
903 init
.parent_names
= &parent_name
;
904 init
.num_parents
= 1;
912 clk
= clk_register(NULL
, &fs
->hw
);
920 static void __init
st_of_create_quadfs_fsynths(
921 struct device_node
*np
, const char *pll_name
,
922 struct clkgen_quadfs_data_clks
*quadfs
, void __iomem
*reg
,
925 struct clk_onecell_data
*clk_data
;
928 clk_data
= kzalloc(sizeof(*clk_data
), GFP_KERNEL
);
932 clk_data
->clk_num
= QUADFS_MAX_CHAN
;
933 clk_data
->clks
= kcalloc(QUADFS_MAX_CHAN
, sizeof(struct clk
*),
936 if (!clk_data
->clks
) {
941 for (fschan
= 0; fschan
< QUADFS_MAX_CHAN
; fschan
++) {
943 const char *clk_name
;
944 unsigned long flags
= 0;
946 if (quadfs
->outputs
) {
947 clk_name
= quadfs
->outputs
[fschan
].name
;
948 flags
= quadfs
->outputs
[fschan
].flags
;
950 if (of_property_read_string_index(np
,
951 "clock-output-names",
954 of_clk_detect_critical(np
, fschan
, &flags
);
958 * If we read an empty clock name then the channel is unused
960 if (*clk_name
== '\0')
963 clk
= st_clk_register_quadfs_fsynth(clk_name
, pll_name
,
964 quadfs
->data
, reg
, fschan
,
968 * If there was an error registering this clock output, clean
969 * up and move on to the next one.
972 clk_data
->clks
[fschan
] = clk
;
973 pr_debug("%s: parent %s rate %u\n",
975 __clk_get_name(clk_get_parent(clk
)),
976 (unsigned int)clk_get_rate(clk
));
980 of_clk_add_provider(np
, of_clk_src_onecell_get
, clk_data
);
983 static void __init
st_of_quadfs_setup(struct device_node
*np
,
984 struct clkgen_quadfs_data_clks
*datac
)
987 const char *pll_name
, *clk_parent_name
;
990 struct device_node
*parent_np
;
993 * First check for reg property within the node to keep backward
994 * compatibility, then if reg doesn't exist look at the parent node
996 reg
= of_iomap(np
, 0);
998 parent_np
= of_get_parent(np
);
999 reg
= of_iomap(parent_np
, 0);
1000 of_node_put(parent_np
);
1002 pr_err("%s: Failed to get base address\n", __func__
);
1007 clk_parent_name
= of_clk_get_parent_name(np
, 0);
1008 if (!clk_parent_name
)
1011 pll_name
= kasprintf(GFP_KERNEL
, "%pOFn.pll", np
);
1015 lock
= kzalloc(sizeof(*lock
), GFP_KERNEL
);
1019 spin_lock_init(lock
);
1021 clk
= st_clk_register_quadfs_pll(pll_name
, clk_parent_name
, datac
->data
,
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
, datac
, reg
, lock
);
1035 kfree(pll_name
); /* No longer need local copy of the PLL name */
1038 static void __init
st_of_quadfs660C_setup(struct device_node
*np
)
1040 st_of_quadfs_setup(np
,
1041 (struct clkgen_quadfs_data_clks
*) &st_fs660c32_C_data
);
1043 CLK_OF_DECLARE(quadfs660C
, "st,quadfs-pll", st_of_quadfs660C_setup
);
1045 static void __init
st_of_quadfs660D_setup(struct device_node
*np
)
1047 st_of_quadfs_setup(np
,
1048 (struct clkgen_quadfs_data_clks
*) &st_fs660c32_D_data
);
1050 CLK_OF_DECLARE(quadfs660D
, "st,quadfs", st_of_quadfs660D_setup
);
1052 static void __init
st_of_quadfs660D0_setup(struct device_node
*np
)
1054 st_of_quadfs_setup(np
,
1055 (struct clkgen_quadfs_data_clks
*) &st_fs660c32_D0_data
);
1057 CLK_OF_DECLARE(quadfs660D0
, "st,quadfs-d0", st_of_quadfs660D0_setup
);
1059 static void __init
st_of_quadfs660D2_setup(struct device_node
*np
)
1061 st_of_quadfs_setup(np
,
1062 (struct clkgen_quadfs_data_clks
*) &st_fs660c32_D2_data
);
1064 CLK_OF_DECLARE(quadfs660D2
, "st,quadfs-d2", st_of_quadfs660D2_setup
);
1066 static void __init
st_of_quadfs660D3_setup(struct device_node
*np
)
1068 st_of_quadfs_setup(np
,
1069 (struct clkgen_quadfs_data_clks
*) &st_fs660c32_D3_data
);
1071 CLK_OF_DECLARE(quadfs660D3
, "st,quadfs-d3", st_of_quadfs660D3_setup
);