clockdomain.c 31 KB

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