mxs-sgtl5000.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2011 Freescale Semiconductor, Inc.
  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 as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/soc.h>
  23. #include <sound/jack.h>
  24. #include <sound/soc-dapm.h>
  25. #include <asm/mach-types.h>
  26. #include "../codecs/sgtl5000.h"
  27. #include "mxs-saif.h"
  28. static int mxs_sgtl5000_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 snd_soc_dai *codec_dai = rtd->codec_dai;
  33. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  34. unsigned int rate = params_rate(params);
  35. u32 dai_format, mclk;
  36. int ret;
  37. /* sgtl5000 does not support 512*rate when in 96000 fs */
  38. switch (rate) {
  39. case 96000:
  40. mclk = 256 * rate;
  41. break;
  42. default:
  43. mclk = 512 * rate;
  44. break;
  45. }
  46. /* Sgtl5000 sysclk should be >= 8MHz and <= 27M */
  47. if (mclk < 8000000 || mclk > 27000000)
  48. return -EINVAL;
  49. /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
  50. ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
  51. if (ret)
  52. return ret;
  53. /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
  54. ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
  55. if (ret)
  56. return ret;
  57. /* set codec to slave mode */
  58. dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  59. SND_SOC_DAIFMT_CBS_CFS;
  60. /* set codec DAI configuration */
  61. ret = snd_soc_dai_set_fmt(codec_dai, dai_format);
  62. if (ret)
  63. return ret;
  64. /* set cpu DAI configuration */
  65. ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
  66. if (ret)
  67. return ret;
  68. return 0;
  69. }
  70. static struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
  71. .hw_params = mxs_sgtl5000_hw_params,
  72. };
  73. static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
  74. {
  75. .name = "HiFi Tx",
  76. .stream_name = "HiFi Playback",
  77. .codec_dai_name = "sgtl5000",
  78. .codec_name = "sgtl5000.0-000a",
  79. .cpu_dai_name = "mxs-saif.0",
  80. .platform_name = "mxs-pcm-audio.0",
  81. .ops = &mxs_sgtl5000_hifi_ops,
  82. }, {
  83. .name = "HiFi Rx",
  84. .stream_name = "HiFi Capture",
  85. .codec_dai_name = "sgtl5000",
  86. .codec_name = "sgtl5000.0-000a",
  87. .cpu_dai_name = "mxs-saif.1",
  88. .platform_name = "mxs-pcm-audio.1",
  89. .ops = &mxs_sgtl5000_hifi_ops,
  90. },
  91. };
  92. static struct snd_soc_card mxs_sgtl5000 = {
  93. .name = "mxs_sgtl5000",
  94. .dai_link = mxs_sgtl5000_dai,
  95. .num_links = ARRAY_SIZE(mxs_sgtl5000_dai),
  96. };
  97. static int __devinit mxs_sgtl5000_probe(struct platform_device *pdev)
  98. {
  99. struct snd_soc_card *card = &mxs_sgtl5000;
  100. int ret;
  101. /*
  102. * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
  103. * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
  104. * should be >= 8MHz and <= 27M.
  105. */
  106. ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
  107. if (ret)
  108. return ret;
  109. card->dev = &pdev->dev;
  110. platform_set_drvdata(pdev, card);
  111. ret = snd_soc_register_card(card);
  112. if (ret) {
  113. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  114. ret);
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. static int __devexit mxs_sgtl5000_remove(struct platform_device *pdev)
  120. {
  121. struct snd_soc_card *card = platform_get_drvdata(pdev);
  122. mxs_saif_put_mclk(0);
  123. snd_soc_unregister_card(card);
  124. return 0;
  125. }
  126. static struct platform_driver mxs_sgtl5000_audio_driver = {
  127. .driver = {
  128. .name = "mxs-sgtl5000",
  129. .owner = THIS_MODULE,
  130. },
  131. .probe = mxs_sgtl5000_probe,
  132. .remove = __devexit_p(mxs_sgtl5000_remove),
  133. };
  134. static int __init mxs_sgtl5000_init(void)
  135. {
  136. return platform_driver_register(&mxs_sgtl5000_audio_driver);
  137. }
  138. module_init(mxs_sgtl5000_init);
  139. static void __exit mxs_sgtl5000_exit(void)
  140. {
  141. platform_driver_unregister(&mxs_sgtl5000_audio_driver);
  142. }
  143. module_exit(mxs_sgtl5000_exit);
  144. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  145. MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
  146. MODULE_LICENSE("GPL");