libceph: clear r_req_lru_item in __unregister_linger_request()
[linux/fpc-iii.git] / arch / arm / mach-imx / clk-pllv3.c
blob57de74da0acfe1aac907e1aa1d5424fffd48530d
1 /*
2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright 2012 Linaro Ltd.
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/io.h>
17 #include <linux/slab.h>
18 #include <linux/jiffies.h>
19 #include <linux/err.h>
20 #include "clk.h"
22 #define PLL_NUM_OFFSET 0x10
23 #define PLL_DENOM_OFFSET 0x20
25 #define BM_PLL_POWER (0x1 << 12)
26 #define BM_PLL_LOCK (0x1 << 31)
28 /**
29 * struct clk_pllv3 - IMX PLL clock version 3
30 * @clk_hw: clock source
31 * @base: base address of PLL registers
32 * @powerup_set: set POWER bit to power up the PLL
33 * @div_mask: mask of divider bits
35 * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
36 * is actually a multiplier, and always sits at bit 0.
38 struct clk_pllv3 {
39 struct clk_hw hw;
40 void __iomem *base;
41 bool powerup_set;
42 u32 div_mask;
45 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
47 static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
49 unsigned long timeout = jiffies + msecs_to_jiffies(10);
50 u32 val = readl_relaxed(pll->base) & BM_PLL_POWER;
52 /* No need to wait for lock when pll is not powered up */
53 if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
54 return 0;
56 /* Wait for PLL to lock */
57 do {
58 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
59 break;
60 if (time_after(jiffies, timeout))
61 break;
62 usleep_range(50, 500);
63 } while (1);
65 return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
68 static int clk_pllv3_prepare(struct clk_hw *hw)
70 struct clk_pllv3 *pll = to_clk_pllv3(hw);
71 u32 val;
72 int ret;
74 val = readl_relaxed(pll->base);
75 if (pll->powerup_set)
76 val |= BM_PLL_POWER;
77 else
78 val &= ~BM_PLL_POWER;
79 writel_relaxed(val, pll->base);
81 ret = clk_pllv3_wait_lock(pll);
82 if (ret)
83 return ret;
85 return 0;
88 static void clk_pllv3_unprepare(struct clk_hw *hw)
90 struct clk_pllv3 *pll = to_clk_pllv3(hw);
91 u32 val;
93 val = readl_relaxed(pll->base);
94 if (pll->powerup_set)
95 val &= ~BM_PLL_POWER;
96 else
97 val |= BM_PLL_POWER;
98 writel_relaxed(val, pll->base);
101 static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
102 unsigned long parent_rate)
104 struct clk_pllv3 *pll = to_clk_pllv3(hw);
105 u32 div = readl_relaxed(pll->base) & pll->div_mask;
107 return (div == 1) ? parent_rate * 22 : parent_rate * 20;
110 static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
111 unsigned long *prate)
113 unsigned long parent_rate = *prate;
115 return (rate >= parent_rate * 22) ? parent_rate * 22 :
116 parent_rate * 20;
119 static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
120 unsigned long parent_rate)
122 struct clk_pllv3 *pll = to_clk_pllv3(hw);
123 u32 val, div;
125 if (rate == parent_rate * 22)
126 div = 1;
127 else if (rate == parent_rate * 20)
128 div = 0;
129 else
130 return -EINVAL;
132 val = readl_relaxed(pll->base);
133 val &= ~pll->div_mask;
134 val |= div;
135 writel_relaxed(val, pll->base);
137 return clk_pllv3_wait_lock(pll);
140 static const struct clk_ops clk_pllv3_ops = {
141 .prepare = clk_pllv3_prepare,
142 .unprepare = clk_pllv3_unprepare,
143 .recalc_rate = clk_pllv3_recalc_rate,
144 .round_rate = clk_pllv3_round_rate,
145 .set_rate = clk_pllv3_set_rate,
148 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
149 unsigned long parent_rate)
151 struct clk_pllv3 *pll = to_clk_pllv3(hw);
152 u32 div = readl_relaxed(pll->base) & pll->div_mask;
154 return parent_rate * div / 2;
157 static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
158 unsigned long *prate)
160 unsigned long parent_rate = *prate;
161 unsigned long min_rate = parent_rate * 54 / 2;
162 unsigned long max_rate = parent_rate * 108 / 2;
163 u32 div;
165 if (rate > max_rate)
166 rate = max_rate;
167 else if (rate < min_rate)
168 rate = min_rate;
169 div = rate * 2 / parent_rate;
171 return parent_rate * div / 2;
174 static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
175 unsigned long parent_rate)
177 struct clk_pllv3 *pll = to_clk_pllv3(hw);
178 unsigned long min_rate = parent_rate * 54 / 2;
179 unsigned long max_rate = parent_rate * 108 / 2;
180 u32 val, div;
182 if (rate < min_rate || rate > max_rate)
183 return -EINVAL;
185 div = rate * 2 / parent_rate;
186 val = readl_relaxed(pll->base);
187 val &= ~pll->div_mask;
188 val |= div;
189 writel_relaxed(val, pll->base);
191 return clk_pllv3_wait_lock(pll);
194 static const struct clk_ops clk_pllv3_sys_ops = {
195 .prepare = clk_pllv3_prepare,
196 .unprepare = clk_pllv3_unprepare,
197 .recalc_rate = clk_pllv3_sys_recalc_rate,
198 .round_rate = clk_pllv3_sys_round_rate,
199 .set_rate = clk_pllv3_sys_set_rate,
202 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
203 unsigned long parent_rate)
205 struct clk_pllv3 *pll = to_clk_pllv3(hw);
206 u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
207 u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
208 u32 div = readl_relaxed(pll->base) & pll->div_mask;
210 return (parent_rate * div) + ((parent_rate / mfd) * mfn);
213 static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
214 unsigned long *prate)
216 unsigned long parent_rate = *prate;
217 unsigned long min_rate = parent_rate * 27;
218 unsigned long max_rate = parent_rate * 54;
219 u32 div;
220 u32 mfn, mfd = 1000000;
221 s64 temp64;
223 if (rate > max_rate)
224 rate = max_rate;
225 else if (rate < min_rate)
226 rate = min_rate;
228 div = rate / parent_rate;
229 temp64 = (u64) (rate - div * parent_rate);
230 temp64 *= mfd;
231 do_div(temp64, parent_rate);
232 mfn = temp64;
234 return parent_rate * div + parent_rate / mfd * mfn;
237 static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
238 unsigned long parent_rate)
240 struct clk_pllv3 *pll = to_clk_pllv3(hw);
241 unsigned long min_rate = parent_rate * 27;
242 unsigned long max_rate = parent_rate * 54;
243 u32 val, div;
244 u32 mfn, mfd = 1000000;
245 s64 temp64;
247 if (rate < min_rate || rate > max_rate)
248 return -EINVAL;
250 div = rate / parent_rate;
251 temp64 = (u64) (rate - div * parent_rate);
252 temp64 *= mfd;
253 do_div(temp64, parent_rate);
254 mfn = temp64;
256 val = readl_relaxed(pll->base);
257 val &= ~pll->div_mask;
258 val |= div;
259 writel_relaxed(val, pll->base);
260 writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
261 writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
263 return clk_pllv3_wait_lock(pll);
266 static const struct clk_ops clk_pllv3_av_ops = {
267 .prepare = clk_pllv3_prepare,
268 .unprepare = clk_pllv3_unprepare,
269 .recalc_rate = clk_pllv3_av_recalc_rate,
270 .round_rate = clk_pllv3_av_round_rate,
271 .set_rate = clk_pllv3_av_set_rate,
274 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
275 unsigned long parent_rate)
277 return 500000000;
280 static const struct clk_ops clk_pllv3_enet_ops = {
281 .prepare = clk_pllv3_prepare,
282 .unprepare = clk_pllv3_unprepare,
283 .recalc_rate = clk_pllv3_enet_recalc_rate,
286 struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
287 const char *parent_name, void __iomem *base,
288 u32 div_mask)
290 struct clk_pllv3 *pll;
291 const struct clk_ops *ops;
292 struct clk *clk;
293 struct clk_init_data init;
295 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
296 if (!pll)
297 return ERR_PTR(-ENOMEM);
299 switch (type) {
300 case IMX_PLLV3_SYS:
301 ops = &clk_pllv3_sys_ops;
302 break;
303 case IMX_PLLV3_USB:
304 ops = &clk_pllv3_ops;
305 pll->powerup_set = true;
306 break;
307 case IMX_PLLV3_AV:
308 ops = &clk_pllv3_av_ops;
309 break;
310 case IMX_PLLV3_ENET:
311 ops = &clk_pllv3_enet_ops;
312 break;
313 default:
314 ops = &clk_pllv3_ops;
316 pll->base = base;
317 pll->div_mask = div_mask;
319 init.name = name;
320 init.ops = ops;
321 init.flags = 0;
322 init.parent_names = &parent_name;
323 init.num_parents = 1;
325 pll->hw.init = &init;
327 clk = clk_register(NULL, &pll->hw);
328 if (IS_ERR(clk))
329 kfree(pll);
331 return clk;