clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * arch/sh/kernel/cpu/clock.c - SuperH clock framework
  3. *
  4. * Copyright (C) 2005 - 2009 Paul Mundt
  5. *
  6. * This clock framework is derived from the OMAP version by:
  7. *
  8. * Copyright (C) 2004 - 2008 Nokia Corporation
  9. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  10. *
  11. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  12. *
  13. * With clkdev bits:
  14. *
  15. * Copyright (C) 2008 Russell King.
  16. *
  17. * This file is subject to the terms and conditions of the GNU General Public
  18. * License. See the file "COPYING" in the main directory of this archive
  19. * for more details.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/list.h>
  26. #include <linux/kobject.h>
  27. #include <linux/sysdev.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/err.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/proc_fs.h>
  32. #include <asm/clock.h>
  33. #include <asm/machvec.h>
  34. static LIST_HEAD(clock_list);
  35. static DEFINE_SPINLOCK(clock_lock);
  36. static DEFINE_MUTEX(clock_list_sem);
  37. /* Used for clocks that always have same value as the parent clock */
  38. unsigned long followparent_recalc(struct clk *clk)
  39. {
  40. return clk->parent->rate;
  41. }
  42. int clk_reparent(struct clk *child, struct clk *parent)
  43. {
  44. list_del_init(&child->sibling);
  45. if (parent)
  46. list_add(&child->sibling, &parent->children);
  47. child->parent = parent;
  48. /* now do the debugfs renaming to reattach the child
  49. to the proper parent */
  50. return 0;
  51. }
  52. /* Propagate rate to children */
  53. void propagate_rate(struct clk *tclk)
  54. {
  55. struct clk *clkp;
  56. list_for_each_entry(clkp, &tclk->children, sibling) {
  57. if (clkp->ops && clkp->ops->recalc)
  58. clkp->rate = clkp->ops->recalc(clkp);
  59. if (clkp->ops && clkp->ops->build_rate_table)
  60. clkp->ops->build_rate_table(clkp);
  61. propagate_rate(clkp);
  62. }
  63. }
  64. static void __clk_disable(struct clk *clk)
  65. {
  66. if (clk->usecount == 0) {
  67. printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
  68. clk->name);
  69. WARN_ON(1);
  70. return;
  71. }
  72. if (!(--clk->usecount)) {
  73. if (likely(clk->ops && clk->ops->disable))
  74. clk->ops->disable(clk);
  75. if (likely(clk->parent))
  76. __clk_disable(clk->parent);
  77. }
  78. }
  79. void clk_disable(struct clk *clk)
  80. {
  81. unsigned long flags;
  82. if (!clk)
  83. return;
  84. spin_lock_irqsave(&clock_lock, flags);
  85. __clk_disable(clk);
  86. spin_unlock_irqrestore(&clock_lock, flags);
  87. }
  88. EXPORT_SYMBOL_GPL(clk_disable);
  89. static int __clk_enable(struct clk *clk)
  90. {
  91. int ret = 0;
  92. if (clk->usecount++ == 0) {
  93. if (clk->parent) {
  94. ret = __clk_enable(clk->parent);
  95. if (unlikely(ret))
  96. goto err;
  97. }
  98. if (clk->ops && clk->ops->enable) {
  99. ret = clk->ops->enable(clk);
  100. if (ret) {
  101. if (clk->parent)
  102. __clk_disable(clk->parent);
  103. goto err;
  104. }
  105. }
  106. }
  107. return ret;
  108. err:
  109. clk->usecount--;
  110. return ret;
  111. }
  112. int clk_enable(struct clk *clk)
  113. {
  114. unsigned long flags;
  115. int ret;
  116. if (!clk)
  117. return -EINVAL;
  118. spin_lock_irqsave(&clock_lock, flags);
  119. ret = __clk_enable(clk);
  120. spin_unlock_irqrestore(&clock_lock, flags);
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(clk_enable);
  124. static LIST_HEAD(root_clks);
  125. /**
  126. * recalculate_root_clocks - recalculate and propagate all root clocks
  127. *
  128. * Recalculates all root clocks (clocks with no parent), which if the
  129. * clock's .recalc is set correctly, should also propagate their rates.
  130. * Called at init.
  131. */
  132. void recalculate_root_clocks(void)
  133. {
  134. struct clk *clkp;
  135. list_for_each_entry(clkp, &root_clks, sibling) {
  136. if (clkp->ops && clkp->ops->recalc)
  137. clkp->rate = clkp->ops->recalc(clkp);
  138. propagate_rate(clkp);
  139. }
  140. }
  141. int clk_register(struct clk *clk)
  142. {
  143. if (clk == NULL || IS_ERR(clk))
  144. return -EINVAL;
  145. /*
  146. * trap out already registered clocks
  147. */
  148. if (clk->node.next || clk->node.prev)
  149. return 0;
  150. mutex_lock(&clock_list_sem);
  151. INIT_LIST_HEAD(&clk->children);
  152. clk->usecount = 0;
  153. if (clk->parent)
  154. list_add(&clk->sibling, &clk->parent->children);
  155. else
  156. list_add(&clk->sibling, &root_clks);
  157. list_add(&clk->node, &clock_list);
  158. if (clk->ops && clk->ops->init)
  159. clk->ops->init(clk);
  160. mutex_unlock(&clock_list_sem);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(clk_register);
  164. void clk_unregister(struct clk *clk)
  165. {
  166. mutex_lock(&clock_list_sem);
  167. list_del(&clk->sibling);
  168. list_del(&clk->node);
  169. mutex_unlock(&clock_list_sem);
  170. }
  171. EXPORT_SYMBOL_GPL(clk_unregister);
  172. static void clk_enable_init_clocks(void)
  173. {
  174. struct clk *clkp;
  175. list_for_each_entry(clkp, &clock_list, node)
  176. if (clkp->flags & CLK_ENABLE_ON_INIT)
  177. clk_enable(clkp);
  178. }
  179. unsigned long clk_get_rate(struct clk *clk)
  180. {
  181. return clk->rate;
  182. }
  183. EXPORT_SYMBOL_GPL(clk_get_rate);
  184. int clk_set_rate(struct clk *clk, unsigned long rate)
  185. {
  186. return clk_set_rate_ex(clk, rate, 0);
  187. }
  188. EXPORT_SYMBOL_GPL(clk_set_rate);
  189. int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
  190. {
  191. int ret = -EOPNOTSUPP;
  192. unsigned long flags;
  193. spin_lock_irqsave(&clock_lock, flags);
  194. if (likely(clk->ops && clk->ops->set_rate)) {
  195. ret = clk->ops->set_rate(clk, rate, algo_id);
  196. if (ret != 0)
  197. goto out_unlock;
  198. } else {
  199. clk->rate = rate;
  200. ret = 0;
  201. }
  202. if (clk->ops && clk->ops->recalc)
  203. clk->rate = clk->ops->recalc(clk);
  204. propagate_rate(clk);
  205. out_unlock:
  206. spin_unlock_irqrestore(&clock_lock, flags);
  207. return ret;
  208. }
  209. EXPORT_SYMBOL_GPL(clk_set_rate_ex);
  210. int clk_set_parent(struct clk *clk, struct clk *parent)
  211. {
  212. unsigned long flags;
  213. int ret = -EINVAL;
  214. if (!parent || !clk)
  215. return ret;
  216. if (clk->parent == parent)
  217. return 0;
  218. spin_lock_irqsave(&clock_lock, flags);
  219. if (clk->usecount == 0) {
  220. if (clk->ops->set_parent)
  221. ret = clk->ops->set_parent(clk, parent);
  222. else
  223. ret = clk_reparent(clk, parent);
  224. if (ret == 0) {
  225. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  226. clk->name, clk->parent->name, clk->rate);
  227. if (clk->ops->recalc)
  228. clk->rate = clk->ops->recalc(clk);
  229. propagate_rate(clk);
  230. }
  231. } else
  232. ret = -EBUSY;
  233. spin_unlock_irqrestore(&clock_lock, flags);
  234. return ret;
  235. }
  236. EXPORT_SYMBOL_GPL(clk_set_parent);
  237. struct clk *clk_get_parent(struct clk *clk)
  238. {
  239. return clk->parent;
  240. }
  241. EXPORT_SYMBOL_GPL(clk_get_parent);
  242. long clk_round_rate(struct clk *clk, unsigned long rate)
  243. {
  244. if (likely(clk->ops && clk->ops->round_rate)) {
  245. unsigned long flags, rounded;
  246. spin_lock_irqsave(&clock_lock, flags);
  247. rounded = clk->ops->round_rate(clk, rate);
  248. spin_unlock_irqrestore(&clock_lock, flags);
  249. return rounded;
  250. }
  251. return clk_get_rate(clk);
  252. }
  253. EXPORT_SYMBOL_GPL(clk_round_rate);
  254. /*
  255. * Find the correct struct clk for the device and connection ID.
  256. * We do slightly fuzzy matching here:
  257. * An entry with a NULL ID is assumed to be a wildcard.
  258. * If an entry has a device ID, it must match
  259. * If an entry has a connection ID, it must match
  260. * Then we take the most specific entry - with the following
  261. * order of precidence: dev+con > dev only > con only.
  262. */
  263. static struct clk *clk_find(const char *dev_id, const char *con_id)
  264. {
  265. struct clk_lookup *p;
  266. struct clk *clk = NULL;
  267. int match, best = 0;
  268. list_for_each_entry(p, &clock_list, node) {
  269. match = 0;
  270. if (p->dev_id) {
  271. if (!dev_id || strcmp(p->dev_id, dev_id))
  272. continue;
  273. match += 2;
  274. }
  275. if (p->con_id) {
  276. if (!con_id || strcmp(p->con_id, con_id))
  277. continue;
  278. match += 1;
  279. }
  280. if (match == 0)
  281. continue;
  282. if (match > best) {
  283. clk = p->clk;
  284. best = match;
  285. }
  286. }
  287. return clk;
  288. }
  289. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  290. {
  291. struct clk *clk;
  292. mutex_lock(&clock_list_sem);
  293. clk = clk_find(dev_id, con_id);
  294. mutex_unlock(&clock_list_sem);
  295. return clk ? clk : ERR_PTR(-ENOENT);
  296. }
  297. EXPORT_SYMBOL_GPL(clk_get_sys);
  298. /*
  299. * Returns a clock. Note that we first try to use device id on the bus
  300. * and clock name. If this fails, we try to use clock name only.
  301. */
  302. struct clk *clk_get(struct device *dev, const char *id)
  303. {
  304. const char *dev_id = dev ? dev_name(dev) : NULL;
  305. struct clk *p, *clk = ERR_PTR(-ENOENT);
  306. int idno;
  307. clk = clk_get_sys(dev_id, id);
  308. if (clk && !IS_ERR(clk))
  309. return clk;
  310. if (dev == NULL || dev->bus != &platform_bus_type)
  311. idno = -1;
  312. else
  313. idno = to_platform_device(dev)->id;
  314. mutex_lock(&clock_list_sem);
  315. list_for_each_entry(p, &clock_list, node) {
  316. if (p->id == idno &&
  317. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  318. clk = p;
  319. goto found;
  320. }
  321. }
  322. list_for_each_entry(p, &clock_list, node) {
  323. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  324. clk = p;
  325. break;
  326. }
  327. }
  328. found:
  329. mutex_unlock(&clock_list_sem);
  330. return clk;
  331. }
  332. EXPORT_SYMBOL_GPL(clk_get);
  333. void clk_put(struct clk *clk)
  334. {
  335. if (clk && !IS_ERR(clk))
  336. module_put(clk->owner);
  337. }
  338. EXPORT_SYMBOL_GPL(clk_put);
  339. static int show_clocks(char *buf, char **start, off_t off,
  340. int len, int *eof, void *data)
  341. {
  342. struct clk *clk;
  343. char *p = buf;
  344. list_for_each_entry_reverse(clk, &clock_list, node) {
  345. unsigned long rate = clk_get_rate(clk);
  346. p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
  347. rate / 1000000, (rate % 1000000) / 10000,
  348. (clk->usecount > 0) ? "enabled" : "disabled");
  349. }
  350. return p - buf;
  351. }
  352. #ifdef CONFIG_PM
  353. static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
  354. {
  355. static pm_message_t prev_state;
  356. struct clk *clkp;
  357. switch (state.event) {
  358. case PM_EVENT_ON:
  359. /* Resumeing from hibernation */
  360. if (prev_state.event != PM_EVENT_FREEZE)
  361. break;
  362. list_for_each_entry(clkp, &clock_list, node) {
  363. if (likely(clkp->ops)) {
  364. unsigned long rate = clkp->rate;
  365. if (likely(clkp->ops->set_parent))
  366. clkp->ops->set_parent(clkp,
  367. clkp->parent);
  368. if (likely(clkp->ops->set_rate))
  369. clkp->ops->set_rate(clkp,
  370. rate, NO_CHANGE);
  371. else if (likely(clkp->ops->recalc))
  372. clkp->rate = clkp->ops->recalc(clkp);
  373. }
  374. }
  375. break;
  376. case PM_EVENT_FREEZE:
  377. break;
  378. case PM_EVENT_SUSPEND:
  379. break;
  380. }
  381. prev_state = state;
  382. return 0;
  383. }
  384. static int clks_sysdev_resume(struct sys_device *dev)
  385. {
  386. return clks_sysdev_suspend(dev, PMSG_ON);
  387. }
  388. static struct sysdev_class clks_sysdev_class = {
  389. .name = "clks",
  390. };
  391. static struct sysdev_driver clks_sysdev_driver = {
  392. .suspend = clks_sysdev_suspend,
  393. .resume = clks_sysdev_resume,
  394. };
  395. static struct sys_device clks_sysdev_dev = {
  396. .cls = &clks_sysdev_class,
  397. };
  398. static int __init clk_sysdev_init(void)
  399. {
  400. sysdev_class_register(&clks_sysdev_class);
  401. sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
  402. sysdev_register(&clks_sysdev_dev);
  403. return 0;
  404. }
  405. subsys_initcall(clk_sysdev_init);
  406. #endif
  407. int __init clk_init(void)
  408. {
  409. int ret;
  410. ret = arch_clk_init();
  411. if (unlikely(ret)) {
  412. pr_err("%s: CPU clock registration failed.\n", __func__);
  413. return ret;
  414. }
  415. if (sh_mv.mv_clk_init) {
  416. ret = sh_mv.mv_clk_init();
  417. if (unlikely(ret)) {
  418. pr_err("%s: machvec clock initialization failed.\n",
  419. __func__);
  420. return ret;
  421. }
  422. }
  423. /* Kick the child clocks.. */
  424. recalculate_root_clocks();
  425. /* Enable the necessary init clocks */
  426. clk_enable_init_clocks();
  427. return ret;
  428. }
  429. static int __init clk_proc_init(void)
  430. {
  431. struct proc_dir_entry *p;
  432. p = create_proc_read_entry("clocks", S_IRUSR, NULL,
  433. show_clocks, NULL);
  434. if (unlikely(!p))
  435. return -EINVAL;
  436. return 0;
  437. }
  438. subsys_initcall(clk_proc_init);