1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2013 Emilio López
5 * Emilio López <emilio@elopez.com.ar>
9 #include <linux/clk-provider.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
15 #include "clk-factors.h"
18 * sun4i_a10_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
19 * MOD0 rate is calculated as follows
20 * rate = (parent_rate >> p) / (m + 1);
23 static void sun4i_a10_get_mod0_factors(struct factors_request
*req
)
27 /* These clocks can only divide, so we will never be able to achieve
28 * frequencies higher than the parent frequency */
29 if (req
->rate
> req
->parent_rate
)
30 req
->rate
= req
->parent_rate
;
32 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
36 else if (div
/ 2 < 16)
38 else if (div
/ 4 < 16)
43 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
45 req
->rate
= (req
->parent_rate
>> calcp
) / calcm
;
50 /* user manual says "n" but it's really "p" */
51 static const struct clk_factors_config sun4i_a10_mod0_config
= {
58 static const struct factors_data sun4i_a10_mod0_data
= {
61 .muxmask
= BIT(1) | BIT(0),
62 .table
= &sun4i_a10_mod0_config
,
63 .getter
= sun4i_a10_get_mod0_factors
,
66 static DEFINE_SPINLOCK(sun4i_a10_mod0_lock
);
68 static void __init
sun4i_a10_mod0_setup(struct device_node
*node
)
72 reg
= of_iomap(node
, 0);
75 * This happens with mod0 clk nodes instantiated through
76 * mfd, as those do not have their resources assigned at
77 * CLK_OF_DECLARE time yet, so do not print an error.
82 sunxi_factors_register(node
, &sun4i_a10_mod0_data
,
83 &sun4i_a10_mod0_lock
, reg
);
85 CLK_OF_DECLARE_DRIVER(sun4i_a10_mod0
, "allwinner,sun4i-a10-mod0-clk",
86 sun4i_a10_mod0_setup
);
88 static int sun4i_a10_mod0_clk_probe(struct platform_device
*pdev
)
90 struct device_node
*np
= pdev
->dev
.of_node
;
97 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
98 reg
= devm_ioremap_resource(&pdev
->dev
, r
);
102 sunxi_factors_register(np
, &sun4i_a10_mod0_data
,
103 &sun4i_a10_mod0_lock
, reg
);
107 static const struct of_device_id sun4i_a10_mod0_clk_dt_ids
[] = {
108 { .compatible
= "allwinner,sun4i-a10-mod0-clk" },
112 static struct platform_driver sun4i_a10_mod0_clk_driver
= {
114 .name
= "sun4i-a10-mod0-clk",
115 .of_match_table
= sun4i_a10_mod0_clk_dt_ids
,
117 .probe
= sun4i_a10_mod0_clk_probe
,
119 builtin_platform_driver(sun4i_a10_mod0_clk_driver
);
121 static const struct factors_data sun9i_a80_mod0_data __initconst
= {
124 .muxmask
= BIT(3) | BIT(2) | BIT(1) | BIT(0),
125 .table
= &sun4i_a10_mod0_config
,
126 .getter
= sun4i_a10_get_mod0_factors
,
129 static void __init
sun9i_a80_mod0_setup(struct device_node
*node
)
133 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
135 pr_err("Could not get registers for mod0-clk: %pOFn\n",
140 sunxi_factors_register(node
, &sun9i_a80_mod0_data
,
141 &sun4i_a10_mod0_lock
, reg
);
143 CLK_OF_DECLARE(sun9i_a80_mod0
, "allwinner,sun9i-a80-mod0-clk", sun9i_a80_mod0_setup
);
145 static DEFINE_SPINLOCK(sun5i_a13_mbus_lock
);
147 static void __init
sun5i_a13_mbus_setup(struct device_node
*node
)
151 reg
= of_iomap(node
, 0);
153 pr_err("Could not get registers for a13-mbus-clk\n");
157 /* The MBUS clocks needs to be always enabled */
158 sunxi_factors_register_critical(node
, &sun4i_a10_mod0_data
,
159 &sun5i_a13_mbus_lock
, reg
);
161 CLK_OF_DECLARE(sun5i_a13_mbus
, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup
);
170 #define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw)
172 static int mmc_get_phase(struct clk_hw
*hw
)
174 struct clk
*mmc
, *mmc_parent
, *clk
= hw
->clk
;
175 struct mmc_phase
*phase
= to_mmc_phase(hw
);
176 unsigned int mmc_rate
, mmc_parent_rate
;
181 value
= readl(phase
->reg
);
182 delay
= (value
>> phase
->offset
) & 0x3;
187 /* Get the main MMC clock */
188 mmc
= clk_get_parent(clk
);
193 mmc_rate
= clk_get_rate(mmc
);
197 /* Now, get the MMC parent (most likely some PLL) */
198 mmc_parent
= clk_get_parent(mmc
);
203 mmc_parent_rate
= clk_get_rate(mmc_parent
);
204 if (!mmc_parent_rate
)
207 /* Get MMC clock divider */
208 mmc_div
= mmc_parent_rate
/ mmc_rate
;
210 step
= DIV_ROUND_CLOSEST(360, mmc_div
);
214 static int mmc_set_phase(struct clk_hw
*hw
, int degrees
)
216 struct clk
*mmc
, *mmc_parent
, *clk
= hw
->clk
;
217 struct mmc_phase
*phase
= to_mmc_phase(hw
);
218 unsigned int mmc_rate
, mmc_parent_rate
;
223 /* Get the main MMC clock */
224 mmc
= clk_get_parent(clk
);
229 mmc_rate
= clk_get_rate(mmc
);
233 /* Now, get the MMC parent (most likely some PLL) */
234 mmc_parent
= clk_get_parent(mmc
);
239 mmc_parent_rate
= clk_get_rate(mmc_parent
);
240 if (!mmc_parent_rate
)
243 if (degrees
!= 180) {
246 /* Get MMC clock divider */
247 mmc_div
= mmc_parent_rate
/ mmc_rate
;
250 * We can only outphase the clocks by multiple of the
253 * Since the MMC clock in only a divider, and the
254 * formula to get the outphasing in degrees is deg =
255 * 360 * delta / period
257 * If we simplify this formula, we can see that the
258 * only thing that we're concerned about is the number
259 * of period we want to outphase our clock from, and
260 * the divider set by the MMC clock.
262 step
= DIV_ROUND_CLOSEST(360, mmc_div
);
263 delay
= DIV_ROUND_CLOSEST(degrees
, step
);
268 spin_lock_irqsave(phase
->lock
, flags
);
269 value
= readl(phase
->reg
);
270 value
&= ~GENMASK(phase
->offset
+ 3, phase
->offset
);
271 value
|= delay
<< phase
->offset
;
272 writel(value
, phase
->reg
);
273 spin_unlock_irqrestore(phase
->lock
, flags
);
278 static const struct clk_ops mmc_clk_ops
= {
279 .get_phase
= mmc_get_phase
,
280 .set_phase
= mmc_set_phase
,
284 * sunxi_mmc_setup - Common setup function for mmc module clocks
286 * The only difference between module clocks on different platforms is the
287 * width of the mux register bits and the valid values, which are passed in
288 * through struct factors_data. The phase clocks parts are identical.
290 static void __init
sunxi_mmc_setup(struct device_node
*node
,
291 const struct factors_data
*data
,
294 struct clk_onecell_data
*clk_data
;
299 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
301 pr_err("Couldn't map the %pOFn clock registers\n", node
);
305 clk_data
= kmalloc(sizeof(*clk_data
), GFP_KERNEL
);
309 clk_data
->clks
= kcalloc(3, sizeof(*clk_data
->clks
), GFP_KERNEL
);
313 clk_data
->clk_num
= 3;
314 clk_data
->clks
[0] = sunxi_factors_register(node
, data
, lock
, reg
);
315 if (!clk_data
->clks
[0])
318 parent
= __clk_get_name(clk_data
->clks
[0]);
320 for (i
= 1; i
< 3; i
++) {
321 struct clk_init_data init
= {
323 .parent_names
= &parent
,
326 struct mmc_phase
*phase
;
328 phase
= kmalloc(sizeof(*phase
), GFP_KERNEL
);
332 phase
->hw
.init
= &init
;
341 if (of_property_read_string_index(node
, "clock-output-names",
343 init
.name
= node
->name
;
345 clk_data
->clks
[i
] = clk_register(NULL
, &phase
->hw
);
346 if (IS_ERR(clk_data
->clks
[i
])) {
352 of_clk_add_provider(node
, of_clk_src_onecell_get
, clk_data
);
357 kfree(clk_data
->clks
);
362 static DEFINE_SPINLOCK(sun4i_a10_mmc_lock
);
364 static void __init
sun4i_a10_mmc_setup(struct device_node
*node
)
366 sunxi_mmc_setup(node
, &sun4i_a10_mod0_data
, &sun4i_a10_mmc_lock
);
368 CLK_OF_DECLARE(sun4i_a10_mmc
, "allwinner,sun4i-a10-mmc-clk", sun4i_a10_mmc_setup
);
370 static DEFINE_SPINLOCK(sun9i_a80_mmc_lock
);
372 static void __init
sun9i_a80_mmc_setup(struct device_node
*node
)
374 sunxi_mmc_setup(node
, &sun9i_a80_mod0_data
, &sun9i_a80_mmc_lock
);
376 CLK_OF_DECLARE(sun9i_a80_mmc
, "allwinner,sun9i-a80-mmc-clk", sun9i_a80_mmc_setup
);