tegra_asoc_utils.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * tegra_asoc_utils.c - Harmony machine ASoC driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010,2012 - 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 <linux/of.h>
  28. #include "tegra_asoc_utils.h"
  29. int tegra_asoc_utils_set_rate(struct tegra_asoc_utils_data *data, int srate,
  30. int mclk)
  31. {
  32. int new_baseclock;
  33. bool clk_change;
  34. int err;
  35. switch (srate) {
  36. case 11025:
  37. case 22050:
  38. case 44100:
  39. case 88200:
  40. if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
  41. new_baseclock = 56448000;
  42. else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
  43. new_baseclock = 564480000;
  44. else
  45. new_baseclock = 282240000;
  46. break;
  47. case 8000:
  48. case 16000:
  49. case 32000:
  50. case 48000:
  51. case 64000:
  52. case 96000:
  53. if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA20)
  54. new_baseclock = 73728000;
  55. else if (data->soc == TEGRA_ASOC_UTILS_SOC_TEGRA30)
  56. new_baseclock = 552960000;
  57. else
  58. new_baseclock = 368640000;
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. clk_change = ((new_baseclock != data->set_baseclock) ||
  64. (mclk != data->set_mclk));
  65. if (!clk_change)
  66. return 0;
  67. data->set_baseclock = 0;
  68. data->set_mclk = 0;
  69. clk_disable_unprepare(data->clk_cdev1);
  70. clk_disable_unprepare(data->clk_pll_a_out0);
  71. clk_disable_unprepare(data->clk_pll_a);
  72. err = clk_set_rate(data->clk_pll_a, new_baseclock);
  73. if (err) {
  74. dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
  75. return err;
  76. }
  77. err = clk_set_rate(data->clk_pll_a_out0, mclk);
  78. if (err) {
  79. dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
  80. return err;
  81. }
  82. /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
  83. err = clk_prepare_enable(data->clk_pll_a);
  84. if (err) {
  85. dev_err(data->dev, "Can't enable pll_a: %d\n", err);
  86. return err;
  87. }
  88. err = clk_prepare_enable(data->clk_pll_a_out0);
  89. if (err) {
  90. dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
  91. return err;
  92. }
  93. err = clk_prepare_enable(data->clk_cdev1);
  94. if (err) {
  95. dev_err(data->dev, "Can't enable cdev1: %d\n", err);
  96. return err;
  97. }
  98. data->set_baseclock = new_baseclock;
  99. data->set_mclk = mclk;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_rate);
  103. int tegra_asoc_utils_set_ac97_rate(struct tegra_asoc_utils_data *data)
  104. {
  105. const int pll_rate = 73728000;
  106. const int ac97_rate = 24576000;
  107. int err;
  108. clk_disable_unprepare(data->clk_cdev1);
  109. clk_disable_unprepare(data->clk_pll_a_out0);
  110. clk_disable_unprepare(data->clk_pll_a);
  111. /*
  112. * AC97 rate is fixed at 24.576MHz and is used for both the host
  113. * controller and the external codec
  114. */
  115. err = clk_set_rate(data->clk_pll_a, pll_rate);
  116. if (err) {
  117. dev_err(data->dev, "Can't set pll_a rate: %d\n", err);
  118. return err;
  119. }
  120. err = clk_set_rate(data->clk_pll_a_out0, ac97_rate);
  121. if (err) {
  122. dev_err(data->dev, "Can't set pll_a_out0 rate: %d\n", err);
  123. return err;
  124. }
  125. /* Don't set cdev1/extern1 rate; it's locked to pll_a_out0 */
  126. err = clk_prepare_enable(data->clk_pll_a);
  127. if (err) {
  128. dev_err(data->dev, "Can't enable pll_a: %d\n", err);
  129. return err;
  130. }
  131. err = clk_prepare_enable(data->clk_pll_a_out0);
  132. if (err) {
  133. dev_err(data->dev, "Can't enable pll_a_out0: %d\n", err);
  134. return err;
  135. }
  136. err = clk_prepare_enable(data->clk_cdev1);
  137. if (err) {
  138. dev_err(data->dev, "Can't enable cdev1: %d\n", err);
  139. return err;
  140. }
  141. data->set_baseclock = pll_rate;
  142. data->set_mclk = ac97_rate;
  143. return 0;
  144. }
  145. EXPORT_SYMBOL_GPL(tegra_asoc_utils_set_ac97_rate);
  146. int tegra_asoc_utils_init(struct tegra_asoc_utils_data *data,
  147. struct device *dev)
  148. {
  149. int ret;
  150. data->dev = dev;
  151. if (of_machine_is_compatible("nvidia,tegra20"))
  152. data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA20;
  153. else if (of_machine_is_compatible("nvidia,tegra30"))
  154. data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA30;
  155. else if (of_machine_is_compatible("nvidia,tegra114"))
  156. data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA114;
  157. else if (of_machine_is_compatible("nvidia,tegra124"))
  158. data->soc = TEGRA_ASOC_UTILS_SOC_TEGRA124;
  159. else {
  160. dev_err(data->dev, "SoC unknown to Tegra ASoC utils\n");
  161. return -EINVAL;
  162. }
  163. data->clk_pll_a = clk_get(dev, "pll_a");
  164. if (IS_ERR(data->clk_pll_a)) {
  165. dev_err(data->dev, "Can't retrieve clk pll_a\n");
  166. ret = PTR_ERR(data->clk_pll_a);
  167. goto err;
  168. }
  169. data->clk_pll_a_out0 = clk_get(dev, "pll_a_out0");
  170. if (IS_ERR(data->clk_pll_a_out0)) {
  171. dev_err(data->dev, "Can't retrieve clk pll_a_out0\n");
  172. ret = PTR_ERR(data->clk_pll_a_out0);
  173. goto err_put_pll_a;
  174. }
  175. data->clk_cdev1 = clk_get(dev, "mclk");
  176. if (IS_ERR(data->clk_cdev1)) {
  177. dev_err(data->dev, "Can't retrieve clk cdev1\n");
  178. ret = PTR_ERR(data->clk_cdev1);
  179. goto err_put_pll_a_out0;
  180. }
  181. ret = tegra_asoc_utils_set_rate(data, 44100, 256 * 44100);
  182. if (ret)
  183. goto err_put_cdev1;
  184. return 0;
  185. err_put_cdev1:
  186. clk_put(data->clk_cdev1);
  187. err_put_pll_a_out0:
  188. clk_put(data->clk_pll_a_out0);
  189. err_put_pll_a:
  190. clk_put(data->clk_pll_a);
  191. err:
  192. return ret;
  193. }
  194. EXPORT_SYMBOL_GPL(tegra_asoc_utils_init);
  195. void tegra_asoc_utils_fini(struct tegra_asoc_utils_data *data)
  196. {
  197. clk_put(data->clk_cdev1);
  198. clk_put(data->clk_pll_a_out0);
  199. clk_put(data->clk_pll_a);
  200. }
  201. EXPORT_SYMBOL_GPL(tegra_asoc_utils_fini);
  202. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  203. MODULE_DESCRIPTION("Tegra ASoC utility code");
  204. MODULE_LICENSE("GPL");