tegra_asoc_utils.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "tegra_asoc_utils.h"
  27. int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
  28. int mclk)
  29. {
  30. int new_baseclock;
  31. bool clk_change;
  32. int err;
  33. switch (srate) {
  34. case 11025:
  35. case 22050:
  36. case 44100:
  37. case 88200:
  38. new_baseclock = 56448000;
  39. break;
  40. case 8000:
  41. case 16000:
  42. case 32000:
  43. case 48000:
  44. case 64000:
  45. case 96000:
  46. new_baseclock = 73728000;
  47. break;
  48. default:
  49. return -EINVAL;
  50. }
  51. clk_change = ((new_baseclock != data->set_baseclock) ||
  52. (mclk != data->set_mclk));
  53. if (!clk_change)
  54. return 0;
  55. data->set_baseclock = 0;
  56. data->set_mclk = 0;
  57. clk_disable(data->clk_cdev1);
  58. clk_disable(data->clk_pll_a_out0);
  59. clk_disable(data->clk_pll_a);
  60. err = clk_set_rate(data->clk_pll_a, new_baseclock);
  61. if (err) {
  62. dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
  63. return err;
  64. }
  65. err = clk_set_rate(data->clk_pll_a_out0, mclk);
  66. if (err) {
  67. dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
  68. return err;
  69. }
  70. /* Don't set cdev1 rate; its locked to pll_a_out0 */
  71. err = clk_enable(data->clk_pll_a);
  72. if (err) {
  73. dev_err(data->dev, "Can't enable pll_a: %d\n", err);
  74. return err;
  75. }
  76. err = clk_enable(data->clk_pll_a_out0);
  77. if (err) {
  78. dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
  79. return err;
  80. }
  81. err = clk_enable(data->clk_cdev1);
  82. if (err) {
  83. dev_err(data->dev, "Can't enable cdev1: %d\n", err);
  84. return err;
  85. }
  86. data->set_baseclock = new_baseclock;
  87. data->set_mclk = mclk;
  88. return 0;
  89. }
  90. EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
  91. int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
  92. struct device *dev)
  93. {
  94. int ret;
  95. data->dev = dev;
  96. data->clk_pll_a = clk_get_sys(NULL, "pll_a");
  97. if (IS_ERR(data->clk_pll_a)) {
  98. dev_err(data->dev, "Can't retrieve clk pll_a\n");
  99. ret = PTR_ERR(data->clk_pll_a);
  100. goto err;
  101. }
  102. data->clk_pll_a_out0 = clk_get_sys(NULL, "pll_a_out0");
  103. if (IS_ERR(data->clk_pll_a_out0)) {
  104. dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
  105. ret = PTR_ERR(data->clk_pll_a_out0);
  106. goto err_put_pll_a;
  107. }
  108. data->clk_cdev1 = clk_get_sys(NULL, "cdev1");
  109. if (IS_ERR(data->clk_cdev1)) {
  110. dev_err(data->dev, "Can't retrieve clk cdev1\n");
  111. ret = PTR_ERR(data->clk_cdev1);
  112. goto err_put_pll_a_out0;
  113. }
  114. return 0;
  115. err_put_pll_a_out0:
  116. clk_put(data->clk_pll_a_out0);
  117. err_put_pll_a:
  118. clk_put(data->clk_pll_a);
  119. err:
  120. return ret;
  121. }
  122. EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
  123. void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
  124. {
  125. clk_put(data->clk_cdev1);
  126. clk_put(data->clk_pll_a_out0);
  127. clk_put(data->clk_pll_a);
  128. }
  129. EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
  130. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  131. MODULE_DESCRIPTION("Tegra ASoC utility code");
  132. MODULE_LICENSE("GPL");