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