smdk_spdif.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * smdk_spdif.c -- S/PDIF audio for SMDK
  3. *
  4. * Copyright 2010 Samsung Electronics Co. Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/clk.h>
  15. #include <plat/devs.h>
  16. #include <sound/soc.h>
  17. #include "s3c-dma.h"
  18. #include "spdif.h"
  19. /* Audio clock settings are belonged to board specific part. Every
  20. * board can set audio source clock setting which is matched with H/W
  21. * like this function-'set_audio_clock_heirachy'.
  22. */
  23. static int set_audio_clock_heirachy(struct platform_device *pdev)
  24. {
  25. struct clk *fout_epll, *mout_epll, *sclk_audio0, *sclk_spdif;
  26. int ret;
  27. fout_epll = clk_get(NULL, "fout_epll");
  28. if (IS_ERR(fout_epll)) {
  29. printk(KERN_WARNING "%s: Cannot find fout_epll.\n",
  30. __func__);
  31. return -EINVAL;
  32. }
  33. mout_epll = clk_get(NULL, "mout_epll");
  34. if (IS_ERR(mout_epll)) {
  35. printk(KERN_WARNING "%s: Cannot find mout_epll.\n",
  36. __func__);
  37. ret = -EINVAL;
  38. goto out1;
  39. }
  40. sclk_audio0 = clk_get(&pdev->dev, "sclk_audio");
  41. if (IS_ERR(sclk_audio0)) {
  42. printk(KERN_WARNING "%s: Cannot find sclk_audio.\n",
  43. __func__);
  44. ret = -EINVAL;
  45. goto out2;
  46. }
  47. sclk_spdif = clk_get(NULL, "sclk_spdif");
  48. if (IS_ERR(sclk_spdif)) {
  49. printk(KERN_WARNING "%s: Cannot find sclk_spdif.\n",
  50. __func__);
  51. ret = -EINVAL;
  52. goto out3;
  53. }
  54. /* Set audio clock heirachy for S/PDIF */
  55. clk_set_parent(mout_epll, fout_epll);
  56. clk_set_parent(sclk_audio0, mout_epll);
  57. clk_set_parent(sclk_spdif, sclk_audio0);
  58. clk_put(sclk_spdif);
  59. out3:
  60. clk_put(sclk_audio0);
  61. out2:
  62. clk_put(mout_epll);
  63. out1:
  64. clk_put(fout_epll);
  65. return ret;
  66. }
  67. /* We should haved to set clock directly on this part because of clock
  68. * scheme of Samsudng SoCs did not support to set rates from abstrct
  69. * clock of it's heirachy.
  70. */
  71. static int set_audio_clock_rate(unsigned long epll_rate,
  72. unsigned long audio_rate)
  73. {
  74. struct clk *fout_epll, *sclk_spdif;
  75. fout_epll = clk_get(NULL, "fout_epll");
  76. if (IS_ERR(fout_epll)) {
  77. printk(KERN_ERR "%s: failed to get fout_epll\n", __func__);
  78. return -ENOENT;
  79. }
  80. clk_set_rate(fout_epll, epll_rate);
  81. clk_put(fout_epll);
  82. sclk_spdif = clk_get(NULL, "sclk_spdif");
  83. if (IS_ERR(sclk_spdif)) {
  84. printk(KERN_ERR "%s: failed to get sclk_spdif\n", __func__);
  85. return -ENOENT;
  86. }
  87. clk_set_rate(sclk_spdif, audio_rate);
  88. clk_put(sclk_spdif);
  89. return 0;
  90. }
  91. static int smdk_hw_params(struct snd_pcm_substream *substream,
  92. struct snd_pcm_hw_params *params)
  93. {
  94. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  95. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  96. unsigned long pll_out, rclk_rate;
  97. int ret, ratio;
  98. switch (params_rate(params)) {
  99. case 44100:
  100. pll_out = 45158400;
  101. break;
  102. case 32000:
  103. case 48000:
  104. case 96000:
  105. pll_out = 49152000;
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. /* Setting ratio to 512fs helps to use S/PDIF with HDMI without
  111. * modify S/PDIF ASoC machine driver.
  112. */
  113. ratio = 512;
  114. rclk_rate = params_rate(params) * ratio;
  115. /* Set audio source clock rates */
  116. ret = set_audio_clock_rate(pll_out, rclk_rate);
  117. if (ret < 0)
  118. return ret;
  119. /* Set S/PDIF uses internal source clock */
  120. ret = snd_soc_dai_set_sysclk(cpu_dai, SND_SOC_SPDIF_INT_MCLK,
  121. rclk_rate, SND_SOC_CLOCK_IN);
  122. if (ret < 0)
  123. return ret;
  124. return ret;
  125. }
  126. static struct snd_soc_ops smdk_spdif_ops = {
  127. .hw_params = smdk_hw_params,
  128. };
  129. static struct snd_soc_card smdk;
  130. static struct snd_soc_dai_link smdk_dai = {
  131. .name = "S/PDIF",
  132. .stream_name = "S/PDIF PCM Playback",
  133. .platform_name = "s3c24xx-pcm-audio",
  134. .cpu_dai_name = "samsung-spdif",
  135. .codec_dai_name = "dit-hifi",
  136. .codec_name = "spdif-dit",
  137. .ops = &smdk_spdif_ops,
  138. };
  139. static struct snd_soc_card smdk = {
  140. .name = "SMDK-S/PDIF",
  141. .dai_link = &smdk_dai,
  142. .num_links = 1,
  143. };
  144. static struct platform_device *smdk_snd_spdif_dit_device;
  145. static struct platform_device *smdk_snd_spdif_device;
  146. static int __init smdk_init(void)
  147. {
  148. int ret;
  149. smdk_snd_spdif_dit_device = platform_device_alloc("spdif-dit", -1);
  150. if (!smdk_snd_spdif_dit_device)
  151. return -ENOMEM;
  152. ret = platform_device_add(smdk_snd_spdif_dit_device);
  153. if (ret)
  154. goto err2;
  155. smdk_snd_spdif_device = platform_device_alloc("soc-audio", -1);
  156. if (!smdk_snd_spdif_device) {
  157. ret = -ENOMEM;
  158. goto err2;
  159. }
  160. platform_set_drvdata(smdk_snd_spdif_device, &smdk);
  161. ret = platform_device_add(smdk_snd_spdif_device);
  162. if (ret)
  163. goto err1;
  164. /* Set audio clock heirachy manually */
  165. ret = set_audio_clock_heirachy(smdk_snd_spdif_device);
  166. if (ret)
  167. goto err1;
  168. return 0;
  169. err1:
  170. platform_device_put(smdk_snd_spdif_device);
  171. err2:
  172. platform_device_put(smdk_snd_spdif_dit_device);
  173. return ret;
  174. }
  175. static void __exit smdk_exit(void)
  176. {
  177. platform_device_unregister(smdk_snd_spdif_device);
  178. }
  179. module_init(smdk_init);
  180. module_exit(smdk_exit);
  181. MODULE_AUTHOR("Seungwhan Youn, <sw.youn@samsung.com>");
  182. MODULE_DESCRIPTION("ALSA SoC SMDK+S/PDIF");
  183. MODULE_LICENSE("GPL");