clock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* arch/arm/mach-msm/clock.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2007-2010, Code Aurora Forum. All rights reserved.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/err.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/pm_qos_params.h>
  21. #include <linux/mutex.h>
  22. #include <linux/clk.h>
  23. #include <linux/string.h>
  24. #include <linux/module.h>
  25. #include <linux/clkdev.h>
  26. #include "clock.h"
  27. static DEFINE_MUTEX(clocks_mutex);
  28. static DEFINE_SPINLOCK(clocks_lock);
  29. static LIST_HEAD(clocks);
  30. /*
  31. * Standard clock functions defined in include/linux/clk.h
  32. */
  33. int clk_enable(struct clk *clk)
  34. {
  35. unsigned long flags;
  36. spin_lock_irqsave(&clocks_lock, flags);
  37. clk->count++;
  38. if (clk->count == 1)
  39. clk->ops->enable(clk->id);
  40. spin_unlock_irqrestore(&clocks_lock, flags);
  41. return 0;
  42. }
  43. EXPORT_SYMBOL(clk_enable);
  44. void clk_disable(struct clk *clk)
  45. {
  46. unsigned long flags;
  47. spin_lock_irqsave(&clocks_lock, flags);
  48. BUG_ON(clk->count == 0);
  49. clk->count--;
  50. if (clk->count == 0)
  51. clk->ops->disable(clk->id);
  52. spin_unlock_irqrestore(&clocks_lock, flags);
  53. }
  54. EXPORT_SYMBOL(clk_disable);
  55. int clk_reset(struct clk *clk, enum clk_reset_action action)
  56. {
  57. return clk->ops->reset(clk->remote_id, action);
  58. }
  59. EXPORT_SYMBOL(clk_reset);
  60. unsigned long clk_get_rate(struct clk *clk)
  61. {
  62. return clk->ops->get_rate(clk->id);
  63. }
  64. EXPORT_SYMBOL(clk_get_rate);
  65. int clk_set_rate(struct clk *clk, unsigned long rate)
  66. {
  67. int ret;
  68. if (clk->flags & CLKFLAG_MAX) {
  69. ret = clk->ops->set_max_rate(clk->id, rate);
  70. if (ret)
  71. return ret;
  72. }
  73. if (clk->flags & CLKFLAG_MIN) {
  74. ret = clk->ops->set_min_rate(clk->id, rate);
  75. if (ret)
  76. return ret;
  77. }
  78. if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN)
  79. return ret;
  80. return clk->ops->set_rate(clk->id, rate);
  81. }
  82. EXPORT_SYMBOL(clk_set_rate);
  83. long clk_round_rate(struct clk *clk, unsigned long rate)
  84. {
  85. return clk->ops->round_rate(clk->id, rate);
  86. }
  87. EXPORT_SYMBOL(clk_round_rate);
  88. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  89. {
  90. return clk->ops->set_min_rate(clk->id, rate);
  91. }
  92. EXPORT_SYMBOL(clk_set_min_rate);
  93. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  94. {
  95. return clk->ops->set_max_rate(clk->id, rate);
  96. }
  97. EXPORT_SYMBOL(clk_set_max_rate);
  98. int clk_set_parent(struct clk *clk, struct clk *parent)
  99. {
  100. return -ENOSYS;
  101. }
  102. EXPORT_SYMBOL(clk_set_parent);
  103. struct clk *clk_get_parent(struct clk *clk)
  104. {
  105. return ERR_PTR(-ENOSYS);
  106. }
  107. EXPORT_SYMBOL(clk_get_parent);
  108. int clk_set_flags(struct clk *clk, unsigned long flags)
  109. {
  110. if (clk == NULL || IS_ERR(clk))
  111. return -EINVAL;
  112. return clk->ops->set_flags(clk->id, flags);
  113. }
  114. EXPORT_SYMBOL(clk_set_flags);
  115. /* EBI1 is the only shared clock that several clients want to vote on as of
  116. * this commit. If this changes in the future, then it might be better to
  117. * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
  118. * generic to support different clocks.
  119. */
  120. static struct clk *ebi1_clk;
  121. void __init msm_clock_init(struct clk_lookup *clock_tbl, unsigned num_clocks)
  122. {
  123. unsigned n;
  124. mutex_lock(&clocks_mutex);
  125. for (n = 0; n < num_clocks; n++) {
  126. clkdev_add(&clock_tbl[n]);
  127. list_add_tail(&clock_tbl[n].clk->list, &clocks);
  128. }
  129. mutex_unlock(&clocks_mutex);
  130. ebi1_clk = clk_get(NULL, "ebi1_clk");
  131. BUG_ON(ebi1_clk == NULL);
  132. }
  133. /* The bootloader and/or AMSS may have left various clocks enabled.
  134. * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
  135. * not been explicitly enabled by a clk_enable() call.
  136. */
  137. static int __init clock_late_init(void)
  138. {
  139. unsigned long flags;
  140. struct clk *clk;
  141. unsigned count = 0;
  142. clock_debug_init();
  143. mutex_lock(&clocks_mutex);
  144. list_for_each_entry(clk, &clocks, list) {
  145. clock_debug_add(clk);
  146. if (clk->flags & CLKFLAG_AUTO_OFF) {
  147. spin_lock_irqsave(&clocks_lock, flags);
  148. if (!clk->count) {
  149. count++;
  150. clk->ops->auto_off(clk->id);
  151. }
  152. spin_unlock_irqrestore(&clocks_lock, flags);
  153. }
  154. }
  155. mutex_unlock(&clocks_mutex);
  156. pr_info("clock_late_init() disabled %d unused clocks\n", count);
  157. return 0;
  158. }
  159. late_initcall(clock_late_init);