mm/zpool: use prefixed module loading
[linux-2.6/btrfs-unstable.git] / drivers / clk / zynq / pll.c
blobcec97596fe65988ff473e7f6d08302a7f68ba375
1 /*
2 * Zynq PLL driver
4 * Copyright (C) 2013 Xilinx
6 * Sören Brinkmann <soren.brinkmann@xilinx.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License v2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/clk/zynq.h>
22 #include <linux/clk-provider.h>
23 #include <linux/slab.h>
24 #include <linux/io.h>
26 /**
27 * struct zynq_pll
28 * @hw: Handle between common and hardware-specific interfaces
29 * @pll_ctrl: PLL control register
30 * @pll_status: PLL status register
31 * @lock: Register lock
32 * @lockbit: Indicates the associated PLL_LOCKED bit in the PLL status
33 * register.
35 struct zynq_pll {
36 struct clk_hw hw;
37 void __iomem *pll_ctrl;
38 void __iomem *pll_status;
39 spinlock_t *lock;
40 u8 lockbit;
42 #define to_zynq_pll(_hw) container_of(_hw, struct zynq_pll, hw)
44 /* Register bitfield defines */
45 #define PLLCTRL_FBDIV_MASK 0x7f000
46 #define PLLCTRL_FBDIV_SHIFT 12
47 #define PLLCTRL_BPQUAL_MASK (1 << 3)
48 #define PLLCTRL_PWRDWN_MASK 2
49 #define PLLCTRL_PWRDWN_SHIFT 1
50 #define PLLCTRL_RESET_MASK 1
51 #define PLLCTRL_RESET_SHIFT 0
53 #define PLL_FBDIV_MIN 13
54 #define PLL_FBDIV_MAX 66
56 /**
57 * zynq_pll_round_rate() - Round a clock frequency
58 * @hw: Handle between common and hardware-specific interfaces
59 * @rate: Desired clock frequency
60 * @prate: Clock frequency of parent clock
61 * Returns frequency closest to @rate the hardware can generate.
63 static long zynq_pll_round_rate(struct clk_hw *hw, unsigned long rate,
64 unsigned long *prate)
66 u32 fbdiv;
68 fbdiv = DIV_ROUND_CLOSEST(rate, *prate);
69 if (fbdiv < PLL_FBDIV_MIN)
70 fbdiv = PLL_FBDIV_MIN;
71 else if (fbdiv > PLL_FBDIV_MAX)
72 fbdiv = PLL_FBDIV_MAX;
74 return *prate * fbdiv;
77 /**
78 * zynq_pll_recalc_rate() - Recalculate clock frequency
79 * @hw: Handle between common and hardware-specific interfaces
80 * @parent_rate: Clock frequency of parent clock
81 * Returns current clock frequency.
83 static unsigned long zynq_pll_recalc_rate(struct clk_hw *hw,
84 unsigned long parent_rate)
86 struct zynq_pll *clk = to_zynq_pll(hw);
87 u32 fbdiv;
90 * makes probably sense to redundantly save fbdiv in the struct
91 * zynq_pll to save the IO access.
93 fbdiv = (clk_readl(clk->pll_ctrl) & PLLCTRL_FBDIV_MASK) >>
94 PLLCTRL_FBDIV_SHIFT;
96 return parent_rate * fbdiv;
99 /**
100 * zynq_pll_is_enabled - Check if a clock is enabled
101 * @hw: Handle between common and hardware-specific interfaces
102 * Returns 1 if the clock is enabled, 0 otherwise.
104 * Not sure this is a good idea, but since disabled means bypassed for
105 * this clock implementation we say we are always enabled.
107 static int zynq_pll_is_enabled(struct clk_hw *hw)
109 unsigned long flags = 0;
110 u32 reg;
111 struct zynq_pll *clk = to_zynq_pll(hw);
113 spin_lock_irqsave(clk->lock, flags);
115 reg = clk_readl(clk->pll_ctrl);
117 spin_unlock_irqrestore(clk->lock, flags);
119 return !(reg & (PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK));
123 * zynq_pll_enable - Enable clock
124 * @hw: Handle between common and hardware-specific interfaces
125 * Returns 0 on success
127 static int zynq_pll_enable(struct clk_hw *hw)
129 unsigned long flags = 0;
130 u32 reg;
131 struct zynq_pll *clk = to_zynq_pll(hw);
133 if (zynq_pll_is_enabled(hw))
134 return 0;
136 pr_info("PLL: enable\n");
138 /* Power up PLL and wait for lock */
139 spin_lock_irqsave(clk->lock, flags);
141 reg = clk_readl(clk->pll_ctrl);
142 reg &= ~(PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK);
143 clk_writel(reg, clk->pll_ctrl);
144 while (!(clk_readl(clk->pll_status) & (1 << clk->lockbit)))
147 spin_unlock_irqrestore(clk->lock, flags);
149 return 0;
153 * zynq_pll_disable - Disable clock
154 * @hw: Handle between common and hardware-specific interfaces
155 * Returns 0 on success
157 static void zynq_pll_disable(struct clk_hw *hw)
159 unsigned long flags = 0;
160 u32 reg;
161 struct zynq_pll *clk = to_zynq_pll(hw);
163 if (!zynq_pll_is_enabled(hw))
164 return;
166 pr_info("PLL: shutdown\n");
168 /* shut down PLL */
169 spin_lock_irqsave(clk->lock, flags);
171 reg = clk_readl(clk->pll_ctrl);
172 reg |= PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK;
173 clk_writel(reg, clk->pll_ctrl);
175 spin_unlock_irqrestore(clk->lock, flags);
178 static const struct clk_ops zynq_pll_ops = {
179 .enable = zynq_pll_enable,
180 .disable = zynq_pll_disable,
181 .is_enabled = zynq_pll_is_enabled,
182 .round_rate = zynq_pll_round_rate,
183 .recalc_rate = zynq_pll_recalc_rate
187 * clk_register_zynq_pll() - Register PLL with the clock framework
188 * @name PLL name
189 * @parent Parent clock name
190 * @pll_ctrl Pointer to PLL control register
191 * @pll_status Pointer to PLL status register
192 * @lock_index Bit index to this PLL's lock status bit in @pll_status
193 * @lock Register lock
194 * Returns handle to the registered clock.
196 struct clk *clk_register_zynq_pll(const char *name, const char *parent,
197 void __iomem *pll_ctrl, void __iomem *pll_status, u8 lock_index,
198 spinlock_t *lock)
200 struct zynq_pll *pll;
201 struct clk *clk;
202 u32 reg;
203 const char *parent_arr[1] = {parent};
204 unsigned long flags = 0;
205 struct clk_init_data initd = {
206 .name = name,
207 .parent_names = parent_arr,
208 .ops = &zynq_pll_ops,
209 .num_parents = 1,
210 .flags = 0
213 pll = kmalloc(sizeof(*pll), GFP_KERNEL);
214 if (!pll) {
215 pr_err("%s: Could not allocate Zynq PLL clk.\n", __func__);
216 return ERR_PTR(-ENOMEM);
219 /* Populate the struct */
220 pll->hw.init = &initd;
221 pll->pll_ctrl = pll_ctrl;
222 pll->pll_status = pll_status;
223 pll->lockbit = lock_index;
224 pll->lock = lock;
226 spin_lock_irqsave(pll->lock, flags);
228 reg = clk_readl(pll->pll_ctrl);
229 reg &= ~PLLCTRL_BPQUAL_MASK;
230 clk_writel(reg, pll->pll_ctrl);
232 spin_unlock_irqrestore(pll->lock, flags);
234 clk = clk_register(NULL, &pll->hw);
235 if (WARN_ON(IS_ERR(clk)))
236 goto free_pll;
238 return clk;
240 free_pll:
241 kfree(pll);
243 return clk;