2 * r8a7779 Core CPG Clocks
4 * Copyright (C) 2013, 2014 Horms Solutions Ltd.
6 * Contact: Simon Horman <horms@verge.net.au>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #include <linux/clk-provider.h>
14 #include <linux/clk/renesas.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
18 #include <linux/of_address.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/soc/renesas/rcar-rst.h>
23 #include <dt-bindings/clock/r8a7779-clock.h>
25 #define CPG_NUM_CLOCKS (R8A7779_CLK_OUT + 1)
28 struct clk_onecell_data data
;
33 /* -----------------------------------------------------------------------------
39 * (PLLA = 1500) (PLLA = 1600)
41 *------------------------------------------------+--------------------
42 * clkz 1000 (2/3) 800 (1/2)
43 * clkzs 250 (1/6) 200 (1/8)
44 * clki 750 (1/2) 800 (1/2)
45 * clks 250 (1/6) 200 (1/8)
46 * clks1 125 (1/12) 100 (1/16)
47 * clks3 187.5 (1/8) 200 (1/8)
48 * clks4 93.7 (1/16) 100 (1/16)
49 * clkp 62.5 (1/24) 50 (1/32)
50 * clkg 62.5 (1/24) 66.6 (1/24)
52 * (MD2 = 0) 62.5 (1/24) 66.6 (1/24)
53 * (MD2 = 1) 41.6 (1/36) 50 (1/32)
56 #define CPG_CLK_CONFIG_INDEX(md) (((md) & (BIT(2)|BIT(1))) >> 1)
58 struct cpg_clk_config
{
61 unsigned int zs_and_s_div
;
64 unsigned int b_and_out_div
;
67 static const struct cpg_clk_config cpg_clk_configs
[4] __initconst
= {
68 { 1, 2, 8, 16, 32, 24 },
69 { 2, 3, 6, 12, 24, 24 },
70 { 1, 2, 8, 16, 32, 32 },
71 { 2, 3, 6, 12, 24, 36 },
77 *------------------------
84 #define CPG_PLLA_MULT_INDEX(md) (((md) & (BIT(12)|BIT(11))) >> 11)
86 static const unsigned int cpg_plla_mult
[4] __initconst
= { 42, 48, 56, 64 };
88 /* -----------------------------------------------------------------------------
92 static struct clk
* __init
93 r8a7779_cpg_register_clock(struct device_node
*np
, struct r8a7779_cpg
*cpg
,
94 const struct cpg_clk_config
*config
,
95 unsigned int plla_mult
, const char *name
)
97 const char *parent_name
= "plla";
98 unsigned int mult
= 1;
101 if (!strcmp(name
, "plla")) {
102 parent_name
= of_clk_get_parent_name(np
, 0);
104 } else if (!strcmp(name
, "z")) {
106 mult
= config
->z_mult
;
107 } else if (!strcmp(name
, "zs") || !strcmp(name
, "s")) {
108 div
= config
->zs_and_s_div
;
109 } else if (!strcmp(name
, "s1")) {
110 div
= config
->s1_div
;
111 } else if (!strcmp(name
, "p")) {
113 } else if (!strcmp(name
, "b") || !strcmp(name
, "out")) {
114 div
= config
->b_and_out_div
;
116 return ERR_PTR(-EINVAL
);
119 return clk_register_fixed_factor(NULL
, name
, parent_name
, 0, mult
, div
);
122 static void __init
r8a7779_cpg_clocks_init(struct device_node
*np
)
124 const struct cpg_clk_config
*config
;
125 struct r8a7779_cpg
*cpg
;
127 unsigned int i
, plla_mult
;
131 if (rcar_rst_read_mode_pins(&mode
))
134 num_clks
= of_property_count_strings(np
, "clock-output-names");
136 pr_err("%s: failed to count clocks\n", __func__
);
140 cpg
= kzalloc(sizeof(*cpg
), GFP_KERNEL
);
141 clks
= kzalloc(CPG_NUM_CLOCKS
* sizeof(*clks
), GFP_KERNEL
);
142 if (cpg
== NULL
|| clks
== NULL
) {
143 /* We're leaking memory on purpose, there's no point in cleaning
144 * up as the system won't boot anyway.
149 spin_lock_init(&cpg
->lock
);
151 cpg
->data
.clks
= clks
;
152 cpg
->data
.clk_num
= num_clks
;
154 config
= &cpg_clk_configs
[CPG_CLK_CONFIG_INDEX(mode
)];
155 plla_mult
= cpg_plla_mult
[CPG_PLLA_MULT_INDEX(mode
)];
157 for (i
= 0; i
< num_clks
; ++i
) {
161 of_property_read_string_index(np
, "clock-output-names", i
,
164 clk
= r8a7779_cpg_register_clock(np
, cpg
, config
,
167 pr_err("%s: failed to register %s %s clock (%ld)\n",
168 __func__
, np
->name
, name
, PTR_ERR(clk
));
170 cpg
->data
.clks
[i
] = clk
;
173 of_clk_add_provider(np
, of_clk_src_onecell_get
, &cpg
->data
);
175 cpg_mstp_add_clk_domain(np
);
177 CLK_OF_DECLARE(r8a7779_cpg_clks
, "renesas,r8a7779-cpg-clocks",
178 r8a7779_cpg_clocks_init
);