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_err("clockdomain: autodeps: powerdomain %s does not exist\n",
79 pwrdm
= ERR_PTR(-ENOENT
);
81 autodep
->pwrdm
.ptr
= pwrdm
;
85 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
86 * @clkdm: struct clockdomain *
88 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
89 * in hardware-supervised mode. Meant to be called from clock framework
90 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
92 static void _clkdm_add_autodeps(struct clockdomain
*clkdm
)
94 struct clkdm_pwrdm_autodep
*autodep
;
96 for (autodep
= autodeps
; autodep
->pwrdm
.ptr
; autodep
++) {
97 if (IS_ERR(autodep
->pwrdm
.ptr
))
100 if (!omap_chip_is(autodep
->omap_chip
))
103 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
104 "pwrdm %s\n", autodep
->pwrdm
.ptr
->name
,
105 clkdm
->pwrdm
.ptr
->name
);
107 pwrdm_add_sleepdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
108 pwrdm_add_wkdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
113 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
114 * @clkdm: struct clockdomain *
116 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
117 * in hardware-supervised mode. Meant to be called from clock framework
118 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
120 static void _clkdm_del_autodeps(struct clockdomain
*clkdm
)
122 struct clkdm_pwrdm_autodep
*autodep
;
124 for (autodep
= autodeps
; autodep
->pwrdm
.ptr
; autodep
++) {
125 if (IS_ERR(autodep
->pwrdm
.ptr
))
128 if (!omap_chip_is(autodep
->omap_chip
))
131 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
132 "pwrdm %s\n", autodep
->pwrdm
.ptr
->name
,
133 clkdm
->pwrdm
.ptr
->name
);
135 pwrdm_del_sleepdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
136 pwrdm_del_wkdep(clkdm
->pwrdm
.ptr
, autodep
->pwrdm
.ptr
);
141 * _omap2_clkdm_set_hwsup - set the hwsup idle transition bit
142 * @clkdm: struct clockdomain *
143 * @enable: int 0 to disable, 1 to enable
145 * Internal helper for actually switching the bit that controls hwsup
146 * idle transitions for clkdm.
148 static void _omap2_clkdm_set_hwsup(struct clockdomain
*clkdm
, int enable
)
152 if (cpu_is_omap24xx()) {
154 v
= OMAP24XX_CLKSTCTRL_ENABLE_AUTO
;
156 v
= OMAP24XX_CLKSTCTRL_DISABLE_AUTO
;
157 } else if (cpu_is_omap34xx()) {
159 v
= OMAP34XX_CLKSTCTRL_ENABLE_AUTO
;
161 v
= OMAP34XX_CLKSTCTRL_DISABLE_AUTO
;
166 cm_rmw_mod_reg_bits(clkdm
->clktrctrl_mask
,
167 v
<< __ffs(clkdm
->clktrctrl_mask
),
168 clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
171 static struct clockdomain
*_clkdm_lookup(const char *name
)
173 struct clockdomain
*clkdm
, *temp_clkdm
;
180 list_for_each_entry(temp_clkdm
, &clkdm_list
, node
) {
181 if (!strcmp(name
, temp_clkdm
->name
)) {
191 /* Public functions */
194 * clkdm_init - set up the clockdomain layer
195 * @clkdms: optional pointer to an array of clockdomains to register
196 * @init_autodeps: optional pointer to an array of autodeps to register
198 * Set up internal state. If a pointer to an array of clockdomains
199 * was supplied, loop through the list of clockdomains, register all
200 * that are available on the current platform. Similarly, if a
201 * pointer to an array of clockdomain-powerdomain autodependencies was
202 * provided, register those. No return value.
204 void clkdm_init(struct clockdomain
**clkdms
,
205 struct clkdm_pwrdm_autodep
*init_autodeps
)
207 struct clockdomain
**c
= NULL
;
208 struct clkdm_pwrdm_autodep
*autodep
= NULL
;
211 for (c
= clkdms
; *c
; c
++)
214 autodeps
= init_autodeps
;
216 for (autodep
= autodeps
; autodep
->pwrdm
.ptr
; autodep
++)
217 _autodep_lookup(autodep
);
221 * clkdm_register - register a clockdomain
222 * @clkdm: struct clockdomain * to register
224 * Adds a clockdomain to the internal clockdomain list.
225 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
226 * already registered by the provided name, or 0 upon success.
228 int clkdm_register(struct clockdomain
*clkdm
)
231 struct powerdomain
*pwrdm
;
233 if (!clkdm
|| !clkdm
->name
)
236 if (!omap_chip_is(clkdm
->omap_chip
))
239 pwrdm
= pwrdm_lookup(clkdm
->pwrdm
.name
);
241 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
242 clkdm
->name
, clkdm
->pwrdm
.name
);
245 clkdm
->pwrdm
.ptr
= pwrdm
;
247 mutex_lock(&clkdm_mutex
);
248 /* Verify that the clockdomain is not already registered */
249 if (_clkdm_lookup(clkdm
->name
)) {
254 list_add(&clkdm
->node
, &clkdm_list
);
256 pwrdm_add_clkdm(pwrdm
, clkdm
);
258 pr_debug("clockdomain: registered %s\n", clkdm
->name
);
262 mutex_unlock(&clkdm_mutex
);
268 * clkdm_unregister - unregister a clockdomain
269 * @clkdm: struct clockdomain * to unregister
271 * Removes a clockdomain from the internal clockdomain list. Returns
272 * -EINVAL if clkdm argument is NULL.
274 int clkdm_unregister(struct clockdomain
*clkdm
)
279 pwrdm_del_clkdm(clkdm
->pwrdm
.ptr
, clkdm
);
281 mutex_lock(&clkdm_mutex
);
282 list_del(&clkdm
->node
);
283 mutex_unlock(&clkdm_mutex
);
285 pr_debug("clockdomain: unregistered %s\n", clkdm
->name
);
291 * clkdm_lookup - look up a clockdomain by name, return a pointer
292 * @name: name of clockdomain
294 * Find a registered clockdomain by its name. Returns a pointer to the
295 * struct clockdomain if found, or NULL otherwise.
297 struct clockdomain
*clkdm_lookup(const char *name
)
299 struct clockdomain
*clkdm
, *temp_clkdm
;
306 mutex_lock(&clkdm_mutex
);
307 list_for_each_entry(temp_clkdm
, &clkdm_list
, node
) {
308 if (!strcmp(name
, temp_clkdm
->name
)) {
313 mutex_unlock(&clkdm_mutex
);
319 * clkdm_for_each - call function on each registered clockdomain
320 * @fn: callback function *
322 * Call the supplied function for each registered clockdomain.
323 * The callback function can return anything but 0 to bail
324 * out early from the iterator. The callback function is called with
325 * the clkdm_mutex held, so no clockdomain structure manipulation
326 * functions should be called from the callback, although hardware
327 * clockdomain control functions are fine. Returns the last return
328 * value of the callback function, which should be 0 for success or
329 * anything else to indicate failure; or -EINVAL if the function pointer
332 int clkdm_for_each(int (*fn
)(struct clockdomain
*clkdm
, void *user
),
335 struct clockdomain
*clkdm
;
341 mutex_lock(&clkdm_mutex
);
342 list_for_each_entry(clkdm
, &clkdm_list
, node
) {
343 ret
= (*fn
)(clkdm
, user
);
347 mutex_unlock(&clkdm_mutex
);
354 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
355 * @clkdm: struct clockdomain *
357 * Return a pointer to the struct powerdomain that the specified clockdomain
358 * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
360 struct powerdomain
*clkdm_get_pwrdm(struct clockdomain
*clkdm
)
365 return clkdm
->pwrdm
.ptr
;
369 /* Hardware clockdomain control */
372 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
373 * @clk: struct clk * of a clockdomain
375 * Return the clockdomain's current state transition mode from the
376 * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if clk
377 * is NULL or the current mode upon success.
379 static int omap2_clkdm_clktrctrl_read(struct clockdomain
*clkdm
)
386 v
= cm_read_mod_reg(clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
387 v
&= clkdm
->clktrctrl_mask
;
388 v
>>= __ffs(clkdm
->clktrctrl_mask
);
394 * omap2_clkdm_sleep - force clockdomain sleep transition
395 * @clkdm: struct clockdomain *
397 * Instruct the CM to force a sleep transition on the specified
398 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
399 * clockdomain does not support software-initiated sleep; 0 upon
402 int omap2_clkdm_sleep(struct clockdomain
*clkdm
)
407 if (!(clkdm
->flags
& CLKDM_CAN_FORCE_SLEEP
)) {
408 pr_debug("clockdomain: %s does not support forcing "
409 "sleep via software\n", clkdm
->name
);
413 pr_debug("clockdomain: forcing sleep on %s\n", clkdm
->name
);
415 if (cpu_is_omap24xx()) {
417 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE
,
418 clkdm
->pwrdm
.ptr
->prcm_offs
, PM_PWSTCTRL
);
420 } else if (cpu_is_omap34xx()) {
422 u32 v
= (OMAP34XX_CLKSTCTRL_FORCE_SLEEP
<<
423 __ffs(clkdm
->clktrctrl_mask
));
425 cm_rmw_mod_reg_bits(clkdm
->clktrctrl_mask
, v
,
426 clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
436 * omap2_clkdm_wakeup - force clockdomain wakeup transition
437 * @clkdm: struct clockdomain *
439 * Instruct the CM to force a wakeup transition on the specified
440 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
441 * clockdomain does not support software-controlled wakeup; 0 upon
444 int omap2_clkdm_wakeup(struct clockdomain
*clkdm
)
449 if (!(clkdm
->flags
& CLKDM_CAN_FORCE_WAKEUP
)) {
450 pr_debug("clockdomain: %s does not support forcing "
451 "wakeup via software\n", clkdm
->name
);
455 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm
->name
);
457 if (cpu_is_omap24xx()) {
459 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE
,
460 clkdm
->pwrdm
.ptr
->prcm_offs
, PM_PWSTCTRL
);
462 } else if (cpu_is_omap34xx()) {
464 u32 v
= (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP
<<
465 __ffs(clkdm
->clktrctrl_mask
));
467 cm_rmw_mod_reg_bits(clkdm
->clktrctrl_mask
, v
,
468 clkdm
->pwrdm
.ptr
->prcm_offs
, CM_CLKSTCTRL
);
478 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
479 * @clkdm: struct clockdomain *
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.
487 void omap2_clkdm_allow_idle(struct clockdomain
*clkdm
)
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
);
498 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
501 if (atomic_read(&clkdm
->usecount
) > 0)
502 _clkdm_add_autodeps(clkdm
);
504 _omap2_clkdm_set_hwsup(clkdm
, 1);
506 pwrdm_clkdm_state_switch(clkdm
);
510 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
511 * @clkdm: struct clockdomain *
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.
518 void omap2_clkdm_deny_idle(struct clockdomain
*clkdm
)
523 if (!(clkdm
->flags
& CLKDM_CAN_DISABLE_AUTO
)) {
524 pr_debug("clockdomain: automatic idle transitions cannot be "
525 "disabled on %s\n", clkdm
->name
);
529 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
532 _omap2_clkdm_set_hwsup(clkdm
, 0);
534 if (atomic_read(&clkdm
->usecount
) > 0)
535 _clkdm_del_autodeps(clkdm
);
539 /* Clockdomain-to-clock framework interface code */
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
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.
555 int omap2_clkdm_clk_enable(struct clockdomain
*clkdm
, struct clk
*clk
)
560 * XXX Rewrite this code to maintain a list of enabled
561 * downstream clocks for debugging purposes?
567 if (atomic_inc_return(&clkdm
->usecount
) > 1)
570 /* Clockdomain now has one enabled downstream clock */
572 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm
->name
,
575 v
= omap2_clkdm_clktrctrl_read(clkdm
);
577 if ((cpu_is_omap34xx() && v
== OMAP34XX_CLKSTCTRL_ENABLE_AUTO
) ||
578 (cpu_is_omap24xx() && v
== OMAP24XX_CLKSTCTRL_ENABLE_AUTO
)) {
579 /* Disable HW transitions when we are changing deps */
580 _omap2_clkdm_set_hwsup(clkdm
, 0);
581 _clkdm_add_autodeps(clkdm
);
582 _omap2_clkdm_set_hwsup(clkdm
, 1);
584 omap2_clkdm_wakeup(clkdm
);
587 pwrdm_wait_transition(clkdm
->pwrdm
.ptr
);
588 pwrdm_clkdm_state_switch(clkdm
);
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
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.
606 int omap2_clkdm_clk_disable(struct clockdomain
*clkdm
, struct clk
*clk
)
611 * XXX Rewrite this code to maintain a list of enabled
612 * downstream clocks for debugging purposes?
619 if (atomic_read(&clkdm
->usecount
) == 0) {
620 WARN_ON(1); /* underflow */
625 if (atomic_dec_return(&clkdm
->usecount
) > 0)
628 /* All downstream clocks of this clockdomain are now disabled */
630 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm
->name
,
633 v
= omap2_clkdm_clktrctrl_read(clkdm
);
635 if ((cpu_is_omap34xx() && v
== OMAP34XX_CLKSTCTRL_ENABLE_AUTO
) ||
636 (cpu_is_omap24xx() && v
== OMAP24XX_CLKSTCTRL_ENABLE_AUTO
)) {
637 /* Disable HW transitions when we are changing deps */
638 _omap2_clkdm_set_hwsup(clkdm
, 0);
639 _clkdm_del_autodeps(clkdm
);
640 _omap2_clkdm_set_hwsup(clkdm
, 1);
642 omap2_clkdm_sleep(clkdm
);
645 pwrdm_clkdm_state_switch(clkdm
);