1 /*
2  *  linux/arch/arm/plat-omap/clock.c
3  *
4  *  Copyright (C) 2004 - 2008 Nokia corporation
5  *  Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6  *
7  *  Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/string.h>
19 #include <linux/clk.h>
20 #include <linux/mutex.h>
21 #include <linux/cpufreq.h>
22 #include <linux/debugfs.h>
23 #include <linux/io.h>
24 
25 #include <plat/clock.h>
26 
27 static LIST_HEAD(clocks);
28 static DEFINE_MUTEX(clocks_mutex);
29 static DEFINE_SPINLOCK(clockfw_lock);
30 
31 static struct clk_functions *arch_clock;
32 
33 /*
34  * Standard clock functions defined in include/linux/clk.h
35  */
36 
clk_enable(struct clk * clk)37 int clk_enable(struct clk *clk)
38 {
39 	unsigned long flags;
40 	int ret;
41 
42 	if (clk == NULL || IS_ERR(clk))
43 		return -EINVAL;
44 
45 	if (!arch_clock || !arch_clock->clk_enable)
46 		return -EINVAL;
47 
48 	spin_lock_irqsave(&clockfw_lock, flags);
49 	ret = arch_clock->clk_enable(clk);
50 	spin_unlock_irqrestore(&clockfw_lock, flags);
51 
52 	return ret;
53 }
54 EXPORT_SYMBOL(clk_enable);
55 
clk_disable(struct clk * clk)56 void clk_disable(struct clk *clk)
57 {
58 	unsigned long flags;
59 
60 	if (clk == NULL || IS_ERR(clk))
61 		return;
62 
63 	if (!arch_clock || !arch_clock->clk_disable)
64 		return;
65 
66 	spin_lock_irqsave(&clockfw_lock, flags);
67 	if (clk->usecount == 0) {
68 		pr_err("Trying disable clock %s with 0 usecount\n",
69 		       clk->name);
70 		WARN_ON(1);
71 		goto out;
72 	}
73 
74 	arch_clock->clk_disable(clk);
75 
76 out:
77 	spin_unlock_irqrestore(&clockfw_lock, flags);
78 }
79 EXPORT_SYMBOL(clk_disable);
80 
clk_get_rate(struct clk * clk)81 unsigned long clk_get_rate(struct clk *clk)
82 {
83 	unsigned long flags;
84 	unsigned long ret;
85 
86 	if (clk == NULL || IS_ERR(clk))
87 		return 0;
88 
89 	spin_lock_irqsave(&clockfw_lock, flags);
90 	ret = clk->rate;
91 	spin_unlock_irqrestore(&clockfw_lock, flags);
92 
93 	return ret;
94 }
95 EXPORT_SYMBOL(clk_get_rate);
96 
97 /*
98  * Optional clock functions defined in include/linux/clk.h
99  */
100 
clk_round_rate(struct clk * clk,unsigned long rate)101 long clk_round_rate(struct clk *clk, unsigned long rate)
102 {
103 	unsigned long flags;
104 	long ret;
105 
106 	if (clk == NULL || IS_ERR(clk))
107 		return 0;
108 
109 	if (!arch_clock || !arch_clock->clk_round_rate)
110 		return 0;
111 
112 	spin_lock_irqsave(&clockfw_lock, flags);
113 	ret = arch_clock->clk_round_rate(clk, rate);
114 	spin_unlock_irqrestore(&clockfw_lock, flags);
115 
116 	return ret;
117 }
118 EXPORT_SYMBOL(clk_round_rate);
119 
clk_set_rate(struct clk * clk,unsigned long rate)120 int clk_set_rate(struct clk *clk, unsigned long rate)
121 {
122 	unsigned long flags;
123 	int ret = -EINVAL;
124 
125 	if (clk == NULL || IS_ERR(clk))
126 		return ret;
127 
128 	if (!arch_clock || !arch_clock->clk_set_rate)
129 		return ret;
130 
131 	spin_lock_irqsave(&clockfw_lock, flags);
132 	ret = arch_clock->clk_set_rate(clk, rate);
133 	if (ret == 0)
134 		propagate_rate(clk);
135 	spin_unlock_irqrestore(&clockfw_lock, flags);
136 
137 	return ret;
138 }
139 EXPORT_SYMBOL(clk_set_rate);
140 
clk_set_parent(struct clk * clk,struct clk * parent)141 int clk_set_parent(struct clk *clk, struct clk *parent)
142 {
143 	unsigned long flags;
144 	int ret = -EINVAL;
145 
146 	if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
147 		return ret;
148 
149 	if (!arch_clock || !arch_clock->clk_set_parent)
150 		return ret;
151 
152 	spin_lock_irqsave(&clockfw_lock, flags);
153 	if (clk->usecount == 0) {
154 		ret = arch_clock->clk_set_parent(clk, parent);
155 		if (ret == 0)
156 			propagate_rate(clk);
157 	} else
158 		ret = -EBUSY;
159 	spin_unlock_irqrestore(&clockfw_lock, flags);
160 
161 	return ret;
162 }
163 EXPORT_SYMBOL(clk_set_parent);
164 
clk_get_parent(struct clk * clk)165 struct clk *clk_get_parent(struct clk *clk)
166 {
167 	return clk->parent;
168 }
169 EXPORT_SYMBOL(clk_get_parent);
170 
171 /*
172  * OMAP specific clock functions shared between omap1 and omap2
173  */
174 
175 int __initdata mpurate;
176 
177 /*
178  * By default we use the rate set by the bootloader.
179  * You can override this with mpurate= cmdline option.
180  */
omap_clk_setup(char * str)181 static int __init omap_clk_setup(char *str)
182 {
183 	get_option(&str, &mpurate);
184 
185 	if (!mpurate)
186 		return 1;
187 
188 	if (mpurate < 1000)
189 		mpurate *= 1000000;
190 
191 	return 1;
192 }
193 __setup("mpurate=", omap_clk_setup);
194 
195 /* Used for clocks that always have same value as the parent clock */
followparent_recalc(struct clk * clk)196 unsigned long followparent_recalc(struct clk *clk)
197 {
198 	return clk->parent->rate;
199 }
200 
201 /*
202  * Used for clocks that have the same value as the parent clock,
203  * divided by some factor
204  */
omap_fixed_divisor_recalc(struct clk * clk)205 unsigned long omap_fixed_divisor_recalc(struct clk *clk)
206 {
207 	WARN_ON(!clk->fixed_div);
208 
209 	return clk->parent->rate / clk->fixed_div;
210 }
211 
clk_reparent(struct clk * child,struct clk * parent)212 void clk_reparent(struct clk *child, struct clk *parent)
213 {
214 	list_del_init(&child->sibling);
215 	if (parent)
216 		list_add(&child->sibling, &parent->children);
217 	child->parent = parent;
218 
219 	/* now do the debugfs renaming to reattach the child
220 	   to the proper parent */
221 }
222 
223 /* Propagate rate to children */
propagate_rate(struct clk * tclk)224 void propagate_rate(struct clk *tclk)
225 {
226 	struct clk *clkp;
227 
228 	list_for_each_entry(clkp, &tclk->children, sibling) {
229 		if (clkp->recalc)
230 			clkp->rate = clkp->recalc(clkp);
231 		propagate_rate(clkp);
232 	}
233 }
234 
235 static LIST_HEAD(root_clks);
236 
237 /**
238  * recalculate_root_clocks - recalculate and propagate all root clocks
239  *
240  * Recalculates all root clocks (clocks with no parent), which if the
241  * clock's .recalc is set correctly, should also propagate their rates.
242  * Called at init.
243  */
recalculate_root_clocks(void)244 void recalculate_root_clocks(void)
245 {
246 	struct clk *clkp;
247 
248 	list_for_each_entry(clkp, &root_clks, sibling) {
249 		if (clkp->recalc)
250 			clkp->rate = clkp->recalc(clkp);
251 		propagate_rate(clkp);
252 	}
253 }
254 
255 /**
256  * clk_preinit - initialize any fields in the struct clk before clk init
257  * @clk: struct clk * to initialize
258  *
259  * Initialize any struct clk fields needed before normal clk initialization
260  * can run.  No return value.
261  */
clk_preinit(struct clk * clk)262 void clk_preinit(struct clk *clk)
263 {
264 	INIT_LIST_HEAD(&clk->children);
265 }
266 
clk_register(struct clk * clk)267 int clk_register(struct clk *clk)
268 {
269 	if (clk == NULL || IS_ERR(clk))
270 		return -EINVAL;
271 
272 	/*
273 	 * trap out already registered clocks
274 	 */
275 	if (clk->node.next || clk->node.prev)
276 		return 0;
277 
278 	mutex_lock(&clocks_mutex);
279 	if (clk->parent)
280 		list_add(&clk->sibling, &clk->parent->children);
281 	else
282 		list_add(&clk->sibling, &root_clks);
283 
284 	list_add(&clk->node, &clocks);
285 	if (clk->init)
286 		clk->init(clk);
287 	mutex_unlock(&clocks_mutex);
288 
289 	return 0;
290 }
291 EXPORT_SYMBOL(clk_register);
292 
clk_unregister(struct clk * clk)293 void clk_unregister(struct clk *clk)
294 {
295 	if (clk == NULL || IS_ERR(clk))
296 		return;
297 
298 	mutex_lock(&clocks_mutex);
299 	list_del(&clk->sibling);
300 	list_del(&clk->node);
301 	mutex_unlock(&clocks_mutex);
302 }
303 EXPORT_SYMBOL(clk_unregister);
304 
clk_enable_init_clocks(void)305 void clk_enable_init_clocks(void)
306 {
307 	struct clk *clkp;
308 
309 	list_for_each_entry(clkp, &clocks, node) {
310 		if (clkp->flags & ENABLE_ON_INIT)
311 			clk_enable(clkp);
312 	}
313 }
314 
315 /**
316  * omap_clk_get_by_name - locate OMAP struct clk by its name
317  * @name: name of the struct clk to locate
318  *
319  * Locate an OMAP struct clk by its name.  Assumes that struct clk
320  * names are unique.  Returns NULL if not found or a pointer to the
321  * struct clk if found.
322  */
omap_clk_get_by_name(const char * name)323 struct clk *omap_clk_get_by_name(const char *name)
324 {
325 	struct clk *c;
326 	struct clk *ret = NULL;
327 
328 	mutex_lock(&clocks_mutex);
329 
330 	list_for_each_entry(c, &clocks, node) {
331 		if (!strcmp(c->name, name)) {
332 			ret = c;
333 			break;
334 		}
335 	}
336 
337 	mutex_unlock(&clocks_mutex);
338 
339 	return ret;
340 }
341 
omap_clk_enable_autoidle_all(void)342 int omap_clk_enable_autoidle_all(void)
343 {
344 	struct clk *c;
345 	unsigned long flags;
346 
347 	spin_lock_irqsave(&clockfw_lock, flags);
348 
349 	list_for_each_entry(c, &clocks, node)
350 		if (c->ops->allow_idle)
351 			c->ops->allow_idle(c);
352 
353 	spin_unlock_irqrestore(&clockfw_lock, flags);
354 
355 	return 0;
356 }
357 
omap_clk_disable_autoidle_all(void)358 int omap_clk_disable_autoidle_all(void)
359 {
360 	struct clk *c;
361 	unsigned long flags;
362 
363 	spin_lock_irqsave(&clockfw_lock, flags);
364 
365 	list_for_each_entry(c, &clocks, node)
366 		if (c->ops->deny_idle)
367 			c->ops->deny_idle(c);
368 
369 	spin_unlock_irqrestore(&clockfw_lock, flags);
370 
371 	return 0;
372 }
373 
374 /*
375  * Low level helpers
376  */
clkll_enable_null(struct clk * clk)377 static int clkll_enable_null(struct clk *clk)
378 {
379 	return 0;
380 }
381 
clkll_disable_null(struct clk * clk)382 static void clkll_disable_null(struct clk *clk)
383 {
384 }
385 
386 const struct clkops clkops_null = {
387 	.enable		= clkll_enable_null,
388 	.disable	= clkll_disable_null,
389 };
390 
391 /*
392  * Dummy clock
393  *
394  * Used for clock aliases that are needed on some OMAPs, but not others
395  */
396 struct clk dummy_ck = {
397 	.name	= "dummy",
398 	.ops	= &clkops_null,
399 };
400 
401 #ifdef CONFIG_CPU_FREQ
clk_init_cpufreq_table(struct cpufreq_frequency_table ** table)402 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
403 {
404 	unsigned long flags;
405 
406 	if (!arch_clock || !arch_clock->clk_init_cpufreq_table)
407 		return;
408 
409 	spin_lock_irqsave(&clockfw_lock, flags);
410 	arch_clock->clk_init_cpufreq_table(table);
411 	spin_unlock_irqrestore(&clockfw_lock, flags);
412 }
413 
clk_exit_cpufreq_table(struct cpufreq_frequency_table ** table)414 void clk_exit_cpufreq_table(struct cpufreq_frequency_table **table)
415 {
416 	unsigned long flags;
417 
418 	if (!arch_clock || !arch_clock->clk_exit_cpufreq_table)
419 		return;
420 
421 	spin_lock_irqsave(&clockfw_lock, flags);
422 	arch_clock->clk_exit_cpufreq_table(table);
423 	spin_unlock_irqrestore(&clockfw_lock, flags);
424 }
425 #endif
426 
427 /*
428  *
429  */
430 
431 #ifdef CONFIG_OMAP_RESET_CLOCKS
432 /*
433  * Disable any unused clocks left on by the bootloader
434  */
clk_disable_unused(void)435 static int __init clk_disable_unused(void)
436 {
437 	struct clk *ck;
438 	unsigned long flags;
439 
440 	if (!arch_clock || !arch_clock->clk_disable_unused)
441 		return 0;
442 
443 	pr_info("clock: disabling unused clocks to save power\n");
444 	list_for_each_entry(ck, &clocks, node) {
445 		if (ck->ops == &clkops_null)
446 			continue;
447 
448 		if (ck->usecount > 0 || !ck->enable_reg)
449 			continue;
450 
451 		spin_lock_irqsave(&clockfw_lock, flags);
452 		arch_clock->clk_disable_unused(ck);
453 		spin_unlock_irqrestore(&clockfw_lock, flags);
454 	}
455 
456 	return 0;
457 }
458 late_initcall(clk_disable_unused);
459 late_initcall(omap_clk_enable_autoidle_all);
460 #endif
461 
clk_init(struct clk_functions * custom_clocks)462 int __init clk_init(struct clk_functions * custom_clocks)
463 {
464 	if (!custom_clocks) {
465 		pr_err("No custom clock functions registered\n");
466 		BUG();
467 	}
468 
469 	arch_clock = custom_clocks;
470 
471 	return 0;
472 }
473 
474 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
475 /*
476  *	debugfs support to trace clock tree hierarchy and attributes
477  */
478 static struct dentry *clk_debugfs_root;
479 
clk_debugfs_register_one(struct clk * c)480 static int clk_debugfs_register_one(struct clk *c)
481 {
482 	int err;
483 	struct dentry *d, *child, *child_tmp;
484 	struct clk *pa = c->parent;
485 	char s[255];
486 	char *p = s;
487 
488 	p += sprintf(p, "%s", c->name);
489 	d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
490 	if (!d)
491 		return -ENOMEM;
492 	c->dent = d;
493 
494 	d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
495 	if (!d) {
496 		err = -ENOMEM;
497 		goto err_out;
498 	}
499 	d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
500 	if (!d) {
501 		err = -ENOMEM;
502 		goto err_out;
503 	}
504 	d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
505 	if (!d) {
506 		err = -ENOMEM;
507 		goto err_out;
508 	}
509 	return 0;
510 
511 err_out:
512 	d = c->dent;
513 	list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
514 		debugfs_remove(child);
515 	debugfs_remove(c->dent);
516 	return err;
517 }
518 
clk_debugfs_register(struct clk * c)519 static int clk_debugfs_register(struct clk *c)
520 {
521 	int err;
522 	struct clk *pa = c->parent;
523 
524 	if (pa && !pa->dent) {
525 		err = clk_debugfs_register(pa);
526 		if (err)
527 			return err;
528 	}
529 
530 	if (!c->dent) {
531 		err = clk_debugfs_register_one(c);
532 		if (err)
533 			return err;
534 	}
535 	return 0;
536 }
537 
clk_debugfs_init(void)538 static int __init clk_debugfs_init(void)
539 {
540 	struct clk *c;
541 	struct dentry *d;
542 	int err;
543 
544 	d = debugfs_create_dir("clock", NULL);
545 	if (!d)
546 		return -ENOMEM;
547 	clk_debugfs_root = d;
548 
549 	list_for_each_entry(c, &clocks, node) {
550 		err = clk_debugfs_register(c);
551 		if (err)
552 			goto err_out;
553 	}
554 	return 0;
555 err_out:
556 	debugfs_remove_recursive(clk_debugfs_root);
557 	return err;
558 }
559 late_initcall(clk_debugfs_init);
560 
561 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */
562