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 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 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
129 "pwrdm %s\n", autodep->pwrdm.ptr->name,
130 clkdm->pwrdm.ptr->name);
132 pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
133 pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
138 static struct clockdomain *_clkdm_lookup(const char *name)
140 struct clockdomain *clkdm, *temp_clkdm;
147 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
148 if (!strcmp(name, temp_clkdm->name)) {
158 /* Public functions */
161 * clkdm_init - set up the clockdomain layer
162 * @clkdms: optional pointer to an array of clockdomains to register
163 * @init_autodeps: optional pointer to an array of autodeps to register
165 * Set up internal state. If a pointer to an array of clockdomains
166 * was supplied, loop through the list of clockdomains, register all
167 * that are available on the current platform. Similarly, if a
168 * pointer to an array of clockdomain-powerdomain autodependencies was
169 * provided, register those. No return value.
171 void clkdm_init(struct clockdomain **clkdms,
172 struct clkdm_pwrdm_autodep *init_autodeps)
174 struct clockdomain **c = NULL;
175 struct clkdm_pwrdm_autodep *autodep = NULL;
178 for (c = clkdms; *c; c++)
181 autodeps = init_autodeps;
183 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
184 _autodep_lookup(autodep);
188 * clkdm_register - register a clockdomain
189 * @clkdm: struct clockdomain * to register
191 * Adds a clockdomain to the internal clockdomain list.
192 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
193 * already registered by the provided name, or 0 upon success.
195 int clkdm_register(struct clockdomain *clkdm)
198 struct powerdomain *pwrdm;
200 if (!clkdm || !clkdm->name)
203 if (!omap_chip_is(clkdm->omap_chip))
206 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
208 pr_debug("clockdomain: clkdm_register %s: powerdomain %s "
209 "does not exist\n", clkdm->name, clkdm->pwrdm.name);
212 clkdm->pwrdm.ptr = pwrdm;
214 mutex_lock(&clkdm_mutex);
215 /* Verify that the clockdomain is not already registered */
216 if (_clkdm_lookup(clkdm->name)) {
221 list_add(&clkdm->node, &clkdm_list);
223 pwrdm_add_clkdm(pwrdm, clkdm);
225 pr_debug("clockdomain: registered %s\n", clkdm->name);
229 mutex_unlock(&clkdm_mutex);
235 * clkdm_unregister - unregister a clockdomain
236 * @clkdm: struct clockdomain * to unregister
238 * Removes a clockdomain from the internal clockdomain list. Returns
239 * -EINVAL if clkdm argument is NULL.
241 int clkdm_unregister(struct clockdomain *clkdm)
246 pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
248 mutex_lock(&clkdm_mutex);
249 list_del(&clkdm->node);
250 mutex_unlock(&clkdm_mutex);
252 pr_debug("clockdomain: unregistered %s\n", clkdm->name);
258 * clkdm_lookup - look up a clockdomain by name, return a pointer
259 * @name: name of clockdomain
261 * Find a registered clockdomain by its name. Returns a pointer to the
262 * struct clockdomain if found, or NULL otherwise.
264 struct clockdomain *clkdm_lookup(const char *name)
266 struct clockdomain *clkdm, *temp_clkdm;
273 mutex_lock(&clkdm_mutex);
274 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
275 if (!strcmp(name, temp_clkdm->name)) {
280 mutex_unlock(&clkdm_mutex);
286 * clkdm_for_each - call function on each registered clockdomain
287 * @fn: callback function *
289 * Call the supplied function for each registered clockdomain.
290 * The callback function can return anything but 0 to bail
291 * out early from the iterator. The callback function is called with
292 * the clkdm_mutex held, so no clockdomain structure manipulation
293 * functions should be called from the callback, although hardware
294 * clockdomain control functions are fine. Returns the last return
295 * value of the callback function, which should be 0 for success or
296 * anything else to indicate failure; or -EINVAL if the function pointer
299 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm))
301 struct clockdomain *clkdm;
307 mutex_lock(&clkdm_mutex);
308 list_for_each_entry(clkdm, &clkdm_list, node) {
313 mutex_unlock(&clkdm_mutex);
320 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
321 * @clkdm: struct clockdomain *
323 * Return a pointer to the struct powerdomain that the specified clockdomain
324 * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
326 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
331 return clkdm->pwrdm.ptr;
335 /* Hardware clockdomain control */
338 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
339 * @clk: struct clk * of a clockdomain
341 * Return the clockdomain's current state transition mode from the
342 * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if clk
343 * is NULL or the current mode upon success.
345 static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
352 v = cm_read_mod_reg(clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
353 v &= clkdm->clktrctrl_mask;
354 v >>= __ffs(clkdm->clktrctrl_mask);
360 * omap2_clkdm_sleep - force clockdomain sleep transition
361 * @clkdm: struct clockdomain *
363 * Instruct the CM to force a sleep transition on the specified
364 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
365 * clockdomain does not support software-initiated sleep; 0 upon
368 int omap2_clkdm_sleep(struct clockdomain *clkdm)
373 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
374 pr_debug("clockdomain: %s does not support forcing "
375 "sleep via software\n", clkdm->name);
379 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
381 if (cpu_is_omap24xx()) {
383 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
384 clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
386 } else if (cpu_is_omap34xx()) {
388 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
389 __ffs(clkdm->clktrctrl_mask));
391 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
392 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
402 * omap2_clkdm_wakeup - force clockdomain wakeup transition
403 * @clkdm: struct clockdomain *
405 * Instruct the CM to force a wakeup transition on the specified
406 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
407 * clockdomain does not support software-controlled wakeup; 0 upon
410 int omap2_clkdm_wakeup(struct clockdomain *clkdm)
415 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
416 pr_debug("clockdomain: %s does not support forcing "
417 "wakeup via software\n", clkdm->name);
421 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
423 if (cpu_is_omap24xx()) {
425 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
426 clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
428 } else if (cpu_is_omap34xx()) {
430 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
431 __ffs(clkdm->clktrctrl_mask));
433 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
434 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
444 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
445 * @clkdm: struct clockdomain *
447 * Allow the hardware to automatically switch the clockdomain into
448 * active or idle states, as needed by downstream clocks. If the
449 * clockdomain has any downstream clocks enabled in the clock
450 * framework, wkdep/sleepdep autodependencies are added; this is so
451 * device drivers can read and write to the device. No return value.
453 void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
460 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
461 pr_debug("clock: automatic idle transitions cannot be enabled "
462 "on clockdomain %s\n", clkdm->name);
466 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
469 if (atomic_read(&clkdm->usecount) > 0)
470 _clkdm_add_autodeps(clkdm);
472 if (cpu_is_omap24xx())
473 v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
474 else if (cpu_is_omap34xx())
475 v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
480 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
481 v << __ffs(clkdm->clktrctrl_mask),
482 clkdm->pwrdm.ptr->prcm_offs,
487 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
488 * @clkdm: struct clockdomain *
490 * Prevent the hardware from automatically switching the clockdomain
491 * into inactive or idle states. If the clockdomain has downstream
492 * clocks enabled in the clock framework, wkdep/sleepdep
493 * autodependencies are removed. No return value.
495 void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
502 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
503 pr_debug("clockdomain: automatic idle transitions cannot be "
504 "disabled on %s\n", clkdm->name);
508 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
511 if (cpu_is_omap24xx())
512 v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
513 else if (cpu_is_omap34xx())
514 v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
518 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
519 v << __ffs(clkdm->clktrctrl_mask),
520 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
522 if (atomic_read(&clkdm->usecount) > 0)
523 _clkdm_del_autodeps(clkdm);
527 /* Clockdomain-to-clock framework interface code */
530 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
531 * @clkdm: struct clockdomain *
532 * @clk: struct clk * of the enabled downstream clock
534 * Increment the usecount of this clockdomain 'clkdm' and ensure that
535 * it is awake. Intended to be called by clk_enable() code. If the
536 * clockdomain is in software-supervised idle mode, force the
537 * clockdomain to wake. If the clockdomain is in hardware-supervised
538 * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
539 * in the clockdomain can be read from/written to by on-chip processors.
540 * Returns -EINVAL if passed null pointers; returns 0 upon success or
541 * if the clockdomain is in hwsup idle mode.
543 int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
548 * XXX Rewrite this code to maintain a list of enabled
549 * downstream clocks for debugging purposes?
555 if (atomic_inc_return(&clkdm->usecount) > 1)
558 /* Clockdomain now has one enabled downstream clock */
560 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
563 v = omap2_clkdm_clktrctrl_read(clkdm);
565 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
566 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
567 _clkdm_add_autodeps(clkdm);
569 omap2_clkdm_wakeup(clkdm);
575 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
576 * @clkdm: struct clockdomain *
577 * @clk: struct clk * of the disabled downstream clock
579 * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
580 * called by clk_disable() code. If the usecount goes to 0, put the
581 * clockdomain to sleep (software-supervised mode) or remove the
582 * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
583 * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
584 * underflows and debugging is enabled; or returns 0 upon success or
585 * if the clockdomain is in hwsup idle mode.
587 int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
592 * XXX Rewrite this code to maintain a list of enabled
593 * downstream clocks for debugging purposes?
600 if (atomic_read(&clkdm->usecount) == 0) {
601 WARN_ON(1); /* underflow */
606 if (atomic_dec_return(&clkdm->usecount) > 0)
609 /* All downstream clocks of this clockdomain are now disabled */
611 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
614 v = omap2_clkdm_clktrctrl_read(clkdm);
616 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
617 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
618 _clkdm_del_autodeps(clkdm);
620 omap2_clkdm_sleep(clkdm);