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 	spin_unlock_irqrestore(&clkdm->lock, flags);
751 	return ret;
752 }
753 
754 /**
755  * clkdm_allow_idle - enable hwsup idle transitions for clkdm
756  * @clkdm: struct clockdomain *
757  *
758  * Allow the hardware to automatically switch the clockdomain @clkdm into
759  * active or idle states, as needed by downstream clocks.  If the
760  * clockdomain has any downstream clocks enabled in the clock
761  * framework, wkdep/sleepdep autodependencies are added; this is so
762  * device drivers can read and write to the device.  No return value.
763  */
764 void clkdm_allow_idle(struct clockdomain *clkdm)
765 {
766 	unsigned long flags;
767 
768 	if (!clkdm)
769 		return;
770 
771 	if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
772 		pr_debug("clock: automatic idle transitions cannot be enabled "
773 			 "on clockdomain %s\n", clkdm->name);
774 		return;
775 	}
776 
777 	if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
778 		return;
779 
780 	pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
781 		 clkdm->name);
782 
783 	spin_lock_irqsave(&clkdm->lock, flags);
784 	clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
785 	arch_clkdm->clkdm_allow_idle(clkdm);
786 	pwrdm_clkdm_state_switch(clkdm);
787 	spin_unlock_irqrestore(&clkdm->lock, flags);
788 }
789 
790 /**
791  * clkdm_deny_idle - disable hwsup idle transitions for clkdm
792  * @clkdm: struct clockdomain *
793  *
794  * Prevent the hardware from automatically switching the clockdomain
795  * @clkdm into inactive or idle states.  If the clockdomain has
796  * downstream clocks enabled in the clock framework, wkdep/sleepdep
797  * autodependencies are removed.  No return value.
798  */
799 void clkdm_deny_idle(struct clockdomain *clkdm)
800 {
801 	unsigned long flags;
802 
803 	if (!clkdm)
804 		return;
805 
806 	if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
807 		pr_debug("clockdomain: automatic idle transitions cannot be "
808 			 "disabled on %s\n", clkdm->name);
809 		return;
810 	}
811 
812 	if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
813 		return;
814 
815 	pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
816 		 clkdm->name);
817 
818 	spin_lock_irqsave(&clkdm->lock, flags);
819 	clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
820 	arch_clkdm->clkdm_deny_idle(clkdm);
821 	spin_unlock_irqrestore(&clkdm->lock, flags);
822 }
823 
824 /**
825  * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
826  * @clkdm: struct clockdomain *
827  *
828  * Returns true if clockdomain @clkdm currently has
829  * hardware-supervised idle enabled, or false if it does not or if
830  * @clkdm is NULL.  It is only valid to call this function after
831  * clkdm_init() has been called.  This function does not actually read
832  * bits from the hardware; it instead tests an in-memory flag that is
833  * changed whenever the clockdomain code changes the auto-idle mode.
834  */
835 bool clkdm_in_hwsup(struct clockdomain *clkdm)
836 {
837 	bool ret;
838 	unsigned long flags;
839 
840 	if (!clkdm)
841 		return false;
842 
843 	spin_lock_irqsave(&clkdm->lock, flags);
844 	ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
845 	spin_unlock_irqrestore(&clkdm->lock, flags);
846 
847 	return ret;
848 }
849 
850 /* Clockdomain-to-clock/hwmod framework interface code */
851 
852 static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
853 {
854 	unsigned long flags;
855 
856 	if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
857 		return -EINVAL;
858 
859 	/*
860 	 * For arch's with no autodeps, clkcm_clk_enable
861 	 * should be called for every clock instance or hwmod that is
862 	 * enabled, so the clkdm can be force woken up.
863 	 */
864 	if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
865 		return 0;
866 
867 	spin_lock_irqsave(&clkdm->lock, flags);
868 	arch_clkdm->clkdm_clk_enable(clkdm);
869 	pwrdm_wait_transition(clkdm->pwrdm.ptr);
870 	pwrdm_clkdm_state_switch(clkdm);
871 	spin_unlock_irqrestore(&clkdm->lock, flags);
872 
873 	pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
874 
875 	return 0;
876 }
877 
878 static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
879 {
880 	unsigned long flags;
881 
882 	if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
883 		return -EINVAL;
884 
885 	if (atomic_read(&clkdm->usecount) == 0) {
886 		WARN_ON(1); /* underflow */
887 		return -ERANGE;
888 	}
889 
890 	if (atomic_dec_return(&clkdm->usecount) > 0)
891 		return 0;
892 
893 	spin_lock_irqsave(&clkdm->lock, flags);
894 	arch_clkdm->clkdm_clk_disable(clkdm);
895 	pwrdm_clkdm_state_switch(clkdm);
896 	spin_unlock_irqrestore(&clkdm->lock, flags);
897 
898 	pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
899 
900 	return 0;
901 }
902 
903 /**
904  * clkdm_clk_enable - add an enabled downstream clock to this clkdm
905  * @clkdm: struct clockdomain *
906  * @clk: struct clk * of the enabled downstream clock
907  *
908  * Increment the usecount of the clockdomain @clkdm and ensure that it
909  * is awake before @clk is enabled.  Intended to be called by
910  * clk_enable() code.  If the clockdomain is in software-supervised
911  * idle mode, force the clockdomain to wake.  If the clockdomain is in
912  * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
913  * ensure that devices in the clockdomain can be read from/written to
914  * by on-chip processors.  Returns -EINVAL if passed null pointers;
915  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
916  */
917 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
918 {
919 	/*
920 	 * XXX Rewrite this code to maintain a list of enabled
921 	 * downstream clocks for debugging purposes?
922 	 */
923 
924 	if (!clk)
925 		return -EINVAL;
926 
927 	return _clkdm_clk_hwmod_enable(clkdm);
928 }
929 
930 /**
931  * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
932  * @clkdm: struct clockdomain *
933  * @clk: struct clk * of the disabled downstream clock
934  *
935  * Decrement the usecount of this clockdomain @clkdm when @clk is
936  * disabled.  Intended to be called by clk_disable() code.  If the
937  * clockdomain usecount goes to 0, put the clockdomain to sleep
938  * (software-supervised mode) or remove the clkdm autodependencies
939  * (hardware-supervised mode).  Returns -EINVAL if passed null
940  * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
941  * upon success or if the clockdomain is in hwsup idle mode.
942  */
943 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
944 {
945 	/*
946 	 * XXX Rewrite this code to maintain a list of enabled
947 	 * downstream clocks for debugging purposes?
948 	 */
949 
950 	if (!clk)
951 		return -EINVAL;
952 
953 	return _clkdm_clk_hwmod_disable(clkdm);
954 }
955 
956 /**
957  * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
958  * @clkdm: struct clockdomain *
959  * @oh: struct omap_hwmod * of the enabled downstream hwmod
960  *
961  * Increment the usecount of the clockdomain @clkdm and ensure that it
962  * is awake before @oh is enabled. Intended to be called by
963  * module_enable() code.
964  * If the clockdomain is in software-supervised idle mode, force the
965  * clockdomain to wake.  If the clockdomain is in hardware-supervised idle
966  * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
967  * clockdomain can be read from/written to by on-chip processors.
968  * Returns -EINVAL if passed null pointers;
969  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
970  */
971 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
972 {
973 	/* The clkdm attribute does not exist yet prior OMAP4 */
974 	if (cpu_is_omap24xx() || cpu_is_omap34xx())
975 		return 0;
976 
977 	/*
978 	 * XXX Rewrite this code to maintain a list of enabled
979 	 * downstream hwmods for debugging purposes?
980 	 */
981 
982 	if (!oh)
983 		return -EINVAL;
984 
985 	return _clkdm_clk_hwmod_enable(clkdm);
986 }
987 
988 /**
989  * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
990  * @clkdm: struct clockdomain *
991  * @oh: struct omap_hwmod * of the disabled downstream hwmod
992  *
993  * Decrement the usecount of this clockdomain @clkdm when @oh is
994  * disabled. Intended to be called by module_disable() code.
995  * If the clockdomain usecount goes to 0, put the clockdomain to sleep
996  * (software-supervised mode) or remove the clkdm autodependencies
997  * (hardware-supervised mode).
998  * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
999  * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1000  * idle mode.
1001  */
1002 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1003 {
1004 	/* The clkdm attribute does not exist yet prior OMAP4 */
1005 	if (cpu_is_omap24xx() || cpu_is_omap34xx())
1006 		return 0;
1007 
1008 	/*
1009 	 * XXX Rewrite this code to maintain a list of enabled
1010 	 * downstream hwmods for debugging purposes?
1011 	 */
1012 
1013 	if (!oh)
1014 		return -EINVAL;
1015 
1016 	return _clkdm_clk_hwmod_disable(clkdm);
1017 }
1018 
1019