1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/clk/clk-axm5516.c
5 * Provides clock implementations for three different types of clock devices on
6 * the Axxia device: PLL clock, a clock divider and a clock mux.
8 * Copyright (C) 2014 LSI Corporation
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
15 #include <linux/of_address.h>
16 #include <linux/clk-provider.h>
17 #include <linux/regmap.h>
18 #include <dt-bindings/clock/lsi,axm5516-clks.h>
22 * struct axxia_clk - Common struct to all Axxia clocks.
23 * @hw: clk_hw for the common clk framework
24 * @regmap: Regmap for the clock control registers
28 struct regmap *regmap;
30 #define to_axxia_clk(_hw) container_of(_hw, struct axxia_clk, hw)
33 * struct axxia_pllclk - Axxia PLL generated clock.
34 * @aclk: Common struct
35 * @reg: Offset into regmap for PLL control register
38 struct axxia_clk aclk;
41 #define to_axxia_pllclk(_aclk) container_of(_aclk, struct axxia_pllclk, aclk)
44 * axxia_pllclk_recalc - Calculate the PLL generated clock rate given the
48 axxia_pllclk_recalc(struct clk_hw *hw, unsigned long parent_rate)
50 struct axxia_clk *aclk = to_axxia_clk(hw);
51 struct axxia_pllclk *pll = to_axxia_pllclk(aclk);
52 unsigned long rate, fbdiv, refdiv, postdiv;
55 regmap_read(aclk->regmap, pll->reg, &control);
56 postdiv = ((control >> 0) & 0xf) + 1;
57 fbdiv = ((control >> 4) & 0xfff) + 3;
58 refdiv = ((control >> 16) & 0x1f) + 1;
59 rate = (parent_rate / (refdiv * postdiv)) * fbdiv;
64 static const struct clk_ops axxia_pllclk_ops = {
65 .recalc_rate = axxia_pllclk_recalc,
69 * struct axxia_divclk - Axxia clock divider
70 * @aclk: Common struct
71 * @reg: Offset into regmap for PLL control register
72 * @shift: Bit position for divider value
73 * @width: Number of bits in divider value
76 struct axxia_clk aclk;
81 #define to_axxia_divclk(_aclk) container_of(_aclk, struct axxia_divclk, aclk)
84 * axxia_divclk_recalc_rate - Calculate clock divider output rage
87 axxia_divclk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
89 struct axxia_clk *aclk = to_axxia_clk(hw);
90 struct axxia_divclk *divclk = to_axxia_divclk(aclk);
93 regmap_read(aclk->regmap, divclk->reg, &ctrl);
94 div = 1 + ((ctrl >> divclk->shift) & ((1 << divclk->width)-1));
96 return parent_rate / div;
99 static const struct clk_ops axxia_divclk_ops = {
100 .recalc_rate = axxia_divclk_recalc_rate,
104 * struct axxia_clkmux - Axxia clock mux
105 * @aclk: Common struct
106 * @reg: Offset into regmap for PLL control register
107 * @shift: Bit position for selection value
108 * @width: Number of bits in selection value
110 struct axxia_clkmux {
111 struct axxia_clk aclk;
116 #define to_axxia_clkmux(_aclk) container_of(_aclk, struct axxia_clkmux, aclk)
119 * axxia_clkmux_get_parent - Return the index of selected parent clock
121 static u8 axxia_clkmux_get_parent(struct clk_hw *hw)
123 struct axxia_clk *aclk = to_axxia_clk(hw);
124 struct axxia_clkmux *mux = to_axxia_clkmux(aclk);
127 regmap_read(aclk->regmap, mux->reg, &ctrl);
128 parent = (ctrl >> mux->shift) & ((1 << mux->width) - 1);
133 static const struct clk_ops axxia_clkmux_ops = {
134 .get_parent = axxia_clkmux_get_parent,
142 static struct axxia_pllclk clk_fab_pll = {
143 .aclk.hw.init = &(struct clk_init_data){
144 .name = "clk_fab_pll",
145 .parent_names = (const char *[]){
149 .ops = &axxia_pllclk_ops,
154 static struct axxia_pllclk clk_cpu_pll = {
155 .aclk.hw.init = &(struct clk_init_data){
156 .name = "clk_cpu_pll",
157 .parent_names = (const char *[]){
161 .ops = &axxia_pllclk_ops,
166 static struct axxia_pllclk clk_sys_pll = {
167 .aclk.hw.init = &(struct clk_init_data){
168 .name = "clk_sys_pll",
169 .parent_names = (const char *[]){
173 .ops = &axxia_pllclk_ops,
178 static struct axxia_pllclk clk_sm0_pll = {
179 .aclk.hw.init = &(struct clk_init_data){
180 .name = "clk_sm0_pll",
181 .parent_names = (const char *[]){
185 .ops = &axxia_pllclk_ops,
190 static struct axxia_pllclk clk_sm1_pll = {
191 .aclk.hw.init = &(struct clk_init_data){
192 .name = "clk_sm1_pll",
193 .parent_names = (const char *[]){
197 .ops = &axxia_pllclk_ops,
206 static struct axxia_divclk clk_cpu0_div = {
207 .aclk.hw.init = &(struct clk_init_data){
208 .name = "clk_cpu0_div",
209 .parent_names = (const char *[]){
213 .ops = &axxia_divclk_ops,
220 static struct axxia_divclk clk_cpu1_div = {
221 .aclk.hw.init = &(struct clk_init_data){
222 .name = "clk_cpu1_div",
223 .parent_names = (const char *[]){
227 .ops = &axxia_divclk_ops,
234 static struct axxia_divclk clk_cpu2_div = {
235 .aclk.hw.init = &(struct clk_init_data){
236 .name = "clk_cpu2_div",
237 .parent_names = (const char *[]){
241 .ops = &axxia_divclk_ops,
248 static struct axxia_divclk clk_cpu3_div = {
249 .aclk.hw.init = &(struct clk_init_data){
250 .name = "clk_cpu3_div",
251 .parent_names = (const char *[]){
255 .ops = &axxia_divclk_ops,
262 static struct axxia_divclk clk_nrcp_div = {
263 .aclk.hw.init = &(struct clk_init_data){
264 .name = "clk_nrcp_div",
265 .parent_names = (const char *[]){
269 .ops = &axxia_divclk_ops,
276 static struct axxia_divclk clk_sys_div = {
277 .aclk.hw.init = &(struct clk_init_data){
278 .name = "clk_sys_div",
279 .parent_names = (const char *[]){
283 .ops = &axxia_divclk_ops,
290 static struct axxia_divclk clk_fab_div = {
291 .aclk.hw.init = &(struct clk_init_data){
292 .name = "clk_fab_div",
293 .parent_names = (const char *[]){
297 .ops = &axxia_divclk_ops,
304 static struct axxia_divclk clk_per_div = {
305 .aclk.hw.init = &(struct clk_init_data){
306 .name = "clk_per_div",
307 .parent_names = (const char *[]){
311 .ops = &axxia_divclk_ops,
318 static struct axxia_divclk clk_mmc_div = {
319 .aclk.hw.init = &(struct clk_init_data){
320 .name = "clk_mmc_div",
321 .parent_names = (const char *[]){
325 .ops = &axxia_divclk_ops,
336 static struct axxia_clkmux clk_cpu0_mux = {
337 .aclk.hw.init = &(struct clk_init_data){
339 .parent_names = (const char *[]){
346 .ops = &axxia_clkmux_ops,
353 static struct axxia_clkmux clk_cpu1_mux = {
354 .aclk.hw.init = &(struct clk_init_data){
356 .parent_names = (const char *[]){
363 .ops = &axxia_clkmux_ops,
370 static struct axxia_clkmux clk_cpu2_mux = {
371 .aclk.hw.init = &(struct clk_init_data){
373 .parent_names = (const char *[]){
380 .ops = &axxia_clkmux_ops,
387 static struct axxia_clkmux clk_cpu3_mux = {
388 .aclk.hw.init = &(struct clk_init_data){
390 .parent_names = (const char *[]){
397 .ops = &axxia_clkmux_ops,
404 static struct axxia_clkmux clk_nrcp_mux = {
405 .aclk.hw.init = &(struct clk_init_data){
407 .parent_names = (const char *[]){
414 .ops = &axxia_clkmux_ops,
421 static struct axxia_clkmux clk_sys_mux = {
422 .aclk.hw.init = &(struct clk_init_data){
424 .parent_names = (const char *[]){
431 .ops = &axxia_clkmux_ops,
438 static struct axxia_clkmux clk_fab_mux = {
439 .aclk.hw.init = &(struct clk_init_data){
441 .parent_names = (const char *[]){
448 .ops = &axxia_clkmux_ops,
455 static struct axxia_clkmux clk_per_mux = {
456 .aclk.hw.init = &(struct clk_init_data){
458 .parent_names = (const char *[]){
463 .ops = &axxia_clkmux_ops,
470 static struct axxia_clkmux clk_mmc_mux = {
471 .aclk.hw.init = &(struct clk_init_data){
473 .parent_names = (const char *[]){
478 .ops = &axxia_clkmux_ops,
485 /* Table of all supported clocks indexed by the clock identifiers from the
486 * device tree binding
488 static struct axxia_clk *axmclk_clocks[] = {
489 [AXXIA_CLK_FAB_PLL] = &clk_fab_pll.aclk,
490 [AXXIA_CLK_CPU_PLL] = &clk_cpu_pll.aclk,
491 [AXXIA_CLK_SYS_PLL] = &clk_sys_pll.aclk,
492 [AXXIA_CLK_SM0_PLL] = &clk_sm0_pll.aclk,
493 [AXXIA_CLK_SM1_PLL] = &clk_sm1_pll.aclk,
494 [AXXIA_CLK_FAB_DIV] = &clk_fab_div.aclk,
495 [AXXIA_CLK_SYS_DIV] = &clk_sys_div.aclk,
496 [AXXIA_CLK_NRCP_DIV] = &clk_nrcp_div.aclk,
497 [AXXIA_CLK_CPU0_DIV] = &clk_cpu0_div.aclk,
498 [AXXIA_CLK_CPU1_DIV] = &clk_cpu1_div.aclk,
499 [AXXIA_CLK_CPU2_DIV] = &clk_cpu2_div.aclk,
500 [AXXIA_CLK_CPU3_DIV] = &clk_cpu3_div.aclk,
501 [AXXIA_CLK_PER_DIV] = &clk_per_div.aclk,
502 [AXXIA_CLK_MMC_DIV] = &clk_mmc_div.aclk,
503 [AXXIA_CLK_FAB] = &clk_fab_mux.aclk,
504 [AXXIA_CLK_SYS] = &clk_sys_mux.aclk,
505 [AXXIA_CLK_NRCP] = &clk_nrcp_mux.aclk,
506 [AXXIA_CLK_CPU0] = &clk_cpu0_mux.aclk,
507 [AXXIA_CLK_CPU1] = &clk_cpu1_mux.aclk,
508 [AXXIA_CLK_CPU2] = &clk_cpu2_mux.aclk,
509 [AXXIA_CLK_CPU3] = &clk_cpu3_mux.aclk,
510 [AXXIA_CLK_PER] = &clk_per_mux.aclk,
511 [AXXIA_CLK_MMC] = &clk_mmc_mux.aclk,
514 static struct clk_hw *
515 of_clk_axmclk_get(struct of_phandle_args *clkspec, void *unused)
517 unsigned int idx = clkspec->args[0];
519 if (idx >= ARRAY_SIZE(axmclk_clocks)) {
520 pr_err("%s: invalid index %u\n", __func__, idx);
521 return ERR_PTR(-EINVAL);
524 return &axmclk_clocks[idx]->hw;
527 static const struct regmap_config axmclk_regmap_config = {
531 .max_register = 0x1fffc,
535 static const struct of_device_id axmclk_match_table[] = {
536 { .compatible = "lsi,axm5516-clks" },
539 MODULE_DEVICE_TABLE(of, axmclk_match_table);
541 static int axmclk_probe(struct platform_device *pdev)
544 struct resource *res;
546 struct device *dev = &pdev->dev;
547 struct regmap *regmap;
550 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
551 base = devm_ioremap_resource(dev, res);
553 return PTR_ERR(base);
555 regmap = devm_regmap_init_mmio(dev, base, &axmclk_regmap_config);
557 return PTR_ERR(regmap);
559 num_clks = ARRAY_SIZE(axmclk_clocks);
560 pr_info("axmclk: supporting %zu clocks\n", num_clks);
562 /* Update each entry with the allocated regmap and register the clock
563 * with the common clock framework
565 for (i = 0; i < num_clks; i++) {
566 axmclk_clocks[i]->regmap = regmap;
567 ret = devm_clk_hw_register(dev, &axmclk_clocks[i]->hw);
572 return of_clk_add_hw_provider(dev->of_node, of_clk_axmclk_get, NULL);
575 static int axmclk_remove(struct platform_device *pdev)
577 of_clk_del_provider(pdev->dev.of_node);
581 static struct platform_driver axmclk_driver = {
582 .probe = axmclk_probe,
583 .remove = axmclk_remove,
585 .name = "clk-axm5516",
586 .of_match_table = axmclk_match_table,
590 static int __init axmclk_init(void)
592 return platform_driver_register(&axmclk_driver);
594 core_initcall(axmclk_init);
596 static void __exit axmclk_exit(void)
598 platform_driver_unregister(&axmclk_driver);
600 module_exit(axmclk_exit);
602 MODULE_DESCRIPTION("AXM5516 clock driver");
603 MODULE_LICENSE("GPL v2");
604 MODULE_ALIAS("platform:clk-axm5516");