// SPDX-License-Identifier: GPL-2.0-only1/*2* OMAP2/3/4 clockdomain framework functions3*4* Copyright (C) 2008-2011 Texas Instruments, Inc.5* Copyright (C) 2008-2011 Nokia Corporation6*7* Written by Paul Walmsley and Jouni Högander8* Added OMAP4 specific support by Abhijit Pagare <[email protected]>9*/10#undef DEBUG1112#include <linux/kernel.h>13#include <linux/device.h>14#include <linux/list.h>15#include <linux/errno.h>16#include <linux/string.h>17#include <linux/delay.h>18#include <linux/clk.h>19#include <linux/limits.h>20#include <linux/err.h>21#include <linux/clk-provider.h>22#include <linux/cpu_pm.h>2324#include <linux/io.h>2526#include <linux/bitops.h>2728#include "soc.h"29#include "clock.h"30#include "clockdomain.h"31#include "pm.h"3233/* clkdm_list contains all registered struct clockdomains */34static LIST_HEAD(clkdm_list);3536/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */37static struct clkdm_autodep *autodeps;3839static struct clkdm_ops *arch_clkdm;40void clkdm_save_context(void);41void clkdm_restore_context(void);4243/* Private functions */4445static struct clockdomain *_clkdm_lookup(const char *name)46{47struct clockdomain *clkdm, *temp_clkdm;4849if (!name)50return NULL;5152clkdm = NULL;5354list_for_each_entry(temp_clkdm, &clkdm_list, node) {55if (!strcmp(name, temp_clkdm->name)) {56clkdm = temp_clkdm;57break;58}59}6061return clkdm;62}6364/**65* _clkdm_register - register a clockdomain66* @clkdm: struct clockdomain * to register67*68* Adds a clockdomain to the internal clockdomain list.69* Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is70* already registered by the provided name, or 0 upon success.71*/72static int _clkdm_register(struct clockdomain *clkdm)73{74struct powerdomain *pwrdm;7576if (!clkdm || !clkdm->name)77return -EINVAL;7879pwrdm = pwrdm_lookup(clkdm->pwrdm.name);80if (!pwrdm) {81pr_err("clockdomain: %s: powerdomain %s does not exist\n",82clkdm->name, clkdm->pwrdm.name);83return -EINVAL;84}85clkdm->pwrdm.ptr = pwrdm;8687/* Verify that the clockdomain is not already registered */88if (_clkdm_lookup(clkdm->name))89return -EEXIST;9091list_add(&clkdm->node, &clkdm_list);9293pwrdm_add_clkdm(pwrdm, clkdm);9495pr_debug("clockdomain: registered %s\n", clkdm->name);9697return 0;98}99100/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */101static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,102struct clkdm_dep *deps)103{104struct clkdm_dep *cd;105106if (!clkdm || !deps)107return ERR_PTR(-EINVAL);108109for (cd = deps; cd->clkdm_name; cd++) {110if (!cd->clkdm && cd->clkdm_name)111cd->clkdm = _clkdm_lookup(cd->clkdm_name);112113if (cd->clkdm == clkdm)114break;115}116117if (!cd->clkdm_name)118return ERR_PTR(-ENOENT);119120return cd;121}122123/**124* _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store125* @autodep: struct clkdm_autodep * to resolve126*127* Resolve autodep clockdomain names to clockdomain pointers via128* clkdm_lookup() and store the pointers in the autodep structure. An129* "autodep" is a clockdomain sleep/wakeup dependency that is130* automatically added and removed whenever clocks in the associated131* clockdomain are enabled or disabled (respectively) when the132* clockdomain is in hardware-supervised mode. Meant to be called133* once at clockdomain layer initialization, since these should remain134* fixed for a particular architecture. No return value.135*136* XXX autodeps are deprecated and should be removed at the earliest137* opportunity138*/139static void _autodep_lookup(struct clkdm_autodep *autodep)140{141struct clockdomain *clkdm;142143if (!autodep)144return;145146clkdm = clkdm_lookup(autodep->clkdm.name);147if (!clkdm) {148pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",149autodep->clkdm.name);150clkdm = ERR_PTR(-ENOENT);151}152autodep->clkdm.ptr = clkdm;153}154155/**156* _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms157* @clkdm: clockdomain that we are resolving dependencies for158* @clkdm_deps: ptr to array of struct clkdm_deps to resolve159*160* Iterates through @clkdm_deps, looking up the struct clockdomain named by161* clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.162* No return value.163*/164static void _resolve_clkdm_deps(struct clockdomain *clkdm,165struct clkdm_dep *clkdm_deps)166{167struct clkdm_dep *cd;168169for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {170if (cd->clkdm)171continue;172cd->clkdm = _clkdm_lookup(cd->clkdm_name);173174WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",175clkdm->name, cd->clkdm_name);176}177}178179/**180* _clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1 (lockless)181* @clkdm1: wake this struct clockdomain * up (dependent)182* @clkdm2: when this struct clockdomain * wakes up (source)183*184* When the clockdomain represented by @clkdm2 wakes up, wake up185* @clkdm1. Implemented in hardware on the OMAP, this feature is186* designed to reduce wakeup latency of the dependent clockdomain @clkdm1.187* Returns -EINVAL if presented with invalid clockdomain pointers,188* -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon189* success.190*/191static int _clkdm_add_wkdep(struct clockdomain *clkdm1,192struct clockdomain *clkdm2)193{194struct clkdm_dep *cd;195int ret = 0;196197if (!clkdm1 || !clkdm2)198return -EINVAL;199200cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);201if (IS_ERR(cd))202ret = PTR_ERR(cd);203204if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)205ret = -EINVAL;206207if (ret) {208pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",209clkdm1->name, clkdm2->name);210return ret;211}212213cd->wkdep_usecount++;214if (cd->wkdep_usecount == 1) {215pr_debug("clockdomain: hardware will wake up %s when %s wakes up\n",216clkdm1->name, clkdm2->name);217218ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);219}220221return ret;222}223224/**225* _clkdm_del_wkdep - remove a wakeup dep from clkdm2 to clkdm1 (lockless)226* @clkdm1: wake this struct clockdomain * up (dependent)227* @clkdm2: when this struct clockdomain * wakes up (source)228*229* Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2230* wakes up. Returns -EINVAL if presented with invalid clockdomain231* pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or232* 0 upon success.233*/234static int _clkdm_del_wkdep(struct clockdomain *clkdm1,235struct clockdomain *clkdm2)236{237struct clkdm_dep *cd;238int ret = 0;239240if (!clkdm1 || !clkdm2)241return -EINVAL;242243cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);244if (IS_ERR(cd))245ret = PTR_ERR(cd);246247if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)248ret = -EINVAL;249250if (ret) {251pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",252clkdm1->name, clkdm2->name);253return ret;254}255256cd->wkdep_usecount--;257if (cd->wkdep_usecount == 0) {258pr_debug("clockdomain: hardware will no longer wake up %s after %s wakes up\n",259clkdm1->name, clkdm2->name);260261ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);262}263264return ret;265}266267/**268* _clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1 (lockless)269* @clkdm1: prevent this struct clockdomain * from sleeping (dependent)270* @clkdm2: when this struct clockdomain * is active (source)271*272* Prevent @clkdm1 from automatically going inactive (and then to273* retention or off) if @clkdm2 is active. Returns -EINVAL if274* presented with invalid clockdomain pointers or called on a machine275* that does not support software-configurable hardware sleep276* dependencies, -ENOENT if the specified dependency cannot be set in277* hardware, or 0 upon success.278*/279static int _clkdm_add_sleepdep(struct clockdomain *clkdm1,280struct clockdomain *clkdm2)281{282struct clkdm_dep *cd;283int ret = 0;284285if (!clkdm1 || !clkdm2)286return -EINVAL;287288cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);289if (IS_ERR(cd))290ret = PTR_ERR(cd);291292if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)293ret = -EINVAL;294295if (ret) {296pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",297clkdm1->name, clkdm2->name);298return ret;299}300301cd->sleepdep_usecount++;302if (cd->sleepdep_usecount == 1) {303pr_debug("clockdomain: will prevent %s from sleeping if %s is active\n",304clkdm1->name, clkdm2->name);305306ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);307}308309return ret;310}311312/**313* _clkdm_del_sleepdep - remove a sleep dep from clkdm2 to clkdm1 (lockless)314* @clkdm1: prevent this struct clockdomain * from sleeping (dependent)315* @clkdm2: when this struct clockdomain * is active (source)316*317* Allow @clkdm1 to automatically go inactive (and then to retention or318* off), independent of the activity state of @clkdm2. Returns -EINVAL319* if presented with invalid clockdomain pointers or called on a machine320* that does not support software-configurable hardware sleep dependencies,321* -ENOENT if the specified dependency cannot be cleared in hardware, or322* 0 upon success.323*/324static int _clkdm_del_sleepdep(struct clockdomain *clkdm1,325struct clockdomain *clkdm2)326{327struct clkdm_dep *cd;328int ret = 0;329330if (!clkdm1 || !clkdm2)331return -EINVAL;332333cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);334if (IS_ERR(cd))335ret = PTR_ERR(cd);336337if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)338ret = -EINVAL;339340if (ret) {341pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",342clkdm1->name, clkdm2->name);343return ret;344}345346cd->sleepdep_usecount--;347if (cd->sleepdep_usecount == 0) {348pr_debug("clockdomain: will no longer prevent %s from sleeping if %s is active\n",349clkdm1->name, clkdm2->name);350351ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);352}353354return ret;355}356357/* Public functions */358359/**360* clkdm_register_platform_funcs - register clockdomain implementation fns361* @co: func pointers for arch specific implementations362*363* Register the list of function pointers used to implement the364* clockdomain functions on different OMAP SoCs. Should be called365* before any other clkdm_register*() function. Returns -EINVAL if366* @co is null, -EEXIST if platform functions have already been367* registered, or 0 upon success.368*/369int clkdm_register_platform_funcs(struct clkdm_ops *co)370{371if (!co)372return -EINVAL;373374if (arch_clkdm)375return -EEXIST;376377arch_clkdm = co;378379return 0;380};381382/**383* clkdm_register_clkdms - register SoC clockdomains384* @cs: pointer to an array of struct clockdomain to register385*386* Register the clockdomains available on a particular OMAP SoC. Must387* be called after clkdm_register_platform_funcs(). May be called388* multiple times. Returns -EACCES if called before389* clkdm_register_platform_funcs(); -EINVAL if the argument @cs is390* null; or 0 upon success.391*/392int clkdm_register_clkdms(struct clockdomain **cs)393{394struct clockdomain **c = NULL;395396if (!arch_clkdm)397return -EACCES;398399if (!cs)400return -EINVAL;401402for (c = cs; *c; c++)403_clkdm_register(*c);404405return 0;406}407408/**409* clkdm_register_autodeps - register autodeps (if required)410* @ia: pointer to a static array of struct clkdm_autodep to register411*412* Register clockdomain "automatic dependencies." These are413* clockdomain wakeup and sleep dependencies that are automatically414* added whenever the first clock inside a clockdomain is enabled, and415* removed whenever the last clock inside a clockdomain is disabled.416* These are currently only used on OMAP3 devices, and are deprecated,417* since they waste energy. However, until the OMAP2/3 IP block418* enable/disable sequence can be converted to match the OMAP4419* sequence, they are needed.420*421* Must be called only after all of the SoC clockdomains are422* registered, since the function will resolve autodep clockdomain423* names into clockdomain pointers.424*425* The struct clkdm_autodep @ia array must be static, as this function426* does not copy the array elements.427*428* Returns -EACCES if called before any clockdomains have been429* registered, -EINVAL if called with a null @ia argument, -EEXIST if430* autodeps have already been registered, or 0 upon success.431*/432int clkdm_register_autodeps(struct clkdm_autodep *ia)433{434struct clkdm_autodep *a = NULL;435436if (list_empty(&clkdm_list))437return -EACCES;438439if (!ia)440return -EINVAL;441442if (autodeps)443return -EEXIST;444445autodeps = ia;446for (a = autodeps; a->clkdm.ptr; a++)447_autodep_lookup(a);448449return 0;450}451452static int cpu_notifier(struct notifier_block *nb, unsigned long cmd, void *v)453{454switch (cmd) {455case CPU_CLUSTER_PM_ENTER:456if (enable_off_mode)457clkdm_save_context();458break;459case CPU_CLUSTER_PM_EXIT:460if (enable_off_mode)461clkdm_restore_context();462break;463}464465return NOTIFY_OK;466}467468/**469* clkdm_complete_init - set up the clockdomain layer470*471* Put all clockdomains into software-supervised mode; PM code should472* later enable hardware-supervised mode as appropriate. Must be473* called after clkdm_register_clkdms(). Returns -EACCES if called474* before clkdm_register_clkdms(), or 0 upon success.475*/476int clkdm_complete_init(void)477{478struct clockdomain *clkdm;479static struct notifier_block nb;480481if (list_empty(&clkdm_list))482return -EACCES;483484list_for_each_entry(clkdm, &clkdm_list, node) {485clkdm_deny_idle(clkdm);486487_resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);488clkdm_clear_all_wkdeps(clkdm);489490_resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);491clkdm_clear_all_sleepdeps(clkdm);492}493494/* Only AM43XX can lose clkdm context during rtc-ddr suspend */495if (soc_is_am43xx()) {496nb.notifier_call = cpu_notifier;497cpu_pm_register_notifier(&nb);498}499500return 0;501}502503/**504* clkdm_lookup - look up a clockdomain by name, return a pointer505* @name: name of clockdomain506*507* Find a registered clockdomain by its name @name. Returns a pointer508* to the struct clockdomain if found, or NULL otherwise.509*/510struct clockdomain *clkdm_lookup(const char *name)511{512struct clockdomain *clkdm, *temp_clkdm;513514if (!name)515return NULL;516517clkdm = NULL;518519list_for_each_entry(temp_clkdm, &clkdm_list, node) {520if (!strcmp(name, temp_clkdm->name)) {521clkdm = temp_clkdm;522break;523}524}525526return clkdm;527}528529/**530* clkdm_for_each - call function on each registered clockdomain531* @fn: callback function *532*533* Call the supplied function @fn for each registered clockdomain.534* The callback function @fn can return anything but 0 to bail535* out early from the iterator. The callback function is called with536* the clkdm_mutex held, so no clockdomain structure manipulation537* functions should be called from the callback, although hardware538* clockdomain control functions are fine. Returns the last return539* value of the callback function, which should be 0 for success or540* anything else to indicate failure; or -EINVAL if the function pointer541* is null.542*/543int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),544void *user)545{546struct clockdomain *clkdm;547int ret = 0;548549if (!fn)550return -EINVAL;551552list_for_each_entry(clkdm, &clkdm_list, node) {553ret = (*fn)(clkdm, user);554if (ret)555break;556}557558return ret;559}560561562/**563* clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in564* @clkdm: struct clockdomain *565*566* Return a pointer to the struct powerdomain that the specified clockdomain567* @clkdm exists in, or returns NULL if @clkdm is NULL.568*/569struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)570{571if (!clkdm)572return NULL;573574return clkdm->pwrdm.ptr;575}576577578/* Hardware clockdomain control */579580/**581* clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1582* @clkdm1: wake this struct clockdomain * up (dependent)583* @clkdm2: when this struct clockdomain * wakes up (source)584*585* When the clockdomain represented by @clkdm2 wakes up, wake up586* @clkdm1. Implemented in hardware on the OMAP, this feature is587* designed to reduce wakeup latency of the dependent clockdomain @clkdm1.588* Returns -EINVAL if presented with invalid clockdomain pointers,589* -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon590* success.591*/592int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)593{594struct clkdm_dep *cd;595int ret;596597if (!clkdm1 || !clkdm2)598return -EINVAL;599600cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);601if (IS_ERR(cd))602return PTR_ERR(cd);603604pwrdm_lock(cd->clkdm->pwrdm.ptr);605ret = _clkdm_add_wkdep(clkdm1, clkdm2);606pwrdm_unlock(cd->clkdm->pwrdm.ptr);607608return ret;609}610611/**612* clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1613* @clkdm1: wake this struct clockdomain * up (dependent)614* @clkdm2: when this struct clockdomain * wakes up (source)615*616* Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2617* wakes up. Returns -EINVAL if presented with invalid clockdomain618* pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or619* 0 upon success.620*/621int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)622{623struct clkdm_dep *cd;624int ret;625626if (!clkdm1 || !clkdm2)627return -EINVAL;628629cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);630if (IS_ERR(cd))631return PTR_ERR(cd);632633pwrdm_lock(cd->clkdm->pwrdm.ptr);634ret = _clkdm_del_wkdep(clkdm1, clkdm2);635pwrdm_unlock(cd->clkdm->pwrdm.ptr);636637return ret;638}639640/**641* clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1642* @clkdm1: wake this struct clockdomain * up (dependent)643* @clkdm2: when this struct clockdomain * wakes up (source)644*645* Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be646* awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL647* if either clockdomain pointer is invalid; or -ENOENT if the hardware648* is incapable.649*650* REVISIT: Currently this function only represents software-controllable651* wakeup dependencies. Wakeup dependencies fixed in hardware are not652* yet handled here.653*/654int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)655{656struct clkdm_dep *cd;657int ret = 0;658659if (!clkdm1 || !clkdm2)660return -EINVAL;661662cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);663if (IS_ERR(cd))664ret = PTR_ERR(cd);665666if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)667ret = -EINVAL;668669if (ret) {670pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",671clkdm1->name, clkdm2->name);672return ret;673}674675/* XXX It's faster to return the wkdep_usecount */676return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);677}678679/**680* clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm681* @clkdm: struct clockdomain * to remove all wakeup dependencies from682*683* Remove all inter-clockdomain wakeup dependencies that could cause684* @clkdm to wake. Intended to be used during boot to initialize the685* PRCM to a known state, after all clockdomains are put into swsup idle686* and woken up. Returns -EINVAL if @clkdm pointer is invalid, or687* 0 upon success.688*/689int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)690{691if (!clkdm)692return -EINVAL;693694if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)695return -EINVAL;696697return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);698}699700/**701* clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1702* @clkdm1: prevent this struct clockdomain * from sleeping (dependent)703* @clkdm2: when this struct clockdomain * is active (source)704*705* Prevent @clkdm1 from automatically going inactive (and then to706* retention or off) if @clkdm2 is active. Returns -EINVAL if707* presented with invalid clockdomain pointers or called on a machine708* that does not support software-configurable hardware sleep709* dependencies, -ENOENT if the specified dependency cannot be set in710* hardware, or 0 upon success.711*/712int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)713{714struct clkdm_dep *cd;715int ret;716717if (!clkdm1 || !clkdm2)718return -EINVAL;719720cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);721if (IS_ERR(cd))722return PTR_ERR(cd);723724pwrdm_lock(cd->clkdm->pwrdm.ptr);725ret = _clkdm_add_sleepdep(clkdm1, clkdm2);726pwrdm_unlock(cd->clkdm->pwrdm.ptr);727728return ret;729}730731/**732* clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1733* @clkdm1: prevent this struct clockdomain * from sleeping (dependent)734* @clkdm2: when this struct clockdomain * is active (source)735*736* Allow @clkdm1 to automatically go inactive (and then to retention or737* off), independent of the activity state of @clkdm2. Returns -EINVAL738* if presented with invalid clockdomain pointers or called on a machine739* that does not support software-configurable hardware sleep dependencies,740* -ENOENT if the specified dependency cannot be cleared in hardware, or741* 0 upon success.742*/743int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)744{745struct clkdm_dep *cd;746int ret;747748if (!clkdm1 || !clkdm2)749return -EINVAL;750751cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);752if (IS_ERR(cd))753return PTR_ERR(cd);754755pwrdm_lock(cd->clkdm->pwrdm.ptr);756ret = _clkdm_del_sleepdep(clkdm1, clkdm2);757pwrdm_unlock(cd->clkdm->pwrdm.ptr);758759return ret;760}761762/**763* clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1764* @clkdm1: prevent this struct clockdomain * from sleeping (dependent)765* @clkdm2: when this struct clockdomain * is active (source)766*767* Return 1 if a hardware sleep dependency exists wherein @clkdm1 will768* not be allowed to automatically go inactive if @clkdm2 is active;769* 0 if @clkdm1's automatic power state inactivity transition is independent770* of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called771* on a machine that does not support software-configurable hardware sleep772* dependencies; or -ENOENT if the hardware is incapable.773*774* REVISIT: Currently this function only represents software-controllable775* sleep dependencies. Sleep dependencies fixed in hardware are not776* yet handled here.777*/778int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)779{780struct clkdm_dep *cd;781int ret = 0;782783if (!clkdm1 || !clkdm2)784return -EINVAL;785786cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);787if (IS_ERR(cd))788ret = PTR_ERR(cd);789790if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)791ret = -EINVAL;792793if (ret) {794pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",795clkdm1->name, clkdm2->name);796return ret;797}798799/* XXX It's faster to return the sleepdep_usecount */800return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);801}802803/**804* clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm805* @clkdm: struct clockdomain * to remove all sleep dependencies from806*807* Remove all inter-clockdomain sleep dependencies that could prevent808* @clkdm from idling. Intended to be used during boot to initialize the809* PRCM to a known state, after all clockdomains are put into swsup idle810* and woken up. Returns -EINVAL if @clkdm pointer is invalid, or811* 0 upon success.812*/813int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)814{815if (!clkdm)816return -EINVAL;817818if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)819return -EINVAL;820821return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);822}823824/**825* clkdm_sleep_nolock - force clockdomain sleep transition (lockless)826* @clkdm: struct clockdomain *827*828* Instruct the CM to force a sleep transition on the specified829* clockdomain @clkdm. Only for use by the powerdomain code. Returns830* -EINVAL if @clkdm is NULL or if clockdomain does not support831* software-initiated sleep; 0 upon success.832*/833static int clkdm_sleep_nolock(struct clockdomain *clkdm)834{835int ret;836837if (!clkdm)838return -EINVAL;839840if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {841pr_debug("clockdomain: %s does not support forcing sleep via software\n",842clkdm->name);843return -EINVAL;844}845846if (!arch_clkdm || !arch_clkdm->clkdm_sleep)847return -EINVAL;848849pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);850851clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;852ret = arch_clkdm->clkdm_sleep(clkdm);853ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);854855return ret;856}857858/**859* clkdm_sleep - force clockdomain sleep transition860* @clkdm: struct clockdomain *861*862* Instruct the CM to force a sleep transition on the specified863* clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if864* clockdomain does not support software-initiated sleep; 0 upon865* success.866*/867int clkdm_sleep(struct clockdomain *clkdm)868{869int ret;870871pwrdm_lock(clkdm->pwrdm.ptr);872ret = clkdm_sleep_nolock(clkdm);873pwrdm_unlock(clkdm->pwrdm.ptr);874875return ret;876}877878/**879* clkdm_wakeup_nolock - force clockdomain wakeup transition (lockless)880* @clkdm: struct clockdomain *881*882* Instruct the CM to force a wakeup transition on the specified883* clockdomain @clkdm. Only for use by the powerdomain code. Returns884* -EINVAL if @clkdm is NULL or if the clockdomain does not support885* software-controlled wakeup; 0 upon success.886*/887static int clkdm_wakeup_nolock(struct clockdomain *clkdm)888{889int ret;890891if (!clkdm)892return -EINVAL;893894if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {895pr_debug("clockdomain: %s does not support forcing wakeup via software\n",896clkdm->name);897return -EINVAL;898}899900if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)901return -EINVAL;902903pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);904905clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;906ret = arch_clkdm->clkdm_wakeup(clkdm);907ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);908909return ret;910}911912/**913* clkdm_wakeup - force clockdomain wakeup transition914* @clkdm: struct clockdomain *915*916* Instruct the CM to force a wakeup transition on the specified917* clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the918* clockdomain does not support software-controlled wakeup; 0 upon919* success.920*/921int clkdm_wakeup(struct clockdomain *clkdm)922{923int ret;924925pwrdm_lock(clkdm->pwrdm.ptr);926ret = clkdm_wakeup_nolock(clkdm);927pwrdm_unlock(clkdm->pwrdm.ptr);928929return ret;930}931932/**933* clkdm_allow_idle_nolock - enable hwsup idle transitions for clkdm934* @clkdm: struct clockdomain *935*936* Allow the hardware to automatically switch the clockdomain @clkdm937* into active or idle states, as needed by downstream clocks. If the938* clockdomain has any downstream clocks enabled in the clock939* framework, wkdep/sleepdep autodependencies are added; this is so940* device drivers can read and write to the device. Only for use by941* the powerdomain code. No return value.942*/943void clkdm_allow_idle_nolock(struct clockdomain *clkdm)944{945if (!clkdm)946return;947948if (!WARN_ON(!clkdm->forcewake_count))949clkdm->forcewake_count--;950951if (clkdm->forcewake_count)952return;953954if (!clkdm->usecount && (clkdm->flags & CLKDM_CAN_FORCE_SLEEP))955clkdm_sleep_nolock(clkdm);956957if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO))958return;959960if (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING)961return;962963if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)964return;965966pr_debug("clockdomain: enabling automatic idle transitions for %s\n",967clkdm->name);968969clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;970arch_clkdm->clkdm_allow_idle(clkdm);971pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);972}973974/**975* clkdm_allow_idle - enable hwsup idle transitions for clkdm976* @clkdm: struct clockdomain *977*978* Allow the hardware to automatically switch the clockdomain @clkdm into979* active or idle states, as needed by downstream clocks. If the980* clockdomain has any downstream clocks enabled in the clock981* framework, wkdep/sleepdep autodependencies are added; this is so982* device drivers can read and write to the device. No return value.983*/984void clkdm_allow_idle(struct clockdomain *clkdm)985{986pwrdm_lock(clkdm->pwrdm.ptr);987clkdm_allow_idle_nolock(clkdm);988pwrdm_unlock(clkdm->pwrdm.ptr);989}990991/**992* clkdm_deny_idle_nolock - disable hwsup idle transitions for clkdm993* @clkdm: struct clockdomain *994*995* Prevent the hardware from automatically switching the clockdomain996* @clkdm into inactive or idle states. If the clockdomain has997* downstream clocks enabled in the clock framework, wkdep/sleepdep998* autodependencies are removed. Only for use by the powerdomain999* code. No return value.1000*/1001void clkdm_deny_idle_nolock(struct clockdomain *clkdm)1002{1003if (!clkdm)1004return;10051006if (clkdm->forcewake_count++)1007return;10081009if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)1010clkdm_wakeup_nolock(clkdm);10111012if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO))1013return;10141015if (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING)1016return;10171018if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)1019return;10201021pr_debug("clockdomain: disabling automatic idle transitions for %s\n",1022clkdm->name);10231024clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;1025arch_clkdm->clkdm_deny_idle(clkdm);1026pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);1027}10281029/**1030* clkdm_deny_idle - disable hwsup idle transitions for clkdm1031* @clkdm: struct clockdomain *1032*1033* Prevent the hardware from automatically switching the clockdomain1034* @clkdm into inactive or idle states. If the clockdomain has1035* downstream clocks enabled in the clock framework, wkdep/sleepdep1036* autodependencies are removed. No return value.1037*/1038void clkdm_deny_idle(struct clockdomain *clkdm)1039{1040pwrdm_lock(clkdm->pwrdm.ptr);1041clkdm_deny_idle_nolock(clkdm);1042pwrdm_unlock(clkdm->pwrdm.ptr);1043}10441045/* Public autodep handling functions (deprecated) */10461047/**1048* clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable1049* @clkdm: struct clockdomain *1050*1051* Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'1052* in hardware-supervised mode. Meant to be called from clock framework1053* when a clock inside clockdomain 'clkdm' is enabled. No return value.1054*1055* XXX autodeps are deprecated and should be removed at the earliest1056* opportunity1057*/1058void clkdm_add_autodeps(struct clockdomain *clkdm)1059{1060struct clkdm_autodep *autodep;10611062if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)1063return;10641065for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {1066if (IS_ERR(autodep->clkdm.ptr))1067continue;10681069pr_debug("clockdomain: %s: adding %s sleepdep/wkdep\n",1070clkdm->name, autodep->clkdm.ptr->name);10711072_clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);1073_clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);1074}1075}10761077/**1078* clkdm_del_autodeps - remove auto sleepdeps/wkdeps from clkdm1079* @clkdm: struct clockdomain *1080*1081* Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'1082* in hardware-supervised mode. Meant to be called from clock framework1083* when a clock inside clockdomain 'clkdm' is disabled. No return value.1084*1085* XXX autodeps are deprecated and should be removed at the earliest1086* opportunity1087*/1088void clkdm_del_autodeps(struct clockdomain *clkdm)1089{1090struct clkdm_autodep *autodep;10911092if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)1093return;10941095for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {1096if (IS_ERR(autodep->clkdm.ptr))1097continue;10981099pr_debug("clockdomain: %s: removing %s sleepdep/wkdep\n",1100clkdm->name, autodep->clkdm.ptr->name);11011102_clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);1103_clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);1104}1105}11061107/* Clockdomain-to-clock/hwmod framework interface code */11081109/**1110* clkdm_clk_enable - add an enabled downstream clock to this clkdm1111* @clkdm: struct clockdomain *1112* @unused: struct clk * of the enabled downstream clock1113*1114* Increment the usecount of the clockdomain @clkdm and ensure that it1115* is awake before @clk is enabled. Intended to be called by1116* clk_enable() code. If the clockdomain is in software-supervised1117* idle mode, force the clockdomain to wake. If the clockdomain is in1118* hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to1119* ensure that devices in the clockdomain can be read from/written to1120* by on-chip processors. Returns -EINVAL if passed null pointers;1121* returns 0 upon success or if the clockdomain is in hwsup idle mode.1122*/1123int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *unused)1124{1125if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)1126return -EINVAL;11271128pwrdm_lock(clkdm->pwrdm.ptr);11291130/*1131* For arch's with no autodeps, clkcm_clk_enable1132* should be called for every clock instance or hwmod that is1133* enabled, so the clkdm can be force woken up.1134*/1135clkdm->usecount++;1136if (clkdm->usecount > 1 && autodeps) {1137pwrdm_unlock(clkdm->pwrdm.ptr);1138return 0;1139}11401141arch_clkdm->clkdm_clk_enable(clkdm);1142pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);1143pwrdm_unlock(clkdm->pwrdm.ptr);11441145pr_debug("clockdomain: %s: enabled\n", clkdm->name);11461147return 0;1148}11491150/**1151* clkdm_clk_disable - remove an enabled downstream clock from this clkdm1152* @clkdm: struct clockdomain *1153* @clk: struct clk * of the disabled downstream clock1154*1155* Decrement the usecount of this clockdomain @clkdm when @clk is1156* disabled. Intended to be called by clk_disable() code. If the1157* clockdomain usecount goes to 0, put the clockdomain to sleep1158* (software-supervised mode) or remove the clkdm autodependencies1159* (hardware-supervised mode). Returns -EINVAL if passed null1160* pointers; -ERANGE if the @clkdm usecount underflows; or returns 01161* upon success or if the clockdomain is in hwsup idle mode.1162*/1163int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)1164{1165if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)1166return -EINVAL;11671168pwrdm_lock(clkdm->pwrdm.ptr);11691170/* corner case: disabling unused clocks */1171if (clk && (__clk_get_enable_count(clk) == 0) && clkdm->usecount == 0)1172goto ccd_exit;11731174if (clkdm->usecount == 0) {1175pwrdm_unlock(clkdm->pwrdm.ptr);1176WARN_ON(1); /* underflow */1177return -ERANGE;1178}11791180clkdm->usecount--;1181if (clkdm->usecount > 0) {1182pwrdm_unlock(clkdm->pwrdm.ptr);1183return 0;1184}11851186arch_clkdm->clkdm_clk_disable(clkdm);1187pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);11881189pr_debug("clockdomain: %s: disabled\n", clkdm->name);11901191ccd_exit:1192pwrdm_unlock(clkdm->pwrdm.ptr);11931194return 0;1195}11961197/**1198* clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm1199* @clkdm: struct clockdomain *1200* @oh: struct omap_hwmod * of the enabled downstream hwmod1201*1202* Increment the usecount of the clockdomain @clkdm and ensure that it1203* is awake before @oh is enabled. Intended to be called by1204* module_enable() code.1205* If the clockdomain is in software-supervised idle mode, force the1206* clockdomain to wake. If the clockdomain is in hardware-supervised idle1207* mode, add clkdm-pwrdm autodependencies, to ensure that devices in the1208* clockdomain can be read from/written to by on-chip processors.1209* Returns -EINVAL if passed null pointers;1210* returns 0 upon success or if the clockdomain is in hwsup idle mode.1211*/1212int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)1213{1214/* The clkdm attribute does not exist yet prior OMAP4 */1215if (cpu_is_omap24xx() || cpu_is_omap34xx())1216return 0;12171218/*1219* XXX Rewrite this code to maintain a list of enabled1220* downstream hwmods for debugging purposes?1221*/12221223if (!oh)1224return -EINVAL;12251226return clkdm_clk_enable(clkdm, NULL);1227}12281229/**1230* clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm1231* @clkdm: struct clockdomain *1232* @oh: struct omap_hwmod * of the disabled downstream hwmod1233*1234* Decrement the usecount of this clockdomain @clkdm when @oh is1235* disabled. Intended to be called by module_disable() code.1236* If the clockdomain usecount goes to 0, put the clockdomain to sleep1237* (software-supervised mode) or remove the clkdm autodependencies1238* (hardware-supervised mode).1239* Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount1240* underflows; or returns 0 upon success or if the clockdomain is in hwsup1241* idle mode.1242*/1243int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)1244{1245/* The clkdm attribute does not exist yet prior OMAP4 */1246if (cpu_is_omap24xx() || cpu_is_omap34xx())1247return 0;12481249if (!oh)1250return -EINVAL;12511252return clkdm_clk_disable(clkdm, NULL);1253}12541255/**1256* _clkdm_save_context - save the context for the control of this clkdm1257*1258* Due to a suspend or hibernation operation, the state of the registers1259* controlling this clkdm will be lost, save their context.1260*/1261static int _clkdm_save_context(struct clockdomain *clkdm, void *unused)1262{1263if (!arch_clkdm || !arch_clkdm->clkdm_save_context)1264return -EINVAL;12651266return arch_clkdm->clkdm_save_context(clkdm);1267}12681269/**1270* _clkdm_restore_context - restore context for control of this clkdm1271*1272* Restore the register values for this clockdomain.1273*/1274static int _clkdm_restore_context(struct clockdomain *clkdm, void *unused)1275{1276if (!arch_clkdm || !arch_clkdm->clkdm_restore_context)1277return -EINVAL;12781279return arch_clkdm->clkdm_restore_context(clkdm);1280}12811282/**1283* clkdm_save_context - Saves the context for each registered clkdm1284*1285* Save the context for each registered clockdomain.1286*/1287void clkdm_save_context(void)1288{1289clkdm_for_each(_clkdm_save_context, NULL);1290}12911292/**1293* clkdm_restore_context - Restores the context for each registered clkdm1294*1295* Restore the context for each registered clockdomain.1296*/1297void clkdm_restore_context(void)1298{1299clkdm_for_each(_clkdm_restore_context, NULL);1300}130113021303