tegra_asoc_utils.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * tegra_asoc_utils.c - Harmony machine ASoC driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010 - NVIDIA, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include "tegra_asoc_utils.h"
  28. int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
  29. int mclk)
  30. {
  31. int new_baseclock;
  32. bool clk_change;
  33. int err;
  34. switch (srate) {
  35. case 11025:
  36. case 22050:
  37. case 44100:
  38. case 88200:
  39. new_baseclock = 56448000;
  40. break;
  41. case 8000:
  42. case 16000:
  43. case 32000:
  44. case 48000:
  45. case 64000:
  46. case 96000:
  47. new_baseclock = 73728000;
  48. break;
  49. default:
  50. return -EINVAL;
  51. }
  52. clk_change = ((new_baseclock != data->set_baseclock) ||
  53. (mclk != data->set_mclk));
  54. if (!clk_change)
  55. return 0;
  56. data->set_baseclock = 0;
  57. data->set_mclk = 0;
  58. clk_disable(data->clk_cdev1);
  59. clk_disable(data->clk_pll_a_out0);
  60. clk_disable(data->clk_pll_a);
  61. err = clk_set_rate(data->clk_pll_a, new_baseclock);
  62. if (err) {
  63. dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
  64. return err;
  65. }
  66. err = clk_set_rate(data->clk_pll_a_out0, mclk);
  67. if (err) {
  68. dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
  69. return err;
  70. }
  71. /* Don't set cdev1 rate; its locked to pll_a_out0 */
  72. err = clk_enable(data->clk_pll_a);
  73. if (err) {
  74. dev_err(data->dev, "Can't enable pll_a: %d\n", err);
  75. return err;
  76. }
  77. err = clk_enable(data->clk_pll_a_out0);
  78. if (err) {
  79. dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
  80. return err;
  81. }
  82. err = clk_enable(data->clk_cdev1);
  83. if (err) {
  84. dev_err(data->dev, "Can't enable cdev1: %d\n", err);
  85. return err;
  86. }
  87. data->set_baseclock = new_baseclock;
  88. data->set_mclk = mclk;
  89. return 0;
  90. }
  91. EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
  92. int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
  93. struct device *dev)
  94. {
  95. int ret;
  96. data->dev = dev;
  97. data->clk_pll_a = clk_get_sys(NULL, "pll_a");
  98. if (IS_ERR(data->clk_pll_a)) {
  99. dev_err(data->dev, "Can't retrieve clk pll_a\n");
  100. ret = PTR_ERR(data->clk_pll_a);
  101. goto err;
  102. }
  103. data->clk_pll_a_out0 = clk_get_sys(NULL, "pll_a_out0");
  104. if (IS_ERR(data->clk_pll_a_out0)) {
  105. dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
  106. ret = PTR_ERR(data->clk_pll_a_out0);
  107. goto err_put_pll_a;
  108. }
  109. data->clk_cdev1 = clk_get_sys(NULL, "cdev1");
  110. if (IS_ERR(data->clk_cdev1)) {
  111. dev_err(data->dev, "Can't retrieve clk cdev1\n");
  112. ret = PTR_ERR(data->clk_cdev1);
  113. goto err_put_pll_a_out0;
  114. }
  115. return 0;
  116. err_put_pll_a_out0:
  117. clk_put(data->clk_pll_a_out0);
  118. err_put_pll_a:
  119. clk_put(data->clk_pll_a);
  120. err:
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
  124. void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
  125. {
  126. clk_put(data->clk_cdev1);
  127. clk_put(data->clk_pll_a_out0);
  128. clk_put(data->clk_pll_a);
  129. }
  130. EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
  131. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  132. MODULE_DESCRIPTION("Tegra ASoC utility code");
  133. MODULE_LICENSE("GPL");