tegra_wm9712.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "tegra_asoc_utils.h"
  30. #define DRV_NAME "tegra-snd-wm9712"
  31. struct tegra_wm9712 {
  32. struct platform_device *codec;
  33. struct tegra_asoc_utils_data util_data;
  34. };
  35. static const struct snd_soc_dapm_widget tegra_wm9712_dapm_widgets[] = {
  36. SND_SOC_DAPM_HP("Headphone", NULL),
  37. SND_SOC_DAPM_LINE("LineIn", NULL),
  38. SND_SOC_DAPM_MIC("Mic", NULL),
  39. };
  40. static int tegra_wm9712_init(struct snd_soc_pcm_runtime *rtd)
  41. {
  42. return snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "Mic Bias");
  43. }
  44. static struct snd_soc_dai_link tegra_wm9712_dai = {
  45. .name = "AC97 HiFi",
  46. .stream_name = "AC97 HiFi",
  47. .codec_dai_name = "wm9712-hifi",
  48. .codec_name = "wm9712-codec",
  49. .init = tegra_wm9712_init,
  50. };
  51. static struct snd_soc_card snd_soc_tegra_wm9712 = {
  52. .name = "tegra-wm9712",
  53. .owner = THIS_MODULE,
  54. .dai_link = &tegra_wm9712_dai,
  55. .num_links = 1,
  56. .dapm_widgets = tegra_wm9712_dapm_widgets,
  57. .num_dapm_widgets = ARRAY_SIZE(tegra_wm9712_dapm_widgets),
  58. .fully_routed = true,
  59. };
  60. static int tegra_wm9712_driver_probe(struct platform_device *pdev)
  61. {
  62. struct device_node *np = pdev->dev.of_node;
  63. struct snd_soc_card *card = &snd_soc_tegra_wm9712;
  64. struct tegra_wm9712 *machine;
  65. int ret;
  66. machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm9712),
  67. GFP_KERNEL);
  68. if (!machine) {
  69. dev_err(&pdev->dev, "Can't allocate tegra_wm9712 struct\n");
  70. return -ENOMEM;
  71. }
  72. card->dev = &pdev->dev;
  73. platform_set_drvdata(pdev, card);
  74. snd_soc_card_set_drvdata(card, machine);
  75. machine->codec = platform_device_alloc("wm9712-codec", -1);
  76. if (!machine->codec) {
  77. dev_err(&pdev->dev, "Can't allocate wm9712 platform device\n");
  78. return -ENOMEM;
  79. }
  80. ret = platform_device_add(machine->codec);
  81. if (ret)
  82. goto codec_put;
  83. ret = snd_soc_of_parse_card_name(card, "nvidia,model");
  84. if (ret)
  85. goto codec_unregister;
  86. ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
  87. if (ret)
  88. goto codec_unregister;
  89. tegra_wm9712_dai.cpu_of_node = of_parse_phandle(np,
  90. "nvidia,ac97-controller", 0);
  91. if (!tegra_wm9712_dai.cpu_of_node) {
  92. dev_err(&pdev->dev,
  93. "Property 'nvidia,ac97-controller' missing or invalid\n");
  94. ret = -EINVAL;
  95. goto codec_unregister;
  96. }
  97. tegra_wm9712_dai.platform_of_node = tegra_wm9712_dai.cpu_of_node;
  98. ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
  99. if (ret)
  100. goto codec_unregister;
  101. ret = tegra_asoc_utils_set_ac97_rate(&machine->util_data);
  102. if (ret)
  103. goto asoc_utils_fini;
  104. ret = snd_soc_register_card(card);
  105. if (ret) {
  106. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
  107. ret);
  108. goto asoc_utils_fini;
  109. }
  110. return 0;
  111. asoc_utils_fini:
  112. tegra_asoc_utils_fini(&machine->util_data);
  113. codec_unregister:
  114. platform_device_del(machine->codec);
  115. codec_put:
  116. platform_device_put(machine->codec);
  117. return ret;
  118. }
  119. static int tegra_wm9712_driver_remove(struct platform_device *pdev)
  120. {
  121. struct snd_soc_card *card = platform_get_drvdata(pdev);
  122. struct tegra_wm9712 *machine = snd_soc_card_get_drvdata(card);
  123. snd_soc_unregister_card(card);
  124. tegra_asoc_utils_fini(&machine->util_data);
  125. platform_device_unregister(machine->codec);
  126. return 0;
  127. }
  128. static const struct of_device_id tegra_wm9712_of_match[] = {
  129. { .compatible = "nvidia,tegra-audio-wm9712", },
  130. {},
  131. };
  132. static struct platform_driver tegra_wm9712_driver = {
  133. .driver = {
  134. .name = DRV_NAME,
  135. .pm = &snd_soc_pm_ops,
  136. .of_match_table = tegra_wm9712_of_match,
  137. },
  138. .probe = tegra_wm9712_driver_probe,
  139. .remove = tegra_wm9712_driver_remove,
  140. };
  141. module_platform_driver(tegra_wm9712_driver);
  142. MODULE_AUTHOR("Lucas Stach");
  143. MODULE_DESCRIPTION("Tegra+WM9712 machine ASoC driver");
  144. MODULE_LICENSE("GPL v2");
  145. MODULE_ALIAS("platform:" DRV_NAME);
  146. MODULE_DEVICE_TABLE(of, tegra_wm9712_of_match);