clock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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/list.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/slab.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/clkdev.h>
  28. #include "clock.h"
  29. #include "board.h"
  30. #include "fuse.h"
  31. static LIST_HEAD(clocks);
  32. static DEFINE_SPINLOCK(clock_lock);
  33. static DEFINE_MUTEX(dvfs_lock);
  34. static int clk_is_dvfs(struct clk *c)
  35. {
  36. return (c->dvfs != NULL);
  37. };
  38. static int dvfs_set_rate(struct dvfs *d, unsigned long rate)
  39. {
  40. struct dvfs_table *t;
  41. if (d->table == NULL)
  42. return -ENODEV;
  43. for (t = d->table; t->rate != 0; t++) {
  44. if (rate <= t->rate) {
  45. if (!d->reg)
  46. return 0;
  47. return regulator_set_voltage(d->reg,
  48. t->millivolts * 1000,
  49. d->max_millivolts * 1000);
  50. }
  51. }
  52. return -EINVAL;
  53. }
  54. static void dvfs_init(struct clk *c)
  55. {
  56. int process_id;
  57. int i;
  58. struct dvfs_table *table;
  59. process_id = c->dvfs->cpu ? tegra_core_process_id() :
  60. tegra_cpu_process_id();
  61. for (i = 0; i < c->dvfs->process_id_table_length; i++)
  62. if (process_id == c->dvfs->process_id_table[i].process_id)
  63. c->dvfs->table = c->dvfs->process_id_table[i].table;
  64. if (c->dvfs->table == NULL) {
  65. pr_err("Failed to find dvfs table for clock %s process %d\n",
  66. c->name, process_id);
  67. return;
  68. }
  69. c->dvfs->max_millivolts = 0;
  70. for (table = c->dvfs->table; table->rate != 0; table++)
  71. if (c->dvfs->max_millivolts < table->millivolts)
  72. c->dvfs->max_millivolts = table->millivolts;
  73. c->dvfs->reg = regulator_get(NULL, c->dvfs->reg_id);
  74. if (IS_ERR(c->dvfs->reg)) {
  75. pr_err("Failed to get regulator %s for clock %s\n",
  76. c->dvfs->reg_id, c->name);
  77. c->dvfs->reg = NULL;
  78. return;
  79. }
  80. if (c->refcnt > 0)
  81. dvfs_set_rate(c->dvfs, c->rate);
  82. }
  83. struct clk *tegra_get_clock_by_name(const char *name)
  84. {
  85. struct clk *c;
  86. struct clk *ret = NULL;
  87. unsigned long flags;
  88. spin_lock_irqsave(&clock_lock, flags);
  89. list_for_each_entry(c, &clocks, node) {
  90. if (strcmp(c->name, name) == 0) {
  91. ret = c;
  92. break;
  93. }
  94. }
  95. spin_unlock_irqrestore(&clock_lock, flags);
  96. return ret;
  97. }
  98. static void clk_recalculate_rate(struct clk *c)
  99. {
  100. u64 rate;
  101. if (!c->parent)
  102. return;
  103. rate = c->parent->rate;
  104. if (c->mul != 0 && c->div != 0) {
  105. rate = rate * c->mul;
  106. do_div(rate, c->div);
  107. }
  108. if (rate > c->max_rate)
  109. pr_warn("clocks: Set clock %s to rate %llu, max is %lu\n",
  110. c->name, rate, c->max_rate);
  111. c->rate = rate;
  112. }
  113. int clk_reparent(struct clk *c, struct clk *parent)
  114. {
  115. c->parent = parent;
  116. list_del(&c->sibling);
  117. list_add_tail(&c->sibling, &parent->children);
  118. return 0;
  119. }
  120. static void propagate_rate(struct clk *c)
  121. {
  122. struct clk *clkp;
  123. list_for_each_entry(clkp, &c->children, sibling) {
  124. clk_recalculate_rate(clkp);
  125. propagate_rate(clkp);
  126. }
  127. }
  128. void clk_init(struct clk *c)
  129. {
  130. unsigned long flags;
  131. spin_lock_irqsave(&clock_lock, flags);
  132. INIT_LIST_HEAD(&c->children);
  133. INIT_LIST_HEAD(&c->sibling);
  134. if (c->ops && c->ops->init)
  135. c->ops->init(c);
  136. if (!c->ops || !c->ops->enable) {
  137. c->refcnt++;
  138. c->set = 1;
  139. if (c->parent)
  140. c->state = c->parent->state;
  141. else
  142. c->state = ON;
  143. }
  144. clk_recalculate_rate(c);
  145. list_add(&c->node, &clocks);
  146. if (c->parent)
  147. list_add_tail(&c->sibling, &c->parent->children);
  148. spin_unlock_irqrestore(&clock_lock, flags);
  149. }
  150. int clk_enable_locked(struct clk *c)
  151. {
  152. int ret;
  153. if (c->refcnt == 0) {
  154. if (c->parent) {
  155. ret = clk_enable_locked(c->parent);
  156. if (ret)
  157. return ret;
  158. }
  159. if (c->ops && c->ops->enable) {
  160. ret = c->ops->enable(c);
  161. if (ret) {
  162. if (c->parent)
  163. clk_disable_locked(c->parent);
  164. return ret;
  165. }
  166. c->state = ON;
  167. #ifdef CONFIG_DEBUG_FS
  168. c->set = 1;
  169. #endif
  170. }
  171. }
  172. c->refcnt++;
  173. return 0;
  174. }
  175. int clk_enable_cansleep(struct clk *c)
  176. {
  177. int ret;
  178. unsigned long flags;
  179. mutex_lock(&dvfs_lock);
  180. if (clk_is_dvfs(c) && c->refcnt > 0)
  181. dvfs_set_rate(c->dvfs, c->rate);
  182. spin_lock_irqsave(&clock_lock, flags);
  183. ret = clk_enable_locked(c);
  184. spin_unlock_irqrestore(&clock_lock, flags);
  185. mutex_unlock(&dvfs_lock);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(clk_enable_cansleep);
  189. int clk_enable(struct clk *c)
  190. {
  191. int ret;
  192. unsigned long flags;
  193. if (clk_is_dvfs(c))
  194. BUG();
  195. spin_lock_irqsave(&clock_lock, flags);
  196. ret = clk_enable_locked(c);
  197. spin_unlock_irqrestore(&clock_lock, flags);
  198. return ret;
  199. }
  200. EXPORT_SYMBOL(clk_enable);
  201. void clk_disable_locked(struct clk *c)
  202. {
  203. if (c->refcnt == 0) {
  204. WARN(1, "Attempting to disable clock %s with refcnt 0", c->name);
  205. return;
  206. }
  207. if (c->refcnt == 1) {
  208. if (c->ops && c->ops->disable)
  209. c->ops->disable(c);
  210. if (c->parent)
  211. clk_disable_locked(c->parent);
  212. c->state = OFF;
  213. }
  214. c->refcnt--;
  215. }
  216. void clk_disable_cansleep(struct clk *c)
  217. {
  218. unsigned long flags;
  219. mutex_lock(&dvfs_lock);
  220. spin_lock_irqsave(&clock_lock, flags);
  221. clk_disable_locked(c);
  222. spin_unlock_irqrestore(&clock_lock, flags);
  223. if (clk_is_dvfs(c) && c->refcnt == 0)
  224. dvfs_set_rate(c->dvfs, c->rate);
  225. mutex_unlock(&dvfs_lock);
  226. }
  227. EXPORT_SYMBOL(clk_disable_cansleep);
  228. void clk_disable(struct clk *c)
  229. {
  230. unsigned long flags;
  231. if (clk_is_dvfs(c))
  232. BUG();
  233. spin_lock_irqsave(&clock_lock, flags);
  234. clk_disable_locked(c);
  235. spin_unlock_irqrestore(&clock_lock, flags);
  236. }
  237. EXPORT_SYMBOL(clk_disable);
  238. int clk_set_parent_locked(struct clk *c, struct clk *parent)
  239. {
  240. int ret;
  241. if (!c->ops || !c->ops->set_parent)
  242. return -ENOSYS;
  243. ret = c->ops->set_parent(c, parent);
  244. if (ret)
  245. return ret;
  246. clk_recalculate_rate(c);
  247. propagate_rate(c);
  248. return 0;
  249. }
  250. int clk_set_parent(struct clk *c, struct clk *parent)
  251. {
  252. int ret;
  253. unsigned long flags;
  254. spin_lock_irqsave(&clock_lock, flags);
  255. ret = clk_set_parent_locked(c, parent);
  256. spin_unlock_irqrestore(&clock_lock, flags);
  257. return ret;
  258. }
  259. EXPORT_SYMBOL(clk_set_parent);
  260. struct clk *clk_get_parent(struct clk *c)
  261. {
  262. return c->parent;
  263. }
  264. EXPORT_SYMBOL(clk_get_parent);
  265. int clk_set_rate_locked(struct clk *c, unsigned long rate)
  266. {
  267. int ret;
  268. if (rate > c->max_rate)
  269. rate = c->max_rate;
  270. if (!c->ops || !c->ops->set_rate)
  271. return -ENOSYS;
  272. ret = c->ops->set_rate(c, rate);
  273. if (ret)
  274. return ret;
  275. clk_recalculate_rate(c);
  276. propagate_rate(c);
  277. return 0;
  278. }
  279. int clk_set_rate_cansleep(struct clk *c, unsigned long rate)
  280. {
  281. int ret = 0;
  282. unsigned long flags;
  283. mutex_lock(&dvfs_lock);
  284. if (rate > c->rate)
  285. ret = dvfs_set_rate(c->dvfs, rate);
  286. if (ret)
  287. goto out;
  288. spin_lock_irqsave(&clock_lock, flags);
  289. ret = clk_set_rate_locked(c, rate);
  290. spin_unlock_irqrestore(&clock_lock, flags);
  291. if (ret)
  292. goto out;
  293. ret = dvfs_set_rate(c->dvfs, rate);
  294. out:
  295. mutex_unlock(&dvfs_lock);
  296. return ret;
  297. }
  298. EXPORT_SYMBOL(clk_set_rate_cansleep);
  299. int clk_set_rate(struct clk *c, unsigned long rate)
  300. {
  301. int ret = 0;
  302. unsigned long flags;
  303. if (clk_is_dvfs(c))
  304. BUG();
  305. spin_lock_irqsave(&clock_lock, flags);
  306. ret = clk_set_rate_locked(c, rate);
  307. spin_unlock_irqrestore(&clock_lock, flags);
  308. return ret;
  309. }
  310. EXPORT_SYMBOL(clk_set_rate);
  311. unsigned long clk_get_rate(struct clk *c)
  312. {
  313. unsigned long flags;
  314. unsigned long ret;
  315. spin_lock_irqsave(&clock_lock, flags);
  316. ret = c->rate;
  317. spin_unlock_irqrestore(&clock_lock, flags);
  318. return ret;
  319. }
  320. EXPORT_SYMBOL(clk_get_rate);
  321. long clk_round_rate(struct clk *c, unsigned long rate)
  322. {
  323. if (!c->ops || !c->ops->round_rate)
  324. return -ENOSYS;
  325. if (rate > c->max_rate)
  326. rate = c->max_rate;
  327. return c->ops->round_rate(c, rate);
  328. }
  329. EXPORT_SYMBOL(clk_round_rate);
  330. static int tegra_clk_init_one_from_table(struct tegra_clk_init_table *table)
  331. {
  332. struct clk *c;
  333. struct clk *p;
  334. int ret = 0;
  335. c = tegra_get_clock_by_name(table->name);
  336. if (!c) {
  337. pr_warning("Unable to initialize clock %s\n",
  338. table->name);
  339. return -ENODEV;
  340. }
  341. if (table->parent) {
  342. p = tegra_get_clock_by_name(table->parent);
  343. if (!p) {
  344. pr_warning("Unable to find parent %s of clock %s\n",
  345. table->parent, table->name);
  346. return -ENODEV;
  347. }
  348. if (c->parent != p) {
  349. ret = clk_set_parent(c, p);
  350. if (ret) {
  351. pr_warning("Unable to set parent %s of clock %s: %d\n",
  352. table->parent, table->name, ret);
  353. return -EINVAL;
  354. }
  355. }
  356. }
  357. if (table->rate && table->rate != clk_get_rate(c)) {
  358. ret = clk_set_rate(c, table->rate);
  359. if (ret) {
  360. pr_warning("Unable to set clock %s to rate %lu: %d\n",
  361. table->name, table->rate, ret);
  362. return -EINVAL;
  363. }
  364. }
  365. if (table->enabled) {
  366. ret = clk_enable(c);
  367. if (ret) {
  368. pr_warning("Unable to enable clock %s: %d\n",
  369. table->name, ret);
  370. return -EINVAL;
  371. }
  372. }
  373. return 0;
  374. }
  375. void tegra_clk_init_from_table(struct tegra_clk_init_table *table)
  376. {
  377. for (; table->name; table++)
  378. tegra_clk_init_one_from_table(table);
  379. }
  380. EXPORT_SYMBOL(tegra_clk_init_from_table);
  381. void tegra_periph_reset_deassert(struct clk *c)
  382. {
  383. tegra2_periph_reset_deassert(c);
  384. }
  385. EXPORT_SYMBOL(tegra_periph_reset_deassert);
  386. void tegra_periph_reset_assert(struct clk *c)
  387. {
  388. tegra2_periph_reset_assert(c);
  389. }
  390. EXPORT_SYMBOL(tegra_periph_reset_assert);
  391. void __init tegra_init_clock(void)
  392. {
  393. tegra2_init_clocks();
  394. }
  395. int __init tegra_init_dvfs(void)
  396. {
  397. struct clk *c, *safe;
  398. mutex_lock(&dvfs_lock);
  399. list_for_each_entry_safe(c, safe, &clocks, node)
  400. if (c->dvfs)
  401. dvfs_init(c);
  402. mutex_unlock(&dvfs_lock);
  403. return 0;
  404. }
  405. late_initcall(tegra_init_dvfs);
  406. #ifdef CONFIG_DEBUG_FS
  407. static struct dentry *clk_debugfs_root;
  408. static void clock_tree_show_one(struct seq_file *s, struct clk *c, int level)
  409. {
  410. struct clk *child;
  411. struct clk *safe;
  412. const char *state = "uninit";
  413. char div[8] = {0};
  414. if (c->state == ON)
  415. state = "on";
  416. else if (c->state == OFF)
  417. state = "off";
  418. if (c->mul != 0 && c->div != 0) {
  419. if (c->mul > c->div) {
  420. int mul = c->mul / c->div;
  421. int mul2 = (c->mul * 10 / c->div) % 10;
  422. int mul3 = (c->mul * 10) % c->div;
  423. if (mul2 == 0 && mul3 == 0)
  424. snprintf(div, sizeof(div), "x%d", mul);
  425. else if (mul3 == 0)
  426. snprintf(div, sizeof(div), "x%d.%d", mul, mul2);
  427. else
  428. snprintf(div, sizeof(div), "x%d.%d..", mul, mul2);
  429. } else {
  430. snprintf(div, sizeof(div), "%d%s", c->div / c->mul,
  431. (c->div % c->mul) ? ".5" : "");
  432. }
  433. }
  434. seq_printf(s, "%*s%c%c%-*s %-6s %-3d %-8s %-10lu\n",
  435. level * 3 + 1, "",
  436. c->rate > c->max_rate ? '!' : ' ',
  437. !c->set ? '*' : ' ',
  438. 30 - level * 3, c->name,
  439. state, c->refcnt, div, c->rate);
  440. list_for_each_entry_safe(child, safe, &c->children, sibling) {
  441. clock_tree_show_one(s, child, level + 1);
  442. }
  443. }
  444. static int clock_tree_show(struct seq_file *s, void *data)
  445. {
  446. struct clk *c;
  447. unsigned long flags;
  448. seq_printf(s, " clock state ref div rate\n");
  449. seq_printf(s, "--------------------------------------------------------------\n");
  450. spin_lock_irqsave(&clock_lock, flags);
  451. list_for_each_entry(c, &clocks, node)
  452. if (c->parent == NULL)
  453. clock_tree_show_one(s, c, 0);
  454. spin_unlock_irqrestore(&clock_lock, flags);
  455. return 0;
  456. }
  457. static int clock_tree_open(struct inode *inode, struct file *file)
  458. {
  459. return single_open(file, clock_tree_show, inode->i_private);
  460. }
  461. static const struct file_operations clock_tree_fops = {
  462. .open = clock_tree_open,
  463. .read = seq_read,
  464. .llseek = seq_lseek,
  465. .release = single_release,
  466. };
  467. static int possible_parents_show(struct seq_file *s, void *data)
  468. {
  469. struct clk *c = s->private;
  470. int i;
  471. for (i = 0; c->inputs[i].input; i++) {
  472. char *first = (i == 0) ? "" : " ";
  473. seq_printf(s, "%s%s", first, c->inputs[i].input->name);
  474. }
  475. seq_printf(s, "\n");
  476. return 0;
  477. }
  478. static int possible_parents_open(struct inode *inode, struct file *file)
  479. {
  480. return single_open(file, possible_parents_show, inode->i_private);
  481. }
  482. static const struct file_operations possible_parents_fops = {
  483. .open = possible_parents_open,
  484. .read = seq_read,
  485. .llseek = seq_lseek,
  486. .release = single_release,
  487. };
  488. static int clk_debugfs_register_one(struct clk *c)
  489. {
  490. struct dentry *d, *child, *child_tmp;
  491. d = debugfs_create_dir(c->name, clk_debugfs_root);
  492. if (!d)
  493. return -ENOMEM;
  494. c->dent = d;
  495. d = debugfs_create_u8("refcnt", S_IRUGO, c->dent, (u8 *)&c->refcnt);
  496. if (!d)
  497. goto err_out;
  498. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  499. if (!d)
  500. goto err_out;
  501. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  502. if (!d)
  503. goto err_out;
  504. if (c->inputs) {
  505. d = debugfs_create_file("possible_parents", S_IRUGO, c->dent,
  506. c, &possible_parents_fops);
  507. if (!d)
  508. goto err_out;
  509. }
  510. return 0;
  511. err_out:
  512. d = c->dent;
  513. list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
  514. debugfs_remove(child);
  515. debugfs_remove(c->dent);
  516. return -ENOMEM;
  517. }
  518. static int clk_debugfs_register(struct clk *c)
  519. {
  520. int err;
  521. struct clk *pa = c->parent;
  522. if (pa && !pa->dent) {
  523. err = clk_debugfs_register(pa);
  524. if (err)
  525. return err;
  526. }
  527. if (!c->dent) {
  528. err = clk_debugfs_register_one(c);
  529. if (err)
  530. return err;
  531. }
  532. return 0;
  533. }
  534. static int __init clk_debugfs_init(void)
  535. {
  536. struct clk *c;
  537. struct dentry *d;
  538. int err = -ENOMEM;
  539. d = debugfs_create_dir("clock", NULL);
  540. if (!d)
  541. return -ENOMEM;
  542. clk_debugfs_root = d;
  543. d = debugfs_create_file("clock_tree", S_IRUGO, clk_debugfs_root, NULL,
  544. &clock_tree_fops);
  545. if (!d)
  546. goto err_out;
  547. list_for_each_entry(c, &clocks, node) {
  548. err = clk_debugfs_register(c);
  549. if (err)
  550. goto err_out;
  551. }
  552. return 0;
  553. err_out:
  554. debugfs_remove_recursive(clk_debugfs_root);
  555. return err;
  556. }
  557. late_initcall(clk_debugfs_init);
  558. #endif