clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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/debugfs.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 ? clk->parent->rate : 0;
  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. #ifdef CONFIG_PM
  340. static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
  341. {
  342. static pm_message_t prev_state;
  343. struct clk *clkp;
  344. switch (state.event) {
  345. case PM_EVENT_ON:
  346. /* Resumeing from hibernation */
  347. if (prev_state.event != PM_EVENT_FREEZE)
  348. break;
  349. list_for_each_entry(clkp, &clock_list, node) {
  350. if (likely(clkp->ops)) {
  351. unsigned long rate = clkp->rate;
  352. if (likely(clkp->ops->set_parent))
  353. clkp->ops->set_parent(clkp,
  354. clkp->parent);
  355. if (likely(clkp->ops->set_rate))
  356. clkp->ops->set_rate(clkp,
  357. rate, NO_CHANGE);
  358. else if (likely(clkp->ops->recalc))
  359. clkp->rate = clkp->ops->recalc(clkp);
  360. }
  361. }
  362. break;
  363. case PM_EVENT_FREEZE:
  364. break;
  365. case PM_EVENT_SUSPEND:
  366. break;
  367. }
  368. prev_state = state;
  369. return 0;
  370. }
  371. static int clks_sysdev_resume(struct sys_device *dev)
  372. {
  373. return clks_sysdev_suspend(dev, PMSG_ON);
  374. }
  375. static struct sysdev_class clks_sysdev_class = {
  376. .name = "clks",
  377. };
  378. static struct sysdev_driver clks_sysdev_driver = {
  379. .suspend = clks_sysdev_suspend,
  380. .resume = clks_sysdev_resume,
  381. };
  382. static struct sys_device clks_sysdev_dev = {
  383. .cls = &clks_sysdev_class,
  384. };
  385. static int __init clk_sysdev_init(void)
  386. {
  387. sysdev_class_register(&clks_sysdev_class);
  388. sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
  389. sysdev_register(&clks_sysdev_dev);
  390. return 0;
  391. }
  392. subsys_initcall(clk_sysdev_init);
  393. #endif
  394. int __init clk_init(void)
  395. {
  396. int ret;
  397. ret = arch_clk_init();
  398. if (unlikely(ret)) {
  399. pr_err("%s: CPU clock registration failed.\n", __func__);
  400. return ret;
  401. }
  402. if (sh_mv.mv_clk_init) {
  403. ret = sh_mv.mv_clk_init();
  404. if (unlikely(ret)) {
  405. pr_err("%s: machvec clock initialization failed.\n",
  406. __func__);
  407. return ret;
  408. }
  409. }
  410. /* Kick the child clocks.. */
  411. recalculate_root_clocks();
  412. /* Enable the necessary init clocks */
  413. clk_enable_init_clocks();
  414. return ret;
  415. }
  416. /*
  417. * debugfs support to trace clock tree hierarchy and attributes
  418. */
  419. static struct dentry *clk_debugfs_root;
  420. static int clk_debugfs_register_one(struct clk *c)
  421. {
  422. int err;
  423. struct dentry *d, *child;
  424. struct clk *pa = c->parent;
  425. char s[255];
  426. char *p = s;
  427. p += sprintf(p, "%s", c->name);
  428. if (c->id >= 0)
  429. sprintf(p, ":%d", c->id);
  430. d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
  431. if (!d)
  432. return -ENOMEM;
  433. c->dentry = d;
  434. d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
  435. if (!d) {
  436. err = -ENOMEM;
  437. goto err_out;
  438. }
  439. d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
  440. if (!d) {
  441. err = -ENOMEM;
  442. goto err_out;
  443. }
  444. d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
  445. if (!d) {
  446. err = -ENOMEM;
  447. goto err_out;
  448. }
  449. return 0;
  450. err_out:
  451. d = c->dentry;
  452. list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
  453. debugfs_remove(child);
  454. debugfs_remove(c->dentry);
  455. return err;
  456. }
  457. static int clk_debugfs_register(struct clk *c)
  458. {
  459. int err;
  460. struct clk *pa = c->parent;
  461. if (pa && !pa->dentry) {
  462. err = clk_debugfs_register(pa);
  463. if (err)
  464. return err;
  465. }
  466. if (!c->dentry) {
  467. err = clk_debugfs_register_one(c);
  468. if (err)
  469. return err;
  470. }
  471. return 0;
  472. }
  473. static int __init clk_debugfs_init(void)
  474. {
  475. struct clk *c;
  476. struct dentry *d;
  477. int err;
  478. d = debugfs_create_dir("clock", NULL);
  479. if (!d)
  480. return -ENOMEM;
  481. clk_debugfs_root = d;
  482. list_for_each_entry(c, &clock_list, node) {
  483. err = clk_debugfs_register(c);
  484. if (err)
  485. goto err_out;
  486. }
  487. return 0;
  488. err_out:
  489. debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
  490. return err;
  491. }
  492. late_initcall(clk_debugfs_init);