clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/timer.h>
  34. static LIST_HEAD(clock_list);
  35. static DEFINE_SPINLOCK(clock_lock);
  36. static DEFINE_MUTEX(clock_list_sem);
  37. /*
  38. * Each subtype is expected to define the init routines for these clocks,
  39. * as each subtype (or processor family) will have these clocks at the
  40. * very least. These are all provided through the CPG, which even some of
  41. * the more quirky parts (such as ST40, SH4-202, etc.) still have.
  42. *
  43. * The processor-specific code is expected to register any additional
  44. * clock sources that are of interest.
  45. */
  46. static struct clk master_clk = {
  47. .name = "master_clk",
  48. .flags = CLK_ENABLE_ON_INIT,
  49. .rate = CONFIG_SH_PCLK_FREQ,
  50. };
  51. static struct clk module_clk = {
  52. .name = "module_clk",
  53. .parent = &master_clk,
  54. .flags = CLK_ENABLE_ON_INIT,
  55. };
  56. static struct clk bus_clk = {
  57. .name = "bus_clk",
  58. .parent = &master_clk,
  59. .flags = CLK_ENABLE_ON_INIT,
  60. };
  61. static struct clk cpu_clk = {
  62. .name = "cpu_clk",
  63. .parent = &master_clk,
  64. .flags = CLK_ENABLE_ON_INIT,
  65. };
  66. /*
  67. * The ordering of these clocks matters, do not change it.
  68. */
  69. static struct clk *onchip_clocks[] = {
  70. &master_clk,
  71. &module_clk,
  72. &bus_clk,
  73. &cpu_clk,
  74. };
  75. /* Used for clocks that always have same value as the parent clock */
  76. unsigned long followparent_recalc(struct clk *clk)
  77. {
  78. return clk->parent->rate;
  79. }
  80. int clk_reparent(struct clk *child, struct clk *parent)
  81. {
  82. list_del_init(&child->sibling);
  83. if (parent)
  84. list_add(&child->sibling, &parent->children);
  85. child->parent = parent;
  86. /* now do the debugfs renaming to reattach the child
  87. to the proper parent */
  88. return 0;
  89. }
  90. /* Propagate rate to children */
  91. void propagate_rate(struct clk *tclk)
  92. {
  93. struct clk *clkp;
  94. list_for_each_entry(clkp, &tclk->children, sibling) {
  95. if (clkp->ops->recalc)
  96. clkp->rate = clkp->ops->recalc(clkp);
  97. propagate_rate(clkp);
  98. }
  99. }
  100. static void __clk_disable(struct clk *clk)
  101. {
  102. if (clk->usecount == 0) {
  103. printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
  104. clk->name);
  105. WARN_ON(1);
  106. return;
  107. }
  108. if (!(--clk->usecount)) {
  109. if (likely(clk->ops && clk->ops->disable))
  110. clk->ops->disable(clk);
  111. if (likely(clk->parent))
  112. __clk_disable(clk->parent);
  113. }
  114. }
  115. void clk_disable(struct clk *clk)
  116. {
  117. unsigned long flags;
  118. if (!clk)
  119. return;
  120. spin_lock_irqsave(&clock_lock, flags);
  121. __clk_disable(clk);
  122. spin_unlock_irqrestore(&clock_lock, flags);
  123. }
  124. EXPORT_SYMBOL_GPL(clk_disable);
  125. static int __clk_enable(struct clk *clk)
  126. {
  127. int ret = 0;
  128. if (clk->usecount++ == 0) {
  129. if (clk->parent) {
  130. ret = __clk_enable(clk->parent);
  131. if (unlikely(ret))
  132. goto err;
  133. }
  134. if (clk->ops && clk->ops->enable) {
  135. ret = clk->ops->enable(clk);
  136. if (ret) {
  137. if (clk->parent)
  138. __clk_disable(clk->parent);
  139. goto err;
  140. }
  141. }
  142. }
  143. return ret;
  144. err:
  145. clk->usecount--;
  146. return ret;
  147. }
  148. int clk_enable(struct clk *clk)
  149. {
  150. unsigned long flags;
  151. int ret;
  152. if (!clk)
  153. return -EINVAL;
  154. spin_lock_irqsave(&clock_lock, flags);
  155. ret = __clk_enable(clk);
  156. spin_unlock_irqrestore(&clock_lock, flags);
  157. return ret;
  158. }
  159. EXPORT_SYMBOL_GPL(clk_enable);
  160. static LIST_HEAD(root_clks);
  161. /**
  162. * recalculate_root_clocks - recalculate and propagate all root clocks
  163. *
  164. * Recalculates all root clocks (clocks with no parent), which if the
  165. * clock's .recalc is set correctly, should also propagate their rates.
  166. * Called at init.
  167. */
  168. void recalculate_root_clocks(void)
  169. {
  170. struct clk *clkp;
  171. list_for_each_entry(clkp, &root_clks, sibling) {
  172. if (clkp->ops->recalc)
  173. clkp->rate = clkp->ops->recalc(clkp);
  174. propagate_rate(clkp);
  175. }
  176. }
  177. int clk_register(struct clk *clk)
  178. {
  179. if (clk == NULL || IS_ERR(clk))
  180. return -EINVAL;
  181. /*
  182. * trap out already registered clocks
  183. */
  184. if (clk->node.next || clk->node.prev)
  185. return 0;
  186. mutex_lock(&clock_list_sem);
  187. INIT_LIST_HEAD(&clk->children);
  188. clk->usecount = 0;
  189. if (clk->parent)
  190. list_add(&clk->sibling, &clk->parent->children);
  191. else
  192. list_add(&clk->sibling, &root_clks);
  193. list_add(&clk->node, &clock_list);
  194. if (clk->ops->init)
  195. clk->ops->init(clk);
  196. mutex_unlock(&clock_list_sem);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(clk_register);
  200. void clk_unregister(struct clk *clk)
  201. {
  202. mutex_lock(&clock_list_sem);
  203. list_del(&clk->sibling);
  204. list_del(&clk->node);
  205. mutex_unlock(&clock_list_sem);
  206. }
  207. EXPORT_SYMBOL_GPL(clk_unregister);
  208. static void clk_enable_init_clocks(void)
  209. {
  210. struct clk *clkp;
  211. list_for_each_entry(clkp, &clock_list, node)
  212. if (clkp->flags & CLK_ENABLE_ON_INIT)
  213. clk_enable(clkp);
  214. }
  215. unsigned long clk_get_rate(struct clk *clk)
  216. {
  217. return clk->rate;
  218. }
  219. EXPORT_SYMBOL_GPL(clk_get_rate);
  220. int clk_set_rate(struct clk *clk, unsigned long rate)
  221. {
  222. return clk_set_rate_ex(clk, rate, 0);
  223. }
  224. EXPORT_SYMBOL_GPL(clk_set_rate);
  225. int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
  226. {
  227. int ret = -EOPNOTSUPP;
  228. if (likely(clk->ops && clk->ops->set_rate)) {
  229. unsigned long flags;
  230. spin_lock_irqsave(&clock_lock, flags);
  231. ret = clk->ops->set_rate(clk, rate, algo_id);
  232. if (ret == 0) {
  233. if (clk->ops->recalc)
  234. clk->rate = clk->ops->recalc(clk);
  235. propagate_rate(clk);
  236. }
  237. spin_unlock_irqrestore(&clock_lock, flags);
  238. }
  239. return ret;
  240. }
  241. EXPORT_SYMBOL_GPL(clk_set_rate_ex);
  242. int clk_set_parent(struct clk *clk, struct clk *parent)
  243. {
  244. unsigned long flags;
  245. int ret = -EINVAL;
  246. if (!parent || !clk)
  247. return ret;
  248. if (clk->parent == parent)
  249. return 0;
  250. spin_lock_irqsave(&clock_lock, flags);
  251. if (clk->usecount == 0) {
  252. if (clk->ops->set_parent)
  253. ret = clk->ops->set_parent(clk, parent);
  254. else
  255. ret = clk_reparent(clk, parent);
  256. if (ret == 0) {
  257. pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
  258. clk->name, clk->parent->name, clk->rate);
  259. if (clk->ops->recalc)
  260. clk->rate = clk->ops->recalc(clk);
  261. propagate_rate(clk);
  262. }
  263. } else
  264. ret = -EBUSY;
  265. spin_unlock_irqrestore(&clock_lock, flags);
  266. return ret;
  267. }
  268. EXPORT_SYMBOL_GPL(clk_set_parent);
  269. struct clk *clk_get_parent(struct clk *clk)
  270. {
  271. return clk->parent;
  272. }
  273. EXPORT_SYMBOL_GPL(clk_get_parent);
  274. long clk_round_rate(struct clk *clk, unsigned long rate)
  275. {
  276. if (likely(clk->ops && clk->ops->round_rate)) {
  277. unsigned long flags, rounded;
  278. spin_lock_irqsave(&clock_lock, flags);
  279. rounded = clk->ops->round_rate(clk, rate);
  280. spin_unlock_irqrestore(&clock_lock, flags);
  281. return rounded;
  282. }
  283. return clk_get_rate(clk);
  284. }
  285. EXPORT_SYMBOL_GPL(clk_round_rate);
  286. /*
  287. * Find the correct struct clk for the device and connection ID.
  288. * We do slightly fuzzy matching here:
  289. * An entry with a NULL ID is assumed to be a wildcard.
  290. * If an entry has a device ID, it must match
  291. * If an entry has a connection ID, it must match
  292. * Then we take the most specific entry - with the following
  293. * order of precidence: dev+con > dev only > con only.
  294. */
  295. static struct clk *clk_find(const char *dev_id, const char *con_id)
  296. {
  297. struct clk_lookup *p;
  298. struct clk *clk = NULL;
  299. int match, best = 0;
  300. list_for_each_entry(p, &clock_list, node) {
  301. match = 0;
  302. if (p->dev_id) {
  303. if (!dev_id || strcmp(p->dev_id, dev_id))
  304. continue;
  305. match += 2;
  306. }
  307. if (p->con_id) {
  308. if (!con_id || strcmp(p->con_id, con_id))
  309. continue;
  310. match += 1;
  311. }
  312. if (match == 0)
  313. continue;
  314. if (match > best) {
  315. clk = p->clk;
  316. best = match;
  317. }
  318. }
  319. return clk;
  320. }
  321. struct clk *clk_get_sys(const char *dev_id, const char *con_id)
  322. {
  323. struct clk *clk;
  324. mutex_lock(&clock_list_sem);
  325. clk = clk_find(dev_id, con_id);
  326. mutex_unlock(&clock_list_sem);
  327. return clk ? clk : ERR_PTR(-ENOENT);
  328. }
  329. EXPORT_SYMBOL_GPL(clk_get_sys);
  330. /*
  331. * Returns a clock. Note that we first try to use device id on the bus
  332. * and clock name. If this fails, we try to use clock name only.
  333. */
  334. struct clk *clk_get(struct device *dev, const char *id)
  335. {
  336. const char *dev_id = dev ? dev_name(dev) : NULL;
  337. struct clk *p, *clk = ERR_PTR(-ENOENT);
  338. int idno;
  339. clk = clk_get_sys(dev_id, id);
  340. if (clk)
  341. return clk;
  342. if (dev == NULL || dev->bus != &platform_bus_type)
  343. idno = -1;
  344. else
  345. idno = to_platform_device(dev)->id;
  346. mutex_lock(&clock_list_sem);
  347. list_for_each_entry(p, &clock_list, node) {
  348. if (p->id == idno &&
  349. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  350. clk = p;
  351. goto found;
  352. }
  353. }
  354. list_for_each_entry(p, &clock_list, node) {
  355. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  356. clk = p;
  357. break;
  358. }
  359. }
  360. found:
  361. mutex_unlock(&clock_list_sem);
  362. return clk;
  363. }
  364. EXPORT_SYMBOL_GPL(clk_get);
  365. void clk_put(struct clk *clk)
  366. {
  367. if (clk && !IS_ERR(clk))
  368. module_put(clk->owner);
  369. }
  370. EXPORT_SYMBOL_GPL(clk_put);
  371. void __init __attribute__ ((weak))
  372. arch_init_clk_ops(struct clk_ops **ops, int type)
  373. {
  374. }
  375. int __init __attribute__ ((weak))
  376. arch_clk_init(void)
  377. {
  378. return 0;
  379. }
  380. static int show_clocks(char *buf, char **start, off_t off,
  381. int len, int *eof, void *data)
  382. {
  383. struct clk *clk;
  384. char *p = buf;
  385. list_for_each_entry_reverse(clk, &clock_list, node) {
  386. unsigned long rate = clk_get_rate(clk);
  387. p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
  388. rate / 1000000, (rate % 1000000) / 10000,
  389. (clk->usecount > 0) ? "enabled" : "disabled");
  390. }
  391. return p - buf;
  392. }
  393. #ifdef CONFIG_PM
  394. static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
  395. {
  396. static pm_message_t prev_state;
  397. struct clk *clkp;
  398. switch (state.event) {
  399. case PM_EVENT_ON:
  400. /* Resumeing from hibernation */
  401. if (prev_state.event != PM_EVENT_FREEZE)
  402. break;
  403. list_for_each_entry(clkp, &clock_list, node) {
  404. if (likely(clkp->ops)) {
  405. unsigned long rate = clkp->rate;
  406. if (likely(clkp->ops->set_parent))
  407. clkp->ops->set_parent(clkp,
  408. clkp->parent);
  409. if (likely(clkp->ops->set_rate))
  410. clkp->ops->set_rate(clkp,
  411. rate, NO_CHANGE);
  412. else if (likely(clkp->ops->recalc))
  413. clkp->rate = clkp->ops->recalc(clkp);
  414. }
  415. }
  416. break;
  417. case PM_EVENT_FREEZE:
  418. break;
  419. case PM_EVENT_SUSPEND:
  420. break;
  421. }
  422. prev_state = state;
  423. return 0;
  424. }
  425. static int clks_sysdev_resume(struct sys_device *dev)
  426. {
  427. return clks_sysdev_suspend(dev, PMSG_ON);
  428. }
  429. static struct sysdev_class clks_sysdev_class = {
  430. .name = "clks",
  431. };
  432. static struct sysdev_driver clks_sysdev_driver = {
  433. .suspend = clks_sysdev_suspend,
  434. .resume = clks_sysdev_resume,
  435. };
  436. static struct sys_device clks_sysdev_dev = {
  437. .cls = &clks_sysdev_class,
  438. };
  439. static int __init clk_sysdev_init(void)
  440. {
  441. sysdev_class_register(&clks_sysdev_class);
  442. sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
  443. sysdev_register(&clks_sysdev_dev);
  444. return 0;
  445. }
  446. subsys_initcall(clk_sysdev_init);
  447. #endif
  448. int __init clk_init(void)
  449. {
  450. int i, ret = 0;
  451. BUG_ON(!master_clk.rate);
  452. for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
  453. struct clk *clk = onchip_clocks[i];
  454. arch_init_clk_ops(&clk->ops, i);
  455. ret |= clk_register(clk);
  456. }
  457. ret |= arch_clk_init();
  458. /* Kick the child clocks.. */
  459. recalculate_root_clocks();
  460. /* Enable the necessary init clocks */
  461. clk_enable_init_clocks();
  462. return ret;
  463. }
  464. static int __init clk_proc_init(void)
  465. {
  466. struct proc_dir_entry *p;
  467. p = create_proc_read_entry("clocks", S_IRUSR, NULL,
  468. show_clocks, NULL);
  469. if (unlikely(!p))
  470. return -EINVAL;
  471. return 0;
  472. }
  473. subsys_initcall(clk_proc_init);