clock.c 24 KB

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