2 * drivers/clk/clk-axm5516.c
4 * Provides clock implementations for three different types of clock devices on
5 * the Axxia device: PLL clock, a clock divider and a clock mux.
7 * Copyright (C) 2014 LSI Corporation
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
18 #include <linux/of_address.h>
19 #include <linux/clk-provider.h>
20 #include <linux/regmap.h>
21 #include <dt-bindings/clock/lsi,axm5516-clks.h>
25 * struct axxia_clk - Common struct to all Axxia clocks.
26 * @hw: clk_hw for the common clk framework
27 * @regmap: Regmap for the clock control registers
31 struct regmap *regmap;
33 #define to_axxia_clk(_hw) container_of(_hw, struct axxia_clk, hw)
36 * struct axxia_pllclk - Axxia PLL generated clock.
37 * @aclk: Common struct
38 * @reg: Offset into regmap for PLL control register
41 struct axxia_clk aclk;
44 #define to_axxia_pllclk(_aclk) container_of(_aclk, struct axxia_pllclk, aclk)
47 * axxia_pllclk_recalc - Calculate the PLL generated clock rate given the
51 axxia_pllclk_recalc(struct clk_hw *hw, unsigned long parent_rate)
53 struct axxia_clk *aclk = to_axxia_clk(hw);
54 struct axxia_pllclk *pll = to_axxia_pllclk(aclk);
55 unsigned long rate, fbdiv, refdiv, postdiv;
58 regmap_read(aclk->regmap, pll->reg, &control);
59 postdiv = ((control >> 0) & 0xf) + 1;
60 fbdiv = ((control >> 4) & 0xfff) + 3;
61 refdiv = ((control >> 16) & 0x1f) + 1;
62 rate = (parent_rate / (refdiv * postdiv)) * fbdiv;
67 static const struct clk_ops axxia_pllclk_ops = {
68 .recalc_rate = axxia_pllclk_recalc,
72 * struct axxia_divclk - Axxia clock divider
73 * @aclk: Common struct
74 * @reg: Offset into regmap for PLL control register
75 * @shift: Bit position for divider value
76 * @width: Number of bits in divider value
79 struct axxia_clk aclk;
84 #define to_axxia_divclk(_aclk) container_of(_aclk, struct axxia_divclk, aclk)
87 * axxia_divclk_recalc_rate - Calculate clock divider output rage
90 axxia_divclk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
92 struct axxia_clk *aclk = to_axxia_clk(hw);
93 struct axxia_divclk *divclk = to_axxia_divclk(aclk);
96 regmap_read(aclk->regmap, divclk->reg, &ctrl);
97 div = 1 + ((ctrl >> divclk->shift) & ((1 << divclk->width)-1));
99 return parent_rate / div;
102 static const struct clk_ops axxia_divclk_ops = {
103 .recalc_rate = axxia_divclk_recalc_rate,
107 * struct axxia_clkmux - Axxia clock mux
108 * @aclk: Common struct
109 * @reg: Offset into regmap for PLL control register
110 * @shift: Bit position for selection value
111 * @width: Number of bits in selection value
113 struct axxia_clkmux {
114 struct axxia_clk aclk;
119 #define to_axxia_clkmux(_aclk) container_of(_aclk, struct axxia_clkmux, aclk)
122 * axxia_clkmux_get_parent - Return the index of selected parent clock
124 static u8 axxia_clkmux_get_parent(struct clk_hw *hw)
126 struct axxia_clk *aclk = to_axxia_clk(hw);
127 struct axxia_clkmux *mux = to_axxia_clkmux(aclk);
130 regmap_read(aclk->regmap, mux->reg, &ctrl);
131 parent = (ctrl >> mux->shift) & ((1 << mux->width) - 1);
136 static const struct clk_ops axxia_clkmux_ops = {
137 .get_parent = axxia_clkmux_get_parent,
145 static struct axxia_pllclk clk_fab_pll = {
146 .aclk.hw.init = &(struct clk_init_data){
147 .name = "clk_fab_pll",
148 .parent_names = (const char *[]){
152 .ops = &axxia_pllclk_ops,
157 static struct axxia_pllclk clk_cpu_pll = {
158 .aclk.hw.init = &(struct clk_init_data){
159 .name = "clk_cpu_pll",
160 .parent_names = (const char *[]){
164 .ops = &axxia_pllclk_ops,
169 static struct axxia_pllclk clk_sys_pll = {
170 .aclk.hw.init = &(struct clk_init_data){
171 .name = "clk_sys_pll",
172 .parent_names = (const char *[]){
176 .ops = &axxia_pllclk_ops,
181 static struct axxia_pllclk clk_sm0_pll = {
182 .aclk.hw.init = &(struct clk_init_data){
183 .name = "clk_sm0_pll",
184 .parent_names = (const char *[]){
188 .ops = &axxia_pllclk_ops,
193 static struct axxia_pllclk clk_sm1_pll = {
194 .aclk.hw.init = &(struct clk_init_data){
195 .name = "clk_sm1_pll",
196 .parent_names = (const char *[]){
200 .ops = &axxia_pllclk_ops,
209 static struct axxia_divclk clk_cpu0_div = {
210 .aclk.hw.init = &(struct clk_init_data){
211 .name = "clk_cpu0_div",
212 .parent_names = (const char *[]){
216 .ops = &axxia_divclk_ops,
223 static struct axxia_divclk clk_cpu1_div = {
224 .aclk.hw.init = &(struct clk_init_data){
225 .name = "clk_cpu1_div",
226 .parent_names = (const char *[]){
230 .ops = &axxia_divclk_ops,
237 static struct axxia_divclk clk_cpu2_div = {
238 .aclk.hw.init = &(struct clk_init_data){
239 .name = "clk_cpu2_div",
240 .parent_names = (const char *[]){
244 .ops = &axxia_divclk_ops,
251 static struct axxia_divclk clk_cpu3_div = {
252 .aclk.hw.init = &(struct clk_init_data){
253 .name = "clk_cpu3_div",
254 .parent_names = (const char *[]){
258 .ops = &axxia_divclk_ops,
265 static struct axxia_divclk clk_nrcp_div = {
266 .aclk.hw.init = &(struct clk_init_data){
267 .name = "clk_nrcp_div",
268 .parent_names = (const char *[]){
272 .ops = &axxia_divclk_ops,
279 static struct axxia_divclk clk_sys_div = {
280 .aclk.hw.init = &(struct clk_init_data){
281 .name = "clk_sys_div",
282 .parent_names = (const char *[]){
286 .ops = &axxia_divclk_ops,
293 static struct axxia_divclk clk_fab_div = {
294 .aclk.hw.init = &(struct clk_init_data){
295 .name = "clk_fab_div",
296 .parent_names = (const char *[]){
300 .ops = &axxia_divclk_ops,
307 static struct axxia_divclk clk_per_div = {
308 .aclk.hw.init = &(struct clk_init_data){
309 .name = "clk_per_div",
310 .parent_names = (const char *[]){
314 .flags = CLK_IS_BASIC,
315 .ops = &axxia_divclk_ops,
322 static struct axxia_divclk clk_mmc_div = {
323 .aclk.hw.init = &(struct clk_init_data){
324 .name = "clk_mmc_div",
325 .parent_names = (const char *[]){
329 .flags = CLK_IS_BASIC,
330 .ops = &axxia_divclk_ops,
341 static struct axxia_clkmux clk_cpu0_mux = {
342 .aclk.hw.init = &(struct clk_init_data){
344 .parent_names = (const char *[]){
351 .ops = &axxia_clkmux_ops,
358 static struct axxia_clkmux clk_cpu1_mux = {
359 .aclk.hw.init = &(struct clk_init_data){
361 .parent_names = (const char *[]){
368 .ops = &axxia_clkmux_ops,
375 static struct axxia_clkmux clk_cpu2_mux = {
376 .aclk.hw.init = &(struct clk_init_data){
378 .parent_names = (const char *[]){
385 .ops = &axxia_clkmux_ops,
392 static struct axxia_clkmux clk_cpu3_mux = {
393 .aclk.hw.init = &(struct clk_init_data){
395 .parent_names = (const char *[]){
402 .ops = &axxia_clkmux_ops,
409 static struct axxia_clkmux clk_nrcp_mux = {
410 .aclk.hw.init = &(struct clk_init_data){
412 .parent_names = (const char *[]){
419 .ops = &axxia_clkmux_ops,
426 static struct axxia_clkmux clk_sys_mux = {
427 .aclk.hw.init = &(struct clk_init_data){
429 .parent_names = (const char *[]){
436 .ops = &axxia_clkmux_ops,
443 static struct axxia_clkmux clk_fab_mux = {
444 .aclk.hw.init = &(struct clk_init_data){
446 .parent_names = (const char *[]){
453 .ops = &axxia_clkmux_ops,
460 static struct axxia_clkmux clk_per_mux = {
461 .aclk.hw.init = &(struct clk_init_data){
463 .parent_names = (const char *[]){
468 .ops = &axxia_clkmux_ops,
475 static struct axxia_clkmux clk_mmc_mux = {
476 .aclk.hw.init = &(struct clk_init_data){
478 .parent_names = (const char *[]){
483 .ops = &axxia_clkmux_ops,
490 /* Table of all supported clocks indexed by the clock identifiers from the
491 * device tree binding
493 static struct axxia_clk *axmclk_clocks[] = {
494 [AXXIA_CLK_FAB_PLL] = &clk_fab_pll.aclk,
495 [AXXIA_CLK_CPU_PLL] = &clk_cpu_pll.aclk,
496 [AXXIA_CLK_SYS_PLL] = &clk_sys_pll.aclk,
497 [AXXIA_CLK_SM0_PLL] = &clk_sm0_pll.aclk,
498 [AXXIA_CLK_SM1_PLL] = &clk_sm1_pll.aclk,
499 [AXXIA_CLK_FAB_DIV] = &clk_fab_div.aclk,
500 [AXXIA_CLK_SYS_DIV] = &clk_sys_div.aclk,
501 [AXXIA_CLK_NRCP_DIV] = &clk_nrcp_div.aclk,
502 [AXXIA_CLK_CPU0_DIV] = &clk_cpu0_div.aclk,
503 [AXXIA_CLK_CPU1_DIV] = &clk_cpu1_div.aclk,
504 [AXXIA_CLK_CPU2_DIV] = &clk_cpu2_div.aclk,
505 [AXXIA_CLK_CPU3_DIV] = &clk_cpu3_div.aclk,
506 [AXXIA_CLK_PER_DIV] = &clk_per_div.aclk,
507 [AXXIA_CLK_MMC_DIV] = &clk_mmc_div.aclk,
508 [AXXIA_CLK_FAB] = &clk_fab_mux.aclk,
509 [AXXIA_CLK_SYS] = &clk_sys_mux.aclk,
510 [AXXIA_CLK_NRCP] = &clk_nrcp_mux.aclk,
511 [AXXIA_CLK_CPU0] = &clk_cpu0_mux.aclk,
512 [AXXIA_CLK_CPU1] = &clk_cpu1_mux.aclk,
513 [AXXIA_CLK_CPU2] = &clk_cpu2_mux.aclk,
514 [AXXIA_CLK_CPU3] = &clk_cpu3_mux.aclk,
515 [AXXIA_CLK_PER] = &clk_per_mux.aclk,
516 [AXXIA_CLK_MMC] = &clk_mmc_mux.aclk,
519 static struct clk_hw *
520 of_clk_axmclk_get(struct of_phandle_args *clkspec, void *unused)
522 unsigned int idx = clkspec->args[0];
524 if (idx >= ARRAY_SIZE(axmclk_clocks)) {
525 pr_err("%s: invalid index %u\n", __func__, idx);
526 return ERR_PTR(-EINVAL);
529 return &axmclk_clocks[idx]->hw;
532 static const struct regmap_config axmclk_regmap_config = {
536 .max_register = 0x1fffc,
540 static const struct of_device_id axmclk_match_table[] = {
541 { .compatible = "lsi,axm5516-clks" },
544 MODULE_DEVICE_TABLE(of, axmclk_match_table);
546 static int axmclk_probe(struct platform_device *pdev)
549 struct resource *res;
551 struct device *dev = &pdev->dev;
552 struct regmap *regmap;
555 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
556 base = devm_ioremap_resource(dev, res);
558 return PTR_ERR(base);
560 regmap = devm_regmap_init_mmio(dev, base, &axmclk_regmap_config);
562 return PTR_ERR(regmap);
564 num_clks = ARRAY_SIZE(axmclk_clocks);
565 pr_info("axmclk: supporting %zu clocks\n", num_clks);
567 /* Update each entry with the allocated regmap and register the clock
568 * with the common clock framework
570 for (i = 0; i < num_clks; i++) {
571 axmclk_clocks[i]->regmap = regmap;
572 ret = devm_clk_hw_register(dev, &axmclk_clocks[i]->hw);
577 return of_clk_add_hw_provider(dev->of_node, of_clk_axmclk_get, NULL);
580 static int axmclk_remove(struct platform_device *pdev)
582 of_clk_del_provider(pdev->dev.of_node);
586 static struct platform_driver axmclk_driver = {
587 .probe = axmclk_probe,
588 .remove = axmclk_remove,
590 .name = "clk-axm5516",
591 .of_match_table = axmclk_match_table,
595 static int __init axmclk_init(void)
597 return platform_driver_register(&axmclk_driver);
599 core_initcall(axmclk_init);
601 static void __exit axmclk_exit(void)
603 platform_driver_unregister(&axmclk_driver);
605 module_exit(axmclk_exit);
607 MODULE_DESCRIPTION("AXM5516 clock driver");
608 MODULE_LICENSE("GPL v2");
609 MODULE_ALIAS("platform:clk-axm5516");