clockdomain.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. /*
  765. * For arch's with no autodeps, clkcm_clk_enable
  766. * should be called for every clock instance or hwmod that is
  767. * enabled, so the clkdm can be force woken up.
  768. */
  769. if ((atomic_inc_return(&clkdm->usecount) > 1) && autodeps)
  770. return 0;
  771. spin_lock_irqsave(&clkdm->lock, flags);
  772. arch_clkdm->clkdm_clk_enable(clkdm);
  773. pwrdm_state_switch(clkdm->pwrdm.ptr);
  774. spin_unlock_irqrestore(&clkdm->lock, flags);
  775. pr_debug("clockdomain: %s: enabled\n", clkdm->name);
  776. return 0;
  777. }
  778. static int _clkdm_clk_hwmod_disable(struct clockdomain *clkdm)
  779. {
  780. unsigned long flags;
  781. if (!clkdm || !arch_clkdm || !arch_clkdm->clkdm_clk_disable)
  782. return -EINVAL;
  783. if (atomic_read(&clkdm->usecount) == 0) {
  784. WARN_ON(1); /* underflow */
  785. return -ERANGE;
  786. }
  787. if (atomic_dec_return(&clkdm->usecount) > 0)
  788. return 0;
  789. spin_lock_irqsave(&clkdm->lock, flags);
  790. arch_clkdm->clkdm_clk_disable(clkdm);
  791. pwrdm_state_switch(clkdm->pwrdm.ptr);
  792. spin_unlock_irqrestore(&clkdm->lock, flags);
  793. pr_debug("clockdomain: %s: disabled\n", clkdm->name);
  794. return 0;
  795. }
  796. /**
  797. * clkdm_clk_enable - add an enabled downstream clock to this clkdm
  798. * @clkdm: struct clockdomain *
  799. * @clk: struct clk * of the enabled downstream clock
  800. *
  801. * Increment the usecount of the clockdomain @clkdm and ensure that it
  802. * is awake before @clk is enabled. Intended to be called by
  803. * clk_enable() code. If the clockdomain is in software-supervised
  804. * idle mode, force the clockdomain to wake. If the clockdomain is in
  805. * hardware-supervised idle mode, add clkdm-pwrdm autodependencies, to
  806. * ensure that devices in the clockdomain can be read from/written to
  807. * by on-chip processors. Returns -EINVAL if passed null pointers;
  808. * returns 0 upon success or if the clockdomain is in hwsup idle mode.
  809. */
  810. int clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
  811. {
  812. /*
  813. * XXX Rewrite this code to maintain a list of enabled
  814. * downstream clocks for debugging purposes?
  815. */
  816. if (!clk)
  817. return -EINVAL;
  818. return _clkdm_clk_hwmod_enable(clkdm);
  819. }
  820. /**
  821. * clkdm_clk_disable - remove an enabled downstream clock from this clkdm
  822. * @clkdm: struct clockdomain *
  823. * @clk: struct clk * of the disabled downstream clock
  824. *
  825. * Decrement the usecount of this clockdomain @clkdm when @clk is
  826. * disabled. Intended to be called by clk_disable() code. If the
  827. * clockdomain usecount goes to 0, put the clockdomain to sleep
  828. * (software-supervised mode) or remove the clkdm autodependencies
  829. * (hardware-supervised mode). Returns -EINVAL if passed null
  830. * pointers; -ERANGE if the @clkdm usecount underflows; or returns 0
  831. * upon success or if the clockdomain is in hwsup idle mode.
  832. */
  833. int clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
  834. {
  835. /*
  836. * XXX Rewrite this code to maintain a list of enabled
  837. * downstream clocks for debugging purposes?
  838. */
  839. if (!clk)
  840. return -EINVAL;
  841. return _clkdm_clk_hwmod_disable(clkdm);
  842. }
  843. /**
  844. * clkdm_hwmod_enable - add an enabled downstream hwmod to this clkdm
  845. * @clkdm: struct clockdomain *
  846. * @oh: struct omap_hwmod * of the enabled downstream hwmod
  847. *
  848. * Increment the usecount of the clockdomain @clkdm and ensure that it
  849. * is awake before @oh is enabled. Intended to be called by
  850. * module_enable() code.
  851. * If the clockdomain is in software-supervised idle mode, force the
  852. * clockdomain to wake. If the clockdomain is in hardware-supervised idle
  853. * mode, add clkdm-pwrdm autodependencies, to ensure that devices in the
  854. * clockdomain can be read from/written to by on-chip processors.
  855. * Returns -EINVAL if passed null pointers;
  856. * returns 0 upon success or if the clockdomain is in hwsup idle mode.
  857. */
  858. int clkdm_hwmod_enable(struct clockdomain *clkdm, struct omap_hwmod *oh)
  859. {
  860. /* The clkdm attribute does not exist yet prior OMAP4 */
  861. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  862. return 0;
  863. /*
  864. * XXX Rewrite this code to maintain a list of enabled
  865. * downstream hwmods for debugging purposes?
  866. */
  867. if (!oh)
  868. return -EINVAL;
  869. return _clkdm_clk_hwmod_enable(clkdm);
  870. }
  871. /**
  872. * clkdm_hwmod_disable - remove an enabled downstream hwmod from this clkdm
  873. * @clkdm: struct clockdomain *
  874. * @oh: struct omap_hwmod * of the disabled downstream hwmod
  875. *
  876. * Decrement the usecount of this clockdomain @clkdm when @oh is
  877. * disabled. Intended to be called by module_disable() code.
  878. * If the clockdomain usecount goes to 0, put the clockdomain to sleep
  879. * (software-supervised mode) or remove the clkdm autodependencies
  880. * (hardware-supervised mode).
  881. * Returns -EINVAL if passed null pointers; -ERANGE if the @clkdm usecount
  882. * underflows; or returns 0 upon success or if the clockdomain is in hwsup
  883. * idle mode.
  884. */
  885. int clkdm_hwmod_disable(struct clockdomain *clkdm, struct omap_hwmod *oh)
  886. {
  887. /* The clkdm attribute does not exist yet prior OMAP4 */
  888. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  889. return 0;
  890. /*
  891. * XXX Rewrite this code to maintain a list of enabled
  892. * downstream hwmods for debugging purposes?
  893. */
  894. if (!oh)
  895. return -EINVAL;
  896. return _clkdm_clk_hwmod_disable(clkdm);
  897. }