clock.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "clock.h"
  22. #include "proc_comm.h"
  23. #include "clock-7x30.h"
  24. #include "clock-pcom.h"
  25. static DEFINE_MUTEX(clocks_mutex);
  26. static DEFINE_SPINLOCK(clocks_lock);
  27. static LIST_HEAD(clocks);
  28. /*
  29. * Standard clock functions defined in include/linux/clk.h
  30. */
  31. struct clk *clk_get(struct device *dev, const char *id)
  32. {
  33. struct clk *clk;
  34. mutex_lock(&clocks_mutex);
  35. list_for_each_entry(clk, &clocks, list)
  36. if (!strcmp(id, clk->name) && clk->dev == dev)
  37. goto found_it;
  38. list_for_each_entry(clk, &clocks, list)
  39. if (!strcmp(id, clk->name) && clk->dev == NULL)
  40. goto found_it;
  41. clk = ERR_PTR(-ENOENT);
  42. found_it:
  43. mutex_unlock(&clocks_mutex);
  44. return clk;
  45. }
  46. EXPORT_SYMBOL(clk_get);
  47. void clk_put(struct clk *clk)
  48. {
  49. }
  50. EXPORT_SYMBOL(clk_put);
  51. int clk_enable(struct clk *clk)
  52. {
  53. unsigned long flags;
  54. spin_lock_irqsave(&clocks_lock, flags);
  55. clk->count++;
  56. if (clk->count == 1)
  57. clk->ops->enable(clk->id);
  58. spin_unlock_irqrestore(&clocks_lock, flags);
  59. return 0;
  60. }
  61. EXPORT_SYMBOL(clk_enable);
  62. void clk_disable(struct clk *clk)
  63. {
  64. unsigned long flags;
  65. spin_lock_irqsave(&clocks_lock, flags);
  66. BUG_ON(clk->count == 0);
  67. clk->count--;
  68. if (clk->count == 0)
  69. clk->ops->disable(clk->id);
  70. spin_unlock_irqrestore(&clocks_lock, flags);
  71. }
  72. EXPORT_SYMBOL(clk_disable);
  73. int clk_reset(struct clk *clk, enum clk_reset_action action)
  74. {
  75. if (!clk->ops->reset)
  76. clk->ops->reset = &pc_clk_reset;
  77. return clk->ops->reset(clk->remote_id, action);
  78. }
  79. EXPORT_SYMBOL(clk_reset);
  80. unsigned long clk_get_rate(struct clk *clk)
  81. {
  82. return clk->ops->get_rate(clk->id);
  83. }
  84. EXPORT_SYMBOL(clk_get_rate);
  85. int clk_set_rate(struct clk *clk, unsigned long rate)
  86. {
  87. int ret;
  88. if (clk->flags & CLKFLAG_MAX) {
  89. ret = clk->ops->set_max_rate(clk->id, rate);
  90. if (ret)
  91. return ret;
  92. }
  93. if (clk->flags & CLKFLAG_MIN) {
  94. ret = clk->ops->set_min_rate(clk->id, rate);
  95. if (ret)
  96. return ret;
  97. }
  98. if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN)
  99. return ret;
  100. return clk->ops->set_rate(clk->id, rate);
  101. }
  102. EXPORT_SYMBOL(clk_set_rate);
  103. long clk_round_rate(struct clk *clk, unsigned long rate)
  104. {
  105. return clk->ops->round_rate(clk->id, rate);
  106. }
  107. EXPORT_SYMBOL(clk_round_rate);
  108. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  109. {
  110. return clk->ops->set_min_rate(clk->id, rate);
  111. }
  112. EXPORT_SYMBOL(clk_set_min_rate);
  113. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  114. {
  115. return clk->ops->set_max_rate(clk->id, rate);
  116. }
  117. EXPORT_SYMBOL(clk_set_max_rate);
  118. int clk_set_parent(struct clk *clk, struct clk *parent)
  119. {
  120. return -ENOSYS;
  121. }
  122. EXPORT_SYMBOL(clk_set_parent);
  123. struct clk *clk_get_parent(struct clk *clk)
  124. {
  125. return ERR_PTR(-ENOSYS);
  126. }
  127. EXPORT_SYMBOL(clk_get_parent);
  128. int clk_set_flags(struct clk *clk, unsigned long flags)
  129. {
  130. if (clk == NULL || IS_ERR(clk))
  131. return -EINVAL;
  132. return clk->ops->set_flags(clk->id, flags);
  133. }
  134. EXPORT_SYMBOL(clk_set_flags);
  135. /* EBI1 is the only shared clock that several clients want to vote on as of
  136. * this commit. If this changes in the future, then it might be better to
  137. * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
  138. * generic to support different clocks.
  139. */
  140. static struct clk *ebi1_clk;
  141. static void __init set_clock_ops(struct clk *clk)
  142. {
  143. if (!clk->ops) {
  144. clk->ops = &clk_ops_pcom;
  145. clk->id = clk->remote_id;
  146. }
  147. }
  148. void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
  149. {
  150. unsigned n;
  151. mutex_lock(&clocks_mutex);
  152. for (n = 0; n < num_clocks; n++) {
  153. set_clock_ops(&clock_tbl[n]);
  154. list_add_tail(&clock_tbl[n].list, &clocks);
  155. }
  156. mutex_unlock(&clocks_mutex);
  157. ebi1_clk = clk_get(NULL, "ebi1_clk");
  158. BUG_ON(ebi1_clk == NULL);
  159. }
  160. /* The bootloader and/or AMSS may have left various clocks enabled.
  161. * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
  162. * not been explicitly enabled by a clk_enable() call.
  163. */
  164. static int __init clock_late_init(void)
  165. {
  166. unsigned long flags;
  167. struct clk *clk;
  168. unsigned count = 0;
  169. clock_debug_init();
  170. mutex_lock(&clocks_mutex);
  171. list_for_each_entry(clk, &clocks, list) {
  172. clock_debug_add(clk);
  173. if (clk->flags & CLKFLAG_AUTO_OFF) {
  174. spin_lock_irqsave(&clocks_lock, flags);
  175. if (!clk->count) {
  176. count++;
  177. clk->ops->auto_off(clk->id);
  178. }
  179. spin_unlock_irqrestore(&clocks_lock, flags);
  180. }
  181. }
  182. mutex_unlock(&clocks_mutex);
  183. pr_info("clock_late_init() disabled %d unused clocks\n", count);
  184. return 0;
  185. }
  186. late_initcall(clock_late_init);