clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. rate += c->div - 1; /* round up */
  82. do_div(rate, c->div);
  83. }
  84. return rate;
  85. }
  86. /* Must be called with c->spinlock held */
  87. unsigned long clk_get_rate_locked(struct clk *c)
  88. {
  89. unsigned long rate;
  90. if (c->parent)
  91. rate = clk_predict_rate_from_parent(c, c->parent);
  92. else
  93. rate = c->rate;
  94. return rate;
  95. }
  96. unsigned long clk_get_rate(struct clk *c)
  97. {
  98. unsigned long flags;
  99. unsigned long rate;
  100. spin_lock_irqsave(&c->spinlock, flags);
  101. rate = clk_get_rate_locked(c);
  102. spin_unlock_irqrestore(&c->spinlock, flags);
  103. return rate;
  104. }
  105. EXPORT_SYMBOL(clk_get_rate);
  106. int clk_reparent(struct clk *c, struct clk *parent)
  107. {
  108. c->parent = parent;
  109. return 0;
  110. }
  111. void clk_init(struct clk *c)
  112. {
  113. spin_lock_init(&c->spinlock);
  114. if (c->ops && c->ops->init)
  115. c->ops->init(c);
  116. if (!c->ops || !c->ops->enable) {
  117. c->refcnt++;
  118. c->set = true;
  119. if (c->parent)
  120. c->state = c->parent->state;
  121. else
  122. c->state = ON;
  123. }
  124. mutex_lock(&clock_list_lock);
  125. list_add(&c->node, &clocks);
  126. mutex_unlock(&clock_list_lock);
  127. }
  128. int clk_enable(struct clk *c)
  129. {
  130. int ret = 0;
  131. unsigned long flags;
  132. spin_lock_irqsave(&c->spinlock, flags);
  133. if (c->refcnt == 0) {
  134. if (c->parent) {
  135. ret = clk_enable(c->parent);
  136. if (ret)
  137. goto out;
  138. }
  139. if (c->ops && c->ops->enable) {
  140. ret = c->ops->enable(c);
  141. if (ret) {
  142. if (c->parent)
  143. clk_disable(c->parent);
  144. goto out;
  145. }
  146. c->state = ON;
  147. c->set = true;
  148. }
  149. }
  150. c->refcnt++;
  151. out:
  152. spin_unlock_irqrestore(&c->spinlock, flags);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(clk_enable);
  156. void clk_disable(struct clk *c)
  157. {
  158. unsigned long flags;
  159. spin_lock_irqsave(&c->spinlock, flags);
  160. if (c->refcnt == 0) {
  161. WARN(1, "Attempting to disable clock %s with refcnt 0", c->name);
  162. spin_unlock_irqrestore(&c->spinlock, flags);
  163. return;
  164. }
  165. if (c->refcnt == 1) {
  166. if (c->ops && c->ops->disable)
  167. c->ops->disable(c);
  168. if (c->parent)
  169. clk_disable(c->parent);
  170. c->state = OFF;
  171. }
  172. c->refcnt--;
  173. spin_unlock_irqrestore(&c->spinlock, flags);
  174. }
  175. EXPORT_SYMBOL(clk_disable);
  176. int clk_set_parent(struct clk *c, struct clk *parent)
  177. {
  178. int ret;
  179. unsigned long flags;
  180. unsigned long new_rate;
  181. unsigned long old_rate;
  182. spin_lock_irqsave(&c->spinlock, flags);
  183. if (!c->ops || !c->ops->set_parent) {
  184. ret = -ENOSYS;
  185. goto out;
  186. }
  187. new_rate = clk_predict_rate_from_parent(c, parent);
  188. old_rate = clk_get_rate_locked(c);
  189. ret = c->ops->set_parent(c, parent);
  190. if (ret)
  191. goto out;
  192. out:
  193. spin_unlock_irqrestore(&c->spinlock, flags);
  194. return ret;
  195. }
  196. EXPORT_SYMBOL(clk_set_parent);
  197. struct clk *clk_get_parent(struct clk *c)
  198. {
  199. return c->parent;
  200. }
  201. EXPORT_SYMBOL(clk_get_parent);
  202. int clk_set_rate_locked(struct clk *c, unsigned long rate)
  203. {
  204. long new_rate;
  205. if (!c->ops || !c->ops->set_rate)
  206. return -ENOSYS;
  207. if (rate > c->max_rate)
  208. rate = c->max_rate;
  209. if (c->ops && c->ops->round_rate) {
  210. new_rate = c->ops->round_rate(c, rate);
  211. if (new_rate < 0)
  212. return new_rate;
  213. rate = new_rate;
  214. }
  215. return c->ops->set_rate(c, rate);
  216. }
  217. int clk_set_rate(struct clk *c, unsigned long rate)
  218. {
  219. int ret;
  220. unsigned long flags;
  221. spin_lock_irqsave(&c->spinlock, flags);
  222. ret = clk_set_rate_locked(c, rate);
  223. spin_unlock_irqrestore(&c->spinlock, flags);
  224. return ret;
  225. }
  226. EXPORT_SYMBOL(clk_set_rate);
  227. /* Must be called with clocks lock and all indvidual clock locks held */
  228. unsigned long clk_get_rate_all_locked(struct clk *c)
  229. {
  230. u64 rate;
  231. int mul = 1;
  232. int div = 1;
  233. struct clk *p = c;
  234. while (p) {
  235. c = p;
  236. if (c->mul != 0 && c->div != 0) {
  237. mul *= c->mul;
  238. div *= c->div;
  239. }
  240. p = c->parent;
  241. }
  242. rate = c->rate;
  243. rate *= mul;
  244. do_div(rate, div);
  245. return rate;
  246. }
  247. long clk_round_rate(struct clk *c, unsigned long rate)
  248. {
  249. unsigned long flags;
  250. long ret;
  251. spin_lock_irqsave(&c->spinlock, flags);
  252. if (!c->ops || !c->ops->round_rate) {
  253. ret = -ENOSYS;
  254. goto out;
  255. }
  256. if (rate > c->max_rate)
  257. rate = c->max_rate;
  258. ret = c->ops->round_rate(c, rate);
  259. out:
  260. spin_unlock_irqrestore(&c->spinlock, flags);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL(clk_round_rate);
  264. static int tegra_clk_init_one_from_table(struct tegra_clk_init_table *table)
  265. {
  266. struct clk *c;
  267. struct clk *p;
  268. int ret = 0;
  269. c = tegra_get_clock_by_name(table->name);
  270. if (!c) {
  271. pr_warning("Unable to initialize clock %s\n",
  272. table->name);
  273. return -ENODEV;
  274. }
  275. if (table->parent) {
  276. p = tegra_get_clock_by_name(table->parent);
  277. if (!p) {
  278. pr_warning("Unable to find parent %s of clock %s\n",
  279. table->parent, table->name);
  280. return -ENODEV;
  281. }
  282. if (c->parent != p) {
  283. ret = clk_set_parent(c, p);
  284. if (ret) {
  285. pr_warning("Unable to set parent %s of clock %s: %d\n",
  286. table->parent, table->name, ret);
  287. return -EINVAL;
  288. }
  289. }
  290. }
  291. if (table->rate && table->rate != clk_get_rate(c)) {
  292. ret = clk_set_rate(c, table->rate);
  293. if (ret) {
  294. pr_warning("Unable to set clock %s to rate %lu: %d\n",
  295. table->name, table->rate, ret);
  296. return -EINVAL;
  297. }
  298. }
  299. if (table->enabled) {
  300. ret = clk_enable(c);
  301. if (ret) {
  302. pr_warning("Unable to enable clock %s: %d\n",
  303. table->name, ret);
  304. return -EINVAL;
  305. }
  306. }
  307. return 0;
  308. }
  309. void tegra_clk_init_from_table(struct tegra_clk_init_table *table)
  310. {
  311. for (; table->name; table++)
  312. tegra_clk_init_one_from_table(table);
  313. }
  314. EXPORT_SYMBOL(tegra_clk_init_from_table);
  315. void tegra_periph_reset_deassert(struct clk *c)
  316. {
  317. tegra2_periph_reset_deassert(c);
  318. }
  319. EXPORT_SYMBOL(tegra_periph_reset_deassert);
  320. void tegra_periph_reset_assert(struct clk *c)
  321. {
  322. tegra2_periph_reset_assert(c);
  323. }
  324. EXPORT_SYMBOL(tegra_periph_reset_assert);
  325. void __init tegra_init_clock(void)
  326. {
  327. tegra2_init_clocks();
  328. }
  329. /*
  330. * The SDMMC controllers have extra bits in the clock source register that
  331. * adjust the delay between the clock and data to compenstate for delays
  332. * on the PCB.
  333. */
  334. void tegra_sdmmc_tap_delay(struct clk *c, int delay)
  335. {
  336. unsigned long flags;
  337. spin_lock_irqsave(&c->spinlock, flags);
  338. tegra2_sdmmc_tap_delay(c, delay);
  339. spin_unlock_irqrestore(&c->spinlock, flags);
  340. }
  341. #ifdef CONFIG_DEBUG_FS
  342. static int __clk_lock_all_spinlocks(void)
  343. {
  344. struct clk *c;
  345. list_for_each_entry(c, &clocks, node)
  346. if (!spin_trylock(&c->spinlock))
  347. goto unlock_spinlocks;
  348. return 0;
  349. unlock_spinlocks:
  350. list_for_each_entry_continue_reverse(c, &clocks, node)
  351. spin_unlock(&c->spinlock);
  352. return -EAGAIN;
  353. }
  354. static void __clk_unlock_all_spinlocks(void)
  355. {
  356. struct clk *c;
  357. list_for_each_entry_reverse(c, &clocks, node)
  358. spin_unlock(&c->spinlock);
  359. }
  360. /*
  361. * This function retries until it can take all locks, and may take
  362. * an arbitrarily long time to complete.
  363. * Must be called with irqs enabled, returns with irqs disabled
  364. * Must be called with clock_list_lock held
  365. */
  366. static void clk_lock_all(void)
  367. {
  368. int ret;
  369. retry:
  370. local_irq_disable();
  371. ret = __clk_lock_all_spinlocks();
  372. if (ret)
  373. goto failed_spinlocks;
  374. /* All locks taken successfully, return */
  375. return;
  376. failed_spinlocks:
  377. local_irq_enable();
  378. yield();
  379. goto retry;
  380. }
  381. /*
  382. * Unlocks all clocks after a clk_lock_all
  383. * Must be called with irqs disabled, returns with irqs enabled
  384. * Must be called with clock_list_lock held
  385. */
  386. static void clk_unlock_all(void)
  387. {
  388. __clk_unlock_all_spinlocks();
  389. local_irq_enable();
  390. }
  391. static struct dentry *clk_debugfs_root;
  392. static void clock_tree_show_one(struct seq_file *s, struct clk *c, int level)
  393. {
  394. struct clk *child;
  395. const char *state = "uninit";
  396. char div[8] = {0};
  397. if (c->state == ON)
  398. state = "on";
  399. else if (c->state == OFF)
  400. state = "off";
  401. if (c->mul != 0 && c->div != 0) {
  402. if (c->mul > c->div) {
  403. int mul = c->mul / c->div;
  404. int mul2 = (c->mul * 10 / c->div) % 10;
  405. int mul3 = (c->mul * 10) % c->div;
  406. if (mul2 == 0 && mul3 == 0)
  407. snprintf(div, sizeof(div), "x%d", mul);
  408. else if (mul3 == 0)
  409. snprintf(div, sizeof(div), "x%d.%d", mul, mul2);
  410. else
  411. snprintf(div, sizeof(div), "x%d.%d..", mul, mul2);
  412. } else {
  413. snprintf(div, sizeof(div), "%d%s", c->div / c->mul,
  414. (c->div % c->mul) ? ".5" : "");
  415. }
  416. }
  417. seq_printf(s, "%*s%c%c%-*s %-6s %-3d %-8s %-10lu\n",
  418. level * 3 + 1, "",
  419. c->rate > c->max_rate ? '!' : ' ',
  420. !c->set ? '*' : ' ',
  421. 30 - level * 3, c->name,
  422. state, c->refcnt, div, clk_get_rate_all_locked(c));
  423. list_for_each_entry(child, &clocks, node) {
  424. if (child->parent != c)
  425. continue;
  426. clock_tree_show_one(s, child, level + 1);
  427. }
  428. }
  429. static int clock_tree_show(struct seq_file *s, void *data)
  430. {
  431. struct clk *c;
  432. seq_printf(s, " clock state ref div rate\n");
  433. seq_printf(s, "--------------------------------------------------------------\n");
  434. mutex_lock(&clock_list_lock);
  435. clk_lock_all();
  436. list_for_each_entry(c, &clocks, node)
  437. if (c->parent == NULL)
  438. clock_tree_show_one(s, c, 0);
  439. clk_unlock_all();
  440. mutex_unlock(&clock_list_lock);
  441. return 0;
  442. }
  443. static int clock_tree_open(struct inode *inode, struct file *file)
  444. {
  445. return single_open(file, clock_tree_show, inode->i_private);
  446. }
  447. static const struct file_operations clock_tree_fops = {
  448. .open = clock_tree_open,
  449. .read = seq_read,
  450. .llseek = seq_lseek,
  451. .release = single_release,
  452. };
  453. static int possible_parents_show(struct seq_file *s, void *data)
  454. {
  455. struct clk *c = s->private;
  456. int i;
  457. for (i = 0; c->inputs[i].input; i++) {
  458. char *first = (i == 0) ? "" : " ";
  459. seq_printf(s, "%s%s", first, c->inputs[i].input->name);
  460. }
  461. seq_printf(s, "\n");
  462. return 0;
  463. }
  464. static int possible_parents_open(struct inode *inode, struct file *file)
  465. {
  466. return single_open(file, possible_parents_show, inode->i_private);
  467. }
  468. static const struct file_operations possible_parents_fops = {
  469. .open = possible_parents_open,
  470. .read = seq_read,
  471. .llseek = seq_lseek,
  472. .release = single_release,
  473. };
  474. static int clk_debugfs_register_one(struct clk *c)
  475. {
  476. struct dentry *d, *child, *child_tmp;
  477. d = debugfs_create_dir(c->name, clk_debugfs_root);
  478. if (!d)
  479. return -ENOMEM;
  480. c->dent = d;
  481. d = debugfs_create_u8("refcnt", S_IRUGO, c->dent, (u8 *)&c->refcnt);
  482. if (!d)
  483. goto err_out;
  484. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  485. if (!d)
  486. goto err_out;
  487. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  488. if (!d)
  489. goto err_out;
  490. if (c->inputs) {
  491. d = debugfs_create_file("possible_parents", S_IRUGO, c->dent,
  492. c, &possible_parents_fops);
  493. if (!d)
  494. goto err_out;
  495. }
  496. return 0;
  497. err_out:
  498. d = c->dent;
  499. list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
  500. debugfs_remove(child);
  501. debugfs_remove(c->dent);
  502. return -ENOMEM;
  503. }
  504. static int clk_debugfs_register(struct clk *c)
  505. {
  506. int err;
  507. struct clk *pa = c->parent;
  508. if (pa && !pa->dent) {
  509. err = clk_debugfs_register(pa);
  510. if (err)
  511. return err;
  512. }
  513. if (!c->dent) {
  514. err = clk_debugfs_register_one(c);
  515. if (err)
  516. return err;
  517. }
  518. return 0;
  519. }
  520. static int __init clk_debugfs_init(void)
  521. {
  522. struct clk *c;
  523. struct dentry *d;
  524. int err = -ENOMEM;
  525. d = debugfs_create_dir("clock", NULL);
  526. if (!d)
  527. return -ENOMEM;
  528. clk_debugfs_root = d;
  529. d = debugfs_create_file("clock_tree", S_IRUGO, clk_debugfs_root, NULL,
  530. &clock_tree_fops);
  531. if (!d)
  532. goto err_out;
  533. list_for_each_entry(c, &clocks, node) {
  534. err = clk_debugfs_register(c);
  535. if (err)
  536. goto err_out;
  537. }
  538. return 0;
  539. err_out:
  540. debugfs_remove_recursive(clk_debugfs_root);
  541. return err;
  542. }
  543. late_initcall(clk_debugfs_init);
  544. #endif