mt8173-max98090.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <[email protected]>
  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 = asoc_substream_to_rtd(substream);
  49. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  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 = asoc_rtd_to_codec(runtime, 0)->component;
  61. /* enable jack detection */
  62. ret = snd_soc_card_jack_new_pins(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. SND_SOC_DAILINK_DEFS(playback,
  73. DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
  74. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  75. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  76. SND_SOC_DAILINK_DEFS(capture,
  77. DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
  78. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  79. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  80. SND_SOC_DAILINK_DEFS(hifi,
  81. DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
  82. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
  83. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  84. /* Digital audio interface glue - connects codec <---> CPU */
  85. static struct snd_soc_dai_link mt8173_max98090_dais[] = {
  86. /* Front End DAI links */
  87. {
  88. .name = "MAX98090 Playback",
  89. .stream_name = "MAX98090 Playback",
  90. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  91. .dynamic = 1,
  92. .dpcm_playback = 1,
  93. SND_SOC_DAILINK_REG(playback),
  94. },
  95. {
  96. .name = "MAX98090 Capture",
  97. .stream_name = "MAX98090 Capture",
  98. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  99. .dynamic = 1,
  100. .dpcm_capture = 1,
  101. SND_SOC_DAILINK_REG(capture),
  102. },
  103. /* Back End DAI links */
  104. {
  105. .name = "Codec",
  106. .no_pcm = 1,
  107. .init = mt8173_max98090_init,
  108. .ops = &mt8173_max98090_ops,
  109. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  110. SND_SOC_DAIFMT_CBS_CFS,
  111. .dpcm_playback = 1,
  112. .dpcm_capture = 1,
  113. SND_SOC_DAILINK_REG(hifi),
  114. },
  115. };
  116. static struct snd_soc_card mt8173_max98090_card = {
  117. .name = "mt8173-max98090",
  118. .owner = THIS_MODULE,
  119. .dai_link = mt8173_max98090_dais,
  120. .num_links = ARRAY_SIZE(mt8173_max98090_dais),
  121. .controls = mt8173_max98090_controls,
  122. .num_controls = ARRAY_SIZE(mt8173_max98090_controls),
  123. .dapm_widgets = mt8173_max98090_widgets,
  124. .num_dapm_widgets = ARRAY_SIZE(mt8173_max98090_widgets),
  125. .dapm_routes = mt8173_max98090_routes,
  126. .num_dapm_routes = ARRAY_SIZE(mt8173_max98090_routes),
  127. };
  128. static int mt8173_max98090_dev_probe(struct platform_device *pdev)
  129. {
  130. struct snd_soc_card *card = &mt8173_max98090_card;
  131. struct device_node *codec_node, *platform_node;
  132. struct snd_soc_dai_link *dai_link;
  133. int ret, i;
  134. platform_node = of_parse_phandle(pdev->dev.of_node,
  135. "mediatek,platform", 0);
  136. if (!platform_node) {
  137. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  138. return -EINVAL;
  139. }
  140. for_each_card_prelinks(card, i, dai_link) {
  141. if (dai_link->platforms->name)
  142. continue;
  143. dai_link->platforms->of_node = platform_node;
  144. }
  145. codec_node = of_parse_phandle(pdev->dev.of_node,
  146. "mediatek,audio-codec", 0);
  147. if (!codec_node) {
  148. dev_err(&pdev->dev,
  149. "Property 'audio-codec' missing or invalid\n");
  150. ret = -EINVAL;
  151. goto put_platform_node;
  152. }
  153. for_each_card_prelinks(card, i, dai_link) {
  154. if (dai_link->codecs->name)
  155. continue;
  156. dai_link->codecs->of_node = codec_node;
  157. }
  158. card->dev = &pdev->dev;
  159. ret = devm_snd_soc_register_card(&pdev->dev, card);
  160. of_node_put(codec_node);
  161. put_platform_node:
  162. of_node_put(platform_node);
  163. return ret;
  164. }
  165. static const struct of_device_id mt8173_max98090_dt_match[] = {
  166. { .compatible = "mediatek,mt8173-max98090", },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(of, mt8173_max98090_dt_match);
  170. static struct platform_driver mt8173_max98090_driver = {
  171. .driver = {
  172. .name = "mt8173-max98090",
  173. .of_match_table = mt8173_max98090_dt_match,
  174. .pm = &snd_soc_pm_ops,
  175. },
  176. .probe = mt8173_max98090_dev_probe,
  177. };
  178. module_platform_driver(mt8173_max98090_driver);
  179. /* Module information */
  180. MODULE_DESCRIPTION("MT8173 MAX98090 ALSA SoC machine driver");
  181. MODULE_AUTHOR("Koro Chen <[email protected]>");
  182. MODULE_LICENSE("GPL v2");
  183. MODULE_ALIAS("platform:mt8173-max98090");