clock.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Based on arch/arm/plat-omap/clock.c
  3. *
  4. * Copyright (C) 2004 - 2005 Nokia corporation
  5. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  6. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  7. * Copyright 2007 Freescale Semiconductor, Inc. All Rights Reserved.
  8. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301, USA.
  23. */
  24. /* #define DEBUG */
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel.h>
  31. #include <linux/list.h>
  32. #include <linux/module.h>
  33. #include <linux/mutex.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/semaphore.h>
  37. #include <linux/string.h>
  38. #include <mach/clock.h>
  39. static LIST_HEAD(clocks);
  40. static DEFINE_MUTEX(clocks_mutex);
  41. /*-------------------------------------------------------------------------
  42. * Standard clock functions defined in include/linux/clk.h
  43. *-------------------------------------------------------------------------*/
  44. static void __clk_disable(struct clk *clk)
  45. {
  46. if (clk == NULL || IS_ERR(clk))
  47. return;
  48. WARN_ON(!clk->usecount);
  49. if (!(--clk->usecount)) {
  50. if (clk->disable)
  51. clk->disable(clk);
  52. __clk_disable(clk->parent);
  53. }
  54. }
  55. static int __clk_enable(struct clk *clk)
  56. {
  57. if (clk == NULL || IS_ERR(clk))
  58. return -EINVAL;
  59. if (clk->usecount++ == 0) {
  60. __clk_enable(clk->parent);
  61. if (clk->enable)
  62. clk->enable(clk);
  63. }
  64. return 0;
  65. }
  66. /*
  67. * The clk_enable/clk_disable could be called by drivers in atomic context,
  68. * so they should not really hold mutex. Instead, clk_prepare/clk_unprepare
  69. * can hold a mutex, as the pair will only be called in non-atomic context.
  70. * Before migrating to common clk framework, we can have __clk_enable and
  71. * __clk_disable called in clk_prepare/clk_unprepare with mutex held and
  72. * leave clk_enable/clk_disable as the dummy functions.
  73. */
  74. int clk_prepare(struct clk *clk)
  75. {
  76. int ret = 0;
  77. if (clk == NULL || IS_ERR(clk))
  78. return -EINVAL;
  79. mutex_lock(&clocks_mutex);
  80. ret = __clk_enable(clk);
  81. mutex_unlock(&clocks_mutex);
  82. return ret;
  83. }
  84. EXPORT_SYMBOL(clk_prepare);
  85. void clk_unprepare(struct clk *clk)
  86. {
  87. if (clk == NULL || IS_ERR(clk))
  88. return;
  89. mutex_lock(&clocks_mutex);
  90. __clk_disable(clk);
  91. mutex_unlock(&clocks_mutex);
  92. }
  93. EXPORT_SYMBOL(clk_unprepare);
  94. int clk_enable(struct clk *clk)
  95. {
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(clk_enable);
  99. void clk_disable(struct clk *clk)
  100. {
  101. /* nothing to do */
  102. }
  103. EXPORT_SYMBOL(clk_disable);
  104. /* Retrieve the *current* clock rate. If the clock itself
  105. * does not provide a special calculation routine, ask
  106. * its parent and so on, until one is able to return
  107. * a valid clock rate
  108. */
  109. unsigned long clk_get_rate(struct clk *clk)
  110. {
  111. if (clk == NULL || IS_ERR(clk))
  112. return 0UL;
  113. if (clk->get_rate)
  114. return clk->get_rate(clk);
  115. return clk_get_rate(clk->parent);
  116. }
  117. EXPORT_SYMBOL(clk_get_rate);
  118. /* Round the requested clock rate to the nearest supported
  119. * rate that is less than or equal to the requested rate.
  120. * This is dependent on the clock's current parent.
  121. */
  122. long clk_round_rate(struct clk *clk, unsigned long rate)
  123. {
  124. if (clk == NULL || IS_ERR(clk) || !clk->round_rate)
  125. return 0;
  126. return clk->round_rate(clk, rate);
  127. }
  128. EXPORT_SYMBOL(clk_round_rate);
  129. /* Set the clock to the requested clock rate. The rate must
  130. * match a supported rate exactly based on what clk_round_rate returns
  131. */
  132. int clk_set_rate(struct clk *clk, unsigned long rate)
  133. {
  134. int ret = -EINVAL;
  135. if (clk == NULL || IS_ERR(clk) || clk->set_rate == NULL || rate == 0)
  136. return ret;
  137. mutex_lock(&clocks_mutex);
  138. ret = clk->set_rate(clk, rate);
  139. mutex_unlock(&clocks_mutex);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL(clk_set_rate);
  143. /* Set the clock's parent to another clock source */
  144. int clk_set_parent(struct clk *clk, struct clk *parent)
  145. {
  146. int ret = -EINVAL;
  147. struct clk *old;
  148. if (clk == NULL || IS_ERR(clk) || parent == NULL ||
  149. IS_ERR(parent) || clk->set_parent == NULL)
  150. return ret;
  151. if (clk->usecount)
  152. clk_prepare_enable(parent);
  153. mutex_lock(&clocks_mutex);
  154. ret = clk->set_parent(clk, parent);
  155. if (ret == 0) {
  156. old = clk->parent;
  157. clk->parent = parent;
  158. } else {
  159. old = parent;
  160. }
  161. mutex_unlock(&clocks_mutex);
  162. if (clk->usecount)
  163. clk_disable(old);
  164. return ret;
  165. }
  166. EXPORT_SYMBOL(clk_set_parent);
  167. /* Retrieve the clock's parent clock source */
  168. struct clk *clk_get_parent(struct clk *clk)
  169. {
  170. struct clk *ret = NULL;
  171. if (clk == NULL || IS_ERR(clk))
  172. return ret;
  173. return clk->parent;
  174. }
  175. EXPORT_SYMBOL(clk_get_parent);