snow.c 6.0 KB

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