clockdomain.c 30 KB

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