clock.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * linux/arch/arm/mach-omap2/clock.c
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2010 Nokia Corporation
  6. *
  7. * Contacts:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Paul Walmsley
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/kernel.h>
  17. #include <linux/export.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/delay.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/bitops.h>
  25. #include <asm/cpu.h>
  26. #include <plat/prcm.h>
  27. #include <trace/events/power.h>
  28. #include "soc.h"
  29. #include "clockdomain.h"
  30. #include "clock.h"
  31. #include "cm2xxx.h"
  32. #include "cm3xxx.h"
  33. #include "cm-regbits-24xx.h"
  34. #include "cm-regbits-34xx.h"
  35. u16 cpu_mask;
  36. /*
  37. * clkdm_control: if true, then when a clock is enabled in the
  38. * hardware, its clockdomain will first be enabled; and when a clock
  39. * is disabled in the hardware, its clockdomain will be disabled
  40. * afterwards.
  41. */
  42. static bool clkdm_control = true;
  43. static LIST_HEAD(clocks);
  44. static DEFINE_MUTEX(clocks_mutex);
  45. static DEFINE_SPINLOCK(clockfw_lock);
  46. /*
  47. * OMAP2+ specific clock functions
  48. */
  49. /* Private functions */
  50. /**
  51. * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
  52. * @clk: struct clk * belonging to the module
  53. *
  54. * If the necessary clocks for the OMAP hardware IP block that
  55. * corresponds to clock @clk are enabled, then wait for the module to
  56. * indicate readiness (i.e., to leave IDLE). This code does not
  57. * belong in the clock code and will be moved in the medium term to
  58. * module-dependent code. No return value.
  59. */
  60. static void _omap2_module_wait_ready(struct clk *clk)
  61. {
  62. void __iomem *companion_reg, *idlest_reg;
  63. u8 other_bit, idlest_bit, idlest_val;
  64. /* Not all modules have multiple clocks that their IDLEST depends on */
  65. if (clk->ops->find_companion) {
  66. clk->ops->find_companion(clk, &companion_reg, &other_bit);
  67. if (!(__raw_readl(companion_reg) & (1 << other_bit)))
  68. return;
  69. }
  70. clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
  71. omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), idlest_val,
  72. __clk_get_name(clk));
  73. }
  74. /* Public functions */
  75. /**
  76. * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
  77. * @clk: OMAP clock struct ptr to use
  78. *
  79. * Convert a clockdomain name stored in a struct clk 'clk' into a
  80. * clockdomain pointer, and save it into the struct clk. Intended to be
  81. * called during clk_register(). No return value.
  82. */
  83. void omap2_init_clk_clkdm(struct clk *clk)
  84. {
  85. struct clockdomain *clkdm;
  86. const char *clk_name;
  87. if (!clk->clkdm_name)
  88. return;
  89. clk_name = __clk_get_name(clk);
  90. clkdm = clkdm_lookup(clk->clkdm_name);
  91. if (clkdm) {
  92. pr_debug("clock: associated clk %s to clkdm %s\n",
  93. clk_name, clk->clkdm_name);
  94. clk->clkdm = clkdm;
  95. } else {
  96. pr_debug("clock: could not associate clk %s to clkdm %s\n",
  97. clk_name, clk->clkdm_name);
  98. }
  99. }
  100. /**
  101. * omap2_clk_disable_clkdm_control - disable clkdm control on clk enable/disable
  102. *
  103. * Prevent the OMAP clock code from calling into the clockdomain code
  104. * when a hardware clock in that clockdomain is enabled or disabled.
  105. * Intended to be called at init time from omap*_clk_init(). No
  106. * return value.
  107. */
  108. void __init omap2_clk_disable_clkdm_control(void)
  109. {
  110. clkdm_control = false;
  111. }
  112. /**
  113. * omap2_clk_dflt_find_companion - find companion clock to @clk
  114. * @clk: struct clk * to find the companion clock of
  115. * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
  116. * @other_bit: u8 ** to return the companion clock bit shift in
  117. *
  118. * Note: We don't need special code here for INVERT_ENABLE for the
  119. * time being since INVERT_ENABLE only applies to clocks enabled by
  120. * CM_CLKEN_PLL
  121. *
  122. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
  123. * just a matter of XORing the bits.
  124. *
  125. * Some clocks don't have companion clocks. For example, modules with
  126. * only an interface clock (such as MAILBOXES) don't have a companion
  127. * clock. Right now, this code relies on the hardware exporting a bit
  128. * in the correct companion register that indicates that the
  129. * nonexistent 'companion clock' is active. Future patches will
  130. * associate this type of code with per-module data structures to
  131. * avoid this issue, and remove the casts. No return value.
  132. */
  133. void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
  134. u8 *other_bit)
  135. {
  136. u32 r;
  137. /*
  138. * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
  139. * it's just a matter of XORing the bits.
  140. */
  141. r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
  142. *other_reg = (__force void __iomem *)r;
  143. *other_bit = clk->enable_bit;
  144. }
  145. /**
  146. * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
  147. * @clk: struct clk * to find IDLEST info for
  148. * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
  149. * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
  150. * @idlest_val: u8 * to return the idle status indicator
  151. *
  152. * Return the CM_IDLEST register address and bit shift corresponding
  153. * to the module that "owns" this clock. This default code assumes
  154. * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
  155. * the IDLEST register address ID corresponds to the CM_*CLKEN
  156. * register address ID (e.g., that CM_FCLKEN2 corresponds to
  157. * CM_IDLEST2). This is not true for all modules. No return value.
  158. */
  159. void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
  160. u8 *idlest_bit, u8 *idlest_val)
  161. {
  162. u32 r;
  163. r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
  164. *idlest_reg = (__force void __iomem *)r;
  165. *idlest_bit = clk->enable_bit;
  166. /*
  167. * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
  168. * 34xx reverses this, just to keep us on our toes
  169. * AM35xx uses both, depending on the module.
  170. */
  171. if (cpu_is_omap24xx())
  172. *idlest_val = OMAP24XX_CM_IDLEST_VAL;
  173. else if (cpu_is_omap34xx())
  174. *idlest_val = OMAP34XX_CM_IDLEST_VAL;
  175. else
  176. BUG();
  177. }
  178. int omap2_dflt_clk_enable(struct clk *clk)
  179. {
  180. u32 v;
  181. if (unlikely(clk->enable_reg == NULL)) {
  182. pr_err("clock.c: Enable for %s without enable code\n",
  183. clk->name);
  184. return 0; /* REVISIT: -EINVAL */
  185. }
  186. v = __raw_readl(clk->enable_reg);
  187. if (clk->flags & INVERT_ENABLE)
  188. v &= ~(1 << clk->enable_bit);
  189. else
  190. v |= (1 << clk->enable_bit);
  191. __raw_writel(v, clk->enable_reg);
  192. v = __raw_readl(clk->enable_reg); /* OCP barrier */
  193. if (clk->ops->find_idlest)
  194. _omap2_module_wait_ready(clk);
  195. return 0;
  196. }
  197. void omap2_dflt_clk_disable(struct clk *clk)
  198. {
  199. u32 v;
  200. if (!clk->enable_reg) {
  201. /*
  202. * 'Independent' here refers to a clock which is not
  203. * controlled by its parent.
  204. */
  205. pr_err("clock: clk_disable called on independent clock %s which has no enable_reg\n", clk->name);
  206. return;
  207. }
  208. v = __raw_readl(clk->enable_reg);
  209. if (clk->flags & INVERT_ENABLE)
  210. v |= (1 << clk->enable_bit);
  211. else
  212. v &= ~(1 << clk->enable_bit);
  213. __raw_writel(v, clk->enable_reg);
  214. /* No OCP barrier needed here since it is a disable operation */
  215. }
  216. const struct clkops clkops_omap2_dflt_wait = {
  217. .enable = omap2_dflt_clk_enable,
  218. .disable = omap2_dflt_clk_disable,
  219. .find_companion = omap2_clk_dflt_find_companion,
  220. .find_idlest = omap2_clk_dflt_find_idlest,
  221. };
  222. const struct clkops clkops_omap2_dflt = {
  223. .enable = omap2_dflt_clk_enable,
  224. .disable = omap2_dflt_clk_disable,
  225. };
  226. /**
  227. * omap2_clk_disable - disable a clock, if the system is not using it
  228. * @clk: struct clk * to disable
  229. *
  230. * Decrements the usecount on struct clk @clk. If there are no users
  231. * left, call the clkops-specific clock disable function to disable it
  232. * in hardware. If the clock is part of a clockdomain (which they all
  233. * should be), request that the clockdomain be disabled. (It too has
  234. * a usecount, and so will not be disabled in the hardware until it no
  235. * longer has any users.) If the clock has a parent clock (most of
  236. * them do), then call ourselves, recursing on the parent clock. This
  237. * can cause an entire branch of the clock tree to be powered off by
  238. * simply disabling one clock. Intended to be called with the clockfw_lock
  239. * spinlock held. No return value.
  240. */
  241. void omap2_clk_disable(struct clk *clk)
  242. {
  243. if (clk->usecount == 0) {
  244. WARN(1, "clock: %s: omap2_clk_disable() called, but usecount already 0?", clk->name);
  245. return;
  246. }
  247. pr_debug("clock: %s: decrementing usecount\n", clk->name);
  248. clk->usecount--;
  249. if (clk->usecount > 0)
  250. return;
  251. pr_debug("clock: %s: disabling in hardware\n", clk->name);
  252. if (clk->ops && clk->ops->disable) {
  253. trace_clock_disable(clk->name, 0, smp_processor_id());
  254. clk->ops->disable(clk);
  255. }
  256. if (clkdm_control && clk->clkdm)
  257. clkdm_clk_disable(clk->clkdm, clk);
  258. if (clk->parent)
  259. omap2_clk_disable(clk->parent);
  260. }
  261. /**
  262. * omap2_clk_enable - request that the system enable a clock
  263. * @clk: struct clk * to enable
  264. *
  265. * Increments the usecount on struct clk @clk. If there were no users
  266. * previously, then recurse up the clock tree, enabling all of the
  267. * clock's parents and all of the parent clockdomains, and finally,
  268. * enabling @clk's clockdomain, and @clk itself. Intended to be
  269. * called with the clockfw_lock spinlock held. Returns 0 upon success
  270. * or a negative error code upon failure.
  271. */
  272. int omap2_clk_enable(struct clk *clk)
  273. {
  274. int ret;
  275. pr_debug("clock: %s: incrementing usecount\n", clk->name);
  276. clk->usecount++;
  277. if (clk->usecount > 1)
  278. return 0;
  279. pr_debug("clock: %s: enabling in hardware\n", clk->name);
  280. if (clk->parent) {
  281. ret = omap2_clk_enable(clk->parent);
  282. if (ret) {
  283. WARN(1, "clock: %s: could not enable parent %s: %d\n",
  284. clk->name, clk->parent->name, ret);
  285. goto oce_err1;
  286. }
  287. }
  288. if (clkdm_control && clk->clkdm) {
  289. ret = clkdm_clk_enable(clk->clkdm, clk);
  290. if (ret) {
  291. WARN(1, "clock: %s: could not enable clockdomain %s: %d\n",
  292. clk->name, clk->clkdm->name, ret);
  293. goto oce_err2;
  294. }
  295. }
  296. if (clk->ops && clk->ops->enable) {
  297. trace_clock_enable(clk->name, 1, smp_processor_id());
  298. ret = clk->ops->enable(clk);
  299. if (ret) {
  300. WARN(1, "clock: %s: could not enable: %d\n",
  301. clk->name, ret);
  302. goto oce_err3;
  303. }
  304. }
  305. return 0;
  306. oce_err3:
  307. if (clkdm_control && clk->clkdm)
  308. clkdm_clk_disable(clk->clkdm, clk);
  309. oce_err2:
  310. if (clk->parent)
  311. omap2_clk_disable(clk->parent);
  312. oce_err1:
  313. clk->usecount--;
  314. return ret;
  315. }
  316. /* Given a clock and a rate apply a clock specific rounding function */
  317. long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
  318. {
  319. if (clk->round_rate)
  320. return clk->round_rate(clk, rate);
  321. return clk->rate;
  322. }
  323. /* Set the clock rate for a clock source */
  324. int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
  325. {
  326. int ret = -EINVAL;
  327. pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
  328. /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
  329. if (clk->set_rate) {
  330. trace_clock_set_rate(clk->name, rate, smp_processor_id());
  331. ret = clk->set_rate(clk, rate);
  332. }
  333. return ret;
  334. }
  335. int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
  336. {
  337. if (!clk->clksel)
  338. return -EINVAL;
  339. if (clk->parent == new_parent)
  340. return 0;
  341. return omap2_clksel_set_parent(clk, new_parent);
  342. }
  343. /*
  344. * OMAP2+ clock reset and init functions
  345. */
  346. #ifdef CONFIG_OMAP_RESET_CLOCKS
  347. void omap2_clk_disable_unused(struct clk *clk)
  348. {
  349. u32 regval32, v;
  350. v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
  351. regval32 = __raw_readl(clk->enable_reg);
  352. if ((regval32 & (1 << clk->enable_bit)) == v)
  353. return;
  354. pr_debug("Disabling unused clock \"%s\"\n", clk->name);
  355. if (cpu_is_omap34xx()) {
  356. omap2_clk_enable(clk);
  357. omap2_clk_disable(clk);
  358. } else {
  359. clk->ops->disable(clk);
  360. }
  361. if (clk->clkdm != NULL)
  362. pwrdm_state_switch(clk->clkdm->pwrdm.ptr);
  363. }
  364. #endif
  365. /**
  366. * omap2_clk_switch_mpurate_at_boot - switch ARM MPU rate by boot-time argument
  367. * @mpurate_ck_name: clk name of the clock to change rate
  368. *
  369. * Change the ARM MPU clock rate to the rate specified on the command
  370. * line, if one was specified. @mpurate_ck_name should be
  371. * "virt_prcm_set" on OMAP2xxx and "dpll1_ck" on OMAP34xx/OMAP36xx.
  372. * XXX Does not handle voltage scaling - on OMAP2xxx this is currently
  373. * handled by the virt_prcm_set clock, but this should be handled by
  374. * the OPP layer. XXX This is intended to be handled by the OPP layer
  375. * code in the near future and should be removed from the clock code.
  376. * Returns -EINVAL if 'mpurate' is zero or if clk_set_rate() rejects
  377. * the rate, -ENOENT if the struct clk referred to by @mpurate_ck_name
  378. * cannot be found, or 0 upon success.
  379. */
  380. int __init omap2_clk_switch_mpurate_at_boot(const char *mpurate_ck_name)
  381. {
  382. struct clk *mpurate_ck;
  383. int r;
  384. if (!mpurate)
  385. return -EINVAL;
  386. mpurate_ck = clk_get(NULL, mpurate_ck_name);
  387. if (WARN(IS_ERR(mpurate_ck), "Failed to get %s.\n", mpurate_ck_name))
  388. return -ENOENT;
  389. r = clk_set_rate(mpurate_ck, mpurate);
  390. if (IS_ERR_VALUE(r)) {
  391. WARN(1, "clock: %s: unable to set MPU rate to %d: %d\n",
  392. mpurate_ck->name, mpurate, r);
  393. clk_put(mpurate_ck);
  394. return -EINVAL;
  395. }
  396. calibrate_delay();
  397. recalculate_root_clocks();
  398. clk_put(mpurate_ck);
  399. return 0;
  400. }
  401. /**
  402. * omap2_clk_print_new_rates - print summary of current clock tree rates
  403. * @hfclkin_ck_name: clk name for the off-chip HF oscillator
  404. * @core_ck_name: clk name for the on-chip CORE_CLK
  405. * @mpu_ck_name: clk name for the ARM MPU clock
  406. *
  407. * Prints a short message to the console with the HFCLKIN oscillator
  408. * rate, the rate of the CORE clock, and the rate of the ARM MPU clock.
  409. * Called by the boot-time MPU rate switching code. XXX This is intended
  410. * to be handled by the OPP layer code in the near future and should be
  411. * removed from the clock code. No return value.
  412. */
  413. void __init omap2_clk_print_new_rates(const char *hfclkin_ck_name,
  414. const char *core_ck_name,
  415. const char *mpu_ck_name)
  416. {
  417. struct clk *hfclkin_ck, *core_ck, *mpu_ck;
  418. unsigned long hfclkin_rate;
  419. mpu_ck = clk_get(NULL, mpu_ck_name);
  420. if (WARN(IS_ERR(mpu_ck), "clock: failed to get %s.\n", mpu_ck_name))
  421. return;
  422. core_ck = clk_get(NULL, core_ck_name);
  423. if (WARN(IS_ERR(core_ck), "clock: failed to get %s.\n", core_ck_name))
  424. return;
  425. hfclkin_ck = clk_get(NULL, hfclkin_ck_name);
  426. if (WARN(IS_ERR(hfclkin_ck), "Failed to get %s.\n", hfclkin_ck_name))
  427. return;
  428. hfclkin_rate = clk_get_rate(hfclkin_ck);
  429. pr_info("Switched to new clocking rate (Crystal/Core/MPU): %ld.%01ld/%ld/%ld MHz\n",
  430. (hfclkin_rate / 1000000), ((hfclkin_rate / 100000) % 10),
  431. (clk_get_rate(core_ck) / 1000000),
  432. (clk_get_rate(mpu_ck) / 1000000));
  433. }
  434. /* Common data */
  435. int clk_enable(struct clk *clk)
  436. {
  437. unsigned long flags;
  438. int ret;
  439. if (clk == NULL || IS_ERR(clk))
  440. return -EINVAL;
  441. spin_lock_irqsave(&clockfw_lock, flags);
  442. ret = omap2_clk_enable(clk);
  443. spin_unlock_irqrestore(&clockfw_lock, flags);
  444. return ret;
  445. }
  446. EXPORT_SYMBOL(clk_enable);
  447. void clk_disable(struct clk *clk)
  448. {
  449. unsigned long flags;
  450. if (clk == NULL || IS_ERR(clk))
  451. return;
  452. spin_lock_irqsave(&clockfw_lock, flags);
  453. if (clk->usecount == 0) {
  454. pr_err("Trying disable clock %s with 0 usecount\n",
  455. clk->name);
  456. WARN_ON(1);
  457. goto out;
  458. }
  459. omap2_clk_disable(clk);
  460. out:
  461. spin_unlock_irqrestore(&clockfw_lock, flags);
  462. }
  463. EXPORT_SYMBOL(clk_disable);
  464. unsigned long clk_get_rate(struct clk *clk)
  465. {
  466. unsigned long flags;
  467. unsigned long ret;
  468. if (clk == NULL || IS_ERR(clk))
  469. return 0;
  470. spin_lock_irqsave(&clockfw_lock, flags);
  471. ret = clk->rate;
  472. spin_unlock_irqrestore(&clockfw_lock, flags);
  473. return ret;
  474. }
  475. EXPORT_SYMBOL(clk_get_rate);
  476. /*
  477. * Optional clock functions defined in include/linux/clk.h
  478. */
  479. long clk_round_rate(struct clk *clk, unsigned long rate)
  480. {
  481. unsigned long flags;
  482. long ret;
  483. if (clk == NULL || IS_ERR(clk))
  484. return 0;
  485. spin_lock_irqsave(&clockfw_lock, flags);
  486. ret = omap2_clk_round_rate(clk, rate);
  487. spin_unlock_irqrestore(&clockfw_lock, flags);
  488. return ret;
  489. }
  490. EXPORT_SYMBOL(clk_round_rate);
  491. int clk_set_rate(struct clk *clk, unsigned long rate)
  492. {
  493. unsigned long flags;
  494. int ret = -EINVAL;
  495. if (clk == NULL || IS_ERR(clk))
  496. return ret;
  497. spin_lock_irqsave(&clockfw_lock, flags);
  498. ret = omap2_clk_set_rate(clk, rate);
  499. if (ret == 0)
  500. propagate_rate(clk);
  501. spin_unlock_irqrestore(&clockfw_lock, flags);
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(clk_set_rate);
  505. int clk_set_parent(struct clk *clk, struct clk *parent)
  506. {
  507. unsigned long flags;
  508. int ret = -EINVAL;
  509. if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
  510. return ret;
  511. spin_lock_irqsave(&clockfw_lock, flags);
  512. if (clk->usecount == 0) {
  513. ret = omap2_clk_set_parent(clk, parent);
  514. if (ret == 0)
  515. propagate_rate(clk);
  516. } else {
  517. ret = -EBUSY;
  518. }
  519. spin_unlock_irqrestore(&clockfw_lock, flags);
  520. return ret;
  521. }
  522. EXPORT_SYMBOL(clk_set_parent);
  523. struct clk *clk_get_parent(struct clk *clk)
  524. {
  525. return clk->parent;
  526. }
  527. EXPORT_SYMBOL(clk_get_parent);
  528. /*
  529. * OMAP specific clock functions shared between omap1 and omap2
  530. */
  531. int __initdata mpurate;
  532. /*
  533. * By default we use the rate set by the bootloader.
  534. * You can override this with mpurate= cmdline option.
  535. */
  536. static int __init omap_clk_setup(char *str)
  537. {
  538. get_option(&str, &mpurate);
  539. if (!mpurate)
  540. return 1;
  541. if (mpurate < 1000)
  542. mpurate *= 1000000;
  543. return 1;
  544. }
  545. __setup("mpurate=", omap_clk_setup);
  546. /* Used for clocks that always have same value as the parent clock */
  547. unsigned long followparent_recalc(struct clk *clk)
  548. {
  549. return clk->parent->rate;
  550. }
  551. /*
  552. * Used for clocks that have the same value as the parent clock,
  553. * divided by some factor
  554. */
  555. unsigned long omap_fixed_divisor_recalc(struct clk *clk)
  556. {
  557. WARN_ON(!clk->fixed_div);
  558. return clk->parent->rate / clk->fixed_div;
  559. }
  560. void clk_reparent(struct clk *child, struct clk *parent)
  561. {
  562. list_del_init(&child->sibling);
  563. if (parent)
  564. list_add(&child->sibling, &parent->children);
  565. child->parent = parent;
  566. /* now do the debugfs renaming to reattach the child
  567. to the proper parent */
  568. }
  569. /* Propagate rate to children */
  570. void propagate_rate(struct clk *tclk)
  571. {
  572. struct clk *clkp;
  573. list_for_each_entry(clkp, &tclk->children, sibling) {
  574. if (clkp->recalc)
  575. clkp->rate = clkp->recalc(clkp);
  576. propagate_rate(clkp);
  577. }
  578. }
  579. static LIST_HEAD(root_clks);
  580. /**
  581. * recalculate_root_clocks - recalculate and propagate all root clocks
  582. *
  583. * Recalculates all root clocks (clocks with no parent), which if the
  584. * clock's .recalc is set correctly, should also propagate their rates.
  585. * Called at init.
  586. */
  587. void recalculate_root_clocks(void)
  588. {
  589. struct clk *clkp;
  590. list_for_each_entry(clkp, &root_clks, sibling) {
  591. if (clkp->recalc)
  592. clkp->rate = clkp->recalc(clkp);
  593. propagate_rate(clkp);
  594. }
  595. }
  596. /**
  597. * clk_preinit - initialize any fields in the struct clk before clk init
  598. * @clk: struct clk * to initialize
  599. *
  600. * Initialize any struct clk fields needed before normal clk initialization
  601. * can run. No return value.
  602. */
  603. void clk_preinit(struct clk *clk)
  604. {
  605. INIT_LIST_HEAD(&clk->children);
  606. }
  607. int clk_register(struct clk *clk)
  608. {
  609. if (clk == NULL || IS_ERR(clk))
  610. return -EINVAL;
  611. /*
  612. * trap out already registered clocks
  613. */
  614. if (clk->node.next || clk->node.prev)
  615. return 0;
  616. mutex_lock(&clocks_mutex);
  617. if (clk->parent)
  618. list_add(&clk->sibling, &clk->parent->children);
  619. else
  620. list_add(&clk->sibling, &root_clks);
  621. list_add(&clk->node, &clocks);
  622. if (clk->init)
  623. clk->init(clk);
  624. mutex_unlock(&clocks_mutex);
  625. return 0;
  626. }
  627. EXPORT_SYMBOL(clk_register);
  628. void clk_unregister(struct clk *clk)
  629. {
  630. if (clk == NULL || IS_ERR(clk))
  631. return;
  632. mutex_lock(&clocks_mutex);
  633. list_del(&clk->sibling);
  634. list_del(&clk->node);
  635. mutex_unlock(&clocks_mutex);
  636. }
  637. EXPORT_SYMBOL(clk_unregister);
  638. void clk_enable_init_clocks(void)
  639. {
  640. struct clk *clkp;
  641. list_for_each_entry(clkp, &clocks, node)
  642. if (clkp->flags & ENABLE_ON_INIT)
  643. clk_enable(clkp);
  644. }
  645. /**
  646. * omap_clk_get_by_name - locate OMAP struct clk by its name
  647. * @name: name of the struct clk to locate
  648. *
  649. * Locate an OMAP struct clk by its name. Assumes that struct clk
  650. * names are unique. Returns NULL if not found or a pointer to the
  651. * struct clk if found.
  652. */
  653. struct clk *omap_clk_get_by_name(const char *name)
  654. {
  655. struct clk *c;
  656. struct clk *ret = NULL;
  657. mutex_lock(&clocks_mutex);
  658. list_for_each_entry(c, &clocks, node) {
  659. if (!strcmp(c->name, name)) {
  660. ret = c;
  661. break;
  662. }
  663. }
  664. mutex_unlock(&clocks_mutex);
  665. return ret;
  666. }
  667. int omap_clk_enable_autoidle_all(void)
  668. {
  669. struct clk *c;
  670. unsigned long flags;
  671. spin_lock_irqsave(&clockfw_lock, flags);
  672. list_for_each_entry(c, &clocks, node)
  673. if (c->ops->allow_idle)
  674. c->ops->allow_idle(c);
  675. spin_unlock_irqrestore(&clockfw_lock, flags);
  676. return 0;
  677. }
  678. int omap_clk_disable_autoidle_all(void)
  679. {
  680. struct clk *c;
  681. unsigned long flags;
  682. spin_lock_irqsave(&clockfw_lock, flags);
  683. list_for_each_entry(c, &clocks, node)
  684. if (c->ops->deny_idle)
  685. c->ops->deny_idle(c);
  686. spin_unlock_irqrestore(&clockfw_lock, flags);
  687. return 0;
  688. }
  689. /*
  690. * Low level helpers
  691. */
  692. static int clkll_enable_null(struct clk *clk)
  693. {
  694. return 0;
  695. }
  696. static void clkll_disable_null(struct clk *clk)
  697. {
  698. }
  699. const struct clkops clkops_null = {
  700. .enable = clkll_enable_null,
  701. .disable = clkll_disable_null,
  702. };
  703. /*
  704. * Dummy clock
  705. *
  706. * Used for clock aliases that are needed on some OMAPs, but not others
  707. */
  708. struct clk dummy_ck = {
  709. .name = "dummy",
  710. .ops = &clkops_null,
  711. };
  712. /*
  713. *
  714. */
  715. #ifdef CONFIG_OMAP_RESET_CLOCKS
  716. /*
  717. * Disable any unused clocks left on by the bootloader
  718. */
  719. static int __init clk_disable_unused(void)
  720. {
  721. struct clk *ck;
  722. unsigned long flags;
  723. pr_info("clock: disabling unused clocks to save power\n");
  724. spin_lock_irqsave(&clockfw_lock, flags);
  725. list_for_each_entry(ck, &clocks, node) {
  726. if (ck->ops == &clkops_null)
  727. continue;
  728. if (ck->usecount > 0 || !ck->enable_reg)
  729. continue;
  730. omap2_clk_disable_unused(ck);
  731. }
  732. spin_unlock_irqrestore(&clockfw_lock, flags);
  733. return 0;
  734. }
  735. late_initcall(clk_disable_unused);
  736. late_initcall(omap_clk_enable_autoidle_all);
  737. #endif
  738. #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
  739. /*
  740. * debugfs support to trace clock tree hierarchy and attributes
  741. */
  742. #include <linux/debugfs.h>
  743. #include <linux/seq_file.h>
  744. static struct dentry *clk_debugfs_root;
  745. static int clk_dbg_show_summary(struct seq_file *s, void *unused)
  746. {
  747. struct clk *c;
  748. struct clk *pa;
  749. mutex_lock(&clocks_mutex);
  750. seq_printf(s, "%-30s %-30s %-10s %s\n",
  751. "clock-name", "parent-name", "rate", "use-count");
  752. list_for_each_entry(c, &clocks, node) {
  753. pa = c->parent;
  754. seq_printf(s, "%-30s %-30s %-10lu %d\n",
  755. c->name, pa ? pa->name : "none", c->rate,
  756. c->usecount);
  757. }
  758. mutex_unlock(&clocks_mutex);
  759. return 0;
  760. }
  761. static int clk_dbg_open(struct inode *inode, struct file *file)
  762. {
  763. return single_open(file, clk_dbg_show_summary, inode->i_private);
  764. }
  765. static const struct file_operations debug_clock_fops = {
  766. .open = clk_dbg_open,
  767. .read = seq_read,
  768. .llseek = seq_lseek,
  769. .release = single_release,
  770. };
  771. static int clk_debugfs_register_one(struct clk *c)
  772. {
  773. int err;
  774. struct dentry *d;
  775. struct clk *pa = c->parent;
  776. d = debugfs_create_dir(c->name, pa ? pa->dent : clk_debugfs_root);
  777. if (!d)
  778. return -ENOMEM;
  779. c->dent = d;
  780. d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
  781. if (!d) {
  782. err = -ENOMEM;
  783. goto err_out;
  784. }
  785. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  786. if (!d) {
  787. err = -ENOMEM;
  788. goto err_out;
  789. }
  790. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  791. if (!d) {
  792. err = -ENOMEM;
  793. goto err_out;
  794. }
  795. return 0;
  796. err_out:
  797. debugfs_remove_recursive(c->dent);
  798. return err;
  799. }
  800. static int clk_debugfs_register(struct clk *c)
  801. {
  802. int err;
  803. struct clk *pa = c->parent;
  804. if (pa && !pa->dent) {
  805. err = clk_debugfs_register(pa);
  806. if (err)
  807. return err;
  808. }
  809. if (!c->dent) {
  810. err = clk_debugfs_register_one(c);
  811. if (err)
  812. return err;
  813. }
  814. return 0;
  815. }
  816. static int __init clk_debugfs_init(void)
  817. {
  818. struct clk *c;
  819. struct dentry *d;
  820. int err;
  821. d = debugfs_create_dir("clock", NULL);
  822. if (!d)
  823. return -ENOMEM;
  824. clk_debugfs_root = d;
  825. list_for_each_entry(c, &clocks, node) {
  826. err = clk_debugfs_register(c);
  827. if (err)
  828. goto err_out;
  829. }
  830. d = debugfs_create_file("summary", S_IRUGO,
  831. d, NULL, &debug_clock_fops);
  832. if (!d)
  833. return -ENOMEM;
  834. return 0;
  835. err_out:
  836. debugfs_remove_recursive(clk_debugfs_root);
  837. return err;
  838. }
  839. late_initcall(clk_debugfs_init);
  840. #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */