clock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. *
  3. * Copyright (C) 2010 Google, Inc.
  4. *
  5. * Author:
  6. * Colin Cross <ccross@google.com>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/clk.h>
  20. #include <linux/clkdev.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/sched.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/slab.h>
  29. #include <mach/clk.h>
  30. #include "board.h"
  31. #include "clock.h"
  32. /*
  33. * Locking:
  34. *
  35. * Each struct clk has a spinlock.
  36. *
  37. * To avoid AB-BA locking problems, locks must always be traversed from child
  38. * clock to parent clock. For example, when enabling a clock, the clock's lock
  39. * is taken, and then clk_enable is called on the parent, which take's the
  40. * parent clock's lock. There is one exceptions to this ordering: When dumping
  41. * the clock tree through debugfs. In this case, clk_lock_all is called,
  42. * which attemps to iterate through the entire list of clocks and take every
  43. * clock lock. If any call to spin_trylock fails, all locked clocks are
  44. * unlocked, and the process is retried. When all the locks are held,
  45. * the only clock operation that can be called is clk_get_rate_all_locked.
  46. *
  47. * Within a single clock, no clock operation can call another clock operation
  48. * on itself, except for clk_get_rate_locked and clk_set_rate_locked. Any
  49. * clock operation can call any other clock operation on any of it's possible
  50. * parents.
  51. *
  52. * An additional mutex, clock_list_lock, is used to protect the list of all
  53. * clocks.
  54. *
  55. * The clock operations must lock internally to protect against
  56. * read-modify-write on registers that are shared by multiple clocks
  57. */
  58. static DEFINE_MUTEX(clock_list_lock);
  59. static LIST_HEAD(clocks);
  60. struct clk *tegra_get_clock_by_name(const char *name)
  61. {
  62. struct clk *c;
  63. struct clk *ret = NULL;
  64. mutex_lock(&clock_list_lock);
  65. list_for_each_entry(c, &clocks, node) {
  66. if (strcmp(c->name, name) == 0) {
  67. ret = c;
  68. break;
  69. }
  70. }
  71. mutex_unlock(&clock_list_lock);
  72. return ret;
  73. }
  74. /* Must be called with c->spinlock held */
  75. static unsigned long clk_predict_rate_from_parent(struct clk *c, struct clk *p)
  76. {
  77. u64 rate;
  78. rate = clk_get_rate(p);
  79. if (c->mul != 0 && c->div != 0) {
  80. rate *= c->mul;
  81. do_div(rate, c->div);
  82. }
  83. return rate;
  84. }
  85. /* Must be called with c->spinlock held */
  86. unsigned long clk_get_rate_locked(struct clk *c)
  87. {
  88. unsigned long rate;
  89. if (c->parent)
  90. rate = clk_predict_rate_from_parent(c, c->parent);
  91. else
  92. rate = c->rate;
  93. return rate;
  94. }
  95. unsigned long clk_get_rate(struct clk *c)
  96. {
  97. unsigned long flags;
  98. unsigned long rate;
  99. spin_lock_irqsave(&c->spinlock, flags);
  100. rate = clk_get_rate_locked(c);
  101. spin_unlock_irqrestore(&c->spinlock, flags);
  102. return rate;
  103. }
  104. EXPORT_SYMBOL(clk_get_rate);
  105. int clk_reparent(struct clk *c, struct clk *parent)
  106. {
  107. c->parent = parent;
  108. return 0;
  109. }
  110. void clk_init(struct clk *c)
  111. {
  112. spin_lock_init(&c->spinlock);
  113. if (c->ops && c->ops->init)
  114. c->ops->init(c);
  115. if (!c->ops || !c->ops->enable) {
  116. c->refcnt++;
  117. c->set = true;
  118. if (c->parent)
  119. c->state = c->parent->state;
  120. else
  121. c->state = ON;
  122. }
  123. mutex_lock(&clock_list_lock);
  124. list_add(&c->node, &clocks);
  125. mutex_unlock(&clock_list_lock);
  126. }
  127. int clk_enable(struct clk *c)
  128. {
  129. int ret = 0;
  130. unsigned long flags;
  131. spin_lock_irqsave(&c->spinlock, flags);
  132. if (c->refcnt == 0) {
  133. if (c->parent) {
  134. ret = clk_enable(c->parent);
  135. if (ret)
  136. goto out;
  137. }
  138. if (c->ops && c->ops->enable) {
  139. ret = c->ops->enable(c);
  140. if (ret) {
  141. if (c->parent)
  142. clk_disable(c->parent);
  143. goto out;
  144. }
  145. c->state = ON;
  146. c->set = true;
  147. }
  148. }
  149. c->refcnt++;
  150. out:
  151. spin_unlock_irqrestore(&c->spinlock, flags);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL(clk_enable);
  155. void clk_disable(struct clk *c)
  156. {
  157. unsigned long flags;
  158. spin_lock_irqsave(&c->spinlock, flags);
  159. if (c->refcnt == 0) {
  160. WARN(1, "Attempting to disable clock %s with refcnt 0", c->name);
  161. spin_unlock_irqrestore(&c->spinlock, flags);
  162. return;
  163. }
  164. if (c->refcnt == 1) {
  165. if (c->ops && c->ops->disable)
  166. c->ops->disable(c);
  167. if (c->parent)
  168. clk_disable(c->parent);
  169. c->state = OFF;
  170. }
  171. c->refcnt--;
  172. spin_unlock_irqrestore(&c->spinlock, flags);
  173. }
  174. EXPORT_SYMBOL(clk_disable);
  175. int clk_set_parent(struct clk *c, struct clk *parent)
  176. {
  177. int ret;
  178. unsigned long flags;
  179. unsigned long new_rate;
  180. unsigned long old_rate;
  181. spin_lock_irqsave(&c->spinlock, flags);
  182. if (!c->ops || !c->ops->set_parent) {
  183. ret = -ENOSYS;
  184. goto out;
  185. }
  186. new_rate = clk_predict_rate_from_parent(c, parent);
  187. old_rate = clk_get_rate_locked(c);
  188. ret = c->ops->set_parent(c, parent);
  189. if (ret)
  190. goto out;
  191. out:
  192. spin_unlock_irqrestore(&c->spinlock, flags);
  193. return ret;
  194. }
  195. EXPORT_SYMBOL(clk_set_parent);
  196. struct clk *clk_get_parent(struct clk *c)
  197. {
  198. return c->parent;
  199. }
  200. EXPORT_SYMBOL(clk_get_parent);
  201. int clk_set_rate_locked(struct clk *c, unsigned long rate)
  202. {
  203. if (!c->ops || !c->ops->set_rate)
  204. return -ENOSYS;
  205. if (rate > c->max_rate)
  206. rate = c->max_rate;
  207. return c->ops->set_rate(c, rate);
  208. }
  209. int clk_set_rate(struct clk *c, unsigned long rate)
  210. {
  211. int ret;
  212. unsigned long flags;
  213. spin_lock_irqsave(&c->spinlock, flags);
  214. ret = clk_set_rate_locked(c, rate);
  215. spin_unlock_irqrestore(&c->spinlock, flags);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(clk_set_rate);
  219. /* Must be called with clocks lock and all indvidual clock locks held */
  220. unsigned long clk_get_rate_all_locked(struct clk *c)
  221. {
  222. u64 rate;
  223. int mul = 1;
  224. int div = 1;
  225. struct clk *p = c;
  226. while (p) {
  227. c = p;
  228. if (c->mul != 0 && c->div != 0) {
  229. mul *= c->mul;
  230. div *= c->div;
  231. }
  232. p = c->parent;
  233. }
  234. rate = c->rate;
  235. rate *= mul;
  236. do_div(rate, div);
  237. return rate;
  238. }
  239. long clk_round_rate(struct clk *c, unsigned long rate)
  240. {
  241. unsigned long flags;
  242. long ret;
  243. spin_lock_irqsave(&c->spinlock, flags);
  244. if (!c->ops || !c->ops->round_rate) {
  245. ret = -ENOSYS;
  246. goto out;
  247. }
  248. if (rate > c->max_rate)
  249. rate = c->max_rate;
  250. ret = c->ops->round_rate(c, rate);
  251. out:
  252. spin_unlock_irqrestore(&c->spinlock, flags);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL(clk_round_rate);
  256. static int tegra_clk_init_one_from_table(struct tegra_clk_init_table *table)
  257. {
  258. struct clk *c;
  259. struct clk *p;
  260. int ret = 0;
  261. c = tegra_get_clock_by_name(table->name);
  262. if (!c) {
  263. pr_warning("Unable to initialize clock %s\n",
  264. table->name);
  265. return -ENODEV;
  266. }
  267. if (table->parent) {
  268. p = tegra_get_clock_by_name(table->parent);
  269. if (!p) {
  270. pr_warning("Unable to find parent %s of clock %s\n",
  271. table->parent, table->name);
  272. return -ENODEV;
  273. }
  274. if (c->parent != p) {
  275. ret = clk_set_parent(c, p);
  276. if (ret) {
  277. pr_warning("Unable to set parent %s of clock %s: %d\n",
  278. table->parent, table->name, ret);
  279. return -EINVAL;
  280. }
  281. }
  282. }
  283. if (table->rate && table->rate != clk_get_rate(c)) {
  284. ret = clk_set_rate(c, table->rate);
  285. if (ret) {
  286. pr_warning("Unable to set clock %s to rate %lu: %d\n",
  287. table->name, table->rate, ret);
  288. return -EINVAL;
  289. }
  290. }
  291. if (table->enabled) {
  292. ret = clk_enable(c);
  293. if (ret) {
  294. pr_warning("Unable to enable clock %s: %d\n",
  295. table->name, ret);
  296. return -EINVAL;
  297. }
  298. }
  299. return 0;
  300. }
  301. void tegra_clk_init_from_table(struct tegra_clk_init_table *table)
  302. {
  303. for (; table->name; table++)
  304. tegra_clk_init_one_from_table(table);
  305. }
  306. EXPORT_SYMBOL(tegra_clk_init_from_table);
  307. void tegra_periph_reset_deassert(struct clk *c)
  308. {
  309. tegra2_periph_reset_deassert(c);
  310. }
  311. EXPORT_SYMBOL(tegra_periph_reset_deassert);
  312. void tegra_periph_reset_assert(struct clk *c)
  313. {
  314. tegra2_periph_reset_assert(c);
  315. }
  316. EXPORT_SYMBOL(tegra_periph_reset_assert);
  317. void __init tegra_init_clock(void)
  318. {
  319. tegra2_init_clocks();
  320. }
  321. #ifdef CONFIG_DEBUG_FS
  322. static int __clk_lock_all_spinlocks(void)
  323. {
  324. struct clk *c;
  325. list_for_each_entry(c, &clocks, node)
  326. if (!spin_trylock(&c->spinlock))
  327. goto unlock_spinlocks;
  328. return 0;
  329. unlock_spinlocks:
  330. list_for_each_entry_continue_reverse(c, &clocks, node)
  331. spin_unlock(&c->spinlock);
  332. return -EAGAIN;
  333. }
  334. static void __clk_unlock_all_spinlocks(void)
  335. {
  336. struct clk *c;
  337. list_for_each_entry_reverse(c, &clocks, node)
  338. spin_unlock(&c->spinlock);
  339. }
  340. /*
  341. * This function retries until it can take all locks, and may take
  342. * an arbitrarily long time to complete.
  343. * Must be called with irqs enabled, returns with irqs disabled
  344. * Must be called with clock_list_lock held
  345. */
  346. static void clk_lock_all(void)
  347. {
  348. int ret;
  349. retry:
  350. local_irq_disable();
  351. ret = __clk_lock_all_spinlocks();
  352. if (ret)
  353. goto failed_spinlocks;
  354. /* All locks taken successfully, return */
  355. return;
  356. failed_spinlocks:
  357. local_irq_enable();
  358. yield();
  359. goto retry;
  360. }
  361. /*
  362. * Unlocks all clocks after a clk_lock_all
  363. * Must be called with irqs disabled, returns with irqs enabled
  364. * Must be called with clock_list_lock held
  365. */
  366. static void clk_unlock_all(void)
  367. {
  368. __clk_unlock_all_spinlocks();
  369. local_irq_enable();
  370. }
  371. static struct dentry *clk_debugfs_root;
  372. static void clock_tree_show_one(struct seq_file *s, struct clk *c, int level)
  373. {
  374. struct clk *child;
  375. const char *state = "uninit";
  376. char div[8] = {0};
  377. if (c->state == ON)
  378. state = "on";
  379. else if (c->state == OFF)
  380. state = "off";
  381. if (c->mul != 0 && c->div != 0) {
  382. if (c->mul > c->div) {
  383. int mul = c->mul / c->div;
  384. int mul2 = (c->mul * 10 / c->div) % 10;
  385. int mul3 = (c->mul * 10) % c->div;
  386. if (mul2 == 0 && mul3 == 0)
  387. snprintf(div, sizeof(div), "x%d", mul);
  388. else if (mul3 == 0)
  389. snprintf(div, sizeof(div), "x%d.%d", mul, mul2);
  390. else
  391. snprintf(div, sizeof(div), "x%d.%d..", mul, mul2);
  392. } else {
  393. snprintf(div, sizeof(div), "%d%s", c->div / c->mul,
  394. (c->div % c->mul) ? ".5" : "");
  395. }
  396. }
  397. seq_printf(s, "%*s%c%c%-*s %-6s %-3d %-8s %-10lu\n",
  398. level * 3 + 1, "",
  399. c->rate > c->max_rate ? '!' : ' ',
  400. !c->set ? '*' : ' ',
  401. 30 - level * 3, c->name,
  402. state, c->refcnt, div, clk_get_rate_all_locked(c));
  403. list_for_each_entry(child, &clocks, node) {
  404. if (child->parent != c)
  405. continue;
  406. clock_tree_show_one(s, child, level + 1);
  407. }
  408. }
  409. static int clock_tree_show(struct seq_file *s, void *data)
  410. {
  411. struct clk *c;
  412. seq_printf(s, " clock state ref div rate\n");
  413. seq_printf(s, "--------------------------------------------------------------\n");
  414. mutex_lock(&clock_list_lock);
  415. clk_lock_all();
  416. list_for_each_entry(c, &clocks, node)
  417. if (c->parent == NULL)
  418. clock_tree_show_one(s, c, 0);
  419. clk_unlock_all();
  420. mutex_unlock(&clock_list_lock);
  421. return 0;
  422. }
  423. static int clock_tree_open(struct inode *inode, struct file *file)
  424. {
  425. return single_open(file, clock_tree_show, inode->i_private);
  426. }
  427. static const struct file_operations clock_tree_fops = {
  428. .open = clock_tree_open,
  429. .read = seq_read,
  430. .llseek = seq_lseek,
  431. .release = single_release,
  432. };
  433. static int possible_parents_show(struct seq_file *s, void *data)
  434. {
  435. struct clk *c = s->private;
  436. int i;
  437. for (i = 0; c->inputs[i].input; i++) {
  438. char *first = (i == 0) ? "" : " ";
  439. seq_printf(s, "%s%s", first, c->inputs[i].input->name);
  440. }
  441. seq_printf(s, "\n");
  442. return 0;
  443. }
  444. static int possible_parents_open(struct inode *inode, struct file *file)
  445. {
  446. return single_open(file, possible_parents_show, inode->i_private);
  447. }
  448. static const struct file_operations possible_parents_fops = {
  449. .open = possible_parents_open,
  450. .read = seq_read,
  451. .llseek = seq_lseek,
  452. .release = single_release,
  453. };
  454. static int clk_debugfs_register_one(struct clk *c)
  455. {
  456. struct dentry *d, *child, *child_tmp;
  457. d = debugfs_create_dir(c->name, clk_debugfs_root);
  458. if (!d)
  459. return -ENOMEM;
  460. c->dent = d;
  461. d = debugfs_create_u8("refcnt", S_IRUGO, c->dent, (u8 *)&c->refcnt);
  462. if (!d)
  463. goto err_out;
  464. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  465. if (!d)
  466. goto err_out;
  467. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  468. if (!d)
  469. goto err_out;
  470. if (c->inputs) {
  471. d = debugfs_create_file("possible_parents", S_IRUGO, c->dent,
  472. c, &possible_parents_fops);
  473. if (!d)
  474. goto err_out;
  475. }
  476. return 0;
  477. err_out:
  478. d = c->dent;
  479. list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
  480. debugfs_remove(child);
  481. debugfs_remove(c->dent);
  482. return -ENOMEM;
  483. }
  484. static int clk_debugfs_register(struct clk *c)
  485. {
  486. int err;
  487. struct clk *pa = c->parent;
  488. if (pa && !pa->dent) {
  489. err = clk_debugfs_register(pa);
  490. if (err)
  491. return err;
  492. }
  493. if (!c->dent) {
  494. err = clk_debugfs_register_one(c);
  495. if (err)
  496. return err;
  497. }
  498. return 0;
  499. }
  500. static int __init clk_debugfs_init(void)
  501. {
  502. struct clk *c;
  503. struct dentry *d;
  504. int err = -ENOMEM;
  505. d = debugfs_create_dir("clock", NULL);
  506. if (!d)
  507. return -ENOMEM;
  508. clk_debugfs_root = d;
  509. d = debugfs_create_file("clock_tree", S_IRUGO, clk_debugfs_root, NULL,
  510. &clock_tree_fops);
  511. if (!d)
  512. goto err_out;
  513. list_for_each_entry(c, &clocks, node) {
  514. err = clk_debugfs_register(c);
  515. if (err)
  516. goto err_out;
  517. }
  518. return 0;
  519. err_out:
  520. debugfs_remove_recursive(clk_debugfs_root);
  521. return err;
  522. }
  523. late_initcall(clk_debugfs_init);
  524. #endif