clock.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Clock framework for Telechips SoCs
  3. * Based on arch/arm/plat-mxc/clock.c
  4. *
  5. * Copyright (C) 2004 - 2005 Nokia corporation
  6. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  7. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  8. * Copyright 2007 Freescale Semiconductor, Inc. All Rights Reserved.
  9. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  10. * Copyright 2010 Hans J. Koch, hjk@linutronix.de
  11. *
  12. * Licensed under the terms of the GPL v2.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/errno.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/string.h>
  20. #include <mach/clock.h>
  21. #include <mach/hardware.h>
  22. static DEFINE_MUTEX(clocks_mutex);
  23. /*-------------------------------------------------------------------------
  24. * Standard clock functions defined in include/linux/clk.h
  25. *-------------------------------------------------------------------------*/
  26. static void __clk_disable(struct clk *clk)
  27. {
  28. BUG_ON(clk->refcount == 0);
  29. if (!(--clk->refcount) && clk->disable) {
  30. /* Unconditionally disable the clock in hardware */
  31. clk->disable(clk);
  32. /* recursively disable parents */
  33. if (clk->parent)
  34. __clk_disable(clk->parent);
  35. }
  36. }
  37. static int __clk_enable(struct clk *clk)
  38. {
  39. int ret = 0;
  40. if (clk->refcount++ == 0 && clk->enable) {
  41. if (clk->parent)
  42. ret = __clk_enable(clk->parent);
  43. if (ret)
  44. return ret;
  45. else
  46. return clk->enable(clk);
  47. }
  48. return 0;
  49. }
  50. /* This function increments the reference count on the clock and enables the
  51. * clock if not already enabled. The parent clock tree is recursively enabled
  52. */
  53. int clk_enable(struct clk *clk)
  54. {
  55. int ret = 0;
  56. if (!clk)
  57. return -EINVAL;
  58. mutex_lock(&clocks_mutex);
  59. ret = __clk_enable(clk);
  60. mutex_unlock(&clocks_mutex);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL_GPL(clk_enable);
  64. /* This function decrements the reference count on the clock and disables
  65. * the clock when reference count is 0. The parent clock tree is
  66. * recursively disabled
  67. */
  68. void clk_disable(struct clk *clk)
  69. {
  70. if (!clk)
  71. return;
  72. mutex_lock(&clocks_mutex);
  73. __clk_disable(clk);
  74. mutex_unlock(&clocks_mutex);
  75. }
  76. EXPORT_SYMBOL_GPL(clk_disable);
  77. /* Retrieve the *current* clock rate. If the clock itself
  78. * does not provide a special calculation routine, ask
  79. * its parent and so on, until one is able to return
  80. * a valid clock rate
  81. */
  82. unsigned long clk_get_rate(struct clk *clk)
  83. {
  84. if (!clk)
  85. return 0UL;
  86. if (clk->get_rate)
  87. return clk->get_rate(clk);
  88. return clk_get_rate(clk->parent);
  89. }
  90. EXPORT_SYMBOL_GPL(clk_get_rate);
  91. /* Round the requested clock rate to the nearest supported
  92. * rate that is less than or equal to the requested rate.
  93. * This is dependent on the clock's current parent.
  94. */
  95. long clk_round_rate(struct clk *clk, unsigned long rate)
  96. {
  97. if (!clk)
  98. return 0;
  99. if (!clk->round_rate)
  100. return 0;
  101. return clk->round_rate(clk, rate);
  102. }
  103. EXPORT_SYMBOL_GPL(clk_round_rate);
  104. /* Set the clock to the requested clock rate. The rate must
  105. * match a supported rate exactly based on what clk_round_rate returns
  106. */
  107. int clk_set_rate(struct clk *clk, unsigned long rate)
  108. {
  109. int ret = -EINVAL;
  110. if (!clk)
  111. return ret;
  112. if (!clk->set_rate || !rate)
  113. return ret;
  114. mutex_lock(&clocks_mutex);
  115. ret = clk->set_rate(clk, rate);
  116. mutex_unlock(&clocks_mutex);
  117. return ret;
  118. }
  119. EXPORT_SYMBOL_GPL(clk_set_rate);
  120. /* Set the clock's parent to another clock source */
  121. int clk_set_parent(struct clk *clk, struct clk *parent)
  122. {
  123. struct clk *old;
  124. int ret = -EINVAL;
  125. if (!clk)
  126. return ret;
  127. if (!clk->set_parent || !parent)
  128. return ret;
  129. mutex_lock(&clocks_mutex);
  130. old = clk->parent;
  131. if (clk->refcount)
  132. __clk_enable(parent);
  133. ret = clk->set_parent(clk, parent);
  134. if (ret)
  135. old = parent;
  136. if (clk->refcount)
  137. __clk_disable(old);
  138. mutex_unlock(&clocks_mutex);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(clk_set_parent);
  142. /* Retrieve the clock's parent clock source */
  143. struct clk *clk_get_parent(struct clk *clk)
  144. {
  145. if (!clk)
  146. return NULL;
  147. return clk->parent;
  148. }
  149. EXPORT_SYMBOL_GPL(clk_get_parent);