atmel_wm8904.c 6.0 KB

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