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_init - set up the clockdomain layer
261  * @clkdms: optional pointer to an array of clockdomains to register
262  * @init_autodeps: optional pointer to an array of autodeps to register
263  * @custom_funcs: func pointers for arch specific implementations
264  *
265  * Set up internal state.  If a pointer to an array of clockdomains
266  * @clkdms was supplied, loop through the list of clockdomains,
267  * register all that are available on the current platform. Similarly,
268  * if a pointer to an array of clockdomain autodependencies
269  * @init_autodeps was provided, register those.  No return value.
270  */
271 void clkdm_init(struct clockdomain **clkdms,
272 		struct clkdm_autodep *init_autodeps,
273 		struct clkdm_ops *custom_funcs)
274 {
275 	struct clockdomain **c = NULL;
276 	struct clockdomain *clkdm;
277 	struct clkdm_autodep *autodep = NULL;
278 
279 	if (!custom_funcs)
280 		WARN(1, "No custom clkdm functions registered\n");
281 	else
282 		arch_clkdm = custom_funcs;
283 
284 	if (clkdms)
285 		for (c = clkdms; *c; c++)
286 			_clkdm_register(*c);
287 
288 	autodeps = init_autodeps;
289 	if (autodeps)
290 		for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
291 			_autodep_lookup(autodep);
292 
293 	/*
294 	 * Put all clockdomains into software-supervised mode; PM code
295 	 * should later enable hardware-supervised mode as appropriate
296 	 */
297 	list_for_each_entry(clkdm, &clkdm_list, node) {
298 		if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
299 			clkdm_wakeup(clkdm);
300 		else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
301 			clkdm_deny_idle(clkdm);
302 
303 		_resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
304 		clkdm_clear_all_wkdeps(clkdm);
305 
306 		_resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
307 		clkdm_clear_all_sleepdeps(clkdm);
308 	}
309 }
310 
311 /**
312  * clkdm_lookup - look up a clockdomain by name, return a pointer
313  * @name: name of clockdomain
314  *
315  * Find a registered clockdomain by its name @name.  Returns a pointer
316  * to the struct clockdomain if found, or NULL otherwise.
317  */
318 struct clockdomain *clkdm_lookup(const char *name)
319 {
320 	struct clockdomain *clkdm, *temp_clkdm;
321 
322 	if (!name)
323 		return NULL;
324 
325 	clkdm = NULL;
326 
327 	list_for_each_entry(temp_clkdm, &clkdm_list, node) {
328 		if (!strcmp(name, temp_clkdm->name)) {
329 			clkdm = temp_clkdm;
330 			break;
331 		}
332 	}
333 
334 	return clkdm;
335 }
336 
337 /**
338  * clkdm_for_each - call function on each registered clockdomain
339  * @fn: callback function *
340  *
341  * Call the supplied function @fn for each registered clockdomain.
342  * The callback function @fn can return anything but 0 to bail
343  * out early from the iterator.  The callback function is called with
344  * the clkdm_mutex held, so no clockdomain structure manipulation
345  * functions should be called from the callback, although hardware
346  * clockdomain control functions are fine.  Returns the last return
347  * value of the callback function, which should be 0 for success or
348  * anything else to indicate failure; or -EINVAL if the function pointer
349  * is null.
350  */
351 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
352 			void *user)
353 {
354 	struct clockdomain *clkdm;
355 	int ret = 0;
356 
357 	if (!fn)
358 		return -EINVAL;
359 
360 	list_for_each_entry(clkdm, &clkdm_list, node) {
361 		ret = (*fn)(clkdm, user);
362 		if (ret)
363 			break;
364 	}
365 
366 	return ret;
367 }
368 
369 
370 /**
371  * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
372  * @clkdm: struct clockdomain *
373  *
374  * Return a pointer to the struct powerdomain that the specified clockdomain
375  * @clkdm exists in, or returns NULL if @clkdm is NULL.
376  */
377 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
378 {
379 	if (!clkdm)
380 		return NULL;
381 
382 	return clkdm->pwrdm.ptr;
383 }
384 
385 
386 /* Hardware clockdomain control */
387 
388 /**
389  * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
390  * @clkdm1: wake this struct clockdomain * up (dependent)
391  * @clkdm2: when this struct clockdomain * wakes up (source)
392  *
393  * When the clockdomain represented by @clkdm2 wakes up, wake up
394  * @clkdm1. Implemented in hardware on the OMAP, this feature is
395  * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
396  * Returns -EINVAL if presented with invalid clockdomain pointers,
397  * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
398  * success.
399  */
400 int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
401 {
402 	struct clkdm_dep *cd;
403 	int ret = 0;
404 
405 	if (!clkdm1 || !clkdm2)
406 		return -EINVAL;
407 
408 	cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
409 	if (IS_ERR(cd))
410 		ret = PTR_ERR(cd);
411 
412 	if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
413 		ret = -EINVAL;
414 
415 	if (ret) {
416 		pr_debug("clockdomain: hardware cannot set/clear wake up of "
417 			 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
418 		return ret;
419 	}
420 
421 	if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
422 		pr_debug("clockdomain: hardware will wake up %s when %s wakes "
423 			 "up\n", clkdm1->name, clkdm2->name);
424 
425 		ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
426 	}
427 
428 	return ret;
429 }
430 
431 /**
432  * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
433  * @clkdm1: wake this struct clockdomain * up (dependent)
434  * @clkdm2: when this struct clockdomain * wakes up (source)
435  *
436  * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
437  * wakes up.  Returns -EINVAL if presented with invalid clockdomain
438  * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
439  * 0 upon success.
440  */
441 int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
442 {
443 	struct clkdm_dep *cd;
444 	int ret = 0;
445 
446 	if (!clkdm1 || !clkdm2)
447 		return -EINVAL;
448 
449 	cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
450 	if (IS_ERR(cd))
451 		ret = PTR_ERR(cd);
452 
453 	if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
454 		ret = -EINVAL;
455 
456 	if (ret) {
457 		pr_debug("clockdomain: hardware cannot set/clear wake up of "
458 			 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
459 		return ret;
460 	}
461 
462 	if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
463 		pr_debug("clockdomain: hardware will no longer wake up %s "
464 			 "after %s wakes up\n", clkdm1->name, clkdm2->name);
465 
466 		ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
467 	}
468 
469 	return ret;
470 }
471 
472 /**
473  * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
474  * @clkdm1: wake this struct clockdomain * up (dependent)
475  * @clkdm2: when this struct clockdomain * wakes up (source)
476  *
477  * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
478  * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
479  * if either clockdomain pointer is invalid; or -ENOENT if the hardware
480  * is incapable.
481  *
482  * REVISIT: Currently this function only represents software-controllable
483  * wakeup dependencies.  Wakeup dependencies fixed in hardware are not
484  * yet handled here.
485  */
486 int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
487 {
488 	struct clkdm_dep *cd;
489 	int ret = 0;
490 
491 	if (!clkdm1 || !clkdm2)
492 		return -EINVAL;
493 
494 	cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
495 	if (IS_ERR(cd))
496 		ret = PTR_ERR(cd);
497 
498 	if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
499 		ret = -EINVAL;
500 
501 	if (ret) {
502 		pr_debug("clockdomain: hardware cannot set/clear wake up of "
503 			 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
504 		return ret;
505 	}
506 
507 	/* XXX It's faster to return the atomic wkdep_usecount */
508 	return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
509 }
510 
511 /**
512  * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
513  * @clkdm: struct clockdomain * to remove all wakeup dependencies from
514  *
515  * Remove all inter-clockdomain wakeup dependencies that could cause
516  * @clkdm to wake.  Intended to be used during boot to initialize the
517  * PRCM to a known state, after all clockdomains are put into swsup idle
518  * and woken up.  Returns -EINVAL if @clkdm pointer is invalid, or
519  * 0 upon success.
520  */
521 int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
522 {
523 	if (!clkdm)
524 		return -EINVAL;
525 
526 	if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
527 		return -EINVAL;
528 
529 	return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
530 }
531 
532 /**
533  * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
534  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
535  * @clkdm2: when this struct clockdomain * is active (source)
536  *
537  * Prevent @clkdm1 from automatically going inactive (and then to
538  * retention or off) if @clkdm2 is active.  Returns -EINVAL if
539  * presented with invalid clockdomain pointers or called on a machine
540  * that does not support software-configurable hardware sleep
541  * dependencies, -ENOENT if the specified dependency cannot be set in
542  * hardware, or 0 upon success.
543  */
544 int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
545 {
546 	struct clkdm_dep *cd;
547 	int ret = 0;
548 
549 	if (!clkdm1 || !clkdm2)
550 		return -EINVAL;
551 
552 	cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
553 	if (IS_ERR(cd))
554 		ret = PTR_ERR(cd);
555 
556 	if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
557 		ret = -EINVAL;
558 
559 	if (ret) {
560 		pr_debug("clockdomain: hardware cannot set/clear sleep "
561 			 "dependency affecting %s from %s\n", clkdm1->name,
562 			 clkdm2->name);
563 		return ret;
564 	}
565 
566 	if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
567 		pr_debug("clockdomain: will prevent %s from sleeping if %s "
568 			 "is active\n", clkdm1->name, clkdm2->name);
569 
570 		ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
571 	}
572 
573 	return ret;
574 }
575 
576 /**
577  * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
578  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
579  * @clkdm2: when this struct clockdomain * is active (source)
580  *
581  * Allow @clkdm1 to automatically go inactive (and then to retention or
582  * off), independent of the activity state of @clkdm2.  Returns -EINVAL
583  * if presented with invalid clockdomain pointers or called on a machine
584  * that does not support software-configurable hardware sleep dependencies,
585  * -ENOENT if the specified dependency cannot be cleared in hardware, or
586  * 0 upon success.
587  */
588 int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
589 {
590 	struct clkdm_dep *cd;
591 	int ret = 0;
592 
593 	if (!clkdm1 || !clkdm2)
594 		return -EINVAL;
595 
596 	cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
597 	if (IS_ERR(cd))
598 		ret = PTR_ERR(cd);
599 
600 	if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
601 		ret = -EINVAL;
602 
603 	if (ret) {
604 		pr_debug("clockdomain: hardware cannot set/clear sleep "
605 			 "dependency affecting %s from %s\n", clkdm1->name,
606 			 clkdm2->name);
607 		return ret;
608 	}
609 
610 	if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
611 		pr_debug("clockdomain: will no longer prevent %s from "
612 			 "sleeping if %s is active\n", clkdm1->name,
613 			 clkdm2->name);
614 
615 		ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
616 	}
617 
618 	return ret;
619 }
620 
621 /**
622  * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
623  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
624  * @clkdm2: when this struct clockdomain * is active (source)
625  *
626  * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
627  * not be allowed to automatically go inactive if @clkdm2 is active;
628  * 0 if @clkdm1's automatic power state inactivity transition is independent
629  * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
630  * on a machine that does not support software-configurable hardware sleep
631  * dependencies; or -ENOENT if the hardware is incapable.
632  *
633  * REVISIT: Currently this function only represents software-controllable
634  * sleep dependencies.	Sleep dependencies fixed in hardware are not
635  * yet handled here.
636  */
637 int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
638 {
639 	struct clkdm_dep *cd;
640 	int ret = 0;
641 
642 	if (!clkdm1 || !clkdm2)
643 		return -EINVAL;
644 
645 	cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
646 	if (IS_ERR(cd))
647 		ret = PTR_ERR(cd);
648 
649 	if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
650 		ret = -EINVAL;
651 
652 	if (ret) {
653 		pr_debug("clockdomain: hardware cannot set/clear sleep "
654 			 "dependency affecting %s from %s\n", clkdm1->name,
655 			 clkdm2->name);
656 		return ret;
657 	}
658 
659 	/* XXX It's faster to return the atomic sleepdep_usecount */
660 	return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
661 }
662 
663 /**
664  * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
665  * @clkdm: struct clockdomain * to remove all sleep dependencies from
666  *
667  * Remove all inter-clockdomain sleep dependencies that could prevent
668  * @clkdm from idling.  Intended to be used during boot to initialize the
669  * PRCM to a known state, after all clockdomains are put into swsup idle
670  * and woken up.  Returns -EINVAL if @clkdm pointer is invalid, or
671  * 0 upon success.
672  */
673 int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
674 {
675 	if (!clkdm)
676 		return -EINVAL;
677 
678 	if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
679 		return -EINVAL;
680 
681 	return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
682 }
683 
684 /**
685  * clkdm_sleep - force clockdomain sleep transition
686  * @clkdm: struct clockdomain *
687  *
688  * Instruct the CM to force a sleep transition on the specified
689  * clockdomain @clkdm.  Returns -EINVAL if @clkdm is NULL or if
690  * clockdomain does not support software-initiated sleep; 0 upon
691  * success.
692  */
693 int clkdm_sleep(struct clockdomain *clkdm)
694 {
695 	int ret;
696 	unsigned long flags;
697 
698 	if (!clkdm)
699 		return -EINVAL;
700 
701 	if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
702 		pr_debug("clockdomain: %s does not support forcing "
703 			 "sleep via software\n", clkdm->name);
704 		return -EINVAL;
705 	}
706 
707 	if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
708 		return -EINVAL;
709 
710 	pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
711 
712 	spin_lock_irqsave(&clkdm->lock, flags);
713 	clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
714 	ret = arch_clkdm->clkdm_sleep(clkdm);
715 	spin_unlock_irqrestore(&clkdm->lock, flags);
716 	return ret;
717 }
718 
719 /**
720  * clkdm_wakeup - force clockdomain wakeup transition
721  * @clkdm: struct clockdomain *
722  *
723  * Instruct the CM to force a wakeup transition on the specified
724  * clockdomain @clkdm.  Returns -EINVAL if @clkdm is NULL or if the
725  * clockdomain does not support software-controlled wakeup; 0 upon
726  * success.
727  */
728 int clkdm_wakeup(struct clockdomain *clkdm)
729 {
730 	int ret;
731 	unsigned long flags;
732 
733 	if (!clkdm)
734 		return -EINVAL;
735 
736 	if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
737 		pr_debug("clockdomain: %s does not support forcing "
738 			 "wakeup via software\n", clkdm->name);
739 		return -EINVAL;
740 	}
741 
742 	if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
743 		return -EINVAL;
744 
745 	pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
746 
747 	spin_lock_irqsave(&clkdm->lock, flags);
748 	clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
749 	ret = arch_clkdm->clkdm_wakeup(clkdm);
750 	ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
751 	spin_unlock_irqrestore(&clkdm->lock, flags);
752 	return ret;
753 }
754 
755 /**
756  * clkdm_allow_idle - enable hwsup idle transitions for clkdm
757  * @clkdm: struct clockdomain *
758  *
759  * Allow the hardware to automatically switch the clockdomain @clkdm into
760  * active or idle states, as needed by downstream clocks.  If the
761  * clockdomain has any downstream clocks enabled in the clock
762  * framework, wkdep/sleepdep autodependencies are added; this is so
763  * device drivers can read and write to the device.  No return value.
764  */
765 void clkdm_allow_idle(struct clockdomain *clkdm)
766 {
767 	unsigned long flags;
768 
769 	if (!clkdm)
770 		return;
771 
772 	if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
773 		pr_debug("clock: automatic idle transitions cannot be enabled "
774 			 "on clockdomain %s\n", clkdm->name);
775 		return;
776 	}
777 
778 	if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
779 		return;
780 
781 	pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
782 		 clkdm->name);
783 
784 	spin_lock_irqsave(&clkdm->lock, flags);
785 	clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
786 	arch_clkdm->clkdm_allow_idle(clkdm);
787 	pwrdm_clkdm_state_switch(clkdm);
788 	spin_unlock_irqrestore(&clkdm->lock, flags);
789 }
790 
791 /**
792  * clkdm_deny_idle - disable hwsup idle transitions for clkdm
793  * @clkdm: struct clockdomain *
794  *
795  * Prevent the hardware from automatically switching the clockdomain
796  * @clkdm into inactive or idle states.  If the clockdomain has
797  * downstream clocks enabled in the clock framework, wkdep/sleepdep
798  * autodependencies are removed.  No return value.
799  */
800 void clkdm_deny_idle(struct clockdomain *clkdm)
801 {
802 	unsigned long flags;
803 
804 	if (!clkdm)
805 		return;
806 
807 	if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
808 		pr_debug("clockdomain: automatic idle transitions cannot be "
809 			 "disabled on %s\n", clkdm->name);
810 		return;
811 	}
812 
813 	if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
814 		return;
815 
816 	pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
817 		 clkdm->name);
818 
819 	spin_lock_irqsave(&clkdm->lock, flags);
820 	clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
821 	arch_clkdm->clkdm_deny_idle(clkdm);
822 	pwrdm_state_switch(clkdm->pwrdm.ptr);
823 	spin_unlock_irqrestore(&clkdm->lock, flags);
824 }
825 
826 /**
827  * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
828  * @clkdm: struct clockdomain *
829  *
830  * Returns true if clockdomain @clkdm currently has
831  * hardware-supervised idle enabled, or false if it does not or if
832  * @clkdm is NULL.  It is only valid to call this function after
833  * clkdm_init() has been called.  This function does not actually read
834  * bits from the hardware; it instead tests an in-memory flag that is
835  * changed whenever the clockdomain code changes the auto-idle mode.
836  */
837 bool clkdm_in_hwsup(struct clockdomain *clkdm)
838 {
839 	bool ret;
840 	unsigned long flags;
841 
842 	if (!clkdm)
843 		return false;
844 
845 	spin_lock_irqsave(&clkdm->lock, flags);
846 	ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
847 	spin_unlock_irqrestore(&clkdm->lock, flags);
848 
849 	return ret;
850 }
851 
852 /* Clockdomain-to-clock/hwmod framework interface code */
853 
854 static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
855 {
856 	unsigned long flags;
857 
858 	if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
859 		return -EINVAL;
860 
861 	/*
862 	 * For arch's with no autodeps, clkcm_clk_enable
863 	 * should be called for every clock instance or hwmod that is
864 	 * enabled, so the clkdm can be force woken up.
865 	 */
866 	if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
867 		return 0;
868 
869 	spin_lock_irqsave(&clkdm->lock, flags);
870 	arch_clkdm->clkdm_clk_enable(clkdm);
871 	pwrdm_wait_transition(clkdm->pwrdm.ptr);
872 	pwrdm_clkdm_state_switch(clkdm);
873 	spin_unlock_irqrestore(&clkdm->lock, flags);
874 
875 	pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
876 
877 	return 0;
878 }
879 
880 static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
881 {
882 	unsigned long flags;
883 
884 	if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
885 		return -EINVAL;
886 
887 	if (atomic_read(&clkdm->usecount) == 0) {
888 		WARN_ON(1); /* underflow */
889 		return -ERANGE;
890 	}
891 
892 	if (atomic_dec_return(&clkdm->usecount) > 0)
893 		return 0;
894 
895 	spin_lock_irqsave(&clkdm->lock, flags);
896 	arch_clkdm->clkdm_clk_disable(clkdm);
897 	pwrdm_clkdm_state_switch(clkdm);
898 	spin_unlock_irqrestore(&clkdm->lock, flags);
899 
900 	pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
901 
902 	return 0;
903 }
904 
905 /**
906  * clkdm_clk_enable - add an enabled downstream clock to this clkdm
907  * @clkdm: struct clockdomain *
908  * @clk: struct clk * of the enabled downstream clock
909  *
910  * Increment the usecount of the clockdomain @clkdm and ensure that it
911  * is awake before @clk is enabled.  Intended to be called by
912  * clk_enable() code.  If the clockdomain is in software-supervised
913  * idle mode, force the clockdomain to wake.  If the clockdomain is in
914  * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
915  * ensure that devices in the clockdomain can be read from/written to
916  * by on-chip processors.  Returns -EINVAL if passed null pointers;
917  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
918  */
919 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
920 {
921 	/*
922 	 * XXX Rewrite this code to maintain a list of enabled
923 	 * downstream clocks for debugging purposes?
924 	 */
925 
926 	if (!clk)
927 		return -EINVAL;
928 
929 	return _clkdm_clk_hwmod_enable(clkdm);
930 }
931 
932 /**
933  * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
934  * @clkdm: struct clockdomain *
935  * @clk: struct clk * of the disabled downstream clock
936  *
937  * Decrement the usecount of this clockdomain @clkdm when @clk is
938  * disabled.  Intended to be called by clk_disable() code.  If the
939  * clockdomain usecount goes to 0, put the clockdomain to sleep
940  * (software-supervised mode) or remove the clkdm autodependencies
941  * (hardware-supervised mode).  Returns -EINVAL if passed null
942  * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
943  * upon success or if the clockdomain is in hwsup idle mode.
944  */
945 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
946 {
947 	/*
948 	 * XXX Rewrite this code to maintain a list of enabled
949 	 * downstream clocks for debugging purposes?
950 	 */
951 
952 	if (!clk)
953 		return -EINVAL;
954 
955 	return _clkdm_clk_hwmod_disable(clkdm);
956 }
957 
958 /**
959  * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
960  * @clkdm: struct clockdomain *
961  * @oh: struct omap_hwmod * of the enabled downstream hwmod
962  *
963  * Increment the usecount of the clockdomain @clkdm and ensure that it
964  * is awake before @oh is enabled. Intended to be called by
965  * module_enable() code.
966  * If the clockdomain is in software-supervised idle mode, force the
967  * clockdomain to wake.  If the clockdomain is in hardware-supervised idle
968  * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
969  * clockdomain can be read from/written to by on-chip processors.
970  * Returns -EINVAL if passed null pointers;
971  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
972  */
973 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
974 {
975 	/* The clkdm attribute does not exist yet prior OMAP4 */
976 	if (cpu_is_omap24xx() || cpu_is_omap34xx())
977 		return 0;
978 
979 	/*
980 	 * XXX Rewrite this code to maintain a list of enabled
981 	 * downstream hwmods for debugging purposes?
982 	 */
983 
984 	if (!oh)
985 		return -EINVAL;
986 
987 	return _clkdm_clk_hwmod_enable(clkdm);
988 }
989 
990 /**
991  * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
992  * @clkdm: struct clockdomain *
993  * @oh: struct omap_hwmod * of the disabled downstream hwmod
994  *
995  * Decrement the usecount of this clockdomain @clkdm when @oh is
996  * disabled. Intended to be called by module_disable() code.
997  * If the clockdomain usecount goes to 0, put the clockdomain to sleep
998  * (software-supervised mode) or remove the clkdm autodependencies
999  * (hardware-supervised mode).
1000  * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1001  * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1002  * idle mode.
1003  */
1004 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1005 {
1006 	/* The clkdm attribute does not exist yet prior OMAP4 */
1007 	if (cpu_is_omap24xx() || cpu_is_omap34xx())
1008 		return 0;
1009 
1010 	/*
1011 	 * XXX Rewrite this code to maintain a list of enabled
1012 	 * downstream hwmods for debugging purposes?
1013 	 */
1014 
1015 	if (!oh)
1016 		return -EINVAL;
1017 
1018 	return _clkdm_clk_hwmod_disable(clkdm);
1019 }
1020 
1021