1919#include <linux/module.h>
2020#include <linux/mutex.h>
2121#include <linux/list.h>
22- #include <linux/kref.h>
2322#include <linux/kobject.h>
2423#include <linux/sysdev.h>
2524#include <linux/seq_file.h>
@@ -90,7 +89,7 @@ static void propagate_rate(struct clk *clk)
9089 }
9190}
9291
93- static int __clk_enable (struct clk * clk )
92+ static void __clk_init (struct clk * clk )
9493{
9594 /*
9695 * See if this is the first time we're enabling the clock, some
@@ -100,19 +99,33 @@ static int __clk_enable(struct clk *clk)
10099 * divisors to use before it can effectively recalc.
101100 */
102101
103- if (clk -> flags & CLK_ALWAYS_ENABLED ) {
104- kref_get (& clk -> kref );
105- return 0 ;
106- }
107-
108- if (unlikely (atomic_read (& clk -> kref .refcount ) == 1 ))
102+ if (clk -> flags & CLK_NEEDS_INIT ) {
109103 if (clk -> ops && clk -> ops -> init )
110104 clk -> ops -> init (clk );
111105
112- kref_get (& clk -> kref );
106+ clk -> flags &= ~CLK_NEEDS_INIT ;
107+ }
108+ }
109+
110+ static int __clk_enable (struct clk * clk )
111+ {
112+ if (!clk )
113+ return - EINVAL ;
114+
115+ clk -> usecount ++ ;
116+
117+ /* nothing to do if always enabled */
118+ if (clk -> flags & CLK_ALWAYS_ENABLED )
119+ return 0 ;
120+
121+ if (clk -> usecount == 1 ) {
122+ __clk_init (clk );
113123
114- if (likely (clk -> ops && clk -> ops -> enable ))
115- clk -> ops -> enable (clk );
124+ __clk_enable (clk -> parent );
125+
126+ if (clk -> ops && clk -> ops -> enable )
127+ clk -> ops -> enable (clk );
128+ }
116129
117130 return 0 ;
118131}
@@ -122,11 +135,6 @@ int clk_enable(struct clk *clk)
122135 unsigned long flags ;
123136 int ret ;
124137
125- if (!clk )
126- return - EINVAL ;
127-
128- clk_enable (clk -> parent );
129-
130138 spin_lock_irqsave (& clock_lock , flags );
131139 ret = __clk_enable (clk );
132140 spin_unlock_irqrestore (& clock_lock , flags );
@@ -135,36 +143,33 @@ int clk_enable(struct clk *clk)
135143}
136144EXPORT_SYMBOL_GPL (clk_enable );
137145
138- static void clk_kref_release (struct kref * kref )
139- {
140- /* Nothing to do */
141- }
142-
143146static void __clk_disable (struct clk * clk )
144147{
145- int count = kref_put (& clk -> kref , clk_kref_release );
148+ if (!clk )
149+ return ;
150+
151+ clk -> usecount -- ;
152+
153+ WARN_ON (clk -> usecount < 0 );
146154
147155 if (clk -> flags & CLK_ALWAYS_ENABLED )
148156 return ;
149157
150- if (! count ) { /* count reaches zero, disable the clock */
158+ if (clk -> usecount == 0 ) {
151159 if (likely (clk -> ops && clk -> ops -> disable ))
152160 clk -> ops -> disable (clk );
161+
162+ __clk_disable (clk -> parent );
153163 }
154164}
155165
156166void clk_disable (struct clk * clk )
157167{
158168 unsigned long flags ;
159169
160- if (!clk )
161- return ;
162-
163170 spin_lock_irqsave (& clock_lock , flags );
164171 __clk_disable (clk );
165172 spin_unlock_irqrestore (& clock_lock , flags );
166-
167- clk_disable (clk -> parent );
168173}
169174EXPORT_SYMBOL_GPL (clk_disable );
170175
@@ -173,14 +178,14 @@ int clk_register(struct clk *clk)
173178 mutex_lock (& clock_list_sem );
174179
175180 list_add (& clk -> node , & clock_list );
176- kref_init (& clk -> kref );
181+ clk -> usecount = 0 ;
182+ clk -> flags |= CLK_NEEDS_INIT ;
177183
178184 mutex_unlock (& clock_list_sem );
179185
180186 if (clk -> flags & CLK_ALWAYS_ENABLED ) {
187+ __clk_init (clk );
181188 pr_debug ( "Clock '%s' is ALWAYS_ENABLED\n" , clk -> name );
182- if (clk -> ops && clk -> ops -> init )
183- clk -> ops -> init (clk );
184189 if (clk -> ops && clk -> ops -> enable )
185190 clk -> ops -> enable (clk );
186191 pr_debug ( "Enabled." );
@@ -356,7 +361,7 @@ static int show_clocks(char *buf, char **start, off_t off,
356361 p += sprintf (p , "%-12s\t: %ld.%02ldMHz\t%s\n" , clk -> name ,
357362 rate / 1000000 , (rate % 1000000 ) / 10000 ,
358363 ((clk -> flags & CLK_ALWAYS_ENABLED ) ||
359- ( atomic_read ( & clk -> kref . refcount ) != 1 ) ) ?
364+ clk -> usecount > 0 ) ?
360365 "enabled" : "disabled" );
361366 }
362367
0 commit comments