atmel_wm8904.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #define MCLK_RATE 32768
  18. static struct clk *mclk;
  19. static const struct snd_soc_dapm_widget atmel_asoc_wm8904_dapm_widgets[] = {
  20. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  21. SND_SOC_DAPM_MIC("Mic", NULL),
  22. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  23. };
  24. static int atmel_asoc_wm8904_hw_params(struct snd_pcm_substream *substream,
  25. struct snd_pcm_hw_params *params)
  26. {
  27. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  28. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  29. int ret;
  30. ret = snd_soc_dai_set_pll(codec_dai, WM8904_FLL_MCLK, WM8904_FLL_MCLK,
  31. 32768, params_rate(params) * 256);
  32. if (ret < 0) {
  33. pr_err("%s - failed to set wm8904 codec PLL.", __func__);
  34. return ret;
  35. }
  36. /*
  37. * As here wm8904 use FLL output as its system clock
  38. * so calling set_sysclk won't care freq parameter
  39. * then we pass 0
  40. */
  41. ret = snd_soc_dai_set_sysclk(codec_dai, WM8904_CLK_FLL,
  42. 0, SND_SOC_CLOCK_IN);
  43. if (ret < 0) {
  44. pr_err("%s -failed to set wm8904 SYSCLK\n", __func__);
  45. return ret;
  46. }
  47. return 0;
  48. }
  49. static struct snd_soc_ops atmel_asoc_wm8904_ops = {
  50. .hw_params = atmel_asoc_wm8904_hw_params,
  51. };
  52. static int atmel_set_bias_level(struct snd_soc_card *card,
  53. struct snd_soc_dapm_context *dapm,
  54. enum snd_soc_bias_level level)
  55. {
  56. if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
  57. switch (level) {
  58. case SND_SOC_BIAS_PREPARE:
  59. clk_prepare_enable(mclk);
  60. break;
  61. case SND_SOC_BIAS_OFF:
  62. clk_disable_unprepare(mclk);
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. return 0;
  69. };
  70. static struct snd_soc_dai_link atmel_asoc_wm8904_dailink = {
  71. .name = "WM8904",
  72. .stream_name = "WM8904 PCM",
  73. .codec_dai_name = "wm8904-hifi",
  74. .dai_fmt = SND_SOC_DAIFMT_I2S
  75. | SND_SOC_DAIFMT_NB_NF
  76. | SND_SOC_DAIFMT_CBM_CFM,
  77. .ops = &atmel_asoc_wm8904_ops,
  78. };
  79. static struct snd_soc_card atmel_asoc_wm8904_card = {
  80. .name = "atmel_asoc_wm8904",
  81. .owner = THIS_MODULE,
  82. .set_bias_level = atmel_set_bias_level,
  83. .dai_link = &atmel_asoc_wm8904_dailink,
  84. .num_links = 1,
  85. .dapm_widgets = atmel_asoc_wm8904_dapm_widgets,
  86. .num_dapm_widgets = ARRAY_SIZE(atmel_asoc_wm8904_dapm_widgets),
  87. .fully_routed = true,
  88. };
  89. static int atmel_asoc_wm8904_dt_init(struct platform_device *pdev)
  90. {
  91. struct device_node *np = pdev->dev.of_node;
  92. struct device_node *codec_np, *cpu_np;
  93. struct snd_soc_card *card = &atmel_asoc_wm8904_card;
  94. struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
  95. int ret;
  96. if (!np) {
  97. dev_err(&pdev->dev, "only device tree supported\n");
  98. return -EINVAL;
  99. }
  100. ret = snd_soc_of_parse_card_name(card, "atmel,model");
  101. if (ret) {
  102. dev_err(&pdev->dev, "failed to parse card name\n");
  103. return ret;
  104. }
  105. ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
  106. if (ret) {
  107. dev_err(&pdev->dev, "failed to parse audio routing\n");
  108. return ret;
  109. }
  110. cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
  111. if (!cpu_np) {
  112. dev_err(&pdev->dev, "failed to get dai and pcm info\n");
  113. ret = -EINVAL;
  114. return ret;
  115. }
  116. dailink->cpu_of_node = cpu_np;
  117. dailink->platform_of_node = cpu_np;
  118. of_node_put(cpu_np);
  119. codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
  120. if (!codec_np) {
  121. dev_err(&pdev->dev, "failed to get codec info\n");
  122. ret = -EINVAL;
  123. return ret;
  124. }
  125. dailink->codec_of_node = codec_np;
  126. of_node_put(codec_np);
  127. return 0;
  128. }
  129. static int atmel_asoc_wm8904_probe(struct platform_device *pdev)
  130. {
  131. struct snd_soc_card *card = &atmel_asoc_wm8904_card;
  132. struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
  133. struct clk *clk_src;
  134. int id, ret;
  135. card->dev = &pdev->dev;
  136. ret = atmel_asoc_wm8904_dt_init(pdev);
  137. if (ret) {
  138. dev_err(&pdev->dev, "failed to init dt info\n");
  139. return ret;
  140. }
  141. id = of_alias_get_id((struct device_node *)dailink->cpu_of_node, "ssc");
  142. ret = atmel_ssc_set_audio(id);
  143. if (ret != 0) {
  144. dev_err(&pdev->dev, "failed to set SSC %d for audio\n", id);
  145. return ret;
  146. }
  147. mclk = clk_get(NULL, "pck0");
  148. if (IS_ERR(mclk)) {
  149. dev_err(&pdev->dev, "failed to get pck0\n");
  150. ret = PTR_ERR(mclk);
  151. goto err_set_audio;
  152. }
  153. clk_src = clk_get(NULL, "clk32k");
  154. if (IS_ERR(clk_src)) {
  155. dev_err(&pdev->dev, "failed to get clk32k\n");
  156. ret = PTR_ERR(clk_src);
  157. goto err_set_audio;
  158. }
  159. ret = clk_set_parent(mclk, clk_src);
  160. clk_put(clk_src);
  161. if (ret != 0) {
  162. dev_err(&pdev->dev, "failed to set MCLK parent\n");
  163. goto err_set_audio;
  164. }
  165. dev_info(&pdev->dev, "setting pck0 to %dHz\n", MCLK_RATE);
  166. clk_set_rate(mclk, MCLK_RATE);
  167. ret = snd_soc_register_card(card);
  168. if (ret) {
  169. dev_err(&pdev->dev, "snd_soc_register_card failed\n");
  170. goto err_set_audio;
  171. }
  172. return 0;
  173. err_set_audio:
  174. atmel_ssc_put_audio(id);
  175. return ret;
  176. }
  177. static int atmel_asoc_wm8904_remove(struct platform_device *pdev)
  178. {
  179. struct snd_soc_card *card = platform_get_drvdata(pdev);
  180. struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
  181. int id;
  182. id = of_alias_get_id((struct device_node *)dailink->cpu_of_node, "ssc");
  183. snd_soc_unregister_card(card);
  184. atmel_ssc_put_audio(id);
  185. return 0;
  186. }
  187. #ifdef CONFIG_OF
  188. static const struct of_device_id atmel_asoc_wm8904_dt_ids[] = {
  189. { .compatible = "atmel,asoc-wm8904", },
  190. { }
  191. };
  192. #endif
  193. static struct platform_driver atmel_asoc_wm8904_driver = {
  194. .driver = {
  195. .name = "atmel-wm8904-audio",
  196. .owner = THIS_MODULE,
  197. .of_match_table = of_match_ptr(atmel_asoc_wm8904_dt_ids),
  198. },
  199. .probe = atmel_asoc_wm8904_probe,
  200. .remove = atmel_asoc_wm8904_remove,
  201. };
  202. module_platform_driver(atmel_asoc_wm8904_driver);
  203. /* Module information */
  204. MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
  205. MODULE_DESCRIPTION("ALSA SoC machine driver for Atmel EK with WM8904 codec");
  206. MODULE_LICENSE("GPL");