blob: 886a8380e91247a199f2852177b9fda5cff8f0ad [file] [log] [blame]
Ulrich Hecht6232c512015-02-26 17:42:07 +01001/*
2 * r8a7778 Core CPG Clocks
3 *
4 * Copyright (C) 2014 Ulrich Hecht
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 */
10
11#include <linux/clk-provider.h>
Simon Horman09c32422016-03-08 09:42:07 +090012#include <linux/clk/renesas.h>
Ulrich Hecht6232c512015-02-26 17:42:07 +010013#include <linux/of_address.h>
Geert Uytterhoeven5a1cfaf2015-06-23 15:09:27 +020014#include <linux/slab.h>
Geert Uytterhoeven578d6012016-06-01 14:46:01 +020015#include <linux/soc/renesas/rcar-rst.h>
Ulrich Hecht6232c512015-02-26 17:42:07 +010016
17struct r8a7778_cpg {
18 struct clk_onecell_data data;
19 spinlock_t lock;
20 void __iomem *reg;
21};
22
23/* PLL multipliers per bits 11, 12, and 18 of MODEMR */
Geert Uytterhoeven7371a202015-10-16 17:14:55 +020024static const struct {
Ulrich Hecht6232c512015-02-26 17:42:07 +010025 unsigned long plla_mult;
26 unsigned long pllb_mult;
Geert Uytterhoeven7371a202015-10-16 17:14:55 +020027} r8a7778_rates[] __initconst = {
Ulrich Hecht6232c512015-02-26 17:42:07 +010028 [0] = { 21, 21 },
29 [1] = { 24, 24 },
30 [2] = { 28, 28 },
31 [3] = { 32, 32 },
32 [5] = { 24, 21 },
33 [6] = { 28, 21 },
34 [7] = { 32, 24 },
35};
36
37/* Clock dividers per bits 1 and 2 of MODEMR */
Geert Uytterhoeven7371a202015-10-16 17:14:55 +020038static const struct {
Ulrich Hecht6232c512015-02-26 17:42:07 +010039 const char *name;
40 unsigned int div[4];
Geert Uytterhoeven7371a202015-10-16 17:14:55 +020041} r8a7778_divs[6] __initconst = {
Ulrich Hecht6232c512015-02-26 17:42:07 +010042 { "b", { 12, 12, 16, 18 } },
43 { "out", { 12, 12, 16, 18 } },
44 { "p", { 16, 12, 16, 12 } },
45 { "s", { 4, 3, 4, 3 } },
46 { "s1", { 8, 6, 8, 6 } },
47};
48
49static u32 cpg_mode_rates __initdata;
50static u32 cpg_mode_divs __initdata;
51
52static struct clk * __init
53r8a7778_cpg_register_clock(struct device_node *np, struct r8a7778_cpg *cpg,
54 const char *name)
55{
56 if (!strcmp(name, "plla")) {
57 return clk_register_fixed_factor(NULL, "plla",
58 of_clk_get_parent_name(np, 0), 0,
59 r8a7778_rates[cpg_mode_rates].plla_mult, 1);
60 } else if (!strcmp(name, "pllb")) {
61 return clk_register_fixed_factor(NULL, "pllb",
62 of_clk_get_parent_name(np, 0), 0,
63 r8a7778_rates[cpg_mode_rates].pllb_mult, 1);
64 } else {
65 unsigned int i;
66
67 for (i = 0; i < ARRAY_SIZE(r8a7778_divs); i++) {
68 if (!strcmp(name, r8a7778_divs[i].name)) {
69 return clk_register_fixed_factor(NULL,
70 r8a7778_divs[i].name,
71 "plla", 0, 1,
72 r8a7778_divs[i].div[cpg_mode_divs]);
73 }
74 }
75 }
76
77 return ERR_PTR(-EINVAL);
78}
79
80
81static void __init r8a7778_cpg_clocks_init(struct device_node *np)
82{
83 struct r8a7778_cpg *cpg;
84 struct clk **clks;
85 unsigned int i;
86 int num_clks;
Geert Uytterhoeven578d6012016-06-01 14:46:01 +020087 u32 mode;
88
89 if (rcar_rst_read_mode_pins(&mode))
90 return;
91
92 BUG_ON(!(mode & BIT(19)));
93
94 cpg_mode_rates = (!!(mode & BIT(18)) << 2) |
95 (!!(mode & BIT(12)) << 1) |
96 (!!(mode & BIT(11)));
97 cpg_mode_divs = (!!(mode & BIT(2)) << 1) |
98 (!!(mode & BIT(1)));
Ulrich Hecht6232c512015-02-26 17:42:07 +010099
100 num_clks = of_property_count_strings(np, "clock-output-names");
101 if (num_clks < 0) {
102 pr_err("%s: failed to count clocks\n", __func__);
103 return;
104 }
105
106 cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
107 clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
108 if (cpg == NULL || clks == NULL) {
109 /* We're leaking memory on purpose, there's no point in cleaning
110 * up as the system won't boot anyway.
111 */
112 return;
113 }
114
115 spin_lock_init(&cpg->lock);
116
117 cpg->data.clks = clks;
118 cpg->data.clk_num = num_clks;
119
120 cpg->reg = of_iomap(np, 0);
121 if (WARN_ON(cpg->reg == NULL))
122 return;
123
124 for (i = 0; i < num_clks; ++i) {
125 const char *name;
126 struct clk *clk;
127
128 of_property_read_string_index(np, "clock-output-names", i,
129 &name);
130
131 clk = r8a7778_cpg_register_clock(np, cpg, name);
132 if (IS_ERR(clk))
133 pr_err("%s: failed to register %s %s clock (%ld)\n",
134 __func__, np->name, name, PTR_ERR(clk));
135 else
136 cpg->data.clks[i] = clk;
137 }
138
139 of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
Geert Uytterhoeven8bc964a2015-08-04 14:28:03 +0200140
141 cpg_mstp_add_clk_domain(np);
Ulrich Hecht6232c512015-02-26 17:42:07 +0100142}
143
144CLK_OF_DECLARE(r8a7778_cpg_clks, "renesas,r8a7778-cpg-clocks",
145 r8a7778_cpg_clocks_init);