clockdomain.c 35 KB

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