blob: ad07689e15639ba1bee03c411c5c9ad173142673 [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>
Paul Gortmakerd44b28c2011-07-31 10:52:44 -040020#include <linux/string.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030021#include <linux/delay.h>
22#include <linux/clk.h>
23#include <linux/limits.h>
Paul Walmsley5b74c672009-02-03 02:10:03 -070024#include <linux/err.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030025
26#include <linux/io.h>
27
28#include <linux/bitops.h>
29
Paul Walmsley55ed9692010-01-26 20:12:59 -070030#include <plat/clock.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -070031#include "clockdomain.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030032
33/* clkdm_list contains all registered struct clockdomains */
34static LIST_HEAD(clkdm_list);
35
Paul Walmsley55ed9692010-01-26 20:12:59 -070036/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
37static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030038
Rajendra Nayak32d40342011-02-25 16:06:47 -070039static struct clkdm_ops *arch_clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030040
41/* Private functions */
42
Paul Walmsley55ed9692010-01-26 20:12:59 -070043static struct clockdomain *_clkdm_lookup(const char *name)
44{
45 struct clockdomain *clkdm, *temp_clkdm;
46
47 if (!name)
48 return NULL;
49
50 clkdm = NULL;
51
52 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
53 if (!strcmp(name, temp_clkdm->name)) {
54 clkdm = temp_clkdm;
55 break;
56 }
57 }
58
59 return clkdm;
60}
61
Paul Walmsleye909d62a82010-01-26 20:13:00 -070062/**
63 * _clkdm_register - register a clockdomain
64 * @clkdm: struct clockdomain * to register
65 *
66 * Adds a clockdomain to the internal clockdomain list.
67 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
68 * already registered by the provided name, or 0 upon success.
69 */
70static int _clkdm_register(struct clockdomain *clkdm)
71{
72 struct powerdomain *pwrdm;
73
74 if (!clkdm || !clkdm->name)
75 return -EINVAL;
76
Paul Walmsleye909d62a82010-01-26 20:13:00 -070077 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
78 if (!pwrdm) {
79 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
80 clkdm->name, clkdm->pwrdm.name);
81 return -EINVAL;
82 }
83 clkdm->pwrdm.ptr = pwrdm;
84
85 /* Verify that the clockdomain is not already registered */
86 if (_clkdm_lookup(clkdm->name))
87 return -EEXIST;
88
89 list_add(&clkdm->node, &clkdm_list);
90
91 pwrdm_add_clkdm(pwrdm, clkdm);
92
Rajendra Nayak555e74e2011-07-10 05:56:55 -060093 spin_lock_init(&clkdm->lock);
94
Paul Walmsleye909d62a82010-01-26 20:13:00 -070095 pr_debug("clockdomain: registered %s\n", clkdm->name);
96
97 return 0;
98}
99
Paul Walmsley55ed9692010-01-26 20:12:59 -0700100/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
101static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
102 struct clkdm_dep *deps)
103{
104 struct clkdm_dep *cd;
105
Paul Walmsleya5ffef62011-09-14 16:01:21 -0600106 if (!clkdm || !deps)
Paul Walmsley55ed9692010-01-26 20:12:59 -0700107 return ERR_PTR(-EINVAL);
108
109 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700110 if (!cd->clkdm && cd->clkdm_name)
111 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
112
113 if (cd->clkdm == clkdm)
114 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700115 }
116
117 if (!cd->clkdm_name)
118 return ERR_PTR(-ENOENT);
119
120 return cd;
121}
122
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300123/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700124 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
125 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300126 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700127 * Resolve autodep clockdomain names to clockdomain pointers via
128 * clkdm_lookup() and store the pointers in the autodep structure. An
129 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300130 * automatically added and removed whenever clocks in the associated
131 * clockdomain are enabled or disabled (respectively) when the
132 * clockdomain is in hardware-supervised mode. Meant to be called
133 * once at clockdomain layer initialization, since these should remain
134 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700135 *
136 * XXX autodeps are deprecated and should be removed at the earliest
137 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300138 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700139static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300140{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700141 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300142
143 if (!autodep)
144 return;
145
Paul Walmsley55ed9692010-01-26 20:12:59 -0700146 clkdm = clkdm_lookup(autodep->clkdm.name);
147 if (!clkdm) {
148 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
149 autodep->clkdm.name);
150 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300151 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700152 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300153}
154
155/*
156 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
157 * @clkdm: struct clockdomain *
158 *
159 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
160 * in hardware-supervised mode. Meant to be called from clock framework
161 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700162 *
163 * XXX autodeps are deprecated and should be removed at the earliest
164 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300165 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700166void _clkdm_add_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300167{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700168 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300169
Paul Walmsley570b54c2011-03-10 03:50:09 -0700170 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700171 return;
172
Paul Walmsley55ed9692010-01-26 20:12:59 -0700173 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
174 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300175 continue;
176
177 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700178 "clkdm %s\n", autodep->clkdm.ptr->name,
179 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300180
Paul Walmsley55ed9692010-01-26 20:12:59 -0700181 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
182 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300183 }
184}
185
186/*
187 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
188 * @clkdm: struct clockdomain *
189 *
190 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
191 * in hardware-supervised mode. Meant to be called from clock framework
192 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700193 *
194 * XXX autodeps are deprecated and should be removed at the earliest
195 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300196 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700197void _clkdm_del_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300198{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700199 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300200
Paul Walmsley570b54c2011-03-10 03:50:09 -0700201 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700202 return;
203
Paul Walmsley55ed9692010-01-26 20:12:59 -0700204 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
205 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300206 continue;
207
208 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700209 "clkdm %s\n", autodep->clkdm.ptr->name,
210 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300211
Paul Walmsley55ed9692010-01-26 20:12:59 -0700212 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
213 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300214 }
215}
216
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700217/**
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700218 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
219 * @clkdm: clockdomain that we are resolving dependencies for
220 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600221 *
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700222 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
223 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700224 * No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700225 */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700226static void _resolve_clkdm_deps(struct clockdomain *clkdm,
227 struct clkdm_dep *clkdm_deps)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600228{
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700229 struct clkdm_dep *cd;
230
231 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700232 if (cd->clkdm)
233 continue;
234 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
235
236 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
237 clkdm->name, cd->clkdm_name);
238 }
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600239}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300240
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300241/* Public functions */
242
243/**
Paul Walmsley08cb9702011-09-14 16:01:20 -0600244 * clkdm_register_platform_funcs - register clockdomain implementation fns
245 * @co: func pointers for arch specific implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300246 *
Paul Walmsley08cb9702011-09-14 16:01:20 -0600247 * Register the list of function pointers used to implement the
248 * clockdomain functions on different OMAP SoCs. Should be called
249 * before any other clkdm_register*() function. Returns -EINVAL if
250 * @co is null, -EEXIST if platform functions have already been
251 * registered, or 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300252 */
Paul Walmsley08cb9702011-09-14 16:01:20 -0600253int clkdm_register_platform_funcs(struct clkdm_ops *co)
254{
255 if (!co)
256 return -EINVAL;
257
258 if (arch_clkdm)
259 return -EEXIST;
260
261 arch_clkdm = co;
262
263 return 0;
264};
265
266/**
267 * clkdm_register_clkdms - register SoC clockdomains
268 * @cs: pointer to an array of struct clockdomain to register
269 *
270 * Register the clockdomains available on a particular OMAP SoC. Must
271 * be called after clkdm_register_platform_funcs(). May be called
272 * multiple times. Returns -EACCES if called before
273 * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
274 * null; or 0 upon success.
275 */
276int clkdm_register_clkdms(struct clockdomain **cs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300277{
278 struct clockdomain **c = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300279
Paul Walmsley08cb9702011-09-14 16:01:20 -0600280 if (!arch_clkdm)
281 return -EACCES;
Rajendra Nayak32d40342011-02-25 16:06:47 -0700282
Paul Walmsley08cb9702011-09-14 16:01:20 -0600283 if (!cs)
284 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300285
Paul Walmsley08cb9702011-09-14 16:01:20 -0600286 for (c = cs; *c; c++)
287 _clkdm_register(*c);
288
289 return 0;
290}
291
292/**
293 * clkdm_register_autodeps - register autodeps (if required)
294 * @ia: pointer to a static array of struct clkdm_autodep to register
295 *
296 * Register clockdomain "automatic dependencies." These are
297 * clockdomain wakeup and sleep dependencies that are automatically
298 * added whenever the first clock inside a clockdomain is enabled, and
299 * removed whenever the last clock inside a clockdomain is disabled.
300 * These are currently only used on OMAP3 devices, and are deprecated,
301 * since they waste energy. However, until the OMAP2/3 IP block
302 * enable/disable sequence can be converted to match the OMAP4
303 * sequence, they are needed.
304 *
305 * Must be called only after all of the SoC clockdomains are
306 * registered, since the function will resolve autodep clockdomain
307 * names into clockdomain pointers.
308 *
309 * The struct clkdm_autodep @ia array must be static, as this function
310 * does not copy the array elements.
311 *
312 * Returns -EACCES if called before any clockdomains have been
313 * registered, -EINVAL if called with a null @ia argument, -EEXIST if
314 * autodeps have already been registered, or 0 upon success.
315 */
316int clkdm_register_autodeps(struct clkdm_autodep *ia)
317{
318 struct clkdm_autodep *a = NULL;
319
320 if (list_empty(&clkdm_list))
321 return -EACCES;
322
323 if (!ia)
324 return -EINVAL;
325
Paul Walmsley55ed9692010-01-26 20:12:59 -0700326 if (autodeps)
Paul Walmsley08cb9702011-09-14 16:01:20 -0600327 return -EEXIST;
Paul Walmsley369d5612010-01-26 20:13:01 -0700328
Paul Walmsley08cb9702011-09-14 16:01:20 -0600329 autodeps = ia;
330 for (a = autodeps; a->clkdm.ptr; a++)
331 _autodep_lookup(a);
332
333 return 0;
334}
335
336/**
337 * clkdm_complete_init - set up the clockdomain layer
338 *
339 * Put all clockdomains into software-supervised mode; PM code should
340 * later enable hardware-supervised mode as appropriate. Must be
341 * called after clkdm_register_clkdms(). Returns -EACCES if called
342 * before clkdm_register_clkdms(), or 0 upon success.
343 */
344int clkdm_complete_init(void)
345{
346 struct clockdomain *clkdm;
347
348 if (list_empty(&clkdm_list))
349 return -EACCES;
350
Paul Walmsley369d5612010-01-26 20:13:01 -0700351 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600352 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700353 clkdm_wakeup(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600354 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700355 clkdm_deny_idle(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600356
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700357 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600358 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700359
360 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600361 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700362 }
Paul Walmsley08cb9702011-09-14 16:01:20 -0600363
364 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300365}
366
367/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300368 * clkdm_lookup - look up a clockdomain by name, return a pointer
369 * @name: name of clockdomain
370 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700371 * Find a registered clockdomain by its name @name. Returns a pointer
372 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300373 */
374struct clockdomain *clkdm_lookup(const char *name)
375{
376 struct clockdomain *clkdm, *temp_clkdm;
377
378 if (!name)
379 return NULL;
380
381 clkdm = NULL;
382
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300383 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
384 if (!strcmp(name, temp_clkdm->name)) {
385 clkdm = temp_clkdm;
386 break;
387 }
388 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300389
390 return clkdm;
391}
392
393/**
394 * clkdm_for_each - call function on each registered clockdomain
395 * @fn: callback function *
396 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700397 * Call the supplied function @fn for each registered clockdomain.
398 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300399 * out early from the iterator. The callback function is called with
400 * the clkdm_mutex held, so no clockdomain structure manipulation
401 * functions should be called from the callback, although hardware
402 * clockdomain control functions are fine. Returns the last return
403 * value of the callback function, which should be 0 for success or
404 * anything else to indicate failure; or -EINVAL if the function pointer
405 * is null.
406 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300407int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
408 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300409{
410 struct clockdomain *clkdm;
411 int ret = 0;
412
413 if (!fn)
414 return -EINVAL;
415
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300416 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300417 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300418 if (ret)
419 break;
420 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300421
422 return ret;
423}
424
425
Paul Walmsleye89087c2008-05-20 18:41:35 -0600426/**
427 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
428 * @clkdm: struct clockdomain *
429 *
430 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700431 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600432 */
433struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
434{
435 if (!clkdm)
436 return NULL;
437
Paul Walmsley5b74c672009-02-03 02:10:03 -0700438 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600439}
440
441
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300442/* Hardware clockdomain control */
443
444/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700445 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
446 * @clkdm1: wake this struct clockdomain * up (dependent)
447 * @clkdm2: when this struct clockdomain * wakes up (source)
448 *
449 * When the clockdomain represented by @clkdm2 wakes up, wake up
450 * @clkdm1. Implemented in hardware on the OMAP, this feature is
451 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
452 * Returns -EINVAL if presented with invalid clockdomain pointers,
453 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
454 * success.
455 */
456int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
457{
458 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700459 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700460
Paul Walmsley55ed9692010-01-26 20:12:59 -0700461 if (!clkdm1 || !clkdm2)
462 return -EINVAL;
463
464 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700465 if (IS_ERR(cd))
466 ret = PTR_ERR(cd);
467
468 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
469 ret = -EINVAL;
470
471 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700472 pr_debug("clockdomain: hardware cannot set/clear wake up of "
473 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700474 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700475 }
476
Paul Walmsley369d5612010-01-26 20:13:01 -0700477 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
478 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
479 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700480
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700481 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700482 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700483
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700484 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700485}
486
487/**
488 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
489 * @clkdm1: wake this struct clockdomain * up (dependent)
490 * @clkdm2: when this struct clockdomain * wakes up (source)
491 *
492 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
493 * wakes up. Returns -EINVAL if presented with invalid clockdomain
494 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
495 * 0 upon success.
496 */
497int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
498{
499 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700500 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700501
Paul Walmsley55ed9692010-01-26 20:12:59 -0700502 if (!clkdm1 || !clkdm2)
503 return -EINVAL;
504
505 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700506 if (IS_ERR(cd))
507 ret = PTR_ERR(cd);
508
509 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
510 ret = -EINVAL;
511
512 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700513 pr_debug("clockdomain: hardware cannot set/clear wake up of "
514 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700515 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700516 }
517
Paul Walmsley369d5612010-01-26 20:13:01 -0700518 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
519 pr_debug("clockdomain: hardware will no longer wake up %s "
520 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700521
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700522 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700523 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700524
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700525 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700526}
527
528/**
529 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
530 * @clkdm1: wake this struct clockdomain * up (dependent)
531 * @clkdm2: when this struct clockdomain * wakes up (source)
532 *
533 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
534 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
535 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
536 * is incapable.
537 *
538 * REVISIT: Currently this function only represents software-controllable
539 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
540 * yet handled here.
541 */
542int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
543{
544 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700545 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700546
547 if (!clkdm1 || !clkdm2)
548 return -EINVAL;
549
550 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700551 if (IS_ERR(cd))
552 ret = PTR_ERR(cd);
553
554 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
555 ret = -EINVAL;
556
557 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700558 pr_debug("clockdomain: hardware cannot set/clear wake up of "
559 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700560 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700561 }
562
Paul Walmsley369d5612010-01-26 20:13:01 -0700563 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700564 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700565}
566
567/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700568 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
569 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
570 *
571 * Remove all inter-clockdomain wakeup dependencies that could cause
572 * @clkdm to wake. Intended to be used during boot to initialize the
573 * PRCM to a known state, after all clockdomains are put into swsup idle
574 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
575 * 0 upon success.
576 */
577int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
578{
Paul Walmsley369d5612010-01-26 20:13:01 -0700579 if (!clkdm)
580 return -EINVAL;
581
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700582 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
583 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700584
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700585 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700586}
587
588/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700589 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
590 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
591 * @clkdm2: when this struct clockdomain * is active (source)
592 *
593 * Prevent @clkdm1 from automatically going inactive (and then to
594 * retention or off) if @clkdm2 is active. Returns -EINVAL if
595 * presented with invalid clockdomain pointers or called on a machine
596 * that does not support software-configurable hardware sleep
597 * dependencies, -ENOENT if the specified dependency cannot be set in
598 * hardware, or 0 upon success.
599 */
600int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
601{
602 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700603 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700604
605 if (!clkdm1 || !clkdm2)
606 return -EINVAL;
607
608 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700609 if (IS_ERR(cd))
610 ret = PTR_ERR(cd);
611
612 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
613 ret = -EINVAL;
614
615 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700616 pr_debug("clockdomain: hardware cannot set/clear sleep "
617 "dependency affecting %s from %s\n", clkdm1->name,
618 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700619 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700620 }
621
Paul Walmsley369d5612010-01-26 20:13:01 -0700622 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
623 pr_debug("clockdomain: will prevent %s from sleeping if %s "
624 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700625
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700626 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700627 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700628
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700629 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700630}
631
632/**
633 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
634 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
635 * @clkdm2: when this struct clockdomain * is active (source)
636 *
637 * Allow @clkdm1 to automatically go inactive (and then to retention or
638 * off), independent of the activity state of @clkdm2. Returns -EINVAL
639 * if presented with invalid clockdomain pointers or called on a machine
640 * that does not support software-configurable hardware sleep dependencies,
641 * -ENOENT if the specified dependency cannot be cleared in hardware, or
642 * 0 upon success.
643 */
644int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
645{
646 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700647 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700648
649 if (!clkdm1 || !clkdm2)
650 return -EINVAL;
651
652 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700653 if (IS_ERR(cd))
654 ret = PTR_ERR(cd);
655
656 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
657 ret = -EINVAL;
658
659 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700660 pr_debug("clockdomain: hardware cannot set/clear sleep "
661 "dependency affecting %s from %s\n", clkdm1->name,
662 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700663 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700664 }
665
Paul Walmsley369d5612010-01-26 20:13:01 -0700666 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
667 pr_debug("clockdomain: will no longer prevent %s from "
668 "sleeping if %s is active\n", clkdm1->name,
669 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700670
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700671 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700672 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700673
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700674 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700675}
676
677/**
678 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
679 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
680 * @clkdm2: when this struct clockdomain * is active (source)
681 *
682 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
683 * not be allowed to automatically go inactive if @clkdm2 is active;
684 * 0 if @clkdm1's automatic power state inactivity transition is independent
685 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
686 * on a machine that does not support software-configurable hardware sleep
687 * dependencies; or -ENOENT if the hardware is incapable.
688 *
689 * REVISIT: Currently this function only represents software-controllable
690 * sleep dependencies. Sleep dependencies fixed in hardware are not
691 * yet handled here.
692 */
693int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
694{
695 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700696 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700697
698 if (!clkdm1 || !clkdm2)
699 return -EINVAL;
700
701 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700702 if (IS_ERR(cd))
703 ret = PTR_ERR(cd);
704
705 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
706 ret = -EINVAL;
707
708 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700709 pr_debug("clockdomain: hardware cannot set/clear sleep "
710 "dependency affecting %s from %s\n", clkdm1->name,
711 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700712 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700713 }
714
Paul Walmsley369d5612010-01-26 20:13:01 -0700715 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700716 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700717}
718
Paul Walmsley369d5612010-01-26 20:13:01 -0700719/**
720 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
721 * @clkdm: struct clockdomain * to remove all sleep dependencies from
722 *
723 * Remove all inter-clockdomain sleep dependencies that could prevent
724 * @clkdm from idling. Intended to be used during boot to initialize the
725 * PRCM to a known state, after all clockdomains are put into swsup idle
726 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
727 * 0 upon success.
728 */
729int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
730{
Paul Walmsley369d5612010-01-26 20:13:01 -0700731 if (!clkdm)
732 return -EINVAL;
733
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700734 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
735 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700736
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700737 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700738}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700739
740/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700741 * clkdm_sleep - force clockdomain sleep transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300742 * @clkdm: struct clockdomain *
743 *
744 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700745 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300746 * clockdomain does not support software-initiated sleep; 0 upon
747 * success.
748 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700749int clkdm_sleep(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300750{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600751 int ret;
752 unsigned long flags;
753
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300754 if (!clkdm)
755 return -EINVAL;
756
757 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
758 pr_debug("clockdomain: %s does not support forcing "
759 "sleep via software\n", clkdm->name);
760 return -EINVAL;
761 }
762
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700763 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
764 return -EINVAL;
765
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300766 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
767
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600768 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600769 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600770 ret = arch_clkdm->clkdm_sleep(clkdm);
771 spin_unlock_irqrestore(&clkdm->lock, flags);
772 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300773}
774
775/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700776 * clkdm_wakeup - force clockdomain wakeup transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300777 * @clkdm: struct clockdomain *
778 *
779 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700780 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300781 * clockdomain does not support software-controlled wakeup; 0 upon
782 * success.
783 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700784int clkdm_wakeup(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300785{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600786 int ret;
787 unsigned long flags;
788
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300789 if (!clkdm)
790 return -EINVAL;
791
792 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
793 pr_debug("clockdomain: %s does not support forcing "
794 "wakeup via software\n", clkdm->name);
795 return -EINVAL;
796 }
797
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700798 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
799 return -EINVAL;
800
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300801 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
802
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600803 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600804 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600805 ret = arch_clkdm->clkdm_wakeup(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600806 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600807 spin_unlock_irqrestore(&clkdm->lock, flags);
808 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300809}
810
811/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700812 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300813 * @clkdm: struct clockdomain *
814 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700815 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300816 * active or idle states, as needed by downstream clocks. If the
817 * clockdomain has any downstream clocks enabled in the clock
818 * framework, wkdep/sleepdep autodependencies are added; this is so
819 * device drivers can read and write to the device. No return value.
820 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700821void clkdm_allow_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300822{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600823 unsigned long flags;
824
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300825 if (!clkdm)
826 return;
827
828 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
829 pr_debug("clock: automatic idle transitions cannot be enabled "
830 "on clockdomain %s\n", clkdm->name);
831 return;
832 }
833
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700834 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
835 return;
836
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300837 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
838 clkdm->name);
839
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600840 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600841 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700842 arch_clkdm->clkdm_allow_idle(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300843 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600844 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300845}
846
847/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700848 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300849 * @clkdm: struct clockdomain *
850 *
851 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700852 * @clkdm into inactive or idle states. If the clockdomain has
853 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300854 * autodependencies are removed. No return value.
855 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700856void clkdm_deny_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300857{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600858 unsigned long flags;
859
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300860 if (!clkdm)
861 return;
862
863 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
864 pr_debug("clockdomain: automatic idle transitions cannot be "
865 "disabled on %s\n", clkdm->name);
866 return;
867 }
868
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700869 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
870 return;
871
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300872 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
873 clkdm->name);
874
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600875 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600876 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700877 arch_clkdm->clkdm_deny_idle(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600878 pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600879 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300880}
881
Paul Walmsley32a363c2011-07-10 05:56:54 -0600882/**
883 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
884 * @clkdm: struct clockdomain *
885 *
886 * Returns true if clockdomain @clkdm currently has
887 * hardware-supervised idle enabled, or false if it does not or if
888 * @clkdm is NULL. It is only valid to call this function after
889 * clkdm_init() has been called. This function does not actually read
890 * bits from the hardware; it instead tests an in-memory flag that is
891 * changed whenever the clockdomain code changes the auto-idle mode.
892 */
893bool clkdm_in_hwsup(struct clockdomain *clkdm)
894{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600895 bool ret;
896 unsigned long flags;
897
Paul Walmsley32a363c2011-07-10 05:56:54 -0600898 if (!clkdm)
899 return false;
900
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600901 spin_lock_irqsave(&clkdm->lock, flags);
902 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
903 spin_unlock_irqrestore(&clkdm->lock, flags);
904
905 return ret;
Paul Walmsley32a363c2011-07-10 05:56:54 -0600906}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300907
Benoit Cousson113a7412011-07-10 05:56:54 -0600908/* Clockdomain-to-clock/hwmod framework interface code */
909
910static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
911{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600912 unsigned long flags;
913
Benoit Cousson113a7412011-07-10 05:56:54 -0600914 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
915 return -EINVAL;
916
917 /*
918 * For arch's with no autodeps, clkcm_clk_enable
919 * should be called for every clock instance or hwmod that is
920 * enabled, so the clkdm can be force woken up.
921 */
922 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
923 return 0;
924
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600925 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600926 arch_clkdm->clkdm_clk_enable(clkdm);
927 pwrdm_wait_transition(clkdm->pwrdm.ptr);
928 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600929 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600930
931 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
932
933 return 0;
934}
935
936static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
937{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600938 unsigned long flags;
939
Benoit Cousson113a7412011-07-10 05:56:54 -0600940 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
941 return -EINVAL;
942
943 if (atomic_read(&clkdm->usecount) == 0) {
944 WARN_ON(1); /* underflow */
945 return -ERANGE;
946 }
947
948 if (atomic_dec_return(&clkdm->usecount) > 0)
949 return 0;
950
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600951 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600952 arch_clkdm->clkdm_clk_disable(clkdm);
953 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600954 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600955
956 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
957
958 return 0;
959}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300960
961/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700962 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300963 * @clkdm: struct clockdomain *
964 * @clk: struct clk * of the enabled downstream clock
965 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700966 * Increment the usecount of the clockdomain @clkdm and ensure that it
967 * is awake before @clk is enabled. Intended to be called by
968 * clk_enable() code. If the clockdomain is in software-supervised
969 * idle mode, force the clockdomain to wake. If the clockdomain is in
970 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
971 * ensure that devices in the clockdomain can be read from/written to
972 * by on-chip processors. Returns -EINVAL if passed null pointers;
973 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300974 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700975int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300976{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300977 /*
978 * XXX Rewrite this code to maintain a list of enabled
979 * downstream clocks for debugging purposes?
980 */
981
Benoit Cousson113a7412011-07-10 05:56:54 -0600982 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300983 return -EINVAL;
984
Benoit Cousson113a7412011-07-10 05:56:54 -0600985 return _clkdm_clk_hwmod_enable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300986}
987
988/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700989 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300990 * @clkdm: struct clockdomain *
991 * @clk: struct clk * of the disabled downstream clock
992 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700993 * Decrement the usecount of this clockdomain @clkdm when @clk is
994 * disabled. Intended to be called by clk_disable() code. If the
995 * clockdomain usecount goes to 0, put the clockdomain to sleep
996 * (software-supervised mode) or remove the clkdm autodependencies
997 * (hardware-supervised mode). Returns -EINVAL if passed null
Benoit Cousson113a7412011-07-10 05:56:54 -0600998 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
999 * upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001000 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001001int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001002{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001003 /*
1004 * XXX Rewrite this code to maintain a list of enabled
1005 * downstream clocks for debugging purposes?
1006 */
1007
Benoit Cousson113a7412011-07-10 05:56:54 -06001008 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001009 return -EINVAL;
1010
Benoit Cousson113a7412011-07-10 05:56:54 -06001011 return _clkdm_clk_hwmod_disable(clkdm);
1012}
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001013
Benoit Cousson113a7412011-07-10 05:56:54 -06001014/**
1015 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1016 * @clkdm: struct clockdomain *
1017 * @oh: struct omap_hwmod * of the enabled downstream hwmod
1018 *
1019 * Increment the usecount of the clockdomain @clkdm and ensure that it
1020 * is awake before @oh is enabled. Intended to be called by
1021 * module_enable() code.
1022 * If the clockdomain is in software-supervised idle mode, force the
1023 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
1024 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1025 * clockdomain can be read from/written to by on-chip processors.
1026 * Returns -EINVAL if passed null pointers;
1027 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1028 */
1029int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1030{
1031 /* The clkdm attribute does not exist yet prior OMAP4 */
1032 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001033 return 0;
1034
Benoit Cousson113a7412011-07-10 05:56:54 -06001035 /*
1036 * XXX Rewrite this code to maintain a list of enabled
1037 * downstream hwmods for debugging purposes?
1038 */
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001039
Benoit Cousson113a7412011-07-10 05:56:54 -06001040 if (!oh)
1041 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001042
Benoit Cousson113a7412011-07-10 05:56:54 -06001043 return _clkdm_clk_hwmod_enable(clkdm);
1044}
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001045
Benoit Cousson113a7412011-07-10 05:56:54 -06001046/**
1047 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1048 * @clkdm: struct clockdomain *
1049 * @oh: struct omap_hwmod * of the disabled downstream hwmod
1050 *
1051 * Decrement the usecount of this clockdomain @clkdm when @oh is
1052 * disabled. Intended to be called by module_disable() code.
1053 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1054 * (software-supervised mode) or remove the clkdm autodependencies
1055 * (hardware-supervised mode).
1056 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1057 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1058 * idle mode.
1059 */
1060int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1061{
1062 /* The clkdm attribute does not exist yet prior OMAP4 */
1063 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1064 return 0;
1065
1066 /*
1067 * XXX Rewrite this code to maintain a list of enabled
1068 * downstream hwmods for debugging purposes?
1069 */
1070
1071 if (!oh)
1072 return -EINVAL;
1073
1074 return _clkdm_clk_hwmod_disable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001075}
1076