acp3x-rt5682-max9836.c (15289B)
1// SPDX-License-Identifier: GPL-2.0+ 2// 3// Machine driver for AMD ACP Audio engine using DA7219 & MAX98357 codec. 4// 5//Copyright 2016 Advanced Micro Devices, Inc. 6 7#include <sound/core.h> 8#include <sound/soc.h> 9#include <sound/pcm.h> 10#include <sound/pcm_params.h> 11#include <sound/soc-dapm.h> 12#include <sound/jack.h> 13#include <linux/clk.h> 14#include <linux/gpio.h> 15#include <linux/gpio/consumer.h> 16#include <linux/module.h> 17#include <linux/i2c.h> 18#include <linux/input.h> 19#include <linux/io.h> 20#include <linux/acpi.h> 21 22#include "raven/acp3x.h" 23#include "../codecs/rt5682.h" 24#include "../codecs/rt1015.h" 25 26#define PCO_PLAT_CLK 48000000 27#define RT5682_PLL_FREQ (48000 * 512) 28#define DUAL_CHANNEL 2 29 30static struct snd_soc_jack pco_jack; 31static struct clk *rt5682_dai_wclk; 32static struct clk *rt5682_dai_bclk; 33static struct gpio_desc *dmic_sel; 34void *soc_is_rltk_max(struct device *dev); 35 36enum { 37 RT5682 = 0, 38 MAX, 39 EC, 40}; 41 42static int acp3x_5682_init(struct snd_soc_pcm_runtime *rtd) 43{ 44 int ret; 45 struct snd_soc_card *card = rtd->card; 46 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 47 struct snd_soc_component *component = codec_dai->component; 48 49 dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name); 50 51 /* set rt5682 dai fmt */ 52 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S 53 | SND_SOC_DAIFMT_NB_NF 54 | SND_SOC_DAIFMT_CBP_CFP); 55 if (ret < 0) { 56 dev_err(rtd->card->dev, 57 "Failed to set rt5682 dai fmt: %d\n", ret); 58 return ret; 59 } 60 61 /* set codec PLL */ 62 ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL2, RT5682_PLL2_S_MCLK, 63 PCO_PLAT_CLK, RT5682_PLL_FREQ); 64 if (ret < 0) { 65 dev_err(rtd->dev, "can't set rt5682 PLL: %d\n", ret); 66 return ret; 67 } 68 69 /* Set codec sysclk */ 70 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL2, 71 RT5682_PLL_FREQ, SND_SOC_CLOCK_IN); 72 if (ret < 0) { 73 dev_err(rtd->dev, 74 "Failed to set rt5682 SYSCLK: %d\n", ret); 75 return ret; 76 } 77 78 /* Set tdm/i2s1 master bclk ratio */ 79 ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64); 80 if (ret < 0) { 81 dev_err(rtd->dev, 82 "Failed to set rt5682 tdm bclk ratio: %d\n", ret); 83 return ret; 84 } 85 86 rt5682_dai_wclk = clk_get(component->dev, "rt5682-dai-wclk"); 87 rt5682_dai_bclk = clk_get(component->dev, "rt5682-dai-bclk"); 88 89 ret = snd_soc_card_jack_new(card, "Headset Jack", 90 SND_JACK_HEADSET | SND_JACK_LINEOUT | 91 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 92 SND_JACK_BTN_2 | SND_JACK_BTN_3, 93 &pco_jack); 94 if (ret) { 95 dev_err(card->dev, "HP jack creation failed %d\n", ret); 96 return ret; 97 } 98 99 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 100 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 101 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 102 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 103 104 ret = snd_soc_component_set_jack(component, &pco_jack, NULL); 105 if (ret) { 106 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret); 107 return ret; 108 } 109 110 return ret; 111} 112 113static int rt5682_clk_enable(struct snd_pcm_substream *substream) 114{ 115 int ret = 0; 116 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 117 118 /* RT5682 will support only 48K output with 48M mclk */ 119 clk_set_rate(rt5682_dai_wclk, 48000); 120 clk_set_rate(rt5682_dai_bclk, 48000 * 64); 121 ret = clk_prepare_enable(rt5682_dai_wclk); 122 if (ret < 0) { 123 dev_err(rtd->dev, "can't enable wclk %d\n", ret); 124 return ret; 125 } 126 127 return ret; 128} 129 130static int acp3x_1015_hw_params(struct snd_pcm_substream *substream, 131 struct snd_pcm_hw_params *params) 132{ 133 struct snd_soc_pcm_runtime *rtd = substream->private_data; 134 struct snd_soc_dai *codec_dai; 135 int srate, i, ret; 136 137 ret = 0; 138 srate = params_rate(params); 139 140 for_each_rtd_codec_dais(rtd, i, codec_dai) { 141 if (strcmp(codec_dai->name, "rt1015-aif")) 142 continue; 143 144 ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK, 145 64 * srate, 256 * srate); 146 if (ret < 0) 147 return ret; 148 ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL, 149 256 * srate, SND_SOC_CLOCK_IN); 150 if (ret < 0) 151 return ret; 152 } 153 return ret; 154} 155 156static void rt5682_clk_disable(void) 157{ 158 clk_disable_unprepare(rt5682_dai_wclk); 159} 160 161static const unsigned int channels[] = { 162 DUAL_CHANNEL, 163}; 164 165static const unsigned int rates[] = { 166 48000, 167}; 168 169static const struct snd_pcm_hw_constraint_list constraints_rates = { 170 .count = ARRAY_SIZE(rates), 171 .list = rates, 172 .mask = 0, 173}; 174 175static const struct snd_pcm_hw_constraint_list constraints_channels = { 176 .count = ARRAY_SIZE(channels), 177 .list = channels, 178 .mask = 0, 179}; 180 181static int acp3x_5682_startup(struct snd_pcm_substream *substream) 182{ 183 struct snd_pcm_runtime *runtime = substream->runtime; 184 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 185 struct snd_soc_card *card = rtd->card; 186 struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card); 187 188 machine->play_i2s_instance = I2S_SP_INSTANCE; 189 machine->cap_i2s_instance = I2S_SP_INSTANCE; 190 191 runtime->hw.channels_max = DUAL_CHANNEL; 192 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 193 &constraints_channels); 194 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 195 &constraints_rates); 196 return rt5682_clk_enable(substream); 197} 198 199static int acp3x_max_startup(struct snd_pcm_substream *substream) 200{ 201 struct snd_pcm_runtime *runtime = substream->runtime; 202 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 203 struct snd_soc_card *card = rtd->card; 204 struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card); 205 206 machine->play_i2s_instance = I2S_BT_INSTANCE; 207 208 runtime->hw.channels_max = DUAL_CHANNEL; 209 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 210 &constraints_channels); 211 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 212 &constraints_rates); 213 return rt5682_clk_enable(substream); 214} 215 216static int acp3x_ec_dmic0_startup(struct snd_pcm_substream *substream) 217{ 218 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 219 struct snd_soc_card *card = rtd->card; 220 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 221 struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card); 222 223 machine->cap_i2s_instance = I2S_BT_INSTANCE; 224 snd_soc_dai_set_bclk_ratio(codec_dai, 64); 225 226 return rt5682_clk_enable(substream); 227} 228 229static int dmic_switch; 230 231static int dmic_get(struct snd_kcontrol *kcontrol, 232 struct snd_ctl_elem_value *ucontrol) 233{ 234 ucontrol->value.integer.value[0] = dmic_switch; 235 return 0; 236} 237 238static int dmic_set(struct snd_kcontrol *kcontrol, 239 struct snd_ctl_elem_value *ucontrol) 240{ 241 if (dmic_sel) { 242 dmic_switch = ucontrol->value.integer.value[0]; 243 gpiod_set_value(dmic_sel, dmic_switch); 244 } 245 return 0; 246} 247 248static void rt5682_shutdown(struct snd_pcm_substream *substream) 249{ 250 rt5682_clk_disable(); 251} 252 253static const struct snd_soc_ops acp3x_5682_ops = { 254 .startup = acp3x_5682_startup, 255 .shutdown = rt5682_shutdown, 256}; 257 258static const struct snd_soc_ops acp3x_max_play_ops = { 259 .startup = acp3x_max_startup, 260 .shutdown = rt5682_shutdown, 261 .hw_params = acp3x_1015_hw_params, 262}; 263 264static const struct snd_soc_ops acp3x_ec_cap0_ops = { 265 .startup = acp3x_ec_dmic0_startup, 266 .shutdown = rt5682_shutdown, 267}; 268 269SND_SOC_DAILINK_DEF(acp3x_i2s, 270 DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.0"))); 271SND_SOC_DAILINK_DEF(acp3x_bt, 272 DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.2"))); 273 274SND_SOC_DAILINK_DEF(rt5682, 275 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", "rt5682-aif1"))); 276SND_SOC_DAILINK_DEF(max, 277 DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", "HiFi"))); 278SND_SOC_DAILINK_DEF(rt1015p, 279 DAILINK_COMP_ARRAY(COMP_CODEC("RTL1015:00", "HiFi"))); 280SND_SOC_DAILINK_DEF(rt1015, 281 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC1015:00", "rt1015-aif"), 282 COMP_CODEC("i2c-10EC1015:01", "rt1015-aif"))); 283SND_SOC_DAILINK_DEF(cros_ec, 284 DAILINK_COMP_ARRAY(COMP_CODEC("GOOG0013:00", "EC Codec I2S RX"))); 285 286SND_SOC_DAILINK_DEF(platform, 287 DAILINK_COMP_ARRAY(COMP_PLATFORM("acp3x_rv_i2s_dma.0"))); 288 289static struct snd_soc_codec_conf rt1015_conf[] = { 290 { 291 .dlc = COMP_CODEC_CONF("i2c-10EC1015:00"), 292 .name_prefix = "Left", 293 }, 294 { 295 .dlc = COMP_CODEC_CONF("i2c-10EC1015:01"), 296 .name_prefix = "Right", 297 }, 298}; 299 300static struct snd_soc_dai_link acp3x_dai[] = { 301 [RT5682] = { 302 .name = "acp3x-5682-play", 303 .stream_name = "Playback", 304 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 305 | SND_SOC_DAIFMT_CBP_CFP, 306 .init = acp3x_5682_init, 307 .dpcm_playback = 1, 308 .dpcm_capture = 1, 309 .ops = &acp3x_5682_ops, 310 SND_SOC_DAILINK_REG(acp3x_i2s, rt5682, platform), 311 }, 312 [MAX] = { 313 .name = "acp3x-max98357-play", 314 .stream_name = "HiFi Playback", 315 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 316 | SND_SOC_DAIFMT_CBC_CFC, 317 .dpcm_playback = 1, 318 .ops = &acp3x_max_play_ops, 319 .cpus = acp3x_bt, 320 .num_cpus = ARRAY_SIZE(acp3x_bt), 321 .platforms = platform, 322 .num_platforms = ARRAY_SIZE(platform), 323 }, 324 [EC] = { 325 .name = "acp3x-ec-dmic0-capture", 326 .stream_name = "Capture DMIC0", 327 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 328 | SND_SOC_DAIFMT_CBC_CFC, 329 .dpcm_capture = 1, 330 .ops = &acp3x_ec_cap0_ops, 331 SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform), 332 }, 333}; 334 335static const char * const dmic_mux_text[] = { 336 "Front Mic", 337 "Rear Mic", 338}; 339 340static SOC_ENUM_SINGLE_DECL( 341 acp3x_dmic_enum, SND_SOC_NOPM, 0, dmic_mux_text); 342 343static const struct snd_kcontrol_new acp3x_dmic_mux_control = 344 SOC_DAPM_ENUM_EXT("DMIC Select Mux", acp3x_dmic_enum, 345 dmic_get, dmic_set); 346 347static const struct snd_soc_dapm_widget acp3x_5682_widgets[] = { 348 SND_SOC_DAPM_HP("Headphone Jack", NULL), 349 SND_SOC_DAPM_SPK("Spk", NULL), 350 SND_SOC_DAPM_MIC("Headset Mic", NULL), 351 SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, 352 &acp3x_dmic_mux_control), 353}; 354 355static const struct snd_soc_dapm_route acp3x_5682_audio_route[] = { 356 {"Headphone Jack", NULL, "HPOL"}, 357 {"Headphone Jack", NULL, "HPOR"}, 358 {"IN1P", NULL, "Headset Mic"}, 359 {"Spk", NULL, "Speaker"}, 360 {"Dmic Mux", "Front Mic", "DMIC"}, 361 {"Dmic Mux", "Rear Mic", "DMIC"}, 362}; 363 364static const struct snd_kcontrol_new acp3x_5682_mc_controls[] = { 365 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 366 SOC_DAPM_PIN_SWITCH("Spk"), 367 SOC_DAPM_PIN_SWITCH("Headset Mic"), 368}; 369 370static struct snd_soc_card acp3x_5682 = { 371 .name = "acp3xalc5682m98357", 372 .owner = THIS_MODULE, 373 .dai_link = acp3x_dai, 374 .num_links = ARRAY_SIZE(acp3x_dai), 375 .dapm_widgets = acp3x_5682_widgets, 376 .num_dapm_widgets = ARRAY_SIZE(acp3x_5682_widgets), 377 .dapm_routes = acp3x_5682_audio_route, 378 .num_dapm_routes = ARRAY_SIZE(acp3x_5682_audio_route), 379 .controls = acp3x_5682_mc_controls, 380 .num_controls = ARRAY_SIZE(acp3x_5682_mc_controls), 381}; 382 383static const struct snd_soc_dapm_widget acp3x_1015_widgets[] = { 384 SND_SOC_DAPM_HP("Headphone Jack", NULL), 385 SND_SOC_DAPM_MIC("Headset Mic", NULL), 386 SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, 387 &acp3x_dmic_mux_control), 388 SND_SOC_DAPM_SPK("Left Spk", NULL), 389 SND_SOC_DAPM_SPK("Right Spk", NULL), 390}; 391 392static const struct snd_soc_dapm_route acp3x_1015_route[] = { 393 {"Headphone Jack", NULL, "HPOL"}, 394 {"Headphone Jack", NULL, "HPOR"}, 395 {"IN1P", NULL, "Headset Mic"}, 396 {"Dmic Mux", "Front Mic", "DMIC"}, 397 {"Dmic Mux", "Rear Mic", "DMIC"}, 398 {"Left Spk", NULL, "Left SPO"}, 399 {"Right Spk", NULL, "Right SPO"}, 400}; 401 402static const struct snd_kcontrol_new acp3x_mc_1015_controls[] = { 403 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 404 SOC_DAPM_PIN_SWITCH("Headset Mic"), 405 SOC_DAPM_PIN_SWITCH("Left Spk"), 406 SOC_DAPM_PIN_SWITCH("Right Spk"), 407}; 408 409static struct snd_soc_card acp3x_1015 = { 410 .name = "acp3xalc56821015", 411 .owner = THIS_MODULE, 412 .dai_link = acp3x_dai, 413 .num_links = ARRAY_SIZE(acp3x_dai), 414 .dapm_widgets = acp3x_1015_widgets, 415 .num_dapm_widgets = ARRAY_SIZE(acp3x_1015_widgets), 416 .dapm_routes = acp3x_1015_route, 417 .num_dapm_routes = ARRAY_SIZE(acp3x_1015_route), 418 .codec_conf = rt1015_conf, 419 .num_configs = ARRAY_SIZE(rt1015_conf), 420 .controls = acp3x_mc_1015_controls, 421 .num_controls = ARRAY_SIZE(acp3x_mc_1015_controls), 422}; 423 424static const struct snd_soc_dapm_widget acp3x_1015p_widgets[] = { 425 SND_SOC_DAPM_HP("Headphone Jack", NULL), 426 SND_SOC_DAPM_MIC("Headset Mic", NULL), 427 SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, 428 &acp3x_dmic_mux_control), 429 SND_SOC_DAPM_SPK("Speakers", NULL), 430}; 431 432static const struct snd_soc_dapm_route acp3x_1015p_route[] = { 433 {"Headphone Jack", NULL, "HPOL"}, 434 {"Headphone Jack", NULL, "HPOR"}, 435 {"IN1P", NULL, "Headset Mic"}, 436 {"Dmic Mux", "Front Mic", "DMIC"}, 437 {"Dmic Mux", "Rear Mic", "DMIC"}, 438 /* speaker */ 439 { "Speakers", NULL, "Speaker" }, 440}; 441 442static const struct snd_kcontrol_new acp3x_mc_1015p_controls[] = { 443 SOC_DAPM_PIN_SWITCH("Speakers"), 444 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 445 SOC_DAPM_PIN_SWITCH("Headset Mic"), 446}; 447 448static struct snd_soc_card acp3x_1015p = { 449 .name = "acp3xalc56821015p", 450 .owner = THIS_MODULE, 451 .dai_link = acp3x_dai, 452 .num_links = ARRAY_SIZE(acp3x_dai), 453 .dapm_widgets = acp3x_1015p_widgets, 454 .num_dapm_widgets = ARRAY_SIZE(acp3x_1015p_widgets), 455 .dapm_routes = acp3x_1015p_route, 456 .num_dapm_routes = ARRAY_SIZE(acp3x_1015p_route), 457 .controls = acp3x_mc_1015p_controls, 458 .num_controls = ARRAY_SIZE(acp3x_mc_1015p_controls), 459}; 460 461void *soc_is_rltk_max(struct device *dev) 462{ 463 const struct acpi_device_id *match; 464 465 match = acpi_match_device(dev->driver->acpi_match_table, dev); 466 if (!match) 467 return NULL; 468 return (void *)match->driver_data; 469} 470 471static void card_spk_dai_link_present(struct snd_soc_dai_link *links, 472 const char *card_name) 473{ 474 if (!strcmp(card_name, "acp3xalc56821015")) { 475 links[1].codecs = rt1015; 476 links[1].num_codecs = ARRAY_SIZE(rt1015); 477 } else if (!strcmp(card_name, "acp3xalc56821015p")) { 478 links[1].codecs = rt1015p; 479 links[1].num_codecs = ARRAY_SIZE(rt1015p); 480 } else { 481 links[1].codecs = max; 482 links[1].num_codecs = ARRAY_SIZE(max); 483 } 484} 485 486static int acp3x_probe(struct platform_device *pdev) 487{ 488 int ret; 489 struct snd_soc_card *card; 490 struct acp3x_platform_info *machine; 491 struct device *dev = &pdev->dev; 492 493 card = (struct snd_soc_card *)soc_is_rltk_max(dev); 494 if (!card) 495 return -ENODEV; 496 497 machine = devm_kzalloc(&pdev->dev, sizeof(*machine), GFP_KERNEL); 498 if (!machine) 499 return -ENOMEM; 500 501 card_spk_dai_link_present(card->dai_link, card->name); 502 card->dev = &pdev->dev; 503 platform_set_drvdata(pdev, card); 504 snd_soc_card_set_drvdata(card, machine); 505 506 dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW); 507 if (IS_ERR(dmic_sel)) { 508 dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n", 509 PTR_ERR(dmic_sel)); 510 return PTR_ERR(dmic_sel); 511 } 512 513 ret = devm_snd_soc_register_card(&pdev->dev, card); 514 if (ret) { 515 return dev_err_probe(&pdev->dev, ret, 516 "devm_snd_soc_register_card(%s) failed\n", 517 card->name); 518 } 519 return 0; 520} 521 522static const struct acpi_device_id acp3x_audio_acpi_match[] = { 523 { "AMDI5682", (unsigned long)&acp3x_5682}, 524 { "AMDI1015", (unsigned long)&acp3x_1015}, 525 { "10021015", (unsigned long)&acp3x_1015p}, 526 {}, 527}; 528MODULE_DEVICE_TABLE(acpi, acp3x_audio_acpi_match); 529 530static struct platform_driver acp3x_audio = { 531 .driver = { 532 .name = "acp3x-alc5682-max98357", 533 .acpi_match_table = ACPI_PTR(acp3x_audio_acpi_match), 534 .pm = &snd_soc_pm_ops, 535 }, 536 .probe = acp3x_probe, 537}; 538 539module_platform_driver(acp3x_audio); 540 541MODULE_AUTHOR("akshu.agrawal@amd.com"); 542MODULE_AUTHOR("Vishnuvardhanrao.Ravulapati@amd.com"); 543MODULE_AUTHOR("Vijendar.Mukunda@amd.com"); 544MODULE_DESCRIPTION("ALC5682 ALC1015, ALC1015P & MAX98357 audio support"); 545MODULE_LICENSE("GPL v2");