blob: 6cb6c03293df98030ed356c958ba501a90f76fbc [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 Walmsley55ed9692010-01-26 20:12:59 -070029#include <plat/clock.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -070030#include "clockdomain.h"
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030031
32/* clkdm_list contains all registered struct clockdomains */
33static LIST_HEAD(clkdm_list);
34
Paul Walmsley55ed9692010-01-26 20:12:59 -070035/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
36static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030037
Rajendra Nayak32d40342011-02-25 16:06:47 -070038static struct clkdm_ops *arch_clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030039
40/* Private functions */
41
Paul Walmsley55ed9692010-01-26 20:12:59 -070042static struct clockdomain *_clkdm_lookup(const char *name)
43{
44 struct clockdomain *clkdm, *temp_clkdm;
45
46 if (!name)
47 return NULL;
48
49 clkdm = NULL;
50
51 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
52 if (!strcmp(name, temp_clkdm->name)) {
53 clkdm = temp_clkdm;
54 break;
55 }
56 }
57
58 return clkdm;
59}
60
Paul Walmsleye909d622010-01-26 20:13:00 -070061/**
62 * _clkdm_register - register a clockdomain
63 * @clkdm: struct clockdomain * to register
64 *
65 * Adds a clockdomain to the internal clockdomain list.
66 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
67 * already registered by the provided name, or 0 upon success.
68 */
69static int _clkdm_register(struct clockdomain *clkdm)
70{
71 struct powerdomain *pwrdm;
72
73 if (!clkdm || !clkdm->name)
74 return -EINVAL;
75
76 if (!omap_chip_is(clkdm->omap_chip))
77 return -EINVAL;
78
79 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
80 if (!pwrdm) {
81 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
82 clkdm->name, clkdm->pwrdm.name);
83 return -EINVAL;
84 }
85 clkdm->pwrdm.ptr = pwrdm;
86
87 /* Verify that the clockdomain is not already registered */
88 if (_clkdm_lookup(clkdm->name))
89 return -EEXIST;
90
91 list_add(&clkdm->node, &clkdm_list);
92
93 pwrdm_add_clkdm(pwrdm, clkdm);
94
95 pr_debug("clockdomain: registered %s\n", clkdm->name);
96
97 return 0;
98}
99
Paul Walmsley55ed9692010-01-26 20:12:59 -0700100/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
101static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
102 struct clkdm_dep *deps)
103{
104 struct clkdm_dep *cd;
105
106 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
107 return ERR_PTR(-EINVAL);
108
109 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700110 if (!omap_chip_is(cd->omap_chip))
111 continue;
112
113 if (!cd->clkdm && cd->clkdm_name)
114 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
115
116 if (cd->clkdm == clkdm)
117 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700118 }
119
120 if (!cd->clkdm_name)
121 return ERR_PTR(-ENOENT);
122
123 return cd;
124}
125
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300126/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700127 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
128 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300129 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700130 * Resolve autodep clockdomain names to clockdomain pointers via
131 * clkdm_lookup() and store the pointers in the autodep structure. An
132 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300133 * automatically added and removed whenever clocks in the associated
134 * clockdomain are enabled or disabled (respectively) when the
135 * clockdomain is in hardware-supervised mode. Meant to be called
136 * once at clockdomain layer initialization, since these should remain
137 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700138 *
139 * XXX autodeps are deprecated and should be removed at the earliest
140 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300141 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700142static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300143{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700144 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300145
146 if (!autodep)
147 return;
148
149 if (!omap_chip_is(autodep->omap_chip))
150 return;
151
Paul Walmsley55ed9692010-01-26 20:12:59 -0700152 clkdm = clkdm_lookup(autodep->clkdm.name);
153 if (!clkdm) {
154 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
155 autodep->clkdm.name);
156 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300157 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700158 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300159}
160
161/*
162 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
163 * @clkdm: struct clockdomain *
164 *
165 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
166 * in hardware-supervised mode. Meant to be called from clock framework
167 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700168 *
169 * XXX autodeps are deprecated and should be removed at the earliest
170 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300171 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700172void _clkdm_add_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300173{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700174 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300175
Paul Walmsley570b54c2011-03-10 03:50:09 -0700176 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700177 return;
178
Paul Walmsley55ed9692010-01-26 20:12:59 -0700179 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
180 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300181 continue;
182
Paul Walmsleyd96df002009-01-27 19:44:35 -0700183 if (!omap_chip_is(autodep->omap_chip))
184 continue;
185
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300186 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700187 "clkdm %s\n", autodep->clkdm.ptr->name,
188 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300189
Paul Walmsley55ed9692010-01-26 20:12:59 -0700190 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
191 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300192 }
193}
194
195/*
196 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
197 * @clkdm: struct clockdomain *
198 *
199 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
200 * in hardware-supervised mode. Meant to be called from clock framework
201 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700202 *
203 * XXX autodeps are deprecated and should be removed at the earliest
204 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300205 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700206void _clkdm_del_autodeps(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300207{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700208 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300209
Paul Walmsley570b54c2011-03-10 03:50:09 -0700210 if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsleyad956162010-02-22 22:09:35 -0700211 return;
212
Paul Walmsley55ed9692010-01-26 20:12:59 -0700213 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
214 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300215 continue;
216
Paul Walmsleyd96df002009-01-27 19:44:35 -0700217 if (!omap_chip_is(autodep->omap_chip))
218 continue;
219
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300220 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700221 "clkdm %s\n", autodep->clkdm.ptr->name,
222 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300223
Paul Walmsley55ed9692010-01-26 20:12:59 -0700224 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
225 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300226 }
227}
228
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700229/**
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700230 * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
231 * @clkdm: clockdomain that we are resolving dependencies for
232 * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600233 *
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700234 * Iterates through @clkdm_deps, looking up the struct clockdomain named by
235 * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700236 * No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700237 */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700238static void _resolve_clkdm_deps(struct clockdomain *clkdm,
239 struct clkdm_dep *clkdm_deps)
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600240{
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700241 struct clkdm_dep *cd;
242
243 for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
244 if (!omap_chip_is(cd->omap_chip))
245 continue;
246 if (cd->clkdm)
247 continue;
248 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
249
250 WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
251 clkdm->name, cd->clkdm_name);
252 }
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600253}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300254
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300255/* Public functions */
256
257/**
258 * clkdm_init - set up the clockdomain layer
259 * @clkdms: optional pointer to an array of clockdomains to register
260 * @init_autodeps: optional pointer to an array of autodeps to register
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300261 * @custom_funcs: func pointers for arch specific implementations
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300262 *
263 * Set up internal state. If a pointer to an array of clockdomains
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700264 * @clkdms was supplied, loop through the list of clockdomains,
265 * register all that are available on the current platform. Similarly,
266 * if a pointer to an array of clockdomain autodependencies
267 * @init_autodeps was provided, register those. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300268 */
269void clkdm_init(struct clockdomain **clkdms,
Rajendra Nayak32d40342011-02-25 16:06:47 -0700270 struct clkdm_autodep *init_autodeps,
271 struct clkdm_ops *custom_funcs)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300272{
273 struct clockdomain **c = NULL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700274 struct clockdomain *clkdm;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700275 struct clkdm_autodep *autodep = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300276
Rajendra Nayak32d40342011-02-25 16:06:47 -0700277 if (!custom_funcs)
278 WARN(1, "No custom clkdm functions registered\n");
279 else
280 arch_clkdm = custom_funcs;
281
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300282 if (clkdms)
283 for (c = clkdms; *c; c++)
Paul Walmsleye909d622010-01-26 20:13:00 -0700284 _clkdm_register(*c);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300285
Paul Walmsley55ed9692010-01-26 20:12:59 -0700286 autodeps = init_autodeps;
287 if (autodeps)
288 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
289 _autodep_lookup(autodep);
Paul Walmsley369d5612010-01-26 20:13:01 -0700290
291 /*
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600292 * Put all clockdomains into software-supervised mode; PM code
293 * should later enable hardware-supervised mode as appropriate
Paul Walmsley369d5612010-01-26 20:13:01 -0700294 */
295 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600296 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700297 clkdm_wakeup(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600298 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700299 clkdm_deny_idle(clkdm);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600300
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700301 _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600302 clkdm_clear_all_wkdeps(clkdm);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700303
304 _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600305 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700306 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300307}
308
309/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300310 * clkdm_lookup - look up a clockdomain by name, return a pointer
311 * @name: name of clockdomain
312 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700313 * Find a registered clockdomain by its name @name. Returns a pointer
314 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300315 */
316struct clockdomain *clkdm_lookup(const char *name)
317{
318 struct clockdomain *clkdm, *temp_clkdm;
319
320 if (!name)
321 return NULL;
322
323 clkdm = NULL;
324
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300325 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
326 if (!strcmp(name, temp_clkdm->name)) {
327 clkdm = temp_clkdm;
328 break;
329 }
330 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300331
332 return clkdm;
333}
334
335/**
336 * clkdm_for_each - call function on each registered clockdomain
337 * @fn: callback function *
338 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700339 * Call the supplied function @fn for each registered clockdomain.
340 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300341 * out early from the iterator. The callback function is called with
342 * the clkdm_mutex held, so no clockdomain structure manipulation
343 * functions should be called from the callback, although hardware
344 * clockdomain control functions are fine. Returns the last return
345 * value of the callback function, which should be 0 for success or
346 * anything else to indicate failure; or -EINVAL if the function pointer
347 * is null.
348 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300349int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
350 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300351{
352 struct clockdomain *clkdm;
353 int ret = 0;
354
355 if (!fn)
356 return -EINVAL;
357
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300358 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300359 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300360 if (ret)
361 break;
362 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300363
364 return ret;
365}
366
367
Paul Walmsleye89087c2008-05-20 18:41:35 -0600368/**
369 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
370 * @clkdm: struct clockdomain *
371 *
372 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700373 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600374 */
375struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
376{
377 if (!clkdm)
378 return NULL;
379
Paul Walmsley5b74c672009-02-03 02:10:03 -0700380 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600381}
382
383
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300384/* Hardware clockdomain control */
385
386/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700387 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
388 * @clkdm1: wake this struct clockdomain * up (dependent)
389 * @clkdm2: when this struct clockdomain * wakes up (source)
390 *
391 * When the clockdomain represented by @clkdm2 wakes up, wake up
392 * @clkdm1. Implemented in hardware on the OMAP, this feature is
393 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
394 * Returns -EINVAL if presented with invalid clockdomain pointers,
395 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
396 * success.
397 */
398int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
399{
400 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700401 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700402
Paul Walmsley55ed9692010-01-26 20:12:59 -0700403 if (!clkdm1 || !clkdm2)
404 return -EINVAL;
405
406 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700407 if (IS_ERR(cd))
408 ret = PTR_ERR(cd);
409
410 if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
411 ret = -EINVAL;
412
413 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700414 pr_debug("clockdomain: hardware cannot set/clear wake up of "
415 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700416 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700417 }
418
Paul Walmsley369d5612010-01-26 20:13:01 -0700419 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
420 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
421 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700422
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700423 ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700424 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700425
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700426 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700427}
428
429/**
430 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
431 * @clkdm1: wake this struct clockdomain * up (dependent)
432 * @clkdm2: when this struct clockdomain * wakes up (source)
433 *
434 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
435 * wakes up. Returns -EINVAL if presented with invalid clockdomain
436 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
437 * 0 upon success.
438 */
439int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
440{
441 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700442 int ret = 0;
Paul Walmsley56bc78d2011-01-17 13:28:17 -0700443
Paul Walmsley55ed9692010-01-26 20:12:59 -0700444 if (!clkdm1 || !clkdm2)
445 return -EINVAL;
446
447 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700448 if (IS_ERR(cd))
449 ret = PTR_ERR(cd);
450
451 if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
452 ret = -EINVAL;
453
454 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700455 pr_debug("clockdomain: hardware cannot set/clear wake up of "
456 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700457 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700458 }
459
Paul Walmsley369d5612010-01-26 20:13:01 -0700460 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
461 pr_debug("clockdomain: hardware will no longer wake up %s "
462 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700463
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700464 ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700465 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700466
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700467 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700468}
469
470/**
471 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
472 * @clkdm1: wake this struct clockdomain * up (dependent)
473 * @clkdm2: when this struct clockdomain * wakes up (source)
474 *
475 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
476 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
477 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
478 * is incapable.
479 *
480 * REVISIT: Currently this function only represents software-controllable
481 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
482 * yet handled here.
483 */
484int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
485{
486 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700487 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700488
489 if (!clkdm1 || !clkdm2)
490 return -EINVAL;
491
492 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700493 if (IS_ERR(cd))
494 ret = PTR_ERR(cd);
495
496 if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
497 ret = -EINVAL;
498
499 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700500 pr_debug("clockdomain: hardware cannot set/clear wake up of "
501 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700502 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700503 }
504
Paul Walmsley369d5612010-01-26 20:13:01 -0700505 /* XXX It's faster to return the atomic wkdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700506 return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700507}
508
509/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700510 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
511 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
512 *
513 * Remove all inter-clockdomain wakeup dependencies that could cause
514 * @clkdm to wake. Intended to be used during boot to initialize the
515 * PRCM to a known state, after all clockdomains are put into swsup idle
516 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
517 * 0 upon success.
518 */
519int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
520{
Paul Walmsley369d5612010-01-26 20:13:01 -0700521 if (!clkdm)
522 return -EINVAL;
523
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700524 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
525 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700526
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700527 return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700528}
529
530/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700531 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
532 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
533 * @clkdm2: when this struct clockdomain * is active (source)
534 *
535 * Prevent @clkdm1 from automatically going inactive (and then to
536 * retention or off) if @clkdm2 is active. Returns -EINVAL if
537 * presented with invalid clockdomain pointers or called on a machine
538 * that does not support software-configurable hardware sleep
539 * dependencies, -ENOENT if the specified dependency cannot be set in
540 * hardware, or 0 upon success.
541 */
542int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
543{
544 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700545 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700546
547 if (!clkdm1 || !clkdm2)
548 return -EINVAL;
549
550 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700551 if (IS_ERR(cd))
552 ret = PTR_ERR(cd);
553
554 if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
555 ret = -EINVAL;
556
557 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700558 pr_debug("clockdomain: hardware cannot set/clear sleep "
559 "dependency affecting %s from %s\n", clkdm1->name,
560 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700561 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700562 }
563
Paul Walmsley369d5612010-01-26 20:13:01 -0700564 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
565 pr_debug("clockdomain: will prevent %s from sleeping if %s "
566 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700567
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700568 ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700569 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700570
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700571 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700572}
573
574/**
575 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
576 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
577 * @clkdm2: when this struct clockdomain * is active (source)
578 *
579 * Allow @clkdm1 to automatically go inactive (and then to retention or
580 * off), independent of the activity state of @clkdm2. Returns -EINVAL
581 * if presented with invalid clockdomain pointers or called on a machine
582 * that does not support software-configurable hardware sleep dependencies,
583 * -ENOENT if the specified dependency cannot be cleared in hardware, or
584 * 0 upon success.
585 */
586int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
587{
588 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700589 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700590
591 if (!clkdm1 || !clkdm2)
592 return -EINVAL;
593
594 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700595 if (IS_ERR(cd))
596 ret = PTR_ERR(cd);
597
598 if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
599 ret = -EINVAL;
600
601 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700602 pr_debug("clockdomain: hardware cannot set/clear sleep "
603 "dependency affecting %s from %s\n", clkdm1->name,
604 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700605 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700606 }
607
Paul Walmsley369d5612010-01-26 20:13:01 -0700608 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
609 pr_debug("clockdomain: will no longer prevent %s from "
610 "sleeping if %s is active\n", clkdm1->name,
611 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700612
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700613 ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
Paul Walmsley369d5612010-01-26 20:13:01 -0700614 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700615
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700616 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700617}
618
619/**
620 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
621 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
622 * @clkdm2: when this struct clockdomain * is active (source)
623 *
624 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
625 * not be allowed to automatically go inactive if @clkdm2 is active;
626 * 0 if @clkdm1's automatic power state inactivity transition is independent
627 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
628 * on a machine that does not support software-configurable hardware sleep
629 * dependencies; or -ENOENT if the hardware is incapable.
630 *
631 * REVISIT: Currently this function only represents software-controllable
632 * sleep dependencies. Sleep dependencies fixed in hardware are not
633 * yet handled here.
634 */
635int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
636{
637 struct clkdm_dep *cd;
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700638 int ret = 0;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700639
640 if (!clkdm1 || !clkdm2)
641 return -EINVAL;
642
643 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700644 if (IS_ERR(cd))
645 ret = PTR_ERR(cd);
646
647 if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
648 ret = -EINVAL;
649
650 if (ret) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700651 pr_debug("clockdomain: hardware cannot set/clear sleep "
652 "dependency affecting %s from %s\n", clkdm1->name,
653 clkdm2->name);
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700654 return ret;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700655 }
656
Paul Walmsley369d5612010-01-26 20:13:01 -0700657 /* XXX It's faster to return the atomic sleepdep_usecount */
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700658 return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700659}
660
Paul Walmsley369d5612010-01-26 20:13:01 -0700661/**
662 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
663 * @clkdm: struct clockdomain * to remove all sleep dependencies from
664 *
665 * Remove all inter-clockdomain sleep dependencies that could prevent
666 * @clkdm from idling. Intended to be used during boot to initialize the
667 * PRCM to a known state, after all clockdomains are put into swsup idle
668 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
669 * 0 upon success.
670 */
671int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
672{
Paul Walmsley369d5612010-01-26 20:13:01 -0700673 if (!clkdm)
674 return -EINVAL;
675
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700676 if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
677 return -EINVAL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700678
Rajendra Nayak4aef7a22011-02-25 16:06:47 -0700679 return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700680}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700681
682/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700683 * clkdm_sleep - force clockdomain sleep transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300684 * @clkdm: struct clockdomain *
685 *
686 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700687 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300688 * clockdomain does not support software-initiated sleep; 0 upon
689 * success.
690 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700691int clkdm_sleep(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300692{
693 if (!clkdm)
694 return -EINVAL;
695
696 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
697 pr_debug("clockdomain: %s does not support forcing "
698 "sleep via software\n", clkdm->name);
699 return -EINVAL;
700 }
701
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700702 if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
703 return -EINVAL;
704
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300705 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
706
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700707 return arch_clkdm->clkdm_sleep(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300708}
709
710/**
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700711 * clkdm_wakeup - force clockdomain wakeup transition
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300712 * @clkdm: struct clockdomain *
713 *
714 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700715 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300716 * clockdomain does not support software-controlled wakeup; 0 upon
717 * success.
718 */
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700719int clkdm_wakeup(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300720{
721 if (!clkdm)
722 return -EINVAL;
723
724 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
725 pr_debug("clockdomain: %s does not support forcing "
726 "wakeup via software\n", clkdm->name);
727 return -EINVAL;
728 }
729
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700730 if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
731 return -EINVAL;
732
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300733 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
734
Rajendra Nayak68b921a2011-02-25 16:06:47 -0700735 return arch_clkdm->clkdm_wakeup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300736}
737
738/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700739 * clkdm_allow_idle - enable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300740 * @clkdm: struct clockdomain *
741 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700742 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300743 * active or idle states, as needed by downstream clocks. If the
744 * clockdomain has any downstream clocks enabled in the clock
745 * framework, wkdep/sleepdep autodependencies are added; this is so
746 * device drivers can read and write to the device. No return value.
747 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700748void clkdm_allow_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300749{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300750 if (!clkdm)
751 return;
752
753 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
754 pr_debug("clock: automatic idle transitions cannot be enabled "
755 "on clockdomain %s\n", clkdm->name);
756 return;
757 }
758
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700759 if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
760 return;
761
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300762 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
763 clkdm->name);
764
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700765 arch_clkdm->clkdm_allow_idle(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300766 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300767}
768
769/**
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700770 * clkdm_deny_idle - disable hwsup idle transitions for clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300771 * @clkdm: struct clockdomain *
772 *
773 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700774 * @clkdm into inactive or idle states. If the clockdomain has
775 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300776 * autodependencies are removed. No return value.
777 */
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700778void clkdm_deny_idle(struct clockdomain *clkdm)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300779{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300780 if (!clkdm)
781 return;
782
783 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
784 pr_debug("clockdomain: automatic idle transitions cannot be "
785 "disabled on %s\n", clkdm->name);
786 return;
787 }
788
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700789 if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
790 return;
791
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300792 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
793 clkdm->name);
794
Rajendra Nayak5cd19372011-02-25 16:06:48 -0700795 arch_clkdm->clkdm_deny_idle(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300796}
797
798
799/* Clockdomain-to-clock framework interface code */
800
801/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700802 * clkdm_clk_enable - add an enabled downstream clock to this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300803 * @clkdm: struct clockdomain *
804 * @clk: struct clk * of the enabled downstream clock
805 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700806 * Increment the usecount of the clockdomain @clkdm and ensure that it
807 * is awake before @clk is enabled. Intended to be called by
808 * clk_enable() code. If the clockdomain is in software-supervised
809 * idle mode, force the clockdomain to wake. If the clockdomain is in
810 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
811 * ensure that devices in the clockdomain can be read from/written to
812 * by on-chip processors. Returns -EINVAL if passed null pointers;
813 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300814 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700815int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300816{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300817 /*
818 * XXX Rewrite this code to maintain a list of enabled
819 * downstream clocks for debugging purposes?
820 */
821
Paul Walmsley30962d92010-02-22 22:09:38 -0700822 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300823 return -EINVAL;
824
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700825 if (!arch_clkdm || !arch_clkdm->clkdm_clk_enable)
826 return -EINVAL;
827
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300828 if (atomic_inc_return(&clkdm->usecount) > 1)
829 return 0;
830
831 /* Clockdomain now has one enabled downstream clock */
832
833 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
834 clk->name);
835
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700836 arch_clkdm->clkdm_clk_enable(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700837 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300838 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700839
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300840 return 0;
841}
842
843/**
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700844 * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300845 * @clkdm: struct clockdomain *
846 * @clk: struct clk * of the disabled downstream clock
847 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700848 * Decrement the usecount of this clockdomain @clkdm when @clk is
849 * disabled. Intended to be called by clk_disable() code. If the
850 * clockdomain usecount goes to 0, put the clockdomain to sleep
851 * (software-supervised mode) or remove the clkdm autodependencies
852 * (hardware-supervised mode). Returns -EINVAL if passed null
853 * pointers; -ERANGE if the @clkdm usecount underflows and debugging
854 * is enabled; or returns 0 upon success or if the clockdomain is in
855 * hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300856 */
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700857int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300858{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300859 /*
860 * XXX Rewrite this code to maintain a list of enabled
861 * downstream clocks for debugging purposes?
862 */
863
Paul Walmsley30962d92010-02-22 22:09:38 -0700864 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300865 return -EINVAL;
866
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700867 if (!arch_clkdm || !arch_clkdm->clkdm_clk_disable)
868 return -EINVAL;
869
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300870#ifdef DEBUG
871 if (atomic_read(&clkdm->usecount) == 0) {
872 WARN_ON(1); /* underflow */
873 return -ERANGE;
874 }
875#endif
876
877 if (atomic_dec_return(&clkdm->usecount) > 0)
878 return 0;
879
880 /* All downstream clocks of this clockdomain are now disabled */
881
882 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
883 clk->name);
884
Rajendra Nayak4da71ae2011-02-25 16:06:48 -0700885 arch_clkdm->clkdm_clk_disable(clkdm);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300886 pwrdm_clkdm_state_switch(clkdm);
887
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300888 return 0;
889}
890