1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Machine driver for AMD Vangogh platform using either 4 * NAU8821 & CS35L41 or NAU8821 & MAX98388 codecs. 5 * 6 * Copyright 2021 Advanced Micro Devices, Inc. 7 */ 8 9 #include <linux/acpi.h> 10 #include <linux/dmi.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/i2c.h> 13 #include <linux/input-event-codes.h> 14 #include <linux/module.h> 15 #include <sound/jack.h> 16 #include <sound/pcm_params.h> 17 #include <sound/soc.h> 18 #include <sound/soc-dapm.h> 19 20 #include "../../codecs/nau8821.h" 21 #include "acp5x.h" 22 23 #define DRV_NAME "acp5x_mach" 24 #define DUAL_CHANNEL 2 25 #define ACP5X_NAU8821_BCLK 3072000 26 #define ACP5X_NAU8821_FREQ_OUT 12288000 27 #define ACP5X_NAU8821_COMP_NAME "i2c-NVTN2020:00" 28 #define ACP5X_NAU8821_DAI_NAME "nau8821-hifi" 29 #define ACP5X_CS35L41_COMP_LNAME "spi-VLV1776:00" 30 #define ACP5X_CS35L41_COMP_RNAME "spi-VLV1776:01" 31 #define ACP5X_CS35L41_DAI_NAME "cs35l41-pcm" 32 #define ACP5X_MAX98388_COMP_LNAME "i2c-ADS8388:00" 33 #define ACP5X_MAX98388_COMP_RNAME "i2c-ADS8388:01" 34 #define ACP5X_MAX98388_DAI_NAME "max98388-aif1" 35 36 static struct snd_soc_jack vg_headset; 37 38 SND_SOC_DAILINK_DEF(platform, DAILINK_COMP_ARRAY(COMP_PLATFORM("acp5x_i2s_dma.0"))); 39 SND_SOC_DAILINK_DEF(acp5x_i2s, DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.0"))); 40 SND_SOC_DAILINK_DEF(acp5x_bt, DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.1"))); 41 SND_SOC_DAILINK_DEF(nau8821, DAILINK_COMP_ARRAY(COMP_CODEC(ACP5X_NAU8821_COMP_NAME, 42 ACP5X_NAU8821_DAI_NAME))); 43 44 static struct snd_soc_jack_pin acp5x_nau8821_jack_pins[] = { 45 { 46 .pin = "Headphone", 47 .mask = SND_JACK_HEADPHONE, 48 }, 49 { 50 .pin = "Headset Mic", 51 .mask = SND_JACK_MICROPHONE, 52 }, 53 }; 54 55 static const struct snd_kcontrol_new acp5x_8821_controls[] = { 56 SOC_DAPM_PIN_SWITCH("Headphone"), 57 SOC_DAPM_PIN_SWITCH("Headset Mic"), 58 SOC_DAPM_PIN_SWITCH("Int Mic"), 59 }; 60 61 static int platform_clock_control(struct snd_soc_dapm_widget *w, 62 struct snd_kcontrol *k, int event) 63 { 64 struct snd_soc_dapm_context *dapm = w->dapm; 65 struct snd_soc_card *card = dapm->card; 66 struct snd_soc_dai *dai; 67 int ret = 0; 68 69 dai = snd_soc_card_get_codec_dai(card, ACP5X_NAU8821_DAI_NAME); 70 if (!dai) { 71 dev_err(card->dev, "Codec dai not found\n"); 72 return -EIO; 73 } 74 75 if (SND_SOC_DAPM_EVENT_OFF(event)) { 76 ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN); 77 if (ret < 0) { 78 dev_err(card->dev, "set sysclk err = %d\n", ret); 79 return -EIO; 80 } 81 } else { 82 ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_FLL_BLK, 0, SND_SOC_CLOCK_IN); 83 if (ret < 0) 84 dev_err(dai->dev, "can't set BLK clock %d\n", ret); 85 ret = snd_soc_dai_set_pll(dai, 0, 0, ACP5X_NAU8821_BCLK, ACP5X_NAU8821_FREQ_OUT); 86 if (ret < 0) 87 dev_err(dai->dev, "can't set FLL: %d\n", ret); 88 } 89 90 return ret; 91 } 92 93 static int acp5x_8821_init(struct snd_soc_pcm_runtime *rtd) 94 { 95 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component; 96 int ret; 97 98 /* 99 * Headset buttons map to the google Reference headset. 100 * These can be configured by userspace. 101 */ 102 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack", 103 SND_JACK_HEADSET | SND_JACK_BTN_0, 104 &vg_headset, acp5x_nau8821_jack_pins, 105 ARRAY_SIZE(acp5x_nau8821_jack_pins)); 106 if (ret) { 107 dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret); 108 return ret; 109 } 110 111 snd_jack_set_key(vg_headset.jack, SND_JACK_BTN_0, KEY_MEDIA); 112 nau8821_enable_jack_detect(component, &vg_headset); 113 114 return ret; 115 } 116 117 static const unsigned int rates[] = { 118 48000, 119 }; 120 121 static const struct snd_pcm_hw_constraint_list constraints_rates = { 122 .count = ARRAY_SIZE(rates), 123 .list = rates, 124 .mask = 0, 125 }; 126 127 static const unsigned int channels[] = { 128 2, 129 }; 130 131 static const struct snd_pcm_hw_constraint_list constraints_channels = { 132 .count = ARRAY_SIZE(channels), 133 .list = channels, 134 .mask = 0, 135 }; 136 137 static const unsigned int acp5x_nau8821_format[] = {32}; 138 139 static struct snd_pcm_hw_constraint_list constraints_sample_bits = { 140 .list = acp5x_nau8821_format, 141 .count = ARRAY_SIZE(acp5x_nau8821_format), 142 }; 143 144 static int acp5x_8821_startup(struct snd_pcm_substream *substream) 145 { 146 struct snd_pcm_runtime *runtime = substream->runtime; 147 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 148 struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(rtd->card); 149 150 machine->play_i2s_instance = I2S_SP_INSTANCE; 151 machine->cap_i2s_instance = I2S_SP_INSTANCE; 152 153 runtime->hw.channels_max = DUAL_CHANNEL; 154 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 155 &constraints_channels); 156 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 157 &constraints_rates); 158 snd_pcm_hw_constraint_list(substream->runtime, 0, 159 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 160 &constraints_sample_bits); 161 162 return 0; 163 } 164 165 static int acp5x_nau8821_hw_params(struct snd_pcm_substream *substream, 166 struct snd_pcm_hw_params *params) 167 { 168 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 169 struct snd_soc_card *card = rtd->card; 170 struct snd_soc_dai *dai = snd_soc_card_get_codec_dai(card, ACP5X_NAU8821_DAI_NAME); 171 int ret, bclk; 172 173 ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_FLL_BLK, 0, SND_SOC_CLOCK_IN); 174 if (ret < 0) 175 dev_err(card->dev, "can't set FS clock %d\n", ret); 176 177 bclk = snd_soc_params_to_bclk(params); 178 if (bclk < 0) { 179 dev_err(dai->dev, "Fail to get BCLK rate: %d\n", bclk); 180 return bclk; 181 } 182 183 ret = snd_soc_dai_set_pll(dai, 0, 0, bclk, params_rate(params) * 256); 184 if (ret < 0) 185 dev_err(card->dev, "can't set FLL: %d\n", ret); 186 187 return ret; 188 } 189 190 static const struct snd_soc_ops acp5x_8821_ops = { 191 .startup = acp5x_8821_startup, 192 .hw_params = acp5x_nau8821_hw_params, 193 }; 194 195 static int acp5x_cs35l41_startup(struct snd_pcm_substream *substream) 196 { 197 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 198 struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(rtd->card); 199 struct snd_pcm_runtime *runtime = substream->runtime; 200 201 machine->play_i2s_instance = I2S_HS_INSTANCE; 202 203 runtime->hw.channels_max = DUAL_CHANNEL; 204 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 205 &constraints_channels); 206 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 207 &constraints_rates); 208 209 return 0; 210 } 211 212 static int acp5x_cs35l41_hw_params(struct snd_pcm_substream *substream, 213 struct snd_pcm_hw_params *params) 214 { 215 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 216 unsigned int bclk, rate = params_rate(params); 217 struct snd_soc_component *comp; 218 int ret, i; 219 220 switch (rate) { 221 case 48000: 222 bclk = 1536000; 223 break; 224 default: 225 bclk = 0; 226 break; 227 } 228 229 for_each_rtd_components(rtd, i, comp) { 230 if (!(strcmp(comp->name, ACP5X_CS35L41_COMP_LNAME)) || 231 !(strcmp(comp->name, ACP5X_CS35L41_COMP_RNAME))) { 232 if (!bclk) { 233 dev_err(comp->dev, "Invalid sample rate: 0x%x\n", rate); 234 return -EINVAL; 235 } 236 237 ret = snd_soc_component_set_sysclk(comp, 0, 0, bclk, SND_SOC_CLOCK_IN); 238 if (ret) { 239 dev_err(comp->dev, "failed to set SYSCLK: %d\n", ret); 240 return ret; 241 } 242 } 243 } 244 245 return 0; 246 } 247 248 static const struct snd_soc_ops acp5x_cs35l41_play_ops = { 249 .startup = acp5x_cs35l41_startup, 250 .hw_params = acp5x_cs35l41_hw_params, 251 }; 252 253 static struct snd_soc_codec_conf acp5x_cs35l41_conf[] = { 254 { 255 .dlc = COMP_CODEC_CONF(ACP5X_CS35L41_COMP_LNAME), 256 .name_prefix = "Left", 257 }, 258 { 259 .dlc = COMP_CODEC_CONF(ACP5X_CS35L41_COMP_RNAME), 260 .name_prefix = "Right", 261 }, 262 }; 263 264 SND_SOC_DAILINK_DEF(cs35l41, DAILINK_COMP_ARRAY(COMP_CODEC(ACP5X_CS35L41_COMP_LNAME, 265 ACP5X_CS35L41_DAI_NAME), 266 COMP_CODEC(ACP5X_CS35L41_COMP_RNAME, 267 ACP5X_CS35L41_DAI_NAME))); 268 269 static struct snd_soc_dai_link acp5x_8821_35l41_dai[] = { 270 { 271 .name = "acp5x-8821-play", 272 .stream_name = "Playback/Capture", 273 .dai_fmt = SND_SOC_DAIFMT_I2S | 274 SND_SOC_DAIFMT_NB_NF | 275 SND_SOC_DAIFMT_CBC_CFC, 276 .dpcm_playback = 1, 277 .dpcm_capture = 1, 278 .ops = &acp5x_8821_ops, 279 .init = acp5x_8821_init, 280 SND_SOC_DAILINK_REG(acp5x_i2s, nau8821, platform), 281 }, 282 { 283 .name = "acp5x-CS35L41-Stereo", 284 .stream_name = "CS35L41 Stereo Playback", 285 .dai_fmt = SND_SOC_DAIFMT_I2S | 286 SND_SOC_DAIFMT_NB_NF | 287 SND_SOC_DAIFMT_CBC_CFC, 288 .dpcm_playback = 1, 289 .playback_only = 1, 290 .ops = &acp5x_cs35l41_play_ops, 291 SND_SOC_DAILINK_REG(acp5x_bt, cs35l41, platform), 292 }, 293 }; 294 295 static const struct snd_soc_dapm_widget acp5x_8821_35l41_widgets[] = { 296 SND_SOC_DAPM_HP("Headphone", NULL), 297 SND_SOC_DAPM_MIC("Headset Mic", NULL), 298 SND_SOC_DAPM_MIC("Int Mic", NULL), 299 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 300 platform_clock_control, 301 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 302 }; 303 304 static const struct snd_soc_dapm_route acp5x_8821_35l41_audio_route[] = { 305 /* HP jack connectors - unknown if we have jack detection */ 306 { "Headphone", NULL, "HPOL" }, 307 { "Headphone", NULL, "HPOR" }, 308 { "MICL", NULL, "Headset Mic" }, 309 { "MICR", NULL, "Headset Mic" }, 310 { "DMIC", NULL, "Int Mic" }, 311 312 { "Headphone", NULL, "Platform Clock" }, 313 { "Headset Mic", NULL, "Platform Clock" }, 314 { "Int Mic", NULL, "Platform Clock" }, 315 }; 316 317 static struct snd_soc_card acp5x_8821_35l41_card = { 318 .name = "acp5x", 319 .owner = THIS_MODULE, 320 .dai_link = acp5x_8821_35l41_dai, 321 .num_links = ARRAY_SIZE(acp5x_8821_35l41_dai), 322 .dapm_widgets = acp5x_8821_35l41_widgets, 323 .num_dapm_widgets = ARRAY_SIZE(acp5x_8821_35l41_widgets), 324 .dapm_routes = acp5x_8821_35l41_audio_route, 325 .num_dapm_routes = ARRAY_SIZE(acp5x_8821_35l41_audio_route), 326 .codec_conf = acp5x_cs35l41_conf, 327 .num_configs = ARRAY_SIZE(acp5x_cs35l41_conf), 328 .controls = acp5x_8821_controls, 329 .num_controls = ARRAY_SIZE(acp5x_8821_controls), 330 }; 331 332 static int acp5x_max98388_startup(struct snd_pcm_substream *substream) 333 { 334 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 335 struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(rtd->card); 336 struct snd_pcm_runtime *runtime = substream->runtime; 337 338 machine->play_i2s_instance = I2S_HS_INSTANCE; 339 340 runtime->hw.channels_max = DUAL_CHANNEL; 341 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 342 &constraints_channels); 343 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 344 &constraints_rates); 345 return 0; 346 } 347 348 static const struct snd_soc_ops acp5x_max98388_play_ops = { 349 .startup = acp5x_max98388_startup, 350 }; 351 352 static struct snd_soc_codec_conf acp5x_max98388_conf[] = { 353 { 354 .dlc = COMP_CODEC_CONF(ACP5X_MAX98388_COMP_LNAME), 355 .name_prefix = "Left", 356 }, 357 { 358 .dlc = COMP_CODEC_CONF(ACP5X_MAX98388_COMP_RNAME), 359 .name_prefix = "Right", 360 }, 361 }; 362 363 SND_SOC_DAILINK_DEF(max98388, DAILINK_COMP_ARRAY(COMP_CODEC(ACP5X_MAX98388_COMP_LNAME, 364 ACP5X_MAX98388_DAI_NAME), 365 COMP_CODEC(ACP5X_MAX98388_COMP_RNAME, 366 ACP5X_MAX98388_DAI_NAME))); 367 368 static struct snd_soc_dai_link acp5x_8821_98388_dai[] = { 369 { 370 .name = "acp5x-8821-play", 371 .stream_name = "Playback/Capture", 372 .dai_fmt = SND_SOC_DAIFMT_I2S | 373 SND_SOC_DAIFMT_NB_NF | 374 SND_SOC_DAIFMT_CBC_CFC, 375 .dpcm_playback = 1, 376 .dpcm_capture = 1, 377 .ops = &acp5x_8821_ops, 378 .init = acp5x_8821_init, 379 SND_SOC_DAILINK_REG(acp5x_i2s, nau8821, platform), 380 }, 381 { 382 .name = "acp5x-max98388-play", 383 .stream_name = "MAX98388 Playback", 384 .dai_fmt = SND_SOC_DAIFMT_I2S | 385 SND_SOC_DAIFMT_NB_NF | 386 SND_SOC_DAIFMT_CBC_CFC, 387 .dpcm_playback = 1, 388 .playback_only = 1, 389 .ops = &acp5x_max98388_play_ops, 390 SND_SOC_DAILINK_REG(acp5x_bt, max98388, platform), 391 }, 392 }; 393 394 static const struct snd_soc_dapm_widget acp5x_8821_98388_widgets[] = { 395 SND_SOC_DAPM_HP("Headphone", NULL), 396 SND_SOC_DAPM_MIC("Headset Mic", NULL), 397 SND_SOC_DAPM_MIC("Int Mic", NULL), 398 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 399 platform_clock_control, 400 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 401 SND_SOC_DAPM_SPK("SPK", NULL), 402 }; 403 404 static const struct snd_soc_dapm_route acp5x_8821_98388_route[] = { 405 { "Headphone", NULL, "HPOL" }, 406 { "Headphone", NULL, "HPOR" }, 407 { "MICL", NULL, "Headset Mic" }, 408 { "MICR", NULL, "Headset Mic" }, 409 { "DMIC", NULL, "Int Mic" }, 410 411 { "Headphone", NULL, "Platform Clock" }, 412 { "Headset Mic", NULL, "Platform Clock" }, 413 { "Int Mic", NULL, "Platform Clock" }, 414 415 { "SPK", NULL, "Left BE_OUT" }, 416 { "SPK", NULL, "Right BE_OUT" }, 417 }; 418 419 static struct snd_soc_card acp5x_8821_98388_card = { 420 .name = "acp5x-max98388", 421 .owner = THIS_MODULE, 422 .dai_link = acp5x_8821_98388_dai, 423 .num_links = ARRAY_SIZE(acp5x_8821_98388_dai), 424 .dapm_widgets = acp5x_8821_98388_widgets, 425 .num_dapm_widgets = ARRAY_SIZE(acp5x_8821_98388_widgets), 426 .dapm_routes = acp5x_8821_98388_route, 427 .num_dapm_routes = ARRAY_SIZE(acp5x_8821_98388_route), 428 .codec_conf = acp5x_max98388_conf, 429 .num_configs = ARRAY_SIZE(acp5x_max98388_conf), 430 .controls = acp5x_8821_controls, 431 .num_controls = ARRAY_SIZE(acp5x_8821_controls), 432 }; 433 434 static const struct dmi_system_id acp5x_vg_quirk_table[] = { 435 { 436 .matches = { 437 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Valve"), 438 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jupiter"), 439 } 440 }, 441 {} 442 }; 443 444 static int acp5x_probe(struct platform_device *pdev) 445 { 446 const struct dmi_system_id *dmi_id; 447 struct acp5x_platform_info *machine; 448 struct device *dev = &pdev->dev; 449 struct snd_soc_card *card; 450 int ret; 451 452 card = (struct snd_soc_card *)device_get_match_data(dev); 453 if (!card) { 454 /* 455 * This is normally the result of directly probing the driver 456 * in pci-acp5x through platform_device_register_full(), which 457 * is necessary for the CS35L41 variant, as it doesn't support 458 * ACPI probing and relies on DMI quirks. 459 */ 460 dmi_id = dmi_first_match(acp5x_vg_quirk_table); 461 if (!dmi_id) 462 return -ENODEV; 463 464 card = &acp5x_8821_35l41_card; 465 } 466 467 machine = devm_kzalloc(dev, sizeof(*machine), GFP_KERNEL); 468 if (!machine) 469 return -ENOMEM; 470 471 card->dev = dev; 472 platform_set_drvdata(pdev, card); 473 snd_soc_card_set_drvdata(card, machine); 474 475 ret = devm_snd_soc_register_card(dev, card); 476 if (ret) 477 return dev_err_probe(dev, ret, "Register card (%s) failed\n", card->name); 478 479 return 0; 480 } 481 482 static const struct acpi_device_id acp5x_acpi_match[] = { 483 { "AMDI8821", (kernel_ulong_t)&acp5x_8821_98388_card }, 484 {}, 485 }; 486 MODULE_DEVICE_TABLE(acpi, acp5x_acpi_match); 487 488 static struct platform_driver acp5x_mach_driver = { 489 .driver = { 490 .name = DRV_NAME, 491 .pm = &snd_soc_pm_ops, 492 .acpi_match_table = acp5x_acpi_match, 493 }, 494 .probe = acp5x_probe, 495 }; 496 497 module_platform_driver(acp5x_mach_driver); 498 499 MODULE_AUTHOR("Vijendar.Mukunda@amd.com"); 500 MODULE_DESCRIPTION("NAU8821/CS35L41 & NAU8821/MAX98388 audio support"); 501 MODULE_LICENSE("GPL v2"); 502 MODULE_ALIAS("platform:" DRV_NAME); 503