clock.c 14 KB

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