odroid.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2017 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/module.h>
  12. #include <sound/soc.h>
  13. #include <sound/pcm_params.h>
  14. #include "i2s.h"
  15. #include "i2s-regs.h"
  16. struct odroid_priv {
  17. struct snd_soc_card card;
  18. struct snd_soc_dai_link dai_link;
  19. struct clk *clk_i2s_bus;
  20. struct clk *sclk_i2s;
  21. };
  22. static int odroid_card_startup(struct snd_pcm_substream *substream)
  23. {
  24. struct snd_pcm_runtime *runtime = substream->runtime;
  25. snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  26. return 0;
  27. }
  28. static int odroid_card_hw_params(struct snd_pcm_substream *substream,
  29. struct snd_pcm_hw_params *params)
  30. {
  31. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  32. struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  33. unsigned int pll_freq, rclk_freq, rfs;
  34. int ret;
  35. switch (params_rate(params)) {
  36. case 64000:
  37. pll_freq = 196608001U;
  38. rfs = 384;
  39. break;
  40. case 44100:
  41. case 88200:
  42. pll_freq = 180633609U;
  43. rfs = 512;
  44. break;
  45. case 32000:
  46. case 48000:
  47. case 96000:
  48. pll_freq = 196608001U;
  49. rfs = 512;
  50. break;
  51. default:
  52. return -EINVAL;
  53. }
  54. ret = clk_set_rate(priv->clk_i2s_bus, pll_freq / 2 + 1);
  55. if (ret < 0)
  56. return ret;
  57. /*
  58. * We add 2 to the rclk_freq value in order to avoid too low clock
  59. * frequency values due to the EPLL output frequency not being exact
  60. * multiple of the audio sampling rate.
  61. */
  62. rclk_freq = params_rate(params) * rfs + 2;
  63. ret = clk_set_rate(priv->sclk_i2s, rclk_freq);
  64. if (ret < 0)
  65. return ret;
  66. if (rtd->num_codecs > 1) {
  67. struct snd_soc_dai *codec_dai = rtd->codec_dais[1];
  68. ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq,
  69. SND_SOC_CLOCK_IN);
  70. if (ret < 0)
  71. return ret;
  72. }
  73. return 0;
  74. }
  75. static const struct snd_soc_ops odroid_card_ops = {
  76. .startup = odroid_card_startup,
  77. .hw_params = odroid_card_hw_params,
  78. };
  79. static int odroid_audio_probe(struct platform_device *pdev)
  80. {
  81. struct device *dev = &pdev->dev;
  82. struct device_node *cpu, *codec;
  83. struct odroid_priv *priv;
  84. struct snd_soc_dai_link *link;
  85. struct snd_soc_card *card;
  86. int ret;
  87. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  88. if (!priv)
  89. return -ENOMEM;
  90. card = &priv->card;
  91. card->dev = dev;
  92. card->owner = THIS_MODULE;
  93. card->fully_routed = true;
  94. snd_soc_card_set_drvdata(card, priv);
  95. ret = snd_soc_of_parse_card_name(card, "model");
  96. if (ret < 0)
  97. return ret;
  98. if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) {
  99. ret = snd_soc_of_parse_audio_simple_widgets(card,
  100. "samsung,audio-widgets");
  101. if (ret < 0)
  102. return ret;
  103. }
  104. if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) {
  105. ret = snd_soc_of_parse_audio_routing(card,
  106. "samsung,audio-routing");
  107. if (ret < 0)
  108. return ret;
  109. }
  110. link = &priv->dai_link;
  111. link->ops = &odroid_card_ops;
  112. link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  113. SND_SOC_DAIFMT_CBS_CFS;
  114. card->dai_link = &priv->dai_link;
  115. card->num_links = 1;
  116. cpu = of_get_child_by_name(dev->of_node, "cpu");
  117. codec = of_get_child_by_name(dev->of_node, "codec");
  118. link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
  119. if (!link->cpu_of_node) {
  120. dev_err(dev, "Failed parsing cpu/sound-dai property\n");
  121. return -EINVAL;
  122. }
  123. ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
  124. if (ret < 0)
  125. goto err_put_codec_n;
  126. link->platform_of_node = link->cpu_of_node;
  127. link->name = "Primary";
  128. link->stream_name = link->name;
  129. priv->sclk_i2s = of_clk_get_by_name(link->cpu_of_node, "i2s_opclk1");
  130. if (IS_ERR(priv->sclk_i2s)) {
  131. ret = PTR_ERR(priv->sclk_i2s);
  132. goto err_put_i2s_n;
  133. }
  134. priv->clk_i2s_bus = of_clk_get_by_name(link->cpu_of_node, "iis");
  135. if (IS_ERR(priv->clk_i2s_bus)) {
  136. ret = PTR_ERR(priv->clk_i2s_bus);
  137. goto err_put_sclk;
  138. }
  139. ret = devm_snd_soc_register_card(dev, card);
  140. if (ret < 0) {
  141. dev_err(dev, "snd_soc_register_card() failed: %d\n", ret);
  142. goto err_put_clk_i2s;
  143. }
  144. return 0;
  145. err_put_clk_i2s:
  146. clk_put(priv->clk_i2s_bus);
  147. err_put_sclk:
  148. clk_put(priv->sclk_i2s);
  149. err_put_i2s_n:
  150. of_node_put(link->cpu_of_node);
  151. err_put_codec_n:
  152. snd_soc_of_put_dai_link_codecs(link);
  153. return ret;
  154. }
  155. static int odroid_audio_remove(struct platform_device *pdev)
  156. {
  157. struct odroid_priv *priv = platform_get_drvdata(pdev);
  158. of_node_put(priv->dai_link.cpu_of_node);
  159. snd_soc_of_put_dai_link_codecs(&priv->dai_link);
  160. clk_put(priv->sclk_i2s);
  161. clk_put(priv->clk_i2s_bus);
  162. return 0;
  163. }
  164. static const struct of_device_id odroid_audio_of_match[] = {
  165. { .compatible = "hardkernel,odroid-xu3-audio" },
  166. { .compatible = "hardkernel,odroid-xu4-audio" },
  167. { .compatible = "samsung,odroid-xu3-audio" },
  168. { .compatible = "samsung,odroid-xu4-audio" },
  169. { },
  170. };
  171. MODULE_DEVICE_TABLE(of, odroid_audio_of_match);
  172. static struct platform_driver odroid_audio_driver = {
  173. .driver = {
  174. .name = "odroid-audio",
  175. .of_match_table = odroid_audio_of_match,
  176. .pm = &snd_soc_pm_ops,
  177. },
  178. .probe = odroid_audio_probe,
  179. .remove = odroid_audio_remove,
  180. };
  181. module_platform_driver(odroid_audio_driver);
  182. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  183. MODULE_DESCRIPTION("Odroid XU3/XU4 audio support");
  184. MODULE_LICENSE("GPL v2");