Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 1 | /* |
Abhijit Pagare | 8a3ddc7 | 2010-01-26 20:12:54 -0700 | [diff] [blame] | 2 | * OMAP2/3/4 clockdomain framework functions |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 3 | * |
Abhijit Pagare | 91808a8 | 2010-02-22 22:09:07 -0700 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 Texas Instruments, Inc. |
| 5 | * Copyright (C) 2008-2010 Nokia Corporation |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 6 | * |
| 7 | * Written by Paul Walmsley and Jouni Högander |
Abhijit Pagare | 8a3ddc7 | 2010-01-26 20:12:54 -0700 | [diff] [blame] | 8 | * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com> |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 9 | * |
| 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 Walmsley | 33903eb | 2009-12-08 16:33:10 -0700 | [diff] [blame] | 14 | #undef DEBUG |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 15 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 16 | #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 Walmsley | 5b74c67 | 2009-02-03 02:10:03 -0700 | [diff] [blame] | 23 | #include <linux/err.h> |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 24 | |
| 25 | #include <linux/io.h> |
| 26 | |
| 27 | #include <linux/bitops.h> |
| 28 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 29 | #include <plat/clock.h> |
Paul Walmsley | 1540f214 | 2010-12-21 21:05:15 -0700 | [diff] [blame] | 30 | #include "clockdomain.h" |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 31 | |
| 32 | /* clkdm_list contains all registered struct clockdomains */ |
| 33 | static LIST_HEAD(clkdm_list); |
| 34 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 35 | /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */ |
| 36 | static struct clkdm_autodep *autodeps; |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 37 | |
Rajendra Nayak | 32d4034 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 38 | static struct clkdm_ops *arch_clkdm; |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 39 | |
| 40 | /* Private functions */ |
| 41 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 42 | static 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 Walmsley | e909d62 | 2010-01-26 20:13:00 -0700 | [diff] [blame] | 61 | /** |
| 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 | */ |
| 69 | static 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 Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 100 | /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */ |
| 101 | static 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 Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 110 | 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 Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if (!cd->clkdm_name) |
| 121 | return ERR_PTR(-ENOENT); |
| 122 | |
| 123 | return cd; |
| 124 | } |
| 125 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 126 | /* |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 127 | * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store |
| 128 | * @autodep: struct clkdm_autodep * to resolve |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 129 | * |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 130 | * 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 Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 133 | * 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 Walmsley | b170fbe | 2010-12-21 21:05:15 -0700 | [diff] [blame] | 138 | * |
| 139 | * XXX autodeps are deprecated and should be removed at the earliest |
| 140 | * opportunity |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 141 | */ |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 142 | static void _autodep_lookup(struct clkdm_autodep *autodep) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 143 | { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 144 | struct clockdomain *clkdm; |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 145 | |
| 146 | if (!autodep) |
| 147 | return; |
| 148 | |
| 149 | if (!omap_chip_is(autodep->omap_chip)) |
| 150 | return; |
| 151 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 152 | 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 Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 157 | } |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 158 | autodep->clkdm.ptr = clkdm; |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 159 | } |
| 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 Walmsley | b170fbe | 2010-12-21 21:05:15 -0700 | [diff] [blame] | 168 | * |
| 169 | * XXX autodeps are deprecated and should be removed at the earliest |
| 170 | * opportunity |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 171 | */ |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 172 | void _clkdm_add_autodeps(struct clockdomain *clkdm) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 173 | { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 174 | struct clkdm_autodep *autodep; |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 175 | |
Paul Walmsley | ad95616 | 2010-02-22 22:09:35 -0700 | [diff] [blame] | 176 | if (!autodeps) |
| 177 | return; |
| 178 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 179 | for (autodep = autodeps; autodep->clkdm.ptr; autodep++) { |
| 180 | if (IS_ERR(autodep->clkdm.ptr)) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 181 | continue; |
| 182 | |
Paul Walmsley | d96df00 | 2009-01-27 19:44:35 -0700 | [diff] [blame] | 183 | if (!omap_chip_is(autodep->omap_chip)) |
| 184 | continue; |
| 185 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 186 | pr_debug("clockdomain: adding %s sleepdep/wkdep for " |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 187 | "clkdm %s\n", autodep->clkdm.ptr->name, |
| 188 | clkdm->name); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 189 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 190 | clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr); |
| 191 | clkdm_add_wkdep(clkdm, autodep->clkdm.ptr); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 192 | } |
| 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 Walmsley | b170fbe | 2010-12-21 21:05:15 -0700 | [diff] [blame] | 202 | * |
| 203 | * XXX autodeps are deprecated and should be removed at the earliest |
| 204 | * opportunity |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 205 | */ |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 206 | void _clkdm_del_autodeps(struct clockdomain *clkdm) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 207 | { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 208 | struct clkdm_autodep *autodep; |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 209 | |
Paul Walmsley | ad95616 | 2010-02-22 22:09:35 -0700 | [diff] [blame] | 210 | if (!autodeps) |
| 211 | return; |
| 212 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 213 | for (autodep = autodeps; autodep->clkdm.ptr; autodep++) { |
| 214 | if (IS_ERR(autodep->clkdm.ptr)) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 215 | continue; |
| 216 | |
Paul Walmsley | d96df00 | 2009-01-27 19:44:35 -0700 | [diff] [blame] | 217 | if (!omap_chip_is(autodep->omap_chip)) |
| 218 | continue; |
| 219 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 220 | pr_debug("clockdomain: removing %s sleepdep/wkdep for " |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 221 | "clkdm %s\n", autodep->clkdm.ptr->name, |
| 222 | clkdm->name); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 223 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 224 | clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr); |
| 225 | clkdm_del_wkdep(clkdm, autodep->clkdm.ptr); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Paul Walmsley | b170fbe | 2010-12-21 21:05:15 -0700 | [diff] [blame] | 229 | /** |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 230 | * _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 |
| 233 | * |
| 234 | * Iterates through @clkdm_deps, looking up the struct clockdomain named by |
| 235 | * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep. |
| 236 | * No return value. |
| 237 | */ |
| 238 | static void _resolve_clkdm_deps(struct clockdomain *clkdm, |
| 239 | struct clkdm_dep *clkdm_deps) |
| 240 | { |
| 241 | 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 | } |
| 253 | } |
| 254 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 255 | /* 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 |
Rajendra Nayak | 32d4034 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 261 | * @custom_funcs: func pointers for arch specfic implementations |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 262 | * |
| 263 | * Set up internal state. If a pointer to an array of clockdomains |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 264 | * @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 Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 268 | */ |
| 269 | void clkdm_init(struct clockdomain **clkdms, |
Rajendra Nayak | 32d4034 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 270 | struct clkdm_autodep *init_autodeps, |
| 271 | struct clkdm_ops *custom_funcs) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 272 | { |
| 273 | struct clockdomain **c = NULL; |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 274 | struct clockdomain *clkdm; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 275 | struct clkdm_autodep *autodep = NULL; |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 276 | |
Rajendra Nayak | 32d4034 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 277 | if (!custom_funcs) |
| 278 | WARN(1, "No custom clkdm functions registered\n"); |
| 279 | else |
| 280 | arch_clkdm = custom_funcs; |
| 281 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 282 | if (clkdms) |
| 283 | for (c = clkdms; *c; c++) |
Paul Walmsley | e909d62 | 2010-01-26 20:13:00 -0700 | [diff] [blame] | 284 | _clkdm_register(*c); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 285 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 286 | autodeps = init_autodeps; |
| 287 | if (autodeps) |
| 288 | for (autodep = autodeps; autodep->clkdm.ptr; autodep++) |
| 289 | _autodep_lookup(autodep); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 290 | |
| 291 | /* |
Paul Walmsley | 6f7f63c | 2010-09-14 15:56:53 -0600 | [diff] [blame] | 292 | * Put all clockdomains into software-supervised mode; PM code |
| 293 | * should later enable hardware-supervised mode as appropriate |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 294 | */ |
| 295 | list_for_each_entry(clkdm, &clkdm_list, node) { |
Paul Walmsley | 6f7f63c | 2010-09-14 15:56:53 -0600 | [diff] [blame] | 296 | if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP) |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 297 | clkdm_wakeup(clkdm); |
Paul Walmsley | 6f7f63c | 2010-09-14 15:56:53 -0600 | [diff] [blame] | 298 | else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO) |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 299 | clkdm_deny_idle(clkdm); |
Paul Walmsley | 6f7f63c | 2010-09-14 15:56:53 -0600 | [diff] [blame] | 300 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 301 | _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs); |
Paul Walmsley | 6f7f63c | 2010-09-14 15:56:53 -0600 | [diff] [blame] | 302 | clkdm_clear_all_wkdeps(clkdm); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 303 | |
| 304 | _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs); |
Paul Walmsley | 6f7f63c | 2010-09-14 15:56:53 -0600 | [diff] [blame] | 305 | clkdm_clear_all_sleepdeps(clkdm); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 306 | } |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 310 | * clkdm_lookup - look up a clockdomain by name, return a pointer |
| 311 | * @name: name of clockdomain |
| 312 | * |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 313 | * Find a registered clockdomain by its name @name. Returns a pointer |
| 314 | * to the struct clockdomain if found, or NULL otherwise. |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 315 | */ |
| 316 | struct 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 Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 325 | 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 Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 331 | |
| 332 | return clkdm; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * clkdm_for_each - call function on each registered clockdomain |
| 337 | * @fn: callback function * |
| 338 | * |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 339 | * Call the supplied function @fn for each registered clockdomain. |
| 340 | * The callback function @fn can return anything but 0 to bail |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 341 | * 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 Schrijver | a23456e | 2008-10-15 18:13:47 +0300 | [diff] [blame] | 349 | int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user), |
| 350 | void *user) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 351 | { |
| 352 | struct clockdomain *clkdm; |
| 353 | int ret = 0; |
| 354 | |
| 355 | if (!fn) |
| 356 | return -EINVAL; |
| 357 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 358 | list_for_each_entry(clkdm, &clkdm_list, node) { |
Peter 'p2' De Schrijver | a23456e | 2008-10-15 18:13:47 +0300 | [diff] [blame] | 359 | ret = (*fn)(clkdm, user); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 360 | if (ret) |
| 361 | break; |
| 362 | } |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 363 | |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | |
Paul Walmsley | e89087c | 2008-05-20 18:41:35 -0600 | [diff] [blame] | 368 | /** |
| 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 Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 373 | * @clkdm exists in, or returns NULL if @clkdm is NULL. |
Paul Walmsley | e89087c | 2008-05-20 18:41:35 -0600 | [diff] [blame] | 374 | */ |
| 375 | struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm) |
| 376 | { |
| 377 | if (!clkdm) |
| 378 | return NULL; |
| 379 | |
Paul Walmsley | 5b74c67 | 2009-02-03 02:10:03 -0700 | [diff] [blame] | 380 | return clkdm->pwrdm.ptr; |
Paul Walmsley | e89087c | 2008-05-20 18:41:35 -0600 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 384 | /* Hardware clockdomain control */ |
| 385 | |
| 386 | /** |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 387 | * 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 | */ |
| 398 | int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2) |
| 399 | { |
| 400 | struct clkdm_dep *cd; |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 401 | int ret = 0; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 402 | |
Paul Walmsley | 56bc78d | 2011-01-17 13:28:17 -0700 | [diff] [blame] | 403 | if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) { |
| 404 | pr_err("clockdomain: %s/%s: %s: not yet implemented\n", |
| 405 | clkdm1->name, clkdm2->name, __func__); |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 409 | if (!clkdm1 || !clkdm2) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 413 | if (IS_ERR(cd)) |
| 414 | ret = PTR_ERR(cd); |
| 415 | |
| 416 | if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep) |
| 417 | ret = -EINVAL; |
| 418 | |
| 419 | if (ret) { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 420 | pr_debug("clockdomain: hardware cannot set/clear wake up of " |
| 421 | "%s when %s wakes up\n", clkdm1->name, clkdm2->name); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 422 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 425 | if (atomic_inc_return(&cd->wkdep_usecount) == 1) { |
| 426 | pr_debug("clockdomain: hardware will wake up %s when %s wakes " |
| 427 | "up\n", clkdm1->name, clkdm2->name); |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 428 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 429 | ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 430 | } |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 431 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 432 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
| 436 | * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1 |
| 437 | * @clkdm1: wake this struct clockdomain * up (dependent) |
| 438 | * @clkdm2: when this struct clockdomain * wakes up (source) |
| 439 | * |
| 440 | * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2 |
| 441 | * wakes up. Returns -EINVAL if presented with invalid clockdomain |
| 442 | * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or |
| 443 | * 0 upon success. |
| 444 | */ |
| 445 | int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2) |
| 446 | { |
| 447 | struct clkdm_dep *cd; |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 448 | int ret = 0; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 449 | |
Paul Walmsley | 56bc78d | 2011-01-17 13:28:17 -0700 | [diff] [blame] | 450 | if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) { |
| 451 | pr_err("clockdomain: %s/%s: %s: not yet implemented\n", |
| 452 | clkdm1->name, clkdm2->name, __func__); |
| 453 | return -EINVAL; |
| 454 | } |
| 455 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 456 | if (!clkdm1 || !clkdm2) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 460 | if (IS_ERR(cd)) |
| 461 | ret = PTR_ERR(cd); |
| 462 | |
| 463 | if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep) |
| 464 | ret = -EINVAL; |
| 465 | |
| 466 | if (ret) { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 467 | pr_debug("clockdomain: hardware cannot set/clear wake up of " |
| 468 | "%s when %s wakes up\n", clkdm1->name, clkdm2->name); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 469 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 472 | if (atomic_dec_return(&cd->wkdep_usecount) == 0) { |
| 473 | pr_debug("clockdomain: hardware will no longer wake up %s " |
| 474 | "after %s wakes up\n", clkdm1->name, clkdm2->name); |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 475 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 476 | ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 477 | } |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 478 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 479 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
| 483 | * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1 |
| 484 | * @clkdm1: wake this struct clockdomain * up (dependent) |
| 485 | * @clkdm2: when this struct clockdomain * wakes up (source) |
| 486 | * |
| 487 | * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be |
| 488 | * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL |
| 489 | * if either clockdomain pointer is invalid; or -ENOENT if the hardware |
| 490 | * is incapable. |
| 491 | * |
| 492 | * REVISIT: Currently this function only represents software-controllable |
| 493 | * wakeup dependencies. Wakeup dependencies fixed in hardware are not |
| 494 | * yet handled here. |
| 495 | */ |
| 496 | int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2) |
| 497 | { |
| 498 | struct clkdm_dep *cd; |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 499 | int ret = 0; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 500 | |
| 501 | if (!clkdm1 || !clkdm2) |
| 502 | return -EINVAL; |
| 503 | |
Paul Walmsley | 56bc78d | 2011-01-17 13:28:17 -0700 | [diff] [blame] | 504 | if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) { |
| 505 | pr_err("clockdomain: %s/%s: %s: not yet implemented\n", |
| 506 | clkdm1->name, clkdm2->name, __func__); |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 510 | cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 511 | if (IS_ERR(cd)) |
| 512 | ret = PTR_ERR(cd); |
| 513 | |
| 514 | if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep) |
| 515 | ret = -EINVAL; |
| 516 | |
| 517 | if (ret) { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 518 | pr_debug("clockdomain: hardware cannot set/clear wake up of " |
| 519 | "%s when %s wakes up\n", clkdm1->name, clkdm2->name); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 520 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 523 | /* XXX It's faster to return the atomic wkdep_usecount */ |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 524 | return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2); |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 528 | * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm |
| 529 | * @clkdm: struct clockdomain * to remove all wakeup dependencies from |
| 530 | * |
| 531 | * Remove all inter-clockdomain wakeup dependencies that could cause |
| 532 | * @clkdm to wake. Intended to be used during boot to initialize the |
| 533 | * PRCM to a known state, after all clockdomains are put into swsup idle |
| 534 | * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or |
| 535 | * 0 upon success. |
| 536 | */ |
| 537 | int clkdm_clear_all_wkdeps(struct clockdomain *clkdm) |
| 538 | { |
Paul Walmsley | 56bc78d | 2011-01-17 13:28:17 -0700 | [diff] [blame] | 539 | if (!cpu_is_omap24xx() && !cpu_is_omap34xx()) { |
| 540 | pr_err("clockdomain: %s: %s: not yet implemented\n", |
| 541 | clkdm->name, __func__); |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 545 | if (!clkdm) |
| 546 | return -EINVAL; |
| 547 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 548 | if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps) |
| 549 | return -EINVAL; |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 550 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 551 | return arch_clkdm->clkdm_clear_all_wkdeps(clkdm); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 555 | * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1 |
| 556 | * @clkdm1: prevent this struct clockdomain * from sleeping (dependent) |
| 557 | * @clkdm2: when this struct clockdomain * is active (source) |
| 558 | * |
| 559 | * Prevent @clkdm1 from automatically going inactive (and then to |
| 560 | * retention or off) if @clkdm2 is active. Returns -EINVAL if |
| 561 | * presented with invalid clockdomain pointers or called on a machine |
| 562 | * that does not support software-configurable hardware sleep |
| 563 | * dependencies, -ENOENT if the specified dependency cannot be set in |
| 564 | * hardware, or 0 upon success. |
| 565 | */ |
| 566 | int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2) |
| 567 | { |
| 568 | struct clkdm_dep *cd; |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 569 | int ret = 0; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 570 | |
| 571 | if (!clkdm1 || !clkdm2) |
| 572 | return -EINVAL; |
| 573 | |
| 574 | cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 575 | if (IS_ERR(cd)) |
| 576 | ret = PTR_ERR(cd); |
| 577 | |
| 578 | if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep) |
| 579 | ret = -EINVAL; |
| 580 | |
| 581 | if (ret) { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 582 | pr_debug("clockdomain: hardware cannot set/clear sleep " |
| 583 | "dependency affecting %s from %s\n", clkdm1->name, |
| 584 | clkdm2->name); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 585 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 588 | if (atomic_inc_return(&cd->sleepdep_usecount) == 1) { |
| 589 | pr_debug("clockdomain: will prevent %s from sleeping if %s " |
| 590 | "is active\n", clkdm1->name, clkdm2->name); |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 591 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 592 | ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 593 | } |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 594 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 595 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 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 | */ |
| 610 | int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2) |
| 611 | { |
| 612 | struct clkdm_dep *cd; |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 613 | int ret = 0; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 614 | |
| 615 | if (!clkdm1 || !clkdm2) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 619 | if (IS_ERR(cd)) |
| 620 | ret = PTR_ERR(cd); |
| 621 | |
| 622 | if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep) |
| 623 | ret = -EINVAL; |
| 624 | |
| 625 | if (ret) { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 626 | pr_debug("clockdomain: hardware cannot set/clear sleep " |
| 627 | "dependency affecting %s from %s\n", clkdm1->name, |
| 628 | clkdm2->name); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 629 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 632 | if (atomic_dec_return(&cd->sleepdep_usecount) == 0) { |
| 633 | pr_debug("clockdomain: will no longer prevent %s from " |
| 634 | "sleeping if %s is active\n", clkdm1->name, |
| 635 | clkdm2->name); |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 636 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 637 | ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 638 | } |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 639 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 640 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
| 644 | * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1 |
| 645 | * @clkdm1: prevent this struct clockdomain * from sleeping (dependent) |
| 646 | * @clkdm2: when this struct clockdomain * is active (source) |
| 647 | * |
| 648 | * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will |
| 649 | * not be allowed to automatically go inactive if @clkdm2 is active; |
| 650 | * 0 if @clkdm1's automatic power state inactivity transition is independent |
| 651 | * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called |
| 652 | * on a machine that does not support software-configurable hardware sleep |
| 653 | * dependencies; or -ENOENT if the hardware is incapable. |
| 654 | * |
| 655 | * REVISIT: Currently this function only represents software-controllable |
| 656 | * sleep dependencies. Sleep dependencies fixed in hardware are not |
| 657 | * yet handled here. |
| 658 | */ |
| 659 | int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2) |
| 660 | { |
| 661 | struct clkdm_dep *cd; |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 662 | int ret = 0; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 663 | |
| 664 | if (!clkdm1 || !clkdm2) |
| 665 | return -EINVAL; |
| 666 | |
| 667 | cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 668 | if (IS_ERR(cd)) |
| 669 | ret = PTR_ERR(cd); |
| 670 | |
| 671 | if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep) |
| 672 | ret = -EINVAL; |
| 673 | |
| 674 | if (ret) { |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 675 | pr_debug("clockdomain: hardware cannot set/clear sleep " |
| 676 | "dependency affecting %s from %s\n", clkdm1->name, |
| 677 | clkdm2->name); |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 678 | return ret; |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 681 | /* XXX It's faster to return the atomic sleepdep_usecount */ |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 682 | return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2); |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 685 | /** |
| 686 | * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm |
| 687 | * @clkdm: struct clockdomain * to remove all sleep dependencies from |
| 688 | * |
| 689 | * Remove all inter-clockdomain sleep dependencies that could prevent |
| 690 | * @clkdm from idling. Intended to be used during boot to initialize the |
| 691 | * PRCM to a known state, after all clockdomains are put into swsup idle |
| 692 | * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or |
| 693 | * 0 upon success. |
| 694 | */ |
| 695 | int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm) |
| 696 | { |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 697 | if (!clkdm) |
| 698 | return -EINVAL; |
| 699 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 700 | if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps) |
| 701 | return -EINVAL; |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 702 | |
Rajendra Nayak | 4aef7a2 | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 703 | return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm); |
Paul Walmsley | 369d561 | 2010-01-26 20:13:01 -0700 | [diff] [blame] | 704 | } |
Paul Walmsley | 55ed969 | 2010-01-26 20:12:59 -0700 | [diff] [blame] | 705 | |
| 706 | /** |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 707 | * clkdm_sleep - force clockdomain sleep transition |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 708 | * @clkdm: struct clockdomain * |
| 709 | * |
| 710 | * Instruct the CM to force a sleep transition on the specified |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 711 | * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 712 | * clockdomain does not support software-initiated sleep; 0 upon |
| 713 | * success. |
| 714 | */ |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 715 | int clkdm_sleep(struct clockdomain *clkdm) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 716 | { |
| 717 | if (!clkdm) |
| 718 | return -EINVAL; |
| 719 | |
| 720 | if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) { |
| 721 | pr_debug("clockdomain: %s does not support forcing " |
| 722 | "sleep via software\n", clkdm->name); |
| 723 | return -EINVAL; |
| 724 | } |
| 725 | |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 726 | if (!arch_clkdm || !arch_clkdm->clkdm_sleep) |
| 727 | return -EINVAL; |
| 728 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 729 | pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name); |
| 730 | |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 731 | return arch_clkdm->clkdm_sleep(clkdm); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /** |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 735 | * clkdm_wakeup - force clockdomain wakeup transition |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 736 | * @clkdm: struct clockdomain * |
| 737 | * |
| 738 | * Instruct the CM to force a wakeup transition on the specified |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 739 | * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 740 | * clockdomain does not support software-controlled wakeup; 0 upon |
| 741 | * success. |
| 742 | */ |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 743 | int clkdm_wakeup(struct clockdomain *clkdm) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 744 | { |
| 745 | if (!clkdm) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) { |
| 749 | pr_debug("clockdomain: %s does not support forcing " |
| 750 | "wakeup via software\n", clkdm->name); |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 754 | if (!arch_clkdm || !arch_clkdm->clkdm_wakeup) |
| 755 | return -EINVAL; |
| 756 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 757 | pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name); |
| 758 | |
Rajendra Nayak | 68b921a | 2011-02-25 16:06:47 -0700 | [diff] [blame] | 759 | return arch_clkdm->clkdm_wakeup(clkdm); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /** |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 763 | * clkdm_allow_idle - enable hwsup idle transitions for clkdm |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 764 | * @clkdm: struct clockdomain * |
| 765 | * |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 766 | * Allow the hardware to automatically switch the clockdomain @clkdm into |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 767 | * active or idle states, as needed by downstream clocks. If the |
| 768 | * clockdomain has any downstream clocks enabled in the clock |
| 769 | * framework, wkdep/sleepdep autodependencies are added; this is so |
| 770 | * device drivers can read and write to the device. No return value. |
| 771 | */ |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 772 | void clkdm_allow_idle(struct clockdomain *clkdm) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 773 | { |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 774 | if (!clkdm) |
| 775 | return; |
| 776 | |
| 777 | if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) { |
| 778 | pr_debug("clock: automatic idle transitions cannot be enabled " |
| 779 | "on clockdomain %s\n", clkdm->name); |
| 780 | return; |
| 781 | } |
| 782 | |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 783 | if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle) |
| 784 | return; |
| 785 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 786 | pr_debug("clockdomain: enabling automatic idle transitions for %s\n", |
| 787 | clkdm->name); |
| 788 | |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 789 | arch_clkdm->clkdm_allow_idle(clkdm); |
Peter 'p2' De Schrijver | ba20bb1 | 2008-10-15 17:48:43 +0300 | [diff] [blame] | 790 | pwrdm_clkdm_state_switch(clkdm); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 794 | * clkdm_deny_idle - disable hwsup idle transitions for clkdm |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 795 | * @clkdm: struct clockdomain * |
| 796 | * |
| 797 | * Prevent the hardware from automatically switching the clockdomain |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 798 | * @clkdm into inactive or idle states. If the clockdomain has |
| 799 | * downstream clocks enabled in the clock framework, wkdep/sleepdep |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 800 | * autodependencies are removed. No return value. |
| 801 | */ |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 802 | void clkdm_deny_idle(struct clockdomain *clkdm) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 803 | { |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 804 | if (!clkdm) |
| 805 | return; |
| 806 | |
| 807 | if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) { |
| 808 | pr_debug("clockdomain: automatic idle transitions cannot be " |
| 809 | "disabled on %s\n", clkdm->name); |
| 810 | return; |
| 811 | } |
| 812 | |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 813 | if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle) |
| 814 | return; |
| 815 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 816 | pr_debug("clockdomain: disabling automatic idle transitions for %s\n", |
| 817 | clkdm->name); |
| 818 | |
Rajendra Nayak | 5cd1937 | 2011-02-25 16:06:48 -0700 | [diff] [blame] | 819 | arch_clkdm->clkdm_deny_idle(clkdm); |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | |
| 823 | /* Clockdomain-to-clock framework interface code */ |
| 824 | |
| 825 | /** |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 826 | * clkdm_clk_enable - add an enabled downstream clock to this clkdm |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 827 | * @clkdm: struct clockdomain * |
| 828 | * @clk: struct clk * of the enabled downstream clock |
| 829 | * |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 830 | * Increment the usecount of the clockdomain @clkdm and ensure that it |
| 831 | * is awake before @clk is enabled. Intended to be called by |
| 832 | * clk_enable() code. If the clockdomain is in software-supervised |
| 833 | * idle mode, force the clockdomain to wake. If the clockdomain is in |
| 834 | * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to |
| 835 | * ensure that devices in the clockdomain can be read from/written to |
| 836 | * by on-chip processors. Returns -EINVAL if passed null pointers; |
| 837 | * returns 0 upon success or if the clockdomain is in hwsup idle mode. |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 838 | */ |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 839 | int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 840 | { |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 841 | /* |
| 842 | * XXX Rewrite this code to maintain a list of enabled |
| 843 | * downstream clocks for debugging purposes? |
| 844 | */ |
| 845 | |
Paul Walmsley | 30962d9 | 2010-02-22 22:09:38 -0700 | [diff] [blame] | 846 | if (!clkdm || !clk) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 847 | return -EINVAL; |
| 848 | |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 849 | if (!arch_clkdm || !arch_clkdm->clkdm_clk_enable) |
| 850 | return -EINVAL; |
| 851 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 852 | if (atomic_inc_return(&clkdm->usecount) > 1) |
| 853 | return 0; |
| 854 | |
| 855 | /* Clockdomain now has one enabled downstream clock */ |
| 856 | |
| 857 | pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name, |
| 858 | clk->name); |
| 859 | |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 860 | arch_clkdm->clkdm_clk_enable(clkdm); |
Tomi Valkeinen | 054ce50 | 2009-01-27 19:44:31 -0700 | [diff] [blame] | 861 | pwrdm_wait_transition(clkdm->pwrdm.ptr); |
Peter 'p2' De Schrijver | fe617af | 2008-10-15 17:48:44 +0300 | [diff] [blame] | 862 | pwrdm_clkdm_state_switch(clkdm); |
Tomi Valkeinen | 054ce50 | 2009-01-27 19:44:31 -0700 | [diff] [blame] | 863 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | /** |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 868 | * clkdm_clk_disable - remove an enabled downstream clock from this clkdm |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 869 | * @clkdm: struct clockdomain * |
| 870 | * @clk: struct clk * of the disabled downstream clock |
| 871 | * |
Paul Walmsley | f0271d6 | 2010-01-26 20:13:02 -0700 | [diff] [blame] | 872 | * Decrement the usecount of this clockdomain @clkdm when @clk is |
| 873 | * disabled. Intended to be called by clk_disable() code. If the |
| 874 | * clockdomain usecount goes to 0, put the clockdomain to sleep |
| 875 | * (software-supervised mode) or remove the clkdm autodependencies |
| 876 | * (hardware-supervised mode). Returns -EINVAL if passed null |
| 877 | * pointers; -ERANGE if the @clkdm usecount underflows and debugging |
| 878 | * is enabled; or returns 0 upon success or if the clockdomain is in |
| 879 | * hwsup idle mode. |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 880 | */ |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 881 | int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 882 | { |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 883 | /* |
| 884 | * XXX Rewrite this code to maintain a list of enabled |
| 885 | * downstream clocks for debugging purposes? |
| 886 | */ |
| 887 | |
Paul Walmsley | 30962d9 | 2010-02-22 22:09:38 -0700 | [diff] [blame] | 888 | if (!clkdm || !clk) |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 889 | return -EINVAL; |
| 890 | |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 891 | if (!arch_clkdm || !arch_clkdm->clkdm_clk_disable) |
| 892 | return -EINVAL; |
| 893 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 894 | #ifdef DEBUG |
| 895 | if (atomic_read(&clkdm->usecount) == 0) { |
| 896 | WARN_ON(1); /* underflow */ |
| 897 | return -ERANGE; |
| 898 | } |
| 899 | #endif |
| 900 | |
| 901 | if (atomic_dec_return(&clkdm->usecount) > 0) |
| 902 | return 0; |
| 903 | |
| 904 | /* All downstream clocks of this clockdomain are now disabled */ |
| 905 | |
| 906 | pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name, |
| 907 | clk->name); |
| 908 | |
Rajendra Nayak | 4da71ae | 2011-02-25 16:06:48 -0700 | [diff] [blame^] | 909 | arch_clkdm->clkdm_clk_disable(clkdm); |
Peter 'p2' De Schrijver | fe617af | 2008-10-15 17:48:44 +0300 | [diff] [blame] | 910 | pwrdm_clkdm_state_switch(clkdm); |
| 911 | |
Paul Walmsley | d459bfe | 2008-08-19 11:08:43 +0300 | [diff] [blame] | 912 | return 0; |
| 913 | } |
| 914 | |