tegra_wm8903.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
  4. *
  5. * Author: Stephen Warren <[email protected]>
  6. * Copyright (C) 2010-2012 - NVIDIA, Inc.
  7. *
  8. * Based on code copyright/by:
  9. *
  10. * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
  11. *
  12. * Copyright 2007 Wolfson Microelectronics PLC.
  13. * Author: Graeme Gregory
  14. * [email protected] or [email protected]
  15. */
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/of.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <sound/core.h>
  21. #include <sound/jack.h>
  22. #include <sound/soc.h>
  23. #include "../codecs/wm8903.h"
  24. #include "tegra_asoc_machine.h"
  25. static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
  26. { .pin = "Mic Jack", .mask = SND_JACK_MICROPHONE },
  27. };
  28. static unsigned int tegra_wm8903_mclk_rate(unsigned int srate)
  29. {
  30. unsigned int mclk;
  31. switch (srate) {
  32. case 64000:
  33. case 88200:
  34. case 96000:
  35. mclk = 128 * srate;
  36. break;
  37. default:
  38. mclk = 256 * srate;
  39. break;
  40. }
  41. /* FIXME: Codec only requires >= 3MHz if OSR==0 */
  42. while (mclk < 6000000)
  43. mclk *= 2;
  44. return mclk;
  45. }
  46. static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
  47. {
  48. struct tegra_machine *machine = snd_soc_card_get_drvdata(rtd->card);
  49. struct snd_soc_card *card = rtd->card;
  50. int err;
  51. /*
  52. * Older version of machine driver was ignoring GPIO polarity,
  53. * forcing it to active-low. This means that all older device-trees
  54. * which set the polarity to active-high are wrong and we need to fix
  55. * them up.
  56. */
  57. if (machine->asoc->hp_jack_gpio_active_low) {
  58. bool active_low = gpiod_is_active_low(machine->gpiod_hp_det);
  59. machine->hp_jack_gpio->invert = !active_low;
  60. }
  61. err = tegra_asoc_machine_init(rtd);
  62. if (err)
  63. return err;
  64. if (!machine->gpiod_mic_det && machine->asoc->add_mic_jack) {
  65. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  66. struct snd_soc_component *component = codec_dai->component;
  67. int shrt = 0;
  68. err = snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
  69. SND_JACK_MICROPHONE,
  70. machine->mic_jack,
  71. tegra_wm8903_mic_jack_pins,
  72. ARRAY_SIZE(tegra_wm8903_mic_jack_pins));
  73. if (err) {
  74. dev_err(rtd->dev, "Mic Jack creation failed: %d\n", err);
  75. return err;
  76. }
  77. if (of_property_read_bool(card->dev->of_node, "nvidia,headset"))
  78. shrt = SND_JACK_MICROPHONE;
  79. wm8903_mic_detect(component, machine->mic_jack,
  80. SND_JACK_MICROPHONE, shrt);
  81. }
  82. snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS");
  83. return 0;
  84. }
  85. static int tegra_wm8903_remove(struct snd_soc_card *card)
  86. {
  87. struct snd_soc_dai_link *link = &card->dai_link[0];
  88. struct snd_soc_pcm_runtime *rtd = snd_soc_get_pcm_runtime(card, link);
  89. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  90. struct snd_soc_component *component = codec_dai->component;
  91. wm8903_mic_detect(component, NULL, 0, 0);
  92. return 0;
  93. }
  94. SND_SOC_DAILINK_DEFS(hifi,
  95. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  96. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8903-hifi")),
  97. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  98. static struct snd_soc_dai_link tegra_wm8903_dai = {
  99. .name = "WM8903",
  100. .stream_name = "WM8903 PCM",
  101. .init = tegra_wm8903_init,
  102. .dai_fmt = SND_SOC_DAIFMT_I2S |
  103. SND_SOC_DAIFMT_NB_NF |
  104. SND_SOC_DAIFMT_CBS_CFS,
  105. SND_SOC_DAILINK_REG(hifi),
  106. };
  107. static struct snd_soc_card snd_soc_tegra_wm8903 = {
  108. .components = "codec:wm8903",
  109. .owner = THIS_MODULE,
  110. .dai_link = &tegra_wm8903_dai,
  111. .num_links = 1,
  112. .remove = tegra_wm8903_remove,
  113. .fully_routed = true,
  114. };
  115. /* older device-trees used wrong polarity for the headphones-detection GPIO */
  116. static const struct tegra_asoc_data tegra_wm8903_data_legacy = {
  117. .mclk_rate = tegra_wm8903_mclk_rate,
  118. .card = &snd_soc_tegra_wm8903,
  119. .hp_jack_gpio_active_low = true,
  120. .add_common_dapm_widgets = true,
  121. .add_common_controls = true,
  122. .add_common_snd_ops = true,
  123. .add_mic_jack = true,
  124. .add_hp_jack = true,
  125. };
  126. static const struct tegra_asoc_data tegra_wm8903_data = {
  127. .mclk_rate = tegra_wm8903_mclk_rate,
  128. .card = &snd_soc_tegra_wm8903,
  129. .add_common_dapm_widgets = true,
  130. .add_common_controls = true,
  131. .add_common_snd_ops = true,
  132. .add_mic_jack = true,
  133. .add_hp_jack = true,
  134. };
  135. static const struct of_device_id tegra_wm8903_of_match[] = {
  136. { .compatible = "ad,tegra-audio-plutux", .data = &tegra_wm8903_data_legacy },
  137. { .compatible = "ad,tegra-audio-wm8903-medcom-wide", .data = &tegra_wm8903_data_legacy },
  138. { .compatible = "ad,tegra-audio-wm8903-tec", .data = &tegra_wm8903_data_legacy },
  139. { .compatible = "nvidia,tegra-audio-wm8903-cardhu", .data = &tegra_wm8903_data_legacy },
  140. { .compatible = "nvidia,tegra-audio-wm8903-harmony", .data = &tegra_wm8903_data_legacy },
  141. { .compatible = "nvidia,tegra-audio-wm8903-picasso", .data = &tegra_wm8903_data_legacy },
  142. { .compatible = "nvidia,tegra-audio-wm8903-seaboard", .data = &tegra_wm8903_data_legacy },
  143. { .compatible = "nvidia,tegra-audio-wm8903-ventana", .data = &tegra_wm8903_data_legacy },
  144. { .compatible = "nvidia,tegra-audio-wm8903", .data = &tegra_wm8903_data },
  145. {},
  146. };
  147. MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);
  148. static struct platform_driver tegra_wm8903_driver = {
  149. .driver = {
  150. .name = "tegra-wm8903",
  151. .of_match_table = tegra_wm8903_of_match,
  152. .pm = &snd_soc_pm_ops,
  153. },
  154. .probe = tegra_asoc_machine_probe,
  155. };
  156. module_platform_driver(tegra_wm8903_driver);
  157. MODULE_AUTHOR("Stephen Warren <[email protected]>");
  158. MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
  159. MODULE_LICENSE("GPL");