OMAP: clockdomain: split clkdm_init()
[deliverable/linux.git] / arch / arm / mach-omap2 / clockdomain.c
CommitLineData
d459bfe0 1/*
8a3ddc75 2 * OMAP2/3/4 clockdomain framework functions
d459bfe0 3 *
32a363c0
PW
4 * Copyright (C) 2008-2011 Texas Instruments, Inc.
5 * Copyright (C) 2008-2011 Nokia Corporation
d459bfe0
PW
6 *
7 * Written by Paul Walmsley and Jouni Högander
8a3ddc75 8 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
d459bfe0
PW
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 */
33903eb5 14#undef DEBUG
d459bfe0 15
d459bfe0
PW
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>
5b74c676 23#include <linux/err.h>
d459bfe0
PW
24
25#include <linux/io.h>
26
27#include <linux/bitops.h>
28
55ed9694 29#include <plat/clock.h>
1540f214 30#include "clockdomain.h"
d459bfe0
PW
31
32/* clkdm_list contains all registered struct clockdomains */
33static LIST_HEAD(clkdm_list);
34
55ed9694
PW
35/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
36static struct clkdm_autodep *autodeps;
d459bfe0 37
32d4034e 38static struct clkdm_ops *arch_clkdm;
d459bfe0
PW
39
40/* Private functions */
41
55ed9694
PW
42static 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
e909d62a
PW
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 */
69static 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
555e74ea
RN
95 spin_lock_init(&clkdm->lock);
96
e909d62a
PW
97 pr_debug("clockdomain: registered %s\n", clkdm->name);
98
99 return 0;
100}
101
55ed9694
PW
102/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
103static 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++) {
55ed9694
PW
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;
55ed9694
PW
120 }
121
122 if (!cd->clkdm_name)
123 return ERR_PTR(-ENOENT);
124
125 return cd;
126}
127
d459bfe0 128/*
55ed9694
PW
129 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
130 * @autodep: struct clkdm_autodep * to resolve
d459bfe0 131 *
55ed9694
PW
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
d459bfe0
PW
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.
b170fbe1
PW
140 *
141 * XXX autodeps are deprecated and should be removed at the earliest
142 * opportunity
d459bfe0 143 */
55ed9694 144static void _autodep_lookup(struct clkdm_autodep *autodep)
d459bfe0 145{
55ed9694 146 struct clockdomain *clkdm;
d459bfe0
PW
147
148 if (!autodep)
149 return;
150
151 if (!omap_chip_is(autodep->omap_chip))
152 return;
153
55ed9694
PW
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);
d459bfe0 159 }
55ed9694 160 autodep->clkdm.ptr = clkdm;
d459bfe0
PW
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.
b170fbe1
PW
170 *
171 * XXX autodeps are deprecated and should be removed at the earliest
172 * opportunity
d459bfe0 173 */
5cd1937b 174void _clkdm_add_autodeps(struct clockdomain *clkdm)
d459bfe0 175{
55ed9694 176 struct clkdm_autodep *autodep;
d459bfe0 177
570b54c7 178 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
ad956160
PW
179 return;
180
55ed9694
PW
181 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
182 if (IS_ERR(autodep->clkdm.ptr))
d459bfe0
PW
183 continue;
184
d96df00d
PW
185 if (!omap_chip_is(autodep->omap_chip))
186 continue;
187
d459bfe0 188 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
55ed9694
PW
189 "clkdm %s\n", autodep->clkdm.ptr->name,
190 clkdm->name);
d459bfe0 191
55ed9694
PW
192 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
193 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
d459bfe0
PW
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.
b170fbe1
PW
204 *
205 * XXX autodeps are deprecated and should be removed at the earliest
206 * opportunity
d459bfe0 207 */
5cd1937b 208void _clkdm_del_autodeps(struct clockdomain *clkdm)
d459bfe0 209{
55ed9694 210 struct clkdm_autodep *autodep;
d459bfe0 211
570b54c7 212 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
ad956160
PW
213 return;
214
55ed9694
PW
215 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
216 if (IS_ERR(autodep->clkdm.ptr))
d459bfe0
PW
217 continue;
218
d96df00d
PW
219 if (!omap_chip_is(autodep->omap_chip))
220 continue;
221
d459bfe0 222 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
55ed9694
PW
223 "clkdm %s\n", autodep->clkdm.ptr->name,
224 clkdm->name);
d459bfe0 225
55ed9694
PW
226 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
227 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
d459bfe0
PW
228 }
229}
230
b170fbe1 231/**
4aef7a2a
RN
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
b170fbe1 235 *
4aef7a2a
RN
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.
b170fbe1 238 * No return value.
a0219fbd 239 */
4aef7a2a
RN
240static void _resolve_clkdm_deps(struct clockdomain *clkdm,
241 struct clkdm_dep *clkdm_deps)
a0219fbd 242{
4aef7a2a
RN
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 }
a0219fbd 255}
d459bfe0 256
d459bfe0
PW
257/* Public functions */
258
259/**
08cb9703
PW
260 * clkdm_register_platform_funcs - register clockdomain implementation fns
261 * @co: func pointers for arch specific implementations
d459bfe0 262 *
08cb9703
PW
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.
d459bfe0 268 */
08cb9703
PW
269int 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 */
292int clkdm_register_clkdms(struct clockdomain **cs)
d459bfe0
PW
293{
294 struct clockdomain **c = NULL;
d459bfe0 295
08cb9703
PW
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 */
332int clkdm_register_autodeps(struct clkdm_autodep *ia)
333{
334 struct clkdm_autodep *a = NULL;
335
336 if (list_empty(&clkdm_list))
337 return -EACCES;
32d4034e 338
08cb9703
PW
339 if (!ia)
340 return -EINVAL;
d459bfe0 341
55ed9694 342 if (autodeps)
08cb9703
PW
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 */
360int clkdm_complete_init(void)
361{
362 struct clockdomain *clkdm;
363
364 if (list_empty(&clkdm_list))
365 return -EACCES;
369d5614 366
369d5614 367 list_for_each_entry(clkdm, &clkdm_list, node) {
6f7f63cc 368 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
68b921ad 369 clkdm_wakeup(clkdm);
6f7f63cc 370 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
5cd1937b 371 clkdm_deny_idle(clkdm);
6f7f63cc 372
4aef7a2a 373 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
6f7f63cc 374 clkdm_clear_all_wkdeps(clkdm);
4aef7a2a
RN
375
376 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
6f7f63cc 377 clkdm_clear_all_sleepdeps(clkdm);
369d5614 378 }
08cb9703
PW
379
380 return 0;
d459bfe0
PW
381}
382
d459bfe0
PW
383/**
384 * clkdm_lookup - look up a clockdomain by name, return a pointer
385 * @name: name of clockdomain
386 *
f0271d65
PW
387 * Find a registered clockdomain by its name @name. Returns a pointer
388 * to the struct clockdomain if found, or NULL otherwise.
d459bfe0
PW
389 */
390struct 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
d459bfe0
PW
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 }
d459bfe0
PW
405
406 return clkdm;
407}
408
409/**
410 * clkdm_for_each - call function on each registered clockdomain
411 * @fn: callback function *
412 *
f0271d65
PW
413 * Call the supplied function @fn for each registered clockdomain.
414 * The callback function @fn can return anything but 0 to bail
d459bfe0
PW
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 */
a23456e9
PDS
423int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
424 void *user)
d459bfe0
PW
425{
426 struct clockdomain *clkdm;
427 int ret = 0;
428
429 if (!fn)
430 return -EINVAL;
431
d459bfe0 432 list_for_each_entry(clkdm, &clkdm_list, node) {
a23456e9 433 ret = (*fn)(clkdm, user);
d459bfe0
PW
434 if (ret)
435 break;
436 }
d459bfe0
PW
437
438 return ret;
439}
440
441
e89087c9
PW
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
f0271d65 447 * @clkdm exists in, or returns NULL if @clkdm is NULL.
e89087c9
PW
448 */
449struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
450{
451 if (!clkdm)
452 return NULL;
453
5b74c676 454 return clkdm->pwrdm.ptr;
e89087c9
PW
455}
456
457
d459bfe0
PW
458/* Hardware clockdomain control */
459
55ed9694
PW
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 */
472int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
473{
474 struct clkdm_dep *cd;
4aef7a2a 475 int ret = 0;
56bc78d4 476
55ed9694
PW
477 if (!clkdm1 || !clkdm2)
478 return -EINVAL;
479
480 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
4aef7a2a
RN
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) {
55ed9694
PW
488 pr_debug("clockdomain: hardware cannot set/clear wake up of "
489 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
4aef7a2a 490 return ret;
55ed9694
PW
491 }
492
369d5614
PW
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);
55ed9694 496
4aef7a2a 497 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
369d5614 498 }
55ed9694 499
4aef7a2a 500 return ret;
55ed9694
PW
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 */
513int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
514{
515 struct clkdm_dep *cd;
4aef7a2a 516 int ret = 0;
56bc78d4 517
55ed9694
PW
518 if (!clkdm1 || !clkdm2)
519 return -EINVAL;
520
521 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
4aef7a2a
RN
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) {
55ed9694
PW
529 pr_debug("clockdomain: hardware cannot set/clear wake up of "
530 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
4aef7a2a 531 return ret;
55ed9694
PW
532 }
533
369d5614
PW
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);
55ed9694 537
4aef7a2a 538 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
369d5614 539 }
55ed9694 540
4aef7a2a 541 return ret;
55ed9694
PW
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 */
558int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
559{
560 struct clkdm_dep *cd;
4aef7a2a 561 int ret = 0;
55ed9694
PW
562
563 if (!clkdm1 || !clkdm2)
564 return -EINVAL;
565
566 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
4aef7a2a
RN
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) {
55ed9694
PW
574 pr_debug("clockdomain: hardware cannot set/clear wake up of "
575 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
4aef7a2a 576 return ret;
55ed9694
PW
577 }
578
369d5614 579 /* XXX It's faster to return the atomic wkdep_usecount */
4aef7a2a 580 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
55ed9694
PW
581}
582
369d5614
PW
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 */
593int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
594{
369d5614
PW
595 if (!clkdm)
596 return -EINVAL;
597
4aef7a2a
RN
598 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
599 return -EINVAL;
369d5614 600
4aef7a2a 601 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
369d5614
PW
602}
603
55ed9694
PW
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 */
616int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
617{
618 struct clkdm_dep *cd;
4aef7a2a 619 int ret = 0;
55ed9694
PW
620
621 if (!clkdm1 || !clkdm2)
622 return -EINVAL;
623
624 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
4aef7a2a
RN
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) {
55ed9694
PW
632 pr_debug("clockdomain: hardware cannot set/clear sleep "
633 "dependency affecting %s from %s\n", clkdm1->name,
634 clkdm2->name);
4aef7a2a 635 return ret;
55ed9694
PW
636 }
637
369d5614
PW
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);
55ed9694 641
4aef7a2a 642 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
369d5614 643 }
55ed9694 644
4aef7a2a 645 return ret;
55ed9694
PW
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 */
660int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
661{
662 struct clkdm_dep *cd;
4aef7a2a 663 int ret = 0;
55ed9694
PW
664
665 if (!clkdm1 || !clkdm2)
666 return -EINVAL;
667
668 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
4aef7a2a
RN
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) {
55ed9694
PW
676 pr_debug("clockdomain: hardware cannot set/clear sleep "
677 "dependency affecting %s from %s\n", clkdm1->name,
678 clkdm2->name);
4aef7a2a 679 return ret;
55ed9694
PW
680 }
681
369d5614
PW
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);
55ed9694 686
4aef7a2a 687 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
369d5614 688 }
55ed9694 689
4aef7a2a 690 return ret;
55ed9694
PW
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 */
709int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
710{
711 struct clkdm_dep *cd;
4aef7a2a 712 int ret = 0;
55ed9694
PW
713
714 if (!clkdm1 || !clkdm2)
715 return -EINVAL;
716
717 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
4aef7a2a
RN
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) {
55ed9694
PW
725 pr_debug("clockdomain: hardware cannot set/clear sleep "
726 "dependency affecting %s from %s\n", clkdm1->name,
727 clkdm2->name);
4aef7a2a 728 return ret;
55ed9694
PW
729 }
730
369d5614 731 /* XXX It's faster to return the atomic sleepdep_usecount */
4aef7a2a 732 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
55ed9694
PW
733}
734
369d5614
PW
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 */
745int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
746{
369d5614
PW
747 if (!clkdm)
748 return -EINVAL;
749
4aef7a2a
RN
750 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
751 return -EINVAL;
369d5614 752
4aef7a2a 753 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
369d5614 754}
55ed9694 755
d459bfe0 756/**
68b921ad 757 * clkdm_sleep - force clockdomain sleep transition
d459bfe0
PW
758 * @clkdm: struct clockdomain *
759 *
760 * Instruct the CM to force a sleep transition on the specified
f0271d65 761 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
d459bfe0
PW
762 * clockdomain does not support software-initiated sleep; 0 upon
763 * success.
764 */
68b921ad 765int clkdm_sleep(struct clockdomain *clkdm)
d459bfe0 766{
555e74ea
RN
767 int ret;
768 unsigned long flags;
769
d459bfe0
PW
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
68b921ad
RN
779 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
780 return -EINVAL;
bd2122ca 781
68b921ad 782 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
d459bfe0 783
555e74ea 784 spin_lock_irqsave(&clkdm->lock, flags);
32a363c0 785 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
555e74ea
RN
786 ret = arch_clkdm->clkdm_sleep(clkdm);
787 spin_unlock_irqrestore(&clkdm->lock, flags);
788 return ret;
d459bfe0
PW
789}
790
791/**
68b921ad 792 * clkdm_wakeup - force clockdomain wakeup transition
d459bfe0
PW
793 * @clkdm: struct clockdomain *
794 *
795 * Instruct the CM to force a wakeup transition on the specified
f0271d65 796 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
d459bfe0
PW
797 * clockdomain does not support software-controlled wakeup; 0 upon
798 * success.
799 */
68b921ad 800int clkdm_wakeup(struct clockdomain *clkdm)
d459bfe0 801{
555e74ea
RN
802 int ret;
803 unsigned long flags;
804
d459bfe0
PW
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
68b921ad
RN
814 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
815 return -EINVAL;
bd2122ca 816
68b921ad 817 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
d459bfe0 818
555e74ea 819 spin_lock_irqsave(&clkdm->lock, flags);
32a363c0 820 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
555e74ea 821 ret = arch_clkdm->clkdm_wakeup(clkdm);
b1cbdb00 822 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
555e74ea
RN
823 spin_unlock_irqrestore(&clkdm->lock, flags);
824 return ret;
d459bfe0
PW
825}
826
827/**
5cd1937b 828 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
d459bfe0
PW
829 * @clkdm: struct clockdomain *
830 *
f0271d65 831 * Allow the hardware to automatically switch the clockdomain @clkdm into
d459bfe0
PW
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 */
5cd1937b 837void clkdm_allow_idle(struct clockdomain *clkdm)
d459bfe0 838{
555e74ea
RN
839 unsigned long flags;
840
d459bfe0
PW
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
5cd1937b
RN
850 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
851 return;
852
d459bfe0
PW
853 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
854 clkdm->name);
855
555e74ea 856 spin_lock_irqsave(&clkdm->lock, flags);
32a363c0 857 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
5cd1937b 858 arch_clkdm->clkdm_allow_idle(clkdm);
ba20bb12 859 pwrdm_clkdm_state_switch(clkdm);
555e74ea 860 spin_unlock_irqrestore(&clkdm->lock, flags);
d459bfe0
PW
861}
862
863/**
5cd1937b 864 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
d459bfe0
PW
865 * @clkdm: struct clockdomain *
866 *
867 * Prevent the hardware from automatically switching the clockdomain
f0271d65
PW
868 * @clkdm into inactive or idle states. If the clockdomain has
869 * downstream clocks enabled in the clock framework, wkdep/sleepdep
d459bfe0
PW
870 * autodependencies are removed. No return value.
871 */
5cd1937b 872void clkdm_deny_idle(struct clockdomain *clkdm)
d459bfe0 873{
555e74ea
RN
874 unsigned long flags;
875
d459bfe0
PW
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
5cd1937b
RN
885 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
886 return;
887
d459bfe0
PW
888 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
889 clkdm->name);
890
555e74ea 891 spin_lock_irqsave(&clkdm->lock, flags);
32a363c0 892 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
5cd1937b 893 arch_clkdm->clkdm_deny_idle(clkdm);
b1cbdb00 894 pwrdm_state_switch(clkdm->pwrdm.ptr);
555e74ea 895 spin_unlock_irqrestore(&clkdm->lock, flags);
d459bfe0
PW
896}
897
32a363c0
PW
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 */
909bool clkdm_in_hwsup(struct clockdomain *clkdm)
910{
555e74ea
RN
911 bool ret;
912 unsigned long flags;
913
32a363c0
PW
914 if (!clkdm)
915 return false;
916
555e74ea
RN
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;
32a363c0 922}
d459bfe0 923
113a7413
BC
924/* Clockdomain-to-clock/hwmod framework interface code */
925
926static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
927{
555e74ea
RN
928 unsigned long flags;
929
113a7413
BC
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
555e74ea 941 spin_lock_irqsave(&clkdm->lock, flags);
113a7413
BC
942 arch_clkdm->clkdm_clk_enable(clkdm);
943 pwrdm_wait_transition(clkdm->pwrdm.ptr);
944 pwrdm_clkdm_state_switch(clkdm);
555e74ea 945 spin_unlock_irqrestore(&clkdm->lock, flags);
113a7413
BC
946
947 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
948
949 return 0;
950}
951
952static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
953{
555e74ea
RN
954 unsigned long flags;
955
113a7413
BC
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
555e74ea 967 spin_lock_irqsave(&clkdm->lock, flags);
113a7413
BC
968 arch_clkdm->clkdm_clk_disable(clkdm);
969 pwrdm_clkdm_state_switch(clkdm);
555e74ea 970 spin_unlock_irqrestore(&clkdm->lock, flags);
113a7413
BC
971
972 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
973
974 return 0;
975}
d459bfe0
PW
976
977/**
4da71ae6 978 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
d459bfe0
PW
979 * @clkdm: struct clockdomain *
980 * @clk: struct clk * of the enabled downstream clock
981 *
f0271d65
PW
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.
d459bfe0 990 */
4da71ae6 991int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
d459bfe0 992{
d459bfe0
PW
993 /*
994 * XXX Rewrite this code to maintain a list of enabled
995 * downstream clocks for debugging purposes?
996 */
997
113a7413 998 if (!clk)
d459bfe0
PW
999 return -EINVAL;
1000
113a7413 1001 return _clkdm_clk_hwmod_enable(clkdm);
d459bfe0
PW
1002}
1003
1004/**
4da71ae6 1005 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
d459bfe0
PW
1006 * @clkdm: struct clockdomain *
1007 * @clk: struct clk * of the disabled downstream clock
1008 *
f0271d65
PW
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
113a7413
BC
1014 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1015 * upon success or if the clockdomain is in hwsup idle mode.
d459bfe0 1016 */
4da71ae6 1017int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
d459bfe0 1018{
d459bfe0
PW
1019 /*
1020 * XXX Rewrite this code to maintain a list of enabled
1021 * downstream clocks for debugging purposes?
1022 */
1023
113a7413 1024 if (!clk)
d459bfe0
PW
1025 return -EINVAL;
1026
113a7413
BC
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 */
1045int 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)
4da71ae6
RN
1057 return -EINVAL;
1058
113a7413
BC
1059 return _clkdm_clk_hwmod_enable(clkdm);
1060}
d459bfe0 1061
113a7413
BC
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 */
1076int 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())
d459bfe0
PW
1080 return 0;
1081
113a7413
BC
1082 /*
1083 * XXX Rewrite this code to maintain a list of enabled
1084 * downstream hwmods for debugging purposes?
1085 */
d459bfe0 1086
113a7413
BC
1087 if (!oh)
1088 return -EINVAL;
fe617af7 1089
113a7413 1090 return _clkdm_clk_hwmod_disable(clkdm);
d459bfe0
PW
1091}
1092
This page took 0.251953 seconds and 5 git commands to generate.