clock.c 7.8 KB

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