1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // mt8183-mt6358.c -- 4 // MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver 5 // 6 // Copyright (c) 2018 MediaTek Inc. 7 // Author: Shunli Wang <shunli.wang@mediatek.com> 8 9 #include <linux/module.h> 10 #include <linux/of_device.h> 11 #include <linux/pinctrl/consumer.h> 12 #include <sound/jack.h> 13 #include <sound/pcm_params.h> 14 #include <sound/soc.h> 15 16 #include "../../codecs/rt1015.h" 17 #include "../../codecs/ts3a227e.h" 18 #include "mt8183-afe-common.h" 19 20 #define RT1015_CODEC_DAI "rt1015-aif" 21 #define RT1015_DEV0_NAME "rt1015.6-0028" 22 #define RT1015_DEV1_NAME "rt1015.6-0029" 23 24 enum PINCTRL_PIN_STATE { 25 PIN_STATE_DEFAULT = 0, 26 PIN_TDM_OUT_ON, 27 PIN_TDM_OUT_OFF, 28 PIN_WOV, 29 PIN_STATE_MAX 30 }; 31 32 static const char * const mt8183_pin_str[PIN_STATE_MAX] = { 33 "default", "aud_tdm_out_on", "aud_tdm_out_off", "wov", 34 }; 35 36 struct mt8183_mt6358_ts3a227_max98357_priv { 37 struct pinctrl *pinctrl; 38 struct pinctrl_state *pin_states[PIN_STATE_MAX]; 39 struct snd_soc_jack headset_jack, hdmi_jack; 40 }; 41 42 static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream, 43 struct snd_pcm_hw_params *params) 44 { 45 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 46 unsigned int rate = params_rate(params); 47 unsigned int mclk_fs_ratio = 128; 48 unsigned int mclk_fs = rate * mclk_fs_ratio; 49 50 return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), 51 0, mclk_fs, SND_SOC_CLOCK_OUT); 52 } 53 54 static const struct snd_soc_ops mt8183_mt6358_i2s_ops = { 55 .hw_params = mt8183_mt6358_i2s_hw_params, 56 }; 57 58 static int 59 mt8183_mt6358_rt1015_i2s_hw_params(struct snd_pcm_substream *substream, 60 struct snd_pcm_hw_params *params) 61 { 62 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 63 unsigned int rate = params_rate(params); 64 unsigned int mclk_fs_ratio = 128; 65 unsigned int mclk_fs = rate * mclk_fs_ratio; 66 struct snd_soc_card *card = rtd->card; 67 struct snd_soc_dai *codec_dai; 68 int ret, i; 69 70 for_each_rtd_codec_dais(rtd, i, codec_dai) { 71 ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK, 72 rate * 64, rate * 256); 73 if (ret < 0) { 74 dev_err(card->dev, "failed to set pll\n"); 75 return ret; 76 } 77 78 ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL, 79 rate * 256, SND_SOC_CLOCK_IN); 80 if (ret < 0) { 81 dev_err(card->dev, "failed to set sysclk\n"); 82 return ret; 83 } 84 } 85 86 return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), 87 0, mclk_fs, SND_SOC_CLOCK_OUT); 88 } 89 90 static const struct snd_soc_ops mt8183_mt6358_rt1015_i2s_ops = { 91 .hw_params = mt8183_mt6358_rt1015_i2s_hw_params, 92 }; 93 94 static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 95 struct snd_pcm_hw_params *params) 96 { 97 dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__); 98 99 /* fix BE i2s format to 32bit, clean param mask first */ 100 snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), 101 0, SNDRV_PCM_FORMAT_LAST); 102 103 params_set_format(params, SNDRV_PCM_FORMAT_S32_LE); 104 return 0; 105 } 106 107 static int mt8183_rt1015_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 108 struct snd_pcm_hw_params *params) 109 { 110 dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__); 111 112 /* fix BE i2s format to 32bit, clean param mask first */ 113 snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), 114 0, SNDRV_PCM_FORMAT_LAST); 115 116 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); 117 return 0; 118 } 119 120 static int 121 mt8183_mt6358_startup(struct snd_pcm_substream *substream) 122 { 123 static const unsigned int rates[] = { 124 48000, 125 }; 126 static const struct snd_pcm_hw_constraint_list constraints_rates = { 127 .count = ARRAY_SIZE(rates), 128 .list = rates, 129 .mask = 0, 130 }; 131 static const unsigned int channels[] = { 132 2, 133 }; 134 static const struct snd_pcm_hw_constraint_list constraints_channels = { 135 .count = ARRAY_SIZE(channels), 136 .list = channels, 137 .mask = 0, 138 }; 139 140 struct snd_pcm_runtime *runtime = substream->runtime; 141 142 snd_pcm_hw_constraint_list(runtime, 0, 143 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates); 144 runtime->hw.channels_max = 2; 145 snd_pcm_hw_constraint_list(runtime, 0, 146 SNDRV_PCM_HW_PARAM_CHANNELS, 147 &constraints_channels); 148 149 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 150 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16); 151 152 return 0; 153 } 154 155 static const struct snd_soc_ops mt8183_mt6358_ops = { 156 .startup = mt8183_mt6358_startup, 157 }; 158 159 static int 160 mt8183_mt6358_ts3a227_max98357_bt_sco_startup( 161 struct snd_pcm_substream *substream) 162 { 163 static const unsigned int rates[] = { 164 8000, 16000 165 }; 166 static const struct snd_pcm_hw_constraint_list constraints_rates = { 167 .count = ARRAY_SIZE(rates), 168 .list = rates, 169 .mask = 0, 170 }; 171 static const unsigned int channels[] = { 172 1, 173 }; 174 static const struct snd_pcm_hw_constraint_list constraints_channels = { 175 .count = ARRAY_SIZE(channels), 176 .list = channels, 177 .mask = 0, 178 }; 179 180 struct snd_pcm_runtime *runtime = substream->runtime; 181 182 snd_pcm_hw_constraint_list(runtime, 0, 183 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates); 184 runtime->hw.channels_max = 1; 185 snd_pcm_hw_constraint_list(runtime, 0, 186 SNDRV_PCM_HW_PARAM_CHANNELS, 187 &constraints_channels); 188 189 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; 190 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16); 191 192 return 0; 193 } 194 195 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_bt_sco_ops = { 196 .startup = mt8183_mt6358_ts3a227_max98357_bt_sco_startup, 197 }; 198 199 /* FE */ 200 SND_SOC_DAILINK_DEFS(playback1, 201 DAILINK_COMP_ARRAY(COMP_CPU("DL1")), 202 DAILINK_COMP_ARRAY(COMP_DUMMY()), 203 DAILINK_COMP_ARRAY(COMP_EMPTY())); 204 205 SND_SOC_DAILINK_DEFS(playback2, 206 DAILINK_COMP_ARRAY(COMP_CPU("DL2")), 207 DAILINK_COMP_ARRAY(COMP_DUMMY()), 208 DAILINK_COMP_ARRAY(COMP_EMPTY())); 209 210 SND_SOC_DAILINK_DEFS(playback3, 211 DAILINK_COMP_ARRAY(COMP_CPU("DL3")), 212 DAILINK_COMP_ARRAY(COMP_DUMMY()), 213 DAILINK_COMP_ARRAY(COMP_EMPTY())); 214 215 SND_SOC_DAILINK_DEFS(capture1, 216 DAILINK_COMP_ARRAY(COMP_CPU("UL1")), 217 DAILINK_COMP_ARRAY(COMP_DUMMY()), 218 DAILINK_COMP_ARRAY(COMP_EMPTY())); 219 220 SND_SOC_DAILINK_DEFS(capture2, 221 DAILINK_COMP_ARRAY(COMP_CPU("UL2")), 222 DAILINK_COMP_ARRAY(COMP_DUMMY()), 223 DAILINK_COMP_ARRAY(COMP_EMPTY())); 224 225 SND_SOC_DAILINK_DEFS(capture3, 226 DAILINK_COMP_ARRAY(COMP_CPU("UL3")), 227 DAILINK_COMP_ARRAY(COMP_DUMMY()), 228 DAILINK_COMP_ARRAY(COMP_EMPTY())); 229 230 SND_SOC_DAILINK_DEFS(capture_mono, 231 DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")), 232 DAILINK_COMP_ARRAY(COMP_DUMMY()), 233 DAILINK_COMP_ARRAY(COMP_EMPTY())); 234 235 SND_SOC_DAILINK_DEFS(playback_hdmi, 236 DAILINK_COMP_ARRAY(COMP_CPU("HDMI")), 237 DAILINK_COMP_ARRAY(COMP_DUMMY()), 238 DAILINK_COMP_ARRAY(COMP_EMPTY())); 239 240 SND_SOC_DAILINK_DEFS(wake_on_voice, 241 DAILINK_COMP_ARRAY(COMP_DUMMY()), 242 DAILINK_COMP_ARRAY(COMP_DUMMY()), 243 DAILINK_COMP_ARRAY(COMP_EMPTY())); 244 245 /* BE */ 246 SND_SOC_DAILINK_DEFS(primary_codec, 247 DAILINK_COMP_ARRAY(COMP_CPU("ADDA")), 248 DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")), 249 DAILINK_COMP_ARRAY(COMP_EMPTY())); 250 251 SND_SOC_DAILINK_DEFS(pcm1, 252 DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")), 253 DAILINK_COMP_ARRAY(COMP_DUMMY()), 254 DAILINK_COMP_ARRAY(COMP_EMPTY())); 255 256 SND_SOC_DAILINK_DEFS(pcm2, 257 DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")), 258 DAILINK_COMP_ARRAY(COMP_DUMMY()), 259 DAILINK_COMP_ARRAY(COMP_EMPTY())); 260 261 SND_SOC_DAILINK_DEFS(i2s0, 262 DAILINK_COMP_ARRAY(COMP_CPU("I2S0")), 263 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")), 264 DAILINK_COMP_ARRAY(COMP_EMPTY())); 265 266 SND_SOC_DAILINK_DEFS(i2s1, 267 DAILINK_COMP_ARRAY(COMP_CPU("I2S1")), 268 DAILINK_COMP_ARRAY(COMP_DUMMY()), 269 DAILINK_COMP_ARRAY(COMP_EMPTY())); 270 271 SND_SOC_DAILINK_DEFS(i2s2, 272 DAILINK_COMP_ARRAY(COMP_CPU("I2S2")), 273 DAILINK_COMP_ARRAY(COMP_DUMMY()), 274 DAILINK_COMP_ARRAY(COMP_EMPTY())); 275 276 SND_SOC_DAILINK_DEFS(i2s3_max98357a, 277 DAILINK_COMP_ARRAY(COMP_CPU("I2S3")), 278 DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi")), 279 DAILINK_COMP_ARRAY(COMP_EMPTY())); 280 281 SND_SOC_DAILINK_DEFS(i2s3_rt1015, 282 DAILINK_COMP_ARRAY(COMP_CPU("I2S3")), 283 DAILINK_COMP_ARRAY(COMP_CODEC(RT1015_DEV0_NAME, RT1015_CODEC_DAI), 284 COMP_CODEC(RT1015_DEV1_NAME, RT1015_CODEC_DAI)), 285 DAILINK_COMP_ARRAY(COMP_EMPTY())); 286 287 SND_SOC_DAILINK_DEFS(i2s5, 288 DAILINK_COMP_ARRAY(COMP_CPU("I2S5")), 289 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")), 290 DAILINK_COMP_ARRAY(COMP_EMPTY())); 291 292 SND_SOC_DAILINK_DEFS(tdm, 293 DAILINK_COMP_ARRAY(COMP_CPU("TDM")), 294 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")), 295 DAILINK_COMP_ARRAY(COMP_EMPTY())); 296 297 static int mt8183_mt6358_tdm_startup(struct snd_pcm_substream *substream) 298 { 299 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 300 struct mt8183_mt6358_ts3a227_max98357_priv *priv = 301 snd_soc_card_get_drvdata(rtd->card); 302 int ret; 303 304 if (IS_ERR(priv->pin_states[PIN_TDM_OUT_ON])) 305 return PTR_ERR(priv->pin_states[PIN_TDM_OUT_ON]); 306 307 ret = pinctrl_select_state(priv->pinctrl, 308 priv->pin_states[PIN_TDM_OUT_ON]); 309 if (ret) 310 dev_err(rtd->card->dev, "%s failed to select state %d\n", 311 __func__, ret); 312 313 return ret; 314 } 315 316 static void mt8183_mt6358_tdm_shutdown(struct snd_pcm_substream *substream) 317 { 318 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 319 struct mt8183_mt6358_ts3a227_max98357_priv *priv = 320 snd_soc_card_get_drvdata(rtd->card); 321 int ret; 322 323 if (IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF])) 324 return; 325 326 ret = pinctrl_select_state(priv->pinctrl, 327 priv->pin_states[PIN_TDM_OUT_OFF]); 328 if (ret) 329 dev_err(rtd->card->dev, "%s failed to select state %d\n", 330 __func__, ret); 331 } 332 333 static struct snd_soc_ops mt8183_mt6358_tdm_ops = { 334 .startup = mt8183_mt6358_tdm_startup, 335 .shutdown = mt8183_mt6358_tdm_shutdown, 336 }; 337 338 static int 339 mt8183_mt6358_ts3a227_max98357_wov_startup( 340 struct snd_pcm_substream *substream) 341 { 342 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 343 struct snd_soc_card *card = rtd->card; 344 struct mt8183_mt6358_ts3a227_max98357_priv *priv = 345 snd_soc_card_get_drvdata(card); 346 347 return pinctrl_select_state(priv->pinctrl, 348 priv->pin_states[PIN_WOV]); 349 } 350 351 static void 352 mt8183_mt6358_ts3a227_max98357_wov_shutdown( 353 struct snd_pcm_substream *substream) 354 { 355 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 356 struct snd_soc_card *card = rtd->card; 357 struct mt8183_mt6358_ts3a227_max98357_priv *priv = 358 snd_soc_card_get_drvdata(card); 359 int ret; 360 361 ret = pinctrl_select_state(priv->pinctrl, 362 priv->pin_states[PIN_STATE_DEFAULT]); 363 if (ret) 364 dev_err(card->dev, "%s failed to select state %d\n", 365 __func__, ret); 366 } 367 368 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_wov_ops = { 369 .startup = mt8183_mt6358_ts3a227_max98357_wov_startup, 370 .shutdown = mt8183_mt6358_ts3a227_max98357_wov_shutdown, 371 }; 372 373 static int 374 mt8183_mt6358_ts3a227_max98357_hdmi_init(struct snd_soc_pcm_runtime *rtd) 375 { 376 struct mt8183_mt6358_ts3a227_max98357_priv *priv = 377 snd_soc_card_get_drvdata(rtd->card); 378 int ret; 379 380 ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT, 381 &priv->hdmi_jack, NULL, 0); 382 if (ret) 383 return ret; 384 385 return snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component, 386 &priv->hdmi_jack, NULL); 387 } 388 389 static struct snd_soc_dai_link mt8183_mt6358_ts3a227_dai_links[] = { 390 /* FE */ 391 { 392 .name = "Playback_1", 393 .stream_name = "Playback_1", 394 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 395 SND_SOC_DPCM_TRIGGER_PRE}, 396 .dynamic = 1, 397 .dpcm_playback = 1, 398 .ops = &mt8183_mt6358_ops, 399 SND_SOC_DAILINK_REG(playback1), 400 }, 401 { 402 .name = "Playback_2", 403 .stream_name = "Playback_2", 404 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 405 SND_SOC_DPCM_TRIGGER_PRE}, 406 .dynamic = 1, 407 .dpcm_playback = 1, 408 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops, 409 SND_SOC_DAILINK_REG(playback2), 410 }, 411 { 412 .name = "Playback_3", 413 .stream_name = "Playback_3", 414 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 415 SND_SOC_DPCM_TRIGGER_PRE}, 416 .dynamic = 1, 417 .dpcm_playback = 1, 418 SND_SOC_DAILINK_REG(playback3), 419 }, 420 { 421 .name = "Capture_1", 422 .stream_name = "Capture_1", 423 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 424 SND_SOC_DPCM_TRIGGER_PRE}, 425 .dynamic = 1, 426 .dpcm_capture = 1, 427 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops, 428 SND_SOC_DAILINK_REG(capture1), 429 }, 430 { 431 .name = "Capture_2", 432 .stream_name = "Capture_2", 433 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 434 SND_SOC_DPCM_TRIGGER_PRE}, 435 .dynamic = 1, 436 .dpcm_capture = 1, 437 SND_SOC_DAILINK_REG(capture2), 438 }, 439 { 440 .name = "Capture_3", 441 .stream_name = "Capture_3", 442 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 443 SND_SOC_DPCM_TRIGGER_PRE}, 444 .dynamic = 1, 445 .dpcm_capture = 1, 446 .ops = &mt8183_mt6358_ops, 447 SND_SOC_DAILINK_REG(capture3), 448 }, 449 { 450 .name = "Capture_Mono_1", 451 .stream_name = "Capture_Mono_1", 452 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 453 SND_SOC_DPCM_TRIGGER_PRE}, 454 .dynamic = 1, 455 .dpcm_capture = 1, 456 SND_SOC_DAILINK_REG(capture_mono), 457 }, 458 { 459 .name = "Playback_HDMI", 460 .stream_name = "Playback_HDMI", 461 .trigger = {SND_SOC_DPCM_TRIGGER_PRE, 462 SND_SOC_DPCM_TRIGGER_PRE}, 463 .dynamic = 1, 464 .dpcm_playback = 1, 465 SND_SOC_DAILINK_REG(playback_hdmi), 466 }, 467 { 468 .name = "Wake on Voice", 469 .stream_name = "Wake on Voice", 470 .ignore_suspend = 1, 471 .ignore = 1, 472 SND_SOC_DAILINK_REG(wake_on_voice), 473 .ops = &mt8183_mt6358_ts3a227_max98357_wov_ops, 474 }, 475 476 /* BE */ 477 { 478 .name = "Primary Codec", 479 .no_pcm = 1, 480 .dpcm_playback = 1, 481 .dpcm_capture = 1, 482 .ignore_suspend = 1, 483 SND_SOC_DAILINK_REG(primary_codec), 484 }, 485 { 486 .name = "PCM 1", 487 .no_pcm = 1, 488 .dpcm_playback = 1, 489 .dpcm_capture = 1, 490 .ignore_suspend = 1, 491 SND_SOC_DAILINK_REG(pcm1), 492 }, 493 { 494 .name = "PCM 2", 495 .no_pcm = 1, 496 .dpcm_playback = 1, 497 .dpcm_capture = 1, 498 .ignore_suspend = 1, 499 SND_SOC_DAILINK_REG(pcm2), 500 }, 501 { 502 .name = "I2S0", 503 .no_pcm = 1, 504 .dpcm_capture = 1, 505 .ignore_suspend = 1, 506 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup, 507 .ops = &mt8183_mt6358_i2s_ops, 508 SND_SOC_DAILINK_REG(i2s0), 509 }, 510 { 511 .name = "I2S1", 512 .no_pcm = 1, 513 .dpcm_playback = 1, 514 .ignore_suspend = 1, 515 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup, 516 .ops = &mt8183_mt6358_i2s_ops, 517 SND_SOC_DAILINK_REG(i2s1), 518 }, 519 { 520 .name = "I2S2", 521 .no_pcm = 1, 522 .dpcm_capture = 1, 523 .ignore_suspend = 1, 524 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup, 525 .ops = &mt8183_mt6358_i2s_ops, 526 SND_SOC_DAILINK_REG(i2s2), 527 }, 528 { 529 .name = "I2S3", 530 .no_pcm = 1, 531 .dpcm_playback = 1, 532 .ignore_suspend = 1, 533 }, 534 { 535 .name = "I2S5", 536 .no_pcm = 1, 537 .dpcm_playback = 1, 538 .ignore_suspend = 1, 539 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup, 540 .ops = &mt8183_mt6358_i2s_ops, 541 SND_SOC_DAILINK_REG(i2s5), 542 }, 543 { 544 .name = "TDM", 545 .no_pcm = 1, 546 .dai_fmt = SND_SOC_DAIFMT_I2S | 547 SND_SOC_DAIFMT_IB_IF | 548 SND_SOC_DAIFMT_CBM_CFM, 549 .dpcm_playback = 1, 550 .ignore_suspend = 1, 551 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup, 552 .ops = &mt8183_mt6358_tdm_ops, 553 .ignore = 1, 554 .init = mt8183_mt6358_ts3a227_max98357_hdmi_init, 555 SND_SOC_DAILINK_REG(tdm), 556 }, 557 }; 558 559 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357_card = { 560 .name = "mt8183_mt6358_ts3a227_max98357", 561 .owner = THIS_MODULE, 562 .dai_link = mt8183_mt6358_ts3a227_dai_links, 563 .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links), 564 }; 565 566 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357b_card = { 567 .name = "mt8183_mt6358_ts3a227_max98357b", 568 .owner = THIS_MODULE, 569 .dai_link = mt8183_mt6358_ts3a227_dai_links, 570 .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links), 571 }; 572 573 static struct snd_soc_codec_conf mt8183_mt6358_ts3a227_rt1015_amp_conf[] = { 574 { 575 .dlc = COMP_CODEC_CONF(RT1015_DEV0_NAME), 576 .name_prefix = "Left", 577 }, 578 { 579 .dlc = COMP_CODEC_CONF(RT1015_DEV1_NAME), 580 .name_prefix = "Right", 581 }, 582 }; 583 584 static struct snd_soc_card mt8183_mt6358_ts3a227_rt1015_card = { 585 .name = "mt8183_mt6358_ts3a227_rt1015", 586 .owner = THIS_MODULE, 587 .dai_link = mt8183_mt6358_ts3a227_dai_links, 588 .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links), 589 .codec_conf = mt8183_mt6358_ts3a227_rt1015_amp_conf, 590 .num_configs = ARRAY_SIZE(mt8183_mt6358_ts3a227_rt1015_amp_conf), 591 }; 592 593 static int 594 mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *component) 595 { 596 int ret; 597 struct mt8183_mt6358_ts3a227_max98357_priv *priv = 598 snd_soc_card_get_drvdata(component->card); 599 600 /* Enable Headset and 4 Buttons Jack detection */ 601 ret = snd_soc_card_jack_new(component->card, 602 "Headset Jack", 603 SND_JACK_HEADSET | 604 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 605 SND_JACK_BTN_2 | SND_JACK_BTN_3, 606 &priv->headset_jack, 607 NULL, 0); 608 if (ret) 609 return ret; 610 611 ret = ts3a227e_enable_jack_detect(component, &priv->headset_jack); 612 613 return ret; 614 } 615 616 static struct snd_soc_aux_dev mt8183_mt6358_ts3a227_max98357_headset_dev = { 617 .dlc = COMP_EMPTY(), 618 .init = mt8183_mt6358_ts3a227_max98357_headset_init, 619 }; 620 621 static int 622 mt8183_mt6358_ts3a227_max98357_dev_probe(struct platform_device *pdev) 623 { 624 struct snd_soc_card *card; 625 struct device_node *platform_node, *ec_codec, *hdmi_codec; 626 struct snd_soc_dai_link *dai_link; 627 struct mt8183_mt6358_ts3a227_max98357_priv *priv; 628 const struct of_device_id *match; 629 int ret, i; 630 631 platform_node = of_parse_phandle(pdev->dev.of_node, 632 "mediatek,platform", 0); 633 if (!platform_node) { 634 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n"); 635 return -EINVAL; 636 } 637 638 match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev); 639 if (!match || !match->data) 640 return -EINVAL; 641 642 card = (struct snd_soc_card *)match->data; 643 card->dev = &pdev->dev; 644 645 ec_codec = of_parse_phandle(pdev->dev.of_node, "mediatek,ec-codec", 0); 646 hdmi_codec = of_parse_phandle(pdev->dev.of_node, 647 "mediatek,hdmi-codec", 0); 648 649 for_each_card_prelinks(card, i, dai_link) { 650 if (ec_codec && strcmp(dai_link->name, "Wake on Voice") == 0) { 651 dai_link->cpus[0].name = NULL; 652 dai_link->cpus[0].of_node = ec_codec; 653 dai_link->cpus[0].dai_name = NULL; 654 dai_link->codecs[0].name = NULL; 655 dai_link->codecs[0].of_node = ec_codec; 656 dai_link->codecs[0].dai_name = "Wake on Voice"; 657 dai_link->platforms[0].of_node = ec_codec; 658 dai_link->ignore = 0; 659 } 660 661 if (strcmp(dai_link->name, "I2S3") == 0) { 662 if (card == &mt8183_mt6358_ts3a227_max98357_card || 663 card == &mt8183_mt6358_ts3a227_max98357b_card) { 664 dai_link->be_hw_params_fixup = 665 mt8183_i2s_hw_params_fixup; 666 dai_link->ops = &mt8183_mt6358_i2s_ops; 667 dai_link->cpus = i2s3_max98357a_cpus; 668 dai_link->num_cpus = 669 ARRAY_SIZE(i2s3_max98357a_cpus); 670 dai_link->codecs = i2s3_max98357a_codecs; 671 dai_link->num_codecs = 672 ARRAY_SIZE(i2s3_max98357a_codecs); 673 dai_link->platforms = i2s3_max98357a_platforms; 674 dai_link->num_platforms = 675 ARRAY_SIZE(i2s3_max98357a_platforms); 676 } else if (card == &mt8183_mt6358_ts3a227_rt1015_card) { 677 dai_link->be_hw_params_fixup = 678 mt8183_rt1015_i2s_hw_params_fixup; 679 dai_link->ops = &mt8183_mt6358_rt1015_i2s_ops; 680 dai_link->cpus = i2s3_rt1015_cpus; 681 dai_link->num_cpus = 682 ARRAY_SIZE(i2s3_rt1015_cpus); 683 dai_link->codecs = i2s3_rt1015_codecs; 684 dai_link->num_codecs = 685 ARRAY_SIZE(i2s3_rt1015_codecs); 686 dai_link->platforms = i2s3_rt1015_platforms; 687 dai_link->num_platforms = 688 ARRAY_SIZE(i2s3_rt1015_platforms); 689 } 690 } 691 692 if (card == &mt8183_mt6358_ts3a227_max98357b_card) { 693 if (strcmp(dai_link->name, "I2S2") == 0 || 694 strcmp(dai_link->name, "I2S3") == 0) 695 dai_link->dai_fmt = SND_SOC_DAIFMT_LEFT_J | 696 SND_SOC_DAIFMT_NB_NF | 697 SND_SOC_DAIFMT_CBM_CFM; 698 } 699 700 if (hdmi_codec && strcmp(dai_link->name, "TDM") == 0) { 701 dai_link->codecs->of_node = hdmi_codec; 702 dai_link->ignore = 0; 703 } 704 705 if (!dai_link->platforms->name) 706 dai_link->platforms->of_node = platform_node; 707 } 708 709 mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node = 710 of_parse_phandle(pdev->dev.of_node, 711 "mediatek,headset-codec", 0); 712 if (mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node) { 713 card->aux_dev = &mt8183_mt6358_ts3a227_max98357_headset_dev; 714 card->num_aux_devs = 1; 715 } 716 717 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 718 if (!priv) 719 return -ENOMEM; 720 721 snd_soc_card_set_drvdata(card, priv); 722 723 priv->pinctrl = devm_pinctrl_get(&pdev->dev); 724 if (IS_ERR(priv->pinctrl)) { 725 dev_err(&pdev->dev, "%s devm_pinctrl_get failed\n", 726 __func__); 727 return PTR_ERR(priv->pinctrl); 728 } 729 730 for (i = 0; i < PIN_STATE_MAX; i++) { 731 priv->pin_states[i] = pinctrl_lookup_state(priv->pinctrl, 732 mt8183_pin_str[i]); 733 if (IS_ERR(priv->pin_states[i])) { 734 ret = PTR_ERR(priv->pin_states[i]); 735 dev_info(&pdev->dev, "%s Can't find pin state %s %d\n", 736 __func__, mt8183_pin_str[i], ret); 737 } 738 } 739 740 if (!IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF])) { 741 ret = pinctrl_select_state(priv->pinctrl, 742 priv->pin_states[PIN_TDM_OUT_OFF]); 743 if (ret) 744 dev_info(&pdev->dev, 745 "%s failed to select state %d\n", 746 __func__, ret); 747 } 748 749 if (!IS_ERR(priv->pin_states[PIN_STATE_DEFAULT])) { 750 ret = pinctrl_select_state(priv->pinctrl, 751 priv->pin_states[PIN_STATE_DEFAULT]); 752 if (ret) 753 dev_info(&pdev->dev, 754 "%s failed to select state %d\n", 755 __func__, ret); 756 } 757 758 return devm_snd_soc_register_card(&pdev->dev, card); 759 } 760 761 #ifdef CONFIG_OF 762 static const struct of_device_id mt8183_mt6358_ts3a227_max98357_dt_match[] = { 763 { 764 .compatible = "mediatek,mt8183_mt6358_ts3a227_max98357", 765 .data = &mt8183_mt6358_ts3a227_max98357_card, 766 }, 767 { 768 .compatible = "mediatek,mt8183_mt6358_ts3a227_max98357b", 769 .data = &mt8183_mt6358_ts3a227_max98357b_card, 770 }, 771 { 772 .compatible = "mediatek,mt8183_mt6358_ts3a227_rt1015", 773 .data = &mt8183_mt6358_ts3a227_rt1015_card, 774 }, 775 {} 776 }; 777 #endif 778 779 static struct platform_driver mt8183_mt6358_ts3a227_max98357_driver = { 780 .driver = { 781 .name = "mt8183_mt6358_ts3a227", 782 #ifdef CONFIG_OF 783 .of_match_table = mt8183_mt6358_ts3a227_max98357_dt_match, 784 #endif 785 .pm = &snd_soc_pm_ops, 786 }, 787 .probe = mt8183_mt6358_ts3a227_max98357_dev_probe, 788 }; 789 790 module_platform_driver(mt8183_mt6358_ts3a227_max98357_driver); 791 792 /* Module information */ 793 MODULE_DESCRIPTION("MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver"); 794 MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>"); 795 MODULE_LICENSE("GPL v2"); 796 MODULE_ALIAS("mt8183_mt6358_ts3a227_max98357 soc card"); 797