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