blob: 24395dc2ae14e343976267d876c06bbf093890bf [file] [log] [blame]
Carlo Caione28b9fcd2015-06-01 13:13:55 +02001/*
Paul Gortmakera718ce32016-07-04 17:12:11 -04002 * AmLogic S805 / Meson8b Clock Controller Driver
3 *
Carlo Caione28b9fcd2015-06-01 13:13:55 +02004 * Copyright (c) 2015 Endless Mobile, Inc.
5 * Author: Carlo Caione <carlo@endlessm.com>
6 *
Michael Turquette796b9aa2016-05-11 11:11:18 -07007 * Copyright (c) 2016 BayLibre, Inc.
8 * Michael Turquette <mturquette@baylibre.com>
9 *
Carlo Caione28b9fcd2015-06-01 13:13:55 +020010 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
Michael Turquette55d42c42016-04-30 12:47:36 -070023#include <linux/clk.h>
Carlo Caione28b9fcd2015-06-01 13:13:55 +020024#include <linux/clk-provider.h>
Carlo Caione28b9fcd2015-06-01 13:13:55 +020025#include <linux/of_address.h>
Carlo Caione28b9fcd2015-06-01 13:13:55 +020026#include <dt-bindings/clock/meson8b-clkc.h>
Michael Turquette796b9aa2016-05-11 11:11:18 -070027#include <linux/platform_device.h>
Paul Gortmakera718ce32016-07-04 17:12:11 -040028#include <linux/init.h>
Carlo Caione28b9fcd2015-06-01 13:13:55 +020029
30#include "clkc.h"
Alexander Müllerd0c175d2016-08-27 19:40:50 +020031#include "meson8b.h"
Carlo Caione28b9fcd2015-06-01 13:13:55 +020032
Michael Turquettec0daa3e2016-04-28 12:01:51 -070033static DEFINE_SPINLOCK(clk_lock);
34
Carlo Caione28b9fcd2015-06-01 13:13:55 +020035static const struct pll_rate_table sys_pll_rate_table[] = {
36 PLL_RATE(312000000, 52, 1, 2),
37 PLL_RATE(336000000, 56, 1, 2),
38 PLL_RATE(360000000, 60, 1, 2),
39 PLL_RATE(384000000, 64, 1, 2),
40 PLL_RATE(408000000, 68, 1, 2),
41 PLL_RATE(432000000, 72, 1, 2),
42 PLL_RATE(456000000, 76, 1, 2),
43 PLL_RATE(480000000, 80, 1, 2),
44 PLL_RATE(504000000, 84, 1, 2),
45 PLL_RATE(528000000, 88, 1, 2),
46 PLL_RATE(552000000, 92, 1, 2),
47 PLL_RATE(576000000, 96, 1, 2),
48 PLL_RATE(600000000, 50, 1, 1),
49 PLL_RATE(624000000, 52, 1, 1),
50 PLL_RATE(648000000, 54, 1, 1),
51 PLL_RATE(672000000, 56, 1, 1),
52 PLL_RATE(696000000, 58, 1, 1),
53 PLL_RATE(720000000, 60, 1, 1),
54 PLL_RATE(744000000, 62, 1, 1),
55 PLL_RATE(768000000, 64, 1, 1),
56 PLL_RATE(792000000, 66, 1, 1),
57 PLL_RATE(816000000, 68, 1, 1),
58 PLL_RATE(840000000, 70, 1, 1),
59 PLL_RATE(864000000, 72, 1, 1),
60 PLL_RATE(888000000, 74, 1, 1),
61 PLL_RATE(912000000, 76, 1, 1),
62 PLL_RATE(936000000, 78, 1, 1),
63 PLL_RATE(960000000, 80, 1, 1),
64 PLL_RATE(984000000, 82, 1, 1),
65 PLL_RATE(1008000000, 84, 1, 1),
66 PLL_RATE(1032000000, 86, 1, 1),
67 PLL_RATE(1056000000, 88, 1, 1),
68 PLL_RATE(1080000000, 90, 1, 1),
69 PLL_RATE(1104000000, 92, 1, 1),
70 PLL_RATE(1128000000, 94, 1, 1),
71 PLL_RATE(1152000000, 96, 1, 1),
72 PLL_RATE(1176000000, 98, 1, 1),
73 PLL_RATE(1200000000, 50, 1, 0),
74 PLL_RATE(1224000000, 51, 1, 0),
75 PLL_RATE(1248000000, 52, 1, 0),
76 PLL_RATE(1272000000, 53, 1, 0),
77 PLL_RATE(1296000000, 54, 1, 0),
78 PLL_RATE(1320000000, 55, 1, 0),
79 PLL_RATE(1344000000, 56, 1, 0),
80 PLL_RATE(1368000000, 57, 1, 0),
81 PLL_RATE(1392000000, 58, 1, 0),
82 PLL_RATE(1416000000, 59, 1, 0),
83 PLL_RATE(1440000000, 60, 1, 0),
84 PLL_RATE(1464000000, 61, 1, 0),
85 PLL_RATE(1488000000, 62, 1, 0),
86 PLL_RATE(1512000000, 63, 1, 0),
87 PLL_RATE(1536000000, 64, 1, 0),
88 { /* sentinel */ },
89};
90
91static const struct clk_div_table cpu_div_table[] = {
92 { .val = 1, .div = 1 },
93 { .val = 2, .div = 2 },
94 { .val = 3, .div = 3 },
95 { .val = 2, .div = 4 },
96 { .val = 3, .div = 6 },
97 { .val = 4, .div = 8 },
98 { .val = 5, .div = 10 },
99 { .val = 6, .div = 12 },
100 { .val = 7, .div = 14 },
101 { .val = 8, .div = 16 },
102 { /* sentinel */ },
103};
104
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700105static struct clk_fixed_rate meson8b_xtal = {
106 .fixed_rate = 24000000,
107 .hw.init = &(struct clk_init_data){
108 .name = "xtal",
109 .num_parents = 0,
110 .ops = &clk_fixed_rate_ops,
111 },
112};
113
Michael Turquetteec623f22016-04-28 12:01:42 -0700114static struct meson_clk_pll meson8b_fixed_pll = {
115 .m = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200116 .reg_off = HHI_MPLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700117 .shift = 0,
118 .width = 9,
119 },
120 .n = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200121 .reg_off = HHI_MPLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700122 .shift = 9,
123 .width = 5,
124 },
125 .od = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200126 .reg_off = HHI_MPLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700127 .shift = 16,
128 .width = 2,
129 },
130 .lock = &clk_lock,
131 .hw.init = &(struct clk_init_data){
132 .name = "fixed_pll",
133 .ops = &meson_clk_pll_ro_ops,
134 .parent_names = (const char *[]){ "xtal" },
135 .num_parents = 1,
136 .flags = CLK_GET_RATE_NOCACHE,
137 },
138};
139
140static struct meson_clk_pll meson8b_vid_pll = {
141 .m = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200142 .reg_off = HHI_VID_PLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700143 .shift = 0,
144 .width = 9,
145 },
146 .n = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200147 .reg_off = HHI_VID_PLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700148 .shift = 9,
149 .width = 5,
150 },
151 .od = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200152 .reg_off = HHI_VID_PLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700153 .shift = 16,
154 .width = 2,
155 },
156 .lock = &clk_lock,
157 .hw.init = &(struct clk_init_data){
158 .name = "vid_pll",
159 .ops = &meson_clk_pll_ro_ops,
160 .parent_names = (const char *[]){ "xtal" },
161 .num_parents = 1,
162 .flags = CLK_GET_RATE_NOCACHE,
163 },
164};
165
166static struct meson_clk_pll meson8b_sys_pll = {
167 .m = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200168 .reg_off = HHI_SYS_PLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700169 .shift = 0,
170 .width = 9,
171 },
172 .n = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200173 .reg_off = HHI_SYS_PLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700174 .shift = 9,
175 .width = 5,
176 },
177 .od = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200178 .reg_off = HHI_SYS_PLL_CNTL,
Michael Turquetteec623f22016-04-28 12:01:42 -0700179 .shift = 16,
180 .width = 2,
181 },
182 .rate_table = sys_pll_rate_table,
183 .rate_count = ARRAY_SIZE(sys_pll_rate_table),
184 .lock = &clk_lock,
185 .hw.init = &(struct clk_init_data){
186 .name = "sys_pll",
187 .ops = &meson_clk_pll_ops,
188 .parent_names = (const char *[]){ "xtal" },
189 .num_parents = 1,
190 .flags = CLK_GET_RATE_NOCACHE,
191 },
192};
193
Michael Turquette6282a2d2016-04-28 12:01:58 -0700194static struct clk_fixed_factor meson8b_fclk_div2 = {
195 .mult = 1,
196 .div = 2,
197 .hw.init = &(struct clk_init_data){
198 .name = "fclk_div2",
199 .ops = &clk_fixed_factor_ops,
200 .parent_names = (const char *[]){ "fixed_pll" },
201 .num_parents = 1,
202 },
203};
204
205static struct clk_fixed_factor meson8b_fclk_div3 = {
206 .mult = 1,
207 .div = 3,
208 .hw.init = &(struct clk_init_data){
209 .name = "fclk_div3",
210 .ops = &clk_fixed_factor_ops,
211 .parent_names = (const char *[]){ "fixed_pll" },
212 .num_parents = 1,
213 },
214};
215
216static struct clk_fixed_factor meson8b_fclk_div4 = {
217 .mult = 1,
218 .div = 4,
219 .hw.init = &(struct clk_init_data){
220 .name = "fclk_div4",
221 .ops = &clk_fixed_factor_ops,
222 .parent_names = (const char *[]){ "fixed_pll" },
223 .num_parents = 1,
224 },
225};
226
227static struct clk_fixed_factor meson8b_fclk_div5 = {
228 .mult = 1,
229 .div = 5,
230 .hw.init = &(struct clk_init_data){
231 .name = "fclk_div5",
232 .ops = &clk_fixed_factor_ops,
233 .parent_names = (const char *[]){ "fixed_pll" },
234 .num_parents = 1,
235 },
236};
237
238static struct clk_fixed_factor meson8b_fclk_div7 = {
239 .mult = 1,
240 .div = 7,
241 .hw.init = &(struct clk_init_data){
242 .name = "fclk_div7",
243 .ops = &clk_fixed_factor_ops,
244 .parent_names = (const char *[]){ "fixed_pll" },
245 .num_parents = 1,
246 },
247};
248
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700249/*
250 * FIXME cpu clocks and the legacy composite clocks (e.g. clk81) are both PLL
251 * post-dividers and should be modeled with their respective PLLs via the
252 * forthcoming coordinated clock rates feature
253 */
Michael Turquette55d42c42016-04-30 12:47:36 -0700254static struct meson_clk_cpu meson8b_cpu_clk = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200255 .reg_off = HHI_SYS_CPU_CLK_CNTL1,
Michael Turquette55d42c42016-04-30 12:47:36 -0700256 .div_table = cpu_div_table,
257 .clk_nb.notifier_call = meson_clk_cpu_notifier_cb,
258 .hw.init = &(struct clk_init_data){
259 .name = "cpu_clk",
260 .ops = &meson_clk_cpu_ops,
261 .parent_names = (const char *[]){ "sys_pll" },
262 .num_parents = 1,
263 },
264};
265
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700266static u32 mux_table_clk81[] = { 6, 5, 7 };
267
268struct clk_mux meson8b_mpeg_clk_sel = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200269 .reg = (void *)HHI_MPEG_CLK_CNTL,
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700270 .mask = 0x7,
271 .shift = 12,
272 .flags = CLK_MUX_READ_ONLY,
273 .table = mux_table_clk81,
274 .lock = &clk_lock,
275 .hw.init = &(struct clk_init_data){
276 .name = "mpeg_clk_sel",
277 .ops = &clk_mux_ro_ops,
278 /*
279 * FIXME bits 14:12 selects from 8 possible parents:
280 * xtal, 1'b0 (wtf), fclk_div7, mpll_clkout1, mpll_clkout2,
281 * fclk_div4, fclk_div3, fclk_div5
282 */
283 .parent_names = (const char *[]){ "fclk_div3", "fclk_div4",
284 "fclk_div5" },
285 .num_parents = 3,
286 .flags = (CLK_SET_RATE_NO_REPARENT | CLK_IGNORE_UNUSED),
287 },
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200288};
289
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700290struct clk_divider meson8b_mpeg_clk_div = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200291 .reg = (void *)HHI_MPEG_CLK_CNTL,
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700292 .shift = 0,
293 .width = 7,
294 .lock = &clk_lock,
295 .hw.init = &(struct clk_init_data){
296 .name = "mpeg_clk_div",
297 .ops = &clk_divider_ops,
298 .parent_names = (const char *[]){ "mpeg_clk_sel" },
299 .num_parents = 1,
300 .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED),
301 },
302};
303
304struct clk_gate meson8b_clk81 = {
Alexander Müllere0818a32016-08-27 19:40:51 +0200305 .reg = (void *)HHI_MPEG_CLK_CNTL,
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700306 .bit_idx = 7,
307 .lock = &clk_lock,
308 .hw.init = &(struct clk_init_data){
309 .name = "clk81",
310 .ops = &clk_gate_ops,
311 .parent_names = (const char *[]){ "mpeg_clk_div" },
312 .num_parents = 1,
313 .flags = (CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED),
314 },
315};
316
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700317static struct clk_hw_onecell_data meson8b_hw_onecell_data = {
318 .hws = {
319 [CLKID_XTAL] = &meson8b_xtal.hw,
Michael Turquetteec623f22016-04-28 12:01:42 -0700320 [CLKID_PLL_FIXED] = &meson8b_fixed_pll.hw,
321 [CLKID_PLL_VID] = &meson8b_vid_pll.hw,
322 [CLKID_PLL_SYS] = &meson8b_sys_pll.hw,
Michael Turquette6282a2d2016-04-28 12:01:58 -0700323 [CLKID_FCLK_DIV2] = &meson8b_fclk_div2.hw,
324 [CLKID_FCLK_DIV3] = &meson8b_fclk_div3.hw,
325 [CLKID_FCLK_DIV4] = &meson8b_fclk_div4.hw,
326 [CLKID_FCLK_DIV5] = &meson8b_fclk_div5.hw,
327 [CLKID_FCLK_DIV7] = &meson8b_fclk_div7.hw,
Michael Turquette55d42c42016-04-30 12:47:36 -0700328 [CLKID_CPUCLK] = &meson8b_cpu_clk.hw,
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700329 [CLKID_MPEG_SEL] = &meson8b_mpeg_clk_sel.hw,
330 [CLKID_MPEG_DIV] = &meson8b_mpeg_clk_div.hw,
331 [CLKID_CLK81] = &meson8b_clk81.hw,
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700332 },
333 .num = CLK_NR_CLKS,
334};
335
Michael Turquetteec623f22016-04-28 12:01:42 -0700336static struct meson_clk_pll *const meson8b_clk_plls[] = {
337 &meson8b_fixed_pll,
338 &meson8b_vid_pll,
339 &meson8b_sys_pll,
340};
341
Michael Turquette796b9aa2016-05-11 11:11:18 -0700342static int meson8b_clkc_probe(struct platform_device *pdev)
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200343{
344 void __iomem *clk_base;
Michael Turquetteec623f22016-04-28 12:01:42 -0700345 int ret, clkid, i;
Michael Turquette55d42c42016-04-30 12:47:36 -0700346 struct clk_hw *parent_hw;
347 struct clk *parent_clk;
Michael Turquette796b9aa2016-05-11 11:11:18 -0700348 struct device *dev = &pdev->dev;
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200349
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200350 /* Generic clocks and PLLs */
Michael Turquette796b9aa2016-05-11 11:11:18 -0700351 clk_base = of_iomap(dev->of_node, 1);
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200352 if (!clk_base) {
353 pr_err("%s: Unable to map clk base\n", __func__);
Michael Turquette796b9aa2016-05-11 11:11:18 -0700354 return -ENXIO;
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200355 }
356
Michael Turquetteec623f22016-04-28 12:01:42 -0700357 /* Populate base address for PLLs */
358 for (i = 0; i < ARRAY_SIZE(meson8b_clk_plls); i++)
359 meson8b_clk_plls[i]->base = clk_base;
360
Michael Turquette55d42c42016-04-30 12:47:36 -0700361 /* Populate the base address for CPU clk */
362 meson8b_cpu_clk.base = clk_base;
363
Michael Turquettec0daa3e2016-04-28 12:01:51 -0700364 /* Populate the base address for the MPEG clks */
365 meson8b_mpeg_clk_sel.reg = clk_base + (u32)meson8b_mpeg_clk_sel.reg;
366 meson8b_mpeg_clk_div.reg = clk_base + (u32)meson8b_mpeg_clk_div.reg;
367 meson8b_clk81.reg = clk_base + (u32)meson8b_clk81.reg;
368
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700369 /*
370 * register all clks
371 * CLKID_UNUSED = 0, so skip it and start with CLKID_XTAL = 1
372 */
373 for (clkid = CLKID_XTAL; clkid < CLK_NR_CLKS; clkid++) {
374 /* array might be sparse */
375 if (!meson8b_hw_onecell_data.hws[clkid])
376 continue;
377
378 /* FIXME convert to devm_clk_register */
Michael Turquette796b9aa2016-05-11 11:11:18 -0700379 ret = devm_clk_hw_register(dev, meson8b_hw_onecell_data.hws[clkid]);
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700380 if (ret)
Michael Turquette796b9aa2016-05-11 11:11:18 -0700381 goto iounmap;
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700382 }
383
Michael Turquette55d42c42016-04-30 12:47:36 -0700384 /*
385 * Register CPU clk notifier
386 *
387 * FIXME this is wrong for a lot of reasons. First, the muxes should be
388 * struct clk_hw objects. Second, we shouldn't program the muxes in
389 * notifier handlers. The tricky programming sequence will be handled
390 * by the forthcoming coordinated clock rates mechanism once that
391 * feature is released.
392 *
393 * Furthermore, looking up the parent this way is terrible. At some
394 * point we will stop allocating a default struct clk when registering
395 * a new clk_hw, and this hack will no longer work. Releasing the ccr
396 * feature before that time solves the problem :-)
397 */
398 parent_hw = clk_hw_get_parent(&meson8b_cpu_clk.hw);
399 parent_clk = parent_hw->clk;
400 ret = clk_notifier_register(parent_clk, &meson8b_cpu_clk.clk_nb);
401 if (ret) {
402 pr_err("%s: failed to register clock notifier for cpu_clk\n",
403 __func__);
Michael Turquette796b9aa2016-05-11 11:11:18 -0700404 goto iounmap;
Michael Turquette55d42c42016-04-30 12:47:36 -0700405 }
406
Michael Turquette796b9aa2016-05-11 11:11:18 -0700407 return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
408 &meson8b_hw_onecell_data);
Michael Turquettee92f7cc2016-04-28 12:00:52 -0700409
Michael Turquette796b9aa2016-05-11 11:11:18 -0700410iounmap:
411 iounmap(clk_base);
412 return ret;
Carlo Caione28b9fcd2015-06-01 13:13:55 +0200413}
Michael Turquette796b9aa2016-05-11 11:11:18 -0700414
415static const struct of_device_id meson8b_clkc_match_table[] = {
416 { .compatible = "amlogic,meson8b-clkc" },
417 { }
418};
Michael Turquette796b9aa2016-05-11 11:11:18 -0700419
420static struct platform_driver meson8b_driver = {
421 .probe = meson8b_clkc_probe,
422 .driver = {
423 .name = "meson8b-clkc",
424 .of_match_table = meson8b_clkc_match_table,
425 },
426};
427
428static int __init meson8b_clkc_init(void)
429{
430 return platform_driver_register(&meson8b_driver);
431}
Paul Gortmakera718ce32016-07-04 17:12:11 -0400432device_initcall(meson8b_clkc_init);