atmel_wm8904.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * atmel_wm8904 - Atmel ASoC driver for boards with WM8904 codec.
  3. *
  4. * Copyright (C) 2012 Atmel
  5. *
  6. * Author: Bo Shen <voice.shen@atmel.com>
  7. *
  8. * GPLv2 or later
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <sound/soc.h>
  15. #include "../codecs/wm8904.h"
  16. #include "atmel_ssc_dai.h"
  17. static const struct snd_soc_dapm_widget atmel_asoc_wm8904_dapm_widgets[] = {
  18. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  19. SND_SOC_DAPM_MIC("Mic", NULL),
  20. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  21. };
  22. static int atmel_asoc_wm8904_hw_params(struct snd_pcm_substream *substream,
  23. struct snd_pcm_hw_params *params)
  24. {
  25. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  26. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  27. int ret;
  28. ret = snd_soc_dai_set_pll(codec_dai, WM8904_FLL_MCLK, WM8904_FLL_MCLK,
  29. 32768, params_rate(params) * 256);
  30. if (ret < 0) {
  31. pr_err("%s - failed to set wm8904 codec PLL.", __func__);
  32. return ret;
  33. }
  34. /*
  35. * As here wm8904 use FLL output as its system clock
  36. * so calling set_sysclk won't care freq parameter
  37. * then we pass 0
  38. */
  39. ret = snd_soc_dai_set_sysclk(codec_dai, WM8904_CLK_FLL,
  40. 0, SND_SOC_CLOCK_IN);
  41. if (ret < 0) {
  42. pr_err("%s -failed to set wm8904 SYSCLK\n", __func__);
  43. return ret;
  44. }
  45. return 0;
  46. }
  47. static struct snd_soc_ops atmel_asoc_wm8904_ops = {
  48. .hw_params = atmel_asoc_wm8904_hw_params,
  49. };
  50. static struct snd_soc_dai_link atmel_asoc_wm8904_dailink = {
  51. .name = "WM8904",
  52. .stream_name = "WM8904 PCM",
  53. .codec_dai_name = "wm8904-hifi",
  54. .dai_fmt = SND_SOC_DAIFMT_I2S
  55. | SND_SOC_DAIFMT_NB_NF
  56. | SND_SOC_DAIFMT_CBM_CFM,
  57. .ops = &atmel_asoc_wm8904_ops,
  58. };
  59. static struct snd_soc_card atmel_asoc_wm8904_card = {
  60. .name = "atmel_asoc_wm8904",
  61. .owner = THIS_MODULE,
  62. .dai_link = &atmel_asoc_wm8904_dailink,
  63. .num_links = 1,
  64. .dapm_widgets = atmel_asoc_wm8904_dapm_widgets,
  65. .num_dapm_widgets = ARRAY_SIZE(atmel_asoc_wm8904_dapm_widgets),
  66. .fully_routed = true,
  67. };
  68. static int atmel_asoc_wm8904_dt_init(struct platform_device *pdev)
  69. {
  70. struct device_node *np = pdev->dev.of_node;
  71. struct device_node *codec_np, *cpu_np;
  72. struct snd_soc_card *card = &atmel_asoc_wm8904_card;
  73. struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
  74. int ret;
  75. if (!np) {
  76. dev_err(&pdev->dev, "only device tree supported\n");
  77. return -EINVAL;
  78. }
  79. ret = snd_soc_of_parse_card_name(card, "atmel,model");
  80. if (ret) {
  81. dev_err(&pdev->dev, "failed to parse card name\n");
  82. return ret;
  83. }
  84. ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
  85. if (ret) {
  86. dev_err(&pdev->dev, "failed to parse audio routing\n");
  87. return ret;
  88. }
  89. cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
  90. if (!cpu_np) {
  91. dev_err(&pdev->dev, "failed to get dai and pcm info\n");
  92. ret = -EINVAL;
  93. return ret;
  94. }
  95. dailink->cpu_of_node = cpu_np;
  96. dailink->platform_of_node = cpu_np;
  97. of_node_put(cpu_np);
  98. codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
  99. if (!codec_np) {
  100. dev_err(&pdev->dev, "failed to get codec info\n");
  101. ret = -EINVAL;
  102. return ret;
  103. }
  104. dailink->codec_of_node = codec_np;
  105. of_node_put(codec_np);
  106. return 0;
  107. }
  108. static int atmel_asoc_wm8904_probe(struct platform_device *pdev)
  109. {
  110. struct snd_soc_card *card = &atmel_asoc_wm8904_card;
  111. struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
  112. int id, ret;
  113. card->dev = &pdev->dev;
  114. ret = atmel_asoc_wm8904_dt_init(pdev);
  115. if (ret) {
  116. dev_err(&pdev->dev, "failed to init dt info\n");
  117. return ret;
  118. }
  119. id = of_alias_get_id((struct device_node *)dailink->cpu_of_node, "ssc");
  120. ret = atmel_ssc_set_audio(id);
  121. if (ret != 0) {
  122. dev_err(&pdev->dev, "failed to set SSC %d for audio\n", id);
  123. return ret;
  124. }
  125. ret = snd_soc_register_card(card);
  126. if (ret) {
  127. dev_err(&pdev->dev, "snd_soc_register_card failed\n");
  128. goto err_set_audio;
  129. }
  130. return 0;
  131. err_set_audio:
  132. atmel_ssc_put_audio(id);
  133. return ret;
  134. }
  135. static int atmel_asoc_wm8904_remove(struct platform_device *pdev)
  136. {
  137. struct snd_soc_card *card = platform_get_drvdata(pdev);
  138. struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
  139. int id;
  140. id = of_alias_get_id((struct device_node *)dailink->cpu_of_node, "ssc");
  141. snd_soc_unregister_card(card);
  142. atmel_ssc_put_audio(id);
  143. return 0;
  144. }
  145. #ifdef CONFIG_OF
  146. static const struct of_device_id atmel_asoc_wm8904_dt_ids[] = {
  147. { .compatible = "atmel,asoc-wm8904", },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(of, atmel_asoc_wm8904_dt_ids);
  151. #endif
  152. static struct platform_driver atmel_asoc_wm8904_driver = {
  153. .driver = {
  154. .name = "atmel-wm8904-audio",
  155. .of_match_table = of_match_ptr(atmel_asoc_wm8904_dt_ids),
  156. },
  157. .probe = atmel_asoc_wm8904_probe,
  158. .remove = atmel_asoc_wm8904_remove,
  159. };
  160. module_platform_driver(atmel_asoc_wm8904_driver);
  161. /* Module information */
  162. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  163. MODULE_DESCRIPTION("ALSA SoC machine driver for Atmel EK with WM8904 codec");
  164. MODULE_LICENSE("GPL");