clockdomain.c 31 KB

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