snow.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // ASoC machine driver for Snow boards
  4. #include <linux/clk.h>
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/of.h>
  8. #include <linux/of_device.h>
  9. #include <sound/pcm_params.h>
  10. #include <sound/soc.h>
  11. #include "i2s.h"
  12. #define FIN_PLL_RATE 24000000
  13. SND_SOC_DAILINK_DEFS(links,
  14. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  15. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  16. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  17. struct snow_priv {
  18. struct snd_soc_dai_link dai_link;
  19. struct clk *clk_i2s_bus;
  20. };
  21. static int snow_card_hw_params(struct snd_pcm_substream *substream,
  22. struct snd_pcm_hw_params *params)
  23. {
  24. static const unsigned int pll_rate[] = {
  25. 73728000U, 67737602U, 49152000U, 45158401U, 32768001U
  26. };
  27. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  28. struct snow_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  29. int bfs, psr, rfs, bitwidth;
  30. unsigned long int rclk;
  31. long int freq = -EINVAL;
  32. int ret, i;
  33. bitwidth = snd_pcm_format_width(params_format(params));
  34. if (bitwidth < 0) {
  35. dev_err(rtd->card->dev, "Invalid bit-width: %d\n", bitwidth);
  36. return bitwidth;
  37. }
  38. if (bitwidth != 16 && bitwidth != 24) {
  39. dev_err(rtd->card->dev, "Unsupported bit-width: %d\n", bitwidth);
  40. return -EINVAL;
  41. }
  42. bfs = 2 * bitwidth;
  43. switch (params_rate(params)) {
  44. case 16000:
  45. case 22050:
  46. case 24000:
  47. case 32000:
  48. case 44100:
  49. case 48000:
  50. case 88200:
  51. case 96000:
  52. rfs = 8 * bfs;
  53. break;
  54. case 64000:
  55. rfs = 384;
  56. break;
  57. case 8000:
  58. case 11025:
  59. case 12000:
  60. rfs = 16 * bfs;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. rclk = params_rate(params) * rfs;
  66. for (psr = 8; psr > 0; psr /= 2) {
  67. for (i = 0; i < ARRAY_SIZE(pll_rate); i++) {
  68. if ((pll_rate[i] - rclk * psr) <= 2) {
  69. freq = pll_rate[i];
  70. break;
  71. }
  72. }
  73. }
  74. if (freq < 0) {
  75. dev_err(rtd->card->dev, "Unsupported RCLK rate: %lu\n", rclk);
  76. return -EINVAL;
  77. }
  78. ret = clk_set_rate(priv->clk_i2s_bus, freq);
  79. if (ret < 0) {
  80. dev_err(rtd->card->dev, "I2S bus clock rate set failed\n");
  81. return ret;
  82. }
  83. return 0;
  84. }
  85. static const struct snd_soc_ops snow_card_ops = {
  86. .hw_params = snow_card_hw_params,
  87. };
  88. static int snow_late_probe(struct snd_soc_card *card)
  89. {
  90. struct snd_soc_pcm_runtime *rtd;
  91. struct snd_soc_dai *codec_dai;
  92. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
  93. /* In the multi-codec case codec_dais 0 is MAX98095 and 1 is HDMI. */
  94. codec_dai = asoc_rtd_to_codec(rtd, 0);
  95. /* Set the MCLK rate for the codec */
  96. return snd_soc_dai_set_sysclk(codec_dai, 0,
  97. FIN_PLL_RATE, SND_SOC_CLOCK_IN);
  98. }
  99. static struct snd_soc_card snow_snd = {
  100. .name = "Snow-I2S",
  101. .owner = THIS_MODULE,
  102. .late_probe = snow_late_probe,
  103. };
  104. static int snow_probe(struct platform_device *pdev)
  105. {
  106. struct device *dev = &pdev->dev;
  107. struct snd_soc_card *card = &snow_snd;
  108. struct device_node *cpu, *codec;
  109. struct snd_soc_dai_link *link;
  110. struct snow_priv *priv;
  111. int ret;
  112. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  113. if (!priv)
  114. return -ENOMEM;
  115. link = &priv->dai_link;
  116. link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  117. SND_SOC_DAIFMT_CBS_CFS;
  118. link->name = "Primary";
  119. link->stream_name = link->name;
  120. link->cpus = links_cpus;
  121. link->num_cpus = ARRAY_SIZE(links_cpus);
  122. link->codecs = links_codecs;
  123. link->num_codecs = ARRAY_SIZE(links_codecs);
  124. link->platforms = links_platforms;
  125. link->num_platforms = ARRAY_SIZE(links_platforms);
  126. card->dai_link = link;
  127. card->num_links = 1;
  128. card->dev = dev;
  129. /* Try new DT bindings with HDMI support first. */
  130. cpu = of_get_child_by_name(dev->of_node, "cpu");
  131. if (cpu) {
  132. link->ops = &snow_card_ops;
  133. link->cpus->of_node = of_parse_phandle(cpu, "sound-dai", 0);
  134. of_node_put(cpu);
  135. if (!link->cpus->of_node) {
  136. dev_err(dev, "Failed parsing cpu/sound-dai property\n");
  137. return -EINVAL;
  138. }
  139. codec = of_get_child_by_name(dev->of_node, "codec");
  140. ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
  141. of_node_put(codec);
  142. if (ret < 0) {
  143. of_node_put(link->cpus->of_node);
  144. dev_err(dev, "Failed parsing codec node\n");
  145. return ret;
  146. }
  147. priv->clk_i2s_bus = of_clk_get_by_name(link->cpus->of_node,
  148. "i2s_opclk0");
  149. if (IS_ERR(priv->clk_i2s_bus)) {
  150. snd_soc_of_put_dai_link_codecs(link);
  151. of_node_put(link->cpus->of_node);
  152. return PTR_ERR(priv->clk_i2s_bus);
  153. }
  154. } else {
  155. link->codecs->dai_name = "HiFi";
  156. link->cpus->of_node = of_parse_phandle(dev->of_node,
  157. "samsung,i2s-controller", 0);
  158. if (!link->cpus->of_node) {
  159. dev_err(dev, "i2s-controller property parse error\n");
  160. return -EINVAL;
  161. }
  162. link->codecs->of_node = of_parse_phandle(dev->of_node,
  163. "samsung,audio-codec", 0);
  164. if (!link->codecs->of_node) {
  165. of_node_put(link->cpus->of_node);
  166. dev_err(dev, "audio-codec property parse error\n");
  167. return -EINVAL;
  168. }
  169. }
  170. link->platforms->of_node = link->cpus->of_node;
  171. /* Update card-name if provided through DT, else use default name */
  172. snd_soc_of_parse_card_name(card, "samsung,model");
  173. snd_soc_card_set_drvdata(card, priv);
  174. ret = devm_snd_soc_register_card(dev, card);
  175. if (ret)
  176. return dev_err_probe(&pdev->dev, ret,
  177. "snd_soc_register_card failed\n");
  178. return 0;
  179. }
  180. static int snow_remove(struct platform_device *pdev)
  181. {
  182. struct snow_priv *priv = platform_get_drvdata(pdev);
  183. struct snd_soc_dai_link *link = &priv->dai_link;
  184. of_node_put(link->cpus->of_node);
  185. of_node_put(link->codecs->of_node);
  186. snd_soc_of_put_dai_link_codecs(link);
  187. clk_put(priv->clk_i2s_bus);
  188. return 0;
  189. }
  190. static const struct of_device_id snow_of_match[] = {
  191. { .compatible = "google,snow-audio-max98090", },
  192. { .compatible = "google,snow-audio-max98091", },
  193. { .compatible = "google,snow-audio-max98095", },
  194. {},
  195. };
  196. MODULE_DEVICE_TABLE(of, snow_of_match);
  197. static struct platform_driver snow_driver = {
  198. .driver = {
  199. .name = "snow-audio",
  200. .pm = &snd_soc_pm_ops,
  201. .of_match_table = snow_of_match,
  202. },
  203. .probe = snow_probe,
  204. .remove = snow_remove,
  205. };
  206. module_platform_driver(snow_driver);
  207. MODULE_DESCRIPTION("ALSA SoC Audio machine driver for Snow");
  208. MODULE_LICENSE("GPL");