clockdomain.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * OMAP2/3/4 clockdomain framework functions
  3. *
  4. * Copyright (C) 2008-2009 Texas Instruments, Inc.
  5. * Copyright (C) 2008-2009 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/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/device.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.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 "prm.h"
  28. #include "prm-regbits-24xx.h"
  29. #include "cm.h"
  30. #include <plat/powerdomain.h>
  31. #include <plat/clockdomain.h>
  32. /* clkdm_list contains all registered struct clockdomains */
  33. static LIST_HEAD(clkdm_list);
  34. /* clkdm_mutex protects clkdm_list add and del ops */
  35. static DEFINE_MUTEX(clkdm_mutex);
  36. /* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
  37. static struct clkdm_pwrdm_autodep *autodeps;
  38. /* Private functions */
  39. /*
  40. * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
  41. * @autodep: struct clkdm_pwrdm_autodep * to resolve
  42. *
  43. * Resolve autodep powerdomain names to powerdomain pointers via
  44. * pwrdm_lookup() and store the pointers in the autodep structure. An
  45. * "autodep" is a powerdomain sleep/wakeup dependency that is
  46. * automatically added and removed whenever clocks in the associated
  47. * clockdomain are enabled or disabled (respectively) when the
  48. * clockdomain is in hardware-supervised mode. Meant to be called
  49. * once at clockdomain layer initialization, since these should remain
  50. * fixed for a particular architecture. No return value.
  51. */
  52. static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
  53. {
  54. struct powerdomain *pwrdm;
  55. if (!autodep)
  56. return;
  57. if (!omap_chip_is(autodep->omap_chip))
  58. return;
  59. pwrdm = pwrdm_lookup(autodep->pwrdm.name);
  60. if (!pwrdm) {
  61. pr_err("clockdomain: autodeps: powerdomain %s does not exist\n",
  62. autodep->pwrdm.name);
  63. pwrdm = ERR_PTR(-ENOENT);
  64. }
  65. autodep->pwrdm.ptr = pwrdm;
  66. }
  67. /*
  68. * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
  69. * @clkdm: struct clockdomain *
  70. *
  71. * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
  72. * in hardware-supervised mode. Meant to be called from clock framework
  73. * when a clock inside clockdomain 'clkdm' is enabled. No return value.
  74. */
  75. static void _clkdm_add_autodeps(struct clockdomain *clkdm)
  76. {
  77. struct clkdm_pwrdm_autodep *autodep;
  78. for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
  79. if (IS_ERR(autodep->pwrdm.ptr))
  80. continue;
  81. if (!omap_chip_is(autodep->omap_chip))
  82. continue;
  83. pr_debug("clockdomain: adding %s sleepdep/wkdep for "
  84. "pwrdm %s\n", autodep->pwrdm.ptr->name,
  85. clkdm->pwrdm.ptr->name);
  86. pwrdm_add_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
  87. pwrdm_add_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
  88. }
  89. }
  90. /*
  91. * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
  92. * @clkdm: struct clockdomain *
  93. *
  94. * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
  95. * in hardware-supervised mode. Meant to be called from clock framework
  96. * when a clock inside clockdomain 'clkdm' is disabled. No return value.
  97. */
  98. static void _clkdm_del_autodeps(struct clockdomain *clkdm)
  99. {
  100. struct clkdm_pwrdm_autodep *autodep;
  101. for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
  102. if (IS_ERR(autodep->pwrdm.ptr))
  103. continue;
  104. if (!omap_chip_is(autodep->omap_chip))
  105. continue;
  106. pr_debug("clockdomain: removing %s sleepdep/wkdep for "
  107. "pwrdm %s\n", autodep->pwrdm.ptr->name,
  108. clkdm->pwrdm.ptr->name);
  109. pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
  110. pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
  111. }
  112. }
  113. /*
  114. * _omap2_clkdm_set_hwsup - set the hwsup idle transition bit
  115. * @clkdm: struct clockdomain *
  116. * @enable: int 0 to disable, 1 to enable
  117. *
  118. * Internal helper for actually switching the bit that controls hwsup
  119. * idle transitions for clkdm.
  120. */
  121. static void _omap2_clkdm_set_hwsup(struct clockdomain *clkdm, int enable)
  122. {
  123. u32 bits, v;
  124. if (cpu_is_omap24xx()) {
  125. if (enable)
  126. bits = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
  127. else
  128. bits = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
  129. } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
  130. if (enable)
  131. bits = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
  132. else
  133. bits = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
  134. } else {
  135. BUG();
  136. }
  137. bits = bits << __ffs(clkdm->clktrctrl_mask);
  138. v = __raw_readl(clkdm->clkstctrl_reg);
  139. v &= ~(clkdm->clktrctrl_mask);
  140. v |= bits;
  141. __raw_writel(v, clkdm->clkstctrl_reg);
  142. }
  143. static struct clockdomain *_clkdm_lookup(const char *name)
  144. {
  145. struct clockdomain *clkdm, *temp_clkdm;
  146. if (!name)
  147. return NULL;
  148. clkdm = NULL;
  149. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  150. if (!strcmp(name, temp_clkdm->name)) {
  151. clkdm = temp_clkdm;
  152. break;
  153. }
  154. }
  155. return clkdm;
  156. }
  157. /* Public functions */
  158. /**
  159. * clkdm_init - set up the clockdomain layer
  160. * @clkdms: optional pointer to an array of clockdomains to register
  161. * @init_autodeps: optional pointer to an array of autodeps to register
  162. *
  163. * Set up internal state. If a pointer to an array of clockdomains
  164. * was supplied, loop through the list of clockdomains, register all
  165. * that are available on the current platform. Similarly, if a
  166. * pointer to an array of clockdomain-powerdomain autodependencies was
  167. * provided, register those. No return value.
  168. */
  169. void clkdm_init(struct clockdomain **clkdms,
  170. struct clkdm_pwrdm_autodep *init_autodeps)
  171. {
  172. struct clockdomain **c = NULL;
  173. struct clkdm_pwrdm_autodep *autodep = NULL;
  174. if (clkdms)
  175. for (c = clkdms; *c; c++)
  176. clkdm_register(*c);
  177. if (!cpu_is_omap44xx()) {
  178. autodeps = init_autodeps;
  179. if (autodeps)
  180. for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
  181. _autodep_lookup(autodep);
  182. }
  183. }
  184. /**
  185. * clkdm_register - register a clockdomain
  186. * @clkdm: struct clockdomain * to register
  187. *
  188. * Adds a clockdomain to the internal clockdomain list.
  189. * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
  190. * already registered by the provided name, or 0 upon success.
  191. */
  192. int clkdm_register(struct clockdomain *clkdm)
  193. {
  194. int ret = -EINVAL;
  195. struct powerdomain *pwrdm;
  196. if (!clkdm || !clkdm->name)
  197. return -EINVAL;
  198. if (!omap_chip_is(clkdm->omap_chip))
  199. return -EINVAL;
  200. pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
  201. if (!pwrdm) {
  202. pr_err("clockdomain: %s: powerdomain %s does not exist\n",
  203. clkdm->name, clkdm->pwrdm.name);
  204. return -EINVAL;
  205. }
  206. clkdm->pwrdm.ptr = pwrdm;
  207. mutex_lock(&clkdm_mutex);
  208. /* Verify that the clockdomain is not already registered */
  209. if (_clkdm_lookup(clkdm->name)) {
  210. ret = -EEXIST;
  211. goto cr_unlock;
  212. }
  213. list_add(&clkdm->node, &clkdm_list);
  214. pwrdm_add_clkdm(pwrdm, clkdm);
  215. pr_debug("clockdomain: registered %s\n", clkdm->name);
  216. ret = 0;
  217. cr_unlock:
  218. mutex_unlock(&clkdm_mutex);
  219. return ret;
  220. }
  221. /**
  222. * clkdm_unregister - unregister a clockdomain
  223. * @clkdm: struct clockdomain * to unregister
  224. *
  225. * Removes a clockdomain from the internal clockdomain list. Returns
  226. * -EINVAL if clkdm argument is NULL.
  227. */
  228. int clkdm_unregister(struct clockdomain *clkdm)
  229. {
  230. if (!clkdm)
  231. return -EINVAL;
  232. pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
  233. mutex_lock(&clkdm_mutex);
  234. list_del(&clkdm->node);
  235. mutex_unlock(&clkdm_mutex);
  236. pr_debug("clockdomain: unregistered %s\n", clkdm->name);
  237. return 0;
  238. }
  239. /**
  240. * clkdm_lookup - look up a clockdomain by name, return a pointer
  241. * @name: name of clockdomain
  242. *
  243. * Find a registered clockdomain by its name. Returns a pointer to the
  244. * struct clockdomain if found, or NULL otherwise.
  245. */
  246. struct clockdomain *clkdm_lookup(const char *name)
  247. {
  248. struct clockdomain *clkdm, *temp_clkdm;
  249. if (!name)
  250. return NULL;
  251. clkdm = NULL;
  252. mutex_lock(&clkdm_mutex);
  253. list_for_each_entry(temp_clkdm, &clkdm_list, node) {
  254. if (!strcmp(name, temp_clkdm->name)) {
  255. clkdm = temp_clkdm;
  256. break;
  257. }
  258. }
  259. mutex_unlock(&clkdm_mutex);
  260. return clkdm;
  261. }
  262. /**
  263. * clkdm_for_each - call function on each registered clockdomain
  264. * @fn: callback function *
  265. *
  266. * Call the supplied function for each registered clockdomain.
  267. * The callback function can return anything but 0 to bail
  268. * out early from the iterator. The callback function is called with
  269. * the clkdm_mutex held, so no clockdomain structure manipulation
  270. * functions should be called from the callback, although hardware
  271. * clockdomain control functions are fine. Returns the last return
  272. * value of the callback function, which should be 0 for success or
  273. * anything else to indicate failure; or -EINVAL if the function pointer
  274. * is null.
  275. */
  276. int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
  277. void *user)
  278. {
  279. struct clockdomain *clkdm;
  280. int ret = 0;
  281. if (!fn)
  282. return -EINVAL;
  283. mutex_lock(&clkdm_mutex);
  284. list_for_each_entry(clkdm, &clkdm_list, node) {
  285. ret = (*fn)(clkdm, user);
  286. if (ret)
  287. break;
  288. }
  289. mutex_unlock(&clkdm_mutex);
  290. return ret;
  291. }
  292. /**
  293. * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
  294. * @clkdm: struct clockdomain *
  295. *
  296. * Return a pointer to the struct powerdomain that the specified clockdomain
  297. * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
  298. */
  299. struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
  300. {
  301. if (!clkdm)
  302. return NULL;
  303. return clkdm->pwrdm.ptr;
  304. }
  305. /* Hardware clockdomain control */
  306. /**
  307. * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
  308. * @clk: struct clk * of a clockdomain
  309. *
  310. * Return the clockdomain's current state transition mode from the
  311. * corresponding domain OMAP2_CM_CLKSTCTRL register. Returns -EINVAL if clk
  312. * is NULL or the current mode upon success.
  313. */
  314. static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
  315. {
  316. u32 v;
  317. if (!clkdm)
  318. return -EINVAL;
  319. v = __raw_readl(clkdm->clkstctrl_reg);
  320. v &= clkdm->clktrctrl_mask;
  321. v >>= __ffs(clkdm->clktrctrl_mask);
  322. return v;
  323. }
  324. /**
  325. * omap2_clkdm_sleep - force clockdomain sleep transition
  326. * @clkdm: struct clockdomain *
  327. *
  328. * Instruct the CM to force a sleep transition on the specified
  329. * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
  330. * clockdomain does not support software-initiated sleep; 0 upon
  331. * success.
  332. */
  333. int omap2_clkdm_sleep(struct clockdomain *clkdm)
  334. {
  335. if (!clkdm)
  336. return -EINVAL;
  337. if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
  338. pr_debug("clockdomain: %s does not support forcing "
  339. "sleep via software\n", clkdm->name);
  340. return -EINVAL;
  341. }
  342. pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
  343. if (cpu_is_omap24xx()) {
  344. cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
  345. clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
  346. } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
  347. u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
  348. __ffs(clkdm->clktrctrl_mask));
  349. u32 v = __raw_readl(clkdm->clkstctrl_reg);
  350. v &= ~(clkdm->clktrctrl_mask);
  351. v |= bits;
  352. __raw_writel(v, clkdm->clkstctrl_reg);
  353. } else {
  354. BUG();
  355. };
  356. return 0;
  357. }
  358. /**
  359. * omap2_clkdm_wakeup - force clockdomain wakeup transition
  360. * @clkdm: struct clockdomain *
  361. *
  362. * Instruct the CM to force a wakeup transition on the specified
  363. * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
  364. * clockdomain does not support software-controlled wakeup; 0 upon
  365. * success.
  366. */
  367. int omap2_clkdm_wakeup(struct clockdomain *clkdm)
  368. {
  369. if (!clkdm)
  370. return -EINVAL;
  371. if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
  372. pr_debug("clockdomain: %s does not support forcing "
  373. "wakeup via software\n", clkdm->name);
  374. return -EINVAL;
  375. }
  376. pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
  377. if (cpu_is_omap24xx()) {
  378. cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
  379. clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
  380. } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
  381. u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
  382. __ffs(clkdm->clktrctrl_mask));
  383. u32 v = __raw_readl(clkdm->clkstctrl_reg);
  384. v &= ~(clkdm->clktrctrl_mask);
  385. v |= bits;
  386. __raw_writel(v, clkdm->clkstctrl_reg);
  387. } else {
  388. BUG();
  389. };
  390. return 0;
  391. }
  392. /**
  393. * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
  394. * @clkdm: struct clockdomain *
  395. *
  396. * Allow the hardware to automatically switch the clockdomain into
  397. * active or idle states, as needed by downstream clocks. If the
  398. * clockdomain has any downstream clocks enabled in the clock
  399. * framework, wkdep/sleepdep autodependencies are added; this is so
  400. * device drivers can read and write to the device. No return value.
  401. */
  402. void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
  403. {
  404. if (!clkdm)
  405. return;
  406. if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
  407. pr_debug("clock: automatic idle transitions cannot be enabled "
  408. "on clockdomain %s\n", clkdm->name);
  409. return;
  410. }
  411. pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
  412. clkdm->name);
  413. if (atomic_read(&clkdm->usecount) > 0)
  414. _clkdm_add_autodeps(clkdm);
  415. _omap2_clkdm_set_hwsup(clkdm, 1);
  416. pwrdm_clkdm_state_switch(clkdm);
  417. }
  418. /**
  419. * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
  420. * @clkdm: struct clockdomain *
  421. *
  422. * Prevent the hardware from automatically switching the clockdomain
  423. * into inactive or idle states. If the clockdomain has downstream
  424. * clocks enabled in the clock framework, wkdep/sleepdep
  425. * autodependencies are removed. No return value.
  426. */
  427. void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
  428. {
  429. if (!clkdm)
  430. return;
  431. if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
  432. pr_debug("clockdomain: automatic idle transitions cannot be "
  433. "disabled on %s\n", clkdm->name);
  434. return;
  435. }
  436. pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
  437. clkdm->name);
  438. _omap2_clkdm_set_hwsup(clkdm, 0);
  439. if (atomic_read(&clkdm->usecount) > 0)
  440. _clkdm_del_autodeps(clkdm);
  441. }
  442. /* Clockdomain-to-clock framework interface code */
  443. /**
  444. * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
  445. * @clkdm: struct clockdomain *
  446. * @clk: struct clk * of the enabled downstream clock
  447. *
  448. * Increment the usecount of this clockdomain 'clkdm' and ensure that
  449. * it is awake. Intended to be called by clk_enable() code. If the
  450. * clockdomain is in software-supervised idle mode, force the
  451. * clockdomain to wake. If the clockdomain is in hardware-supervised
  452. * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
  453. * in the clockdomain can be read from/written to by on-chip processors.
  454. * Returns -EINVAL if passed null pointers; returns 0 upon success or
  455. * if the clockdomain is in hwsup idle mode.
  456. */
  457. int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
  458. {
  459. int v;
  460. /*
  461. * XXX Rewrite this code to maintain a list of enabled
  462. * downstream clocks for debugging purposes?
  463. */
  464. if (!clkdm || !clk || !clkdm->clkstctrl_reg)
  465. return -EINVAL;
  466. if (atomic_inc_return(&clkdm->usecount) > 1)
  467. return 0;
  468. /* Clockdomain now has one enabled downstream clock */
  469. pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
  470. clk->name);
  471. v = omap2_clkdm_clktrctrl_read(clkdm);
  472. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  473. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
  474. /* Disable HW transitions when we are changing deps */
  475. _omap2_clkdm_set_hwsup(clkdm, 0);
  476. _clkdm_add_autodeps(clkdm);
  477. _omap2_clkdm_set_hwsup(clkdm, 1);
  478. } else {
  479. omap2_clkdm_wakeup(clkdm);
  480. }
  481. pwrdm_wait_transition(clkdm->pwrdm.ptr);
  482. pwrdm_clkdm_state_switch(clkdm);
  483. return 0;
  484. }
  485. /**
  486. * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
  487. * @clkdm: struct clockdomain *
  488. * @clk: struct clk * of the disabled downstream clock
  489. *
  490. * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
  491. * called by clk_disable() code. If the usecount goes to 0, put the
  492. * clockdomain to sleep (software-supervised mode) or remove the
  493. * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
  494. * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
  495. * underflows and debugging is enabled; or returns 0 upon success or
  496. * if the clockdomain is in hwsup idle mode.
  497. */
  498. int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
  499. {
  500. int v;
  501. /*
  502. * XXX Rewrite this code to maintain a list of enabled
  503. * downstream clocks for debugging purposes?
  504. */
  505. if (!clkdm || !clk || !clkdm->clkstctrl_reg)
  506. return -EINVAL;
  507. #ifdef DEBUG
  508. if (atomic_read(&clkdm->usecount) == 0) {
  509. WARN_ON(1); /* underflow */
  510. return -ERANGE;
  511. }
  512. #endif
  513. if (atomic_dec_return(&clkdm->usecount) > 0)
  514. return 0;
  515. /* All downstream clocks of this clockdomain are now disabled */
  516. pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
  517. clk->name);
  518. v = omap2_clkdm_clktrctrl_read(clkdm);
  519. if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
  520. (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
  521. /* Disable HW transitions when we are changing deps */
  522. _omap2_clkdm_set_hwsup(clkdm, 0);
  523. _clkdm_del_autodeps(clkdm);
  524. _omap2_clkdm_set_hwsup(clkdm, 1);
  525. } else {
  526. omap2_clkdm_sleep(clkdm);
  527. }
  528. pwrdm_clkdm_state_switch(clkdm);
  529. return 0;
  530. }