blob: e20b98636ab444b1d6e21b4e00ab9ab5eb980a67 [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 Walmsley55ae3502010-12-21 21:05:15 -070032#include "cm-regbits-24xx.h"
Paul Walmsleybd2122c2010-12-21 21:05:15 -070033#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>
Paul Walmsley72e06d02010-12-21 21:05:16 -070037#include "powerdomain.h"
Paul Walmsley1540f2142010-12-21 21:05:15 -070038#include "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{
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700249 if (cpu_is_omap24xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700250 omap2xxx_cm_clkdm_enable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
251 clkdm->clktrctrl_mask);
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700252 else if (cpu_is_omap34xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700253 omap3xxx_cm_clkdm_enable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
254 clkdm->clktrctrl_mask);
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();
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700261}
262
263/**
264 * _disable_hwsup - place a clockdomain into software-supervised idle
265 * @clkdm: struct clockdomain *
266 *
267 * Place the clockdomain @clkdm into software-supervised idle mode.
268 * No return value.
269 *
270 * XXX Should this return an error if the clockdomain does not support
271 * software-supervised idle mode?
272 */
273static void _disable_hwsup(struct clockdomain *clkdm)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600274{
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700275 if (cpu_is_omap24xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700276 omap2xxx_cm_clkdm_disable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
277 clkdm->clktrctrl_mask);
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700278 else if (cpu_is_omap34xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700279 omap3xxx_cm_clkdm_disable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
280 clkdm->clktrctrl_mask);
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700281 else if (cpu_is_omap44xx())
282 return omap4_cminst_clkdm_disable_hwsup(clkdm->prcm_partition,
283 clkdm->cm_inst,
284 clkdm->clkdm_offs);
285 else
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600286 BUG();
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600287}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300288
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300289/* Public functions */
290
291/**
292 * clkdm_init - set up the clockdomain layer
293 * @clkdms: optional pointer to an array of clockdomains to register
294 * @init_autodeps: optional pointer to an array of autodeps to register
295 *
296 * Set up internal state. If a pointer to an array of clockdomains
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700297 * @clkdms was supplied, loop through the list of clockdomains,
298 * register all that are available on the current platform. Similarly,
299 * if a pointer to an array of clockdomain autodependencies
300 * @init_autodeps was provided, register those. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300301 */
302void clkdm_init(struct clockdomain **clkdms,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700303 struct clkdm_autodep *init_autodeps)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300304{
305 struct clockdomain **c = NULL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700306 struct clockdomain *clkdm;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700307 struct clkdm_autodep *autodep = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300308
309 if (clkdms)
310 for (c = clkdms; *c; c++)
Paul Walmsleye909d622010-01-26 20:13:00 -0700311 _clkdm_register(*c);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300312
Paul Walmsley55ed9692010-01-26 20:12:59 -0700313 autodeps = init_autodeps;
314 if (autodeps)
315 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
316 _autodep_lookup(autodep);
Paul Walmsley369d5612010-01-26 20:13:01 -0700317
318 /*
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600319 * Put all clockdomains into software-supervised mode; PM code
320 * should later enable hardware-supervised mode as appropriate
Paul Walmsley369d5612010-01-26 20:13:01 -0700321 */
322 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600323 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
324 omap2_clkdm_wakeup(clkdm);
325 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
326 omap2_clkdm_deny_idle(clkdm);
327
328 clkdm_clear_all_wkdeps(clkdm);
329 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700330 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300331}
332
333/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300334 * clkdm_lookup - look up a clockdomain by name, return a pointer
335 * @name: name of clockdomain
336 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700337 * Find a registered clockdomain by its name @name. Returns a pointer
338 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300339 */
340struct clockdomain *clkdm_lookup(const char *name)
341{
342 struct clockdomain *clkdm, *temp_clkdm;
343
344 if (!name)
345 return NULL;
346
347 clkdm = NULL;
348
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300349 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
350 if (!strcmp(name, temp_clkdm->name)) {
351 clkdm = temp_clkdm;
352 break;
353 }
354 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300355
356 return clkdm;
357}
358
359/**
360 * clkdm_for_each - call function on each registered clockdomain
361 * @fn: callback function *
362 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700363 * Call the supplied function @fn for each registered clockdomain.
364 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300365 * out early from the iterator. The callback function is called with
366 * the clkdm_mutex held, so no clockdomain structure manipulation
367 * functions should be called from the callback, although hardware
368 * clockdomain control functions are fine. Returns the last return
369 * value of the callback function, which should be 0 for success or
370 * anything else to indicate failure; or -EINVAL if the function pointer
371 * is null.
372 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300373int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
374 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300375{
376 struct clockdomain *clkdm;
377 int ret = 0;
378
379 if (!fn)
380 return -EINVAL;
381
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300382 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300383 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300384 if (ret)
385 break;
386 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300387
388 return ret;
389}
390
391
Paul Walmsleye89087c2008-05-20 18:41:35 -0600392/**
393 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
394 * @clkdm: struct clockdomain *
395 *
396 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700397 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600398 */
399struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
400{
401 if (!clkdm)
402 return NULL;
403
Paul Walmsley5b74c672009-02-03 02:10:03 -0700404 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600405}
406
407
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300408/* Hardware clockdomain control */
409
410/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700411 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
412 * @clkdm1: wake this struct clockdomain * up (dependent)
413 * @clkdm2: when this struct clockdomain * wakes up (source)
414 *
415 * When the clockdomain represented by @clkdm2 wakes up, wake up
416 * @clkdm1. Implemented in hardware on the OMAP, this feature is
417 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
418 * Returns -EINVAL if presented with invalid clockdomain pointers,
419 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
420 * success.
421 */
422int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
423{
424 struct clkdm_dep *cd;
425
426 if (!clkdm1 || !clkdm2)
427 return -EINVAL;
428
429 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
430 if (IS_ERR(cd)) {
431 pr_debug("clockdomain: hardware cannot set/clear wake up of "
432 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
433 return PTR_ERR(cd);
434 }
435
Paul Walmsley369d5612010-01-26 20:13:01 -0700436 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
437 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
438 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700439
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700440 omap2_prm_set_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700441 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
442 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700443
444 return 0;
445}
446
447/**
448 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
449 * @clkdm1: wake this struct clockdomain * up (dependent)
450 * @clkdm2: when this struct clockdomain * wakes up (source)
451 *
452 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
453 * wakes up. Returns -EINVAL if presented with invalid clockdomain
454 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
455 * 0 upon success.
456 */
457int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
458{
459 struct clkdm_dep *cd;
460
461 if (!clkdm1 || !clkdm2)
462 return -EINVAL;
463
464 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
465 if (IS_ERR(cd)) {
466 pr_debug("clockdomain: hardware cannot set/clear wake up of "
467 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
468 return PTR_ERR(cd);
469 }
470
Paul Walmsley369d5612010-01-26 20:13:01 -0700471 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
472 pr_debug("clockdomain: hardware will no longer wake up %s "
473 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700474
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700475 omap2_prm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700476 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
477 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700478
479 return 0;
480}
481
482/**
483 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
484 * @clkdm1: wake this struct clockdomain * up (dependent)
485 * @clkdm2: when this struct clockdomain * wakes up (source)
486 *
487 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
488 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
489 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
490 * is incapable.
491 *
492 * REVISIT: Currently this function only represents software-controllable
493 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
494 * yet handled here.
495 */
496int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
497{
498 struct clkdm_dep *cd;
499
500 if (!clkdm1 || !clkdm2)
501 return -EINVAL;
502
503 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
504 if (IS_ERR(cd)) {
505 pr_debug("clockdomain: hardware cannot set/clear wake up of "
506 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
507 return PTR_ERR(cd);
508 }
509
Paul Walmsley369d5612010-01-26 20:13:01 -0700510 /* XXX It's faster to return the atomic wkdep_usecount */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700511 return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700512 (1 << clkdm2->dep_bit));
513}
514
515/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700516 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
517 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
518 *
519 * Remove all inter-clockdomain wakeup dependencies that could cause
520 * @clkdm to wake. Intended to be used during boot to initialize the
521 * PRCM to a known state, after all clockdomains are put into swsup idle
522 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
523 * 0 upon success.
524 */
525int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
526{
527 struct clkdm_dep *cd;
528 u32 mask = 0;
529
530 if (!clkdm)
531 return -EINVAL;
532
533 for (cd = clkdm->wkdep_srcs; cd && cd->clkdm_name; cd++) {
534 if (!omap_chip_is(cd->omap_chip))
535 continue;
536
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600537 if (!cd->clkdm && cd->clkdm_name)
538 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
539
Paul Walmsley369d5612010-01-26 20:13:01 -0700540 /* PRM accesses are slow, so minimize them */
541 mask |= 1 << cd->clkdm->dep_bit;
542 atomic_set(&cd->wkdep_usecount, 0);
543 }
544
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700545 omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs, PM_WKDEP);
Paul Walmsley369d5612010-01-26 20:13:01 -0700546
547 return 0;
548}
549
550/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700551 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
552 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
553 * @clkdm2: when this struct clockdomain * is active (source)
554 *
555 * Prevent @clkdm1 from automatically going inactive (and then to
556 * retention or off) if @clkdm2 is active. Returns -EINVAL if
557 * presented with invalid clockdomain pointers or called on a machine
558 * that does not support software-configurable hardware sleep
559 * dependencies, -ENOENT if the specified dependency cannot be set in
560 * hardware, or 0 upon success.
561 */
562int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
563{
564 struct clkdm_dep *cd;
565
566 if (!cpu_is_omap34xx())
567 return -EINVAL;
568
569 if (!clkdm1 || !clkdm2)
570 return -EINVAL;
571
572 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
573 if (IS_ERR(cd)) {
574 pr_debug("clockdomain: hardware cannot set/clear sleep "
575 "dependency affecting %s from %s\n", clkdm1->name,
576 clkdm2->name);
577 return PTR_ERR(cd);
578 }
579
Paul Walmsley369d5612010-01-26 20:13:01 -0700580 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
581 pr_debug("clockdomain: will prevent %s from sleeping if %s "
582 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700583
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700584 omap2_cm_set_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700585 clkdm1->pwrdm.ptr->prcm_offs,
586 OMAP3430_CM_SLEEPDEP);
587 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700588
589 return 0;
590}
591
592/**
593 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
594 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
595 * @clkdm2: when this struct clockdomain * is active (source)
596 *
597 * Allow @clkdm1 to automatically go inactive (and then to retention or
598 * off), independent of the activity state of @clkdm2. Returns -EINVAL
599 * if presented with invalid clockdomain pointers or called on a machine
600 * that does not support software-configurable hardware sleep dependencies,
601 * -ENOENT if the specified dependency cannot be cleared in hardware, or
602 * 0 upon success.
603 */
604int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
605{
606 struct clkdm_dep *cd;
607
608 if (!cpu_is_omap34xx())
609 return -EINVAL;
610
611 if (!clkdm1 || !clkdm2)
612 return -EINVAL;
613
614 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
615 if (IS_ERR(cd)) {
616 pr_debug("clockdomain: hardware cannot set/clear sleep "
617 "dependency affecting %s from %s\n", clkdm1->name,
618 clkdm2->name);
619 return PTR_ERR(cd);
620 }
621
Paul Walmsley369d5612010-01-26 20:13:01 -0700622 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
623 pr_debug("clockdomain: will no longer prevent %s from "
624 "sleeping if %s is active\n", clkdm1->name,
625 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700626
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700627 omap2_cm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700628 clkdm1->pwrdm.ptr->prcm_offs,
629 OMAP3430_CM_SLEEPDEP);
630 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700631
632 return 0;
633}
634
635/**
636 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
637 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
638 * @clkdm2: when this struct clockdomain * is active (source)
639 *
640 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
641 * not be allowed to automatically go inactive if @clkdm2 is active;
642 * 0 if @clkdm1's automatic power state inactivity transition is independent
643 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
644 * on a machine that does not support software-configurable hardware sleep
645 * dependencies; or -ENOENT if the hardware is incapable.
646 *
647 * REVISIT: Currently this function only represents software-controllable
648 * sleep dependencies. Sleep dependencies fixed in hardware are not
649 * yet handled here.
650 */
651int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
652{
653 struct clkdm_dep *cd;
654
655 if (!cpu_is_omap34xx())
656 return -EINVAL;
657
658 if (!clkdm1 || !clkdm2)
659 return -EINVAL;
660
661 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
662 if (IS_ERR(cd)) {
663 pr_debug("clockdomain: hardware cannot set/clear sleep "
664 "dependency affecting %s from %s\n", clkdm1->name,
665 clkdm2->name);
666 return PTR_ERR(cd);
667 }
668
Paul Walmsley369d5612010-01-26 20:13:01 -0700669 /* XXX It's faster to return the atomic sleepdep_usecount */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700670 return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700671 OMAP3430_CM_SLEEPDEP,
672 (1 << clkdm2->dep_bit));
673}
674
Paul Walmsley369d5612010-01-26 20:13:01 -0700675/**
676 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
677 * @clkdm: struct clockdomain * to remove all sleep dependencies from
678 *
679 * Remove all inter-clockdomain sleep dependencies that could prevent
680 * @clkdm from idling. Intended to be used during boot to initialize the
681 * PRCM to a known state, after all clockdomains are put into swsup idle
682 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
683 * 0 upon success.
684 */
685int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
686{
687 struct clkdm_dep *cd;
688 u32 mask = 0;
689
690 if (!cpu_is_omap34xx())
691 return -EINVAL;
692
693 if (!clkdm)
694 return -EINVAL;
695
696 for (cd = clkdm->sleepdep_srcs; cd && cd->clkdm_name; cd++) {
697 if (!omap_chip_is(cd->omap_chip))
698 continue;
699
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600700 if (!cd->clkdm && cd->clkdm_name)
701 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
702
Paul Walmsley369d5612010-01-26 20:13:01 -0700703 /* PRM accesses are slow, so minimize them */
704 mask |= 1 << cd->clkdm->dep_bit;
705 atomic_set(&cd->sleepdep_usecount, 0);
706 }
707
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700708 omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs,
Paul Walmsley369d5612010-01-26 20:13:01 -0700709 OMAP3430_CM_SLEEPDEP);
710
711 return 0;
712}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700713
714/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300715 * omap2_clkdm_sleep - force clockdomain sleep transition
716 * @clkdm: struct clockdomain *
717 *
718 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700719 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300720 * clockdomain does not support software-initiated sleep; 0 upon
721 * success.
722 */
723int omap2_clkdm_sleep(struct clockdomain *clkdm)
724{
725 if (!clkdm)
726 return -EINVAL;
727
728 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
729 pr_debug("clockdomain: %s does not support forcing "
730 "sleep via software\n", clkdm->name);
731 return -EINVAL;
732 }
733
734 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
735
736 if (cpu_is_omap24xx()) {
737
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700738 omap2_cm_set_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700739 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300740
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700741 } else if (cpu_is_omap34xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300742
Paul Walmsley55ae3502010-12-21 21:05:15 -0700743 omap3xxx_cm_clkdm_force_sleep(clkdm->pwrdm.ptr->prcm_offs,
744 clkdm->clktrctrl_mask);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300745
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700746 } else if (cpu_is_omap44xx()) {
747
748 omap4_cminst_clkdm_force_sleep(clkdm->prcm_partition,
749 clkdm->cm_inst,
750 clkdm->clkdm_offs);
751
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300752 } else {
753 BUG();
754 };
755
756 return 0;
757}
758
759/**
760 * omap2_clkdm_wakeup - force clockdomain wakeup transition
761 * @clkdm: struct clockdomain *
762 *
763 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700764 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300765 * clockdomain does not support software-controlled wakeup; 0 upon
766 * success.
767 */
768int omap2_clkdm_wakeup(struct clockdomain *clkdm)
769{
770 if (!clkdm)
771 return -EINVAL;
772
773 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
774 pr_debug("clockdomain: %s does not support forcing "
775 "wakeup via software\n", clkdm->name);
776 return -EINVAL;
777 }
778
779 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
780
781 if (cpu_is_omap24xx()) {
782
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700783 omap2_cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700784 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300785
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700786 } else if (cpu_is_omap34xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300787
Paul Walmsley55ae3502010-12-21 21:05:15 -0700788 omap3xxx_cm_clkdm_force_wakeup(clkdm->pwrdm.ptr->prcm_offs,
789 clkdm->clktrctrl_mask);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300790
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700791 } else if (cpu_is_omap44xx()) {
792
793 omap4_cminst_clkdm_force_wakeup(clkdm->prcm_partition,
794 clkdm->cm_inst,
795 clkdm->clkdm_offs);
796
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300797 } else {
798 BUG();
799 };
800
801 return 0;
802}
803
804/**
805 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
806 * @clkdm: struct clockdomain *
807 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700808 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300809 * active or idle states, as needed by downstream clocks. If the
810 * clockdomain has any downstream clocks enabled in the clock
811 * framework, wkdep/sleepdep autodependencies are added; this is so
812 * device drivers can read and write to the device. No return value.
813 */
814void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
815{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300816 if (!clkdm)
817 return;
818
819 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
820 pr_debug("clock: automatic idle transitions cannot be enabled "
821 "on clockdomain %s\n", clkdm->name);
822 return;
823 }
824
825 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
826 clkdm->name);
827
Abhijit Pagare91808a82010-02-22 22:09:07 -0700828 /*
829 * XXX This should be removed once TI adds wakeup/sleep
830 * dependency code and data for OMAP4.
831 */
832 if (cpu_is_omap44xx()) {
833 WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
834 "support is not yet implemented\n");
835 } else {
836 if (atomic_read(&clkdm->usecount) > 0)
837 _clkdm_add_autodeps(clkdm);
838 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300839
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700840 _enable_hwsup(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300841
842 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300843}
844
845/**
846 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
847 * @clkdm: struct clockdomain *
848 *
849 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700850 * @clkdm into inactive or idle states. If the clockdomain has
851 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300852 * autodependencies are removed. No return value.
853 */
854void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
855{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300856 if (!clkdm)
857 return;
858
859 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
860 pr_debug("clockdomain: automatic idle transitions cannot be "
861 "disabled on %s\n", clkdm->name);
862 return;
863 }
864
865 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
866 clkdm->name);
867
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700868 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300869
Abhijit Pagare91808a82010-02-22 22:09:07 -0700870 /*
871 * XXX This should be removed once TI adds wakeup/sleep
872 * dependency code and data for OMAP4.
873 */
874 if (cpu_is_omap44xx()) {
875 WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
876 "support is not yet implemented\n");
877 } else {
878 if (atomic_read(&clkdm->usecount) > 0)
879 _clkdm_del_autodeps(clkdm);
880 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300881}
882
883
884/* Clockdomain-to-clock framework interface code */
885
886/**
887 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
888 * @clkdm: struct clockdomain *
889 * @clk: struct clk * of the enabled downstream clock
890 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700891 * Increment the usecount of the clockdomain @clkdm and ensure that it
892 * is awake before @clk is enabled. Intended to be called by
893 * clk_enable() code. If the clockdomain is in software-supervised
894 * idle mode, force the clockdomain to wake. If the clockdomain is in
895 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
896 * ensure that devices in the clockdomain can be read from/written to
897 * by on-chip processors. Returns -EINVAL if passed null pointers;
898 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300899 */
900int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
901{
Paul Walmsley55ae3502010-12-21 21:05:15 -0700902 bool hwsup = false;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300903
904 /*
905 * XXX Rewrite this code to maintain a list of enabled
906 * downstream clocks for debugging purposes?
907 */
908
Paul Walmsley30962d92010-02-22 22:09:38 -0700909 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300910 return -EINVAL;
911
912 if (atomic_inc_return(&clkdm->usecount) > 1)
913 return 0;
914
915 /* Clockdomain now has one enabled downstream clock */
916
917 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
918 clk->name);
919
Paul Walmsley55ae3502010-12-21 21:05:15 -0700920 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Paul Walmsley30962d92010-02-22 22:09:38 -0700921
Paul Walmsley55ae3502010-12-21 21:05:15 -0700922 if (!clkdm->clktrctrl_mask)
923 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300924
Paul Walmsley55ae3502010-12-21 21:05:15 -0700925 hwsup = omap2_cm_is_clkdm_in_hwsup(clkdm->pwrdm.ptr->prcm_offs,
926 clkdm->clktrctrl_mask);
927
928 } else if (cpu_is_omap44xx()) {
929
930 hwsup = omap4_cminst_is_clkdm_in_hwsup(clkdm->prcm_partition,
931 clkdm->cm_inst,
932 clkdm->clkdm_offs);
933
934 }
935
936 if (hwsup) {
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600937 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700938 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300939 _clkdm_add_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700940 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600941 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300942 omap2_clkdm_wakeup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600943 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300944
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700945 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300946 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700947
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300948 return 0;
949}
950
951/**
952 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
953 * @clkdm: struct clockdomain *
954 * @clk: struct clk * of the disabled downstream clock
955 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700956 * Decrement the usecount of this clockdomain @clkdm when @clk is
957 * disabled. Intended to be called by clk_disable() code. If the
958 * clockdomain usecount goes to 0, put the clockdomain to sleep
959 * (software-supervised mode) or remove the clkdm autodependencies
960 * (hardware-supervised mode). Returns -EINVAL if passed null
961 * pointers; -ERANGE if the @clkdm usecount underflows and debugging
962 * is enabled; or returns 0 upon success or if the clockdomain is in
963 * hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300964 */
965int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
966{
Paul Walmsley55ae3502010-12-21 21:05:15 -0700967 bool hwsup = false;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300968
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#ifdef DEBUG
978 if (atomic_read(&clkdm->usecount) == 0) {
979 WARN_ON(1); /* underflow */
980 return -ERANGE;
981 }
982#endif
983
984 if (atomic_dec_return(&clkdm->usecount) > 0)
985 return 0;
986
987 /* All downstream clocks of this clockdomain are now disabled */
988
989 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
990 clk->name);
991
Paul Walmsley55ae3502010-12-21 21:05:15 -0700992 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Paul Walmsley30962d92010-02-22 22:09:38 -0700993
Paul Walmsley55ae3502010-12-21 21:05:15 -0700994 if (!clkdm->clktrctrl_mask)
995 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300996
Paul Walmsley55ae3502010-12-21 21:05:15 -0700997 hwsup = omap2_cm_is_clkdm_in_hwsup(clkdm->pwrdm.ptr->prcm_offs,
998 clkdm->clktrctrl_mask);
999
1000 } else if (cpu_is_omap44xx()) {
1001
1002 hwsup = omap4_cminst_is_clkdm_in_hwsup(clkdm->prcm_partition,
1003 clkdm->cm_inst,
1004 clkdm->clkdm_offs);
1005
1006 }
1007
1008 if (hwsup) {
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001009 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001010 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001011 _clkdm_del_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001012 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001013 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001014 omap2_clkdm_sleep(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001015 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001016
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001017 pwrdm_clkdm_state_switch(clkdm);
1018
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001019 return 0;
1020}
1021