blob: 8480ee4344ea0ad1d12ef437735585efb25d34ea [file] [log] [blame]
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001/*
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07002 * OMAP2/3/4 clockdomain framework functions
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03003 *
Paul Walmsley32a363c2011-07-10 05:56:54 -06004 * Copyright (C) 2008-2011 Texas Instruments, Inc.
5 * Copyright (C) 2008-2011 Nokia Corporation
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03006 *
7 * Written by Paul Walmsley and Jouni Högander
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07008 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03009 *
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 */
Paul Walmsley33903eb2009-12-08 16:33:10 -070014#undef DEBUG
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030015
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030016#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>
Paul Walmsley5b74c672009-02-03 02:10:03 -070023#include <linux/err.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030024
25#include <linux/io.h>
26
27#include <linux/bitops.h>
28
Paul Walmsley55ed9692010-01-26 20:12:59 -070029#include <plat/clock.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -070030#include "clockdomain.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030031
32/* clkdm_list contains all registered struct clockdomains */
33static LIST_HEAD(clkdm_list);
34
Paul Walmsley55ed9692010-01-26 20:12:59 -070035/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
36static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030037
Rajendra Nayak32d40342011-02-25 16:06:47 -070038static struct clkdm_ops *arch_clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030039
40/* Private functions */
41
Paul Walmsley55ed9692010-01-26 20:12:59 -070042static 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
Paul Walmsleye909d62a82010-01-26 20:13:00 -070061/**
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
Paul Walmsleye909d62a82010-01-26 20:13:00 -070076 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
77 if (!pwrdm) {
78 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
79 clkdm->name, clkdm->pwrdm.name);
80 return -EINVAL;
81 }
82 clkdm->pwrdm.ptr = pwrdm;
83
84 /* Verify that the clockdomain is not already registered */
85 if (_clkdm_lookup(clkdm->name))
86 return -EEXIST;
87
88 list_add(&clkdm->node, &clkdm_list);
89
90 pwrdm_add_clkdm(pwrdm, clkdm);
91
Rajendra Nayak555e74e2011-07-10 05:56:55 -060092 spin_lock_init(&clkdm->lock);
93
Paul Walmsleye909d62a82010-01-26 20:13:00 -070094 pr_debug("clockdomain: registered %s\n", clkdm->name);
95
96 return 0;
97}
98
Paul Walmsley55ed9692010-01-26 20:12:59 -070099/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
100static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
101 struct clkdm_dep *deps)
102{
103 struct clkdm_dep *cd;
104
Paul Walmsleya5ffef62011-09-14 16:01:21 -0600105 if (!clkdm || !deps)
Paul Walmsley55ed9692010-01-26 20:12:59 -0700106 return ERR_PTR(-EINVAL);
107
108 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700109 if (!cd->clkdm && cd->clkdm_name)
110 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
111
112 if (cd->clkdm == clkdm)
113 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700114 }
115
116 if (!cd->clkdm_name)
117 return ERR_PTR(-ENOENT);
118
119 return cd;
120}
121
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300122/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700123 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
124 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300125 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700126 * Resolve autodep clockdomain names to clockdomain pointers via
127 * clkdm_lookup() and store the pointers in the autodep structure. An
128 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300129 * automatically added and removed whenever clocks in the associated
130 * clockdomain are enabled or disabled (respectively) when the
131 * clockdomain is in hardware-supervised mode. Meant to be called
132 * once at clockdomain layer initialization, since these should remain
133 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700134 *
135 * XXX autodeps are deprecated and should be removed at the earliest
136 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300137 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700138static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300139{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700140 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300141
142 if (!autodep)
143 return;
144
Paul Walmsley55ed9692010-01-26 20:12:59 -0700145 clkdm = clkdm_lookup(autodep->clkdm.name);
146 if (!clkdm) {
147 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
148 autodep->clkdm.name);
149 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300150 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700151 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300152}
153
154/*
155 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
156 * @clkdm: struct clockdomain *
157 *
158 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
159 * in hardware-supervised mode. Meant to be called from clock framework
160 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700161 *
162 * XXX autodeps are deprecated and should be removed at the earliest
163 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300164 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700165void _clkdm_add_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300166{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700167 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300168
Paul Walmsley570b54c2011-03-10 03:50:09 -0700169 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700170 return;
171
Paul Walmsley55ed9692010-01-26 20:12:59 -0700172 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
173 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300174 continue;
175
176 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700177 "clkdm %s\n", autodep->clkdm.ptr->name,
178 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300179
Paul Walmsley55ed9692010-01-26 20:12:59 -0700180 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
181 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300182 }
183}
184
185/*
186 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
187 * @clkdm: struct clockdomain *
188 *
189 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
190 * in hardware-supervised mode. Meant to be called from clock framework
191 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700192 *
193 * XXX autodeps are deprecated and should be removed at the earliest
194 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300195 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700196void _clkdm_del_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300197{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700198 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300199
Paul Walmsley570b54c2011-03-10 03:50:09 -0700200 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700201 return;
202
Paul Walmsley55ed9692010-01-26 20:12:59 -0700203 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
204 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300205 continue;
206
207 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700208 "clkdm %s\n", autodep->clkdm.ptr->name,
209 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300210
Paul Walmsley55ed9692010-01-26 20:12:59 -0700211 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
212 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300213 }
214}
215
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700216/**
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700217 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
218 * @clkdm: clockdomain that we are resolving dependencies for
219 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600220 *
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700221 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
222 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700223 * No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700224 */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700225static void _resolve_clkdm_deps(struct clockdomain *clkdm,
226 struct clkdm_dep *clkdm_deps)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600227{
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700228 struct clkdm_dep *cd;
229
230 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700231 if (cd->clkdm)
232 continue;
233 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
234
235 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
236 clkdm->name, cd->clkdm_name);
237 }
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600238}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300239
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300240/* Public functions */
241
242/**
Paul Walmsley08cb9702011-09-14 16:01:20 -0600243 * clkdm_register_platform_funcs - register clockdomain implementation fns
244 * @co: func pointers for arch specific implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300245 *
Paul Walmsley08cb9702011-09-14 16:01:20 -0600246 * Register the list of function pointers used to implement the
247 * clockdomain functions on different OMAP SoCs. Should be called
248 * before any other clkdm_register*() function. Returns -EINVAL if
249 * @co is null, -EEXIST if platform functions have already been
250 * registered, or 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300251 */
Paul Walmsley08cb9702011-09-14 16:01:20 -0600252int clkdm_register_platform_funcs(struct clkdm_ops *co)
253{
254 if (!co)
255 return -EINVAL;
256
257 if (arch_clkdm)
258 return -EEXIST;
259
260 arch_clkdm = co;
261
262 return 0;
263};
264
265/**
266 * clkdm_register_clkdms - register SoC clockdomains
267 * @cs: pointer to an array of struct clockdomain to register
268 *
269 * Register the clockdomains available on a particular OMAP SoC. Must
270 * be called after clkdm_register_platform_funcs(). May be called
271 * multiple times. Returns -EACCES if called before
272 * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
273 * null; or 0 upon success.
274 */
275int clkdm_register_clkdms(struct clockdomain **cs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300276{
277 struct clockdomain **c = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300278
Paul Walmsley08cb9702011-09-14 16:01:20 -0600279 if (!arch_clkdm)
280 return -EACCES;
Rajendra Nayak32d40342011-02-25 16:06:47 -0700281
Paul Walmsley08cb9702011-09-14 16:01:20 -0600282 if (!cs)
283 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300284
Paul Walmsley08cb9702011-09-14 16:01:20 -0600285 for (c = cs; *c; c++)
286 _clkdm_register(*c);
287
288 return 0;
289}
290
291/**
292 * clkdm_register_autodeps - register autodeps (if required)
293 * @ia: pointer to a static array of struct clkdm_autodep to register
294 *
295 * Register clockdomain "automatic dependencies." These are
296 * clockdomain wakeup and sleep dependencies that are automatically
297 * added whenever the first clock inside a clockdomain is enabled, and
298 * removed whenever the last clock inside a clockdomain is disabled.
299 * These are currently only used on OMAP3 devices, and are deprecated,
300 * since they waste energy. However, until the OMAP2/3 IP block
301 * enable/disable sequence can be converted to match the OMAP4
302 * sequence, they are needed.
303 *
304 * Must be called only after all of the SoC clockdomains are
305 * registered, since the function will resolve autodep clockdomain
306 * names into clockdomain pointers.
307 *
308 * The struct clkdm_autodep @ia array must be static, as this function
309 * does not copy the array elements.
310 *
311 * Returns -EACCES if called before any clockdomains have been
312 * registered, -EINVAL if called with a null @ia argument, -EEXIST if
313 * autodeps have already been registered, or 0 upon success.
314 */
315int clkdm_register_autodeps(struct clkdm_autodep *ia)
316{
317 struct clkdm_autodep *a = NULL;
318
319 if (list_empty(&clkdm_list))
320 return -EACCES;
321
322 if (!ia)
323 return -EINVAL;
324
Paul Walmsley55ed9692010-01-26 20:12:59 -0700325 if (autodeps)
Paul Walmsley08cb9702011-09-14 16:01:20 -0600326 return -EEXIST;
Paul Walmsley369d5612010-01-26 20:13:01 -0700327
Paul Walmsley08cb9702011-09-14 16:01:20 -0600328 autodeps = ia;
329 for (a = autodeps; a->clkdm.ptr; a++)
330 _autodep_lookup(a);
331
332 return 0;
333}
334
335/**
336 * clkdm_complete_init - set up the clockdomain layer
337 *
338 * Put all clockdomains into software-supervised mode; PM code should
339 * later enable hardware-supervised mode as appropriate. Must be
340 * called after clkdm_register_clkdms(). Returns -EACCES if called
341 * before clkdm_register_clkdms(), or 0 upon success.
342 */
343int clkdm_complete_init(void)
344{
345 struct clockdomain *clkdm;
346
347 if (list_empty(&clkdm_list))
348 return -EACCES;
349
Paul Walmsley369d5612010-01-26 20:13:01 -0700350 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600351 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700352 clkdm_wakeup(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600353 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700354 clkdm_deny_idle(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600355
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700356 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600357 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700358
359 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600360 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700361 }
Paul Walmsley08cb9702011-09-14 16:01:20 -0600362
363 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300364}
365
366/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300367 * clkdm_lookup - look up a clockdomain by name, return a pointer
368 * @name: name of clockdomain
369 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700370 * Find a registered clockdomain by its name @name. Returns a pointer
371 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300372 */
373struct clockdomain *clkdm_lookup(const char *name)
374{
375 struct clockdomain *clkdm, *temp_clkdm;
376
377 if (!name)
378 return NULL;
379
380 clkdm = NULL;
381
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300382 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
383 if (!strcmp(name, temp_clkdm->name)) {
384 clkdm = temp_clkdm;
385 break;
386 }
387 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300388
389 return clkdm;
390}
391
392/**
393 * clkdm_for_each - call function on each registered clockdomain
394 * @fn: callback function *
395 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700396 * Call the supplied function @fn for each registered clockdomain.
397 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300398 * out early from the iterator. The callback function is called with
399 * the clkdm_mutex held, so no clockdomain structure manipulation
400 * functions should be called from the callback, although hardware
401 * clockdomain control functions are fine. Returns the last return
402 * value of the callback function, which should be 0 for success or
403 * anything else to indicate failure; or -EINVAL if the function pointer
404 * is null.
405 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300406int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
407 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300408{
409 struct clockdomain *clkdm;
410 int ret = 0;
411
412 if (!fn)
413 return -EINVAL;
414
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300415 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300416 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300417 if (ret)
418 break;
419 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300420
421 return ret;
422}
423
424
Paul Walmsleye89087c2008-05-20 18:41:35 -0600425/**
426 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
427 * @clkdm: struct clockdomain *
428 *
429 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700430 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600431 */
432struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
433{
434 if (!clkdm)
435 return NULL;
436
Paul Walmsley5b74c672009-02-03 02:10:03 -0700437 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600438}
439
440
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300441/* Hardware clockdomain control */
442
443/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700444 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
445 * @clkdm1: wake this struct clockdomain * up (dependent)
446 * @clkdm2: when this struct clockdomain * wakes up (source)
447 *
448 * When the clockdomain represented by @clkdm2 wakes up, wake up
449 * @clkdm1. Implemented in hardware on the OMAP, this feature is
450 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
451 * Returns -EINVAL if presented with invalid clockdomain pointers,
452 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
453 * success.
454 */
455int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
456{
457 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700458 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700459
Paul Walmsley55ed9692010-01-26 20:12:59 -0700460 if (!clkdm1 || !clkdm2)
461 return -EINVAL;
462
463 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700464 if (IS_ERR(cd))
465 ret = PTR_ERR(cd);
466
467 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
468 ret = -EINVAL;
469
470 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700471 pr_debug("clockdomain: hardware cannot set/clear wake up of "
472 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700473 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700474 }
475
Paul Walmsley369d5612010-01-26 20:13:01 -0700476 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
477 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
478 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700479
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700480 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700481 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700482
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700483 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700484}
485
486/**
487 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
488 * @clkdm1: wake this struct clockdomain * up (dependent)
489 * @clkdm2: when this struct clockdomain * wakes up (source)
490 *
491 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
492 * wakes up. Returns -EINVAL if presented with invalid clockdomain
493 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
494 * 0 upon success.
495 */
496int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
497{
498 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700499 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700500
Paul Walmsley55ed9692010-01-26 20:12:59 -0700501 if (!clkdm1 || !clkdm2)
502 return -EINVAL;
503
504 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700505 if (IS_ERR(cd))
506 ret = PTR_ERR(cd);
507
508 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
509 ret = -EINVAL;
510
511 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700512 pr_debug("clockdomain: hardware cannot set/clear wake up of "
513 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700514 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700515 }
516
Paul Walmsley369d5612010-01-26 20:13:01 -0700517 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
518 pr_debug("clockdomain: hardware will no longer wake up %s "
519 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700520
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700521 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700522 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700523
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700524 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700525}
526
527/**
528 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
529 * @clkdm1: wake this struct clockdomain * up (dependent)
530 * @clkdm2: when this struct clockdomain * wakes up (source)
531 *
532 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
533 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
534 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
535 * is incapable.
536 *
537 * REVISIT: Currently this function only represents software-controllable
538 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
539 * yet handled here.
540 */
541int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
542{
543 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700544 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700545
546 if (!clkdm1 || !clkdm2)
547 return -EINVAL;
548
549 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700550 if (IS_ERR(cd))
551 ret = PTR_ERR(cd);
552
553 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
554 ret = -EINVAL;
555
556 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700557 pr_debug("clockdomain: hardware cannot set/clear wake up of "
558 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700559 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700560 }
561
Paul Walmsley369d5612010-01-26 20:13:01 -0700562 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700563 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700564}
565
566/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700567 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
568 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
569 *
570 * Remove all inter-clockdomain wakeup dependencies that could cause
571 * @clkdm to wake. Intended to be used during boot to initialize the
572 * PRCM to a known state, after all clockdomains are put into swsup idle
573 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
574 * 0 upon success.
575 */
576int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
577{
Paul Walmsley369d5612010-01-26 20:13:01 -0700578 if (!clkdm)
579 return -EINVAL;
580
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700581 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
582 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700583
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700584 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700585}
586
587/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700588 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
589 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
590 * @clkdm2: when this struct clockdomain * is active (source)
591 *
592 * Prevent @clkdm1 from automatically going inactive (and then to
593 * retention or off) if @clkdm2 is active. Returns -EINVAL if
594 * presented with invalid clockdomain pointers or called on a machine
595 * that does not support software-configurable hardware sleep
596 * dependencies, -ENOENT if the specified dependency cannot be set in
597 * hardware, or 0 upon success.
598 */
599int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
600{
601 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700602 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700603
604 if (!clkdm1 || !clkdm2)
605 return -EINVAL;
606
607 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700608 if (IS_ERR(cd))
609 ret = PTR_ERR(cd);
610
611 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
612 ret = -EINVAL;
613
614 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700615 pr_debug("clockdomain: hardware cannot set/clear sleep "
616 "dependency affecting %s from %s\n", clkdm1->name,
617 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700618 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700619 }
620
Paul Walmsley369d5612010-01-26 20:13:01 -0700621 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
622 pr_debug("clockdomain: will prevent %s from sleeping if %s "
623 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700624
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700625 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700626 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700627
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700628 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700629}
630
631/**
632 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
633 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
634 * @clkdm2: when this struct clockdomain * is active (source)
635 *
636 * Allow @clkdm1 to automatically go inactive (and then to retention or
637 * off), independent of the activity state of @clkdm2. Returns -EINVAL
638 * if presented with invalid clockdomain pointers or called on a machine
639 * that does not support software-configurable hardware sleep dependencies,
640 * -ENOENT if the specified dependency cannot be cleared in hardware, or
641 * 0 upon success.
642 */
643int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
644{
645 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700646 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700647
648 if (!clkdm1 || !clkdm2)
649 return -EINVAL;
650
651 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700652 if (IS_ERR(cd))
653 ret = PTR_ERR(cd);
654
655 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
656 ret = -EINVAL;
657
658 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700659 pr_debug("clockdomain: hardware cannot set/clear sleep "
660 "dependency affecting %s from %s\n", clkdm1->name,
661 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700662 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700663 }
664
Paul Walmsley369d5612010-01-26 20:13:01 -0700665 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
666 pr_debug("clockdomain: will no longer prevent %s from "
667 "sleeping if %s is active\n", clkdm1->name,
668 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700669
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700670 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700671 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700672
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700673 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700674}
675
676/**
677 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
678 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
679 * @clkdm2: when this struct clockdomain * is active (source)
680 *
681 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
682 * not be allowed to automatically go inactive if @clkdm2 is active;
683 * 0 if @clkdm1's automatic power state inactivity transition is independent
684 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
685 * on a machine that does not support software-configurable hardware sleep
686 * dependencies; or -ENOENT if the hardware is incapable.
687 *
688 * REVISIT: Currently this function only represents software-controllable
689 * sleep dependencies. Sleep dependencies fixed in hardware are not
690 * yet handled here.
691 */
692int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
693{
694 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700695 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700696
697 if (!clkdm1 || !clkdm2)
698 return -EINVAL;
699
700 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700701 if (IS_ERR(cd))
702 ret = PTR_ERR(cd);
703
704 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
705 ret = -EINVAL;
706
707 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700708 pr_debug("clockdomain: hardware cannot set/clear sleep "
709 "dependency affecting %s from %s\n", clkdm1->name,
710 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700711 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700712 }
713
Paul Walmsley369d5612010-01-26 20:13:01 -0700714 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700715 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700716}
717
Paul Walmsley369d5612010-01-26 20:13:01 -0700718/**
719 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
720 * @clkdm: struct clockdomain * to remove all sleep dependencies from
721 *
722 * Remove all inter-clockdomain sleep dependencies that could prevent
723 * @clkdm from idling. Intended to be used during boot to initialize the
724 * PRCM to a known state, after all clockdomains are put into swsup idle
725 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
726 * 0 upon success.
727 */
728int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
729{
Paul Walmsley369d5612010-01-26 20:13:01 -0700730 if (!clkdm)
731 return -EINVAL;
732
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700733 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
734 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700735
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700736 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700737}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700738
739/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700740 * clkdm_sleep - force clockdomain sleep transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300741 * @clkdm: struct clockdomain *
742 *
743 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700744 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300745 * clockdomain does not support software-initiated sleep; 0 upon
746 * success.
747 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700748int clkdm_sleep(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300749{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600750 int ret;
751 unsigned long flags;
752
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300753 if (!clkdm)
754 return -EINVAL;
755
756 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
757 pr_debug("clockdomain: %s does not support forcing "
758 "sleep via software\n", clkdm->name);
759 return -EINVAL;
760 }
761
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700762 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
763 return -EINVAL;
764
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300765 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
766
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600767 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600768 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600769 ret = arch_clkdm->clkdm_sleep(clkdm);
770 spin_unlock_irqrestore(&clkdm->lock, flags);
771 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300772}
773
774/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700775 * clkdm_wakeup - force clockdomain wakeup transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300776 * @clkdm: struct clockdomain *
777 *
778 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700779 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300780 * clockdomain does not support software-controlled wakeup; 0 upon
781 * success.
782 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700783int clkdm_wakeup(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300784{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600785 int ret;
786 unsigned long flags;
787
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300788 if (!clkdm)
789 return -EINVAL;
790
791 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
792 pr_debug("clockdomain: %s does not support forcing "
793 "wakeup via software\n", clkdm->name);
794 return -EINVAL;
795 }
796
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700797 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
798 return -EINVAL;
799
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300800 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
801
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600802 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600803 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600804 ret = arch_clkdm->clkdm_wakeup(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600805 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600806 spin_unlock_irqrestore(&clkdm->lock, flags);
807 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300808}
809
810/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700811 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300812 * @clkdm: struct clockdomain *
813 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700814 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300815 * active or idle states, as needed by downstream clocks. If the
816 * clockdomain has any downstream clocks enabled in the clock
817 * framework, wkdep/sleepdep autodependencies are added; this is so
818 * device drivers can read and write to the device. No return value.
819 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700820void clkdm_allow_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300821{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600822 unsigned long flags;
823
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300824 if (!clkdm)
825 return;
826
827 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
828 pr_debug("clock: automatic idle transitions cannot be enabled "
829 "on clockdomain %s\n", clkdm->name);
830 return;
831 }
832
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700833 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
834 return;
835
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300836 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
837 clkdm->name);
838
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600839 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600840 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700841 arch_clkdm->clkdm_allow_idle(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300842 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600843 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300844}
845
846/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700847 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300848 * @clkdm: struct clockdomain *
849 *
850 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700851 * @clkdm into inactive or idle states. If the clockdomain has
852 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300853 * autodependencies are removed. No return value.
854 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700855void clkdm_deny_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300856{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600857 unsigned long flags;
858
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300859 if (!clkdm)
860 return;
861
862 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
863 pr_debug("clockdomain: automatic idle transitions cannot be "
864 "disabled on %s\n", clkdm->name);
865 return;
866 }
867
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700868 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
869 return;
870
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300871 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
872 clkdm->name);
873
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600874 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600875 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700876 arch_clkdm->clkdm_deny_idle(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600877 pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600878 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300879}
880
Paul Walmsley32a363c2011-07-10 05:56:54 -0600881/**
882 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
883 * @clkdm: struct clockdomain *
884 *
885 * Returns true if clockdomain @clkdm currently has
886 * hardware-supervised idle enabled, or false if it does not or if
887 * @clkdm is NULL. It is only valid to call this function after
888 * clkdm_init() has been called. This function does not actually read
889 * bits from the hardware; it instead tests an in-memory flag that is
890 * changed whenever the clockdomain code changes the auto-idle mode.
891 */
892bool clkdm_in_hwsup(struct clockdomain *clkdm)
893{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600894 bool ret;
895 unsigned long flags;
896
Paul Walmsley32a363c2011-07-10 05:56:54 -0600897 if (!clkdm)
898 return false;
899
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600900 spin_lock_irqsave(&clkdm->lock, flags);
901 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
902 spin_unlock_irqrestore(&clkdm->lock, flags);
903
904 return ret;
Paul Walmsley32a363c2011-07-10 05:56:54 -0600905}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300906
Benoit Cousson113a7412011-07-10 05:56:54 -0600907/* Clockdomain-to-clock/hwmod framework interface code */
908
909static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
910{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600911 unsigned long flags;
912
Benoit Cousson113a7412011-07-10 05:56:54 -0600913 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
914 return -EINVAL;
915
916 /*
917 * For arch's with no autodeps, clkcm_clk_enable
918 * should be called for every clock instance or hwmod that is
919 * enabled, so the clkdm can be force woken up.
920 */
921 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
922 return 0;
923
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600924 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600925 arch_clkdm->clkdm_clk_enable(clkdm);
926 pwrdm_wait_transition(clkdm->pwrdm.ptr);
927 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600928 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600929
930 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
931
932 return 0;
933}
934
935static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
936{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600937 unsigned long flags;
938
Benoit Cousson113a7412011-07-10 05:56:54 -0600939 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
940 return -EINVAL;
941
942 if (atomic_read(&clkdm->usecount) == 0) {
943 WARN_ON(1); /* underflow */
944 return -ERANGE;
945 }
946
947 if (atomic_dec_return(&clkdm->usecount) > 0)
948 return 0;
949
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600950 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600951 arch_clkdm->clkdm_clk_disable(clkdm);
952 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600953 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600954
955 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
956
957 return 0;
958}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300959
960/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700961 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300962 * @clkdm: struct clockdomain *
963 * @clk: struct clk * of the enabled downstream clock
964 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700965 * Increment the usecount of the clockdomain @clkdm and ensure that it
966 * is awake before @clk is enabled. Intended to be called by
967 * clk_enable() code. If the clockdomain is in software-supervised
968 * idle mode, force the clockdomain to wake. If the clockdomain is in
969 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
970 * ensure that devices in the clockdomain can be read from/written to
971 * by on-chip processors. Returns -EINVAL if passed null pointers;
972 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300973 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700974int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300975{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300976 /*
977 * XXX Rewrite this code to maintain a list of enabled
978 * downstream clocks for debugging purposes?
979 */
980
Benoit Cousson113a7412011-07-10 05:56:54 -0600981 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300982 return -EINVAL;
983
Benoit Cousson113a7412011-07-10 05:56:54 -0600984 return _clkdm_clk_hwmod_enable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300985}
986
987/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700988 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300989 * @clkdm: struct clockdomain *
990 * @clk: struct clk * of the disabled downstream clock
991 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700992 * Decrement the usecount of this clockdomain @clkdm when @clk is
993 * disabled. Intended to be called by clk_disable() code. If the
994 * clockdomain usecount goes to 0, put the clockdomain to sleep
995 * (software-supervised mode) or remove the clkdm autodependencies
996 * (hardware-supervised mode). Returns -EINVAL if passed null
Benoit Cousson113a7412011-07-10 05:56:54 -0600997 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
998 * upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300999 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001000int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001001{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001002 /*
1003 * XXX Rewrite this code to maintain a list of enabled
1004 * downstream clocks for debugging purposes?
1005 */
1006
Benoit Cousson113a7412011-07-10 05:56:54 -06001007 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001008 return -EINVAL;
1009
Benoit Cousson113a7412011-07-10 05:56:54 -06001010 return _clkdm_clk_hwmod_disable(clkdm);
1011}
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001012
Benoit Cousson113a7412011-07-10 05:56:54 -06001013/**
1014 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1015 * @clkdm: struct clockdomain *
1016 * @oh: struct omap_hwmod * of the enabled downstream hwmod
1017 *
1018 * Increment the usecount of the clockdomain @clkdm and ensure that it
1019 * is awake before @oh is enabled. Intended to be called by
1020 * module_enable() code.
1021 * If the clockdomain is in software-supervised idle mode, force the
1022 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
1023 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1024 * clockdomain can be read from/written to by on-chip processors.
1025 * Returns -EINVAL if passed null pointers;
1026 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1027 */
1028int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1029{
1030 /* The clkdm attribute does not exist yet prior OMAP4 */
1031 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001032 return 0;
1033
Benoit Cousson113a7412011-07-10 05:56:54 -06001034 /*
1035 * XXX Rewrite this code to maintain a list of enabled
1036 * downstream hwmods for debugging purposes?
1037 */
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001038
Benoit Cousson113a7412011-07-10 05:56:54 -06001039 if (!oh)
1040 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001041
Benoit Cousson113a7412011-07-10 05:56:54 -06001042 return _clkdm_clk_hwmod_enable(clkdm);
1043}
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001044
Benoit Cousson113a7412011-07-10 05:56:54 -06001045/**
1046 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1047 * @clkdm: struct clockdomain *
1048 * @oh: struct omap_hwmod * of the disabled downstream hwmod
1049 *
1050 * Decrement the usecount of this clockdomain @clkdm when @oh is
1051 * disabled. Intended to be called by module_disable() code.
1052 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1053 * (software-supervised mode) or remove the clkdm autodependencies
1054 * (hardware-supervised mode).
1055 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1056 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1057 * idle mode.
1058 */
1059int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1060{
1061 /* The clkdm attribute does not exist yet prior OMAP4 */
1062 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1063 return 0;
1064
1065 /*
1066 * XXX Rewrite this code to maintain a list of enabled
1067 * downstream hwmods for debugging purposes?
1068 */
1069
1070 if (!oh)
1071 return -EINVAL;
1072
1073 return _clkdm_clk_hwmod_disable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001074}
1075