Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / arm / mach-omap2 / clockdomain.c
blobb79b1ca9aee9edbd96099495e0c6165089c0c01e
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/string.h>
21 #include <linux/delay.h>
22 #include <linux/clk.h>
23 #include <linux/limits.h>
24 #include <linux/err.h>
25 #include <linux/clk-provider.h>
27 #include <linux/io.h>
29 #include <linux/bitops.h>
31 #include "soc.h"
32 #include "clock.h"
33 #include "clockdomain.h"
35 /* clkdm_list contains all registered struct clockdomains */
36 static LIST_HEAD(clkdm_list);
38 /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
39 static struct clkdm_autodep *autodeps;
41 static struct clkdm_ops *arch_clkdm;
43 /* Private functions */
45 static struct clockdomain *_clkdm_lookup(const char *name)
47 struct clockdomain *clkdm, *temp_clkdm;
49 if (!name)
50 return NULL;
52 clkdm = NULL;
54 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
55 if (!strcmp(name, temp_clkdm->name)) {
56 clkdm = temp_clkdm;
57 break;
61 return clkdm;
64 /**
65 * _clkdm_register - register a clockdomain
66 * @clkdm: struct clockdomain * to register
68 * Adds a clockdomain to the internal clockdomain list.
69 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
70 * already registered by the provided name, or 0 upon success.
72 static int _clkdm_register(struct clockdomain *clkdm)
74 struct powerdomain *pwrdm;
76 if (!clkdm || !clkdm->name)
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 pr_debug("clockdomain: registered %s\n", clkdm->name);
97 return 0;
100 /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
101 static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
102 struct clkdm_dep *deps)
104 struct clkdm_dep *cd;
106 if (!clkdm || !deps)
107 return ERR_PTR(-EINVAL);
109 for (cd = deps; cd->clkdm_name; cd++) {
110 if (!cd->clkdm && cd->clkdm_name)
111 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
113 if (cd->clkdm == clkdm)
114 break;
117 if (!cd->clkdm_name)
118 return ERR_PTR(-ENOENT);
120 return cd;
124 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
125 * @autodep: struct clkdm_autodep * to resolve
127 * Resolve autodep clockdomain names to clockdomain pointers via
128 * clkdm_lookup() and store the pointers in the autodep structure. An
129 * "autodep" is a clockdomain sleep/wakeup dependency that is
130 * automatically added and removed whenever clocks in the associated
131 * clockdomain are enabled or disabled (respectively) when the
132 * clockdomain is in hardware-supervised mode. Meant to be called
133 * once at clockdomain layer initialization, since these should remain
134 * fixed for a particular architecture. No return value.
136 * XXX autodeps are deprecated and should be removed at the earliest
137 * opportunity
139 static void _autodep_lookup(struct clkdm_autodep *autodep)
141 struct clockdomain *clkdm;
143 if (!autodep)
144 return;
146 clkdm = clkdm_lookup(autodep->clkdm.name);
147 if (!clkdm) {
148 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
149 autodep->clkdm.name);
150 clkdm = ERR_PTR(-ENOENT);
152 autodep->clkdm.ptr = clkdm;
156 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
157 * @clkdm: clockdomain that we are resolving dependencies for
158 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
160 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
161 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
162 * No return value.
164 static void _resolve_clkdm_deps(struct clockdomain *clkdm,
165 struct clkdm_dep *clkdm_deps)
167 struct clkdm_dep *cd;
169 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
170 if (cd->clkdm)
171 continue;
172 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
174 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
175 clkdm->name, cd->clkdm_name);
180 * _clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1 (lockless)
181 * @clkdm1: wake this struct clockdomain * up (dependent)
182 * @clkdm2: when this struct clockdomain * wakes up (source)
184 * When the clockdomain represented by @clkdm2 wakes up, wake up
185 * @clkdm1. Implemented in hardware on the OMAP, this feature is
186 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
187 * Returns -EINVAL if presented with invalid clockdomain pointers,
188 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
189 * success.
191 static int _clkdm_add_wkdep(struct clockdomain *clkdm1,
192 struct clockdomain *clkdm2)
194 struct clkdm_dep *cd;
195 int ret = 0;
197 if (!clkdm1 || !clkdm2)
198 return -EINVAL;
200 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
201 if (IS_ERR(cd))
202 ret = PTR_ERR(cd);
204 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
205 ret = -EINVAL;
207 if (ret) {
208 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
209 clkdm1->name, clkdm2->name);
210 return ret;
213 cd->wkdep_usecount++;
214 if (cd->wkdep_usecount == 1) {
215 pr_debug("clockdomain: hardware will wake up %s when %s wakes up\n",
216 clkdm1->name, clkdm2->name);
218 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
221 return ret;
225 * _clkdm_del_wkdep - remove a wakeup dep from clkdm2 to clkdm1 (lockless)
226 * @clkdm1: wake this struct clockdomain * up (dependent)
227 * @clkdm2: when this struct clockdomain * wakes up (source)
229 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
230 * wakes up. Returns -EINVAL if presented with invalid clockdomain
231 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
232 * 0 upon success.
234 static int _clkdm_del_wkdep(struct clockdomain *clkdm1,
235 struct clockdomain *clkdm2)
237 struct clkdm_dep *cd;
238 int ret = 0;
240 if (!clkdm1 || !clkdm2)
241 return -EINVAL;
243 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
244 if (IS_ERR(cd))
245 ret = PTR_ERR(cd);
247 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
248 ret = -EINVAL;
250 if (ret) {
251 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
252 clkdm1->name, clkdm2->name);
253 return ret;
256 cd->wkdep_usecount--;
257 if (cd->wkdep_usecount == 0) {
258 pr_debug("clockdomain: hardware will no longer wake up %s after %s wakes up\n",
259 clkdm1->name, clkdm2->name);
261 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
264 return ret;
268 * _clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1 (lockless)
269 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
270 * @clkdm2: when this struct clockdomain * is active (source)
272 * Prevent @clkdm1 from automatically going inactive (and then to
273 * retention or off) if @clkdm2 is active. Returns -EINVAL if
274 * presented with invalid clockdomain pointers or called on a machine
275 * that does not support software-configurable hardware sleep
276 * dependencies, -ENOENT if the specified dependency cannot be set in
277 * hardware, or 0 upon success.
279 static int _clkdm_add_sleepdep(struct clockdomain *clkdm1,
280 struct clockdomain *clkdm2)
282 struct clkdm_dep *cd;
283 int ret = 0;
285 if (!clkdm1 || !clkdm2)
286 return -EINVAL;
288 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
289 if (IS_ERR(cd))
290 ret = PTR_ERR(cd);
292 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
293 ret = -EINVAL;
295 if (ret) {
296 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
297 clkdm1->name, clkdm2->name);
298 return ret;
301 cd->sleepdep_usecount++;
302 if (cd->sleepdep_usecount == 1) {
303 pr_debug("clockdomain: will prevent %s from sleeping if %s is active\n",
304 clkdm1->name, clkdm2->name);
306 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
309 return ret;
313 * _clkdm_del_sleepdep - remove a sleep dep from clkdm2 to clkdm1 (lockless)
314 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
315 * @clkdm2: when this struct clockdomain * is active (source)
317 * Allow @clkdm1 to automatically go inactive (and then to retention or
318 * off), independent of the activity state of @clkdm2. Returns -EINVAL
319 * if presented with invalid clockdomain pointers or called on a machine
320 * that does not support software-configurable hardware sleep dependencies,
321 * -ENOENT if the specified dependency cannot be cleared in hardware, or
322 * 0 upon success.
324 static int _clkdm_del_sleepdep(struct clockdomain *clkdm1,
325 struct clockdomain *clkdm2)
327 struct clkdm_dep *cd;
328 int ret = 0;
330 if (!clkdm1 || !clkdm2)
331 return -EINVAL;
333 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
334 if (IS_ERR(cd))
335 ret = PTR_ERR(cd);
337 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
338 ret = -EINVAL;
340 if (ret) {
341 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
342 clkdm1->name, clkdm2->name);
343 return ret;
346 cd->sleepdep_usecount--;
347 if (cd->sleepdep_usecount == 0) {
348 pr_debug("clockdomain: will no longer prevent %s from sleeping if %s is active\n",
349 clkdm1->name, clkdm2->name);
351 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
354 return ret;
357 /* Public functions */
360 * clkdm_register_platform_funcs - register clockdomain implementation fns
361 * @co: func pointers for arch specific implementations
363 * Register the list of function pointers used to implement the
364 * clockdomain functions on different OMAP SoCs. Should be called
365 * before any other clkdm_register*() function. Returns -EINVAL if
366 * @co is null, -EEXIST if platform functions have already been
367 * registered, or 0 upon success.
369 int clkdm_register_platform_funcs(struct clkdm_ops *co)
371 if (!co)
372 return -EINVAL;
374 if (arch_clkdm)
375 return -EEXIST;
377 arch_clkdm = co;
379 return 0;
383 * clkdm_register_clkdms - register SoC clockdomains
384 * @cs: pointer to an array of struct clockdomain to register
386 * Register the clockdomains available on a particular OMAP SoC. Must
387 * be called after clkdm_register_platform_funcs(). May be called
388 * multiple times. Returns -EACCES if called before
389 * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
390 * null; or 0 upon success.
392 int clkdm_register_clkdms(struct clockdomain **cs)
394 struct clockdomain **c = NULL;
396 if (!arch_clkdm)
397 return -EACCES;
399 if (!cs)
400 return -EINVAL;
402 for (c = cs; *c; c++)
403 _clkdm_register(*c);
405 return 0;
409 * clkdm_register_autodeps - register autodeps (if required)
410 * @ia: pointer to a static array of struct clkdm_autodep to register
412 * Register clockdomain "automatic dependencies." These are
413 * clockdomain wakeup and sleep dependencies that are automatically
414 * added whenever the first clock inside a clockdomain is enabled, and
415 * removed whenever the last clock inside a clockdomain is disabled.
416 * These are currently only used on OMAP3 devices, and are deprecated,
417 * since they waste energy. However, until the OMAP2/3 IP block
418 * enable/disable sequence can be converted to match the OMAP4
419 * sequence, they are needed.
421 * Must be called only after all of the SoC clockdomains are
422 * registered, since the function will resolve autodep clockdomain
423 * names into clockdomain pointers.
425 * The struct clkdm_autodep @ia array must be static, as this function
426 * does not copy the array elements.
428 * Returns -EACCES if called before any clockdomains have been
429 * registered, -EINVAL if called with a null @ia argument, -EEXIST if
430 * autodeps have already been registered, or 0 upon success.
432 int clkdm_register_autodeps(struct clkdm_autodep *ia)
434 struct clkdm_autodep *a = NULL;
436 if (list_empty(&clkdm_list))
437 return -EACCES;
439 if (!ia)
440 return -EINVAL;
442 if (autodeps)
443 return -EEXIST;
445 autodeps = ia;
446 for (a = autodeps; a->clkdm.ptr; a++)
447 _autodep_lookup(a);
449 return 0;
453 * clkdm_complete_init - set up the clockdomain layer
455 * Put all clockdomains into software-supervised mode; PM code should
456 * later enable hardware-supervised mode as appropriate. Must be
457 * called after clkdm_register_clkdms(). Returns -EACCES if called
458 * before clkdm_register_clkdms(), or 0 upon success.
460 int clkdm_complete_init(void)
462 struct clockdomain *clkdm;
464 if (list_empty(&clkdm_list))
465 return -EACCES;
467 list_for_each_entry(clkdm, &clkdm_list, node) {
468 clkdm_deny_idle(clkdm);
470 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
471 clkdm_clear_all_wkdeps(clkdm);
473 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
474 clkdm_clear_all_sleepdeps(clkdm);
477 return 0;
481 * clkdm_lookup - look up a clockdomain by name, return a pointer
482 * @name: name of clockdomain
484 * Find a registered clockdomain by its name @name. Returns a pointer
485 * to the struct clockdomain if found, or NULL otherwise.
487 struct clockdomain *clkdm_lookup(const char *name)
489 struct clockdomain *clkdm, *temp_clkdm;
491 if (!name)
492 return NULL;
494 clkdm = NULL;
496 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
497 if (!strcmp(name, temp_clkdm->name)) {
498 clkdm = temp_clkdm;
499 break;
503 return clkdm;
507 * clkdm_for_each - call function on each registered clockdomain
508 * @fn: callback function *
510 * Call the supplied function @fn for each registered clockdomain.
511 * The callback function @fn can return anything but 0 to bail
512 * out early from the iterator. The callback function is called with
513 * the clkdm_mutex held, so no clockdomain structure manipulation
514 * functions should be called from the callback, although hardware
515 * clockdomain control functions are fine. Returns the last return
516 * value of the callback function, which should be 0 for success or
517 * anything else to indicate failure; or -EINVAL if the function pointer
518 * is null.
520 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
521 void *user)
523 struct clockdomain *clkdm;
524 int ret = 0;
526 if (!fn)
527 return -EINVAL;
529 list_for_each_entry(clkdm, &clkdm_list, node) {
530 ret = (*fn)(clkdm, user);
531 if (ret)
532 break;
535 return ret;
540 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
541 * @clkdm: struct clockdomain *
543 * Return a pointer to the struct powerdomain that the specified clockdomain
544 * @clkdm exists in, or returns NULL if @clkdm is NULL.
546 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
548 if (!clkdm)
549 return NULL;
551 return clkdm->pwrdm.ptr;
555 /* Hardware clockdomain control */
558 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
559 * @clkdm1: wake this struct clockdomain * up (dependent)
560 * @clkdm2: when this struct clockdomain * wakes up (source)
562 * When the clockdomain represented by @clkdm2 wakes up, wake up
563 * @clkdm1. Implemented in hardware on the OMAP, this feature is
564 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
565 * Returns -EINVAL if presented with invalid clockdomain pointers,
566 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
567 * success.
569 int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
571 struct clkdm_dep *cd;
572 int ret;
574 if (!clkdm1 || !clkdm2)
575 return -EINVAL;
577 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
578 if (IS_ERR(cd))
579 return PTR_ERR(cd);
581 pwrdm_lock(cd->clkdm->pwrdm.ptr);
582 ret = _clkdm_add_wkdep(clkdm1, clkdm2);
583 pwrdm_unlock(cd->clkdm->pwrdm.ptr);
585 return ret;
589 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
590 * @clkdm1: wake this struct clockdomain * up (dependent)
591 * @clkdm2: when this struct clockdomain * wakes up (source)
593 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
594 * wakes up. Returns -EINVAL if presented with invalid clockdomain
595 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
596 * 0 upon success.
598 int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
600 struct clkdm_dep *cd;
601 int ret;
603 if (!clkdm1 || !clkdm2)
604 return -EINVAL;
606 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
607 if (IS_ERR(cd))
608 return PTR_ERR(cd);
610 pwrdm_lock(cd->clkdm->pwrdm.ptr);
611 ret = _clkdm_del_wkdep(clkdm1, clkdm2);
612 pwrdm_unlock(cd->clkdm->pwrdm.ptr);
614 return ret;
618 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
619 * @clkdm1: wake this struct clockdomain * up (dependent)
620 * @clkdm2: when this struct clockdomain * wakes up (source)
622 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
623 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
624 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
625 * is incapable.
627 * REVISIT: Currently this function only represents software-controllable
628 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
629 * yet handled here.
631 int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
633 struct clkdm_dep *cd;
634 int ret = 0;
636 if (!clkdm1 || !clkdm2)
637 return -EINVAL;
639 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
640 if (IS_ERR(cd))
641 ret = PTR_ERR(cd);
643 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
644 ret = -EINVAL;
646 if (ret) {
647 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
648 clkdm1->name, clkdm2->name);
649 return ret;
652 /* XXX It's faster to return the wkdep_usecount */
653 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
657 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
658 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
660 * Remove all inter-clockdomain wakeup dependencies that could cause
661 * @clkdm to wake. Intended to be used during boot to initialize the
662 * PRCM to a known state, after all clockdomains are put into swsup idle
663 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
664 * 0 upon success.
666 int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
668 if (!clkdm)
669 return -EINVAL;
671 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
672 return -EINVAL;
674 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
678 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
679 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
680 * @clkdm2: when this struct clockdomain * is active (source)
682 * Prevent @clkdm1 from automatically going inactive (and then to
683 * retention or off) if @clkdm2 is active. Returns -EINVAL if
684 * presented with invalid clockdomain pointers or called on a machine
685 * that does not support software-configurable hardware sleep
686 * dependencies, -ENOENT if the specified dependency cannot be set in
687 * hardware, or 0 upon success.
689 int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
691 struct clkdm_dep *cd;
692 int ret;
694 if (!clkdm1 || !clkdm2)
695 return -EINVAL;
697 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
698 if (IS_ERR(cd))
699 return PTR_ERR(cd);
701 pwrdm_lock(cd->clkdm->pwrdm.ptr);
702 ret = _clkdm_add_sleepdep(clkdm1, clkdm2);
703 pwrdm_unlock(cd->clkdm->pwrdm.ptr);
705 return ret;
709 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
710 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
711 * @clkdm2: when this struct clockdomain * is active (source)
713 * Allow @clkdm1 to automatically go inactive (and then to retention or
714 * off), independent of the activity state of @clkdm2. Returns -EINVAL
715 * if presented with invalid clockdomain pointers or called on a machine
716 * that does not support software-configurable hardware sleep dependencies,
717 * -ENOENT if the specified dependency cannot be cleared in hardware, or
718 * 0 upon success.
720 int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
722 struct clkdm_dep *cd;
723 int ret;
725 if (!clkdm1 || !clkdm2)
726 return -EINVAL;
728 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
729 if (IS_ERR(cd))
730 return PTR_ERR(cd);
732 pwrdm_lock(cd->clkdm->pwrdm.ptr);
733 ret = _clkdm_del_sleepdep(clkdm1, clkdm2);
734 pwrdm_unlock(cd->clkdm->pwrdm.ptr);
736 return ret;
740 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
741 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
742 * @clkdm2: when this struct clockdomain * is active (source)
744 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
745 * not be allowed to automatically go inactive if @clkdm2 is active;
746 * 0 if @clkdm1's automatic power state inactivity transition is independent
747 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
748 * on a machine that does not support software-configurable hardware sleep
749 * dependencies; or -ENOENT if the hardware is incapable.
751 * REVISIT: Currently this function only represents software-controllable
752 * sleep dependencies. Sleep dependencies fixed in hardware are not
753 * yet handled here.
755 int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
757 struct clkdm_dep *cd;
758 int ret = 0;
760 if (!clkdm1 || !clkdm2)
761 return -EINVAL;
763 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
764 if (IS_ERR(cd))
765 ret = PTR_ERR(cd);
767 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
768 ret = -EINVAL;
770 if (ret) {
771 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
772 clkdm1->name, clkdm2->name);
773 return ret;
776 /* XXX It's faster to return the sleepdep_usecount */
777 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
781 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
782 * @clkdm: struct clockdomain * to remove all sleep dependencies from
784 * Remove all inter-clockdomain sleep dependencies that could prevent
785 * @clkdm from idling. Intended to be used during boot to initialize the
786 * PRCM to a known state, after all clockdomains are put into swsup idle
787 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
788 * 0 upon success.
790 int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
792 if (!clkdm)
793 return -EINVAL;
795 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
796 return -EINVAL;
798 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
802 * clkdm_sleep_nolock - force clockdomain sleep transition (lockless)
803 * @clkdm: struct clockdomain *
805 * Instruct the CM to force a sleep transition on the specified
806 * clockdomain @clkdm. Only for use by the powerdomain code. Returns
807 * -EINVAL if @clkdm is NULL or if clockdomain does not support
808 * software-initiated sleep; 0 upon success.
810 int clkdm_sleep_nolock(struct clockdomain *clkdm)
812 int ret;
814 if (!clkdm)
815 return -EINVAL;
817 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
818 pr_debug("clockdomain: %s does not support forcing sleep via software\n",
819 clkdm->name);
820 return -EINVAL;
823 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
824 return -EINVAL;
826 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
828 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
829 ret = arch_clkdm->clkdm_sleep(clkdm);
830 ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
832 return ret;
836 * clkdm_sleep - force clockdomain sleep transition
837 * @clkdm: struct clockdomain *
839 * Instruct the CM to force a sleep transition on the specified
840 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
841 * clockdomain does not support software-initiated sleep; 0 upon
842 * success.
844 int clkdm_sleep(struct clockdomain *clkdm)
846 int ret;
848 pwrdm_lock(clkdm->pwrdm.ptr);
849 ret = clkdm_sleep_nolock(clkdm);
850 pwrdm_unlock(clkdm->pwrdm.ptr);
852 return ret;
856 * clkdm_wakeup_nolock - force clockdomain wakeup transition (lockless)
857 * @clkdm: struct clockdomain *
859 * Instruct the CM to force a wakeup transition on the specified
860 * clockdomain @clkdm. Only for use by the powerdomain code. Returns
861 * -EINVAL if @clkdm is NULL or if the clockdomain does not support
862 * software-controlled wakeup; 0 upon success.
864 int clkdm_wakeup_nolock(struct clockdomain *clkdm)
866 int ret;
868 if (!clkdm)
869 return -EINVAL;
871 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
872 pr_debug("clockdomain: %s does not support forcing wakeup via software\n",
873 clkdm->name);
874 return -EINVAL;
877 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
878 return -EINVAL;
880 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
882 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
883 ret = arch_clkdm->clkdm_wakeup(clkdm);
884 ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
886 return ret;
890 * clkdm_wakeup - force clockdomain wakeup transition
891 * @clkdm: struct clockdomain *
893 * Instruct the CM to force a wakeup transition on the specified
894 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
895 * clockdomain does not support software-controlled wakeup; 0 upon
896 * success.
898 int clkdm_wakeup(struct clockdomain *clkdm)
900 int ret;
902 pwrdm_lock(clkdm->pwrdm.ptr);
903 ret = clkdm_wakeup_nolock(clkdm);
904 pwrdm_unlock(clkdm->pwrdm.ptr);
906 return ret;
910 * clkdm_allow_idle_nolock - enable hwsup idle transitions for clkdm
911 * @clkdm: struct clockdomain *
913 * Allow the hardware to automatically switch the clockdomain @clkdm
914 * into active or idle states, as needed by downstream clocks. If the
915 * clockdomain has any downstream clocks enabled in the clock
916 * framework, wkdep/sleepdep autodependencies are added; this is so
917 * device drivers can read and write to the device. Only for use by
918 * the powerdomain code. No return value.
920 void clkdm_allow_idle_nolock(struct clockdomain *clkdm)
922 if (!clkdm)
923 return;
925 if (!WARN_ON(!clkdm->forcewake_count))
926 clkdm->forcewake_count--;
928 if (clkdm->forcewake_count)
929 return;
931 if (!clkdm->usecount && (clkdm->flags & CLKDM_CAN_FORCE_SLEEP))
932 clkdm_sleep_nolock(clkdm);
934 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO))
935 return;
937 if (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING)
938 return;
940 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
941 return;
943 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
944 clkdm->name);
946 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
947 arch_clkdm->clkdm_allow_idle(clkdm);
948 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
952 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
953 * @clkdm: struct clockdomain *
955 * Allow the hardware to automatically switch the clockdomain @clkdm into
956 * active or idle states, as needed by downstream clocks. If the
957 * clockdomain has any downstream clocks enabled in the clock
958 * framework, wkdep/sleepdep autodependencies are added; this is so
959 * device drivers can read and write to the device. No return value.
961 void clkdm_allow_idle(struct clockdomain *clkdm)
963 pwrdm_lock(clkdm->pwrdm.ptr);
964 clkdm_allow_idle_nolock(clkdm);
965 pwrdm_unlock(clkdm->pwrdm.ptr);
969 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
970 * @clkdm: struct clockdomain *
972 * Prevent the hardware from automatically switching the clockdomain
973 * @clkdm into inactive or idle states. If the clockdomain has
974 * downstream clocks enabled in the clock framework, wkdep/sleepdep
975 * autodependencies are removed. Only for use by the powerdomain
976 * code. No return value.
978 void clkdm_deny_idle_nolock(struct clockdomain *clkdm)
980 if (!clkdm)
981 return;
983 if (clkdm->forcewake_count++)
984 return;
986 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
987 clkdm_wakeup_nolock(clkdm);
989 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO))
990 return;
992 if (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING)
993 return;
995 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
996 return;
998 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
999 clkdm->name);
1001 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
1002 arch_clkdm->clkdm_deny_idle(clkdm);
1003 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1007 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
1008 * @clkdm: struct clockdomain *
1010 * Prevent the hardware from automatically switching the clockdomain
1011 * @clkdm into inactive or idle states. If the clockdomain has
1012 * downstream clocks enabled in the clock framework, wkdep/sleepdep
1013 * autodependencies are removed. No return value.
1015 void clkdm_deny_idle(struct clockdomain *clkdm)
1017 pwrdm_lock(clkdm->pwrdm.ptr);
1018 clkdm_deny_idle_nolock(clkdm);
1019 pwrdm_unlock(clkdm->pwrdm.ptr);
1023 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
1024 * @clkdm: struct clockdomain *
1026 * Returns true if clockdomain @clkdm currently has
1027 * hardware-supervised idle enabled, or false if it does not or if
1028 * @clkdm is NULL. It is only valid to call this function after
1029 * clkdm_init() has been called. This function does not actually read
1030 * bits from the hardware; it instead tests an in-memory flag that is
1031 * changed whenever the clockdomain code changes the auto-idle mode.
1033 bool clkdm_in_hwsup(struct clockdomain *clkdm)
1035 bool ret;
1037 if (!clkdm)
1038 return false;
1040 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
1042 return ret;
1046 * clkdm_missing_idle_reporting - can @clkdm enter autoidle even if in use?
1047 * @clkdm: struct clockdomain *
1049 * Returns true if clockdomain @clkdm has the
1050 * CLKDM_MISSING_IDLE_REPORTING flag set, or false if not or @clkdm is
1051 * null. More information is available in the documentation for the
1052 * CLKDM_MISSING_IDLE_REPORTING macro.
1054 bool clkdm_missing_idle_reporting(struct clockdomain *clkdm)
1056 if (!clkdm)
1057 return false;
1059 return (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING) ? true : false;
1062 /* Public autodep handling functions (deprecated) */
1065 * clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
1066 * @clkdm: struct clockdomain *
1068 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
1069 * in hardware-supervised mode. Meant to be called from clock framework
1070 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
1072 * XXX autodeps are deprecated and should be removed at the earliest
1073 * opportunity
1075 void clkdm_add_autodeps(struct clockdomain *clkdm)
1077 struct clkdm_autodep *autodep;
1079 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
1080 return;
1082 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
1083 if (IS_ERR(autodep->clkdm.ptr))
1084 continue;
1086 pr_debug("clockdomain: %s: adding %s sleepdep/wkdep\n",
1087 clkdm->name, autodep->clkdm.ptr->name);
1089 _clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
1090 _clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
1095 * clkdm_del_autodeps - remove auto sleepdeps/wkdeps from clkdm
1096 * @clkdm: struct clockdomain *
1098 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
1099 * in hardware-supervised mode. Meant to be called from clock framework
1100 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
1102 * XXX autodeps are deprecated and should be removed at the earliest
1103 * opportunity
1105 void clkdm_del_autodeps(struct clockdomain *clkdm)
1107 struct clkdm_autodep *autodep;
1109 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
1110 return;
1112 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
1113 if (IS_ERR(autodep->clkdm.ptr))
1114 continue;
1116 pr_debug("clockdomain: %s: removing %s sleepdep/wkdep\n",
1117 clkdm->name, autodep->clkdm.ptr->name);
1119 _clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
1120 _clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
1124 /* Clockdomain-to-clock/hwmod framework interface code */
1126 static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
1128 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
1129 return -EINVAL;
1131 pwrdm_lock(clkdm->pwrdm.ptr);
1134 * For arch's with no autodeps, clkcm_clk_enable
1135 * should be called for every clock instance or hwmod that is
1136 * enabled, so the clkdm can be force woken up.
1138 clkdm->usecount++;
1139 if (clkdm->usecount > 1 && autodeps) {
1140 pwrdm_unlock(clkdm->pwrdm.ptr);
1141 return 0;
1144 arch_clkdm->clkdm_clk_enable(clkdm);
1145 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1146 pwrdm_unlock(clkdm->pwrdm.ptr);
1148 pr_debug("clockdomain: %s: enabled\n", clkdm->name);
1150 return 0;
1154 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
1155 * @clkdm: struct clockdomain *
1156 * @clk: struct clk * of the enabled downstream clock
1158 * Increment the usecount of the clockdomain @clkdm and ensure that it
1159 * is awake before @clk is enabled. Intended to be called by
1160 * clk_enable() code. If the clockdomain is in software-supervised
1161 * idle mode, force the clockdomain to wake. If the clockdomain is in
1162 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
1163 * ensure that devices in the clockdomain can be read from/written to
1164 * by on-chip processors. Returns -EINVAL if passed null pointers;
1165 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1167 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
1170 * XXX Rewrite this code to maintain a list of enabled
1171 * downstream clocks for debugging purposes?
1174 if (!clk)
1175 return -EINVAL;
1177 return _clkdm_clk_hwmod_enable(clkdm);
1181 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
1182 * @clkdm: struct clockdomain *
1183 * @clk: struct clk * of the disabled downstream clock
1185 * Decrement the usecount of this clockdomain @clkdm when @clk is
1186 * disabled. Intended to be called by clk_disable() code. If the
1187 * clockdomain usecount goes to 0, put the clockdomain to sleep
1188 * (software-supervised mode) or remove the clkdm autodependencies
1189 * (hardware-supervised mode). Returns -EINVAL if passed null
1190 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1191 * upon success or if the clockdomain is in hwsup idle mode.
1193 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
1195 if (!clkdm || !clk || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
1196 return -EINVAL;
1198 pwrdm_lock(clkdm->pwrdm.ptr);
1200 /* corner case: disabling unused clocks */
1201 if ((__clk_get_enable_count(clk) == 0) && clkdm->usecount == 0)
1202 goto ccd_exit;
1204 if (clkdm->usecount == 0) {
1205 pwrdm_unlock(clkdm->pwrdm.ptr);
1206 WARN_ON(1); /* underflow */
1207 return -ERANGE;
1210 clkdm->usecount--;
1211 if (clkdm->usecount > 0) {
1212 pwrdm_unlock(clkdm->pwrdm.ptr);
1213 return 0;
1216 arch_clkdm->clkdm_clk_disable(clkdm);
1217 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1219 pr_debug("clockdomain: %s: disabled\n", clkdm->name);
1221 ccd_exit:
1222 pwrdm_unlock(clkdm->pwrdm.ptr);
1224 return 0;
1228 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1229 * @clkdm: struct clockdomain *
1230 * @oh: struct omap_hwmod * of the enabled downstream hwmod
1232 * Increment the usecount of the clockdomain @clkdm and ensure that it
1233 * is awake before @oh is enabled. Intended to be called by
1234 * module_enable() code.
1235 * If the clockdomain is in software-supervised idle mode, force the
1236 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
1237 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1238 * clockdomain can be read from/written to by on-chip processors.
1239 * Returns -EINVAL if passed null pointers;
1240 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1242 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1244 /* The clkdm attribute does not exist yet prior OMAP4 */
1245 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1246 return 0;
1249 * XXX Rewrite this code to maintain a list of enabled
1250 * downstream hwmods for debugging purposes?
1253 if (!oh)
1254 return -EINVAL;
1256 return _clkdm_clk_hwmod_enable(clkdm);
1260 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1261 * @clkdm: struct clockdomain *
1262 * @oh: struct omap_hwmod * of the disabled downstream hwmod
1264 * Decrement the usecount of this clockdomain @clkdm when @oh is
1265 * disabled. Intended to be called by module_disable() code.
1266 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1267 * (software-supervised mode) or remove the clkdm autodependencies
1268 * (hardware-supervised mode).
1269 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1270 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1271 * idle mode.
1273 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1275 /* The clkdm attribute does not exist yet prior OMAP4 */
1276 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1277 return 0;
1280 * XXX Rewrite this code to maintain a list of enabled
1281 * downstream hwmods for debugging purposes?
1284 if (!clkdm || !oh || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
1285 return -EINVAL;
1287 pwrdm_lock(clkdm->pwrdm.ptr);
1289 if (clkdm->usecount == 0) {
1290 pwrdm_unlock(clkdm->pwrdm.ptr);
1291 WARN_ON(1); /* underflow */
1292 return -ERANGE;
1295 clkdm->usecount--;
1296 if (clkdm->usecount > 0) {
1297 pwrdm_unlock(clkdm->pwrdm.ptr);
1298 return 0;
1301 arch_clkdm->clkdm_clk_disable(clkdm);
1302 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1303 pwrdm_unlock(clkdm->pwrdm.ptr);
1305 pr_debug("clockdomain: %s: disabled\n", clkdm->name);
1307 return 0;