clock.c 25 KB

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