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