smdk_spdif.c 5.1 KB

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