1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * mt8173-rt5650-rt5514.c -- MT8173 machine driver with RT5650/5514 codecs 4 * 5 * Copyright (c) 2016 MediaTek Inc. 6 * Author: Koro Chen <koro.chen@mediatek.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/gpio.h> 11 #include <linux/of_gpio.h> 12 #include <sound/soc.h> 13 #include <sound/jack.h> 14 #include "../../codecs/rt5645.h" 15 16 #define MCLK_FOR_CODECS 12288000 17 18 static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_widgets[] = { 19 SND_SOC_DAPM_SPK("Speaker", NULL), 20 SND_SOC_DAPM_MIC("Int Mic", NULL), 21 SND_SOC_DAPM_HP("Headphone", NULL), 22 SND_SOC_DAPM_MIC("Headset Mic", NULL), 23 }; 24 25 static const struct snd_soc_dapm_route mt8173_rt5650_rt5514_routes[] = { 26 {"Speaker", NULL, "SPOL"}, 27 {"Speaker", NULL, "SPOR"}, 28 {"Sub DMIC1L", NULL, "Int Mic"}, 29 {"Sub DMIC1R", NULL, "Int Mic"}, 30 {"Headphone", NULL, "HPOL"}, 31 {"Headphone", NULL, "HPOR"}, 32 {"IN1P", NULL, "Headset Mic"}, 33 {"IN1N", NULL, "Headset Mic"}, 34 }; 35 36 static const struct snd_kcontrol_new mt8173_rt5650_rt5514_controls[] = { 37 SOC_DAPM_PIN_SWITCH("Speaker"), 38 SOC_DAPM_PIN_SWITCH("Int Mic"), 39 SOC_DAPM_PIN_SWITCH("Headphone"), 40 SOC_DAPM_PIN_SWITCH("Headset Mic"), 41 }; 42 43 static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream *substream, 44 struct snd_pcm_hw_params *params) 45 { 46 struct snd_soc_pcm_runtime *rtd = substream->private_data; 47 struct snd_soc_dai *codec_dai; 48 int i, ret; 49 50 for_each_rtd_codec_dai(rtd, i, codec_dai) { 51 /* pll from mclk 12.288M */ 52 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS, 53 params_rate(params) * 512); 54 if (ret) 55 return ret; 56 57 /* sysclk from pll */ 58 ret = snd_soc_dai_set_sysclk(codec_dai, 1, 59 params_rate(params) * 512, 60 SND_SOC_CLOCK_IN); 61 if (ret) 62 return ret; 63 } 64 return 0; 65 } 66 67 static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = { 68 .hw_params = mt8173_rt5650_rt5514_hw_params, 69 }; 70 71 static struct snd_soc_jack mt8173_rt5650_rt5514_jack; 72 73 static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime) 74 { 75 struct snd_soc_card *card = runtime->card; 76 struct snd_soc_component *component = runtime->codec_dais[0]->component; 77 int ret; 78 79 rt5645_sel_asrc_clk_src(component, 80 RT5645_DA_STEREO_FILTER | 81 RT5645_AD_STEREO_FILTER, 82 RT5645_CLK_SEL_I2S1_ASRC); 83 84 /* enable jack detection */ 85 ret = snd_soc_card_jack_new(card, "Headset Jack", 86 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE | 87 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 88 SND_JACK_BTN_2 | SND_JACK_BTN_3, 89 &mt8173_rt5650_rt5514_jack, NULL, 0); 90 if (ret) { 91 dev_err(card->dev, "Can't new Headset Jack %d\n", ret); 92 return ret; 93 } 94 95 return rt5645_set_jack_detect(component, 96 &mt8173_rt5650_rt5514_jack, 97 &mt8173_rt5650_rt5514_jack, 98 &mt8173_rt5650_rt5514_jack); 99 } 100 101 static struct snd_soc_dai_link_component mt8173_rt5650_rt5514_codecs[] = { 102 { 103 .dai_name = "rt5645-aif1", 104 }, 105 { 106 .dai_name = "rt5514-aif1", 107 }, 108 }; 109 110 enum { 111 DAI_LINK_PLAYBACK, 112 DAI_LINK_CAPTURE, 113 DAI_LINK_CODEC_I2S, 114 }; 115 116 /* Digital audio interface glue - connects codec <---> CPU */ 117 static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = { 118 /* Front End DAI links */ 119 [DAI_LINK_PLAYBACK] = { 120 .name = "rt5650_rt5514 Playback", 121 .stream_name = "rt5650_rt5514 Playback", 122 .cpu_dai_name = "DL1", 123 .codec_name = "snd-soc-dummy", 124 .codec_dai_name = "snd-soc-dummy-dai", 125 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 126 .dynamic = 1, 127 .dpcm_playback = 1, 128 }, 129 [DAI_LINK_CAPTURE] = { 130 .name = "rt5650_rt5514 Capture", 131 .stream_name = "rt5650_rt5514 Capture", 132 .cpu_dai_name = "VUL", 133 .codec_name = "snd-soc-dummy", 134 .codec_dai_name = "snd-soc-dummy-dai", 135 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 136 .dynamic = 1, 137 .dpcm_capture = 1, 138 }, 139 /* Back End DAI links */ 140 [DAI_LINK_CODEC_I2S] = { 141 .name = "Codec", 142 .cpu_dai_name = "I2S", 143 .no_pcm = 1, 144 .codecs = mt8173_rt5650_rt5514_codecs, 145 .num_codecs = 2, 146 .init = mt8173_rt5650_rt5514_init, 147 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 148 SND_SOC_DAIFMT_CBS_CFS, 149 .ops = &mt8173_rt5650_rt5514_ops, 150 .ignore_pmdown_time = 1, 151 .dpcm_playback = 1, 152 .dpcm_capture = 1, 153 }, 154 }; 155 156 static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = { 157 { 158 .name_prefix = "Sub", 159 }, 160 }; 161 162 static struct snd_soc_card mt8173_rt5650_rt5514_card = { 163 .name = "mtk-rt5650-rt5514", 164 .owner = THIS_MODULE, 165 .dai_link = mt8173_rt5650_rt5514_dais, 166 .num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais), 167 .codec_conf = mt8173_rt5650_rt5514_codec_conf, 168 .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf), 169 .controls = mt8173_rt5650_rt5514_controls, 170 .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls), 171 .dapm_widgets = mt8173_rt5650_rt5514_widgets, 172 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets), 173 .dapm_routes = mt8173_rt5650_rt5514_routes, 174 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes), 175 }; 176 177 static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev) 178 { 179 struct snd_soc_card *card = &mt8173_rt5650_rt5514_card; 180 struct device_node *platform_node; 181 struct snd_soc_dai_link *dai_link; 182 int i, ret; 183 184 platform_node = of_parse_phandle(pdev->dev.of_node, 185 "mediatek,platform", 0); 186 if (!platform_node) { 187 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n"); 188 return -EINVAL; 189 } 190 191 for_each_card_prelinks(card, i, dai_link) { 192 if (dai_link->platform_name) 193 continue; 194 dai_link->platform_of_node = platform_node; 195 } 196 197 mt8173_rt5650_rt5514_codecs[0].of_node = 198 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0); 199 if (!mt8173_rt5650_rt5514_codecs[0].of_node) { 200 dev_err(&pdev->dev, 201 "Property 'audio-codec' missing or invalid\n"); 202 return -EINVAL; 203 } 204 mt8173_rt5650_rt5514_codecs[1].of_node = 205 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1); 206 if (!mt8173_rt5650_rt5514_codecs[1].of_node) { 207 dev_err(&pdev->dev, 208 "Property 'audio-codec' missing or invalid\n"); 209 return -EINVAL; 210 } 211 mt8173_rt5650_rt5514_codec_conf[0].of_node = 212 mt8173_rt5650_rt5514_codecs[1].of_node; 213 214 card->dev = &pdev->dev; 215 216 ret = devm_snd_soc_register_card(&pdev->dev, card); 217 if (ret) 218 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n", 219 __func__, ret); 220 return ret; 221 } 222 223 static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = { 224 { .compatible = "mediatek,mt8173-rt5650-rt5514", }, 225 { } 226 }; 227 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match); 228 229 static struct platform_driver mt8173_rt5650_rt5514_driver = { 230 .driver = { 231 .name = "mtk-rt5650-rt5514", 232 .of_match_table = mt8173_rt5650_rt5514_dt_match, 233 #ifdef CONFIG_PM 234 .pm = &snd_soc_pm_ops, 235 #endif 236 }, 237 .probe = mt8173_rt5650_rt5514_dev_probe, 238 }; 239 240 module_platform_driver(mt8173_rt5650_rt5514_driver); 241 242 /* Module information */ 243 MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver"); 244 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>"); 245 MODULE_LICENSE("GPL v2"); 246 MODULE_ALIAS("platform:mtk-rt5650-rt5514"); 247 248