1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Freescale Generic ASoC Sound Card driver with ASRC 4 // 5 // Copyright (C) 2014 Freescale Semiconductor, Inc. 6 // 7 // Author: Nicolin Chen <nicoleotsuka@gmail.com> 8 9 #include <linux/clk.h> 10 #include <linux/i2c.h> 11 #include <linux/module.h> 12 #include <linux/of_platform.h> 13 #if IS_ENABLED(CONFIG_SND_AC97_CODEC) 14 #include <sound/ac97_codec.h> 15 #endif 16 #include <sound/pcm_params.h> 17 #include <sound/soc.h> 18 #include <sound/jack.h> 19 #include <sound/simple_card_utils.h> 20 21 #include "fsl_esai.h" 22 #include "fsl_sai.h" 23 #include "imx-audmux.h" 24 25 #include "../codecs/sgtl5000.h" 26 #include "../codecs/wm8962.h" 27 #include "../codecs/wm8960.h" 28 #include "../codecs/wm8994.h" 29 #include "../codecs/tlv320aic31xx.h" 30 #include "../codecs/nau8822.h" 31 #include "../codecs/wm8904.h" 32 33 #define DRIVER_NAME "fsl-asoc-card" 34 35 #define CS427x_SYSCLK_MCLK 0 36 37 #define RX 0 38 #define TX 1 39 40 /* Default DAI format without Master and Slave flag */ 41 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF) 42 43 /** 44 * struct codec_priv - CODEC private data 45 * @mclk: Main clock of the CODEC 46 * @mclk_freq: Clock rate of MCLK 47 * @free_freq: Clock rate of MCLK for hw_free() 48 * @mclk_id: MCLK (or main clock) id for set_sysclk() 49 * @fll_id: FLL (or secordary clock) id for set_sysclk() 50 * @pll_id: PLL id for set_pll() 51 */ 52 struct codec_priv { 53 struct clk *mclk; 54 unsigned long mclk_freq; 55 unsigned long free_freq; 56 u32 mclk_id; 57 int fll_id; 58 int pll_id; 59 }; 60 61 /** 62 * struct cpu_priv - CPU private data 63 * @sysclk_freq: SYSCLK rates for set_sysclk() 64 * @sysclk_dir: SYSCLK directions for set_sysclk() 65 * @sysclk_id: SYSCLK ids for set_sysclk() 66 * @slot_width: Slot width of each frame 67 * @slot_num: Number of slots of each frame 68 * 69 * Note: [1] for tx and [0] for rx 70 */ 71 struct cpu_priv { 72 unsigned long sysclk_freq[2]; 73 u32 sysclk_dir[2]; 74 u32 sysclk_id[2]; 75 u32 slot_width; 76 u32 slot_num; 77 }; 78 79 /** 80 * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data 81 * @dai_link: DAI link structure including normal one and DPCM link 82 * @hp_jack: Headphone Jack structure 83 * @mic_jack: Microphone Jack structure 84 * @pdev: platform device pointer 85 * @codec_priv: CODEC private data 86 * @cpu_priv: CPU private data 87 * @card: ASoC card structure 88 * @streams: Mask of current active streams 89 * @sample_rate: Current sample rate 90 * @sample_format: Current sample format 91 * @asrc_rate: ASRC sample rate used by Back-Ends 92 * @asrc_format: ASRC sample format used by Back-Ends 93 * @dai_fmt: DAI format between CPU and CODEC 94 * @name: Card name 95 */ 96 97 struct fsl_asoc_card_priv { 98 struct snd_soc_dai_link dai_link[3]; 99 struct simple_util_jack hp_jack; 100 struct simple_util_jack mic_jack; 101 struct platform_device *pdev; 102 struct codec_priv codec_priv; 103 struct cpu_priv cpu_priv; 104 struct snd_soc_card card; 105 u8 streams; 106 u32 sample_rate; 107 snd_pcm_format_t sample_format; 108 u32 asrc_rate; 109 snd_pcm_format_t asrc_format; 110 u32 dai_fmt; 111 char name[32]; 112 }; 113 114 /* 115 * This dapm route map exists for DPCM link only. 116 * The other routes shall go through Device Tree. 117 * 118 * Note: keep all ASRC routes in the second half 119 * to drop them easily for non-ASRC cases. 120 */ 121 static const struct snd_soc_dapm_route audio_map[] = { 122 /* 1st half -- Normal DAPM routes */ 123 {"Playback", NULL, "CPU-Playback"}, 124 {"CPU-Capture", NULL, "Capture"}, 125 /* 2nd half -- ASRC DAPM routes */ 126 {"CPU-Playback", NULL, "ASRC-Playback"}, 127 {"ASRC-Capture", NULL, "CPU-Capture"}, 128 }; 129 130 static const struct snd_soc_dapm_route audio_map_ac97[] = { 131 /* 1st half -- Normal DAPM routes */ 132 {"AC97 Playback", NULL, "CPU AC97 Playback"}, 133 {"CPU AC97 Capture", NULL, "AC97 Capture"}, 134 /* 2nd half -- ASRC DAPM routes */ 135 {"CPU AC97 Playback", NULL, "ASRC-Playback"}, 136 {"ASRC-Capture", NULL, "CPU AC97 Capture"}, 137 }; 138 139 static const struct snd_soc_dapm_route audio_map_tx[] = { 140 /* 1st half -- Normal DAPM routes */ 141 {"Playback", NULL, "CPU-Playback"}, 142 /* 2nd half -- ASRC DAPM routes */ 143 {"CPU-Playback", NULL, "ASRC-Playback"}, 144 }; 145 146 static const struct snd_soc_dapm_route audio_map_rx[] = { 147 /* 1st half -- Normal DAPM routes */ 148 {"CPU-Capture", NULL, "Capture"}, 149 /* 2nd half -- ASRC DAPM routes */ 150 {"ASRC-Capture", NULL, "CPU-Capture"}, 151 }; 152 153 /* Add all possible widgets into here without being redundant */ 154 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = { 155 SND_SOC_DAPM_LINE("Line Out Jack", NULL), 156 SND_SOC_DAPM_LINE("Line In Jack", NULL), 157 SND_SOC_DAPM_HP("Headphone Jack", NULL), 158 SND_SOC_DAPM_SPK("Ext Spk", NULL), 159 SND_SOC_DAPM_MIC("Mic Jack", NULL), 160 SND_SOC_DAPM_MIC("AMIC", NULL), 161 SND_SOC_DAPM_MIC("DMIC", NULL), 162 }; 163 164 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv) 165 { 166 return priv->dai_fmt == SND_SOC_DAIFMT_AC97; 167 } 168 169 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream, 170 struct snd_pcm_hw_params *params) 171 { 172 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 173 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card); 174 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 175 struct codec_priv *codec_priv = &priv->codec_priv; 176 struct cpu_priv *cpu_priv = &priv->cpu_priv; 177 struct device *dev = rtd->card->dev; 178 unsigned int pll_out; 179 int ret; 180 181 priv->sample_rate = params_rate(params); 182 priv->sample_format = params_format(params); 183 priv->streams |= BIT(substream->stream); 184 185 if (fsl_asoc_card_is_ac97(priv)) 186 return 0; 187 188 /* Specific configurations of DAIs starts from here */ 189 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx], 190 cpu_priv->sysclk_freq[tx], 191 cpu_priv->sysclk_dir[tx]); 192 if (ret && ret != -ENOTSUPP) { 193 dev_err(dev, "failed to set sysclk for cpu dai\n"); 194 goto fail; 195 } 196 197 if (cpu_priv->slot_width) { 198 if (!cpu_priv->slot_num) 199 cpu_priv->slot_num = 2; 200 201 ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 202 cpu_priv->slot_num, 203 cpu_priv->slot_width); 204 if (ret && ret != -ENOTSUPP) { 205 dev_err(dev, "failed to set TDM slot for cpu dai\n"); 206 goto fail; 207 } 208 } 209 210 /* Specific configuration for PLL */ 211 if (codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) { 212 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE) 213 pll_out = priv->sample_rate * 384; 214 else 215 pll_out = priv->sample_rate * 256; 216 217 ret = snd_soc_dai_set_pll(snd_soc_rtd_to_codec(rtd, 0), 218 codec_priv->pll_id, 219 codec_priv->mclk_id, 220 codec_priv->mclk_freq, pll_out); 221 if (ret) { 222 dev_err(dev, "failed to start FLL: %d\n", ret); 223 goto fail; 224 } 225 226 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_codec(rtd, 0), 227 codec_priv->fll_id, 228 pll_out, SND_SOC_CLOCK_IN); 229 230 if (ret && ret != -ENOTSUPP) { 231 dev_err(dev, "failed to set SYSCLK: %d\n", ret); 232 goto fail; 233 } 234 } 235 236 return 0; 237 238 fail: 239 priv->streams &= ~BIT(substream->stream); 240 return ret; 241 } 242 243 static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream) 244 { 245 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 246 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card); 247 struct codec_priv *codec_priv = &priv->codec_priv; 248 struct device *dev = rtd->card->dev; 249 int ret; 250 251 priv->streams &= ~BIT(substream->stream); 252 253 if (!priv->streams && codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) { 254 /* Force freq to be free_freq to avoid error message in codec */ 255 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_codec(rtd, 0), 256 codec_priv->mclk_id, 257 codec_priv->free_freq, 258 SND_SOC_CLOCK_IN); 259 if (ret) { 260 dev_err(dev, "failed to switch away from FLL: %d\n", ret); 261 return ret; 262 } 263 264 ret = snd_soc_dai_set_pll(snd_soc_rtd_to_codec(rtd, 0), 265 codec_priv->pll_id, 0, 0, 0); 266 if (ret && ret != -ENOTSUPP) { 267 dev_err(dev, "failed to stop FLL: %d\n", ret); 268 return ret; 269 } 270 } 271 272 return 0; 273 } 274 275 static const struct snd_soc_ops fsl_asoc_card_ops = { 276 .hw_params = fsl_asoc_card_hw_params, 277 .hw_free = fsl_asoc_card_hw_free, 278 }; 279 280 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 281 struct snd_pcm_hw_params *params) 282 { 283 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card); 284 struct snd_interval *rate; 285 struct snd_mask *mask; 286 287 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 288 rate->max = rate->min = priv->asrc_rate; 289 290 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 291 snd_mask_none(mask); 292 snd_mask_set_format(mask, priv->asrc_format); 293 294 return 0; 295 } 296 297 SND_SOC_DAILINK_DEFS(hifi, 298 DAILINK_COMP_ARRAY(COMP_EMPTY()), 299 DAILINK_COMP_ARRAY(COMP_EMPTY()), 300 DAILINK_COMP_ARRAY(COMP_EMPTY())); 301 302 SND_SOC_DAILINK_DEFS(hifi_fe, 303 DAILINK_COMP_ARRAY(COMP_EMPTY()), 304 DAILINK_COMP_ARRAY(COMP_DUMMY()), 305 DAILINK_COMP_ARRAY(COMP_EMPTY())); 306 307 SND_SOC_DAILINK_DEFS(hifi_be, 308 DAILINK_COMP_ARRAY(COMP_EMPTY()), 309 DAILINK_COMP_ARRAY(COMP_EMPTY())); 310 311 static const struct snd_soc_dai_link fsl_asoc_card_dai[] = { 312 /* Default ASoC DAI Link*/ 313 { 314 .name = "HiFi", 315 .stream_name = "HiFi", 316 .ops = &fsl_asoc_card_ops, 317 SND_SOC_DAILINK_REG(hifi), 318 }, 319 /* DPCM Link between Front-End and Back-End (Optional) */ 320 { 321 .name = "HiFi-ASRC-FE", 322 .stream_name = "HiFi-ASRC-FE", 323 .dpcm_playback = 1, 324 .dpcm_capture = 1, 325 .dynamic = 1, 326 SND_SOC_DAILINK_REG(hifi_fe), 327 }, 328 { 329 .name = "HiFi-ASRC-BE", 330 .stream_name = "HiFi-ASRC-BE", 331 .be_hw_params_fixup = be_hw_params_fixup, 332 .ops = &fsl_asoc_card_ops, 333 .dpcm_playback = 1, 334 .dpcm_capture = 1, 335 .no_pcm = 1, 336 SND_SOC_DAILINK_REG(hifi_be), 337 }, 338 }; 339 340 static int fsl_asoc_card_audmux_init(struct device_node *np, 341 struct fsl_asoc_card_priv *priv) 342 { 343 struct device *dev = &priv->pdev->dev; 344 u32 int_ptcr = 0, ext_ptcr = 0; 345 int int_port, ext_port; 346 int ret; 347 348 ret = of_property_read_u32(np, "mux-int-port", &int_port); 349 if (ret) { 350 dev_err(dev, "mux-int-port missing or invalid\n"); 351 return ret; 352 } 353 ret = of_property_read_u32(np, "mux-ext-port", &ext_port); 354 if (ret) { 355 dev_err(dev, "mux-ext-port missing or invalid\n"); 356 return ret; 357 } 358 359 /* 360 * The port numbering in the hardware manual starts at 1, while 361 * the AUDMUX API expects it starts at 0. 362 */ 363 int_port--; 364 ext_port--; 365 366 /* 367 * Use asynchronous mode (6 wires) for all cases except AC97. 368 * If only 4 wires are needed, just set SSI into 369 * synchronous mode and enable 4 PADs in IOMUX. 370 */ 371 switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 372 case SND_SOC_DAIFMT_CBP_CFP: 373 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) | 374 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) | 375 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) | 376 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) | 377 IMX_AUDMUX_V2_PTCR_RFSDIR | 378 IMX_AUDMUX_V2_PTCR_RCLKDIR | 379 IMX_AUDMUX_V2_PTCR_TFSDIR | 380 IMX_AUDMUX_V2_PTCR_TCLKDIR; 381 break; 382 case SND_SOC_DAIFMT_CBP_CFC: 383 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) | 384 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) | 385 IMX_AUDMUX_V2_PTCR_RCLKDIR | 386 IMX_AUDMUX_V2_PTCR_TCLKDIR; 387 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) | 388 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) | 389 IMX_AUDMUX_V2_PTCR_RFSDIR | 390 IMX_AUDMUX_V2_PTCR_TFSDIR; 391 break; 392 case SND_SOC_DAIFMT_CBC_CFP: 393 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) | 394 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) | 395 IMX_AUDMUX_V2_PTCR_RFSDIR | 396 IMX_AUDMUX_V2_PTCR_TFSDIR; 397 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) | 398 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) | 399 IMX_AUDMUX_V2_PTCR_RCLKDIR | 400 IMX_AUDMUX_V2_PTCR_TCLKDIR; 401 break; 402 case SND_SOC_DAIFMT_CBC_CFC: 403 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) | 404 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) | 405 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) | 406 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) | 407 IMX_AUDMUX_V2_PTCR_RFSDIR | 408 IMX_AUDMUX_V2_PTCR_RCLKDIR | 409 IMX_AUDMUX_V2_PTCR_TFSDIR | 410 IMX_AUDMUX_V2_PTCR_TCLKDIR; 411 break; 412 default: 413 if (!fsl_asoc_card_is_ac97(priv)) 414 return -EINVAL; 415 } 416 417 if (fsl_asoc_card_is_ac97(priv)) { 418 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN | 419 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) | 420 IMX_AUDMUX_V2_PTCR_TCLKDIR; 421 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN | 422 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) | 423 IMX_AUDMUX_V2_PTCR_TFSDIR; 424 } 425 426 /* Asynchronous mode can not be set along with RCLKDIR */ 427 if (!fsl_asoc_card_is_ac97(priv)) { 428 unsigned int pdcr = 429 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port); 430 431 ret = imx_audmux_v2_configure_port(int_port, 0, 432 pdcr); 433 if (ret) { 434 dev_err(dev, "audmux internal port setup failed\n"); 435 return ret; 436 } 437 } 438 439 ret = imx_audmux_v2_configure_port(int_port, int_ptcr, 440 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)); 441 if (ret) { 442 dev_err(dev, "audmux internal port setup failed\n"); 443 return ret; 444 } 445 446 if (!fsl_asoc_card_is_ac97(priv)) { 447 unsigned int pdcr = 448 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port); 449 450 ret = imx_audmux_v2_configure_port(ext_port, 0, 451 pdcr); 452 if (ret) { 453 dev_err(dev, "audmux external port setup failed\n"); 454 return ret; 455 } 456 } 457 458 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr, 459 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)); 460 if (ret) { 461 dev_err(dev, "audmux external port setup failed\n"); 462 return ret; 463 } 464 465 return 0; 466 } 467 468 static int hp_jack_event(struct notifier_block *nb, unsigned long event, 469 void *data) 470 { 471 struct snd_soc_jack *jack = (struct snd_soc_jack *)data; 472 struct snd_soc_dapm_context *dapm = &jack->card->dapm; 473 474 if (event & SND_JACK_HEADPHONE) 475 /* Disable speaker if headphone is plugged in */ 476 return snd_soc_dapm_disable_pin(dapm, "Ext Spk"); 477 else 478 return snd_soc_dapm_enable_pin(dapm, "Ext Spk"); 479 } 480 481 static struct notifier_block hp_jack_nb = { 482 .notifier_call = hp_jack_event, 483 }; 484 485 static int mic_jack_event(struct notifier_block *nb, unsigned long event, 486 void *data) 487 { 488 struct snd_soc_jack *jack = (struct snd_soc_jack *)data; 489 struct snd_soc_dapm_context *dapm = &jack->card->dapm; 490 491 if (event & SND_JACK_MICROPHONE) 492 /* Disable dmic if microphone is plugged in */ 493 return snd_soc_dapm_disable_pin(dapm, "DMIC"); 494 else 495 return snd_soc_dapm_enable_pin(dapm, "DMIC"); 496 } 497 498 static struct notifier_block mic_jack_nb = { 499 .notifier_call = mic_jack_event, 500 }; 501 502 static int fsl_asoc_card_late_probe(struct snd_soc_card *card) 503 { 504 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card); 505 struct snd_soc_pcm_runtime *rtd = list_first_entry( 506 &card->rtd_list, struct snd_soc_pcm_runtime, list); 507 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 508 struct codec_priv *codec_priv = &priv->codec_priv; 509 struct device *dev = card->dev; 510 int ret; 511 512 if (fsl_asoc_card_is_ac97(priv)) { 513 #if IS_ENABLED(CONFIG_SND_AC97_CODEC) 514 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component; 515 struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component); 516 517 /* 518 * Use slots 3/4 for S/PDIF so SSI won't try to enable 519 * other slots and send some samples there 520 * due to SLOTREQ bits for S/PDIF received from codec 521 */ 522 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, 523 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4); 524 #endif 525 526 return 0; 527 } 528 529 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id, 530 codec_priv->mclk_freq, SND_SOC_CLOCK_IN); 531 if (ret && ret != -ENOTSUPP) { 532 dev_err(dev, "failed to set sysclk in %s\n", __func__); 533 return ret; 534 } 535 536 if (!IS_ERR_OR_NULL(codec_priv->mclk)) 537 clk_prepare_enable(codec_priv->mclk); 538 539 return 0; 540 } 541 542 static int fsl_asoc_card_probe(struct platform_device *pdev) 543 { 544 struct device_node *cpu_np, *codec_np, *asrc_np; 545 struct device_node *np = pdev->dev.of_node; 546 struct platform_device *asrc_pdev = NULL; 547 struct device_node *bitclkprovider = NULL; 548 struct device_node *frameprovider = NULL; 549 struct platform_device *cpu_pdev; 550 struct fsl_asoc_card_priv *priv; 551 struct device *codec_dev = NULL; 552 const char *codec_dai_name; 553 const char *codec_dev_name; 554 u32 asrc_fmt = 0; 555 u32 width; 556 int ret; 557 558 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 559 if (!priv) 560 return -ENOMEM; 561 562 priv->pdev = pdev; 563 564 cpu_np = of_parse_phandle(np, "audio-cpu", 0); 565 /* Give a chance to old DT binding */ 566 if (!cpu_np) 567 cpu_np = of_parse_phandle(np, "ssi-controller", 0); 568 if (!cpu_np) { 569 dev_err(&pdev->dev, "CPU phandle missing or invalid\n"); 570 ret = -EINVAL; 571 goto fail; 572 } 573 574 cpu_pdev = of_find_device_by_node(cpu_np); 575 if (!cpu_pdev) { 576 dev_err(&pdev->dev, "failed to find CPU DAI device\n"); 577 ret = -EINVAL; 578 goto fail; 579 } 580 581 codec_np = of_parse_phandle(np, "audio-codec", 0); 582 if (codec_np) { 583 struct platform_device *codec_pdev; 584 struct i2c_client *codec_i2c; 585 586 codec_i2c = of_find_i2c_device_by_node(codec_np); 587 if (codec_i2c) { 588 codec_dev = &codec_i2c->dev; 589 codec_dev_name = codec_i2c->name; 590 } 591 if (!codec_dev) { 592 codec_pdev = of_find_device_by_node(codec_np); 593 if (codec_pdev) { 594 codec_dev = &codec_pdev->dev; 595 codec_dev_name = codec_pdev->name; 596 } 597 } 598 } 599 600 asrc_np = of_parse_phandle(np, "audio-asrc", 0); 601 if (asrc_np) 602 asrc_pdev = of_find_device_by_node(asrc_np); 603 604 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */ 605 if (codec_dev) { 606 struct clk *codec_clk = clk_get(codec_dev, NULL); 607 608 if (!IS_ERR(codec_clk)) { 609 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk); 610 clk_put(codec_clk); 611 } 612 } 613 614 /* Default sample rate and format, will be updated in hw_params() */ 615 priv->sample_rate = 44100; 616 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE; 617 618 /* Assign a default DAI format, and allow each card to overwrite it */ 619 priv->dai_fmt = DAI_FMT_BASE; 620 621 memcpy(priv->dai_link, fsl_asoc_card_dai, 622 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link)); 623 624 priv->card.dapm_routes = audio_map; 625 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map); 626 priv->card.driver_name = DRIVER_NAME; 627 628 priv->codec_priv.fll_id = -1; 629 priv->codec_priv.pll_id = -1; 630 631 /* Diversify the card configurations */ 632 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) { 633 codec_dai_name = "cs42888"; 634 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq; 635 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq; 636 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT; 637 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT; 638 priv->cpu_priv.slot_width = 32; 639 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC; 640 } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) { 641 codec_dai_name = "cs4271-hifi"; 642 priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK; 643 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 644 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) { 645 codec_dai_name = "sgtl5000"; 646 priv->codec_priv.mclk_id = SGTL5000_SYSCLK; 647 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 648 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) { 649 codec_dai_name = "tlv320aic32x4-hifi"; 650 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 651 } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) { 652 codec_dai_name = "tlv320dac31xx-hifi"; 653 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 654 priv->dai_link[1].dpcm_capture = 0; 655 priv->dai_link[2].dpcm_capture = 0; 656 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT; 657 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT; 658 priv->card.dapm_routes = audio_map_tx; 659 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx); 660 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) { 661 codec_dai_name = "wm8962"; 662 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK; 663 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL; 664 priv->codec_priv.pll_id = WM8962_FLL; 665 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 666 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) { 667 codec_dai_name = "wm8960-hifi"; 668 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO; 669 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO; 670 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 671 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) { 672 codec_dai_name = "ac97-hifi"; 673 priv->dai_fmt = SND_SOC_DAIFMT_AC97; 674 priv->card.dapm_routes = audio_map_ac97; 675 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97); 676 } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) { 677 codec_dai_name = "fsl-mqs-dai"; 678 priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J | 679 SND_SOC_DAIFMT_CBC_CFC | 680 SND_SOC_DAIFMT_NB_NF; 681 priv->dai_link[1].dpcm_capture = 0; 682 priv->dai_link[2].dpcm_capture = 0; 683 priv->card.dapm_routes = audio_map_tx; 684 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx); 685 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) { 686 codec_dai_name = "wm8524-hifi"; 687 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC; 688 priv->dai_link[1].dpcm_capture = 0; 689 priv->dai_link[2].dpcm_capture = 0; 690 priv->cpu_priv.slot_width = 32; 691 priv->card.dapm_routes = audio_map_tx; 692 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx); 693 } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) { 694 codec_dai_name = "si476x-codec"; 695 priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC; 696 priv->card.dapm_routes = audio_map_rx; 697 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx); 698 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) { 699 codec_dai_name = "wm8994-aif1"; 700 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 701 priv->codec_priv.mclk_id = WM8994_FLL_SRC_MCLK1; 702 priv->codec_priv.fll_id = WM8994_SYSCLK_FLL1; 703 priv->codec_priv.pll_id = WM8994_FLL1; 704 priv->codec_priv.free_freq = priv->codec_priv.mclk_freq; 705 priv->card.dapm_routes = NULL; 706 priv->card.num_dapm_routes = 0; 707 } else if (of_device_is_compatible(np, "fsl,imx-audio-nau8822")) { 708 codec_dai_name = "nau8822-hifi"; 709 priv->codec_priv.mclk_id = NAU8822_CLK_MCLK; 710 priv->codec_priv.fll_id = NAU8822_CLK_PLL; 711 priv->codec_priv.pll_id = NAU8822_CLK_PLL; 712 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 713 if (codec_dev) 714 priv->codec_priv.mclk = devm_clk_get(codec_dev, NULL); 715 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8904")) { 716 codec_dai_name = "wm8904-hifi"; 717 priv->codec_priv.mclk_id = WM8904_FLL_MCLK; 718 priv->codec_priv.fll_id = WM8904_CLK_FLL; 719 priv->codec_priv.pll_id = WM8904_FLL_MCLK; 720 priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 721 } else { 722 dev_err(&pdev->dev, "unknown Device Tree compatible\n"); 723 ret = -EINVAL; 724 goto asrc_fail; 725 } 726 727 /* 728 * Allow setting mclk-id from the device-tree node. Otherwise, the 729 * default value for each card configuration is used. 730 */ 731 of_property_read_u32(np, "mclk-id", &priv->codec_priv.mclk_id); 732 733 /* Format info from DT is optional. */ 734 snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider); 735 if (bitclkprovider || frameprovider) { 736 unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL); 737 738 if (codec_np == bitclkprovider) 739 daifmt |= (codec_np == frameprovider) ? 740 SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC; 741 else 742 daifmt |= (codec_np == frameprovider) ? 743 SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC; 744 745 /* Override dai_fmt with value from DT */ 746 priv->dai_fmt = daifmt; 747 } 748 749 /* Change direction according to format */ 750 if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) { 751 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN; 752 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN; 753 } 754 755 of_node_put(bitclkprovider); 756 of_node_put(frameprovider); 757 758 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) { 759 dev_dbg(&pdev->dev, "failed to find codec device\n"); 760 ret = -EPROBE_DEFER; 761 goto asrc_fail; 762 } 763 764 /* Common settings for corresponding Freescale CPU DAI driver */ 765 if (of_node_name_eq(cpu_np, "ssi")) { 766 /* Only SSI needs to configure AUDMUX */ 767 ret = fsl_asoc_card_audmux_init(np, priv); 768 if (ret) { 769 dev_err(&pdev->dev, "failed to init audmux\n"); 770 goto asrc_fail; 771 } 772 } else if (of_node_name_eq(cpu_np, "esai")) { 773 struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal"); 774 775 if (!IS_ERR(esai_clk)) { 776 priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk); 777 priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk); 778 clk_put(esai_clk); 779 } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) { 780 ret = -EPROBE_DEFER; 781 goto asrc_fail; 782 } 783 784 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL; 785 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL; 786 } else if (of_node_name_eq(cpu_np, "sai")) { 787 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1; 788 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1; 789 } 790 791 /* Initialize sound card */ 792 priv->card.dev = &pdev->dev; 793 priv->card.owner = THIS_MODULE; 794 ret = snd_soc_of_parse_card_name(&priv->card, "model"); 795 if (ret) { 796 snprintf(priv->name, sizeof(priv->name), "%s-audio", 797 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name); 798 priv->card.name = priv->name; 799 } 800 priv->card.dai_link = priv->dai_link; 801 priv->card.late_probe = fsl_asoc_card_late_probe; 802 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets; 803 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets); 804 805 /* Drop the second half of DAPM routes -- ASRC */ 806 if (!asrc_pdev) 807 priv->card.num_dapm_routes /= 2; 808 809 if (of_property_read_bool(np, "audio-routing")) { 810 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing"); 811 if (ret) { 812 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret); 813 goto asrc_fail; 814 } 815 } 816 817 /* Normal DAI Link */ 818 priv->dai_link[0].cpus->of_node = cpu_np; 819 priv->dai_link[0].codecs->dai_name = codec_dai_name; 820 821 if (!fsl_asoc_card_is_ac97(priv)) 822 priv->dai_link[0].codecs->of_node = codec_np; 823 else { 824 u32 idx; 825 826 ret = of_property_read_u32(cpu_np, "cell-index", &idx); 827 if (ret) { 828 dev_err(&pdev->dev, 829 "cannot get CPU index property\n"); 830 goto asrc_fail; 831 } 832 833 priv->dai_link[0].codecs->name = 834 devm_kasprintf(&pdev->dev, GFP_KERNEL, 835 "ac97-codec.%u", 836 (unsigned int)idx); 837 if (!priv->dai_link[0].codecs->name) { 838 ret = -ENOMEM; 839 goto asrc_fail; 840 } 841 } 842 843 priv->dai_link[0].platforms->of_node = cpu_np; 844 priv->dai_link[0].dai_fmt = priv->dai_fmt; 845 priv->card.num_links = 1; 846 847 if (asrc_pdev) { 848 /* DPCM DAI Links only if ASRC exists */ 849 priv->dai_link[1].cpus->of_node = asrc_np; 850 priv->dai_link[1].platforms->of_node = asrc_np; 851 priv->dai_link[2].codecs->dai_name = codec_dai_name; 852 priv->dai_link[2].codecs->of_node = codec_np; 853 priv->dai_link[2].codecs->name = 854 priv->dai_link[0].codecs->name; 855 priv->dai_link[2].cpus->of_node = cpu_np; 856 priv->dai_link[2].dai_fmt = priv->dai_fmt; 857 priv->card.num_links = 3; 858 859 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate", 860 &priv->asrc_rate); 861 if (ret) { 862 dev_err(&pdev->dev, "failed to get output rate\n"); 863 ret = -EINVAL; 864 goto asrc_fail; 865 } 866 867 ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt); 868 priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt; 869 if (ret) { 870 /* Fallback to old binding; translate to asrc_format */ 871 ret = of_property_read_u32(asrc_np, "fsl,asrc-width", 872 &width); 873 if (ret) { 874 dev_err(&pdev->dev, 875 "failed to decide output format\n"); 876 goto asrc_fail; 877 } 878 879 if (width == 24) 880 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE; 881 else 882 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE; 883 } 884 } 885 886 /* Finish card registering */ 887 platform_set_drvdata(pdev, priv); 888 snd_soc_card_set_drvdata(&priv->card, priv); 889 890 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card); 891 if (ret) { 892 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n"); 893 goto asrc_fail; 894 } 895 896 /* 897 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and 898 * simple_util_init_jack() uses these properties for creating 899 * Headphone Jack and Microphone Jack. 900 * 901 * The notifier is initialized in snd_soc_card_jack_new(), then 902 * snd_soc_jack_notifier_register can be called. 903 */ 904 if (of_property_read_bool(np, "hp-det-gpio")) { 905 ret = simple_util_init_jack(&priv->card, &priv->hp_jack, 906 1, NULL, "Headphone Jack"); 907 if (ret) 908 goto asrc_fail; 909 910 snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb); 911 } 912 913 if (of_property_read_bool(np, "mic-det-gpio")) { 914 ret = simple_util_init_jack(&priv->card, &priv->mic_jack, 915 0, NULL, "Mic Jack"); 916 if (ret) 917 goto asrc_fail; 918 919 snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb); 920 } 921 922 asrc_fail: 923 of_node_put(asrc_np); 924 of_node_put(codec_np); 925 put_device(&cpu_pdev->dev); 926 fail: 927 of_node_put(cpu_np); 928 929 return ret; 930 } 931 932 static const struct of_device_id fsl_asoc_card_dt_ids[] = { 933 { .compatible = "fsl,imx-audio-ac97", }, 934 { .compatible = "fsl,imx-audio-cs42888", }, 935 { .compatible = "fsl,imx-audio-cs427x", }, 936 { .compatible = "fsl,imx-audio-tlv320aic32x4", }, 937 { .compatible = "fsl,imx-audio-tlv320aic31xx", }, 938 { .compatible = "fsl,imx-audio-sgtl5000", }, 939 { .compatible = "fsl,imx-audio-wm8962", }, 940 { .compatible = "fsl,imx-audio-wm8960", }, 941 { .compatible = "fsl,imx-audio-mqs", }, 942 { .compatible = "fsl,imx-audio-wm8524", }, 943 { .compatible = "fsl,imx-audio-si476x", }, 944 { .compatible = "fsl,imx-audio-wm8958", }, 945 { .compatible = "fsl,imx-audio-nau8822", }, 946 { .compatible = "fsl,imx-audio-wm8904", }, 947 {} 948 }; 949 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids); 950 951 static struct platform_driver fsl_asoc_card_driver = { 952 .probe = fsl_asoc_card_probe, 953 .driver = { 954 .name = DRIVER_NAME, 955 .pm = &snd_soc_pm_ops, 956 .of_match_table = fsl_asoc_card_dt_ids, 957 }, 958 }; 959 module_platform_driver(fsl_asoc_card_driver); 960 961 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC"); 962 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>"); 963 MODULE_ALIAS("platform:" DRIVER_NAME); 964 MODULE_LICENSE("GPL"); 965