Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * mt2701-wm8960.c  --  MT2701 WM8960 ALSA SoC machine driver
0004  *
0005  * Copyright (c) 2017 MediaTek Inc.
0006  * Author: Ryder Lee <ryder.lee@mediatek.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <sound/soc.h>
0011 
0012 #include "mt2701-afe-common.h"
0013 
0014 static const struct snd_soc_dapm_widget mt2701_wm8960_widgets[] = {
0015     SND_SOC_DAPM_HP("Headphone", NULL),
0016     SND_SOC_DAPM_MIC("AMIC", NULL),
0017 };
0018 
0019 static const struct snd_kcontrol_new mt2701_wm8960_controls[] = {
0020     SOC_DAPM_PIN_SWITCH("Headphone"),
0021     SOC_DAPM_PIN_SWITCH("AMIC"),
0022 };
0023 
0024 static int mt2701_wm8960_be_ops_hw_params(struct snd_pcm_substream *substream,
0025                       struct snd_pcm_hw_params *params)
0026 {
0027     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0028     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0029     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0030     unsigned int mclk_rate;
0031     unsigned int rate = params_rate(params);
0032     unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;
0033     unsigned int div_bck_over_lrck = 64;
0034 
0035     mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
0036 
0037     snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
0038     snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
0039 
0040     return 0;
0041 }
0042 
0043 static const struct snd_soc_ops mt2701_wm8960_be_ops = {
0044     .hw_params = mt2701_wm8960_be_ops_hw_params
0045 };
0046 
0047 SND_SOC_DAILINK_DEFS(playback,
0048     DAILINK_COMP_ARRAY(COMP_CPU("PCMO0")),
0049     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0050     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0051 
0052 SND_SOC_DAILINK_DEFS(capture,
0053     DAILINK_COMP_ARRAY(COMP_CPU("PCM0")),
0054     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0055     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0056 
0057 SND_SOC_DAILINK_DEFS(codec,
0058     DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
0059     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8960-hifi")),
0060     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0061 
0062 static struct snd_soc_dai_link mt2701_wm8960_dai_links[] = {
0063     /* FE */
0064     {
0065         .name = "wm8960-playback",
0066         .stream_name = "wm8960-playback",
0067         .trigger = {SND_SOC_DPCM_TRIGGER_POST,
0068                 SND_SOC_DPCM_TRIGGER_POST},
0069         .dynamic = 1,
0070         .dpcm_playback = 1,
0071         SND_SOC_DAILINK_REG(playback),
0072     },
0073     {
0074         .name = "wm8960-capture",
0075         .stream_name = "wm8960-capture",
0076         .trigger = {SND_SOC_DPCM_TRIGGER_POST,
0077                 SND_SOC_DPCM_TRIGGER_POST},
0078         .dynamic = 1,
0079         .dpcm_capture = 1,
0080         SND_SOC_DAILINK_REG(capture),
0081     },
0082     /* BE */
0083     {
0084         .name = "wm8960-codec",
0085         .no_pcm = 1,
0086         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
0087             | SND_SOC_DAIFMT_GATED,
0088         .ops = &mt2701_wm8960_be_ops,
0089         .dpcm_playback = 1,
0090         .dpcm_capture = 1,
0091         SND_SOC_DAILINK_REG(codec),
0092     },
0093 };
0094 
0095 static struct snd_soc_card mt2701_wm8960_card = {
0096     .name = "mt2701-wm8960",
0097     .owner = THIS_MODULE,
0098     .dai_link = mt2701_wm8960_dai_links,
0099     .num_links = ARRAY_SIZE(mt2701_wm8960_dai_links),
0100     .controls = mt2701_wm8960_controls,
0101     .num_controls = ARRAY_SIZE(mt2701_wm8960_controls),
0102     .dapm_widgets = mt2701_wm8960_widgets,
0103     .num_dapm_widgets = ARRAY_SIZE(mt2701_wm8960_widgets),
0104 };
0105 
0106 static int mt2701_wm8960_machine_probe(struct platform_device *pdev)
0107 {
0108     struct snd_soc_card *card = &mt2701_wm8960_card;
0109     struct device_node *platform_node, *codec_node;
0110     struct snd_soc_dai_link *dai_link;
0111     int ret, i;
0112 
0113     platform_node = of_parse_phandle(pdev->dev.of_node,
0114                      "mediatek,platform", 0);
0115     if (!platform_node) {
0116         dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
0117         return -EINVAL;
0118     }
0119     for_each_card_prelinks(card, i, dai_link) {
0120         if (dai_link->platforms->name)
0121             continue;
0122         dai_link->platforms->of_node = platform_node;
0123     }
0124 
0125     card->dev = &pdev->dev;
0126 
0127     codec_node = of_parse_phandle(pdev->dev.of_node,
0128                       "mediatek,audio-codec", 0);
0129     if (!codec_node) {
0130         dev_err(&pdev->dev,
0131             "Property 'audio-codec' missing or invalid\n");
0132         ret = -EINVAL;
0133         goto put_platform_node;
0134     }
0135     for_each_card_prelinks(card, i, dai_link) {
0136         if (dai_link->codecs->name)
0137             continue;
0138         dai_link->codecs->of_node = codec_node;
0139     }
0140 
0141     ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
0142     if (ret) {
0143         dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
0144         goto put_codec_node;
0145     }
0146 
0147     ret = devm_snd_soc_register_card(&pdev->dev, card);
0148     if (ret)
0149         dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
0150             __func__, ret);
0151 
0152 put_codec_node:
0153     of_node_put(codec_node);
0154 put_platform_node:
0155     of_node_put(platform_node);
0156     return ret;
0157 }
0158 
0159 #ifdef CONFIG_OF
0160 static const struct of_device_id mt2701_wm8960_machine_dt_match[] = {
0161     {.compatible = "mediatek,mt2701-wm8960-machine",},
0162     {}
0163 };
0164 #endif
0165 
0166 static struct platform_driver mt2701_wm8960_machine = {
0167     .driver = {
0168         .name = "mt2701-wm8960",
0169 #ifdef CONFIG_OF
0170         .of_match_table = mt2701_wm8960_machine_dt_match,
0171 #endif
0172     },
0173     .probe = mt2701_wm8960_machine_probe,
0174 };
0175 
0176 module_platform_driver(mt2701_wm8960_machine);
0177 
0178 /* Module information */
0179 MODULE_DESCRIPTION("MT2701 WM8960 ALSA SoC machine driver");
0180 MODULE_AUTHOR("Ryder Lee <ryder.lee@mediatek.com>");
0181 MODULE_LICENSE("GPL v2");
0182 MODULE_ALIAS("mt2701 wm8960 soc card");
0183