1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2022 Intel Corporation. All rights reserved. 4 5 /* 6 * sof_ssp_amp.c - ASoc Machine driver for Intel platforms 7 * with RT1308/CS35L41 codec. 8 */ 9 10 #include <linux/acpi.h> 11 #include <linux/delay.h> 12 #include <linux/dmi.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <sound/core.h> 16 #include <sound/jack.h> 17 #include <sound/pcm.h> 18 #include <sound/pcm_params.h> 19 #include <sound/sof.h> 20 #include "../../codecs/hdac_hdmi.h" 21 #include "hda_dsp_common.h" 22 #include "sof_realtek_common.h" 23 #include "sof_cirrus_common.h" 24 25 #define NAME_SIZE 32 26 27 /* SSP port ID for speaker amplifier */ 28 #define SOF_AMPLIFIER_SSP(quirk) ((quirk) & GENMASK(3, 0)) 29 #define SOF_AMPLIFIER_SSP_MASK (GENMASK(3, 0)) 30 31 /* HDMI capture*/ 32 #define SOF_SSP_HDMI_CAPTURE_PRESENT BIT(4) 33 #define SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT 5 34 #define SOF_NO_OF_HDMI_CAPTURE_SSP_MASK (GENMASK(6, 5)) 35 #define SOF_NO_OF_HDMI_CAPTURE_SSP(quirk) \ 36 (((quirk) << SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT) & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) 37 38 #define SOF_HDMI_CAPTURE_1_SSP_SHIFT 7 39 #define SOF_HDMI_CAPTURE_1_SSP_MASK (GENMASK(9, 7)) 40 #define SOF_HDMI_CAPTURE_1_SSP(quirk) \ 41 (((quirk) << SOF_HDMI_CAPTURE_1_SSP_SHIFT) & SOF_HDMI_CAPTURE_1_SSP_MASK) 42 43 #define SOF_HDMI_CAPTURE_2_SSP_SHIFT 10 44 #define SOF_HDMI_CAPTURE_2_SSP_MASK (GENMASK(12, 10)) 45 #define SOF_HDMI_CAPTURE_2_SSP(quirk) \ 46 (((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK) 47 48 /* HDMI playback */ 49 #define SOF_HDMI_PLAYBACK_PRESENT BIT(13) 50 #define SOF_NO_OF_HDMI_PLAYBACK_SHIFT 14 51 #define SOF_NO_OF_HDMI_PLAYBACK_MASK (GENMASK(16, 14)) 52 #define SOF_NO_OF_HDMI_PLAYBACK(quirk) \ 53 (((quirk) << SOF_NO_OF_HDMI_PLAYBACK_SHIFT) & SOF_NO_OF_HDMI_PLAYBACK_MASK) 54 55 /* BT audio offload */ 56 #define SOF_SSP_BT_OFFLOAD_PRESENT BIT(17) 57 #define SOF_BT_OFFLOAD_SSP_SHIFT 18 58 #define SOF_BT_OFFLOAD_SSP_MASK (GENMASK(20, 18)) 59 #define SOF_BT_OFFLOAD_SSP(quirk) \ 60 (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK) 61 62 /* Speaker amplifiers */ 63 #define SOF_RT1308_SPEAKER_AMP_PRESENT BIT(21) 64 #define SOF_CS35L41_SPEAKER_AMP_PRESENT BIT(22) 65 66 /* Default: SSP2 */ 67 static unsigned long sof_ssp_amp_quirk = SOF_AMPLIFIER_SSP(2); 68 69 struct sof_hdmi_pcm { 70 struct list_head head; 71 struct snd_soc_jack sof_hdmi; 72 struct snd_soc_dai *codec_dai; 73 int device; 74 }; 75 76 struct sof_card_private { 77 struct list_head hdmi_pcm_list; 78 bool common_hdmi_codec_drv; 79 bool idisp_codec; 80 }; 81 82 static const struct dmi_system_id chromebook_platforms[] = { 83 { 84 .ident = "Google Chromebooks", 85 .matches = { 86 DMI_MATCH(DMI_SYS_VENDOR, "Google"), 87 } 88 }, 89 {}, 90 }; 91 92 static const struct snd_soc_dapm_widget sof_ssp_amp_dapm_widgets[] = { 93 SND_SOC_DAPM_MIC("SoC DMIC", NULL), 94 }; 95 96 static const struct snd_soc_dapm_route sof_ssp_amp_dapm_routes[] = { 97 /* digital mics */ 98 {"DMic", NULL, "SoC DMIC"}, 99 }; 100 101 static int sof_card_late_probe(struct snd_soc_card *card) 102 { 103 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card); 104 struct snd_soc_component *component = NULL; 105 char jack_name[NAME_SIZE]; 106 struct sof_hdmi_pcm *pcm; 107 int err; 108 int i; 109 110 if (!(sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT)) 111 return 0; 112 113 /* HDMI is not supported by SOF on Baytrail/CherryTrail */ 114 if (!ctx->idisp_codec) 115 return 0; 116 117 if (list_empty(&ctx->hdmi_pcm_list)) 118 return -EINVAL; 119 120 if (ctx->common_hdmi_codec_drv) { 121 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, 122 head); 123 component = pcm->codec_dai->component; 124 return hda_dsp_hdmi_build_controls(card, component); 125 } 126 127 i = 0; 128 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) { 129 component = pcm->codec_dai->component; 130 snprintf(jack_name, sizeof(jack_name), 131 "HDMI/DP, pcm=%d Jack", pcm->device); 132 err = snd_soc_card_jack_new(card, jack_name, 133 SND_JACK_AVOUT, &pcm->sof_hdmi); 134 135 if (err) 136 return err; 137 138 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device, 139 &pcm->sof_hdmi); 140 if (err < 0) 141 return err; 142 143 i++; 144 } 145 146 return hdac_hdmi_jack_port_init(component, &card->dapm); 147 } 148 149 static struct snd_soc_card sof_ssp_amp_card = { 150 .name = "ssp_amp", 151 .owner = THIS_MODULE, 152 .dapm_widgets = sof_ssp_amp_dapm_widgets, 153 .num_dapm_widgets = ARRAY_SIZE(sof_ssp_amp_dapm_widgets), 154 .dapm_routes = sof_ssp_amp_dapm_routes, 155 .num_dapm_routes = ARRAY_SIZE(sof_ssp_amp_dapm_routes), 156 .fully_routed = true, 157 .late_probe = sof_card_late_probe, 158 }; 159 160 static struct snd_soc_dai_link_component platform_component[] = { 161 { 162 /* name might be overridden during probe */ 163 .name = "0000:00:1f.3" 164 } 165 }; 166 167 static struct snd_soc_dai_link_component dmic_component[] = { 168 { 169 .name = "dmic-codec", 170 .dai_name = "dmic-hifi", 171 } 172 }; 173 174 static struct snd_soc_dai_link_component dummy_component[] = { 175 { 176 .name = "snd-soc-dummy", 177 .dai_name = "snd-soc-dummy-dai", 178 } 179 }; 180 181 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd) 182 { 183 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 184 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0); 185 struct sof_hdmi_pcm *pcm; 186 187 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL); 188 if (!pcm) 189 return -ENOMEM; 190 191 /* dai_link id is 1:1 mapped to the PCM device */ 192 pcm->device = rtd->dai_link->id; 193 pcm->codec_dai = dai; 194 195 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list); 196 197 return 0; 198 } 199 200 #define IDISP_CODEC_MASK 0x4 201 202 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev, 203 int ssp_codec, 204 int dmic_be_num, 205 int hdmi_num, 206 bool idisp_codec) 207 { 208 struct snd_soc_dai_link_component *idisp_components; 209 struct snd_soc_dai_link_component *cpus; 210 struct snd_soc_dai_link *links; 211 int i, id = 0; 212 213 links = devm_kcalloc(dev, sof_ssp_amp_card.num_links, 214 sizeof(struct snd_soc_dai_link), GFP_KERNEL); 215 cpus = devm_kcalloc(dev, sof_ssp_amp_card.num_links, 216 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL); 217 if (!links || !cpus) 218 return NULL; 219 220 /* HDMI-In SSP */ 221 if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) { 222 int num_of_hdmi_ssp = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >> 223 SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT; 224 225 for (i = 1; i <= num_of_hdmi_ssp; i++) { 226 int port = (i == 1 ? (sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >> 227 SOF_HDMI_CAPTURE_1_SSP_SHIFT : 228 (sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >> 229 SOF_HDMI_CAPTURE_2_SSP_SHIFT); 230 231 links[id].cpus = &cpus[id]; 232 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 233 "SSP%d Pin", port); 234 if (!links[id].cpus->dai_name) 235 return NULL; 236 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port); 237 if (!links[id].name) 238 return NULL; 239 links[id].id = id; 240 links[id].codecs = dummy_component; 241 links[id].num_codecs = ARRAY_SIZE(dummy_component); 242 links[id].platforms = platform_component; 243 links[id].num_platforms = ARRAY_SIZE(platform_component); 244 links[id].dpcm_capture = 1; 245 links[id].no_pcm = 1; 246 links[id].num_cpus = 1; 247 id++; 248 } 249 } 250 251 /* codec SSP */ 252 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_codec); 253 if (!links[id].name) 254 return NULL; 255 256 links[id].id = id; 257 if (sof_ssp_amp_quirk & SOF_RT1308_SPEAKER_AMP_PRESENT) { 258 sof_rt1308_dai_link(&links[id]); 259 } else if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) { 260 cs35l41_set_dai_link(&links[id]); 261 262 /* feedback from amplifier */ 263 links[id].dpcm_capture = 1; 264 } 265 links[id].platforms = platform_component; 266 links[id].num_platforms = ARRAY_SIZE(platform_component); 267 links[id].dpcm_playback = 1; 268 links[id].no_pcm = 1; 269 links[id].cpus = &cpus[id]; 270 links[id].num_cpus = 1; 271 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_codec); 272 if (!links[id].cpus->dai_name) 273 return NULL; 274 275 id++; 276 277 /* dmic */ 278 if (dmic_be_num > 0) { 279 /* at least we have dmic01 */ 280 links[id].name = "dmic01"; 281 links[id].cpus = &cpus[id]; 282 links[id].cpus->dai_name = "DMIC01 Pin"; 283 if (dmic_be_num > 1) { 284 /* set up 2 BE links at most */ 285 links[id + 1].name = "dmic16k"; 286 links[id + 1].cpus = &cpus[id + 1]; 287 links[id + 1].cpus->dai_name = "DMIC16k Pin"; 288 dmic_be_num = 2; 289 } 290 } 291 292 for (i = 0; i < dmic_be_num; i++) { 293 links[id].id = id; 294 links[id].num_cpus = 1; 295 links[id].codecs = dmic_component; 296 links[id].num_codecs = ARRAY_SIZE(dmic_component); 297 links[id].platforms = platform_component; 298 links[id].num_platforms = ARRAY_SIZE(platform_component); 299 links[id].ignore_suspend = 1; 300 links[id].dpcm_capture = 1; 301 links[id].no_pcm = 1; 302 id++; 303 } 304 305 /* HDMI playback */ 306 if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) { 307 /* HDMI */ 308 if (hdmi_num > 0) { 309 idisp_components = devm_kcalloc(dev, 310 hdmi_num, 311 sizeof(struct snd_soc_dai_link_component), 312 GFP_KERNEL); 313 if (!idisp_components) 314 goto devm_err; 315 } 316 for (i = 1; i <= hdmi_num; i++) { 317 links[id].name = devm_kasprintf(dev, GFP_KERNEL, 318 "iDisp%d", i); 319 if (!links[id].name) 320 goto devm_err; 321 322 links[id].id = id; 323 links[id].cpus = &cpus[id]; 324 links[id].num_cpus = 1; 325 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 326 "iDisp%d Pin", i); 327 if (!links[id].cpus->dai_name) 328 goto devm_err; 329 330 if (idisp_codec) { 331 idisp_components[i - 1].name = "ehdaudio0D2"; 332 idisp_components[i - 1].dai_name = devm_kasprintf(dev, 333 GFP_KERNEL, 334 "intel-hdmi-hifi%d", 335 i); 336 if (!idisp_components[i - 1].dai_name) 337 goto devm_err; 338 } else { 339 idisp_components[i - 1].name = "snd-soc-dummy"; 340 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai"; 341 } 342 343 links[id].codecs = &idisp_components[i - 1]; 344 links[id].num_codecs = 1; 345 links[id].platforms = platform_component; 346 links[id].num_platforms = ARRAY_SIZE(platform_component); 347 links[id].init = sof_hdmi_init; 348 links[id].dpcm_playback = 1; 349 links[id].no_pcm = 1; 350 id++; 351 } 352 } 353 354 /* BT audio offload */ 355 if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) { 356 int port = (sof_ssp_amp_quirk & SOF_BT_OFFLOAD_SSP_MASK) >> 357 SOF_BT_OFFLOAD_SSP_SHIFT; 358 359 links[id].id = id; 360 links[id].cpus = &cpus[id]; 361 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 362 "SSP%d Pin", port); 363 if (!links[id].cpus->dai_name) 364 goto devm_err; 365 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port); 366 if (!links[id].name) 367 goto devm_err; 368 links[id].codecs = dummy_component; 369 links[id].num_codecs = ARRAY_SIZE(dummy_component); 370 links[id].platforms = platform_component; 371 links[id].num_platforms = ARRAY_SIZE(platform_component); 372 links[id].dpcm_playback = 1; 373 links[id].dpcm_capture = 1; 374 links[id].no_pcm = 1; 375 links[id].num_cpus = 1; 376 id++; 377 } 378 379 return links; 380 devm_err: 381 return NULL; 382 } 383 384 static int sof_ssp_amp_probe(struct platform_device *pdev) 385 { 386 struct snd_soc_dai_link *dai_links; 387 struct snd_soc_acpi_mach *mach; 388 struct sof_card_private *ctx; 389 int dmic_be_num = 0, hdmi_num = 0; 390 int ret, ssp_codec; 391 392 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 393 if (!ctx) 394 return -ENOMEM; 395 396 if (pdev->id_entry && pdev->id_entry->driver_data) 397 sof_ssp_amp_quirk = (unsigned long)pdev->id_entry->driver_data; 398 399 mach = pdev->dev.platform_data; 400 401 if (dmi_check_system(chromebook_platforms) || mach->mach_params.dmic_num > 0) 402 dmic_be_num = 2; 403 404 ssp_codec = sof_ssp_amp_quirk & SOF_AMPLIFIER_SSP_MASK; 405 406 /* set number of dai links */ 407 sof_ssp_amp_card.num_links = 1 + dmic_be_num; 408 409 if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) 410 sof_ssp_amp_card.num_links += (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >> 411 SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT; 412 413 if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) { 414 hdmi_num = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_PLAYBACK_MASK) >> 415 SOF_NO_OF_HDMI_PLAYBACK_SHIFT; 416 /* default number of HDMI DAI's */ 417 if (!hdmi_num) 418 hdmi_num = 3; 419 420 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK) 421 ctx->idisp_codec = true; 422 423 sof_ssp_amp_card.num_links += hdmi_num; 424 } 425 426 if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) 427 sof_ssp_amp_card.num_links++; 428 429 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, dmic_be_num, hdmi_num, ctx->idisp_codec); 430 if (!dai_links) 431 return -ENOMEM; 432 433 sof_ssp_amp_card.dai_link = dai_links; 434 435 /* update codec_conf */ 436 if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) { 437 cs35l41_set_codec_conf(&sof_ssp_amp_card); 438 } 439 440 INIT_LIST_HEAD(&ctx->hdmi_pcm_list); 441 442 sof_ssp_amp_card.dev = &pdev->dev; 443 444 /* set platform name for each dailink */ 445 ret = snd_soc_fixup_dai_links_platform_name(&sof_ssp_amp_card, 446 mach->mach_params.platform); 447 if (ret) 448 return ret; 449 450 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv; 451 452 snd_soc_card_set_drvdata(&sof_ssp_amp_card, ctx); 453 454 return devm_snd_soc_register_card(&pdev->dev, &sof_ssp_amp_card); 455 } 456 457 static const struct platform_device_id board_ids[] = { 458 { 459 .name = "sof_ssp_amp", 460 }, 461 { 462 .name = "tgl_rt1308_hdmi_ssp", 463 .driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(2) | 464 SOF_NO_OF_HDMI_CAPTURE_SSP(2) | 465 SOF_HDMI_CAPTURE_1_SSP(1) | 466 SOF_HDMI_CAPTURE_2_SSP(5) | 467 SOF_SSP_HDMI_CAPTURE_PRESENT | 468 SOF_RT1308_SPEAKER_AMP_PRESENT), 469 }, 470 { 471 .name = "adl_cs35l41", 472 .driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(1) | 473 SOF_NO_OF_HDMI_PLAYBACK(4) | 474 SOF_HDMI_PLAYBACK_PRESENT | 475 SOF_BT_OFFLOAD_SSP(2) | 476 SOF_SSP_BT_OFFLOAD_PRESENT | 477 SOF_CS35L41_SPEAKER_AMP_PRESENT), 478 }, 479 { } 480 }; 481 MODULE_DEVICE_TABLE(platform, board_ids); 482 483 static struct platform_driver sof_ssp_amp_driver = { 484 .probe = sof_ssp_amp_probe, 485 .driver = { 486 .name = "sof_ssp_amp", 487 .pm = &snd_soc_pm_ops, 488 }, 489 .id_table = board_ids, 490 }; 491 module_platform_driver(sof_ssp_amp_driver); 492 493 MODULE_DESCRIPTION("ASoC Intel(R) SOF Amplifier Machine driver"); 494 MODULE_AUTHOR("balamurugan.c <balamurugan.c@intel.com>"); 495 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>"); 496 MODULE_LICENSE("GPL"); 497 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON); 498 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON); 499 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_CIRRUS_COMMON); 500