clockdomain.c 31 KB

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