blob: 868cb76934997f52d2361a8b79fdcd8ca9cf0441 [file] [log] [blame]
Vladimir Barinov3e062b02007-06-05 16:36:55 +01001/*
Kevin Hilmanc5b736d2009-03-20 17:29:01 -07002 * Clock and PLL control for DaVinci devices
Vladimir Barinov3e062b02007-06-05 16:36:55 +01003 *
Kevin Hilmanc5b736d2009-03-20 17:29:01 -07004 * Copyright (C) 2006-2007 Texas Instruments.
5 * Copyright (C) 2008-2009 Deep Root Systems, LLC
Vladimir Barinov3e062b02007-06-05 16:36:55 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/errno.h>
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070017#include <linux/clk.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010018#include <linux/err.h>
19#include <linux/mutex.h>
Russell Kingfced80c2008-09-06 12:10:45 +010020#include <linux/io.h>
Sekhar Norid6a61562009-08-31 15:48:03 +053021#include <linux/delay.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010022
Russell Kinga09e64f2008-08-05 16:14:15 +010023#include <mach/hardware.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010024
Kevin Hilman28552c22010-02-25 15:36:38 -080025#include <mach/clock.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/psc.h>
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070027#include <mach/cputype.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +010028#include "clock.h"
29
Vladimir Barinov3e062b02007-06-05 16:36:55 +010030static LIST_HEAD(clocks);
31static DEFINE_MUTEX(clocks_mutex);
32static DEFINE_SPINLOCK(clockfw_lock);
33
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070034static unsigned psc_domain(struct clk *clk)
Vladimir Barinov3e062b02007-06-05 16:36:55 +010035{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070036 return (clk->flags & PSC_DSP)
37 ? DAVINCI_GPSC_DSPDOMAIN
38 : DAVINCI_GPSC_ARMDOMAIN;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010039}
Vladimir Barinov3e062b02007-06-05 16:36:55 +010040
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070041static void __clk_enable(struct clk *clk)
Vladimir Barinov3e062b02007-06-05 16:36:55 +010042{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070043 if (clk->parent)
44 __clk_enable(clk->parent);
45 if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
Cyril Chemparathy52958be2010-03-25 17:43:47 -040046 davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
47 PSC_STATE_ENABLE);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010048}
49
50static void __clk_disable(struct clk *clk)
51{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070052 if (WARN_ON(clk->usecount == 0))
Vladimir Barinov3e062b02007-06-05 16:36:55 +010053 return;
Chaithrika U S679f9212009-12-15 18:02:58 +053054 if (--clk->usecount == 0 && !(clk->flags & CLK_PLL) &&
55 (clk->flags & CLK_PSC))
Cyril Chemparathy52958be2010-03-25 17:43:47 -040056 davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
57 (clk->flags & PSC_SWRSTDISABLE) ?
58 PSC_STATE_SWRSTDISABLE : PSC_STATE_DISABLE);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070059 if (clk->parent)
60 __clk_disable(clk->parent);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010061}
62
63int clk_enable(struct clk *clk)
64{
65 unsigned long flags;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010066
67 if (clk == NULL || IS_ERR(clk))
68 return -EINVAL;
69
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070070 spin_lock_irqsave(&clockfw_lock, flags);
71 __clk_enable(clk);
72 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010073
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070074 return 0;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010075}
76EXPORT_SYMBOL(clk_enable);
77
78void clk_disable(struct clk *clk)
79{
80 unsigned long flags;
81
82 if (clk == NULL || IS_ERR(clk))
83 return;
84
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070085 spin_lock_irqsave(&clockfw_lock, flags);
86 __clk_disable(clk);
87 spin_unlock_irqrestore(&clockfw_lock, flags);
Vladimir Barinov3e062b02007-06-05 16:36:55 +010088}
89EXPORT_SYMBOL(clk_disable);
90
91unsigned long clk_get_rate(struct clk *clk)
92{
93 if (clk == NULL || IS_ERR(clk))
94 return -EINVAL;
95
Kevin Hilmanc5b736d2009-03-20 17:29:01 -070096 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +010097}
98EXPORT_SYMBOL(clk_get_rate);
99
100long clk_round_rate(struct clk *clk, unsigned long rate)
101{
102 if (clk == NULL || IS_ERR(clk))
103 return -EINVAL;
104
Sekhar Norid6a61562009-08-31 15:48:03 +0530105 if (clk->round_rate)
106 return clk->round_rate(clk, rate);
107
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700108 return clk->rate;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100109}
110EXPORT_SYMBOL(clk_round_rate);
111
Sekhar Norid6a61562009-08-31 15:48:03 +0530112/* Propagate rate to children */
113static void propagate_rate(struct clk *root)
114{
115 struct clk *clk;
116
117 list_for_each_entry(clk, &root->children, childnode) {
118 if (clk->recalc)
119 clk->rate = clk->recalc(clk);
120 propagate_rate(clk);
121 }
122}
123
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100124int clk_set_rate(struct clk *clk, unsigned long rate)
125{
Sekhar Norid6a61562009-08-31 15:48:03 +0530126 unsigned long flags;
127 int ret = -EINVAL;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100128
Sekhar Norid6a61562009-08-31 15:48:03 +0530129 if (clk == NULL || IS_ERR(clk))
130 return ret;
131
Sekhar Norid6a61562009-08-31 15:48:03 +0530132 if (clk->set_rate)
133 ret = clk->set_rate(clk, rate);
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530134
135 spin_lock_irqsave(&clockfw_lock, flags);
Sekhar Norid6a61562009-08-31 15:48:03 +0530136 if (ret == 0) {
137 if (clk->recalc)
138 clk->rate = clk->recalc(clk);
139 propagate_rate(clk);
140 }
141 spin_unlock_irqrestore(&clockfw_lock, flags);
142
143 return ret;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100144}
145EXPORT_SYMBOL(clk_set_rate);
146
Sekhar Norib82a51e2009-08-31 15:48:04 +0530147int clk_set_parent(struct clk *clk, struct clk *parent)
148{
149 unsigned long flags;
150
151 if (clk == NULL || IS_ERR(clk))
152 return -EINVAL;
153
154 /* Cannot change parent on enabled clock */
155 if (WARN_ON(clk->usecount))
156 return -EINVAL;
157
158 mutex_lock(&clocks_mutex);
159 clk->parent = parent;
160 list_del_init(&clk->childnode);
161 list_add(&clk->childnode, &clk->parent->children);
162 mutex_unlock(&clocks_mutex);
163
164 spin_lock_irqsave(&clockfw_lock, flags);
165 if (clk->recalc)
166 clk->rate = clk->recalc(clk);
167 propagate_rate(clk);
168 spin_unlock_irqrestore(&clockfw_lock, flags);
169
170 return 0;
171}
172EXPORT_SYMBOL(clk_set_parent);
173
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100174int clk_register(struct clk *clk)
175{
176 if (clk == NULL || IS_ERR(clk))
177 return -EINVAL;
178
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700179 if (WARN(clk->parent && !clk->parent->rate,
180 "CLK: %s parent %s has no rate!\n",
181 clk->name, clk->parent->name))
182 return -EINVAL;
183
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530184 INIT_LIST_HEAD(&clk->children);
185
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100186 mutex_lock(&clocks_mutex);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700187 list_add_tail(&clk->node, &clocks);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530188 if (clk->parent)
189 list_add_tail(&clk->childnode, &clk->parent->children);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100190 mutex_unlock(&clocks_mutex);
191
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700192 /* If rate is already set, use it */
193 if (clk->rate)
194 return 0;
195
Sekhar Noride381a92009-08-31 15:48:02 +0530196 /* Else, see if there is a way to calculate it */
197 if (clk->recalc)
198 clk->rate = clk->recalc(clk);
199
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700200 /* Otherwise, default to parent rate */
Sekhar Noride381a92009-08-31 15:48:02 +0530201 else if (clk->parent)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700202 clk->rate = clk->parent->rate;
203
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100204 return 0;
205}
206EXPORT_SYMBOL(clk_register);
207
208void clk_unregister(struct clk *clk)
209{
210 if (clk == NULL || IS_ERR(clk))
211 return;
212
213 mutex_lock(&clocks_mutex);
214 list_del(&clk->node);
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530215 list_del(&clk->childnode);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100216 mutex_unlock(&clocks_mutex);
217}
218EXPORT_SYMBOL(clk_unregister);
219
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700220#ifdef CONFIG_DAVINCI_RESET_CLOCKS
221/*
222 * Disable any unused clocks left on by the bootloader
223 */
224static int __init clk_disable_unused(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100225{
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700226 struct clk *ck;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100227
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700228 spin_lock_irq(&clockfw_lock);
229 list_for_each_entry(ck, &clocks, node) {
230 if (ck->usecount > 0)
231 continue;
232 if (!(ck->flags & CLK_PSC))
233 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100234
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700235 /* ignore if in Disabled or SwRstDisable states */
Sergei Shtylyov789a7852009-09-30 19:48:03 +0400236 if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700237 continue;
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100238
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700239 pr_info("Clocks: disable unused %s\n", ck->name);
Cyril Chemparathy52958be2010-03-25 17:43:47 -0400240
241 davinci_psc_config(psc_domain(ck), ck->gpsc, ck->lpsc,
242 (ck->flags & PSC_SWRSTDISABLE) ?
243 PSC_STATE_SWRSTDISABLE : PSC_STATE_DISABLE);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700244 }
245 spin_unlock_irq(&clockfw_lock);
246
247 return 0;
248}
249late_initcall(clk_disable_unused);
250#endif
251
Sekhar Noride381a92009-08-31 15:48:02 +0530252static unsigned long clk_sysclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700253{
254 u32 v, plldiv;
255 struct pll_data *pll;
Sekhar Noride381a92009-08-31 15:48:02 +0530256 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700257
258 /* If this is the PLL base clock, no more calculations needed */
259 if (clk->pll_data)
Sekhar Noride381a92009-08-31 15:48:02 +0530260 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700261
262 if (WARN_ON(!clk->parent))
Sekhar Noride381a92009-08-31 15:48:02 +0530263 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700264
Sekhar Noride381a92009-08-31 15:48:02 +0530265 rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700266
267 /* Otherwise, the parent must be a PLL */
268 if (WARN_ON(!clk->parent->pll_data))
Sekhar Noride381a92009-08-31 15:48:02 +0530269 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700270
271 pll = clk->parent->pll_data;
272
273 /* If pre-PLL, source clock is before the multiplier and divider(s) */
274 if (clk->flags & PRE_PLL)
Sekhar Noride381a92009-08-31 15:48:02 +0530275 rate = pll->input_rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700276
277 if (!clk->div_reg)
Sekhar Noride381a92009-08-31 15:48:02 +0530278 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700279
280 v = __raw_readl(pll->base + clk->div_reg);
281 if (v & PLLDIV_EN) {
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400282 plldiv = (v & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700283 if (plldiv)
Sekhar Noride381a92009-08-31 15:48:02 +0530284 rate /= plldiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700285 }
Sekhar Noride381a92009-08-31 15:48:02 +0530286
287 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700288}
289
Sekhar Noride381a92009-08-31 15:48:02 +0530290static unsigned long clk_leafclk_recalc(struct clk *clk)
291{
292 if (WARN_ON(!clk->parent))
293 return clk->rate;
294
295 return clk->parent->rate;
296}
297
298static unsigned long clk_pllclk_recalc(struct clk *clk)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700299{
300 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
301 u8 bypass;
302 struct pll_data *pll = clk->pll_data;
Sekhar Noride381a92009-08-31 15:48:02 +0530303 unsigned long rate = clk->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700304
305 pll->base = IO_ADDRESS(pll->phys_base);
306 ctrl = __raw_readl(pll->base + PLLCTL);
Sekhar Noride381a92009-08-31 15:48:02 +0530307 rate = pll->input_rate = clk->parent->rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700308
309 if (ctrl & PLLCTL_PLLEN) {
310 bypass = 0;
311 mult = __raw_readl(pll->base + PLLM);
Sandeep Paulrajfb8fcb82009-06-11 09:41:05 -0400312 if (cpu_is_davinci_dm365())
313 mult = 2 * (mult & PLLM_PLLM_MASK);
314 else
315 mult = (mult & PLLM_PLLM_MASK) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700316 } else
317 bypass = 1;
318
319 if (pll->flags & PLL_HAS_PREDIV) {
320 prediv = __raw_readl(pll->base + PREDIV);
321 if (prediv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400322 prediv = (prediv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700323 else
324 prediv = 1;
325 }
326
327 /* pre-divider is fixed, but (some?) chips won't report that */
328 if (cpu_is_davinci_dm355() && pll->num == 1)
329 prediv = 8;
330
331 if (pll->flags & PLL_HAS_POSTDIV) {
332 postdiv = __raw_readl(pll->base + POSTDIV);
333 if (postdiv & PLLDIV_EN)
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400334 postdiv = (postdiv & pll->div_ratio_mask) + 1;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700335 else
336 postdiv = 1;
337 }
338
339 if (!bypass) {
Sekhar Noride381a92009-08-31 15:48:02 +0530340 rate /= prediv;
341 rate *= mult;
342 rate /= postdiv;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700343 }
344
345 pr_debug("PLL%d: input = %lu MHz [ ",
346 pll->num, clk->parent->rate / 1000000);
347 if (bypass)
348 pr_debug("bypass ");
349 if (prediv > 1)
350 pr_debug("/ %d ", prediv);
351 if (mult > 1)
352 pr_debug("* %d ", mult);
353 if (postdiv > 1)
354 pr_debug("/ %d ", postdiv);
Sekhar Noride381a92009-08-31 15:48:02 +0530355 pr_debug("] --> %lu MHz output.\n", rate / 1000000);
356
357 return rate;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700358}
359
Sekhar Norid6a61562009-08-31 15:48:03 +0530360/**
361 * davinci_set_pllrate - set the output rate of a given PLL.
362 *
363 * Note: Currently tested to work with OMAP-L138 only.
364 *
365 * @pll: pll whose rate needs to be changed.
366 * @prediv: The pre divider value. Passing 0 disables the pre-divider.
367 * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
368 * @postdiv: The post divider value. Passing 0 disables the post-divider.
369 */
370int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
371 unsigned int mult, unsigned int postdiv)
372{
373 u32 ctrl;
374 unsigned int locktime;
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530375 unsigned long flags;
Sekhar Norid6a61562009-08-31 15:48:03 +0530376
377 if (pll->base == NULL)
378 return -EINVAL;
379
380 /*
381 * PLL lock time required per OMAP-L138 datasheet is
382 * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
383 * as 4 and OSCIN cycle as 25 MHz.
384 */
385 if (prediv) {
386 locktime = ((2000 * prediv) / 100);
387 prediv = (prediv - 1) | PLLDIV_EN;
388 } else {
Sekhar Nori9a219a92009-11-16 17:21:33 +0530389 locktime = PLL_LOCK_TIME;
Sekhar Norid6a61562009-08-31 15:48:03 +0530390 }
391 if (postdiv)
392 postdiv = (postdiv - 1) | PLLDIV_EN;
393 if (mult)
394 mult = mult - 1;
395
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530396 /* Protect against simultaneous calls to PLL setting seqeunce */
397 spin_lock_irqsave(&clockfw_lock, flags);
398
Sekhar Norid6a61562009-08-31 15:48:03 +0530399 ctrl = __raw_readl(pll->base + PLLCTL);
400
401 /* Switch the PLL to bypass mode */
402 ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
403 __raw_writel(ctrl, pll->base + PLLCTL);
404
Sekhar Nori9a219a92009-11-16 17:21:33 +0530405 udelay(PLL_BYPASS_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530406
407 /* Reset and enable PLL */
408 ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
409 __raw_writel(ctrl, pll->base + PLLCTL);
410
411 if (pll->flags & PLL_HAS_PREDIV)
412 __raw_writel(prediv, pll->base + PREDIV);
413
414 __raw_writel(mult, pll->base + PLLM);
415
416 if (pll->flags & PLL_HAS_POSTDIV)
417 __raw_writel(postdiv, pll->base + POSTDIV);
418
Sekhar Nori9a219a92009-11-16 17:21:33 +0530419 udelay(PLL_RESET_TIME);
Sekhar Norid6a61562009-08-31 15:48:03 +0530420
421 /* Bring PLL out of reset */
422 ctrl |= PLLCTL_PLLRST;
423 __raw_writel(ctrl, pll->base + PLLCTL);
424
425 udelay(locktime);
426
427 /* Remove PLL from bypass mode */
428 ctrl |= PLLCTL_PLLEN;
429 __raw_writel(ctrl, pll->base + PLLCTL);
430
Sekhar Nori3b43cd62010-01-12 18:55:35 +0530431 spin_unlock_irqrestore(&clockfw_lock, flags);
432
Sekhar Norid6a61562009-08-31 15:48:03 +0530433 return 0;
434}
435EXPORT_SYMBOL(davinci_set_pllrate);
436
Kevin Hilman08aca082010-01-11 08:22:23 -0800437int __init davinci_clk_init(struct clk_lookup *clocks)
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700438 {
Kevin Hilman08aca082010-01-11 08:22:23 -0800439 struct clk_lookup *c;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700440 struct clk *clk;
Kevin Hilman08aca082010-01-11 08:22:23 -0800441 size_t num_clocks = 0;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700442
Kevin Hilman08aca082010-01-11 08:22:23 -0800443 for (c = clocks; c->clk; c++) {
444 clk = c->clk;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700445
Sekhar Noride381a92009-08-31 15:48:02 +0530446 if (!clk->recalc) {
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700447
Sekhar Noride381a92009-08-31 15:48:02 +0530448 /* Check if clock is a PLL */
449 if (clk->pll_data)
450 clk->recalc = clk_pllclk_recalc;
451
452 /* Else, if it is a PLL-derived clock */
453 else if (clk->flags & CLK_PLL)
454 clk->recalc = clk_sysclk_recalc;
455
456 /* Otherwise, it is a leaf clock (PSC clock) */
457 else if (clk->parent)
458 clk->recalc = clk_leafclk_recalc;
459 }
460
Cyril Chemparathyd6961e62010-04-14 14:44:49 -0400461 if (clk->pll_data && !clk->pll_data->div_ratio_mask)
462 clk->pll_data->div_ratio_mask = PLLDIV_RATIO_MASK;
463
Sekhar Noride381a92009-08-31 15:48:02 +0530464 if (clk->recalc)
465 clk->rate = clk->recalc(clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700466
467 if (clk->lpsc)
468 clk->flags |= CLK_PSC;
469
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700470 clk_register(clk);
Kevin Hilman08aca082010-01-11 08:22:23 -0800471 num_clocks++;
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700472
473 /* Turn on clocks that Linux doesn't otherwise manage */
474 if (clk->flags & ALWAYS_ENABLED)
475 clk_enable(clk);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100476 }
477
Kevin Hilman08aca082010-01-11 08:22:23 -0800478 clkdev_add_table(clocks, num_clocks);
479
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100480 return 0;
481}
482
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530483#ifdef CONFIG_DEBUG_FS
484
485#include <linux/debugfs.h>
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100486#include <linux/seq_file.h>
487
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700488#define CLKNAME_MAX 10 /* longest clock name */
489#define NEST_DELTA 2
490#define NEST_MAX 4
491
492static void
493dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
494{
495 char *state;
496 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
497 struct clk *clk;
498 unsigned i;
499
500 if (parent->flags & CLK_PLL)
501 state = "pll";
502 else if (parent->flags & CLK_PSC)
503 state = "psc";
504 else
505 state = "";
506
507 /* <nest spaces> name <pad to end> */
508 memset(buf, ' ', sizeof(buf) - 1);
509 buf[sizeof(buf) - 1] = 0;
510 i = strlen(parent->name);
511 memcpy(buf + nest, parent->name,
512 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
513
514 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
515 buf, parent->usecount, state, clk_get_rate(parent));
516 /* REVISIT show device associations too */
517
518 /* cost is now small, but not linear... */
Sekhar Norif02bf3b2009-08-31 15:48:01 +0530519 list_for_each_entry(clk, &parent->children, childnode) {
520 dump_clock(s, nest + NEST_DELTA, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700521 }
522}
523
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100524static int davinci_ck_show(struct seq_file *m, void *v)
525{
Sekhar Norif979aa62009-12-03 15:36:51 +0530526 struct clk *clk;
527
528 /*
529 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700530 */
531 mutex_lock(&clocks_mutex);
Sekhar Norif979aa62009-12-03 15:36:51 +0530532 list_for_each_entry(clk, &clocks, node)
533 if (!clk->parent)
534 dump_clock(m, 0, clk);
Kevin Hilmanc5b736d2009-03-20 17:29:01 -0700535 mutex_unlock(&clocks_mutex);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100536
537 return 0;
538}
539
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100540static int davinci_ck_open(struct inode *inode, struct file *file)
541{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530542 return single_open(file, davinci_ck_show, NULL);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100543}
544
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530545static const struct file_operations davinci_ck_operations = {
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100546 .open = davinci_ck_open,
547 .read = seq_read,
548 .llseek = seq_lseek,
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530549 .release = single_release,
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100550};
551
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530552static int __init davinci_clk_debugfs_init(void)
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100553{
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530554 debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
555 &davinci_ck_operations);
Vladimir Barinov3e062b02007-06-05 16:36:55 +0100556 return 0;
557
558}
Sekhar Nori2f72e8d2009-12-03 15:36:52 +0530559device_initcall(davinci_clk_debugfs_init);
560#endif /* CONFIG_DEBUG_FS */