sam9x5_wm8731.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * sam9x5_wm8731 -- SoC audio for AT91SAM9X5-based boards
  3. * that are using WM8731 as codec.
  4. *
  5. * Copyright (C) 2011 Atmel,
  6. * Nicolas Ferre <nicolas.ferre@atmel.com>
  7. *
  8. * Copyright (C) 2013 Paratronic,
  9. * Richard Genoud <richard.genoud@gmail.com>
  10. *
  11. * Based on sam9g20_wm8731.c by:
  12. * Sedji Gaouaou <sedji.gaouaou@atmel.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. *
  19. */
  20. #include <linux/of.h>
  21. #include <linux/export.h>
  22. #include <linux/module.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/device.h>
  26. #include <sound/soc.h>
  27. #include <sound/soc-dai.h>
  28. #include <sound/soc-dapm.h>
  29. #include "../codecs/wm8731.h"
  30. #include "atmel_ssc_dai.h"
  31. #define MCLK_RATE 12288000
  32. #define DRV_NAME "sam9x5-snd-wm8731"
  33. struct sam9x5_drvdata {
  34. int ssc_id;
  35. };
  36. /*
  37. * Logic for a wm8731 as connected on a at91sam9x5ek based board.
  38. */
  39. static int sam9x5_wm8731_init(struct snd_soc_pcm_runtime *rtd)
  40. {
  41. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  42. struct device *dev = rtd->dev;
  43. int ret;
  44. dev_dbg(dev, "ASoC: %s called\n", __func__);
  45. /* set the codec system clock for DAC and ADC */
  46. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL,
  47. MCLK_RATE, SND_SOC_CLOCK_IN);
  48. if (ret < 0) {
  49. dev_err(dev, "ASoC: Failed to set WM8731 SYSCLK: %d\n", ret);
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. /*
  55. * Audio paths on at91sam9x5ek board:
  56. *
  57. * |A| ------------> | | ---R----> Headphone Jack
  58. * |T| <----\ | WM | ---L--/
  59. * |9| ---> CLK <--> | 8731 | <--R----- Line In Jack
  60. * |1| <------------ | | <--L--/
  61. */
  62. static const struct snd_soc_dapm_widget sam9x5_dapm_widgets[] = {
  63. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  64. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  65. };
  66. static int sam9x5_wm8731_driver_probe(struct platform_device *pdev)
  67. {
  68. struct device_node *np = pdev->dev.of_node;
  69. struct device_node *codec_np, *cpu_np;
  70. struct snd_soc_card *card;
  71. struct snd_soc_dai_link *dai;
  72. struct sam9x5_drvdata *priv;
  73. int ret;
  74. if (!np) {
  75. dev_err(&pdev->dev, "No device node supplied\n");
  76. return -EINVAL;
  77. }
  78. card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
  79. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  80. dai = devm_kzalloc(&pdev->dev, sizeof(*dai), GFP_KERNEL);
  81. if (!dai || !card || !priv) {
  82. ret = -ENOMEM;
  83. goto out;
  84. }
  85. snd_soc_card_set_drvdata(card, priv);
  86. card->dev = &pdev->dev;
  87. card->owner = THIS_MODULE;
  88. card->dai_link = dai;
  89. card->num_links = 1;
  90. card->dapm_widgets = sam9x5_dapm_widgets;
  91. card->num_dapm_widgets = ARRAY_SIZE(sam9x5_dapm_widgets);
  92. dai->name = "WM8731";
  93. dai->stream_name = "WM8731 PCM";
  94. dai->codec_dai_name = "wm8731-hifi";
  95. dai->init = sam9x5_wm8731_init;
  96. dai->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF
  97. | SND_SOC_DAIFMT_CBM_CFM;
  98. ret = snd_soc_of_parse_card_name(card, "atmel,model");
  99. if (ret) {
  100. dev_err(&pdev->dev, "atmel,model node missing\n");
  101. goto out;
  102. }
  103. ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
  104. if (ret) {
  105. dev_err(&pdev->dev, "atmel,audio-routing node missing\n");
  106. goto out;
  107. }
  108. codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
  109. if (!codec_np) {
  110. dev_err(&pdev->dev, "atmel,audio-codec node missing\n");
  111. ret = -EINVAL;
  112. goto out;
  113. }
  114. dai->codec_of_node = codec_np;
  115. cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
  116. if (!cpu_np) {
  117. dev_err(&pdev->dev, "atmel,ssc-controller node missing\n");
  118. ret = -EINVAL;
  119. goto out;
  120. }
  121. dai->cpu_of_node = cpu_np;
  122. dai->platform_of_node = cpu_np;
  123. priv->ssc_id = of_alias_get_id(cpu_np, "ssc");
  124. ret = atmel_ssc_set_audio(priv->ssc_id);
  125. if (ret != 0) {
  126. dev_err(&pdev->dev,
  127. "ASoC: Failed to set SSC %d for audio: %d\n",
  128. ret, priv->ssc_id);
  129. goto out;
  130. }
  131. of_node_put(codec_np);
  132. of_node_put(cpu_np);
  133. ret = snd_soc_register_card(card);
  134. if (ret) {
  135. dev_err(&pdev->dev,
  136. "ASoC: Platform device allocation failed\n");
  137. goto out_put_audio;
  138. }
  139. dev_dbg(&pdev->dev, "ASoC: %s ok\n", __func__);
  140. return ret;
  141. out_put_audio:
  142. atmel_ssc_put_audio(priv->ssc_id);
  143. out:
  144. return ret;
  145. }
  146. static int sam9x5_wm8731_driver_remove(struct platform_device *pdev)
  147. {
  148. struct snd_soc_card *card = platform_get_drvdata(pdev);
  149. struct sam9x5_drvdata *priv = card->drvdata;
  150. snd_soc_unregister_card(card);
  151. atmel_ssc_put_audio(priv->ssc_id);
  152. return 0;
  153. }
  154. static const struct of_device_id sam9x5_wm8731_of_match[] = {
  155. { .compatible = "atmel,sam9x5-wm8731-audio", },
  156. {},
  157. };
  158. MODULE_DEVICE_TABLE(of, sam9x5_wm8731_of_match);
  159. static struct platform_driver sam9x5_wm8731_driver = {
  160. .driver = {
  161. .name = DRV_NAME,
  162. .of_match_table = of_match_ptr(sam9x5_wm8731_of_match),
  163. },
  164. .probe = sam9x5_wm8731_driver_probe,
  165. .remove = sam9x5_wm8731_driver_remove,
  166. };
  167. module_platform_driver(sam9x5_wm8731_driver);
  168. /* Module information */
  169. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  170. MODULE_AUTHOR("Richard Genoud <richard.genoud@gmail.com>");
  171. MODULE_DESCRIPTION("ALSA SoC machine driver for AT91SAM9x5 - WM8731");
  172. MODULE_LICENSE("GPL");
  173. MODULE_ALIAS("platform:" DRV_NAME);