clockdomain.c 36 KB

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