blob: 555a518836b9781282f1e4c85342ed1f090c087c [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 *
Abhijit Pagare91808a82010-02-22 22:09:07 -07004 * Copyright (C) 2008-2010 Texas Instruments, Inc.
5 * Copyright (C) 2008-2010 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 Walmsley59fb6592010-12-21 15:30:55 -070029#include "prm2xxx_3xxx.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030030#include "prm-regbits-24xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -070031#include "cm2xxx_3xxx.h"
Paul Walmsleybd2122c2010-12-21 21:05:15 -070032#include "cm-regbits-34xx.h"
33#include "cminst44xx.h"
34#include "prcm44xx.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030035
Paul Walmsley55ed9692010-01-26 20:12:59 -070036#include <plat/clock.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070037#include <plat/powerdomain.h>
38#include <plat/clockdomain.h>
Paul Walmsley55ed9692010-01-26 20:12:59 -070039#include <plat/prcm.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030040
41/* clkdm_list contains all registered struct clockdomains */
42static LIST_HEAD(clkdm_list);
43
Paul Walmsley55ed9692010-01-26 20:12:59 -070044/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
45static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030046
47
48/* Private functions */
49
Paul Walmsley55ed9692010-01-26 20:12:59 -070050static struct clockdomain *_clkdm_lookup(const char *name)
51{
52 struct clockdomain *clkdm, *temp_clkdm;
53
54 if (!name)
55 return NULL;
56
57 clkdm = NULL;
58
59 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
60 if (!strcmp(name, temp_clkdm->name)) {
61 clkdm = temp_clkdm;
62 break;
63 }
64 }
65
66 return clkdm;
67}
68
Paul Walmsleye909d622010-01-26 20:13:00 -070069/**
70 * _clkdm_register - register a clockdomain
71 * @clkdm: struct clockdomain * to register
72 *
73 * Adds a clockdomain to the internal clockdomain list.
74 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
75 * already registered by the provided name, or 0 upon success.
76 */
77static int _clkdm_register(struct clockdomain *clkdm)
78{
79 struct powerdomain *pwrdm;
80
81 if (!clkdm || !clkdm->name)
82 return -EINVAL;
83
84 if (!omap_chip_is(clkdm->omap_chip))
85 return -EINVAL;
86
87 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
88 if (!pwrdm) {
89 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
90 clkdm->name, clkdm->pwrdm.name);
91 return -EINVAL;
92 }
93 clkdm->pwrdm.ptr = pwrdm;
94
95 /* Verify that the clockdomain is not already registered */
96 if (_clkdm_lookup(clkdm->name))
97 return -EEXIST;
98
99 list_add(&clkdm->node, &clkdm_list);
100
101 pwrdm_add_clkdm(pwrdm, clkdm);
102
103 pr_debug("clockdomain: registered %s\n", clkdm->name);
104
105 return 0;
106}
107
Paul Walmsley55ed9692010-01-26 20:12:59 -0700108/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
109static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
110 struct clkdm_dep *deps)
111{
112 struct clkdm_dep *cd;
113
114 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
115 return ERR_PTR(-EINVAL);
116
117 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700118 if (!omap_chip_is(cd->omap_chip))
119 continue;
120
121 if (!cd->clkdm && cd->clkdm_name)
122 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
123
124 if (cd->clkdm == clkdm)
125 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700126 }
127
128 if (!cd->clkdm_name)
129 return ERR_PTR(-ENOENT);
130
131 return cd;
132}
133
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300134/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700135 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
136 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300137 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700138 * Resolve autodep clockdomain names to clockdomain pointers via
139 * clkdm_lookup() and store the pointers in the autodep structure. An
140 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300141 * automatically added and removed whenever clocks in the associated
142 * clockdomain are enabled or disabled (respectively) when the
143 * clockdomain is in hardware-supervised mode. Meant to be called
144 * once at clockdomain layer initialization, since these should remain
145 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700146 *
147 * XXX autodeps are deprecated and should be removed at the earliest
148 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300149 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700150static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300151{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700152 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300153
154 if (!autodep)
155 return;
156
157 if (!omap_chip_is(autodep->omap_chip))
158 return;
159
Paul Walmsley55ed9692010-01-26 20:12:59 -0700160 clkdm = clkdm_lookup(autodep->clkdm.name);
161 if (!clkdm) {
162 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
163 autodep->clkdm.name);
164 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300165 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700166 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300167}
168
169/*
170 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
171 * @clkdm: struct clockdomain *
172 *
173 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
174 * in hardware-supervised mode. Meant to be called from clock framework
175 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700176 *
177 * XXX autodeps are deprecated and should be removed at the earliest
178 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300179 */
180static void _clkdm_add_autodeps(struct clockdomain *clkdm)
181{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700182 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300183
Paul Walmsleyad956162010-02-22 22:09:35 -0700184 if (!autodeps)
185 return;
186
Paul Walmsley55ed9692010-01-26 20:12:59 -0700187 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
188 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300189 continue;
190
Paul Walmsleyd96df002009-01-27 19:44:35 -0700191 if (!omap_chip_is(autodep->omap_chip))
192 continue;
193
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300194 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700195 "clkdm %s\n", autodep->clkdm.ptr->name,
196 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300197
Paul Walmsley55ed9692010-01-26 20:12:59 -0700198 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
199 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300200 }
201}
202
203/*
204 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
205 * @clkdm: struct clockdomain *
206 *
207 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
208 * in hardware-supervised mode. Meant to be called from clock framework
209 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700210 *
211 * XXX autodeps are deprecated and should be removed at the earliest
212 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300213 */
214static void _clkdm_del_autodeps(struct clockdomain *clkdm)
215{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700216 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300217
Paul Walmsleyad956162010-02-22 22:09:35 -0700218 if (!autodeps)
219 return;
220
Paul Walmsley55ed9692010-01-26 20:12:59 -0700221 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
222 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300223 continue;
224
Paul Walmsleyd96df002009-01-27 19:44:35 -0700225 if (!omap_chip_is(autodep->omap_chip))
226 continue;
227
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300228 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700229 "clkdm %s\n", autodep->clkdm.ptr->name,
230 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300231
Paul Walmsley55ed9692010-01-26 20:12:59 -0700232 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
233 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300234 }
235}
236
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700237/**
238 * _enable_hwsup - place a clockdomain into hardware-supervised idle
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600239 * @clkdm: struct clockdomain *
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600240 *
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700241 * Place the clockdomain into hardware-supervised idle mode. No return
242 * value.
243 *
244 * XXX Should this return an error if the clockdomain does not support
245 * hardware-supervised idle mode?
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600246 */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700247static void _enable_hwsup(struct clockdomain *clkdm)
248{
249 u32 bits, v;
250
251 if (cpu_is_omap24xx())
252 bits = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700253 else if (cpu_is_omap34xx())
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700254 bits = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700255 else if (cpu_is_omap44xx())
256 return omap4_cminst_clkdm_enable_hwsup(clkdm->prcm_partition,
257 clkdm->cm_inst,
258 clkdm->clkdm_offs);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700259 else
260 BUG();
261
262 bits = bits << __ffs(clkdm->clktrctrl_mask);
263
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700264 /*
265 * XXX clkstctrl_reg is known on OMAP2 - this clkdm
266 * field is not needed
267 */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700268 v = __raw_readl(clkdm->clkstctrl_reg);
269 v &= ~(clkdm->clktrctrl_mask);
270 v |= bits;
271 __raw_writel(v, clkdm->clkstctrl_reg);
272
273}
274
275/**
276 * _disable_hwsup - place a clockdomain into software-supervised idle
277 * @clkdm: struct clockdomain *
278 *
279 * Place the clockdomain @clkdm into software-supervised idle mode.
280 * No return value.
281 *
282 * XXX Should this return an error if the clockdomain does not support
283 * software-supervised idle mode?
284 */
285static void _disable_hwsup(struct clockdomain *clkdm)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600286{
Abhijit Pagareb0994742010-01-26 20:12:53 -0700287 u32 bits, v;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600288
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700289 if (cpu_is_omap24xx())
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700290 bits = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700291 else if (cpu_is_omap34xx())
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700292 bits = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700293 else if (cpu_is_omap44xx())
294 return omap4_cminst_clkdm_disable_hwsup(clkdm->prcm_partition,
295 clkdm->cm_inst,
296 clkdm->clkdm_offs);
297 else
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600298 BUG();
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600299
Abhijit Pagareb0994742010-01-26 20:12:53 -0700300 bits = bits << __ffs(clkdm->clktrctrl_mask);
301
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700302 /*
303 * XXX clkstctrl_reg is known on OMAP2 - this clkdm
304 * field is not needed
305 */
Abhijit Pagareb0994742010-01-26 20:12:53 -0700306 v = __raw_readl(clkdm->clkstctrl_reg);
307 v &= ~(clkdm->clktrctrl_mask);
308 v |= bits;
309 __raw_writel(v, clkdm->clkstctrl_reg);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600310}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300311
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300312/* Public functions */
313
314/**
315 * clkdm_init - set up the clockdomain layer
316 * @clkdms: optional pointer to an array of clockdomains to register
317 * @init_autodeps: optional pointer to an array of autodeps to register
318 *
319 * Set up internal state. If a pointer to an array of clockdomains
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700320 * @clkdms was supplied, loop through the list of clockdomains,
321 * register all that are available on the current platform. Similarly,
322 * if a pointer to an array of clockdomain autodependencies
323 * @init_autodeps was provided, register those. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300324 */
325void clkdm_init(struct clockdomain **clkdms,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700326 struct clkdm_autodep *init_autodeps)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300327{
328 struct clockdomain **c = NULL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700329 struct clockdomain *clkdm;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700330 struct clkdm_autodep *autodep = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300331
332 if (clkdms)
333 for (c = clkdms; *c; c++)
Paul Walmsleye909d622010-01-26 20:13:00 -0700334 _clkdm_register(*c);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300335
Paul Walmsley55ed9692010-01-26 20:12:59 -0700336 autodeps = init_autodeps;
337 if (autodeps)
338 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
339 _autodep_lookup(autodep);
Paul Walmsley369d5612010-01-26 20:13:01 -0700340
341 /*
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600342 * Put all clockdomains into software-supervised mode; PM code
343 * should later enable hardware-supervised mode as appropriate
Paul Walmsley369d5612010-01-26 20:13:01 -0700344 */
345 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600346 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
347 omap2_clkdm_wakeup(clkdm);
348 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
349 omap2_clkdm_deny_idle(clkdm);
350
351 clkdm_clear_all_wkdeps(clkdm);
352 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700353 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300354}
355
356/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300357 * clkdm_lookup - look up a clockdomain by name, return a pointer
358 * @name: name of clockdomain
359 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700360 * Find a registered clockdomain by its name @name. Returns a pointer
361 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300362 */
363struct clockdomain *clkdm_lookup(const char *name)
364{
365 struct clockdomain *clkdm, *temp_clkdm;
366
367 if (!name)
368 return NULL;
369
370 clkdm = NULL;
371
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300372 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
373 if (!strcmp(name, temp_clkdm->name)) {
374 clkdm = temp_clkdm;
375 break;
376 }
377 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300378
379 return clkdm;
380}
381
382/**
383 * clkdm_for_each - call function on each registered clockdomain
384 * @fn: callback function *
385 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700386 * Call the supplied function @fn for each registered clockdomain.
387 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300388 * out early from the iterator. The callback function is called with
389 * the clkdm_mutex held, so no clockdomain structure manipulation
390 * functions should be called from the callback, although hardware
391 * clockdomain control functions are fine. Returns the last return
392 * value of the callback function, which should be 0 for success or
393 * anything else to indicate failure; or -EINVAL if the function pointer
394 * is null.
395 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300396int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
397 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300398{
399 struct clockdomain *clkdm;
400 int ret = 0;
401
402 if (!fn)
403 return -EINVAL;
404
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300405 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300406 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300407 if (ret)
408 break;
409 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300410
411 return ret;
412}
413
414
Paul Walmsleye89087c2008-05-20 18:41:35 -0600415/**
416 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
417 * @clkdm: struct clockdomain *
418 *
419 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700420 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600421 */
422struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
423{
424 if (!clkdm)
425 return NULL;
426
Paul Walmsley5b74c672009-02-03 02:10:03 -0700427 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600428}
429
430
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300431/* Hardware clockdomain control */
432
433/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700434 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
435 * @clkdm1: wake this struct clockdomain * up (dependent)
436 * @clkdm2: when this struct clockdomain * wakes up (source)
437 *
438 * When the clockdomain represented by @clkdm2 wakes up, wake up
439 * @clkdm1. Implemented in hardware on the OMAP, this feature is
440 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
441 * Returns -EINVAL if presented with invalid clockdomain pointers,
442 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
443 * success.
444 */
445int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
446{
447 struct clkdm_dep *cd;
448
449 if (!clkdm1 || !clkdm2)
450 return -EINVAL;
451
452 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
453 if (IS_ERR(cd)) {
454 pr_debug("clockdomain: hardware cannot set/clear wake up of "
455 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
456 return PTR_ERR(cd);
457 }
458
Paul Walmsley369d5612010-01-26 20:13:01 -0700459 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
460 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
461 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700462
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700463 omap2_prm_set_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700464 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
465 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700466
467 return 0;
468}
469
470/**
471 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
472 * @clkdm1: wake this struct clockdomain * up (dependent)
473 * @clkdm2: when this struct clockdomain * wakes up (source)
474 *
475 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
476 * wakes up. Returns -EINVAL if presented with invalid clockdomain
477 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
478 * 0 upon success.
479 */
480int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
481{
482 struct clkdm_dep *cd;
483
484 if (!clkdm1 || !clkdm2)
485 return -EINVAL;
486
487 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
488 if (IS_ERR(cd)) {
489 pr_debug("clockdomain: hardware cannot set/clear wake up of "
490 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
491 return PTR_ERR(cd);
492 }
493
Paul Walmsley369d5612010-01-26 20:13:01 -0700494 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
495 pr_debug("clockdomain: hardware will no longer wake up %s "
496 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700497
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700498 omap2_prm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700499 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
500 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700501
502 return 0;
503}
504
505/**
506 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
507 * @clkdm1: wake this struct clockdomain * up (dependent)
508 * @clkdm2: when this struct clockdomain * wakes up (source)
509 *
510 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
511 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
512 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
513 * is incapable.
514 *
515 * REVISIT: Currently this function only represents software-controllable
516 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
517 * yet handled here.
518 */
519int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
520{
521 struct clkdm_dep *cd;
522
523 if (!clkdm1 || !clkdm2)
524 return -EINVAL;
525
526 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
527 if (IS_ERR(cd)) {
528 pr_debug("clockdomain: hardware cannot set/clear wake up of "
529 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
530 return PTR_ERR(cd);
531 }
532
Paul Walmsley369d5612010-01-26 20:13:01 -0700533 /* XXX It's faster to return the atomic wkdep_usecount */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700534 return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700535 (1 << clkdm2->dep_bit));
536}
537
538/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700539 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
540 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
541 *
542 * Remove all inter-clockdomain wakeup dependencies that could cause
543 * @clkdm to wake. Intended to be used during boot to initialize the
544 * PRCM to a known state, after all clockdomains are put into swsup idle
545 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
546 * 0 upon success.
547 */
548int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
549{
550 struct clkdm_dep *cd;
551 u32 mask = 0;
552
553 if (!clkdm)
554 return -EINVAL;
555
556 for (cd = clkdm->wkdep_srcs; cd && cd->clkdm_name; cd++) {
557 if (!omap_chip_is(cd->omap_chip))
558 continue;
559
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600560 if (!cd->clkdm && cd->clkdm_name)
561 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
562
Paul Walmsley369d5612010-01-26 20:13:01 -0700563 /* PRM accesses are slow, so minimize them */
564 mask |= 1 << cd->clkdm->dep_bit;
565 atomic_set(&cd->wkdep_usecount, 0);
566 }
567
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700568 omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs, PM_WKDEP);
Paul Walmsley369d5612010-01-26 20:13:01 -0700569
570 return 0;
571}
572
573/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700574 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
575 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
576 * @clkdm2: when this struct clockdomain * is active (source)
577 *
578 * Prevent @clkdm1 from automatically going inactive (and then to
579 * retention or off) if @clkdm2 is active. Returns -EINVAL if
580 * presented with invalid clockdomain pointers or called on a machine
581 * that does not support software-configurable hardware sleep
582 * dependencies, -ENOENT if the specified dependency cannot be set in
583 * hardware, or 0 upon success.
584 */
585int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
586{
587 struct clkdm_dep *cd;
588
589 if (!cpu_is_omap34xx())
590 return -EINVAL;
591
592 if (!clkdm1 || !clkdm2)
593 return -EINVAL;
594
595 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
596 if (IS_ERR(cd)) {
597 pr_debug("clockdomain: hardware cannot set/clear sleep "
598 "dependency affecting %s from %s\n", clkdm1->name,
599 clkdm2->name);
600 return PTR_ERR(cd);
601 }
602
Paul Walmsley369d5612010-01-26 20:13:01 -0700603 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
604 pr_debug("clockdomain: will prevent %s from sleeping if %s "
605 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700606
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700607 omap2_cm_set_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700608 clkdm1->pwrdm.ptr->prcm_offs,
609 OMAP3430_CM_SLEEPDEP);
610 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700611
612 return 0;
613}
614
615/**
616 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
617 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
618 * @clkdm2: when this struct clockdomain * is active (source)
619 *
620 * Allow @clkdm1 to automatically go inactive (and then to retention or
621 * off), independent of the activity state of @clkdm2. Returns -EINVAL
622 * if presented with invalid clockdomain pointers or called on a machine
623 * that does not support software-configurable hardware sleep dependencies,
624 * -ENOENT if the specified dependency cannot be cleared in hardware, or
625 * 0 upon success.
626 */
627int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
628{
629 struct clkdm_dep *cd;
630
631 if (!cpu_is_omap34xx())
632 return -EINVAL;
633
634 if (!clkdm1 || !clkdm2)
635 return -EINVAL;
636
637 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
638 if (IS_ERR(cd)) {
639 pr_debug("clockdomain: hardware cannot set/clear sleep "
640 "dependency affecting %s from %s\n", clkdm1->name,
641 clkdm2->name);
642 return PTR_ERR(cd);
643 }
644
Paul Walmsley369d5612010-01-26 20:13:01 -0700645 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
646 pr_debug("clockdomain: will no longer prevent %s from "
647 "sleeping if %s is active\n", clkdm1->name,
648 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700649
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700650 omap2_cm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700651 clkdm1->pwrdm.ptr->prcm_offs,
652 OMAP3430_CM_SLEEPDEP);
653 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700654
655 return 0;
656}
657
658/**
659 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
660 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
661 * @clkdm2: when this struct clockdomain * is active (source)
662 *
663 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
664 * not be allowed to automatically go inactive if @clkdm2 is active;
665 * 0 if @clkdm1's automatic power state inactivity transition is independent
666 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
667 * on a machine that does not support software-configurable hardware sleep
668 * dependencies; or -ENOENT if the hardware is incapable.
669 *
670 * REVISIT: Currently this function only represents software-controllable
671 * sleep dependencies. Sleep dependencies fixed in hardware are not
672 * yet handled here.
673 */
674int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
675{
676 struct clkdm_dep *cd;
677
678 if (!cpu_is_omap34xx())
679 return -EINVAL;
680
681 if (!clkdm1 || !clkdm2)
682 return -EINVAL;
683
684 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
685 if (IS_ERR(cd)) {
686 pr_debug("clockdomain: hardware cannot set/clear sleep "
687 "dependency affecting %s from %s\n", clkdm1->name,
688 clkdm2->name);
689 return PTR_ERR(cd);
690 }
691
Paul Walmsley369d5612010-01-26 20:13:01 -0700692 /* XXX It's faster to return the atomic sleepdep_usecount */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700693 return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700694 OMAP3430_CM_SLEEPDEP,
695 (1 << clkdm2->dep_bit));
696}
697
Paul Walmsley369d5612010-01-26 20:13:01 -0700698/**
699 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
700 * @clkdm: struct clockdomain * to remove all sleep dependencies from
701 *
702 * Remove all inter-clockdomain sleep dependencies that could prevent
703 * @clkdm from idling. Intended to be used during boot to initialize the
704 * PRCM to a known state, after all clockdomains are put into swsup idle
705 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
706 * 0 upon success.
707 */
708int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
709{
710 struct clkdm_dep *cd;
711 u32 mask = 0;
712
713 if (!cpu_is_omap34xx())
714 return -EINVAL;
715
716 if (!clkdm)
717 return -EINVAL;
718
719 for (cd = clkdm->sleepdep_srcs; cd && cd->clkdm_name; cd++) {
720 if (!omap_chip_is(cd->omap_chip))
721 continue;
722
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600723 if (!cd->clkdm && cd->clkdm_name)
724 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
725
Paul Walmsley369d5612010-01-26 20:13:01 -0700726 /* PRM accesses are slow, so minimize them */
727 mask |= 1 << cd->clkdm->dep_bit;
728 atomic_set(&cd->sleepdep_usecount, 0);
729 }
730
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700731 omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs,
Paul Walmsley369d5612010-01-26 20:13:01 -0700732 OMAP3430_CM_SLEEPDEP);
733
734 return 0;
735}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700736
737/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300738 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700739 * @clkdm: struct clkdm * of a clockdomain
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300740 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700741 * Return the clockdomain @clkdm current state transition mode from the
742 * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if @clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300743 * is NULL or the current mode upon success.
744 */
745static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
746{
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700747 u32 v = 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300748
749 if (!clkdm)
750 return -EINVAL;
751
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700752 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
753 v = __raw_readl(clkdm->clkstctrl_reg);
754 v &= clkdm->clktrctrl_mask;
755 v >>= __ffs(clkdm->clktrctrl_mask);
756 } else if (cpu_is_omap44xx()) {
757 pr_warn("OMAP4 clockdomain: missing wakeup/sleep deps\n");
758 } else {
759 BUG();
760 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300761
762 return v;
763}
764
765/**
766 * omap2_clkdm_sleep - force clockdomain sleep transition
767 * @clkdm: struct clockdomain *
768 *
769 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700770 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300771 * clockdomain does not support software-initiated sleep; 0 upon
772 * success.
773 */
774int omap2_clkdm_sleep(struct clockdomain *clkdm)
775{
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700776 u32 bits, v;
777
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300778 if (!clkdm)
779 return -EINVAL;
780
781 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
782 pr_debug("clockdomain: %s does not support forcing "
783 "sleep via software\n", clkdm->name);
784 return -EINVAL;
785 }
786
787 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
788
789 if (cpu_is_omap24xx()) {
790
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700791 omap2_cm_set_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700792 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300793
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700794 } else if (cpu_is_omap34xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300795
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700796 bits = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
797 __ffs(clkdm->clktrctrl_mask));
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300798
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700799 v = __raw_readl(clkdm->clkstctrl_reg);
Abhijit Pagareb0994742010-01-26 20:12:53 -0700800 v &= ~(clkdm->clktrctrl_mask);
801 v |= bits;
802 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300803
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700804 } else if (cpu_is_omap44xx()) {
805
806 omap4_cminst_clkdm_force_sleep(clkdm->prcm_partition,
807 clkdm->cm_inst,
808 clkdm->clkdm_offs);
809
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300810 } else {
811 BUG();
812 };
813
814 return 0;
815}
816
817/**
818 * omap2_clkdm_wakeup - force clockdomain wakeup transition
819 * @clkdm: struct clockdomain *
820 *
821 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700822 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300823 * clockdomain does not support software-controlled wakeup; 0 upon
824 * success.
825 */
826int omap2_clkdm_wakeup(struct clockdomain *clkdm)
827{
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700828 u32 bits, v;
829
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300830 if (!clkdm)
831 return -EINVAL;
832
833 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
834 pr_debug("clockdomain: %s does not support forcing "
835 "wakeup via software\n", clkdm->name);
836 return -EINVAL;
837 }
838
839 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
840
841 if (cpu_is_omap24xx()) {
842
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700843 omap2_cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700844 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300845
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700846 } else if (cpu_is_omap34xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300847
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700848 bits = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
849 __ffs(clkdm->clktrctrl_mask));
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300850
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700851 v = __raw_readl(clkdm->clkstctrl_reg);
Abhijit Pagareb0994742010-01-26 20:12:53 -0700852 v &= ~(clkdm->clktrctrl_mask);
853 v |= bits;
854 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300855
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700856 } else if (cpu_is_omap44xx()) {
857
858 omap4_cminst_clkdm_force_wakeup(clkdm->prcm_partition,
859 clkdm->cm_inst,
860 clkdm->clkdm_offs);
861
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300862 } else {
863 BUG();
864 };
865
866 return 0;
867}
868
869/**
870 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
871 * @clkdm: struct clockdomain *
872 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700873 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300874 * active or idle states, as needed by downstream clocks. If the
875 * clockdomain has any downstream clocks enabled in the clock
876 * framework, wkdep/sleepdep autodependencies are added; this is so
877 * device drivers can read and write to the device. No return value.
878 */
879void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
880{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300881 if (!clkdm)
882 return;
883
884 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
885 pr_debug("clock: automatic idle transitions cannot be enabled "
886 "on clockdomain %s\n", clkdm->name);
887 return;
888 }
889
890 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
891 clkdm->name);
892
Abhijit Pagare91808a82010-02-22 22:09:07 -0700893 /*
894 * XXX This should be removed once TI adds wakeup/sleep
895 * dependency code and data for OMAP4.
896 */
897 if (cpu_is_omap44xx()) {
898 WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
899 "support is not yet implemented\n");
900 } else {
901 if (atomic_read(&clkdm->usecount) > 0)
902 _clkdm_add_autodeps(clkdm);
903 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300904
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700905 _enable_hwsup(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300906
907 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300908}
909
910/**
911 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
912 * @clkdm: struct clockdomain *
913 *
914 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700915 * @clkdm into inactive or idle states. If the clockdomain has
916 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300917 * autodependencies are removed. No return value.
918 */
919void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
920{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300921 if (!clkdm)
922 return;
923
924 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
925 pr_debug("clockdomain: automatic idle transitions cannot be "
926 "disabled on %s\n", clkdm->name);
927 return;
928 }
929
930 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
931 clkdm->name);
932
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700933 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300934
Abhijit Pagare91808a82010-02-22 22:09:07 -0700935 /*
936 * XXX This should be removed once TI adds wakeup/sleep
937 * dependency code and data for OMAP4.
938 */
939 if (cpu_is_omap44xx()) {
940 WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
941 "support is not yet implemented\n");
942 } else {
943 if (atomic_read(&clkdm->usecount) > 0)
944 _clkdm_del_autodeps(clkdm);
945 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300946}
947
948
949/* Clockdomain-to-clock framework interface code */
950
951/**
952 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
953 * @clkdm: struct clockdomain *
954 * @clk: struct clk * of the enabled downstream clock
955 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700956 * Increment the usecount of the clockdomain @clkdm and ensure that it
957 * is awake before @clk is enabled. Intended to be called by
958 * clk_enable() code. If the clockdomain is in software-supervised
959 * idle mode, force the clockdomain to wake. If the clockdomain is in
960 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
961 * ensure that devices in the clockdomain can be read from/written to
962 * by on-chip processors. Returns -EINVAL if passed null pointers;
963 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300964 */
965int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
966{
967 int v;
968
969 /*
970 * XXX Rewrite this code to maintain a list of enabled
971 * downstream clocks for debugging purposes?
972 */
973
Paul Walmsley30962d92010-02-22 22:09:38 -0700974 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300975 return -EINVAL;
976
977 if (atomic_inc_return(&clkdm->usecount) > 1)
978 return 0;
979
980 /* Clockdomain now has one enabled downstream clock */
981
982 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
983 clk->name);
984
Paul Walmsley30962d92010-02-22 22:09:38 -0700985 if (!clkdm->clkstctrl_reg)
986 return 0;
987
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300988 v = omap2_clkdm_clktrctrl_read(clkdm);
989
990 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600991 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
992 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700993 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300994 _clkdm_add_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700995 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600996 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300997 omap2_clkdm_wakeup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600998 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300999
Tomi Valkeinen054ce502009-01-27 19:44:31 -07001000 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001001 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -07001002
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001003 return 0;
1004}
1005
1006/**
1007 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
1008 * @clkdm: struct clockdomain *
1009 * @clk: struct clk * of the disabled downstream clock
1010 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -07001011 * Decrement the usecount of this clockdomain @clkdm when @clk is
1012 * disabled. Intended to be called by clk_disable() code. If the
1013 * clockdomain usecount goes to 0, put the clockdomain to sleep
1014 * (software-supervised mode) or remove the clkdm autodependencies
1015 * (hardware-supervised mode). Returns -EINVAL if passed null
1016 * pointers; -ERANGE if the @clkdm usecount underflows and debugging
1017 * is enabled; or returns 0 upon success or if the clockdomain is in
1018 * hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001019 */
1020int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
1021{
1022 int v;
1023
1024 /*
1025 * XXX Rewrite this code to maintain a list of enabled
1026 * downstream clocks for debugging purposes?
1027 */
1028
Paul Walmsley30962d92010-02-22 22:09:38 -07001029 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001030 return -EINVAL;
1031
1032#ifdef DEBUG
1033 if (atomic_read(&clkdm->usecount) == 0) {
1034 WARN_ON(1); /* underflow */
1035 return -ERANGE;
1036 }
1037#endif
1038
1039 if (atomic_dec_return(&clkdm->usecount) > 0)
1040 return 0;
1041
1042 /* All downstream clocks of this clockdomain are now disabled */
1043
1044 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
1045 clk->name);
1046
Paul Walmsley30962d92010-02-22 22:09:38 -07001047 if (!clkdm->clkstctrl_reg)
1048 return 0;
1049
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001050 v = omap2_clkdm_clktrctrl_read(clkdm);
1051
1052 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001053 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
1054 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001055 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001056 _clkdm_del_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001057 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001058 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001059 omap2_clkdm_sleep(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001060 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001061
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001062 pwrdm_clkdm_state_switch(clkdm);
1063
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001064 return 0;
1065}
1066