1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright(c) 2018-19 Canonical Corporation. 3 4 /* 5 * Intel Kabylake I2S Machine Driver with RT5660 Codec 6 * 7 * Modified from: 8 * Intel Kabylake I2S Machine driver supporting MAXIM98357a and 9 * DA7219 codecs 10 * Also referred to: 11 * Intel Broadwell I2S Machine driver supporting RT5677 codec 12 */ 13 14 #include <linux/module.h> 15 #include <linux/platform_device.h> 16 #include <linux/gpio/consumer.h> 17 #include <linux/acpi.h> 18 #include <sound/core.h> 19 #include <sound/jack.h> 20 #include <sound/pcm.h> 21 #include <sound/pcm_params.h> 22 #include <sound/soc.h> 23 24 #include "../../codecs/hdac_hdmi.h" 25 #include "../../codecs/rt5660.h" 26 27 #define KBL_RT5660_CODEC_DAI "rt5660-aif1" 28 #define DUAL_CHANNEL 2 29 30 static struct snd_soc_card *kabylake_audio_card; 31 static struct snd_soc_jack skylake_hdmi[3]; 32 static struct snd_soc_jack lineout_jack; 33 static struct snd_soc_jack mic_jack; 34 35 struct kbl_hdmi_pcm { 36 struct list_head head; 37 struct snd_soc_dai *codec_dai; 38 int device; 39 }; 40 41 struct kbl_codec_private { 42 struct gpio_desc *gpio_lo_mute; 43 struct list_head hdmi_pcm_list; 44 }; 45 46 enum { 47 KBL_DPCM_AUDIO_PB = 0, 48 KBL_DPCM_AUDIO_CP, 49 KBL_DPCM_AUDIO_HDMI1_PB, 50 KBL_DPCM_AUDIO_HDMI2_PB, 51 KBL_DPCM_AUDIO_HDMI3_PB, 52 }; 53 54 #define GPIO_LINEOUT_MUTE_INDEX 0 55 #define GPIO_LINEOUT_DET_INDEX 3 56 #define GPIO_LINEIN_DET_INDEX 4 57 58 static const struct acpi_gpio_params lineout_mute_gpio = { GPIO_LINEOUT_MUTE_INDEX, 0, true }; 59 static const struct acpi_gpio_params lineout_det_gpio = { GPIO_LINEOUT_DET_INDEX, 0, false }; 60 static const struct acpi_gpio_params mic_det_gpio = { GPIO_LINEIN_DET_INDEX, 0, false }; 61 62 63 static const struct acpi_gpio_mapping acpi_rt5660_gpios[] = { 64 { "lineout-mute-gpios", &lineout_mute_gpio, 1 }, 65 { "lineout-det-gpios", &lineout_det_gpio, 1 }, 66 { "mic-det-gpios", &mic_det_gpio, 1 }, 67 { NULL }, 68 }; 69 70 static struct snd_soc_jack_pin lineout_jack_pin = { 71 .pin = "Line Out", 72 .mask = SND_JACK_LINEOUT, 73 }; 74 75 static struct snd_soc_jack_pin mic_jack_pin = { 76 .pin = "Line In", 77 .mask = SND_JACK_MICROPHONE, 78 }; 79 80 static struct snd_soc_jack_gpio lineout_jack_gpio = { 81 .name = "lineout-det", 82 .report = SND_JACK_LINEOUT, 83 .debounce_time = 200, 84 }; 85 86 static struct snd_soc_jack_gpio mic_jack_gpio = { 87 .name = "mic-det", 88 .report = SND_JACK_MICROPHONE, 89 .debounce_time = 200, 90 }; 91 92 static int kabylake_5660_event_lineout(struct snd_soc_dapm_widget *w, 93 struct snd_kcontrol *k, int event) 94 { 95 struct snd_soc_dapm_context *dapm = w->dapm; 96 struct kbl_codec_private *priv = snd_soc_card_get_drvdata(dapm->card); 97 98 gpiod_set_value_cansleep(priv->gpio_lo_mute, 99 !(SND_SOC_DAPM_EVENT_ON(event))); 100 101 return 0; 102 } 103 104 static const struct snd_kcontrol_new kabylake_rt5660_controls[] = { 105 SOC_DAPM_PIN_SWITCH("Line In"), 106 SOC_DAPM_PIN_SWITCH("Line Out"), 107 }; 108 109 static const struct snd_soc_dapm_widget kabylake_rt5660_widgets[] = { 110 SND_SOC_DAPM_MIC("Line In", NULL), 111 SND_SOC_DAPM_LINE("Line Out", kabylake_5660_event_lineout), 112 }; 113 114 static const struct snd_soc_dapm_route kabylake_rt5660_map[] = { 115 /* other jacks */ 116 {"IN1P", NULL, "Line In"}, 117 {"IN2P", NULL, "Line In"}, 118 {"Line Out", NULL, "LOUTR"}, 119 {"Line Out", NULL, "LOUTL"}, 120 121 /* CODEC BE connections */ 122 { "AIF1 Playback", NULL, "ssp0 Tx"}, 123 { "ssp0 Tx", NULL, "codec0_out"}, 124 125 { "codec0_in", NULL, "ssp0 Rx" }, 126 { "ssp0 Rx", NULL, "AIF1 Capture" }, 127 128 { "hifi1", NULL, "iDisp1 Tx"}, 129 { "iDisp1 Tx", NULL, "iDisp1_out"}, 130 { "hifi2", NULL, "iDisp2 Tx"}, 131 { "iDisp2 Tx", NULL, "iDisp2_out"}, 132 { "hifi3", NULL, "iDisp3 Tx"}, 133 { "iDisp3 Tx", NULL, "iDisp3_out"}, 134 }; 135 136 static int kabylake_ssp0_fixup(struct snd_soc_pcm_runtime *rtd, 137 struct snd_pcm_hw_params *params) 138 { 139 struct snd_interval *rate = hw_param_interval(params, 140 SNDRV_PCM_HW_PARAM_RATE); 141 struct snd_interval *chan = hw_param_interval(params, 142 SNDRV_PCM_HW_PARAM_CHANNELS); 143 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 144 145 /* The ADSP will convert the FE rate to 48k, stereo */ 146 rate->min = rate->max = 48000; 147 chan->min = chan->max = DUAL_CHANNEL; 148 149 /* set SSP0 to 24 bit */ 150 snd_mask_none(fmt); 151 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 152 153 return 0; 154 } 155 156 static int kabylake_rt5660_codec_init(struct snd_soc_pcm_runtime *rtd) 157 { 158 int ret; 159 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card); 160 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component; 161 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 162 163 ret = devm_acpi_dev_add_driver_gpios(component->dev, acpi_rt5660_gpios); 164 if (ret) 165 dev_warn(component->dev, "Failed to add driver gpios\n"); 166 167 /* Request rt5660 GPIO for lineout mute control, return if fails */ 168 ctx->gpio_lo_mute = gpiod_get(component->dev, "lineout-mute", 169 GPIOD_OUT_HIGH); 170 if (IS_ERR(ctx->gpio_lo_mute)) { 171 dev_err(component->dev, "Can't find GPIO_MUTE# gpio\n"); 172 return PTR_ERR(ctx->gpio_lo_mute); 173 } 174 175 /* Create and initialize headphone jack, this jack is not mandatory, don't return if fails */ 176 ret = snd_soc_card_jack_new(rtd->card, "Lineout Jack", 177 SND_JACK_LINEOUT, &lineout_jack, 178 &lineout_jack_pin, 1); 179 if (ret) 180 dev_warn(component->dev, "Can't create Lineout jack\n"); 181 else { 182 lineout_jack_gpio.gpiod_dev = component->dev; 183 ret = snd_soc_jack_add_gpios(&lineout_jack, 1, 184 &lineout_jack_gpio); 185 if (ret) 186 dev_warn(component->dev, "Can't add Lineout jack gpio\n"); 187 } 188 189 /* Create and initialize mic jack, this jack is not mandatory, don't return if fails */ 190 ret = snd_soc_card_jack_new(rtd->card, "Mic Jack", 191 SND_JACK_MICROPHONE, &mic_jack, 192 &mic_jack_pin, 1); 193 if (ret) 194 dev_warn(component->dev, "Can't create mic jack\n"); 195 else { 196 mic_jack_gpio.gpiod_dev = component->dev; 197 ret = snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio); 198 if (ret) 199 dev_warn(component->dev, "Can't add mic jack gpio\n"); 200 } 201 202 /* Here we enable some dapms in advance to reduce the pop noise for recording via line-in */ 203 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1"); 204 snd_soc_dapm_force_enable_pin(dapm, "BST1"); 205 snd_soc_dapm_force_enable_pin(dapm, "BST2"); 206 207 return 0; 208 } 209 210 static void kabylake_rt5660_codec_exit(struct snd_soc_pcm_runtime *rtd) 211 { 212 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card); 213 214 /* 215 * The .exit() can be reached without going through the .init() 216 * so explicitly test if the gpiod is valid 217 */ 218 if (!IS_ERR_OR_NULL(ctx->gpio_lo_mute)) 219 gpiod_put(ctx->gpio_lo_mute); 220 } 221 222 static int kabylake_hdmi_init(struct snd_soc_pcm_runtime *rtd, int device) 223 { 224 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card); 225 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0); 226 struct kbl_hdmi_pcm *pcm; 227 228 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL); 229 if (!pcm) 230 return -ENOMEM; 231 232 pcm->device = device; 233 pcm->codec_dai = dai; 234 235 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list); 236 237 return 0; 238 } 239 240 static int kabylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd) 241 { 242 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI1_PB); 243 } 244 245 static int kabylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd) 246 { 247 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI2_PB); 248 } 249 250 static int kabylake_hdmi3_init(struct snd_soc_pcm_runtime *rtd) 251 { 252 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI3_PB); 253 } 254 255 static int kabylake_rt5660_hw_params(struct snd_pcm_substream *substream, 256 struct snd_pcm_hw_params *params) 257 { 258 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 259 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 260 int ret; 261 262 ret = snd_soc_dai_set_sysclk(codec_dai, 263 RT5660_SCLK_S_PLL1, params_rate(params) * 512, 264 SND_SOC_CLOCK_IN); 265 if (ret < 0) { 266 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret); 267 return ret; 268 } 269 270 ret = snd_soc_dai_set_pll(codec_dai, 0, 271 RT5660_PLL1_S_BCLK, 272 params_rate(params) * 50, 273 params_rate(params) * 512); 274 if (ret < 0) 275 dev_err(codec_dai->dev, "can't set codec pll: %d\n", ret); 276 277 return ret; 278 } 279 280 static struct snd_soc_ops kabylake_rt5660_ops = { 281 .hw_params = kabylake_rt5660_hw_params, 282 }; 283 284 static const unsigned int rates[] = { 285 48000, 286 }; 287 288 static const struct snd_pcm_hw_constraint_list constraints_rates = { 289 .count = ARRAY_SIZE(rates), 290 .list = rates, 291 .mask = 0, 292 }; 293 294 static const unsigned int channels[] = { 295 DUAL_CHANNEL, 296 }; 297 298 static const struct snd_pcm_hw_constraint_list constraints_channels = { 299 .count = ARRAY_SIZE(channels), 300 .list = channels, 301 .mask = 0, 302 }; 303 304 static int kbl_fe_startup(struct snd_pcm_substream *substream) 305 { 306 struct snd_pcm_runtime *runtime = substream->runtime; 307 308 /* 309 * On this platform for PCM device we support, 310 * 48Khz 311 * stereo 312 * 16 bit audio 313 */ 314 315 runtime->hw.channels_max = DUAL_CHANNEL; 316 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 317 &constraints_channels); 318 319 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 320 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16); 321 322 snd_pcm_hw_constraint_list(runtime, 0, 323 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates); 324 325 return 0; 326 } 327 328 static const struct snd_soc_ops kabylake_rt5660_fe_ops = { 329 .startup = kbl_fe_startup, 330 }; 331 332 SND_SOC_DAILINK_DEF(dummy, 333 DAILINK_COMP_ARRAY(COMP_DUMMY())); 334 335 SND_SOC_DAILINK_DEF(system, 336 DAILINK_COMP_ARRAY(COMP_CPU("System Pin"))); 337 338 SND_SOC_DAILINK_DEF(hdmi1, 339 DAILINK_COMP_ARRAY(COMP_CPU("HDMI1 Pin"))); 340 341 SND_SOC_DAILINK_DEF(hdmi2, 342 DAILINK_COMP_ARRAY(COMP_CPU("HDMI2 Pin"))); 343 344 SND_SOC_DAILINK_DEF(hdmi3, 345 DAILINK_COMP_ARRAY(COMP_CPU("HDMI3 Pin"))); 346 347 SND_SOC_DAILINK_DEF(ssp0_pin, 348 DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin"))); 349 SND_SOC_DAILINK_DEF(ssp0_codec, 350 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC3277:00", KBL_RT5660_CODEC_DAI))); 351 352 SND_SOC_DAILINK_DEF(idisp1_pin, 353 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin"))); 354 SND_SOC_DAILINK_DEF(idisp1_codec, 355 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1"))); 356 357 SND_SOC_DAILINK_DEF(idisp2_pin, 358 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin"))); 359 SND_SOC_DAILINK_DEF(idisp2_codec, 360 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2"))); 361 362 SND_SOC_DAILINK_DEF(idisp3_pin, 363 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin"))); 364 SND_SOC_DAILINK_DEF(idisp3_codec, 365 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3"))); 366 367 SND_SOC_DAILINK_DEF(platform, 368 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3"))); 369 370 /* kabylake digital audio interface glue - connects rt5660 codec <--> CPU */ 371 static struct snd_soc_dai_link kabylake_rt5660_dais[] = { 372 /* Front End DAI links */ 373 [KBL_DPCM_AUDIO_PB] = { 374 .name = "Kbl Audio Port", 375 .stream_name = "Audio", 376 .dynamic = 1, 377 .nonatomic = 1, 378 .trigger = { 379 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 380 .dpcm_playback = 1, 381 .ops = &kabylake_rt5660_fe_ops, 382 SND_SOC_DAILINK_REG(system, dummy, platform), 383 }, 384 [KBL_DPCM_AUDIO_CP] = { 385 .name = "Kbl Audio Capture Port", 386 .stream_name = "Audio Record", 387 .dynamic = 1, 388 .nonatomic = 1, 389 .trigger = { 390 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 391 .dpcm_capture = 1, 392 .ops = &kabylake_rt5660_fe_ops, 393 SND_SOC_DAILINK_REG(system, dummy, platform), 394 }, 395 [KBL_DPCM_AUDIO_HDMI1_PB] = { 396 .name = "Kbl HDMI Port1", 397 .stream_name = "Hdmi1", 398 .dpcm_playback = 1, 399 .init = NULL, 400 .trigger = { 401 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 402 .nonatomic = 1, 403 .dynamic = 1, 404 SND_SOC_DAILINK_REG(hdmi1, dummy, platform), 405 }, 406 [KBL_DPCM_AUDIO_HDMI2_PB] = { 407 .name = "Kbl HDMI Port2", 408 .stream_name = "Hdmi2", 409 .dpcm_playback = 1, 410 .init = NULL, 411 .trigger = { 412 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 413 .nonatomic = 1, 414 .dynamic = 1, 415 SND_SOC_DAILINK_REG(hdmi2, dummy, platform), 416 }, 417 [KBL_DPCM_AUDIO_HDMI3_PB] = { 418 .name = "Kbl HDMI Port3", 419 .stream_name = "Hdmi3", 420 .trigger = { 421 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 422 .dpcm_playback = 1, 423 .init = NULL, 424 .nonatomic = 1, 425 .dynamic = 1, 426 SND_SOC_DAILINK_REG(hdmi3, dummy, platform), 427 }, 428 429 /* Back End DAI links */ 430 { 431 /* SSP0 - Codec */ 432 .name = "SSP0-Codec", 433 .id = 0, 434 .no_pcm = 1, 435 .init = kabylake_rt5660_codec_init, 436 .exit = kabylake_rt5660_codec_exit, 437 .dai_fmt = SND_SOC_DAIFMT_I2S | 438 SND_SOC_DAIFMT_NB_NF | 439 SND_SOC_DAIFMT_CBS_CFS, 440 .ignore_pmdown_time = 1, 441 .be_hw_params_fixup = kabylake_ssp0_fixup, 442 .ops = &kabylake_rt5660_ops, 443 .dpcm_playback = 1, 444 .dpcm_capture = 1, 445 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform), 446 }, 447 { 448 .name = "iDisp1", 449 .id = 1, 450 .dpcm_playback = 1, 451 .init = kabylake_hdmi1_init, 452 .no_pcm = 1, 453 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform), 454 }, 455 { 456 .name = "iDisp2", 457 .id = 2, 458 .init = kabylake_hdmi2_init, 459 .dpcm_playback = 1, 460 .no_pcm = 1, 461 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform), 462 }, 463 { 464 .name = "iDisp3", 465 .id = 3, 466 .init = kabylake_hdmi3_init, 467 .dpcm_playback = 1, 468 .no_pcm = 1, 469 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform), 470 }, 471 }; 472 473 474 #define NAME_SIZE 32 475 static int kabylake_card_late_probe(struct snd_soc_card *card) 476 { 477 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(card); 478 struct kbl_hdmi_pcm *pcm; 479 struct snd_soc_component *component = NULL; 480 int err, i = 0; 481 char jack_name[NAME_SIZE]; 482 483 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) { 484 component = pcm->codec_dai->component; 485 snprintf(jack_name, sizeof(jack_name), 486 "HDMI/DP, pcm=%d Jack", pcm->device); 487 err = snd_soc_card_jack_new(card, jack_name, 488 SND_JACK_AVOUT, &skylake_hdmi[i], 489 NULL, 0); 490 491 if (err) 492 return err; 493 494 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device, 495 &skylake_hdmi[i]); 496 if (err < 0) 497 return err; 498 499 i++; 500 501 } 502 503 if (!component) 504 return -EINVAL; 505 506 return hdac_hdmi_jack_port_init(component, &card->dapm); 507 } 508 509 /* kabylake audio machine driver for rt5660 */ 510 static struct snd_soc_card kabylake_audio_card_rt5660 = { 511 .name = "kblrt5660", 512 .owner = THIS_MODULE, 513 .dai_link = kabylake_rt5660_dais, 514 .num_links = ARRAY_SIZE(kabylake_rt5660_dais), 515 .controls = kabylake_rt5660_controls, 516 .num_controls = ARRAY_SIZE(kabylake_rt5660_controls), 517 .dapm_widgets = kabylake_rt5660_widgets, 518 .num_dapm_widgets = ARRAY_SIZE(kabylake_rt5660_widgets), 519 .dapm_routes = kabylake_rt5660_map, 520 .num_dapm_routes = ARRAY_SIZE(kabylake_rt5660_map), 521 .fully_routed = true, 522 .late_probe = kabylake_card_late_probe, 523 }; 524 525 static int kabylake_audio_probe(struct platform_device *pdev) 526 { 527 struct kbl_codec_private *ctx; 528 529 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); 530 if (!ctx) 531 return -ENOMEM; 532 533 INIT_LIST_HEAD(&ctx->hdmi_pcm_list); 534 535 kabylake_audio_card = 536 (struct snd_soc_card *)pdev->id_entry->driver_data; 537 538 kabylake_audio_card->dev = &pdev->dev; 539 snd_soc_card_set_drvdata(kabylake_audio_card, ctx); 540 return devm_snd_soc_register_card(&pdev->dev, kabylake_audio_card); 541 } 542 543 static const struct platform_device_id kbl_board_ids[] = { 544 { 545 .name = "kbl_rt5660", 546 .driver_data = 547 (kernel_ulong_t)&kabylake_audio_card_rt5660, 548 }, 549 { } 550 }; 551 552 static struct platform_driver kabylake_audio = { 553 .probe = kabylake_audio_probe, 554 .driver = { 555 .name = "kbl_rt5660", 556 .pm = &snd_soc_pm_ops, 557 }, 558 .id_table = kbl_board_ids, 559 }; 560 561 module_platform_driver(kabylake_audio) 562 563 /* Module information */ 564 MODULE_DESCRIPTION("Audio Machine driver-RT5660 in I2S mode"); 565 MODULE_AUTHOR("Hui Wang <hui.wang@canonical.com>"); 566 MODULE_LICENSE("GPL v2"); 567 MODULE_ALIAS("platform:kbl_rt5660"); 568