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>
28 #include <linux/bitops.h>
30 #include <mach/clock.h>
33 #include "prm-regbits-24xx.h"
36 #include <mach/powerdomain.h>
37 #include <mach/clockdomain.h>
39 /* clkdm_list contains all registered struct clockdomains */
40 static LIST_HEAD(clkdm_list);
42 /* clkdm_mutex protects clkdm_list add and del ops */
43 static DEFINE_MUTEX(clkdm_mutex);
45 /* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
46 static struct clkdm_pwrdm_autodep *autodeps;
49 /* Private functions */
52 * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
53 * @autodep: struct clkdm_pwrdm_autodep * to resolve
55 * Resolve autodep powerdomain names to powerdomain pointers via
56 * pwrdm_lookup() and store the pointers in the autodep structure. An
57 * "autodep" is a powerdomain sleep/wakeup dependency that is
58 * automatically added and removed whenever clocks in the associated
59 * clockdomain are enabled or disabled (respectively) when the
60 * clockdomain is in hardware-supervised mode. Meant to be called
61 * once at clockdomain layer initialization, since these should remain
62 * fixed for a particular architecture. No return value.
64 static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
66 struct powerdomain *pwrdm;
71 if (!omap_chip_is(autodep->omap_chip))
74 pwrdm = pwrdm_lookup(autodep->pwrdm_name);
76 pr_debug("clockdomain: _autodep_lookup: powerdomain %s "
77 "does not exist\n", autodep->pwrdm_name);
81 autodep->pwrdm = pwrdm;
87 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
88 * @clkdm: struct clockdomain *
90 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
91 * in hardware-supervised mode. Meant to be called from clock framework
92 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
94 static void _clkdm_add_autodeps(struct clockdomain *clkdm)
96 struct clkdm_pwrdm_autodep *autodep;
98 for (autodep = autodeps; autodep->pwrdm_name; autodep++) {
102 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
103 "pwrdm %s\n", autodep->pwrdm_name,
106 pwrdm_add_sleepdep(clkdm->pwrdm, autodep->pwrdm);
107 pwrdm_add_wkdep(clkdm->pwrdm, autodep->pwrdm);
112 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
113 * @clkdm: struct clockdomain *
115 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
116 * in hardware-supervised mode. Meant to be called from clock framework
117 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
119 static void _clkdm_del_autodeps(struct clockdomain *clkdm)
121 struct clkdm_pwrdm_autodep *autodep;
123 for (autodep = autodeps; autodep->pwrdm_name; autodep++) {
127 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
128 "pwrdm %s\n", autodep->pwrdm_name,
131 pwrdm_del_sleepdep(clkdm->pwrdm, autodep->pwrdm);
132 pwrdm_del_wkdep(clkdm->pwrdm, autodep->pwrdm);
137 static struct clockdomain *_clkdm_lookup(const char *name)
139 struct clockdomain *clkdm, *temp_clkdm;
146 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
147 if (!strcmp(name, temp_clkdm->name)) {
157 /* Public functions */
160 * clkdm_init - set up the clockdomain layer
161 * @clkdms: optional pointer to an array of clockdomains to register
162 * @init_autodeps: optional pointer to an array of autodeps to register
164 * Set up internal state. If a pointer to an array of clockdomains
165 * was supplied, loop through the list of clockdomains, register all
166 * that are available on the current platform. Similarly, if a
167 * pointer to an array of clockdomain-powerdomain autodependencies was
168 * provided, register those. No return value.
170 void clkdm_init(struct clockdomain **clkdms,
171 struct clkdm_pwrdm_autodep *init_autodeps)
173 struct clockdomain **c = NULL;
174 struct clkdm_pwrdm_autodep *autodep = NULL;
177 for (c = clkdms; *c; c++)
180 autodeps = init_autodeps;
182 for (autodep = autodeps; autodep->pwrdm_name; autodep++)
183 _autodep_lookup(autodep);
187 * clkdm_register - register a clockdomain
188 * @clkdm: struct clockdomain * to register
190 * Adds a clockdomain to the internal clockdomain list.
191 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
192 * already registered by the provided name, or 0 upon success.
194 int clkdm_register(struct clockdomain *clkdm)
197 struct powerdomain *pwrdm;
199 if (!clkdm || !clkdm->name)
202 if (!omap_chip_is(clkdm->omap_chip))
205 pwrdm = pwrdm_lookup(clkdm->pwrdm_name);
207 pr_debug("clockdomain: clkdm_register %s: powerdomain %s "
208 "does not exist\n", clkdm->name, clkdm->pwrdm_name);
211 clkdm->pwrdm = pwrdm;
213 mutex_lock(&clkdm_mutex);
214 /* Verify that the clockdomain is not already registered */
215 if (_clkdm_lookup(clkdm->name)) {
220 list_add(&clkdm->node, &clkdm_list);
222 pr_debug("clockdomain: registered %s\n", clkdm->name);
226 mutex_unlock(&clkdm_mutex);
232 * clkdm_unregister - unregister a clockdomain
233 * @clkdm: struct clockdomain * to unregister
235 * Removes a clockdomain from the internal clockdomain list. Returns
236 * -EINVAL if clkdm argument is NULL.
238 int clkdm_unregister(struct clockdomain *clkdm)
243 mutex_lock(&clkdm_mutex);
244 list_del(&clkdm->node);
245 mutex_unlock(&clkdm_mutex);
247 pr_debug("clockdomain: unregistered %s\n", clkdm->name);
253 * clkdm_lookup - look up a clockdomain by name, return a pointer
254 * @name: name of clockdomain
256 * Find a registered clockdomain by its name. Returns a pointer to the
257 * struct clockdomain if found, or NULL otherwise.
259 struct clockdomain *clkdm_lookup(const char *name)
261 struct clockdomain *clkdm, *temp_clkdm;
268 mutex_lock(&clkdm_mutex);
269 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
270 if (!strcmp(name, temp_clkdm->name)) {
275 mutex_unlock(&clkdm_mutex);
281 * clkdm_for_each - call function on each registered clockdomain
282 * @fn: callback function *
284 * Call the supplied function for each registered clockdomain.
285 * The callback function can return anything but 0 to bail
286 * out early from the iterator. The callback function is called with
287 * the clkdm_mutex held, so no clockdomain structure manipulation
288 * functions should be called from the callback, although hardware
289 * clockdomain control functions are fine. Returns the last return
290 * value of the callback function, which should be 0 for success or
291 * anything else to indicate failure; or -EINVAL if the function pointer
294 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm))
296 struct clockdomain *clkdm;
302 mutex_lock(&clkdm_mutex);
303 list_for_each_entry(clkdm, &clkdm_list, node) {
308 mutex_unlock(&clkdm_mutex);
314 /* Hardware clockdomain control */
317 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
318 * @clk: struct clk * of a clockdomain
320 * Return the clockdomain's current state transition mode from the
321 * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if clk
322 * is NULL or the current mode upon success.
324 static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
331 v = cm_read_mod_reg(clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
332 v &= clkdm->clktrctrl_mask;
333 v >>= __ffs(clkdm->clktrctrl_mask);
339 * omap2_clkdm_sleep - force clockdomain sleep transition
340 * @clkdm: struct clockdomain *
342 * Instruct the CM to force a sleep transition on the specified
343 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
344 * clockdomain does not support software-initiated sleep; 0 upon
347 int omap2_clkdm_sleep(struct clockdomain *clkdm)
352 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
353 pr_debug("clockdomain: %s does not support forcing "
354 "sleep via software\n", clkdm->name);
358 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
360 if (cpu_is_omap24xx()) {
362 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
363 clkdm->pwrdm->prcm_offs, PM_PWSTCTRL);
365 } else if (cpu_is_omap34xx()) {
367 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
368 __ffs(clkdm->clktrctrl_mask));
370 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
371 clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
381 * omap2_clkdm_wakeup - force clockdomain wakeup transition
382 * @clkdm: struct clockdomain *
384 * Instruct the CM to force a wakeup transition on the specified
385 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
386 * clockdomain does not support software-controlled wakeup; 0 upon
389 int omap2_clkdm_wakeup(struct clockdomain *clkdm)
394 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
395 pr_debug("clockdomain: %s does not support forcing "
396 "wakeup via software\n", clkdm->name);
400 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
402 if (cpu_is_omap24xx()) {
404 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
405 clkdm->pwrdm->prcm_offs, PM_PWSTCTRL);
407 } else if (cpu_is_omap34xx()) {
409 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
410 __ffs(clkdm->clktrctrl_mask));
412 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
413 clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
423 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
424 * @clkdm: struct clockdomain *
426 * Allow the hardware to automatically switch the clockdomain into
427 * active or idle states, as needed by downstream clocks. If the
428 * clockdomain has any downstream clocks enabled in the clock
429 * framework, wkdep/sleepdep autodependencies are added; this is so
430 * device drivers can read and write to the device. No return value.
432 void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
439 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
440 pr_debug("clock: automatic idle transitions cannot be enabled "
441 "on clockdomain %s\n", clkdm->name);
445 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
448 if (atomic_read(&clkdm->usecount) > 0)
449 _clkdm_add_autodeps(clkdm);
451 if (cpu_is_omap24xx())
452 v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
453 else if (cpu_is_omap34xx())
454 v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
459 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
460 v << __ffs(clkdm->clktrctrl_mask),
461 clkdm->pwrdm->prcm_offs,
466 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
467 * @clkdm: struct clockdomain *
469 * Prevent the hardware from automatically switching the clockdomain
470 * into inactive or idle states. If the clockdomain has downstream
471 * clocks enabled in the clock framework, wkdep/sleepdep
472 * autodependencies are removed. No return value.
474 void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
481 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
482 pr_debug("clockdomain: automatic idle transitions cannot be "
483 "disabled on %s\n", clkdm->name);
487 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
490 if (cpu_is_omap24xx())
491 v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
492 else if (cpu_is_omap34xx())
493 v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
497 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
498 v << __ffs(clkdm->clktrctrl_mask),
499 clkdm->pwrdm->prcm_offs, CM_CLKSTCTRL);
501 if (atomic_read(&clkdm->usecount) > 0)
502 _clkdm_del_autodeps(clkdm);
506 /* Clockdomain-to-clock framework interface code */
509 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
510 * @clkdm: struct clockdomain *
511 * @clk: struct clk * of the enabled downstream clock
513 * Increment the usecount of this clockdomain 'clkdm' and ensure that
514 * it is awake. Intended to be called by clk_enable() code. If the
515 * clockdomain is in software-supervised idle mode, force the
516 * clockdomain to wake. If the clockdomain is in hardware-supervised
517 * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
518 * in the clockdomain can be read from/written to by on-chip processors.
519 * Returns -EINVAL if passed null pointers; returns 0 upon success or
520 * if the clockdomain is in hwsup idle mode.
522 int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
527 * XXX Rewrite this code to maintain a list of enabled
528 * downstream clocks for debugging purposes?
534 if (atomic_inc_return(&clkdm->usecount) > 1)
537 /* Clockdomain now has one enabled downstream clock */
539 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
542 v = omap2_clkdm_clktrctrl_read(clkdm);
544 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
545 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
546 _clkdm_add_autodeps(clkdm);
548 omap2_clkdm_wakeup(clkdm);
554 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
555 * @clkdm: struct clockdomain *
556 * @clk: struct clk * of the disabled downstream clock
558 * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
559 * called by clk_disable() code. If the usecount goes to 0, put the
560 * clockdomain to sleep (software-supervised mode) or remove the
561 * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
562 * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
563 * underflows and debugging is enabled; or returns 0 upon success or
564 * if the clockdomain is in hwsup idle mode.
566 int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
571 * XXX Rewrite this code to maintain a list of enabled
572 * downstream clocks for debugging purposes?
579 if (atomic_read(&clkdm->usecount) == 0) {
580 WARN_ON(1); /* underflow */
585 if (atomic_dec_return(&clkdm->usecount) > 0)
588 /* All downstream clocks of this clockdomain are now disabled */
590 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
593 v = omap2_clkdm_clktrctrl_read(clkdm);
595 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
596 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
597 _clkdm_del_autodeps(clkdm);
599 omap2_clkdm_sleep(clkdm);