blob: 8e3276bfed257897df6df176a1a5e44a32ad7c8e [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 Walmsleyd459bfe2008-08-19 11:08:43 +030032
Paul Walmsley55ed9692010-01-26 20:12:59 -070033#include <plat/clock.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070034#include <plat/powerdomain.h>
35#include <plat/clockdomain.h>
Paul Walmsley55ed9692010-01-26 20:12:59 -070036#include <plat/prcm.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030037
38/* clkdm_list contains all registered struct clockdomains */
39static LIST_HEAD(clkdm_list);
40
Paul Walmsley55ed9692010-01-26 20:12:59 -070041/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
42static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030043
44
45/* Private functions */
46
Paul Walmsley55ed9692010-01-26 20:12:59 -070047static struct clockdomain *_clkdm_lookup(const char *name)
48{
49 struct clockdomain *clkdm, *temp_clkdm;
50
51 if (!name)
52 return NULL;
53
54 clkdm = NULL;
55
56 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
57 if (!strcmp(name, temp_clkdm->name)) {
58 clkdm = temp_clkdm;
59 break;
60 }
61 }
62
63 return clkdm;
64}
65
Paul Walmsleye909d622010-01-26 20:13:00 -070066/**
67 * _clkdm_register - register a clockdomain
68 * @clkdm: struct clockdomain * to register
69 *
70 * Adds a clockdomain to the internal clockdomain list.
71 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
72 * already registered by the provided name, or 0 upon success.
73 */
74static int _clkdm_register(struct clockdomain *clkdm)
75{
76 struct powerdomain *pwrdm;
77
78 if (!clkdm || !clkdm->name)
79 return -EINVAL;
80
81 if (!omap_chip_is(clkdm->omap_chip))
82 return -EINVAL;
83
84 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
85 if (!pwrdm) {
86 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
87 clkdm->name, clkdm->pwrdm.name);
88 return -EINVAL;
89 }
90 clkdm->pwrdm.ptr = pwrdm;
91
92 /* Verify that the clockdomain is not already registered */
93 if (_clkdm_lookup(clkdm->name))
94 return -EEXIST;
95
96 list_add(&clkdm->node, &clkdm_list);
97
98 pwrdm_add_clkdm(pwrdm, clkdm);
99
100 pr_debug("clockdomain: registered %s\n", clkdm->name);
101
102 return 0;
103}
104
Paul Walmsley55ed9692010-01-26 20:12:59 -0700105/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
106static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
107 struct clkdm_dep *deps)
108{
109 struct clkdm_dep *cd;
110
111 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
112 return ERR_PTR(-EINVAL);
113
114 for (cd = deps; cd->clkdm_name; cd++) {
Paul Walmsley55ed9692010-01-26 20:12:59 -0700115 if (!omap_chip_is(cd->omap_chip))
116 continue;
117
118 if (!cd->clkdm && cd->clkdm_name)
119 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
120
121 if (cd->clkdm == clkdm)
122 break;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700123 }
124
125 if (!cd->clkdm_name)
126 return ERR_PTR(-ENOENT);
127
128 return cd;
129}
130
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300131/*
Paul Walmsley55ed9692010-01-26 20:12:59 -0700132 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
133 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300134 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700135 * Resolve autodep clockdomain names to clockdomain pointers via
136 * clkdm_lookup() and store the pointers in the autodep structure. An
137 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300138 * automatically added and removed whenever clocks in the associated
139 * clockdomain are enabled or disabled (respectively) when the
140 * clockdomain is in hardware-supervised mode. Meant to be called
141 * once at clockdomain layer initialization, since these should remain
142 * fixed for a particular architecture. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700143 *
144 * XXX autodeps are deprecated and should be removed at the earliest
145 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300146 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700147static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300148{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700149 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300150
151 if (!autodep)
152 return;
153
154 if (!omap_chip_is(autodep->omap_chip))
155 return;
156
Paul Walmsley55ed9692010-01-26 20:12:59 -0700157 clkdm = clkdm_lookup(autodep->clkdm.name);
158 if (!clkdm) {
159 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
160 autodep->clkdm.name);
161 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300162 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700163 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300164}
165
166/*
167 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
168 * @clkdm: struct clockdomain *
169 *
170 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
171 * in hardware-supervised mode. Meant to be called from clock framework
172 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700173 *
174 * XXX autodeps are deprecated and should be removed at the earliest
175 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300176 */
177static void _clkdm_add_autodeps(struct clockdomain *clkdm)
178{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700179 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300180
Paul Walmsleyad956162010-02-22 22:09:35 -0700181 if (!autodeps)
182 return;
183
Paul Walmsley55ed9692010-01-26 20:12:59 -0700184 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
185 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300186 continue;
187
Paul Walmsleyd96df002009-01-27 19:44:35 -0700188 if (!omap_chip_is(autodep->omap_chip))
189 continue;
190
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300191 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700192 "clkdm %s\n", autodep->clkdm.ptr->name,
193 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300194
Paul Walmsley55ed9692010-01-26 20:12:59 -0700195 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
196 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300197 }
198}
199
200/*
201 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
202 * @clkdm: struct clockdomain *
203 *
204 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
205 * in hardware-supervised mode. Meant to be called from clock framework
206 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700207 *
208 * XXX autodeps are deprecated and should be removed at the earliest
209 * opportunity
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300210 */
211static void _clkdm_del_autodeps(struct clockdomain *clkdm)
212{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700213 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300214
Paul Walmsleyad956162010-02-22 22:09:35 -0700215 if (!autodeps)
216 return;
217
Paul Walmsley55ed9692010-01-26 20:12:59 -0700218 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
219 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300220 continue;
221
Paul Walmsleyd96df002009-01-27 19:44:35 -0700222 if (!omap_chip_is(autodep->omap_chip))
223 continue;
224
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300225 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700226 "clkdm %s\n", autodep->clkdm.ptr->name,
227 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300228
Paul Walmsley55ed9692010-01-26 20:12:59 -0700229 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
230 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300231 }
232}
233
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700234/**
235 * _enable_hwsup - place a clockdomain into hardware-supervised idle
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600236 * @clkdm: struct clockdomain *
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600237 *
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700238 * Place the clockdomain into hardware-supervised idle mode. No return
239 * value.
240 *
241 * XXX Should this return an error if the clockdomain does not support
242 * hardware-supervised idle mode?
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600243 */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700244static void _enable_hwsup(struct clockdomain *clkdm)
245{
246 u32 bits, v;
247
248 if (cpu_is_omap24xx())
249 bits = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
250 else if (cpu_is_omap34xx() || cpu_is_omap44xx())
251 bits = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
252 else
253 BUG();
254
255 bits = bits << __ffs(clkdm->clktrctrl_mask);
256
257 v = __raw_readl(clkdm->clkstctrl_reg);
258 v &= ~(clkdm->clktrctrl_mask);
259 v |= bits;
260 __raw_writel(v, clkdm->clkstctrl_reg);
261
262}
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{
Abhijit Pagareb0994742010-01-26 20:12:53 -0700276 u32 bits, v;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600277
278 if (cpu_is_omap24xx()) {
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700279 bits = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
Rajendra Nayak766d3052010-03-31 04:16:30 -0600280 } else if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700281 bits = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600282 } else {
283 BUG();
284 }
285
Abhijit Pagareb0994742010-01-26 20:12:53 -0700286 bits = bits << __ffs(clkdm->clktrctrl_mask);
287
288 v = __raw_readl(clkdm->clkstctrl_reg);
289 v &= ~(clkdm->clktrctrl_mask);
290 v |= bits;
291 __raw_writel(v, clkdm->clkstctrl_reg);
292
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600293}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300294
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300295/* Public functions */
296
297/**
298 * clkdm_init - set up the clockdomain layer
299 * @clkdms: optional pointer to an array of clockdomains to register
300 * @init_autodeps: optional pointer to an array of autodeps to register
301 *
302 * Set up internal state. If a pointer to an array of clockdomains
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700303 * @clkdms was supplied, loop through the list of clockdomains,
304 * register all that are available on the current platform. Similarly,
305 * if a pointer to an array of clockdomain autodependencies
306 * @init_autodeps was provided, register those. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300307 */
308void clkdm_init(struct clockdomain **clkdms,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700309 struct clkdm_autodep *init_autodeps)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300310{
311 struct clockdomain **c = NULL;
Paul Walmsley369d5612010-01-26 20:13:01 -0700312 struct clockdomain *clkdm;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700313 struct clkdm_autodep *autodep = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300314
315 if (clkdms)
316 for (c = clkdms; *c; c++)
Paul Walmsleye909d622010-01-26 20:13:00 -0700317 _clkdm_register(*c);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300318
Paul Walmsley55ed9692010-01-26 20:12:59 -0700319 autodeps = init_autodeps;
320 if (autodeps)
321 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
322 _autodep_lookup(autodep);
Paul Walmsley369d5612010-01-26 20:13:01 -0700323
324 /*
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600325 * Put all clockdomains into software-supervised mode; PM code
326 * should later enable hardware-supervised mode as appropriate
Paul Walmsley369d5612010-01-26 20:13:01 -0700327 */
328 list_for_each_entry(clkdm, &clkdm_list, node) {
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600329 if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
330 omap2_clkdm_wakeup(clkdm);
331 else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
332 omap2_clkdm_deny_idle(clkdm);
333
334 clkdm_clear_all_wkdeps(clkdm);
335 clkdm_clear_all_sleepdeps(clkdm);
Paul Walmsley369d5612010-01-26 20:13:01 -0700336 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300337}
338
339/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300340 * clkdm_lookup - look up a clockdomain by name, return a pointer
341 * @name: name of clockdomain
342 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700343 * Find a registered clockdomain by its name @name. Returns a pointer
344 * to the struct clockdomain if found, or NULL otherwise.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300345 */
346struct clockdomain *clkdm_lookup(const char *name)
347{
348 struct clockdomain *clkdm, *temp_clkdm;
349
350 if (!name)
351 return NULL;
352
353 clkdm = NULL;
354
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300355 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
356 if (!strcmp(name, temp_clkdm->name)) {
357 clkdm = temp_clkdm;
358 break;
359 }
360 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300361
362 return clkdm;
363}
364
365/**
366 * clkdm_for_each - call function on each registered clockdomain
367 * @fn: callback function *
368 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700369 * Call the supplied function @fn for each registered clockdomain.
370 * The callback function @fn can return anything but 0 to bail
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300371 * out early from the iterator. The callback function is called with
372 * the clkdm_mutex held, so no clockdomain structure manipulation
373 * functions should be called from the callback, although hardware
374 * clockdomain control functions are fine. Returns the last return
375 * value of the callback function, which should be 0 for success or
376 * anything else to indicate failure; or -EINVAL if the function pointer
377 * is null.
378 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300379int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
380 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300381{
382 struct clockdomain *clkdm;
383 int ret = 0;
384
385 if (!fn)
386 return -EINVAL;
387
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300388 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300389 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300390 if (ret)
391 break;
392 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300393
394 return ret;
395}
396
397
Paul Walmsleye89087c2008-05-20 18:41:35 -0600398/**
399 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
400 * @clkdm: struct clockdomain *
401 *
402 * Return a pointer to the struct powerdomain that the specified clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700403 * @clkdm exists in, or returns NULL if @clkdm is NULL.
Paul Walmsleye89087c2008-05-20 18:41:35 -0600404 */
405struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
406{
407 if (!clkdm)
408 return NULL;
409
Paul Walmsley5b74c672009-02-03 02:10:03 -0700410 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600411}
412
413
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300414/* Hardware clockdomain control */
415
416/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700417 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
418 * @clkdm1: wake this struct clockdomain * up (dependent)
419 * @clkdm2: when this struct clockdomain * wakes up (source)
420 *
421 * When the clockdomain represented by @clkdm2 wakes up, wake up
422 * @clkdm1. Implemented in hardware on the OMAP, this feature is
423 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
424 * Returns -EINVAL if presented with invalid clockdomain pointers,
425 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
426 * success.
427 */
428int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
429{
430 struct clkdm_dep *cd;
431
432 if (!clkdm1 || !clkdm2)
433 return -EINVAL;
434
435 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
436 if (IS_ERR(cd)) {
437 pr_debug("clockdomain: hardware cannot set/clear wake up of "
438 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
439 return PTR_ERR(cd);
440 }
441
Paul Walmsley369d5612010-01-26 20:13:01 -0700442 if (atomic_inc_return(&cd->wkdep_usecount) == 1) {
443 pr_debug("clockdomain: hardware will wake up %s when %s wakes "
444 "up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700445
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700446 omap2_prm_set_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700447 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
448 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700449
450 return 0;
451}
452
453/**
454 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
455 * @clkdm1: wake this struct clockdomain * up (dependent)
456 * @clkdm2: when this struct clockdomain * wakes up (source)
457 *
458 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
459 * wakes up. Returns -EINVAL if presented with invalid clockdomain
460 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
461 * 0 upon success.
462 */
463int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
464{
465 struct clkdm_dep *cd;
466
467 if (!clkdm1 || !clkdm2)
468 return -EINVAL;
469
470 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
471 if (IS_ERR(cd)) {
472 pr_debug("clockdomain: hardware cannot set/clear wake up of "
473 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
474 return PTR_ERR(cd);
475 }
476
Paul Walmsley369d5612010-01-26 20:13:01 -0700477 if (atomic_dec_return(&cd->wkdep_usecount) == 0) {
478 pr_debug("clockdomain: hardware will no longer wake up %s "
479 "after %s wakes up\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700480
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700481 omap2_prm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700482 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
483 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700484
485 return 0;
486}
487
488/**
489 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
490 * @clkdm1: wake this struct clockdomain * up (dependent)
491 * @clkdm2: when this struct clockdomain * wakes up (source)
492 *
493 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
494 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
495 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
496 * is incapable.
497 *
498 * REVISIT: Currently this function only represents software-controllable
499 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
500 * yet handled here.
501 */
502int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
503{
504 struct clkdm_dep *cd;
505
506 if (!clkdm1 || !clkdm2)
507 return -EINVAL;
508
509 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
510 if (IS_ERR(cd)) {
511 pr_debug("clockdomain: hardware cannot set/clear wake up of "
512 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
513 return PTR_ERR(cd);
514 }
515
Paul Walmsley369d5612010-01-26 20:13:01 -0700516 /* XXX It's faster to return the atomic wkdep_usecount */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700517 return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700518 (1 << clkdm2->dep_bit));
519}
520
521/**
Paul Walmsley369d5612010-01-26 20:13:01 -0700522 * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
523 * @clkdm: struct clockdomain * to remove all wakeup dependencies from
524 *
525 * Remove all inter-clockdomain wakeup dependencies that could cause
526 * @clkdm to wake. Intended to be used during boot to initialize the
527 * PRCM to a known state, after all clockdomains are put into swsup idle
528 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
529 * 0 upon success.
530 */
531int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
532{
533 struct clkdm_dep *cd;
534 u32 mask = 0;
535
536 if (!clkdm)
537 return -EINVAL;
538
539 for (cd = clkdm->wkdep_srcs; cd && cd->clkdm_name; cd++) {
540 if (!omap_chip_is(cd->omap_chip))
541 continue;
542
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600543 if (!cd->clkdm && cd->clkdm_name)
544 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
545
Paul Walmsley369d5612010-01-26 20:13:01 -0700546 /* PRM accesses are slow, so minimize them */
547 mask |= 1 << cd->clkdm->dep_bit;
548 atomic_set(&cd->wkdep_usecount, 0);
549 }
550
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700551 omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs, PM_WKDEP);
Paul Walmsley369d5612010-01-26 20:13:01 -0700552
553 return 0;
554}
555
556/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700557 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
558 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
559 * @clkdm2: when this struct clockdomain * is active (source)
560 *
561 * Prevent @clkdm1 from automatically going inactive (and then to
562 * retention or off) if @clkdm2 is active. Returns -EINVAL if
563 * presented with invalid clockdomain pointers or called on a machine
564 * that does not support software-configurable hardware sleep
565 * dependencies, -ENOENT if the specified dependency cannot be set in
566 * hardware, or 0 upon success.
567 */
568int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
569{
570 struct clkdm_dep *cd;
571
572 if (!cpu_is_omap34xx())
573 return -EINVAL;
574
575 if (!clkdm1 || !clkdm2)
576 return -EINVAL;
577
578 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
579 if (IS_ERR(cd)) {
580 pr_debug("clockdomain: hardware cannot set/clear sleep "
581 "dependency affecting %s from %s\n", clkdm1->name,
582 clkdm2->name);
583 return PTR_ERR(cd);
584 }
585
Paul Walmsley369d5612010-01-26 20:13:01 -0700586 if (atomic_inc_return(&cd->sleepdep_usecount) == 1) {
587 pr_debug("clockdomain: will prevent %s from sleeping if %s "
588 "is active\n", clkdm1->name, clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700589
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700590 omap2_cm_set_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700591 clkdm1->pwrdm.ptr->prcm_offs,
592 OMAP3430_CM_SLEEPDEP);
593 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700594
595 return 0;
596}
597
598/**
599 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
600 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
601 * @clkdm2: when this struct clockdomain * is active (source)
602 *
603 * Allow @clkdm1 to automatically go inactive (and then to retention or
604 * off), independent of the activity state of @clkdm2. Returns -EINVAL
605 * if presented with invalid clockdomain pointers or called on a machine
606 * that does not support software-configurable hardware sleep dependencies,
607 * -ENOENT if the specified dependency cannot be cleared in hardware, or
608 * 0 upon success.
609 */
610int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
611{
612 struct clkdm_dep *cd;
613
614 if (!cpu_is_omap34xx())
615 return -EINVAL;
616
617 if (!clkdm1 || !clkdm2)
618 return -EINVAL;
619
620 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
621 if (IS_ERR(cd)) {
622 pr_debug("clockdomain: hardware cannot set/clear sleep "
623 "dependency affecting %s from %s\n", clkdm1->name,
624 clkdm2->name);
625 return PTR_ERR(cd);
626 }
627
Paul Walmsley369d5612010-01-26 20:13:01 -0700628 if (atomic_dec_return(&cd->sleepdep_usecount) == 0) {
629 pr_debug("clockdomain: will no longer prevent %s from "
630 "sleeping if %s is active\n", clkdm1->name,
631 clkdm2->name);
Paul Walmsley55ed9692010-01-26 20:12:59 -0700632
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700633 omap2_cm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
Paul Walmsley369d5612010-01-26 20:13:01 -0700634 clkdm1->pwrdm.ptr->prcm_offs,
635 OMAP3430_CM_SLEEPDEP);
636 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700637
638 return 0;
639}
640
641/**
642 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
643 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
644 * @clkdm2: when this struct clockdomain * is active (source)
645 *
646 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
647 * not be allowed to automatically go inactive if @clkdm2 is active;
648 * 0 if @clkdm1's automatic power state inactivity transition is independent
649 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
650 * on a machine that does not support software-configurable hardware sleep
651 * dependencies; or -ENOENT if the hardware is incapable.
652 *
653 * REVISIT: Currently this function only represents software-controllable
654 * sleep dependencies. Sleep dependencies fixed in hardware are not
655 * yet handled here.
656 */
657int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
658{
659 struct clkdm_dep *cd;
660
661 if (!cpu_is_omap34xx())
662 return -EINVAL;
663
664 if (!clkdm1 || !clkdm2)
665 return -EINVAL;
666
667 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
668 if (IS_ERR(cd)) {
669 pr_debug("clockdomain: hardware cannot set/clear sleep "
670 "dependency affecting %s from %s\n", clkdm1->name,
671 clkdm2->name);
672 return PTR_ERR(cd);
673 }
674
Paul Walmsley369d5612010-01-26 20:13:01 -0700675 /* XXX It's faster to return the atomic sleepdep_usecount */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700676 return omap2_prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700677 OMAP3430_CM_SLEEPDEP,
678 (1 << clkdm2->dep_bit));
679}
680
Paul Walmsley369d5612010-01-26 20:13:01 -0700681/**
682 * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
683 * @clkdm: struct clockdomain * to remove all sleep dependencies from
684 *
685 * Remove all inter-clockdomain sleep dependencies that could prevent
686 * @clkdm from idling. Intended to be used during boot to initialize the
687 * PRCM to a known state, after all clockdomains are put into swsup idle
688 * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
689 * 0 upon success.
690 */
691int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
692{
693 struct clkdm_dep *cd;
694 u32 mask = 0;
695
696 if (!cpu_is_omap34xx())
697 return -EINVAL;
698
699 if (!clkdm)
700 return -EINVAL;
701
702 for (cd = clkdm->sleepdep_srcs; cd && cd->clkdm_name; cd++) {
703 if (!omap_chip_is(cd->omap_chip))
704 continue;
705
Paul Walmsley6f7f63c2010-09-14 15:56:53 -0600706 if (!cd->clkdm && cd->clkdm_name)
707 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
708
Paul Walmsley369d5612010-01-26 20:13:01 -0700709 /* PRM accesses are slow, so minimize them */
710 mask |= 1 << cd->clkdm->dep_bit;
711 atomic_set(&cd->sleepdep_usecount, 0);
712 }
713
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700714 omap2_prm_clear_mod_reg_bits(mask, clkdm->pwrdm.ptr->prcm_offs,
Paul Walmsley369d5612010-01-26 20:13:01 -0700715 OMAP3430_CM_SLEEPDEP);
716
717 return 0;
718}
Paul Walmsley55ed9692010-01-26 20:12:59 -0700719
720/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300721 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700722 * @clkdm: struct clkdm * of a clockdomain
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300723 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700724 * Return the clockdomain @clkdm current state transition mode from the
725 * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if @clkdm
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300726 * is NULL or the current mode upon success.
727 */
728static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
729{
730 u32 v;
731
732 if (!clkdm)
733 return -EINVAL;
734
Abhijit Pagareb0994742010-01-26 20:12:53 -0700735 v = __raw_readl(clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300736 v &= clkdm->clktrctrl_mask;
737 v >>= __ffs(clkdm->clktrctrl_mask);
738
739 return v;
740}
741
742/**
743 * omap2_clkdm_sleep - force clockdomain sleep transition
744 * @clkdm: struct clockdomain *
745 *
746 * Instruct the CM to force a sleep transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700747 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300748 * clockdomain does not support software-initiated sleep; 0 upon
749 * success.
750 */
751int omap2_clkdm_sleep(struct clockdomain *clkdm)
752{
753 if (!clkdm)
754 return -EINVAL;
755
756 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
757 pr_debug("clockdomain: %s does not support forcing "
758 "sleep via software\n", clkdm->name);
759 return -EINVAL;
760 }
761
762 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
763
764 if (cpu_is_omap24xx()) {
765
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700766 omap2_cm_set_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700767 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300768
Rajendra Nayak766d3052010-03-31 04:16:30 -0600769 } else if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300770
Abhijit Pagareb0994742010-01-26 20:12:53 -0700771 u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300772 __ffs(clkdm->clktrctrl_mask));
773
Abhijit Pagareb0994742010-01-26 20:12:53 -0700774 u32 v = __raw_readl(clkdm->clkstctrl_reg);
775 v &= ~(clkdm->clktrctrl_mask);
776 v |= bits;
777 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300778
779 } else {
780 BUG();
781 };
782
783 return 0;
784}
785
786/**
787 * omap2_clkdm_wakeup - force clockdomain wakeup transition
788 * @clkdm: struct clockdomain *
789 *
790 * Instruct the CM to force a wakeup transition on the specified
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700791 * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300792 * clockdomain does not support software-controlled wakeup; 0 upon
793 * success.
794 */
795int omap2_clkdm_wakeup(struct clockdomain *clkdm)
796{
797 if (!clkdm)
798 return -EINVAL;
799
800 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
801 pr_debug("clockdomain: %s does not support forcing "
802 "wakeup via software\n", clkdm->name);
803 return -EINVAL;
804 }
805
806 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
807
808 if (cpu_is_omap24xx()) {
809
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700810 omap2_cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700811 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300812
Rajendra Nayak766d3052010-03-31 04:16:30 -0600813 } else if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300814
Abhijit Pagareb0994742010-01-26 20:12:53 -0700815 u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300816 __ffs(clkdm->clktrctrl_mask));
817
Abhijit Pagareb0994742010-01-26 20:12:53 -0700818 u32 v = __raw_readl(clkdm->clkstctrl_reg);
819 v &= ~(clkdm->clktrctrl_mask);
820 v |= bits;
821 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300822
823 } else {
824 BUG();
825 };
826
827 return 0;
828}
829
830/**
831 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
832 * @clkdm: struct clockdomain *
833 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700834 * Allow the hardware to automatically switch the clockdomain @clkdm into
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300835 * active or idle states, as needed by downstream clocks. If the
836 * clockdomain has any downstream clocks enabled in the clock
837 * framework, wkdep/sleepdep autodependencies are added; this is so
838 * device drivers can read and write to the device. No return value.
839 */
840void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
841{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300842 if (!clkdm)
843 return;
844
845 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
846 pr_debug("clock: automatic idle transitions cannot be enabled "
847 "on clockdomain %s\n", clkdm->name);
848 return;
849 }
850
851 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
852 clkdm->name);
853
Abhijit Pagare91808a82010-02-22 22:09:07 -0700854 /*
855 * XXX This should be removed once TI adds wakeup/sleep
856 * dependency code and data for OMAP4.
857 */
858 if (cpu_is_omap44xx()) {
859 WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
860 "support is not yet implemented\n");
861 } else {
862 if (atomic_read(&clkdm->usecount) > 0)
863 _clkdm_add_autodeps(clkdm);
864 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300865
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700866 _enable_hwsup(clkdm);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300867
868 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300869}
870
871/**
872 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
873 * @clkdm: struct clockdomain *
874 *
875 * Prevent the hardware from automatically switching the clockdomain
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700876 * @clkdm into inactive or idle states. If the clockdomain has
877 * downstream clocks enabled in the clock framework, wkdep/sleepdep
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300878 * autodependencies are removed. No return value.
879 */
880void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
881{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300882 if (!clkdm)
883 return;
884
885 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
886 pr_debug("clockdomain: automatic idle transitions cannot be "
887 "disabled on %s\n", clkdm->name);
888 return;
889 }
890
891 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
892 clkdm->name);
893
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700894 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300895
Abhijit Pagare91808a82010-02-22 22:09:07 -0700896 /*
897 * XXX This should be removed once TI adds wakeup/sleep
898 * dependency code and data for OMAP4.
899 */
900 if (cpu_is_omap44xx()) {
901 WARN_ONCE(1, "clockdomain: OMAP4 wakeup/sleep dependency "
902 "support is not yet implemented\n");
903 } else {
904 if (atomic_read(&clkdm->usecount) > 0)
905 _clkdm_del_autodeps(clkdm);
906 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300907}
908
909
910/* Clockdomain-to-clock framework interface code */
911
912/**
913 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
914 * @clkdm: struct clockdomain *
915 * @clk: struct clk * of the enabled downstream clock
916 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700917 * Increment the usecount of the clockdomain @clkdm and ensure that it
918 * is awake before @clk is enabled. Intended to be called by
919 * clk_enable() code. If the clockdomain is in software-supervised
920 * idle mode, force the clockdomain to wake. If the clockdomain is in
921 * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
922 * ensure that devices in the clockdomain can be read from/written to
923 * by on-chip processors. Returns -EINVAL if passed null pointers;
924 * returns 0 upon success or if the clockdomain is in hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300925 */
926int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
927{
928 int v;
929
930 /*
931 * XXX Rewrite this code to maintain a list of enabled
932 * downstream clocks for debugging purposes?
933 */
934
Paul Walmsley30962d92010-02-22 22:09:38 -0700935 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300936 return -EINVAL;
937
938 if (atomic_inc_return(&clkdm->usecount) > 1)
939 return 0;
940
941 /* Clockdomain now has one enabled downstream clock */
942
943 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
944 clk->name);
945
Paul Walmsley30962d92010-02-22 22:09:38 -0700946 if (!clkdm->clkstctrl_reg)
947 return 0;
948
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300949 v = omap2_clkdm_clktrctrl_read(clkdm);
950
951 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600952 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
953 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700954 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300955 _clkdm_add_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -0700956 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600957 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300958 omap2_clkdm_wakeup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600959 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300960
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700961 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300962 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700963
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300964 return 0;
965}
966
967/**
968 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
969 * @clkdm: struct clockdomain *
970 * @clk: struct clk * of the disabled downstream clock
971 *
Paul Walmsleyf0271d62010-01-26 20:13:02 -0700972 * Decrement the usecount of this clockdomain @clkdm when @clk is
973 * disabled. Intended to be called by clk_disable() code. If the
974 * clockdomain usecount goes to 0, put the clockdomain to sleep
975 * (software-supervised mode) or remove the clkdm autodependencies
976 * (hardware-supervised mode). Returns -EINVAL if passed null
977 * pointers; -ERANGE if the @clkdm usecount underflows and debugging
978 * is enabled; or returns 0 upon success or if the clockdomain is in
979 * hwsup idle mode.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300980 */
981int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
982{
983 int v;
984
985 /*
986 * XXX Rewrite this code to maintain a list of enabled
987 * downstream clocks for debugging purposes?
988 */
989
Paul Walmsley30962d92010-02-22 22:09:38 -0700990 if (!clkdm || !clk)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300991 return -EINVAL;
992
993#ifdef DEBUG
994 if (atomic_read(&clkdm->usecount) == 0) {
995 WARN_ON(1); /* underflow */
996 return -ERANGE;
997 }
998#endif
999
1000 if (atomic_dec_return(&clkdm->usecount) > 0)
1001 return 0;
1002
1003 /* All downstream clocks of this clockdomain are now disabled */
1004
1005 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
1006 clk->name);
1007
Paul Walmsley30962d92010-02-22 22:09:38 -07001008 if (!clkdm->clkstctrl_reg)
1009 return 0;
1010
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001011 v = omap2_clkdm_clktrctrl_read(clkdm);
1012
1013 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001014 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
1015 /* Disable HW transitions when we are changing deps */
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001016 _disable_hwsup(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001017 _clkdm_del_autodeps(clkdm);
Paul Walmsleyb170fbe2010-12-21 21:05:15 -07001018 _enable_hwsup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001019 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001020 omap2_clkdm_sleep(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -06001021 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001022
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +03001023 pwrdm_clkdm_state_switch(clkdm);
1024
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001025 return 0;
1026}
1027