hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/clk/at91/clk-utmi.c
....@@ -1,11 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3
- *
4
- * This program is free software; you can redistribute it and/or modify
5
- * it under the terms of the GNU General Public License as published by
6
- * the Free Software Foundation; either version 2 of the License, or
7
- * (at your option) any later version.
8
- *
94 */
105
116 #include <linux/clk-provider.h>
....@@ -126,12 +121,14 @@
126121 };
127122
128123 static struct clk_hw * __init
129
-at91_clk_register_utmi(struct regmap *regmap_pmc, struct regmap *regmap_sfr,
130
- const char *name, const char *parent_name)
124
+at91_clk_register_utmi_internal(struct regmap *regmap_pmc,
125
+ struct regmap *regmap_sfr,
126
+ const char *name, const char *parent_name,
127
+ const struct clk_ops *ops, unsigned long flags)
131128 {
132129 struct clk_utmi *utmi;
133130 struct clk_hw *hw;
134
- struct clk_init_data init = {};
131
+ struct clk_init_data init;
135132 int ret;
136133
137134 utmi = kzalloc(sizeof(*utmi), GFP_KERNEL);
....@@ -139,10 +136,10 @@
139136 return ERR_PTR(-ENOMEM);
140137
141138 init.name = name;
142
- init.ops = &utmi_ops;
139
+ init.ops = ops;
143140 init.parent_names = parent_name ? &parent_name : NULL;
144141 init.num_parents = parent_name ? 1 : 0;
145
- init.flags = CLK_SET_RATE_GATE;
142
+ init.flags = flags;
146143
147144 utmi->hw.init = &init;
148145 utmi->regmap_pmc = regmap_pmc;
....@@ -158,45 +155,93 @@
158155 return hw;
159156 }
160157
161
-static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
158
+struct clk_hw * __init
159
+at91_clk_register_utmi(struct regmap *regmap_pmc, struct regmap *regmap_sfr,
160
+ const char *name, const char *parent_name)
162161 {
163
- struct clk_hw *hw;
164
- const char *parent_name;
165
- const char *name = np->name;
166
- struct regmap *regmap_pmc, *regmap_sfr;
162
+ return at91_clk_register_utmi_internal(regmap_pmc, regmap_sfr, name,
163
+ parent_name, &utmi_ops, CLK_SET_RATE_GATE);
164
+}
167165
168
- parent_name = of_clk_get_parent_name(np, 0);
166
+static int clk_utmi_sama7g5_prepare(struct clk_hw *hw)
167
+{
168
+ struct clk_utmi *utmi = to_clk_utmi(hw);
169
+ struct clk_hw *hw_parent;
170
+ unsigned long parent_rate;
171
+ unsigned int val;
169172
170
- of_property_read_string(np, "clock-output-names", &name);
173
+ hw_parent = clk_hw_get_parent(hw);
174
+ parent_rate = clk_hw_get_rate(hw_parent);
171175
172
- regmap_pmc = syscon_node_to_regmap(of_get_parent(np));
173
- if (IS_ERR(regmap_pmc))
174
- return;
175
-
176
- /*
177
- * If the device supports different mainck rates, this value has to be
178
- * set in the UTMI Clock Trimming register.
179
- * - 9x5: mainck supports several rates but it is indicated that a
180
- * 12 MHz is needed in case of USB.
181
- * - sama5d3 and sama5d2: mainck supports several rates. Configuring
182
- * the FREQ field of the UTMI Clock Trimming register is mandatory.
183
- * - sama5d4: mainck is at 12 MHz.
184
- *
185
- * We only need to retrieve sama5d3 or sama5d2 sfr regmap.
186
- */
187
- regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d3-sfr");
188
- if (IS_ERR(regmap_sfr)) {
189
- regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
190
- if (IS_ERR(regmap_sfr))
191
- regmap_sfr = NULL;
176
+ switch (parent_rate) {
177
+ case 16000000:
178
+ val = 0;
179
+ break;
180
+ case 20000000:
181
+ val = 2;
182
+ break;
183
+ case 24000000:
184
+ val = 3;
185
+ break;
186
+ case 32000000:
187
+ val = 5;
188
+ break;
189
+ default:
190
+ pr_err("UTMICK: unsupported main_xtal rate\n");
191
+ return -EINVAL;
192192 }
193193
194
- hw = at91_clk_register_utmi(regmap_pmc, regmap_sfr, name, parent_name);
195
- if (IS_ERR(hw))
196
- return;
194
+ regmap_write(utmi->regmap_pmc, AT91_PMC_XTALF, val);
197195
198
- of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
199
- return;
196
+ return 0;
197
+
200198 }
201
-CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
202
- of_at91sam9x5_clk_utmi_setup);
199
+
200
+static int clk_utmi_sama7g5_is_prepared(struct clk_hw *hw)
201
+{
202
+ struct clk_utmi *utmi = to_clk_utmi(hw);
203
+ struct clk_hw *hw_parent;
204
+ unsigned long parent_rate;
205
+ unsigned int val;
206
+
207
+ hw_parent = clk_hw_get_parent(hw);
208
+ parent_rate = clk_hw_get_rate(hw_parent);
209
+
210
+ regmap_read(utmi->regmap_pmc, AT91_PMC_XTALF, &val);
211
+ switch (val & 0x7) {
212
+ case 0:
213
+ if (parent_rate == 16000000)
214
+ return 1;
215
+ break;
216
+ case 2:
217
+ if (parent_rate == 20000000)
218
+ return 1;
219
+ break;
220
+ case 3:
221
+ if (parent_rate == 24000000)
222
+ return 1;
223
+ break;
224
+ case 5:
225
+ if (parent_rate == 32000000)
226
+ return 1;
227
+ break;
228
+ default:
229
+ break;
230
+ }
231
+
232
+ return 0;
233
+}
234
+
235
+static const struct clk_ops sama7g5_utmi_ops = {
236
+ .prepare = clk_utmi_sama7g5_prepare,
237
+ .is_prepared = clk_utmi_sama7g5_is_prepared,
238
+ .recalc_rate = clk_utmi_recalc_rate,
239
+};
240
+
241
+struct clk_hw * __init
242
+at91_clk_sama7g5_register_utmi(struct regmap *regmap_pmc, const char *name,
243
+ const char *parent_name)
244
+{
245
+ return at91_clk_register_utmi_internal(regmap_pmc, NULL, name,
246
+ parent_name, &sama7g5_utmi_ops, 0);
247
+}