mxs-sgtl5000.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2011 Freescale Semiconductor, Inc.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/device.h>
  7. #include <linux/of.h>
  8. #include <linux/of_device.h>
  9. #include <sound/core.h>
  10. #include <sound/pcm.h>
  11. #include <sound/soc.h>
  12. #include <sound/jack.h>
  13. #include <sound/soc-dapm.h>
  14. #include "../codecs/sgtl5000.h"
  15. #include "mxs-saif.h"
  16. static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream,
  17. struct snd_pcm_hw_params *params)
  18. {
  19. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  20. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  21. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  22. unsigned int rate = params_rate(params);
  23. u32 mclk;
  24. int ret;
  25. /* sgtl5000 does not support 512*rate when in 96000 fs */
  26. switch (rate) {
  27. case 96000:
  28. mclk = 256 * rate;
  29. break;
  30. default:
  31. mclk = 512 * rate;
  32. break;
  33. }
  34. /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
  35. ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
  36. if (ret) {
  37. dev_err(codec_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
  38. mclk / 1000000, mclk / 1000 % 1000);
  39. return ret;
  40. }
  41. /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
  42. ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
  43. if (ret) {
  44. dev_err(cpu_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
  45. mclk / 1000000, mclk / 1000 % 1000);
  46. return ret;
  47. }
  48. return 0;
  49. }
  50. static const struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
  51. .hw_params = mxs_sgtl5000_hw_params,
  52. };
  53. #define MXS_SGTL5000_DAI_FMT (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | \
  54. SND_SOC_DAIFMT_CBS_CFS)
  55. SND_SOC_DAILINK_DEFS(hifi_tx,
  56. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  57. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")),
  58. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  59. SND_SOC_DAILINK_DEFS(hifi_rx,
  60. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  61. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")),
  62. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  63. static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
  64. {
  65. .name = "HiFi Tx",
  66. .stream_name = "HiFi Playback",
  67. .dai_fmt = MXS_SGTL5000_DAI_FMT,
  68. .ops = &mxs_sgtl5000_hifi_ops,
  69. .playback_only = true,
  70. SND_SOC_DAILINK_REG(hifi_tx),
  71. }, {
  72. .name = "HiFi Rx",
  73. .stream_name = "HiFi Capture",
  74. .dai_fmt = MXS_SGTL5000_DAI_FMT,
  75. .ops = &mxs_sgtl5000_hifi_ops,
  76. .capture_only = true,
  77. SND_SOC_DAILINK_REG(hifi_rx),
  78. },
  79. };
  80. static const struct snd_soc_dapm_widget mxs_sgtl5000_dapm_widgets[] = {
  81. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  82. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  83. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  84. SND_SOC_DAPM_SPK("Line Out Jack", NULL),
  85. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  86. };
  87. static struct snd_soc_card mxs_sgtl5000 = {
  88. .name = "mxs_sgtl5000",
  89. .owner = THIS_MODULE,
  90. .dai_link = mxs_sgtl5000_dai,
  91. .num_links = ARRAY_SIZE(mxs_sgtl5000_dai),
  92. };
  93. static int mxs_sgtl5000_probe(struct platform_device *pdev)
  94. {
  95. struct snd_soc_card *card = &mxs_sgtl5000;
  96. int ret, i;
  97. struct device_node *np = pdev->dev.of_node;
  98. struct device_node *saif_np[2], *codec_np;
  99. saif_np[0] = of_parse_phandle(np, "saif-controllers", 0);
  100. saif_np[1] = of_parse_phandle(np, "saif-controllers", 1);
  101. codec_np = of_parse_phandle(np, "audio-codec", 0);
  102. if (!saif_np[0] || !saif_np[1] || !codec_np) {
  103. dev_err(&pdev->dev, "phandle missing or invalid\n");
  104. of_node_put(codec_np);
  105. of_node_put(saif_np[0]);
  106. of_node_put(saif_np[1]);
  107. return -EINVAL;
  108. }
  109. for (i = 0; i < 2; i++) {
  110. mxs_sgtl5000_dai[i].codecs->name = NULL;
  111. mxs_sgtl5000_dai[i].codecs->of_node = codec_np;
  112. mxs_sgtl5000_dai[i].cpus->dai_name = NULL;
  113. mxs_sgtl5000_dai[i].cpus->of_node = saif_np[i];
  114. mxs_sgtl5000_dai[i].platforms->name = NULL;
  115. mxs_sgtl5000_dai[i].platforms->of_node = saif_np[i];
  116. }
  117. of_node_put(codec_np);
  118. of_node_put(saif_np[0]);
  119. of_node_put(saif_np[1]);
  120. /*
  121. * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
  122. * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
  123. * should be >= 8MHz and <= 27M.
  124. */
  125. ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
  126. if (ret) {
  127. dev_err(&pdev->dev, "failed to get mclk\n");
  128. return ret;
  129. }
  130. card->dev = &pdev->dev;
  131. if (of_find_property(np, "audio-routing", NULL)) {
  132. card->dapm_widgets = mxs_sgtl5000_dapm_widgets;
  133. card->num_dapm_widgets = ARRAY_SIZE(mxs_sgtl5000_dapm_widgets);
  134. ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
  135. if (ret) {
  136. dev_err(&pdev->dev, "failed to parse audio-routing (%d)\n",
  137. ret);
  138. return ret;
  139. }
  140. }
  141. ret = devm_snd_soc_register_card(&pdev->dev, card);
  142. if (ret)
  143. return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
  144. return 0;
  145. }
  146. static int mxs_sgtl5000_remove(struct platform_device *pdev)
  147. {
  148. mxs_saif_put_mclk(0);
  149. return 0;
  150. }
  151. static const struct of_device_id mxs_sgtl5000_dt_ids[] = {
  152. { .compatible = "fsl,mxs-audio-sgtl5000", },
  153. { /* sentinel */ }
  154. };
  155. MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids);
  156. static struct platform_driver mxs_sgtl5000_audio_driver = {
  157. .driver = {
  158. .name = "mxs-sgtl5000",
  159. .of_match_table = mxs_sgtl5000_dt_ids,
  160. },
  161. .probe = mxs_sgtl5000_probe,
  162. .remove = mxs_sgtl5000_remove,
  163. };
  164. module_platform_driver(mxs_sgtl5000_audio_driver);
  165. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  166. MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
  167. MODULE_LICENSE("GPL");
  168. MODULE_ALIAS("platform:mxs-sgtl5000");