2 * OMAP2/3 clockdomain framework functions
4 * Copyright (C) 2008 Texas Instruments, Inc.
5 * Copyright (C) 2008 Nokia Corporation
7 * Written by Paul Walmsley and Jouni Högander
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.
13 #ifdef CONFIG_OMAP_DEBUG_CLOCKDOMAIN
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/list.h>
21 #include <linux/errno.h>
22 #include <linux/delay.h>
23 #include <linux/clk.h>
24 #include <linux/limits.h>
25 #include <linux/err.h>
29 #include <linux/bitops.h>
31 #include <mach/clock.h>
34 #include "prm-regbits-24xx.h"
37 #include <mach/powerdomain.h>
38 #include <mach/clockdomain.h>
40 /* clkdm_list contains all registered struct clockdomains */
41 static LIST_HEAD(clkdm_list
);
43 /* clkdm_mutex protects clkdm_list add and del ops */
44 static DEFINE_MUTEX(clkdm_mutex
);
46 /* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
47 static struct clkdm_pwrdm_autodep
*autodeps
;
50 /* Private functions */
53 * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
54 * @autodep: struct clkdm_pwrdm_autodep * to resolve
56 * Resolve autodep powerdomain names to powerdomain pointers via
57 * pwrdm_lookup() and store the pointers in the autodep structure. An
58 * "autodep" is a powerdomain sleep/wakeup dependency that is
59 * automatically added and removed whenever clocks in the associated
60 * clockdomain are enabled or disabled (respectively) when the
61 * clockdomain is in hardware-supervised mode. Meant to be called
62 * once at clockdomain layer initialization, since these should remain
63 * fixed for a particular architecture. No return value.
65 static void _autodep_lookup(struct clkdm_pwrdm_autodep
*autodep
)
67 struct powerdomain
*pwrdm
;
72 if (!omap_chip_is(autodep
->omap_chip
))
75 pwrdm
= pwrdm_lookup(autodep
->pwrdm
.name
);
77 pr_debug("clockdomain: _autodep_lookup: powerdomain %s "
78 "does not exist\n", autodep
->pwrdm
.name
);
80 pwrdm
= ERR_PTR(-ENOENT
);
82 autodep
->pwrdm
.ptr
= pwrdm
;
88 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
89 * @clkdm: struct clockdomain *
91 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
92 * in hardware-supervised mode. Meant to be called from clock framework
93 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
95 static void _clkdm_add_autodeps(struct clockdomain
*clkdm
)
97 struct clkdm_pwrdm_autodep
*autodep
;
99 for (autodep
= autodeps
; autodep
->pwrdm
.ptr
; autodep
++) {
100 if (IS_ERR(autodep
->pwrdm
.ptr
))
103 if (!omap_chip_is(autodep
->omap_chip
))
106 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
107 "pwrdm %s\n", autodep
->pwrdm
.ptr
->name
,
108 clkdm
->pwrdm
.ptr
->name
);
110 pwrdm_add_sleepdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
111 pwrdm_add_wkdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
116 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
117 * @clkdm: struct clockdomain *
119 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
120 * in hardware-supervised mode. Meant to be called from clock framework
121 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
123 static void _clkdm_del_autodeps(struct clockdomain
*clkdm
)
125 struct clkdm_pwrdm_autodep
*autodep
;
127 for (autodep
= autodeps
; autodep
->pwrdm
.ptr
; autodep
++) {
128 if (IS_ERR(autodep
->pwrdm
.ptr
))
131 if (!omap_chip_is(autodep
->omap_chip
))
134 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
135 "pwrdm %s\n", autodep
->pwrdm
.ptr
->name
,
136 clkdm
->pwrdm
.ptr
->name
);
138 pwrdm_del_sleepdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
139 pwrdm_del_wkdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
144 static struct clockdomain
*_clkdm_lookup(const char *name
)
146 struct clockdomain
*clkdm
, *temp_clkdm
;
153 list_for_each_entry(temp_clkdm
, &clkdm_list
, node
) {
154 if (!strcmp(name
, temp_clkdm
->name
)) {
164 /* Public functions */
167 * clkdm_init - set up the clockdomain layer
168 * @clkdms: optional pointer to an array of clockdomains to register
169 * @init_autodeps: optional pointer to an array of autodeps to register
171 * Set up internal state. If a pointer to an array of clockdomains
172 * was supplied, loop through the list of clockdomains, register all
173 * that are available on the current platform. Similarly, if a
174 * pointer to an array of clockdomain-powerdomain autodependencies was
175 * provided, register those. No return value.
177 void clkdm_init(struct clockdomain
**clkdms
,
178 struct clkdm_pwrdm_autodep
*init_autodeps
)
180 struct clockdomain
**c
= NULL
;
181 struct clkdm_pwrdm_autodep
*autodep
= NULL
;
184 for (c
= clkdms
; *c
; c
++)
187 autodeps
= init_autodeps
;
189 for (autodep
= autodeps
; autodep
->pwrdm
.ptr
; autodep
++)
190 _autodep_lookup(autodep
);
194 * clkdm_register - register a clockdomain
195 * @clkdm: struct clockdomain * to register
197 * Adds a clockdomain to the internal clockdomain list.
198 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
199 * already registered by the provided name, or 0 upon success.
201 int clkdm_register(struct clockdomain
*clkdm
)
204 struct powerdomain
*pwrdm
;
206 if (!clkdm
|| !clkdm
->name
)
209 if (!omap_chip_is(clkdm
->omap_chip
))
212 pwrdm
= pwrdm_lookup(clkdm
->pwrdm
.name
);
214 pr_debug("clockdomain: clkdm_register %s: powerdomain %s "
215 "does not exist\n", clkdm
->name
, clkdm
->pwrdm
.name
);
218 clkdm
->pwrdm
.ptr
= pwrdm
;
220 mutex_lock(&clkdm_mutex
);
221 /* Verify that the clockdomain is not already registered */
222 if (_clkdm_lookup(clkdm
->name
)) {
227 list_add(&clkdm
->node
, &clkdm_list
);
229 pwrdm_add_clkdm(pwrdm
, clkdm
);
231 pr_debug("clockdomain: registered %s\n", clkdm
->name
);
235 mutex_unlock(&clkdm_mutex
);
241 * clkdm_unregister - unregister a clockdomain
242 * @clkdm: struct clockdomain * to unregister
244 * Removes a clockdomain from the internal clockdomain list. Returns
245 * -EINVAL if clkdm argument is NULL.
247 int clkdm_unregister(struct clockdomain
*clkdm
)
252 pwrdm_del_clkdm(clkdm
->pwrdm
.ptr
, clkdm
);
254 mutex_lock(&clkdm_mutex
);
255 list_del(&clkdm
->node
);
256 mutex_unlock(&clkdm_mutex
);
258 pr_debug("clockdomain: unregistered %s\n", clkdm
->name
);
264 * clkdm_lookup - look up a clockdomain by name, return a pointer
265 * @name: name of clockdomain
267 * Find a registered clockdomain by its name. Returns a pointer to the
268 * struct clockdomain if found, or NULL otherwise.
270 struct clockdomain
*clkdm_lookup(const char *name
)
272 struct clockdomain
*clkdm
, *temp_clkdm
;
279 mutex_lock(&clkdm_mutex
);
280 list_for_each_entry(temp_clkdm
, &clkdm_list
, node
) {
281 if (!strcmp(name
, temp_clkdm
->name
)) {
286 mutex_unlock(&clkdm_mutex
);
292 * clkdm_for_each - call function on each registered clockdomain
293 * @fn: callback function *
295 * Call the supplied function for each registered clockdomain.
296 * The callback function can return anything but 0 to bail
297 * out early from the iterator. The callback function is called with
298 * the clkdm_mutex held, so no clockdomain structure manipulation
299 * functions should be called from the callback, although hardware
300 * clockdomain control functions are fine. Returns the last return
301 * value of the callback function, which should be 0 for success or
302 * anything else to indicate failure; or -EINVAL if the function pointer
305 int clkdm_for_each(int (*fn
)(struct clockdomain
*clkdm
))
307 struct clockdomain
*clkdm
;
313 mutex_lock(&clkdm_mutex
);
314 list_for_each_entry(clkdm
, &clkdm_list
, node
) {
319 mutex_unlock(&clkdm_mutex
);
326 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
327 * @clkdm: struct clockdomain *
329 * Return a pointer to the struct powerdomain that the specified clockdomain
330 * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
332 struct powerdomain
*clkdm_get_pwrdm(struct clockdomain
*clkdm
)
337 return clkdm
->pwrdm
.ptr
;
341 /* Hardware clockdomain control */
344 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
345 * @clk: struct clk * of a clockdomain
347 * Return the clockdomain's current state transition mode from the
348 * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if clk
349 * is NULL or the current mode upon success.
351 static int omap2_clkdm_clktrctrl_read(struct clockdomain
*clkdm
)
358 v
= cm_read_mod_reg(clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
359 v
&= clkdm
->clktrctrl_mask
;
360 v
>>= __ffs(clkdm
->clktrctrl_mask
);
366 * omap2_clkdm_sleep - force clockdomain sleep transition
367 * @clkdm: struct clockdomain *
369 * Instruct the CM to force a sleep transition on the specified
370 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
371 * clockdomain does not support software-initiated sleep; 0 upon
374 int omap2_clkdm_sleep(struct clockdomain
*clkdm
)
379 if (!(clkdm
->flags
& CLKDM_CAN_FORCE_SLEEP
)) {
380 pr_debug("clockdomain: %s does not support forcing "
381 "sleep via software\n", clkdm
->name
);
385 pr_debug("clockdomain: forcing sleep on %s\n", clkdm
->name
);
387 if (cpu_is_omap24xx()) {
389 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE
,
390 clkdm
->pwrdm
.ptr
->prcm_offs
, PM_PWSTCTRL
);
392 } else if (cpu_is_omap34xx()) {
394 u32 v
= (OMAP34XX_CLKSTCTRL_FORCE_SLEEP
<<
395 __ffs(clkdm
->clktrctrl_mask
));
397 cm_rmw_mod_reg_bits(clkdm
->clktrctrl_mask
, v
,
398 clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
408 * omap2_clkdm_wakeup - force clockdomain wakeup transition
409 * @clkdm: struct clockdomain *
411 * Instruct the CM to force a wakeup transition on the specified
412 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
413 * clockdomain does not support software-controlled wakeup; 0 upon
416 int omap2_clkdm_wakeup(struct clockdomain
*clkdm
)
421 if (!(clkdm
->flags
& CLKDM_CAN_FORCE_WAKEUP
)) {
422 pr_debug("clockdomain: %s does not support forcing "
423 "wakeup via software\n", clkdm
->name
);
427 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm
->name
);
429 if (cpu_is_omap24xx()) {
431 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE
,
432 clkdm
->pwrdm
.ptr
->prcm_offs
, PM_PWSTCTRL
);
434 } else if (cpu_is_omap34xx()) {
436 u32 v
= (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP
<<
437 __ffs(clkdm
->clktrctrl_mask
));
439 cm_rmw_mod_reg_bits(clkdm
->clktrctrl_mask
, v
,
440 clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
450 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
451 * @clkdm: struct clockdomain *
453 * Allow the hardware to automatically switch the clockdomain into
454 * active or idle states, as needed by downstream clocks. If the
455 * clockdomain has any downstream clocks enabled in the clock
456 * framework, wkdep/sleepdep autodependencies are added; this is so
457 * device drivers can read and write to the device. No return value.
459 void omap2_clkdm_allow_idle(struct clockdomain
*clkdm
)
466 if (!(clkdm
->flags
& CLKDM_CAN_ENABLE_AUTO
)) {
467 pr_debug("clock: automatic idle transitions cannot be enabled "
468 "on clockdomain %s\n", clkdm
->name
);
472 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
475 if (atomic_read(&clkdm
->usecount
) > 0)
476 _clkdm_add_autodeps(clkdm
);
478 if (cpu_is_omap24xx())
479 v
= OMAP24XX_CLKSTCTRL_ENABLE_AUTO
;
480 else if (cpu_is_omap34xx())
481 v
= OMAP34XX_CLKSTCTRL_ENABLE_AUTO
;
486 cm_rmw_mod_reg_bits(clkdm
->clktrctrl_mask
,
487 v
<< __ffs(clkdm
->clktrctrl_mask
),
488 clkdm
->pwrdm
.ptr
->prcm_offs
,
493 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
494 * @clkdm: struct clockdomain *
496 * Prevent the hardware from automatically switching the clockdomain
497 * into inactive or idle states. If the clockdomain has downstream
498 * clocks enabled in the clock framework, wkdep/sleepdep
499 * autodependencies are removed. No return value.
501 void omap2_clkdm_deny_idle(struct clockdomain
*clkdm
)
508 if (!(clkdm
->flags
& CLKDM_CAN_DISABLE_AUTO
)) {
509 pr_debug("clockdomain: automatic idle transitions cannot be "
510 "disabled on %s\n", clkdm
->name
);
514 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
517 if (cpu_is_omap24xx())
518 v
= OMAP24XX_CLKSTCTRL_DISABLE_AUTO
;
519 else if (cpu_is_omap34xx())
520 v
= OMAP34XX_CLKSTCTRL_DISABLE_AUTO
;
524 cm_rmw_mod_reg_bits(clkdm
->clktrctrl_mask
,
525 v
<< __ffs(clkdm
->clktrctrl_mask
),
526 clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
528 if (atomic_read(&clkdm
->usecount
) > 0)
529 _clkdm_del_autodeps(clkdm
);
533 /* Clockdomain-to-clock framework interface code */
536 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
537 * @clkdm: struct clockdomain *
538 * @clk: struct clk * of the enabled downstream clock
540 * Increment the usecount of this clockdomain 'clkdm' and ensure that
541 * it is awake. Intended to be called by clk_enable() code. If the
542 * clockdomain is in software-supervised idle mode, force the
543 * clockdomain to wake. If the clockdomain is in hardware-supervised
544 * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
545 * in the clockdomain can be read from/written to by on-chip processors.
546 * Returns -EINVAL if passed null pointers; returns 0 upon success or
547 * if the clockdomain is in hwsup idle mode.
549 int omap2_clkdm_clk_enable(struct clockdomain
*clkdm
, struct clk
*clk
)
554 * XXX Rewrite this code to maintain a list of enabled
555 * downstream clocks for debugging purposes?
561 if (atomic_inc_return(&clkdm
->usecount
) > 1)
564 /* Clockdomain now has one enabled downstream clock */
566 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm
->name
,
569 v
= omap2_clkdm_clktrctrl_read(clkdm
);
571 if ((cpu_is_omap34xx() && v
== OMAP34XX_CLKSTCTRL_ENABLE_AUTO
) ||
572 (cpu_is_omap24xx() && v
== OMAP24XX_CLKSTCTRL_ENABLE_AUTO
))
573 _clkdm_add_autodeps(clkdm
);
575 omap2_clkdm_wakeup(clkdm
);
577 pwrdm_wait_transition(clkdm
->pwrdm
.ptr
);
583 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
584 * @clkdm: struct clockdomain *
585 * @clk: struct clk * of the disabled downstream clock
587 * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
588 * called by clk_disable() code. If the usecount goes to 0, put the
589 * clockdomain to sleep (software-supervised mode) or remove the
590 * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
591 * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
592 * underflows and debugging is enabled; or returns 0 upon success or
593 * if the clockdomain is in hwsup idle mode.
595 int omap2_clkdm_clk_disable(struct clockdomain
*clkdm
, struct clk
*clk
)
600 * XXX Rewrite this code to maintain a list of enabled
601 * downstream clocks for debugging purposes?
608 if (atomic_read(&clkdm
->usecount
) == 0) {
609 WARN_ON(1); /* underflow */
614 if (atomic_dec_return(&clkdm
->usecount
) > 0)
617 /* All downstream clocks of this clockdomain are now disabled */
619 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm
->name
,
622 v
= omap2_clkdm_clktrctrl_read(clkdm
);
624 if ((cpu_is_omap34xx() && v
== OMAP34XX_CLKSTCTRL_ENABLE_AUTO
) ||
625 (cpu_is_omap24xx() && v
== OMAP24XX_CLKSTCTRL_ENABLE_AUTO
))
626 _clkdm_del_autodeps(clkdm
);
628 omap2_clkdm_sleep(clkdm
);