2 * Copyright 2013 Emilio López
4 * Emilio López <emilio@elopez.com.ar>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk-provider.h>
18 #include <linux/clkdev.h>
19 #include <linux/of_address.h>
21 #include "clk-factors.h"
24 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
25 * MOD0 rate is calculated as follows
26 * rate = (parent_rate >> p) / (m + 1);
29 static void sun4i_a10_get_mod0_factors(u32 *freq, u32 parent_rate,
30 u8 *n, u8 *k, u8 *m, u8 *p)
34 /* These clocks can only divide, so we will never be able to achieve
35 * frequencies higher than the parent frequency */
36 if (*freq > parent_rate)
39 div = DIV_ROUND_UP(parent_rate, *freq);
43 else if (div / 2 < 16)
45 else if (div / 4 < 16)
50 calcm = DIV_ROUND_UP(div, 1 << calcp);
52 *freq = (parent_rate >> calcp) / calcm;
54 /* we were called to round the frequency, we can now return */
62 /* user manual says "n" but it's really "p" */
63 static struct clk_factors_config sun4i_a10_mod0_config = {
70 static const struct factors_data sun4i_a10_mod0_data __initconst = {
73 .table = &sun4i_a10_mod0_config,
74 .getter = sun4i_a10_get_mod0_factors,
77 static DEFINE_SPINLOCK(sun4i_a10_mod0_lock);
79 static void __init sun4i_a10_mod0_setup(struct device_node *node)
81 sunxi_factors_register(node, &sun4i_a10_mod0_data, &sun4i_a10_mod0_lock);
83 CLK_OF_DECLARE(sun4i_a10_mod0, "allwinner,sun4i-a10-mod0-clk", sun4i_a10_mod0_setup);
85 static DEFINE_SPINLOCK(sun5i_a13_mbus_lock);
87 static void __init sun5i_a13_mbus_setup(struct device_node *node)
89 struct clk *mbus = sunxi_factors_register(node, &sun4i_a10_mod0_data, &sun5i_a13_mbus_lock);
91 /* The MBUS clocks needs to be always enabled */
93 clk_prepare_enable(mbus);
95 CLK_OF_DECLARE(sun5i_a13_mbus, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup);
97 struct mmc_phase_data {
104 struct mmc_phase_data *data;
108 #define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw)
110 static int mmc_get_phase(struct clk_hw *hw)
112 struct clk *mmc, *mmc_parent, *clk = hw->clk;
113 struct mmc_phase *phase = to_mmc_phase(hw);
114 unsigned int mmc_rate, mmc_parent_rate;
119 value = readl(phase->reg);
120 delay = (value >> phase->data->offset) & 0x3;
125 /* Get the main MMC clock */
126 mmc = clk_get_parent(clk);
131 mmc_rate = clk_get_rate(mmc);
135 /* Now, get the MMC parent (most likely some PLL) */
136 mmc_parent = clk_get_parent(mmc);
141 mmc_parent_rate = clk_get_rate(mmc_parent);
142 if (!mmc_parent_rate)
145 /* Get MMC clock divider */
146 mmc_div = mmc_parent_rate / mmc_rate;
148 step = DIV_ROUND_CLOSEST(360, mmc_div);
152 static int mmc_set_phase(struct clk_hw *hw, int degrees)
154 struct clk *mmc, *mmc_parent, *clk = hw->clk;
155 struct mmc_phase *phase = to_mmc_phase(hw);
156 unsigned int mmc_rate, mmc_parent_rate;
161 /* Get the main MMC clock */
162 mmc = clk_get_parent(clk);
167 mmc_rate = clk_get_rate(mmc);
171 /* Now, get the MMC parent (most likely some PLL) */
172 mmc_parent = clk_get_parent(mmc);
177 mmc_parent_rate = clk_get_rate(mmc_parent);
178 if (!mmc_parent_rate)
181 if (degrees != 180) {
184 /* Get MMC clock divider */
185 mmc_div = mmc_parent_rate / mmc_rate;
188 * We can only outphase the clocks by multiple of the
191 * Since the MMC clock in only a divider, and the
192 * formula to get the outphasing in degrees is deg =
193 * 360 * delta / period
195 * If we simplify this formula, we can see that the
196 * only thing that we're concerned about is the number
197 * of period we want to outphase our clock from, and
198 * the divider set by the MMC clock.
200 step = DIV_ROUND_CLOSEST(360, mmc_div);
201 delay = DIV_ROUND_CLOSEST(degrees, step);
206 spin_lock_irqsave(phase->lock, flags);
207 value = readl(phase->reg);
208 value &= ~GENMASK(phase->data->offset + 3, phase->data->offset);
209 value |= delay << phase->data->offset;
210 writel(value, phase->reg);
211 spin_unlock_irqrestore(phase->lock, flags);
216 static const struct clk_ops mmc_clk_ops = {
217 .get_phase = mmc_get_phase,
218 .set_phase = mmc_set_phase,
221 static void __init sun4i_a10_mmc_phase_setup(struct device_node *node,
222 struct mmc_phase_data *data)
224 const char *parent_names[1] = { of_clk_get_parent_name(node, 0) };
225 struct clk_init_data init = {
227 .parent_names = parent_names,
231 struct mmc_phase *phase;
234 phase = kmalloc(sizeof(*phase), GFP_KERNEL);
238 phase->hw.init = &init;
240 phase->reg = of_iomap(node, 0);
245 phase->lock = &sun4i_a10_mod0_lock;
247 if (of_property_read_string(node, "clock-output-names", &init.name))
248 init.name = node->name;
250 clk = clk_register(NULL, &phase->hw);
254 of_clk_add_provider(node, of_clk_src_simple_get, clk);
265 static struct mmc_phase_data mmc_output_clk = {
269 static struct mmc_phase_data mmc_sample_clk = {
273 static void __init sun4i_a10_mmc_output_setup(struct device_node *node)
275 sun4i_a10_mmc_phase_setup(node, &mmc_output_clk);
277 CLK_OF_DECLARE(sun4i_a10_mmc_output, "allwinner,sun4i-a10-mmc-output-clk", sun4i_a10_mmc_output_setup);
279 static void __init sun4i_a10_mmc_sample_setup(struct device_node *node)
281 sun4i_a10_mmc_phase_setup(node, &mmc_sample_clk);
283 CLK_OF_DECLARE(sun4i_a10_mmc_sample, "allwinner,sun4i-a10-mmc-sample-clk", sun4i_a10_mmc_sample_setup);