clock.c 7.7 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. #include "clock-pcom.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. * Standard clock functions defined in include/linux/clk.h
  38. */
  39. struct clk *clk_get(struct device *dev, const char *id)
  40. {
  41. struct clk *clk;
  42. mutex_lock(&clocks_mutex);
  43. list_for_each_entry(clk, &clocks, list)
  44. if (!strcmp(id, clk->name) && clk->dev == dev)
  45. goto found_it;
  46. list_for_each_entry(clk, &clocks, list)
  47. if (!strcmp(id, clk->name) && clk->dev == NULL)
  48. goto found_it;
  49. clk = ERR_PTR(-ENOENT);
  50. found_it:
  51. mutex_unlock(&clocks_mutex);
  52. return clk;
  53. }
  54. EXPORT_SYMBOL(clk_get);
  55. void clk_put(struct clk *clk)
  56. {
  57. }
  58. EXPORT_SYMBOL(clk_put);
  59. int clk_enable(struct clk *clk)
  60. {
  61. unsigned long flags;
  62. spin_lock_irqsave(&clocks_lock, flags);
  63. clk->count++;
  64. if (clk->count == 1)
  65. clk->ops->enable(clk->id);
  66. spin_unlock_irqrestore(&clocks_lock, flags);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL(clk_enable);
  70. void clk_disable(struct clk *clk)
  71. {
  72. unsigned long flags;
  73. spin_lock_irqsave(&clocks_lock, flags);
  74. BUG_ON(clk->count == 0);
  75. clk->count--;
  76. if (clk->count == 0)
  77. clk->ops->disable(clk->id);
  78. spin_unlock_irqrestore(&clocks_lock, flags);
  79. }
  80. EXPORT_SYMBOL(clk_disable);
  81. int clk_reset(struct clk *clk, enum clk_reset_action action)
  82. {
  83. if (!clk->ops->reset)
  84. clk->ops->reset = &pc_clk_reset;
  85. return clk->ops->reset(clk->remote_id, action);
  86. }
  87. EXPORT_SYMBOL(clk_reset);
  88. unsigned long clk_get_rate(struct clk *clk)
  89. {
  90. return clk->ops->get_rate(clk->id);
  91. }
  92. EXPORT_SYMBOL(clk_get_rate);
  93. int clk_set_rate(struct clk *clk, unsigned long rate)
  94. {
  95. int ret;
  96. if (clk->flags & CLKFLAG_MAX) {
  97. ret = clk->ops->set_max_rate(clk->id, rate);
  98. if (ret)
  99. return ret;
  100. }
  101. if (clk->flags & CLKFLAG_MIN) {
  102. ret = clk->ops->set_min_rate(clk->id, rate);
  103. if (ret)
  104. return ret;
  105. }
  106. if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN)
  107. return ret;
  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. mutex_lock(&clocks_mutex);
  160. msm_clocks = clock_tbl;
  161. msm_num_clocks = num_clocks;
  162. for (n = 0; n < msm_num_clocks; n++) {
  163. set_clock_ops(&msm_clocks[n]);
  164. list_add_tail(&msm_clocks[n].list, &clocks);
  165. }
  166. mutex_unlock(&clocks_mutex);
  167. ebi1_clk = clk_get(NULL, "ebi1_clk");
  168. BUG_ON(ebi1_clk == NULL);
  169. }
  170. #if defined(CONFIG_DEBUG_FS)
  171. static struct clk *msm_clock_get_nth(unsigned index)
  172. {
  173. if (index < msm_num_clocks)
  174. return msm_clocks + index;
  175. else
  176. return 0;
  177. }
  178. static int clock_debug_rate_set(void *data, u64 val)
  179. {
  180. struct clk *clock = data;
  181. int ret;
  182. /* Only increases to max rate will succeed, but that's actually good
  183. * for debugging purposes. So we don't check for error. */
  184. if (clock->flags & CLK_MAX)
  185. clk_set_max_rate(clock, val);
  186. if (clock->flags & CLK_MIN)
  187. ret = clk_set_min_rate(clock, val);
  188. else
  189. ret = clk_set_rate(clock, val);
  190. if (ret != 0)
  191. printk(KERN_ERR "clk_set%s_rate failed (%d)\n",
  192. (clock->flags & CLK_MIN) ? "_min" : "", ret);
  193. return ret;
  194. }
  195. static int clock_debug_rate_get(void *data, u64 *val)
  196. {
  197. struct clk *clock = data;
  198. *val = clk_get_rate(clock);
  199. return 0;
  200. }
  201. static int clock_debug_enable_set(void *data, u64 val)
  202. {
  203. struct clk *clock = data;
  204. int rc = 0;
  205. if (val)
  206. rc = clock->ops->enable(clock->id);
  207. else
  208. clock->ops->disable(clock->id);
  209. return rc;
  210. }
  211. static int clock_debug_enable_get(void *data, u64 *val)
  212. {
  213. struct clk *clock = data;
  214. *val = clock->ops->is_enabled(clock->id);
  215. return 0;
  216. }
  217. static int clock_debug_local_get(void *data, u64 *val)
  218. {
  219. struct clk *clock = data;
  220. *val = clock->ops != &clk_ops_pcom;
  221. return 0;
  222. }
  223. DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
  224. clock_debug_rate_set, "%llu\n");
  225. DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
  226. clock_debug_enable_set, "%llu\n");
  227. DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
  228. NULL, "%llu\n");
  229. static int __init clock_debug_init(void)
  230. {
  231. struct dentry *dent_rate, *dent_enable, *dent_local;
  232. struct clk *clock;
  233. unsigned n = 0;
  234. char temp[50], *ptr;
  235. dent_rate = debugfs_create_dir("clk_rate", 0);
  236. if (IS_ERR(dent_rate))
  237. return PTR_ERR(dent_rate);
  238. dent_enable = debugfs_create_dir("clk_enable", 0);
  239. if (IS_ERR(dent_enable))
  240. return PTR_ERR(dent_enable);
  241. dent_local = debugfs_create_dir("clk_local", NULL);
  242. if (IS_ERR(dent_local))
  243. return PTR_ERR(dent_local);
  244. while ((clock = msm_clock_get_nth(n++)) != 0) {
  245. strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1);
  246. for (ptr = temp; *ptr; ptr++)
  247. *ptr = tolower(*ptr);
  248. debugfs_create_file(temp, 0644, dent_rate,
  249. clock, &clock_rate_fops);
  250. debugfs_create_file(temp, 0644, dent_enable,
  251. clock, &clock_enable_fops);
  252. debugfs_create_file(temp, S_IRUGO, dent_local,
  253. clock, &clock_local_fops);
  254. }
  255. return 0;
  256. }
  257. device_initcall(clock_debug_init);
  258. #endif
  259. /* The bootloader and/or AMSS may have left various clocks enabled.
  260. * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
  261. * not been explicitly enabled by a clk_enable() call.
  262. */
  263. static int __init clock_late_init(void)
  264. {
  265. unsigned long flags;
  266. struct clk *clk;
  267. unsigned count = 0;
  268. mutex_lock(&clocks_mutex);
  269. list_for_each_entry(clk, &clocks, list) {
  270. if (clk->flags & CLKFLAG_AUTO_OFF) {
  271. spin_lock_irqsave(&clocks_lock, flags);
  272. if (!clk->count) {
  273. count++;
  274. clk->ops->auto_off(clk->id);
  275. }
  276. spin_unlock_irqrestore(&clocks_lock, flags);
  277. }
  278. }
  279. mutex_unlock(&clocks_mutex);
  280. pr_info("clock_late_init() disabled %d unused clocks\n", count);
  281. return 0;
  282. }
  283. late_initcall(clock_late_init);