1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // soc-pcm.c -- ALSA SoC PCM 4 // 5 // Copyright 2005 Wolfson Microelectronics PLC. 6 // Copyright 2005 Openedhand Ltd. 7 // Copyright (C) 2010 Slimlogic Ltd. 8 // Copyright (C) 2010 Texas Instruments Inc. 9 // 10 // Authors: Liam Girdwood <lrg@ti.com> 11 // Mark Brown <broonie@opensource.wolfsonmicro.com> 12 13 #include <linux/kernel.h> 14 #include <linux/init.h> 15 #include <linux/delay.h> 16 #include <linux/pinctrl/consumer.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/module.h> 19 #include <linux/slab.h> 20 #include <linux/workqueue.h> 21 #include <linux/export.h> 22 #include <linux/debugfs.h> 23 #include <sound/core.h> 24 #include <sound/pcm.h> 25 #include <sound/pcm_params.h> 26 #include <sound/soc.h> 27 #include <sound/soc-dpcm.h> 28 #include <sound/initval.h> 29 30 #define DPCM_MAX_BE_USERS 8 31 32 /* 33 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream 34 * 35 * Returns true if the DAI supports the indicated stream type. 36 */ 37 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream) 38 { 39 struct snd_soc_pcm_stream *codec_stream; 40 41 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 42 codec_stream = &dai->driver->playback; 43 else 44 codec_stream = &dai->driver->capture; 45 46 /* If the codec specifies any rate at all, it supports the stream. */ 47 return codec_stream->rates; 48 } 49 50 /** 51 * snd_soc_runtime_activate() - Increment active count for PCM runtime components 52 * @rtd: ASoC PCM runtime that is activated 53 * @stream: Direction of the PCM stream 54 * 55 * Increments the active count for all the DAIs and components attached to a PCM 56 * runtime. Should typically be called when a stream is opened. 57 * 58 * Must be called with the rtd->pcm_mutex being held 59 */ 60 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream) 61 { 62 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 63 struct snd_soc_dai *codec_dai; 64 int i; 65 66 lockdep_assert_held(&rtd->pcm_mutex); 67 68 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 69 cpu_dai->playback_active++; 70 for_each_rtd_codec_dai(rtd, i, codec_dai) 71 codec_dai->playback_active++; 72 } else { 73 cpu_dai->capture_active++; 74 for_each_rtd_codec_dai(rtd, i, codec_dai) 75 codec_dai->capture_active++; 76 } 77 78 cpu_dai->active++; 79 cpu_dai->component->active++; 80 for_each_rtd_codec_dai(rtd, i, codec_dai) { 81 codec_dai->active++; 82 codec_dai->component->active++; 83 } 84 } 85 86 /** 87 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components 88 * @rtd: ASoC PCM runtime that is deactivated 89 * @stream: Direction of the PCM stream 90 * 91 * Decrements the active count for all the DAIs and components attached to a PCM 92 * runtime. Should typically be called when a stream is closed. 93 * 94 * Must be called with the rtd->pcm_mutex being held 95 */ 96 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream) 97 { 98 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 99 struct snd_soc_dai *codec_dai; 100 int i; 101 102 lockdep_assert_held(&rtd->pcm_mutex); 103 104 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 105 cpu_dai->playback_active--; 106 for_each_rtd_codec_dai(rtd, i, codec_dai) 107 codec_dai->playback_active--; 108 } else { 109 cpu_dai->capture_active--; 110 for_each_rtd_codec_dai(rtd, i, codec_dai) 111 codec_dai->capture_active--; 112 } 113 114 cpu_dai->active--; 115 cpu_dai->component->active--; 116 for_each_rtd_codec_dai(rtd, i, codec_dai) { 117 codec_dai->component->active--; 118 codec_dai->active--; 119 } 120 } 121 122 /** 123 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay 124 * @rtd: The ASoC PCM runtime that should be checked. 125 * 126 * This function checks whether the power down delay should be ignored for a 127 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has 128 * been configured to ignore the delay, or if none of the components benefits 129 * from having the delay. 130 */ 131 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) 132 { 133 struct snd_soc_rtdcom_list *rtdcom; 134 struct snd_soc_component *component; 135 bool ignore = true; 136 137 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) 138 return true; 139 140 for_each_rtdcom(rtd, rtdcom) { 141 component = rtdcom->component; 142 143 ignore &= !component->driver->use_pmdown_time; 144 } 145 146 return ignore; 147 } 148 149 /** 150 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 151 * @substream: the pcm substream 152 * @hw: the hardware parameters 153 * 154 * Sets the substream runtime hardware parameters. 155 */ 156 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 157 const struct snd_pcm_hardware *hw) 158 { 159 struct snd_pcm_runtime *runtime = substream->runtime; 160 runtime->hw.info = hw->info; 161 runtime->hw.formats = hw->formats; 162 runtime->hw.period_bytes_min = hw->period_bytes_min; 163 runtime->hw.period_bytes_max = hw->period_bytes_max; 164 runtime->hw.periods_min = hw->periods_min; 165 runtime->hw.periods_max = hw->periods_max; 166 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 167 runtime->hw.fifo_size = hw->fifo_size; 168 return 0; 169 } 170 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 171 172 /* DPCM stream event, send event to FE and all active BEs. */ 173 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, 174 int event) 175 { 176 struct snd_soc_dpcm *dpcm; 177 178 for_each_dpcm_be(fe, dir, dpcm) { 179 180 struct snd_soc_pcm_runtime *be = dpcm->be; 181 182 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", 183 be->dai_link->name, event, dir); 184 185 if ((event == SND_SOC_DAPM_STREAM_STOP) && 186 (be->dpcm[dir].users >= 1)) 187 continue; 188 189 snd_soc_dapm_stream_event(be, dir, event); 190 } 191 192 snd_soc_dapm_stream_event(fe, dir, event); 193 194 return 0; 195 } 196 197 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, 198 struct snd_soc_dai *soc_dai) 199 { 200 struct snd_soc_pcm_runtime *rtd = substream->private_data; 201 int ret; 202 203 if (soc_dai->rate && (soc_dai->driver->symmetric_rates || 204 rtd->dai_link->symmetric_rates)) { 205 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", 206 soc_dai->rate); 207 208 ret = snd_pcm_hw_constraint_single(substream->runtime, 209 SNDRV_PCM_HW_PARAM_RATE, 210 soc_dai->rate); 211 if (ret < 0) { 212 dev_err(soc_dai->dev, 213 "ASoC: Unable to apply rate constraint: %d\n", 214 ret); 215 return ret; 216 } 217 } 218 219 if (soc_dai->channels && (soc_dai->driver->symmetric_channels || 220 rtd->dai_link->symmetric_channels)) { 221 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", 222 soc_dai->channels); 223 224 ret = snd_pcm_hw_constraint_single(substream->runtime, 225 SNDRV_PCM_HW_PARAM_CHANNELS, 226 soc_dai->channels); 227 if (ret < 0) { 228 dev_err(soc_dai->dev, 229 "ASoC: Unable to apply channel symmetry constraint: %d\n", 230 ret); 231 return ret; 232 } 233 } 234 235 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || 236 rtd->dai_link->symmetric_samplebits)) { 237 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", 238 soc_dai->sample_bits); 239 240 ret = snd_pcm_hw_constraint_single(substream->runtime, 241 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 242 soc_dai->sample_bits); 243 if (ret < 0) { 244 dev_err(soc_dai->dev, 245 "ASoC: Unable to apply sample bits symmetry constraint: %d\n", 246 ret); 247 return ret; 248 } 249 } 250 251 return 0; 252 } 253 254 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, 255 struct snd_pcm_hw_params *params) 256 { 257 struct snd_soc_pcm_runtime *rtd = substream->private_data; 258 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 259 struct snd_soc_dai *codec_dai; 260 unsigned int rate, channels, sample_bits, symmetry, i; 261 262 rate = params_rate(params); 263 channels = params_channels(params); 264 sample_bits = snd_pcm_format_physical_width(params_format(params)); 265 266 /* reject unmatched parameters when applying symmetry */ 267 symmetry = cpu_dai->driver->symmetric_rates || 268 rtd->dai_link->symmetric_rates; 269 270 for_each_rtd_codec_dai(rtd, i, codec_dai) 271 symmetry |= codec_dai->driver->symmetric_rates; 272 273 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) { 274 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", 275 cpu_dai->rate, rate); 276 return -EINVAL; 277 } 278 279 symmetry = cpu_dai->driver->symmetric_channels || 280 rtd->dai_link->symmetric_channels; 281 282 for_each_rtd_codec_dai(rtd, i, codec_dai) 283 symmetry |= codec_dai->driver->symmetric_channels; 284 285 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) { 286 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", 287 cpu_dai->channels, channels); 288 return -EINVAL; 289 } 290 291 symmetry = cpu_dai->driver->symmetric_samplebits || 292 rtd->dai_link->symmetric_samplebits; 293 294 for_each_rtd_codec_dai(rtd, i, codec_dai) 295 symmetry |= codec_dai->driver->symmetric_samplebits; 296 297 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) { 298 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", 299 cpu_dai->sample_bits, sample_bits); 300 return -EINVAL; 301 } 302 303 return 0; 304 } 305 306 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) 307 { 308 struct snd_soc_pcm_runtime *rtd = substream->private_data; 309 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver; 310 struct snd_soc_dai_link *link = rtd->dai_link; 311 struct snd_soc_dai *codec_dai; 312 unsigned int symmetry, i; 313 314 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates || 315 cpu_driver->symmetric_channels || link->symmetric_channels || 316 cpu_driver->symmetric_samplebits || link->symmetric_samplebits; 317 318 for_each_rtd_codec_dai(rtd, i, codec_dai) 319 symmetry = symmetry || 320 codec_dai->driver->symmetric_rates || 321 codec_dai->driver->symmetric_channels || 322 codec_dai->driver->symmetric_samplebits; 323 324 return symmetry; 325 } 326 327 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) 328 { 329 struct snd_soc_pcm_runtime *rtd = substream->private_data; 330 int ret; 331 332 if (!bits) 333 return; 334 335 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); 336 if (ret != 0) 337 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", 338 bits, ret); 339 } 340 341 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) 342 { 343 struct snd_soc_pcm_runtime *rtd = substream->private_data; 344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 345 struct snd_soc_dai *codec_dai; 346 int i; 347 unsigned int bits = 0, cpu_bits; 348 349 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 350 for_each_rtd_codec_dai(rtd, i, codec_dai) { 351 if (codec_dai->driver->playback.sig_bits == 0) { 352 bits = 0; 353 break; 354 } 355 bits = max(codec_dai->driver->playback.sig_bits, bits); 356 } 357 cpu_bits = cpu_dai->driver->playback.sig_bits; 358 } else { 359 for_each_rtd_codec_dai(rtd, i, codec_dai) { 360 if (codec_dai->driver->capture.sig_bits == 0) { 361 bits = 0; 362 break; 363 } 364 bits = max(codec_dai->driver->capture.sig_bits, bits); 365 } 366 cpu_bits = cpu_dai->driver->capture.sig_bits; 367 } 368 369 soc_pcm_set_msb(substream, bits); 370 soc_pcm_set_msb(substream, cpu_bits); 371 } 372 373 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) 374 { 375 struct snd_pcm_runtime *runtime = substream->runtime; 376 struct snd_pcm_hardware *hw = &runtime->hw; 377 struct snd_soc_pcm_runtime *rtd = substream->private_data; 378 struct snd_soc_dai *codec_dai; 379 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver; 380 struct snd_soc_dai_driver *codec_dai_drv; 381 struct snd_soc_pcm_stream *codec_stream; 382 struct snd_soc_pcm_stream *cpu_stream; 383 unsigned int chan_min = 0, chan_max = UINT_MAX; 384 unsigned int rate_min = 0, rate_max = UINT_MAX; 385 unsigned int rates = UINT_MAX; 386 u64 formats = ULLONG_MAX; 387 int i; 388 389 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 390 cpu_stream = &cpu_dai_drv->playback; 391 else 392 cpu_stream = &cpu_dai_drv->capture; 393 394 /* first calculate min/max only for CODECs in the DAI link */ 395 for_each_rtd_codec_dai(rtd, i, codec_dai) { 396 397 /* 398 * Skip CODECs which don't support the current stream type. 399 * Otherwise, since the rate, channel, and format values will 400 * zero in that case, we would have no usable settings left, 401 * causing the resulting setup to fail. 402 * At least one CODEC should match, otherwise we should have 403 * bailed out on a higher level, since there would be no 404 * CODEC to support the transfer direction in that case. 405 */ 406 if (!snd_soc_dai_stream_valid(codec_dai, 407 substream->stream)) 408 continue; 409 410 codec_dai_drv = codec_dai->driver; 411 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 412 codec_stream = &codec_dai_drv->playback; 413 else 414 codec_stream = &codec_dai_drv->capture; 415 chan_min = max(chan_min, codec_stream->channels_min); 416 chan_max = min(chan_max, codec_stream->channels_max); 417 rate_min = max(rate_min, codec_stream->rate_min); 418 rate_max = min_not_zero(rate_max, codec_stream->rate_max); 419 formats &= codec_stream->formats; 420 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates); 421 } 422 423 /* 424 * chan min/max cannot be enforced if there are multiple CODEC DAIs 425 * connected to a single CPU DAI, use CPU DAI's directly and let 426 * channel allocation be fixed up later 427 */ 428 if (rtd->num_codecs > 1) { 429 chan_min = cpu_stream->channels_min; 430 chan_max = cpu_stream->channels_max; 431 } 432 433 hw->channels_min = max(chan_min, cpu_stream->channels_min); 434 hw->channels_max = min(chan_max, cpu_stream->channels_max); 435 if (hw->formats) 436 hw->formats &= formats & cpu_stream->formats; 437 else 438 hw->formats = formats & cpu_stream->formats; 439 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates); 440 441 snd_pcm_limit_hw_rates(runtime); 442 443 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min); 444 hw->rate_min = max(hw->rate_min, rate_min); 445 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max); 446 hw->rate_max = min_not_zero(hw->rate_max, rate_max); 447 } 448 449 static int soc_pcm_components_close(struct snd_pcm_substream *substream, 450 struct snd_soc_component *last) 451 { 452 struct snd_soc_pcm_runtime *rtd = substream->private_data; 453 struct snd_soc_rtdcom_list *rtdcom; 454 struct snd_soc_component *component; 455 456 for_each_rtdcom(rtd, rtdcom) { 457 component = rtdcom->component; 458 459 if (component == last) 460 break; 461 462 if (!component->driver->ops || 463 !component->driver->ops->close) 464 continue; 465 466 component->driver->ops->close(substream); 467 468 if (component->driver->module_get_upon_open) 469 module_put(component->dev->driver->owner); 470 } 471 472 return 0; 473 } 474 475 /* 476 * Called by ALSA when a PCM substream is opened, the runtime->hw record is 477 * then initialized and any private data can be allocated. This also calls 478 * startup for the cpu DAI, component, machine and codec DAI. 479 */ 480 static int soc_pcm_open(struct snd_pcm_substream *substream) 481 { 482 struct snd_soc_pcm_runtime *rtd = substream->private_data; 483 struct snd_pcm_runtime *runtime = substream->runtime; 484 struct snd_soc_component *component; 485 struct snd_soc_rtdcom_list *rtdcom; 486 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 487 struct snd_soc_dai *codec_dai; 488 const char *codec_dai_name = "multicodec"; 489 int i, ret = 0; 490 491 pinctrl_pm_select_default_state(cpu_dai->dev); 492 for_each_rtd_codec_dai(rtd, i, codec_dai) 493 pinctrl_pm_select_default_state(codec_dai->dev); 494 495 for_each_rtdcom(rtd, rtdcom) { 496 component = rtdcom->component; 497 498 pm_runtime_get_sync(component->dev); 499 } 500 501 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 502 503 /* startup the audio subsystem */ 504 if (cpu_dai->driver->ops->startup) { 505 ret = cpu_dai->driver->ops->startup(substream, cpu_dai); 506 if (ret < 0) { 507 dev_err(cpu_dai->dev, "ASoC: can't open interface" 508 " %s: %d\n", cpu_dai->name, ret); 509 goto out; 510 } 511 } 512 513 for_each_rtdcom(rtd, rtdcom) { 514 component = rtdcom->component; 515 516 if (!component->driver->ops || 517 !component->driver->ops->open) 518 continue; 519 520 if (component->driver->module_get_upon_open && 521 !try_module_get(component->dev->driver->owner)) 522 return -ENODEV; 523 524 ret = component->driver->ops->open(substream); 525 if (ret < 0) { 526 dev_err(component->dev, 527 "ASoC: can't open component %s: %d\n", 528 component->name, ret); 529 goto component_err; 530 } 531 } 532 component = NULL; 533 534 for_each_rtd_codec_dai(rtd, i, codec_dai) { 535 if (codec_dai->driver->ops->startup) { 536 ret = codec_dai->driver->ops->startup(substream, 537 codec_dai); 538 if (ret < 0) { 539 dev_err(codec_dai->dev, 540 "ASoC: can't open codec %s: %d\n", 541 codec_dai->name, ret); 542 goto codec_dai_err; 543 } 544 } 545 546 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 547 codec_dai->tx_mask = 0; 548 else 549 codec_dai->rx_mask = 0; 550 } 551 552 if (rtd->dai_link->ops->startup) { 553 ret = rtd->dai_link->ops->startup(substream); 554 if (ret < 0) { 555 pr_err("ASoC: %s startup failed: %d\n", 556 rtd->dai_link->name, ret); 557 goto machine_err; 558 } 559 } 560 561 /* Dynamic PCM DAI links compat checks use dynamic capabilities */ 562 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) 563 goto dynamic; 564 565 /* Check that the codec and cpu DAIs are compatible */ 566 soc_pcm_init_runtime_hw(substream); 567 568 if (rtd->num_codecs == 1) 569 codec_dai_name = rtd->codec_dai->name; 570 571 if (soc_pcm_has_symmetry(substream)) 572 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 573 574 ret = -EINVAL; 575 if (!runtime->hw.rates) { 576 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", 577 codec_dai_name, cpu_dai->name); 578 goto config_err; 579 } 580 if (!runtime->hw.formats) { 581 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", 582 codec_dai_name, cpu_dai->name); 583 goto config_err; 584 } 585 if (!runtime->hw.channels_min || !runtime->hw.channels_max || 586 runtime->hw.channels_min > runtime->hw.channels_max) { 587 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", 588 codec_dai_name, cpu_dai->name); 589 goto config_err; 590 } 591 592 soc_pcm_apply_msb(substream); 593 594 /* Symmetry only applies if we've already got an active stream. */ 595 if (cpu_dai->active) { 596 ret = soc_pcm_apply_symmetry(substream, cpu_dai); 597 if (ret != 0) 598 goto config_err; 599 } 600 601 for_each_rtd_codec_dai(rtd, i, codec_dai) { 602 if (codec_dai->active) { 603 ret = soc_pcm_apply_symmetry(substream, codec_dai); 604 if (ret != 0) 605 goto config_err; 606 } 607 } 608 609 pr_debug("ASoC: %s <-> %s info:\n", 610 codec_dai_name, cpu_dai->name); 611 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); 612 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, 613 runtime->hw.channels_max); 614 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, 615 runtime->hw.rate_max); 616 617 dynamic: 618 619 snd_soc_runtime_activate(rtd, substream->stream); 620 621 mutex_unlock(&rtd->pcm_mutex); 622 return 0; 623 624 config_err: 625 if (rtd->dai_link->ops->shutdown) 626 rtd->dai_link->ops->shutdown(substream); 627 628 machine_err: 629 i = rtd->num_codecs; 630 631 codec_dai_err: 632 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) { 633 if (codec_dai->driver->ops->shutdown) 634 codec_dai->driver->ops->shutdown(substream, codec_dai); 635 } 636 637 component_err: 638 soc_pcm_components_close(substream, component); 639 640 if (cpu_dai->driver->ops->shutdown) 641 cpu_dai->driver->ops->shutdown(substream, cpu_dai); 642 out: 643 mutex_unlock(&rtd->pcm_mutex); 644 645 for_each_rtdcom(rtd, rtdcom) { 646 component = rtdcom->component; 647 648 pm_runtime_mark_last_busy(component->dev); 649 pm_runtime_put_autosuspend(component->dev); 650 } 651 652 for_each_rtd_codec_dai(rtd, i, codec_dai) { 653 if (!codec_dai->active) 654 pinctrl_pm_select_sleep_state(codec_dai->dev); 655 } 656 if (!cpu_dai->active) 657 pinctrl_pm_select_sleep_state(cpu_dai->dev); 658 659 return ret; 660 } 661 662 /* 663 * Power down the audio subsystem pmdown_time msecs after close is called. 664 * This is to ensure there are no pops or clicks in between any music tracks 665 * due to DAPM power cycling. 666 */ 667 static void close_delayed_work(struct work_struct *work) 668 { 669 struct snd_soc_pcm_runtime *rtd = 670 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); 671 struct snd_soc_dai *codec_dai = rtd->codec_dais[0]; 672 673 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 674 675 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n", 676 codec_dai->driver->playback.stream_name, 677 codec_dai->playback_active ? "active" : "inactive", 678 rtd->pop_wait ? "yes" : "no"); 679 680 /* are we waiting on this codec DAI stream */ 681 if (rtd->pop_wait == 1) { 682 rtd->pop_wait = 0; 683 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, 684 SND_SOC_DAPM_STREAM_STOP); 685 } 686 687 mutex_unlock(&rtd->pcm_mutex); 688 } 689 690 /* 691 * Called by ALSA when a PCM substream is closed. Private data can be 692 * freed here. The cpu DAI, codec DAI, machine and components are also 693 * shutdown. 694 */ 695 static int soc_pcm_close(struct snd_pcm_substream *substream) 696 { 697 struct snd_soc_pcm_runtime *rtd = substream->private_data; 698 struct snd_soc_component *component; 699 struct snd_soc_rtdcom_list *rtdcom; 700 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 701 struct snd_soc_dai *codec_dai; 702 int i; 703 704 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 705 706 snd_soc_runtime_deactivate(rtd, substream->stream); 707 708 /* clear the corresponding DAIs rate when inactive */ 709 if (!cpu_dai->active) 710 cpu_dai->rate = 0; 711 712 for_each_rtd_codec_dai(rtd, i, codec_dai) { 713 if (!codec_dai->active) 714 codec_dai->rate = 0; 715 } 716 717 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream); 718 719 if (cpu_dai->driver->ops->shutdown) 720 cpu_dai->driver->ops->shutdown(substream, cpu_dai); 721 722 for_each_rtd_codec_dai(rtd, i, codec_dai) { 723 if (codec_dai->driver->ops->shutdown) 724 codec_dai->driver->ops->shutdown(substream, codec_dai); 725 } 726 727 if (rtd->dai_link->ops->shutdown) 728 rtd->dai_link->ops->shutdown(substream); 729 730 soc_pcm_components_close(substream, NULL); 731 732 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 733 if (snd_soc_runtime_ignore_pmdown_time(rtd)) { 734 /* powered down playback stream now */ 735 snd_soc_dapm_stream_event(rtd, 736 SNDRV_PCM_STREAM_PLAYBACK, 737 SND_SOC_DAPM_STREAM_STOP); 738 } else { 739 /* start delayed pop wq here for playback streams */ 740 rtd->pop_wait = 1; 741 queue_delayed_work(system_power_efficient_wq, 742 &rtd->delayed_work, 743 msecs_to_jiffies(rtd->pmdown_time)); 744 } 745 } else { 746 /* capture streams can be powered down now */ 747 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, 748 SND_SOC_DAPM_STREAM_STOP); 749 } 750 751 mutex_unlock(&rtd->pcm_mutex); 752 753 for_each_rtdcom(rtd, rtdcom) { 754 component = rtdcom->component; 755 756 pm_runtime_mark_last_busy(component->dev); 757 pm_runtime_put_autosuspend(component->dev); 758 } 759 760 for_each_rtd_codec_dai(rtd, i, codec_dai) { 761 if (!codec_dai->active) 762 pinctrl_pm_select_sleep_state(codec_dai->dev); 763 } 764 if (!cpu_dai->active) 765 pinctrl_pm_select_sleep_state(cpu_dai->dev); 766 767 return 0; 768 } 769 770 /* 771 * Called by ALSA when the PCM substream is prepared, can set format, sample 772 * rate, etc. This function is non atomic and can be called multiple times, 773 * it can refer to the runtime info. 774 */ 775 static int soc_pcm_prepare(struct snd_pcm_substream *substream) 776 { 777 struct snd_soc_pcm_runtime *rtd = substream->private_data; 778 struct snd_soc_component *component; 779 struct snd_soc_rtdcom_list *rtdcom; 780 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 781 struct snd_soc_dai *codec_dai; 782 int i, ret = 0; 783 784 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 785 786 if (rtd->dai_link->ops->prepare) { 787 ret = rtd->dai_link->ops->prepare(substream); 788 if (ret < 0) { 789 dev_err(rtd->card->dev, "ASoC: machine prepare error:" 790 " %d\n", ret); 791 goto out; 792 } 793 } 794 795 for_each_rtdcom(rtd, rtdcom) { 796 component = rtdcom->component; 797 798 if (!component->driver->ops || 799 !component->driver->ops->prepare) 800 continue; 801 802 ret = component->driver->ops->prepare(substream); 803 if (ret < 0) { 804 dev_err(component->dev, 805 "ASoC: platform prepare error: %d\n", ret); 806 goto out; 807 } 808 } 809 810 for_each_rtd_codec_dai(rtd, i, codec_dai) { 811 if (codec_dai->driver->ops->prepare) { 812 ret = codec_dai->driver->ops->prepare(substream, 813 codec_dai); 814 if (ret < 0) { 815 dev_err(codec_dai->dev, 816 "ASoC: codec DAI prepare error: %d\n", 817 ret); 818 goto out; 819 } 820 } 821 } 822 823 if (cpu_dai->driver->ops->prepare) { 824 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); 825 if (ret < 0) { 826 dev_err(cpu_dai->dev, 827 "ASoC: cpu DAI prepare error: %d\n", ret); 828 goto out; 829 } 830 } 831 832 /* cancel any delayed stream shutdown that is pending */ 833 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 834 rtd->pop_wait) { 835 rtd->pop_wait = 0; 836 cancel_delayed_work(&rtd->delayed_work); 837 } 838 839 snd_soc_dapm_stream_event(rtd, substream->stream, 840 SND_SOC_DAPM_STREAM_START); 841 842 for_each_rtd_codec_dai(rtd, i, codec_dai) 843 snd_soc_dai_digital_mute(codec_dai, 0, 844 substream->stream); 845 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream); 846 847 out: 848 mutex_unlock(&rtd->pcm_mutex); 849 return ret; 850 } 851 852 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, 853 unsigned int mask) 854 { 855 struct snd_interval *interval; 856 int channels = hweight_long(mask); 857 858 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 859 interval->min = channels; 860 interval->max = channels; 861 } 862 863 int soc_dai_hw_params(struct snd_pcm_substream *substream, 864 struct snd_pcm_hw_params *params, 865 struct snd_soc_dai *dai) 866 { 867 struct snd_soc_pcm_runtime *rtd = substream->private_data; 868 int ret; 869 870 /* perform any topology hw_params fixups before DAI */ 871 if (rtd->dai_link->be_hw_params_fixup) { 872 ret = rtd->dai_link->be_hw_params_fixup(rtd, params); 873 if (ret < 0) { 874 dev_err(rtd->dev, 875 "ASoC: hw_params topology fixup failed %d\n", 876 ret); 877 return ret; 878 } 879 } 880 881 if (dai->driver->ops->hw_params) { 882 ret = dai->driver->ops->hw_params(substream, params, dai); 883 if (ret < 0) { 884 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n", 885 dai->name, ret); 886 return ret; 887 } 888 } 889 890 return 0; 891 } 892 893 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream, 894 struct snd_soc_component *last) 895 { 896 struct snd_soc_pcm_runtime *rtd = substream->private_data; 897 struct snd_soc_rtdcom_list *rtdcom; 898 struct snd_soc_component *component; 899 900 for_each_rtdcom(rtd, rtdcom) { 901 component = rtdcom->component; 902 903 if (component == last) 904 break; 905 906 if (!component->driver->ops || 907 !component->driver->ops->hw_free) 908 continue; 909 910 component->driver->ops->hw_free(substream); 911 } 912 913 return 0; 914 } 915 916 /* 917 * Called by ALSA when the hardware params are set by application. This 918 * function can also be called multiple times and can allocate buffers 919 * (using snd_pcm_lib_* ). It's non-atomic. 920 */ 921 static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 922 struct snd_pcm_hw_params *params) 923 { 924 struct snd_soc_pcm_runtime *rtd = substream->private_data; 925 struct snd_soc_component *component; 926 struct snd_soc_rtdcom_list *rtdcom; 927 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 928 struct snd_soc_dai *codec_dai; 929 int i, ret = 0; 930 931 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 932 if (rtd->dai_link->ops->hw_params) { 933 ret = rtd->dai_link->ops->hw_params(substream, params); 934 if (ret < 0) { 935 dev_err(rtd->card->dev, "ASoC: machine hw_params" 936 " failed: %d\n", ret); 937 goto out; 938 } 939 } 940 941 for_each_rtd_codec_dai(rtd, i, codec_dai) { 942 struct snd_pcm_hw_params codec_params; 943 944 /* 945 * Skip CODECs which don't support the current stream type, 946 * the idea being that if a CODEC is not used for the currently 947 * set up transfer direction, it should not need to be 948 * configured, especially since the configuration used might 949 * not even be supported by that CODEC. There may be cases 950 * however where a CODEC needs to be set up although it is 951 * actually not being used for the transfer, e.g. if a 952 * capture-only CODEC is acting as an LRCLK and/or BCLK master 953 * for the DAI link including a playback-only CODEC. 954 * If this becomes necessary, we will have to augment the 955 * machine driver setup with information on how to act, so 956 * we can do the right thing here. 957 */ 958 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 959 continue; 960 961 /* copy params for each codec */ 962 codec_params = *params; 963 964 /* fixup params based on TDM slot masks */ 965 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 966 codec_dai->tx_mask) 967 soc_pcm_codec_params_fixup(&codec_params, 968 codec_dai->tx_mask); 969 970 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && 971 codec_dai->rx_mask) 972 soc_pcm_codec_params_fixup(&codec_params, 973 codec_dai->rx_mask); 974 975 ret = soc_dai_hw_params(substream, &codec_params, codec_dai); 976 if(ret < 0) 977 goto codec_err; 978 979 codec_dai->rate = params_rate(&codec_params); 980 codec_dai->channels = params_channels(&codec_params); 981 codec_dai->sample_bits = snd_pcm_format_physical_width( 982 params_format(&codec_params)); 983 984 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); 985 } 986 987 ret = soc_dai_hw_params(substream, params, cpu_dai); 988 if (ret < 0) 989 goto interface_err; 990 991 for_each_rtdcom(rtd, rtdcom) { 992 component = rtdcom->component; 993 994 if (!component->driver->ops || 995 !component->driver->ops->hw_params) 996 continue; 997 998 ret = component->driver->ops->hw_params(substream, params); 999 if (ret < 0) { 1000 dev_err(component->dev, 1001 "ASoC: %s hw params failed: %d\n", 1002 component->name, ret); 1003 goto component_err; 1004 } 1005 } 1006 component = NULL; 1007 1008 /* store the parameters for each DAIs */ 1009 cpu_dai->rate = params_rate(params); 1010 cpu_dai->channels = params_channels(params); 1011 cpu_dai->sample_bits = 1012 snd_pcm_format_physical_width(params_format(params)); 1013 1014 snd_soc_dapm_update_dai(substream, params, cpu_dai); 1015 1016 ret = soc_pcm_params_symmetry(substream, params); 1017 if (ret) 1018 goto component_err; 1019 out: 1020 mutex_unlock(&rtd->pcm_mutex); 1021 return ret; 1022 1023 component_err: 1024 soc_pcm_components_hw_free(substream, component); 1025 1026 if (cpu_dai->driver->ops->hw_free) 1027 cpu_dai->driver->ops->hw_free(substream, cpu_dai); 1028 1029 interface_err: 1030 i = rtd->num_codecs; 1031 1032 codec_err: 1033 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) { 1034 if (codec_dai->driver->ops->hw_free) 1035 codec_dai->driver->ops->hw_free(substream, codec_dai); 1036 codec_dai->rate = 0; 1037 } 1038 1039 if (rtd->dai_link->ops->hw_free) 1040 rtd->dai_link->ops->hw_free(substream); 1041 1042 mutex_unlock(&rtd->pcm_mutex); 1043 return ret; 1044 } 1045 1046 /* 1047 * Frees resources allocated by hw_params, can be called multiple times 1048 */ 1049 static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 1050 { 1051 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1052 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1053 struct snd_soc_dai *codec_dai; 1054 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 1055 int i; 1056 1057 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 1058 1059 /* clear the corresponding DAIs parameters when going to be inactive */ 1060 if (cpu_dai->active == 1) { 1061 cpu_dai->rate = 0; 1062 cpu_dai->channels = 0; 1063 cpu_dai->sample_bits = 0; 1064 } 1065 1066 for_each_rtd_codec_dai(rtd, i, codec_dai) { 1067 if (codec_dai->active == 1) { 1068 codec_dai->rate = 0; 1069 codec_dai->channels = 0; 1070 codec_dai->sample_bits = 0; 1071 } 1072 } 1073 1074 /* apply codec digital mute */ 1075 for_each_rtd_codec_dai(rtd, i, codec_dai) { 1076 if ((playback && codec_dai->playback_active == 1) || 1077 (!playback && codec_dai->capture_active == 1)) 1078 snd_soc_dai_digital_mute(codec_dai, 1, 1079 substream->stream); 1080 } 1081 1082 /* free any machine hw params */ 1083 if (rtd->dai_link->ops->hw_free) 1084 rtd->dai_link->ops->hw_free(substream); 1085 1086 /* free any component resources */ 1087 soc_pcm_components_hw_free(substream, NULL); 1088 1089 /* now free hw params for the DAIs */ 1090 for_each_rtd_codec_dai(rtd, i, codec_dai) { 1091 if (codec_dai->driver->ops->hw_free) 1092 codec_dai->driver->ops->hw_free(substream, codec_dai); 1093 } 1094 1095 if (cpu_dai->driver->ops->hw_free) 1096 cpu_dai->driver->ops->hw_free(substream, cpu_dai); 1097 1098 mutex_unlock(&rtd->pcm_mutex); 1099 return 0; 1100 } 1101 1102 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 1103 { 1104 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1105 struct snd_soc_component *component; 1106 struct snd_soc_rtdcom_list *rtdcom; 1107 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1108 struct snd_soc_dai *codec_dai; 1109 int i, ret; 1110 1111 for_each_rtd_codec_dai(rtd, i, codec_dai) { 1112 if (codec_dai->driver->ops->trigger) { 1113 ret = codec_dai->driver->ops->trigger(substream, 1114 cmd, codec_dai); 1115 if (ret < 0) 1116 return ret; 1117 } 1118 } 1119 1120 for_each_rtdcom(rtd, rtdcom) { 1121 component = rtdcom->component; 1122 1123 if (!component->driver->ops || 1124 !component->driver->ops->trigger) 1125 continue; 1126 1127 ret = component->driver->ops->trigger(substream, cmd); 1128 if (ret < 0) 1129 return ret; 1130 } 1131 1132 if (cpu_dai->driver->ops->trigger) { 1133 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); 1134 if (ret < 0) 1135 return ret; 1136 } 1137 1138 if (rtd->dai_link->ops->trigger) { 1139 ret = rtd->dai_link->ops->trigger(substream, cmd); 1140 if (ret < 0) 1141 return ret; 1142 } 1143 1144 return 0; 1145 } 1146 1147 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, 1148 int cmd) 1149 { 1150 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1151 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1152 struct snd_soc_dai *codec_dai; 1153 int i, ret; 1154 1155 for_each_rtd_codec_dai(rtd, i, codec_dai) { 1156 if (codec_dai->driver->ops->bespoke_trigger) { 1157 ret = codec_dai->driver->ops->bespoke_trigger(substream, 1158 cmd, codec_dai); 1159 if (ret < 0) 1160 return ret; 1161 } 1162 } 1163 1164 if (cpu_dai->driver->ops->bespoke_trigger) { 1165 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai); 1166 if (ret < 0) 1167 return ret; 1168 } 1169 return 0; 1170 } 1171 /* 1172 * soc level wrapper for pointer callback 1173 * If cpu_dai, codec_dai, component driver has the delay callback, then 1174 * the runtime->delay will be updated accordingly. 1175 */ 1176 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 1177 { 1178 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1179 struct snd_soc_component *component; 1180 struct snd_soc_rtdcom_list *rtdcom; 1181 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1182 struct snd_soc_dai *codec_dai; 1183 struct snd_pcm_runtime *runtime = substream->runtime; 1184 snd_pcm_uframes_t offset = 0; 1185 snd_pcm_sframes_t delay = 0; 1186 snd_pcm_sframes_t codec_delay = 0; 1187 int i; 1188 1189 /* clearing the previous total delay */ 1190 runtime->delay = 0; 1191 1192 for_each_rtdcom(rtd, rtdcom) { 1193 component = rtdcom->component; 1194 1195 if (!component->driver->ops || 1196 !component->driver->ops->pointer) 1197 continue; 1198 1199 /* FIXME: use 1st pointer */ 1200 offset = component->driver->ops->pointer(substream); 1201 break; 1202 } 1203 /* base delay if assigned in pointer callback */ 1204 delay = runtime->delay; 1205 1206 if (cpu_dai->driver->ops->delay) 1207 delay += cpu_dai->driver->ops->delay(substream, cpu_dai); 1208 1209 for_each_rtd_codec_dai(rtd, i, codec_dai) { 1210 if (codec_dai->driver->ops->delay) 1211 codec_delay = max(codec_delay, 1212 codec_dai->driver->ops->delay(substream, 1213 codec_dai)); 1214 } 1215 delay += codec_delay; 1216 1217 runtime->delay = delay; 1218 1219 return offset; 1220 } 1221 1222 /* connect a FE and BE */ 1223 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, 1224 struct snd_soc_pcm_runtime *be, int stream) 1225 { 1226 struct snd_soc_dpcm *dpcm; 1227 unsigned long flags; 1228 1229 /* only add new dpcms */ 1230 for_each_dpcm_be(fe, stream, dpcm) { 1231 if (dpcm->be == be && dpcm->fe == fe) 1232 return 0; 1233 } 1234 1235 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); 1236 if (!dpcm) 1237 return -ENOMEM; 1238 1239 dpcm->be = be; 1240 dpcm->fe = fe; 1241 be->dpcm[stream].runtime = fe->dpcm[stream].runtime; 1242 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; 1243 spin_lock_irqsave(&fe->card->dpcm_lock, flags); 1244 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); 1245 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); 1246 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 1247 1248 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", 1249 stream ? "capture" : "playback", fe->dai_link->name, 1250 stream ? "<-" : "->", be->dai_link->name); 1251 1252 #ifdef CONFIG_DEBUG_FS 1253 if (fe->debugfs_dpcm_root) 1254 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644, 1255 fe->debugfs_dpcm_root, &dpcm->state); 1256 #endif 1257 return 1; 1258 } 1259 1260 /* reparent a BE onto another FE */ 1261 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, 1262 struct snd_soc_pcm_runtime *be, int stream) 1263 { 1264 struct snd_soc_dpcm *dpcm; 1265 struct snd_pcm_substream *fe_substream, *be_substream; 1266 1267 /* reparent if BE is connected to other FEs */ 1268 if (!be->dpcm[stream].users) 1269 return; 1270 1271 be_substream = snd_soc_dpcm_get_substream(be, stream); 1272 1273 for_each_dpcm_fe(be, stream, dpcm) { 1274 if (dpcm->fe == fe) 1275 continue; 1276 1277 dev_dbg(fe->dev, "reparent %s path %s %s %s\n", 1278 stream ? "capture" : "playback", 1279 dpcm->fe->dai_link->name, 1280 stream ? "<-" : "->", dpcm->be->dai_link->name); 1281 1282 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); 1283 be_substream->runtime = fe_substream->runtime; 1284 break; 1285 } 1286 } 1287 1288 /* disconnect a BE and FE */ 1289 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) 1290 { 1291 struct snd_soc_dpcm *dpcm, *d; 1292 unsigned long flags; 1293 1294 for_each_dpcm_be_safe(fe, stream, dpcm, d) { 1295 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", 1296 stream ? "capture" : "playback", 1297 dpcm->be->dai_link->name); 1298 1299 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) 1300 continue; 1301 1302 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", 1303 stream ? "capture" : "playback", fe->dai_link->name, 1304 stream ? "<-" : "->", dpcm->be->dai_link->name); 1305 1306 /* BEs still alive need new FE */ 1307 dpcm_be_reparent(fe, dpcm->be, stream); 1308 1309 #ifdef CONFIG_DEBUG_FS 1310 debugfs_remove(dpcm->debugfs_state); 1311 #endif 1312 spin_lock_irqsave(&fe->card->dpcm_lock, flags); 1313 list_del(&dpcm->list_be); 1314 list_del(&dpcm->list_fe); 1315 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 1316 kfree(dpcm); 1317 } 1318 } 1319 1320 /* get BE for DAI widget and stream */ 1321 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, 1322 struct snd_soc_dapm_widget *widget, int stream) 1323 { 1324 struct snd_soc_pcm_runtime *be; 1325 struct snd_soc_dai *dai; 1326 int i; 1327 1328 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); 1329 1330 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 1331 for_each_card_rtds(card, be) { 1332 1333 if (!be->dai_link->no_pcm) 1334 continue; 1335 1336 dev_dbg(card->dev, "ASoC: try BE : %s\n", 1337 be->cpu_dai->playback_widget ? 1338 be->cpu_dai->playback_widget->name : "(not set)"); 1339 1340 if (be->cpu_dai->playback_widget == widget) 1341 return be; 1342 1343 for_each_rtd_codec_dai(be, i, dai) { 1344 if (dai->playback_widget == widget) 1345 return be; 1346 } 1347 } 1348 } else { 1349 1350 for_each_card_rtds(card, be) { 1351 1352 if (!be->dai_link->no_pcm) 1353 continue; 1354 1355 dev_dbg(card->dev, "ASoC: try BE %s\n", 1356 be->cpu_dai->capture_widget ? 1357 be->cpu_dai->capture_widget->name : "(not set)"); 1358 1359 if (be->cpu_dai->capture_widget == widget) 1360 return be; 1361 1362 for_each_rtd_codec_dai(be, i, dai) { 1363 if (dai->capture_widget == widget) 1364 return be; 1365 } 1366 } 1367 } 1368 1369 /* dai link name and stream name set correctly ? */ 1370 dev_err(card->dev, "ASoC: can't get %s BE for %s\n", 1371 stream ? "capture" : "playback", widget->name); 1372 return NULL; 1373 } 1374 1375 static inline struct snd_soc_dapm_widget * 1376 dai_get_widget(struct snd_soc_dai *dai, int stream) 1377 { 1378 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1379 return dai->playback_widget; 1380 else 1381 return dai->capture_widget; 1382 } 1383 1384 static int widget_in_list(struct snd_soc_dapm_widget_list *list, 1385 struct snd_soc_dapm_widget *widget) 1386 { 1387 int i; 1388 1389 for (i = 0; i < list->num_widgets; i++) { 1390 if (widget == list->widgets[i]) 1391 return 1; 1392 } 1393 1394 return 0; 1395 } 1396 1397 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, 1398 enum snd_soc_dapm_direction dir) 1399 { 1400 struct snd_soc_card *card = widget->dapm->card; 1401 struct snd_soc_pcm_runtime *rtd; 1402 struct snd_soc_dai *dai; 1403 int i; 1404 1405 if (dir == SND_SOC_DAPM_DIR_OUT) { 1406 for_each_card_rtds(card, rtd) { 1407 if (!rtd->dai_link->no_pcm) 1408 continue; 1409 1410 if (rtd->cpu_dai->playback_widget == widget) 1411 return true; 1412 1413 for_each_rtd_codec_dai(rtd, i, dai) { 1414 if (dai->playback_widget == widget) 1415 return true; 1416 } 1417 } 1418 } else { /* SND_SOC_DAPM_DIR_IN */ 1419 for_each_card_rtds(card, rtd) { 1420 if (!rtd->dai_link->no_pcm) 1421 continue; 1422 1423 if (rtd->cpu_dai->capture_widget == widget) 1424 return true; 1425 1426 for_each_rtd_codec_dai(rtd, i, dai) { 1427 if (dai->capture_widget == widget) 1428 return true; 1429 } 1430 } 1431 } 1432 1433 return false; 1434 } 1435 1436 int dpcm_path_get(struct snd_soc_pcm_runtime *fe, 1437 int stream, struct snd_soc_dapm_widget_list **list) 1438 { 1439 struct snd_soc_dai *cpu_dai = fe->cpu_dai; 1440 int paths; 1441 1442 /* get number of valid DAI paths and their widgets */ 1443 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, 1444 dpcm_end_walk_at_be); 1445 1446 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, 1447 stream ? "capture" : "playback"); 1448 1449 return paths; 1450 } 1451 1452 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, 1453 struct snd_soc_dapm_widget_list **list_) 1454 { 1455 struct snd_soc_dpcm *dpcm; 1456 struct snd_soc_dapm_widget_list *list = *list_; 1457 struct snd_soc_dapm_widget *widget; 1458 struct snd_soc_dai *dai; 1459 int prune = 0; 1460 1461 /* Destroy any old FE <--> BE connections */ 1462 for_each_dpcm_be(fe, stream, dpcm) { 1463 unsigned int i; 1464 1465 /* is there a valid CPU DAI widget for this BE */ 1466 widget = dai_get_widget(dpcm->be->cpu_dai, stream); 1467 1468 /* prune the BE if it's no longer in our active list */ 1469 if (widget && widget_in_list(list, widget)) 1470 continue; 1471 1472 /* is there a valid CODEC DAI widget for this BE */ 1473 for_each_rtd_codec_dai(dpcm->be, i, dai) { 1474 widget = dai_get_widget(dai, stream); 1475 1476 /* prune the BE if it's no longer in our active list */ 1477 if (widget && widget_in_list(list, widget)) 1478 continue; 1479 } 1480 1481 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", 1482 stream ? "capture" : "playback", 1483 dpcm->be->dai_link->name, fe->dai_link->name); 1484 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 1485 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 1486 prune++; 1487 } 1488 1489 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); 1490 return prune; 1491 } 1492 1493 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, 1494 struct snd_soc_dapm_widget_list **list_) 1495 { 1496 struct snd_soc_card *card = fe->card; 1497 struct snd_soc_dapm_widget_list *list = *list_; 1498 struct snd_soc_pcm_runtime *be; 1499 int i, new = 0, err; 1500 1501 /* Create any new FE <--> BE connections */ 1502 for (i = 0; i < list->num_widgets; i++) { 1503 1504 switch (list->widgets[i]->id) { 1505 case snd_soc_dapm_dai_in: 1506 if (stream != SNDRV_PCM_STREAM_PLAYBACK) 1507 continue; 1508 break; 1509 case snd_soc_dapm_dai_out: 1510 if (stream != SNDRV_PCM_STREAM_CAPTURE) 1511 continue; 1512 break; 1513 default: 1514 continue; 1515 } 1516 1517 /* is there a valid BE rtd for this widget */ 1518 be = dpcm_get_be(card, list->widgets[i], stream); 1519 if (!be) { 1520 dev_err(fe->dev, "ASoC: no BE found for %s\n", 1521 list->widgets[i]->name); 1522 continue; 1523 } 1524 1525 /* make sure BE is a real BE */ 1526 if (!be->dai_link->no_pcm) 1527 continue; 1528 1529 /* don't connect if FE is not running */ 1530 if (!fe->dpcm[stream].runtime && !fe->fe_compr) 1531 continue; 1532 1533 /* newly connected FE and BE */ 1534 err = dpcm_be_connect(fe, be, stream); 1535 if (err < 0) { 1536 dev_err(fe->dev, "ASoC: can't connect %s\n", 1537 list->widgets[i]->name); 1538 break; 1539 } else if (err == 0) /* already connected */ 1540 continue; 1541 1542 /* new */ 1543 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 1544 new++; 1545 } 1546 1547 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); 1548 return new; 1549 } 1550 1551 /* 1552 * Find the corresponding BE DAIs that source or sink audio to this 1553 * FE substream. 1554 */ 1555 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, 1556 int stream, struct snd_soc_dapm_widget_list **list, int new) 1557 { 1558 if (new) 1559 return dpcm_add_paths(fe, stream, list); 1560 else 1561 return dpcm_prune_paths(fe, stream, list); 1562 } 1563 1564 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) 1565 { 1566 struct snd_soc_dpcm *dpcm; 1567 unsigned long flags; 1568 1569 spin_lock_irqsave(&fe->card->dpcm_lock, flags); 1570 for_each_dpcm_be(fe, stream, dpcm) 1571 dpcm->be->dpcm[stream].runtime_update = 1572 SND_SOC_DPCM_UPDATE_NO; 1573 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 1574 } 1575 1576 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, 1577 int stream) 1578 { 1579 struct snd_soc_dpcm *dpcm; 1580 1581 /* disable any enabled and non active backends */ 1582 for_each_dpcm_be(fe, stream, dpcm) { 1583 1584 struct snd_soc_pcm_runtime *be = dpcm->be; 1585 struct snd_pcm_substream *be_substream = 1586 snd_soc_dpcm_get_substream(be, stream); 1587 1588 if (be->dpcm[stream].users == 0) 1589 dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 1590 stream ? "capture" : "playback", 1591 be->dpcm[stream].state); 1592 1593 if (--be->dpcm[stream].users != 0) 1594 continue; 1595 1596 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 1597 continue; 1598 1599 soc_pcm_close(be_substream); 1600 be_substream->runtime = NULL; 1601 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1602 } 1603 } 1604 1605 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) 1606 { 1607 struct snd_soc_dpcm *dpcm; 1608 int err, count = 0; 1609 1610 /* only startup BE DAIs that are either sinks or sources to this FE DAI */ 1611 for_each_dpcm_be(fe, stream, dpcm) { 1612 1613 struct snd_soc_pcm_runtime *be = dpcm->be; 1614 struct snd_pcm_substream *be_substream = 1615 snd_soc_dpcm_get_substream(be, stream); 1616 1617 if (!be_substream) { 1618 dev_err(be->dev, "ASoC: no backend %s stream\n", 1619 stream ? "capture" : "playback"); 1620 continue; 1621 } 1622 1623 /* is this op for this BE ? */ 1624 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1625 continue; 1626 1627 /* first time the dpcm is open ? */ 1628 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) 1629 dev_err(be->dev, "ASoC: too many users %s at open %d\n", 1630 stream ? "capture" : "playback", 1631 be->dpcm[stream].state); 1632 1633 if (be->dpcm[stream].users++ != 0) 1634 continue; 1635 1636 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 1637 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 1638 continue; 1639 1640 dev_dbg(be->dev, "ASoC: open %s BE %s\n", 1641 stream ? "capture" : "playback", be->dai_link->name); 1642 1643 be_substream->runtime = be->dpcm[stream].runtime; 1644 err = soc_pcm_open(be_substream); 1645 if (err < 0) { 1646 dev_err(be->dev, "ASoC: BE open failed %d\n", err); 1647 be->dpcm[stream].users--; 1648 if (be->dpcm[stream].users < 0) 1649 dev_err(be->dev, "ASoC: no users %s at unwind %d\n", 1650 stream ? "capture" : "playback", 1651 be->dpcm[stream].state); 1652 1653 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1654 goto unwind; 1655 } 1656 1657 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 1658 count++; 1659 } 1660 1661 return count; 1662 1663 unwind: 1664 /* disable any enabled and non active backends */ 1665 for_each_dpcm_be_rollback(fe, stream, dpcm) { 1666 struct snd_soc_pcm_runtime *be = dpcm->be; 1667 struct snd_pcm_substream *be_substream = 1668 snd_soc_dpcm_get_substream(be, stream); 1669 1670 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1671 continue; 1672 1673 if (be->dpcm[stream].users == 0) 1674 dev_err(be->dev, "ASoC: no users %s at close %d\n", 1675 stream ? "capture" : "playback", 1676 be->dpcm[stream].state); 1677 1678 if (--be->dpcm[stream].users != 0) 1679 continue; 1680 1681 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 1682 continue; 1683 1684 soc_pcm_close(be_substream); 1685 be_substream->runtime = NULL; 1686 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1687 } 1688 1689 return err; 1690 } 1691 1692 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, 1693 struct snd_soc_pcm_stream *stream) 1694 { 1695 runtime->hw.rate_min = stream->rate_min; 1696 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX); 1697 runtime->hw.channels_min = stream->channels_min; 1698 runtime->hw.channels_max = stream->channels_max; 1699 if (runtime->hw.formats) 1700 runtime->hw.formats &= stream->formats; 1701 else 1702 runtime->hw.formats = stream->formats; 1703 runtime->hw.rates = stream->rates; 1704 } 1705 1706 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream, 1707 u64 *formats) 1708 { 1709 struct snd_soc_pcm_runtime *fe = substream->private_data; 1710 struct snd_soc_dpcm *dpcm; 1711 struct snd_soc_dai *dai; 1712 int stream = substream->stream; 1713 1714 if (!fe->dai_link->dpcm_merged_format) 1715 return; 1716 1717 /* 1718 * It returns merged BE codec format 1719 * if FE want to use it (= dpcm_merged_format) 1720 */ 1721 1722 for_each_dpcm_be(fe, stream, dpcm) { 1723 struct snd_soc_pcm_runtime *be = dpcm->be; 1724 struct snd_soc_dai_driver *codec_dai_drv; 1725 struct snd_soc_pcm_stream *codec_stream; 1726 int i; 1727 1728 for_each_rtd_codec_dai(be, i, dai) { 1729 /* 1730 * Skip CODECs which don't support the current stream 1731 * type. See soc_pcm_init_runtime_hw() for more details 1732 */ 1733 if (!snd_soc_dai_stream_valid(dai, stream)) 1734 continue; 1735 1736 codec_dai_drv = dai->driver; 1737 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1738 codec_stream = &codec_dai_drv->playback; 1739 else 1740 codec_stream = &codec_dai_drv->capture; 1741 1742 *formats &= codec_stream->formats; 1743 } 1744 } 1745 } 1746 1747 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream, 1748 unsigned int *channels_min, 1749 unsigned int *channels_max) 1750 { 1751 struct snd_soc_pcm_runtime *fe = substream->private_data; 1752 struct snd_soc_dpcm *dpcm; 1753 int stream = substream->stream; 1754 1755 if (!fe->dai_link->dpcm_merged_chan) 1756 return; 1757 1758 /* 1759 * It returns merged BE codec channel; 1760 * if FE want to use it (= dpcm_merged_chan) 1761 */ 1762 1763 for_each_dpcm_be(fe, stream, dpcm) { 1764 struct snd_soc_pcm_runtime *be = dpcm->be; 1765 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver; 1766 struct snd_soc_dai_driver *codec_dai_drv; 1767 struct snd_soc_pcm_stream *codec_stream; 1768 struct snd_soc_pcm_stream *cpu_stream; 1769 1770 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1771 cpu_stream = &cpu_dai_drv->playback; 1772 else 1773 cpu_stream = &cpu_dai_drv->capture; 1774 1775 *channels_min = max(*channels_min, cpu_stream->channels_min); 1776 *channels_max = min(*channels_max, cpu_stream->channels_max); 1777 1778 /* 1779 * chan min/max cannot be enforced if there are multiple CODEC 1780 * DAIs connected to a single CPU DAI, use CPU DAI's directly 1781 */ 1782 if (be->num_codecs == 1) { 1783 codec_dai_drv = be->codec_dais[0]->driver; 1784 1785 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1786 codec_stream = &codec_dai_drv->playback; 1787 else 1788 codec_stream = &codec_dai_drv->capture; 1789 1790 *channels_min = max(*channels_min, 1791 codec_stream->channels_min); 1792 *channels_max = min(*channels_max, 1793 codec_stream->channels_max); 1794 } 1795 } 1796 } 1797 1798 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream, 1799 unsigned int *rates, 1800 unsigned int *rate_min, 1801 unsigned int *rate_max) 1802 { 1803 struct snd_soc_pcm_runtime *fe = substream->private_data; 1804 struct snd_soc_dpcm *dpcm; 1805 int stream = substream->stream; 1806 1807 if (!fe->dai_link->dpcm_merged_rate) 1808 return; 1809 1810 /* 1811 * It returns merged BE codec channel; 1812 * if FE want to use it (= dpcm_merged_chan) 1813 */ 1814 1815 for_each_dpcm_be(fe, stream, dpcm) { 1816 struct snd_soc_pcm_runtime *be = dpcm->be; 1817 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver; 1818 struct snd_soc_dai_driver *codec_dai_drv; 1819 struct snd_soc_pcm_stream *codec_stream; 1820 struct snd_soc_pcm_stream *cpu_stream; 1821 struct snd_soc_dai *dai; 1822 int i; 1823 1824 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1825 cpu_stream = &cpu_dai_drv->playback; 1826 else 1827 cpu_stream = &cpu_dai_drv->capture; 1828 1829 *rate_min = max(*rate_min, cpu_stream->rate_min); 1830 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max); 1831 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates); 1832 1833 for_each_rtd_codec_dai(be, i, dai) { 1834 /* 1835 * Skip CODECs which don't support the current stream 1836 * type. See soc_pcm_init_runtime_hw() for more details 1837 */ 1838 if (!snd_soc_dai_stream_valid(dai, stream)) 1839 continue; 1840 1841 codec_dai_drv = dai->driver; 1842 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1843 codec_stream = &codec_dai_drv->playback; 1844 else 1845 codec_stream = &codec_dai_drv->capture; 1846 1847 *rate_min = max(*rate_min, codec_stream->rate_min); 1848 *rate_max = min_not_zero(*rate_max, 1849 codec_stream->rate_max); 1850 *rates = snd_pcm_rate_mask_intersect(*rates, 1851 codec_stream->rates); 1852 } 1853 } 1854 } 1855 1856 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) 1857 { 1858 struct snd_pcm_runtime *runtime = substream->runtime; 1859 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1860 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1861 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 1862 1863 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 1864 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback); 1865 else 1866 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture); 1867 1868 dpcm_runtime_merge_format(substream, &runtime->hw.formats); 1869 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min, 1870 &runtime->hw.channels_max); 1871 dpcm_runtime_merge_rate(substream, &runtime->hw.rates, 1872 &runtime->hw.rate_min, &runtime->hw.rate_max); 1873 } 1874 1875 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); 1876 1877 /* Set FE's runtime_update state; the state is protected via PCM stream lock 1878 * for avoiding the race with trigger callback. 1879 * If the state is unset and a trigger is pending while the previous operation, 1880 * process the pending trigger action here. 1881 */ 1882 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, 1883 int stream, enum snd_soc_dpcm_update state) 1884 { 1885 struct snd_pcm_substream *substream = 1886 snd_soc_dpcm_get_substream(fe, stream); 1887 1888 snd_pcm_stream_lock_irq(substream); 1889 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { 1890 dpcm_fe_dai_do_trigger(substream, 1891 fe->dpcm[stream].trigger_pending - 1); 1892 fe->dpcm[stream].trigger_pending = 0; 1893 } 1894 fe->dpcm[stream].runtime_update = state; 1895 snd_pcm_stream_unlock_irq(substream); 1896 } 1897 1898 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, 1899 int stream) 1900 { 1901 struct snd_soc_dpcm *dpcm; 1902 struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 1903 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai; 1904 int err; 1905 1906 /* apply symmetry for FE */ 1907 if (soc_pcm_has_symmetry(fe_substream)) 1908 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1909 1910 /* Symmetry only applies if we've got an active stream. */ 1911 if (fe_cpu_dai->active) { 1912 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); 1913 if (err < 0) 1914 return err; 1915 } 1916 1917 /* apply symmetry for BE */ 1918 for_each_dpcm_be(fe, stream, dpcm) { 1919 struct snd_soc_pcm_runtime *be = dpcm->be; 1920 struct snd_pcm_substream *be_substream = 1921 snd_soc_dpcm_get_substream(be, stream); 1922 struct snd_soc_pcm_runtime *rtd; 1923 struct snd_soc_dai *codec_dai; 1924 int i; 1925 1926 /* A backend may not have the requested substream */ 1927 if (!be_substream) 1928 continue; 1929 1930 rtd = be_substream->private_data; 1931 if (rtd->dai_link->be_hw_params_fixup) 1932 continue; 1933 1934 if (soc_pcm_has_symmetry(be_substream)) 1935 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1936 1937 /* Symmetry only applies if we've got an active stream. */ 1938 if (rtd->cpu_dai->active) { 1939 err = soc_pcm_apply_symmetry(fe_substream, 1940 rtd->cpu_dai); 1941 if (err < 0) 1942 return err; 1943 } 1944 1945 for_each_rtd_codec_dai(rtd, i, codec_dai) { 1946 if (codec_dai->active) { 1947 err = soc_pcm_apply_symmetry(fe_substream, 1948 codec_dai); 1949 if (err < 0) 1950 return err; 1951 } 1952 } 1953 } 1954 1955 return 0; 1956 } 1957 1958 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) 1959 { 1960 struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 1961 struct snd_pcm_runtime *runtime = fe_substream->runtime; 1962 int stream = fe_substream->stream, ret = 0; 1963 1964 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 1965 1966 ret = dpcm_be_dai_startup(fe, fe_substream->stream); 1967 if (ret < 0) { 1968 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); 1969 goto be_err; 1970 } 1971 1972 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); 1973 1974 /* start the DAI frontend */ 1975 ret = soc_pcm_open(fe_substream); 1976 if (ret < 0) { 1977 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); 1978 goto unwind; 1979 } 1980 1981 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 1982 1983 dpcm_set_fe_runtime(fe_substream); 1984 snd_pcm_limit_hw_rates(runtime); 1985 1986 ret = dpcm_apply_symmetry(fe_substream, stream); 1987 if (ret < 0) { 1988 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", 1989 ret); 1990 goto unwind; 1991 } 1992 1993 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 1994 return 0; 1995 1996 unwind: 1997 dpcm_be_dai_startup_unwind(fe, fe_substream->stream); 1998 be_err: 1999 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2000 return ret; 2001 } 2002 2003 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 2004 { 2005 struct snd_soc_dpcm *dpcm; 2006 2007 /* only shutdown BEs that are either sinks or sources to this FE DAI */ 2008 for_each_dpcm_be(fe, stream, dpcm) { 2009 2010 struct snd_soc_pcm_runtime *be = dpcm->be; 2011 struct snd_pcm_substream *be_substream = 2012 snd_soc_dpcm_get_substream(be, stream); 2013 2014 /* is this op for this BE ? */ 2015 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2016 continue; 2017 2018 if (be->dpcm[stream].users == 0) 2019 dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 2020 stream ? "capture" : "playback", 2021 be->dpcm[stream].state); 2022 2023 if (--be->dpcm[stream].users != 0) 2024 continue; 2025 2026 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 2027 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) { 2028 soc_pcm_hw_free(be_substream); 2029 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2030 } 2031 2032 dev_dbg(be->dev, "ASoC: close BE %s\n", 2033 be->dai_link->name); 2034 2035 soc_pcm_close(be_substream); 2036 be_substream->runtime = NULL; 2037 2038 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 2039 } 2040 return 0; 2041 } 2042 2043 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) 2044 { 2045 struct snd_soc_pcm_runtime *fe = substream->private_data; 2046 int stream = substream->stream; 2047 2048 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2049 2050 /* shutdown the BEs */ 2051 dpcm_be_dai_shutdown(fe, substream->stream); 2052 2053 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); 2054 2055 /* now shutdown the frontend */ 2056 soc_pcm_close(substream); 2057 2058 /* run the stream event for each BE */ 2059 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); 2060 2061 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 2062 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2063 return 0; 2064 } 2065 2066 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) 2067 { 2068 struct snd_soc_dpcm *dpcm; 2069 2070 /* only hw_params backends that are either sinks or sources 2071 * to this frontend DAI */ 2072 for_each_dpcm_be(fe, stream, dpcm) { 2073 2074 struct snd_soc_pcm_runtime *be = dpcm->be; 2075 struct snd_pcm_substream *be_substream = 2076 snd_soc_dpcm_get_substream(be, stream); 2077 2078 /* is this op for this BE ? */ 2079 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2080 continue; 2081 2082 /* only free hw when no longer used - check all FEs */ 2083 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 2084 continue; 2085 2086 /* do not free hw if this BE is used by other FE */ 2087 if (be->dpcm[stream].users > 1) 2088 continue; 2089 2090 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2091 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 2092 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 2093 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && 2094 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 2095 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 2096 continue; 2097 2098 dev_dbg(be->dev, "ASoC: hw_free BE %s\n", 2099 be->dai_link->name); 2100 2101 soc_pcm_hw_free(be_substream); 2102 2103 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2104 } 2105 2106 return 0; 2107 } 2108 2109 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) 2110 { 2111 struct snd_soc_pcm_runtime *fe = substream->private_data; 2112 int err, stream = substream->stream; 2113 2114 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2115 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2116 2117 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); 2118 2119 /* call hw_free on the frontend */ 2120 err = soc_pcm_hw_free(substream); 2121 if (err < 0) 2122 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", 2123 fe->dai_link->name); 2124 2125 /* only hw_params backends that are either sinks or sources 2126 * to this frontend DAI */ 2127 err = dpcm_be_dai_hw_free(fe, stream); 2128 2129 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2130 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2131 2132 mutex_unlock(&fe->card->mutex); 2133 return 0; 2134 } 2135 2136 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) 2137 { 2138 struct snd_soc_dpcm *dpcm; 2139 int ret; 2140 2141 for_each_dpcm_be(fe, stream, dpcm) { 2142 2143 struct snd_soc_pcm_runtime *be = dpcm->be; 2144 struct snd_pcm_substream *be_substream = 2145 snd_soc_dpcm_get_substream(be, stream); 2146 2147 /* is this op for this BE ? */ 2148 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2149 continue; 2150 2151 /* copy params for each dpcm */ 2152 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, 2153 sizeof(struct snd_pcm_hw_params)); 2154 2155 /* perform any hw_params fixups */ 2156 if (be->dai_link->be_hw_params_fixup) { 2157 ret = be->dai_link->be_hw_params_fixup(be, 2158 &dpcm->hw_params); 2159 if (ret < 0) { 2160 dev_err(be->dev, 2161 "ASoC: hw_params BE fixup failed %d\n", 2162 ret); 2163 goto unwind; 2164 } 2165 } 2166 2167 /* copy the fixed-up hw params for BE dai */ 2168 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params, 2169 sizeof(struct snd_pcm_hw_params)); 2170 2171 /* only allow hw_params() if no connected FEs are running */ 2172 if (!snd_soc_dpcm_can_be_params(fe, be, stream)) 2173 continue; 2174 2175 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2176 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2177 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) 2178 continue; 2179 2180 dev_dbg(be->dev, "ASoC: hw_params BE %s\n", 2181 be->dai_link->name); 2182 2183 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); 2184 if (ret < 0) { 2185 dev_err(dpcm->be->dev, 2186 "ASoC: hw_params BE failed %d\n", ret); 2187 goto unwind; 2188 } 2189 2190 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 2191 } 2192 return 0; 2193 2194 unwind: 2195 /* disable any enabled and non active backends */ 2196 for_each_dpcm_be_rollback(fe, stream, dpcm) { 2197 struct snd_soc_pcm_runtime *be = dpcm->be; 2198 struct snd_pcm_substream *be_substream = 2199 snd_soc_dpcm_get_substream(be, stream); 2200 2201 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2202 continue; 2203 2204 /* only allow hw_free() if no connected FEs are running */ 2205 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 2206 continue; 2207 2208 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2209 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2210 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 2211 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 2212 continue; 2213 2214 soc_pcm_hw_free(be_substream); 2215 } 2216 2217 return ret; 2218 } 2219 2220 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, 2221 struct snd_pcm_hw_params *params) 2222 { 2223 struct snd_soc_pcm_runtime *fe = substream->private_data; 2224 int ret, stream = substream->stream; 2225 2226 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2227 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2228 2229 memcpy(&fe->dpcm[substream->stream].hw_params, params, 2230 sizeof(struct snd_pcm_hw_params)); 2231 ret = dpcm_be_dai_hw_params(fe, substream->stream); 2232 if (ret < 0) { 2233 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); 2234 goto out; 2235 } 2236 2237 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", 2238 fe->dai_link->name, params_rate(params), 2239 params_channels(params), params_format(params)); 2240 2241 /* call hw_params on the frontend */ 2242 ret = soc_pcm_hw_params(substream, params); 2243 if (ret < 0) { 2244 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); 2245 dpcm_be_dai_hw_free(fe, stream); 2246 } else 2247 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 2248 2249 out: 2250 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2251 mutex_unlock(&fe->card->mutex); 2252 return ret; 2253 } 2254 2255 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, 2256 struct snd_pcm_substream *substream, int cmd) 2257 { 2258 int ret; 2259 2260 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", 2261 dpcm->be->dai_link->name, cmd); 2262 2263 ret = soc_pcm_trigger(substream, cmd); 2264 if (ret < 0) 2265 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); 2266 2267 return ret; 2268 } 2269 2270 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, 2271 int cmd) 2272 { 2273 struct snd_soc_dpcm *dpcm; 2274 int ret = 0; 2275 2276 for_each_dpcm_be(fe, stream, dpcm) { 2277 2278 struct snd_soc_pcm_runtime *be = dpcm->be; 2279 struct snd_pcm_substream *be_substream = 2280 snd_soc_dpcm_get_substream(be, stream); 2281 2282 /* is this op for this BE ? */ 2283 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2284 continue; 2285 2286 switch (cmd) { 2287 case SNDRV_PCM_TRIGGER_START: 2288 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 2289 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 2290 continue; 2291 2292 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 2293 if (ret) 2294 return ret; 2295 2296 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2297 break; 2298 case SNDRV_PCM_TRIGGER_RESUME: 2299 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 2300 continue; 2301 2302 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 2303 if (ret) 2304 return ret; 2305 2306 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2307 break; 2308 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2309 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 2310 continue; 2311 2312 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 2313 if (ret) 2314 return ret; 2315 2316 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2317 break; 2318 case SNDRV_PCM_TRIGGER_STOP: 2319 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2320 continue; 2321 2322 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 2323 continue; 2324 2325 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 2326 if (ret) 2327 return ret; 2328 2329 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 2330 break; 2331 case SNDRV_PCM_TRIGGER_SUSPEND: 2332 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2333 continue; 2334 2335 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 2336 continue; 2337 2338 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 2339 if (ret) 2340 return ret; 2341 2342 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; 2343 break; 2344 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2345 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2346 continue; 2347 2348 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 2349 continue; 2350 2351 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 2352 if (ret) 2353 return ret; 2354 2355 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 2356 break; 2357 } 2358 } 2359 2360 return ret; 2361 } 2362 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); 2363 2364 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) 2365 { 2366 struct snd_soc_pcm_runtime *fe = substream->private_data; 2367 int stream = substream->stream, ret; 2368 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2369 2370 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 2371 2372 switch (trigger) { 2373 case SND_SOC_DPCM_TRIGGER_PRE: 2374 /* call trigger on the frontend before the backend. */ 2375 2376 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", 2377 fe->dai_link->name, cmd); 2378 2379 ret = soc_pcm_trigger(substream, cmd); 2380 if (ret < 0) { 2381 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2382 goto out; 2383 } 2384 2385 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2386 break; 2387 case SND_SOC_DPCM_TRIGGER_POST: 2388 /* call trigger on the frontend after the backend. */ 2389 2390 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2391 if (ret < 0) { 2392 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2393 goto out; 2394 } 2395 2396 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", 2397 fe->dai_link->name, cmd); 2398 2399 ret = soc_pcm_trigger(substream, cmd); 2400 break; 2401 case SND_SOC_DPCM_TRIGGER_BESPOKE: 2402 /* bespoke trigger() - handles both FE and BEs */ 2403 2404 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", 2405 fe->dai_link->name, cmd); 2406 2407 ret = soc_pcm_bespoke_trigger(substream, cmd); 2408 if (ret < 0) { 2409 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2410 goto out; 2411 } 2412 break; 2413 default: 2414 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, 2415 fe->dai_link->name); 2416 ret = -EINVAL; 2417 goto out; 2418 } 2419 2420 switch (cmd) { 2421 case SNDRV_PCM_TRIGGER_START: 2422 case SNDRV_PCM_TRIGGER_RESUME: 2423 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2424 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2425 break; 2426 case SNDRV_PCM_TRIGGER_STOP: 2427 case SNDRV_PCM_TRIGGER_SUSPEND: 2428 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 2429 break; 2430 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2431 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 2432 break; 2433 } 2434 2435 out: 2436 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 2437 return ret; 2438 } 2439 2440 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) 2441 { 2442 struct snd_soc_pcm_runtime *fe = substream->private_data; 2443 int stream = substream->stream; 2444 2445 /* if FE's runtime_update is already set, we're in race; 2446 * process this trigger later at exit 2447 */ 2448 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { 2449 fe->dpcm[stream].trigger_pending = cmd + 1; 2450 return 0; /* delayed, assuming it's successful */ 2451 } 2452 2453 /* we're alone, let's trigger */ 2454 return dpcm_fe_dai_do_trigger(substream, cmd); 2455 } 2456 2457 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) 2458 { 2459 struct snd_soc_dpcm *dpcm; 2460 int ret = 0; 2461 2462 for_each_dpcm_be(fe, stream, dpcm) { 2463 2464 struct snd_soc_pcm_runtime *be = dpcm->be; 2465 struct snd_pcm_substream *be_substream = 2466 snd_soc_dpcm_get_substream(be, stream); 2467 2468 /* is this op for this BE ? */ 2469 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2470 continue; 2471 2472 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2473 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 2474 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 2475 continue; 2476 2477 dev_dbg(be->dev, "ASoC: prepare BE %s\n", 2478 be->dai_link->name); 2479 2480 ret = soc_pcm_prepare(be_substream); 2481 if (ret < 0) { 2482 dev_err(be->dev, "ASoC: backend prepare failed %d\n", 2483 ret); 2484 break; 2485 } 2486 2487 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 2488 } 2489 return ret; 2490 } 2491 2492 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) 2493 { 2494 struct snd_soc_pcm_runtime *fe = substream->private_data; 2495 int stream = substream->stream, ret = 0; 2496 2497 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2498 2499 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); 2500 2501 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2502 2503 /* there is no point preparing this FE if there are no BEs */ 2504 if (list_empty(&fe->dpcm[stream].be_clients)) { 2505 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", 2506 fe->dai_link->name); 2507 ret = -EINVAL; 2508 goto out; 2509 } 2510 2511 ret = dpcm_be_dai_prepare(fe, substream->stream); 2512 if (ret < 0) 2513 goto out; 2514 2515 /* call prepare on the frontend */ 2516 ret = soc_pcm_prepare(substream); 2517 if (ret < 0) { 2518 dev_err(fe->dev,"ASoC: prepare FE %s failed\n", 2519 fe->dai_link->name); 2520 goto out; 2521 } 2522 2523 /* run the stream event for each BE */ 2524 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); 2525 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 2526 2527 out: 2528 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2529 mutex_unlock(&fe->card->mutex); 2530 2531 return ret; 2532 } 2533 2534 static int soc_pcm_ioctl(struct snd_pcm_substream *substream, 2535 unsigned int cmd, void *arg) 2536 { 2537 struct snd_soc_pcm_runtime *rtd = substream->private_data; 2538 struct snd_soc_component *component; 2539 struct snd_soc_rtdcom_list *rtdcom; 2540 2541 for_each_rtdcom(rtd, rtdcom) { 2542 component = rtdcom->component; 2543 2544 if (!component->driver->ops || 2545 !component->driver->ops->ioctl) 2546 continue; 2547 2548 /* FIXME: use 1st ioctl */ 2549 return component->driver->ops->ioctl(substream, cmd, arg); 2550 } 2551 2552 return snd_pcm_lib_ioctl(substream, cmd, arg); 2553 } 2554 2555 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 2556 { 2557 struct snd_pcm_substream *substream = 2558 snd_soc_dpcm_get_substream(fe, stream); 2559 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2560 int err; 2561 2562 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", 2563 stream ? "capture" : "playback", fe->dai_link->name); 2564 2565 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 2566 /* call bespoke trigger - FE takes care of all BE triggers */ 2567 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", 2568 fe->dai_link->name); 2569 2570 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); 2571 if (err < 0) 2572 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 2573 } else { 2574 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", 2575 fe->dai_link->name); 2576 2577 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); 2578 if (err < 0) 2579 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 2580 } 2581 2582 err = dpcm_be_dai_hw_free(fe, stream); 2583 if (err < 0) 2584 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); 2585 2586 err = dpcm_be_dai_shutdown(fe, stream); 2587 if (err < 0) 2588 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); 2589 2590 /* run the stream event for each BE */ 2591 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2592 2593 return 0; 2594 } 2595 2596 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) 2597 { 2598 struct snd_pcm_substream *substream = 2599 snd_soc_dpcm_get_substream(fe, stream); 2600 struct snd_soc_dpcm *dpcm; 2601 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2602 int ret; 2603 unsigned long flags; 2604 2605 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", 2606 stream ? "capture" : "playback", fe->dai_link->name); 2607 2608 /* Only start the BE if the FE is ready */ 2609 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || 2610 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) 2611 return -EINVAL; 2612 2613 /* startup must always be called for new BEs */ 2614 ret = dpcm_be_dai_startup(fe, stream); 2615 if (ret < 0) 2616 goto disconnect; 2617 2618 /* keep going if FE state is > open */ 2619 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) 2620 return 0; 2621 2622 ret = dpcm_be_dai_hw_params(fe, stream); 2623 if (ret < 0) 2624 goto close; 2625 2626 /* keep going if FE state is > hw_params */ 2627 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) 2628 return 0; 2629 2630 2631 ret = dpcm_be_dai_prepare(fe, stream); 2632 if (ret < 0) 2633 goto hw_free; 2634 2635 /* run the stream event for each BE */ 2636 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2637 2638 /* keep going if FE state is > prepare */ 2639 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || 2640 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) 2641 return 0; 2642 2643 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 2644 /* call trigger on the frontend - FE takes care of all BE triggers */ 2645 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", 2646 fe->dai_link->name); 2647 2648 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); 2649 if (ret < 0) { 2650 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); 2651 goto hw_free; 2652 } 2653 } else { 2654 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", 2655 fe->dai_link->name); 2656 2657 ret = dpcm_be_dai_trigger(fe, stream, 2658 SNDRV_PCM_TRIGGER_START); 2659 if (ret < 0) { 2660 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2661 goto hw_free; 2662 } 2663 } 2664 2665 return 0; 2666 2667 hw_free: 2668 dpcm_be_dai_hw_free(fe, stream); 2669 close: 2670 dpcm_be_dai_shutdown(fe, stream); 2671 disconnect: 2672 /* disconnect any non started BEs */ 2673 spin_lock_irqsave(&fe->card->dpcm_lock, flags); 2674 for_each_dpcm_be(fe, stream, dpcm) { 2675 struct snd_soc_pcm_runtime *be = dpcm->be; 2676 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2677 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2678 } 2679 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 2680 2681 return ret; 2682 } 2683 2684 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream) 2685 { 2686 int ret; 2687 2688 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); 2689 ret = dpcm_run_update_startup(fe, stream); 2690 if (ret < 0) 2691 dev_err(fe->dev, "ASoC: failed to startup some BEs\n"); 2692 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2693 2694 return ret; 2695 } 2696 2697 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream) 2698 { 2699 int ret; 2700 2701 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); 2702 ret = dpcm_run_update_shutdown(fe, stream); 2703 if (ret < 0) 2704 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); 2705 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2706 2707 return ret; 2708 } 2709 2710 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) 2711 { 2712 struct snd_soc_dapm_widget_list *list; 2713 int count, paths; 2714 2715 if (!fe->dai_link->dynamic) 2716 return 0; 2717 2718 /* only check active links */ 2719 if (!fe->cpu_dai->active) 2720 return 0; 2721 2722 /* DAPM sync will call this to update DSP paths */ 2723 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", 2724 new ? "new" : "old", fe->dai_link->name); 2725 2726 /* skip if FE doesn't have playback capability */ 2727 if (!fe->cpu_dai->driver->playback.channels_min || 2728 !fe->codec_dai->driver->playback.channels_min) 2729 goto capture; 2730 2731 /* skip if FE isn't currently playing */ 2732 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active) 2733 goto capture; 2734 2735 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list); 2736 if (paths < 0) { 2737 dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 2738 fe->dai_link->name, "playback"); 2739 return paths; 2740 } 2741 2742 /* update any playback paths */ 2743 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new); 2744 if (count) { 2745 if (new) 2746 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK); 2747 else 2748 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK); 2749 2750 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); 2751 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); 2752 } 2753 2754 dpcm_path_put(&list); 2755 2756 capture: 2757 /* skip if FE doesn't have capture capability */ 2758 if (!fe->cpu_dai->driver->capture.channels_min || 2759 !fe->codec_dai->driver->capture.channels_min) 2760 return 0; 2761 2762 /* skip if FE isn't currently capturing */ 2763 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active) 2764 return 0; 2765 2766 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list); 2767 if (paths < 0) { 2768 dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 2769 fe->dai_link->name, "capture"); 2770 return paths; 2771 } 2772 2773 /* update any old capture paths */ 2774 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new); 2775 if (count) { 2776 if (new) 2777 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE); 2778 else 2779 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE); 2780 2781 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); 2782 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); 2783 } 2784 2785 dpcm_path_put(&list); 2786 2787 return 0; 2788 } 2789 2790 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and 2791 * any DAI links. 2792 */ 2793 int soc_dpcm_runtime_update(struct snd_soc_card *card) 2794 { 2795 struct snd_soc_pcm_runtime *fe; 2796 int ret = 0; 2797 2798 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2799 /* shutdown all old paths first */ 2800 for_each_card_rtds(card, fe) { 2801 ret = soc_dpcm_fe_runtime_update(fe, 0); 2802 if (ret) 2803 goto out; 2804 } 2805 2806 /* bring new paths up */ 2807 for_each_card_rtds(card, fe) { 2808 ret = soc_dpcm_fe_runtime_update(fe, 1); 2809 if (ret) 2810 goto out; 2811 } 2812 2813 out: 2814 mutex_unlock(&card->mutex); 2815 return ret; 2816 } 2817 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute) 2818 { 2819 struct snd_soc_dpcm *dpcm; 2820 struct snd_soc_dai *dai; 2821 2822 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) { 2823 2824 struct snd_soc_pcm_runtime *be = dpcm->be; 2825 int i; 2826 2827 if (be->dai_link->ignore_suspend) 2828 continue; 2829 2830 for_each_rtd_codec_dai(be, i, dai) { 2831 struct snd_soc_dai_driver *drv = dai->driver; 2832 2833 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", 2834 be->dai_link->name); 2835 2836 if (drv->ops && drv->ops->digital_mute && 2837 dai->playback_active) 2838 drv->ops->digital_mute(dai, mute); 2839 } 2840 } 2841 2842 return 0; 2843 } 2844 2845 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) 2846 { 2847 struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 2848 struct snd_soc_dpcm *dpcm; 2849 struct snd_soc_dapm_widget_list *list; 2850 int ret; 2851 int stream = fe_substream->stream; 2852 2853 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2854 fe->dpcm[stream].runtime = fe_substream->runtime; 2855 2856 ret = dpcm_path_get(fe, stream, &list); 2857 if (ret < 0) { 2858 mutex_unlock(&fe->card->mutex); 2859 return ret; 2860 } else if (ret == 0) { 2861 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", 2862 fe->dai_link->name, stream ? "capture" : "playback"); 2863 } 2864 2865 /* calculate valid and active FE <-> BE dpcms */ 2866 dpcm_process_paths(fe, stream, &list, 1); 2867 2868 ret = dpcm_fe_dai_startup(fe_substream); 2869 if (ret < 0) { 2870 /* clean up all links */ 2871 for_each_dpcm_be(fe, stream, dpcm) 2872 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2873 2874 dpcm_be_disconnect(fe, stream); 2875 fe->dpcm[stream].runtime = NULL; 2876 } 2877 2878 dpcm_clear_pending_state(fe, stream); 2879 dpcm_path_put(&list); 2880 mutex_unlock(&fe->card->mutex); 2881 return ret; 2882 } 2883 2884 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) 2885 { 2886 struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 2887 struct snd_soc_dpcm *dpcm; 2888 int stream = fe_substream->stream, ret; 2889 2890 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2891 ret = dpcm_fe_dai_shutdown(fe_substream); 2892 2893 /* mark FE's links ready to prune */ 2894 for_each_dpcm_be(fe, stream, dpcm) 2895 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2896 2897 dpcm_be_disconnect(fe, stream); 2898 2899 fe->dpcm[stream].runtime = NULL; 2900 mutex_unlock(&fe->card->mutex); 2901 return ret; 2902 } 2903 2904 static void soc_pcm_private_free(struct snd_pcm *pcm) 2905 { 2906 struct snd_soc_pcm_runtime *rtd = pcm->private_data; 2907 struct snd_soc_rtdcom_list *rtdcom; 2908 struct snd_soc_component *component; 2909 2910 /* need to sync the delayed work before releasing resources */ 2911 flush_delayed_work(&rtd->delayed_work); 2912 for_each_rtdcom(rtd, rtdcom) { 2913 component = rtdcom->component; 2914 2915 if (component->driver->pcm_free) 2916 component->driver->pcm_free(pcm); 2917 } 2918 } 2919 2920 static int soc_rtdcom_ack(struct snd_pcm_substream *substream) 2921 { 2922 struct snd_soc_pcm_runtime *rtd = substream->private_data; 2923 struct snd_soc_rtdcom_list *rtdcom; 2924 struct snd_soc_component *component; 2925 2926 for_each_rtdcom(rtd, rtdcom) { 2927 component = rtdcom->component; 2928 2929 if (!component->driver->ops || 2930 !component->driver->ops->ack) 2931 continue; 2932 2933 /* FIXME. it returns 1st ask now */ 2934 return component->driver->ops->ack(substream); 2935 } 2936 2937 return -EINVAL; 2938 } 2939 2940 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel, 2941 unsigned long pos, void __user *buf, 2942 unsigned long bytes) 2943 { 2944 struct snd_soc_pcm_runtime *rtd = substream->private_data; 2945 struct snd_soc_rtdcom_list *rtdcom; 2946 struct snd_soc_component *component; 2947 2948 for_each_rtdcom(rtd, rtdcom) { 2949 component = rtdcom->component; 2950 2951 if (!component->driver->ops || 2952 !component->driver->ops->copy_user) 2953 continue; 2954 2955 /* FIXME. it returns 1st copy now */ 2956 return component->driver->ops->copy_user(substream, channel, 2957 pos, buf, bytes); 2958 } 2959 2960 return -EINVAL; 2961 } 2962 2963 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel, 2964 unsigned long pos, void *buf, unsigned long bytes) 2965 { 2966 struct snd_soc_pcm_runtime *rtd = substream->private_data; 2967 struct snd_soc_rtdcom_list *rtdcom; 2968 struct snd_soc_component *component; 2969 2970 for_each_rtdcom(rtd, rtdcom) { 2971 component = rtdcom->component; 2972 2973 if (!component->driver->ops || 2974 !component->driver->ops->copy_kernel) 2975 continue; 2976 2977 /* FIXME. it returns 1st copy now */ 2978 return component->driver->ops->copy_kernel(substream, channel, 2979 pos, buf, bytes); 2980 } 2981 2982 return -EINVAL; 2983 } 2984 2985 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel, 2986 unsigned long pos, unsigned long bytes) 2987 { 2988 struct snd_soc_pcm_runtime *rtd = substream->private_data; 2989 struct snd_soc_rtdcom_list *rtdcom; 2990 struct snd_soc_component *component; 2991 2992 for_each_rtdcom(rtd, rtdcom) { 2993 component = rtdcom->component; 2994 2995 if (!component->driver->ops || 2996 !component->driver->ops->fill_silence) 2997 continue; 2998 2999 /* FIXME. it returns 1st silence now */ 3000 return component->driver->ops->fill_silence(substream, channel, 3001 pos, bytes); 3002 } 3003 3004 return -EINVAL; 3005 } 3006 3007 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream, 3008 unsigned long offset) 3009 { 3010 struct snd_soc_pcm_runtime *rtd = substream->private_data; 3011 struct snd_soc_rtdcom_list *rtdcom; 3012 struct snd_soc_component *component; 3013 struct page *page; 3014 3015 for_each_rtdcom(rtd, rtdcom) { 3016 component = rtdcom->component; 3017 3018 if (!component->driver->ops || 3019 !component->driver->ops->page) 3020 continue; 3021 3022 /* FIXME. it returns 1st page now */ 3023 page = component->driver->ops->page(substream, offset); 3024 if (page) 3025 return page; 3026 } 3027 3028 return NULL; 3029 } 3030 3031 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream, 3032 struct vm_area_struct *vma) 3033 { 3034 struct snd_soc_pcm_runtime *rtd = substream->private_data; 3035 struct snd_soc_rtdcom_list *rtdcom; 3036 struct snd_soc_component *component; 3037 3038 for_each_rtdcom(rtd, rtdcom) { 3039 component = rtdcom->component; 3040 3041 if (!component->driver->ops || 3042 !component->driver->ops->mmap) 3043 continue; 3044 3045 /* FIXME. it returns 1st mmap now */ 3046 return component->driver->ops->mmap(substream, vma); 3047 } 3048 3049 return -EINVAL; 3050 } 3051 3052 /* create a new pcm */ 3053 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 3054 { 3055 struct snd_soc_dai *codec_dai; 3056 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 3057 struct snd_soc_component *component; 3058 struct snd_soc_rtdcom_list *rtdcom; 3059 struct snd_pcm *pcm; 3060 char new_name[64]; 3061 int ret = 0, playback = 0, capture = 0; 3062 int i; 3063 3064 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { 3065 playback = rtd->dai_link->dpcm_playback; 3066 capture = rtd->dai_link->dpcm_capture; 3067 } else { 3068 for_each_rtd_codec_dai(rtd, i, codec_dai) { 3069 if (codec_dai->driver->playback.channels_min) 3070 playback = 1; 3071 if (codec_dai->driver->capture.channels_min) 3072 capture = 1; 3073 } 3074 3075 capture = capture && cpu_dai->driver->capture.channels_min; 3076 playback = playback && cpu_dai->driver->playback.channels_min; 3077 } 3078 3079 if (rtd->dai_link->playback_only) { 3080 playback = 1; 3081 capture = 0; 3082 } 3083 3084 if (rtd->dai_link->capture_only) { 3085 playback = 0; 3086 capture = 1; 3087 } 3088 3089 /* create the PCM */ 3090 if (rtd->dai_link->no_pcm) { 3091 snprintf(new_name, sizeof(new_name), "(%s)", 3092 rtd->dai_link->stream_name); 3093 3094 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 3095 playback, capture, &pcm); 3096 } else { 3097 if (rtd->dai_link->dynamic) 3098 snprintf(new_name, sizeof(new_name), "%s (*)", 3099 rtd->dai_link->stream_name); 3100 else 3101 snprintf(new_name, sizeof(new_name), "%s %s-%d", 3102 rtd->dai_link->stream_name, 3103 (rtd->num_codecs > 1) ? 3104 "multicodec" : rtd->codec_dai->name, num); 3105 3106 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, 3107 capture, &pcm); 3108 } 3109 if (ret < 0) { 3110 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", 3111 rtd->dai_link->name); 3112 return ret; 3113 } 3114 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); 3115 3116 /* DAPM dai link stream work */ 3117 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 3118 3119 pcm->nonatomic = rtd->dai_link->nonatomic; 3120 rtd->pcm = pcm; 3121 pcm->private_data = rtd; 3122 3123 if (rtd->dai_link->no_pcm) { 3124 if (playback) 3125 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 3126 if (capture) 3127 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 3128 goto out; 3129 } 3130 3131 /* ASoC PCM operations */ 3132 if (rtd->dai_link->dynamic) { 3133 rtd->ops.open = dpcm_fe_dai_open; 3134 rtd->ops.hw_params = dpcm_fe_dai_hw_params; 3135 rtd->ops.prepare = dpcm_fe_dai_prepare; 3136 rtd->ops.trigger = dpcm_fe_dai_trigger; 3137 rtd->ops.hw_free = dpcm_fe_dai_hw_free; 3138 rtd->ops.close = dpcm_fe_dai_close; 3139 rtd->ops.pointer = soc_pcm_pointer; 3140 rtd->ops.ioctl = soc_pcm_ioctl; 3141 } else { 3142 rtd->ops.open = soc_pcm_open; 3143 rtd->ops.hw_params = soc_pcm_hw_params; 3144 rtd->ops.prepare = soc_pcm_prepare; 3145 rtd->ops.trigger = soc_pcm_trigger; 3146 rtd->ops.hw_free = soc_pcm_hw_free; 3147 rtd->ops.close = soc_pcm_close; 3148 rtd->ops.pointer = soc_pcm_pointer; 3149 rtd->ops.ioctl = soc_pcm_ioctl; 3150 } 3151 3152 for_each_rtdcom(rtd, rtdcom) { 3153 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops; 3154 3155 if (!ops) 3156 continue; 3157 3158 if (ops->ack) 3159 rtd->ops.ack = soc_rtdcom_ack; 3160 if (ops->copy_user) 3161 rtd->ops.copy_user = soc_rtdcom_copy_user; 3162 if (ops->copy_kernel) 3163 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel; 3164 if (ops->fill_silence) 3165 rtd->ops.fill_silence = soc_rtdcom_fill_silence; 3166 if (ops->page) 3167 rtd->ops.page = soc_rtdcom_page; 3168 if (ops->mmap) 3169 rtd->ops.mmap = soc_rtdcom_mmap; 3170 } 3171 3172 if (playback) 3173 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); 3174 3175 if (capture) 3176 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); 3177 3178 for_each_rtdcom(rtd, rtdcom) { 3179 component = rtdcom->component; 3180 3181 if (!component->driver->pcm_new) 3182 continue; 3183 3184 ret = component->driver->pcm_new(rtd); 3185 if (ret < 0) { 3186 dev_err(component->dev, 3187 "ASoC: pcm constructor failed: %d\n", 3188 ret); 3189 return ret; 3190 } 3191 } 3192 3193 pcm->private_free = soc_pcm_private_free; 3194 pcm->no_device_suspend = true; 3195 out: 3196 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", 3197 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name, 3198 cpu_dai->name); 3199 return ret; 3200 } 3201 3202 /* is the current PCM operation for this FE ? */ 3203 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) 3204 { 3205 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) 3206 return 1; 3207 return 0; 3208 } 3209 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); 3210 3211 /* is the current PCM operation for this BE ? */ 3212 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, 3213 struct snd_soc_pcm_runtime *be, int stream) 3214 { 3215 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || 3216 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && 3217 be->dpcm[stream].runtime_update)) 3218 return 1; 3219 return 0; 3220 } 3221 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); 3222 3223 /* get the substream for this BE */ 3224 struct snd_pcm_substream * 3225 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) 3226 { 3227 return be->pcm->streams[stream].substream; 3228 } 3229 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); 3230 3231 /* get the BE runtime state */ 3232 enum snd_soc_dpcm_state 3233 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream) 3234 { 3235 return be->dpcm[stream].state; 3236 } 3237 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state); 3238 3239 /* set the BE runtime state */ 3240 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be, 3241 int stream, enum snd_soc_dpcm_state state) 3242 { 3243 be->dpcm[stream].state = state; 3244 } 3245 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state); 3246 3247 /* 3248 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE 3249 * are not running, paused or suspended for the specified stream direction. 3250 */ 3251 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, 3252 struct snd_soc_pcm_runtime *be, int stream) 3253 { 3254 struct snd_soc_dpcm *dpcm; 3255 int state; 3256 int ret = 1; 3257 unsigned long flags; 3258 3259 spin_lock_irqsave(&fe->card->dpcm_lock, flags); 3260 for_each_dpcm_fe(be, stream, dpcm) { 3261 3262 if (dpcm->fe == fe) 3263 continue; 3264 3265 state = dpcm->fe->dpcm[stream].state; 3266 if (state == SND_SOC_DPCM_STATE_START || 3267 state == SND_SOC_DPCM_STATE_PAUSED || 3268 state == SND_SOC_DPCM_STATE_SUSPEND) { 3269 ret = 0; 3270 break; 3271 } 3272 } 3273 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 3274 3275 /* it's safe to free/stop this BE DAI */ 3276 return ret; 3277 } 3278 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); 3279 3280 /* 3281 * We can only change hw params a BE DAI if any of it's FE are not prepared, 3282 * running, paused or suspended for the specified stream direction. 3283 */ 3284 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, 3285 struct snd_soc_pcm_runtime *be, int stream) 3286 { 3287 struct snd_soc_dpcm *dpcm; 3288 int state; 3289 int ret = 1; 3290 unsigned long flags; 3291 3292 spin_lock_irqsave(&fe->card->dpcm_lock, flags); 3293 for_each_dpcm_fe(be, stream, dpcm) { 3294 3295 if (dpcm->fe == fe) 3296 continue; 3297 3298 state = dpcm->fe->dpcm[stream].state; 3299 if (state == SND_SOC_DPCM_STATE_START || 3300 state == SND_SOC_DPCM_STATE_PAUSED || 3301 state == SND_SOC_DPCM_STATE_SUSPEND || 3302 state == SND_SOC_DPCM_STATE_PREPARE) { 3303 ret = 0; 3304 break; 3305 } 3306 } 3307 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 3308 3309 /* it's safe to change hw_params */ 3310 return ret; 3311 } 3312 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); 3313 3314 #ifdef CONFIG_DEBUG_FS 3315 static const char *dpcm_state_string(enum snd_soc_dpcm_state state) 3316 { 3317 switch (state) { 3318 case SND_SOC_DPCM_STATE_NEW: 3319 return "new"; 3320 case SND_SOC_DPCM_STATE_OPEN: 3321 return "open"; 3322 case SND_SOC_DPCM_STATE_HW_PARAMS: 3323 return "hw_params"; 3324 case SND_SOC_DPCM_STATE_PREPARE: 3325 return "prepare"; 3326 case SND_SOC_DPCM_STATE_START: 3327 return "start"; 3328 case SND_SOC_DPCM_STATE_STOP: 3329 return "stop"; 3330 case SND_SOC_DPCM_STATE_SUSPEND: 3331 return "suspend"; 3332 case SND_SOC_DPCM_STATE_PAUSED: 3333 return "paused"; 3334 case SND_SOC_DPCM_STATE_HW_FREE: 3335 return "hw_free"; 3336 case SND_SOC_DPCM_STATE_CLOSE: 3337 return "close"; 3338 } 3339 3340 return "unknown"; 3341 } 3342 3343 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, 3344 int stream, char *buf, size_t size) 3345 { 3346 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; 3347 struct snd_soc_dpcm *dpcm; 3348 ssize_t offset = 0; 3349 unsigned long flags; 3350 3351 /* FE state */ 3352 offset += snprintf(buf + offset, size - offset, 3353 "[%s - %s]\n", fe->dai_link->name, 3354 stream ? "Capture" : "Playback"); 3355 3356 offset += snprintf(buf + offset, size - offset, "State: %s\n", 3357 dpcm_state_string(fe->dpcm[stream].state)); 3358 3359 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 3360 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 3361 offset += snprintf(buf + offset, size - offset, 3362 "Hardware Params: " 3363 "Format = %s, Channels = %d, Rate = %d\n", 3364 snd_pcm_format_name(params_format(params)), 3365 params_channels(params), 3366 params_rate(params)); 3367 3368 /* BEs state */ 3369 offset += snprintf(buf + offset, size - offset, "Backends:\n"); 3370 3371 if (list_empty(&fe->dpcm[stream].be_clients)) { 3372 offset += snprintf(buf + offset, size - offset, 3373 " No active DSP links\n"); 3374 goto out; 3375 } 3376 3377 spin_lock_irqsave(&fe->card->dpcm_lock, flags); 3378 for_each_dpcm_be(fe, stream, dpcm) { 3379 struct snd_soc_pcm_runtime *be = dpcm->be; 3380 params = &dpcm->hw_params; 3381 3382 offset += snprintf(buf + offset, size - offset, 3383 "- %s\n", be->dai_link->name); 3384 3385 offset += snprintf(buf + offset, size - offset, 3386 " State: %s\n", 3387 dpcm_state_string(be->dpcm[stream].state)); 3388 3389 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 3390 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 3391 offset += snprintf(buf + offset, size - offset, 3392 " Hardware Params: " 3393 "Format = %s, Channels = %d, Rate = %d\n", 3394 snd_pcm_format_name(params_format(params)), 3395 params_channels(params), 3396 params_rate(params)); 3397 } 3398 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 3399 out: 3400 return offset; 3401 } 3402 3403 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, 3404 size_t count, loff_t *ppos) 3405 { 3406 struct snd_soc_pcm_runtime *fe = file->private_data; 3407 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; 3408 char *buf; 3409 3410 buf = kmalloc(out_count, GFP_KERNEL); 3411 if (!buf) 3412 return -ENOMEM; 3413 3414 if (fe->cpu_dai->driver->playback.channels_min) 3415 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK, 3416 buf + offset, out_count - offset); 3417 3418 if (fe->cpu_dai->driver->capture.channels_min) 3419 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE, 3420 buf + offset, out_count - offset); 3421 3422 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); 3423 3424 kfree(buf); 3425 return ret; 3426 } 3427 3428 static const struct file_operations dpcm_state_fops = { 3429 .open = simple_open, 3430 .read = dpcm_state_read_file, 3431 .llseek = default_llseek, 3432 }; 3433 3434 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) 3435 { 3436 if (!rtd->dai_link) 3437 return; 3438 3439 if (!rtd->card->debugfs_card_root) 3440 return; 3441 3442 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, 3443 rtd->card->debugfs_card_root); 3444 if (!rtd->debugfs_dpcm_root) { 3445 dev_dbg(rtd->dev, 3446 "ASoC: Failed to create dpcm debugfs directory %s\n", 3447 rtd->dai_link->name); 3448 return; 3449 } 3450 3451 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, 3452 rtd, &dpcm_state_fops); 3453 } 3454 #endif 3455