clockdomain.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. * OMAP2/3/4 clockdomain framework functions
  3. *
  4. * Copyright (C) 2008-2011 Texas Instruments, Inc.
  5. * Copyright (C) 2008-2011 Nokia Corporation
  6. *
  7. * Written by Paul Walmsley and Jouni Högander
  8. * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
  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. */
  14. #undef DEBUG
  15. #include <linux/kernel.h>
  16. #include <linux/device.h>
  17. #include <linux/list.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/delay.h>
  21. #include <linux/clk.h>
  22. #include <linux/limits.h>
  23. #include <linux/err.h>
  24. #include <linux/clk-provider.h>
  25. #include <linux/io.h>
  26. #include <linux/bitops.h>
  27. #include "soc.h"
  28. #include "clock.h"
  29. #include "clockdomain.h"
  30. /* clkdm_list contains all registered struct clockdomains */
  31. static LIST_HEAD(clkdm_list);
  32. /* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
  33. static struct clkdm_autodep *autodeps;
  34. static struct clkdm_ops *arch_clkdm;
  35. /* Private functions */
  36. static struct clockdomain *_clkdm_lookup(const char *name)
  37. {
  38. struct clockdomain *clkdm, *temp_clkdm;
  39. if (!name)
  40. return NULL;
  41. clkdm = NULL;
  42. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  43. if (!strcmp(name, temp_clkdm->name)) {
  44. clkdm = temp_clkdm;
  45. break;
  46. }
  47. }
  48. return clkdm;
  49. }
  50. /**
  51. * _clkdm_register - register a clockdomain
  52. * @clkdm: struct clockdomain * to register
  53. *
  54. * Adds a clockdomain to the internal clockdomain list.
  55. * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
  56. * already registered by the provided name, or 0 upon success.
  57. */
  58. static int _clkdm_register(struct clockdomain *clkdm)
  59. {
  60. struct powerdomain *pwrdm;
  61. if (!clkdm || !clkdm->name)
  62. return -EINVAL;
  63. pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
  64. if (!pwrdm) {
  65. pr_err("clockdomain: %s: powerdomain %s does not exist\n",
  66. clkdm->name, clkdm->pwrdm.name);
  67. return -EINVAL;
  68. }
  69. clkdm->pwrdm.ptr = pwrdm;
  70. /* Verify that the clockdomain is not already registered */
  71. if (_clkdm_lookup(clkdm->name))
  72. return -EEXIST;
  73. list_add(&clkdm->node, &clkdm_list);
  74. pwrdm_add_clkdm(pwrdm, clkdm);
  75. pr_debug("clockdomain: registered %s\n", clkdm->name);
  76. return 0;
  77. }
  78. /* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
  79. static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
  80. struct clkdm_dep *deps)
  81. {
  82. struct clkdm_dep *cd;
  83. if (!clkdm || !deps)
  84. return ERR_PTR(-EINVAL);
  85. for (cd = deps; cd->clkdm_name; cd++) {
  86. if (!cd->clkdm && cd->clkdm_name)
  87. cd->clkdm = _clkdm_lookup(cd->clkdm_name);
  88. if (cd->clkdm == clkdm)
  89. break;
  90. }
  91. if (!cd->clkdm_name)
  92. return ERR_PTR(-ENOENT);
  93. return cd;
  94. }
  95. /**
  96. * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
  97. * @autodep: struct clkdm_autodep * to resolve
  98. *
  99. * Resolve autodep clockdomain names to clockdomain pointers via
  100. * clkdm_lookup() and store the pointers in the autodep structure. An
  101. * "autodep" is a clockdomain sleep/wakeup dependency that is
  102. * automatically added and removed whenever clocks in the associated
  103. * clockdomain are enabled or disabled (respectively) when the
  104. * clockdomain is in hardware-supervised mode. Meant to be called
  105. * once at clockdomain layer initialization, since these should remain
  106. * fixed for a particular architecture. No return value.
  107. *
  108. * XXX autodeps are deprecated and should be removed at the earliest
  109. * opportunity
  110. */
  111. static void _autodep_lookup(struct clkdm_autodep *autodep)
  112. {
  113. struct clockdomain *clkdm;
  114. if (!autodep)
  115. return;
  116. clkdm = clkdm_lookup(autodep->clkdm.name);
  117. if (!clkdm) {
  118. pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
  119. autodep->clkdm.name);
  120. clkdm = ERR_PTR(-ENOENT);
  121. }
  122. autodep->clkdm.ptr = clkdm;
  123. }
  124. /**
  125. * _resolve_clkdm_deps() - resolve clkdm_names in @clkdm_deps to clkdms
  126. * @clkdm: clockdomain that we are resolving dependencies for
  127. * @clkdm_deps: ptr to array of struct clkdm_deps to resolve
  128. *
  129. * Iterates through @clkdm_deps, looking up the struct clockdomain named by
  130. * clkdm_name and storing the clockdomain pointer in the struct clkdm_dep.
  131. * No return value.
  132. */
  133. static void _resolve_clkdm_deps(struct clockdomain *clkdm,
  134. struct clkdm_dep *clkdm_deps)
  135. {
  136. struct clkdm_dep *cd;
  137. for (cd = clkdm_deps; cd && cd->clkdm_name; cd++) {
  138. if (cd->clkdm)
  139. continue;
  140. cd->clkdm = _clkdm_lookup(cd->clkdm_name);
  141. WARN(!cd->clkdm, "clockdomain: %s: could not find clkdm %s while resolving dependencies - should never happen",
  142. clkdm->name, cd->clkdm_name);
  143. }
  144. }
  145. /**
  146. * _clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1 (lockless)
  147. * @clkdm1: wake this struct clockdomain * up (dependent)
  148. * @clkdm2: when this struct clockdomain * wakes up (source)
  149. *
  150. * When the clockdomain represented by @clkdm2 wakes up, wake up
  151. * @clkdm1. Implemented in hardware on the OMAP, this feature is
  152. * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
  153. * Returns -EINVAL if presented with invalid clockdomain pointers,
  154. * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
  155. * success.
  156. */
  157. static int _clkdm_add_wkdep(struct clockdomain *clkdm1,
  158. struct clockdomain *clkdm2)
  159. {
  160. struct clkdm_dep *cd;
  161. int ret = 0;
  162. if (!clkdm1 || !clkdm2)
  163. return -EINVAL;
  164. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  165. if (IS_ERR(cd))
  166. ret = PTR_ERR(cd);
  167. if (!arch_clkdm || !arch_clkdm->clkdm_add_wkdep)
  168. ret = -EINVAL;
  169. if (ret) {
  170. pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
  171. clkdm1->name, clkdm2->name);
  172. return ret;
  173. }
  174. cd->wkdep_usecount++;
  175. if (cd->wkdep_usecount == 1) {
  176. pr_debug("clockdomain: hardware will wake up %s when %s wakes up\n",
  177. clkdm1->name, clkdm2->name);
  178. ret = arch_clkdm->clkdm_add_wkdep(clkdm1, clkdm2);
  179. }
  180. return ret;
  181. }
  182. /**
  183. * _clkdm_del_wkdep - remove a wakeup dep from clkdm2 to clkdm1 (lockless)
  184. * @clkdm1: wake this struct clockdomain * up (dependent)
  185. * @clkdm2: when this struct clockdomain * wakes up (source)
  186. *
  187. * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
  188. * wakes up. Returns -EINVAL if presented with invalid clockdomain
  189. * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
  190. * 0 upon success.
  191. */
  192. static int _clkdm_del_wkdep(struct clockdomain *clkdm1,
  193. struct clockdomain *clkdm2)
  194. {
  195. struct clkdm_dep *cd;
  196. int ret = 0;
  197. if (!clkdm1 || !clkdm2)
  198. return -EINVAL;
  199. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  200. if (IS_ERR(cd))
  201. ret = PTR_ERR(cd);
  202. if (!arch_clkdm || !arch_clkdm->clkdm_del_wkdep)
  203. ret = -EINVAL;
  204. if (ret) {
  205. pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
  206. clkdm1->name, clkdm2->name);
  207. return ret;
  208. }
  209. cd->wkdep_usecount--;
  210. if (cd->wkdep_usecount == 0) {
  211. pr_debug("clockdomain: hardware will no longer wake up %s after %s wakes up\n",
  212. clkdm1->name, clkdm2->name);
  213. ret = arch_clkdm->clkdm_del_wkdep(clkdm1, clkdm2);
  214. }
  215. return ret;
  216. }
  217. /**
  218. * _clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1 (lockless)
  219. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  220. * @clkdm2: when this struct clockdomain * is active (source)
  221. *
  222. * Prevent @clkdm1 from automatically going inactive (and then to
  223. * retention or off) if @clkdm2 is active. Returns -EINVAL if
  224. * presented with invalid clockdomain pointers or called on a machine
  225. * that does not support software-configurable hardware sleep
  226. * dependencies, -ENOENT if the specified dependency cannot be set in
  227. * hardware, or 0 upon success.
  228. */
  229. static int _clkdm_add_sleepdep(struct clockdomain *clkdm1,
  230. struct clockdomain *clkdm2)
  231. {
  232. struct clkdm_dep *cd;
  233. int ret = 0;
  234. if (!clkdm1 || !clkdm2)
  235. return -EINVAL;
  236. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  237. if (IS_ERR(cd))
  238. ret = PTR_ERR(cd);
  239. if (!arch_clkdm || !arch_clkdm->clkdm_add_sleepdep)
  240. ret = -EINVAL;
  241. if (ret) {
  242. pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
  243. clkdm1->name, clkdm2->name);
  244. return ret;
  245. }
  246. cd->sleepdep_usecount++;
  247. if (cd->sleepdep_usecount == 1) {
  248. pr_debug("clockdomain: will prevent %s from sleeping if %s is active\n",
  249. clkdm1->name, clkdm2->name);
  250. ret = arch_clkdm->clkdm_add_sleepdep(clkdm1, clkdm2);
  251. }
  252. return ret;
  253. }
  254. /**
  255. * _clkdm_del_sleepdep - remove a sleep dep from clkdm2 to clkdm1 (lockless)
  256. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  257. * @clkdm2: when this struct clockdomain * is active (source)
  258. *
  259. * Allow @clkdm1 to automatically go inactive (and then to retention or
  260. * off), independent of the activity state of @clkdm2. Returns -EINVAL
  261. * if presented with invalid clockdomain pointers or called on a machine
  262. * that does not support software-configurable hardware sleep dependencies,
  263. * -ENOENT if the specified dependency cannot be cleared in hardware, or
  264. * 0 upon success.
  265. */
  266. static int _clkdm_del_sleepdep(struct clockdomain *clkdm1,
  267. struct clockdomain *clkdm2)
  268. {
  269. struct clkdm_dep *cd;
  270. int ret = 0;
  271. if (!clkdm1 || !clkdm2)
  272. return -EINVAL;
  273. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  274. if (IS_ERR(cd))
  275. ret = PTR_ERR(cd);
  276. if (!arch_clkdm || !arch_clkdm->clkdm_del_sleepdep)
  277. ret = -EINVAL;
  278. if (ret) {
  279. pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
  280. clkdm1->name, clkdm2->name);
  281. return ret;
  282. }
  283. cd->sleepdep_usecount--;
  284. if (cd->sleepdep_usecount == 0) {
  285. pr_debug("clockdomain: will no longer prevent %s from sleeping if %s is active\n",
  286. clkdm1->name, clkdm2->name);
  287. ret = arch_clkdm->clkdm_del_sleepdep(clkdm1, clkdm2);
  288. }
  289. return ret;
  290. }
  291. /* Public functions */
  292. /**
  293. * clkdm_register_platform_funcs - register clockdomain implementation fns
  294. * @co: func pointers for arch specific implementations
  295. *
  296. * Register the list of function pointers used to implement the
  297. * clockdomain functions on different OMAP SoCs. Should be called
  298. * before any other clkdm_register*() function. Returns -EINVAL if
  299. * @co is null, -EEXIST if platform functions have already been
  300. * registered, or 0 upon success.
  301. */
  302. int clkdm_register_platform_funcs(struct clkdm_ops *co)
  303. {
  304. if (!co)
  305. return -EINVAL;
  306. if (arch_clkdm)
  307. return -EEXIST;
  308. arch_clkdm = co;
  309. return 0;
  310. };
  311. /**
  312. * clkdm_register_clkdms - register SoC clockdomains
  313. * @cs: pointer to an array of struct clockdomain to register
  314. *
  315. * Register the clockdomains available on a particular OMAP SoC. Must
  316. * be called after clkdm_register_platform_funcs(). May be called
  317. * multiple times. Returns -EACCES if called before
  318. * clkdm_register_platform_funcs(); -EINVAL if the argument @cs is
  319. * null; or 0 upon success.
  320. */
  321. int clkdm_register_clkdms(struct clockdomain **cs)
  322. {
  323. struct clockdomain **c = NULL;
  324. if (!arch_clkdm)
  325. return -EACCES;
  326. if (!cs)
  327. return -EINVAL;
  328. for (c = cs; *c; c++)
  329. _clkdm_register(*c);
  330. return 0;
  331. }
  332. /**
  333. * clkdm_register_autodeps - register autodeps (if required)
  334. * @ia: pointer to a static array of struct clkdm_autodep to register
  335. *
  336. * Register clockdomain "automatic dependencies." These are
  337. * clockdomain wakeup and sleep dependencies that are automatically
  338. * added whenever the first clock inside a clockdomain is enabled, and
  339. * removed whenever the last clock inside a clockdomain is disabled.
  340. * These are currently only used on OMAP3 devices, and are deprecated,
  341. * since they waste energy. However, until the OMAP2/3 IP block
  342. * enable/disable sequence can be converted to match the OMAP4
  343. * sequence, they are needed.
  344. *
  345. * Must be called only after all of the SoC clockdomains are
  346. * registered, since the function will resolve autodep clockdomain
  347. * names into clockdomain pointers.
  348. *
  349. * The struct clkdm_autodep @ia array must be static, as this function
  350. * does not copy the array elements.
  351. *
  352. * Returns -EACCES if called before any clockdomains have been
  353. * registered, -EINVAL if called with a null @ia argument, -EEXIST if
  354. * autodeps have already been registered, or 0 upon success.
  355. */
  356. int clkdm_register_autodeps(struct clkdm_autodep *ia)
  357. {
  358. struct clkdm_autodep *a = NULL;
  359. if (list_empty(&clkdm_list))
  360. return -EACCES;
  361. if (!ia)
  362. return -EINVAL;
  363. if (autodeps)
  364. return -EEXIST;
  365. autodeps = ia;
  366. for (a = autodeps; a->clkdm.ptr; a++)
  367. _autodep_lookup(a);
  368. return 0;
  369. }
  370. /**
  371. * clkdm_complete_init - set up the clockdomain layer
  372. *
  373. * Put all clockdomains into software-supervised mode; PM code should
  374. * later enable hardware-supervised mode as appropriate. Must be
  375. * called after clkdm_register_clkdms(). Returns -EACCES if called
  376. * before clkdm_register_clkdms(), or 0 upon success.
  377. */
  378. int clkdm_complete_init(void)
  379. {
  380. struct clockdomain *clkdm;
  381. if (list_empty(&clkdm_list))
  382. return -EACCES;
  383. list_for_each_entry(clkdm, &clkdm_list, node) {
  384. if (clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)
  385. clkdm_wakeup(clkdm);
  386. else if (clkdm->flags & CLKDM_CAN_DISABLE_AUTO)
  387. clkdm_deny_idle(clkdm);
  388. _resolve_clkdm_deps(clkdm, clkdm->wkdep_srcs);
  389. clkdm_clear_all_wkdeps(clkdm);
  390. _resolve_clkdm_deps(clkdm, clkdm->sleepdep_srcs);
  391. clkdm_clear_all_sleepdeps(clkdm);
  392. }
  393. return 0;
  394. }
  395. /**
  396. * clkdm_lookup - look up a clockdomain by name, return a pointer
  397. * @name: name of clockdomain
  398. *
  399. * Find a registered clockdomain by its name @name. Returns a pointer
  400. * to the struct clockdomain if found, or NULL otherwise.
  401. */
  402. struct clockdomain *clkdm_lookup(const char *name)
  403. {
  404. struct clockdomain *clkdm, *temp_clkdm;
  405. if (!name)
  406. return NULL;
  407. clkdm = NULL;
  408. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  409. if (!strcmp(name, temp_clkdm->name)) {
  410. clkdm = temp_clkdm;
  411. break;
  412. }
  413. }
  414. return clkdm;
  415. }
  416. /**
  417. * clkdm_for_each - call function on each registered clockdomain
  418. * @fn: callback function *
  419. *
  420. * Call the supplied function @fn for each registered clockdomain.
  421. * The callback function @fn can return anything but 0 to bail
  422. * out early from the iterator. The callback function is called with
  423. * the clkdm_mutex held, so no clockdomain structure manipulation
  424. * functions should be called from the callback, although hardware
  425. * clockdomain control functions are fine. Returns the last return
  426. * value of the callback function, which should be 0 for success or
  427. * anything else to indicate failure; or -EINVAL if the function pointer
  428. * is null.
  429. */
  430. int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
  431. void *user)
  432. {
  433. struct clockdomain *clkdm;
  434. int ret = 0;
  435. if (!fn)
  436. return -EINVAL;
  437. list_for_each_entry(clkdm, &clkdm_list, node) {
  438. ret = (*fn)(clkdm, user);
  439. if (ret)
  440. break;
  441. }
  442. return ret;
  443. }
  444. /**
  445. * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
  446. * @clkdm: struct clockdomain *
  447. *
  448. * Return a pointer to the struct powerdomain that the specified clockdomain
  449. * @clkdm exists in, or returns NULL if @clkdm is NULL.
  450. */
  451. struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
  452. {
  453. if (!clkdm)
  454. return NULL;
  455. return clkdm->pwrdm.ptr;
  456. }
  457. /* Hardware clockdomain control */
  458. /**
  459. * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
  460. * @clkdm1: wake this struct clockdomain * up (dependent)
  461. * @clkdm2: when this struct clockdomain * wakes up (source)
  462. *
  463. * When the clockdomain represented by @clkdm2 wakes up, wake up
  464. * @clkdm1. Implemented in hardware on the OMAP, this feature is
  465. * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
  466. * Returns -EINVAL if presented with invalid clockdomain pointers,
  467. * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
  468. * success.
  469. */
  470. int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  471. {
  472. struct clkdm_dep *cd;
  473. int ret;
  474. if (!clkdm1 || !clkdm2)
  475. return -EINVAL;
  476. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  477. if (IS_ERR(cd))
  478. return PTR_ERR(cd);
  479. pwrdm_lock(cd->clkdm->pwrdm.ptr);
  480. ret = _clkdm_add_wkdep(clkdm1, clkdm2);
  481. pwrdm_unlock(cd->clkdm->pwrdm.ptr);
  482. return ret;
  483. }
  484. /**
  485. * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
  486. * @clkdm1: wake this struct clockdomain * up (dependent)
  487. * @clkdm2: when this struct clockdomain * wakes up (source)
  488. *
  489. * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
  490. * wakes up. Returns -EINVAL if presented with invalid clockdomain
  491. * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
  492. * 0 upon success.
  493. */
  494. int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  495. {
  496. struct clkdm_dep *cd;
  497. int ret;
  498. if (!clkdm1 || !clkdm2)
  499. return -EINVAL;
  500. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  501. if (IS_ERR(cd))
  502. return PTR_ERR(cd);
  503. pwrdm_lock(cd->clkdm->pwrdm.ptr);
  504. ret = _clkdm_del_wkdep(clkdm1, clkdm2);
  505. pwrdm_unlock(cd->clkdm->pwrdm.ptr);
  506. return ret;
  507. }
  508. /**
  509. * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
  510. * @clkdm1: wake this struct clockdomain * up (dependent)
  511. * @clkdm2: when this struct clockdomain * wakes up (source)
  512. *
  513. * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
  514. * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
  515. * if either clockdomain pointer is invalid; or -ENOENT if the hardware
  516. * is incapable.
  517. *
  518. * REVISIT: Currently this function only represents software-controllable
  519. * wakeup dependencies. Wakeup dependencies fixed in hardware are not
  520. * yet handled here.
  521. */
  522. int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  523. {
  524. struct clkdm_dep *cd;
  525. int ret = 0;
  526. if (!clkdm1 || !clkdm2)
  527. return -EINVAL;
  528. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  529. if (IS_ERR(cd))
  530. ret = PTR_ERR(cd);
  531. if (!arch_clkdm || !arch_clkdm->clkdm_read_wkdep)
  532. ret = -EINVAL;
  533. if (ret) {
  534. pr_debug("clockdomain: hardware cannot set/clear wake up of %s when %s wakes up\n",
  535. clkdm1->name, clkdm2->name);
  536. return ret;
  537. }
  538. /* XXX It's faster to return the wkdep_usecount */
  539. return arch_clkdm->clkdm_read_wkdep(clkdm1, clkdm2);
  540. }
  541. /**
  542. * clkdm_clear_all_wkdeps - remove all wakeup dependencies from target clkdm
  543. * @clkdm: struct clockdomain * to remove all wakeup dependencies from
  544. *
  545. * Remove all inter-clockdomain wakeup dependencies that could cause
  546. * @clkdm to wake. Intended to be used during boot to initialize the
  547. * PRCM to a known state, after all clockdomains are put into swsup idle
  548. * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
  549. * 0 upon success.
  550. */
  551. int clkdm_clear_all_wkdeps(struct clockdomain *clkdm)
  552. {
  553. if (!clkdm)
  554. return -EINVAL;
  555. if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_wkdeps)
  556. return -EINVAL;
  557. return arch_clkdm->clkdm_clear_all_wkdeps(clkdm);
  558. }
  559. /**
  560. * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
  561. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  562. * @clkdm2: when this struct clockdomain * is active (source)
  563. *
  564. * Prevent @clkdm1 from automatically going inactive (and then to
  565. * retention or off) if @clkdm2 is active. Returns -EINVAL if
  566. * presented with invalid clockdomain pointers or called on a machine
  567. * that does not support software-configurable hardware sleep
  568. * dependencies, -ENOENT if the specified dependency cannot be set in
  569. * hardware, or 0 upon success.
  570. */
  571. int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  572. {
  573. struct clkdm_dep *cd;
  574. int ret;
  575. if (!clkdm1 || !clkdm2)
  576. return -EINVAL;
  577. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  578. if (IS_ERR(cd))
  579. return PTR_ERR(cd);
  580. pwrdm_lock(cd->clkdm->pwrdm.ptr);
  581. ret = _clkdm_add_sleepdep(clkdm1, clkdm2);
  582. pwrdm_unlock(cd->clkdm->pwrdm.ptr);
  583. return ret;
  584. }
  585. /**
  586. * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
  587. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  588. * @clkdm2: when this struct clockdomain * is active (source)
  589. *
  590. * Allow @clkdm1 to automatically go inactive (and then to retention or
  591. * off), independent of the activity state of @clkdm2. Returns -EINVAL
  592. * if presented with invalid clockdomain pointers or called on a machine
  593. * that does not support software-configurable hardware sleep dependencies,
  594. * -ENOENT if the specified dependency cannot be cleared in hardware, or
  595. * 0 upon success.
  596. */
  597. int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  598. {
  599. struct clkdm_dep *cd;
  600. int ret;
  601. if (!clkdm1 || !clkdm2)
  602. return -EINVAL;
  603. cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
  604. if (IS_ERR(cd))
  605. return PTR_ERR(cd);
  606. pwrdm_lock(cd->clkdm->pwrdm.ptr);
  607. ret = _clkdm_del_sleepdep(clkdm1, clkdm2);
  608. pwrdm_unlock(cd->clkdm->pwrdm.ptr);
  609. return ret;
  610. }
  611. /**
  612. * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
  613. * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
  614. * @clkdm2: when this struct clockdomain * is active (source)
  615. *
  616. * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
  617. * not be allowed to automatically go inactive if @clkdm2 is active;
  618. * 0 if @clkdm1's automatic power state inactivity transition is independent
  619. * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
  620. * on a machine that does not support software-configurable hardware sleep
  621. * dependencies; or -ENOENT if the hardware is incapable.
  622. *
  623. * REVISIT: Currently this function only represents software-controllable
  624. * sleep dependencies. Sleep dependencies fixed in hardware are not
  625. * yet handled here.
  626. */
  627. int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
  628. {
  629. struct clkdm_dep *cd;
  630. int ret = 0;
  631. if (!clkdm1 || !clkdm2)
  632. return -EINVAL;
  633. cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
  634. if (IS_ERR(cd))
  635. ret = PTR_ERR(cd);
  636. if (!arch_clkdm || !arch_clkdm->clkdm_read_sleepdep)
  637. ret = -EINVAL;
  638. if (ret) {
  639. pr_debug("clockdomain: hardware cannot set/clear sleep dependency affecting %s from %s\n",
  640. clkdm1->name, clkdm2->name);
  641. return ret;
  642. }
  643. /* XXX It's faster to return the sleepdep_usecount */
  644. return arch_clkdm->clkdm_read_sleepdep(clkdm1, clkdm2);
  645. }
  646. /**
  647. * clkdm_clear_all_sleepdeps - remove all sleep dependencies from target clkdm
  648. * @clkdm: struct clockdomain * to remove all sleep dependencies from
  649. *
  650. * Remove all inter-clockdomain sleep dependencies that could prevent
  651. * @clkdm from idling. Intended to be used during boot to initialize the
  652. * PRCM to a known state, after all clockdomains are put into swsup idle
  653. * and woken up. Returns -EINVAL if @clkdm pointer is invalid, or
  654. * 0 upon success.
  655. */
  656. int clkdm_clear_all_sleepdeps(struct clockdomain *clkdm)
  657. {
  658. if (!clkdm)
  659. return -EINVAL;
  660. if (!arch_clkdm || !arch_clkdm->clkdm_clear_all_sleepdeps)
  661. return -EINVAL;
  662. return arch_clkdm->clkdm_clear_all_sleepdeps(clkdm);
  663. }
  664. /**
  665. * clkdm_sleep_nolock - force clockdomain sleep transition (lockless)
  666. * @clkdm: struct clockdomain *
  667. *
  668. * Instruct the CM to force a sleep transition on the specified
  669. * clockdomain @clkdm. Only for use by the powerdomain code. Returns
  670. * -EINVAL if @clkdm is NULL or if clockdomain does not support
  671. * software-initiated sleep; 0 upon success.
  672. */
  673. int clkdm_sleep_nolock(struct clockdomain *clkdm)
  674. {
  675. int ret;
  676. if (!clkdm)
  677. return -EINVAL;
  678. if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
  679. pr_debug("clockdomain: %s does not support forcing sleep via software\n",
  680. clkdm->name);
  681. return -EINVAL;
  682. }
  683. if (!arch_clkdm || !arch_clkdm->clkdm_sleep)
  684. return -EINVAL;
  685. pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
  686. clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
  687. ret = arch_clkdm->clkdm_sleep(clkdm);
  688. ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
  689. return ret;
  690. }
  691. /**
  692. * clkdm_sleep - force clockdomain sleep transition
  693. * @clkdm: struct clockdomain *
  694. *
  695. * Instruct the CM to force a sleep transition on the specified
  696. * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if
  697. * clockdomain does not support software-initiated sleep; 0 upon
  698. * success.
  699. */
  700. int clkdm_sleep(struct clockdomain *clkdm)
  701. {
  702. int ret;
  703. pwrdm_lock(clkdm->pwrdm.ptr);
  704. ret = clkdm_sleep_nolock(clkdm);
  705. pwrdm_unlock(clkdm->pwrdm.ptr);
  706. return ret;
  707. }
  708. /**
  709. * clkdm_wakeup_nolock - force clockdomain wakeup transition (lockless)
  710. * @clkdm: struct clockdomain *
  711. *
  712. * Instruct the CM to force a wakeup transition on the specified
  713. * clockdomain @clkdm. Only for use by the powerdomain code. Returns
  714. * -EINVAL if @clkdm is NULL or if the clockdomain does not support
  715. * software-controlled wakeup; 0 upon success.
  716. */
  717. int clkdm_wakeup_nolock(struct clockdomain *clkdm)
  718. {
  719. int ret;
  720. if (!clkdm)
  721. return -EINVAL;
  722. if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
  723. pr_debug("clockdomain: %s does not support forcing wakeup via software\n",
  724. clkdm->name);
  725. return -EINVAL;
  726. }
  727. if (!arch_clkdm || !arch_clkdm->clkdm_wakeup)
  728. return -EINVAL;
  729. pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
  730. clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
  731. ret = arch_clkdm->clkdm_wakeup(clkdm);
  732. ret |= pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
  733. return ret;
  734. }
  735. /**
  736. * clkdm_wakeup - force clockdomain wakeup transition
  737. * @clkdm: struct clockdomain *
  738. *
  739. * Instruct the CM to force a wakeup transition on the specified
  740. * clockdomain @clkdm. Returns -EINVAL if @clkdm is NULL or if the
  741. * clockdomain does not support software-controlled wakeup; 0 upon
  742. * success.
  743. */
  744. int clkdm_wakeup(struct clockdomain *clkdm)
  745. {
  746. int ret;
  747. pwrdm_lock(clkdm->pwrdm.ptr);
  748. ret = clkdm_wakeup_nolock(clkdm);
  749. pwrdm_unlock(clkdm->pwrdm.ptr);
  750. return ret;
  751. }
  752. /**
  753. * clkdm_allow_idle_nolock - enable hwsup idle transitions for clkdm
  754. * @clkdm: struct clockdomain *
  755. *
  756. * Allow the hardware to automatically switch the clockdomain @clkdm
  757. * into active or idle states, as needed by downstream clocks. If the
  758. * clockdomain has any downstream clocks enabled in the clock
  759. * framework, wkdep/sleepdep autodependencies are added; this is so
  760. * device drivers can read and write to the device. Only for use by
  761. * the powerdomain code. No return value.
  762. */
  763. void clkdm_allow_idle_nolock(struct clockdomain *clkdm)
  764. {
  765. if (!clkdm)
  766. return;
  767. if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
  768. pr_debug("clock: %s: automatic idle transitions cannot be enabled\n",
  769. clkdm->name);
  770. return;
  771. }
  772. if (!arch_clkdm || !arch_clkdm->clkdm_allow_idle)
  773. return;
  774. pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
  775. clkdm->name);
  776. clkdm->_flags |= _CLKDM_FLAG_HWSUP_ENABLED;
  777. arch_clkdm->clkdm_allow_idle(clkdm);
  778. pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
  779. }
  780. /**
  781. * clkdm_allow_idle - enable hwsup idle transitions for clkdm
  782. * @clkdm: struct clockdomain *
  783. *
  784. * Allow the hardware to automatically switch the clockdomain @clkdm into
  785. * active or idle states, as needed by downstream clocks. If the
  786. * clockdomain has any downstream clocks enabled in the clock
  787. * framework, wkdep/sleepdep autodependencies are added; this is so
  788. * device drivers can read and write to the device. No return value.
  789. */
  790. void clkdm_allow_idle(struct clockdomain *clkdm)
  791. {
  792. pwrdm_lock(clkdm->pwrdm.ptr);
  793. clkdm_allow_idle_nolock(clkdm);
  794. pwrdm_unlock(clkdm->pwrdm.ptr);
  795. }
  796. /**
  797. * clkdm_deny_idle - disable hwsup idle transitions for clkdm
  798. * @clkdm: struct clockdomain *
  799. *
  800. * Prevent the hardware from automatically switching the clockdomain
  801. * @clkdm into inactive or idle states. If the clockdomain has
  802. * downstream clocks enabled in the clock framework, wkdep/sleepdep
  803. * autodependencies are removed. Only for use by the powerdomain
  804. * code. No return value.
  805. */
  806. void clkdm_deny_idle_nolock(struct clockdomain *clkdm)
  807. {
  808. if (!clkdm)
  809. return;
  810. if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
  811. pr_debug("clockdomain: %s: automatic idle transitions cannot be disabled\n",
  812. clkdm->name);
  813. return;
  814. }
  815. if (!arch_clkdm || !arch_clkdm->clkdm_deny_idle)
  816. return;
  817. pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
  818. clkdm->name);
  819. clkdm->_flags &= ~_CLKDM_FLAG_HWSUP_ENABLED;
  820. arch_clkdm->clkdm_deny_idle(clkdm);
  821. pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
  822. }
  823. /**
  824. * clkdm_deny_idle - disable hwsup idle transitions for clkdm
  825. * @clkdm: struct clockdomain *
  826. *
  827. * Prevent the hardware from automatically switching the clockdomain
  828. * @clkdm into inactive or idle states. If the clockdomain has
  829. * downstream clocks enabled in the clock framework, wkdep/sleepdep
  830. * autodependencies are removed. No return value.
  831. */
  832. void clkdm_deny_idle(struct clockdomain *clkdm)
  833. {
  834. pwrdm_lock(clkdm->pwrdm.ptr);
  835. clkdm_deny_idle_nolock(clkdm);
  836. pwrdm_unlock(clkdm->pwrdm.ptr);
  837. }
  838. /**
  839. * clkdm_in_hwsup - is clockdomain @clkdm have hardware-supervised idle enabled?
  840. * @clkdm: struct clockdomain *
  841. *
  842. * Returns true if clockdomain @clkdm currently has
  843. * hardware-supervised idle enabled, or false if it does not or if
  844. * @clkdm is NULL. It is only valid to call this function after
  845. * clkdm_init() has been called. This function does not actually read
  846. * bits from the hardware; it instead tests an in-memory flag that is
  847. * changed whenever the clockdomain code changes the auto-idle mode.
  848. */
  849. bool clkdm_in_hwsup(struct clockdomain *clkdm)
  850. {
  851. bool ret;
  852. if (!clkdm)
  853. return false;
  854. ret = (clkdm->_flags & _CLKDM_FLAG_HWSUP_ENABLED) ? true : false;
  855. return ret;
  856. }
  857. /**
  858. * clkdm_missing_idle_reporting - can @clkdm enter autoidle even if in use?
  859. * @clkdm: struct clockdomain *
  860. *
  861. * Returns true if clockdomain @clkdm has the
  862. * CLKDM_MISSING_IDLE_REPORTING flag set, or false if not or @clkdm is
  863. * null. More information is available in the documentation for the
  864. * CLKDM_MISSING_IDLE_REPORTING macro.
  865. */
  866. bool clkdm_missing_idle_reporting(struct clockdomain *clkdm)
  867. {
  868. if (!clkdm)
  869. return false;
  870. return (clkdm->flags & CLKDM_MISSING_IDLE_REPORTING) ? true : false;
  871. }
  872. /* Public autodep handling functions (deprecated) */
  873. /**
  874. * clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
  875. * @clkdm: struct clockdomain *
  876. *
  877. * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
  878. * in hardware-supervised mode. Meant to be called from clock framework
  879. * when a clock inside clockdomain 'clkdm' is enabled. No return value.
  880. *
  881. * XXX autodeps are deprecated and should be removed at the earliest
  882. * opportunity
  883. */
  884. void clkdm_add_autodeps(struct clockdomain *clkdm)
  885. {
  886. struct clkdm_autodep *autodep;
  887. if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
  888. return;
  889. for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
  890. if (IS_ERR(autodep->clkdm.ptr))
  891. continue;
  892. pr_debug("clockdomain: %s: adding %s sleepdep/wkdep\n",
  893. clkdm->name, autodep->clkdm.ptr->name);
  894. _clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
  895. _clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
  896. }
  897. }
  898. /**
  899. * clkdm_del_autodeps - remove auto sleepdeps/wkdeps from clkdm
  900. * @clkdm: struct clockdomain *
  901. *
  902. * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
  903. * in hardware-supervised mode. Meant to be called from clock framework
  904. * when a clock inside clockdomain 'clkdm' is disabled. No return value.
  905. *
  906. * XXX autodeps are deprecated and should be removed at the earliest
  907. * opportunity
  908. */
  909. void clkdm_del_autodeps(struct clockdomain *clkdm)
  910. {
  911. struct clkdm_autodep *autodep;
  912. if (!autodeps || clkdm->flags & CLKDM_NO_AUTODEPS)
  913. return;
  914. for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
  915. if (IS_ERR(autodep->clkdm.ptr))
  916. continue;
  917. pr_debug("clockdomain: %s: removing %s sleepdep/wkdep\n",
  918. clkdm->name, autodep->clkdm.ptr->name);
  919. _clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
  920. _clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
  921. }
  922. }
  923. /* Clockdomain-to-clock/hwmod framework interface code */
  924. static int _clkdm_clk_hwmod_enable(struct clockdomain *clkdm)
  925. {
  926. if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_enable)
  927. return -EINVAL;
  928. pwrdm_lock(clkdm->pwrdm.ptr);
  929. /*
  930. * For arch's with no autodeps, clkcm_clk_enable
  931. * should be called for every clock instance or hwmod that is
  932. * enabled, so the clkdm can be force woken up.
  933. */
  934. clkdm->usecount++;
  935. if (clkdm->usecount > 1 && autodeps) {
  936. pwrdm_unlock(clkdm->pwrdm.ptr);
  937. return 0;
  938. }
  939. arch_clkdm->clkdm_clk_enable(clkdm);
  940. pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
  941. pwrdm_unlock(clkdm->pwrdm.ptr);
  942. pr_debug("clockdomain: %s: enabled\n", clkdm->name);
  943. return 0;
  944. }
  945. /**
  946. * clkdm_clk_enable - add an enabled downstream clock to this clkdm
  947. * @clkdm: struct clockdomain *
  948. * @clk: struct clk * of the enabled downstream clock
  949. *
  950. * Increment the usecount of the clockdomain @clkdm and ensure that it
  951. * is awake before @clk is enabled. Intended to be called by
  952. * clk_enable() code. If the clockdomain is in software-supervised
  953. * idle mode, force the clockdomain to wake. If the clockdomain is in
  954. * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
  955. * ensure that devices in the clockdomain can be read from/written to
  956. * by on-chip processors. Returns -EINVAL if passed null pointers;
  957. * returns 0 upon success or if the clockdomain is in hwsup idle mode.
  958. */
  959. int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
  960. {
  961. /*
  962. * XXX Rewrite this code to maintain a list of enabled
  963. * downstream clocks for debugging purposes?
  964. */
  965. if (!clk)
  966. return -EINVAL;
  967. return _clkdm_clk_hwmod_enable(clkdm);
  968. }
  969. /**
  970. * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
  971. * @clkdm: struct clockdomain *
  972. * @clk: struct clk * of the disabled downstream clock
  973. *
  974. * Decrement the usecount of this clockdomain @clkdm when @clk is
  975. * disabled. Intended to be called by clk_disable() code. If the
  976. * clockdomain usecount goes to 0, put the clockdomain to sleep
  977. * (software-supervised mode) or remove the clkdm autodependencies
  978. * (hardware-supervised mode). Returns -EINVAL if passed null
  979. * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
  980. * upon success or if the clockdomain is in hwsup idle mode.
  981. */
  982. int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
  983. {
  984. if (!clkdm || !clk || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
  985. return -EINVAL;
  986. pwrdm_lock(clkdm->pwrdm.ptr);
  987. /* corner case: disabling unused clocks */
  988. if ((__clk_get_enable_count(clk) == 0) && clkdm->usecount == 0)
  989. goto ccd_exit;
  990. if (clkdm->usecount == 0) {
  991. pwrdm_unlock(clkdm->pwrdm.ptr);
  992. WARN_ON(1); /* underflow */
  993. return -ERANGE;
  994. }
  995. clkdm->usecount--;
  996. if (clkdm->usecount > 0) {
  997. pwrdm_unlock(clkdm->pwrdm.ptr);
  998. return 0;
  999. }
  1000. arch_clkdm->clkdm_clk_disable(clkdm);
  1001. pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
  1002. pr_debug("clockdomain: %s: disabled\n", clkdm->name);
  1003. ccd_exit:
  1004. pwrdm_unlock(clkdm->pwrdm.ptr);
  1005. return 0;
  1006. }
  1007. /**
  1008. * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
  1009. * @clkdm: struct clockdomain *
  1010. * @oh: struct omap_hwmod * of the enabled downstream hwmod
  1011. *
  1012. * Increment the usecount of the clockdomain @clkdm and ensure that it
  1013. * is awake before @oh is enabled. Intended to be called by
  1014. * module_enable() code.
  1015. * If the clockdomain is in software-supervised idle mode, force the
  1016. * clockdomain to wake. If the clockdomain is in hardware-supervised idle
  1017. * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
  1018. * clockdomain can be read from/written to by on-chip processors.
  1019. * Returns -EINVAL if passed null pointers;
  1020. * returns 0 upon success or if the clockdomain is in hwsup idle mode.
  1021. */
  1022. int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
  1023. {
  1024. /* The clkdm attribute does not exist yet prior OMAP4 */
  1025. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  1026. return 0;
  1027. /*
  1028. * XXX Rewrite this code to maintain a list of enabled
  1029. * downstream hwmods for debugging purposes?
  1030. */
  1031. if (!oh)
  1032. return -EINVAL;
  1033. return _clkdm_clk_hwmod_enable(clkdm);
  1034. }
  1035. /**
  1036. * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
  1037. * @clkdm: struct clockdomain *
  1038. * @oh: struct omap_hwmod * of the disabled downstream hwmod
  1039. *
  1040. * Decrement the usecount of this clockdomain @clkdm when @oh is
  1041. * disabled. Intended to be called by module_disable() code.
  1042. * If the clockdomain usecount goes to 0, put the clockdomain to sleep
  1043. * (software-supervised mode) or remove the clkdm autodependencies
  1044. * (hardware-supervised mode).
  1045. * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
  1046. * underflows; or returns 0 upon success or if the clockdomain is in hwsup
  1047. * idle mode.
  1048. */
  1049. int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
  1050. {
  1051. /* The clkdm attribute does not exist yet prior OMAP4 */
  1052. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  1053. return 0;
  1054. /*
  1055. * XXX Rewrite this code to maintain a list of enabled
  1056. * downstream hwmods for debugging purposes?
  1057. */
  1058. if (!clkdm || !oh || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
  1059. return -EINVAL;
  1060. pwrdm_lock(clkdm->pwrdm.ptr);
  1061. if (clkdm->usecount == 0) {
  1062. pwrdm_unlock(clkdm->pwrdm.ptr);
  1063. WARN_ON(1); /* underflow */
  1064. return -ERANGE;
  1065. }
  1066. clkdm->usecount--;
  1067. if (clkdm->usecount > 0) {
  1068. pwrdm_unlock(clkdm->pwrdm.ptr);
  1069. return 0;
  1070. }
  1071. arch_clkdm->clkdm_clk_disable(clkdm);
  1072. pwrdm_state_switch_nolock(clkdm->pwrdm.ptr);
  1073. pwrdm_unlock(clkdm->pwrdm.ptr);
  1074. pr_debug("clockdomain: %s: disabled\n", clkdm->name);
  1075. return 0;
  1076. }