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.h>
18 #include <linux/clkdev.h>
19 #include <linux/clk-provider.h>
20 #include <linux/of_address.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #include "clk-factors.h"
27 * sun4i_a10_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
28 * MOD0 rate is calculated as follows
29 * rate = (parent_rate >> p) / (m + 1);
32 static void sun4i_a10_get_mod0_factors(struct factors_request
*req
)
36 /* These clocks can only divide, so we will never be able to achieve
37 * frequencies higher than the parent frequency */
38 if (req
->rate
> req
->parent_rate
)
39 req
->rate
= req
->parent_rate
;
41 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
45 else if (div
/ 2 < 16)
47 else if (div
/ 4 < 16)
52 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
54 req
->rate
= (req
->parent_rate
>> calcp
) / calcm
;
59 /* user manual says "n" but it's really "p" */
60 static const struct clk_factors_config sun4i_a10_mod0_config
= {
67 static const struct factors_data sun4i_a10_mod0_data
= {
70 .muxmask
= BIT(1) | BIT(0),
71 .table
= &sun4i_a10_mod0_config
,
72 .getter
= sun4i_a10_get_mod0_factors
,
75 static DEFINE_SPINLOCK(sun4i_a10_mod0_lock
);
77 static void __init
sun4i_a10_mod0_setup(struct device_node
*node
)
81 reg
= of_iomap(node
, 0);
84 * This happens with mod0 clk nodes instantiated through
85 * mfd, as those do not have their resources assigned at
86 * CLK_OF_DECLARE time yet, so do not print an error.
91 sunxi_factors_register(node
, &sun4i_a10_mod0_data
,
92 &sun4i_a10_mod0_lock
, reg
);
94 CLK_OF_DECLARE_DRIVER(sun4i_a10_mod0
, "allwinner,sun4i-a10-mod0-clk",
95 sun4i_a10_mod0_setup
);
97 static int sun4i_a10_mod0_clk_probe(struct platform_device
*pdev
)
99 struct device_node
*np
= pdev
->dev
.of_node
;
106 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
107 reg
= devm_ioremap_resource(&pdev
->dev
, r
);
111 sunxi_factors_register(np
, &sun4i_a10_mod0_data
,
112 &sun4i_a10_mod0_lock
, reg
);
116 static const struct of_device_id sun4i_a10_mod0_clk_dt_ids
[] = {
117 { .compatible
= "allwinner,sun4i-a10-mod0-clk" },
121 static struct platform_driver sun4i_a10_mod0_clk_driver
= {
123 .name
= "sun4i-a10-mod0-clk",
124 .of_match_table
= sun4i_a10_mod0_clk_dt_ids
,
126 .probe
= sun4i_a10_mod0_clk_probe
,
128 builtin_platform_driver(sun4i_a10_mod0_clk_driver
);
130 static const struct factors_data sun9i_a80_mod0_data __initconst
= {
133 .muxmask
= BIT(3) | BIT(2) | BIT(1) | BIT(0),
134 .table
= &sun4i_a10_mod0_config
,
135 .getter
= sun4i_a10_get_mod0_factors
,
138 static void __init
sun9i_a80_mod0_setup(struct device_node
*node
)
142 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
144 pr_err("Could not get registers for mod0-clk: %s\n",
149 sunxi_factors_register(node
, &sun9i_a80_mod0_data
,
150 &sun4i_a10_mod0_lock
, reg
);
152 CLK_OF_DECLARE(sun9i_a80_mod0
, "allwinner,sun9i-a80-mod0-clk", sun9i_a80_mod0_setup
);
154 static DEFINE_SPINLOCK(sun5i_a13_mbus_lock
);
156 static void __init
sun5i_a13_mbus_setup(struct device_node
*node
)
161 reg
= of_iomap(node
, 0);
163 pr_err("Could not get registers for a13-mbus-clk\n");
167 mbus
= sunxi_factors_register(node
, &sun4i_a10_mod0_data
,
168 &sun5i_a13_mbus_lock
, reg
);
170 /* The MBUS clocks needs to be always enabled */
172 clk_prepare_enable(mbus
);
174 CLK_OF_DECLARE(sun5i_a13_mbus
, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup
);
183 #define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw)
185 static int mmc_get_phase(struct clk_hw
*hw
)
187 struct clk
*mmc
, *mmc_parent
, *clk
= hw
->clk
;
188 struct mmc_phase
*phase
= to_mmc_phase(hw
);
189 unsigned int mmc_rate
, mmc_parent_rate
;
194 value
= readl(phase
->reg
);
195 delay
= (value
>> phase
->offset
) & 0x3;
200 /* Get the main MMC clock */
201 mmc
= clk_get_parent(clk
);
206 mmc_rate
= clk_get_rate(mmc
);
210 /* Now, get the MMC parent (most likely some PLL) */
211 mmc_parent
= clk_get_parent(mmc
);
216 mmc_parent_rate
= clk_get_rate(mmc_parent
);
217 if (!mmc_parent_rate
)
220 /* Get MMC clock divider */
221 mmc_div
= mmc_parent_rate
/ mmc_rate
;
223 step
= DIV_ROUND_CLOSEST(360, mmc_div
);
227 static int mmc_set_phase(struct clk_hw
*hw
, int degrees
)
229 struct clk
*mmc
, *mmc_parent
, *clk
= hw
->clk
;
230 struct mmc_phase
*phase
= to_mmc_phase(hw
);
231 unsigned int mmc_rate
, mmc_parent_rate
;
236 /* Get the main MMC clock */
237 mmc
= clk_get_parent(clk
);
242 mmc_rate
= clk_get_rate(mmc
);
246 /* Now, get the MMC parent (most likely some PLL) */
247 mmc_parent
= clk_get_parent(mmc
);
252 mmc_parent_rate
= clk_get_rate(mmc_parent
);
253 if (!mmc_parent_rate
)
256 if (degrees
!= 180) {
259 /* Get MMC clock divider */
260 mmc_div
= mmc_parent_rate
/ mmc_rate
;
263 * We can only outphase the clocks by multiple of the
266 * Since the MMC clock in only a divider, and the
267 * formula to get the outphasing in degrees is deg =
268 * 360 * delta / period
270 * If we simplify this formula, we can see that the
271 * only thing that we're concerned about is the number
272 * of period we want to outphase our clock from, and
273 * the divider set by the MMC clock.
275 step
= DIV_ROUND_CLOSEST(360, mmc_div
);
276 delay
= DIV_ROUND_CLOSEST(degrees
, step
);
281 spin_lock_irqsave(phase
->lock
, flags
);
282 value
= readl(phase
->reg
);
283 value
&= ~GENMASK(phase
->offset
+ 3, phase
->offset
);
284 value
|= delay
<< phase
->offset
;
285 writel(value
, phase
->reg
);
286 spin_unlock_irqrestore(phase
->lock
, flags
);
291 static const struct clk_ops mmc_clk_ops
= {
292 .get_phase
= mmc_get_phase
,
293 .set_phase
= mmc_set_phase
,
297 * sunxi_mmc_setup - Common setup function for mmc module clocks
299 * The only difference between module clocks on different platforms is the
300 * width of the mux register bits and the valid values, which are passed in
301 * through struct factors_data. The phase clocks parts are identical.
303 static void __init
sunxi_mmc_setup(struct device_node
*node
,
304 const struct factors_data
*data
,
307 struct clk_onecell_data
*clk_data
;
312 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
314 pr_err("Couldn't map the %s clock registers\n", node
->name
);
318 clk_data
= kmalloc(sizeof(*clk_data
), GFP_KERNEL
);
322 clk_data
->clks
= kcalloc(3, sizeof(*clk_data
->clks
), GFP_KERNEL
);
326 clk_data
->clk_num
= 3;
327 clk_data
->clks
[0] = sunxi_factors_register(node
, data
, lock
, reg
);
328 if (!clk_data
->clks
[0])
331 parent
= __clk_get_name(clk_data
->clks
[0]);
333 for (i
= 1; i
< 3; i
++) {
334 struct clk_init_data init
= {
336 .parent_names
= &parent
,
339 struct mmc_phase
*phase
;
341 phase
= kmalloc(sizeof(*phase
), GFP_KERNEL
);
345 phase
->hw
.init
= &init
;
354 if (of_property_read_string_index(node
, "clock-output-names",
356 init
.name
= node
->name
;
358 clk_data
->clks
[i
] = clk_register(NULL
, &phase
->hw
);
359 if (IS_ERR(clk_data
->clks
[i
])) {
365 of_clk_add_provider(node
, of_clk_src_onecell_get
, clk_data
);
370 kfree(clk_data
->clks
);
375 static DEFINE_SPINLOCK(sun4i_a10_mmc_lock
);
377 static void __init
sun4i_a10_mmc_setup(struct device_node
*node
)
379 sunxi_mmc_setup(node
, &sun4i_a10_mod0_data
, &sun4i_a10_mmc_lock
);
381 CLK_OF_DECLARE(sun4i_a10_mmc
, "allwinner,sun4i-a10-mmc-clk", sun4i_a10_mmc_setup
);
383 static DEFINE_SPINLOCK(sun9i_a80_mmc_lock
);
385 static void __init
sun9i_a80_mmc_setup(struct device_node
*node
)
387 sunxi_mmc_setup(node
, &sun9i_a80_mod0_data
, &sun9i_a80_mmc_lock
);
389 CLK_OF_DECLARE(sun9i_a80_mmc
, "allwinner,sun9i-a80-mmc-clk", sun9i_a80_mmc_setup
);