Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / arch / arm / mach-omap2 / clockdomain.c
blob8f0890685d7b10f7f594e9cd44c8a972c33d15e9
1 /*
2 * OMAP2/3/4 clockdomain framework functions
4 * Copyright (C) 2008-2011 Texas Instruments, Inc.
5 * Copyright (C) 2008-2011 Nokia Corporation
7 * Written by Paul Walmsley and Jouni Högander
8 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 #undef DEBUG
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>
23 #include <linux/err.h>
25 #include <linux/io.h>
27 #include <linux/bitops.h>
29 #include <plat/clock.h>
30 #include "clockdomain.h"
32 /* clkdm_list contains all registered struct clockdomains */
33 static LIST_HEAD(clkdm_list);
35 /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
36 static struct clkdm_autodep *autodeps;
38 static struct clkdm_ops *arch_clkdm;
40 /* Private functions */
42 static struct clockdomain *_clkdm_lookup(const char *name)
44 struct clockdomain *clkdm, *temp_clkdm;
46 if (!name)
47 return NULL;
49 clkdm = NULL;
51 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
52 if (!strcmp(name, temp_clkdm->name)) {
53 clkdm = temp_clkdm;
54 break;
58 return clkdm;
61 /**
62 * _clkdm_register - register a clockdomain
63 * @clkdm: struct clockdomain * to register
65 * Adds a clockdomain to the internal clockdomain list.
66 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
67 * already registered by the provided name, or 0 upon success.
69 static int _clkdm_register(struct clockdomain *clkdm)
71 struct powerdomain *pwrdm;
73 if (!clkdm || !clkdm->name)
74 return -EINVAL;
76 if (!omap_chip_is(clkdm->omap_chip))
77 return -EINVAL;
79 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
80 if (!pwrdm) {
81 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
82 clkdm->name, clkdm->pwrdm.name);
83 return -EINVAL;
85 clkdm->pwrdm.ptr = pwrdm;
87 /* Verify that the clockdomain is not already registered */
88 if (_clkdm_lookup(clkdm->name))
89 return -EEXIST;
91 list_add(&clkdm->node, &clkdm_list);
93 pwrdm_add_clkdm(pwrdm, clkdm);
95 spin_lock_init(&clkdm->lock);
97 pr_debug("clockdomain: registered %s\n", clkdm->name);
99 return 0;
102 /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
103 static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
104 struct clkdm_dep *deps)
106 struct clkdm_dep *cd;
108 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
109 return ERR_PTR(-EINVAL);
111 for (cd = deps; cd->clkdm_name; cd++) {
112 if (!omap_chip_is(cd->omap_chip))
113 continue;
115 if (!cd->clkdm && cd->clkdm_name)
116 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
118 if (cd->clkdm == clkdm)
119 break;
122 if (!cd->clkdm_name)
123 return ERR_PTR(-ENOENT);
125 return cd;
129 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
130 * @autodep: struct clkdm_autodep * to resolve
132 * Resolve autodep clockdomain names to clockdomain pointers via
133 * clkdm_lookup() and store the pointers in the autodep structure. An
134 * "autodep" is a clockdomain sleep/wakeup dependency that is
135 * automatically added and removed whenever clocks in the associated
136 * clockdomain are enabled or disabled (respectively) when the
137 * clockdomain is in hardware-supervised mode. Meant to be called
138 * once at clockdomain layer initialization, since these should remain
139 * fixed for a particular architecture. No return value.
141 * XXX autodeps are deprecated and should be removed at the earliest
142 * opportunity
144 static void _autodep_lookup(struct clkdm_autodep *autodep)
146 struct clockdomain *clkdm;
148 if (!autodep)
149 return;
151 if (!omap_chip_is(autodep->omap_chip))
152 return;
154 clkdm = clkdm_lookup(autodep->clkdm.name);
155 if (!clkdm) {
156 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
157 autodep->clkdm.name);
158 clkdm = ERR_PTR(-ENOENT);
160 autodep->clkdm.ptr = clkdm;
164 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
165 * @clkdm: struct clockdomain *
167 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
168 * in hardware-supervised mode. Meant to be called from clock framework
169 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
171 * XXX autodeps are deprecated and should be removed at the earliest
172 * opportunity
174 void _clkdm_add_autodeps(struct clockdomain *clkdm)
176 struct clkdm_autodep *autodep;
178 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
179 return;
181 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
182 if (IS_ERR(autodep->clkdm.ptr))
183 continue;
185 if (!omap_chip_is(autodep->omap_chip))
186 continue;
188 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
189 "clkdm %s\n", autodep->clkdm.ptr->name,
190 clkdm->name);
192 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
193 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
198 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
199 * @clkdm: struct clockdomain *
201 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
202 * in hardware-supervised mode. Meant to be called from clock framework
203 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
205 * XXX autodeps are deprecated and should be removed at the earliest
206 * opportunity
208 void _clkdm_del_autodeps(struct clockdomain *clkdm)
210 struct clkdm_autodep *autodep;
212 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
213 return;
215 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
216 if (IS_ERR(autodep->clkdm.ptr))
217 continue;
219 if (!omap_chip_is(autodep->omap_chip))
220 continue;
222 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
223 "clkdm %s\n", autodep->clkdm.ptr->name,
224 clkdm->name);
226 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
227 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
232 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
233 * @clkdm: clockdomain that we are resolving dependencies for
234 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
236 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
237 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
238 * No return value.
240 static void _resolve_clkdm_deps(struct clockdomain *clkdm,
241 struct clkdm_dep *clkdm_deps)
243 struct clkdm_dep *cd;
245 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
246 if (!omap_chip_is(cd->omap_chip))
247 continue;
248 if (cd->clkdm)
249 continue;
250 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
252 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
253 clkdm->name, cd->clkdm_name);
257 /* Public functions */
260 * clkdm_init - set up the clockdomain layer
261 * @clkdms: optional pointer to an array of clockdomains to register
262 * @init_autodeps: optional pointer to an array of autodeps to register
263 * @custom_funcs: func pointers for arch specific implementations
265 * Set up internal state. If a pointer to an array of clockdomains
266 * @clkdms was supplied, loop through the list of clockdomains,
267 * register all that are available on the current platform. Similarly,
268 * if a pointer to an array of clockdomain autodependencies
269 * @init_autodeps was provided, register those. No return value.
271 void clkdm_init(struct clockdomain **clkdms,
272 struct clkdm_autodep *init_autodeps,
273 struct clkdm_ops *custom_funcs)
275 struct clockdomain **c = NULL;
276 struct clockdomain *clkdm;
277 struct clkdm_autodep *autodep = NULL;
279 if (!custom_funcs)
280 WARN(1, "No custom clkdm functions registered\n");
281 else
282 arch_clkdm = custom_funcs;
284 if (clkdms)
285 for (c = clkdms; *c; c++)
286 _clkdm_register(*c);
288 autodeps = init_autodeps;
289 if (autodeps)
290 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
291 _autodep_lookup(autodep);
294 * Put all clockdomains into software-supervised mode; PM code
295 * should later enable hardware-supervised mode as appropriate
297 list_for_each_entry(clkdm, &clkdm_list, node) {
298 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
299 clkdm_wakeup(clkdm);
300 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
301 clkdm_deny_idle(clkdm);
303 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
304 clkdm_clear_all_wkdeps(clkdm);
306 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
307 clkdm_clear_all_sleepdeps(clkdm);
312 * clkdm_lookup - look up a clockdomain by name, return a pointer
313 * @name: name of clockdomain
315 * Find a registered clockdomain by its name @name. Returns a pointer
316 * to the struct clockdomain if found, or NULL otherwise.
318 struct clockdomain *clkdm_lookup(const char *name)
320 struct clockdomain *clkdm, *temp_clkdm;
322 if (!name)
323 return NULL;
325 clkdm = NULL;
327 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
328 if (!strcmp(name, temp_clkdm->name)) {
329 clkdm = temp_clkdm;
330 break;
334 return clkdm;
338 * clkdm_for_each - call function on each registered clockdomain
339 * @fn: callback function *
341 * Call the supplied function @fn for each registered clockdomain.
342 * The callback function @fn can return anything but 0 to bail
343 * out early from the iterator. The callback function is called with
344 * the clkdm_mutex held, so no clockdomain structure manipulation
345 * functions should be called from the callback, although hardware
346 * clockdomain control functions are fine. Returns the last return
347 * value of the callback function, which should be 0 for success or
348 * anything else to indicate failure; or -EINVAL if the function pointer
349 * is null.
351 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
352 void *user)
354 struct clockdomain *clkdm;
355 int ret = 0;
357 if (!fn)
358 return -EINVAL;
360 list_for_each_entry(clkdm, &clkdm_list, node) {
361 ret = (*fn)(clkdm, user);
362 if (ret)
363 break;
366 return ret;
371 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
372 * @clkdm: struct clockdomain *
374 * Return a pointer to the struct powerdomain that the specified clockdomain
375 * @clkdm exists in, or returns NULL if @clkdm is NULL.
377 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
379 if (!clkdm)
380 return NULL;
382 return clkdm->pwrdm.ptr;
386 /* Hardware clockdomain control */
389 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
390 * @clkdm1: wake this struct clockdomain * up (dependent)
391 * @clkdm2: when this struct clockdomain * wakes up (source)
393 * When the clockdomain represented by @clkdm2 wakes up, wake up
394 * @clkdm1. Implemented in hardware on the OMAP, this feature is
395 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
396 * Returns -EINVAL if presented with invalid clockdomain pointers,
397 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
398 * success.
400 int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
402 struct clkdm_dep *cd;
403 int ret = 0;
405 if (!clkdm1 || !clkdm2)
406 return -EINVAL;
408 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
409 if (IS_ERR(cd))
410 ret = PTR_ERR(cd);
412 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
413 ret = -EINVAL;
415 if (ret) {
416 pr_debug("clockdomain: hardware cannot set/clear wake up of "
417 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
418 return ret;
421 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
422 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
423 "up\n", clkdm1->name, clkdm2->name);
425 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
428 return ret;
432 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
433 * @clkdm1: wake this struct clockdomain * up (dependent)
434 * @clkdm2: when this struct clockdomain * wakes up (source)
436 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
437 * wakes up. Returns -EINVAL if presented with invalid clockdomain
438 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
439 * 0 upon success.
441 int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
443 struct clkdm_dep *cd;
444 int ret = 0;
446 if (!clkdm1 || !clkdm2)
447 return -EINVAL;
449 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
450 if (IS_ERR(cd))
451 ret = PTR_ERR(cd);
453 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
454 ret = -EINVAL;
456 if (ret) {
457 pr_debug("clockdomain: hardware cannot set/clear wake up of "
458 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
459 return ret;
462 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
463 pr_debug("clockdomain: hardware will no longer wake up %s "
464 "after %s wakes up\n", clkdm1->name, clkdm2->name);
466 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
469 return ret;
473 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
474 * @clkdm1: wake this struct clockdomain * up (dependent)
475 * @clkdm2: when this struct clockdomain * wakes up (source)
477 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
478 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
479 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
480 * is incapable.
482 * REVISIT: Currently this function only represents software-controllable
483 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
484 * yet handled here.
486 int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
488 struct clkdm_dep *cd;
489 int ret = 0;
491 if (!clkdm1 || !clkdm2)
492 return -EINVAL;
494 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
495 if (IS_ERR(cd))
496 ret = PTR_ERR(cd);
498 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
499 ret = -EINVAL;
501 if (ret) {
502 pr_debug("clockdomain: hardware cannot set/clear wake up of "
503 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
504 return ret;
507 /* XXX It's faster to return the atomic wkdep_usecount */
508 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
512 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
513 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
515 * Remove all inter-clockdomain wakeup dependencies that could cause
516 * @clkdm to wake. Intended to be used during boot to initialize the
517 * PRCM to a known state, after all clockdomains are put into swsup idle
518 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
519 * 0 upon success.
521 int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
523 if (!clkdm)
524 return -EINVAL;
526 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
527 return -EINVAL;
529 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
533 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
534 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
535 * @clkdm2: when this struct clockdomain * is active (source)
537 * Prevent @clkdm1 from automatically going inactive (and then to
538 * retention or off) if @clkdm2 is active. Returns -EINVAL if
539 * presented with invalid clockdomain pointers or called on a machine
540 * that does not support software-configurable hardware sleep
541 * dependencies, -ENOENT if the specified dependency cannot be set in
542 * hardware, or 0 upon success.
544 int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
546 struct clkdm_dep *cd;
547 int ret = 0;
549 if (!clkdm1 || !clkdm2)
550 return -EINVAL;
552 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
553 if (IS_ERR(cd))
554 ret = PTR_ERR(cd);
556 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
557 ret = -EINVAL;
559 if (ret) {
560 pr_debug("clockdomain: hardware cannot set/clear sleep "
561 "dependency affecting %s from %s\n", clkdm1->name,
562 clkdm2->name);
563 return ret;
566 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
567 pr_debug("clockdomain: will prevent %s from sleeping if %s "
568 "is active\n", clkdm1->name, clkdm2->name);
570 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
573 return ret;
577 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
578 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
579 * @clkdm2: when this struct clockdomain * is active (source)
581 * Allow @clkdm1 to automatically go inactive (and then to retention or
582 * off), independent of the activity state of @clkdm2. Returns -EINVAL
583 * if presented with invalid clockdomain pointers or called on a machine
584 * that does not support software-configurable hardware sleep dependencies,
585 * -ENOENT if the specified dependency cannot be cleared in hardware, or
586 * 0 upon success.
588 int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
590 struct clkdm_dep *cd;
591 int ret = 0;
593 if (!clkdm1 || !clkdm2)
594 return -EINVAL;
596 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
597 if (IS_ERR(cd))
598 ret = PTR_ERR(cd);
600 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
601 ret = -EINVAL;
603 if (ret) {
604 pr_debug("clockdomain: hardware cannot set/clear sleep "
605 "dependency affecting %s from %s\n", clkdm1->name,
606 clkdm2->name);
607 return ret;
610 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
611 pr_debug("clockdomain: will no longer prevent %s from "
612 "sleeping if %s is active\n", clkdm1->name,
613 clkdm2->name);
615 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
618 return ret;
622 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
623 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
624 * @clkdm2: when this struct clockdomain * is active (source)
626 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
627 * not be allowed to automatically go inactive if @clkdm2 is active;
628 * 0 if @clkdm1's automatic power state inactivity transition is independent
629 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
630 * on a machine that does not support software-configurable hardware sleep
631 * dependencies; or -ENOENT if the hardware is incapable.
633 * REVISIT: Currently this function only represents software-controllable
634 * sleep dependencies. Sleep dependencies fixed in hardware are not
635 * yet handled here.
637 int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
639 struct clkdm_dep *cd;
640 int ret = 0;
642 if (!clkdm1 || !clkdm2)
643 return -EINVAL;
645 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
646 if (IS_ERR(cd))
647 ret = PTR_ERR(cd);
649 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
650 ret = -EINVAL;
652 if (ret) {
653 pr_debug("clockdomain: hardware cannot set/clear sleep "
654 "dependency affecting %s from %s\n", clkdm1->name,
655 clkdm2->name);
656 return ret;
659 /* XXX It's faster to return the atomic sleepdep_usecount */
660 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
664 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
665 * @clkdm: struct clockdomain * to remove all sleep dependencies from
667 * Remove all inter-clockdomain sleep dependencies that could prevent
668 * @clkdm from idling. Intended to be used during boot to initialize the
669 * PRCM to a known state, after all clockdomains are put into swsup idle
670 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
671 * 0 upon success.
673 int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
675 if (!clkdm)
676 return -EINVAL;
678 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
679 return -EINVAL;
681 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
685 * clkdm_sleep - force clockdomain sleep transition
686 * @clkdm: struct clockdomain *
688 * Instruct the CM to force a sleep transition on the specified
689 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
690 * clockdomain does not support software-initiated sleep; 0 upon
691 * success.
693 int clkdm_sleep(struct clockdomain *clkdm)
695 int ret;
696 unsigned long flags;
698 if (!clkdm)
699 return -EINVAL;
701 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
702 pr_debug("clockdomain: %s does not support forcing "
703 "sleep via software\n", clkdm->name);
704 return -EINVAL;
707 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
708 return -EINVAL;
710 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
712 spin_lock_irqsave(&clkdm->lock, flags);
713 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
714 ret = arch_clkdm->clkdm_sleep(clkdm);
715 spin_unlock_irqrestore(&clkdm->lock, flags);
716 return ret;
720 * clkdm_wakeup - force clockdomain wakeup transition
721 * @clkdm: struct clockdomain *
723 * Instruct the CM to force a wakeup transition on the specified
724 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
725 * clockdomain does not support software-controlled wakeup; 0 upon
726 * success.
728 int clkdm_wakeup(struct clockdomain *clkdm)
730 int ret;
731 unsigned long flags;
733 if (!clkdm)
734 return -EINVAL;
736 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
737 pr_debug("clockdomain: %s does not support forcing "
738 "wakeup via software\n", clkdm->name);
739 return -EINVAL;
742 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
743 return -EINVAL;
745 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
747 spin_lock_irqsave(&clkdm->lock, flags);
748 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
749 ret = arch_clkdm->clkdm_wakeup(clkdm);
750 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
751 spin_unlock_irqrestore(&clkdm->lock, flags);
752 return ret;
756 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
757 * @clkdm: struct clockdomain *
759 * Allow the hardware to automatically switch the clockdomain @clkdm into
760 * active or idle states, as needed by downstream clocks. If the
761 * clockdomain has any downstream clocks enabled in the clock
762 * framework, wkdep/sleepdep autodependencies are added; this is so
763 * device drivers can read and write to the device. No return value.
765 void clkdm_allow_idle(struct clockdomain *clkdm)
767 unsigned long flags;
769 if (!clkdm)
770 return;
772 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
773 pr_debug("clock: automatic idle transitions cannot be enabled "
774 "on clockdomain %s\n", clkdm->name);
775 return;
778 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
779 return;
781 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
782 clkdm->name);
784 spin_lock_irqsave(&clkdm->lock, flags);
785 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
786 arch_clkdm->clkdm_allow_idle(clkdm);
787 pwrdm_clkdm_state_switch(clkdm);
788 spin_unlock_irqrestore(&clkdm->lock, flags);
792 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
793 * @clkdm: struct clockdomain *
795 * Prevent the hardware from automatically switching the clockdomain
796 * @clkdm into inactive or idle states. If the clockdomain has
797 * downstream clocks enabled in the clock framework, wkdep/sleepdep
798 * autodependencies are removed. No return value.
800 void clkdm_deny_idle(struct clockdomain *clkdm)
802 unsigned long flags;
804 if (!clkdm)
805 return;
807 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
808 pr_debug("clockdomain: automatic idle transitions cannot be "
809 "disabled on %s\n", clkdm->name);
810 return;
813 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
814 return;
816 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
817 clkdm->name);
819 spin_lock_irqsave(&clkdm->lock, flags);
820 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
821 arch_clkdm->clkdm_deny_idle(clkdm);
822 pwrdm_state_switch(clkdm->pwrdm.ptr);
823 spin_unlock_irqrestore(&clkdm->lock, flags);
827 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
828 * @clkdm: struct clockdomain *
830 * Returns true if clockdomain @clkdm currently has
831 * hardware-supervised idle enabled, or false if it does not or if
832 * @clkdm is NULL. It is only valid to call this function after
833 * clkdm_init() has been called. This function does not actually read
834 * bits from the hardware; it instead tests an in-memory flag that is
835 * changed whenever the clockdomain code changes the auto-idle mode.
837 bool clkdm_in_hwsup(struct clockdomain *clkdm)
839 bool ret;
840 unsigned long flags;
842 if (!clkdm)
843 return false;
845 spin_lock_irqsave(&clkdm->lock, flags);
846 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
847 spin_unlock_irqrestore(&clkdm->lock, flags);
849 return ret;
852 /* Clockdomain-to-clock/hwmod framework interface code */
854 static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
856 unsigned long flags;
858 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
859 return -EINVAL;
862 * For arch's with no autodeps, clkcm_clk_enable
863 * should be called for every clock instance or hwmod that is
864 * enabled, so the clkdm can be force woken up.
866 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
867 return 0;
869 spin_lock_irqsave(&clkdm->lock, flags);
870 arch_clkdm->clkdm_clk_enable(clkdm);
871 pwrdm_wait_transition(clkdm->pwrdm.ptr);
872 pwrdm_clkdm_state_switch(clkdm);
873 spin_unlock_irqrestore(&clkdm->lock, flags);
875 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
877 return 0;
880 static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
882 unsigned long flags;
884 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
885 return -EINVAL;
887 if (atomic_read(&clkdm->usecount) == 0) {
888 WARN_ON(1); /* underflow */
889 return -ERANGE;
892 if (atomic_dec_return(&clkdm->usecount) > 0)
893 return 0;
895 spin_lock_irqsave(&clkdm->lock, flags);
896 arch_clkdm->clkdm_clk_disable(clkdm);
897 pwrdm_clkdm_state_switch(clkdm);
898 spin_unlock_irqrestore(&clkdm->lock, flags);
900 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
902 return 0;
906 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
907 * @clkdm: struct clockdomain *
908 * @clk: struct clk * of the enabled downstream clock
910 * Increment the usecount of the clockdomain @clkdm and ensure that it
911 * is awake before @clk is enabled. Intended to be called by
912 * clk_enable() code. If the clockdomain is in software-supervised
913 * idle mode, force the clockdomain to wake. If the clockdomain is in
914 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
915 * ensure that devices in the clockdomain can be read from/written to
916 * by on-chip processors. Returns -EINVAL if passed null pointers;
917 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
919 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
922 * XXX Rewrite this code to maintain a list of enabled
923 * downstream clocks for debugging purposes?
926 if (!clk)
927 return -EINVAL;
929 return _clkdm_clk_hwmod_enable(clkdm);
933 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
934 * @clkdm: struct clockdomain *
935 * @clk: struct clk * of the disabled downstream clock
937 * Decrement the usecount of this clockdomain @clkdm when @clk is
938 * disabled. Intended to be called by clk_disable() code. If the
939 * clockdomain usecount goes to 0, put the clockdomain to sleep
940 * (software-supervised mode) or remove the clkdm autodependencies
941 * (hardware-supervised mode). Returns -EINVAL if passed null
942 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
943 * upon success or if the clockdomain is in hwsup idle mode.
945 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
948 * XXX Rewrite this code to maintain a list of enabled
949 * downstream clocks for debugging purposes?
952 if (!clk)
953 return -EINVAL;
955 return _clkdm_clk_hwmod_disable(clkdm);
959 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
960 * @clkdm: struct clockdomain *
961 * @oh: struct omap_hwmod * of the enabled downstream hwmod
963 * Increment the usecount of the clockdomain @clkdm and ensure that it
964 * is awake before @oh is enabled. Intended to be called by
965 * module_enable() code.
966 * If the clockdomain is in software-supervised idle mode, force the
967 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
968 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
969 * clockdomain can be read from/written to by on-chip processors.
970 * Returns -EINVAL if passed null pointers;
971 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
973 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
975 /* The clkdm attribute does not exist yet prior OMAP4 */
976 if (cpu_is_omap24xx() || cpu_is_omap34xx())
977 return 0;
980 * XXX Rewrite this code to maintain a list of enabled
981 * downstream hwmods for debugging purposes?
984 if (!oh)
985 return -EINVAL;
987 return _clkdm_clk_hwmod_enable(clkdm);
991 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
992 * @clkdm: struct clockdomain *
993 * @oh: struct omap_hwmod * of the disabled downstream hwmod
995 * Decrement the usecount of this clockdomain @clkdm when @oh is
996 * disabled. Intended to be called by module_disable() code.
997 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
998 * (software-supervised mode) or remove the clkdm autodependencies
999 * (hardware-supervised mode).
1000 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1001 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1002 * idle mode.
1004 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1006 /* The clkdm attribute does not exist yet prior OMAP4 */
1007 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1008 return 0;
1011 * XXX Rewrite this code to maintain a list of enabled
1012 * downstream hwmods for debugging purposes?
1015 if (!oh)
1016 return -EINVAL;
1018 return _clkdm_clk_hwmod_disable(clkdm);