WIP FPC-III support
[linux/fpc-iii.git] / drivers / clk / at91 / clk-pll.c
blob6ed986d3eee08eba71d0f6021e775c63c172098b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 */
6 #include <linux/clk-provider.h>
7 #include <linux/clkdev.h>
8 #include <linux/clk/at91_pmc.h>
9 #include <linux/of.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/regmap.h>
13 #include "pmc.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) & \
21 (layout)->mul_mask)
22 #define PLL_MUL_MIN 2
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
30 #define PLL_MAX_ID 1
32 #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
34 struct clk_pll {
35 struct clk_hw hw;
36 struct regmap *regmap;
37 u8 id;
38 u8 div;
39 u8 range;
40 u16 mul;
41 const struct clk_pll_layout *layout;
42 const struct clk_pll_characteristics *characteristics;
45 static inline bool clk_pll_ready(struct regmap *regmap, int id)
47 unsigned int status;
49 regmap_read(regmap, AT91_PMC_SR, &status);
51 return status & PLL_STATUS_MASK(id) ? 1 : 0;
54 static int clk_pll_prepare(struct clk_hw *hw)
56 struct clk_pll *pll = to_clk_pll(hw);
57 struct regmap *regmap = pll->regmap;
58 const struct clk_pll_layout *layout = pll->layout;
59 const struct clk_pll_characteristics *characteristics =
60 pll->characteristics;
61 u8 id = pll->id;
62 u32 mask = PLL_STATUS_MASK(id);
63 int offset = PLL_REG(id);
64 u8 out = 0;
65 unsigned int pllr;
66 unsigned int status;
67 u8 div;
68 u16 mul;
70 regmap_read(regmap, offset, &pllr);
71 div = PLL_DIV(pllr);
72 mul = PLL_MUL(pllr, layout);
74 regmap_read(regmap, AT91_PMC_SR, &status);
75 if ((status & mask) &&
76 (div == pll->div && mul == pll->mul))
77 return 0;
79 if (characteristics->out)
80 out = characteristics->out[pll->range];
82 if (characteristics->icpll)
83 regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
84 characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
86 regmap_update_bits(regmap, offset, layout->pllr_mask,
87 pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
88 (out << PLL_OUT_SHIFT) |
89 ((pll->mul & layout->mul_mask) << layout->mul_shift));
91 while (!clk_pll_ready(regmap, pll->id))
92 cpu_relax();
94 return 0;
97 static int clk_pll_is_prepared(struct clk_hw *hw)
99 struct clk_pll *pll = to_clk_pll(hw);
101 return clk_pll_ready(pll->regmap, pll->id);
104 static void clk_pll_unprepare(struct clk_hw *hw)
106 struct clk_pll *pll = to_clk_pll(hw);
107 unsigned int mask = pll->layout->pllr_mask;
109 regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
112 static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
113 unsigned long parent_rate)
115 struct clk_pll *pll = to_clk_pll(hw);
117 if (!pll->div || !pll->mul)
118 return 0;
120 return (parent_rate / pll->div) * (pll->mul + 1);
123 static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
124 unsigned long parent_rate,
125 u32 *div, u32 *mul,
126 u32 *index) {
127 const struct clk_pll_layout *layout = pll->layout;
128 const struct clk_pll_characteristics *characteristics =
129 pll->characteristics;
130 unsigned long bestremainder = ULONG_MAX;
131 unsigned long maxdiv, mindiv, tmpdiv;
132 long bestrate = -ERANGE;
133 unsigned long bestdiv;
134 unsigned long bestmul;
135 int i = 0;
137 /* Check if parent_rate is a valid input rate */
138 if (parent_rate < characteristics->input.min)
139 return -ERANGE;
142 * Calculate minimum divider based on the minimum multiplier, the
143 * parent_rate and the requested rate.
144 * Should always be 2 according to the input and output characteristics
145 * of the PLL blocks.
147 mindiv = (parent_rate * PLL_MUL_MIN) / rate;
148 if (!mindiv)
149 mindiv = 1;
151 if (parent_rate > characteristics->input.max) {
152 tmpdiv = DIV_ROUND_UP(parent_rate, characteristics->input.max);
153 if (tmpdiv > PLL_DIV_MAX)
154 return -ERANGE;
156 if (tmpdiv > mindiv)
157 mindiv = tmpdiv;
161 * Calculate the maximum divider which is limited by PLL register
162 * layout (limited by the MUL or DIV field size).
164 maxdiv = DIV_ROUND_UP(parent_rate * PLL_MUL_MAX(layout), rate);
165 if (maxdiv > PLL_DIV_MAX)
166 maxdiv = PLL_DIV_MAX;
169 * Iterate over the acceptable divider values to find the best
170 * divider/multiplier pair (the one that generates the closest
171 * rate to the requested one).
173 for (tmpdiv = mindiv; tmpdiv <= maxdiv; tmpdiv++) {
174 unsigned long remainder;
175 unsigned long tmprate;
176 unsigned long tmpmul;
179 * Calculate the multiplier associated with the current
180 * divider that provide the closest rate to the requested one.
182 tmpmul = DIV_ROUND_CLOSEST(rate, parent_rate / tmpdiv);
183 tmprate = (parent_rate / tmpdiv) * tmpmul;
184 if (tmprate > rate)
185 remainder = tmprate - rate;
186 else
187 remainder = rate - tmprate;
190 * Compare the remainder with the best remainder found until
191 * now and elect a new best multiplier/divider pair if the
192 * current remainder is smaller than the best one.
194 if (remainder < bestremainder) {
195 bestremainder = remainder;
196 bestdiv = tmpdiv;
197 bestmul = tmpmul;
198 bestrate = tmprate;
202 * We've found a perfect match!
203 * Stop searching now and use this multiplier/divider pair.
205 if (!remainder)
206 break;
209 /* We haven't found any multiplier/divider pair => return -ERANGE */
210 if (bestrate < 0)
211 return bestrate;
213 /* Check if bestrate is a valid output rate */
214 for (i = 0; i < characteristics->num_output; i++) {
215 if (bestrate >= characteristics->output[i].min &&
216 bestrate <= characteristics->output[i].max)
217 break;
220 if (i >= characteristics->num_output)
221 return -ERANGE;
223 if (div)
224 *div = bestdiv;
225 if (mul)
226 *mul = bestmul - 1;
227 if (index)
228 *index = i;
230 return bestrate;
233 static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
234 unsigned long *parent_rate)
236 struct clk_pll *pll = to_clk_pll(hw);
238 return clk_pll_get_best_div_mul(pll, rate, *parent_rate,
239 NULL, NULL, NULL);
242 static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
243 unsigned long parent_rate)
245 struct clk_pll *pll = to_clk_pll(hw);
246 long ret;
247 u32 div;
248 u32 mul;
249 u32 index;
251 ret = clk_pll_get_best_div_mul(pll, rate, parent_rate,
252 &div, &mul, &index);
253 if (ret < 0)
254 return ret;
256 pll->range = index;
257 pll->div = div;
258 pll->mul = mul;
260 return 0;
263 static const struct clk_ops pll_ops = {
264 .prepare = clk_pll_prepare,
265 .unprepare = clk_pll_unprepare,
266 .is_prepared = clk_pll_is_prepared,
267 .recalc_rate = clk_pll_recalc_rate,
268 .round_rate = clk_pll_round_rate,
269 .set_rate = clk_pll_set_rate,
272 struct clk_hw * __init
273 at91_clk_register_pll(struct regmap *regmap, const char *name,
274 const char *parent_name, u8 id,
275 const struct clk_pll_layout *layout,
276 const struct clk_pll_characteristics *characteristics)
278 struct clk_pll *pll;
279 struct clk_hw *hw;
280 struct clk_init_data init;
281 int offset = PLL_REG(id);
282 unsigned int pllr;
283 int ret;
285 if (id > PLL_MAX_ID)
286 return ERR_PTR(-EINVAL);
288 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
289 if (!pll)
290 return ERR_PTR(-ENOMEM);
292 init.name = name;
293 init.ops = &pll_ops;
294 init.parent_names = &parent_name;
295 init.num_parents = 1;
296 init.flags = CLK_SET_RATE_GATE;
298 pll->id = id;
299 pll->hw.init = &init;
300 pll->layout = layout;
301 pll->characteristics = characteristics;
302 pll->regmap = regmap;
303 regmap_read(regmap, offset, &pllr);
304 pll->div = PLL_DIV(pllr);
305 pll->mul = PLL_MUL(pllr, layout);
307 hw = &pll->hw;
308 ret = clk_hw_register(NULL, &pll->hw);
309 if (ret) {
310 kfree(pll);
311 hw = ERR_PTR(ret);
314 return hw;
318 const struct clk_pll_layout at91rm9200_pll_layout = {
319 .pllr_mask = 0x7FFFFFF,
320 .mul_shift = 16,
321 .mul_mask = 0x7FF,
324 const struct clk_pll_layout at91sam9g45_pll_layout = {
325 .pllr_mask = 0xFFFFFF,
326 .mul_shift = 16,
327 .mul_mask = 0xFF,
330 const struct clk_pll_layout at91sam9g20_pllb_layout = {
331 .pllr_mask = 0x3FFFFF,
332 .mul_shift = 16,
333 .mul_mask = 0x3F,
336 const struct clk_pll_layout sama5d3_pll_layout = {
337 .pllr_mask = 0x1FFFFFF,
338 .mul_shift = 18,
339 .mul_mask = 0x7F,