blob: b73a1dc37dfb4b07cafbefe9052ba0bc84313c5c [file] [log] [blame]
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001/*
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07002 * OMAP2/3/4 clockdomain framework functions
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03003 *
Paul Walmsley32a363c2011-07-10 05:56:54 -06004 * Copyright (C) 2008-2011 Texas Instruments, Inc.
5 * Copyright (C) 2008-2011 Nokia Corporation
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03006 *
7 * Written by Paul Walmsley and Jouni Högander
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07008 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
Paul Walmsley33903eb2009-12-08 16:33:10 -070014#undef DEBUG
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030015
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030016#include <linux/kernel.h>
17#include <linux/device.h>
18#include <linux/list.h>
19#include <linux/errno.h>
20#include <linux/delay.h>
21#include <linux/clk.h>
22#include <linux/limits.h>
Paul Walmsley5b74c672009-02-03 02:10:03 -070023#include <linux/err.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030024
25#include <linux/io.h>
26
27#include <linux/bitops.h>
28
Paul Walmsley55ed9692010-01-26 20:12:59 -070029#include <plat/clock.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -070030#include "clockdomain.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030031
32/* clkdm_list contains all registered struct clockdomains */
33static LIST_HEAD(clkdm_list);
34
Paul Walmsley55ed9692010-01-26 20:12:59 -070035/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
36static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030037
Rajendra Nayak32d40342011-02-25 16:06:47 -070038static struct clkdm_ops *arch_clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030039
40/* Private functions */
41
Paul Walmsley55ed9692010-01-26 20:12:59 -070042static struct clockdomain *_clkdm_lookup(const char *name)
43{
44 struct clockdomain *clkdm, *temp_clkdm;
45
46 if (!name)
47 return NULL;
48
49 clkdm = NULL;
50
51 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
52 if (!strcmp(name, temp_clkdm->name)) {
53 clkdm = temp_clkdm;
54 break;
55 }
56 }
57
58 return clkdm;
59}
60
Paul Walmsleye909d622010-01-26 20:13:00 -070061/**
62 * _clkdm_register - register a clockdomain
63 * @clkdm: struct clockdomain * to register
64 *
65 * Adds a clockdomain to the internal clockdomain list.
66 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
67 * already registered by the provided name, or 0 upon success.
68 */
69static int _clkdm_register(struct clockdomain *clkdm)
70{
71 struct powerdomain *pwrdm;
72
73 if (!clkdm || !clkdm->name)
74 return -EINVAL;
75
76 if (!omap_chip_is(clkdm->omap_chip))
77 return -EINVAL;
78
79 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
80 if (!pwrdm) {
81 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
82 clkdm->name, clkdm->pwrdm.name);
83 return -EINVAL;
84 }
85 clkdm->pwrdm.ptr = pwrdm;
86
87 /* Verify that the clockdomain is not already registered */
88 if (_clkdm_lookup(clkdm->name))
89 return -EEXIST;
90
91 list_add(&clkdm->node, &clkdm_list);
92
93 pwrdm_add_clkdm(pwrdm, clkdm);
94
Rajendra Nayak555e74e2011-07-10 05:56:55 -060095 spin_lock_init(&clkdm->lock);
96
Paul Walmsleye909d622010-01-26 20:13:00 -070097 pr_debug("clockdomain: registered %s\n", clkdm->name);
98
99 return 0;
100}
101
Paul Walmsley55ed9692010-01-26 20:12:59 -0700102/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
103static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
104 struct clkdm_dep *deps)
105{
106 struct clkdm_dep *cd;
107
108 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
109 return ERR_PTR(-EINVAL);
110
111 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700112 if (!omap_chip_is(cd->omap_chip))
113 continue;
114
115 if (!cd->clkdm && cd->clkdm_name)
116 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
117
118 if (cd->clkdm == clkdm)
119 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700120 }
121
122 if (!cd->clkdm_name)
123 return ERR_PTR(-ENOENT);
124
125 return cd;
126}
127
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300128/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700129 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
130 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300131 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700132 * Resolve autodep clockdomain names to clockdomain pointers via
133 * clkdm_lookup() and store the pointers in the autodep structure. An
134 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300135 * automatically added and removed whenever clocks in the associated
136 * clockdomain are enabled or disabled (respectively) when the
137 * clockdomain is in hardware-supervised mode. Meant to be called
138 * once at clockdomain layer initialization, since these should remain
139 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700140 *
141 * XXX autodeps are deprecated and should be removed at the earliest
142 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300143 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700144static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300145{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700146 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300147
148 if (!autodep)
149 return;
150
151 if (!omap_chip_is(autodep->omap_chip))
152 return;
153
Paul Walmsley55ed9692010-01-26 20:12:59 -0700154 clkdm = clkdm_lookup(autodep->clkdm.name);
155 if (!clkdm) {
156 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
157 autodep->clkdm.name);
158 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300159 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700160 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300161}
162
163/*
164 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
165 * @clkdm: struct clockdomain *
166 *
167 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
168 * in hardware-supervised mode. Meant to be called from clock framework
169 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700170 *
171 * XXX autodeps are deprecated and should be removed at the earliest
172 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300173 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700174void _clkdm_add_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300175{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700176 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300177
Paul Walmsley570b54c2011-03-10 03:50:09 -0700178 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700179 return;
180
Paul Walmsley55ed9692010-01-26 20:12:59 -0700181 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
182 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300183 continue;
184
Paul Walmsleyd96df002009-01-27 19:44:35 -0700185 if (!omap_chip_is(autodep->omap_chip))
186 continue;
187
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300188 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700189 "clkdm %s\n", autodep->clkdm.ptr->name,
190 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300191
Paul Walmsley55ed9692010-01-26 20:12:59 -0700192 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
193 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300194 }
195}
196
197/*
198 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
199 * @clkdm: struct clockdomain *
200 *
201 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
202 * in hardware-supervised mode. Meant to be called from clock framework
203 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700204 *
205 * XXX autodeps are deprecated and should be removed at the earliest
206 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300207 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700208void _clkdm_del_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300209{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700210 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300211
Paul Walmsley570b54c2011-03-10 03:50:09 -0700212 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700213 return;
214
Paul Walmsley55ed9692010-01-26 20:12:59 -0700215 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
216 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300217 continue;
218
Paul Walmsleyd96df002009-01-27 19:44:35 -0700219 if (!omap_chip_is(autodep->omap_chip))
220 continue;
221
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300222 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700223 "clkdm %s\n", autodep->clkdm.ptr->name,
224 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300225
Paul Walmsley55ed9692010-01-26 20:12:59 -0700226 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
227 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300228 }
229}
230
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700231/**
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700232 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
233 * @clkdm: clockdomain that we are resolving dependencies for
234 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600235 *
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700236 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
237 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700238 * No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700239 */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700240static void _resolve_clkdm_deps(struct clockdomain *clkdm,
241 struct clkdm_dep *clkdm_deps)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600242{
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700243 struct clkdm_dep *cd;
244
245 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
246 if (!omap_chip_is(cd->omap_chip))
247 continue;
248 if (cd->clkdm)
249 continue;
250 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
251
252 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
253 clkdm->name, cd->clkdm_name);
254 }
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600255}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300256
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300257/* Public functions */
258
259/**
Paul Walmsley08cb9702011-09-14 16:01:20 -0600260 * clkdm_register_platform_funcs - register clockdomain implementation fns
261 * @co: func pointers for arch specific implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300262 *
Paul Walmsley08cb9702011-09-14 16:01:20 -0600263 * Register the list of function pointers used to implement the
264 * clockdomain functions on different OMAP SoCs. Should be called
265 * before any other clkdm_register*() function. Returns -EINVAL if
266 * @co is null, -EEXIST if platform functions have already been
267 * registered, or 0 upon success.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300268 */
Paul Walmsley08cb9702011-09-14 16:01:20 -0600269int clkdm_register_platform_funcs(struct clkdm_ops *co)
270{
271 if (!co)
272 return -EINVAL;
273
274 if (arch_clkdm)
275 return -EEXIST;
276
277 arch_clkdm = co;
278
279 return 0;
280};
281
282/**
283 * clkdm_register_clkdms - register SoC clockdomains
284 * @cs: pointer to an array of struct clockdomain to register
285 *
286 * Register the clockdomains available on a particular OMAP SoC. Must
287 * be called after clkdm_register_platform_funcs(). May be called
288 * multiple times. Returns -EACCES if called before
289 * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
290 * null; or 0 upon success.
291 */
292int clkdm_register_clkdms(struct clockdomain **cs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300293{
294 struct clockdomain **c = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300295
Paul Walmsley08cb9702011-09-14 16:01:20 -0600296 if (!arch_clkdm)
297 return -EACCES;
Rajendra Nayak32d40342011-02-25 16:06:47 -0700298
Paul Walmsley08cb9702011-09-14 16:01:20 -0600299 if (!cs)
300 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300301
Paul Walmsley08cb9702011-09-14 16:01:20 -0600302 for (c = cs; *c; c++)
303 _clkdm_register(*c);
304
305 return 0;
306}
307
308/**
309 * clkdm_register_autodeps - register autodeps (if required)
310 * @ia: pointer to a static array of struct clkdm_autodep to register
311 *
312 * Register clockdomain "automatic dependencies." These are
313 * clockdomain wakeup and sleep dependencies that are automatically
314 * added whenever the first clock inside a clockdomain is enabled, and
315 * removed whenever the last clock inside a clockdomain is disabled.
316 * These are currently only used on OMAP3 devices, and are deprecated,
317 * since they waste energy. However, until the OMAP2/3 IP block
318 * enable/disable sequence can be converted to match the OMAP4
319 * sequence, they are needed.
320 *
321 * Must be called only after all of the SoC clockdomains are
322 * registered, since the function will resolve autodep clockdomain
323 * names into clockdomain pointers.
324 *
325 * The struct clkdm_autodep @ia array must be static, as this function
326 * does not copy the array elements.
327 *
328 * Returns -EACCES if called before any clockdomains have been
329 * registered, -EINVAL if called with a null @ia argument, -EEXIST if
330 * autodeps have already been registered, or 0 upon success.
331 */
332int clkdm_register_autodeps(struct clkdm_autodep *ia)
333{
334 struct clkdm_autodep *a = NULL;
335
336 if (list_empty(&clkdm_list))
337 return -EACCES;
338
339 if (!ia)
340 return -EINVAL;
341
Paul Walmsley55ed9692010-01-26 20:12:59 -0700342 if (autodeps)
Paul Walmsley08cb9702011-09-14 16:01:20 -0600343 return -EEXIST;
Paul Walmsley369d5612010-01-26 20:13:01 -0700344
Paul Walmsley08cb9702011-09-14 16:01:20 -0600345 autodeps = ia;
346 for (a = autodeps; a->clkdm.ptr; a++)
347 _autodep_lookup(a);
348
349 return 0;
350}
351
352/**
353 * clkdm_complete_init - set up the clockdomain layer
354 *
355 * Put all clockdomains into software-supervised mode; PM code should
356 * later enable hardware-supervised mode as appropriate. Must be
357 * called after clkdm_register_clkdms(). Returns -EACCES if called
358 * before clkdm_register_clkdms(), or 0 upon success.
359 */
360int clkdm_complete_init(void)
361{
362 struct clockdomain *clkdm;
363
364 if (list_empty(&clkdm_list))
365 return -EACCES;
366
Paul Walmsley369d5612010-01-26 20:13:01 -0700367 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600368 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700369 clkdm_wakeup(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600370 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700371 clkdm_deny_idle(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600372
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700373 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600374 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700375
376 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600377 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700378 }
Paul Walmsley08cb9702011-09-14 16:01:20 -0600379
380 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300381}
382
383/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300384 * clkdm_lookup - look up a clockdomain by name, return a pointer
385 * @name: name of clockdomain
386 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700387 * Find a registered clockdomain by its name @name. Returns a pointer
388 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300389 */
390struct clockdomain *clkdm_lookup(const char *name)
391{
392 struct clockdomain *clkdm, *temp_clkdm;
393
394 if (!name)
395 return NULL;
396
397 clkdm = NULL;
398
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300399 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
400 if (!strcmp(name, temp_clkdm->name)) {
401 clkdm = temp_clkdm;
402 break;
403 }
404 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300405
406 return clkdm;
407}
408
409/**
410 * clkdm_for_each - call function on each registered clockdomain
411 * @fn: callback function *
412 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700413 * Call the supplied function @fn for each registered clockdomain.
414 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300415 * out early from the iterator. The callback function is called with
416 * the clkdm_mutex held, so no clockdomain structure manipulation
417 * functions should be called from the callback, although hardware
418 * clockdomain control functions are fine. Returns the last return
419 * value of the callback function, which should be 0 for success or
420 * anything else to indicate failure; or -EINVAL if the function pointer
421 * is null.
422 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300423int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
424 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300425{
426 struct clockdomain *clkdm;
427 int ret = 0;
428
429 if (!fn)
430 return -EINVAL;
431
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300432 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300433 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300434 if (ret)
435 break;
436 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300437
438 return ret;
439}
440
441
Paul Walmsleye89087c2008-05-20 18:41:35 -0600442/**
443 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
444 * @clkdm: struct clockdomain *
445 *
446 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700447 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600448 */
449struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
450{
451 if (!clkdm)
452 return NULL;
453
Paul Walmsley5b74c672009-02-03 02:10:03 -0700454 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600455}
456
457
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300458/* Hardware clockdomain control */
459
460/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700461 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
462 * @clkdm1: wake this struct clockdomain * up (dependent)
463 * @clkdm2: when this struct clockdomain * wakes up (source)
464 *
465 * When the clockdomain represented by @clkdm2 wakes up, wake up
466 * @clkdm1. Implemented in hardware on the OMAP, this feature is
467 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
468 * Returns -EINVAL if presented with invalid clockdomain pointers,
469 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
470 * success.
471 */
472int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
473{
474 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700475 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700476
Paul Walmsley55ed9692010-01-26 20:12:59 -0700477 if (!clkdm1 || !clkdm2)
478 return -EINVAL;
479
480 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700481 if (IS_ERR(cd))
482 ret = PTR_ERR(cd);
483
484 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
485 ret = -EINVAL;
486
487 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700488 pr_debug("clockdomain: hardware cannot set/clear wake up of "
489 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700490 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700491 }
492
Paul Walmsley369d5612010-01-26 20:13:01 -0700493 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
494 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
495 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700496
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700497 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700498 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700499
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700500 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700501}
502
503/**
504 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
505 * @clkdm1: wake this struct clockdomain * up (dependent)
506 * @clkdm2: when this struct clockdomain * wakes up (source)
507 *
508 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
509 * wakes up. Returns -EINVAL if presented with invalid clockdomain
510 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
511 * 0 upon success.
512 */
513int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
514{
515 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700516 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700517
Paul Walmsley55ed9692010-01-26 20:12:59 -0700518 if (!clkdm1 || !clkdm2)
519 return -EINVAL;
520
521 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700522 if (IS_ERR(cd))
523 ret = PTR_ERR(cd);
524
525 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
526 ret = -EINVAL;
527
528 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700529 pr_debug("clockdomain: hardware cannot set/clear wake up of "
530 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700531 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700532 }
533
Paul Walmsley369d5612010-01-26 20:13:01 -0700534 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
535 pr_debug("clockdomain: hardware will no longer wake up %s "
536 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700537
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700538 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700539 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700540
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700541 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700542}
543
544/**
545 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
546 * @clkdm1: wake this struct clockdomain * up (dependent)
547 * @clkdm2: when this struct clockdomain * wakes up (source)
548 *
549 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
550 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
551 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
552 * is incapable.
553 *
554 * REVISIT: Currently this function only represents software-controllable
555 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
556 * yet handled here.
557 */
558int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
559{
560 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700561 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700562
563 if (!clkdm1 || !clkdm2)
564 return -EINVAL;
565
566 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700567 if (IS_ERR(cd))
568 ret = PTR_ERR(cd);
569
570 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
571 ret = -EINVAL;
572
573 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700574 pr_debug("clockdomain: hardware cannot set/clear wake up of "
575 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700576 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700577 }
578
Paul Walmsley369d5612010-01-26 20:13:01 -0700579 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700580 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700581}
582
583/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700584 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
585 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
586 *
587 * Remove all inter-clockdomain wakeup dependencies that could cause
588 * @clkdm to wake. Intended to be used during boot to initialize the
589 * PRCM to a known state, after all clockdomains are put into swsup idle
590 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
591 * 0 upon success.
592 */
593int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
594{
Paul Walmsley369d5612010-01-26 20:13:01 -0700595 if (!clkdm)
596 return -EINVAL;
597
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700598 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
599 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700600
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700601 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700602}
603
604/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700605 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
606 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
607 * @clkdm2: when this struct clockdomain * is active (source)
608 *
609 * Prevent @clkdm1 from automatically going inactive (and then to
610 * retention or off) if @clkdm2 is active. Returns -EINVAL if
611 * presented with invalid clockdomain pointers or called on a machine
612 * that does not support software-configurable hardware sleep
613 * dependencies, -ENOENT if the specified dependency cannot be set in
614 * hardware, or 0 upon success.
615 */
616int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
617{
618 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700619 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700620
621 if (!clkdm1 || !clkdm2)
622 return -EINVAL;
623
624 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700625 if (IS_ERR(cd))
626 ret = PTR_ERR(cd);
627
628 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
629 ret = -EINVAL;
630
631 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700632 pr_debug("clockdomain: hardware cannot set/clear sleep "
633 "dependency affecting %s from %s\n", clkdm1->name,
634 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700635 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700636 }
637
Paul Walmsley369d5612010-01-26 20:13:01 -0700638 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
639 pr_debug("clockdomain: will prevent %s from sleeping if %s "
640 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700641
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700642 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700643 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700644
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700645 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700646}
647
648/**
649 * clkdm_del_sleepdep - remove 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 * Allow @clkdm1 to automatically go inactive (and then to retention or
654 * off), independent of the activity state of @clkdm2. Returns -EINVAL
655 * if presented with invalid clockdomain pointers or called on a machine
656 * that does not support software-configurable hardware sleep dependencies,
657 * -ENOENT if the specified dependency cannot be cleared in hardware, or
658 * 0 upon success.
659 */
660int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
661{
662 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700663 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700664
665 if (!clkdm1 || !clkdm2)
666 return -EINVAL;
667
668 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700669 if (IS_ERR(cd))
670 ret = PTR_ERR(cd);
671
672 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
673 ret = -EINVAL;
674
675 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700676 pr_debug("clockdomain: hardware cannot set/clear sleep "
677 "dependency affecting %s from %s\n", clkdm1->name,
678 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700679 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700680 }
681
Paul Walmsley369d5612010-01-26 20:13:01 -0700682 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
683 pr_debug("clockdomain: will no longer prevent %s from "
684 "sleeping if %s is active\n", clkdm1->name,
685 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700686
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700687 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700688 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700689
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700690 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700691}
692
693/**
694 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
695 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
696 * @clkdm2: when this struct clockdomain * is active (source)
697 *
698 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
699 * not be allowed to automatically go inactive if @clkdm2 is active;
700 * 0 if @clkdm1's automatic power state inactivity transition is independent
701 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
702 * on a machine that does not support software-configurable hardware sleep
703 * dependencies; or -ENOENT if the hardware is incapable.
704 *
705 * REVISIT: Currently this function only represents software-controllable
706 * sleep dependencies. Sleep dependencies fixed in hardware are not
707 * yet handled here.
708 */
709int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
710{
711 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700712 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700713
714 if (!clkdm1 || !clkdm2)
715 return -EINVAL;
716
717 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700718 if (IS_ERR(cd))
719 ret = PTR_ERR(cd);
720
721 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
722 ret = -EINVAL;
723
724 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700725 pr_debug("clockdomain: hardware cannot set/clear sleep "
726 "dependency affecting %s from %s\n", clkdm1->name,
727 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700728 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700729 }
730
Paul Walmsley369d5612010-01-26 20:13:01 -0700731 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700732 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700733}
734
Paul Walmsley369d5612010-01-26 20:13:01 -0700735/**
736 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
737 * @clkdm: struct clockdomain * to remove all sleep dependencies from
738 *
739 * Remove all inter-clockdomain sleep dependencies that could prevent
740 * @clkdm from idling. Intended to be used during boot to initialize the
741 * PRCM to a known state, after all clockdomains are put into swsup idle
742 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
743 * 0 upon success.
744 */
745int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
746{
Paul Walmsley369d5612010-01-26 20:13:01 -0700747 if (!clkdm)
748 return -EINVAL;
749
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700750 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
751 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700752
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700753 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700754}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700755
756/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700757 * clkdm_sleep - force clockdomain sleep transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300758 * @clkdm: struct clockdomain *
759 *
760 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700761 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300762 * clockdomain does not support software-initiated sleep; 0 upon
763 * success.
764 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700765int clkdm_sleep(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300766{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600767 int ret;
768 unsigned long flags;
769
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300770 if (!clkdm)
771 return -EINVAL;
772
773 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
774 pr_debug("clockdomain: %s does not support forcing "
775 "sleep via software\n", clkdm->name);
776 return -EINVAL;
777 }
778
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700779 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
780 return -EINVAL;
781
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300782 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
783
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600784 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600785 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600786 ret = arch_clkdm->clkdm_sleep(clkdm);
787 spin_unlock_irqrestore(&clkdm->lock, flags);
788 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300789}
790
791/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700792 * clkdm_wakeup - force clockdomain wakeup transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300793 * @clkdm: struct clockdomain *
794 *
795 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700796 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300797 * clockdomain does not support software-controlled wakeup; 0 upon
798 * success.
799 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700800int clkdm_wakeup(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300801{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600802 int ret;
803 unsigned long flags;
804
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300805 if (!clkdm)
806 return -EINVAL;
807
808 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
809 pr_debug("clockdomain: %s does not support forcing "
810 "wakeup via software\n", clkdm->name);
811 return -EINVAL;
812 }
813
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700814 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
815 return -EINVAL;
816
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300817 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
818
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600819 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600820 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600821 ret = arch_clkdm->clkdm_wakeup(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600822 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600823 spin_unlock_irqrestore(&clkdm->lock, flags);
824 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300825}
826
827/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700828 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300829 * @clkdm: struct clockdomain *
830 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700831 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300832 * active or idle states, as needed by downstream clocks. If the
833 * clockdomain has any downstream clocks enabled in the clock
834 * framework, wkdep/sleepdep autodependencies are added; this is so
835 * device drivers can read and write to the device. No return value.
836 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700837void clkdm_allow_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300838{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600839 unsigned long flags;
840
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300841 if (!clkdm)
842 return;
843
844 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
845 pr_debug("clock: automatic idle transitions cannot be enabled "
846 "on clockdomain %s\n", clkdm->name);
847 return;
848 }
849
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700850 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
851 return;
852
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300853 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
854 clkdm->name);
855
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600856 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600857 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700858 arch_clkdm->clkdm_allow_idle(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300859 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600860 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300861}
862
863/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700864 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300865 * @clkdm: struct clockdomain *
866 *
867 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700868 * @clkdm into inactive or idle states. If the clockdomain has
869 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300870 * autodependencies are removed. No return value.
871 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700872void clkdm_deny_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300873{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600874 unsigned long flags;
875
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300876 if (!clkdm)
877 return;
878
879 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
880 pr_debug("clockdomain: automatic idle transitions cannot be "
881 "disabled on %s\n", clkdm->name);
882 return;
883 }
884
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700885 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
886 return;
887
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300888 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
889 clkdm->name);
890
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600891 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600892 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700893 arch_clkdm->clkdm_deny_idle(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600894 pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600895 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300896}
897
Paul Walmsley32a363c2011-07-10 05:56:54 -0600898/**
899 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
900 * @clkdm: struct clockdomain *
901 *
902 * Returns true if clockdomain @clkdm currently has
903 * hardware-supervised idle enabled, or false if it does not or if
904 * @clkdm is NULL. It is only valid to call this function after
905 * clkdm_init() has been called. This function does not actually read
906 * bits from the hardware; it instead tests an in-memory flag that is
907 * changed whenever the clockdomain code changes the auto-idle mode.
908 */
909bool clkdm_in_hwsup(struct clockdomain *clkdm)
910{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600911 bool ret;
912 unsigned long flags;
913
Paul Walmsley32a363c2011-07-10 05:56:54 -0600914 if (!clkdm)
915 return false;
916
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600917 spin_lock_irqsave(&clkdm->lock, flags);
918 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
919 spin_unlock_irqrestore(&clkdm->lock, flags);
920
921 return ret;
Paul Walmsley32a363c2011-07-10 05:56:54 -0600922}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300923
Benoit Cousson113a7412011-07-10 05:56:54 -0600924/* Clockdomain-to-clock/hwmod framework interface code */
925
926static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
927{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600928 unsigned long flags;
929
Benoit Cousson113a7412011-07-10 05:56:54 -0600930 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
931 return -EINVAL;
932
933 /*
934 * For arch's with no autodeps, clkcm_clk_enable
935 * should be called for every clock instance or hwmod that is
936 * enabled, so the clkdm can be force woken up.
937 */
938 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
939 return 0;
940
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600941 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600942 arch_clkdm->clkdm_clk_enable(clkdm);
943 pwrdm_wait_transition(clkdm->pwrdm.ptr);
944 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600945 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600946
947 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
948
949 return 0;
950}
951
952static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
953{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600954 unsigned long flags;
955
Benoit Cousson113a7412011-07-10 05:56:54 -0600956 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
957 return -EINVAL;
958
959 if (atomic_read(&clkdm->usecount) == 0) {
960 WARN_ON(1); /* underflow */
961 return -ERANGE;
962 }
963
964 if (atomic_dec_return(&clkdm->usecount) > 0)
965 return 0;
966
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600967 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600968 arch_clkdm->clkdm_clk_disable(clkdm);
969 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600970 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600971
972 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
973
974 return 0;
975}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300976
977/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700978 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300979 * @clkdm: struct clockdomain *
980 * @clk: struct clk * of the enabled downstream clock
981 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700982 * Increment the usecount of the clockdomain @clkdm and ensure that it
983 * is awake before @clk is enabled. Intended to be called by
984 * clk_enable() code. If the clockdomain is in software-supervised
985 * idle mode, force the clockdomain to wake. If the clockdomain is in
986 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
987 * ensure that devices in the clockdomain can be read from/written to
988 * by on-chip processors. Returns -EINVAL if passed null pointers;
989 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300990 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700991int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300992{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300993 /*
994 * XXX Rewrite this code to maintain a list of enabled
995 * downstream clocks for debugging purposes?
996 */
997
Benoit Cousson113a7412011-07-10 05:56:54 -0600998 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300999 return -EINVAL;
1000
Benoit Cousson113a7412011-07-10 05:56:54 -06001001 return _clkdm_clk_hwmod_enable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001002}
1003
1004/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001005 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001006 * @clkdm: struct clockdomain *
1007 * @clk: struct clk * of the disabled downstream clock
1008 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -07001009 * Decrement the usecount of this clockdomain @clkdm when @clk is
1010 * disabled. Intended to be called by clk_disable() code. If the
1011 * clockdomain usecount goes to 0, put the clockdomain to sleep
1012 * (software-supervised mode) or remove the clkdm autodependencies
1013 * (hardware-supervised mode). Returns -EINVAL if passed null
Benoit Cousson113a7412011-07-10 05:56:54 -06001014 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1015 * upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001016 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001017int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001018{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001019 /*
1020 * XXX Rewrite this code to maintain a list of enabled
1021 * downstream clocks for debugging purposes?
1022 */
1023
Benoit Cousson113a7412011-07-10 05:56:54 -06001024 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001025 return -EINVAL;
1026
Benoit Cousson113a7412011-07-10 05:56:54 -06001027 return _clkdm_clk_hwmod_disable(clkdm);
1028}
Rajendra Nayak4da71ae2011-02-25 16:06:48 -07001029
Benoit Cousson113a7412011-07-10 05:56:54 -06001030/**
1031 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1032 * @clkdm: struct clockdomain *
1033 * @oh: struct omap_hwmod * of the enabled downstream hwmod
1034 *
1035 * Increment the usecount of the clockdomain @clkdm and ensure that it
1036 * is awake before @oh is enabled. Intended to be called by
1037 * module_enable() code.
1038 * If the clockdomain is in software-supervised idle mode, force the
1039 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
1040 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1041 * clockdomain can be read from/written to by on-chip processors.
1042 * Returns -EINVAL if passed null pointers;
1043 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1044 */
1045int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1046{
1047 /* The clkdm attribute does not exist yet prior OMAP4 */
1048 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001049 return 0;
1050
Benoit Cousson113a7412011-07-10 05:56:54 -06001051 /*
1052 * XXX Rewrite this code to maintain a list of enabled
1053 * downstream hwmods for debugging purposes?
1054 */
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001055
Benoit Cousson113a7412011-07-10 05:56:54 -06001056 if (!oh)
1057 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001058
Benoit Cousson113a7412011-07-10 05:56:54 -06001059 return _clkdm_clk_hwmod_enable(clkdm);
1060}
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001061
Benoit Cousson113a7412011-07-10 05:56:54 -06001062/**
1063 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1064 * @clkdm: struct clockdomain *
1065 * @oh: struct omap_hwmod * of the disabled downstream hwmod
1066 *
1067 * Decrement the usecount of this clockdomain @clkdm when @oh is
1068 * disabled. Intended to be called by module_disable() code.
1069 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1070 * (software-supervised mode) or remove the clkdm autodependencies
1071 * (hardware-supervised mode).
1072 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1073 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1074 * idle mode.
1075 */
1076int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1077{
1078 /* The clkdm attribute does not exist yet prior OMAP4 */
1079 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1080 return 0;
1081
1082 /*
1083 * XXX Rewrite this code to maintain a list of enabled
1084 * downstream hwmods for debugging purposes?
1085 */
1086
1087 if (!oh)
1088 return -EINVAL;
1089
1090 return _clkdm_clk_hwmod_disable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001091}
1092