clock.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/init.h>
  18. #include <linux/module.h>
  19. #include <linux/list.h>
  20. #include <linux/err.h>
  21. #include <linux/clk.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/ctype.h>
  25. #include <linux/pm_qos_params.h>
  26. #include <mach/clk.h>
  27. #include "clock.h"
  28. #include "proc_comm.h"
  29. #include "clock-7x30.h"
  30. static DEFINE_MUTEX(clocks_mutex);
  31. static DEFINE_SPINLOCK(clocks_lock);
  32. static LIST_HEAD(clocks);
  33. struct clk *msm_clocks;
  34. unsigned msm_num_clocks;
  35. /*
  36. * Bitmap of enabled clocks, excluding ACPU which is always
  37. * enabled
  38. */
  39. static DECLARE_BITMAP(clock_map_enabled, NR_CLKS);
  40. static DEFINE_SPINLOCK(clock_map_lock);
  41. /*
  42. * Standard clock functions defined in include/linux/clk.h
  43. */
  44. struct clk *clk_get(struct device *dev, const char *id)
  45. {
  46. struct clk *clk;
  47. mutex_lock(&clocks_mutex);
  48. list_for_each_entry(clk, &clocks, list)
  49. if (!strcmp(id, clk->name) && clk->dev == dev)
  50. goto found_it;
  51. list_for_each_entry(clk, &clocks, list)
  52. if (!strcmp(id, clk->name) && clk->dev == NULL)
  53. goto found_it;
  54. clk = ERR_PTR(-ENOENT);
  55. found_it:
  56. mutex_unlock(&clocks_mutex);
  57. return clk;
  58. }
  59. EXPORT_SYMBOL(clk_get);
  60. void clk_put(struct clk *clk)
  61. {
  62. }
  63. EXPORT_SYMBOL(clk_put);
  64. int clk_enable(struct clk *clk)
  65. {
  66. unsigned long flags;
  67. spin_lock_irqsave(&clocks_lock, flags);
  68. clk->count++;
  69. if (clk->count == 1) {
  70. clk->ops->enable(clk->id);
  71. spin_lock(&clock_map_lock);
  72. clock_map_enabled[BIT_WORD(clk->id)] |= BIT_MASK(clk->id);
  73. spin_unlock(&clock_map_lock);
  74. }
  75. spin_unlock_irqrestore(&clocks_lock, flags);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(clk_enable);
  79. void clk_disable(struct clk *clk)
  80. {
  81. unsigned long flags;
  82. spin_lock_irqsave(&clocks_lock, flags);
  83. BUG_ON(clk->count == 0);
  84. clk->count--;
  85. if (clk->count == 0) {
  86. clk->ops->disable(clk->id);
  87. spin_lock(&clock_map_lock);
  88. clock_map_enabled[BIT_WORD(clk->id)] &= ~BIT_MASK(clk->id);
  89. spin_unlock(&clock_map_lock);
  90. }
  91. spin_unlock_irqrestore(&clocks_lock, flags);
  92. }
  93. EXPORT_SYMBOL(clk_disable);
  94. int clk_reset(struct clk *clk, enum clk_reset_action action)
  95. {
  96. if (!clk->ops->reset)
  97. clk->ops->reset = &pc_clk_reset;
  98. return clk->ops->reset(clk->remote_id, action);
  99. }
  100. EXPORT_SYMBOL(clk_reset);
  101. unsigned long clk_get_rate(struct clk *clk)
  102. {
  103. return clk->ops->get_rate(clk->id);
  104. }
  105. EXPORT_SYMBOL(clk_get_rate);
  106. int clk_set_rate(struct clk *clk, unsigned long rate)
  107. {
  108. return clk->ops->set_rate(clk->id, rate);
  109. }
  110. EXPORT_SYMBOL(clk_set_rate);
  111. long clk_round_rate(struct clk *clk, unsigned long rate)
  112. {
  113. return clk->ops->round_rate(clk->id, rate);
  114. }
  115. EXPORT_SYMBOL(clk_round_rate);
  116. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  117. {
  118. return clk->ops->set_min_rate(clk->id, rate);
  119. }
  120. EXPORT_SYMBOL(clk_set_min_rate);
  121. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  122. {
  123. return clk->ops->set_max_rate(clk->id, rate);
  124. }
  125. EXPORT_SYMBOL(clk_set_max_rate);
  126. int clk_set_parent(struct clk *clk, struct clk *parent)
  127. {
  128. return -ENOSYS;
  129. }
  130. EXPORT_SYMBOL(clk_set_parent);
  131. struct clk *clk_get_parent(struct clk *clk)
  132. {
  133. return ERR_PTR(-ENOSYS);
  134. }
  135. EXPORT_SYMBOL(clk_get_parent);
  136. int clk_set_flags(struct clk *clk, unsigned long flags)
  137. {
  138. if (clk == NULL || IS_ERR(clk))
  139. return -EINVAL;
  140. return clk->ops->set_flags(clk->id, flags);
  141. }
  142. EXPORT_SYMBOL(clk_set_flags);
  143. /* EBI1 is the only shared clock that several clients want to vote on as of
  144. * this commit. If this changes in the future, then it might be better to
  145. * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
  146. * generic to support different clocks.
  147. */
  148. static struct clk *ebi1_clk;
  149. static void __init set_clock_ops(struct clk *clk)
  150. {
  151. if (!clk->ops) {
  152. clk->ops = &clk_ops_pcom;
  153. clk->id = clk->remote_id;
  154. }
  155. }
  156. void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
  157. {
  158. unsigned n;
  159. spin_lock_init(&clocks_lock);
  160. mutex_lock(&clocks_mutex);
  161. msm_clocks = clock_tbl;
  162. msm_num_clocks = num_clocks;
  163. for (n = 0; n < msm_num_clocks; n++) {
  164. set_clock_ops(&msm_clocks[n]);
  165. list_add_tail(&msm_clocks[n].list, &clocks);
  166. }
  167. mutex_unlock(&clocks_mutex);
  168. ebi1_clk = clk_get(NULL, "ebi1_clk");
  169. BUG_ON(ebi1_clk == NULL);
  170. }
  171. #if defined(CONFIG_DEBUG_FS)
  172. static struct clk *msm_clock_get_nth(unsigned index)
  173. {
  174. if (index < msm_num_clocks)
  175. return msm_clocks + index;
  176. else
  177. return 0;
  178. }
  179. static int clock_debug_rate_set(void *data, u64 val)
  180. {
  181. struct clk *clock = data;
  182. int ret;
  183. /* Only increases to max rate will succeed, but that's actually good
  184. * for debugging purposes. So we don't check for error. */
  185. if (clock->flags & CLK_MAX)
  186. clk_set_max_rate(clock, val);
  187. if (clock->flags & CLK_MIN)
  188. ret = clk_set_min_rate(clock, val);
  189. else
  190. ret = clk_set_rate(clock, val);
  191. if (ret != 0)
  192. printk(KERN_ERR "clk_set%s_rate failed (%d)\n",
  193. (clock->flags & CLK_MIN) ? "_min" : "", ret);
  194. return ret;
  195. }
  196. static int clock_debug_rate_get(void *data, u64 *val)
  197. {
  198. struct clk *clock = data;
  199. *val = clk_get_rate(clock);
  200. return 0;
  201. }
  202. static int clock_debug_enable_set(void *data, u64 val)
  203. {
  204. struct clk *clock = data;
  205. int rc = 0;
  206. if (val)
  207. rc = clock->ops->enable(clock->id);
  208. else
  209. clock->ops->disable(clock->id);
  210. return rc;
  211. }
  212. static int clock_debug_enable_get(void *data, u64 *val)
  213. {
  214. struct clk *clock = data;
  215. *val = clock->ops->is_enabled(clock->id);
  216. return 0;
  217. }
  218. static int clock_debug_local_get(void *data, u64 *val)
  219. {
  220. struct clk *clock = data;
  221. *val = clock->ops != &clk_ops_pcom;
  222. return 0;
  223. }
  224. DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
  225. clock_debug_rate_set, "%llu\n");
  226. DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
  227. clock_debug_enable_set, "%llu\n");
  228. DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
  229. NULL, "%llu\n");
  230. static int __init clock_debug_init(void)
  231. {
  232. struct dentry *dent_rate, *dent_enable, *dent_local;
  233. struct clk *clock;
  234. unsigned n = 0;
  235. char temp[50], *ptr;
  236. dent_rate = debugfs_create_dir("clk_rate", 0);
  237. if (IS_ERR(dent_rate))
  238. return PTR_ERR(dent_rate);
  239. dent_enable = debugfs_create_dir("clk_enable", 0);
  240. if (IS_ERR(dent_enable))
  241. return PTR_ERR(dent_enable);
  242. dent_local = debugfs_create_dir("clk_local", NULL);
  243. if (IS_ERR(dent_local))
  244. return PTR_ERR(dent_local);
  245. while ((clock = msm_clock_get_nth(n++)) != 0) {
  246. strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1);
  247. for (ptr = temp; *ptr; ptr++)
  248. *ptr = tolower(*ptr);
  249. debugfs_create_file(temp, 0644, dent_rate,
  250. clock, &clock_rate_fops);
  251. debugfs_create_file(temp, 0644, dent_enable,
  252. clock, &clock_enable_fops);
  253. debugfs_create_file(temp, S_IRUGO, dent_local,
  254. clock, &clock_local_fops);
  255. }
  256. return 0;
  257. }
  258. device_initcall(clock_debug_init);
  259. #endif
  260. /* The bootloader and/or AMSS may have left various clocks enabled.
  261. * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
  262. * not been explicitly enabled by a clk_enable() call.
  263. */
  264. static int __init clock_late_init(void)
  265. {
  266. unsigned long flags;
  267. struct clk *clk;
  268. unsigned count = 0;
  269. mutex_lock(&clocks_mutex);
  270. list_for_each_entry(clk, &clocks, list) {
  271. if (clk->flags & CLKFLAG_AUTO_OFF) {
  272. spin_lock_irqsave(&clocks_lock, flags);
  273. if (!clk->count) {
  274. count++;
  275. clk->ops->auto_off(clk->id);
  276. }
  277. spin_unlock_irqrestore(&clocks_lock, flags);
  278. }
  279. }
  280. mutex_unlock(&clocks_mutex);
  281. pr_info("clock_late_init() disabled %d unused clocks\n", count);
  282. return 0;
  283. }
  284. late_initcall(clock_late_init);