mt8173-max98090.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mt8173-max98090.c -- MT8173 MAX98090 ALSA SoC machine driver
  4. *
  5. * Copyright (c) 2015 MediaTek Inc.
  6. * Author: Koro Chen <koro.chen@mediatek.com>
  7. */
  8. #include <linux/module.h>
  9. #include <sound/soc.h>
  10. #include <sound/jack.h>
  11. #include <linux/gpio.h>
  12. #include "../../codecs/max98090.h"
  13. static struct snd_soc_jack mt8173_max98090_jack;
  14. static struct snd_soc_jack_pin mt8173_max98090_jack_pins[] = {
  15. {
  16. .pin = "Headphone",
  17. .mask = SND_JACK_HEADPHONE,
  18. },
  19. {
  20. .pin = "Headset Mic",
  21. .mask = SND_JACK_MICROPHONE,
  22. },
  23. };
  24. static const struct snd_soc_dapm_widget mt8173_max98090_widgets[] = {
  25. SND_SOC_DAPM_SPK("Speaker", NULL),
  26. SND_SOC_DAPM_MIC("Int Mic", NULL),
  27. SND_SOC_DAPM_HP("Headphone", NULL),
  28. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  29. };
  30. static const struct snd_soc_dapm_route mt8173_max98090_routes[] = {
  31. {"Speaker", NULL, "SPKL"},
  32. {"Speaker", NULL, "SPKR"},
  33. {"DMICL", NULL, "Int Mic"},
  34. {"Headphone", NULL, "HPL"},
  35. {"Headphone", NULL, "HPR"},
  36. {"Headset Mic", NULL, "MICBIAS"},
  37. {"IN34", NULL, "Headset Mic"},
  38. };
  39. static const struct snd_kcontrol_new mt8173_max98090_controls[] = {
  40. SOC_DAPM_PIN_SWITCH("Speaker"),
  41. SOC_DAPM_PIN_SWITCH("Int Mic"),
  42. SOC_DAPM_PIN_SWITCH("Headphone"),
  43. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  44. };
  45. static int mt8173_max98090_hw_params(struct snd_pcm_substream *substream,
  46. struct snd_pcm_hw_params *params)
  47. {
  48. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  49. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  50. return snd_soc_dai_set_sysclk(codec_dai, 0, params_rate(params) * 256,
  51. SND_SOC_CLOCK_IN);
  52. }
  53. static const struct snd_soc_ops mt8173_max98090_ops = {
  54. .hw_params = mt8173_max98090_hw_params,
  55. };
  56. static int mt8173_max98090_init(struct snd_soc_pcm_runtime *runtime)
  57. {
  58. int ret;
  59. struct snd_soc_card *card = runtime->card;
  60. struct snd_soc_component *component = runtime->codec_dai->component;
  61. /* enable jack detection */
  62. ret = snd_soc_card_jack_new(card, "Headphone", SND_JACK_HEADPHONE,
  63. &mt8173_max98090_jack,
  64. mt8173_max98090_jack_pins,
  65. ARRAY_SIZE(mt8173_max98090_jack_pins));
  66. if (ret) {
  67. dev_err(card->dev, "Can't create a new Jack %d\n", ret);
  68. return ret;
  69. }
  70. return max98090_mic_detect(component, &mt8173_max98090_jack);
  71. }
  72. /* Digital audio interface glue - connects codec <---> CPU */
  73. static struct snd_soc_dai_link mt8173_max98090_dais[] = {
  74. /* Front End DAI links */
  75. {
  76. .name = "MAX98090 Playback",
  77. .stream_name = "MAX98090 Playback",
  78. .cpu_dai_name = "DL1",
  79. .codec_name = "snd-soc-dummy",
  80. .codec_dai_name = "snd-soc-dummy-dai",
  81. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  82. .dynamic = 1,
  83. .dpcm_playback = 1,
  84. },
  85. {
  86. .name = "MAX98090 Capture",
  87. .stream_name = "MAX98090 Capture",
  88. .cpu_dai_name = "VUL",
  89. .codec_name = "snd-soc-dummy",
  90. .codec_dai_name = "snd-soc-dummy-dai",
  91. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  92. .dynamic = 1,
  93. .dpcm_capture = 1,
  94. },
  95. /* Back End DAI links */
  96. {
  97. .name = "Codec",
  98. .cpu_dai_name = "I2S",
  99. .no_pcm = 1,
  100. .codec_dai_name = "HiFi",
  101. .init = mt8173_max98090_init,
  102. .ops = &mt8173_max98090_ops,
  103. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  104. SND_SOC_DAIFMT_CBS_CFS,
  105. .dpcm_playback = 1,
  106. .dpcm_capture = 1,
  107. },
  108. };
  109. static struct snd_soc_card mt8173_max98090_card = {
  110. .name = "mt8173-max98090",
  111. .owner = THIS_MODULE,
  112. .dai_link = mt8173_max98090_dais,
  113. .num_links = ARRAY_SIZE(mt8173_max98090_dais),
  114. .controls = mt8173_max98090_controls,
  115. .num_controls = ARRAY_SIZE(mt8173_max98090_controls),
  116. .dapm_widgets = mt8173_max98090_widgets,
  117. .num_dapm_widgets = ARRAY_SIZE(mt8173_max98090_widgets),
  118. .dapm_routes = mt8173_max98090_routes,
  119. .num_dapm_routes = ARRAY_SIZE(mt8173_max98090_routes),
  120. };
  121. static int mt8173_max98090_dev_probe(struct platform_device *pdev)
  122. {
  123. struct snd_soc_card *card = &mt8173_max98090_card;
  124. struct device_node *codec_node, *platform_node;
  125. int ret, i;
  126. platform_node = of_parse_phandle(pdev->dev.of_node,
  127. "mediatek,platform", 0);
  128. if (!platform_node) {
  129. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  130. return -EINVAL;
  131. }
  132. for (i = 0; i < card->num_links; i++) {
  133. if (mt8173_max98090_dais[i].platform_name)
  134. continue;
  135. mt8173_max98090_dais[i].platform_of_node = platform_node;
  136. }
  137. codec_node = of_parse_phandle(pdev->dev.of_node,
  138. "mediatek,audio-codec", 0);
  139. if (!codec_node) {
  140. dev_err(&pdev->dev,
  141. "Property 'audio-codec' missing or invalid\n");
  142. return -EINVAL;
  143. }
  144. for (i = 0; i < card->num_links; i++) {
  145. if (mt8173_max98090_dais[i].codec_name)
  146. continue;
  147. mt8173_max98090_dais[i].codec_of_node = codec_node;
  148. }
  149. card->dev = &pdev->dev;
  150. ret = devm_snd_soc_register_card(&pdev->dev, card);
  151. if (ret)
  152. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  153. __func__, ret);
  154. return ret;
  155. }
  156. static const struct of_device_id mt8173_max98090_dt_match[] = {
  157. { .compatible = "mediatek,mt8173-max98090", },
  158. { }
  159. };
  160. MODULE_DEVICE_TABLE(of, mt8173_max98090_dt_match);
  161. static struct platform_driver mt8173_max98090_driver = {
  162. .driver = {
  163. .name = "mt8173-max98090",
  164. .of_match_table = mt8173_max98090_dt_match,
  165. #ifdef CONFIG_PM
  166. .pm = &snd_soc_pm_ops,
  167. #endif
  168. },
  169. .probe = mt8173_max98090_dev_probe,
  170. };
  171. module_platform_driver(mt8173_max98090_driver);
  172. /* Module information */
  173. MODULE_DESCRIPTION("MT8173 MAX98090 ALSA SoC machine driver");
  174. MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
  175. MODULE_LICENSE("GPL v2");
  176. MODULE_ALIAS("platform:mt8173-max98090");