clockdomain.c 30 KB

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