PageRenderTime 32ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/soc/samsung/snow.c

http://github.com/torvalds/linux
C | 263 lines | 207 code | 49 blank | 7 comment | 23 complexity | 502e416b9b99054665d360fc1b6a6b1a MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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 = substream->private_data;
  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. if (rtd->num_codecs > 1)
  95. codec_dai = asoc_rtd_to_codec(rtd, 0);
  96. else
  97. codec_dai = asoc_rtd_to_codec(rtd, 0);
  98. /* Set the MCLK rate for the codec */
  99. return snd_soc_dai_set_sysclk(codec_dai, 0,
  100. FIN_PLL_RATE, SND_SOC_CLOCK_IN);
  101. }
  102. static struct snd_soc_card snow_snd = {
  103. .name = "Snow-I2S",
  104. .owner = THIS_MODULE,
  105. .late_probe = snow_late_probe,
  106. };
  107. static int snow_probe(struct platform_device *pdev)
  108. {
  109. struct device *dev = &pdev->dev;
  110. struct snd_soc_card *card = &snow_snd;
  111. struct device_node *cpu, *codec;
  112. struct snd_soc_dai_link *link;
  113. struct snow_priv *priv;
  114. int ret;
  115. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  116. if (!priv)
  117. return -ENOMEM;
  118. link = &priv->dai_link;
  119. link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  120. SND_SOC_DAIFMT_CBS_CFS;
  121. link->name = "Primary";
  122. link->stream_name = link->name;
  123. link->cpus = links_cpus;
  124. link->num_cpus = ARRAY_SIZE(links_cpus);
  125. link->codecs = links_codecs;
  126. link->num_codecs = ARRAY_SIZE(links_codecs);
  127. link->platforms = links_platforms;
  128. link->num_platforms = ARRAY_SIZE(links_platforms);
  129. card->dai_link = link;
  130. card->num_links = 1;
  131. card->dev = dev;
  132. /* Try new DT bindings with HDMI support first. */
  133. cpu = of_get_child_by_name(dev->of_node, "cpu");
  134. if (cpu) {
  135. link->ops = &snow_card_ops;
  136. link->cpus->of_node = of_parse_phandle(cpu, "sound-dai", 0);
  137. of_node_put(cpu);
  138. if (!link->cpus->of_node) {
  139. dev_err(dev, "Failed parsing cpu/sound-dai property\n");
  140. return -EINVAL;
  141. }
  142. codec = of_get_child_by_name(dev->of_node, "codec");
  143. ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
  144. of_node_put(codec);
  145. if (ret < 0) {
  146. of_node_put(link->cpus->of_node);
  147. dev_err(dev, "Failed parsing codec node\n");
  148. return ret;
  149. }
  150. priv->clk_i2s_bus = of_clk_get_by_name(link->cpus->of_node,
  151. "i2s_opclk0");
  152. if (IS_ERR(priv->clk_i2s_bus)) {
  153. snd_soc_of_put_dai_link_codecs(link);
  154. of_node_put(link->cpus->of_node);
  155. return PTR_ERR(priv->clk_i2s_bus);
  156. }
  157. } else {
  158. link->codecs->dai_name = "HiFi",
  159. link->cpus->of_node = of_parse_phandle(dev->of_node,
  160. "samsung,i2s-controller", 0);
  161. if (!link->cpus->of_node) {
  162. dev_err(dev, "i2s-controller property parse error\n");
  163. return -EINVAL;
  164. }
  165. link->codecs->of_node = of_parse_phandle(dev->of_node,
  166. "samsung,audio-codec", 0);
  167. if (!link->codecs->of_node) {
  168. of_node_put(link->cpus->of_node);
  169. dev_err(dev, "audio-codec property parse error\n");
  170. return -EINVAL;
  171. }
  172. }
  173. link->platforms->of_node = link->cpus->of_node;
  174. /* Update card-name if provided through DT, else use default name */
  175. snd_soc_of_parse_card_name(card, "samsung,model");
  176. snd_soc_card_set_drvdata(card, priv);
  177. ret = devm_snd_soc_register_card(dev, card);
  178. if (ret) {
  179. if (ret != -EPROBE_DEFER)
  180. dev_err(&pdev->dev,
  181. "snd_soc_register_card failed (%d)\n", ret);
  182. return ret;
  183. }
  184. return ret;
  185. }
  186. static int snow_remove(struct platform_device *pdev)
  187. {
  188. struct snow_priv *priv = platform_get_drvdata(pdev);
  189. struct snd_soc_dai_link *link = &priv->dai_link;
  190. of_node_put(link->cpus->of_node);
  191. of_node_put(link->codecs->of_node);
  192. snd_soc_of_put_dai_link_codecs(link);
  193. clk_put(priv->clk_i2s_bus);
  194. return 0;
  195. }
  196. static const struct of_device_id snow_of_match[] = {
  197. { .compatible = "google,snow-audio-max98090", },
  198. { .compatible = "google,snow-audio-max98091", },
  199. { .compatible = "google,snow-audio-max98095", },
  200. {},
  201. };
  202. MODULE_DEVICE_TABLE(of, snow_of_match);
  203. static struct platform_driver snow_driver = {
  204. .driver = {
  205. .name = "snow-audio",
  206. .pm = &snd_soc_pm_ops,
  207. .of_match_table = snow_of_match,
  208. },
  209. .probe = snow_probe,
  210. .remove = snow_remove,
  211. };
  212. module_platform_driver(snow_driver);
  213. MODULE_DESCRIPTION("ALSA SoC Audio machine driver for Snow");
  214. MODULE_LICENSE("GPL");