mxs-sgtl5000.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/soc.h>
  25. #include <sound/jack.h>
  26. #include <sound/soc-dapm.h>
  27. #include <asm/mach-types.h>
  28. #include "../codecs/sgtl5000.h"
  29. #include "mxs-saif.h"
  30. static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream,
  31. struct snd_pcm_hw_params *params)
  32. {
  33. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  34. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  35. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  36. unsigned int rate = params_rate(params);
  37. u32 dai_format, mclk;
  38. int ret;
  39. /* sgtl5000 does not support 512*rate when in 96000 fs */
  40. switch (rate) {
  41. case 96000:
  42. mclk = 256 * rate;
  43. break;
  44. default:
  45. mclk = 512 * rate;
  46. break;
  47. }
  48. /* Sgtl5000 sysclk should be >= 8MHz and <= 27M */
  49. if (mclk < 8000000 || mclk > 27000000)
  50. return -EINVAL;
  51. /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
  52. ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
  53. if (ret)
  54. return ret;
  55. /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
  56. ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
  57. if (ret)
  58. return ret;
  59. /* set codec to slave mode */
  60. dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  61. SND_SOC_DAIFMT_CBS_CFS;
  62. /* set codec DAI configuration */
  63. ret = snd_soc_dai_set_fmt(codec_dai, dai_format);
  64. if (ret)
  65. return ret;
  66. /* set cpu DAI configuration */
  67. ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
  68. if (ret)
  69. return ret;
  70. return 0;
  71. }
  72. static struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
  73. .hw_params = mxs_sgtl5000_hw_params,
  74. };
  75. static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
  76. {
  77. .name = "HiFi Tx",
  78. .stream_name = "HiFi Playback",
  79. .codec_dai_name = "sgtl5000",
  80. .codec_name = "sgtl5000.0-000a",
  81. .cpu_dai_name = "mxs-saif.0",
  82. .platform_name = "mxs-saif.0",
  83. .ops = &mxs_sgtl5000_hifi_ops,
  84. }, {
  85. .name = "HiFi Rx",
  86. .stream_name = "HiFi Capture",
  87. .codec_dai_name = "sgtl5000",
  88. .codec_name = "sgtl5000.0-000a",
  89. .cpu_dai_name = "mxs-saif.1",
  90. .platform_name = "mxs-saif.1",
  91. .ops = &mxs_sgtl5000_hifi_ops,
  92. },
  93. };
  94. static struct snd_soc_card mxs_sgtl5000 = {
  95. .name = "mxs_sgtl5000",
  96. .owner = THIS_MODULE,
  97. .dai_link = mxs_sgtl5000_dai,
  98. .num_links = ARRAY_SIZE(mxs_sgtl5000_dai),
  99. };
  100. static int __devinit mxs_sgtl5000_probe_dt(struct platform_device *pdev)
  101. {
  102. struct device_node *np = pdev->dev.of_node;
  103. struct device_node *saif_np[2], *codec_np;
  104. int i, ret = 0;
  105. if (!np)
  106. return 1; /* no device tree */
  107. saif_np[0] = of_parse_phandle(np, "saif-controllers", 0);
  108. saif_np[1] = of_parse_phandle(np, "saif-controllers", 1);
  109. codec_np = of_parse_phandle(np, "audio-codec", 0);
  110. if (!saif_np[0] || !saif_np[1] || !codec_np) {
  111. dev_err(&pdev->dev, "phandle missing or invalid\n");
  112. return -EINVAL;
  113. }
  114. for (i = 0; i < 2; i++) {
  115. mxs_sgtl5000_dai[i].codec_name = NULL;
  116. mxs_sgtl5000_dai[i].codec_of_node = codec_np;
  117. mxs_sgtl5000_dai[i].cpu_dai_name = NULL;
  118. mxs_sgtl5000_dai[i].cpu_dai_of_node = saif_np[i];
  119. mxs_sgtl5000_dai[i].platform_name = NULL;
  120. mxs_sgtl5000_dai[i].platform_of_node = saif_np[i];
  121. }
  122. of_node_put(codec_np);
  123. of_node_put(saif_np[0]);
  124. of_node_put(saif_np[1]);
  125. return ret;
  126. }
  127. static int __devinit mxs_sgtl5000_probe(struct platform_device *pdev)
  128. {
  129. struct snd_soc_card *card = &mxs_sgtl5000;
  130. int ret;
  131. ret = mxs_sgtl5000_probe_dt(pdev);
  132. if (ret < 0)
  133. return ret;
  134. /*
  135. * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
  136. * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
  137. * should be >= 8MHz and <= 27M.
  138. */
  139. ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
  140. if (ret)
  141. return ret;
  142. card->dev = &pdev->dev;
  143. platform_set_drvdata(pdev, card);
  144. ret = snd_soc_register_card(card);
  145. if (ret) {
  146. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  147. ret);
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static int __devexit mxs_sgtl5000_remove(struct platform_device *pdev)
  153. {
  154. struct snd_soc_card *card = platform_get_drvdata(pdev);
  155. mxs_saif_put_mclk(0);
  156. snd_soc_unregister_card(card);
  157. return 0;
  158. }
  159. static const struct of_device_id mxs_sgtl5000_dt_ids[] = {
  160. { .compatible = "fsl,mxs-audio-sgtl5000", },
  161. { /* sentinel */ }
  162. };
  163. MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids);
  164. static struct platform_driver mxs_sgtl5000_audio_driver = {
  165. .driver = {
  166. .name = "mxs-sgtl5000",
  167. .owner = THIS_MODULE,
  168. .of_match_table = mxs_sgtl5000_dt_ids,
  169. },
  170. .probe = mxs_sgtl5000_probe,
  171. .remove = __devexit_p(mxs_sgtl5000_remove),
  172. };
  173. module_platform_driver(mxs_sgtl5000_audio_driver);
  174. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  175. MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
  176. MODULE_LICENSE("GPL");
  177. MODULE_ALIAS("platform:mxs-sgtl5000");