OMAP: clockdomain: split clkdm_init()
[pandora-kernel.git] / arch / arm / mach-omap2 / clockdomain.c
1 /*
2  * OMAP2/3/4 clockdomain framework functions
3  *
4  * Copyright (C) 2008-2011 Texas Instruments, Inc.
5  * Copyright (C) 2008-2011 Nokia Corporation
6  *
7  * Written by Paul Walmsley and Jouni Högander
8  * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
9  *
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.
13  */
14 #undef DEBUG
15
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>
24
25 #include <linux/io.h>
26
27 #include <linux/bitops.h>
28
29 #include <plat/clock.h>
30 #include "clockdomain.h"
31
32 /* clkdm_list contains all registered struct clockdomains */
33 static LIST_HEAD(clkdm_list);
34
35 /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
36 static struct clkdm_autodep *autodeps;
37
38 static struct clkdm_ops *arch_clkdm;
39
40 /* Private functions */
41
42 static struct clockdomain *_clkdm_lookup(const char *name)
43 {
44         struct clockdomain *clkdm, *temp_clkdm;
45
46         if (!name)
47                 return NULL;
48
49         clkdm = NULL;
50
51         list_for_each_entry(temp_clkdm, &clkdm_list, node) {
52                 if (!strcmp(name, temp_clkdm->name)) {
53                         clkdm = temp_clkdm;
54                         break;
55                 }
56         }
57
58         return clkdm;
59 }
60
61 /**
62  * _clkdm_register - register a clockdomain
63  * @clkdm: struct clockdomain * to register
64  *
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.
68  */
69 static int _clkdm_register(struct clockdomain *clkdm)
70 {
71         struct powerdomain *pwrdm;
72
73         if (!clkdm || !clkdm->name)
74                 return -EINVAL;
75
76         if (!omap_chip_is(clkdm->omap_chip))
77                 return -EINVAL;
78
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;
84         }
85         clkdm->pwrdm.ptr = pwrdm;
86
87         /* Verify that the clockdomain is not already registered */
88         if (_clkdm_lookup(clkdm->name))
89                 return -EEXIST;
90
91         list_add(&clkdm->node, &clkdm_list);
92
93         pwrdm_add_clkdm(pwrdm, clkdm);
94
95         spin_lock_init(&clkdm->lock);
96
97         pr_debug("clockdomain: registered %s\n", clkdm->name);
98
99         return 0;
100 }
101
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)
105 {
106         struct clkdm_dep *cd;
107
108         if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
109                 return ERR_PTR(-EINVAL);
110
111         for (cd = deps; cd->clkdm_name; cd++) {
112                 if (!omap_chip_is(cd->omap_chip))
113                         continue;
114
115                 if (!cd->clkdm && cd->clkdm_name)
116                         cd->clkdm = _clkdm_lookup(cd->clkdm_name);
117
118                 if (cd->clkdm == clkdm)
119                         break;
120         }
121
122         if (!cd->clkdm_name)
123                 return ERR_PTR(-ENOENT);
124
125         return cd;
126 }
127
128 /*
129  * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
130  * @autodep: struct clkdm_autodep * to resolve
131  *
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.
140  *
141  * XXX autodeps are deprecated and should be removed at the earliest
142  * opportunity
143  */
144 static void _autodep_lookup(struct clkdm_autodep *autodep)
145 {
146         struct clockdomain *clkdm;
147
148         if (!autodep)
149                 return;
150
151         if (!omap_chip_is(autodep->omap_chip))
152                 return;
153
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);
159         }
160         autodep->clkdm.ptr = clkdm;
161 }
162
163 /*
164  * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
165  * @clkdm: struct clockdomain *
166  *
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.
170  *
171  * XXX autodeps are deprecated and should be removed at the earliest
172  * opportunity
173  */
174 void _clkdm_add_autodeps(struct clockdomain *clkdm)
175 {
176         struct clkdm_autodep *autodep;
177
178         if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
179                 return;
180
181         for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
182                 if (IS_ERR(autodep->clkdm.ptr))
183                         continue;
184
185                 if (!omap_chip_is(autodep->omap_chip))
186                         continue;
187
188                 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
189                          "clkdm %s\n", autodep->clkdm.ptr->name,
190                          clkdm->name);
191
192                 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
193                 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
194         }
195 }
196
197 /*
198  * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
199  * @clkdm: struct clockdomain *
200  *
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.
204  *
205  * XXX autodeps are deprecated and should be removed at the earliest
206  * opportunity
207  */
208 void _clkdm_del_autodeps(struct clockdomain *clkdm)
209 {
210         struct clkdm_autodep *autodep;
211
212         if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
213                 return;
214
215         for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
216                 if (IS_ERR(autodep->clkdm.ptr))
217                         continue;
218
219                 if (!omap_chip_is(autodep->omap_chip))
220                         continue;
221
222                 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
223                          "clkdm %s\n", autodep->clkdm.ptr->name,
224                          clkdm->name);
225
226                 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
227                 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
228         }
229 }
230
231 /**
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
235  *
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.
239  */
240 static void _resolve_clkdm_deps(struct clockdomain *clkdm,
241                                 struct clkdm_dep *clkdm_deps)
242 {
243         struct clkdm_dep *cd;
244
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);
251
252                 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
253                      clkdm->name, cd->clkdm_name);
254         }
255 }
256
257 /* Public functions */
258
259 /**
260  * clkdm_register_platform_funcs - register clockdomain implementation fns
261  * @co: func pointers for arch specific implementations
262  *
263  * Register the list of function pointers used to implement the
264  * clockdomain functions on different OMAP SoCs.  Should be called
265  * before any other clkdm_register*() function.  Returns -EINVAL if
266  * @co is null, -EEXIST if platform functions have already been
267  * registered, or 0 upon success.
268  */
269 int clkdm_register_platform_funcs(struct clkdm_ops *co)
270 {
271         if (!co)
272                 return -EINVAL;
273
274         if (arch_clkdm)
275                 return -EEXIST;
276
277         arch_clkdm = co;
278
279         return 0;
280 };
281
282 /**
283  * clkdm_register_clkdms - register SoC clockdomains
284  * @cs: pointer to an array of struct clockdomain to register
285  *
286  * Register the clockdomains available on a particular OMAP SoC.  Must
287  * be called after clkdm_register_platform_funcs().  May be called
288  * multiple times.  Returns -EACCES if called before
289  * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
290  * null; or 0 upon success.
291  */
292 int clkdm_register_clkdms(struct clockdomain **cs)
293 {
294         struct clockdomain **c = NULL;
295
296         if (!arch_clkdm)
297                 return -EACCES;
298
299         if (!cs)
300                 return -EINVAL;
301
302         for (c = cs; *c; c++)
303                 _clkdm_register(*c);
304
305         return 0;
306 }
307
308 /**
309  * clkdm_register_autodeps - register autodeps (if required)
310  * @ia: pointer to a static array of struct clkdm_autodep to register
311  *
312  * Register clockdomain "automatic dependencies."  These are
313  * clockdomain wakeup and sleep dependencies that are automatically
314  * added whenever the first clock inside a clockdomain is enabled, and
315  * removed whenever the last clock inside a clockdomain is disabled.
316  * These are currently only used on OMAP3 devices, and are deprecated,
317  * since they waste energy.  However, until the OMAP2/3 IP block
318  * enable/disable sequence can be converted to match the OMAP4
319  * sequence, they are needed.
320  *
321  * Must be called only after all of the SoC clockdomains are
322  * registered, since the function will resolve autodep clockdomain
323  * names into clockdomain pointers.
324  *
325  * The struct clkdm_autodep @ia array must be static, as this function
326  * does not copy the array elements.
327  *
328  * Returns -EACCES if called before any clockdomains have been
329  * registered, -EINVAL if called with a null @ia argument, -EEXIST if
330  * autodeps have already been registered, or 0 upon success.
331  */
332 int clkdm_register_autodeps(struct clkdm_autodep *ia)
333 {
334         struct clkdm_autodep *a = NULL;
335
336         if (list_empty(&clkdm_list))
337                 return -EACCES;
338
339         if (!ia)
340                 return -EINVAL;
341
342         if (autodeps)
343                 return -EEXIST;
344
345         autodeps = ia;
346         for (a = autodeps; a->clkdm.ptr; a++)
347                 _autodep_lookup(a);
348
349         return 0;
350 }
351
352 /**
353  * clkdm_complete_init - set up the clockdomain layer
354  *
355  * Put all clockdomains into software-supervised mode; PM code should
356  * later enable hardware-supervised mode as appropriate.  Must be
357  * called after clkdm_register_clkdms().  Returns -EACCES if called
358  * before clkdm_register_clkdms(), or 0 upon success.
359  */
360 int clkdm_complete_init(void)
361 {
362         struct clockdomain *clkdm;
363
364         if (list_empty(&clkdm_list))
365                 return -EACCES;
366
367         list_for_each_entry(clkdm, &clkdm_list, node) {
368                 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
369                         clkdm_wakeup(clkdm);
370                 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
371                         clkdm_deny_idle(clkdm);
372
373                 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
374                 clkdm_clear_all_wkdeps(clkdm);
375
376                 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
377                 clkdm_clear_all_sleepdeps(clkdm);
378         }
379
380         return 0;
381 }
382
383 /**
384  * clkdm_lookup - look up a clockdomain by name, return a pointer
385  * @name: name of clockdomain
386  *
387  * Find a registered clockdomain by its name @name.  Returns a pointer
388  * to the struct clockdomain if found, or NULL otherwise.
389  */
390 struct clockdomain *clkdm_lookup(const char *name)
391 {
392         struct clockdomain *clkdm, *temp_clkdm;
393
394         if (!name)
395                 return NULL;
396
397         clkdm = NULL;
398
399         list_for_each_entry(temp_clkdm, &clkdm_list, node) {
400                 if (!strcmp(name, temp_clkdm->name)) {
401                         clkdm = temp_clkdm;
402                         break;
403                 }
404         }
405
406         return clkdm;
407 }
408
409 /**
410  * clkdm_for_each - call function on each registered clockdomain
411  * @fn: callback function *
412  *
413  * Call the supplied function @fn for each registered clockdomain.
414  * The callback function @fn can return anything but 0 to bail
415  * out early from the iterator.  The callback function is called with
416  * the clkdm_mutex held, so no clockdomain structure manipulation
417  * functions should be called from the callback, although hardware
418  * clockdomain control functions are fine.  Returns the last return
419  * value of the callback function, which should be 0 for success or
420  * anything else to indicate failure; or -EINVAL if the function pointer
421  * is null.
422  */
423 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
424                         void *user)
425 {
426         struct clockdomain *clkdm;
427         int ret = 0;
428
429         if (!fn)
430                 return -EINVAL;
431
432         list_for_each_entry(clkdm, &clkdm_list, node) {
433                 ret = (*fn)(clkdm, user);
434                 if (ret)
435                         break;
436         }
437
438         return ret;
439 }
440
441
442 /**
443  * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
444  * @clkdm: struct clockdomain *
445  *
446  * Return a pointer to the struct powerdomain that the specified clockdomain
447  * @clkdm exists in, or returns NULL if @clkdm is NULL.
448  */
449 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
450 {
451         if (!clkdm)
452                 return NULL;
453
454         return clkdm->pwrdm.ptr;
455 }
456
457
458 /* Hardware clockdomain control */
459
460 /**
461  * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
462  * @clkdm1: wake this struct clockdomain * up (dependent)
463  * @clkdm2: when this struct clockdomain * wakes up (source)
464  *
465  * When the clockdomain represented by @clkdm2 wakes up, wake up
466  * @clkdm1. Implemented in hardware on the OMAP, this feature is
467  * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
468  * Returns -EINVAL if presented with invalid clockdomain pointers,
469  * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
470  * success.
471  */
472 int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
473 {
474         struct clkdm_dep *cd;
475         int ret = 0;
476
477         if (!clkdm1 || !clkdm2)
478                 return -EINVAL;
479
480         cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
481         if (IS_ERR(cd))
482                 ret = PTR_ERR(cd);
483
484         if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
485                 ret = -EINVAL;
486
487         if (ret) {
488                 pr_debug("clockdomain: hardware cannot set/clear wake up of "
489                          "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
490                 return ret;
491         }
492
493         if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
494                 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
495                          "up\n", clkdm1->name, clkdm2->name);
496
497                 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
498         }
499
500         return ret;
501 }
502
503 /**
504  * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
505  * @clkdm1: wake this struct clockdomain * up (dependent)
506  * @clkdm2: when this struct clockdomain * wakes up (source)
507  *
508  * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
509  * wakes up.  Returns -EINVAL if presented with invalid clockdomain
510  * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
511  * 0 upon success.
512  */
513 int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
514 {
515         struct clkdm_dep *cd;
516         int ret = 0;
517
518         if (!clkdm1 || !clkdm2)
519                 return -EINVAL;
520
521         cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
522         if (IS_ERR(cd))
523                 ret = PTR_ERR(cd);
524
525         if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
526                 ret = -EINVAL;
527
528         if (ret) {
529                 pr_debug("clockdomain: hardware cannot set/clear wake up of "
530                          "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
531                 return ret;
532         }
533
534         if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
535                 pr_debug("clockdomain: hardware will no longer wake up %s "
536                          "after %s wakes up\n", clkdm1->name, clkdm2->name);
537
538                 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
539         }
540
541         return ret;
542 }
543
544 /**
545  * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
546  * @clkdm1: wake this struct clockdomain * up (dependent)
547  * @clkdm2: when this struct clockdomain * wakes up (source)
548  *
549  * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
550  * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
551  * if either clockdomain pointer is invalid; or -ENOENT if the hardware
552  * is incapable.
553  *
554  * REVISIT: Currently this function only represents software-controllable
555  * wakeup dependencies.  Wakeup dependencies fixed in hardware are not
556  * yet handled here.
557  */
558 int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
559 {
560         struct clkdm_dep *cd;
561         int ret = 0;
562
563         if (!clkdm1 || !clkdm2)
564                 return -EINVAL;
565
566         cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
567         if (IS_ERR(cd))
568                 ret = PTR_ERR(cd);
569
570         if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
571                 ret = -EINVAL;
572
573         if (ret) {
574                 pr_debug("clockdomain: hardware cannot set/clear wake up of "
575                          "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
576                 return ret;
577         }
578
579         /* XXX It's faster to return the atomic wkdep_usecount */
580         return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
581 }
582
583 /**
584  * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
585  * @clkdm: struct clockdomain * to remove all wakeup dependencies from
586  *
587  * Remove all inter-clockdomain wakeup dependencies that could cause
588  * @clkdm to wake.  Intended to be used during boot to initialize the
589  * PRCM to a known state, after all clockdomains are put into swsup idle
590  * and woken up.  Returns -EINVAL if @clkdm pointer is invalid, or
591  * 0 upon success.
592  */
593 int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
594 {
595         if (!clkdm)
596                 return -EINVAL;
597
598         if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
599                 return -EINVAL;
600
601         return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
602 }
603
604 /**
605  * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
606  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
607  * @clkdm2: when this struct clockdomain * is active (source)
608  *
609  * Prevent @clkdm1 from automatically going inactive (and then to
610  * retention or off) if @clkdm2 is active.  Returns -EINVAL if
611  * presented with invalid clockdomain pointers or called on a machine
612  * that does not support software-configurable hardware sleep
613  * dependencies, -ENOENT if the specified dependency cannot be set in
614  * hardware, or 0 upon success.
615  */
616 int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
617 {
618         struct clkdm_dep *cd;
619         int ret = 0;
620
621         if (!clkdm1 || !clkdm2)
622                 return -EINVAL;
623
624         cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
625         if (IS_ERR(cd))
626                 ret = PTR_ERR(cd);
627
628         if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
629                 ret = -EINVAL;
630
631         if (ret) {
632                 pr_debug("clockdomain: hardware cannot set/clear sleep "
633                          "dependency affecting %s from %s\n", clkdm1->name,
634                          clkdm2->name);
635                 return ret;
636         }
637
638         if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
639                 pr_debug("clockdomain: will prevent %s from sleeping if %s "
640                          "is active\n", clkdm1->name, clkdm2->name);
641
642                 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
643         }
644
645         return ret;
646 }
647
648 /**
649  * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
650  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
651  * @clkdm2: when this struct clockdomain * is active (source)
652  *
653  * Allow @clkdm1 to automatically go inactive (and then to retention or
654  * off), independent of the activity state of @clkdm2.  Returns -EINVAL
655  * if presented with invalid clockdomain pointers or called on a machine
656  * that does not support software-configurable hardware sleep dependencies,
657  * -ENOENT if the specified dependency cannot be cleared in hardware, or
658  * 0 upon success.
659  */
660 int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
661 {
662         struct clkdm_dep *cd;
663         int ret = 0;
664
665         if (!clkdm1 || !clkdm2)
666                 return -EINVAL;
667
668         cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
669         if (IS_ERR(cd))
670                 ret = PTR_ERR(cd);
671
672         if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
673                 ret = -EINVAL;
674
675         if (ret) {
676                 pr_debug("clockdomain: hardware cannot set/clear sleep "
677                          "dependency affecting %s from %s\n", clkdm1->name,
678                          clkdm2->name);
679                 return ret;
680         }
681
682         if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
683                 pr_debug("clockdomain: will no longer prevent %s from "
684                          "sleeping if %s is active\n", clkdm1->name,
685                          clkdm2->name);
686
687                 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
688         }
689
690         return ret;
691 }
692
693 /**
694  * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
695  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
696  * @clkdm2: when this struct clockdomain * is active (source)
697  *
698  * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
699  * not be allowed to automatically go inactive if @clkdm2 is active;
700  * 0 if @clkdm1's automatic power state inactivity transition is independent
701  * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
702  * on a machine that does not support software-configurable hardware sleep
703  * dependencies; or -ENOENT if the hardware is incapable.
704  *
705  * REVISIT: Currently this function only represents software-controllable
706  * sleep dependencies.  Sleep dependencies fixed in hardware are not
707  * yet handled here.
708  */
709 int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
710 {
711         struct clkdm_dep *cd;
712         int ret = 0;
713
714         if (!clkdm1 || !clkdm2)
715                 return -EINVAL;
716
717         cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
718         if (IS_ERR(cd))
719                 ret = PTR_ERR(cd);
720
721         if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
722                 ret = -EINVAL;
723
724         if (ret) {
725                 pr_debug("clockdomain: hardware cannot set/clear sleep "
726                          "dependency affecting %s from %s\n", clkdm1->name,
727                          clkdm2->name);
728                 return ret;
729         }
730
731         /* XXX It's faster to return the atomic sleepdep_usecount */
732         return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
733 }
734
735 /**
736  * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
737  * @clkdm: struct clockdomain * to remove all sleep dependencies from
738  *
739  * Remove all inter-clockdomain sleep dependencies that could prevent
740  * @clkdm from idling.  Intended to be used during boot to initialize the
741  * PRCM to a known state, after all clockdomains are put into swsup idle
742  * and woken up.  Returns -EINVAL if @clkdm pointer is invalid, or
743  * 0 upon success.
744  */
745 int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
746 {
747         if (!clkdm)
748                 return -EINVAL;
749
750         if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
751                 return -EINVAL;
752
753         return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
754 }
755
756 /**
757  * clkdm_sleep - force clockdomain sleep transition
758  * @clkdm: struct clockdomain *
759  *
760  * Instruct the CM to force a sleep transition on the specified
761  * clockdomain @clkdm.  Returns -EINVAL if @clkdm is NULL or if
762  * clockdomain does not support software-initiated sleep; 0 upon
763  * success.
764  */
765 int clkdm_sleep(struct clockdomain *clkdm)
766 {
767         int ret;
768         unsigned long flags;
769
770         if (!clkdm)
771                 return -EINVAL;
772
773         if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
774                 pr_debug("clockdomain: %s does not support forcing "
775                          "sleep via software\n", clkdm->name);
776                 return -EINVAL;
777         }
778
779         if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
780                 return -EINVAL;
781
782         pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
783
784         spin_lock_irqsave(&clkdm->lock, flags);
785         clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
786         ret = arch_clkdm->clkdm_sleep(clkdm);
787         spin_unlock_irqrestore(&clkdm->lock, flags);
788         return ret;
789 }
790
791 /**
792  * clkdm_wakeup - force clockdomain wakeup transition
793  * @clkdm: struct clockdomain *
794  *
795  * Instruct the CM to force a wakeup transition on the specified
796  * clockdomain @clkdm.  Returns -EINVAL if @clkdm is NULL or if the
797  * clockdomain does not support software-controlled wakeup; 0 upon
798  * success.
799  */
800 int clkdm_wakeup(struct clockdomain *clkdm)
801 {
802         int ret;
803         unsigned long flags;
804
805         if (!clkdm)
806                 return -EINVAL;
807
808         if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
809                 pr_debug("clockdomain: %s does not support forcing "
810                          "wakeup via software\n", clkdm->name);
811                 return -EINVAL;
812         }
813
814         if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
815                 return -EINVAL;
816
817         pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
818
819         spin_lock_irqsave(&clkdm->lock, flags);
820         clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
821         ret = arch_clkdm->clkdm_wakeup(clkdm);
822         ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
823         spin_unlock_irqrestore(&clkdm->lock, flags);
824         return ret;
825 }
826
827 /**
828  * clkdm_allow_idle - enable hwsup idle transitions for clkdm
829  * @clkdm: struct clockdomain *
830  *
831  * Allow the hardware to automatically switch the clockdomain @clkdm into
832  * active or idle states, as needed by downstream clocks.  If the
833  * clockdomain has any downstream clocks enabled in the clock
834  * framework, wkdep/sleepdep autodependencies are added; this is so
835  * device drivers can read and write to the device.  No return value.
836  */
837 void clkdm_allow_idle(struct clockdomain *clkdm)
838 {
839         unsigned long flags;
840
841         if (!clkdm)
842                 return;
843
844         if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
845                 pr_debug("clock: automatic idle transitions cannot be enabled "
846                          "on clockdomain %s\n", clkdm->name);
847                 return;
848         }
849
850         if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
851                 return;
852
853         pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
854                  clkdm->name);
855
856         spin_lock_irqsave(&clkdm->lock, flags);
857         clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
858         arch_clkdm->clkdm_allow_idle(clkdm);
859         pwrdm_clkdm_state_switch(clkdm);
860         spin_unlock_irqrestore(&clkdm->lock, flags);
861 }
862
863 /**
864  * clkdm_deny_idle - disable hwsup idle transitions for clkdm
865  * @clkdm: struct clockdomain *
866  *
867  * Prevent the hardware from automatically switching the clockdomain
868  * @clkdm into inactive or idle states.  If the clockdomain has
869  * downstream clocks enabled in the clock framework, wkdep/sleepdep
870  * autodependencies are removed.  No return value.
871  */
872 void clkdm_deny_idle(struct clockdomain *clkdm)
873 {
874         unsigned long flags;
875
876         if (!clkdm)
877                 return;
878
879         if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
880                 pr_debug("clockdomain: automatic idle transitions cannot be "
881                          "disabled on %s\n", clkdm->name);
882                 return;
883         }
884
885         if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
886                 return;
887
888         pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
889                  clkdm->name);
890
891         spin_lock_irqsave(&clkdm->lock, flags);
892         clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
893         arch_clkdm->clkdm_deny_idle(clkdm);
894         pwrdm_state_switch(clkdm->pwrdm.ptr);
895         spin_unlock_irqrestore(&clkdm->lock, flags);
896 }
897
898 /**
899  * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
900  * @clkdm: struct clockdomain *
901  *
902  * Returns true if clockdomain @clkdm currently has
903  * hardware-supervised idle enabled, or false if it does not or if
904  * @clkdm is NULL.  It is only valid to call this function after
905  * clkdm_init() has been called.  This function does not actually read
906  * bits from the hardware; it instead tests an in-memory flag that is
907  * changed whenever the clockdomain code changes the auto-idle mode.
908  */
909 bool clkdm_in_hwsup(struct clockdomain *clkdm)
910 {
911         bool ret;
912         unsigned long flags;
913
914         if (!clkdm)
915                 return false;
916
917         spin_lock_irqsave(&clkdm->lock, flags);
918         ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
919         spin_unlock_irqrestore(&clkdm->lock, flags);
920
921         return ret;
922 }
923
924 /* Clockdomain-to-clock/hwmod framework interface code */
925
926 static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
927 {
928         unsigned long flags;
929
930         if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
931                 return -EINVAL;
932
933         /*
934          * For arch's with no autodeps, clkcm_clk_enable
935          * should be called for every clock instance or hwmod that is
936          * enabled, so the clkdm can be force woken up.
937          */
938         if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
939                 return 0;
940
941         spin_lock_irqsave(&clkdm->lock, flags);
942         arch_clkdm->clkdm_clk_enable(clkdm);
943         pwrdm_wait_transition(clkdm->pwrdm.ptr);
944         pwrdm_clkdm_state_switch(clkdm);
945         spin_unlock_irqrestore(&clkdm->lock, flags);
946
947         pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
948
949         return 0;
950 }
951
952 static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
953 {
954         unsigned long flags;
955
956         if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
957                 return -EINVAL;
958
959         if (atomic_read(&clkdm->usecount) == 0) {
960                 WARN_ON(1); /* underflow */
961                 return -ERANGE;
962         }
963
964         if (atomic_dec_return(&clkdm->usecount) > 0)
965                 return 0;
966
967         spin_lock_irqsave(&clkdm->lock, flags);
968         arch_clkdm->clkdm_clk_disable(clkdm);
969         pwrdm_clkdm_state_switch(clkdm);
970         spin_unlock_irqrestore(&clkdm->lock, flags);
971
972         pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
973
974         return 0;
975 }
976
977 /**
978  * clkdm_clk_enable - add an enabled downstream clock to this clkdm
979  * @clkdm: struct clockdomain *
980  * @clk: struct clk * of the enabled downstream clock
981  *
982  * Increment the usecount of the clockdomain @clkdm and ensure that it
983  * is awake before @clk is enabled.  Intended to be called by
984  * clk_enable() code.  If the clockdomain is in software-supervised
985  * idle mode, force the clockdomain to wake.  If the clockdomain is in
986  * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
987  * ensure that devices in the clockdomain can be read from/written to
988  * by on-chip processors.  Returns -EINVAL if passed null pointers;
989  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
990  */
991 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
992 {
993         /*
994          * XXX Rewrite this code to maintain a list of enabled
995          * downstream clocks for debugging purposes?
996          */
997
998         if (!clk)
999                 return -EINVAL;
1000
1001         return _clkdm_clk_hwmod_enable(clkdm);
1002 }
1003
1004 /**
1005  * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
1006  * @clkdm: struct clockdomain *
1007  * @clk: struct clk * of the disabled downstream clock
1008  *
1009  * Decrement the usecount of this clockdomain @clkdm when @clk is
1010  * disabled.  Intended to be called by clk_disable() code.  If the
1011  * clockdomain usecount goes to 0, put the clockdomain to sleep
1012  * (software-supervised mode) or remove the clkdm autodependencies
1013  * (hardware-supervised mode).  Returns -EINVAL if passed null
1014  * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1015  * upon success or if the clockdomain is in hwsup idle mode.
1016  */
1017 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
1018 {
1019         /*
1020          * XXX Rewrite this code to maintain a list of enabled
1021          * downstream clocks for debugging purposes?
1022          */
1023
1024         if (!clk)
1025                 return -EINVAL;
1026
1027         return _clkdm_clk_hwmod_disable(clkdm);
1028 }
1029
1030 /**
1031  * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1032  * @clkdm: struct clockdomain *
1033  * @oh: struct omap_hwmod * of the enabled downstream hwmod
1034  *
1035  * Increment the usecount of the clockdomain @clkdm and ensure that it
1036  * is awake before @oh is enabled. Intended to be called by
1037  * module_enable() code.
1038  * If the clockdomain is in software-supervised idle mode, force the
1039  * clockdomain to wake.  If the clockdomain is in hardware-supervised idle
1040  * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1041  * clockdomain can be read from/written to by on-chip processors.
1042  * Returns -EINVAL if passed null pointers;
1043  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1044  */
1045 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1046 {
1047         /* The clkdm attribute does not exist yet prior OMAP4 */
1048         if (cpu_is_omap24xx() || cpu_is_omap34xx())
1049                 return 0;
1050
1051         /*
1052          * XXX Rewrite this code to maintain a list of enabled
1053          * downstream hwmods for debugging purposes?
1054          */
1055
1056         if (!oh)
1057                 return -EINVAL;
1058
1059         return _clkdm_clk_hwmod_enable(clkdm);
1060 }
1061
1062 /**
1063  * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1064  * @clkdm: struct clockdomain *
1065  * @oh: struct omap_hwmod * of the disabled downstream hwmod
1066  *
1067  * Decrement the usecount of this clockdomain @clkdm when @oh is
1068  * disabled. Intended to be called by module_disable() code.
1069  * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1070  * (software-supervised mode) or remove the clkdm autodependencies
1071  * (hardware-supervised mode).
1072  * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1073  * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1074  * idle mode.
1075  */
1076 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1077 {
1078         /* The clkdm attribute does not exist yet prior OMAP4 */
1079         if (cpu_is_omap24xx() || cpu_is_omap34xx())
1080                 return 0;
1081
1082         /*
1083          * XXX Rewrite this code to maintain a list of enabled
1084          * downstream hwmods for debugging purposes?
1085          */
1086
1087         if (!oh)
1088                 return -EINVAL;
1089
1090         return _clkdm_clk_hwmod_disable(clkdm);
1091 }
1092