11/*
22 * arch/sh/kernel/cpu/clock.c - SuperH clock framework
33 *
4- * Copyright (C) 2005, 2006, 2007 Paul Mundt
4+ * Copyright (C) 2005 - 2009 Paul Mundt
55 *
66 * This clock framework is derived from the OMAP version by:
77 *
8- * Copyright (C) 2004 - 2005 Nokia Corporation
8+ * Copyright (C) 2004 - 2008 Nokia Corporation
99 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
1010 *
1111 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
@@ -43,20 +43,20 @@ static DEFINE_MUTEX(clock_list_sem);
4343 */
4444static struct clk master_clk = {
4545 .name = "master_clk" ,
46- .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES ,
46+ .flags = CLK_ALWAYS_ENABLED ,
4747 .rate = CONFIG_SH_PCLK_FREQ ,
4848};
4949
5050static struct clk module_clk = {
5151 .name = "module_clk" ,
5252 .parent = & master_clk ,
53- .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES ,
53+ .flags = CLK_ALWAYS_ENABLED ,
5454};
5555
5656static struct clk bus_clk = {
5757 .name = "bus_clk" ,
5858 .parent = & master_clk ,
59- .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES ,
59+ .flags = CLK_ALWAYS_ENABLED ,
6060};
6161
6262static struct clk cpu_clk = {
@@ -75,27 +75,24 @@ static struct clk *onchip_clocks[] = {
7575 & cpu_clk ,
7676};
7777
78+ /* Used for clocks that always have same value as the parent clock */
79+ unsigned long followparent_recalc (struct clk * clk )
80+ {
81+ return clk -> parent -> rate ;
82+ }
83+
7884/* Propagate rate to children */
79- static void propagate_rate (struct clk * clk )
85+ void propagate_rate (struct clk * tclk )
8086{
8187 struct clk * clkp ;
8288
83- list_for_each_entry (clkp , & clock_list , node ) {
84- if (likely (clkp -> parent != clk ))
85- continue ;
86- if (likely (clkp -> ops && clkp -> ops -> recalc ))
89+ list_for_each_entry (clkp , & tclk -> children , sibling ) {
90+ if (clkp -> ops -> recalc )
8791 clkp -> rate = clkp -> ops -> recalc (clkp );
88- if (unlikely (clkp -> flags & CLK_RATE_PROPAGATES ))
89- propagate_rate (clkp );
92+ propagate_rate (clkp );
9093 }
9194}
9295
93- /* Used for clocks that always have same value as the parent clock */
94- unsigned long followparent_recalc (struct clk * clk )
95- {
96- return clk -> parent -> rate ;
97- }
98-
9996static void __clk_init (struct clk * clk )
10097{
10198 /*
@@ -180,10 +177,46 @@ void clk_disable(struct clk *clk)
180177}
181178EXPORT_SYMBOL_GPL (clk_disable );
182179
180+ static LIST_HEAD (root_clks );
181+
182+ /**
183+ * recalculate_root_clocks - recalculate and propagate all root clocks
184+ *
185+ * Recalculates all root clocks (clocks with no parent), which if the
186+ * clock's .recalc is set correctly, should also propagate their rates.
187+ * Called at init.
188+ */
189+ void recalculate_root_clocks (void )
190+ {
191+ struct clk * clkp ;
192+
193+ list_for_each_entry (clkp , & root_clks , sibling ) {
194+ if (clkp -> ops -> recalc )
195+ clkp -> rate = clkp -> ops -> recalc (clkp );
196+ propagate_rate (clkp );
197+ }
198+ }
199+
183200int clk_register (struct clk * clk )
184201{
202+ if (clk == NULL || IS_ERR (clk ))
203+ return - EINVAL ;
204+
205+ /*
206+ * trap out already registered clocks
207+ */
208+ if (clk -> node .next || clk -> node .prev )
209+ return 0 ;
210+
185211 mutex_lock (& clock_list_sem );
186212
213+ INIT_LIST_HEAD (& clk -> children );
214+
215+ if (clk -> parent )
216+ list_add (& clk -> sibling , & clk -> parent -> children );
217+ else
218+ list_add (& clk -> sibling , & root_clks );
219+
187220 list_add (& clk -> node , & clock_list );
188221 clk -> usecount = 0 ;
189222 clk -> flags |= CLK_NEEDS_INIT ;
@@ -205,6 +238,7 @@ EXPORT_SYMBOL_GPL(clk_register);
205238void clk_unregister (struct clk * clk )
206239{
207240 mutex_lock (& clock_list_sem );
241+ list_del (& clk -> sibling );
208242 list_del (& clk -> node );
209243 mutex_unlock (& clock_list_sem );
210244}
@@ -231,50 +265,53 @@ int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
231265
232266 spin_lock_irqsave (& clock_lock , flags );
233267 ret = clk -> ops -> set_rate (clk , rate , algo_id );
268+ if (ret == 0 ) {
269+ if (clk -> ops -> recalc )
270+ clk -> rate = clk -> ops -> recalc (clk );
271+ propagate_rate (clk );
272+ }
234273 spin_unlock_irqrestore (& clock_lock , flags );
235274 }
236275
237- if (unlikely (clk -> flags & CLK_RATE_PROPAGATES ))
238- propagate_rate (clk );
239-
240276 return ret ;
241277}
242278EXPORT_SYMBOL_GPL (clk_set_rate_ex );
243279
244280void clk_recalc_rate (struct clk * clk )
245281{
246- if (likely (clk -> ops && clk -> ops -> recalc )) {
247- unsigned long flags ;
282+ unsigned long flags ;
248283
249- spin_lock_irqsave (& clock_lock , flags );
250- clk -> rate = clk -> ops -> recalc (clk );
251- spin_unlock_irqrestore (& clock_lock , flags );
252- }
284+ if (!clk -> ops -> recalc )
285+ return ;
253286
254- if (unlikely (clk -> flags & CLK_RATE_PROPAGATES ))
255- propagate_rate (clk );
287+ spin_lock_irqsave (& clock_lock , flags );
288+ clk -> rate = clk -> ops -> recalc (clk );
289+ propagate_rate (clk );
290+ spin_unlock_irqrestore (& clock_lock , flags );
256291}
257292EXPORT_SYMBOL_GPL (clk_recalc_rate );
258293
259294int clk_set_parent (struct clk * clk , struct clk * parent )
260295{
296+ unsigned long flags ;
261297 int ret = - EINVAL ;
262- struct clk * old ;
263298
264299 if (!parent || !clk )
265300 return ret ;
266301
267- old = clk -> parent ;
268- if (likely (clk -> ops && clk -> ops -> set_parent )) {
269- unsigned long flags ;
270- spin_lock_irqsave (& clock_lock , flags );
271- ret = clk -> ops -> set_parent (clk , parent );
272- spin_unlock_irqrestore (& clock_lock , flags );
273- clk -> parent = (ret ? old : parent );
274- }
302+ spin_lock_irqsave (& clock_lock , flags );
303+ if (clk -> usecount == 0 ) {
304+ if (clk -> ops -> set_parent )
305+ ret = clk -> ops -> set_parent (clk , parent );
306+ if (ret == 0 ) {
307+ if (clk -> ops -> recalc )
308+ clk -> rate = clk -> ops -> recalc (clk );
309+ propagate_rate (clk );
310+ }
311+ } else
312+ ret = - EBUSY ;
313+ spin_unlock_irqrestore (& clock_lock , flags );
275314
276- if (unlikely (clk -> flags & CLK_RATE_PROPAGATES ))
277- propagate_rate (clk );
278315 return ret ;
279316}
280317EXPORT_SYMBOL_GPL (clk_set_parent );
@@ -457,8 +494,7 @@ int __init clk_init(void)
457494 ret |= arch_clk_init ();
458495
459496 /* Kick the child clocks.. */
460- propagate_rate (& master_clk );
461- propagate_rate (& bus_clk );
497+ recalculate_root_clocks ();
462498
463499 return ret ;
464500}
0 commit comments