clock.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. int ret;
  109. if (clk->flags & CLKFLAG_MAX) {
  110. ret = clk->ops->set_max_rate(clk->id, rate);
  111. if (ret)
  112. return ret;
  113. }
  114. if (clk->flags & CLKFLAG_MIN) {
  115. ret = clk->ops->set_min_rate(clk->id, rate);
  116. if (ret)
  117. return ret;
  118. }
  119. if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN)
  120. return ret;
  121. return clk->ops->set_rate(clk->id, rate);
  122. }
  123. EXPORT_SYMBOL(clk_set_rate);
  124. long clk_round_rate(struct clk *clk, unsigned long rate)
  125. {
  126. return clk->ops->round_rate(clk->id, rate);
  127. }
  128. EXPORT_SYMBOL(clk_round_rate);
  129. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  130. {
  131. return clk->ops->set_min_rate(clk->id, rate);
  132. }
  133. EXPORT_SYMBOL(clk_set_min_rate);
  134. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  135. {
  136. return clk->ops->set_max_rate(clk->id, rate);
  137. }
  138. EXPORT_SYMBOL(clk_set_max_rate);
  139. int clk_set_parent(struct clk *clk, struct clk *parent)
  140. {
  141. return -ENOSYS;
  142. }
  143. EXPORT_SYMBOL(clk_set_parent);
  144. struct clk *clk_get_parent(struct clk *clk)
  145. {
  146. return ERR_PTR(-ENOSYS);
  147. }
  148. EXPORT_SYMBOL(clk_get_parent);
  149. int clk_set_flags(struct clk *clk, unsigned long flags)
  150. {
  151. if (clk == NULL || IS_ERR(clk))
  152. return -EINVAL;
  153. return clk->ops->set_flags(clk->id, flags);
  154. }
  155. EXPORT_SYMBOL(clk_set_flags);
  156. /* EBI1 is the only shared clock that several clients want to vote on as of
  157. * this commit. If this changes in the future, then it might be better to
  158. * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
  159. * generic to support different clocks.
  160. */
  161. static struct clk *ebi1_clk;
  162. static void __init set_clock_ops(struct clk *clk)
  163. {
  164. if (!clk->ops) {
  165. clk->ops = &clk_ops_pcom;
  166. clk->id = clk->remote_id;
  167. }
  168. }
  169. void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
  170. {
  171. unsigned n;
  172. spin_lock_init(&clocks_lock);
  173. mutex_lock(&clocks_mutex);
  174. msm_clocks = clock_tbl;
  175. msm_num_clocks = num_clocks;
  176. for (n = 0; n < msm_num_clocks; n++) {
  177. set_clock_ops(&msm_clocks[n]);
  178. list_add_tail(&msm_clocks[n].list, &clocks);
  179. }
  180. mutex_unlock(&clocks_mutex);
  181. ebi1_clk = clk_get(NULL, "ebi1_clk");
  182. BUG_ON(ebi1_clk == NULL);
  183. }
  184. #if defined(CONFIG_DEBUG_FS)
  185. static struct clk *msm_clock_get_nth(unsigned index)
  186. {
  187. if (index < msm_num_clocks)
  188. return msm_clocks + index;
  189. else
  190. return 0;
  191. }
  192. static int clock_debug_rate_set(void *data, u64 val)
  193. {
  194. struct clk *clock = data;
  195. int ret;
  196. /* Only increases to max rate will succeed, but that's actually good
  197. * for debugging purposes. So we don't check for error. */
  198. if (clock->flags & CLK_MAX)
  199. clk_set_max_rate(clock, val);
  200. if (clock->flags & CLK_MIN)
  201. ret = clk_set_min_rate(clock, val);
  202. else
  203. ret = clk_set_rate(clock, val);
  204. if (ret != 0)
  205. printk(KERN_ERR "clk_set%s_rate failed (%d)\n",
  206. (clock->flags & CLK_MIN) ? "_min" : "", ret);
  207. return ret;
  208. }
  209. static int clock_debug_rate_get(void *data, u64 *val)
  210. {
  211. struct clk *clock = data;
  212. *val = clk_get_rate(clock);
  213. return 0;
  214. }
  215. static int clock_debug_enable_set(void *data, u64 val)
  216. {
  217. struct clk *clock = data;
  218. int rc = 0;
  219. if (val)
  220. rc = clock->ops->enable(clock->id);
  221. else
  222. clock->ops->disable(clock->id);
  223. return rc;
  224. }
  225. static int clock_debug_enable_get(void *data, u64 *val)
  226. {
  227. struct clk *clock = data;
  228. *val = clock->ops->is_enabled(clock->id);
  229. return 0;
  230. }
  231. static int clock_debug_local_get(void *data, u64 *val)
  232. {
  233. struct clk *clock = data;
  234. *val = clock->ops != &clk_ops_pcom;
  235. return 0;
  236. }
  237. DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
  238. clock_debug_rate_set, "%llu\n");
  239. DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
  240. clock_debug_enable_set, "%llu\n");
  241. DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
  242. NULL, "%llu\n");
  243. static int __init clock_debug_init(void)
  244. {
  245. struct dentry *dent_rate, *dent_enable, *dent_local;
  246. struct clk *clock;
  247. unsigned n = 0;
  248. char temp[50], *ptr;
  249. dent_rate = debugfs_create_dir("clk_rate", 0);
  250. if (IS_ERR(dent_rate))
  251. return PTR_ERR(dent_rate);
  252. dent_enable = debugfs_create_dir("clk_enable", 0);
  253. if (IS_ERR(dent_enable))
  254. return PTR_ERR(dent_enable);
  255. dent_local = debugfs_create_dir("clk_local", NULL);
  256. if (IS_ERR(dent_local))
  257. return PTR_ERR(dent_local);
  258. while ((clock = msm_clock_get_nth(n++)) != 0) {
  259. strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1);
  260. for (ptr = temp; *ptr; ptr++)
  261. *ptr = tolower(*ptr);
  262. debugfs_create_file(temp, 0644, dent_rate,
  263. clock, &clock_rate_fops);
  264. debugfs_create_file(temp, 0644, dent_enable,
  265. clock, &clock_enable_fops);
  266. debugfs_create_file(temp, S_IRUGO, dent_local,
  267. clock, &clock_local_fops);
  268. }
  269. return 0;
  270. }
  271. device_initcall(clock_debug_init);
  272. #endif
  273. /* The bootloader and/or AMSS may have left various clocks enabled.
  274. * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
  275. * not been explicitly enabled by a clk_enable() call.
  276. */
  277. static int __init clock_late_init(void)
  278. {
  279. unsigned long flags;
  280. struct clk *clk;
  281. unsigned count = 0;
  282. mutex_lock(&clocks_mutex);
  283. list_for_each_entry(clk, &clocks, list) {
  284. if (clk->flags & CLKFLAG_AUTO_OFF) {
  285. spin_lock_irqsave(&clocks_lock, flags);
  286. if (!clk->count) {
  287. count++;
  288. clk->ops->auto_off(clk->id);
  289. }
  290. spin_unlock_irqrestore(&clocks_lock, flags);
  291. }
  292. }
  293. mutex_unlock(&clocks_mutex);
  294. pr_info("clock_late_init() disabled %d unused clocks\n", count);
  295. return 0;
  296. }
  297. late_initcall(clock_late_init);