blob: fe36081b0d675a45c3f8811151b35b511ebfbb40 [file] [log] [blame]
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001/*
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07002 * OMAP2/3/4 clockdomain framework functions
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03003 *
Paul Walmsley32a363c2011-07-10 05:56:54 -06004 * Copyright (C) 2008-2011 Texas Instruments, Inc.
5 * Copyright (C) 2008-2011 Nokia Corporation
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03006 *
7 * Written by Paul Walmsley and Jouni Högander
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07008 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
Paul Walmsley33903eb2009-12-08 16:33:10 -070014#undef DEBUG
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030015
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030016#include <linux/kernel.h>
17#include <linux/device.h>
18#include <linux/list.h>
19#include <linux/errno.h>
Paul Gortmakerd44b28c2011-07-31 10:52:44 -040020#include <linux/string.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030021#include <linux/delay.h>
22#include <linux/clk.h>
23#include <linux/limits.h>
Paul Walmsley5b74c672009-02-03 02:10:03 -070024#include <linux/err.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030025
26#include <linux/io.h>
27
28#include <linux/bitops.h>
29
Paul Walmsley55ed9692010-01-26 20:12:59 -070030#include <plat/clock.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -070031#include "clockdomain.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030032
33/* clkdm_list contains all registered struct clockdomains */
34static LIST_HEAD(clkdm_list);
35
Paul Walmsley55ed9692010-01-26 20:12:59 -070036/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
37static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030038
Rajendra Nayak32d40342011-02-25 16:06:47 -070039static struct clkdm_ops *arch_clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030040
41/* Private functions */
42
Paul Walmsley55ed9692010-01-26 20:12:59 -070043static struct clockdomain *_clkdm_lookup(const char *name)
44{
45 struct clockdomain *clkdm, *temp_clkdm;
46
47 if (!name)
48 return NULL;
49
50 clkdm = NULL;
51
52 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
53 if (!strcmp(name, temp_clkdm->name)) {
54 clkdm = temp_clkdm;
55 break;
56 }
57 }
58
59 return clkdm;
60}
61
Paul Walmsleye909d62a82010-01-26 20:13:00 -070062/**
63 * _clkdm_register - register a clockdomain
64 * @clkdm: struct clockdomain * to register
65 *
66 * Adds a clockdomain to the internal clockdomain list.
67 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
68 * already registered by the provided name, or 0 upon success.
69 */
70static int _clkdm_register(struct clockdomain *clkdm)
71{
72 struct powerdomain *pwrdm;
73
74 if (!clkdm || !clkdm->name)
75 return -EINVAL;
76
77 if (!omap_chip_is(clkdm->omap_chip))
78 return -EINVAL;
79
80 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
81 if (!pwrdm) {
82 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
83 clkdm->name, clkdm->pwrdm.name);
84 return -EINVAL;
85 }
86 clkdm->pwrdm.ptr = pwrdm;
87
88 /* Verify that the clockdomain is not already registered */
89 if (_clkdm_lookup(clkdm->name))
90 return -EEXIST;
91
92 list_add(&clkdm->node, &clkdm_list);
93
94 pwrdm_add_clkdm(pwrdm, clkdm);
95
Rajendra Nayak555e74e2011-07-10 05:56:55 -060096 spin_lock_init(&clkdm->lock);
97
Paul Walmsleye909d62a82010-01-26 20:13:00 -070098 pr_debug("clockdomain: registered %s\n", clkdm->name);
99
100 return 0;
101}
102
Paul Walmsley55ed9692010-01-26 20:12:59 -0700103/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
104static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
105 struct clkdm_dep *deps)
106{
107 struct clkdm_dep *cd;
108
109 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
110 return ERR_PTR(-EINVAL);
111
112 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700113 if (!omap_chip_is(cd->omap_chip))
114 continue;
115
116 if (!cd->clkdm && cd->clkdm_name)
117 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
118
119 if (cd->clkdm == clkdm)
120 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700121 }
122
123 if (!cd->clkdm_name)
124 return ERR_PTR(-ENOENT);
125
126 return cd;
127}
128
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300129/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700130 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
131 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300132 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700133 * Resolve autodep clockdomain names to clockdomain pointers via
134 * clkdm_lookup() and store the pointers in the autodep structure. An
135 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300136 * automatically added and removed whenever clocks in the associated
137 * clockdomain are enabled or disabled (respectively) when the
138 * clockdomain is in hardware-supervised mode. Meant to be called
139 * once at clockdomain layer initialization, since these should remain
140 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700141 *
142 * XXX autodeps are deprecated and should be removed at the earliest
143 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300144 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700145static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300146{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700147 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300148
149 if (!autodep)
150 return;
151
152 if (!omap_chip_is(autodep->omap_chip))
153 return;
154
Paul Walmsley55ed9692010-01-26 20:12:59 -0700155 clkdm = clkdm_lookup(autodep->clkdm.name);
156 if (!clkdm) {
157 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
158 autodep->clkdm.name);
159 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300160 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700161 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300162}
163
164/*
165 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
166 * @clkdm: struct clockdomain *
167 *
168 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
169 * in hardware-supervised mode. Meant to be called from clock framework
170 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700171 *
172 * XXX autodeps are deprecated and should be removed at the earliest
173 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300174 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700175void _clkdm_add_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300176{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700177 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300178
Paul Walmsley570b54c2011-03-10 03:50:09 -0700179 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700180 return;
181
Paul Walmsley55ed9692010-01-26 20:12:59 -0700182 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
183 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300184 continue;
185
Paul Walmsleyd96df002009-01-27 19:44:35 -0700186 if (!omap_chip_is(autodep->omap_chip))
187 continue;
188
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300189 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700190 "clkdm %s\n", autodep->clkdm.ptr->name,
191 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300192
Paul Walmsley55ed9692010-01-26 20:12:59 -0700193 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
194 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300195 }
196}
197
198/*
199 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
200 * @clkdm: struct clockdomain *
201 *
202 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
203 * in hardware-supervised mode. Meant to be called from clock framework
204 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700205 *
206 * XXX autodeps are deprecated and should be removed at the earliest
207 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300208 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700209void _clkdm_del_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300210{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700211 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300212
Paul Walmsley570b54c2011-03-10 03:50:09 -0700213 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700214 return;
215
Paul Walmsley55ed9692010-01-26 20:12:59 -0700216 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
217 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300218 continue;
219
Paul Walmsleyd96df002009-01-27 19:44:35 -0700220 if (!omap_chip_is(autodep->omap_chip))
221 continue;
222
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300223 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700224 "clkdm %s\n", autodep->clkdm.ptr->name,
225 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300226
Paul Walmsley55ed9692010-01-26 20:12:59 -0700227 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
228 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300229 }
230}
231
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700232/**
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700233 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
234 * @clkdm: clockdomain that we are resolving dependencies for
235 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600236 *
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700237 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
238 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700239 * No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700240 */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700241static void _resolve_clkdm_deps(struct clockdomain *clkdm,
242 struct clkdm_dep *clkdm_deps)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600243{
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700244 struct clkdm_dep *cd;
245
246 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
247 if (!omap_chip_is(cd->omap_chip))
248 continue;
249 if (cd->clkdm)
250 continue;
251 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
252
253 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
254 clkdm->name, cd->clkdm_name);
255 }
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600256}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300257
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300258/* Public functions */
259
260/**
261 * clkdm_init - set up the clockdomain layer
262 * @clkdms: optional pointer to an array of clockdomains to register
263 * @init_autodeps: optional pointer to an array of autodeps to register
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300264 * @custom_funcs: func pointers for arch specific implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300265 *
266 * Set up internal state. If a pointer to an array of clockdomains
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700267 * @clkdms was supplied, loop through the list of clockdomains,
268 * register all that are available on the current platform. Similarly,
269 * if a pointer to an array of clockdomain autodependencies
270 * @init_autodeps was provided, register those. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300271 */
272void clkdm_init(struct clockdomain **clkdms,
Rajendra Nayak32d40342011-02-25 16:06:47 -0700273 struct clkdm_autodep *init_autodeps,
274 struct clkdm_ops *custom_funcs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300275{
276 struct clockdomain **c = NULL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700277 struct clockdomain *clkdm;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700278 struct clkdm_autodep *autodep = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300279
Rajendra Nayak32d40342011-02-25 16:06:47 -0700280 if (!custom_funcs)
281 WARN(1, "No custom clkdm functions registered\n");
282 else
283 arch_clkdm = custom_funcs;
284
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300285 if (clkdms)
286 for (c = clkdms; *c; c++)
Paul Walmsleye909d62a82010-01-26 20:13:00 -0700287 _clkdm_register(*c);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300288
Paul Walmsley55ed9692010-01-26 20:12:59 -0700289 autodeps = init_autodeps;
290 if (autodeps)
291 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
292 _autodep_lookup(autodep);
Paul Walmsley369d5612010-01-26 20:13:01 -0700293
294 /*
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600295 * Put all clockdomains into software-supervised mode; PM code
296 * should later enable hardware-supervised mode as appropriate
Paul Walmsley369d5612010-01-26 20:13:01 -0700297 */
298 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600299 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700300 clkdm_wakeup(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600301 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700302 clkdm_deny_idle(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600303
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700304 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600305 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700306
307 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600308 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700309 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300310}
311
312/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300313 * clkdm_lookup - look up a clockdomain by name, return a pointer
314 * @name: name of clockdomain
315 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700316 * Find a registered clockdomain by its name @name. Returns a pointer
317 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300318 */
319struct clockdomain *clkdm_lookup(const char *name)
320{
321 struct clockdomain *clkdm, *temp_clkdm;
322
323 if (!name)
324 return NULL;
325
326 clkdm = NULL;
327
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300328 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
329 if (!strcmp(name, temp_clkdm->name)) {
330 clkdm = temp_clkdm;
331 break;
332 }
333 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300334
335 return clkdm;
336}
337
338/**
339 * clkdm_for_each - call function on each registered clockdomain
340 * @fn: callback function *
341 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700342 * Call the supplied function @fn for each registered clockdomain.
343 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300344 * out early from the iterator. The callback function is called with
345 * the clkdm_mutex held, so no clockdomain structure manipulation
346 * functions should be called from the callback, although hardware
347 * clockdomain control functions are fine. Returns the last return
348 * value of the callback function, which should be 0 for success or
349 * anything else to indicate failure; or -EINVAL if the function pointer
350 * is null.
351 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300352int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
353 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300354{
355 struct clockdomain *clkdm;
356 int ret = 0;
357
358 if (!fn)
359 return -EINVAL;
360
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300361 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300362 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300363 if (ret)
364 break;
365 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300366
367 return ret;
368}
369
370
Paul Walmsleye89087c2008-05-20 18:41:35 -0600371/**
372 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
373 * @clkdm: struct clockdomain *
374 *
375 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700376 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600377 */
378struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
379{
380 if (!clkdm)
381 return NULL;
382
Paul Walmsley5b74c672009-02-03 02:10:03 -0700383 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600384}
385
386
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300387/* Hardware clockdomain control */
388
389/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700390 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
391 * @clkdm1: wake this struct clockdomain * up (dependent)
392 * @clkdm2: when this struct clockdomain * wakes up (source)
393 *
394 * When the clockdomain represented by @clkdm2 wakes up, wake up
395 * @clkdm1. Implemented in hardware on the OMAP, this feature is
396 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
397 * Returns -EINVAL if presented with invalid clockdomain pointers,
398 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
399 * success.
400 */
401int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
402{
403 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700404 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700405
Paul Walmsley55ed9692010-01-26 20:12:59 -0700406 if (!clkdm1 || !clkdm2)
407 return -EINVAL;
408
409 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700410 if (IS_ERR(cd))
411 ret = PTR_ERR(cd);
412
413 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
414 ret = -EINVAL;
415
416 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700417 pr_debug("clockdomain: hardware cannot set/clear wake up of "
418 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700419 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700420 }
421
Paul Walmsley369d5612010-01-26 20:13:01 -0700422 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
423 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
424 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700425
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700426 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700427 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700428
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700429 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700430}
431
432/**
433 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
434 * @clkdm1: wake this struct clockdomain * up (dependent)
435 * @clkdm2: when this struct clockdomain * wakes up (source)
436 *
437 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
438 * wakes up. Returns -EINVAL if presented with invalid clockdomain
439 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
440 * 0 upon success.
441 */
442int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
443{
444 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700445 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700446
Paul Walmsley55ed9692010-01-26 20:12:59 -0700447 if (!clkdm1 || !clkdm2)
448 return -EINVAL;
449
450 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700451 if (IS_ERR(cd))
452 ret = PTR_ERR(cd);
453
454 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
455 ret = -EINVAL;
456
457 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700458 pr_debug("clockdomain: hardware cannot set/clear wake up of "
459 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700460 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700461 }
462
Paul Walmsley369d5612010-01-26 20:13:01 -0700463 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
464 pr_debug("clockdomain: hardware will no longer wake up %s "
465 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700466
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700467 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700468 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700469
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700470 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700471}
472
473/**
474 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
475 * @clkdm1: wake this struct clockdomain * up (dependent)
476 * @clkdm2: when this struct clockdomain * wakes up (source)
477 *
478 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
479 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
480 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
481 * is incapable.
482 *
483 * REVISIT: Currently this function only represents software-controllable
484 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
485 * yet handled here.
486 */
487int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
488{
489 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700490 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700491
492 if (!clkdm1 || !clkdm2)
493 return -EINVAL;
494
495 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700496 if (IS_ERR(cd))
497 ret = PTR_ERR(cd);
498
499 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
500 ret = -EINVAL;
501
502 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700503 pr_debug("clockdomain: hardware cannot set/clear wake up of "
504 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700505 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700506 }
507
Paul Walmsley369d5612010-01-26 20:13:01 -0700508 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700509 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700510}
511
512/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700513 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
514 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
515 *
516 * Remove all inter-clockdomain wakeup dependencies that could cause
517 * @clkdm to wake. Intended to be used during boot to initialize the
518 * PRCM to a known state, after all clockdomains are put into swsup idle
519 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
520 * 0 upon success.
521 */
522int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
523{
Paul Walmsley369d5612010-01-26 20:13:01 -0700524 if (!clkdm)
525 return -EINVAL;
526
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700527 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
528 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700529
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700530 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700531}
532
533/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700534 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
535 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
536 * @clkdm2: when this struct clockdomain * is active (source)
537 *
538 * Prevent @clkdm1 from automatically going inactive (and then to
539 * retention or off) if @clkdm2 is active. Returns -EINVAL if
540 * presented with invalid clockdomain pointers or called on a machine
541 * that does not support software-configurable hardware sleep
542 * dependencies, -ENOENT if the specified dependency cannot be set in
543 * hardware, or 0 upon success.
544 */
545int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
546{
547 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700548 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700549
550 if (!clkdm1 || !clkdm2)
551 return -EINVAL;
552
553 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700554 if (IS_ERR(cd))
555 ret = PTR_ERR(cd);
556
557 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
558 ret = -EINVAL;
559
560 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700561 pr_debug("clockdomain: hardware cannot set/clear sleep "
562 "dependency affecting %s from %s\n", clkdm1->name,
563 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700564 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700565 }
566
Paul Walmsley369d5612010-01-26 20:13:01 -0700567 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
568 pr_debug("clockdomain: will prevent %s from sleeping if %s "
569 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700570
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700571 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700572 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700573
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700574 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700575}
576
577/**
578 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
579 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
580 * @clkdm2: when this struct clockdomain * is active (source)
581 *
582 * Allow @clkdm1 to automatically go inactive (and then to retention or
583 * off), independent of the activity state of @clkdm2. Returns -EINVAL
584 * if presented with invalid clockdomain pointers or called on a machine
585 * that does not support software-configurable hardware sleep dependencies,
586 * -ENOENT if the specified dependency cannot be cleared in hardware, or
587 * 0 upon success.
588 */
589int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
590{
591 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700592 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700593
594 if (!clkdm1 || !clkdm2)
595 return -EINVAL;
596
597 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700598 if (IS_ERR(cd))
599 ret = PTR_ERR(cd);
600
601 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
602 ret = -EINVAL;
603
604 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700605 pr_debug("clockdomain: hardware cannot set/clear sleep "
606 "dependency affecting %s from %s\n", clkdm1->name,
607 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700608 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700609 }
610
Paul Walmsley369d5612010-01-26 20:13:01 -0700611 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
612 pr_debug("clockdomain: will no longer prevent %s from "
613 "sleeping if %s is active\n", clkdm1->name,
614 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700615
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700616 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700617 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700618
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700619 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700620}
621
622/**
623 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
624 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
625 * @clkdm2: when this struct clockdomain * is active (source)
626 *
627 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
628 * not be allowed to automatically go inactive if @clkdm2 is active;
629 * 0 if @clkdm1's automatic power state inactivity transition is independent
630 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
631 * on a machine that does not support software-configurable hardware sleep
632 * dependencies; or -ENOENT if the hardware is incapable.
633 *
634 * REVISIT: Currently this function only represents software-controllable
635 * sleep dependencies. Sleep dependencies fixed in hardware are not
636 * yet handled here.
637 */
638int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
639{
640 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700641 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700642
643 if (!clkdm1 || !clkdm2)
644 return -EINVAL;
645
646 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700647 if (IS_ERR(cd))
648 ret = PTR_ERR(cd);
649
650 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
651 ret = -EINVAL;
652
653 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700654 pr_debug("clockdomain: hardware cannot set/clear sleep "
655 "dependency affecting %s from %s\n", clkdm1->name,
656 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700657 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700658 }
659
Paul Walmsley369d5612010-01-26 20:13:01 -0700660 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700661 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700662}
663
Paul Walmsley369d5612010-01-26 20:13:01 -0700664/**
665 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
666 * @clkdm: struct clockdomain * to remove all sleep dependencies from
667 *
668 * Remove all inter-clockdomain sleep dependencies that could prevent
669 * @clkdm from idling. Intended to be used during boot to initialize the
670 * PRCM to a known state, after all clockdomains are put into swsup idle
671 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
672 * 0 upon success.
673 */
674int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
675{
Paul Walmsley369d5612010-01-26 20:13:01 -0700676 if (!clkdm)
677 return -EINVAL;
678
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700679 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
680 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700681
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700682 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700683}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700684
685/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700686 * clkdm_sleep - force clockdomain sleep transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300687 * @clkdm: struct clockdomain *
688 *
689 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700690 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300691 * clockdomain does not support software-initiated sleep; 0 upon
692 * success.
693 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700694int clkdm_sleep(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300695{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600696 int ret;
697 unsigned long flags;
698
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300699 if (!clkdm)
700 return -EINVAL;
701
702 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
703 pr_debug("clockdomain: %s does not support forcing "
704 "sleep via software\n", clkdm->name);
705 return -EINVAL;
706 }
707
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700708 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
709 return -EINVAL;
710
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300711 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
712
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600713 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600714 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600715 ret = arch_clkdm->clkdm_sleep(clkdm);
716 spin_unlock_irqrestore(&clkdm->lock, flags);
717 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300718}
719
720/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700721 * clkdm_wakeup - force clockdomain wakeup transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300722 * @clkdm: struct clockdomain *
723 *
724 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700725 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300726 * clockdomain does not support software-controlled wakeup; 0 upon
727 * success.
728 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700729int clkdm_wakeup(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300730{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600731 int ret;
732 unsigned long flags;
733
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300734 if (!clkdm)
735 return -EINVAL;
736
737 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
738 pr_debug("clockdomain: %s does not support forcing "
739 "wakeup via software\n", clkdm->name);
740 return -EINVAL;
741 }
742
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700743 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
744 return -EINVAL;
745
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300746 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
747
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600748 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600749 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600750 ret = arch_clkdm->clkdm_wakeup(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600751 ret |= pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600752 spin_unlock_irqrestore(&clkdm->lock, flags);
753 return ret;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300754}
755
756/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700757 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300758 * @clkdm: struct clockdomain *
759 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700760 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300761 * active or idle states, as needed by downstream clocks. If the
762 * clockdomain has any downstream clocks enabled in the clock
763 * framework, wkdep/sleepdep autodependencies are added; this is so
764 * device drivers can read and write to the device. No return value.
765 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700766void clkdm_allow_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300767{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600768 unsigned long flags;
769
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300770 if (!clkdm)
771 return;
772
773 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
774 pr_debug("clock: automatic idle transitions cannot be enabled "
775 "on clockdomain %s\n", clkdm->name);
776 return;
777 }
778
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700779 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
780 return;
781
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300782 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
783 clkdm->name);
784
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600785 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600786 clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700787 arch_clkdm->clkdm_allow_idle(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300788 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600789 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300790}
791
792/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700793 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300794 * @clkdm: struct clockdomain *
795 *
796 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700797 * @clkdm into inactive or idle states. If the clockdomain has
798 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300799 * autodependencies are removed. No return value.
800 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700801void clkdm_deny_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300802{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600803 unsigned long flags;
804
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300805 if (!clkdm)
806 return;
807
808 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
809 pr_debug("clockdomain: automatic idle transitions cannot be "
810 "disabled on %s\n", clkdm->name);
811 return;
812 }
813
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700814 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
815 return;
816
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300817 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
818 clkdm->name);
819
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600820 spin_lock_irqsave(&clkdm->lock, flags);
Paul Walmsley32a363c2011-07-10 05:56:54 -0600821 clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700822 arch_clkdm->clkdm_deny_idle(clkdm);
Santosh Shilimkarb1cbdb02011-08-19 16:59:39 -0600823 pwrdm_state_switch(clkdm->pwrdm.ptr);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600824 spin_unlock_irqrestore(&clkdm->lock, flags);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300825}
826
Paul Walmsley32a363c2011-07-10 05:56:54 -0600827/**
828 * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
829 * @clkdm: struct clockdomain *
830 *
831 * Returns true if clockdomain @clkdm currently has
832 * hardware-supervised idle enabled, or false if it does not or if
833 * @clkdm is NULL. It is only valid to call this function after
834 * clkdm_init() has been called. This function does not actually read
835 * bits from the hardware; it instead tests an in-memory flag that is
836 * changed whenever the clockdomain code changes the auto-idle mode.
837 */
838bool clkdm_in_hwsup(struct clockdomain *clkdm)
839{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600840 bool ret;
841 unsigned long flags;
842
Paul Walmsley32a363c2011-07-10 05:56:54 -0600843 if (!clkdm)
844 return false;
845
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600846 spin_lock_irqsave(&clkdm->lock, flags);
847 ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
848 spin_unlock_irqrestore(&clkdm->lock, flags);
849
850 return ret;
Paul Walmsley32a363c2011-07-10 05:56:54 -0600851}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300852
Benoit Cousson113a7412011-07-10 05:56:54 -0600853/* Clockdomain-to-clock/hwmod framework interface code */
854
855static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
856{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600857 unsigned long flags;
858
Benoit Cousson113a7412011-07-10 05:56:54 -0600859 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
860 return -EINVAL;
861
862 /*
863 * For arch's with no autodeps, clkcm_clk_enable
864 * should be called for every clock instance or hwmod that is
865 * enabled, so the clkdm can be force woken up.
866 */
867 if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
868 return 0;
869
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600870 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600871 arch_clkdm->clkdm_clk_enable(clkdm);
872 pwrdm_wait_transition(clkdm->pwrdm.ptr);
873 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600874 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600875
876 pr_debug("clockdomain: clkdm %s: enabled\n", clkdm->name);
877
878 return 0;
879}
880
881static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
882{
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600883 unsigned long flags;
884
Benoit Cousson113a7412011-07-10 05:56:54 -0600885 if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
886 return -EINVAL;
887
888 if (atomic_read(&clkdm->usecount) == 0) {
889 WARN_ON(1); /* underflow */
890 return -ERANGE;
891 }
892
893 if (atomic_dec_return(&clkdm->usecount) > 0)
894 return 0;
895
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600896 spin_lock_irqsave(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600897 arch_clkdm->clkdm_clk_disable(clkdm);
898 pwrdm_clkdm_state_switch(clkdm);
Rajendra Nayak555e74e2011-07-10 05:56:55 -0600899 spin_unlock_irqrestore(&clkdm->lock, flags);
Benoit Cousson113a7412011-07-10 05:56:54 -0600900
901 pr_debug("clockdomain: clkdm %s: disabled\n", clkdm->name);
902
903 return 0;
904}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300905
906/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700907 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300908 * @clkdm: struct clockdomain *
909 * @clk: struct clk * of the enabled downstream clock
910 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700911 * Increment the usecount of the clockdomain @clkdm and ensure that it
912 * is awake before @clk is enabled. Intended to be called by
913 * clk_enable() code. If the clockdomain is in software-supervised
914 * idle mode, force the clockdomain to wake. If the clockdomain is in
915 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
916 * ensure that devices in the clockdomain can be read from/written to
917 * by on-chip processors. Returns -EINVAL if passed null pointers;
918 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300919 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700920int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300921{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300922 /*
923 * XXX Rewrite this code to maintain a list of enabled
924 * downstream clocks for debugging purposes?
925 */
926
Benoit Cousson113a7412011-07-10 05:56:54 -0600927 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300928 return -EINVAL;
929
Benoit Cousson113a7412011-07-10 05:56:54 -0600930 return _clkdm_clk_hwmod_enable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300931}
932
933/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700934 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300935 * @clkdm: struct clockdomain *
936 * @clk: struct clk * of the disabled downstream clock
937 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700938 * Decrement the usecount of this clockdomain @clkdm when @clk is
939 * disabled. Intended to be called by clk_disable() code. If the
940 * clockdomain usecount goes to 0, put the clockdomain to sleep
941 * (software-supervised mode) or remove the clkdm autodependencies
942 * (hardware-supervised mode). Returns -EINVAL if passed null
Benoit Cousson113a7412011-07-10 05:56:54 -0600943 * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
944 * upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300945 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700946int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300947{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300948 /*
949 * XXX Rewrite this code to maintain a list of enabled
950 * downstream clocks for debugging purposes?
951 */
952
Benoit Cousson113a7412011-07-10 05:56:54 -0600953 if (!clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300954 return -EINVAL;
955
Benoit Cousson113a7412011-07-10 05:56:54 -0600956 return _clkdm_clk_hwmod_disable(clkdm);
957}
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700958
Benoit Cousson113a7412011-07-10 05:56:54 -0600959/**
960 * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
961 * @clkdm: struct clockdomain *
962 * @oh: struct omap_hwmod * of the enabled downstream hwmod
963 *
964 * Increment the usecount of the clockdomain @clkdm and ensure that it
965 * is awake before @oh is enabled. Intended to be called by
966 * module_enable() code.
967 * If the clockdomain is in software-supervised idle mode, force the
968 * clockdomain to wake. If the clockdomain is in hardware-supervised idle
969 * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
970 * clockdomain can be read from/written to by on-chip processors.
971 * Returns -EINVAL if passed null pointers;
972 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
973 */
974int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
975{
976 /* The clkdm attribute does not exist yet prior OMAP4 */
977 if (cpu_is_omap24xx() || cpu_is_omap34xx())
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300978 return 0;
979
Benoit Cousson113a7412011-07-10 05:56:54 -0600980 /*
981 * XXX Rewrite this code to maintain a list of enabled
982 * downstream hwmods for debugging purposes?
983 */
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300984
Benoit Cousson113a7412011-07-10 05:56:54 -0600985 if (!oh)
986 return -EINVAL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300987
Benoit Cousson113a7412011-07-10 05:56:54 -0600988 return _clkdm_clk_hwmod_enable(clkdm);
989}
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300990
Benoit Cousson113a7412011-07-10 05:56:54 -0600991/**
992 * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
993 * @clkdm: struct clockdomain *
994 * @oh: struct omap_hwmod * of the disabled downstream hwmod
995 *
996 * Decrement the usecount of this clockdomain @clkdm when @oh is
997 * disabled. Intended to be called by module_disable() code.
998 * If the clockdomain usecount goes to 0, put the clockdomain to sleep
999 * (software-supervised mode) or remove the clkdm autodependencies
1000 * (hardware-supervised mode).
1001 * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1002 * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1003 * idle mode.
1004 */
1005int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1006{
1007 /* The clkdm attribute does not exist yet prior OMAP4 */
1008 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1009 return 0;
1010
1011 /*
1012 * XXX Rewrite this code to maintain a list of enabled
1013 * downstream hwmods for debugging purposes?
1014 */
1015
1016 if (!oh)
1017 return -EINVAL;
1018
1019 return _clkdm_clk_hwmod_disable(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001020}
1021