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