tegra_audio_graph_card.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // SPDX-FileCopyrightText: Copyright (c) 2020-2025 NVIDIA CORPORATION. All rights reserved.
  3. //
  4. // tegra_audio_graph_card.c - Audio Graph based Tegra Machine Driver
  5. #include <linux/math64.h>
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/platform_device.h>
  9. #include <sound/graph_card.h>
  10. #include <sound/pcm_params.h>
  11. #include <sound/soc-dai.h>
  12. #define MAX_PLLA_OUT0_DIV 128
  13. #define simple_to_tegra_priv(simple) \
  14. container_of(simple, struct tegra_audio_priv, simple)
  15. enum srate_type {
  16. /*
  17. * Sample rates multiple of 8000 Hz and below are supported:
  18. * ( 8000, 16000, 32000, 48000, 96000, 192000 Hz )
  19. */
  20. x8_RATE,
  21. /*
  22. * Sample rates multiple of 11025 Hz and below are supported:
  23. * ( 11025, 22050, 44100, 88200, 176400 Hz )
  24. */
  25. x11_RATE,
  26. NUM_RATE_TYPE,
  27. };
  28. struct tegra_audio_priv {
  29. struct simple_util_priv simple;
  30. struct clk *clk_plla_out0;
  31. struct clk *clk_plla;
  32. };
  33. /* Tegra audio chip data */
  34. struct tegra_audio_cdata {
  35. unsigned int plla_rates[NUM_RATE_TYPE];
  36. unsigned int plla_out0_rates[NUM_RATE_TYPE];
  37. };
  38. static bool need_clk_update(struct snd_soc_dai *dai)
  39. {
  40. if (snd_soc_dai_is_dummy(dai) ||
  41. !dai->driver->ops ||
  42. !dai->driver->name)
  43. return false;
  44. if (strstr(dai->driver->name, "I2S") ||
  45. strstr(dai->driver->name, "DMIC") ||
  46. strstr(dai->driver->name, "DSPK"))
  47. return true;
  48. return false;
  49. }
  50. /* Setup PLL clock as per the given sample rate */
  51. static int tegra_audio_graph_update_pll(struct snd_pcm_substream *substream,
  52. struct snd_pcm_hw_params *params)
  53. {
  54. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  55. struct simple_util_priv *simple = snd_soc_card_get_drvdata(rtd->card);
  56. struct tegra_audio_priv *priv = simple_to_tegra_priv(simple);
  57. struct device *dev = rtd->card->dev;
  58. const struct tegra_audio_cdata *data = of_device_get_match_data(dev);
  59. unsigned int plla_rate, plla_out0_rate, bclk;
  60. unsigned int srate = params_rate(params);
  61. int err;
  62. switch (srate) {
  63. case 11025:
  64. case 22050:
  65. case 44100:
  66. case 88200:
  67. case 176400:
  68. plla_out0_rate = data->plla_out0_rates[x11_RATE];
  69. plla_rate = data->plla_rates[x11_RATE];
  70. break;
  71. case 8000:
  72. case 16000:
  73. case 32000:
  74. case 48000:
  75. case 96000:
  76. case 192000:
  77. plla_out0_rate = data->plla_out0_rates[x8_RATE];
  78. plla_rate = data->plla_rates[x8_RATE];
  79. break;
  80. default:
  81. dev_err(rtd->card->dev, "Unsupported sample rate %u\n",
  82. srate);
  83. return -EINVAL;
  84. }
  85. /*
  86. * Below is the clock relation:
  87. *
  88. * PLLA
  89. * |
  90. * |--> PLLA_OUT0
  91. * |
  92. * |---> I2S modules
  93. * |
  94. * |---> DMIC modules
  95. * |
  96. * |---> DSPK modules
  97. *
  98. *
  99. * Default PLLA_OUT0 rate might be too high when I/O is running
  100. * at minimum PCM configurations. This may result in incorrect
  101. * clock rates and glitchy audio. The maximum divider is 128
  102. * and any thing higher than that won't work. Thus reduce PLLA_OUT0
  103. * to work for lower configurations.
  104. *
  105. * This problem is seen for I2S only, as DMIC and DSPK minimum
  106. * clock requirements are under allowed divider limits.
  107. */
  108. bclk = srate * params_channels(params) * params_width(params);
  109. if (div_u64(plla_out0_rate, bclk) > MAX_PLLA_OUT0_DIV)
  110. plla_out0_rate >>= 1;
  111. dev_dbg(rtd->card->dev,
  112. "Update clock rates: PLLA(= %u Hz) and PLLA_OUT0(= %u Hz)\n",
  113. plla_rate, plla_out0_rate);
  114. /* Set PLLA rate */
  115. err = clk_set_rate(priv->clk_plla, plla_rate);
  116. if (err) {
  117. dev_err(rtd->card->dev,
  118. "Can't set plla rate for %u, err: %d\n",
  119. plla_rate, err);
  120. return err;
  121. }
  122. /* Set PLLA_OUT0 rate */
  123. err = clk_set_rate(priv->clk_plla_out0, plla_out0_rate);
  124. if (err) {
  125. dev_err(rtd->card->dev,
  126. "Can't set plla_out0 rate %u, err: %d\n",
  127. plla_out0_rate, err);
  128. return err;
  129. }
  130. return err;
  131. }
  132. static int tegra_audio_graph_hw_params(struct snd_pcm_substream *substream,
  133. struct snd_pcm_hw_params *params)
  134. {
  135. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  136. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  137. int err;
  138. if (need_clk_update(cpu_dai)) {
  139. err = tegra_audio_graph_update_pll(substream, params);
  140. if (err)
  141. return err;
  142. }
  143. return simple_util_hw_params(substream, params);
  144. }
  145. static const struct snd_soc_ops tegra_audio_graph_ops = {
  146. .startup = simple_util_startup,
  147. .shutdown = simple_util_shutdown,
  148. .hw_params = tegra_audio_graph_hw_params,
  149. };
  150. static int tegra_audio_graph_card_probe(struct snd_soc_card *card)
  151. {
  152. struct simple_util_priv *simple = snd_soc_card_get_drvdata(card);
  153. struct tegra_audio_priv *priv = simple_to_tegra_priv(simple);
  154. priv->clk_plla = devm_clk_get(card->dev, "pll_a");
  155. if (IS_ERR(priv->clk_plla)) {
  156. dev_err(card->dev, "Can't retrieve clk pll_a\n");
  157. return PTR_ERR(priv->clk_plla);
  158. }
  159. priv->clk_plla_out0 = devm_clk_get(card->dev, "plla_out0");
  160. if (IS_ERR(priv->clk_plla_out0)) {
  161. dev_err(card->dev, "Can't retrieve clk plla_out0\n");
  162. return PTR_ERR(priv->clk_plla_out0);
  163. }
  164. return graph_util_card_probe(card);
  165. }
  166. static int tegra_audio_graph_probe(struct platform_device *pdev)
  167. {
  168. struct tegra_audio_priv *priv;
  169. struct device *dev = &pdev->dev;
  170. struct snd_soc_card *card;
  171. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  172. if (!priv)
  173. return -ENOMEM;
  174. card = simple_priv_to_card(&priv->simple);
  175. card->driver_name = "tegra-ape";
  176. card->probe = tegra_audio_graph_card_probe;
  177. /* audio_graph_parse_of() depends on below */
  178. card->component_chaining = 1;
  179. priv->simple.ops = &tegra_audio_graph_ops;
  180. priv->simple.force_dpcm = 1;
  181. return audio_graph_parse_of(&priv->simple, dev);
  182. }
  183. static const struct tegra_audio_cdata tegra210_data = {
  184. /* PLLA */
  185. .plla_rates[x8_RATE] = 368640000,
  186. .plla_rates[x11_RATE] = 338688000,
  187. /* PLLA_OUT0 */
  188. .plla_out0_rates[x8_RATE] = 49152000,
  189. .plla_out0_rates[x11_RATE] = 45158400,
  190. };
  191. static const struct tegra_audio_cdata tegra186_data = {
  192. /* PLLA */
  193. .plla_rates[x8_RATE] = 245760000,
  194. .plla_rates[x11_RATE] = 270950400,
  195. /* PLLA_OUT0 */
  196. .plla_out0_rates[x8_RATE] = 49152000,
  197. .plla_out0_rates[x11_RATE] = 45158400,
  198. };
  199. static const struct tegra_audio_cdata tegra238_data = {
  200. /* PLLA */
  201. .plla_rates[x8_RATE] = 1277952000,
  202. .plla_rates[x11_RATE] = 1264435200,
  203. /* PLLA_OUT0 */
  204. .plla_out0_rates[x8_RATE] = 49152000,
  205. .plla_out0_rates[x11_RATE] = 45158400,
  206. };
  207. static const struct tegra_audio_cdata tegra264_data = {
  208. /* PLLA1 */
  209. .plla_rates[x8_RATE] = 983040000,
  210. .plla_rates[x11_RATE] = 993484800,
  211. /* PLLA1_OUT1 */
  212. .plla_out0_rates[x8_RATE] = 49152000,
  213. .plla_out0_rates[x11_RATE] = 45158400,
  214. };
  215. static const struct of_device_id graph_of_tegra_match[] = {
  216. { .compatible = "nvidia,tegra210-audio-graph-card",
  217. .data = &tegra210_data },
  218. { .compatible = "nvidia,tegra186-audio-graph-card",
  219. .data = &tegra186_data },
  220. { .compatible = "nvidia,tegra238-audio-graph-card",
  221. .data = &tegra238_data },
  222. { .compatible = "nvidia,tegra264-audio-graph-card",
  223. .data = &tegra264_data },
  224. {},
  225. };
  226. MODULE_DEVICE_TABLE(of, graph_of_tegra_match);
  227. static struct platform_driver tegra_audio_graph_card = {
  228. .driver = {
  229. .name = "tegra-audio-graph-card",
  230. .pm = &snd_soc_pm_ops,
  231. .of_match_table = graph_of_tegra_match,
  232. },
  233. .probe = tegra_audio_graph_probe,
  234. .remove = simple_util_remove,
  235. };
  236. module_platform_driver(tegra_audio_graph_card);
  237. MODULE_LICENSE("GPL v2");
  238. MODULE_DESCRIPTION("ASoC Tegra Audio Graph Sound Card");
  239. MODULE_AUTHOR("Sameer Pujar <spujar@nvidia.com>");