1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright(c) 2019 Intel Corporation. 3 4 /* 5 * Intel SOF Machine driver for Dialog headphone codec 6 */ 7 8 #include <linux/input.h> 9 #include <linux/module.h> 10 #include <sound/jack.h> 11 #include <sound/pcm.h> 12 #include <sound/pcm_params.h> 13 #include <linux/platform_device.h> 14 #include <sound/soc.h> 15 #include <sound/soc-acpi.h> 16 #include <sound/sof.h> 17 #include "../../codecs/da7219.h" 18 #include "sof_board_helpers.h" 19 #include "sof_maxim_common.h" 20 21 /* Driver-specific board quirks: from bit 0 to 7 */ 22 #define SOF_DA7219_GLK_BOARD BIT(0) 23 #define SOF_DA7219_CML_BOARD BIT(1) 24 #define SOF_DA7219_JSL_BOARD BIT(2) 25 #define SOF_DA7219_MCLK_EN BIT(3) 26 27 #define DIALOG_CODEC_DAI "da7219-hifi" 28 29 static int platform_clock_control(struct snd_soc_dapm_widget *w, 30 struct snd_kcontrol *k, int event) 31 { 32 struct snd_soc_dapm_context *dapm = w->dapm; 33 struct snd_soc_card *card = dapm->card; 34 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card); 35 struct snd_soc_dai *codec_dai; 36 int ret = 0; 37 38 if (ctx->da7219.pll_bypass) 39 return ret; 40 41 /* PLL SRM mode */ 42 codec_dai = snd_soc_card_get_codec_dai(card, DIALOG_CODEC_DAI); 43 if (!codec_dai) { 44 dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n"); 45 return -EIO; 46 } 47 48 if (SND_SOC_DAPM_EVENT_OFF(event)) { 49 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 50 0, 0); 51 if (ret) 52 dev_err(card->dev, "failed to stop PLL: %d\n", ret); 53 } else if (SND_SOC_DAPM_EVENT_ON(event)) { 54 dev_dbg(card->dev, "pll srm mode\n"); 55 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("Line Out"), 69 }; 70 71 static const struct snd_soc_dapm_widget widgets[] = { 72 SND_SOC_DAPM_HP("Headphone Jack", NULL), 73 SND_SOC_DAPM_MIC("Headset Mic", NULL), 74 SND_SOC_DAPM_LINE("Line Out", NULL), 75 76 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 77 platform_clock_control, SND_SOC_DAPM_POST_PMD | 78 SND_SOC_DAPM_PRE_PMU), 79 }; 80 81 static const struct snd_soc_dapm_route audio_map[] = { 82 { "Headphone Jack", NULL, "HPL" }, 83 { "Headphone Jack", NULL, "HPR" }, 84 85 { "MIC", NULL, "Headset Mic" }, 86 87 { "Headphone Jack", NULL, "Platform Clock" }, 88 { "Headset Mic", NULL, "Platform Clock" }, 89 { "Line Out", NULL, "Platform Clock" }, 90 }; 91 92 static struct snd_soc_jack_pin jack_pins[] = { 93 { 94 .pin = "Headphone Jack", 95 .mask = SND_JACK_HEADPHONE, 96 }, 97 { 98 .pin = "Headset Mic", 99 .mask = SND_JACK_MICROPHONE, 100 }, 101 { 102 .pin = "Line Out", 103 .mask = SND_JACK_LINEOUT, 104 }, 105 }; 106 107 static int da7219_codec_init(struct snd_soc_pcm_runtime *rtd) 108 { 109 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card); 110 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 111 struct snd_soc_component *component = codec_dai->component; 112 struct snd_soc_jack *jack = &ctx->headset_jack; 113 int mclk_rate, ret; 114 115 mclk_rate = sof_dai_get_mclk(rtd); 116 if (mclk_rate <= 0) { 117 dev_err(rtd->dev, "invalid mclk freq %d\n", mclk_rate); 118 return -EINVAL; 119 } 120 121 ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, mclk_rate, 122 SND_SOC_CLOCK_IN); 123 if (ret) { 124 dev_err(rtd->dev, "fail to set sysclk, ret %d\n", ret); 125 return ret; 126 } 127 128 /* 129 * Use PLL bypass mode if MCLK is available, be sure to set the 130 * frequency of MCLK to 12.288 or 24.576MHz on topology side. 131 */ 132 if (ctx->da7219.mclk_en && 133 (mclk_rate == 12288000 || mclk_rate == 24576000)) { 134 /* PLL bypass mode */ 135 dev_dbg(rtd->dev, "pll bypass mode, mclk rate %d\n", mclk_rate); 136 137 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0); 138 if (ret) { 139 dev_err(rtd->dev, "fail to set pll, ret %d\n", ret); 140 return ret; 141 } 142 143 ctx->da7219.pll_bypass = true; 144 } 145 146 /* 147 * Headset buttons map to the google Reference headset. 148 * These can be configured by userspace. 149 */ 150 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack", 151 SND_JACK_HEADSET | SND_JACK_BTN_0 | 152 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 153 SND_JACK_BTN_3 | SND_JACK_LINEOUT, 154 jack, jack_pins, ARRAY_SIZE(jack_pins)); 155 if (ret) { 156 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret); 157 return ret; 158 } 159 160 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 161 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP); 162 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN); 163 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND); 164 165 ret = snd_soc_component_set_jack(component, jack, NULL); 166 if (ret) { 167 dev_err(rtd->dev, "fail to set component jack, ret %d\n", ret); 168 return ret; 169 } 170 171 return ret; 172 } 173 174 static void da7219_codec_exit(struct snd_soc_pcm_runtime *rtd) 175 { 176 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component; 177 178 snd_soc_component_set_jack(component, NULL, NULL); 179 } 180 181 static int max98373_hw_params(struct snd_pcm_substream *substream, 182 struct snd_pcm_hw_params *params) 183 { 184 struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream); 185 int ret, j; 186 187 for (j = 0; j < runtime->dai_link->num_codecs; j++) { 188 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, j); 189 190 if (!strcmp(codec_dai->component->name, MAX_98373_DEV0_NAME)) { 191 /* vmon_slot_no = 0 imon_slot_no = 1 for TX slots */ 192 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 3, 4, 16); 193 if (ret < 0) { 194 dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret); 195 return ret; 196 } 197 } 198 if (!strcmp(codec_dai->component->name, MAX_98373_DEV1_NAME)) { 199 /* vmon_slot_no = 2 imon_slot_no = 3 for TX slots */ 200 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC, 3, 4, 16); 201 if (ret < 0) { 202 dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret); 203 return ret; 204 } 205 } 206 } 207 208 return 0; 209 } 210 211 static const struct snd_soc_ops max98373_ops = { 212 .hw_params = max98373_hw_params, 213 }; 214 215 static int card_late_probe(struct snd_soc_card *card) 216 { 217 return sof_intel_board_card_late_probe(card); 218 } 219 220 static struct snd_soc_card card_da7219 = { 221 .name = "da7219", /* the sof- prefix is added by the core */ 222 .owner = THIS_MODULE, 223 .controls = controls, 224 .num_controls = ARRAY_SIZE(controls), 225 .dapm_widgets = widgets, 226 .num_dapm_widgets = ARRAY_SIZE(widgets), 227 .dapm_routes = audio_map, 228 .num_dapm_routes = ARRAY_SIZE(audio_map), 229 .fully_routed = true, 230 .late_probe = card_late_probe, 231 }; 232 233 static struct snd_soc_dai_link_component da7219_component[] = { 234 { 235 .name = "i2c-DLGS7219:00", 236 .dai_name = DIALOG_CODEC_DAI, 237 } 238 }; 239 240 static int 241 sof_card_dai_links_create(struct device *dev, struct snd_soc_card *card, 242 struct sof_card_private *ctx) 243 { 244 int ret; 245 246 ret = sof_intel_board_set_dai_link(dev, card, ctx); 247 if (ret) 248 return ret; 249 250 if (!ctx->codec_link) { 251 dev_err(dev, "codec link not available"); 252 return -EINVAL; 253 } 254 255 /* codec-specific fields for headphone codec */ 256 ctx->codec_link->codecs = da7219_component; 257 ctx->codec_link->num_codecs = ARRAY_SIZE(da7219_component); 258 ctx->codec_link->init = da7219_codec_init; 259 ctx->codec_link->exit = da7219_codec_exit; 260 261 if (ctx->amp_type == CODEC_NONE) 262 return 0; 263 264 if (!ctx->amp_link) { 265 dev_err(dev, "amp link not available"); 266 return -EINVAL; 267 } 268 269 /* codec-specific fields for speaker amplifier */ 270 switch (ctx->amp_type) { 271 case CODEC_MAX98357A: 272 max_98357a_dai_link(ctx->amp_link); 273 break; 274 case CODEC_MAX98360A: 275 max_98360a_dai_link(ctx->amp_link); 276 break; 277 case CODEC_MAX98373: 278 ctx->amp_link->codecs = max_98373_components; 279 ctx->amp_link->num_codecs = ARRAY_SIZE(max_98373_components); 280 ctx->amp_link->init = max_98373_spk_codec_init; 281 if (ctx->da7219.is_jsl_board) { 282 ctx->amp_link->ops = &max98373_ops; /* use local ops */ 283 } else { 284 /* TBD: implement the amp for later platform */ 285 dev_err(dev, "max98373 not support yet\n"); 286 return -EINVAL; 287 } 288 break; 289 case CODEC_MAX98390: 290 max_98390_dai_link(dev, ctx->amp_link); 291 break; 292 default: 293 dev_err(dev, "invalid amp type %d\n", ctx->amp_type); 294 return -EINVAL; 295 } 296 297 return 0; 298 } 299 300 #define GLK_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_AMP, \ 301 SOF_LINK_CODEC, \ 302 SOF_LINK_DMIC01, \ 303 SOF_LINK_IDISP_HDMI, \ 304 SOF_LINK_NONE, \ 305 SOF_LINK_NONE, \ 306 SOF_LINK_NONE) 307 308 #define CML_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_AMP, \ 309 SOF_LINK_CODEC, \ 310 SOF_LINK_DMIC01, \ 311 SOF_LINK_IDISP_HDMI, \ 312 SOF_LINK_DMIC16K, \ 313 SOF_LINK_NONE, \ 314 SOF_LINK_NONE) 315 316 #define JSL_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_AMP, \ 317 SOF_LINK_CODEC, \ 318 SOF_LINK_DMIC01, \ 319 SOF_LINK_IDISP_HDMI, \ 320 SOF_LINK_DMIC16K, \ 321 SOF_LINK_NONE, \ 322 SOF_LINK_NONE) 323 324 static int audio_probe(struct platform_device *pdev) 325 { 326 struct snd_soc_acpi_mach *mach = pdev->dev.platform_data; 327 struct sof_card_private *ctx; 328 char *card_name; 329 unsigned long board_quirk = 0; 330 int ret; 331 332 if (pdev->id_entry && pdev->id_entry->driver_data) 333 board_quirk = (unsigned long)pdev->id_entry->driver_data; 334 335 dev_dbg(&pdev->dev, "board_quirk = %lx\n", board_quirk); 336 337 /* initialize ctx with board quirk */ 338 ctx = sof_intel_board_get_ctx(&pdev->dev, board_quirk); 339 if (!ctx) 340 return -ENOMEM; 341 342 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK) 343 ctx->hdmi.idisp_codec = true; 344 345 if (board_quirk & SOF_DA7219_GLK_BOARD) { 346 /* dmic16k not support */ 347 ctx->dmic_be_num = 1; 348 349 /* overwrite the DAI link order for GLK boards */ 350 ctx->link_order_overwrite = GLK_LINK_ORDER; 351 352 /* backward-compatible with existing devices */ 353 switch (ctx->amp_type) { 354 case CODEC_MAX98357A: 355 card_name = devm_kstrdup(&pdev->dev, "glkda7219max", 356 GFP_KERNEL); 357 if (!card_name) 358 return -ENOMEM; 359 360 card_da7219.name = card_name; 361 break; 362 default: 363 break; 364 } 365 } else if (board_quirk & SOF_DA7219_CML_BOARD) { 366 /* overwrite the DAI link order for CML boards */ 367 ctx->link_order_overwrite = CML_LINK_ORDER; 368 369 /* backward-compatible with existing devices */ 370 switch (ctx->amp_type) { 371 case CODEC_MAX98357A: 372 card_name = devm_kstrdup(&pdev->dev, "cmlda7219max", 373 GFP_KERNEL); 374 if (!card_name) 375 return -ENOMEM; 376 377 card_da7219.name = card_name; 378 break; 379 case CODEC_MAX98390: 380 card_name = devm_kstrdup(&pdev->dev, 381 "cml_max98390_da7219", 382 GFP_KERNEL); 383 if (!card_name) 384 return -ENOMEM; 385 386 card_da7219.name = card_name; 387 break; 388 default: 389 break; 390 } 391 } else if (board_quirk & SOF_DA7219_JSL_BOARD) { 392 ctx->da7219.is_jsl_board = true; 393 394 /* overwrite the DAI link order for JSL boards */ 395 ctx->link_order_overwrite = JSL_LINK_ORDER; 396 397 /* backward-compatible with existing devices */ 398 switch (ctx->amp_type) { 399 case CODEC_MAX98360A: 400 card_name = devm_kstrdup(&pdev->dev, "da7219max98360a", 401 GFP_KERNEL); 402 if (!card_name) 403 return -ENOMEM; 404 405 card_da7219.name = card_name; 406 break; 407 case CODEC_MAX98373: 408 card_name = devm_kstrdup(&pdev->dev, "da7219max", 409 GFP_KERNEL); 410 if (!card_name) 411 return -ENOMEM; 412 413 card_da7219.name = card_name; 414 break; 415 default: 416 break; 417 } 418 } 419 420 if (board_quirk & SOF_DA7219_MCLK_EN) 421 ctx->da7219.mclk_en = true; 422 423 /* update dai_link */ 424 ret = sof_card_dai_links_create(&pdev->dev, &card_da7219, ctx); 425 if (ret) 426 return ret; 427 428 /* update codec_conf */ 429 switch (ctx->amp_type) { 430 case CODEC_MAX98373: 431 max_98373_set_codec_conf(&card_da7219); 432 break; 433 case CODEC_MAX98390: 434 max_98390_set_codec_conf(&pdev->dev, &card_da7219); 435 break; 436 case CODEC_MAX98357A: 437 case CODEC_MAX98360A: 438 case CODEC_NONE: 439 /* no codec conf required */ 440 break; 441 default: 442 dev_err(&pdev->dev, "invalid amp type %d\n", ctx->amp_type); 443 return -EINVAL; 444 } 445 446 card_da7219.dev = &pdev->dev; 447 448 ret = snd_soc_fixup_dai_links_platform_name(&card_da7219, 449 mach->mach_params.platform); 450 if (ret) 451 return ret; 452 453 snd_soc_card_set_drvdata(&card_da7219, ctx); 454 455 return devm_snd_soc_register_card(&pdev->dev, &card_da7219); 456 } 457 458 static const struct platform_device_id board_ids[] = { 459 { 460 .name = "glk_da7219_def", 461 .driver_data = (kernel_ulong_t)(SOF_DA7219_GLK_BOARD | 462 SOF_SSP_PORT_CODEC(2) | 463 SOF_SSP_PORT_AMP(1)), 464 }, 465 { 466 .name = "cml_da7219_def", 467 .driver_data = (kernel_ulong_t)(SOF_DA7219_CML_BOARD | 468 SOF_SSP_PORT_CODEC(0) | 469 SOF_SSP_PORT_AMP(1)), 470 }, 471 { 472 .name = "jsl_da7219_def", 473 .driver_data = (kernel_ulong_t)(SOF_DA7219_JSL_BOARD | 474 SOF_SSP_PORT_CODEC(0) | 475 SOF_SSP_PORT_AMP(1)), 476 }, 477 { 478 .name = "adl_da7219_def", 479 .driver_data = (kernel_ulong_t)(SOF_DA7219_MCLK_EN | 480 SOF_SSP_PORT_CODEC(0) | 481 SOF_SSP_PORT_AMP(1) | 482 SOF_NUM_IDISP_HDMI(4) | 483 SOF_SSP_PORT_BT_OFFLOAD(2) | 484 SOF_BT_OFFLOAD_PRESENT), 485 }, 486 { 487 .name = "rpl_da7219_def", 488 .driver_data = (kernel_ulong_t)(SOF_DA7219_MCLK_EN | 489 SOF_SSP_PORT_CODEC(0) | 490 SOF_SSP_PORT_AMP(1) | 491 SOF_NUM_IDISP_HDMI(4) | 492 SOF_SSP_PORT_BT_OFFLOAD(2) | 493 SOF_BT_OFFLOAD_PRESENT), 494 }, 495 { 496 .name = "mtl_da7219_def", 497 .driver_data = (kernel_ulong_t)(SOF_DA7219_MCLK_EN | 498 SOF_SSP_PORT_CODEC(2) | 499 SOF_SSP_PORT_AMP(0) | 500 SOF_SSP_PORT_BT_OFFLOAD(1) | 501 SOF_BT_OFFLOAD_PRESENT), 502 }, 503 { } 504 }; 505 MODULE_DEVICE_TABLE(platform, board_ids); 506 507 static struct platform_driver audio = { 508 .probe = audio_probe, 509 .driver = { 510 .name = "sof_da7219", 511 .pm = &snd_soc_pm_ops, 512 }, 513 .id_table = board_ids, 514 }; 515 module_platform_driver(audio) 516 517 /* Module information */ 518 MODULE_DESCRIPTION("ASoC Intel(R) SOF Machine driver for Dialog codec"); 519 MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>"); 520 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>"); 521 MODULE_LICENSE("GPL v2"); 522 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_BOARD_HELPERS); 523 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON); 524