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