blob: ab194753eb7a8f35c8e67257bbd7b1e777083cef [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 Walmsley65958fb2013-01-26 00:58:17 -0700123/**
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
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700155/**
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700156 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
157 * @clkdm: clockdomain that we are resolving dependencies for
158 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600159 *
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700160 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
161 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700162 * No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700163 */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700164static void _resolve_clkdm_deps(struct clockdomain *clkdm,
165 struct clkdm_dep *clkdm_deps)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600166{
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700167 struct clkdm_dep *cd;
168
169 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700170 if (cd->clkdm)
171 continue;
172 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
173
174 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
175 clkdm->name, cd->clkdm_name);
176 }
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600177}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300178
Paul Walmsley65958fb2013-01-26 00:58:17 -0700179/**
180 * _clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1 (lockless)
181 * @clkdm1: wake this struct clockdomain * up (dependent)
182 * @clkdm2: when this struct clockdomain * wakes up (source)
183 *
184 * When the clockdomain represented by @clkdm2 wakes up, wake up
185 * @clkdm1. Implemented in hardware on the OMAP, this feature is
186 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
187 * Returns -EINVAL if presented with invalid clockdomain pointers,
188 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
189 * success.
190 */
191static int _clkdm_add_wkdep(struct clockdomain *clkdm1,
192 struct clockdomain *clkdm2)
193{
194 struct clkdm_dep *cd;
195 int ret = 0;
196
197 if (!clkdm1 || !clkdm2)
198 return -EINVAL;
199
200 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
201 if (IS_ERR(cd))
202 ret = PTR_ERR(cd);
203
204 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
205 ret = -EINVAL;
206
207 if (ret) {
208 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
209 clkdm1->name, clkdm2->name);
210 return ret;
211 }
212
213 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
214 pr_debug("clockdomain: hardware will wake up %s when %s wakes up\n",
215 clkdm1->name, clkdm2->name);
216
217 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
218 }
219
220 return ret;
221}
222
223/**
224 * _clkdm_del_wkdep - remove a wakeup dep from clkdm2 to clkdm1 (lockless)
225 * @clkdm1: wake this struct clockdomain * up (dependent)
226 * @clkdm2: when this struct clockdomain * wakes up (source)
227 *
228 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
229 * wakes up. Returns -EINVAL if presented with invalid clockdomain
230 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
231 * 0 upon success.
232 */
233static int _clkdm_del_wkdep(struct clockdomain *clkdm1,
234 struct clockdomain *clkdm2)
235{
236 struct clkdm_dep *cd;
237 int ret = 0;
238
239 if (!clkdm1 || !clkdm2)
240 return -EINVAL;
241
242 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
243 if (IS_ERR(cd))
244 ret = PTR_ERR(cd);
245
246 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
247 ret = -EINVAL;
248
249 if (ret) {
250 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
251 clkdm1->name, clkdm2->name);
252 return ret;
253 }
254
255 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
256 pr_debug("clockdomain: hardware will no longer wake up %s after %s wakes up\n",
257 clkdm1->name, clkdm2->name);
258
259 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
260 }
261
262 return ret;
263}
264
265/**
266 * _clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1 (lockless)
267 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
268 * @clkdm2: when this struct clockdomain * is active (source)
269 *
270 * Prevent @clkdm1 from automatically going inactive (and then to
271 * retention or off) if @clkdm2 is active. Returns -EINVAL if
272 * presented with invalid clockdomain pointers or called on a machine
273 * that does not support software-configurable hardware sleep
274 * dependencies, -ENOENT if the specified dependency cannot be set in
275 * hardware, or 0 upon success.
276 */
277static int _clkdm_add_sleepdep(struct clockdomain *clkdm1,
278 struct clockdomain *clkdm2)
279{
280 struct clkdm_dep *cd;
281 int ret = 0;
282
283 if (!clkdm1 || !clkdm2)
284 return -EINVAL;
285
286 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
287 if (IS_ERR(cd))
288 ret = PTR_ERR(cd);
289
290 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
291 ret = -EINVAL;
292
293 if (ret) {
294 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
295 clkdm1->name, clkdm2->name);
296 return ret;
297 }
298
299 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
300 pr_debug("clockdomain: will prevent %s from sleeping if %s is active\n",
301 clkdm1->name, clkdm2->name);
302
303 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
304 }
305
306 return ret;
307}
308
309/**
310 * _clkdm_del_sleepdep - remove a sleep dep from clkdm2 to clkdm1 (lockless)
311 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
312 * @clkdm2: when this struct clockdomain * is active (source)
313 *
314 * Allow @clkdm1 to automatically go inactive (and then to retention or
315 * off), independent of the activity state of @clkdm2. Returns -EINVAL
316 * if presented with invalid clockdomain pointers or called on a machine
317 * that does not support software-configurable hardware sleep dependencies,
318 * -ENOENT if the specified dependency cannot be cleared in hardware, or
319 * 0 upon success.
320 */
321static int _clkdm_del_sleepdep(struct clockdomain *clkdm1,
322 struct clockdomain *clkdm2)
323{
324 struct clkdm_dep *cd;
325 int ret = 0;
326
327 if (!clkdm1 || !clkdm2)
328 return -EINVAL;
329
330 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
331 if (IS_ERR(cd))
332 ret = PTR_ERR(cd);
333
334 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
335 ret = -EINVAL;
336
337 if (ret) {
338 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
339 clkdm1->name, clkdm2->name);
340 return ret;
341 }
342
343 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
344 pr_debug("clockdomain: will no longer prevent %s from sleeping if %s is active\n",
345 clkdm1->name, clkdm2->name);
346
347 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
348 }
349
350 return ret;
351}
352
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300353/* Public functions */
354
355/**
Paul Walmsley08cb9702011-09-14 16:01:20 -0600356 * clkdm_register_platform_funcs - register clockdomain implementation fns
357 * @co: func pointers for arch specific implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300358 *
Paul Walmsley08cb9702011-09-14 16:01:20 -0600359 * Register the list of function pointers used to implement the
360 * clockdomain functions on different OMAP SoCs. Should be called
361 * before any other clkdm_register*() function. Returns -EINVAL if
362 * @co is null, -EEXIST if platform functions have already been
363 * registered, or 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300364 */
Paul Walmsley08cb9702011-09-14 16:01:20 -0600365int clkdm_register_platform_funcs(struct clkdm_ops *co)
366{
367 if (!co)
368 return -EINVAL;
369
370 if (arch_clkdm)
371 return -EEXIST;
372
373 arch_clkdm = co;
374
375 return 0;
376};
377
378/**
379 * clkdm_register_clkdms - register SoC clockdomains
380 * @cs: pointer to an array of struct clockdomain to register
381 *
382 * Register the clockdomains available on a particular OMAP SoC. Must
383 * be called after clkdm_register_platform_funcs(). May be called
384 * multiple times. Returns -EACCES if called before
385 * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
386 * null; or 0 upon success.
387 */
388int clkdm_register_clkdms(struct clockdomain **cs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300389{
390 struct clockdomain **c = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300391
Paul Walmsley08cb9702011-09-14 16:01:20 -0600392 if (!arch_clkdm)
393 return -EACCES;
Rajendra Nayak32d40342011-02-25 16:06:47 -0700394
Paul Walmsley08cb9702011-09-14 16:01:20 -0600395 if (!cs)
396 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300397
Paul Walmsley08cb9702011-09-14 16:01:20 -0600398 for (c = cs; *c; c++)
399 _clkdm_register(*c);
400
401 return 0;
402}
403
404/**
405 * clkdm_register_autodeps - register autodeps (if required)
406 * @ia: pointer to a static array of struct clkdm_autodep to register
407 *
408 * Register clockdomain "automatic dependencies." These are
409 * clockdomain wakeup and sleep dependencies that are automatically
410 * added whenever the first clock inside a clockdomain is enabled, and
411 * removed whenever the last clock inside a clockdomain is disabled.
412 * These are currently only used on OMAP3 devices, and are deprecated,
413 * since they waste energy. However, until the OMAP2/3 IP block
414 * enable/disable sequence can be converted to match the OMAP4
415 * sequence, they are needed.
416 *
417 * Must be called only after all of the SoC clockdomains are
418 * registered, since the function will resolve autodep clockdomain
419 * names into clockdomain pointers.
420 *
421 * The struct clkdm_autodep @ia array must be static, as this function
422 * does not copy the array elements.
423 *
424 * Returns -EACCES if called before any clockdomains have been
425 * registered, -EINVAL if called with a null @ia argument, -EEXIST if
426 * autodeps have already been registered, or 0 upon success.
427 */
428int clkdm_register_autodeps(struct clkdm_autodep *ia)
429{
430 struct clkdm_autodep *a = NULL;
431
432 if (list_empty(&clkdm_list))
433 return -EACCES;
434
435 if (!ia)
436 return -EINVAL;
437
Paul Walmsley55ed9692010-01-26 20:12:59 -0700438 if (autodeps)
Paul Walmsley08cb9702011-09-14 16:01:20 -0600439 return -EEXIST;
Paul Walmsley369d5612010-01-26 20:13:01 -0700440
Paul Walmsley08cb9702011-09-14 16:01:20 -0600441 autodeps = ia;
442 for (a = autodeps; a->clkdm.ptr; a++)
443 _autodep_lookup(a);
444
445 return 0;
446}
447
448/**
449 * clkdm_complete_init - set up the clockdomain layer
450 *
451 * Put all clockdomains into software-supervised mode; PM code should
452 * later enable hardware-supervised mode as appropriate. Must be
453 * called after clkdm_register_clkdms(). Returns -EACCES if called
454 * before clkdm_register_clkdms(), or 0 upon success.
455 */
456int clkdm_complete_init(void)
457{
458 struct clockdomain *clkdm;
459
460 if (list_empty(&clkdm_list))
461 return -EACCES;
462
Paul Walmsley369d5612010-01-26 20:13:01 -0700463 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600464 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700465 clkdm_wakeup(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600466 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700467 clkdm_deny_idle(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600468
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700469 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600470 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700471
472 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600473 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700474 }
Paul Walmsley08cb9702011-09-14 16:01:20 -0600475
476 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300477}
478
479/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300480 * clkdm_lookup - look up a clockdomain by name, return a pointer
481 * @name: name of clockdomain
482 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700483 * Find a registered clockdomain by its name @name. Returns a pointer
484 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300485 */
486struct clockdomain *clkdm_lookup(const char *name)
487{
488 struct clockdomain *clkdm, *temp_clkdm;
489
490 if (!name)
491 return NULL;
492
493 clkdm = NULL;
494
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300495 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
496 if (!strcmp(name, temp_clkdm->name)) {
497 clkdm = temp_clkdm;
498 break;
499 }
500 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300501
502 return clkdm;
503}
504
505/**
506 * clkdm_for_each - call function on each registered clockdomain
507 * @fn: callback function *
508 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700509 * Call the supplied function @fn for each registered clockdomain.
510 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300511 * out early from the iterator. The callback function is called with
512 * the clkdm_mutex held, so no clockdomain structure manipulation
513 * functions should be called from the callback, although hardware
514 * clockdomain control functions are fine. Returns the last return
515 * value of the callback function, which should be 0 for success or
516 * anything else to indicate failure; or -EINVAL if the function pointer
517 * is null.
518 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300519int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
520 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300521{
522 struct clockdomain *clkdm;
523 int ret = 0;
524
525 if (!fn)
526 return -EINVAL;
527
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300528 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300529 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300530 if (ret)
531 break;
532 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300533
534 return ret;
535}
536
537
Paul Walmsleye89087c2008-05-20 18:41:35 -0600538/**
539 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
540 * @clkdm: struct clockdomain *
541 *
542 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700543 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600544 */
545struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
546{
547 if (!clkdm)
548 return NULL;
549
Paul Walmsley5b74c672009-02-03 02:10:03 -0700550 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600551}
552
553
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300554/* Hardware clockdomain control */
555
556/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700557 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
558 * @clkdm1: wake this struct clockdomain * up (dependent)
559 * @clkdm2: when this struct clockdomain * wakes up (source)
560 *
561 * When the clockdomain represented by @clkdm2 wakes up, wake up
562 * @clkdm1. Implemented in hardware on the OMAP, this feature is
563 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
564 * Returns -EINVAL if presented with invalid clockdomain pointers,
565 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
566 * success.
567 */
568int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
569{
Paul Walmsley65958fb2013-01-26 00:58:17 -0700570 return _clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700571}
572
573/**
574 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
575 * @clkdm1: wake this struct clockdomain * up (dependent)
576 * @clkdm2: when this struct clockdomain * wakes up (source)
577 *
578 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
579 * wakes up. Returns -EINVAL if presented with invalid clockdomain
580 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
581 * 0 upon success.
582 */
583int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
584{
Paul Walmsley65958fb2013-01-26 00:58:17 -0700585 return _clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700586}
587
588/**
589 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
590 * @clkdm1: wake this struct clockdomain * up (dependent)
591 * @clkdm2: when this struct clockdomain * wakes up (source)
592 *
593 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
594 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
595 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
596 * is incapable.
597 *
598 * REVISIT: Currently this function only represents software-controllable
599 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
600 * yet handled here.
601 */
602int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
603{
604 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700605 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700606
607 if (!clkdm1 || !clkdm2)
608 return -EINVAL;
609
610 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700611 if (IS_ERR(cd))
612 ret = PTR_ERR(cd);
613
614 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
615 ret = -EINVAL;
616
617 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600618 pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
619 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700620 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700621 }
622
Paul Walmsley369d5612010-01-26 20:13:01 -0700623 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700624 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700625}
626
627/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700628 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
629 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
630 *
631 * Remove all inter-clockdomain wakeup dependencies that could cause
632 * @clkdm to wake. Intended to be used during boot to initialize the
633 * PRCM to a known state, after all clockdomains are put into swsup idle
634 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
635 * 0 upon success.
636 */
637int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
638{
Paul Walmsley369d5612010-01-26 20:13:01 -0700639 if (!clkdm)
640 return -EINVAL;
641
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700642 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
643 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700644
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700645 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700646}
647
648/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700649 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
650 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
651 * @clkdm2: when this struct clockdomain * is active (source)
652 *
653 * Prevent @clkdm1 from automatically going inactive (and then to
654 * retention or off) if @clkdm2 is active. Returns -EINVAL if
655 * presented with invalid clockdomain pointers or called on a machine
656 * that does not support software-configurable hardware sleep
657 * dependencies, -ENOENT if the specified dependency cannot be set in
658 * hardware, or 0 upon success.
659 */
660int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
661{
Paul Walmsley65958fb2013-01-26 00:58:17 -0700662 return _clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700663}
664
665/**
666 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
667 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
668 * @clkdm2: when this struct clockdomain * is active (source)
669 *
670 * Allow @clkdm1 to automatically go inactive (and then to retention or
671 * off), independent of the activity state of @clkdm2. Returns -EINVAL
672 * if presented with invalid clockdomain pointers or called on a machine
673 * that does not support software-configurable hardware sleep dependencies,
674 * -ENOENT if the specified dependency cannot be cleared in hardware, or
675 * 0 upon success.
676 */
677int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
678{
Paul Walmsley65958fb2013-01-26 00:58:17 -0700679 return _clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700680}
681
682/**
683 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
684 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
685 * @clkdm2: when this struct clockdomain * is active (source)
686 *
687 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
688 * not be allowed to automatically go inactive if @clkdm2 is active;
689 * 0 if @clkdm1's automatic power state inactivity transition is independent
690 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
691 * on a machine that does not support software-configurable hardware sleep
692 * dependencies; or -ENOENT if the hardware is incapable.
693 *
694 * REVISIT: Currently this function only represents software-controllable
695 * sleep dependencies. Sleep dependencies fixed in hardware are not
696 * yet handled here.
697 */
698int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
699{
700 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700701 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700702
703 if (!clkdm1 || !clkdm2)
704 return -EINVAL;
705
706 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700707 if (IS_ERR(cd))
708 ret = PTR_ERR(cd);
709
710 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
711 ret = -EINVAL;
712
713 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600714 pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
715 clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700716 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700717 }
718
Paul Walmsley369d5612010-01-26 20:13:01 -0700719 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700720 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700721}
722
Paul Walmsley369d5612010-01-26 20:13:01 -0700723/**
724 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
725 * @clkdm: struct clockdomain * to remove all sleep dependencies from
726 *
727 * Remove all inter-clockdomain sleep dependencies that could prevent
728 * @clkdm from idling. Intended to be used during boot to initialize the
729 * PRCM to a known state, after all clockdomains are put into swsup idle
730 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
731 * 0 upon success.
732 */
733int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
734{
Paul Walmsley369d5612010-01-26 20:13:01 -0700735 if (!clkdm)
736 return -EINVAL;
737
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700738 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
739 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700740
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700741 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700742}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700743
744/**
Paul Walmsley3a090282013-01-26 00:58:16 -0700745 * clkdm_sleep_nolock - force clockdomain sleep transition (lockless)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300746 * @clkdm: struct clockdomain *
747 *
748 * Instruct the CM to force a sleep transition on the specified
Paul Walmsley3a090282013-01-26 00:58:16 -0700749 * clockdomain @clkdm. Only for use by the powerdomain code. Returns
750 * -EINVAL if @clkdm is NULL or if clockdomain does not support
751 * software-initiated sleep; 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300752 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700753int clkdm_sleep_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300754{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600755 int ret;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600756
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300757 if (!clkdm)
758 return -EINVAL;
759
760 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600761 pr_debug("clockdomain: %s does not support forcing sleep via software\n",
762 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300763 return -EINVAL;
764 }
765
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700766 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
767 return -EINVAL;
768
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300769 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
770
Paul Walmsley32a363c2011-07-10 05:56:54 -0600771 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600772 ret = arch_clkdm->clkdm_sleep(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700773 ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
774
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600775 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300776}
777
778/**
Paul Walmsley3a090282013-01-26 00:58:16 -0700779 * clkdm_sleep - force clockdomain sleep transition
780 * @clkdm: struct clockdomain *
781 *
782 * Instruct the CM to force a sleep transition on the specified
783 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
784 * clockdomain does not support software-initiated sleep; 0 upon
785 * success.
786 */
787int clkdm_sleep(struct clockdomain *clkdm)
788{
789 int ret;
790
791 pwrdm_lock(clkdm->pwrdm.ptr);
792 ret = clkdm_sleep_nolock(clkdm);
793 pwrdm_unlock(clkdm->pwrdm.ptr);
794
795 return ret;
796}
797
798/**
799 * clkdm_wakeup_nolock - force clockdomain wakeup transition (lockless)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300800 * @clkdm: struct clockdomain *
801 *
802 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsley3a090282013-01-26 00:58:16 -0700803 * clockdomain @clkdm. Only for use by the powerdomain code. Returns
804 * -EINVAL if @clkdm is NULL or if the clockdomain does not support
805 * software-controlled wakeup; 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300806 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700807int clkdm_wakeup_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300808{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600809 int ret;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600810
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300811 if (!clkdm)
812 return -EINVAL;
813
814 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600815 pr_debug("clockdomain: %s does not support forcing wakeup via software\n",
816 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300817 return -EINVAL;
818 }
819
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700820 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
821 return -EINVAL;
822
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300823 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
824
Paul Walmsley32a363c2011-07-10 05:56:54 -0600825 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600826 ret = arch_clkdm->clkdm_wakeup(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700827 ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
828
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600829 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300830}
831
832/**
Paul Walmsley3a090282013-01-26 00:58:16 -0700833 * clkdm_wakeup - force clockdomain wakeup transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300834 * @clkdm: struct clockdomain *
835 *
Paul Walmsley3a090282013-01-26 00:58:16 -0700836 * Instruct the CM to force a wakeup transition on the specified
837 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
838 * clockdomain does not support software-controlled wakeup; 0 upon
839 * success.
840 */
841int clkdm_wakeup(struct clockdomain *clkdm)
842{
843 int ret;
844
845 pwrdm_lock(clkdm->pwrdm.ptr);
846 ret = clkdm_wakeup_nolock(clkdm);
847 pwrdm_unlock(clkdm->pwrdm.ptr);
848
849 return ret;
850}
851
852/**
853 * clkdm_allow_idle_nolock - enable hwsup idle transitions for clkdm
854 * @clkdm: struct clockdomain *
855 *
856 * Allow the hardware to automatically switch the clockdomain @clkdm
857 * into active or idle states, as needed by downstream clocks. If the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300858 * clockdomain has any downstream clocks enabled in the clock
859 * framework, wkdep/sleepdep autodependencies are added; this is so
Paul Walmsley3a090282013-01-26 00:58:16 -0700860 * device drivers can read and write to the device. Only for use by
861 * the powerdomain code. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300862 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700863void clkdm_allow_idle_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300864{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300865 if (!clkdm)
866 return;
867
868 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600869 pr_debug("clock: %s: automatic idle transitions cannot be enabled\n",
870 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300871 return;
872 }
873
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700874 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
875 return;
876
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300877 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
878 clkdm->name);
879
Paul Walmsley32a363c2011-07-10 05:56:54 -0600880 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700881 arch_clkdm->clkdm_allow_idle(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700882 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
883}
884
885/**
886 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
887 * @clkdm: struct clockdomain *
888 *
889 * Allow the hardware to automatically switch the clockdomain @clkdm into
890 * active or idle states, as needed by downstream clocks. If the
891 * clockdomain has any downstream clocks enabled in the clock
892 * framework, wkdep/sleepdep autodependencies are added; this is so
893 * device drivers can read and write to the device. No return value.
894 */
895void clkdm_allow_idle(struct clockdomain *clkdm)
896{
897 pwrdm_lock(clkdm->pwrdm.ptr);
898 clkdm_allow_idle_nolock(clkdm);
899 pwrdm_unlock(clkdm->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300900}
901
902/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700903 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300904 * @clkdm: struct clockdomain *
905 *
906 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700907 * @clkdm into inactive or idle states. If the clockdomain has
908 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsley3a090282013-01-26 00:58:16 -0700909 * autodependencies are removed. Only for use by the powerdomain
910 * code. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300911 */
Paul Walmsley3a090282013-01-26 00:58:16 -0700912void clkdm_deny_idle_nolock(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300913{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300914 if (!clkdm)
915 return;
916
917 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600918 pr_debug("clockdomain: %s: automatic idle transitions cannot be disabled\n",
919 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300920 return;
921 }
922
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700923 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
924 return;
925
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300926 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
927 clkdm->name);
928
Paul Walmsley32a363c2011-07-10 05:56:54 -0600929 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700930 arch_clkdm->clkdm_deny_idle(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -0700931 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
932}
933
934/**
935 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
936 * @clkdm: struct clockdomain *
937 *
938 * Prevent the hardware from automatically switching the clockdomain
939 * @clkdm into inactive or idle states. If the clockdomain has
940 * downstream clocks enabled in the clock framework, wkdep/sleepdep
941 * autodependencies are removed. No return value.
942 */
943void clkdm_deny_idle(struct clockdomain *clkdm)
944{
945 pwrdm_lock(clkdm->pwrdm.ptr);
946 clkdm_deny_idle_nolock(clkdm);
947 pwrdm_unlock(clkdm->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300948}
949
Paul Walmsley32a363c2011-07-10 05:56:54 -0600950/**
951 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
952 * @clkdm: struct clockdomain *
953 *
954 * Returns true if clockdomain @clkdm currently has
955 * hardware-supervised idle enabled, or false if it does not or if
956 * @clkdm is NULL. It is only valid to call this function after
957 * clkdm_init() has been called. This function does not actually read
958 * bits from the hardware; it instead tests an in-memory flag that is
959 * changed whenever the clockdomain code changes the auto-idle mode.
960 */
961bool clkdm_in_hwsup(struct clockdomain *clkdm)
962{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600963 bool ret;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600964
Paul Walmsley32a363c2011-07-10 05:56:54 -0600965 if (!clkdm)
966 return false;
967
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600968 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600969
970 return ret;
Paul Walmsley32a363c2011-07-10 05:56:54 -0600971}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300972
Paul Walmsleyb71c7212012-09-23 17:28:28 -0600973/**
974 * clkdm_missing_idle_reporting - can @clkdm enter autoidle even if in use?
975 * @clkdm: struct clockdomain *
976 *
977 * Returns true if clockdomain @clkdm has the
978 * CLKDM_MISSING_IDLE_REPORTING flag set, or false if not or @clkdm is
979 * null. More information is available in the documentation for the
980 * CLKDM_MISSING_IDLE_REPORTING macro.
981 */
982bool clkdm_missing_idle_reporting(struct clockdomain *clkdm)
983{
984 if (!clkdm)
985 return false;
986
987 return (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING) ? true : false;
988}
989
Paul Walmsley65958fb2013-01-26 00:58:17 -0700990/* Public autodep handling functions (deprecated) */
991
992/**
993 * clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
994 * @clkdm: struct clockdomain *
995 *
996 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
997 * in hardware-supervised mode. Meant to be called from clock framework
998 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
999 *
1000 * XXX autodeps are deprecated and should be removed at the earliest
1001 * opportunity
1002 */
1003void clkdm_add_autodeps(struct clockdomain *clkdm)
1004{
1005 struct clkdm_autodep *autodep;
1006
1007 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
1008 return;
1009
1010 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
1011 if (IS_ERR(autodep->clkdm.ptr))
1012 continue;
1013
1014 pr_debug("clockdomain: %s: adding %s sleepdep/wkdep\n",
1015 clkdm->name, autodep->clkdm.ptr->name);
1016
1017 _clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
1018 _clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
1019 }
1020}
1021
1022/**
1023 * clkdm_del_autodeps - remove auto sleepdeps/wkdeps from clkdm
1024 * @clkdm: struct clockdomain *
1025 *
1026 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
1027 * in hardware-supervised mode. Meant to be called from clock framework
1028 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
1029 *
1030 * XXX autodeps are deprecated and should be removed at the earliest
1031 * opportunity
1032 */
1033void clkdm_del_autodeps(struct clockdomain *clkdm)
1034{
1035 struct clkdm_autodep *autodep;
1036
1037 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
1038 return;
1039
1040 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
1041 if (IS_ERR(autodep->clkdm.ptr))
1042 continue;
1043
1044 pr_debug("clockdomain: %s: removing %s sleepdep/wkdep\n",
1045 clkdm->name, autodep->clkdm.ptr->name);
1046
1047 _clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
1048 _clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
1049 }
1050}
1051
Benoit Cousson113a7412011-07-10 05:56:54 -06001052/* Clockdomain-to-clock/hwmod framework interface code */
1053
1054static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
1055{
1056 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
1057 return -EINVAL;
1058
Paul Walmsley3a090282013-01-26 00:58:16 -07001059 pwrdm_lock(clkdm->pwrdm.ptr);
Tero Kristo64e29fd2012-09-25 19:05:32 +03001060
Benoit Cousson113a7412011-07-10 05:56:54 -06001061 /*
1062 * For arch's with no autodeps, clkcm_clk_enable
1063 * should be called for every clock instance or hwmod that is
1064 * enabled, so the clkdm can be force woken up.
1065 */
Tero Kristo64e29fd2012-09-25 19:05:32 +03001066 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001067 pwrdm_unlock(clkdm->pwrdm.ptr);
Benoit Cousson113a7412011-07-10 05:56:54 -06001068 return 0;
Tero Kristo64e29fd2012-09-25 19:05:32 +03001069 }
Benoit Cousson113a7412011-07-10 05:56:54 -06001070
1071 arch_clkdm->clkdm_clk_enable(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -07001072 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1073 pwrdm_unlock(clkdm->pwrdm.ptr);
Benoit Cousson113a7412011-07-10 05:56:54 -06001074
Paul Walmsley7852ec02012-07-26 00:54:26 -06001075 pr_debug("clockdomain: %s: enabled\n", clkdm->name);
Benoit Cousson113a7412011-07-10 05:56:54 -06001076
1077 return 0;
1078}
1079
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001080/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001081 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001082 * @clkdm: struct clockdomain *
1083 * @clk: struct clk * of the enabled downstream clock
1084 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -07001085 * Increment the usecount of the clockdomain @clkdm and ensure that it
1086 * is awake before @clk is enabled. Intended to be called by
1087 * clk_enable() code. If the clockdomain is in software-supervised
1088 * idle mode, force the clockdomain to wake. If the clockdomain is in
1089 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
1090 * ensure that devices in the clockdomain can be read from/written to
1091 * by on-chip processors. Returns -EINVAL if passed null pointers;
1092 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001093 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001094int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001095{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001096 /*
1097 * XXX Rewrite this code to maintain a list of enabled
1098 * downstream clocks for debugging purposes?
1099 */
1100
Benoit Cousson113a7412011-07-10 05:56:54 -06001101 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001102 return -EINVAL;
1103
Benoit Cousson113a7412011-07-10 05:56:54 -06001104 return _clkdm_clk_hwmod_enable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001105}
1106
1107/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001108 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001109 * @clkdm: struct clockdomain *
1110 * @clk: struct clk * of the disabled downstream clock
1111 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -07001112 * Decrement the usecount of this clockdomain @clkdm when @clk is
1113 * disabled. Intended to be called by clk_disable() code. If the
1114 * clockdomain usecount goes to 0, put the clockdomain to sleep
1115 * (software-supervised mode) or remove the clkdm autodependencies
1116 * (hardware-supervised mode). Returns -EINVAL if passed null
Benoit Cousson113a7412011-07-10 05:56:54 -06001117 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1118 * upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001119 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001120int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001121{
Mike Turquetted043d872012-11-09 11:28:42 -07001122 if (!clkdm || !clk || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001123 return -EINVAL;
1124
Paul Walmsley3a090282013-01-26 00:58:16 -07001125 pwrdm_lock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001126
Mike Turquetted043d872012-11-09 11:28:42 -07001127 /* corner case: disabling unused clocks */
Jon Hunter29f06672012-12-15 01:35:54 -07001128 if ((__clk_get_enable_count(clk) == 0) &&
1129 (atomic_read(&clkdm->usecount) == 0))
Mike Turquetted043d872012-11-09 11:28:42 -07001130 goto ccd_exit;
Mike Turquetted043d872012-11-09 11:28:42 -07001131
1132 if (atomic_read(&clkdm->usecount) == 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001133 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001134 WARN_ON(1); /* underflow */
1135 return -ERANGE;
1136 }
1137
1138 if (atomic_dec_return(&clkdm->usecount) > 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001139 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001140 return 0;
1141 }
1142
1143 arch_clkdm->clkdm_clk_disable(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -07001144 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001145
1146 pr_debug("clockdomain: %s: disabled\n", clkdm->name);
1147
Mike Turquetted043d872012-11-09 11:28:42 -07001148ccd_exit:
Paul Walmsley3a090282013-01-26 00:58:16 -07001149 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001150
1151 return 0;
Benoit Cousson113a7412011-07-10 05:56:54 -06001152}
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001153
Benoit Cousson113a7412011-07-10 05:56:54 -06001154/**
1155 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1156 * @clkdm: struct clockdomain *
1157 * @oh: struct omap_hwmod * of the enabled downstream hwmod
1158 *
1159 * Increment the usecount of the clockdomain @clkdm and ensure that it
1160 * is awake before @oh is enabled. Intended to be called by
1161 * module_enable() code.
1162 * If the clockdomain is in software-supervised idle mode, force the
1163 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
1164 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1165 * clockdomain can be read from/written to by on-chip processors.
1166 * Returns -EINVAL if passed null pointers;
1167 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1168 */
1169int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1170{
1171 /* The clkdm attribute does not exist yet prior OMAP4 */
1172 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001173 return 0;
1174
Benoit Cousson113a7412011-07-10 05:56:54 -06001175 /*
1176 * XXX Rewrite this code to maintain a list of enabled
1177 * downstream hwmods for debugging purposes?
1178 */
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001179
Benoit Cousson113a7412011-07-10 05:56:54 -06001180 if (!oh)
1181 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001182
Benoit Cousson113a7412011-07-10 05:56:54 -06001183 return _clkdm_clk_hwmod_enable(clkdm);
1184}
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001185
Benoit Cousson113a7412011-07-10 05:56:54 -06001186/**
1187 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1188 * @clkdm: struct clockdomain *
1189 * @oh: struct omap_hwmod * of the disabled downstream hwmod
1190 *
1191 * Decrement the usecount of this clockdomain @clkdm when @oh is
1192 * disabled. Intended to be called by module_disable() code.
1193 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1194 * (software-supervised mode) or remove the clkdm autodependencies
1195 * (hardware-supervised mode).
1196 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1197 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1198 * idle mode.
1199 */
1200int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1201{
1202 /* The clkdm attribute does not exist yet prior OMAP4 */
1203 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1204 return 0;
1205
1206 /*
1207 * XXX Rewrite this code to maintain a list of enabled
1208 * downstream hwmods for debugging purposes?
1209 */
1210
Mike Turquetted043d872012-11-09 11:28:42 -07001211 if (!clkdm || !oh || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
Benoit Cousson113a7412011-07-10 05:56:54 -06001212 return -EINVAL;
1213
Paul Walmsley3a090282013-01-26 00:58:16 -07001214 pwrdm_lock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001215
1216 if (atomic_read(&clkdm->usecount) == 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001217 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001218 WARN_ON(1); /* underflow */
1219 return -ERANGE;
1220 }
1221
1222 if (atomic_dec_return(&clkdm->usecount) > 0) {
Paul Walmsley3a090282013-01-26 00:58:16 -07001223 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001224 return 0;
1225 }
1226
1227 arch_clkdm->clkdm_clk_disable(clkdm);
Paul Walmsley3a090282013-01-26 00:58:16 -07001228 pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1229 pwrdm_unlock(clkdm->pwrdm.ptr);
Mike Turquetted043d872012-11-09 11:28:42 -07001230
1231 pr_debug("clockdomain: %s: disabled\n", clkdm->name);
1232
1233 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001234}
1235