2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/regmap.h>
20 #define PLL_STATUS_MASK(id) (1 << (1 + (id)))
21 #define PLL_REG(id) (AT91_CKGR_PLLAR + ((id) * 4))
22 #define PLL_DIV_MASK 0xff
23 #define PLL_DIV_MAX PLL_DIV_MASK
24 #define PLL_DIV(reg) ((reg) & PLL_DIV_MASK)
25 #define PLL_MUL(reg, layout) (((reg) >> (layout)->mul_shift) & \
28 #define PLL_MUL_MASK(layout) ((layout)->mul_mask)
29 #define PLL_MUL_MAX(layout) (PLL_MUL_MASK(layout) + 1)
30 #define PLL_ICPR_SHIFT(id) ((id) * 16)
31 #define PLL_ICPR_MASK(id) (0xffff << PLL_ICPR_SHIFT(id))
32 #define PLL_MAX_COUNT 0x3f
33 #define PLL_COUNT_SHIFT 8
34 #define PLL_OUT_SHIFT 14
37 struct clk_pll_characteristics
{
38 struct clk_range input
;
40 struct clk_range
*output
;
45 struct clk_pll_layout
{
51 #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
55 struct regmap
*regmap
;
60 const struct clk_pll_layout
*layout
;
61 const struct clk_pll_characteristics
*characteristics
;
64 static inline bool clk_pll_ready(struct regmap
*regmap
, int id
)
68 regmap_read(regmap
, AT91_PMC_SR
, &status
);
70 return status
& PLL_STATUS_MASK(id
) ? 1 : 0;
73 static int clk_pll_prepare(struct clk_hw
*hw
)
75 struct clk_pll
*pll
= to_clk_pll(hw
);
76 struct regmap
*regmap
= pll
->regmap
;
77 const struct clk_pll_layout
*layout
= pll
->layout
;
78 const struct clk_pll_characteristics
*characteristics
=
81 u32 mask
= PLL_STATUS_MASK(id
);
82 int offset
= PLL_REG(id
);
89 regmap_read(regmap
, offset
, &pllr
);
91 mul
= PLL_MUL(pllr
, layout
);
93 regmap_read(regmap
, AT91_PMC_SR
, &status
);
94 if ((status
& mask
) &&
95 (div
== pll
->div
&& mul
== pll
->mul
))
98 if (characteristics
->out
)
99 out
= characteristics
->out
[pll
->range
];
101 if (characteristics
->icpll
)
102 regmap_update_bits(regmap
, AT91_PMC_PLLICPR
, PLL_ICPR_MASK(id
),
103 characteristics
->icpll
[pll
->range
] << PLL_ICPR_SHIFT(id
));
105 regmap_update_bits(regmap
, offset
, layout
->pllr_mask
,
106 pll
->div
| (PLL_MAX_COUNT
<< PLL_COUNT_SHIFT
) |
107 (out
<< PLL_OUT_SHIFT
) |
108 ((pll
->mul
& layout
->mul_mask
) << layout
->mul_shift
));
110 while (!clk_pll_ready(regmap
, pll
->id
))
116 static int clk_pll_is_prepared(struct clk_hw
*hw
)
118 struct clk_pll
*pll
= to_clk_pll(hw
);
120 return clk_pll_ready(pll
->regmap
, pll
->id
);
123 static void clk_pll_unprepare(struct clk_hw
*hw
)
125 struct clk_pll
*pll
= to_clk_pll(hw
);
126 unsigned int mask
= pll
->layout
->pllr_mask
;
128 regmap_update_bits(pll
->regmap
, PLL_REG(pll
->id
), mask
, ~mask
);
131 static unsigned long clk_pll_recalc_rate(struct clk_hw
*hw
,
132 unsigned long parent_rate
)
134 struct clk_pll
*pll
= to_clk_pll(hw
);
139 regmap_read(pll
->regmap
, PLL_REG(pll
->id
), &pllr
);
142 mul
= PLL_MUL(pllr
, pll
->layout
);
147 return (parent_rate
/ div
) * (mul
+ 1);
150 static long clk_pll_get_best_div_mul(struct clk_pll
*pll
, unsigned long rate
,
151 unsigned long parent_rate
,
154 const struct clk_pll_layout
*layout
= pll
->layout
;
155 const struct clk_pll_characteristics
*characteristics
=
156 pll
->characteristics
;
157 unsigned long bestremainder
= ULONG_MAX
;
158 unsigned long maxdiv
, mindiv
, tmpdiv
;
159 long bestrate
= -ERANGE
;
160 unsigned long bestdiv
;
161 unsigned long bestmul
;
164 /* Check if parent_rate is a valid input rate */
165 if (parent_rate
< characteristics
->input
.min
)
169 * Calculate minimum divider based on the minimum multiplier, the
170 * parent_rate and the requested rate.
171 * Should always be 2 according to the input and output characteristics
174 mindiv
= (parent_rate
* PLL_MUL_MIN
) / rate
;
178 if (parent_rate
> characteristics
->input
.max
) {
179 tmpdiv
= DIV_ROUND_UP(parent_rate
, characteristics
->input
.max
);
180 if (tmpdiv
> PLL_DIV_MAX
)
188 * Calculate the maximum divider which is limited by PLL register
189 * layout (limited by the MUL or DIV field size).
191 maxdiv
= DIV_ROUND_UP(parent_rate
* PLL_MUL_MAX(layout
), rate
);
192 if (maxdiv
> PLL_DIV_MAX
)
193 maxdiv
= PLL_DIV_MAX
;
196 * Iterate over the acceptable divider values to find the best
197 * divider/multiplier pair (the one that generates the closest
198 * rate to the requested one).
200 for (tmpdiv
= mindiv
; tmpdiv
<= maxdiv
; tmpdiv
++) {
201 unsigned long remainder
;
202 unsigned long tmprate
;
203 unsigned long tmpmul
;
206 * Calculate the multiplier associated with the current
207 * divider that provide the closest rate to the requested one.
209 tmpmul
= DIV_ROUND_CLOSEST(rate
, parent_rate
/ tmpdiv
);
210 tmprate
= (parent_rate
/ tmpdiv
) * tmpmul
;
212 remainder
= tmprate
- rate
;
214 remainder
= rate
- tmprate
;
217 * Compare the remainder with the best remainder found until
218 * now and elect a new best multiplier/divider pair if the
219 * current remainder is smaller than the best one.
221 if (remainder
< bestremainder
) {
222 bestremainder
= remainder
;
229 * We've found a perfect match!
230 * Stop searching now and use this multiplier/divider pair.
236 /* We haven't found any multiplier/divider pair => return -ERANGE */
240 /* Check if bestrate is a valid output rate */
241 for (i
= 0; i
< characteristics
->num_output
; i
++) {
242 if (bestrate
>= characteristics
->output
[i
].min
&&
243 bestrate
<= characteristics
->output
[i
].max
)
247 if (i
>= characteristics
->num_output
)
260 static long clk_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
261 unsigned long *parent_rate
)
263 struct clk_pll
*pll
= to_clk_pll(hw
);
265 return clk_pll_get_best_div_mul(pll
, rate
, *parent_rate
,
269 static int clk_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
270 unsigned long parent_rate
)
272 struct clk_pll
*pll
= to_clk_pll(hw
);
278 ret
= clk_pll_get_best_div_mul(pll
, rate
, parent_rate
,
290 static const struct clk_ops pll_ops
= {
291 .prepare
= clk_pll_prepare
,
292 .unprepare
= clk_pll_unprepare
,
293 .is_prepared
= clk_pll_is_prepared
,
294 .recalc_rate
= clk_pll_recalc_rate
,
295 .round_rate
= clk_pll_round_rate
,
296 .set_rate
= clk_pll_set_rate
,
299 static struct clk_hw
* __init
300 at91_clk_register_pll(struct regmap
*regmap
, const char *name
,
301 const char *parent_name
, u8 id
,
302 const struct clk_pll_layout
*layout
,
303 const struct clk_pll_characteristics
*characteristics
)
307 struct clk_init_data init
;
308 int offset
= PLL_REG(id
);
313 return ERR_PTR(-EINVAL
);
315 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
317 return ERR_PTR(-ENOMEM
);
321 init
.parent_names
= &parent_name
;
322 init
.num_parents
= 1;
323 init
.flags
= CLK_SET_RATE_GATE
;
326 pll
->hw
.init
= &init
;
327 pll
->layout
= layout
;
328 pll
->characteristics
= characteristics
;
329 pll
->regmap
= regmap
;
330 regmap_read(regmap
, offset
, &pllr
);
331 pll
->div
= PLL_DIV(pllr
);
332 pll
->mul
= PLL_MUL(pllr
, layout
);
335 ret
= clk_hw_register(NULL
, &pll
->hw
);
345 static const struct clk_pll_layout at91rm9200_pll_layout
= {
346 .pllr_mask
= 0x7FFFFFF,
351 static const struct clk_pll_layout at91sam9g45_pll_layout
= {
352 .pllr_mask
= 0xFFFFFF,
357 static const struct clk_pll_layout at91sam9g20_pllb_layout
= {
358 .pllr_mask
= 0x3FFFFF,
363 static const struct clk_pll_layout sama5d3_pll_layout
= {
364 .pllr_mask
= 0x1FFFFFF,
370 static struct clk_pll_characteristics
* __init
371 of_at91_clk_pll_get_characteristics(struct device_node
*np
)
378 struct clk_range input
;
379 struct clk_range
*output
;
382 struct clk_pll_characteristics
*characteristics
;
384 if (of_at91_get_clk_range(np
, "atmel,clk-input-range", &input
))
387 if (of_property_read_u32(np
, "#atmel,pll-clk-output-range-cells",
391 if (num_cells
< 2 || num_cells
> 4)
394 if (!of_get_property(np
, "atmel,pll-clk-output-ranges", &tmp
))
396 num_output
= tmp
/ (sizeof(u32
) * num_cells
);
398 characteristics
= kzalloc(sizeof(*characteristics
), GFP_KERNEL
);
399 if (!characteristics
)
402 output
= kcalloc(num_output
, sizeof(*output
), GFP_KERNEL
);
404 goto out_free_characteristics
;
407 out
= kcalloc(num_output
, sizeof(*out
), GFP_KERNEL
);
409 goto out_free_output
;
413 icpll
= kcalloc(num_output
, sizeof(*icpll
), GFP_KERNEL
);
415 goto out_free_output
;
418 for (i
= 0; i
< num_output
; i
++) {
419 offset
= i
* num_cells
;
420 if (of_property_read_u32_index(np
,
421 "atmel,pll-clk-output-ranges",
423 goto out_free_output
;
425 if (of_property_read_u32_index(np
,
426 "atmel,pll-clk-output-ranges",
428 goto out_free_output
;
434 if (of_property_read_u32_index(np
,
435 "atmel,pll-clk-output-ranges",
437 goto out_free_output
;
443 if (of_property_read_u32_index(np
,
444 "atmel,pll-clk-output-ranges",
446 goto out_free_output
;
450 characteristics
->input
= input
;
451 characteristics
->num_output
= num_output
;
452 characteristics
->output
= output
;
453 characteristics
->out
= out
;
454 characteristics
->icpll
= icpll
;
455 return characteristics
;
461 out_free_characteristics
:
462 kfree(characteristics
);
467 of_at91_clk_pll_setup(struct device_node
*np
,
468 const struct clk_pll_layout
*layout
)
472 struct regmap
*regmap
;
473 const char *parent_name
;
474 const char *name
= np
->name
;
475 struct clk_pll_characteristics
*characteristics
;
477 if (of_property_read_u32(np
, "reg", &id
))
480 parent_name
= of_clk_get_parent_name(np
, 0);
482 of_property_read_string(np
, "clock-output-names", &name
);
484 regmap
= syscon_node_to_regmap(of_get_parent(np
));
488 characteristics
= of_at91_clk_pll_get_characteristics(np
);
489 if (!characteristics
)
492 hw
= at91_clk_register_pll(regmap
, name
, parent_name
, id
, layout
,
495 goto out_free_characteristics
;
497 of_clk_add_hw_provider(np
, of_clk_hw_simple_get
, hw
);
500 out_free_characteristics
:
501 kfree(characteristics
);
504 static void __init
of_at91rm9200_clk_pll_setup(struct device_node
*np
)
506 of_at91_clk_pll_setup(np
, &at91rm9200_pll_layout
);
508 CLK_OF_DECLARE(at91rm9200_clk_pll
, "atmel,at91rm9200-clk-pll",
509 of_at91rm9200_clk_pll_setup
);
511 static void __init
of_at91sam9g45_clk_pll_setup(struct device_node
*np
)
513 of_at91_clk_pll_setup(np
, &at91sam9g45_pll_layout
);
515 CLK_OF_DECLARE(at91sam9g45_clk_pll
, "atmel,at91sam9g45-clk-pll",
516 of_at91sam9g45_clk_pll_setup
);
518 static void __init
of_at91sam9g20_clk_pllb_setup(struct device_node
*np
)
520 of_at91_clk_pll_setup(np
, &at91sam9g20_pllb_layout
);
522 CLK_OF_DECLARE(at91sam9g20_clk_pllb
, "atmel,at91sam9g20-clk-pllb",
523 of_at91sam9g20_clk_pllb_setup
);
525 static void __init
of_sama5d3_clk_pll_setup(struct device_node
*np
)
527 of_at91_clk_pll_setup(np
, &sama5d3_pll_layout
);
529 CLK_OF_DECLARE(sama5d3_clk_pll
, "atmel,sama5d3-clk-pll",
530 of_sama5d3_clk_pll_setup
);