clock.c 12 KB

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