clock.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <mach/clock.h>
  39. static LIST_HEAD(clocks);
  40. static DEFINE_MUTEX(clocks_mutex);
  41. /*-------------------------------------------------------------------------
  42. * Standard clock functions defined in include/linux/clk.h
  43. *-------------------------------------------------------------------------*/
  44. /*
  45. * Retrieve a clock by name.
  46. *
  47. * Note that we first try to use device id on the bus
  48. * and clock name. If this fails, we try to use "<name>.<id>". If this fails,
  49. * we try to use clock name only.
  50. * The reference count to the clock's module owner ref count is incremented.
  51. */
  52. struct clk *clk_get(struct device *dev, const char *id)
  53. {
  54. struct clk *p, *clk = ERR_PTR(-ENOENT);
  55. int idno;
  56. const char *str;
  57. if (id == NULL)
  58. return clk;
  59. if (dev == NULL || dev->bus != &platform_bus_type)
  60. idno = -1;
  61. else
  62. idno = to_platform_device(dev)->id;
  63. mutex_lock(&clocks_mutex);
  64. list_for_each_entry(p, &clocks, node) {
  65. if (p->id == idno &&
  66. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  67. clk = p;
  68. goto found;
  69. }
  70. }
  71. str = strrchr(id, '.');
  72. if (str) {
  73. int cnt = str - id;
  74. str++;
  75. idno = simple_strtol(str, NULL, 10);
  76. list_for_each_entry(p, &clocks, node) {
  77. if (p->id == idno &&
  78. strlen(p->name) == cnt &&
  79. strncmp(id, p->name, cnt) == 0 &&
  80. try_module_get(p->owner)) {
  81. clk = p;
  82. goto found;
  83. }
  84. }
  85. }
  86. list_for_each_entry(p, &clocks, node) {
  87. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  88. clk = p;
  89. goto found;
  90. }
  91. }
  92. printk(KERN_WARNING "clk: Unable to get requested clock: %s\n", id);
  93. found:
  94. mutex_unlock(&clocks_mutex);
  95. return clk;
  96. }
  97. EXPORT_SYMBOL(clk_get);
  98. static void __clk_disable(struct clk *clk)
  99. {
  100. if (clk == NULL || IS_ERR(clk))
  101. return;
  102. __clk_disable(clk->parent);
  103. __clk_disable(clk->secondary);
  104. if (!(--clk->usecount) && clk->disable)
  105. clk->disable(clk);
  106. }
  107. static int __clk_enable(struct clk *clk)
  108. {
  109. if (clk == NULL || IS_ERR(clk))
  110. return -EINVAL;
  111. __clk_enable(clk->parent);
  112. __clk_enable(clk->secondary);
  113. if (clk->usecount++ == 0 && clk->enable)
  114. clk->enable(clk);
  115. return 0;
  116. }
  117. /* This function increments the reference count on the clock and enables the
  118. * clock if not already enabled. The parent clock tree is recursively enabled
  119. */
  120. int clk_enable(struct clk *clk)
  121. {
  122. int ret = 0;
  123. if (clk == NULL || IS_ERR(clk))
  124. return -EINVAL;
  125. mutex_lock(&clocks_mutex);
  126. ret = __clk_enable(clk);
  127. mutex_unlock(&clocks_mutex);
  128. return ret;
  129. }
  130. EXPORT_SYMBOL(clk_enable);
  131. /* This function decrements the reference count on the clock and disables
  132. * the clock when reference count is 0. The parent clock tree is
  133. * recursively disabled
  134. */
  135. void clk_disable(struct clk *clk)
  136. {
  137. if (clk == NULL || IS_ERR(clk))
  138. return;
  139. mutex_lock(&clocks_mutex);
  140. __clk_disable(clk);
  141. mutex_unlock(&clocks_mutex);
  142. }
  143. EXPORT_SYMBOL(clk_disable);
  144. /* Retrieve the *current* clock rate. If the clock itself
  145. * does not provide a special calculation routine, ask
  146. * its parent and so on, until one is able to return
  147. * a valid clock rate
  148. */
  149. unsigned long clk_get_rate(struct clk *clk)
  150. {
  151. if (clk == NULL || IS_ERR(clk))
  152. return 0UL;
  153. if (clk->get_rate)
  154. return clk->get_rate(clk);
  155. return clk_get_rate(clk->parent);
  156. }
  157. EXPORT_SYMBOL(clk_get_rate);
  158. /* Decrement the clock's module reference count */
  159. void clk_put(struct clk *clk)
  160. {
  161. if (clk && !IS_ERR(clk))
  162. module_put(clk->owner);
  163. }
  164. EXPORT_SYMBOL(clk_put);
  165. /* Round the requested clock rate to the nearest supported
  166. * rate that is less than or equal to the requested rate.
  167. * This is dependent on the clock's current parent.
  168. */
  169. long clk_round_rate(struct clk *clk, unsigned long rate)
  170. {
  171. if (clk == NULL || IS_ERR(clk) || !clk->round_rate)
  172. return 0;
  173. return clk->round_rate(clk, rate);
  174. }
  175. EXPORT_SYMBOL(clk_round_rate);
  176. /* Set the clock to the requested clock rate. The rate must
  177. * match a supported rate exactly based on what clk_round_rate returns
  178. */
  179. int clk_set_rate(struct clk *clk, unsigned long rate)
  180. {
  181. int ret = -EINVAL;
  182. if (clk == NULL || IS_ERR(clk) || clk->set_rate == NULL || rate == 0)
  183. return ret;
  184. mutex_lock(&clocks_mutex);
  185. ret = clk->set_rate(clk, rate);
  186. mutex_unlock(&clocks_mutex);
  187. return ret;
  188. }
  189. EXPORT_SYMBOL(clk_set_rate);
  190. /* Set the clock's parent to another clock source */
  191. int clk_set_parent(struct clk *clk, struct clk *parent)
  192. {
  193. int ret = -EINVAL;
  194. if (clk == NULL || IS_ERR(clk) || parent == NULL ||
  195. IS_ERR(parent) || clk->set_parent == NULL)
  196. return ret;
  197. mutex_lock(&clocks_mutex);
  198. ret = clk->set_parent(clk, parent);
  199. if (ret == 0)
  200. clk->parent = parent;
  201. mutex_unlock(&clocks_mutex);
  202. return ret;
  203. }
  204. EXPORT_SYMBOL(clk_set_parent);
  205. /* Retrieve the clock's parent clock source */
  206. struct clk *clk_get_parent(struct clk *clk)
  207. {
  208. struct clk *ret = NULL;
  209. if (clk == NULL || IS_ERR(clk))
  210. return ret;
  211. return clk->parent;
  212. }
  213. EXPORT_SYMBOL(clk_get_parent);
  214. /*
  215. * Add a new clock to the clock tree.
  216. */
  217. int clk_register(struct clk *clk)
  218. {
  219. if (clk == NULL || IS_ERR(clk))
  220. return -EINVAL;
  221. mutex_lock(&clocks_mutex);
  222. list_add(&clk->node, &clocks);
  223. mutex_unlock(&clocks_mutex);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(clk_register);
  227. /* Remove a clock from the clock tree */
  228. void clk_unregister(struct clk *clk)
  229. {
  230. if (clk == NULL || IS_ERR(clk))
  231. return;
  232. mutex_lock(&clocks_mutex);
  233. list_del(&clk->node);
  234. mutex_unlock(&clocks_mutex);
  235. }
  236. EXPORT_SYMBOL(clk_unregister);
  237. #ifdef CONFIG_PROC_FS
  238. static int mxc_clock_read_proc(char *page, char **start, off_t off,
  239. int count, int *eof, void *data)
  240. {
  241. struct clk *clkp;
  242. char *p = page;
  243. int len;
  244. list_for_each_entry(clkp, &clocks, node) {
  245. p += sprintf(p, "%s-%d:\t\t%lu, %d", clkp->name, clkp->id,
  246. clk_get_rate(clkp), clkp->usecount);
  247. if (clkp->parent)
  248. p += sprintf(p, ", %s-%d\n", clkp->parent->name,
  249. clkp->parent->id);
  250. else
  251. p += sprintf(p, "\n");
  252. }
  253. len = (p - page) - off;
  254. if (len < 0)
  255. len = 0;
  256. *eof = (len <= count) ? 1 : 0;
  257. *start = page + off;
  258. return len;
  259. }
  260. static int __init mxc_setup_proc_entry(void)
  261. {
  262. struct proc_dir_entry *res;
  263. res = create_proc_read_entry("cpu/clocks", 0, NULL,
  264. mxc_clock_read_proc, NULL);
  265. if (!res) {
  266. printk(KERN_ERR "Failed to create proc/cpu/clocks\n");
  267. return -ENOMEM;
  268. }
  269. return 0;
  270. }
  271. late_initcall(mxc_setup_proc_entry);
  272. #endif