1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright 2017-2021 NXP 3 4 #include <linux/module.h> 5 #include <linux/init.h> 6 #include <linux/slab.h> 7 #include <linux/gpio/consumer.h> 8 #include <linux/of_device.h> 9 #include <linux/i2c.h> 10 #include <linux/of_gpio.h> 11 #include <linux/clk.h> 12 #include <sound/soc.h> 13 #include <sound/pcm_params.h> 14 #include <sound/pcm.h> 15 #include <sound/soc-dapm.h> 16 #include <sound/simple_card_utils.h> 17 18 #include "fsl_sai.h" 19 20 enum codec_type { 21 CODEC_DUMMY = 0, 22 CODEC_AK5558 = 1, 23 CODEC_AK4458, 24 CODEC_AK4497, 25 CODEC_AK5552, 26 }; 27 28 /* 29 * Mapping LRCK fs and frame width, table 3 & 4 in datasheet 30 * @rmin: min rate 31 * @rmax: max rate 32 * @wmin: min frame ratio 33 * @wmax: max frame ratio 34 */ 35 struct imx_akcodec_fs_mul { 36 unsigned int rmin; 37 unsigned int rmax; 38 unsigned int wmin; 39 unsigned int wmax; 40 }; 41 42 /* 43 * Mapping TDM mode and frame width 44 */ 45 struct imx_akcodec_tdm_fs_mul { 46 unsigned int min; 47 unsigned int max; 48 unsigned int mul; 49 }; 50 51 /* 52 * struct imx_card_plat_data - specific info for codecs 53 * 54 * @fs_mul: ratio of mclk/fs for normal mode 55 * @tdm_fs_mul: ratio of mclk/fs for tdm mode 56 * @support_rates: supported sample rate 57 * @support_tdm_rates: supported sample rate for tdm mode 58 * @support_channels: supported channels 59 * @support_tdm_channels: supported channels for tdm mode 60 * @num_fs_mul: ARRAY_SIZE of fs_mul 61 * @num_tdm_fs_mul: ARRAY_SIZE of tdm_fs_mul 62 * @num_rates: ARRAY_SIZE of support_rates 63 * @num_tdm_rates: ARRAY_SIZE of support_tdm_rates 64 * @num_channels: ARRAY_SIZE of support_channels 65 * @num_tdm_channels: ARRAY_SIZE of support_tdm_channels 66 * @type: codec type 67 */ 68 struct imx_card_plat_data { 69 struct imx_akcodec_fs_mul *fs_mul; 70 struct imx_akcodec_tdm_fs_mul *tdm_fs_mul; 71 const u32 *support_rates; 72 const u32 *support_tdm_rates; 73 const u32 *support_channels; 74 const u32 *support_tdm_channels; 75 unsigned int num_fs_mul; 76 unsigned int num_tdm_fs_mul; 77 unsigned int num_rates; 78 unsigned int num_tdm_rates; 79 unsigned int num_channels; 80 unsigned int num_tdm_channels; 81 unsigned int num_codecs; 82 enum codec_type type; 83 }; 84 85 /* 86 * struct dai_link_data - specific info for dai link 87 * 88 * @slots: slot number 89 * @slot_width: slot width value 90 * @cpu_sysclk_id: sysclk id for cpu dai 91 * @one2one_ratio: true if mclk equal to bclk 92 */ 93 struct dai_link_data { 94 unsigned int slots; 95 unsigned int slot_width; 96 unsigned int cpu_sysclk_id; 97 bool one2one_ratio; 98 }; 99 100 /* 101 * struct imx_card_data - platform device data 102 * 103 * @plat_data: pointer of imx_card_plat_data 104 * @dapm_routes: pointer of dapm_routes 105 * @link_data: private data for dai link 106 * @card: card instance 107 * @num_dapm_routes: number of dapm_routes 108 * @asrc_rate: asrc rates 109 * @asrc_format: asrc format 110 */ 111 struct imx_card_data { 112 struct imx_card_plat_data *plat_data; 113 struct snd_soc_dapm_route *dapm_routes; 114 struct dai_link_data *link_data; 115 struct snd_soc_card card; 116 int num_dapm_routes; 117 u32 asrc_rate; 118 u32 asrc_format; 119 }; 120 121 static struct imx_akcodec_fs_mul ak4458_fs_mul[] = { 122 /* Normal, < 32kHz */ 123 { .rmin = 8000, .rmax = 24000, .wmin = 1024, .wmax = 1024, }, 124 /* Normal, 32kHz */ 125 { .rmin = 32000, .rmax = 32000, .wmin = 256, .wmax = 1024, }, 126 /* Normal */ 127 { .rmin = 44100, .rmax = 48000, .wmin = 256, .wmax = 768, }, 128 /* Double */ 129 { .rmin = 88200, .rmax = 96000, .wmin = 256, .wmax = 512, }, 130 /* Quad */ 131 { .rmin = 176400, .rmax = 192000, .wmin = 128, .wmax = 256, }, 132 /* Oct */ 133 { .rmin = 352800, .rmax = 384000, .wmin = 32, .wmax = 128, }, 134 /* Hex */ 135 { .rmin = 705600, .rmax = 768000, .wmin = 16, .wmax = 64, }, 136 }; 137 138 static struct imx_akcodec_tdm_fs_mul ak4458_tdm_fs_mul[] = { 139 /* 140 * Table 13 - Audio Interface Format 141 * For TDM mode, MCLK should is set to 142 * obtained from 2 * slots * slot_width 143 */ 144 { .min = 128, .max = 128, .mul = 256 }, /* TDM128 */ 145 { .min = 256, .max = 256, .mul = 512 }, /* TDM256 */ 146 { .min = 512, .max = 512, .mul = 1024 }, /* TDM512 */ 147 }; 148 149 static struct imx_akcodec_fs_mul ak4497_fs_mul[] = { 150 /** 151 * Table 7 - mapping multiplier and speed mode 152 * Tables 8 & 9 - mapping speed mode and LRCK fs 153 */ 154 { .rmin = 8000, .rmax = 32000, .wmin = 1024, .wmax = 1024, }, /* Normal, <= 32kHz */ 155 { .rmin = 44100, .rmax = 48000, .wmin = 512, .wmax = 512, }, /* Normal */ 156 { .rmin = 88200, .rmax = 96000, .wmin = 256, .wmax = 256, }, /* Double */ 157 { .rmin = 176400, .rmax = 192000, .wmin = 128, .wmax = 128, }, /* Quad */ 158 { .rmin = 352800, .rmax = 384000, .wmin = 128, .wmax = 128, }, /* Oct */ 159 { .rmin = 705600, .rmax = 768000, .wmin = 64, .wmax = 64, }, /* Hex */ 160 }; 161 162 /* 163 * Auto MCLK selection based on LRCK for Normal Mode 164 * (Table 4 from datasheet) 165 */ 166 static struct imx_akcodec_fs_mul ak5558_fs_mul[] = { 167 { .rmin = 8000, .rmax = 32000, .wmin = 1024, .wmax = 1024, }, 168 { .rmin = 44100, .rmax = 48000, .wmin = 512, .wmax = 512, }, 169 { .rmin = 88200, .rmax = 96000, .wmin = 256, .wmax = 256, }, 170 { .rmin = 176400, .rmax = 192000, .wmin = 128, .wmax = 128, }, 171 { .rmin = 352800, .rmax = 384000, .wmin = 64, .wmax = 64, }, 172 { .rmin = 705600, .rmax = 768000, .wmin = 32, .wmax = 32, }, 173 }; 174 175 /* 176 * MCLK and BCLK selection based on TDM mode 177 * because of SAI we also add the restriction: MCLK >= 2 * BCLK 178 * (Table 9 from datasheet) 179 */ 180 static struct imx_akcodec_tdm_fs_mul ak5558_tdm_fs_mul[] = { 181 { .min = 128, .max = 128, .mul = 256 }, 182 { .min = 256, .max = 256, .mul = 512 }, 183 { .min = 512, .max = 512, .mul = 1024 }, 184 }; 185 186 static const u32 akcodec_rates[] = { 187 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200, 188 96000, 176400, 192000, 352800, 384000, 705600, 768000, 189 }; 190 191 static const u32 akcodec_tdm_rates[] = { 192 8000, 16000, 32000, 48000, 96000, 193 }; 194 195 static const u32 ak4458_channels[] = { 196 1, 2, 4, 6, 8, 10, 12, 14, 16, 197 }; 198 199 static const u32 ak4458_tdm_channels[] = { 200 1, 2, 3, 4, 5, 6, 7, 8, 16, 201 }; 202 203 static const u32 ak5558_channels[] = { 204 1, 2, 4, 6, 8, 205 }; 206 207 static const u32 ak5558_tdm_channels[] = { 208 1, 2, 3, 4, 5, 6, 7, 8, 209 }; 210 211 static bool format_is_dsd(struct snd_pcm_hw_params *params) 212 { 213 snd_pcm_format_t format = params_format(params); 214 215 switch (format) { 216 case SNDRV_PCM_FORMAT_DSD_U8: 217 case SNDRV_PCM_FORMAT_DSD_U16_LE: 218 case SNDRV_PCM_FORMAT_DSD_U16_BE: 219 case SNDRV_PCM_FORMAT_DSD_U32_LE: 220 case SNDRV_PCM_FORMAT_DSD_U32_BE: 221 return true; 222 default: 223 return false; 224 } 225 } 226 227 static bool format_is_tdm(struct dai_link_data *link_data) 228 { 229 if (link_data->slots > 2) 230 return true; 231 else 232 return false; 233 } 234 235 static bool codec_is_akcodec(unsigned int type) 236 { 237 switch (type) { 238 case CODEC_AK4458: 239 case CODEC_AK4497: 240 case CODEC_AK5558: 241 case CODEC_AK5552: 242 return true; 243 default: 244 break; 245 } 246 return false; 247 } 248 249 static unsigned long akcodec_get_mclk_rate(struct snd_pcm_substream *substream, 250 struct snd_pcm_hw_params *params) 251 { 252 struct snd_soc_pcm_runtime *rtd = substream->private_data; 253 struct imx_card_data *data = snd_soc_card_get_drvdata(rtd->card); 254 const struct imx_card_plat_data *plat_data = data->plat_data; 255 struct dai_link_data *link_data = &data->link_data[rtd->num]; 256 unsigned int width = link_data->slots * link_data->slot_width; 257 unsigned int rate = params_rate(params); 258 int i; 259 260 if (format_is_tdm(link_data)) { 261 for (i = 0; i < plat_data->num_tdm_fs_mul; i++) { 262 /* min = max = slots * slots_width */ 263 if (width != plat_data->tdm_fs_mul[i].min) 264 continue; 265 return rate * plat_data->tdm_fs_mul[i].mul; 266 } 267 } else { 268 for (i = 0; i < plat_data->num_fs_mul; i++) { 269 if (rate >= plat_data->fs_mul[i].rmin && 270 rate <= plat_data->fs_mul[i].rmax) { 271 width = max(width, plat_data->fs_mul[i].wmin); 272 width = min(width, plat_data->fs_mul[i].wmax); 273 274 /* Adjust SAI bclk:mclk ratio */ 275 width *= link_data->one2one_ratio ? 1 : 2; 276 277 return rate * width; 278 } 279 } 280 } 281 282 /* Let DAI manage clk frequency by default */ 283 return 0; 284 } 285 286 static int imx_aif_hw_params(struct snd_pcm_substream *substream, 287 struct snd_pcm_hw_params *params) 288 { 289 struct snd_soc_pcm_runtime *rtd = substream->private_data; 290 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 291 struct snd_soc_card *card = rtd->card; 292 struct imx_card_data *data = snd_soc_card_get_drvdata(card); 293 struct dai_link_data *link_data = &data->link_data[rtd->num]; 294 struct imx_card_plat_data *plat_data = data->plat_data; 295 struct device *dev = card->dev; 296 struct snd_soc_dai *codec_dai; 297 unsigned long mclk_freq; 298 unsigned int fmt = rtd->dai_link->dai_fmt; 299 unsigned int slots, slot_width; 300 int ret, i; 301 302 slots = link_data->slots; 303 slot_width = link_data->slot_width; 304 305 if (!format_is_tdm(link_data)) { 306 if (format_is_dsd(params)) { 307 slots = 1; 308 slot_width = params_width(params); 309 fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | 310 SND_SOC_DAIFMT_PDM; 311 } else { 312 slots = 2; 313 slot_width = params_physical_width(params); 314 fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | 315 SND_SOC_DAIFMT_I2S; 316 } 317 } 318 319 ret = snd_soc_dai_set_fmt(cpu_dai, fmt); 320 if (ret && ret != -ENOTSUPP) { 321 dev_err(dev, "failed to set cpu dai fmt: %d\n", ret); 322 return ret; 323 } 324 ret = snd_soc_dai_set_tdm_slot(cpu_dai, 325 BIT(slots) - 1, 326 BIT(slots) - 1, 327 slots, slot_width); 328 if (ret && ret != -ENOTSUPP) { 329 dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret); 330 return ret; 331 } 332 333 for_each_rtd_codec_dais(rtd, i, codec_dai) { 334 ret = snd_soc_dai_set_fmt(codec_dai, fmt); 335 if (ret && ret != -ENOTSUPP) { 336 dev_err(dev, "failed to set codec dai[%d] fmt: %d\n", i, ret); 337 return ret; 338 } 339 340 ret = snd_soc_dai_set_tdm_slot(codec_dai, 341 BIT(slots) - 1, 342 BIT(slots) - 1, 343 slots, slot_width); 344 if (ret && ret != -ENOTSUPP) { 345 dev_err(dev, "failed to set codec dai[%d] tdm slot: %d\n", i, ret); 346 return ret; 347 } 348 } 349 350 /* Set MCLK freq */ 351 if (codec_is_akcodec(plat_data->type)) 352 mclk_freq = akcodec_get_mclk_rate(substream, params); 353 else 354 mclk_freq = params_rate(params) * slots * slot_width; 355 /* Use the maximum freq from DSD512 (512*44100 = 22579200) */ 356 if (format_is_dsd(params)) 357 mclk_freq = 22579200; 358 359 ret = snd_soc_dai_set_sysclk(cpu_dai, link_data->cpu_sysclk_id, mclk_freq, 360 SND_SOC_CLOCK_OUT); 361 if (ret && ret != -ENOTSUPP) { 362 dev_err(dev, "failed to set cpui dai mclk1 rate (%lu): %d\n", mclk_freq, ret); 363 return ret; 364 } 365 366 return 0; 367 } 368 369 static int ak5558_hw_rule_rate(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *r) 370 { 371 struct dai_link_data *link_data = r->private; 372 struct snd_interval t = { .min = 8000, .max = 8000, }; 373 unsigned long mclk_freq; 374 unsigned int fs; 375 int i; 376 377 fs = hw_param_interval(p, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min; 378 fs *= link_data->slots; 379 380 /* Identify maximum supported rate */ 381 for (i = 0; i < ARRAY_SIZE(akcodec_rates); i++) { 382 mclk_freq = fs * akcodec_rates[i]; 383 /* Adjust SAI bclk:mclk ratio */ 384 mclk_freq *= link_data->one2one_ratio ? 1 : 2; 385 386 /* Skip rates for which MCLK is beyond supported value */ 387 if (mclk_freq > 36864000) 388 continue; 389 390 if (t.max < akcodec_rates[i]) 391 t.max = akcodec_rates[i]; 392 } 393 394 return snd_interval_refine(hw_param_interval(p, r->var), &t); 395 } 396 397 static int imx_aif_startup(struct snd_pcm_substream *substream) 398 { 399 struct snd_pcm_runtime *runtime = substream->runtime; 400 struct snd_soc_pcm_runtime *rtd = substream->private_data; 401 struct snd_soc_card *card = rtd->card; 402 struct imx_card_data *data = snd_soc_card_get_drvdata(card); 403 struct dai_link_data *link_data = &data->link_data[rtd->num]; 404 static struct snd_pcm_hw_constraint_list constraint_rates; 405 static struct snd_pcm_hw_constraint_list constraint_channels; 406 int ret = 0; 407 408 if (format_is_tdm(link_data)) { 409 constraint_channels.list = data->plat_data->support_tdm_channels; 410 constraint_channels.count = data->plat_data->num_tdm_channels; 411 constraint_rates.list = data->plat_data->support_tdm_rates; 412 constraint_rates.count = data->plat_data->num_tdm_rates; 413 } else { 414 constraint_channels.list = data->plat_data->support_channels; 415 constraint_channels.count = data->plat_data->num_channels; 416 constraint_rates.list = data->plat_data->support_rates; 417 constraint_rates.count = data->plat_data->num_rates; 418 } 419 420 if (constraint_channels.count) { 421 ret = snd_pcm_hw_constraint_list(runtime, 0, 422 SNDRV_PCM_HW_PARAM_CHANNELS, 423 &constraint_channels); 424 if (ret) 425 return ret; 426 } 427 428 if (constraint_rates.count) { 429 ret = snd_pcm_hw_constraint_list(runtime, 0, 430 SNDRV_PCM_HW_PARAM_RATE, 431 &constraint_rates); 432 if (ret) 433 return ret; 434 } 435 436 if (data->plat_data->type == CODEC_AK5558) 437 ret = snd_pcm_hw_rule_add(substream->runtime, 0, 438 SNDRV_PCM_HW_PARAM_RATE, 439 ak5558_hw_rule_rate, link_data, 440 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 441 442 return ret; 443 } 444 445 static const struct snd_soc_ops imx_aif_ops = { 446 .hw_params = imx_aif_hw_params, 447 .startup = imx_aif_startup, 448 }; 449 450 static const struct snd_soc_ops imx_aif_ops_be = { 451 .hw_params = imx_aif_hw_params, 452 }; 453 454 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 455 struct snd_pcm_hw_params *params) 456 { 457 struct snd_soc_card *card = rtd->card; 458 struct imx_card_data *data = snd_soc_card_get_drvdata(card); 459 struct snd_interval *rate; 460 struct snd_mask *mask; 461 462 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 463 rate->max = data->asrc_rate; 464 rate->min = data->asrc_rate; 465 466 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 467 snd_mask_none(mask); 468 snd_mask_set(mask, data->asrc_format); 469 470 return 0; 471 } 472 473 static int imx_card_parse_of(struct imx_card_data *data) 474 { 475 struct imx_card_plat_data *plat_data = data->plat_data; 476 struct snd_soc_card *card = &data->card; 477 struct snd_soc_dai_link_component *dlc; 478 struct device_node *platform = NULL; 479 struct device_node *codec = NULL; 480 struct device_node *cpu = NULL; 481 struct device_node *np; 482 struct device *dev = card->dev; 483 struct snd_soc_dai_link *link; 484 struct dai_link_data *link_data; 485 struct of_phandle_args args; 486 int ret, num_links; 487 u32 width; 488 489 ret = snd_soc_of_parse_card_name(card, "model"); 490 if (ret) { 491 dev_err(dev, "Error parsing card name: %d\n", ret); 492 return ret; 493 } 494 495 /* DAPM routes */ 496 if (of_property_read_bool(dev->of_node, "audio-routing")) { 497 ret = snd_soc_of_parse_audio_routing(card, "audio-routing"); 498 if (ret) 499 return ret; 500 } 501 502 /* Populate links */ 503 num_links = of_get_child_count(dev->of_node); 504 505 /* Allocate the DAI link array */ 506 card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL); 507 if (!card->dai_link) 508 return -ENOMEM; 509 510 data->link_data = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL); 511 if (!data->link_data) 512 return -ENOMEM; 513 514 card->num_links = num_links; 515 link = card->dai_link; 516 link_data = data->link_data; 517 518 for_each_child_of_node(dev->of_node, np) { 519 dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL); 520 if (!dlc) { 521 ret = -ENOMEM; 522 goto err_put_np; 523 } 524 525 link->cpus = &dlc[0]; 526 link->platforms = &dlc[1]; 527 528 link->num_cpus = 1; 529 link->num_platforms = 1; 530 531 ret = of_property_read_string(np, "link-name", &link->name); 532 if (ret) { 533 dev_err(card->dev, "error getting codec dai_link name\n"); 534 goto err_put_np; 535 } 536 537 cpu = of_get_child_by_name(np, "cpu"); 538 if (!cpu) { 539 dev_err(dev, "%s: Can't find cpu DT node\n", link->name); 540 ret = -EINVAL; 541 goto err; 542 } 543 544 ret = of_parse_phandle_with_args(cpu, "sound-dai", 545 "#sound-dai-cells", 0, &args); 546 if (ret) { 547 dev_err(card->dev, "%s: error getting cpu phandle\n", link->name); 548 goto err; 549 } 550 551 if (of_node_name_eq(args.np, "sai")) { 552 /* sai sysclk id */ 553 link_data->cpu_sysclk_id = FSL_SAI_CLK_MAST1; 554 555 /* sai may support mclk/bclk = 1 */ 556 if (of_find_property(np, "fsl,mclk-equal-bclk", NULL)) 557 link_data->one2one_ratio = true; 558 } 559 560 link->cpus->of_node = args.np; 561 link->platforms->of_node = link->cpus->of_node; 562 link->id = args.args[0]; 563 564 ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name); 565 if (ret) { 566 dev_err_probe(card->dev, ret, 567 "%s: error getting cpu dai name\n", link->name); 568 goto err; 569 } 570 571 codec = of_get_child_by_name(np, "codec"); 572 if (codec) { 573 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); 574 if (ret < 0) { 575 dev_err_probe(dev, ret, "%s: codec dai not found\n", 576 link->name); 577 goto err; 578 } 579 580 plat_data->num_codecs = link->num_codecs; 581 582 /* Check the akcodec type */ 583 if (!strcmp(link->codecs->dai_name, "ak4458-aif")) 584 plat_data->type = CODEC_AK4458; 585 else if (!strcmp(link->codecs->dai_name, "ak4497-aif")) 586 plat_data->type = CODEC_AK4497; 587 else if (!strcmp(link->codecs->dai_name, "ak5558-aif")) 588 plat_data->type = CODEC_AK5558; 589 else if (!strcmp(link->codecs->dai_name, "ak5552-aif")) 590 plat_data->type = CODEC_AK5552; 591 592 } else { 593 dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL); 594 if (!dlc) { 595 ret = -ENOMEM; 596 goto err; 597 } 598 599 link->codecs = dlc; 600 link->num_codecs = 1; 601 602 link->codecs->dai_name = "snd-soc-dummy-dai"; 603 link->codecs->name = "snd-soc-dummy"; 604 } 605 606 if (!strncmp(link->name, "HiFi-ASRC-FE", 12)) { 607 /* DPCM frontend */ 608 link->dynamic = 1; 609 link->dpcm_merged_chan = 1; 610 611 ret = of_property_read_u32(args.np, "fsl,asrc-rate", &data->asrc_rate); 612 if (ret) { 613 dev_err(dev, "failed to get output rate\n"); 614 ret = -EINVAL; 615 goto err; 616 } 617 618 ret = of_property_read_u32(args.np, "fsl,asrc-format", &data->asrc_format); 619 if (ret) { 620 /* Fallback to old binding; translate to asrc_format */ 621 ret = of_property_read_u32(args.np, "fsl,asrc-width", &width); 622 if (ret) { 623 dev_err(dev, 624 "failed to decide output format\n"); 625 goto err; 626 } 627 628 if (width == 24) 629 data->asrc_format = SNDRV_PCM_FORMAT_S24_LE; 630 else 631 data->asrc_format = SNDRV_PCM_FORMAT_S16_LE; 632 } 633 } else if (!strncmp(link->name, "HiFi-ASRC-BE", 12)) { 634 /* DPCM backend */ 635 link->no_pcm = 1; 636 link->platforms->of_node = NULL; 637 link->platforms->name = "snd-soc-dummy"; 638 639 link->be_hw_params_fixup = be_hw_params_fixup; 640 link->ops = &imx_aif_ops_be; 641 } else { 642 link->ops = &imx_aif_ops; 643 } 644 645 if (link->no_pcm || link->dynamic) 646 snd_soc_dai_link_set_capabilities(link); 647 648 /* Get dai fmt */ 649 ret = asoc_simple_parse_daifmt(dev, np, codec, 650 NULL, &link->dai_fmt); 651 if (ret) 652 link->dai_fmt = SND_SOC_DAIFMT_NB_NF | 653 SND_SOC_DAIFMT_CBC_CFC | 654 SND_SOC_DAIFMT_I2S; 655 656 /* Get tdm slot */ 657 snd_soc_of_parse_tdm_slot(np, NULL, NULL, 658 &link_data->slots, 659 &link_data->slot_width); 660 /* default value */ 661 if (!link_data->slots) 662 link_data->slots = 2; 663 664 if (!link_data->slot_width) 665 link_data->slot_width = 32; 666 667 link->ignore_pmdown_time = 1; 668 link->stream_name = link->name; 669 link++; 670 link_data++; 671 672 of_node_put(cpu); 673 of_node_put(codec); 674 of_node_put(platform); 675 } 676 677 return 0; 678 err: 679 of_node_put(cpu); 680 of_node_put(codec); 681 of_node_put(platform); 682 err_put_np: 683 of_node_put(np); 684 return ret; 685 } 686 687 static int imx_card_probe(struct platform_device *pdev) 688 { 689 struct snd_soc_dai_link *link_be = NULL, *link; 690 struct imx_card_plat_data *plat_data; 691 struct imx_card_data *data; 692 int ret, i; 693 694 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 695 if (!data) 696 return -ENOMEM; 697 698 plat_data = devm_kzalloc(&pdev->dev, sizeof(*plat_data), GFP_KERNEL); 699 if (!plat_data) 700 return -ENOMEM; 701 702 data->plat_data = plat_data; 703 data->card.dev = &pdev->dev; 704 705 dev_set_drvdata(&pdev->dev, &data->card); 706 snd_soc_card_set_drvdata(&data->card, data); 707 ret = imx_card_parse_of(data); 708 if (ret) 709 return ret; 710 711 data->num_dapm_routes = plat_data->num_codecs + 1; 712 data->dapm_routes = devm_kcalloc(&pdev->dev, data->num_dapm_routes, 713 sizeof(struct snd_soc_dapm_route), 714 GFP_KERNEL); 715 if (!data->dapm_routes) 716 return -ENOMEM; 717 718 /* configure the dapm routes */ 719 switch (plat_data->type) { 720 case CODEC_AK4458: 721 case CODEC_AK4497: 722 if (plat_data->num_codecs == 1) { 723 data->dapm_routes[0].sink = "Playback"; 724 data->dapm_routes[0].source = "CPU-Playback"; 725 i = 1; 726 } else { 727 for (i = 0; i < plat_data->num_codecs; i++) { 728 data->dapm_routes[i].sink = 729 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s", 730 i + 1, "Playback"); 731 data->dapm_routes[i].source = "CPU-Playback"; 732 } 733 } 734 data->dapm_routes[i].sink = "CPU-Playback"; 735 data->dapm_routes[i].source = "ASRC-Playback"; 736 break; 737 case CODEC_AK5558: 738 case CODEC_AK5552: 739 if (plat_data->num_codecs == 1) { 740 data->dapm_routes[0].sink = "CPU-Capture"; 741 data->dapm_routes[0].source = "Capture"; 742 i = 1; 743 } else { 744 for (i = 0; i < plat_data->num_codecs; i++) { 745 data->dapm_routes[i].source = 746 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s", 747 i + 1, "Capture"); 748 data->dapm_routes[i].sink = "CPU-Capture"; 749 } 750 } 751 data->dapm_routes[i].sink = "ASRC-Capture"; 752 data->dapm_routes[i].source = "CPU-Capture"; 753 break; 754 default: 755 break; 756 } 757 758 /* default platform data for akcodecs */ 759 if (codec_is_akcodec(plat_data->type)) { 760 plat_data->support_rates = akcodec_rates; 761 plat_data->num_rates = ARRAY_SIZE(akcodec_rates); 762 plat_data->support_tdm_rates = akcodec_tdm_rates; 763 plat_data->num_tdm_rates = ARRAY_SIZE(akcodec_tdm_rates); 764 765 switch (plat_data->type) { 766 case CODEC_AK4458: 767 plat_data->fs_mul = ak4458_fs_mul; 768 plat_data->num_fs_mul = ARRAY_SIZE(ak4458_fs_mul); 769 plat_data->tdm_fs_mul = ak4458_tdm_fs_mul; 770 plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak4458_tdm_fs_mul); 771 plat_data->support_channels = ak4458_channels; 772 plat_data->num_channels = ARRAY_SIZE(ak4458_channels); 773 plat_data->support_tdm_channels = ak4458_tdm_channels; 774 plat_data->num_tdm_channels = ARRAY_SIZE(ak4458_tdm_channels); 775 break; 776 case CODEC_AK4497: 777 plat_data->fs_mul = ak4497_fs_mul; 778 plat_data->num_fs_mul = ARRAY_SIZE(ak4497_fs_mul); 779 plat_data->support_channels = ak4458_channels; 780 plat_data->num_channels = ARRAY_SIZE(ak4458_channels); 781 break; 782 case CODEC_AK5558: 783 case CODEC_AK5552: 784 plat_data->fs_mul = ak5558_fs_mul; 785 plat_data->num_fs_mul = ARRAY_SIZE(ak5558_fs_mul); 786 plat_data->tdm_fs_mul = ak5558_tdm_fs_mul; 787 plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak5558_tdm_fs_mul); 788 plat_data->support_channels = ak5558_channels; 789 plat_data->num_channels = ARRAY_SIZE(ak5558_channels); 790 plat_data->support_tdm_channels = ak5558_tdm_channels; 791 plat_data->num_tdm_channels = ARRAY_SIZE(ak5558_tdm_channels); 792 break; 793 default: 794 break; 795 } 796 } 797 798 /* with asrc as front end */ 799 if (data->card.num_links == 3) { 800 data->card.dapm_routes = data->dapm_routes; 801 data->card.num_dapm_routes = data->num_dapm_routes; 802 for_each_card_prelinks(&data->card, i, link) { 803 if (link->no_pcm == 1) 804 link_be = link; 805 } 806 for_each_card_prelinks(&data->card, i, link) { 807 if (link->dynamic == 1 && link_be) { 808 link->dpcm_playback = link_be->dpcm_playback; 809 link->dpcm_capture = link_be->dpcm_capture; 810 } 811 } 812 } 813 814 ret = devm_snd_soc_register_card(&pdev->dev, &data->card); 815 if (ret) 816 return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n"); 817 818 return 0; 819 } 820 821 static const struct of_device_id imx_card_dt_ids[] = { 822 { .compatible = "fsl,imx-audio-card", }, 823 { }, 824 }; 825 MODULE_DEVICE_TABLE(of, imx_card_dt_ids); 826 827 static struct platform_driver imx_card_driver = { 828 .driver = { 829 .name = "imx-card", 830 .pm = &snd_soc_pm_ops, 831 .of_match_table = imx_card_dt_ids, 832 }, 833 .probe = imx_card_probe, 834 }; 835 module_platform_driver(imx_card_driver); 836 837 MODULE_DESCRIPTION("Freescale i.MX ASoC Machine Driver"); 838 MODULE_LICENSE("GPL v2"); 839 MODULE_ALIAS("platform:imx-card"); 840