clock.c 7.6 KB

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