blob: 58b83e0af90f54f0080fc3e50a841d14444ea9b7 [file] [log] [blame]
Tero Kristoa8acecc2013-07-18 11:52:33 +03001/*
2 * TI clock support
3 *
4 * Copyright (C) 2013 Texas Instruments, Inc.
5 *
6 * Tero Kristo <t-kristo@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/clk-provider.h>
19#include <linux/clkdev.h>
20#include <linux/clk/ti.h>
21#include <linux/of.h>
Tero Kristo819b4862013-10-22 11:39:36 +030022#include <linux/of_address.h>
23#include <linux/list.h>
Tero Kristoa8acecc2013-07-18 11:52:33 +030024
Tero Kristoc82f8952014-12-16 18:20:46 +020025#include "clock.h"
26
Tero Kristoa8acecc2013-07-18 11:52:33 +030027#undef pr_fmt
28#define pr_fmt(fmt) "%s: " fmt, __func__
29
Tero Kristo819b4862013-10-22 11:39:36 +030030struct ti_clk_ll_ops *ti_clk_ll_ops;
Tero Kristoc08ee142014-09-12 15:01:57 +030031static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
Tero Kristo819b4862013-10-22 11:39:36 +030032
Tero Kristof3b19aa2015-02-27 17:54:14 +020033struct ti_clk_features ti_clk_features;
34
Tero Kristoa8acecc2013-07-18 11:52:33 +030035/**
Tero Kristoe9e63082015-04-27 21:55:42 +030036 * ti_clk_setup_ll_ops - setup low level clock operations
37 * @ops: low level clock ops descriptor
38 *
39 * Sets up low level clock operations for TI clock driver. This is used
40 * to provide various callbacks for the clock driver towards platform
41 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
42 * registered already.
43 */
44int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
45{
46 if (ti_clk_ll_ops) {
47 pr_err("Attempt to register ll_ops multiple times.\n");
48 return -EBUSY;
49 }
50
51 ti_clk_ll_ops = ops;
52
53 return 0;
54}
55
56/**
Tero Kristoa8acecc2013-07-18 11:52:33 +030057 * ti_dt_clocks_register - register DT alias clocks during boot
58 * @oclks: list of clocks to register
59 *
60 * Register alias or non-standard DT clock entries during boot. By
61 * default, DT clocks are found based on their node name. If any
62 * additional con-id / dev-id -> clock mapping is required, use this
63 * function to list these.
64 */
65void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
66{
67 struct ti_dt_clk *c;
68 struct device_node *node;
69 struct clk *clk;
70 struct of_phandle_args clkspec;
71
72 for (c = oclks; c->node_name != NULL; c++) {
73 node = of_find_node_by_name(NULL, c->node_name);
74 clkspec.np = node;
75 clk = of_clk_get_from_provider(&clkspec);
76
77 if (!IS_ERR(clk)) {
78 c->lk.clk = clk;
79 clkdev_add(&c->lk);
80 } else {
81 pr_warn("failed to lookup clock node %s\n",
82 c->node_name);
83 }
84 }
85}
Tero Kristo819b4862013-10-22 11:39:36 +030086
87struct clk_init_item {
88 struct device_node *node;
89 struct clk_hw *hw;
90 ti_of_clk_init_cb_t func;
91 struct list_head link;
92};
93
94static LIST_HEAD(retry_list);
95
96/**
97 * ti_clk_retry_init - retries a failed clock init at later phase
98 * @node: device not for the clock
99 * @hw: partially initialized clk_hw struct for the clock
100 * @func: init function to be called for the clock
101 *
102 * Adds a failed clock init to the retry list. The retry list is parsed
103 * once all the other clocks have been initialized.
104 */
105int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
106 ti_of_clk_init_cb_t func)
107{
108 struct clk_init_item *retry;
109
110 pr_debug("%s: adding to retry list...\n", node->name);
111 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
112 if (!retry)
113 return -ENOMEM;
114
115 retry->node = node;
116 retry->func = func;
117 retry->hw = hw;
118 list_add(&retry->link, &retry_list);
119
120 return 0;
121}
122
123/**
124 * ti_clk_get_reg_addr - get register address for a clock register
125 * @node: device node for the clock
126 * @index: register index from the clock node
127 *
128 * Builds clock register address from device tree information. This
Tero Kristoc807dbe2015-02-23 21:06:08 +0200129 * is a struct of type clk_omap_reg. Returns a pointer to the register
130 * address, or a pointer error value in failure.
Tero Kristo819b4862013-10-22 11:39:36 +0300131 */
132void __iomem *ti_clk_get_reg_addr(struct device_node *node, int index)
133{
134 struct clk_omap_reg *reg;
135 u32 val;
136 u32 tmp;
Tero Kristoc08ee142014-09-12 15:01:57 +0300137 int i;
Tero Kristo819b4862013-10-22 11:39:36 +0300138
139 reg = (struct clk_omap_reg *)&tmp;
Tero Kristoc08ee142014-09-12 15:01:57 +0300140
141 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
142 if (clocks_node_ptr[i] == node->parent)
143 break;
144 }
145
146 if (i == CLK_MAX_MEMMAPS) {
147 pr_err("clk-provider not found for %s!\n", node->name);
Tero Kristoc807dbe2015-02-23 21:06:08 +0200148 return ERR_PTR(-ENOENT);
Tero Kristoc08ee142014-09-12 15:01:57 +0300149 }
150
151 reg->index = i;
Tero Kristo819b4862013-10-22 11:39:36 +0300152
153 if (of_property_read_u32_index(node, "reg", index, &val)) {
154 pr_err("%s must have reg[%d]!\n", node->name, index);
Tero Kristoc807dbe2015-02-23 21:06:08 +0200155 return ERR_PTR(-EINVAL);
Tero Kristo819b4862013-10-22 11:39:36 +0300156 }
157
158 reg->offset = val;
159
160 return (void __iomem *)tmp;
161}
162
163/**
164 * ti_dt_clk_init_provider - init master clock provider
165 * @parent: master node
166 * @index: internal index for clk_reg_ops
167 *
Tero Kristoc08ee142014-09-12 15:01:57 +0300168 * Initializes a master clock IP block. This basically sets up the
169 * mapping from clocks node to the memory map index. All the clocks
170 * are then initialized through the common of_clk_init call, and the
171 * clocks will access their memory maps based on the node layout.
Tero Kristo819b4862013-10-22 11:39:36 +0300172 */
173void ti_dt_clk_init_provider(struct device_node *parent, int index)
174{
Tero Kristo819b4862013-10-22 11:39:36 +0300175 struct device_node *clocks;
Tero Kristo819b4862013-10-22 11:39:36 +0300176
177 /* get clocks for this parent */
178 clocks = of_get_child_by_name(parent, "clocks");
179 if (!clocks) {
180 pr_err("%s missing 'clocks' child node.\n", parent->name);
181 return;
182 }
183
Tero Kristoc08ee142014-09-12 15:01:57 +0300184 /* add clocks node info */
185 clocks_node_ptr[index] = clocks;
186}
Tero Kristo819b4862013-10-22 11:39:36 +0300187
Tero Kristoc08ee142014-09-12 15:01:57 +0300188/**
189 * ti_dt_clk_init_retry_clks - init clocks from the retry list
190 *
191 * Initializes any clocks that have failed to initialize before,
192 * reasons being missing parent node(s) during earlier init. This
193 * typically happens only for DPLLs which need to have both of their
194 * parent clocks ready during init.
195 */
196void ti_dt_clk_init_retry_clks(void)
197{
198 struct clk_init_item *retry;
199 struct clk_init_item *tmp;
200 int retries = 5;
201
202 while (!list_empty(&retry_list) && retries) {
203 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
204 pr_debug("retry-init: %s\n", retry->node->name);
205 retry->func(retry->hw, retry->node);
206 list_del(&retry->link);
207 kfree(retry);
208 }
209 retries--;
Tero Kristo819b4862013-10-22 11:39:36 +0300210 }
211}
Tero Kristoc82f8952014-12-16 18:20:46 +0200212
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100213#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
Tero Kristoc82f8952014-12-16 18:20:46 +0200214void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
215{
216 while (*patch) {
217 memcpy((*patch)->patch, *patch, sizeof(**patch));
218 patch++;
219 }
220}
221
222struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
223{
224 struct clk *clk;
225 struct ti_clk_fixed *fixed;
226 struct ti_clk_fixed_factor *fixed_factor;
227 struct clk_hw *clk_hw;
228
229 if (setup->clk)
230 return setup->clk;
231
232 switch (setup->type) {
233 case TI_CLK_FIXED:
234 fixed = setup->data;
235
236 clk = clk_register_fixed_rate(NULL, setup->name, NULL,
237 CLK_IS_ROOT, fixed->frequency);
238 break;
Tero Kristo7c18a652014-12-16 18:20:47 +0200239 case TI_CLK_MUX:
240 clk = ti_clk_register_mux(setup);
241 break;
Tero Kristod96f7742014-12-16 18:20:50 +0200242 case TI_CLK_DIVIDER:
243 clk = ti_clk_register_divider(setup);
244 break;
Tero Kristob26bcf92014-12-16 18:20:52 +0200245 case TI_CLK_COMPOSITE:
246 clk = ti_clk_register_composite(setup);
247 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200248 case TI_CLK_FIXED_FACTOR:
249 fixed_factor = setup->data;
250
251 clk = clk_register_fixed_factor(NULL, setup->name,
252 fixed_factor->parent,
253 0, fixed_factor->mult,
254 fixed_factor->div);
255 break;
Tero Kristof1876162014-12-16 18:20:48 +0200256 case TI_CLK_GATE:
257 clk = ti_clk_register_gate(setup);
258 break;
Tero Kristoed405a22015-01-29 22:24:28 +0200259 case TI_CLK_DPLL:
260 clk = ti_clk_register_dpll(setup);
261 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200262 default:
263 pr_err("bad type for %s!\n", setup->name);
264 clk = ERR_PTR(-EINVAL);
265 }
266
267 if (!IS_ERR(clk)) {
268 setup->clk = clk;
269 if (setup->clkdm_name) {
270 if (__clk_get_flags(clk) & CLK_IS_BASIC) {
271 pr_warn("can't setup clkdm for basic clk %s\n",
272 setup->name);
273 } else {
274 clk_hw = __clk_get_hw(clk);
275 to_clk_hw_omap(clk_hw)->clkdm_name =
276 setup->clkdm_name;
277 omap2_init_clk_clkdm(clk_hw);
278 }
279 }
280 }
281
282 return clk;
283}
284
285int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
286{
287 struct clk *clk;
288 bool retry;
289 struct ti_clk_alias *retry_clk;
290 struct ti_clk_alias *tmp;
291
292 while (clks->clk) {
293 clk = ti_clk_register_clk(clks->clk);
294 if (IS_ERR(clk)) {
295 if (PTR_ERR(clk) == -EAGAIN) {
296 list_add(&clks->link, &retry_list);
297 } else {
298 pr_err("register for %s failed: %ld\n",
299 clks->clk->name, PTR_ERR(clk));
300 return PTR_ERR(clk);
301 }
302 } else {
303 clks->lk.clk = clk;
304 clkdev_add(&clks->lk);
305 }
306 clks++;
307 }
308
309 retry = true;
310
311 while (!list_empty(&retry_list) && retry) {
312 retry = false;
313 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
314 pr_debug("retry-init: %s\n", retry_clk->clk->name);
315 clk = ti_clk_register_clk(retry_clk->clk);
316 if (IS_ERR(clk)) {
317 if (PTR_ERR(clk) == -EAGAIN) {
318 continue;
319 } else {
320 pr_err("register for %s failed: %ld\n",
321 retry_clk->clk->name,
322 PTR_ERR(clk));
323 return PTR_ERR(clk);
324 }
325 } else {
326 retry = true;
327 retry_clk->lk.clk = clk;
328 clkdev_add(&retry_clk->lk);
329 list_del(&retry_clk->link);
330 }
331 }
332 }
333
334 return 0;
335}
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100336#endif
Tero Kristof3b19aa2015-02-27 17:54:14 +0200337
338/**
339 * ti_clk_setup_features - setup clock features flags
340 * @features: features definition to use
341 *
342 * Initializes the clock driver features flags based on platform
343 * provided data. No return value.
344 */
345void __init ti_clk_setup_features(struct ti_clk_features *features)
346{
347 memcpy(&ti_clk_features, features, sizeof(*features));
348}
349
350/**
351 * ti_clk_get_features - get clock driver features flags
352 *
353 * Get TI clock driver features description. Returns a pointer
354 * to the current feature setup.
355 */
356const struct ti_clk_features *ti_clk_get_features(void)
357{
358 return &ti_clk_features;
359}
Tero Kristoa5aa8a62015-03-03 10:51:01 +0200360
361/**
362 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
363 * @clk_names: ptr to an array of strings of clock names to enable
364 * @num_clocks: number of clock names in @clk_names
365 *
366 * Prepare and enable a list of clocks, named by @clk_names. No
367 * return value. XXX Deprecated; only needed until these clocks are
368 * properly claimed and enabled by the drivers or core code that uses
369 * them. XXX What code disables & calls clk_put on these clocks?
370 */
371void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
372{
373 struct clk *init_clk;
374 int i;
375
376 for (i = 0; i < num_clocks; i++) {
377 init_clk = clk_get(NULL, clk_names[i]);
378 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
379 clk_names[i]))
380 continue;
381 clk_prepare_enable(init_clk);
382 }
383}