1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright(c) 2019 Intel Corporation. 3 4 /* 5 * Intel SOF Machine driver for DA7219 + MAX98373/MAX98360A codec 6 */ 7 8 #include <linux/input.h> 9 #include <linux/module.h> 10 #include <sound/pcm.h> 11 #include <sound/pcm_params.h> 12 #include <linux/platform_device.h> 13 #include <sound/soc.h> 14 #include <sound/soc-acpi.h> 15 #include "../../codecs/da7219.h" 16 #include "../../codecs/da7219-aad.h" 17 #include "hda_dsp_common.h" 18 19 #define DIALOG_CODEC_DAI "da7219-hifi" 20 #define MAX98373_CODEC_DAI "max98373-aif1" 21 #define MAXIM_DEV0_NAME "i2c-MX98373:00" 22 #define MAXIM_DEV1_NAME "i2c-MX98373:01" 23 24 struct hdmi_pcm { 25 struct list_head head; 26 struct snd_soc_dai *codec_dai; 27 int device; 28 }; 29 30 struct card_private { 31 struct snd_soc_jack headset; 32 struct list_head hdmi_pcm_list; 33 struct snd_soc_jack hdmi[3]; 34 }; 35 36 static int platform_clock_control(struct snd_soc_dapm_widget *w, 37 struct snd_kcontrol *k, int event) 38 { 39 struct snd_soc_dapm_context *dapm = w->dapm; 40 struct snd_soc_card *card = dapm->card; 41 struct snd_soc_dai *codec_dai; 42 int ret = 0; 43 44 codec_dai = snd_soc_card_get_codec_dai(card, DIALOG_CODEC_DAI); 45 if (!codec_dai) { 46 dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n"); 47 return -EIO; 48 } 49 50 if (SND_SOC_DAPM_EVENT_OFF(event)) { 51 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 52 0, 0); 53 if (ret) 54 dev_err(card->dev, "failed to stop PLL: %d\n", ret); 55 } else if (SND_SOC_DAPM_EVENT_ON(event)) { 56 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM, 57 0, DA7219_PLL_FREQ_OUT_98304); 58 if (ret) 59 dev_err(card->dev, "failed to start PLL: %d\n", ret); 60 } 61 62 return ret; 63 } 64 65 static const struct snd_kcontrol_new controls[] = { 66 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 67 SOC_DAPM_PIN_SWITCH("Headset Mic"), 68 SOC_DAPM_PIN_SWITCH("Left Spk"), 69 SOC_DAPM_PIN_SWITCH("Right Spk"), 70 }; 71 72 static const struct snd_kcontrol_new m98360a_controls[] = { 73 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 74 SOC_DAPM_PIN_SWITCH("Headset Mic"), 75 SOC_DAPM_PIN_SWITCH("Spk"), 76 }; 77 78 /* For MAX98373 amp */ 79 static const struct snd_soc_dapm_widget widgets[] = { 80 SND_SOC_DAPM_HP("Headphone Jack", NULL), 81 SND_SOC_DAPM_MIC("Headset Mic", NULL), 82 83 SND_SOC_DAPM_SPK("Left Spk", NULL), 84 SND_SOC_DAPM_SPK("Right Spk", NULL), 85 86 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 87 platform_clock_control, SND_SOC_DAPM_POST_PMD | 88 SND_SOC_DAPM_PRE_PMU), 89 90 SND_SOC_DAPM_MIC("SoC DMIC", NULL), 91 }; 92 93 static const struct snd_soc_dapm_route audio_map[] = { 94 { "Headphone Jack", NULL, "HPL" }, 95 { "Headphone Jack", NULL, "HPR" }, 96 97 { "MIC", NULL, "Headset Mic" }, 98 99 { "Headphone Jack", NULL, "Platform Clock" }, 100 { "Headset Mic", NULL, "Platform Clock" }, 101 102 { "Left Spk", NULL, "Left BE_OUT" }, 103 { "Right Spk", NULL, "Right BE_OUT" }, 104 105 /* digital mics */ 106 {"DMic", NULL, "SoC DMIC"}, 107 }; 108 109 /* For MAX98360A amp */ 110 static const struct snd_soc_dapm_widget max98360a_widgets[] = { 111 SND_SOC_DAPM_HP("Headphone Jack", NULL), 112 SND_SOC_DAPM_MIC("Headset Mic", NULL), 113 114 SND_SOC_DAPM_SPK("Spk", NULL), 115 116 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 117 platform_clock_control, SND_SOC_DAPM_POST_PMD | 118 SND_SOC_DAPM_PRE_PMU), 119 120 SND_SOC_DAPM_MIC("SoC DMIC", NULL), 121 }; 122 123 static const struct snd_soc_dapm_route max98360a_map[] = { 124 { "Headphone Jack", NULL, "HPL" }, 125 { "Headphone Jack", NULL, "HPR" }, 126 127 { "MIC", NULL, "Headset Mic" }, 128 129 { "Headphone Jack", NULL, "Platform Clock" }, 130 { "Headset Mic", NULL, "Platform Clock" }, 131 132 {"Spk", NULL, "Speaker"}, 133 134 /* digital mics */ 135 {"DMic", NULL, "SoC DMIC"}, 136 }; 137 138 static struct snd_soc_jack_pin jack_pins[] = { 139 { 140 .pin = "Headphone Jack", 141 .mask = SND_JACK_HEADPHONE, 142 }, 143 { 144 .pin = "Headset Mic", 145 .mask = SND_JACK_MICROPHONE, 146 }, 147 }; 148 149 static struct snd_soc_jack headset; 150 151 static int da7219_codec_init(struct snd_soc_pcm_runtime *rtd) 152 { 153 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 154 struct snd_soc_component *component = codec_dai->component; 155 struct snd_soc_jack *jack; 156 int ret; 157 158 /* Configure sysclk for codec */ 159 ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, 24000000, 160 SND_SOC_CLOCK_IN); 161 if (ret) { 162 dev_err(rtd->dev, "can't set codec sysclk configuration\n"); 163 return ret; 164 } 165 166 /* 167 * Headset buttons map to the google Reference headset. 168 * These can be configured by userspace. 169 */ 170 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack", 171 SND_JACK_HEADSET | SND_JACK_BTN_0 | 172 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 173 SND_JACK_BTN_3 | SND_JACK_LINEOUT, 174 &headset, 175 jack_pins, 176 ARRAY_SIZE(jack_pins)); 177 if (ret) { 178 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret); 179 return ret; 180 } 181 182 jack = &headset; 183 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 184 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP); 185 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN); 186 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND); 187 da7219_aad_jack_det(component, jack); 188 189 return ret; 190 } 191 192 static int ssp1_hw_params(struct snd_pcm_substream *substream, 193 struct snd_pcm_hw_params *params) 194 { 195 struct snd_soc_pcm_runtime *runtime = asoc_substream_to_rtd(substream); 196 int ret, j; 197 198 for (j = 0; j < runtime->num_codecs; j++) { 199 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, j); 200 201 if (!strcmp(codec_dai->component->name, MAXIM_DEV0_NAME)) { 202 /* vmon_slot_no = 0 imon_slot_no = 1 for TX slots */ 203 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 3, 4, 16); 204 if (ret < 0) { 205 dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret); 206 return ret; 207 } 208 } 209 if (!strcmp(codec_dai->component->name, MAXIM_DEV1_NAME)) { 210 /* vmon_slot_no = 2 imon_slot_no = 3 for TX slots */ 211 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC, 3, 4, 16); 212 if (ret < 0) { 213 dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret); 214 return ret; 215 } 216 } 217 } 218 219 return 0; 220 } 221 222 static struct snd_soc_ops ssp1_ops = { 223 .hw_params = ssp1_hw_params, 224 }; 225 226 static struct snd_soc_codec_conf max98373_codec_conf[] = { 227 { 228 .dlc = COMP_CODEC_CONF(MAXIM_DEV0_NAME), 229 .name_prefix = "Right", 230 }, 231 { 232 .dlc = COMP_CODEC_CONF(MAXIM_DEV1_NAME), 233 .name_prefix = "Left", 234 }, 235 }; 236 237 static int hdmi_init(struct snd_soc_pcm_runtime *rtd) 238 { 239 struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 240 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0); 241 struct hdmi_pcm *pcm; 242 243 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL); 244 if (!pcm) 245 return -ENOMEM; 246 247 pcm->device = dai->id; 248 pcm->codec_dai = dai; 249 250 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list); 251 252 return 0; 253 } 254 255 static int card_late_probe(struct snd_soc_card *card) 256 { 257 struct card_private *ctx = snd_soc_card_get_drvdata(card); 258 struct snd_soc_acpi_mach *mach = (card->dev)->platform_data; 259 struct hdmi_pcm *pcm; 260 261 if (mach->mach_params.common_hdmi_codec_drv) { 262 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct hdmi_pcm, 263 head); 264 return hda_dsp_hdmi_build_controls(card, 265 pcm->codec_dai->component); 266 } 267 268 return -EINVAL; 269 } 270 271 SND_SOC_DAILINK_DEF(ssp0_pin, 272 DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin"))); 273 SND_SOC_DAILINK_DEF(ssp0_codec, 274 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7219:00", DIALOG_CODEC_DAI))); 275 276 SND_SOC_DAILINK_DEF(ssp1_pin, 277 DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin"))); 278 SND_SOC_DAILINK_DEF(ssp1_amps, 279 DAILINK_COMP_ARRAY( 280 /* Left */ COMP_CODEC(MAXIM_DEV0_NAME, MAX98373_CODEC_DAI), 281 /* Right */ COMP_CODEC(MAXIM_DEV1_NAME, MAX98373_CODEC_DAI))); 282 283 SND_SOC_DAILINK_DEF(ssp1_m98360a, 284 DAILINK_COMP_ARRAY(COMP_CODEC("MX98360A:00", "HiFi"))); 285 286 SND_SOC_DAILINK_DEF(dmic_pin, 287 DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin"))); 288 SND_SOC_DAILINK_DEF(dmic_codec, 289 DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi"))); 290 291 SND_SOC_DAILINK_DEF(dmic16k_pin, 292 DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin"))); 293 294 SND_SOC_DAILINK_DEF(idisp1_pin, 295 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin"))); 296 SND_SOC_DAILINK_DEF(idisp1_codec, 297 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1"))); 298 299 SND_SOC_DAILINK_DEF(idisp2_pin, 300 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin"))); 301 SND_SOC_DAILINK_DEF(idisp2_codec, 302 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2"))); 303 304 SND_SOC_DAILINK_DEF(idisp3_pin, 305 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin"))); 306 SND_SOC_DAILINK_DEF(idisp3_codec, 307 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3"))); 308 309 SND_SOC_DAILINK_DEF(platform, /* subject to be overridden during probe */ 310 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3"))); 311 312 static struct snd_soc_dai_link dais[] = { 313 /* Back End DAI links */ 314 { 315 .name = "SSP1-Codec", 316 .id = 0, 317 .ignore_pmdown_time = 1, 318 .no_pcm = 1, 319 .dpcm_playback = 1, 320 .dpcm_capture = 1, /* IV feedback */ 321 .ops = &ssp1_ops, 322 SND_SOC_DAILINK_REG(ssp1_pin, ssp1_amps, platform), 323 }, 324 { 325 .name = "SSP0-Codec", 326 .id = 1, 327 .no_pcm = 1, 328 .init = da7219_codec_init, 329 .ignore_pmdown_time = 1, 330 .dpcm_playback = 1, 331 .dpcm_capture = 1, 332 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform), 333 }, 334 { 335 .name = "dmic01", 336 .id = 2, 337 .ignore_suspend = 1, 338 .dpcm_capture = 1, 339 .no_pcm = 1, 340 SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform), 341 }, 342 { 343 .name = "iDisp1", 344 .id = 3, 345 .init = hdmi_init, 346 .dpcm_playback = 1, 347 .no_pcm = 1, 348 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform), 349 }, 350 { 351 .name = "iDisp2", 352 .id = 4, 353 .init = hdmi_init, 354 .dpcm_playback = 1, 355 .no_pcm = 1, 356 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform), 357 }, 358 { 359 .name = "iDisp3", 360 .id = 5, 361 .init = hdmi_init, 362 .dpcm_playback = 1, 363 .no_pcm = 1, 364 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform), 365 }, 366 { 367 .name = "dmic16k", 368 .id = 6, 369 .ignore_suspend = 1, 370 .dpcm_capture = 1, 371 .no_pcm = 1, 372 SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform), 373 } 374 }; 375 376 static struct snd_soc_card card_da7219_m98373 = { 377 .name = "da7219max", 378 .owner = THIS_MODULE, 379 .dai_link = dais, 380 .num_links = ARRAY_SIZE(dais), 381 .controls = controls, 382 .num_controls = ARRAY_SIZE(controls), 383 .dapm_widgets = widgets, 384 .num_dapm_widgets = ARRAY_SIZE(widgets), 385 .dapm_routes = audio_map, 386 .num_dapm_routes = ARRAY_SIZE(audio_map), 387 .codec_conf = max98373_codec_conf, 388 .num_configs = ARRAY_SIZE(max98373_codec_conf), 389 .fully_routed = true, 390 .late_probe = card_late_probe, 391 }; 392 393 static struct snd_soc_card card_da7219_m98360a = { 394 .name = "da7219max98360a", 395 .owner = THIS_MODULE, 396 .dai_link = dais, 397 .num_links = ARRAY_SIZE(dais), 398 .controls = m98360a_controls, 399 .num_controls = ARRAY_SIZE(m98360a_controls), 400 .dapm_widgets = max98360a_widgets, 401 .num_dapm_widgets = ARRAY_SIZE(max98360a_widgets), 402 .dapm_routes = max98360a_map, 403 .num_dapm_routes = ARRAY_SIZE(max98360a_map), 404 .fully_routed = true, 405 .late_probe = card_late_probe, 406 }; 407 408 static int audio_probe(struct platform_device *pdev) 409 { 410 static struct snd_soc_card *card; 411 struct snd_soc_acpi_mach *mach; 412 struct card_private *ctx; 413 int ret; 414 415 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 416 if (!ctx) 417 return -ENOMEM; 418 419 /* By default dais[0] is configured for max98373 */ 420 if (!strcmp(pdev->name, "sof_da7219_mx98360a")) { 421 dais[0] = (struct snd_soc_dai_link) { 422 .name = "SSP1-Codec", 423 .id = 0, 424 .no_pcm = 1, 425 .dpcm_playback = 1, 426 .ignore_pmdown_time = 1, 427 SND_SOC_DAILINK_REG(ssp1_pin, ssp1_m98360a, platform) }; 428 } 429 430 INIT_LIST_HEAD(&ctx->hdmi_pcm_list); 431 card = (struct snd_soc_card *)pdev->id_entry->driver_data; 432 card->dev = &pdev->dev; 433 434 mach = pdev->dev.platform_data; 435 ret = snd_soc_fixup_dai_links_platform_name(card, 436 mach->mach_params.platform); 437 if (ret) 438 return ret; 439 440 snd_soc_card_set_drvdata(card, ctx); 441 442 return devm_snd_soc_register_card(&pdev->dev, card); 443 } 444 445 static const struct platform_device_id board_ids[] = { 446 { 447 .name = "sof_da7219_mx98373", 448 .driver_data = (kernel_ulong_t)&card_da7219_m98373, 449 }, 450 { 451 .name = "sof_da7219_mx98360a", 452 .driver_data = (kernel_ulong_t)&card_da7219_m98360a, 453 }, 454 { } 455 }; 456 MODULE_DEVICE_TABLE(platform, board_ids); 457 458 static struct platform_driver audio = { 459 .probe = audio_probe, 460 .driver = { 461 .name = "sof_da7219_max98_360a_373", 462 .pm = &snd_soc_pm_ops, 463 }, 464 .id_table = board_ids, 465 }; 466 module_platform_driver(audio) 467 468 /* Module information */ 469 MODULE_DESCRIPTION("ASoC Intel(R) SOF Machine driver"); 470 MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>"); 471 MODULE_LICENSE("GPL v2"); 472 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON); 473