clk: meson8b: clean up pll clocks
[sfrench/cifs-2.6.git] / drivers / clk / meson / meson8b-clkc.c
1 /*
2  * Copyright (c) 2015 Endless Mobile, Inc.
3  * Author: Carlo Caione <carlo@endlessm.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/clk-provider.h>
19 #include <linux/kernel.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/slab.h>
23 #include <dt-bindings/clock/meson8b-clkc.h>
24
25 #include "clkc.h"
26
27 /*
28  * Clock controller register offsets
29  *
30  * Register offsets from the HardKernel[0] data sheet are listed in comment
31  * blocks below. Those offsets must be multiplied by 4 before adding them to
32  * the base address to get the right value
33  *
34  * [0] http://dn.odroid.com/S805/Datasheet/S805_Datasheet%20V0.8%2020150126.pdf
35  */
36 #define MESON8B_REG_SYS_CPU_CNTL1       0x015c /* 0x57 offset in data sheet */
37 #define MESON8B_REG_HHI_MPEG            0x0174 /* 0x5d offset in data sheet */
38 #define MESON8B_REG_MALI                0x01b0 /* 0x6c offset in data sheet */
39 #define MESON8B_REG_PLL_FIXED           0x0280
40 #define MESON8B_REG_PLL_SYS             0x0300
41 #define MESON8B_REG_PLL_VID             0x0320
42
43 static const struct pll_rate_table sys_pll_rate_table[] = {
44         PLL_RATE(312000000, 52, 1, 2),
45         PLL_RATE(336000000, 56, 1, 2),
46         PLL_RATE(360000000, 60, 1, 2),
47         PLL_RATE(384000000, 64, 1, 2),
48         PLL_RATE(408000000, 68, 1, 2),
49         PLL_RATE(432000000, 72, 1, 2),
50         PLL_RATE(456000000, 76, 1, 2),
51         PLL_RATE(480000000, 80, 1, 2),
52         PLL_RATE(504000000, 84, 1, 2),
53         PLL_RATE(528000000, 88, 1, 2),
54         PLL_RATE(552000000, 92, 1, 2),
55         PLL_RATE(576000000, 96, 1, 2),
56         PLL_RATE(600000000, 50, 1, 1),
57         PLL_RATE(624000000, 52, 1, 1),
58         PLL_RATE(648000000, 54, 1, 1),
59         PLL_RATE(672000000, 56, 1, 1),
60         PLL_RATE(696000000, 58, 1, 1),
61         PLL_RATE(720000000, 60, 1, 1),
62         PLL_RATE(744000000, 62, 1, 1),
63         PLL_RATE(768000000, 64, 1, 1),
64         PLL_RATE(792000000, 66, 1, 1),
65         PLL_RATE(816000000, 68, 1, 1),
66         PLL_RATE(840000000, 70, 1, 1),
67         PLL_RATE(864000000, 72, 1, 1),
68         PLL_RATE(888000000, 74, 1, 1),
69         PLL_RATE(912000000, 76, 1, 1),
70         PLL_RATE(936000000, 78, 1, 1),
71         PLL_RATE(960000000, 80, 1, 1),
72         PLL_RATE(984000000, 82, 1, 1),
73         PLL_RATE(1008000000, 84, 1, 1),
74         PLL_RATE(1032000000, 86, 1, 1),
75         PLL_RATE(1056000000, 88, 1, 1),
76         PLL_RATE(1080000000, 90, 1, 1),
77         PLL_RATE(1104000000, 92, 1, 1),
78         PLL_RATE(1128000000, 94, 1, 1),
79         PLL_RATE(1152000000, 96, 1, 1),
80         PLL_RATE(1176000000, 98, 1, 1),
81         PLL_RATE(1200000000, 50, 1, 0),
82         PLL_RATE(1224000000, 51, 1, 0),
83         PLL_RATE(1248000000, 52, 1, 0),
84         PLL_RATE(1272000000, 53, 1, 0),
85         PLL_RATE(1296000000, 54, 1, 0),
86         PLL_RATE(1320000000, 55, 1, 0),
87         PLL_RATE(1344000000, 56, 1, 0),
88         PLL_RATE(1368000000, 57, 1, 0),
89         PLL_RATE(1392000000, 58, 1, 0),
90         PLL_RATE(1416000000, 59, 1, 0),
91         PLL_RATE(1440000000, 60, 1, 0),
92         PLL_RATE(1464000000, 61, 1, 0),
93         PLL_RATE(1488000000, 62, 1, 0),
94         PLL_RATE(1512000000, 63, 1, 0),
95         PLL_RATE(1536000000, 64, 1, 0),
96         { /* sentinel */ },
97 };
98
99 static const struct clk_div_table cpu_div_table[] = {
100         { .val = 1, .div = 1 },
101         { .val = 2, .div = 2 },
102         { .val = 3, .div = 3 },
103         { .val = 2, .div = 4 },
104         { .val = 3, .div = 6 },
105         { .val = 4, .div = 8 },
106         { .val = 5, .div = 10 },
107         { .val = 6, .div = 12 },
108         { .val = 7, .div = 14 },
109         { .val = 8, .div = 16 },
110         { /* sentinel */ },
111 };
112
113 PNAME(p_fclk_div)       = { "fixed_pll" };
114 PNAME(p_cpu_clk)        = { "sys_pll" };
115 PNAME(p_clk81)          = { "fclk_div3", "fclk_div4", "fclk_div5" };
116 PNAME(p_mali)           = { "fclk_div3", "fclk_div4", "fclk_div5",
117                             "fclk_div7", "zero" };
118
119 static u32 mux_table_clk81[]    = { 6, 5, 7 };
120 static u32 mux_table_mali[]     = { 6, 5, 7, 4, 0 };
121
122 static const struct composite_conf clk81_conf __initconst = {
123         .mux_table              = mux_table_clk81,
124         .mux_flags              = CLK_MUX_READ_ONLY,
125         .mux_parm               = PARM(0x00, 12, 3),
126         .div_parm               = PARM(0x00, 0, 7),
127         .gate_parm              = PARM(0x00, 7, 1),
128 };
129
130 static const struct composite_conf mali_conf __initconst = {
131         .mux_table              = mux_table_mali,
132         .mux_parm               = PARM(0x00, 9, 3),
133         .div_parm               = PARM(0x00, 0, 7),
134         .gate_parm              = PARM(0x00, 8, 1),
135 };
136
137 static struct clk_fixed_rate meson8b_xtal = {
138         .fixed_rate = 24000000,
139         .hw.init = &(struct clk_init_data){
140                 .name = "xtal",
141                 .num_parents = 0,
142                 .ops = &clk_fixed_rate_ops,
143         },
144 };
145
146 static struct clk_fixed_rate meson8b_zero = {
147         .fixed_rate = 0,
148         .hw.init = &(struct clk_init_data){
149                 .name = "zero",
150                 .num_parents = 0,
151                 .ops = &clk_fixed_rate_ops,
152         },
153 };
154
155 static struct meson_clk_pll meson8b_fixed_pll = {
156         .m = {
157                 .reg_off = MESON8B_REG_PLL_FIXED,
158                 .shift   = 0,
159                 .width   = 9,
160         },
161         .n = {
162                 .reg_off = MESON8B_REG_PLL_FIXED,
163                 .shift   = 9,
164                 .width   = 5,
165         },
166         .od = {
167                 .reg_off = MESON8B_REG_PLL_FIXED,
168                 .shift   = 16,
169                 .width   = 2,
170         },
171         .lock = &clk_lock,
172         .hw.init = &(struct clk_init_data){
173                 .name = "fixed_pll",
174                 .ops = &meson_clk_pll_ro_ops,
175                 .parent_names = (const char *[]){ "xtal" },
176                 .num_parents = 1,
177                 .flags = CLK_GET_RATE_NOCACHE,
178         },
179 };
180
181 static struct meson_clk_pll meson8b_vid_pll = {
182         .m = {
183                 .reg_off = MESON8B_REG_PLL_VID,
184                 .shift   = 0,
185                 .width   = 9,
186         },
187         .n = {
188                 .reg_off = MESON8B_REG_PLL_VID,
189                 .shift   = 9,
190                 .width   = 5,
191         },
192         .od = {
193                 .reg_off = MESON8B_REG_PLL_VID,
194                 .shift   = 16,
195                 .width   = 2,
196         },
197         .lock = &clk_lock,
198         .hw.init = &(struct clk_init_data){
199                 .name = "vid_pll",
200                 .ops = &meson_clk_pll_ro_ops,
201                 .parent_names = (const char *[]){ "xtal" },
202                 .num_parents = 1,
203                 .flags = CLK_GET_RATE_NOCACHE,
204         },
205 };
206
207 static struct meson_clk_pll meson8b_sys_pll = {
208         .m = {
209                 .reg_off = MESON8B_REG_PLL_SYS,
210                 .shift   = 0,
211                 .width   = 9,
212         },
213         .n = {
214                 .reg_off = MESON8B_REG_PLL_SYS,
215                 .shift   = 9,
216                 .width   = 5,
217         },
218         .od = {
219                 .reg_off = MESON8B_REG_PLL_SYS,
220                 .shift   = 16,
221                 .width   = 2,
222         },
223         .rate_table = sys_pll_rate_table,
224         .rate_count = ARRAY_SIZE(sys_pll_rate_table),
225         .lock = &clk_lock,
226         .hw.init = &(struct clk_init_data){
227                 .name = "sys_pll",
228                 .ops = &meson_clk_pll_ops,
229                 .parent_names = (const char *[]){ "xtal" },
230                 .num_parents = 1,
231                 .flags = CLK_GET_RATE_NOCACHE,
232         },
233 };
234
235 static const struct clk_conf meson8b_clk_confs[] __initconst = {
236         FIXED_FACTOR_DIV(CLKID_FCLK_DIV2, "fclk_div2", p_fclk_div, 0, 2),
237         FIXED_FACTOR_DIV(CLKID_FCLK_DIV3, "fclk_div3", p_fclk_div, 0, 3),
238         FIXED_FACTOR_DIV(CLKID_FCLK_DIV4, "fclk_div4", p_fclk_div, 0, 4),
239         FIXED_FACTOR_DIV(CLKID_FCLK_DIV5, "fclk_div5", p_fclk_div, 0, 5),
240         FIXED_FACTOR_DIV(CLKID_FCLK_DIV7, "fclk_div7", p_fclk_div, 0, 7),
241         CPU(MESON8B_REG_SYS_CPU_CNTL1, CLKID_CPUCLK, "a5_clk", p_cpu_clk,
242             cpu_div_table),
243         COMPOSITE(MESON8B_REG_HHI_MPEG, CLKID_CLK81, "clk81", p_clk81,
244                   CLK_SET_RATE_NO_REPARENT | CLK_IGNORE_UNUSED, &clk81_conf),
245         COMPOSITE(MESON8B_REG_MALI, CLKID_MALI, "mali", p_mali,
246                   CLK_IGNORE_UNUSED, &mali_conf),
247 };
248
249 /*
250  * FIXME we cannot register two providers w/o breaking things. Luckily only
251  * clk81 is actually used by any drivers. Convert clk81 to use
252  * clk_hw_onecell_data last and flip the switch to call of_clk_add_hw_provider
253  * instead of of_clk_add_provider in the clk81 conversion patch to keep from
254  * breaking bisect. Then delete this comment ;-)
255  */
256 static struct clk_hw_onecell_data meson8b_hw_onecell_data = {
257         .hws = {
258                 [CLKID_XTAL] = &meson8b_xtal.hw,
259                 [CLKID_ZERO] = &meson8b_zero.hw,
260                 [CLKID_PLL_FIXED] = &meson8b_fixed_pll.hw,
261                 [CLKID_PLL_VID] = &meson8b_vid_pll.hw,
262                 [CLKID_PLL_SYS] = &meson8b_sys_pll.hw,
263         },
264         .num = CLK_NR_CLKS,
265 };
266
267 static struct meson_clk_pll *const meson8b_clk_plls[] = {
268         &meson8b_fixed_pll,
269         &meson8b_vid_pll,
270         &meson8b_sys_pll,
271 };
272
273 static void __init meson8b_clkc_init(struct device_node *np)
274 {
275         void __iomem *clk_base;
276         int ret, clkid, i;
277
278         if (!meson_clk_init(np, CLK_NR_CLKS))
279                 return;
280
281         /*  Generic clocks and PLLs */
282         clk_base = of_iomap(np, 1);
283         if (!clk_base) {
284                 pr_err("%s: Unable to map clk base\n", __func__);
285                 return;
286         }
287
288         /* Populate base address for PLLs */
289         for (i = 0; i < ARRAY_SIZE(meson8b_clk_plls); i++)
290                 meson8b_clk_plls[i]->base = clk_base;
291
292         /*
293          * register all clks
294          * CLKID_UNUSED = 0, so skip it and start with CLKID_XTAL = 1
295          */
296         for (clkid = CLKID_XTAL; clkid < CLK_NR_CLKS; clkid++) {
297                 /* array might be sparse */
298                 if (!meson8b_hw_onecell_data.hws[clkid])
299                         continue;
300
301                 /* FIXME convert to devm_clk_register */
302                 ret = clk_hw_register(NULL, meson8b_hw_onecell_data.hws[clkid]);
303                 if (ret)
304                         goto unregister;
305         }
306
307         meson_clk_register_clks(meson8b_clk_confs,
308                                 ARRAY_SIZE(meson8b_clk_confs),
309                                 clk_base);
310         return;
311
312 /* FIXME remove after converting to platform_driver/devm_clk_register */
313 unregister:
314         for (clkid = CLK_NR_CLKS - 1; clkid >= 0; clkid--)
315                 clk_hw_unregister(meson8b_hw_onecell_data.hws[clkid]);
316 }
317 CLK_OF_DECLARE(meson8b_clock, "amlogic,meson8b-clkc", meson8b_clkc_init);