blob: 8c9e78c2c17f8fa85ee3304033eb6308c5be9abc [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 Pagare8a3ddc72010-01-26 20:12:54 -07004 * Copyright (C) 2008-2009 Texas Instruments, Inc.
Paul Walmsley33903eb2009-12-08 16:33:10 -07005 * Copyright (C) 2008-2009 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
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/device.h>
19#include <linux/list.h>
20#include <linux/errno.h>
21#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
Tony Lindgrence491cf2009-10-20 09:40:47 -070030#include <plat/clock.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030031
32#include "prm.h"
33#include "prm-regbits-24xx.h"
34#include "cm.h"
35
Tony Lindgrence491cf2009-10-20 09:40:47 -070036#include <plat/powerdomain.h>
37#include <plat/clockdomain.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030038
39/* clkdm_list contains all registered struct clockdomains */
40static LIST_HEAD(clkdm_list);
41
42/* clkdm_mutex protects clkdm_list add and del ops */
43static DEFINE_MUTEX(clkdm_mutex);
44
45/* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
46static struct clkdm_pwrdm_autodep *autodeps;
47
48
49/* Private functions */
50
51/*
52 * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
53 * @autodep: struct clkdm_pwrdm_autodep * to resolve
54 *
55 * Resolve autodep powerdomain names to powerdomain pointers via
56 * pwrdm_lookup() and store the pointers in the autodep structure. An
57 * "autodep" is a powerdomain sleep/wakeup dependency that is
58 * automatically added and removed whenever clocks in the associated
59 * clockdomain are enabled or disabled (respectively) when the
60 * clockdomain is in hardware-supervised mode. Meant to be called
61 * once at clockdomain layer initialization, since these should remain
62 * fixed for a particular architecture. No return value.
63 */
64static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
65{
66 struct powerdomain *pwrdm;
67
68 if (!autodep)
69 return;
70
71 if (!omap_chip_is(autodep->omap_chip))
72 return;
73
Paul Walmsley5b74c672009-02-03 02:10:03 -070074 pwrdm = pwrdm_lookup(autodep->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030075 if (!pwrdm) {
Russell King7aec53a2009-02-22 21:00:55 +000076 pr_err("clockdomain: autodeps: powerdomain %s does not exist\n",
77 autodep->pwrdm.name);
Paul Walmsley5b74c672009-02-03 02:10:03 -070078 pwrdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030079 }
Paul Walmsley5b74c672009-02-03 02:10:03 -070080 autodep->pwrdm.ptr = pwrdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030081}
82
83/*
84 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
85 * @clkdm: struct clockdomain *
86 *
87 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
88 * in hardware-supervised mode. Meant to be called from clock framework
89 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
90 */
91static void _clkdm_add_autodeps(struct clockdomain *clkdm)
92{
93 struct clkdm_pwrdm_autodep *autodep;
94
Paul Walmsley5b74c672009-02-03 02:10:03 -070095 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
96 if (IS_ERR(autodep->pwrdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030097 continue;
98
Paul Walmsleyd96df002009-01-27 19:44:35 -070099 if (!omap_chip_is(autodep->omap_chip))
100 continue;
101
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300102 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley5b74c672009-02-03 02:10:03 -0700103 "pwrdm %s\n", autodep->pwrdm.ptr->name,
104 clkdm->pwrdm.ptr->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300105
Paul Walmsley5b74c672009-02-03 02:10:03 -0700106 pwrdm_add_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
107 pwrdm_add_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300108 }
109}
110
111/*
112 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
113 * @clkdm: struct clockdomain *
114 *
115 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
116 * in hardware-supervised mode. Meant to be called from clock framework
117 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
118 */
119static void _clkdm_del_autodeps(struct clockdomain *clkdm)
120{
121 struct clkdm_pwrdm_autodep *autodep;
122
Paul Walmsley5b74c672009-02-03 02:10:03 -0700123 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
124 if (IS_ERR(autodep->pwrdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300125 continue;
126
Paul Walmsleyd96df002009-01-27 19:44:35 -0700127 if (!omap_chip_is(autodep->omap_chip))
128 continue;
129
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300130 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley5b74c672009-02-03 02:10:03 -0700131 "pwrdm %s\n", autodep->pwrdm.ptr->name,
132 clkdm->pwrdm.ptr->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300133
Paul Walmsley5b74c672009-02-03 02:10:03 -0700134 pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
135 pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300136 }
137}
138
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600139/*
140 * _omap2_clkdm_set_hwsup - set the hwsup idle transition bit
141 * @clkdm: struct clockdomain *
142 * @enable: int 0 to disable, 1 to enable
143 *
144 * Internal helper for actually switching the bit that controls hwsup
145 * idle transitions for clkdm.
146 */
147static void _omap2_clkdm_set_hwsup(struct clockdomain *clkdm, int enable)
148{
Abhijit Pagareb0994742010-01-26 20:12:53 -0700149 u32 bits, v;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600150
151 if (cpu_is_omap24xx()) {
152 if (enable)
Abhijit Pagareb0994742010-01-26 20:12:53 -0700153 bits = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600154 else
Abhijit Pagareb0994742010-01-26 20:12:53 -0700155 bits = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -0700156 } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600157 if (enable)
Abhijit Pagareb0994742010-01-26 20:12:53 -0700158 bits = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600159 else
Abhijit Pagareb0994742010-01-26 20:12:53 -0700160 bits = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600161 } else {
162 BUG();
163 }
164
Abhijit Pagareb0994742010-01-26 20:12:53 -0700165 bits = bits << __ffs(clkdm->clktrctrl_mask);
166
167 v = __raw_readl(clkdm->clkstctrl_reg);
168 v &= ~(clkdm->clktrctrl_mask);
169 v |= bits;
170 __raw_writel(v, clkdm->clkstctrl_reg);
171
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600172}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300173
174static struct clockdomain *_clkdm_lookup(const char *name)
175{
176 struct clockdomain *clkdm, *temp_clkdm;
177
178 if (!name)
179 return NULL;
180
181 clkdm = NULL;
182
183 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
184 if (!strcmp(name, temp_clkdm->name)) {
185 clkdm = temp_clkdm;
186 break;
187 }
188 }
189
190 return clkdm;
191}
192
193
194/* Public functions */
195
196/**
197 * clkdm_init - set up the clockdomain layer
198 * @clkdms: optional pointer to an array of clockdomains to register
199 * @init_autodeps: optional pointer to an array of autodeps to register
200 *
201 * Set up internal state. If a pointer to an array of clockdomains
202 * was supplied, loop through the list of clockdomains, register all
203 * that are available on the current platform. Similarly, if a
204 * pointer to an array of clockdomain-powerdomain autodependencies was
205 * provided, register those. No return value.
206 */
207void clkdm_init(struct clockdomain **clkdms,
208 struct clkdm_pwrdm_autodep *init_autodeps)
209{
210 struct clockdomain **c = NULL;
211 struct clkdm_pwrdm_autodep *autodep = NULL;
212
213 if (clkdms)
214 for (c = clkdms; *c; c++)
215 clkdm_register(*c);
216
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -0700217 if (!cpu_is_omap44xx()) {
218 autodeps = init_autodeps;
219 if (autodeps)
220 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
221 _autodep_lookup(autodep);
222 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300223}
224
225/**
226 * clkdm_register - register a clockdomain
227 * @clkdm: struct clockdomain * to register
228 *
229 * Adds a clockdomain to the internal clockdomain list.
230 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
231 * already registered by the provided name, or 0 upon success.
232 */
233int clkdm_register(struct clockdomain *clkdm)
234{
235 int ret = -EINVAL;
236 struct powerdomain *pwrdm;
237
238 if (!clkdm || !clkdm->name)
239 return -EINVAL;
240
241 if (!omap_chip_is(clkdm->omap_chip))
242 return -EINVAL;
243
Paul Walmsley5b74c672009-02-03 02:10:03 -0700244 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300245 if (!pwrdm) {
Russell King7aec53a2009-02-22 21:00:55 +0000246 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
247 clkdm->name, clkdm->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300248 return -EINVAL;
249 }
Paul Walmsley5b74c672009-02-03 02:10:03 -0700250 clkdm->pwrdm.ptr = pwrdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300251
252 mutex_lock(&clkdm_mutex);
253 /* Verify that the clockdomain is not already registered */
254 if (_clkdm_lookup(clkdm->name)) {
255 ret = -EEXIST;
256 goto cr_unlock;
Russell King7aec53a2009-02-22 21:00:55 +0000257 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300258
259 list_add(&clkdm->node, &clkdm_list);
260
Paul Walmsley8420bb12008-08-19 11:08:44 +0300261 pwrdm_add_clkdm(pwrdm, clkdm);
262
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300263 pr_debug("clockdomain: registered %s\n", clkdm->name);
264 ret = 0;
265
266cr_unlock:
267 mutex_unlock(&clkdm_mutex);
268
269 return ret;
270}
271
272/**
273 * clkdm_unregister - unregister a clockdomain
274 * @clkdm: struct clockdomain * to unregister
275 *
276 * Removes a clockdomain from the internal clockdomain list. Returns
277 * -EINVAL if clkdm argument is NULL.
278 */
279int clkdm_unregister(struct clockdomain *clkdm)
280{
281 if (!clkdm)
282 return -EINVAL;
283
Paul Walmsley5b74c672009-02-03 02:10:03 -0700284 pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300285
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300286 mutex_lock(&clkdm_mutex);
287 list_del(&clkdm->node);
288 mutex_unlock(&clkdm_mutex);
289
290 pr_debug("clockdomain: unregistered %s\n", clkdm->name);
291
292 return 0;
293}
294
295/**
296 * clkdm_lookup - look up a clockdomain by name, return a pointer
297 * @name: name of clockdomain
298 *
299 * Find a registered clockdomain by its name. Returns a pointer to the
300 * struct clockdomain if found, or NULL otherwise.
301 */
302struct clockdomain *clkdm_lookup(const char *name)
303{
304 struct clockdomain *clkdm, *temp_clkdm;
305
306 if (!name)
307 return NULL;
308
309 clkdm = NULL;
310
311 mutex_lock(&clkdm_mutex);
312 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
313 if (!strcmp(name, temp_clkdm->name)) {
314 clkdm = temp_clkdm;
315 break;
316 }
317 }
318 mutex_unlock(&clkdm_mutex);
319
320 return clkdm;
321}
322
323/**
324 * clkdm_for_each - call function on each registered clockdomain
325 * @fn: callback function *
326 *
327 * Call the supplied function for each registered clockdomain.
328 * The callback function can return anything but 0 to bail
329 * out early from the iterator. The callback function is called with
330 * the clkdm_mutex held, so no clockdomain structure manipulation
331 * functions should be called from the callback, although hardware
332 * clockdomain control functions are fine. Returns the last return
333 * value of the callback function, which should be 0 for success or
334 * anything else to indicate failure; or -EINVAL if the function pointer
335 * is null.
336 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300337int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
338 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300339{
340 struct clockdomain *clkdm;
341 int ret = 0;
342
343 if (!fn)
344 return -EINVAL;
345
346 mutex_lock(&clkdm_mutex);
347 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300348 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300349 if (ret)
350 break;
351 }
352 mutex_unlock(&clkdm_mutex);
353
354 return ret;
355}
356
357
Paul Walmsleye89087c2008-05-20 18:41:35 -0600358/**
359 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
360 * @clkdm: struct clockdomain *
361 *
362 * Return a pointer to the struct powerdomain that the specified clockdomain
363 * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
364 */
365struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
366{
367 if (!clkdm)
368 return NULL;
369
Paul Walmsley5b74c672009-02-03 02:10:03 -0700370 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600371}
372
373
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300374/* Hardware clockdomain control */
375
376/**
377 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
378 * @clk: struct clk * of a clockdomain
379 *
380 * Return the clockdomain's current state transition mode from the
Abhijit Pagare84c0c392010-01-26 20:12:53 -0700381 * corresponding domain OMAP2_CM_CLKSTCTRL register. Returns -EINVAL if clk
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300382 * is NULL or the current mode upon success.
383 */
384static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
385{
386 u32 v;
387
388 if (!clkdm)
389 return -EINVAL;
390
Abhijit Pagareb0994742010-01-26 20:12:53 -0700391 v = __raw_readl(clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300392 v &= clkdm->clktrctrl_mask;
393 v >>= __ffs(clkdm->clktrctrl_mask);
394
395 return v;
396}
397
398/**
399 * omap2_clkdm_sleep - force clockdomain sleep transition
400 * @clkdm: struct clockdomain *
401 *
402 * Instruct the CM to force a sleep transition on the specified
403 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
404 * clockdomain does not support software-initiated sleep; 0 upon
405 * success.
406 */
407int omap2_clkdm_sleep(struct clockdomain *clkdm)
408{
409 if (!clkdm)
410 return -EINVAL;
411
412 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
413 pr_debug("clockdomain: %s does not support forcing "
414 "sleep via software\n", clkdm->name);
415 return -EINVAL;
416 }
417
418 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
419
420 if (cpu_is_omap24xx()) {
421
422 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
Abhijit Pagare37903002010-01-26 20:12:51 -0700423 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300424
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -0700425 } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300426
Abhijit Pagareb0994742010-01-26 20:12:53 -0700427 u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300428 __ffs(clkdm->clktrctrl_mask));
429
Abhijit Pagareb0994742010-01-26 20:12:53 -0700430 u32 v = __raw_readl(clkdm->clkstctrl_reg);
431 v &= ~(clkdm->clktrctrl_mask);
432 v |= bits;
433 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300434
435 } else {
436 BUG();
437 };
438
439 return 0;
440}
441
442/**
443 * omap2_clkdm_wakeup - force clockdomain wakeup transition
444 * @clkdm: struct clockdomain *
445 *
446 * Instruct the CM to force a wakeup transition on the specified
447 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
448 * clockdomain does not support software-controlled wakeup; 0 upon
449 * success.
450 */
451int omap2_clkdm_wakeup(struct clockdomain *clkdm)
452{
453 if (!clkdm)
454 return -EINVAL;
455
456 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
457 pr_debug("clockdomain: %s does not support forcing "
458 "wakeup via software\n", clkdm->name);
459 return -EINVAL;
460 }
461
462 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
463
464 if (cpu_is_omap24xx()) {
465
466 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
Abhijit Pagare37903002010-01-26 20:12:51 -0700467 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300468
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -0700469 } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300470
Abhijit Pagareb0994742010-01-26 20:12:53 -0700471 u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300472 __ffs(clkdm->clktrctrl_mask));
473
Abhijit Pagareb0994742010-01-26 20:12:53 -0700474 u32 v = __raw_readl(clkdm->clkstctrl_reg);
475 v &= ~(clkdm->clktrctrl_mask);
476 v |= bits;
477 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300478
479 } else {
480 BUG();
481 };
482
483 return 0;
484}
485
486/**
487 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
488 * @clkdm: struct clockdomain *
489 *
490 * Allow the hardware to automatically switch the clockdomain into
491 * active or idle states, as needed by downstream clocks. If the
492 * clockdomain has any downstream clocks enabled in the clock
493 * framework, wkdep/sleepdep autodependencies are added; this is so
494 * device drivers can read and write to the device. No return value.
495 */
496void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
497{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300498 if (!clkdm)
499 return;
500
501 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
502 pr_debug("clock: automatic idle transitions cannot be enabled "
503 "on clockdomain %s\n", clkdm->name);
504 return;
505 }
506
507 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
508 clkdm->name);
509
510 if (atomic_read(&clkdm->usecount) > 0)
511 _clkdm_add_autodeps(clkdm);
512
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600513 _omap2_clkdm_set_hwsup(clkdm, 1);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300514
515 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300516}
517
518/**
519 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
520 * @clkdm: struct clockdomain *
521 *
522 * Prevent the hardware from automatically switching the clockdomain
523 * into inactive or idle states. If the clockdomain has downstream
524 * clocks enabled in the clock framework, wkdep/sleepdep
525 * autodependencies are removed. No return value.
526 */
527void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
528{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300529 if (!clkdm)
530 return;
531
532 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
533 pr_debug("clockdomain: automatic idle transitions cannot be "
534 "disabled on %s\n", clkdm->name);
535 return;
536 }
537
538 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
539 clkdm->name);
540
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600541 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300542
543 if (atomic_read(&clkdm->usecount) > 0)
544 _clkdm_del_autodeps(clkdm);
545}
546
547
548/* Clockdomain-to-clock framework interface code */
549
550/**
551 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
552 * @clkdm: struct clockdomain *
553 * @clk: struct clk * of the enabled downstream clock
554 *
555 * Increment the usecount of this clockdomain 'clkdm' and ensure that
556 * it is awake. Intended to be called by clk_enable() code. If the
557 * clockdomain is in software-supervised idle mode, force the
558 * clockdomain to wake. If the clockdomain is in hardware-supervised
559 * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
560 * in the clockdomain can be read from/written to by on-chip processors.
561 * Returns -EINVAL if passed null pointers; returns 0 upon success or
562 * if the clockdomain is in hwsup idle mode.
563 */
564int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
565{
566 int v;
567
568 /*
569 * XXX Rewrite this code to maintain a list of enabled
570 * downstream clocks for debugging purposes?
571 */
572
Abhijit Pagareb0994742010-01-26 20:12:53 -0700573 if (!clkdm || !clk || !clkdm->clkstctrl_reg)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300574 return -EINVAL;
575
576 if (atomic_inc_return(&clkdm->usecount) > 1)
577 return 0;
578
579 /* Clockdomain now has one enabled downstream clock */
580
581 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
582 clk->name);
583
584 v = omap2_clkdm_clktrctrl_read(clkdm);
585
586 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600587 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
588 /* Disable HW transitions when we are changing deps */
589 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300590 _clkdm_add_autodeps(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600591 _omap2_clkdm_set_hwsup(clkdm, 1);
592 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300593 omap2_clkdm_wakeup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600594 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300595
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700596 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300597 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700598
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300599 return 0;
600}
601
602/**
603 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
604 * @clkdm: struct clockdomain *
605 * @clk: struct clk * of the disabled downstream clock
606 *
607 * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
608 * called by clk_disable() code. If the usecount goes to 0, put the
609 * clockdomain to sleep (software-supervised mode) or remove the
610 * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
611 * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
612 * underflows and debugging is enabled; or returns 0 upon success or
613 * if the clockdomain is in hwsup idle mode.
614 */
615int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
616{
617 int v;
618
619 /*
620 * XXX Rewrite this code to maintain a list of enabled
621 * downstream clocks for debugging purposes?
622 */
623
Abhijit Pagareb0994742010-01-26 20:12:53 -0700624 if (!clkdm || !clk || !clkdm->clkstctrl_reg)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300625 return -EINVAL;
626
627#ifdef DEBUG
628 if (atomic_read(&clkdm->usecount) == 0) {
629 WARN_ON(1); /* underflow */
630 return -ERANGE;
631 }
632#endif
633
634 if (atomic_dec_return(&clkdm->usecount) > 0)
635 return 0;
636
637 /* All downstream clocks of this clockdomain are now disabled */
638
639 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
640 clk->name);
641
642 v = omap2_clkdm_clktrctrl_read(clkdm);
643
644 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600645 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
646 /* Disable HW transitions when we are changing deps */
647 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300648 _clkdm_del_autodeps(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600649 _omap2_clkdm_set_hwsup(clkdm, 1);
650 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300651 omap2_clkdm_sleep(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600652 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300653
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300654 pwrdm_clkdm_state_switch(clkdm);
655
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300656 return 0;
657}
658