1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Microchip Technology Inc.
7 #include <linux/bitfield.h>
9 #include <linux/clk-provider.h>
10 #include <linux/clkdev.h>
11 #include <linux/clk/at91_pmc.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/regmap.h>
18 #define PMC_PLL_CTRL0_DIV_MSK GENMASK(7, 0)
19 #define PMC_PLL_CTRL1_MUL_MSK GENMASK(31, 24)
20 #define PMC_PLL_CTRL1_FRACR_MSK GENMASK(21, 0)
22 #define PLL_DIV_MAX (FIELD_GET(PMC_PLL_CTRL0_DIV_MSK, UINT_MAX) + 1)
24 #define PLL_MUL_MAX (FIELD_GET(PMC_PLL_CTRL1_MUL_MSK, UINT_MAX) + 1)
28 struct sam9x60_pll_core
{
29 struct regmap
*regmap
;
31 const struct clk_pll_characteristics
*characteristics
;
32 const struct clk_pll_layout
*layout
;
38 struct sam9x60_pll_core core
;
39 struct at91_clk_pms pms
;
45 struct sam9x60_pll_core core
;
46 struct at91_clk_pms pms
;
51 #define to_sam9x60_pll_core(hw) container_of(hw, struct sam9x60_pll_core, hw)
52 #define to_sam9x60_frac(core) container_of(core, struct sam9x60_frac, core)
53 #define to_sam9x60_div(core) container_of(core, struct sam9x60_div, core)
55 static struct sam9x60_div
*notifier_div
;
57 static inline bool sam9x60_pll_ready(struct regmap
*regmap
, int id
)
61 regmap_read(regmap
, AT91_PMC_PLL_ISR0
, &status
);
63 return !!(status
& BIT(id
));
66 static bool sam9x60_frac_pll_ready(struct regmap
*regmap
, u8 id
)
68 return sam9x60_pll_ready(regmap
, id
);
71 static unsigned long sam9x60_frac_pll_recalc_rate(struct clk_hw
*hw
,
72 unsigned long parent_rate
)
74 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
75 struct sam9x60_frac
*frac
= to_sam9x60_frac(core
);
78 freq
= parent_rate
* (frac
->mul
+ 1) +
79 DIV_ROUND_CLOSEST_ULL((u64
)parent_rate
* frac
->frac
, (1 << 22));
81 if (core
->layout
->div2
)
87 static int sam9x60_frac_pll_set(struct sam9x60_pll_core
*core
)
89 struct sam9x60_frac
*frac
= to_sam9x60_frac(core
);
90 struct regmap
*regmap
= core
->regmap
;
91 unsigned int val
, cfrac
, cmul
;
94 spin_lock_irqsave(core
->lock
, flags
);
96 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
97 AT91_PMC_PLL_UPDT_ID_MSK
, core
->id
);
98 regmap_read(regmap
, AT91_PMC_PLL_CTRL1
, &val
);
99 cmul
= (val
& core
->layout
->mul_mask
) >> core
->layout
->mul_shift
;
100 cfrac
= (val
& core
->layout
->frac_mask
) >> core
->layout
->frac_shift
;
102 if (sam9x60_frac_pll_ready(regmap
, core
->id
) &&
103 (cmul
== frac
->mul
&& cfrac
== frac
->frac
))
106 /* Recommended value for PMC_PLL_ACR */
107 if (core
->characteristics
->upll
)
108 val
= AT91_PMC_PLL_ACR_DEFAULT_UPLL
;
110 val
= AT91_PMC_PLL_ACR_DEFAULT_PLLA
;
111 regmap_write(regmap
, AT91_PMC_PLL_ACR
, val
);
113 regmap_write(regmap
, AT91_PMC_PLL_CTRL1
,
114 (frac
->mul
<< core
->layout
->mul_shift
) |
115 (frac
->frac
<< core
->layout
->frac_shift
));
117 if (core
->characteristics
->upll
) {
118 /* Enable the UTMI internal bandgap */
119 val
|= AT91_PMC_PLL_ACR_UTMIBG
;
120 regmap_write(regmap
, AT91_PMC_PLL_ACR
, val
);
124 /* Enable the UTMI internal regulator */
125 val
|= AT91_PMC_PLL_ACR_UTMIVR
;
126 regmap_write(regmap
, AT91_PMC_PLL_ACR
, val
);
131 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
132 AT91_PMC_PLL_UPDT_UPDATE
| AT91_PMC_PLL_UPDT_ID_MSK
,
133 AT91_PMC_PLL_UPDT_UPDATE
| core
->id
);
135 regmap_update_bits(regmap
, AT91_PMC_PLL_CTRL0
,
136 AT91_PMC_PLL_CTRL0_ENLOCK
| AT91_PMC_PLL_CTRL0_ENPLL
,
137 AT91_PMC_PLL_CTRL0_ENLOCK
| AT91_PMC_PLL_CTRL0_ENPLL
);
139 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
140 AT91_PMC_PLL_UPDT_UPDATE
| AT91_PMC_PLL_UPDT_ID_MSK
,
141 AT91_PMC_PLL_UPDT_UPDATE
| core
->id
);
143 while (!sam9x60_pll_ready(regmap
, core
->id
))
147 spin_unlock_irqrestore(core
->lock
, flags
);
152 static int sam9x60_frac_pll_prepare(struct clk_hw
*hw
)
154 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
156 return sam9x60_frac_pll_set(core
);
159 static void sam9x60_frac_pll_unprepare(struct clk_hw
*hw
)
161 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
162 struct regmap
*regmap
= core
->regmap
;
165 spin_lock_irqsave(core
->lock
, flags
);
167 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
168 AT91_PMC_PLL_UPDT_ID_MSK
, core
->id
);
170 regmap_update_bits(regmap
, AT91_PMC_PLL_CTRL0
, AT91_PMC_PLL_CTRL0_ENPLL
, 0);
172 if (core
->characteristics
->upll
)
173 regmap_update_bits(regmap
, AT91_PMC_PLL_ACR
,
174 AT91_PMC_PLL_ACR_UTMIBG
| AT91_PMC_PLL_ACR_UTMIVR
, 0);
176 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
177 AT91_PMC_PLL_UPDT_UPDATE
| AT91_PMC_PLL_UPDT_ID_MSK
,
178 AT91_PMC_PLL_UPDT_UPDATE
| core
->id
);
180 spin_unlock_irqrestore(core
->lock
, flags
);
183 static int sam9x60_frac_pll_is_prepared(struct clk_hw
*hw
)
185 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
187 return sam9x60_pll_ready(core
->regmap
, core
->id
);
190 static long sam9x60_frac_pll_compute_mul_frac(struct sam9x60_pll_core
*core
,
192 unsigned long parent_rate
,
195 struct sam9x60_frac
*frac
= to_sam9x60_frac(core
);
196 unsigned long tmprate
, remainder
;
197 unsigned long nmul
= 0;
198 unsigned long nfrac
= 0;
200 if (rate
< core
->characteristics
->core_output
[0].min
||
201 rate
> core
->characteristics
->core_output
[0].max
)
205 * Calculate the multiplier associated with the current
206 * divider that provide the closest rate to the requested one.
208 nmul
= mult_frac(rate
, 1, parent_rate
);
209 tmprate
= mult_frac(parent_rate
, nmul
, 1);
210 remainder
= rate
- tmprate
;
213 nfrac
= DIV_ROUND_CLOSEST_ULL((u64
)remainder
* (1 << 22),
216 tmprate
+= DIV_ROUND_CLOSEST_ULL((u64
)nfrac
* parent_rate
,
220 /* Check if resulted rate is a valid. */
221 if (tmprate
< core
->characteristics
->core_output
[0].min
||
222 tmprate
> core
->characteristics
->core_output
[0].max
)
226 frac
->mul
= nmul
- 1;
233 static long sam9x60_frac_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
234 unsigned long *parent_rate
)
236 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
238 return sam9x60_frac_pll_compute_mul_frac(core
, rate
, *parent_rate
, false);
241 static int sam9x60_frac_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
242 unsigned long parent_rate
)
244 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
246 return sam9x60_frac_pll_compute_mul_frac(core
, rate
, parent_rate
, true);
249 static int sam9x60_frac_pll_set_rate_chg(struct clk_hw
*hw
, unsigned long rate
,
250 unsigned long parent_rate
)
252 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
253 struct sam9x60_frac
*frac
= to_sam9x60_frac(core
);
254 struct regmap
*regmap
= core
->regmap
;
255 unsigned long irqflags
;
256 unsigned int val
, cfrac
, cmul
;
259 ret
= sam9x60_frac_pll_compute_mul_frac(core
, rate
, parent_rate
, true);
263 spin_lock_irqsave(core
->lock
, irqflags
);
265 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
, AT91_PMC_PLL_UPDT_ID_MSK
,
267 regmap_read(regmap
, AT91_PMC_PLL_CTRL1
, &val
);
268 cmul
= (val
& core
->layout
->mul_mask
) >> core
->layout
->mul_shift
;
269 cfrac
= (val
& core
->layout
->frac_mask
) >> core
->layout
->frac_shift
;
271 if (cmul
== frac
->mul
&& cfrac
== frac
->frac
)
274 regmap_write(regmap
, AT91_PMC_PLL_CTRL1
,
275 (frac
->mul
<< core
->layout
->mul_shift
) |
276 (frac
->frac
<< core
->layout
->frac_shift
));
278 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
279 AT91_PMC_PLL_UPDT_UPDATE
| AT91_PMC_PLL_UPDT_ID_MSK
,
280 AT91_PMC_PLL_UPDT_UPDATE
| core
->id
);
282 regmap_update_bits(regmap
, AT91_PMC_PLL_CTRL0
,
283 AT91_PMC_PLL_CTRL0_ENLOCK
| AT91_PMC_PLL_CTRL0_ENPLL
,
284 AT91_PMC_PLL_CTRL0_ENLOCK
|
285 AT91_PMC_PLL_CTRL0_ENPLL
);
287 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
288 AT91_PMC_PLL_UPDT_UPDATE
| AT91_PMC_PLL_UPDT_ID_MSK
,
289 AT91_PMC_PLL_UPDT_UPDATE
| core
->id
);
291 while (!sam9x60_pll_ready(regmap
, core
->id
))
295 spin_unlock_irqrestore(core
->lock
, irqflags
);
300 static int sam9x60_frac_pll_save_context(struct clk_hw
*hw
)
302 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
303 struct sam9x60_frac
*frac
= to_sam9x60_frac(core
);
305 frac
->pms
.status
= sam9x60_pll_ready(core
->regmap
, core
->id
);
310 static void sam9x60_frac_pll_restore_context(struct clk_hw
*hw
)
312 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
313 struct sam9x60_frac
*frac
= to_sam9x60_frac(core
);
315 if (frac
->pms
.status
)
316 sam9x60_frac_pll_set(core
);
319 static const struct clk_ops sam9x60_frac_pll_ops
= {
320 .prepare
= sam9x60_frac_pll_prepare
,
321 .unprepare
= sam9x60_frac_pll_unprepare
,
322 .is_prepared
= sam9x60_frac_pll_is_prepared
,
323 .recalc_rate
= sam9x60_frac_pll_recalc_rate
,
324 .round_rate
= sam9x60_frac_pll_round_rate
,
325 .set_rate
= sam9x60_frac_pll_set_rate
,
326 .save_context
= sam9x60_frac_pll_save_context
,
327 .restore_context
= sam9x60_frac_pll_restore_context
,
330 static const struct clk_ops sam9x60_frac_pll_ops_chg
= {
331 .prepare
= sam9x60_frac_pll_prepare
,
332 .unprepare
= sam9x60_frac_pll_unprepare
,
333 .is_prepared
= sam9x60_frac_pll_is_prepared
,
334 .recalc_rate
= sam9x60_frac_pll_recalc_rate
,
335 .round_rate
= sam9x60_frac_pll_round_rate
,
336 .set_rate
= sam9x60_frac_pll_set_rate_chg
,
337 .save_context
= sam9x60_frac_pll_save_context
,
338 .restore_context
= sam9x60_frac_pll_restore_context
,
341 /* This function should be called with spinlock acquired. */
342 static void sam9x60_div_pll_set_div(struct sam9x60_pll_core
*core
, u32 div
,
345 struct regmap
*regmap
= core
->regmap
;
346 u32 ena_msk
= enable
? core
->layout
->endiv_mask
: 0;
347 u32 ena_val
= enable
? (1 << core
->layout
->endiv_shift
) : 0;
349 regmap_update_bits(regmap
, AT91_PMC_PLL_CTRL0
,
350 core
->layout
->div_mask
| ena_msk
,
351 (div
<< core
->layout
->div_shift
) | ena_val
);
353 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
354 AT91_PMC_PLL_UPDT_UPDATE
| AT91_PMC_PLL_UPDT_ID_MSK
,
355 AT91_PMC_PLL_UPDT_UPDATE
| core
->id
);
357 while (!sam9x60_pll_ready(regmap
, core
->id
))
361 static int sam9x60_div_pll_set(struct sam9x60_pll_core
*core
)
363 struct sam9x60_div
*div
= to_sam9x60_div(core
);
364 struct regmap
*regmap
= core
->regmap
;
366 unsigned int val
, cdiv
;
368 spin_lock_irqsave(core
->lock
, flags
);
369 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
370 AT91_PMC_PLL_UPDT_ID_MSK
, core
->id
);
371 regmap_read(regmap
, AT91_PMC_PLL_CTRL0
, &val
);
372 cdiv
= (val
& core
->layout
->div_mask
) >> core
->layout
->div_shift
;
374 /* Stop if enabled an nothing changed. */
375 if (!!(val
& core
->layout
->endiv_mask
) && cdiv
== div
->div
)
378 sam9x60_div_pll_set_div(core
, div
->div
, 1);
381 spin_unlock_irqrestore(core
->lock
, flags
);
386 static int sam9x60_div_pll_prepare(struct clk_hw
*hw
)
388 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
390 return sam9x60_div_pll_set(core
);
393 static void sam9x60_div_pll_unprepare(struct clk_hw
*hw
)
395 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
396 struct regmap
*regmap
= core
->regmap
;
399 spin_lock_irqsave(core
->lock
, flags
);
401 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
402 AT91_PMC_PLL_UPDT_ID_MSK
, core
->id
);
404 regmap_update_bits(regmap
, AT91_PMC_PLL_CTRL0
,
405 core
->layout
->endiv_mask
, 0);
407 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
408 AT91_PMC_PLL_UPDT_UPDATE
| AT91_PMC_PLL_UPDT_ID_MSK
,
409 AT91_PMC_PLL_UPDT_UPDATE
| core
->id
);
411 spin_unlock_irqrestore(core
->lock
, flags
);
414 static int sam9x60_div_pll_is_prepared(struct clk_hw
*hw
)
416 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
417 struct regmap
*regmap
= core
->regmap
;
421 spin_lock_irqsave(core
->lock
, flags
);
423 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
424 AT91_PMC_PLL_UPDT_ID_MSK
, core
->id
);
425 regmap_read(regmap
, AT91_PMC_PLL_CTRL0
, &val
);
427 spin_unlock_irqrestore(core
->lock
, flags
);
429 return !!(val
& core
->layout
->endiv_mask
);
432 static unsigned long sam9x60_div_pll_recalc_rate(struct clk_hw
*hw
,
433 unsigned long parent_rate
)
435 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
436 struct sam9x60_div
*div
= to_sam9x60_div(core
);
438 return DIV_ROUND_CLOSEST_ULL(parent_rate
, (div
->div
+ 1));
441 static unsigned long sam9x60_fixed_div_pll_recalc_rate(struct clk_hw
*hw
,
442 unsigned long parent_rate
)
444 return parent_rate
>> 1;
447 static long sam9x60_div_pll_compute_div(struct sam9x60_pll_core
*core
,
448 unsigned long *parent_rate
,
451 const struct clk_pll_characteristics
*characteristics
=
452 core
->characteristics
;
453 struct clk_hw
*parent
= clk_hw_get_parent(&core
->hw
);
454 unsigned long tmp_rate
, tmp_parent_rate
, tmp_diff
;
455 long best_diff
= -1, best_rate
= -EINVAL
;
461 if (rate
< characteristics
->output
[0].min
||
462 rate
> characteristics
->output
[0].max
)
465 for (divid
= 1; divid
< core
->layout
->div_mask
; divid
++) {
466 tmp_parent_rate
= clk_hw_round_rate(parent
, rate
* divid
);
467 if (!tmp_parent_rate
)
470 tmp_rate
= DIV_ROUND_CLOSEST_ULL(tmp_parent_rate
, divid
);
471 tmp_diff
= abs(rate
- tmp_rate
);
473 if (best_diff
< 0 || best_diff
> tmp_diff
) {
474 *parent_rate
= tmp_parent_rate
;
475 best_rate
= tmp_rate
;
476 best_diff
= tmp_diff
;
483 if (best_rate
< characteristics
->output
[0].min
||
484 best_rate
> characteristics
->output
[0].max
)
490 static long sam9x60_div_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
491 unsigned long *parent_rate
)
493 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
495 return sam9x60_div_pll_compute_div(core
, parent_rate
, rate
);
498 static int sam9x60_div_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
499 unsigned long parent_rate
)
501 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
502 struct sam9x60_div
*div
= to_sam9x60_div(core
);
504 div
->div
= DIV_ROUND_CLOSEST(parent_rate
, rate
) - 1;
509 static int sam9x60_div_pll_set_rate_chg(struct clk_hw
*hw
, unsigned long rate
,
510 unsigned long parent_rate
)
512 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
513 struct sam9x60_div
*div
= to_sam9x60_div(core
);
514 struct regmap
*regmap
= core
->regmap
;
515 unsigned long irqflags
;
516 unsigned int val
, cdiv
;
518 div
->div
= DIV_ROUND_CLOSEST(parent_rate
, rate
) - 1;
520 spin_lock_irqsave(core
->lock
, irqflags
);
521 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
, AT91_PMC_PLL_UPDT_ID_MSK
,
523 regmap_read(regmap
, AT91_PMC_PLL_CTRL0
, &val
);
524 cdiv
= (val
& core
->layout
->div_mask
) >> core
->layout
->div_shift
;
526 /* Stop if nothing changed. */
527 if (cdiv
== div
->div
)
530 sam9x60_div_pll_set_div(core
, div
->div
, 0);
533 spin_unlock_irqrestore(core
->lock
, irqflags
);
538 static int sam9x60_div_pll_save_context(struct clk_hw
*hw
)
540 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
541 struct sam9x60_div
*div
= to_sam9x60_div(core
);
543 div
->pms
.status
= sam9x60_div_pll_is_prepared(hw
);
548 static void sam9x60_div_pll_restore_context(struct clk_hw
*hw
)
550 struct sam9x60_pll_core
*core
= to_sam9x60_pll_core(hw
);
551 struct sam9x60_div
*div
= to_sam9x60_div(core
);
554 sam9x60_div_pll_set(core
);
557 static int sam9x60_div_pll_notifier_fn(struct notifier_block
*notifier
,
558 unsigned long code
, void *data
)
560 struct sam9x60_div
*div
= notifier_div
;
561 struct sam9x60_pll_core core
= div
->core
;
562 struct regmap
*regmap
= core
.regmap
;
563 unsigned long irqflags
;
565 int ret
= NOTIFY_DONE
;
567 if (code
!= PRE_RATE_CHANGE
)
571 * We switch to safe divider to avoid overclocking of other domains
572 * feed by us while the frac PLL (our parent) is changed.
574 div
->div
= div
->safe_div
;
576 spin_lock_irqsave(core
.lock
, irqflags
);
577 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
, AT91_PMC_PLL_UPDT_ID_MSK
,
579 regmap_read(regmap
, AT91_PMC_PLL_CTRL0
, &val
);
580 cdiv
= (val
& core
.layout
->div_mask
) >> core
.layout
->div_shift
;
582 /* Stop if nothing changed. */
583 if (cdiv
== div
->safe_div
)
586 sam9x60_div_pll_set_div(&core
, div
->div
, 0);
590 spin_unlock_irqrestore(core
.lock
, irqflags
);
595 static struct notifier_block sam9x60_div_pll_notifier
= {
596 .notifier_call
= sam9x60_div_pll_notifier_fn
,
599 static const struct clk_ops sam9x60_div_pll_ops
= {
600 .prepare
= sam9x60_div_pll_prepare
,
601 .unprepare
= sam9x60_div_pll_unprepare
,
602 .is_prepared
= sam9x60_div_pll_is_prepared
,
603 .recalc_rate
= sam9x60_div_pll_recalc_rate
,
604 .round_rate
= sam9x60_div_pll_round_rate
,
605 .set_rate
= sam9x60_div_pll_set_rate
,
606 .save_context
= sam9x60_div_pll_save_context
,
607 .restore_context
= sam9x60_div_pll_restore_context
,
610 static const struct clk_ops sam9x60_div_pll_ops_chg
= {
611 .prepare
= sam9x60_div_pll_prepare
,
612 .unprepare
= sam9x60_div_pll_unprepare
,
613 .is_prepared
= sam9x60_div_pll_is_prepared
,
614 .recalc_rate
= sam9x60_div_pll_recalc_rate
,
615 .round_rate
= sam9x60_div_pll_round_rate
,
616 .set_rate
= sam9x60_div_pll_set_rate_chg
,
617 .save_context
= sam9x60_div_pll_save_context
,
618 .restore_context
= sam9x60_div_pll_restore_context
,
621 static const struct clk_ops sam9x60_fixed_div_pll_ops
= {
622 .prepare
= sam9x60_div_pll_prepare
,
623 .unprepare
= sam9x60_div_pll_unprepare
,
624 .is_prepared
= sam9x60_div_pll_is_prepared
,
625 .recalc_rate
= sam9x60_fixed_div_pll_recalc_rate
,
626 .round_rate
= sam9x60_div_pll_round_rate
,
627 .save_context
= sam9x60_div_pll_save_context
,
628 .restore_context
= sam9x60_div_pll_restore_context
,
631 struct clk_hw
* __init
632 sam9x60_clk_register_frac_pll(struct regmap
*regmap
, spinlock_t
*lock
,
633 const char *name
, const char *parent_name
,
634 struct clk_hw
*parent_hw
, u8 id
,
635 const struct clk_pll_characteristics
*characteristics
,
636 const struct clk_pll_layout
*layout
, u32 flags
)
638 struct sam9x60_frac
*frac
;
640 struct clk_init_data init
= {};
641 unsigned long parent_rate
, irqflags
;
645 if (id
> PLL_MAX_ID
|| !lock
|| !parent_hw
)
646 return ERR_PTR(-EINVAL
);
648 frac
= kzalloc(sizeof(*frac
), GFP_KERNEL
);
650 return ERR_PTR(-ENOMEM
);
654 init
.parent_names
= &parent_name
;
656 init
.parent_hws
= (const struct clk_hw
**)&parent_hw
;
657 init
.num_parents
= 1;
658 if (flags
& CLK_SET_RATE_GATE
)
659 init
.ops
= &sam9x60_frac_pll_ops
;
661 init
.ops
= &sam9x60_frac_pll_ops_chg
;
666 frac
->core
.hw
.init
= &init
;
667 frac
->core
.characteristics
= characteristics
;
668 frac
->core
.layout
= layout
;
669 frac
->core
.regmap
= regmap
;
670 frac
->core
.lock
= lock
;
672 spin_lock_irqsave(frac
->core
.lock
, irqflags
);
673 if (sam9x60_pll_ready(regmap
, id
)) {
674 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
675 AT91_PMC_PLL_UPDT_ID_MSK
, id
);
676 regmap_read(regmap
, AT91_PMC_PLL_CTRL1
, &val
);
677 frac
->mul
= FIELD_GET(PMC_PLL_CTRL1_MUL_MSK
, val
);
678 frac
->frac
= FIELD_GET(PMC_PLL_CTRL1_FRACR_MSK
, val
);
681 * This means the PLL is not setup by bootloaders. In this
682 * case we need to set the minimum rate for it. Otherwise
683 * a clock child of this PLL may be enabled before setting
684 * its rate leading to enabling this PLL with unsupported
685 * rate. This will lead to PLL not being locked at all.
687 parent_rate
= clk_hw_get_rate(parent_hw
);
689 hw
= ERR_PTR(-EINVAL
);
693 ret
= sam9x60_frac_pll_compute_mul_frac(&frac
->core
,
694 characteristics
->core_output
[0].min
,
701 spin_unlock_irqrestore(frac
->core
.lock
, irqflags
);
704 ret
= clk_hw_register(NULL
, hw
);
713 spin_unlock_irqrestore(frac
->core
.lock
, irqflags
);
718 struct clk_hw
* __init
719 sam9x60_clk_register_div_pll(struct regmap
*regmap
, spinlock_t
*lock
,
720 const char *name
, const char *parent_name
,
721 struct clk_hw
*parent_hw
, u8 id
,
722 const struct clk_pll_characteristics
*characteristics
,
723 const struct clk_pll_layout
*layout
, u32 flags
,
726 struct sam9x60_div
*div
;
728 struct clk_init_data init
= {};
729 unsigned long irqflags
;
733 /* We only support one changeable PLL. */
734 if (id
> PLL_MAX_ID
|| !lock
|| (safe_div
&& notifier_div
))
735 return ERR_PTR(-EINVAL
);
737 if (safe_div
>= PLL_DIV_MAX
)
738 safe_div
= PLL_DIV_MAX
- 1;
740 div
= kzalloc(sizeof(*div
), GFP_KERNEL
);
742 return ERR_PTR(-ENOMEM
);
746 init
.parent_hws
= (const struct clk_hw
**)&parent_hw
;
748 init
.parent_names
= &parent_name
;
749 init
.num_parents
= 1;
752 init
.ops
= &sam9x60_fixed_div_pll_ops
;
753 else if (flags
& CLK_SET_RATE_GATE
)
754 init
.ops
= &sam9x60_div_pll_ops
;
756 init
.ops
= &sam9x60_div_pll_ops_chg
;
761 div
->core
.hw
.init
= &init
;
762 div
->core
.characteristics
= characteristics
;
763 div
->core
.layout
= layout
;
764 div
->core
.regmap
= regmap
;
765 div
->core
.lock
= lock
;
766 div
->safe_div
= safe_div
;
768 spin_lock_irqsave(div
->core
.lock
, irqflags
);
770 regmap_update_bits(regmap
, AT91_PMC_PLL_UPDT
,
771 AT91_PMC_PLL_UPDT_ID_MSK
, id
);
772 regmap_read(regmap
, AT91_PMC_PLL_CTRL0
, &val
);
773 div
->div
= FIELD_GET(PMC_PLL_CTRL0_DIV_MSK
, val
);
775 spin_unlock_irqrestore(div
->core
.lock
, irqflags
);
778 ret
= clk_hw_register(NULL
, hw
);
782 } else if (div
->safe_div
) {
784 clk_notifier_register(hw
->clk
, &sam9x60_div_pll_notifier
);