clockdomain.c 30 KB

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