Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * OMAP2/3/4 clockdomain framework functions
0004  *
0005  * Copyright (C) 2008-2011 Texas Instruments, Inc.
0006  * Copyright (C) 2008-2011 Nokia Corporation
0007  *
0008  * Written by Paul Walmsley and Jouni Högander
0009  * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
0010  */
0011 #undef DEBUG
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/device.h>
0015 #include <linux/list.h>
0016 #include <linux/errno.h>
0017 #include <linux/string.h>
0018 #include <linux/delay.h>
0019 #include <linux/clk.h>
0020 #include <linux/limits.h>
0021 #include <linux/err.h>
0022 #include <linux/clk-provider.h>
0023 #include <linux/cpu_pm.h>
0024 
0025 #include <linux/io.h>
0026 
0027 #include <linux/bitops.h>
0028 
0029 #include "soc.h"
0030 #include "clock.h"
0031 #include "clockdomain.h"
0032 #include "pm.h"
0033 
0034 /* clkdm_list contains all registered struct clockdomains */
0035 static LIST_HEAD(clkdm_list);
0036 
0037 /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
0038 static struct clkdm_autodep *autodeps;
0039 
0040 static struct clkdm_ops *arch_clkdm;
0041 void clkdm_save_context(void);
0042 void clkdm_restore_context(void);
0043 
0044 /* Private functions */
0045 
0046 static struct clockdomain *_clkdm_lookup(const char *name)
0047 {
0048     struct clockdomain *clkdm, *temp_clkdm;
0049 
0050     if (!name)
0051         return NULL;
0052 
0053     clkdm = NULL;
0054 
0055     list_for_each_entry(temp_clkdm, &clkdm_list, node) {
0056         if (!strcmp(name, temp_clkdm->name)) {
0057             clkdm = temp_clkdm;
0058             break;
0059         }
0060     }
0061 
0062     return clkdm;
0063 }
0064 
0065 /**
0066  * _clkdm_register - register a clockdomain
0067  * @clkdm: struct clockdomain * to register
0068  *
0069  * Adds a clockdomain to the internal clockdomain list.
0070  * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
0071  * already registered by the provided name, or 0 upon success.
0072  */
0073 static int _clkdm_register(struct clockdomain *clkdm)
0074 {
0075     struct powerdomain *pwrdm;
0076 
0077     if (!clkdm || !clkdm->name)
0078         return -EINVAL;
0079 
0080     pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
0081     if (!pwrdm) {
0082         pr_err("clockdomain: %s: powerdomain %s does not exist\n",
0083             clkdm->name, clkdm->pwrdm.name);
0084         return -EINVAL;
0085     }
0086     clkdm->pwrdm.ptr = pwrdm;
0087 
0088     /* Verify that the clockdomain is not already registered */
0089     if (_clkdm_lookup(clkdm->name))
0090         return -EEXIST;
0091 
0092     list_add(&clkdm->node, &clkdm_list);
0093 
0094     pwrdm_add_clkdm(pwrdm, clkdm);
0095 
0096     pr_debug("clockdomain: registered %s\n", clkdm->name);
0097 
0098     return 0;
0099 }
0100 
0101 /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
0102 static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
0103                         struct clkdm_dep *deps)
0104 {
0105     struct clkdm_dep *cd;
0106 
0107     if (!clkdm || !deps)
0108         return ERR_PTR(-EINVAL);
0109 
0110     for (cd = deps; cd->clkdm_name; cd++) {
0111         if (!cd->clkdm && cd->clkdm_name)
0112             cd->clkdm = _clkdm_lookup(cd->clkdm_name);
0113 
0114         if (cd->clkdm == clkdm)
0115             break;
0116     }
0117 
0118     if (!cd->clkdm_name)
0119         return ERR_PTR(-ENOENT);
0120 
0121     return cd;
0122 }
0123 
0124 /**
0125  * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
0126  * @autodep: struct clkdm_autodep * to resolve
0127  *
0128  * Resolve autodep clockdomain names to clockdomain pointers via
0129  * clkdm_lookup() and store the pointers in the autodep structure.  An
0130  * "autodep" is a clockdomain sleep/wakeup dependency that is
0131  * automatically added and removed whenever clocks in the associated
0132  * clockdomain are enabled or disabled (respectively) when the
0133  * clockdomain is in hardware-supervised mode.  Meant to be called
0134  * once at clockdomain layer initialization, since these should remain
0135  * fixed for a particular architecture.  No return value.
0136  *
0137  * XXX autodeps are deprecated and should be removed at the earliest
0138  * opportunity
0139  */
0140 static void _autodep_lookup(struct clkdm_autodep *autodep)
0141 {
0142     struct clockdomain *clkdm;
0143 
0144     if (!autodep)
0145         return;
0146 
0147     clkdm = clkdm_lookup(autodep->clkdm.name);
0148     if (!clkdm) {
0149         pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
0150              autodep->clkdm.name);
0151         clkdm = ERR_PTR(-ENOENT);
0152     }
0153     autodep->clkdm.ptr = clkdm;
0154 }
0155 
0156 /**
0157  * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
0158  * @clkdm: clockdomain that we are resolving dependencies for
0159  * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
0160  *
0161  * Iterates through @clkdm_deps, looking up the struct clockdomain named by
0162  * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
0163  * No return value.
0164  */
0165 static void _resolve_clkdm_deps(struct clockdomain *clkdm,
0166                 struct clkdm_dep *clkdm_deps)
0167 {
0168     struct clkdm_dep *cd;
0169 
0170     for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
0171         if (cd->clkdm)
0172             continue;
0173         cd->clkdm = _clkdm_lookup(cd->clkdm_name);
0174 
0175         WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
0176              clkdm->name, cd->clkdm_name);
0177     }
0178 }
0179 
0180 /**
0181  * _clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1 (lockless)
0182  * @clkdm1: wake this struct clockdomain * up (dependent)
0183  * @clkdm2: when this struct clockdomain * wakes up (source)
0184  *
0185  * When the clockdomain represented by @clkdm2 wakes up, wake up
0186  * @clkdm1. Implemented in hardware on the OMAP, this feature is
0187  * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
0188  * Returns -EINVAL if presented with invalid clockdomain pointers,
0189  * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
0190  * success.
0191  */
0192 static int _clkdm_add_wkdep(struct clockdomain *clkdm1,
0193                 struct clockdomain *clkdm2)
0194 {
0195     struct clkdm_dep *cd;
0196     int ret = 0;
0197 
0198     if (!clkdm1 || !clkdm2)
0199         return -EINVAL;
0200 
0201     cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
0202     if (IS_ERR(cd))
0203         ret = PTR_ERR(cd);
0204 
0205     if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
0206         ret = -EINVAL;
0207 
0208     if (ret) {
0209         pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
0210              clkdm1->name, clkdm2->name);
0211         return ret;
0212     }
0213 
0214     cd->wkdep_usecount++;
0215     if (cd->wkdep_usecount == 1) {
0216         pr_debug("clockdomain: hardware will wake up %s when %s wakes up\n",
0217              clkdm1->name, clkdm2->name);
0218 
0219         ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
0220     }
0221 
0222     return ret;
0223 }
0224 
0225 /**
0226  * _clkdm_del_wkdep - remove a wakeup dep from clkdm2 to clkdm1 (lockless)
0227  * @clkdm1: wake this struct clockdomain * up (dependent)
0228  * @clkdm2: when this struct clockdomain * wakes up (source)
0229  *
0230  * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
0231  * wakes up.  Returns -EINVAL if presented with invalid clockdomain
0232  * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
0233  * 0 upon success.
0234  */
0235 static int _clkdm_del_wkdep(struct clockdomain *clkdm1,
0236                 struct clockdomain *clkdm2)
0237 {
0238     struct clkdm_dep *cd;
0239     int ret = 0;
0240 
0241     if (!clkdm1 || !clkdm2)
0242         return -EINVAL;
0243 
0244     cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
0245     if (IS_ERR(cd))
0246         ret = PTR_ERR(cd);
0247 
0248     if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
0249         ret = -EINVAL;
0250 
0251     if (ret) {
0252         pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
0253              clkdm1->name, clkdm2->name);
0254         return ret;
0255     }
0256 
0257     cd->wkdep_usecount--;
0258     if (cd->wkdep_usecount == 0) {
0259         pr_debug("clockdomain: hardware will no longer wake up %s after %s wakes up\n",
0260              clkdm1->name, clkdm2->name);
0261 
0262         ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
0263     }
0264 
0265     return ret;
0266 }
0267 
0268 /**
0269  * _clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1 (lockless)
0270  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
0271  * @clkdm2: when this struct clockdomain * is active (source)
0272  *
0273  * Prevent @clkdm1 from automatically going inactive (and then to
0274  * retention or off) if @clkdm2 is active.  Returns -EINVAL if
0275  * presented with invalid clockdomain pointers or called on a machine
0276  * that does not support software-configurable hardware sleep
0277  * dependencies, -ENOENT if the specified dependency cannot be set in
0278  * hardware, or 0 upon success.
0279  */
0280 static int _clkdm_add_sleepdep(struct clockdomain *clkdm1,
0281                    struct clockdomain *clkdm2)
0282 {
0283     struct clkdm_dep *cd;
0284     int ret = 0;
0285 
0286     if (!clkdm1 || !clkdm2)
0287         return -EINVAL;
0288 
0289     cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
0290     if (IS_ERR(cd))
0291         ret = PTR_ERR(cd);
0292 
0293     if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
0294         ret = -EINVAL;
0295 
0296     if (ret) {
0297         pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
0298              clkdm1->name, clkdm2->name);
0299         return ret;
0300     }
0301 
0302     cd->sleepdep_usecount++;
0303     if (cd->sleepdep_usecount == 1) {
0304         pr_debug("clockdomain: will prevent %s from sleeping if %s is active\n",
0305              clkdm1->name, clkdm2->name);
0306 
0307         ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
0308     }
0309 
0310     return ret;
0311 }
0312 
0313 /**
0314  * _clkdm_del_sleepdep - remove a sleep dep from clkdm2 to clkdm1 (lockless)
0315  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
0316  * @clkdm2: when this struct clockdomain * is active (source)
0317  *
0318  * Allow @clkdm1 to automatically go inactive (and then to retention or
0319  * off), independent of the activity state of @clkdm2.  Returns -EINVAL
0320  * if presented with invalid clockdomain pointers or called on a machine
0321  * that does not support software-configurable hardware sleep dependencies,
0322  * -ENOENT if the specified dependency cannot be cleared in hardware, or
0323  * 0 upon success.
0324  */
0325 static int _clkdm_del_sleepdep(struct clockdomain *clkdm1,
0326                    struct clockdomain *clkdm2)
0327 {
0328     struct clkdm_dep *cd;
0329     int ret = 0;
0330 
0331     if (!clkdm1 || !clkdm2)
0332         return -EINVAL;
0333 
0334     cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
0335     if (IS_ERR(cd))
0336         ret = PTR_ERR(cd);
0337 
0338     if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
0339         ret = -EINVAL;
0340 
0341     if (ret) {
0342         pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
0343              clkdm1->name, clkdm2->name);
0344         return ret;
0345     }
0346 
0347     cd->sleepdep_usecount--;
0348     if (cd->sleepdep_usecount == 0) {
0349         pr_debug("clockdomain: will no longer prevent %s from sleeping if %s is active\n",
0350              clkdm1->name, clkdm2->name);
0351 
0352         ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
0353     }
0354 
0355     return ret;
0356 }
0357 
0358 /* Public functions */
0359 
0360 /**
0361  * clkdm_register_platform_funcs - register clockdomain implementation fns
0362  * @co: func pointers for arch specific implementations
0363  *
0364  * Register the list of function pointers used to implement the
0365  * clockdomain functions on different OMAP SoCs.  Should be called
0366  * before any other clkdm_register*() function.  Returns -EINVAL if
0367  * @co is null, -EEXIST if platform functions have already been
0368  * registered, or 0 upon success.
0369  */
0370 int clkdm_register_platform_funcs(struct clkdm_ops *co)
0371 {
0372     if (!co)
0373         return -EINVAL;
0374 
0375     if (arch_clkdm)
0376         return -EEXIST;
0377 
0378     arch_clkdm = co;
0379 
0380     return 0;
0381 };
0382 
0383 /**
0384  * clkdm_register_clkdms - register SoC clockdomains
0385  * @cs: pointer to an array of struct clockdomain to register
0386  *
0387  * Register the clockdomains available on a particular OMAP SoC.  Must
0388  * be called after clkdm_register_platform_funcs().  May be called
0389  * multiple times.  Returns -EACCES if called before
0390  * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
0391  * null; or 0 upon success.
0392  */
0393 int clkdm_register_clkdms(struct clockdomain **cs)
0394 {
0395     struct clockdomain **c = NULL;
0396 
0397     if (!arch_clkdm)
0398         return -EACCES;
0399 
0400     if (!cs)
0401         return -EINVAL;
0402 
0403     for (c = cs; *c; c++)
0404         _clkdm_register(*c);
0405 
0406     return 0;
0407 }
0408 
0409 /**
0410  * clkdm_register_autodeps - register autodeps (if required)
0411  * @ia: pointer to a static array of struct clkdm_autodep to register
0412  *
0413  * Register clockdomain "automatic dependencies."  These are
0414  * clockdomain wakeup and sleep dependencies that are automatically
0415  * added whenever the first clock inside a clockdomain is enabled, and
0416  * removed whenever the last clock inside a clockdomain is disabled.
0417  * These are currently only used on OMAP3 devices, and are deprecated,
0418  * since they waste energy.  However, until the OMAP2/3 IP block
0419  * enable/disable sequence can be converted to match the OMAP4
0420  * sequence, they are needed.
0421  *
0422  * Must be called only after all of the SoC clockdomains are
0423  * registered, since the function will resolve autodep clockdomain
0424  * names into clockdomain pointers.
0425  *
0426  * The struct clkdm_autodep @ia array must be static, as this function
0427  * does not copy the array elements.
0428  *
0429  * Returns -EACCES if called before any clockdomains have been
0430  * registered, -EINVAL if called with a null @ia argument, -EEXIST if
0431  * autodeps have already been registered, or 0 upon success.
0432  */
0433 int clkdm_register_autodeps(struct clkdm_autodep *ia)
0434 {
0435     struct clkdm_autodep *a = NULL;
0436 
0437     if (list_empty(&clkdm_list))
0438         return -EACCES;
0439 
0440     if (!ia)
0441         return -EINVAL;
0442 
0443     if (autodeps)
0444         return -EEXIST;
0445 
0446     autodeps = ia;
0447     for (a = autodeps; a->clkdm.ptr; a++)
0448         _autodep_lookup(a);
0449 
0450     return 0;
0451 }
0452 
0453 static int cpu_notifier(struct notifier_block *nb, unsigned long cmd, void *v)
0454 {
0455     switch (cmd) {
0456     case CPU_CLUSTER_PM_ENTER:
0457         if (enable_off_mode)
0458             clkdm_save_context();
0459         break;
0460     case CPU_CLUSTER_PM_EXIT:
0461         if (enable_off_mode)
0462             clkdm_restore_context();
0463         break;
0464     }
0465 
0466     return NOTIFY_OK;
0467 }
0468 
0469 /**
0470  * clkdm_complete_init - set up the clockdomain layer
0471  *
0472  * Put all clockdomains into software-supervised mode; PM code should
0473  * later enable hardware-supervised mode as appropriate.  Must be
0474  * called after clkdm_register_clkdms().  Returns -EACCES if called
0475  * before clkdm_register_clkdms(), or 0 upon success.
0476  */
0477 int clkdm_complete_init(void)
0478 {
0479     struct clockdomain *clkdm;
0480     static struct notifier_block nb;
0481 
0482     if (list_empty(&clkdm_list))
0483         return -EACCES;
0484 
0485     list_for_each_entry(clkdm, &clkdm_list, node) {
0486         clkdm_deny_idle(clkdm);
0487 
0488         _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
0489         clkdm_clear_all_wkdeps(clkdm);
0490 
0491         _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
0492         clkdm_clear_all_sleepdeps(clkdm);
0493     }
0494 
0495     /* Only AM43XX can lose clkdm context during rtc-ddr suspend */
0496     if (soc_is_am43xx()) {
0497         nb.notifier_call = cpu_notifier;
0498         cpu_pm_register_notifier(&nb);
0499     }
0500 
0501     return 0;
0502 }
0503 
0504 /**
0505  * clkdm_lookup - look up a clockdomain by name, return a pointer
0506  * @name: name of clockdomain
0507  *
0508  * Find a registered clockdomain by its name @name.  Returns a pointer
0509  * to the struct clockdomain if found, or NULL otherwise.
0510  */
0511 struct clockdomain *clkdm_lookup(const char *name)
0512 {
0513     struct clockdomain *clkdm, *temp_clkdm;
0514 
0515     if (!name)
0516         return NULL;
0517 
0518     clkdm = NULL;
0519 
0520     list_for_each_entry(temp_clkdm, &clkdm_list, node) {
0521         if (!strcmp(name, temp_clkdm->name)) {
0522             clkdm = temp_clkdm;
0523             break;
0524         }
0525     }
0526 
0527     return clkdm;
0528 }
0529 
0530 /**
0531  * clkdm_for_each - call function on each registered clockdomain
0532  * @fn: callback function *
0533  *
0534  * Call the supplied function @fn for each registered clockdomain.
0535  * The callback function @fn can return anything but 0 to bail
0536  * out early from the iterator.  The callback function is called with
0537  * the clkdm_mutex held, so no clockdomain structure manipulation
0538  * functions should be called from the callback, although hardware
0539  * clockdomain control functions are fine.  Returns the last return
0540  * value of the callback function, which should be 0 for success or
0541  * anything else to indicate failure; or -EINVAL if the function pointer
0542  * is null.
0543  */
0544 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
0545             void *user)
0546 {
0547     struct clockdomain *clkdm;
0548     int ret = 0;
0549 
0550     if (!fn)
0551         return -EINVAL;
0552 
0553     list_for_each_entry(clkdm, &clkdm_list, node) {
0554         ret = (*fn)(clkdm, user);
0555         if (ret)
0556             break;
0557     }
0558 
0559     return ret;
0560 }
0561 
0562 
0563 /**
0564  * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
0565  * @clkdm: struct clockdomain *
0566  *
0567  * Return a pointer to the struct powerdomain that the specified clockdomain
0568  * @clkdm exists in, or returns NULL if @clkdm is NULL.
0569  */
0570 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
0571 {
0572     if (!clkdm)
0573         return NULL;
0574 
0575     return clkdm->pwrdm.ptr;
0576 }
0577 
0578 
0579 /* Hardware clockdomain control */
0580 
0581 /**
0582  * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
0583  * @clkdm1: wake this struct clockdomain * up (dependent)
0584  * @clkdm2: when this struct clockdomain * wakes up (source)
0585  *
0586  * When the clockdomain represented by @clkdm2 wakes up, wake up
0587  * @clkdm1. Implemented in hardware on the OMAP, this feature is
0588  * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
0589  * Returns -EINVAL if presented with invalid clockdomain pointers,
0590  * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
0591  * success.
0592  */
0593 int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
0594 {
0595     struct clkdm_dep *cd;
0596     int ret;
0597 
0598     if (!clkdm1 || !clkdm2)
0599         return -EINVAL;
0600 
0601     cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
0602     if (IS_ERR(cd))
0603         return PTR_ERR(cd);
0604 
0605     pwrdm_lock(cd->clkdm->pwrdm.ptr);
0606     ret = _clkdm_add_wkdep(clkdm1, clkdm2);
0607     pwrdm_unlock(cd->clkdm->pwrdm.ptr);
0608 
0609     return ret;
0610 }
0611 
0612 /**
0613  * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
0614  * @clkdm1: wake this struct clockdomain * up (dependent)
0615  * @clkdm2: when this struct clockdomain * wakes up (source)
0616  *
0617  * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
0618  * wakes up.  Returns -EINVAL if presented with invalid clockdomain
0619  * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
0620  * 0 upon success.
0621  */
0622 int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
0623 {
0624     struct clkdm_dep *cd;
0625     int ret;
0626 
0627     if (!clkdm1 || !clkdm2)
0628         return -EINVAL;
0629 
0630     cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
0631     if (IS_ERR(cd))
0632         return PTR_ERR(cd);
0633 
0634     pwrdm_lock(cd->clkdm->pwrdm.ptr);
0635     ret = _clkdm_del_wkdep(clkdm1, clkdm2);
0636     pwrdm_unlock(cd->clkdm->pwrdm.ptr);
0637 
0638     return ret;
0639 }
0640 
0641 /**
0642  * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
0643  * @clkdm1: wake this struct clockdomain * up (dependent)
0644  * @clkdm2: when this struct clockdomain * wakes up (source)
0645  *
0646  * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
0647  * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
0648  * if either clockdomain pointer is invalid; or -ENOENT if the hardware
0649  * is incapable.
0650  *
0651  * REVISIT: Currently this function only represents software-controllable
0652  * wakeup dependencies.  Wakeup dependencies fixed in hardware are not
0653  * yet handled here.
0654  */
0655 int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
0656 {
0657     struct clkdm_dep *cd;
0658     int ret = 0;
0659 
0660     if (!clkdm1 || !clkdm2)
0661         return -EINVAL;
0662 
0663     cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
0664     if (IS_ERR(cd))
0665         ret = PTR_ERR(cd);
0666 
0667     if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
0668         ret = -EINVAL;
0669 
0670     if (ret) {
0671         pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
0672              clkdm1->name, clkdm2->name);
0673         return ret;
0674     }
0675 
0676     /* XXX It's faster to return the wkdep_usecount */
0677     return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
0678 }
0679 
0680 /**
0681  * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
0682  * @clkdm: struct clockdomain * to remove all wakeup dependencies from
0683  *
0684  * Remove all inter-clockdomain wakeup dependencies that could cause
0685  * @clkdm to wake.  Intended to be used during boot to initialize the
0686  * PRCM to a known state, after all clockdomains are put into swsup idle
0687  * and woken up.  Returns -EINVAL if @clkdm pointer is invalid, or
0688  * 0 upon success.
0689  */
0690 int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
0691 {
0692     if (!clkdm)
0693         return -EINVAL;
0694 
0695     if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
0696         return -EINVAL;
0697 
0698     return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
0699 }
0700 
0701 /**
0702  * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
0703  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
0704  * @clkdm2: when this struct clockdomain * is active (source)
0705  *
0706  * Prevent @clkdm1 from automatically going inactive (and then to
0707  * retention or off) if @clkdm2 is active.  Returns -EINVAL if
0708  * presented with invalid clockdomain pointers or called on a machine
0709  * that does not support software-configurable hardware sleep
0710  * dependencies, -ENOENT if the specified dependency cannot be set in
0711  * hardware, or 0 upon success.
0712  */
0713 int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
0714 {
0715     struct clkdm_dep *cd;
0716     int ret;
0717 
0718     if (!clkdm1 || !clkdm2)
0719         return -EINVAL;
0720 
0721     cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
0722     if (IS_ERR(cd))
0723         return PTR_ERR(cd);
0724 
0725     pwrdm_lock(cd->clkdm->pwrdm.ptr);
0726     ret = _clkdm_add_sleepdep(clkdm1, clkdm2);
0727     pwrdm_unlock(cd->clkdm->pwrdm.ptr);
0728 
0729     return ret;
0730 }
0731 
0732 /**
0733  * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
0734  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
0735  * @clkdm2: when this struct clockdomain * is active (source)
0736  *
0737  * Allow @clkdm1 to automatically go inactive (and then to retention or
0738  * off), independent of the activity state of @clkdm2.  Returns -EINVAL
0739  * if presented with invalid clockdomain pointers or called on a machine
0740  * that does not support software-configurable hardware sleep dependencies,
0741  * -ENOENT if the specified dependency cannot be cleared in hardware, or
0742  * 0 upon success.
0743  */
0744 int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
0745 {
0746     struct clkdm_dep *cd;
0747     int ret;
0748 
0749     if (!clkdm1 || !clkdm2)
0750         return -EINVAL;
0751 
0752     cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
0753     if (IS_ERR(cd))
0754         return PTR_ERR(cd);
0755 
0756     pwrdm_lock(cd->clkdm->pwrdm.ptr);
0757     ret = _clkdm_del_sleepdep(clkdm1, clkdm2);
0758     pwrdm_unlock(cd->clkdm->pwrdm.ptr);
0759 
0760     return ret;
0761 }
0762 
0763 /**
0764  * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
0765  * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
0766  * @clkdm2: when this struct clockdomain * is active (source)
0767  *
0768  * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
0769  * not be allowed to automatically go inactive if @clkdm2 is active;
0770  * 0 if @clkdm1's automatic power state inactivity transition is independent
0771  * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
0772  * on a machine that does not support software-configurable hardware sleep
0773  * dependencies; or -ENOENT if the hardware is incapable.
0774  *
0775  * REVISIT: Currently this function only represents software-controllable
0776  * sleep dependencies.  Sleep dependencies fixed in hardware are not
0777  * yet handled here.
0778  */
0779 int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
0780 {
0781     struct clkdm_dep *cd;
0782     int ret = 0;
0783 
0784     if (!clkdm1 || !clkdm2)
0785         return -EINVAL;
0786 
0787     cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
0788     if (IS_ERR(cd))
0789         ret = PTR_ERR(cd);
0790 
0791     if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
0792         ret = -EINVAL;
0793 
0794     if (ret) {
0795         pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
0796              clkdm1->name, clkdm2->name);
0797         return ret;
0798     }
0799 
0800     /* XXX It's faster to return the sleepdep_usecount */
0801     return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
0802 }
0803 
0804 /**
0805  * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
0806  * @clkdm: struct clockdomain * to remove all sleep dependencies from
0807  *
0808  * Remove all inter-clockdomain sleep dependencies that could prevent
0809  * @clkdm from idling.  Intended to be used during boot to initialize the
0810  * PRCM to a known state, after all clockdomains are put into swsup idle
0811  * and woken up.  Returns -EINVAL if @clkdm pointer is invalid, or
0812  * 0 upon success.
0813  */
0814 int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
0815 {
0816     if (!clkdm)
0817         return -EINVAL;
0818 
0819     if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
0820         return -EINVAL;
0821 
0822     return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
0823 }
0824 
0825 /**
0826  * clkdm_sleep_nolock - force clockdomain sleep transition (lockless)
0827  * @clkdm: struct clockdomain *
0828  *
0829  * Instruct the CM to force a sleep transition on the specified
0830  * clockdomain @clkdm.  Only for use by the powerdomain code.  Returns
0831  * -EINVAL if @clkdm is NULL or if clockdomain does not support
0832  * software-initiated sleep; 0 upon success.
0833  */
0834 int clkdm_sleep_nolock(struct clockdomain *clkdm)
0835 {
0836     int ret;
0837 
0838     if (!clkdm)
0839         return -EINVAL;
0840 
0841     if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
0842         pr_debug("clockdomain: %s does not support forcing sleep via software\n",
0843              clkdm->name);
0844         return -EINVAL;
0845     }
0846 
0847     if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
0848         return -EINVAL;
0849 
0850     pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
0851 
0852     clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
0853     ret = arch_clkdm->clkdm_sleep(clkdm);
0854     ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
0855 
0856     return ret;
0857 }
0858 
0859 /**
0860  * clkdm_sleep - force clockdomain sleep transition
0861  * @clkdm: struct clockdomain *
0862  *
0863  * Instruct the CM to force a sleep transition on the specified
0864  * clockdomain @clkdm.  Returns -EINVAL if @clkdm is NULL or if
0865  * clockdomain does not support software-initiated sleep; 0 upon
0866  * success.
0867  */
0868 int clkdm_sleep(struct clockdomain *clkdm)
0869 {
0870     int ret;
0871 
0872     pwrdm_lock(clkdm->pwrdm.ptr);
0873     ret = clkdm_sleep_nolock(clkdm);
0874     pwrdm_unlock(clkdm->pwrdm.ptr);
0875 
0876     return ret;
0877 }
0878 
0879 /**
0880  * clkdm_wakeup_nolock - force clockdomain wakeup transition (lockless)
0881  * @clkdm: struct clockdomain *
0882  *
0883  * Instruct the CM to force a wakeup transition on the specified
0884  * clockdomain @clkdm.  Only for use by the powerdomain code.  Returns
0885  * -EINVAL if @clkdm is NULL or if the clockdomain does not support
0886  * software-controlled wakeup; 0 upon success.
0887  */
0888 int clkdm_wakeup_nolock(struct clockdomain *clkdm)
0889 {
0890     int ret;
0891 
0892     if (!clkdm)
0893         return -EINVAL;
0894 
0895     if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
0896         pr_debug("clockdomain: %s does not support forcing wakeup via software\n",
0897              clkdm->name);
0898         return -EINVAL;
0899     }
0900 
0901     if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
0902         return -EINVAL;
0903 
0904     pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
0905 
0906     clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
0907     ret = arch_clkdm->clkdm_wakeup(clkdm);
0908     ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
0909 
0910     return ret;
0911 }
0912 
0913 /**
0914  * clkdm_wakeup - force clockdomain wakeup transition
0915  * @clkdm: struct clockdomain *
0916  *
0917  * Instruct the CM to force a wakeup transition on the specified
0918  * clockdomain @clkdm.  Returns -EINVAL if @clkdm is NULL or if the
0919  * clockdomain does not support software-controlled wakeup; 0 upon
0920  * success.
0921  */
0922 int clkdm_wakeup(struct clockdomain *clkdm)
0923 {
0924     int ret;
0925 
0926     pwrdm_lock(clkdm->pwrdm.ptr);
0927     ret = clkdm_wakeup_nolock(clkdm);
0928     pwrdm_unlock(clkdm->pwrdm.ptr);
0929 
0930     return ret;
0931 }
0932 
0933 /**
0934  * clkdm_allow_idle_nolock - enable hwsup idle transitions for clkdm
0935  * @clkdm: struct clockdomain *
0936  *
0937  * Allow the hardware to automatically switch the clockdomain @clkdm
0938  * into active or idle states, as needed by downstream clocks.  If the
0939  * clockdomain has any downstream clocks enabled in the clock
0940  * framework, wkdep/sleepdep autodependencies are added; this is so
0941  * device drivers can read and write to the device.  Only for use by
0942  * the powerdomain code.  No return value.
0943  */
0944 void clkdm_allow_idle_nolock(struct clockdomain *clkdm)
0945 {
0946     if (!clkdm)
0947         return;
0948 
0949     if (!WARN_ON(!clkdm->forcewake_count))
0950         clkdm->forcewake_count--;
0951 
0952     if (clkdm->forcewake_count)
0953         return;
0954 
0955     if (!clkdm->usecount && (clkdm->flags & CLKDM_CAN_FORCE_SLEEP))
0956         clkdm_sleep_nolock(clkdm);
0957 
0958     if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO))
0959         return;
0960 
0961     if (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING)
0962         return;
0963 
0964     if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
0965         return;
0966 
0967     pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
0968          clkdm->name);
0969 
0970     clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
0971     arch_clkdm->clkdm_allow_idle(clkdm);
0972     pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
0973 }
0974 
0975 /**
0976  * clkdm_allow_idle - enable hwsup idle transitions for clkdm
0977  * @clkdm: struct clockdomain *
0978  *
0979  * Allow the hardware to automatically switch the clockdomain @clkdm into
0980  * active or idle states, as needed by downstream clocks.  If the
0981  * clockdomain has any downstream clocks enabled in the clock
0982  * framework, wkdep/sleepdep autodependencies are added; this is so
0983  * device drivers can read and write to the device.  No return value.
0984  */
0985 void clkdm_allow_idle(struct clockdomain *clkdm)
0986 {
0987     pwrdm_lock(clkdm->pwrdm.ptr);
0988     clkdm_allow_idle_nolock(clkdm);
0989     pwrdm_unlock(clkdm->pwrdm.ptr);
0990 }
0991 
0992 /**
0993  * clkdm_deny_idle - disable hwsup idle transitions for clkdm
0994  * @clkdm: struct clockdomain *
0995  *
0996  * Prevent the hardware from automatically switching the clockdomain
0997  * @clkdm into inactive or idle states.  If the clockdomain has
0998  * downstream clocks enabled in the clock framework, wkdep/sleepdep
0999  * autodependencies are removed.  Only for use by the powerdomain
1000  * code.  No return value.
1001  */
1002 void clkdm_deny_idle_nolock(struct clockdomain *clkdm)
1003 {
1004     if (!clkdm)
1005         return;
1006 
1007     if (clkdm->forcewake_count++)
1008         return;
1009 
1010     if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
1011         clkdm_wakeup_nolock(clkdm);
1012 
1013     if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO))
1014         return;
1015 
1016     if (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING)
1017         return;
1018 
1019     if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
1020         return;
1021 
1022     pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
1023          clkdm->name);
1024 
1025     clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
1026     arch_clkdm->clkdm_deny_idle(clkdm);
1027     pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1028 }
1029 
1030 /**
1031  * clkdm_deny_idle - disable hwsup idle transitions for clkdm
1032  * @clkdm: struct clockdomain *
1033  *
1034  * Prevent the hardware from automatically switching the clockdomain
1035  * @clkdm into inactive or idle states.  If the clockdomain has
1036  * downstream clocks enabled in the clock framework, wkdep/sleepdep
1037  * autodependencies are removed.  No return value.
1038  */
1039 void clkdm_deny_idle(struct clockdomain *clkdm)
1040 {
1041     pwrdm_lock(clkdm->pwrdm.ptr);
1042     clkdm_deny_idle_nolock(clkdm);
1043     pwrdm_unlock(clkdm->pwrdm.ptr);
1044 }
1045 
1046 /**
1047  * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
1048  * @clkdm: struct clockdomain *
1049  *
1050  * Returns true if clockdomain @clkdm currently has
1051  * hardware-supervised idle enabled, or false if it does not or if
1052  * @clkdm is NULL.  It is only valid to call this function after
1053  * clkdm_init() has been called.  This function does not actually read
1054  * bits from the hardware; it instead tests an in-memory flag that is
1055  * changed whenever the clockdomain code changes the auto-idle mode.
1056  */
1057 bool clkdm_in_hwsup(struct clockdomain *clkdm)
1058 {
1059     bool ret;
1060 
1061     if (!clkdm)
1062         return false;
1063 
1064     ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
1065 
1066     return ret;
1067 }
1068 
1069 /**
1070  * clkdm_missing_idle_reporting - can @clkdm enter autoidle even if in use?
1071  * @clkdm: struct clockdomain *
1072  *
1073  * Returns true if clockdomain @clkdm has the
1074  * CLKDM_MISSING_IDLE_REPORTING flag set, or false if not or @clkdm is
1075  * null.  More information is available in the documentation for the
1076  * CLKDM_MISSING_IDLE_REPORTING macro.
1077  */
1078 bool clkdm_missing_idle_reporting(struct clockdomain *clkdm)
1079 {
1080     if (!clkdm)
1081         return false;
1082 
1083     return (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING) ? true : false;
1084 }
1085 
1086 /* Public autodep handling functions (deprecated) */
1087 
1088 /**
1089  * clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
1090  * @clkdm: struct clockdomain *
1091  *
1092  * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
1093  * in hardware-supervised mode.  Meant to be called from clock framework
1094  * when a clock inside clockdomain 'clkdm' is enabled.  No return value.
1095  *
1096  * XXX autodeps are deprecated and should be removed at the earliest
1097  * opportunity
1098  */
1099 void clkdm_add_autodeps(struct clockdomain *clkdm)
1100 {
1101     struct clkdm_autodep *autodep;
1102 
1103     if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
1104         return;
1105 
1106     for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
1107         if (IS_ERR(autodep->clkdm.ptr))
1108             continue;
1109 
1110         pr_debug("clockdomain: %s: adding %s sleepdep/wkdep\n",
1111              clkdm->name, autodep->clkdm.ptr->name);
1112 
1113         _clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
1114         _clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
1115     }
1116 }
1117 
1118 /**
1119  * clkdm_del_autodeps - remove auto sleepdeps/wkdeps from clkdm
1120  * @clkdm: struct clockdomain *
1121  *
1122  * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
1123  * in hardware-supervised mode.  Meant to be called from clock framework
1124  * when a clock inside clockdomain 'clkdm' is disabled.  No return value.
1125  *
1126  * XXX autodeps are deprecated and should be removed at the earliest
1127  * opportunity
1128  */
1129 void clkdm_del_autodeps(struct clockdomain *clkdm)
1130 {
1131     struct clkdm_autodep *autodep;
1132 
1133     if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
1134         return;
1135 
1136     for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
1137         if (IS_ERR(autodep->clkdm.ptr))
1138             continue;
1139 
1140         pr_debug("clockdomain: %s: removing %s sleepdep/wkdep\n",
1141              clkdm->name, autodep->clkdm.ptr->name);
1142 
1143         _clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
1144         _clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
1145     }
1146 }
1147 
1148 /* Clockdomain-to-clock/hwmod framework interface code */
1149 
1150 /**
1151  * clkdm_clk_enable - add an enabled downstream clock to this clkdm
1152  * @clkdm: struct clockdomain *
1153  * @clk: struct clk * of the enabled downstream clock
1154  *
1155  * Increment the usecount of the clockdomain @clkdm and ensure that it
1156  * is awake before @clk is enabled.  Intended to be called by
1157  * clk_enable() code.  If the clockdomain is in software-supervised
1158  * idle mode, force the clockdomain to wake.  If the clockdomain is in
1159  * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
1160  * ensure that devices in the clockdomain can be read from/written to
1161  * by on-chip processors.  Returns -EINVAL if passed null pointers;
1162  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1163  */
1164 int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *unused)
1165 {
1166     if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
1167         return -EINVAL;
1168 
1169     pwrdm_lock(clkdm->pwrdm.ptr);
1170 
1171     /*
1172      * For arch's with no autodeps, clkcm_clk_enable
1173      * should be called for every clock instance or hwmod that is
1174      * enabled, so the clkdm can be force woken up.
1175      */
1176     clkdm->usecount++;
1177     if (clkdm->usecount > 1 && autodeps) {
1178         pwrdm_unlock(clkdm->pwrdm.ptr);
1179         return 0;
1180     }
1181 
1182     arch_clkdm->clkdm_clk_enable(clkdm);
1183     pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1184     pwrdm_unlock(clkdm->pwrdm.ptr);
1185 
1186     pr_debug("clockdomain: %s: enabled\n", clkdm->name);
1187 
1188     return 0;
1189 }
1190 
1191 /**
1192  * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
1193  * @clkdm: struct clockdomain *
1194  * @clk: struct clk * of the disabled downstream clock
1195  *
1196  * Decrement the usecount of this clockdomain @clkdm when @clk is
1197  * disabled.  Intended to be called by clk_disable() code.  If the
1198  * clockdomain usecount goes to 0, put the clockdomain to sleep
1199  * (software-supervised mode) or remove the clkdm autodependencies
1200  * (hardware-supervised mode).  Returns -EINVAL if passed null
1201  * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
1202  * upon success or if the clockdomain is in hwsup idle mode.
1203  */
1204 int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
1205 {
1206     if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
1207         return -EINVAL;
1208 
1209     pwrdm_lock(clkdm->pwrdm.ptr);
1210 
1211     /* corner case: disabling unused clocks */
1212     if (clk && (__clk_get_enable_count(clk) == 0) && clkdm->usecount == 0)
1213         goto ccd_exit;
1214 
1215     if (clkdm->usecount == 0) {
1216         pwrdm_unlock(clkdm->pwrdm.ptr);
1217         WARN_ON(1); /* underflow */
1218         return -ERANGE;
1219     }
1220 
1221     clkdm->usecount--;
1222     if (clkdm->usecount > 0) {
1223         pwrdm_unlock(clkdm->pwrdm.ptr);
1224         return 0;
1225     }
1226 
1227     arch_clkdm->clkdm_clk_disable(clkdm);
1228     pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
1229 
1230     pr_debug("clockdomain: %s: disabled\n", clkdm->name);
1231 
1232 ccd_exit:
1233     pwrdm_unlock(clkdm->pwrdm.ptr);
1234 
1235     return 0;
1236 }
1237 
1238 /**
1239  * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
1240  * @clkdm: struct clockdomain *
1241  * @oh: struct omap_hwmod * of the enabled downstream hwmod
1242  *
1243  * Increment the usecount of the clockdomain @clkdm and ensure that it
1244  * is awake before @oh is enabled. Intended to be called by
1245  * module_enable() code.
1246  * If the clockdomain is in software-supervised idle mode, force the
1247  * clockdomain to wake.  If the clockdomain is in hardware-supervised idle
1248  * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
1249  * clockdomain can be read from/written to by on-chip processors.
1250  * Returns -EINVAL if passed null pointers;
1251  * returns 0 upon success or if the clockdomain is in hwsup idle mode.
1252  */
1253 int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1254 {
1255     /* The clkdm attribute does not exist yet prior OMAP4 */
1256     if (cpu_is_omap24xx() || cpu_is_omap34xx())
1257         return 0;
1258 
1259     /*
1260      * XXX Rewrite this code to maintain a list of enabled
1261      * downstream hwmods for debugging purposes?
1262      */
1263 
1264     if (!oh)
1265         return -EINVAL;
1266 
1267     return clkdm_clk_enable(clkdm, NULL);
1268 }
1269 
1270 /**
1271  * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
1272  * @clkdm: struct clockdomain *
1273  * @oh: struct omap_hwmod * of the disabled downstream hwmod
1274  *
1275  * Decrement the usecount of this clockdomain @clkdm when @oh is
1276  * disabled. Intended to be called by module_disable() code.
1277  * If the clockdomain usecount goes to 0, put the clockdomain to sleep
1278  * (software-supervised mode) or remove the clkdm autodependencies
1279  * (hardware-supervised mode).
1280  * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
1281  * underflows; or returns 0 upon success or if the clockdomain is in hwsup
1282  * idle mode.
1283  */
1284 int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
1285 {
1286     /* The clkdm attribute does not exist yet prior OMAP4 */
1287     if (cpu_is_omap24xx() || cpu_is_omap34xx())
1288         return 0;
1289 
1290     if (!oh)
1291         return -EINVAL;
1292 
1293     return clkdm_clk_disable(clkdm, NULL);
1294 }
1295 
1296 /**
1297  * _clkdm_save_context - save the context for the control of this clkdm
1298  *
1299  * Due to a suspend or hibernation operation, the state of the registers
1300  * controlling this clkdm will be lost, save their context.
1301  */
1302 static int _clkdm_save_context(struct clockdomain *clkdm, void *unused)
1303 {
1304     if (!arch_clkdm || !arch_clkdm->clkdm_save_context)
1305         return -EINVAL;
1306 
1307     return arch_clkdm->clkdm_save_context(clkdm);
1308 }
1309 
1310 /**
1311  * _clkdm_restore_context - restore context for control of this clkdm
1312  *
1313  * Restore the register values for this clockdomain.
1314  */
1315 static int _clkdm_restore_context(struct clockdomain *clkdm, void *unused)
1316 {
1317     if (!arch_clkdm || !arch_clkdm->clkdm_restore_context)
1318         return -EINVAL;
1319 
1320     return arch_clkdm->clkdm_restore_context(clkdm);
1321 }
1322 
1323 /**
1324  * clkdm_save_context - Saves the context for each registered clkdm
1325  *
1326  * Save the context for each registered clockdomain.
1327  */
1328 void clkdm_save_context(void)
1329 {
1330     clkdm_for_each(_clkdm_save_context, NULL);
1331 }
1332 
1333 /**
1334  * clkdm_restore_context - Restores the context for each registered clkdm
1335  *
1336  * Restore the context for each registered clockdomain.
1337  */
1338 void clkdm_restore_context(void)
1339 {
1340     clkdm_for_each(_clkdm_restore_context, NULL);
1341 }