blob: 275bdfbb6fc541e7ccba3fc7324e90590fcec48b [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>
Mike Turquetted043d872012-11-09 11:28:42 -070025#include <linux/clk-provider.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030026
27#include <linux/io.h>
28
29#include <linux/bitops.h>
30
Tony Lindgrene4c060d2012-10-05 13:25:59 -070031#include "soc.h"
Paul Walmsleya135eaa2012-09-27 10:33:34 -060032#include "clock.h"
Paul Walmsley1540f2142010-12-21 21:05:15 -070033#include "clockdomain.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030034
35/* clkdm_list contains all registered struct clockdomains */
36static LIST_HEAD(clkdm_list);
37
Paul Walmsley55ed9692010-01-26 20:12:59 -070038/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
39static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030040
Rajendra Nayak32d40342011-02-25 16:06:47 -070041static struct clkdm_ops *arch_clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030042
43/* Private functions */
44
Paul Walmsley55ed9692010-01-26 20:12:59 -070045static struct clockdomain *_clkdm_lookup(const char *name)
46{
47 struct clockdomain *clkdm, *temp_clkdm;
48
49 if (!name)
50 return NULL;
51
52 clkdm = NULL;
53
54 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
55 if (!strcmp(name, temp_clkdm->name)) {
56 clkdm = temp_clkdm;
57 break;
58 }
59 }
60
61 return clkdm;
62}
63
Paul Walmsleye909d62a82010-01-26 20:13:00 -070064/**
65 * _clkdm_register - register a clockdomain
66 * @clkdm: struct clockdomain * to register
67 *
68 * Adds a clockdomain to the internal clockdomain list.
69 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
70 * already registered by the provided name, or 0 upon success.
71 */
72static int _clkdm_register(struct clockdomain *clkdm)
73{
74 struct powerdomain *pwrdm;
75
76 if (!clkdm || !clkdm->name)
77 return -EINVAL;
78
Paul Walmsleye909d62a82010-01-26 20:13:00 -070079 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
80 if (!pwrdm) {
81 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
82 clkdm->name, clkdm->pwrdm.name);
83 return -EINVAL;
84 }
85 clkdm->pwrdm.ptr = pwrdm;
86
87 /* Verify that the clockdomain is not already registered */
88 if (_clkdm_lookup(clkdm->name))
89 return -EEXIST;
90
91 list_add(&clkdm->node, &clkdm_list);
92
93 pwrdm_add_clkdm(pwrdm, clkdm);
94
95 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
Paul Walmsley7852ec02012-07-26 00:54:26 -0600177 pr_debug("clockdomain: %s: adding %s sleepdep/wkdep\n",
178 clkdm->name, autodep->clkdm.ptr->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
Paul Walmsley7852ec02012-07-26 00:54:26 -0600207 pr_debug("clockdomain: %s: removing %s sleepdep/wkdep\n",
208 clkdm->name, autodep->clkdm.ptr->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300209
Paul Walmsley55ed9692010-01-26 20:12:59 -0700210 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
211 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300212 }
213}
214
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700215/**
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700216 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
217 * @clkdm: clockdomain that we are resolving dependencies for
218 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600219 *
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700220 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
221 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700222 * No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700223 */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700224static void _resolve_clkdm_deps(struct clockdomain *clkdm,
225 struct clkdm_dep *clkdm_deps)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600226{
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700227 struct clkdm_dep *cd;
228
229 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700230 if (cd->clkdm)
231 continue;
232 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
233
234 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
235 clkdm->name, cd->clkdm_name);
236 }
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600237}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300238
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300239/* Public functions */
240
241/**
Paul Walmsley08cb9702011-09-14 16:01:20 -0600242 * clkdm_register_platform_funcs - register clockdomain implementation fns
243 * @co: func pointers for arch specific implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300244 *
Paul Walmsley08cb9702011-09-14 16:01:20 -0600245 * Register the list of function pointers used to implement the
246 * clockdomain functions on different OMAP SoCs. Should be called
247 * before any other clkdm_register*() function. Returns -EINVAL if
248 * @co is null, -EEXIST if platform functions have already been
249 * registered, or 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300250 */
Paul Walmsley08cb9702011-09-14 16:01:20 -0600251int clkdm_register_platform_funcs(struct clkdm_ops *co)
252{
253 if (!co)
254 return -EINVAL;
255
256 if (arch_clkdm)
257 return -EEXIST;
258
259 arch_clkdm = co;
260
261 return 0;
262};
263
264/**
265 * clkdm_register_clkdms - register SoC clockdomains
266 * @cs: pointer to an array of struct clockdomain to register
267 *
268 * Register the clockdomains available on a particular OMAP SoC. Must
269 * be called after clkdm_register_platform_funcs(). May be called
270 * multiple times. Returns -EACCES if called before
271 * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
272 * null; or 0 upon success.
273 */
274int clkdm_register_clkdms(struct clockdomain **cs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300275{
276 struct clockdomain **c = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300277
Paul Walmsley08cb9702011-09-14 16:01:20 -0600278 if (!arch_clkdm)
279 return -EACCES;
Rajendra Nayak32d40342011-02-25 16:06:47 -0700280
Paul Walmsley08cb9702011-09-14 16:01:20 -0600281 if (!cs)
282 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300283
Paul Walmsley08cb9702011-09-14 16:01:20 -0600284 for (c = cs; *c; c++)
285 _clkdm_register(*c);
286
287 return 0;
288}
289
290/**
291 * clkdm_register_autodeps - register autodeps (if required)
292 * @ia: pointer to a static array of struct clkdm_autodep to register
293 *
294 * Register clockdomain "automatic dependencies." These are
295 * clockdomain wakeup and sleep dependencies that are automatically
296 * added whenever the first clock inside a clockdomain is enabled, and
297 * removed whenever the last clock inside a clockdomain is disabled.
298 * These are currently only used on OMAP3 devices, and are deprecated,
299 * since they waste energy. However, until the OMAP2/3 IP block
300 * enable/disable sequence can be converted to match the OMAP4
301 * sequence, they are needed.
302 *
303 * Must be called only after all of the SoC clockdomains are
304 * registered, since the function will resolve autodep clockdomain
305 * names into clockdomain pointers.
306 *
307 * The struct clkdm_autodep @ia array must be static, as this function
308 * does not copy the array elements.
309 *
310 * Returns -EACCES if called before any clockdomains have been
311 * registered, -EINVAL if called with a null @ia argument, -EEXIST if
312 * autodeps have already been registered, or 0 upon success.
313 */
314int clkdm_register_autodeps(struct clkdm_autodep *ia)
315{
316 struct clkdm_autodep *a = NULL;
317
318 if (list_empty(&clkdm_list))
319 return -EACCES;
320
321 if (!ia)
322 return -EINVAL;
323
Paul Walmsley55ed9692010-01-26 20:12:59 -0700324 if (autodeps)
Paul Walmsley08cb9702011-09-14 16:01:20 -0600325 return -EEXIST;
Paul Walmsley369d5612010-01-26 20:13:01 -0700326
Paul Walmsley08cb9702011-09-14 16:01:20 -0600327 autodeps = ia;
328 for (a = autodeps; a->clkdm.ptr; a++)
329 _autodep_lookup(a);
330
331 return 0;
332}
333
334/**
335 * clkdm_complete_init - set up the clockdomain layer
336 *
337 * Put all clockdomains into software-supervised mode; PM code should
338 * later enable hardware-supervised mode as appropriate. Must be
339 * called after clkdm_register_clkdms(). Returns -EACCES if called
340 * before clkdm_register_clkdms(), or 0 upon success.
341 */
342int clkdm_complete_init(void)
343{
344 struct clockdomain *clkdm;
345
346 if (list_empty(&clkdm_list))
347 return -EACCES;
348
Paul Walmsley369d5612010-01-26 20:13:01 -0700349 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600350 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700351 clkdm_wakeup(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600352 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700353 clkdm_deny_idle(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600354
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700355 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600356 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700357
358 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600359 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700360 }
Paul Walmsley08cb9702011-09-14 16:01:20 -0600361
362 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300363}
364
365/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300366 * clkdm_lookup - look up a clockdomain by name, return a pointer
367 * @name: name of clockdomain
368 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700369 * Find a registered clockdomain by its name @name. Returns a pointer
370 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300371 */
372struct clockdomain *clkdm_lookup(const char *name)
373{
374 struct clockdomain *clkdm, *temp_clkdm;
375
376 if (!name)
377 return NULL;
378
379 clkdm = NULL;
380
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300381 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
382 if (!strcmp(name, temp_clkdm->name)) {
383 clkdm = temp_clkdm;
384 break;
385 }
386 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300387
388 return clkdm;
389}
390
391/**
392 * clkdm_for_each - call function on each registered clockdomain
393 * @fn: callback function *
394 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700395 * Call the supplied function @fn for each registered clockdomain.
396 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300397 * out early from the iterator. The callback function is called with
398 * the clkdm_mutex held, so no clockdomain structure manipulation
399 * functions should be called from the callback, although hardware
400 * clockdomain control functions are fine. Returns the last return
401 * value of the callback function, which should be 0 for success or
402 * anything else to indicate failure; or -EINVAL if the function pointer
403 * is null.
404 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300405int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
406 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300407{
408 struct clockdomain *clkdm;
409 int ret = 0;
410
411 if (!fn)
412 return -EINVAL;
413
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300414 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300415 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300416 if (ret)
417 break;
418 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300419
420 return ret;
421}
422
423
Paul Walmsleye89087c2008-05-20 18:41:35 -0600424/**
425 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
426 * @clkdm: struct clockdomain *
427 *
428 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700429 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600430 */
431struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
432{
433 if (!clkdm)
434 return NULL;
435
Paul Walmsley5b74c672009-02-03 02:10:03 -0700436 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600437}
438
439
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300440/* Hardware clockdomain control */
441
442/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700443 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
444 * @clkdm1: wake this struct clockdomain * up (dependent)
445 * @clkdm2: when this struct clockdomain * wakes up (source)
446 *
447 * When the clockdomain represented by @clkdm2 wakes up, wake up
448 * @clkdm1. Implemented in hardware on the OMAP, this feature is
449 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
450 * Returns -EINVAL if presented with invalid clockdomain pointers,
451 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
452 * success.
453 */
454int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
455{
456 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700457 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700458
Paul Walmsley55ed9692010-01-26 20:12:59 -0700459 if (!clkdm1 || !clkdm2)
460 return -EINVAL;
461
462 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700463 if (IS_ERR(cd))
464 ret = PTR_ERR(cd);
465
466 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
467 ret = -EINVAL;
468
469 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600470 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
471 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700472 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700473 }
474
Paul Walmsley369d5612010-01-26 20:13:01 -0700475 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600476 pr_debug("clockdomain: hardware will wake up %s when %s wakes up\n",
477 clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700478
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700479 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700480 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700481
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700482 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700483}
484
485/**
486 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
487 * @clkdm1: wake this struct clockdomain * up (dependent)
488 * @clkdm2: when this struct clockdomain * wakes up (source)
489 *
490 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
491 * wakes up. Returns -EINVAL if presented with invalid clockdomain
492 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
493 * 0 upon success.
494 */
495int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
496{
497 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700498 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700499
Paul Walmsley55ed9692010-01-26 20:12:59 -0700500 if (!clkdm1 || !clkdm2)
501 return -EINVAL;
502
503 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700504 if (IS_ERR(cd))
505 ret = PTR_ERR(cd);
506
507 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
508 ret = -EINVAL;
509
510 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600511 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
512 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700513 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700514 }
515
Paul Walmsley369d5612010-01-26 20:13:01 -0700516 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600517 pr_debug("clockdomain: hardware will no longer wake up %s after %s wakes up\n",
518 clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700519
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700520 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700521 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700522
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700523 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700524}
525
526/**
527 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
528 * @clkdm1: wake this struct clockdomain * up (dependent)
529 * @clkdm2: when this struct clockdomain * wakes up (source)
530 *
531 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
532 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
533 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
534 * is incapable.
535 *
536 * REVISIT: Currently this function only represents software-controllable
537 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
538 * yet handled here.
539 */
540int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
541{
542 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700543 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700544
545 if (!clkdm1 || !clkdm2)
546 return -EINVAL;
547
548 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700549 if (IS_ERR(cd))
550 ret = PTR_ERR(cd);
551
552 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
553 ret = -EINVAL;
554
555 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600556 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
557 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700558 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700559 }
560
Paul Walmsley369d5612010-01-26 20:13:01 -0700561 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700562 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700563}
564
565/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700566 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
567 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
568 *
569 * Remove all inter-clockdomain wakeup dependencies that could cause
570 * @clkdm to wake. Intended to be used during boot to initialize the
571 * PRCM to a known state, after all clockdomains are put into swsup idle
572 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
573 * 0 upon success.
574 */
575int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
576{
Paul Walmsley369d5612010-01-26 20:13:01 -0700577 if (!clkdm)
578 return -EINVAL;
579
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700580 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
581 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700582
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700583 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700584}
585
586/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700587 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
588 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
589 * @clkdm2: when this struct clockdomain * is active (source)
590 *
591 * Prevent @clkdm1 from automatically going inactive (and then to
592 * retention or off) if @clkdm2 is active. Returns -EINVAL if
593 * presented with invalid clockdomain pointers or called on a machine
594 * that does not support software-configurable hardware sleep
595 * dependencies, -ENOENT if the specified dependency cannot be set in
596 * hardware, or 0 upon success.
597 */
598int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
599{
600 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700601 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700602
603 if (!clkdm1 || !clkdm2)
604 return -EINVAL;
605
606 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700607 if (IS_ERR(cd))
608 ret = PTR_ERR(cd);
609
610 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
611 ret = -EINVAL;
612
613 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600614 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
615 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700616 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700617 }
618
Paul Walmsley369d5612010-01-26 20:13:01 -0700619 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600620 pr_debug("clockdomain: will prevent %s from sleeping if %s is active\n",
621 clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700622
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700623 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700624 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700625
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700626 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700627}
628
629/**
630 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
631 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
632 * @clkdm2: when this struct clockdomain * is active (source)
633 *
634 * Allow @clkdm1 to automatically go inactive (and then to retention or
635 * off), independent of the activity state of @clkdm2. Returns -EINVAL
636 * if presented with invalid clockdomain pointers or called on a machine
637 * that does not support software-configurable hardware sleep dependencies,
638 * -ENOENT if the specified dependency cannot be cleared in hardware, or
639 * 0 upon success.
640 */
641int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
642{
643 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700644 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700645
646 if (!clkdm1 || !clkdm2)
647 return -EINVAL;
648
649 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700650 if (IS_ERR(cd))
651 ret = PTR_ERR(cd);
652
653 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
654 ret = -EINVAL;
655
656 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600657 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
658 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700659 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700660 }
661
Paul Walmsley369d5612010-01-26 20:13:01 -0700662 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600663 pr_debug("clockdomain: will no longer prevent %s from sleeping if %s is active\n",
664 clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700665
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700666 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700667 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700668
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700669 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700670}
671
672/**
673 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
674 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
675 * @clkdm2: when this struct clockdomain * is active (source)
676 *
677 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
678 * not be allowed to automatically go inactive if @clkdm2 is active;
679 * 0 if @clkdm1's automatic power state inactivity transition is independent
680 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
681 * on a machine that does not support software-configurable hardware sleep
682 * dependencies; or -ENOENT if the hardware is incapable.
683 *
684 * REVISIT: Currently this function only represents software-controllable
685 * sleep dependencies. Sleep dependencies fixed in hardware are not
686 * yet handled here.
687 */
688int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
689{
690 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700691 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700692
693 if (!clkdm1 || !clkdm2)
694 return -EINVAL;
695
696 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700697 if (IS_ERR(cd))
698 ret = PTR_ERR(cd);
699
700 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
701 ret = -EINVAL;
702
703 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600704 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
705 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700706 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700707 }
708
Paul Walmsley369d5612010-01-26 20:13:01 -0700709 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700710 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700711}
712
Paul Walmsley369d5612010-01-26 20:13:01 -0700713/**
714 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
715 * @clkdm: struct clockdomain * to remove all sleep dependencies from
716 *
717 * Remove all inter-clockdomain sleep dependencies that could prevent
718 * @clkdm from idling. Intended to be used during boot to initialize the
719 * PRCM to a known state, after all clockdomains are put into swsup idle
720 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
721 * 0 upon success.
722 */
723int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
724{
Paul Walmsley369d5612010-01-26 20:13:01 -0700725 if (!clkdm)
726 return -EINVAL;
727
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700728 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
729 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700730
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700731 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700732}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700733
734/**
Paul Walmsley3a090282013-01-26 00:58:16 -0700735 * clkdm_sleep_nolock - force clockdomain sleep transition (lockless)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300736 * @clkdm: struct clockdomain *
737 *
738 * Instruct the CM to force a sleep transition on the specified
Paul Walmsley3a090282013-01-26 00:58:16 -0700739 * clockdomain @clkdm. Only for use by the powerdomain code. Returns
740 * -EINVAL if @clkdm is NULL or if clockdomain does not support
741 * software-initiated sleep; 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300742 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700743int clkdm_sleep_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300744{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600745 int ret;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600746
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300747 if (!clkdm)
748 return -EINVAL;
749
750 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600751 pr_debug("clockdomain: %s does not support forcing sleep via software\n",
752 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300753 return -EINVAL;
754 }
755
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700756 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
757 return -EINVAL;
758
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300759 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
760
Paul Walmsley32a363c2011-07-10 05:56:54 -0600761 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600762 ret = arch_clkdm->clkdm_sleep(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700763 ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
764
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600765 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300766}
767
768/**
Paul Walmsley3a090282013-01-26 00:58:16 -0700769 * clkdm_sleep - force clockdomain sleep transition
770 * @clkdm: struct clockdomain *
771 *
772 * Instruct the CM to force a sleep transition on the specified
773 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
774 * clockdomain does not support software-initiated sleep; 0 upon
775 * success.
776 */
777int clkdm_sleep(struct clockdomain *clkdm)
778{
779 int ret;
780
781 pwrdm_lock(clkdm->pwrdm.ptr);
782 ret = clkdm_sleep_nolock(clkdm);
783 pwrdm_unlock(clkdm->pwrdm.ptr);
784
785 return ret;
786}
787
788/**
789 * clkdm_wakeup_nolock - force clockdomain wakeup transition (lockless)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300790 * @clkdm: struct clockdomain *
791 *
792 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsley3a090282013-01-26 00:58:16 -0700793 * clockdomain @clkdm. Only for use by the powerdomain code. Returns
794 * -EINVAL if @clkdm is NULL or if the clockdomain does not support
795 * software-controlled wakeup; 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300796 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700797int clkdm_wakeup_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300798{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600799 int ret;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600800
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300801 if (!clkdm)
802 return -EINVAL;
803
804 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600805 pr_debug("clockdomain: %s does not support forcing wakeup via software\n",
806 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300807 return -EINVAL;
808 }
809
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700810 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
811 return -EINVAL;
812
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300813 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
814
Paul Walmsley32a363c2011-07-10 05:56:54 -0600815 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600816 ret = arch_clkdm->clkdm_wakeup(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700817 ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
818
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600819 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300820}
821
822/**
Paul Walmsley3a090282013-01-26 00:58:16 -0700823 * clkdm_wakeup - force clockdomain wakeup transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300824 * @clkdm: struct clockdomain *
825 *
Paul Walmsley3a090282013-01-26 00:58:16 -0700826 * Instruct the CM to force a wakeup transition on the specified
827 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
828 * clockdomain does not support software-controlled wakeup; 0 upon
829 * success.
830 */
831int clkdm_wakeup(struct clockdomain *clkdm)
832{
833 int ret;
834
835 pwrdm_lock(clkdm->pwrdm.ptr);
836 ret = clkdm_wakeup_nolock(clkdm);
837 pwrdm_unlock(clkdm->pwrdm.ptr);
838
839 return ret;
840}
841
842/**
843 * clkdm_allow_idle_nolock - enable hwsup idle transitions for clkdm
844 * @clkdm: struct clockdomain *
845 *
846 * Allow the hardware to automatically switch the clockdomain @clkdm
847 * into active or idle states, as needed by downstream clocks. If the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300848 * clockdomain has any downstream clocks enabled in the clock
849 * framework, wkdep/sleepdep autodependencies are added; this is so
Paul Walmsley3a090282013-01-26 00:58:16 -0700850 * device drivers can read and write to the device. Only for use by
851 * the powerdomain code. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300852 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700853void clkdm_allow_idle_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300854{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300855 if (!clkdm)
856 return;
857
858 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600859 pr_debug("clock: %s: automatic idle transitions cannot be enabled\n",
860 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300861 return;
862 }
863
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700864 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
865 return;
866
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300867 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
868 clkdm->name);
869
Paul Walmsley32a363c2011-07-10 05:56:54 -0600870 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700871 arch_clkdm->clkdm_allow_idle(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700872 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
873}
874
875/**
876 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
877 * @clkdm: struct clockdomain *
878 *
879 * Allow the hardware to automatically switch the clockdomain @clkdm into
880 * active or idle states, as needed by downstream clocks. If the
881 * clockdomain has any downstream clocks enabled in the clock
882 * framework, wkdep/sleepdep autodependencies are added; this is so
883 * device drivers can read and write to the device. No return value.
884 */
885void clkdm_allow_idle(struct clockdomain *clkdm)
886{
887 pwrdm_lock(clkdm->pwrdm.ptr);
888 clkdm_allow_idle_nolock(clkdm);
889 pwrdm_unlock(clkdm->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300890}
891
892/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700893 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300894 * @clkdm: struct clockdomain *
895 *
896 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700897 * @clkdm into inactive or idle states. If the clockdomain has
898 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsley3a090282013-01-26 00:58:16 -0700899 * autodependencies are removed. Only for use by the powerdomain
900 * code. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300901 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700902void clkdm_deny_idle_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300903{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300904 if (!clkdm)
905 return;
906
907 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600908 pr_debug("clockdomain: %s: automatic idle transitions cannot be disabled\n",
909 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300910 return;
911 }
912
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700913 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
914 return;
915
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300916 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
917 clkdm->name);
918
Paul Walmsley32a363c2011-07-10 05:56:54 -0600919 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700920 arch_clkdm->clkdm_deny_idle(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700921 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
922}
923
924/**
925 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
926 * @clkdm: struct clockdomain *
927 *
928 * Prevent the hardware from automatically switching the clockdomain
929 * @clkdm into inactive or idle states. If the clockdomain has
930 * downstream clocks enabled in the clock framework, wkdep/sleepdep
931 * autodependencies are removed. No return value.
932 */
933void clkdm_deny_idle(struct clockdomain *clkdm)
934{
935 pwrdm_lock(clkdm->pwrdm.ptr);
936 clkdm_deny_idle_nolock(clkdm);
937 pwrdm_unlock(clkdm->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300938}
939
Paul Walmsley32a363c2011-07-10 05:56:54 -0600940/**
941 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
942 * @clkdm: struct clockdomain *
943 *
944 * Returns true if clockdomain @clkdm currently has
945 * hardware-supervised idle enabled, or false if it does not or if
946 * @clkdm is NULL. It is only valid to call this function after
947 * clkdm_init() has been called. This function does not actually read
948 * bits from the hardware; it instead tests an in-memory flag that is
949 * changed whenever the clockdomain code changes the auto-idle mode.
950 */
951bool clkdm_in_hwsup(struct clockdomain *clkdm)
952{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600953 bool ret;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600954
Paul Walmsley32a363c2011-07-10 05:56:54 -0600955 if (!clkdm)
956 return false;
957
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600958 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600959
960 return ret;
Paul Walmsley32a363c2011-07-10 05:56:54 -0600961}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300962
Paul Walmsleyb71c7212012-09-23 17:28:28 -0600963/**
964 * clkdm_missing_idle_reporting - can @clkdm enter autoidle even if in use?
965 * @clkdm: struct clockdomain *
966 *
967 * Returns true if clockdomain @clkdm has the
968 * CLKDM_MISSING_IDLE_REPORTING flag set, or false if not or @clkdm is
969 * null. More information is available in the documentation for the
970 * CLKDM_MISSING_IDLE_REPORTING macro.
971 */
972bool clkdm_missing_idle_reporting(struct clockdomain *clkdm)
973{
974 if (!clkdm)
975 return false;
976
977 return (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING) ? true : false;
978}
979
Benoit Cousson113a7412011-07-10 05:56:54 -0600980/* Clockdomain-to-clock/hwmod framework interface code */
981
982static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
983{
984 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
985 return -EINVAL;
986
Paul Walmsley3a090282013-01-26 00:58:16 -0700987 pwrdm_lock(clkdm->pwrdm.ptr);
Tero Kristo64e29fd2012-09-25 19:05:32 +0300988
Benoit Cousson113a7412011-07-10 05:56:54 -0600989 /*
990 * For arch's with no autodeps, clkcm_clk_enable
991 * should be called for every clock instance or hwmod that is
992 * enabled, so the clkdm can be force woken up.
993 */
Tero Kristo64e29fd2012-09-25 19:05:32 +0300994 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps) {
Paul Walmsley3a090282013-01-26 00:58:16 -0700995 pwrdm_unlock(clkdm->pwrdm.ptr);
Benoit Cousson113a7412011-07-10 05:56:54 -0600996 return 0;
Tero Kristo64e29fd2012-09-25 19:05:32 +0300997 }
Benoit Cousson113a7412011-07-10 05:56:54 -0600998
999 arch_clkdm->clkdm_clk_enable(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -07001000 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1001 pwrdm_unlock(clkdm->pwrdm.ptr);
Benoit Cousson113a7412011-07-10 05:56:54 -06001002
Paul Walmsley7852ec02012-07-26 00:54:26 -06001003 pr_debug("clockdomain: %s: enabled\n", clkdm->name);
Benoit Cousson113a7412011-07-10 05:56:54 -06001004
1005 return 0;
1006}
1007
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001008/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001009 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001010 * @clkdm: struct clockdomain *
1011 * @clk: struct clk * of the enabled downstream clock
1012 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -07001013 * Increment the usecount of the clockdomain @clkdm and ensure that it
1014 * is awake before @clk is enabled. Intended to be called by
1015 * clk_enable() code. If the clockdomain is in software-supervised
1016 * idle mode, force the clockdomain to wake. If the clockdomain is in
1017 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
1018 * ensure that devices in the clockdomain can be read from/written to
1019 * by on-chip processors. Returns -EINVAL if passed null pointers;
1020 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001021 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001022int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001023{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001024 /*
1025 * XXX Rewrite this code to maintain a list of enabled
1026 * downstream clocks for debugging purposes?
1027 */
1028
Benoit Cousson113a7412011-07-10 05:56:54 -06001029 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001030 return -EINVAL;
1031
Benoit Cousson113a7412011-07-10 05:56:54 -06001032 return _clkdm_clk_hwmod_enable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001033}
1034
1035/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001036 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001037 * @clkdm: struct clockdomain *
1038 * @clk: struct clk * of the disabled downstream clock
1039 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -07001040 * Decrement the usecount of this clockdomain @clkdm when @clk is
1041 * disabled. Intended to be called by clk_disable() code. If the
1042 * clockdomain usecount goes to 0, put the clockdomain to sleep
1043 * (software-supervised mode) or remove the clkdm autodependencies
1044 * (hardware-supervised mode). Returns -EINVAL if passed null
Benoit Cousson113a7412011-07-10 05:56:54 -06001045 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1046 * upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001047 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001048int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001049{
Mike Turquetted043d872012-11-09 11:28:42 -07001050 if (!clkdm || !clk || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001051 return -EINVAL;
1052
Paul Walmsley3a090282013-01-26 00:58:16 -07001053 pwrdm_lock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001054
Mike Turquetted043d872012-11-09 11:28:42 -07001055 /* corner case: disabling unused clocks */
Jon Hunter29f06672012-12-15 01:35:54 -07001056 if ((__clk_get_enable_count(clk) == 0) &&
1057 (atomic_read(&clkdm->usecount) == 0))
Mike Turquetted043d872012-11-09 11:28:42 -07001058 goto ccd_exit;
Mike Turquetted043d872012-11-09 11:28:42 -07001059
1060 if (atomic_read(&clkdm->usecount) == 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001061 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001062 WARN_ON(1); /* underflow */
1063 return -ERANGE;
1064 }
1065
1066 if (atomic_dec_return(&clkdm->usecount) > 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001067 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001068 return 0;
1069 }
1070
1071 arch_clkdm->clkdm_clk_disable(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -07001072 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001073
1074 pr_debug("clockdomain: %s: disabled\n", clkdm->name);
1075
Mike Turquetted043d872012-11-09 11:28:42 -07001076ccd_exit:
Paul Walmsley3a090282013-01-26 00:58:16 -07001077 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001078
1079 return 0;
Benoit Cousson113a7412011-07-10 05:56:54 -06001080}
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001081
Benoit Cousson113a7412011-07-10 05:56:54 -06001082/**
1083 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1084 * @clkdm: struct clockdomain *
1085 * @oh: struct omap_hwmod * of the enabled downstream hwmod
1086 *
1087 * Increment the usecount of the clockdomain @clkdm and ensure that it
1088 * is awake before @oh is enabled. Intended to be called by
1089 * module_enable() code.
1090 * If the clockdomain is in software-supervised idle mode, force the
1091 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
1092 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1093 * clockdomain can be read from/written to by on-chip processors.
1094 * Returns -EINVAL if passed null pointers;
1095 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1096 */
1097int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1098{
1099 /* The clkdm attribute does not exist yet prior OMAP4 */
1100 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001101 return 0;
1102
Benoit Cousson113a7412011-07-10 05:56:54 -06001103 /*
1104 * XXX Rewrite this code to maintain a list of enabled
1105 * downstream hwmods for debugging purposes?
1106 */
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001107
Benoit Cousson113a7412011-07-10 05:56:54 -06001108 if (!oh)
1109 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001110
Benoit Cousson113a7412011-07-10 05:56:54 -06001111 return _clkdm_clk_hwmod_enable(clkdm);
1112}
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001113
Benoit Cousson113a7412011-07-10 05:56:54 -06001114/**
1115 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1116 * @clkdm: struct clockdomain *
1117 * @oh: struct omap_hwmod * of the disabled downstream hwmod
1118 *
1119 * Decrement the usecount of this clockdomain @clkdm when @oh is
1120 * disabled. Intended to be called by module_disable() code.
1121 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1122 * (software-supervised mode) or remove the clkdm autodependencies
1123 * (hardware-supervised mode).
1124 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1125 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1126 * idle mode.
1127 */
1128int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1129{
1130 /* The clkdm attribute does not exist yet prior OMAP4 */
1131 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1132 return 0;
1133
1134 /*
1135 * XXX Rewrite this code to maintain a list of enabled
1136 * downstream hwmods for debugging purposes?
1137 */
1138
Mike Turquetted043d872012-11-09 11:28:42 -07001139 if (!clkdm || !oh || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
Benoit Cousson113a7412011-07-10 05:56:54 -06001140 return -EINVAL;
1141
Paul Walmsley3a090282013-01-26 00:58:16 -07001142 pwrdm_lock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001143
1144 if (atomic_read(&clkdm->usecount) == 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001145 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001146 WARN_ON(1); /* underflow */
1147 return -ERANGE;
1148 }
1149
1150 if (atomic_dec_return(&clkdm->usecount) > 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001151 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001152 return 0;
1153 }
1154
1155 arch_clkdm->clkdm_clk_disable(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -07001156 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1157 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001158
1159 pr_debug("clockdomain: %s: disabled\n", clkdm->name);
1160
1161 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001162}
1163