clock.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* arch/arm/mach-msm/clock.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2007 QUALCOMM Incorporated
  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/version.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/list.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/spinlock.h>
  24. #include "clock.h"
  25. #include "proc_comm.h"
  26. static DEFINE_MUTEX(clocks_mutex);
  27. static DEFINE_SPINLOCK(clocks_lock);
  28. static LIST_HEAD(clocks);
  29. /*
  30. * glue for the proc_comm interface
  31. */
  32. static inline int pc_clk_enable(unsigned id)
  33. {
  34. return msm_proc_comm(PCOM_CLKCTL_RPC_ENABLE, &id, NULL);
  35. }
  36. static inline void pc_clk_disable(unsigned id)
  37. {
  38. msm_proc_comm(PCOM_CLKCTL_RPC_DISABLE, &id, NULL);
  39. }
  40. static inline int pc_clk_set_rate(unsigned id, unsigned rate)
  41. {
  42. return msm_proc_comm(PCOM_CLKCTL_RPC_SET_RATE, &id, &rate);
  43. }
  44. static inline int pc_clk_set_min_rate(unsigned id, unsigned rate)
  45. {
  46. return msm_proc_comm(PCOM_CLKCTL_RPC_MIN_RATE, &id, &rate);
  47. }
  48. static inline int pc_clk_set_max_rate(unsigned id, unsigned rate)
  49. {
  50. return msm_proc_comm(PCOM_CLKCTL_RPC_MAX_RATE, &id, &rate);
  51. }
  52. static inline int pc_clk_set_flags(unsigned id, unsigned flags)
  53. {
  54. return msm_proc_comm(PCOM_CLKCTL_RPC_SET_FLAGS, &id, &flags);
  55. }
  56. static inline unsigned pc_clk_get_rate(unsigned id)
  57. {
  58. if (msm_proc_comm(PCOM_CLKCTL_RPC_RATE, &id, NULL))
  59. return 0;
  60. else
  61. return id;
  62. }
  63. static inline unsigned pc_clk_is_enabled(unsigned id)
  64. {
  65. if (msm_proc_comm(PCOM_CLKCTL_RPC_ENABLED, &id, NULL))
  66. return 0;
  67. else
  68. return id;
  69. }
  70. static inline int pc_pll_request(unsigned id, unsigned on)
  71. {
  72. on = !!on;
  73. return msm_proc_comm(PCOM_CLKCTL_RPC_PLL_REQUEST, &id, &on);
  74. }
  75. /*
  76. * Standard clock functions defined in include/linux/clk.h
  77. */
  78. struct clk *clk_get(struct device *dev, const char *id)
  79. {
  80. struct clk *clk;
  81. mutex_lock(&clocks_mutex);
  82. list_for_each_entry(clk, &clocks, list)
  83. if (!strcmp(id, clk->name) && clk->dev == dev)
  84. goto found_it;
  85. list_for_each_entry(clk, &clocks, list)
  86. if (!strcmp(id, clk->name) && clk->dev == NULL)
  87. goto found_it;
  88. clk = ERR_PTR(-ENOENT);
  89. found_it:
  90. mutex_unlock(&clocks_mutex);
  91. return clk;
  92. }
  93. EXPORT_SYMBOL(clk_get);
  94. void clk_put(struct clk *clk)
  95. {
  96. }
  97. EXPORT_SYMBOL(clk_put);
  98. int clk_enable(struct clk *clk)
  99. {
  100. unsigned long flags;
  101. spin_lock_irqsave(&clocks_lock, flags);
  102. clk->count++;
  103. if (clk->count == 1)
  104. pc_clk_enable(clk->id);
  105. spin_unlock_irqrestore(&clocks_lock, flags);
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(clk_enable);
  109. void clk_disable(struct clk *clk)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&clocks_lock, flags);
  113. BUG_ON(clk->count == 0);
  114. clk->count--;
  115. if (clk->count == 0)
  116. pc_clk_disable(clk->id);
  117. spin_unlock_irqrestore(&clocks_lock, flags);
  118. }
  119. EXPORT_SYMBOL(clk_disable);
  120. unsigned long clk_get_rate(struct clk *clk)
  121. {
  122. return pc_clk_get_rate(clk->id);
  123. }
  124. EXPORT_SYMBOL(clk_get_rate);
  125. int clk_set_rate(struct clk *clk, unsigned long rate)
  126. {
  127. int ret;
  128. if (clk->flags & CLKFLAG_USE_MIN_MAX_TO_SET) {
  129. ret = pc_clk_set_max_rate(clk->id, rate);
  130. if (ret)
  131. return ret;
  132. return pc_clk_set_min_rate(clk->id, rate);
  133. }
  134. return pc_clk_set_rate(clk->id, rate);
  135. }
  136. EXPORT_SYMBOL(clk_set_rate);
  137. int clk_set_parent(struct clk *clk, struct clk *parent)
  138. {
  139. return -ENOSYS;
  140. }
  141. EXPORT_SYMBOL(clk_set_parent);
  142. struct clk *clk_get_parent(struct clk *clk)
  143. {
  144. return ERR_PTR(-ENOSYS);
  145. }
  146. EXPORT_SYMBOL(clk_get_parent);
  147. int clk_set_flags(struct clk *clk, unsigned long flags)
  148. {
  149. if (clk == NULL || IS_ERR(clk))
  150. return -EINVAL;
  151. return pc_clk_set_flags(clk->id, flags);
  152. }
  153. EXPORT_SYMBOL(clk_set_flags);
  154. void __init msm_clock_init(void)
  155. {
  156. unsigned n;
  157. spin_lock_init(&clocks_lock);
  158. mutex_lock(&clocks_mutex);
  159. for (n = 0; n < msm_num_clocks; n++)
  160. list_add_tail(&msm_clocks[n].list, &clocks);
  161. mutex_unlock(&clocks_mutex);
  162. }
  163. /* The bootloader and/or AMSS may have left various clocks enabled.
  164. * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
  165. * not been explicitly enabled by a clk_enable() call.
  166. */
  167. static int __init clock_late_init(void)
  168. {
  169. unsigned long flags;
  170. struct clk *clk;
  171. unsigned count = 0;
  172. mutex_lock(&clocks_mutex);
  173. list_for_each_entry(clk, &clocks, list) {
  174. if (clk->flags & CLKFLAG_AUTO_OFF) {
  175. spin_lock_irqsave(&clocks_lock, flags);
  176. if (!clk->count) {
  177. count++;
  178. pc_clk_disable(clk->id);
  179. }
  180. spin_unlock_irqrestore(&clocks_lock, flags);
  181. }
  182. }
  183. mutex_unlock(&clocks_mutex);
  184. pr_info("clock_late_init() disabled %d unused clocks\n", count);
  185. return 0;
  186. }
  187. late_initcall(clock_late_init);