clock.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Based on arch/arm/plat-omap/clock.c
  3. *
  4. * Copyright (C) 2004 - 2005 Nokia corporation
  5. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  6. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  7. * Copyright 2007 Freescale Semiconductor, Inc. All Rights Reserved.
  8. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301, USA.
  23. */
  24. /* #define DEBUG */
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel.h>
  31. #include <linux/list.h>
  32. #include <linux/module.h>
  33. #include <linux/mutex.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/semaphore.h>
  37. #include <linux/string.h>
  38. #include <linux/version.h>
  39. #include <asm/arch/clock.h>
  40. static LIST_HEAD(clocks);
  41. static DEFINE_MUTEX(clocks_mutex);
  42. /*-------------------------------------------------------------------------
  43. * Standard clock functions defined in include/linux/clk.h
  44. *-------------------------------------------------------------------------*/
  45. /*
  46. * Retrieve a clock by name.
  47. *
  48. * Note that we first try to use device id on the bus
  49. * and clock name. If this fails, we try to use "<name>.<id>". If this fails,
  50. * we try to use clock name only.
  51. * The reference count to the clock's module owner ref count is incremented.
  52. */
  53. struct clk *clk_get(struct device *dev, const char *id)
  54. {
  55. struct clk *p, *clk = ERR_PTR(-ENOENT);
  56. int idno;
  57. const char *str;
  58. if (id == NULL)
  59. return clk;
  60. if (dev == NULL || dev->bus != &platform_bus_type)
  61. idno = -1;
  62. else
  63. idno = to_platform_device(dev)->id;
  64. mutex_lock(&clocks_mutex);
  65. list_for_each_entry(p, &clocks, node) {
  66. if (p->id == idno &&
  67. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  68. clk = p;
  69. goto found;
  70. }
  71. }
  72. str = strrchr(id, '.');
  73. if (str) {
  74. int cnt = str - id;
  75. str++;
  76. idno = simple_strtol(str, NULL, 10);
  77. list_for_each_entry(p, &clocks, node) {
  78. if (p->id == idno &&
  79. strlen(p->name) == cnt &&
  80. strncmp(id, p->name, cnt) == 0 &&
  81. try_module_get(p->owner)) {
  82. clk = p;
  83. goto found;
  84. }
  85. }
  86. }
  87. list_for_each_entry(p, &clocks, node) {
  88. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  89. clk = p;
  90. goto found;
  91. }
  92. }
  93. printk(KERN_WARNING "clk: Unable to get requested clock: %s\n", id);
  94. found:
  95. mutex_unlock(&clocks_mutex);
  96. return clk;
  97. }
  98. EXPORT_SYMBOL(clk_get);
  99. static void __clk_disable(struct clk *clk)
  100. {
  101. if (clk == NULL || IS_ERR(clk))
  102. return;
  103. __clk_disable(clk->parent);
  104. __clk_disable(clk->secondary);
  105. if (!(--clk->usecount) && clk->disable)
  106. clk->disable(clk);
  107. }
  108. static int __clk_enable(struct clk *clk)
  109. {
  110. if (clk == NULL || IS_ERR(clk))
  111. return -EINVAL;
  112. __clk_enable(clk->parent);
  113. __clk_enable(clk->secondary);
  114. if (clk->usecount++ == 0 && clk->enable)
  115. clk->enable(clk);
  116. return 0;
  117. }
  118. /* This function increments the reference count on the clock and enables the
  119. * clock if not already enabled. The parent clock tree is recursively enabled
  120. */
  121. int clk_enable(struct clk *clk)
  122. {
  123. int ret = 0;
  124. if (clk == NULL || IS_ERR(clk))
  125. return -EINVAL;
  126. mutex_lock(&clocks_mutex);
  127. ret = __clk_enable(clk);
  128. mutex_unlock(&clocks_mutex);
  129. return ret;
  130. }
  131. EXPORT_SYMBOL(clk_enable);
  132. /* This function decrements the reference count on the clock and disables
  133. * the clock when reference count is 0. The parent clock tree is
  134. * recursively disabled
  135. */
  136. void clk_disable(struct clk *clk)
  137. {
  138. if (clk == NULL || IS_ERR(clk))
  139. return;
  140. mutex_lock(&clocks_mutex);
  141. __clk_disable(clk);
  142. mutex_unlock(&clocks_mutex);
  143. }
  144. EXPORT_SYMBOL(clk_disable);
  145. /* Retrieve the *current* clock rate. If the clock itself
  146. * does not provide a special calculation routine, ask
  147. * its parent and so on, until one is able to return
  148. * a valid clock rate
  149. */
  150. unsigned long clk_get_rate(struct clk *clk)
  151. {
  152. if (clk == NULL || IS_ERR(clk))
  153. return 0UL;
  154. if (clk->get_rate)
  155. return clk->get_rate(clk);
  156. return clk_get_rate(clk->parent);
  157. }
  158. EXPORT_SYMBOL(clk_get_rate);
  159. /* Decrement the clock's module reference count */
  160. void clk_put(struct clk *clk)
  161. {
  162. if (clk && !IS_ERR(clk))
  163. module_put(clk->owner);
  164. }
  165. EXPORT_SYMBOL(clk_put);
  166. /* Round the requested clock rate to the nearest supported
  167. * rate that is less than or equal to the requested rate.
  168. * This is dependent on the clock's current parent.
  169. */
  170. long clk_round_rate(struct clk *clk, unsigned long rate)
  171. {
  172. if (clk == NULL || IS_ERR(clk) || !clk->round_rate)
  173. return 0;
  174. return clk->round_rate(clk, rate);
  175. }
  176. EXPORT_SYMBOL(clk_round_rate);
  177. /* Set the clock to the requested clock rate. The rate must
  178. * match a supported rate exactly based on what clk_round_rate returns
  179. */
  180. int clk_set_rate(struct clk *clk, unsigned long rate)
  181. {
  182. int ret = -EINVAL;
  183. if (clk == NULL || IS_ERR(clk) || clk->set_rate == NULL || rate == 0)
  184. return ret;
  185. mutex_lock(&clocks_mutex);
  186. ret = clk->set_rate(clk, rate);
  187. mutex_unlock(&clocks_mutex);
  188. return ret;
  189. }
  190. EXPORT_SYMBOL(clk_set_rate);
  191. /* Set the clock's parent to another clock source */
  192. int clk_set_parent(struct clk *clk, struct clk *parent)
  193. {
  194. int ret = -EINVAL;
  195. if (clk == NULL || IS_ERR(clk) || parent == NULL ||
  196. IS_ERR(parent) || clk->set_parent == NULL)
  197. return ret;
  198. mutex_lock(&clocks_mutex);
  199. ret = clk->set_parent(clk, parent);
  200. if (ret == 0)
  201. clk->parent = parent;
  202. mutex_unlock(&clocks_mutex);
  203. return ret;
  204. }
  205. EXPORT_SYMBOL(clk_set_parent);
  206. /* Retrieve the clock's parent clock source */
  207. struct clk *clk_get_parent(struct clk *clk)
  208. {
  209. struct clk *ret = NULL;
  210. if (clk == NULL || IS_ERR(clk))
  211. return ret;
  212. return clk->parent;
  213. }
  214. EXPORT_SYMBOL(clk_get_parent);
  215. /*
  216. * Add a new clock to the clock tree.
  217. */
  218. int clk_register(struct clk *clk)
  219. {
  220. if (clk == NULL || IS_ERR(clk))
  221. return -EINVAL;
  222. mutex_lock(&clocks_mutex);
  223. list_add(&clk->node, &clocks);
  224. mutex_unlock(&clocks_mutex);
  225. return 0;
  226. }
  227. EXPORT_SYMBOL(clk_register);
  228. /* Remove a clock from the clock tree */
  229. void clk_unregister(struct clk *clk)
  230. {
  231. if (clk == NULL || IS_ERR(clk))
  232. return;
  233. mutex_lock(&clocks_mutex);
  234. list_del(&clk->node);
  235. mutex_unlock(&clocks_mutex);
  236. }
  237. EXPORT_SYMBOL(clk_unregister);
  238. #ifdef CONFIG_PROC_FS
  239. static int mxc_clock_read_proc(char *page, char **start, off_t off,
  240. int count, int *eof, void *data)
  241. {
  242. struct clk *clkp;
  243. char *p = page;
  244. int len;
  245. list_for_each_entry(clkp, &clocks, node) {
  246. p += sprintf(p, "%s-%d:\t\t%lu, %d", clkp->name, clkp->id,
  247. clk_get_rate(clkp), clkp->usecount);
  248. if (clkp->parent)
  249. p += sprintf(p, ", %s-%d\n", clkp->parent->name,
  250. clkp->parent->id);
  251. else
  252. p += sprintf(p, "\n");
  253. }
  254. len = (p - page) - off;
  255. if (len < 0)
  256. len = 0;
  257. *eof = (len <= count) ? 1 : 0;
  258. *start = page + off;
  259. return len;
  260. }
  261. static int __init mxc_setup_proc_entry(void)
  262. {
  263. struct proc_dir_entry *res;
  264. res = create_proc_read_entry("cpu/clocks", 0, NULL,
  265. mxc_clock_read_proc, NULL);
  266. if (!res) {
  267. printk(KERN_ERR "Failed to create proc/cpu/clocks\n");
  268. return -ENOMEM;
  269. }
  270. return 0;
  271. }
  272. late_initcall(mxc_setup_proc_entry);
  273. #endif