clk: meson: remove obsolete comments
[linux-2.6-microblaze.git] / drivers / clk / meson / clk-mpll.c
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright (c) 2016 AmLogic, Inc.
8  * Author: Michael Turquette <mturquette@baylibre.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING
24  *
25  * BSD LICENSE
26  *
27  * Copyright (c) 2016 AmLogic, Inc.
28  * Author: Michael Turquette <mturquette@baylibre.com>
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  *
34  *   * Redistributions of source code must retain the above copyright
35  *     notice, this list of conditions and the following disclaimer.
36  *   * Redistributions in binary form must reproduce the above copyright
37  *     notice, this list of conditions and the following disclaimer in
38  *     the documentation and/or other materials provided with the
39  *     distribution.
40  *   * Neither the name of Intel Corporation nor the names of its
41  *     contributors may be used to endorse or promote products derived
42  *     from this software without specific prior written permission.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
45  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
46  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
48  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  */
56
57 /*
58  * MultiPhase Locked Loops are outputs from a PLL with additional frequency
59  * scaling capabilities. MPLL rates are calculated as:
60  *
61  * f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
62  */
63
64 #include <linux/clk-provider.h>
65 #include "clkc.h"
66
67 #define SDM_DEN 16384
68 #define N2_MIN  4
69 #define N2_MAX  511
70
71 #define to_meson_clk_mpll(_hw) container_of(_hw, struct meson_clk_mpll, hw)
72
73 static long rate_from_params(unsigned long parent_rate,
74                                       unsigned long sdm,
75                                       unsigned long n2)
76 {
77         unsigned long divisor = (SDM_DEN * n2) + sdm;
78
79         if (n2 < N2_MIN)
80                 return -EINVAL;
81
82         return DIV_ROUND_UP_ULL((u64)parent_rate * SDM_DEN, divisor);
83 }
84
85 static void params_from_rate(unsigned long requested_rate,
86                              unsigned long parent_rate,
87                              unsigned long *sdm,
88                              unsigned long *n2)
89 {
90         uint64_t div = parent_rate;
91         unsigned long rem = do_div(div, requested_rate);
92
93         if (div < N2_MIN) {
94                 *n2 = N2_MIN;
95                 *sdm = 0;
96         } else if (div > N2_MAX) {
97                 *n2 = N2_MAX;
98                 *sdm = SDM_DEN - 1;
99         } else {
100                 *n2 = div;
101                 *sdm = DIV_ROUND_UP_ULL((u64)rem * SDM_DEN, requested_rate);
102         }
103 }
104
105 static unsigned long mpll_recalc_rate(struct clk_hw *hw,
106                 unsigned long parent_rate)
107 {
108         struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
109         struct parm *p;
110         unsigned long reg, sdm, n2;
111         long rate;
112
113         p = &mpll->sdm;
114         reg = readl(mpll->base + p->reg_off);
115         sdm = PARM_GET(p->width, p->shift, reg);
116
117         p = &mpll->n2;
118         reg = readl(mpll->base + p->reg_off);
119         n2 = PARM_GET(p->width, p->shift, reg);
120
121         rate = rate_from_params(parent_rate, sdm, n2);
122         if (rate < 0)
123                 return 0;
124
125         return rate;
126 }
127
128 static long mpll_round_rate(struct clk_hw *hw,
129                             unsigned long rate,
130                             unsigned long *parent_rate)
131 {
132         unsigned long sdm, n2;
133
134         params_from_rate(rate, *parent_rate, &sdm, &n2);
135         return rate_from_params(*parent_rate, sdm, n2);
136 }
137
138 static int mpll_set_rate(struct clk_hw *hw,
139                          unsigned long rate,
140                          unsigned long parent_rate)
141 {
142         struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
143         struct parm *p;
144         unsigned long reg, sdm, n2;
145         unsigned long flags = 0;
146
147         params_from_rate(rate, parent_rate, &sdm, &n2);
148
149         if (mpll->lock)
150                 spin_lock_irqsave(mpll->lock, flags);
151         else
152                 __acquire(mpll->lock);
153
154         p = &mpll->sdm;
155         reg = readl(mpll->base + p->reg_off);
156         reg = PARM_SET(p->width, p->shift, reg, sdm);
157         writel(reg, mpll->base + p->reg_off);
158
159         p = &mpll->sdm_en;
160         reg = readl(mpll->base + p->reg_off);
161         reg = PARM_SET(p->width, p->shift, reg, 1);
162         writel(reg, mpll->base + p->reg_off);
163
164         p = &mpll->ssen;
165         if (p->width != 0) {
166                 reg = readl(mpll->base + p->reg_off);
167                 reg = PARM_SET(p->width, p->shift, reg, 1);
168                 writel(reg, mpll->base + p->reg_off);
169         }
170
171         p = &mpll->n2;
172         reg = readl(mpll->base + p->reg_off);
173         reg = PARM_SET(p->width, p->shift, reg, n2);
174         writel(reg, mpll->base + p->reg_off);
175
176         p = &mpll->misc;
177         if (p->width != 0) {
178                 reg = readl(mpll->base + p->reg_off);
179                 reg = PARM_SET(p->width, p->shift, reg, 1);
180                 writel(reg, mpll->base + p->reg_off);
181         }
182
183         if (mpll->lock)
184                 spin_unlock_irqrestore(mpll->lock, flags);
185         else
186                 __release(mpll->lock);
187
188         return 0;
189 }
190
191 static void mpll_enable_core(struct clk_hw *hw, int enable)
192 {
193         struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
194         struct parm *p;
195         unsigned long reg;
196         unsigned long flags = 0;
197
198         if (mpll->lock)
199                 spin_lock_irqsave(mpll->lock, flags);
200         else
201                 __acquire(mpll->lock);
202
203         p = &mpll->en;
204         reg = readl(mpll->base + p->reg_off);
205         reg = PARM_SET(p->width, p->shift, reg, enable ? 1 : 0);
206         writel(reg, mpll->base + p->reg_off);
207
208         if (mpll->lock)
209                 spin_unlock_irqrestore(mpll->lock, flags);
210         else
211                 __release(mpll->lock);
212 }
213
214
215 static int mpll_enable(struct clk_hw *hw)
216 {
217         mpll_enable_core(hw, 1);
218
219         return 0;
220 }
221
222 static void mpll_disable(struct clk_hw *hw)
223 {
224         mpll_enable_core(hw, 0);
225 }
226
227 static int mpll_is_enabled(struct clk_hw *hw)
228 {
229         struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
230         struct parm *p;
231         unsigned long reg;
232         int en;
233
234         p = &mpll->en;
235         reg = readl(mpll->base + p->reg_off);
236         en = PARM_GET(p->width, p->shift, reg);
237
238         return en;
239 }
240
241 const struct clk_ops meson_clk_mpll_ro_ops = {
242         .recalc_rate    = mpll_recalc_rate,
243         .round_rate     = mpll_round_rate,
244         .is_enabled     = mpll_is_enabled,
245 };
246
247 const struct clk_ops meson_clk_mpll_ops = {
248         .recalc_rate    = mpll_recalc_rate,
249         .round_rate     = mpll_round_rate,
250         .set_rate       = mpll_set_rate,
251         .enable         = mpll_enable,
252         .disable        = mpll_disable,
253         .is_enabled     = mpll_is_enabled,
254 };