blob: 895c153c18e0c3aec4c82ae957209c4be8a966f8 [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
Rajendra Nayak32d40342011-02-25 16:06:47 -070047static struct clkdm_ops *arch_clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030048
49/* Private functions */
50
Paul Walmsley55ed9692010-01-26 20:12:59 -070051static struct clockdomain *_clkdm_lookup(const char *name)
52{
53 struct clockdomain *clkdm, *temp_clkdm;
54
55 if (!name)
56 return NULL;
57
58 clkdm = NULL;
59
60 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
61 if (!strcmp(name, temp_clkdm->name)) {
62 clkdm = temp_clkdm;
63 break;
64 }
65 }
66
67 return clkdm;
68}
69
Paul Walmsleye909d622010-01-26 20:13:00 -070070/**
71 * _clkdm_register - register a clockdomain
72 * @clkdm: struct clockdomain * to register
73 *
74 * Adds a clockdomain to the internal clockdomain list.
75 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
76 * already registered by the provided name, or 0 upon success.
77 */
78static int _clkdm_register(struct clockdomain *clkdm)
79{
80 struct powerdomain *pwrdm;
81
82 if (!clkdm || !clkdm->name)
83 return -EINVAL;
84
85 if (!omap_chip_is(clkdm->omap_chip))
86 return -EINVAL;
87
88 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
89 if (!pwrdm) {
90 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
91 clkdm->name, clkdm->pwrdm.name);
92 return -EINVAL;
93 }
94 clkdm->pwrdm.ptr = pwrdm;
95
96 /* Verify that the clockdomain is not already registered */
97 if (_clkdm_lookup(clkdm->name))
98 return -EEXIST;
99
100 list_add(&clkdm->node, &clkdm_list);
101
102 pwrdm_add_clkdm(pwrdm, clkdm);
103
104 pr_debug("clockdomain: registered %s\n", clkdm->name);
105
106 return 0;
107}
108
Paul Walmsley55ed9692010-01-26 20:12:59 -0700109/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
110static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
111 struct clkdm_dep *deps)
112{
113 struct clkdm_dep *cd;
114
115 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
116 return ERR_PTR(-EINVAL);
117
118 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700119 if (!omap_chip_is(cd->omap_chip))
120 continue;
121
122 if (!cd->clkdm && cd->clkdm_name)
123 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
124
125 if (cd->clkdm == clkdm)
126 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700127 }
128
129 if (!cd->clkdm_name)
130 return ERR_PTR(-ENOENT);
131
132 return cd;
133}
134
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300135/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700136 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
137 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300138 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700139 * Resolve autodep clockdomain names to clockdomain pointers via
140 * clkdm_lookup() and store the pointers in the autodep structure. An
141 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300142 * automatically added and removed whenever clocks in the associated
143 * clockdomain are enabled or disabled (respectively) when the
144 * clockdomain is in hardware-supervised mode. Meant to be called
145 * once at clockdomain layer initialization, since these should remain
146 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700147 *
148 * XXX autodeps are deprecated and should be removed at the earliest
149 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300150 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700151static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300152{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700153 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300154
155 if (!autodep)
156 return;
157
158 if (!omap_chip_is(autodep->omap_chip))
159 return;
160
Paul Walmsley55ed9692010-01-26 20:12:59 -0700161 clkdm = clkdm_lookup(autodep->clkdm.name);
162 if (!clkdm) {
163 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
164 autodep->clkdm.name);
165 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300166 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700167 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300168}
169
170/*
171 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
172 * @clkdm: struct clockdomain *
173 *
174 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
175 * in hardware-supervised mode. Meant to be called from clock framework
176 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700177 *
178 * XXX autodeps are deprecated and should be removed at the earliest
179 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300180 */
181static void _clkdm_add_autodeps(struct clockdomain *clkdm)
182{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700183 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300184
Paul Walmsleyad956162010-02-22 22:09:35 -0700185 if (!autodeps)
186 return;
187
Paul Walmsley55ed9692010-01-26 20:12:59 -0700188 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
189 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300190 continue;
191
Paul Walmsleyd96df002009-01-27 19:44:35 -0700192 if (!omap_chip_is(autodep->omap_chip))
193 continue;
194
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300195 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700196 "clkdm %s\n", autodep->clkdm.ptr->name,
197 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300198
Paul Walmsley55ed9692010-01-26 20:12:59 -0700199 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
200 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300201 }
202}
203
204/*
205 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
206 * @clkdm: struct clockdomain *
207 *
208 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
209 * in hardware-supervised mode. Meant to be called from clock framework
210 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700211 *
212 * XXX autodeps are deprecated and should be removed at the earliest
213 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300214 */
215static void _clkdm_del_autodeps(struct clockdomain *clkdm)
216{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700217 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300218
Paul Walmsleyad956162010-02-22 22:09:35 -0700219 if (!autodeps)
220 return;
221
Paul Walmsley55ed9692010-01-26 20:12:59 -0700222 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
223 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300224 continue;
225
Paul Walmsleyd96df002009-01-27 19:44:35 -0700226 if (!omap_chip_is(autodep->omap_chip))
227 continue;
228
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300229 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700230 "clkdm %s\n", autodep->clkdm.ptr->name,
231 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300232
Paul Walmsley55ed9692010-01-26 20:12:59 -0700233 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
234 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300235 }
236}
237
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700238/**
239 * _enable_hwsup - place a clockdomain into hardware-supervised idle
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600240 * @clkdm: struct clockdomain *
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600241 *
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700242 * Place the clockdomain into hardware-supervised idle mode. No return
243 * value.
244 *
245 * XXX Should this return an error if the clockdomain does not support
246 * hardware-supervised idle mode?
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600247 */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700248static void _enable_hwsup(struct clockdomain *clkdm)
249{
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700250 if (cpu_is_omap24xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700251 omap2xxx_cm_clkdm_enable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
252 clkdm->clktrctrl_mask);
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700253 else if (cpu_is_omap34xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700254 omap3xxx_cm_clkdm_enable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
255 clkdm->clktrctrl_mask);
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700256 else if (cpu_is_omap44xx())
257 return omap4_cminst_clkdm_enable_hwsup(clkdm->prcm_partition,
258 clkdm->cm_inst,
259 clkdm->clkdm_offs);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700260 else
261 BUG();
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700262}
263
264/**
265 * _disable_hwsup - place a clockdomain into software-supervised idle
266 * @clkdm: struct clockdomain *
267 *
268 * Place the clockdomain @clkdm into software-supervised idle mode.
269 * No return value.
270 *
271 * XXX Should this return an error if the clockdomain does not support
272 * software-supervised idle mode?
273 */
274static void _disable_hwsup(struct clockdomain *clkdm)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600275{
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700276 if (cpu_is_omap24xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700277 omap2xxx_cm_clkdm_disable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
278 clkdm->clktrctrl_mask);
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700279 else if (cpu_is_omap34xx())
Paul Walmsley55ae3502010-12-21 21:05:15 -0700280 omap3xxx_cm_clkdm_disable_hwsup(clkdm->pwrdm.ptr->prcm_offs,
281 clkdm->clktrctrl_mask);
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700282 else if (cpu_is_omap44xx())
283 return omap4_cminst_clkdm_disable_hwsup(clkdm->prcm_partition,
284 clkdm->cm_inst,
285 clkdm->clkdm_offs);
286 else
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600287 BUG();
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600288}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300289
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700290/**
291 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
292 * @clkdm: clockdomain that we are resolving dependencies for
293 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
294 *
295 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
296 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
297 * No return value.
298 */
299static void _resolve_clkdm_deps(struct clockdomain *clkdm,
300 struct clkdm_dep *clkdm_deps)
301{
302 struct clkdm_dep *cd;
303
304 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
305 if (!omap_chip_is(cd->omap_chip))
306 continue;
307 if (cd->clkdm)
308 continue;
309 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
310
311 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
312 clkdm->name, cd->clkdm_name);
313 }
314}
315
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300316/* Public functions */
317
318/**
319 * clkdm_init - set up the clockdomain layer
320 * @clkdms: optional pointer to an array of clockdomains to register
321 * @init_autodeps: optional pointer to an array of autodeps to register
Rajendra Nayak32d40342011-02-25 16:06:47 -0700322 * @custom_funcs: func pointers for arch specfic implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300323 *
324 * Set up internal state. If a pointer to an array of clockdomains
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700325 * @clkdms was supplied, loop through the list of clockdomains,
326 * register all that are available on the current platform. Similarly,
327 * if a pointer to an array of clockdomain autodependencies
328 * @init_autodeps was provided, register those. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300329 */
330void clkdm_init(struct clockdomain **clkdms,
Rajendra Nayak32d40342011-02-25 16:06:47 -0700331 struct clkdm_autodep *init_autodeps,
332 struct clkdm_ops *custom_funcs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300333{
334 struct clockdomain **c = NULL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700335 struct clockdomain *clkdm;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700336 struct clkdm_autodep *autodep = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300337
Rajendra Nayak32d40342011-02-25 16:06:47 -0700338 if (!custom_funcs)
339 WARN(1, "No custom clkdm functions registered\n");
340 else
341 arch_clkdm = custom_funcs;
342
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300343 if (clkdms)
344 for (c = clkdms; *c; c++)
Paul Walmsleye909d622010-01-26 20:13:00 -0700345 _clkdm_register(*c);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300346
Paul Walmsley55ed9692010-01-26 20:12:59 -0700347 autodeps = init_autodeps;
348 if (autodeps)
349 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
350 _autodep_lookup(autodep);
Paul Walmsley369d5612010-01-26 20:13:01 -0700351
352 /*
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600353 * Put all clockdomains into software-supervised mode; PM code
354 * should later enable hardware-supervised mode as appropriate
Paul Walmsley369d5612010-01-26 20:13:01 -0700355 */
356 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600357 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
358 omap2_clkdm_wakeup(clkdm);
359 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
360 omap2_clkdm_deny_idle(clkdm);
361
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700362 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600363 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700364
365 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600366 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700367 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300368}
369
370/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300371 * clkdm_lookup - look up a clockdomain by name, return a pointer
372 * @name: name of clockdomain
373 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700374 * Find a registered clockdomain by its name @name. Returns a pointer
375 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300376 */
377struct clockdomain *clkdm_lookup(const char *name)
378{
379 struct clockdomain *clkdm, *temp_clkdm;
380
381 if (!name)
382 return NULL;
383
384 clkdm = NULL;
385
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300386 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
387 if (!strcmp(name, temp_clkdm->name)) {
388 clkdm = temp_clkdm;
389 break;
390 }
391 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300392
393 return clkdm;
394}
395
396/**
397 * clkdm_for_each - call function on each registered clockdomain
398 * @fn: callback function *
399 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700400 * Call the supplied function @fn for each registered clockdomain.
401 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300402 * out early from the iterator. The callback function is called with
403 * the clkdm_mutex held, so no clockdomain structure manipulation
404 * functions should be called from the callback, although hardware
405 * clockdomain control functions are fine. Returns the last return
406 * value of the callback function, which should be 0 for success or
407 * anything else to indicate failure; or -EINVAL if the function pointer
408 * is null.
409 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300410int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
411 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300412{
413 struct clockdomain *clkdm;
414 int ret = 0;
415
416 if (!fn)
417 return -EINVAL;
418
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300419 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300420 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300421 if (ret)
422 break;
423 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300424
425 return ret;
426}
427
428
Paul Walmsleye89087c2008-05-20 18:41:35 -0600429/**
430 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
431 * @clkdm: struct clockdomain *
432 *
433 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700434 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600435 */
436struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
437{
438 if (!clkdm)
439 return NULL;
440
Paul Walmsley5b74c672009-02-03 02:10:03 -0700441 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600442}
443
444
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300445/* Hardware clockdomain control */
446
447/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700448 * clkdm_add_wkdep - add 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 * When the clockdomain represented by @clkdm2 wakes up, wake up
453 * @clkdm1. Implemented in hardware on the OMAP, this feature is
454 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
455 * Returns -EINVAL if presented with invalid clockdomain pointers,
456 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
457 * success.
458 */
459int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
460{
461 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700462 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700463
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700464 if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) {
465 pr_err("clockdomain: %s/%s: %s: not yet implemented\n",
466 clkdm1->name, clkdm2->name, __func__);
467 return -EINVAL;
468 }
469
Paul Walmsley55ed9692010-01-26 20:12:59 -0700470 if (!clkdm1 || !clkdm2)
471 return -EINVAL;
472
473 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700474 if (IS_ERR(cd))
475 ret = PTR_ERR(cd);
476
477 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
478 ret = -EINVAL;
479
480 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700481 pr_debug("clockdomain: hardware cannot set/clear wake up of "
482 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700483 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700484 }
485
Paul Walmsley369d5612010-01-26 20:13:01 -0700486 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
487 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
488 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700489
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700490 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700491 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700492
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700493 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700494}
495
496/**
497 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
498 * @clkdm1: wake this struct clockdomain * up (dependent)
499 * @clkdm2: when this struct clockdomain * wakes up (source)
500 *
501 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
502 * wakes up. Returns -EINVAL if presented with invalid clockdomain
503 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
504 * 0 upon success.
505 */
506int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
507{
508 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700509 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700510
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700511 if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) {
512 pr_err("clockdomain: %s/%s: %s: not yet implemented\n",
513 clkdm1->name, clkdm2->name, __func__);
514 return -EINVAL;
515 }
516
Paul Walmsley55ed9692010-01-26 20:12:59 -0700517 if (!clkdm1 || !clkdm2)
518 return -EINVAL;
519
520 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700521 if (IS_ERR(cd))
522 ret = PTR_ERR(cd);
523
524 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
525 ret = -EINVAL;
526
527 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700528 pr_debug("clockdomain: hardware cannot set/clear wake up of "
529 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700530 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700531 }
532
Paul Walmsley369d5612010-01-26 20:13:01 -0700533 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
534 pr_debug("clockdomain: hardware will no longer wake up %s "
535 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700536
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700537 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700538 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700539
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700540 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700541}
542
543/**
544 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
545 * @clkdm1: wake this struct clockdomain * up (dependent)
546 * @clkdm2: when this struct clockdomain * wakes up (source)
547 *
548 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
549 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
550 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
551 * is incapable.
552 *
553 * REVISIT: Currently this function only represents software-controllable
554 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
555 * yet handled here.
556 */
557int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
558{
559 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700560 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700561
562 if (!clkdm1 || !clkdm2)
563 return -EINVAL;
564
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700565 if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) {
566 pr_err("clockdomain: %s/%s: %s: not yet implemented\n",
567 clkdm1->name, clkdm2->name, __func__);
568 return -EINVAL;
569 }
570
Paul Walmsley55ed9692010-01-26 20:12:59 -0700571 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700572 if (IS_ERR(cd))
573 ret = PTR_ERR(cd);
574
575 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
576 ret = -EINVAL;
577
578 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700579 pr_debug("clockdomain: hardware cannot set/clear wake up of "
580 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700581 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700582 }
583
Paul Walmsley369d5612010-01-26 20:13:01 -0700584 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700585 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700586}
587
588/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700589 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
590 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
591 *
592 * Remove all inter-clockdomain wakeup dependencies that could cause
593 * @clkdm to wake. Intended to be used during boot to initialize the
594 * PRCM to a known state, after all clockdomains are put into swsup idle
595 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
596 * 0 upon success.
597 */
598int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
599{
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700600 if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) {
601 pr_err("clockdomain: %s: %s: not yet implemented\n",
602 clkdm->name, __func__);
603 return -EINVAL;
604 }
605
Paul Walmsley369d5612010-01-26 20:13:01 -0700606 if (!clkdm)
607 return -EINVAL;
608
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700609 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
610 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700611
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700612 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700613}
614
615/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700616 * clkdm_add_sleepdep - add 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 * Prevent @clkdm1 from automatically going inactive (and then to
621 * retention or off) if @clkdm2 is active. Returns -EINVAL if
622 * presented with invalid clockdomain pointers or called on a machine
623 * that does not support software-configurable hardware sleep
624 * dependencies, -ENOENT if the specified dependency cannot be set in
625 * hardware, or 0 upon success.
626 */
627int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
628{
629 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700630 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700631
632 if (!clkdm1 || !clkdm2)
633 return -EINVAL;
634
635 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700636 if (IS_ERR(cd))
637 ret = PTR_ERR(cd);
638
639 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
640 ret = -EINVAL;
641
642 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700643 pr_debug("clockdomain: hardware cannot set/clear sleep "
644 "dependency affecting %s from %s\n", clkdm1->name,
645 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700646 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700647 }
648
Paul Walmsley369d5612010-01-26 20:13:01 -0700649 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
650 pr_debug("clockdomain: will prevent %s from sleeping if %s "
651 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700652
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700653 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700654 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700655
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700656 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700657}
658
659/**
660 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
661 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
662 * @clkdm2: when this struct clockdomain * is active (source)
663 *
664 * Allow @clkdm1 to automatically go inactive (and then to retention or
665 * off), independent of the activity state of @clkdm2. Returns -EINVAL
666 * if presented with invalid clockdomain pointers or called on a machine
667 * that does not support software-configurable hardware sleep dependencies,
668 * -ENOENT if the specified dependency cannot be cleared in hardware, or
669 * 0 upon success.
670 */
671int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
672{
673 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700674 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700675
676 if (!clkdm1 || !clkdm2)
677 return -EINVAL;
678
679 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700680 if (IS_ERR(cd))
681 ret = PTR_ERR(cd);
682
683 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
684 ret = -EINVAL;
685
686 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700687 pr_debug("clockdomain: hardware cannot set/clear sleep "
688 "dependency affecting %s from %s\n", clkdm1->name,
689 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700690 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700691 }
692
Paul Walmsley369d5612010-01-26 20:13:01 -0700693 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
694 pr_debug("clockdomain: will no longer prevent %s from "
695 "sleeping if %s is active\n", clkdm1->name,
696 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700697
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700698 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700699 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700700
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700701 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700702}
703
704/**
705 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
706 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
707 * @clkdm2: when this struct clockdomain * is active (source)
708 *
709 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
710 * not be allowed to automatically go inactive if @clkdm2 is active;
711 * 0 if @clkdm1's automatic power state inactivity transition is independent
712 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
713 * on a machine that does not support software-configurable hardware sleep
714 * dependencies; or -ENOENT if the hardware is incapable.
715 *
716 * REVISIT: Currently this function only represents software-controllable
717 * sleep dependencies. Sleep dependencies fixed in hardware are not
718 * yet handled here.
719 */
720int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
721{
722 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700723 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700724
725 if (!clkdm1 || !clkdm2)
726 return -EINVAL;
727
728 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700729 if (IS_ERR(cd))
730 ret = PTR_ERR(cd);
731
732 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
733 ret = -EINVAL;
734
735 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700736 pr_debug("clockdomain: hardware cannot set/clear sleep "
737 "dependency affecting %s from %s\n", clkdm1->name,
738 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700739 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700740 }
741
Paul Walmsley369d5612010-01-26 20:13:01 -0700742 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700743 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700744}
745
Paul Walmsley369d5612010-01-26 20:13:01 -0700746/**
747 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
748 * @clkdm: struct clockdomain * to remove all sleep dependencies from
749 *
750 * Remove all inter-clockdomain sleep dependencies that could prevent
751 * @clkdm from idling. Intended to be used during boot to initialize the
752 * PRCM to a known state, after all clockdomains are put into swsup idle
753 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
754 * 0 upon success.
755 */
756int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
757{
Paul Walmsley369d5612010-01-26 20:13:01 -0700758 if (!clkdm)
759 return -EINVAL;
760
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700761 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
762 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700763
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700764 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700765}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700766
767/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300768 * omap2_clkdm_sleep - force clockdomain sleep transition
769 * @clkdm: struct clockdomain *
770 *
771 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700772 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300773 * clockdomain does not support software-initiated sleep; 0 upon
774 * success.
775 */
776int omap2_clkdm_sleep(struct clockdomain *clkdm)
777{
778 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 Walmsley55ae3502010-12-21 21:05:15 -0700796 omap3xxx_cm_clkdm_force_sleep(clkdm->pwrdm.ptr->prcm_offs,
797 clkdm->clktrctrl_mask);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300798
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700799 } else if (cpu_is_omap44xx()) {
800
801 omap4_cminst_clkdm_force_sleep(clkdm->prcm_partition,
802 clkdm->cm_inst,
803 clkdm->clkdm_offs);
804
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300805 } else {
806 BUG();
807 };
808
809 return 0;
810}
811
812/**
813 * omap2_clkdm_wakeup - force clockdomain wakeup transition
814 * @clkdm: struct clockdomain *
815 *
816 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700817 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300818 * clockdomain does not support software-controlled wakeup; 0 upon
819 * success.
820 */
821int omap2_clkdm_wakeup(struct clockdomain *clkdm)
822{
823 if (!clkdm)
824 return -EINVAL;
825
826 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
827 pr_debug("clockdomain: %s does not support forcing "
828 "wakeup via software\n", clkdm->name);
829 return -EINVAL;
830 }
831
832 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
833
834 if (cpu_is_omap24xx()) {
835
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700836 omap2_cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700837 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300838
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700839 } else if (cpu_is_omap34xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300840
Paul Walmsley55ae3502010-12-21 21:05:15 -0700841 omap3xxx_cm_clkdm_force_wakeup(clkdm->pwrdm.ptr->prcm_offs,
842 clkdm->clktrctrl_mask);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300843
Paul Walmsleybd2122c2010-12-21 21:05:15 -0700844 } else if (cpu_is_omap44xx()) {
845
846 omap4_cminst_clkdm_force_wakeup(clkdm->prcm_partition,
847 clkdm->cm_inst,
848 clkdm->clkdm_offs);
849
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300850 } else {
851 BUG();
852 };
853
854 return 0;
855}
856
857/**
858 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
859 * @clkdm: struct clockdomain *
860 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700861 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300862 * active or idle states, as needed by downstream clocks. If the
863 * clockdomain has any downstream clocks enabled in the clock
864 * framework, wkdep/sleepdep autodependencies are added; this is so
865 * device drivers can read and write to the device. No return value.
866 */
867void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
868{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300869 if (!clkdm)
870 return;
871
872 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
873 pr_debug("clock: automatic idle transitions cannot be enabled "
874 "on clockdomain %s\n", clkdm->name);
875 return;
876 }
877
878 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
879 clkdm->name);
880
Abhijit Pagare91808a82010-02-22 22:09:07 -0700881 /*
882 * XXX This should be removed once TI adds wakeup/sleep
883 * dependency code and data for OMAP4.
884 */
885 if (cpu_is_omap44xx()) {
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700886 pr_err("clockdomain: %s: OMAP4 wakeup/sleep dependency support: not yet implemented\n", clkdm->name);
Abhijit Pagare91808a82010-02-22 22:09:07 -0700887 } else {
888 if (atomic_read(&clkdm->usecount) > 0)
889 _clkdm_add_autodeps(clkdm);
890 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300891
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700892 _enable_hwsup(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300893
894 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300895}
896
897/**
898 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
899 * @clkdm: struct clockdomain *
900 *
901 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700902 * @clkdm into inactive or idle states. If the clockdomain has
903 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300904 * autodependencies are removed. No return value.
905 */
906void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
907{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300908 if (!clkdm)
909 return;
910
911 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
912 pr_debug("clockdomain: automatic idle transitions cannot be "
913 "disabled on %s\n", clkdm->name);
914 return;
915 }
916
917 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
918 clkdm->name);
919
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700920 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300921
Abhijit Pagare91808a82010-02-22 22:09:07 -0700922 /*
923 * XXX This should be removed once TI adds wakeup/sleep
924 * dependency code and data for OMAP4.
925 */
926 if (cpu_is_omap44xx()) {
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700927 pr_err("clockdomain: %s: OMAP4 wakeup/sleep dependency support: not yet implemented\n", clkdm->name);
Abhijit Pagare91808a82010-02-22 22:09:07 -0700928 } else {
929 if (atomic_read(&clkdm->usecount) > 0)
930 _clkdm_del_autodeps(clkdm);
931 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300932}
933
934
935/* Clockdomain-to-clock framework interface code */
936
937/**
938 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
939 * @clkdm: struct clockdomain *
940 * @clk: struct clk * of the enabled downstream clock
941 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700942 * Increment the usecount of the clockdomain @clkdm and ensure that it
943 * is awake before @clk is enabled. Intended to be called by
944 * clk_enable() code. If the clockdomain is in software-supervised
945 * idle mode, force the clockdomain to wake. If the clockdomain is in
946 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
947 * ensure that devices in the clockdomain can be read from/written to
948 * by on-chip processors. Returns -EINVAL if passed null pointers;
949 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300950 */
951int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
952{
Paul Walmsley55ae3502010-12-21 21:05:15 -0700953 bool hwsup = false;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300954
955 /*
956 * XXX Rewrite this code to maintain a list of enabled
957 * downstream clocks for debugging purposes?
958 */
959
Paul Walmsley30962d92010-02-22 22:09:38 -0700960 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300961 return -EINVAL;
962
963 if (atomic_inc_return(&clkdm->usecount) > 1)
964 return 0;
965
966 /* Clockdomain now has one enabled downstream clock */
967
968 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
969 clk->name);
970
Paul Walmsley55ae3502010-12-21 21:05:15 -0700971 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Paul Walmsley30962d92010-02-22 22:09:38 -0700972
Paul Walmsley55ae3502010-12-21 21:05:15 -0700973 if (!clkdm->clktrctrl_mask)
974 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300975
Paul Walmsley55ae3502010-12-21 21:05:15 -0700976 hwsup = omap2_cm_is_clkdm_in_hwsup(clkdm->pwrdm.ptr->prcm_offs,
977 clkdm->clktrctrl_mask);
978
979 } else if (cpu_is_omap44xx()) {
980
981 hwsup = omap4_cminst_is_clkdm_in_hwsup(clkdm->prcm_partition,
982 clkdm->cm_inst,
983 clkdm->clkdm_offs);
984
985 }
986
987 if (hwsup) {
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600988 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700989 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300990 _clkdm_add_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700991 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600992 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300993 omap2_clkdm_wakeup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600994 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300995
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700996 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300997 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700998
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300999 return 0;
1000}
1001
1002/**
1003 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
1004 * @clkdm: struct clockdomain *
1005 * @clk: struct clk * of the disabled downstream clock
1006 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -07001007 * Decrement the usecount of this clockdomain @clkdm when @clk is
1008 * disabled. Intended to be called by clk_disable() code. If the
1009 * clockdomain usecount goes to 0, put the clockdomain to sleep
1010 * (software-supervised mode) or remove the clkdm autodependencies
1011 * (hardware-supervised mode). Returns -EINVAL if passed null
1012 * pointers; -ERANGE if the @clkdm usecount underflows and debugging
1013 * is enabled; or returns 0 upon success or if the clockdomain is in
1014 * hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001015 */
1016int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
1017{
Paul Walmsley55ae3502010-12-21 21:05:15 -07001018 bool hwsup = false;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001019
1020 /*
1021 * XXX Rewrite this code to maintain a list of enabled
1022 * downstream clocks for debugging purposes?
1023 */
1024
Paul Walmsley30962d92010-02-22 22:09:38 -07001025 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001026 return -EINVAL;
1027
1028#ifdef DEBUG
1029 if (atomic_read(&clkdm->usecount) == 0) {
1030 WARN_ON(1); /* underflow */
1031 return -ERANGE;
1032 }
1033#endif
1034
1035 if (atomic_dec_return(&clkdm->usecount) > 0)
1036 return 0;
1037
1038 /* All downstream clocks of this clockdomain are now disabled */
1039
1040 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
1041 clk->name);
1042
Paul Walmsley55ae3502010-12-21 21:05:15 -07001043 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Paul Walmsley30962d92010-02-22 22:09:38 -07001044
Paul Walmsley55ae3502010-12-21 21:05:15 -07001045 if (!clkdm->clktrctrl_mask)
1046 return 0;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001047
Paul Walmsley55ae3502010-12-21 21:05:15 -07001048 hwsup = omap2_cm_is_clkdm_in_hwsup(clkdm->pwrdm.ptr->prcm_offs,
1049 clkdm->clktrctrl_mask);
1050
1051 } else if (cpu_is_omap44xx()) {
1052
1053 hwsup = omap4_cminst_is_clkdm_in_hwsup(clkdm->prcm_partition,
1054 clkdm->cm_inst,
1055 clkdm->clkdm_offs);
1056
1057 }
1058
1059 if (hwsup) {
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001060 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001061 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001062 _clkdm_del_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001063 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001064 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001065 omap2_clkdm_sleep(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001066 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001067
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001068 pwrdm_clkdm_state_switch(clkdm);
1069
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001070 return 0;
1071}
1072