blob: 52885ac5bb5d28949394d39353c410f9d329cfad [file] [log] [blame]
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001/*
2 * OMAP2/3 clockdomain framework functions
3 *
4 * Copyright (C) 2008 Texas Instruments, Inc.
Paul Walmsley33903eb2009-12-08 16:33:10 -07005 * Copyright (C) 2008-2009 Nokia Corporation
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03006 *
7 * Written by Paul Walmsley and Jouni Högander
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 */
Paul Walmsley33903eb2009-12-08 16:33:10 -070013#undef DEBUG
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030014
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/device.h>
18#include <linux/list.h>
19#include <linux/errno.h>
20#include <linux/delay.h>
21#include <linux/clk.h>
22#include <linux/limits.h>
Paul Walmsley5b74c672009-02-03 02:10:03 -070023#include <linux/err.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030024
25#include <linux/io.h>
26
27#include <linux/bitops.h>
28
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/clock.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030030
31#include "prm.h"
32#include "prm-regbits-24xx.h"
33#include "cm.h"
34
Tony Lindgrence491cf2009-10-20 09:40:47 -070035#include <plat/powerdomain.h>
36#include <plat/clockdomain.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030037
38/* clkdm_list contains all registered struct clockdomains */
39static LIST_HEAD(clkdm_list);
40
41/* clkdm_mutex protects clkdm_list add and del ops */
42static DEFINE_MUTEX(clkdm_mutex);
43
44/* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
45static struct clkdm_pwrdm_autodep *autodeps;
46
47
48/* Private functions */
49
50/*
51 * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
52 * @autodep: struct clkdm_pwrdm_autodep * to resolve
53 *
54 * Resolve autodep powerdomain names to powerdomain pointers via
55 * pwrdm_lookup() and store the pointers in the autodep structure. An
56 * "autodep" is a powerdomain sleep/wakeup dependency that is
57 * automatically added and removed whenever clocks in the associated
58 * clockdomain are enabled or disabled (respectively) when the
59 * clockdomain is in hardware-supervised mode. Meant to be called
60 * once at clockdomain layer initialization, since these should remain
61 * fixed for a particular architecture. No return value.
62 */
63static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
64{
65 struct powerdomain *pwrdm;
66
67 if (!autodep)
68 return;
69
70 if (!omap_chip_is(autodep->omap_chip))
71 return;
72
Paul Walmsley5b74c672009-02-03 02:10:03 -070073 pwrdm = pwrdm_lookup(autodep->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030074 if (!pwrdm) {
Russell King7aec53a2009-02-22 21:00:55 +000075 pr_err("clockdomain: autodeps: powerdomain %s does not exist\n",
76 autodep->pwrdm.name);
Paul Walmsley5b74c672009-02-03 02:10:03 -070077 pwrdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030078 }
Paul Walmsley5b74c672009-02-03 02:10:03 -070079 autodep->pwrdm.ptr = pwrdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030080}
81
82/*
83 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
84 * @clkdm: struct clockdomain *
85 *
86 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
87 * in hardware-supervised mode. Meant to be called from clock framework
88 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
89 */
90static void _clkdm_add_autodeps(struct clockdomain *clkdm)
91{
92 struct clkdm_pwrdm_autodep *autodep;
93
Paul Walmsley5b74c672009-02-03 02:10:03 -070094 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
95 if (IS_ERR(autodep->pwrdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030096 continue;
97
Paul Walmsleyd96df002009-01-27 19:44:35 -070098 if (!omap_chip_is(autodep->omap_chip))
99 continue;
100
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300101 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley5b74c672009-02-03 02:10:03 -0700102 "pwrdm %s\n", autodep->pwrdm.ptr->name,
103 clkdm->pwrdm.ptr->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300104
Paul Walmsley5b74c672009-02-03 02:10:03 -0700105 pwrdm_add_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
106 pwrdm_add_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300107 }
108}
109
110/*
111 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
112 * @clkdm: struct clockdomain *
113 *
114 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
115 * in hardware-supervised mode. Meant to be called from clock framework
116 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
117 */
118static void _clkdm_del_autodeps(struct clockdomain *clkdm)
119{
120 struct clkdm_pwrdm_autodep *autodep;
121
Paul Walmsley5b74c672009-02-03 02:10:03 -0700122 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
123 if (IS_ERR(autodep->pwrdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300124 continue;
125
Paul Walmsleyd96df002009-01-27 19:44:35 -0700126 if (!omap_chip_is(autodep->omap_chip))
127 continue;
128
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300129 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley5b74c672009-02-03 02:10:03 -0700130 "pwrdm %s\n", autodep->pwrdm.ptr->name,
131 clkdm->pwrdm.ptr->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300132
Paul Walmsley5b74c672009-02-03 02:10:03 -0700133 pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
134 pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300135 }
136}
137
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600138/*
139 * _omap2_clkdm_set_hwsup - set the hwsup idle transition bit
140 * @clkdm: struct clockdomain *
141 * @enable: int 0 to disable, 1 to enable
142 *
143 * Internal helper for actually switching the bit that controls hwsup
144 * idle transitions for clkdm.
145 */
146static void _omap2_clkdm_set_hwsup(struct clockdomain *clkdm, int enable)
147{
148 u32 v;
149
150 if (cpu_is_omap24xx()) {
151 if (enable)
152 v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
153 else
154 v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
155 } else if (cpu_is_omap34xx()) {
156 if (enable)
157 v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
158 else
159 v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
160 } else {
161 BUG();
162 }
163
164 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
165 v << __ffs(clkdm->clktrctrl_mask),
Abhijit Pagare84c0c392010-01-26 20:12:53 -0700166 clkdm->pwrdm.ptr->prcm_offs, OMAP2_CM_CLKSTCTRL);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600167}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300168
169static struct clockdomain *_clkdm_lookup(const char *name)
170{
171 struct clockdomain *clkdm, *temp_clkdm;
172
173 if (!name)
174 return NULL;
175
176 clkdm = NULL;
177
178 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
179 if (!strcmp(name, temp_clkdm->name)) {
180 clkdm = temp_clkdm;
181 break;
182 }
183 }
184
185 return clkdm;
186}
187
188
189/* Public functions */
190
191/**
192 * clkdm_init - set up the clockdomain layer
193 * @clkdms: optional pointer to an array of clockdomains to register
194 * @init_autodeps: optional pointer to an array of autodeps to register
195 *
196 * Set up internal state. If a pointer to an array of clockdomains
197 * was supplied, loop through the list of clockdomains, register all
198 * that are available on the current platform. Similarly, if a
199 * pointer to an array of clockdomain-powerdomain autodependencies was
200 * provided, register those. No return value.
201 */
202void clkdm_init(struct clockdomain **clkdms,
203 struct clkdm_pwrdm_autodep *init_autodeps)
204{
205 struct clockdomain **c = NULL;
206 struct clkdm_pwrdm_autodep *autodep = NULL;
207
208 if (clkdms)
209 for (c = clkdms; *c; c++)
210 clkdm_register(*c);
211
212 autodeps = init_autodeps;
213 if (autodeps)
Paul Walmsley5b74c672009-02-03 02:10:03 -0700214 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300215 _autodep_lookup(autodep);
216}
217
218/**
219 * clkdm_register - register a clockdomain
220 * @clkdm: struct clockdomain * to register
221 *
222 * Adds a clockdomain to the internal clockdomain list.
223 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
224 * already registered by the provided name, or 0 upon success.
225 */
226int clkdm_register(struct clockdomain *clkdm)
227{
228 int ret = -EINVAL;
229 struct powerdomain *pwrdm;
230
231 if (!clkdm || !clkdm->name)
232 return -EINVAL;
233
234 if (!omap_chip_is(clkdm->omap_chip))
235 return -EINVAL;
236
Paul Walmsley5b74c672009-02-03 02:10:03 -0700237 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300238 if (!pwrdm) {
Russell King7aec53a2009-02-22 21:00:55 +0000239 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
240 clkdm->name, clkdm->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300241 return -EINVAL;
242 }
Paul Walmsley5b74c672009-02-03 02:10:03 -0700243 clkdm->pwrdm.ptr = pwrdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300244
245 mutex_lock(&clkdm_mutex);
246 /* Verify that the clockdomain is not already registered */
247 if (_clkdm_lookup(clkdm->name)) {
248 ret = -EEXIST;
249 goto cr_unlock;
Russell King7aec53a2009-02-22 21:00:55 +0000250 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300251
252 list_add(&clkdm->node, &clkdm_list);
253
Paul Walmsley8420bb12008-08-19 11:08:44 +0300254 pwrdm_add_clkdm(pwrdm, clkdm);
255
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300256 pr_debug("clockdomain: registered %s\n", clkdm->name);
257 ret = 0;
258
259cr_unlock:
260 mutex_unlock(&clkdm_mutex);
261
262 return ret;
263}
264
265/**
266 * clkdm_unregister - unregister a clockdomain
267 * @clkdm: struct clockdomain * to unregister
268 *
269 * Removes a clockdomain from the internal clockdomain list. Returns
270 * -EINVAL if clkdm argument is NULL.
271 */
272int clkdm_unregister(struct clockdomain *clkdm)
273{
274 if (!clkdm)
275 return -EINVAL;
276
Paul Walmsley5b74c672009-02-03 02:10:03 -0700277 pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300278
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300279 mutex_lock(&clkdm_mutex);
280 list_del(&clkdm->node);
281 mutex_unlock(&clkdm_mutex);
282
283 pr_debug("clockdomain: unregistered %s\n", clkdm->name);
284
285 return 0;
286}
287
288/**
289 * clkdm_lookup - look up a clockdomain by name, return a pointer
290 * @name: name of clockdomain
291 *
292 * Find a registered clockdomain by its name. Returns a pointer to the
293 * struct clockdomain if found, or NULL otherwise.
294 */
295struct clockdomain *clkdm_lookup(const char *name)
296{
297 struct clockdomain *clkdm, *temp_clkdm;
298
299 if (!name)
300 return NULL;
301
302 clkdm = NULL;
303
304 mutex_lock(&clkdm_mutex);
305 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
306 if (!strcmp(name, temp_clkdm->name)) {
307 clkdm = temp_clkdm;
308 break;
309 }
310 }
311 mutex_unlock(&clkdm_mutex);
312
313 return clkdm;
314}
315
316/**
317 * clkdm_for_each - call function on each registered clockdomain
318 * @fn: callback function *
319 *
320 * Call the supplied function for each registered clockdomain.
321 * The callback function can return anything but 0 to bail
322 * out early from the iterator. The callback function is called with
323 * the clkdm_mutex held, so no clockdomain structure manipulation
324 * functions should be called from the callback, although hardware
325 * clockdomain control functions are fine. Returns the last return
326 * value of the callback function, which should be 0 for success or
327 * anything else to indicate failure; or -EINVAL if the function pointer
328 * is null.
329 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300330int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
331 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300332{
333 struct clockdomain *clkdm;
334 int ret = 0;
335
336 if (!fn)
337 return -EINVAL;
338
339 mutex_lock(&clkdm_mutex);
340 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300341 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300342 if (ret)
343 break;
344 }
345 mutex_unlock(&clkdm_mutex);
346
347 return ret;
348}
349
350
Paul Walmsleye89087c2008-05-20 18:41:35 -0600351/**
352 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
353 * @clkdm: struct clockdomain *
354 *
355 * Return a pointer to the struct powerdomain that the specified clockdomain
356 * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
357 */
358struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
359{
360 if (!clkdm)
361 return NULL;
362
Paul Walmsley5b74c672009-02-03 02:10:03 -0700363 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600364}
365
366
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300367/* Hardware clockdomain control */
368
369/**
370 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
371 * @clk: struct clk * of a clockdomain
372 *
373 * Return the clockdomain's current state transition mode from the
Abhijit Pagare84c0c392010-01-26 20:12:53 -0700374 * corresponding domain OMAP2_CM_CLKSTCTRL register. Returns -EINVAL if clk
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300375 * is NULL or the current mode upon success.
376 */
377static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
378{
379 u32 v;
380
381 if (!clkdm)
382 return -EINVAL;
383
Abhijit Pagare84c0c392010-01-26 20:12:53 -0700384 v = cm_read_mod_reg(clkdm->pwrdm.ptr->prcm_offs, OMAP2_CM_CLKSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300385 v &= clkdm->clktrctrl_mask;
386 v >>= __ffs(clkdm->clktrctrl_mask);
387
388 return v;
389}
390
391/**
392 * omap2_clkdm_sleep - force clockdomain sleep transition
393 * @clkdm: struct clockdomain *
394 *
395 * Instruct the CM to force a sleep transition on the specified
396 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
397 * clockdomain does not support software-initiated sleep; 0 upon
398 * success.
399 */
400int omap2_clkdm_sleep(struct clockdomain *clkdm)
401{
402 if (!clkdm)
403 return -EINVAL;
404
405 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
406 pr_debug("clockdomain: %s does not support forcing "
407 "sleep via software\n", clkdm->name);
408 return -EINVAL;
409 }
410
411 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
412
413 if (cpu_is_omap24xx()) {
414
415 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
Abhijit Pagare37903002010-01-26 20:12:51 -0700416 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300417
418 } else if (cpu_is_omap34xx()) {
419
420 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
421 __ffs(clkdm->clktrctrl_mask));
422
423 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
Abhijit Pagare84c0c392010-01-26 20:12:53 -0700424 clkdm->pwrdm.ptr->prcm_offs,
425 OMAP2_CM_CLKSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300426
427 } else {
428 BUG();
429 };
430
431 return 0;
432}
433
434/**
435 * omap2_clkdm_wakeup - force clockdomain wakeup transition
436 * @clkdm: struct clockdomain *
437 *
438 * Instruct the CM to force a wakeup transition on the specified
439 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
440 * clockdomain does not support software-controlled wakeup; 0 upon
441 * success.
442 */
443int omap2_clkdm_wakeup(struct clockdomain *clkdm)
444{
445 if (!clkdm)
446 return -EINVAL;
447
448 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
449 pr_debug("clockdomain: %s does not support forcing "
450 "wakeup via software\n", clkdm->name);
451 return -EINVAL;
452 }
453
454 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
455
456 if (cpu_is_omap24xx()) {
457
458 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
Abhijit Pagare37903002010-01-26 20:12:51 -0700459 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300460
461 } else if (cpu_is_omap34xx()) {
462
463 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
464 __ffs(clkdm->clktrctrl_mask));
465
466 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
Abhijit Pagare84c0c392010-01-26 20:12:53 -0700467 clkdm->pwrdm.ptr->prcm_offs,
468 OMAP2_CM_CLKSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300469
470 } else {
471 BUG();
472 };
473
474 return 0;
475}
476
477/**
478 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
479 * @clkdm: struct clockdomain *
480 *
481 * Allow the hardware to automatically switch the clockdomain into
482 * active or idle states, as needed by downstream clocks. If the
483 * clockdomain has any downstream clocks enabled in the clock
484 * framework, wkdep/sleepdep autodependencies are added; this is so
485 * device drivers can read and write to the device. No return value.
486 */
487void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
488{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300489 if (!clkdm)
490 return;
491
492 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
493 pr_debug("clock: automatic idle transitions cannot be enabled "
494 "on clockdomain %s\n", clkdm->name);
495 return;
496 }
497
498 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
499 clkdm->name);
500
501 if (atomic_read(&clkdm->usecount) > 0)
502 _clkdm_add_autodeps(clkdm);
503
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600504 _omap2_clkdm_set_hwsup(clkdm, 1);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300505
506 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300507}
508
509/**
510 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
511 * @clkdm: struct clockdomain *
512 *
513 * Prevent the hardware from automatically switching the clockdomain
514 * into inactive or idle states. If the clockdomain has downstream
515 * clocks enabled in the clock framework, wkdep/sleepdep
516 * autodependencies are removed. No return value.
517 */
518void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
519{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300520 if (!clkdm)
521 return;
522
523 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
524 pr_debug("clockdomain: automatic idle transitions cannot be "
525 "disabled on %s\n", clkdm->name);
526 return;
527 }
528
529 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
530 clkdm->name);
531
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600532 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300533
534 if (atomic_read(&clkdm->usecount) > 0)
535 _clkdm_del_autodeps(clkdm);
536}
537
538
539/* Clockdomain-to-clock framework interface code */
540
541/**
542 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
543 * @clkdm: struct clockdomain *
544 * @clk: struct clk * of the enabled downstream clock
545 *
546 * Increment the usecount of this clockdomain 'clkdm' and ensure that
547 * it is awake. Intended to be called by clk_enable() code. If the
548 * clockdomain is in software-supervised idle mode, force the
549 * clockdomain to wake. If the clockdomain is in hardware-supervised
550 * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
551 * in the clockdomain can be read from/written to by on-chip processors.
552 * Returns -EINVAL if passed null pointers; returns 0 upon success or
553 * if the clockdomain is in hwsup idle mode.
554 */
555int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
556{
557 int v;
558
559 /*
560 * XXX Rewrite this code to maintain a list of enabled
561 * downstream clocks for debugging purposes?
562 */
563
Abhijit Pagare98bb1552010-01-08 15:23:04 -0700564 if (!clkdm || !clk || !clkdm->clktrctrl_mask)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300565 return -EINVAL;
566
567 if (atomic_inc_return(&clkdm->usecount) > 1)
568 return 0;
569
570 /* Clockdomain now has one enabled downstream clock */
571
572 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
573 clk->name);
574
575 v = omap2_clkdm_clktrctrl_read(clkdm);
576
577 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600578 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
579 /* Disable HW transitions when we are changing deps */
580 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300581 _clkdm_add_autodeps(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600582 _omap2_clkdm_set_hwsup(clkdm, 1);
583 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300584 omap2_clkdm_wakeup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600585 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300586
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700587 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300588 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700589
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300590 return 0;
591}
592
593/**
594 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
595 * @clkdm: struct clockdomain *
596 * @clk: struct clk * of the disabled downstream clock
597 *
598 * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
599 * called by clk_disable() code. If the usecount goes to 0, put the
600 * clockdomain to sleep (software-supervised mode) or remove the
601 * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
602 * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
603 * underflows and debugging is enabled; or returns 0 upon success or
604 * if the clockdomain is in hwsup idle mode.
605 */
606int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
607{
608 int v;
609
610 /*
611 * XXX Rewrite this code to maintain a list of enabled
612 * downstream clocks for debugging purposes?
613 */
614
Abhijit Pagare98bb1552010-01-08 15:23:04 -0700615 if (!clkdm || !clk || !clkdm->clktrctrl_mask)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300616 return -EINVAL;
617
618#ifdef DEBUG
619 if (atomic_read(&clkdm->usecount) == 0) {
620 WARN_ON(1); /* underflow */
621 return -ERANGE;
622 }
623#endif
624
625 if (atomic_dec_return(&clkdm->usecount) > 0)
626 return 0;
627
628 /* All downstream clocks of this clockdomain are now disabled */
629
630 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
631 clk->name);
632
633 v = omap2_clkdm_clktrctrl_read(clkdm);
634
635 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600636 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
637 /* Disable HW transitions when we are changing deps */
638 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300639 _clkdm_del_autodeps(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600640 _omap2_clkdm_set_hwsup(clkdm, 1);
641 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300642 omap2_clkdm_sleep(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600643 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300644
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300645 pwrdm_clkdm_state_switch(clkdm);
646
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300647 return 0;
648}
649