1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
6 #include <linux/clk-provider.h>
7 #include <linux/clkdev.h>
8 #include <linux/clk/at91_pmc.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/regmap.h>
15 #define PLL_STATUS_MASK(id) (1 << (1 + (id)))
16 #define PLL_REG(id) (AT91_CKGR_PLLAR + ((id) * 4))
17 #define PLL_DIV_MASK 0xff
18 #define PLL_DIV_MAX PLL_DIV_MASK
19 #define PLL_DIV(reg) ((reg) & PLL_DIV_MASK)
20 #define PLL_MUL(reg, layout) (((reg) >> (layout)->mul_shift) & \
23 #define PLL_MUL_MASK(layout) ((layout)->mul_mask)
24 #define PLL_MUL_MAX(layout) (PLL_MUL_MASK(layout) + 1)
25 #define PLL_ICPR_SHIFT(id) ((id) * 16)
26 #define PLL_ICPR_MASK(id) (0xffff << PLL_ICPR_SHIFT(id))
27 #define PLL_MAX_COUNT 0x3f
28 #define PLL_COUNT_SHIFT 8
29 #define PLL_OUT_SHIFT 14
32 #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
36 struct regmap *regmap;
41 const struct clk_pll_layout *layout;
42 const struct clk_pll_characteristics *characteristics;
43 struct at91_clk_pms pms;
46 static inline bool clk_pll_ready(struct regmap *regmap, int id)
50 regmap_read(regmap, AT91_PMC_SR, &status);
52 return status & PLL_STATUS_MASK(id) ? 1 : 0;
55 static int clk_pll_prepare(struct clk_hw *hw)
57 struct clk_pll *pll = to_clk_pll(hw);
58 struct regmap *regmap = pll->regmap;
59 const struct clk_pll_layout *layout = pll->layout;
60 const struct clk_pll_characteristics *characteristics =
63 u32 mask = PLL_STATUS_MASK(id);
64 int offset = PLL_REG(id);
71 regmap_read(regmap, offset, &pllr);
73 mul = PLL_MUL(pllr, layout);
75 regmap_read(regmap, AT91_PMC_SR, &status);
76 if ((status & mask) &&
77 (div == pll->div && mul == pll->mul))
80 if (characteristics->out)
81 out = characteristics->out[pll->range];
83 if (characteristics->icpll)
84 regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
85 characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
87 regmap_update_bits(regmap, offset, layout->pllr_mask,
88 pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
89 (out << PLL_OUT_SHIFT) |
90 ((pll->mul & layout->mul_mask) << layout->mul_shift));
92 while (!clk_pll_ready(regmap, pll->id))
98 static int clk_pll_is_prepared(struct clk_hw *hw)
100 struct clk_pll *pll = to_clk_pll(hw);
102 return clk_pll_ready(pll->regmap, pll->id);
105 static void clk_pll_unprepare(struct clk_hw *hw)
107 struct clk_pll *pll = to_clk_pll(hw);
108 unsigned int mask = pll->layout->pllr_mask;
110 regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
113 static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
114 unsigned long parent_rate)
116 struct clk_pll *pll = to_clk_pll(hw);
118 if (!pll->div || !pll->mul)
121 return (parent_rate / pll->div) * (pll->mul + 1);
124 static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
125 unsigned long parent_rate,
128 const struct clk_pll_layout *layout = pll->layout;
129 const struct clk_pll_characteristics *characteristics =
130 pll->characteristics;
131 unsigned long bestremainder = ULONG_MAX;
132 unsigned long maxdiv, mindiv, tmpdiv;
133 long bestrate = -ERANGE;
134 unsigned long bestdiv;
135 unsigned long bestmul;
138 /* Check if parent_rate is a valid input rate */
139 if (parent_rate < characteristics->input.min)
143 * Calculate minimum divider based on the minimum multiplier, the
144 * parent_rate and the requested rate.
145 * Should always be 2 according to the input and output characteristics
148 mindiv = (parent_rate * PLL_MUL_MIN) / rate;
152 if (parent_rate > characteristics->input.max) {
153 tmpdiv = DIV_ROUND_UP(parent_rate, characteristics->input.max);
154 if (tmpdiv > PLL_DIV_MAX)
162 * Calculate the maximum divider which is limited by PLL register
163 * layout (limited by the MUL or DIV field size).
165 maxdiv = DIV_ROUND_UP(parent_rate * PLL_MUL_MAX(layout), rate);
166 if (maxdiv > PLL_DIV_MAX)
167 maxdiv = PLL_DIV_MAX;
170 * Iterate over the acceptable divider values to find the best
171 * divider/multiplier pair (the one that generates the closest
172 * rate to the requested one).
174 for (tmpdiv = mindiv; tmpdiv <= maxdiv; tmpdiv++) {
175 unsigned long remainder;
176 unsigned long tmprate;
177 unsigned long tmpmul;
180 * Calculate the multiplier associated with the current
181 * divider that provide the closest rate to the requested one.
183 tmpmul = DIV_ROUND_CLOSEST(rate, parent_rate / tmpdiv);
184 tmprate = (parent_rate / tmpdiv) * tmpmul;
186 remainder = tmprate - rate;
188 remainder = rate - tmprate;
191 * Compare the remainder with the best remainder found until
192 * now and elect a new best multiplier/divider pair if the
193 * current remainder is smaller than the best one.
195 if (remainder < bestremainder) {
196 bestremainder = remainder;
203 * We've found a perfect match!
204 * Stop searching now and use this multiplier/divider pair.
210 /* We haven't found any multiplier/divider pair => return -ERANGE */
214 /* Check if bestrate is a valid output rate */
215 for (i = 0; i < characteristics->num_output; i++) {
216 if (bestrate >= characteristics->output[i].min &&
217 bestrate <= characteristics->output[i].max)
221 if (i >= characteristics->num_output)
234 static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
235 unsigned long *parent_rate)
237 struct clk_pll *pll = to_clk_pll(hw);
239 return clk_pll_get_best_div_mul(pll, rate, *parent_rate,
243 static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
244 unsigned long parent_rate)
246 struct clk_pll *pll = to_clk_pll(hw);
252 ret = clk_pll_get_best_div_mul(pll, rate, parent_rate,
264 static int clk_pll_save_context(struct clk_hw *hw)
266 struct clk_pll *pll = to_clk_pll(hw);
267 struct clk_hw *parent_hw = clk_hw_get_parent(hw);
269 pll->pms.parent_rate = clk_hw_get_rate(parent_hw);
270 pll->pms.rate = clk_pll_recalc_rate(&pll->hw, pll->pms.parent_rate);
271 pll->pms.status = clk_pll_ready(pll->regmap, PLL_REG(pll->id));
276 static void clk_pll_restore_context(struct clk_hw *hw)
278 struct clk_pll *pll = to_clk_pll(hw);
279 unsigned long calc_rate;
280 unsigned int pllr, pllr_out, pllr_count;
283 if (pll->characteristics->out)
284 out = pll->characteristics->out[pll->range];
286 regmap_read(pll->regmap, PLL_REG(pll->id), &pllr);
288 calc_rate = (pll->pms.parent_rate / PLL_DIV(pllr)) *
289 (PLL_MUL(pllr, pll->layout) + 1);
290 pllr_count = (pllr >> PLL_COUNT_SHIFT) & PLL_MAX_COUNT;
291 pllr_out = (pllr >> PLL_OUT_SHIFT) & out;
293 if (pll->pms.rate != calc_rate ||
294 pll->pms.status != clk_pll_ready(pll->regmap, PLL_REG(pll->id)) ||
295 pllr_count != PLL_MAX_COUNT ||
296 (out && pllr_out != out))
297 pr_warn("PLLAR was not configured properly by firmware\n");
300 static const struct clk_ops pll_ops = {
301 .prepare = clk_pll_prepare,
302 .unprepare = clk_pll_unprepare,
303 .is_prepared = clk_pll_is_prepared,
304 .recalc_rate = clk_pll_recalc_rate,
305 .round_rate = clk_pll_round_rate,
306 .set_rate = clk_pll_set_rate,
307 .save_context = clk_pll_save_context,
308 .restore_context = clk_pll_restore_context,
311 struct clk_hw * __init
312 at91_clk_register_pll(struct regmap *regmap, const char *name,
313 const char *parent_name, u8 id,
314 const struct clk_pll_layout *layout,
315 const struct clk_pll_characteristics *characteristics)
319 struct clk_init_data init;
320 int offset = PLL_REG(id);
325 return ERR_PTR(-EINVAL);
327 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
329 return ERR_PTR(-ENOMEM);
333 init.parent_names = &parent_name;
334 init.num_parents = 1;
335 init.flags = CLK_SET_RATE_GATE;
338 pll->hw.init = &init;
339 pll->layout = layout;
340 pll->characteristics = characteristics;
341 pll->regmap = regmap;
342 regmap_read(regmap, offset, &pllr);
343 pll->div = PLL_DIV(pllr);
344 pll->mul = PLL_MUL(pllr, layout);
347 ret = clk_hw_register(NULL, &pll->hw);
357 const struct clk_pll_layout at91rm9200_pll_layout = {
358 .pllr_mask = 0x7FFFFFF,
363 const struct clk_pll_layout at91sam9g45_pll_layout = {
364 .pllr_mask = 0xFFFFFF,
369 const struct clk_pll_layout at91sam9g20_pllb_layout = {
370 .pllr_mask = 0x3FFFFF,
375 const struct clk_pll_layout sama5d3_pll_layout = {
376 .pllr_mask = 0x1FFFFFF,