clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. BUG_ON(!c->ops->reset);
  318. c->ops->reset(c, false);
  319. }
  320. EXPORT_SYMBOL(tegra_periph_reset_deassert);
  321. void tegra_periph_reset_assert(struct clk *c)
  322. {
  323. BUG_ON(!c->ops->reset);
  324. c->ops->reset(c, true);
  325. }
  326. EXPORT_SYMBOL(tegra_periph_reset_assert);
  327. /* Several extended clock configuration bits (e.g., clock routing, clock
  328. * phase control) are included in PLL and peripheral clock source
  329. * registers. */
  330. int tegra_clk_cfg_ex(struct clk *c, enum tegra_clk_ex_param p, u32 setting)
  331. {
  332. int ret = 0;
  333. unsigned long flags;
  334. spin_lock_irqsave(&c->spinlock, flags);
  335. if (!c->ops || !c->ops->clk_cfg_ex) {
  336. ret = -ENOSYS;
  337. goto out;
  338. }
  339. ret = c->ops->clk_cfg_ex(c, p, setting);
  340. out:
  341. spin_unlock_irqrestore(&c->spinlock, flags);
  342. return ret;
  343. }
  344. #ifdef CONFIG_DEBUG_FS
  345. static int __clk_lock_all_spinlocks(void)
  346. {
  347. struct clk *c;
  348. list_for_each_entry(c, &clocks, node)
  349. if (!spin_trylock(&c->spinlock))
  350. goto unlock_spinlocks;
  351. return 0;
  352. unlock_spinlocks:
  353. list_for_each_entry_continue_reverse(c, &clocks, node)
  354. spin_unlock(&c->spinlock);
  355. return -EAGAIN;
  356. }
  357. static void __clk_unlock_all_spinlocks(void)
  358. {
  359. struct clk *c;
  360. list_for_each_entry_reverse(c, &clocks, node)
  361. spin_unlock(&c->spinlock);
  362. }
  363. /*
  364. * This function retries until it can take all locks, and may take
  365. * an arbitrarily long time to complete.
  366. * Must be called with irqs enabled, returns with irqs disabled
  367. * Must be called with clock_list_lock held
  368. */
  369. static void clk_lock_all(void)
  370. {
  371. int ret;
  372. retry:
  373. local_irq_disable();
  374. ret = __clk_lock_all_spinlocks();
  375. if (ret)
  376. goto failed_spinlocks;
  377. /* All locks taken successfully, return */
  378. return;
  379. failed_spinlocks:
  380. local_irq_enable();
  381. yield();
  382. goto retry;
  383. }
  384. /*
  385. * Unlocks all clocks after a clk_lock_all
  386. * Must be called with irqs disabled, returns with irqs enabled
  387. * Must be called with clock_list_lock held
  388. */
  389. static void clk_unlock_all(void)
  390. {
  391. __clk_unlock_all_spinlocks();
  392. local_irq_enable();
  393. }
  394. static struct dentry *clk_debugfs_root;
  395. static void clock_tree_show_one(struct seq_file *s, struct clk *c, int level)
  396. {
  397. struct clk *child;
  398. const char *state = "uninit";
  399. char div[8] = {0};
  400. if (c->state == ON)
  401. state = "on";
  402. else if (c->state == OFF)
  403. state = "off";
  404. if (c->mul != 0 && c->div != 0) {
  405. if (c->mul > c->div) {
  406. int mul = c->mul / c->div;
  407. int mul2 = (c->mul * 10 / c->div) % 10;
  408. int mul3 = (c->mul * 10) % c->div;
  409. if (mul2 == 0 && mul3 == 0)
  410. snprintf(div, sizeof(div), "x%d", mul);
  411. else if (mul3 == 0)
  412. snprintf(div, sizeof(div), "x%d.%d", mul, mul2);
  413. else
  414. snprintf(div, sizeof(div), "x%d.%d..", mul, mul2);
  415. } else {
  416. snprintf(div, sizeof(div), "%d%s", c->div / c->mul,
  417. (c->div % c->mul) ? ".5" : "");
  418. }
  419. }
  420. seq_printf(s, "%*s%c%c%-*s %-6s %-3d %-8s %-10lu\n",
  421. level * 3 + 1, "",
  422. c->rate > c->max_rate ? '!' : ' ',
  423. !c->set ? '*' : ' ',
  424. 30 - level * 3, c->name,
  425. state, c->refcnt, div, clk_get_rate_all_locked(c));
  426. list_for_each_entry(child, &clocks, node) {
  427. if (child->parent != c)
  428. continue;
  429. clock_tree_show_one(s, child, level + 1);
  430. }
  431. }
  432. static int clock_tree_show(struct seq_file *s, void *data)
  433. {
  434. struct clk *c;
  435. seq_printf(s, " clock state ref div rate\n");
  436. seq_printf(s, "--------------------------------------------------------------\n");
  437. mutex_lock(&clock_list_lock);
  438. clk_lock_all();
  439. list_for_each_entry(c, &clocks, node)
  440. if (c->parent == NULL)
  441. clock_tree_show_one(s, c, 0);
  442. clk_unlock_all();
  443. mutex_unlock(&clock_list_lock);
  444. return 0;
  445. }
  446. static int clock_tree_open(struct inode *inode, struct file *file)
  447. {
  448. return single_open(file, clock_tree_show, inode->i_private);
  449. }
  450. static const struct file_operations clock_tree_fops = {
  451. .open = clock_tree_open,
  452. .read = seq_read,
  453. .llseek = seq_lseek,
  454. .release = single_release,
  455. };
  456. static int possible_parents_show(struct seq_file *s, void *data)
  457. {
  458. struct clk *c = s->private;
  459. int i;
  460. for (i = 0; c->inputs[i].input; i++) {
  461. char *first = (i == 0) ? "" : " ";
  462. seq_printf(s, "%s%s", first, c->inputs[i].input->name);
  463. }
  464. seq_printf(s, "\n");
  465. return 0;
  466. }
  467. static int possible_parents_open(struct inode *inode, struct file *file)
  468. {
  469. return single_open(file, possible_parents_show, inode->i_private);
  470. }
  471. static const struct file_operations possible_parents_fops = {
  472. .open = possible_parents_open,
  473. .read = seq_read,
  474. .llseek = seq_lseek,
  475. .release = single_release,
  476. };
  477. static int clk_debugfs_register_one(struct clk *c)
  478. {
  479. struct dentry *d;
  480. d = debugfs_create_dir(c->name, clk_debugfs_root);
  481. if (!d)
  482. return -ENOMEM;
  483. c->dent = d;
  484. d = debugfs_create_u8("refcnt", S_IRUGO, c->dent, (u8 *)&c->refcnt);
  485. if (!d)
  486. goto err_out;
  487. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  488. if (!d)
  489. goto err_out;
  490. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  491. if (!d)
  492. goto err_out;
  493. if (c->inputs) {
  494. d = debugfs_create_file("possible_parents", S_IRUGO, c->dent,
  495. c, &possible_parents_fops);
  496. if (!d)
  497. goto err_out;
  498. }
  499. return 0;
  500. err_out:
  501. debugfs_remove_recursive(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