tegra_wm9712.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * tegra20_wm9712.c - Tegra machine ASoC driver for boards using WM9712 codec.
  3. *
  4. * Copyright 2012 Lucas Stach <dev@lynxeye.de>
  5. *
  6. * Partly based on code copyright/by:
  7. * Copyright 2011,2012 Toradex Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/gpio.h>
  23. #include <linux/of_gpio.h>
  24. #include <sound/core.h>
  25. #include <sound/jack.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #define DRV_NAME "tegra-snd-wm9712"
  30. struct tegra_wm9712 {
  31. struct platform_device *codec;
  32. };
  33. static const struct snd_soc_dapm_widget tegra_wm9712_dapm_widgets[] = {
  34. SND_SOC_DAPM_HP("Headphone", NULL),
  35. SND_SOC_DAPM_LINE("LineIn", NULL),
  36. SND_SOC_DAPM_MIC("Mic", NULL),
  37. };
  38. static int tegra_wm9712_init(struct snd_soc_pcm_runtime *rtd)
  39. {
  40. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  41. struct snd_soc_codec *codec = codec_dai->codec;
  42. struct snd_soc_dapm_context *dapm = &codec->dapm;
  43. snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
  44. return snd_soc_dapm_sync(dapm);
  45. }
  46. static struct snd_soc_dai_link tegra_wm9712_dai = {
  47. .name = "AC97 HiFi",
  48. .stream_name = "AC97 HiFi",
  49. .cpu_dai_name = "tegra-ac97-pcm",
  50. .codec_dai_name = "wm9712-hifi",
  51. .codec_name = "wm9712-codec",
  52. .init = tegra_wm9712_init,
  53. };
  54. static struct snd_soc_card snd_soc_tegra_wm9712 = {
  55. .name = "tegra-wm9712",
  56. .owner = THIS_MODULE,
  57. .dai_link = &tegra_wm9712_dai,
  58. .num_links = 1,
  59. .dapm_widgets = tegra_wm9712_dapm_widgets,
  60. .num_dapm_widgets = ARRAY_SIZE(tegra_wm9712_dapm_widgets),
  61. .fully_routed = true,
  62. };
  63. static int tegra_wm9712_driver_probe(struct platform_device *pdev)
  64. {
  65. struct device_node *np = pdev->dev.of_node;
  66. struct snd_soc_card *card = &snd_soc_tegra_wm9712;
  67. struct tegra_wm9712 *machine;
  68. int ret;
  69. if (!pdev->dev.of_node) {
  70. dev_err(&pdev->dev, "No platform data supplied\n");
  71. return -EINVAL;
  72. }
  73. machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm9712),
  74. GFP_KERNEL);
  75. if (!machine) {
  76. dev_err(&pdev->dev, "Can't allocate tegra_wm9712 struct\n");
  77. return -ENOMEM;
  78. }
  79. card->dev = &pdev->dev;
  80. platform_set_drvdata(pdev, card);
  81. snd_soc_card_set_drvdata(card, machine);
  82. machine->codec = platform_device_alloc("wm9712-codec", -1);
  83. if (!machine->codec) {
  84. dev_err(&pdev->dev, "Can't allocate wm9712 platform device\n");
  85. return -ENOMEM;
  86. }
  87. ret = platform_device_add(machine->codec);
  88. if (ret)
  89. goto codec_put;
  90. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  91. if (ret)
  92. goto codec_unregister;
  93. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  94. if (ret)
  95. goto codec_unregister;
  96. tegra_wm9712_dai.cpu_of_node = of_parse_phandle(np,
  97. "nvidia,ac97-controller", 0);
  98. if (!tegra_wm9712_dai.cpu_of_node) {
  99. dev_err(&pdev->dev,
  100. "Property 'nvidia,ac97-controller' missing or invalid\n");
  101. ret = -EINVAL;
  102. goto codec_unregister;
  103. }
  104. tegra_wm9712_dai.platform_of_node = tegra_wm9712_dai.cpu_of_node;
  105. ret = snd_soc_register_card(card);
  106. if (ret) {
  107. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  108. ret);
  109. goto codec_unregister;
  110. }
  111. return 0;
  112. codec_unregister:
  113. platform_device_del(machine->codec);
  114. codec_put:
  115. platform_device_put(machine->codec);
  116. return ret;
  117. }
  118. static int tegra_wm9712_driver_remove(struct platform_device *pdev)
  119. {
  120. struct snd_soc_card *card = platform_get_drvdata(pdev);
  121. struct tegra_wm9712 *machine = snd_soc_card_get_drvdata(card);
  122. snd_soc_unregister_card(card);
  123. platform_device_unregister(machine->codec);
  124. return 0;
  125. }
  126. static const struct of_device_id tegra_wm9712_of_match[] = {
  127. { .compatible = "nvidia,tegra-audio-wm9712", },
  128. {},
  129. };
  130. static struct platform_driver tegra_wm9712_driver = {
  131. .driver = {
  132. .name = DRV_NAME,
  133. .owner = THIS_MODULE,
  134. .pm = &snd_soc_pm_ops,
  135. .of_match_table = tegra_wm9712_of_match,
  136. },
  137. .probe = tegra_wm9712_driver_probe,
  138. .remove = tegra_wm9712_driver_remove,
  139. };
  140. module_platform_driver(tegra_wm9712_driver);
  141. MODULE_AUTHOR("Lucas Stach");
  142. MODULE_DESCRIPTION("Tegra+WM9712 machine ASoC driver");
  143. MODULE_LICENSE("GPL v2");
  144. MODULE_ALIAS("platform:" DRV_NAME);
  145. MODULE_DEVICE_TABLE(of, tegra_wm9712_of_match);