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.
7 * Copyright (c) 2016 AmLogic, Inc.
8 * Author: Michael Turquette <mturquette@baylibre.com>
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.
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.
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
27 * Copyright (c) 2016 AmLogic, Inc.
28 * Author: Michael Turquette <mturquette@baylibre.com>
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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
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.
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.
58 * MultiPhase Locked Loops are outputs from a PLL with additional frequency
59 * scaling capabilities. MPLL rates are calculated as:
61 * f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
64 #include <linux/clk-provider.h>
73 #define to_meson_clk_mpll(_hw) container_of(_hw, struct meson_clk_mpll, hw)
75 static unsigned long rate_from_params(unsigned long parent_rate,
79 return (parent_rate * SDM_DEN) / ((SDM_DEN * n2) + sdm);
82 static void params_from_rate(unsigned long requested_rate,
83 unsigned long parent_rate,
87 uint64_t div = parent_rate;
88 unsigned long rem = do_div(div, requested_rate);
93 } else if (div > N2_MAX) {
98 *sdm = DIV_ROUND_UP(rem * SDM_DEN, requested_rate);
101 else if (*sdm > SDM_MAX)
106 static unsigned long mpll_recalc_rate(struct clk_hw *hw,
107 unsigned long parent_rate)
109 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
111 unsigned long reg, sdm, n2;
114 reg = readl(mpll->base + p->reg_off);
115 sdm = PARM_GET(p->width, p->shift, reg);
118 reg = readl(mpll->base + p->reg_off);
119 n2 = PARM_GET(p->width, p->shift, reg);
121 return rate_from_params(parent_rate, sdm, n2);
124 static long mpll_round_rate(struct clk_hw *hw,
126 unsigned long *parent_rate)
128 unsigned long sdm, n2;
130 params_from_rate(rate, *parent_rate, &sdm, &n2);
131 return rate_from_params(*parent_rate, sdm, n2);
134 static int mpll_set_rate(struct clk_hw *hw,
136 unsigned long parent_rate)
138 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
140 unsigned long reg, sdm, n2;
141 unsigned long flags = 0;
143 params_from_rate(rate, parent_rate, &sdm, &n2);
146 spin_lock_irqsave(mpll->lock, flags);
148 __acquire(mpll->lock);
151 reg = readl(mpll->base + p->reg_off);
152 reg = PARM_SET(p->width, p->shift, reg, sdm);
153 writel(reg, mpll->base + p->reg_off);
156 reg = readl(mpll->base + p->reg_off);
157 reg = PARM_SET(p->width, p->shift, reg, 1);
158 writel(reg, mpll->base + p->reg_off);
161 reg = readl(mpll->base + p->reg_off);
162 reg = PARM_SET(p->width, p->shift, reg, n2);
163 writel(reg, mpll->base + p->reg_off);
166 spin_unlock_irqrestore(mpll->lock, flags);
168 __release(mpll->lock);
173 static void mpll_enable_core(struct clk_hw *hw, int enable)
175 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
178 unsigned long flags = 0;
181 spin_lock_irqsave(mpll->lock, flags);
183 __acquire(mpll->lock);
186 reg = readl(mpll->base + p->reg_off);
187 reg = PARM_SET(p->width, p->shift, reg, enable ? 1 : 0);
188 writel(reg, mpll->base + p->reg_off);
191 spin_unlock_irqrestore(mpll->lock, flags);
193 __release(mpll->lock);
197 static int mpll_enable(struct clk_hw *hw)
199 mpll_enable_core(hw, 1);
204 static void mpll_disable(struct clk_hw *hw)
206 mpll_enable_core(hw, 0);
209 static int mpll_is_enabled(struct clk_hw *hw)
211 struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
217 reg = readl(mpll->base + p->reg_off);
218 en = PARM_GET(p->width, p->shift, reg);
223 const struct clk_ops meson_clk_mpll_ro_ops = {
224 .recalc_rate = mpll_recalc_rate,
225 .round_rate = mpll_round_rate,
226 .is_enabled = mpll_is_enabled,
229 const struct clk_ops meson_clk_mpll_ops = {
230 .recalc_rate = mpll_recalc_rate,
231 .round_rate = mpll_round_rate,
232 .set_rate = mpll_set_rate,
233 .enable = mpll_enable,
234 .disable = mpll_disable,
235 .is_enabled = mpll_is_enabled,