clock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 <linux/cpufreq.h>
  33. #include <asm/clock.h>
  34. #include <asm/machvec.h>
  35. static LIST_HEAD(clock_list);
  36. static DEFINE_SPINLOCK(clock_lock);
  37. static DEFINE_MUTEX(clock_list_sem);
  38. void clk_rate_table_build(struct clk *clk,
  39. struct cpufreq_frequency_table *freq_table,
  40. int nr_freqs,
  41. struct clk_div_mult_table *src_table,
  42. unsigned long *bitmap)
  43. {
  44. unsigned long mult, div;
  45. unsigned long freq;
  46. int i;
  47. for (i = 0; i < nr_freqs; i++) {
  48. div = 1;
  49. mult = 1;
  50. if (src_table->divisors && i < src_table->nr_divisors)
  51. div = src_table->divisors[i];
  52. if (src_table->multipliers && i < src_table->nr_multipliers)
  53. mult = src_table->multipliers[i];
  54. if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
  55. freq = CPUFREQ_ENTRY_INVALID;
  56. else
  57. freq = clk->parent->rate * mult / div;
  58. freq_table[i].index = i;
  59. freq_table[i].frequency = freq;
  60. }
  61. /* Termination entry */
  62. freq_table[i].index = i;
  63. freq_table[i].frequency = CPUFREQ_TABLE_END;
  64. }
  65. long clk_rate_table_round(struct clk *clk,
  66. struct cpufreq_frequency_table *freq_table,
  67. unsigned long rate)
  68. {
  69. unsigned long rate_error, rate_error_prev = ~0UL;
  70. unsigned long rate_best_fit = rate;
  71. unsigned long highest, lowest;
  72. int i;
  73. highest = lowest = 0;
  74. for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
  75. unsigned long freq = freq_table[i].frequency;
  76. if (freq == CPUFREQ_ENTRY_INVALID)
  77. continue;
  78. if (freq > highest)
  79. highest = freq;
  80. if (freq < lowest)
  81. lowest = freq;
  82. rate_error = abs(freq - rate);
  83. if (rate_error < rate_error_prev) {
  84. rate_best_fit = freq;
  85. rate_error_prev = rate_error;
  86. }
  87. if (rate_error == 0)
  88. break;
  89. }
  90. if (rate >= highest)
  91. rate_best_fit = highest;
  92. if (rate <= lowest)
  93. rate_best_fit = lowest;
  94. return rate_best_fit;
  95. }
  96. /* Used for clocks that always have same value as the parent clock */
  97. unsigned long followparent_recalc(struct clk *clk)
  98. {
  99. return clk->parent ? clk->parent->rate : 0;
  100. }
  101. int clk_reparent(struct clk *child, struct clk *parent)
  102. {
  103. list_del_init(&child->sibling);
  104. if (parent)
  105. list_add(&child->sibling, &parent->children);
  106. child->parent = parent;
  107. /* now do the debugfs renaming to reattach the child
  108. to the proper parent */
  109. return 0;
  110. }
  111. /* Propagate rate to children */
  112. void propagate_rate(struct clk *tclk)
  113. {
  114. struct clk *clkp;
  115. list_for_each_entry(clkp, &tclk->children, sibling) {
  116. if (clkp->ops && clkp->ops->recalc)
  117. clkp->rate = clkp->ops->recalc(clkp);
  118. propagate_rate(clkp);
  119. }
  120. }
  121. static void __clk_disable(struct clk *clk)
  122. {
  123. if (clk->usecount == 0) {
  124. printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
  125. clk->name);
  126. WARN_ON(1);
  127. return;
  128. }
  129. if (!(--clk->usecount)) {
  130. if (likely(clk->ops && clk->ops->disable))
  131. clk->ops->disable(clk);
  132. if (likely(clk->parent))
  133. __clk_disable(clk->parent);
  134. }
  135. }
  136. void clk_disable(struct clk *clk)
  137. {
  138. unsigned long flags;
  139. if (!clk)
  140. return;
  141. spin_lock_irqsave(&clock_lock, flags);
  142. __clk_disable(clk);
  143. spin_unlock_irqrestore(&clock_lock, flags);
  144. }
  145. EXPORT_SYMBOL_GPL(clk_disable);
  146. static int __clk_enable(struct clk *clk)
  147. {
  148. int ret = 0;
  149. if (clk->usecount++ == 0) {
  150. if (clk->parent) {
  151. ret = __clk_enable(clk->parent);
  152. if (unlikely(ret))
  153. goto err;
  154. }
  155. if (clk->ops && clk->ops->enable) {
  156. ret = clk->ops->enable(clk);
  157. if (ret) {
  158. if (clk->parent)
  159. __clk_disable(clk->parent);
  160. goto err;
  161. }
  162. }
  163. }
  164. return ret;
  165. err:
  166. clk->usecount--;
  167. return ret;
  168. }
  169. int clk_enable(struct clk *clk)
  170. {
  171. unsigned long flags;
  172. int ret;
  173. if (!clk)
  174. return -EINVAL;
  175. spin_lock_irqsave(&clock_lock, flags);
  176. ret = __clk_enable(clk);
  177. spin_unlock_irqrestore(&clock_lock, flags);
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(clk_enable);
  181. static LIST_HEAD(root_clks);
  182. /**
  183. * recalculate_root_clocks - recalculate and propagate all root clocks
  184. *
  185. * Recalculates all root clocks (clocks with no parent), which if the
  186. * clock's .recalc is set correctly, should also propagate their rates.
  187. * Called at init.
  188. */
  189. void recalculate_root_clocks(void)
  190. {
  191. struct clk *clkp;
  192. list_for_each_entry(clkp, &root_clks, sibling) {
  193. if (clkp->ops && clkp->ops->recalc)
  194. clkp->rate = clkp->ops->recalc(clkp);
  195. propagate_rate(clkp);
  196. }
  197. }
  198. int clk_register(struct clk *clk)
  199. {
  200. if (clk == NULL || IS_ERR(clk))
  201. return -EINVAL;
  202. /*
  203. * trap out already registered clocks
  204. */
  205. if (clk->node.next || clk->node.prev)
  206. return 0;
  207. mutex_lock(&clock_list_sem);
  208. INIT_LIST_HEAD(&clk->children);
  209. clk->usecount = 0;
  210. if (clk->parent)
  211. list_add(&clk->sibling, &clk->parent->children);
  212. else
  213. list_add(&clk->sibling, &root_clks);
  214. list_add(&clk->node, &clock_list);
  215. if (clk->ops && clk->ops->init)
  216. clk->ops->init(clk);
  217. mutex_unlock(&clock_list_sem);
  218. return 0;
  219. }
  220. EXPORT_SYMBOL_GPL(clk_register);
  221. void clk_unregister(struct clk *clk)
  222. {
  223. mutex_lock(&clock_list_sem);
  224. list_del(&clk->sibling);
  225. list_del(&clk->node);
  226. mutex_unlock(&clock_list_sem);
  227. }
  228. EXPORT_SYMBOL_GPL(clk_unregister);
  229. static void clk_enable_init_clocks(void)
  230. {
  231. struct clk *clkp;
  232. list_for_each_entry(clkp, &clock_list, node)
  233. if (clkp->flags & CLK_ENABLE_ON_INIT)
  234. clk_enable(clkp);
  235. }
  236. unsigned long clk_get_rate(struct clk *clk)
  237. {
  238. return clk->rate;
  239. }
  240. EXPORT_SYMBOL_GPL(clk_get_rate);
  241. int clk_set_rate(struct clk *clk, unsigned long rate)
  242. {
  243. return clk_set_rate_ex(clk, rate, 0);
  244. }
  245. EXPORT_SYMBOL_GPL(clk_set_rate);
  246. int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
  247. {
  248. int ret = -EOPNOTSUPP;
  249. unsigned long flags;
  250. spin_lock_irqsave(&clock_lock, flags);
  251. if (likely(clk->ops && clk->ops->set_rate)) {
  252. ret = clk->ops->set_rate(clk, rate, algo_id);
  253. if (ret != 0)
  254. goto out_unlock;
  255. } else {
  256. clk->rate = rate;
  257. ret = 0;
  258. }
  259. if (clk->ops && clk->ops->recalc)
  260. clk->rate = clk->ops->recalc(clk);
  261. propagate_rate(clk);
  262. out_unlock:
  263. spin_unlock_irqrestore(&clock_lock, flags);
  264. return ret;
  265. }
  266. EXPORT_SYMBOL_GPL(clk_set_rate_ex);
  267. int clk_set_parent(struct clk *clk, struct clk *parent)
  268. {
  269. unsigned long flags;
  270. int ret = -EINVAL;
  271. if (!parent || !clk)
  272. return ret;
  273. if (clk->parent == parent)
  274. return 0;
  275. spin_lock_irqsave(&clock_lock, flags);
  276. if (clk->usecount == 0) {
  277. if (clk->ops->set_parent)
  278. ret = clk->ops->set_parent(clk, parent);
  279. else
  280. ret = clk_reparent(clk, parent);
  281. if (ret == 0) {
  282. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  283. clk->name, clk->parent->name, clk->rate);
  284. if (clk->ops->recalc)
  285. clk->rate = clk->ops->recalc(clk);
  286. propagate_rate(clk);
  287. }
  288. } else
  289. ret = -EBUSY;
  290. spin_unlock_irqrestore(&clock_lock, flags);
  291. return ret;
  292. }
  293. EXPORT_SYMBOL_GPL(clk_set_parent);
  294. struct clk *clk_get_parent(struct clk *clk)
  295. {
  296. return clk->parent;
  297. }
  298. EXPORT_SYMBOL_GPL(clk_get_parent);
  299. long clk_round_rate(struct clk *clk, unsigned long rate)
  300. {
  301. if (likely(clk->ops && clk->ops->round_rate)) {
  302. unsigned long flags, rounded;
  303. spin_lock_irqsave(&clock_lock, flags);
  304. rounded = clk->ops->round_rate(clk, rate);
  305. spin_unlock_irqrestore(&clock_lock, flags);
  306. return rounded;
  307. }
  308. return clk_get_rate(clk);
  309. }
  310. EXPORT_SYMBOL_GPL(clk_round_rate);
  311. /*
  312. * Find the correct struct clk for the device and connection ID.
  313. * We do slightly fuzzy matching here:
  314. * An entry with a NULL ID is assumed to be a wildcard.
  315. * If an entry has a device ID, it must match
  316. * If an entry has a connection ID, it must match
  317. * Then we take the most specific entry - with the following
  318. * order of precidence: dev+con > dev only > con only.
  319. */
  320. static struct clk *clk_find(const char *dev_id, const char *con_id)
  321. {
  322. struct clk_lookup *p;
  323. struct clk *clk = NULL;
  324. int match, best = 0;
  325. list_for_each_entry(p, &clock_list, node) {
  326. match = 0;
  327. if (p->dev_id) {
  328. if (!dev_id || strcmp(p->dev_id, dev_id))
  329. continue;
  330. match += 2;
  331. }
  332. if (p->con_id) {
  333. if (!con_id || strcmp(p->con_id, con_id))
  334. continue;
  335. match += 1;
  336. }
  337. if (match == 0)
  338. continue;
  339. if (match > best) {
  340. clk = p->clk;
  341. best = match;
  342. }
  343. }
  344. return clk;
  345. }
  346. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  347. {
  348. struct clk *clk;
  349. mutex_lock(&clock_list_sem);
  350. clk = clk_find(dev_id, con_id);
  351. mutex_unlock(&clock_list_sem);
  352. return clk ? clk : ERR_PTR(-ENOENT);
  353. }
  354. EXPORT_SYMBOL_GPL(clk_get_sys);
  355. /*
  356. * Returns a clock. Note that we first try to use device id on the bus
  357. * and clock name. If this fails, we try to use clock name only.
  358. */
  359. struct clk *clk_get(struct device *dev, const char *id)
  360. {
  361. const char *dev_id = dev ? dev_name(dev) : NULL;
  362. struct clk *p, *clk = ERR_PTR(-ENOENT);
  363. int idno;
  364. clk = clk_get_sys(dev_id, id);
  365. if (clk && !IS_ERR(clk))
  366. return clk;
  367. if (dev == NULL || dev->bus != &platform_bus_type)
  368. idno = -1;
  369. else
  370. idno = to_platform_device(dev)->id;
  371. mutex_lock(&clock_list_sem);
  372. list_for_each_entry(p, &clock_list, node) {
  373. if (p->id == idno &&
  374. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  375. clk = p;
  376. goto found;
  377. }
  378. }
  379. list_for_each_entry(p, &clock_list, node) {
  380. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  381. clk = p;
  382. break;
  383. }
  384. }
  385. found:
  386. mutex_unlock(&clock_list_sem);
  387. return clk;
  388. }
  389. EXPORT_SYMBOL_GPL(clk_get);
  390. void clk_put(struct clk *clk)
  391. {
  392. if (clk && !IS_ERR(clk))
  393. module_put(clk->owner);
  394. }
  395. EXPORT_SYMBOL_GPL(clk_put);
  396. #ifdef CONFIG_PM
  397. static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
  398. {
  399. static pm_message_t prev_state;
  400. struct clk *clkp;
  401. switch (state.event) {
  402. case PM_EVENT_ON:
  403. /* Resumeing from hibernation */
  404. if (prev_state.event != PM_EVENT_FREEZE)
  405. break;
  406. list_for_each_entry(clkp, &clock_list, node) {
  407. if (likely(clkp->ops)) {
  408. unsigned long rate = clkp->rate;
  409. if (likely(clkp->ops->set_parent))
  410. clkp->ops->set_parent(clkp,
  411. clkp->parent);
  412. if (likely(clkp->ops->set_rate))
  413. clkp->ops->set_rate(clkp,
  414. rate, NO_CHANGE);
  415. else if (likely(clkp->ops->recalc))
  416. clkp->rate = clkp->ops->recalc(clkp);
  417. }
  418. }
  419. break;
  420. case PM_EVENT_FREEZE:
  421. break;
  422. case PM_EVENT_SUSPEND:
  423. break;
  424. }
  425. prev_state = state;
  426. return 0;
  427. }
  428. static int clks_sysdev_resume(struct sys_device *dev)
  429. {
  430. return clks_sysdev_suspend(dev, PMSG_ON);
  431. }
  432. static struct sysdev_class clks_sysdev_class = {
  433. .name = "clks",
  434. };
  435. static struct sysdev_driver clks_sysdev_driver = {
  436. .suspend = clks_sysdev_suspend,
  437. .resume = clks_sysdev_resume,
  438. };
  439. static struct sys_device clks_sysdev_dev = {
  440. .cls = &clks_sysdev_class,
  441. };
  442. static int __init clk_sysdev_init(void)
  443. {
  444. sysdev_class_register(&clks_sysdev_class);
  445. sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
  446. sysdev_register(&clks_sysdev_dev);
  447. return 0;
  448. }
  449. subsys_initcall(clk_sysdev_init);
  450. #endif
  451. int __init clk_init(void)
  452. {
  453. int ret;
  454. ret = arch_clk_init();
  455. if (unlikely(ret)) {
  456. pr_err("%s: CPU clock registration failed.\n", __func__);
  457. return ret;
  458. }
  459. if (sh_mv.mv_clk_init) {
  460. ret = sh_mv.mv_clk_init();
  461. if (unlikely(ret)) {
  462. pr_err("%s: machvec clock initialization failed.\n",
  463. __func__);
  464. return ret;
  465. }
  466. }
  467. /* Kick the child clocks.. */
  468. recalculate_root_clocks();
  469. /* Enable the necessary init clocks */
  470. clk_enable_init_clocks();
  471. return ret;
  472. }
  473. /*
  474. * debugfs support to trace clock tree hierarchy and attributes
  475. */
  476. static struct dentry *clk_debugfs_root;
  477. static int clk_debugfs_register_one(struct clk *c)
  478. {
  479. int err;
  480. struct dentry *d, *child;
  481. struct clk *pa = c->parent;
  482. char s[255];
  483. char *p = s;
  484. p += sprintf(p, "%s", c->name);
  485. if (c->id >= 0)
  486. sprintf(p, ":%d", c->id);
  487. d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
  488. if (!d)
  489. return -ENOMEM;
  490. c->dentry = d;
  491. d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
  492. if (!d) {
  493. err = -ENOMEM;
  494. goto err_out;
  495. }
  496. d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
  497. if (!d) {
  498. err = -ENOMEM;
  499. goto err_out;
  500. }
  501. d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
  502. if (!d) {
  503. err = -ENOMEM;
  504. goto err_out;
  505. }
  506. return 0;
  507. err_out:
  508. d = c->dentry;
  509. list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
  510. debugfs_remove(child);
  511. debugfs_remove(c->dentry);
  512. return err;
  513. }
  514. static int clk_debugfs_register(struct clk *c)
  515. {
  516. int err;
  517. struct clk *pa = c->parent;
  518. if (pa && !pa->dentry) {
  519. err = clk_debugfs_register(pa);
  520. if (err)
  521. return err;
  522. }
  523. if (!c->dentry) {
  524. err = clk_debugfs_register_one(c);
  525. if (err)
  526. return err;
  527. }
  528. return 0;
  529. }
  530. static int __init clk_debugfs_init(void)
  531. {
  532. struct clk *c;
  533. struct dentry *d;
  534. int err;
  535. d = debugfs_create_dir("clock", NULL);
  536. if (!d)
  537. return -ENOMEM;
  538. clk_debugfs_root = d;
  539. list_for_each_entry(c, &clock_list, node) {
  540. err = clk_debugfs_register(c);
  541. if (err)
  542. goto err_out;
  543. }
  544. return 0;
  545. err_out:
  546. debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
  547. return err;
  548. }
  549. late_initcall(clk_debugfs_init);