1 /* 2 * soc-pcm.c -- ALSA SoC PCM 3 * 4 * Copyright 2005 Wolfson Microelectronics PLC. 5 * Copyright 2005 Openedhand Ltd. 6 * Copyright (C) 2010 Slimlogic Ltd. 7 * Copyright (C) 2010 Texas Instruments Inc. 8 * 9 * Authors: Liam Girdwood <lrg@ti.com> 10 * Mark Brown <broonie@opensource.wolfsonmicro.com> 11 * 12 * This program is free software; you can redistribute it and/or modify it 13 * under the terms of the GNU General Public License as published by the 14 * Free Software Foundation; either version 2 of the License, or (at your 15 * option) any later version. 16 * 17 */ 18 19 #include <linux/kernel.h> 20 #include <linux/init.h> 21 #include <linux/delay.h> 22 #include <linux/pinctrl/consumer.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/slab.h> 25 #include <linux/workqueue.h> 26 #include <linux/export.h> 27 #include <linux/debugfs.h> 28 #include <sound/core.h> 29 #include <sound/pcm.h> 30 #include <sound/pcm_params.h> 31 #include <sound/soc.h> 32 #include <sound/soc-dpcm.h> 33 #include <sound/initval.h> 34 35 #define DPCM_MAX_BE_USERS 8 36 37 /** 38 * snd_soc_runtime_activate() - Increment active count for PCM runtime components 39 * @rtd: ASoC PCM runtime that is activated 40 * @stream: Direction of the PCM stream 41 * 42 * Increments the active count for all the DAIs and components attached to a PCM 43 * runtime. Should typically be called when a stream is opened. 44 * 45 * Must be called with the rtd->pcm_mutex being held 46 */ 47 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream) 48 { 49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 50 struct snd_soc_dai *codec_dai = rtd->codec_dai; 51 52 lockdep_assert_held(&rtd->pcm_mutex); 53 54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 55 cpu_dai->playback_active++; 56 codec_dai->playback_active++; 57 } else { 58 cpu_dai->capture_active++; 59 codec_dai->capture_active++; 60 } 61 62 cpu_dai->active++; 63 codec_dai->active++; 64 cpu_dai->component->active++; 65 codec_dai->component->active++; 66 } 67 68 /** 69 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components 70 * @rtd: ASoC PCM runtime that is deactivated 71 * @stream: Direction of the PCM stream 72 * 73 * Decrements the active count for all the DAIs and components attached to a PCM 74 * runtime. Should typically be called when a stream is closed. 75 * 76 * Must be called with the rtd->pcm_mutex being held 77 */ 78 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream) 79 { 80 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 81 struct snd_soc_dai *codec_dai = rtd->codec_dai; 82 83 lockdep_assert_held(&rtd->pcm_mutex); 84 85 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 86 cpu_dai->playback_active--; 87 codec_dai->playback_active--; 88 } else { 89 cpu_dai->capture_active--; 90 codec_dai->capture_active--; 91 } 92 93 cpu_dai->active--; 94 codec_dai->active--; 95 cpu_dai->component->active--; 96 codec_dai->component->active--; 97 } 98 99 /** 100 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay 101 * @rtd: The ASoC PCM runtime that should be checked. 102 * 103 * This function checks whether the power down delay should be ignored for a 104 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has 105 * been configured to ignore the delay, or if none of the components benefits 106 * from having the delay. 107 */ 108 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) 109 { 110 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) 111 return true; 112 113 return rtd->cpu_dai->component->ignore_pmdown_time && 114 rtd->codec_dai->component->ignore_pmdown_time; 115 } 116 117 /** 118 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 119 * @substream: the pcm substream 120 * @hw: the hardware parameters 121 * 122 * Sets the substream runtime hardware parameters. 123 */ 124 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 125 const struct snd_pcm_hardware *hw) 126 { 127 struct snd_pcm_runtime *runtime = substream->runtime; 128 runtime->hw.info = hw->info; 129 runtime->hw.formats = hw->formats; 130 runtime->hw.period_bytes_min = hw->period_bytes_min; 131 runtime->hw.period_bytes_max = hw->period_bytes_max; 132 runtime->hw.periods_min = hw->periods_min; 133 runtime->hw.periods_max = hw->periods_max; 134 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 135 runtime->hw.fifo_size = hw->fifo_size; 136 return 0; 137 } 138 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 139 140 /* DPCM stream event, send event to FE and all active BEs. */ 141 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, 142 int event) 143 { 144 struct snd_soc_dpcm *dpcm; 145 146 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) { 147 148 struct snd_soc_pcm_runtime *be = dpcm->be; 149 150 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", 151 be->dai_link->name, event, dir); 152 153 snd_soc_dapm_stream_event(be, dir, event); 154 } 155 156 snd_soc_dapm_stream_event(fe, dir, event); 157 158 return 0; 159 } 160 161 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, 162 struct snd_soc_dai *soc_dai) 163 { 164 struct snd_soc_pcm_runtime *rtd = substream->private_data; 165 int ret; 166 167 if (soc_dai->rate && (soc_dai->driver->symmetric_rates || 168 rtd->dai_link->symmetric_rates)) { 169 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", 170 soc_dai->rate); 171 172 ret = snd_pcm_hw_constraint_minmax(substream->runtime, 173 SNDRV_PCM_HW_PARAM_RATE, 174 soc_dai->rate, soc_dai->rate); 175 if (ret < 0) { 176 dev_err(soc_dai->dev, 177 "ASoC: Unable to apply rate constraint: %d\n", 178 ret); 179 return ret; 180 } 181 } 182 183 if (soc_dai->channels && (soc_dai->driver->symmetric_channels || 184 rtd->dai_link->symmetric_channels)) { 185 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", 186 soc_dai->channels); 187 188 ret = snd_pcm_hw_constraint_minmax(substream->runtime, 189 SNDRV_PCM_HW_PARAM_CHANNELS, 190 soc_dai->channels, 191 soc_dai->channels); 192 if (ret < 0) { 193 dev_err(soc_dai->dev, 194 "ASoC: Unable to apply channel symmetry constraint: %d\n", 195 ret); 196 return ret; 197 } 198 } 199 200 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || 201 rtd->dai_link->symmetric_samplebits)) { 202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", 203 soc_dai->sample_bits); 204 205 ret = snd_pcm_hw_constraint_minmax(substream->runtime, 206 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 207 soc_dai->sample_bits, 208 soc_dai->sample_bits); 209 if (ret < 0) { 210 dev_err(soc_dai->dev, 211 "ASoC: Unable to apply sample bits symmetry constraint: %d\n", 212 ret); 213 return ret; 214 } 215 } 216 217 return 0; 218 } 219 220 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, 221 struct snd_pcm_hw_params *params) 222 { 223 struct snd_soc_pcm_runtime *rtd = substream->private_data; 224 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 225 struct snd_soc_dai *codec_dai = rtd->codec_dai; 226 unsigned int rate, channels, sample_bits, symmetry; 227 228 rate = params_rate(params); 229 channels = params_channels(params); 230 sample_bits = snd_pcm_format_physical_width(params_format(params)); 231 232 /* reject unmatched parameters when applying symmetry */ 233 symmetry = cpu_dai->driver->symmetric_rates || 234 codec_dai->driver->symmetric_rates || 235 rtd->dai_link->symmetric_rates; 236 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) { 237 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", 238 cpu_dai->rate, rate); 239 return -EINVAL; 240 } 241 242 symmetry = cpu_dai->driver->symmetric_channels || 243 codec_dai->driver->symmetric_channels || 244 rtd->dai_link->symmetric_channels; 245 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) { 246 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", 247 cpu_dai->channels, channels); 248 return -EINVAL; 249 } 250 251 symmetry = cpu_dai->driver->symmetric_samplebits || 252 codec_dai->driver->symmetric_samplebits || 253 rtd->dai_link->symmetric_samplebits; 254 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) { 255 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", 256 cpu_dai->sample_bits, sample_bits); 257 return -EINVAL; 258 } 259 260 return 0; 261 } 262 263 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) 264 { 265 struct snd_soc_pcm_runtime *rtd = substream->private_data; 266 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver; 267 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver; 268 struct snd_soc_dai_link *link = rtd->dai_link; 269 270 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates || 271 link->symmetric_rates || cpu_driver->symmetric_channels || 272 codec_driver->symmetric_channels || link->symmetric_channels || 273 cpu_driver->symmetric_samplebits || 274 codec_driver->symmetric_samplebits || 275 link->symmetric_samplebits; 276 } 277 278 /* 279 * List of sample sizes that might go over the bus for parameter 280 * application. There ought to be a wildcard sample size for things 281 * like the DAC/ADC resolution to use but there isn't right now. 282 */ 283 static int sample_sizes[] = { 284 24, 32, 285 }; 286 287 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream, 288 struct snd_soc_dai *dai) 289 { 290 int ret, i, bits; 291 292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 293 bits = dai->driver->playback.sig_bits; 294 else 295 bits = dai->driver->capture.sig_bits; 296 297 if (!bits) 298 return; 299 300 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) { 301 if (bits >= sample_sizes[i]) 302 continue; 303 304 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 305 sample_sizes[i], bits); 306 if (ret != 0) 307 dev_warn(dai->dev, 308 "ASoC: Failed to set MSB %d/%d: %d\n", 309 bits, sample_sizes[i], ret); 310 } 311 } 312 313 static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime, 314 struct snd_soc_pcm_stream *codec_stream, 315 struct snd_soc_pcm_stream *cpu_stream) 316 { 317 struct snd_pcm_hardware *hw = &runtime->hw; 318 319 hw->channels_min = max(codec_stream->channels_min, 320 cpu_stream->channels_min); 321 hw->channels_max = min(codec_stream->channels_max, 322 cpu_stream->channels_max); 323 if (hw->formats) 324 hw->formats &= codec_stream->formats & cpu_stream->formats; 325 else 326 hw->formats = codec_stream->formats & cpu_stream->formats; 327 hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates, 328 cpu_stream->rates); 329 330 hw->rate_min = 0; 331 hw->rate_max = UINT_MAX; 332 333 snd_pcm_limit_hw_rates(runtime); 334 335 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min); 336 hw->rate_min = max(hw->rate_min, codec_stream->rate_min); 337 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max); 338 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max); 339 } 340 341 /* 342 * Called by ALSA when a PCM substream is opened, the runtime->hw record is 343 * then initialized and any private data can be allocated. This also calls 344 * startup for the cpu DAI, platform, machine and codec DAI. 345 */ 346 static int soc_pcm_open(struct snd_pcm_substream *substream) 347 { 348 struct snd_soc_pcm_runtime *rtd = substream->private_data; 349 struct snd_pcm_runtime *runtime = substream->runtime; 350 struct snd_soc_platform *platform = rtd->platform; 351 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 352 struct snd_soc_dai *codec_dai = rtd->codec_dai; 353 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 354 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; 355 int ret = 0; 356 357 pinctrl_pm_select_default_state(cpu_dai->dev); 358 pinctrl_pm_select_default_state(codec_dai->dev); 359 pm_runtime_get_sync(cpu_dai->dev); 360 pm_runtime_get_sync(codec_dai->dev); 361 pm_runtime_get_sync(platform->dev); 362 363 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 364 365 /* startup the audio subsystem */ 366 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) { 367 ret = cpu_dai->driver->ops->startup(substream, cpu_dai); 368 if (ret < 0) { 369 dev_err(cpu_dai->dev, "ASoC: can't open interface" 370 " %s: %d\n", cpu_dai->name, ret); 371 goto out; 372 } 373 } 374 375 if (platform->driver->ops && platform->driver->ops->open) { 376 ret = platform->driver->ops->open(substream); 377 if (ret < 0) { 378 dev_err(platform->dev, "ASoC: can't open platform" 379 " %s: %d\n", platform->name, ret); 380 goto platform_err; 381 } 382 } 383 384 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) { 385 ret = codec_dai->driver->ops->startup(substream, codec_dai); 386 if (ret < 0) { 387 dev_err(codec_dai->dev, "ASoC: can't open codec" 388 " %s: %d\n", codec_dai->name, ret); 389 goto codec_dai_err; 390 } 391 } 392 393 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { 394 ret = rtd->dai_link->ops->startup(substream); 395 if (ret < 0) { 396 pr_err("ASoC: %s startup failed: %d\n", 397 rtd->dai_link->name, ret); 398 goto machine_err; 399 } 400 } 401 402 /* Dynamic PCM DAI links compat checks use dynamic capabilities */ 403 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) 404 goto dynamic; 405 406 /* Check that the codec and cpu DAIs are compatible */ 407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 408 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback, 409 &cpu_dai_drv->playback); 410 } else { 411 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture, 412 &cpu_dai_drv->capture); 413 } 414 415 if (soc_pcm_has_symmetry(substream)) 416 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 417 418 ret = -EINVAL; 419 if (!runtime->hw.rates) { 420 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", 421 codec_dai->name, cpu_dai->name); 422 goto config_err; 423 } 424 if (!runtime->hw.formats) { 425 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", 426 codec_dai->name, cpu_dai->name); 427 goto config_err; 428 } 429 if (!runtime->hw.channels_min || !runtime->hw.channels_max || 430 runtime->hw.channels_min > runtime->hw.channels_max) { 431 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", 432 codec_dai->name, cpu_dai->name); 433 goto config_err; 434 } 435 436 soc_pcm_apply_msb(substream, codec_dai); 437 soc_pcm_apply_msb(substream, cpu_dai); 438 439 /* Symmetry only applies if we've already got an active stream. */ 440 if (cpu_dai->active) { 441 ret = soc_pcm_apply_symmetry(substream, cpu_dai); 442 if (ret != 0) 443 goto config_err; 444 } 445 446 if (codec_dai->active) { 447 ret = soc_pcm_apply_symmetry(substream, codec_dai); 448 if (ret != 0) 449 goto config_err; 450 } 451 452 pr_debug("ASoC: %s <-> %s info:\n", 453 codec_dai->name, cpu_dai->name); 454 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); 455 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, 456 runtime->hw.channels_max); 457 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, 458 runtime->hw.rate_max); 459 460 dynamic: 461 462 snd_soc_runtime_activate(rtd, substream->stream); 463 464 mutex_unlock(&rtd->pcm_mutex); 465 return 0; 466 467 config_err: 468 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 469 rtd->dai_link->ops->shutdown(substream); 470 471 machine_err: 472 if (codec_dai->driver->ops->shutdown) 473 codec_dai->driver->ops->shutdown(substream, codec_dai); 474 475 codec_dai_err: 476 if (platform->driver->ops && platform->driver->ops->close) 477 platform->driver->ops->close(substream); 478 479 platform_err: 480 if (cpu_dai->driver->ops->shutdown) 481 cpu_dai->driver->ops->shutdown(substream, cpu_dai); 482 out: 483 mutex_unlock(&rtd->pcm_mutex); 484 485 pm_runtime_put(platform->dev); 486 pm_runtime_put(codec_dai->dev); 487 pm_runtime_put(cpu_dai->dev); 488 if (!codec_dai->active) 489 pinctrl_pm_select_sleep_state(codec_dai->dev); 490 if (!cpu_dai->active) 491 pinctrl_pm_select_sleep_state(cpu_dai->dev); 492 493 return ret; 494 } 495 496 /* 497 * Power down the audio subsystem pmdown_time msecs after close is called. 498 * This is to ensure there are no pops or clicks in between any music tracks 499 * due to DAPM power cycling. 500 */ 501 static void close_delayed_work(struct work_struct *work) 502 { 503 struct snd_soc_pcm_runtime *rtd = 504 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); 505 struct snd_soc_dai *codec_dai = rtd->codec_dai; 506 507 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 508 509 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n", 510 codec_dai->driver->playback.stream_name, 511 codec_dai->playback_active ? "active" : "inactive", 512 rtd->pop_wait ? "yes" : "no"); 513 514 /* are we waiting on this codec DAI stream */ 515 if (rtd->pop_wait == 1) { 516 rtd->pop_wait = 0; 517 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, 518 SND_SOC_DAPM_STREAM_STOP); 519 } 520 521 mutex_unlock(&rtd->pcm_mutex); 522 } 523 524 /* 525 * Called by ALSA when a PCM substream is closed. Private data can be 526 * freed here. The cpu DAI, codec DAI, machine and platform are also 527 * shutdown. 528 */ 529 static int soc_pcm_close(struct snd_pcm_substream *substream) 530 { 531 struct snd_soc_pcm_runtime *rtd = substream->private_data; 532 struct snd_soc_platform *platform = rtd->platform; 533 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 534 struct snd_soc_dai *codec_dai = rtd->codec_dai; 535 536 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 537 538 snd_soc_runtime_deactivate(rtd, substream->stream); 539 540 /* clear the corresponding DAIs rate when inactive */ 541 if (!cpu_dai->active) 542 cpu_dai->rate = 0; 543 544 if (!codec_dai->active) 545 codec_dai->rate = 0; 546 547 if (cpu_dai->driver->ops->shutdown) 548 cpu_dai->driver->ops->shutdown(substream, cpu_dai); 549 550 if (codec_dai->driver->ops->shutdown) 551 codec_dai->driver->ops->shutdown(substream, codec_dai); 552 553 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) 554 rtd->dai_link->ops->shutdown(substream); 555 556 if (platform->driver->ops && platform->driver->ops->close) 557 platform->driver->ops->close(substream); 558 cpu_dai->runtime = NULL; 559 560 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 561 if (snd_soc_runtime_ignore_pmdown_time(rtd)) { 562 /* powered down playback stream now */ 563 snd_soc_dapm_stream_event(rtd, 564 SNDRV_PCM_STREAM_PLAYBACK, 565 SND_SOC_DAPM_STREAM_STOP); 566 } else { 567 /* start delayed pop wq here for playback streams */ 568 rtd->pop_wait = 1; 569 queue_delayed_work(system_power_efficient_wq, 570 &rtd->delayed_work, 571 msecs_to_jiffies(rtd->pmdown_time)); 572 } 573 } else { 574 /* capture streams can be powered down now */ 575 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, 576 SND_SOC_DAPM_STREAM_STOP); 577 } 578 579 mutex_unlock(&rtd->pcm_mutex); 580 581 pm_runtime_put(platform->dev); 582 pm_runtime_put(codec_dai->dev); 583 pm_runtime_put(cpu_dai->dev); 584 if (!codec_dai->active) 585 pinctrl_pm_select_sleep_state(codec_dai->dev); 586 if (!cpu_dai->active) 587 pinctrl_pm_select_sleep_state(cpu_dai->dev); 588 589 return 0; 590 } 591 592 /* 593 * Called by ALSA when the PCM substream is prepared, can set format, sample 594 * rate, etc. This function is non atomic and can be called multiple times, 595 * it can refer to the runtime info. 596 */ 597 static int soc_pcm_prepare(struct snd_pcm_substream *substream) 598 { 599 struct snd_soc_pcm_runtime *rtd = substream->private_data; 600 struct snd_soc_platform *platform = rtd->platform; 601 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 602 struct snd_soc_dai *codec_dai = rtd->codec_dai; 603 int ret = 0; 604 605 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 606 607 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { 608 ret = rtd->dai_link->ops->prepare(substream); 609 if (ret < 0) { 610 dev_err(rtd->card->dev, "ASoC: machine prepare error:" 611 " %d\n", ret); 612 goto out; 613 } 614 } 615 616 if (platform->driver->ops && platform->driver->ops->prepare) { 617 ret = platform->driver->ops->prepare(substream); 618 if (ret < 0) { 619 dev_err(platform->dev, "ASoC: platform prepare error:" 620 " %d\n", ret); 621 goto out; 622 } 623 } 624 625 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) { 626 ret = codec_dai->driver->ops->prepare(substream, codec_dai); 627 if (ret < 0) { 628 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n", 629 ret); 630 goto out; 631 } 632 } 633 634 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) { 635 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); 636 if (ret < 0) { 637 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n", 638 ret); 639 goto out; 640 } 641 } 642 643 /* cancel any delayed stream shutdown that is pending */ 644 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 645 rtd->pop_wait) { 646 rtd->pop_wait = 0; 647 cancel_delayed_work(&rtd->delayed_work); 648 } 649 650 snd_soc_dapm_stream_event(rtd, substream->stream, 651 SND_SOC_DAPM_STREAM_START); 652 653 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream); 654 655 out: 656 mutex_unlock(&rtd->pcm_mutex); 657 return ret; 658 } 659 660 /* 661 * Called by ALSA when the hardware params are set by application. This 662 * function can also be called multiple times and can allocate buffers 663 * (using snd_pcm_lib_* ). It's non-atomic. 664 */ 665 static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 666 struct snd_pcm_hw_params *params) 667 { 668 struct snd_soc_pcm_runtime *rtd = substream->private_data; 669 struct snd_soc_platform *platform = rtd->platform; 670 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 671 struct snd_soc_dai *codec_dai = rtd->codec_dai; 672 int ret = 0; 673 674 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 675 676 ret = soc_pcm_params_symmetry(substream, params); 677 if (ret) 678 goto out; 679 680 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { 681 ret = rtd->dai_link->ops->hw_params(substream, params); 682 if (ret < 0) { 683 dev_err(rtd->card->dev, "ASoC: machine hw_params" 684 " failed: %d\n", ret); 685 goto out; 686 } 687 } 688 689 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) { 690 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); 691 if (ret < 0) { 692 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:" 693 " %d\n", codec_dai->name, ret); 694 goto codec_err; 695 } 696 } 697 698 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) { 699 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); 700 if (ret < 0) { 701 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n", 702 cpu_dai->name, ret); 703 goto interface_err; 704 } 705 } 706 707 if (platform->driver->ops && platform->driver->ops->hw_params) { 708 ret = platform->driver->ops->hw_params(substream, params); 709 if (ret < 0) { 710 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n", 711 platform->name, ret); 712 goto platform_err; 713 } 714 } 715 716 /* store the parameters for each DAIs */ 717 cpu_dai->rate = params_rate(params); 718 cpu_dai->channels = params_channels(params); 719 cpu_dai->sample_bits = 720 snd_pcm_format_physical_width(params_format(params)); 721 722 codec_dai->rate = params_rate(params); 723 codec_dai->channels = params_channels(params); 724 codec_dai->sample_bits = 725 snd_pcm_format_physical_width(params_format(params)); 726 727 out: 728 mutex_unlock(&rtd->pcm_mutex); 729 return ret; 730 731 platform_err: 732 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free) 733 cpu_dai->driver->ops->hw_free(substream, cpu_dai); 734 735 interface_err: 736 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free) 737 codec_dai->driver->ops->hw_free(substream, codec_dai); 738 739 codec_err: 740 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 741 rtd->dai_link->ops->hw_free(substream); 742 743 mutex_unlock(&rtd->pcm_mutex); 744 return ret; 745 } 746 747 /* 748 * Frees resources allocated by hw_params, can be called multiple times 749 */ 750 static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 751 { 752 struct snd_soc_pcm_runtime *rtd = substream->private_data; 753 struct snd_soc_platform *platform = rtd->platform; 754 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 755 struct snd_soc_dai *codec_dai = rtd->codec_dai; 756 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 757 758 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); 759 760 /* clear the corresponding DAIs parameters when going to be inactive */ 761 if (cpu_dai->active == 1) { 762 cpu_dai->rate = 0; 763 cpu_dai->channels = 0; 764 cpu_dai->sample_bits = 0; 765 } 766 767 if (codec_dai->active == 1) { 768 codec_dai->rate = 0; 769 codec_dai->channels = 0; 770 codec_dai->sample_bits = 0; 771 } 772 773 /* apply codec digital mute */ 774 if ((playback && codec_dai->playback_active == 1) || 775 (!playback && codec_dai->capture_active == 1)) 776 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream); 777 778 /* free any machine hw params */ 779 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) 780 rtd->dai_link->ops->hw_free(substream); 781 782 /* free any DMA resources */ 783 if (platform->driver->ops && platform->driver->ops->hw_free) 784 platform->driver->ops->hw_free(substream); 785 786 /* now free hw params for the DAIs */ 787 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free) 788 codec_dai->driver->ops->hw_free(substream, codec_dai); 789 790 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free) 791 cpu_dai->driver->ops->hw_free(substream, cpu_dai); 792 793 mutex_unlock(&rtd->pcm_mutex); 794 return 0; 795 } 796 797 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 798 { 799 struct snd_soc_pcm_runtime *rtd = substream->private_data; 800 struct snd_soc_platform *platform = rtd->platform; 801 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 802 struct snd_soc_dai *codec_dai = rtd->codec_dai; 803 int ret; 804 805 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) { 806 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); 807 if (ret < 0) 808 return ret; 809 } 810 811 if (platform->driver->ops && platform->driver->ops->trigger) { 812 ret = platform->driver->ops->trigger(substream, cmd); 813 if (ret < 0) 814 return ret; 815 } 816 817 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) { 818 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); 819 if (ret < 0) 820 return ret; 821 } 822 return 0; 823 } 824 825 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, 826 int cmd) 827 { 828 struct snd_soc_pcm_runtime *rtd = substream->private_data; 829 struct snd_soc_platform *platform = rtd->platform; 830 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 831 struct snd_soc_dai *codec_dai = rtd->codec_dai; 832 int ret; 833 834 if (codec_dai->driver->ops && 835 codec_dai->driver->ops->bespoke_trigger) { 836 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai); 837 if (ret < 0) 838 return ret; 839 } 840 841 if (platform->driver->bespoke_trigger) { 842 ret = platform->driver->bespoke_trigger(substream, cmd); 843 if (ret < 0) 844 return ret; 845 } 846 847 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) { 848 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai); 849 if (ret < 0) 850 return ret; 851 } 852 return 0; 853 } 854 /* 855 * soc level wrapper for pointer callback 856 * If cpu_dai, codec_dai, platform driver has the delay callback, than 857 * the runtime->delay will be updated accordingly. 858 */ 859 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 860 { 861 struct snd_soc_pcm_runtime *rtd = substream->private_data; 862 struct snd_soc_platform *platform = rtd->platform; 863 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 864 struct snd_soc_dai *codec_dai = rtd->codec_dai; 865 struct snd_pcm_runtime *runtime = substream->runtime; 866 snd_pcm_uframes_t offset = 0; 867 snd_pcm_sframes_t delay = 0; 868 869 if (platform->driver->ops && platform->driver->ops->pointer) 870 offset = platform->driver->ops->pointer(substream); 871 872 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay) 873 delay += cpu_dai->driver->ops->delay(substream, cpu_dai); 874 875 if (codec_dai->driver->ops && codec_dai->driver->ops->delay) 876 delay += codec_dai->driver->ops->delay(substream, codec_dai); 877 878 if (platform->driver->delay) 879 delay += platform->driver->delay(substream, codec_dai); 880 881 runtime->delay = delay; 882 883 return offset; 884 } 885 886 /* connect a FE and BE */ 887 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, 888 struct snd_soc_pcm_runtime *be, int stream) 889 { 890 struct snd_soc_dpcm *dpcm; 891 892 /* only add new dpcms */ 893 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 894 if (dpcm->be == be && dpcm->fe == fe) 895 return 0; 896 } 897 898 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); 899 if (!dpcm) 900 return -ENOMEM; 901 902 dpcm->be = be; 903 dpcm->fe = fe; 904 be->dpcm[stream].runtime = fe->dpcm[stream].runtime; 905 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; 906 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); 907 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); 908 909 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", 910 stream ? "capture" : "playback", fe->dai_link->name, 911 stream ? "<-" : "->", be->dai_link->name); 912 913 #ifdef CONFIG_DEBUG_FS 914 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644, 915 fe->debugfs_dpcm_root, &dpcm->state); 916 #endif 917 return 1; 918 } 919 920 /* reparent a BE onto another FE */ 921 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, 922 struct snd_soc_pcm_runtime *be, int stream) 923 { 924 struct snd_soc_dpcm *dpcm; 925 struct snd_pcm_substream *fe_substream, *be_substream; 926 927 /* reparent if BE is connected to other FEs */ 928 if (!be->dpcm[stream].users) 929 return; 930 931 be_substream = snd_soc_dpcm_get_substream(be, stream); 932 933 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { 934 if (dpcm->fe == fe) 935 continue; 936 937 dev_dbg(fe->dev, "reparent %s path %s %s %s\n", 938 stream ? "capture" : "playback", 939 dpcm->fe->dai_link->name, 940 stream ? "<-" : "->", dpcm->be->dai_link->name); 941 942 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); 943 be_substream->runtime = fe_substream->runtime; 944 break; 945 } 946 } 947 948 /* disconnect a BE and FE */ 949 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) 950 { 951 struct snd_soc_dpcm *dpcm, *d; 952 953 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) { 954 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", 955 stream ? "capture" : "playback", 956 dpcm->be->dai_link->name); 957 958 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) 959 continue; 960 961 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", 962 stream ? "capture" : "playback", fe->dai_link->name, 963 stream ? "<-" : "->", dpcm->be->dai_link->name); 964 965 /* BEs still alive need new FE */ 966 dpcm_be_reparent(fe, dpcm->be, stream); 967 968 #ifdef CONFIG_DEBUG_FS 969 debugfs_remove(dpcm->debugfs_state); 970 #endif 971 list_del(&dpcm->list_be); 972 list_del(&dpcm->list_fe); 973 kfree(dpcm); 974 } 975 } 976 977 /* get BE for DAI widget and stream */ 978 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, 979 struct snd_soc_dapm_widget *widget, int stream) 980 { 981 struct snd_soc_pcm_runtime *be; 982 int i; 983 984 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 985 for (i = 0; i < card->num_links; i++) { 986 be = &card->rtd[i]; 987 988 if (!be->dai_link->no_pcm) 989 continue; 990 991 if (be->cpu_dai->playback_widget == widget || 992 be->codec_dai->playback_widget == widget) 993 return be; 994 } 995 } else { 996 997 for (i = 0; i < card->num_links; i++) { 998 be = &card->rtd[i]; 999 1000 if (!be->dai_link->no_pcm) 1001 continue; 1002 1003 if (be->cpu_dai->capture_widget == widget || 1004 be->codec_dai->capture_widget == widget) 1005 return be; 1006 } 1007 } 1008 1009 dev_err(card->dev, "ASoC: can't get %s BE for %s\n", 1010 stream ? "capture" : "playback", widget->name); 1011 return NULL; 1012 } 1013 1014 static inline struct snd_soc_dapm_widget * 1015 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream) 1016 { 1017 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1018 return rtd->cpu_dai->playback_widget; 1019 else 1020 return rtd->cpu_dai->capture_widget; 1021 } 1022 1023 static inline struct snd_soc_dapm_widget * 1024 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream) 1025 { 1026 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1027 return rtd->codec_dai->playback_widget; 1028 else 1029 return rtd->codec_dai->capture_widget; 1030 } 1031 1032 static int widget_in_list(struct snd_soc_dapm_widget_list *list, 1033 struct snd_soc_dapm_widget *widget) 1034 { 1035 int i; 1036 1037 for (i = 0; i < list->num_widgets; i++) { 1038 if (widget == list->widgets[i]) 1039 return 1; 1040 } 1041 1042 return 0; 1043 } 1044 1045 int dpcm_path_get(struct snd_soc_pcm_runtime *fe, 1046 int stream, struct snd_soc_dapm_widget_list **list_) 1047 { 1048 struct snd_soc_dai *cpu_dai = fe->cpu_dai; 1049 struct snd_soc_dapm_widget_list *list; 1050 int paths; 1051 1052 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) + 1053 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL); 1054 if (list == NULL) 1055 return -ENOMEM; 1056 1057 /* get number of valid DAI paths and their widgets */ 1058 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list); 1059 1060 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, 1061 stream ? "capture" : "playback"); 1062 1063 *list_ = list; 1064 return paths; 1065 } 1066 1067 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, 1068 struct snd_soc_dapm_widget_list **list_) 1069 { 1070 struct snd_soc_dpcm *dpcm; 1071 struct snd_soc_dapm_widget_list *list = *list_; 1072 struct snd_soc_dapm_widget *widget; 1073 int prune = 0; 1074 1075 /* Destroy any old FE <--> BE connections */ 1076 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1077 1078 /* is there a valid CPU DAI widget for this BE */ 1079 widget = rtd_get_cpu_widget(dpcm->be, stream); 1080 1081 /* prune the BE if it's no longer in our active list */ 1082 if (widget && widget_in_list(list, widget)) 1083 continue; 1084 1085 /* is there a valid CODEC DAI widget for this BE */ 1086 widget = rtd_get_codec_widget(dpcm->be, stream); 1087 1088 /* prune the BE if it's no longer in our active list */ 1089 if (widget && widget_in_list(list, widget)) 1090 continue; 1091 1092 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", 1093 stream ? "capture" : "playback", 1094 dpcm->be->dai_link->name, fe->dai_link->name); 1095 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 1096 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 1097 prune++; 1098 } 1099 1100 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); 1101 return prune; 1102 } 1103 1104 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, 1105 struct snd_soc_dapm_widget_list **list_) 1106 { 1107 struct snd_soc_card *card = fe->card; 1108 struct snd_soc_dapm_widget_list *list = *list_; 1109 struct snd_soc_pcm_runtime *be; 1110 int i, new = 0, err; 1111 1112 /* Create any new FE <--> BE connections */ 1113 for (i = 0; i < list->num_widgets; i++) { 1114 1115 switch (list->widgets[i]->id) { 1116 case snd_soc_dapm_dai_in: 1117 case snd_soc_dapm_dai_out: 1118 break; 1119 default: 1120 continue; 1121 } 1122 1123 /* is there a valid BE rtd for this widget */ 1124 be = dpcm_get_be(card, list->widgets[i], stream); 1125 if (!be) { 1126 dev_err(fe->dev, "ASoC: no BE found for %s\n", 1127 list->widgets[i]->name); 1128 continue; 1129 } 1130 1131 /* make sure BE is a real BE */ 1132 if (!be->dai_link->no_pcm) 1133 continue; 1134 1135 /* don't connect if FE is not running */ 1136 if (!fe->dpcm[stream].runtime && !fe->fe_compr) 1137 continue; 1138 1139 /* newly connected FE and BE */ 1140 err = dpcm_be_connect(fe, be, stream); 1141 if (err < 0) { 1142 dev_err(fe->dev, "ASoC: can't connect %s\n", 1143 list->widgets[i]->name); 1144 break; 1145 } else if (err == 0) /* already connected */ 1146 continue; 1147 1148 /* new */ 1149 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 1150 new++; 1151 } 1152 1153 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); 1154 return new; 1155 } 1156 1157 /* 1158 * Find the corresponding BE DAIs that source or sink audio to this 1159 * FE substream. 1160 */ 1161 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, 1162 int stream, struct snd_soc_dapm_widget_list **list, int new) 1163 { 1164 if (new) 1165 return dpcm_add_paths(fe, stream, list); 1166 else 1167 return dpcm_prune_paths(fe, stream, list); 1168 } 1169 1170 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) 1171 { 1172 struct snd_soc_dpcm *dpcm; 1173 1174 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) 1175 dpcm->be->dpcm[stream].runtime_update = 1176 SND_SOC_DPCM_UPDATE_NO; 1177 } 1178 1179 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, 1180 int stream) 1181 { 1182 struct snd_soc_dpcm *dpcm; 1183 1184 /* disable any enabled and non active backends */ 1185 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1186 1187 struct snd_soc_pcm_runtime *be = dpcm->be; 1188 struct snd_pcm_substream *be_substream = 1189 snd_soc_dpcm_get_substream(be, stream); 1190 1191 if (be->dpcm[stream].users == 0) 1192 dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 1193 stream ? "capture" : "playback", 1194 be->dpcm[stream].state); 1195 1196 if (--be->dpcm[stream].users != 0) 1197 continue; 1198 1199 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 1200 continue; 1201 1202 soc_pcm_close(be_substream); 1203 be_substream->runtime = NULL; 1204 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1205 } 1206 } 1207 1208 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) 1209 { 1210 struct snd_soc_dpcm *dpcm; 1211 int err, count = 0; 1212 1213 /* only startup BE DAIs that are either sinks or sources to this FE DAI */ 1214 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1215 1216 struct snd_soc_pcm_runtime *be = dpcm->be; 1217 struct snd_pcm_substream *be_substream = 1218 snd_soc_dpcm_get_substream(be, stream); 1219 1220 if (!be_substream) { 1221 dev_err(be->dev, "ASoC: no backend %s stream\n", 1222 stream ? "capture" : "playback"); 1223 continue; 1224 } 1225 1226 /* is this op for this BE ? */ 1227 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1228 continue; 1229 1230 /* first time the dpcm is open ? */ 1231 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) 1232 dev_err(be->dev, "ASoC: too many users %s at open %d\n", 1233 stream ? "capture" : "playback", 1234 be->dpcm[stream].state); 1235 1236 if (be->dpcm[stream].users++ != 0) 1237 continue; 1238 1239 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 1240 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 1241 continue; 1242 1243 dev_dbg(be->dev, "ASoC: open %s BE %s\n", 1244 stream ? "capture" : "playback", be->dai_link->name); 1245 1246 be_substream->runtime = be->dpcm[stream].runtime; 1247 err = soc_pcm_open(be_substream); 1248 if (err < 0) { 1249 dev_err(be->dev, "ASoC: BE open failed %d\n", err); 1250 be->dpcm[stream].users--; 1251 if (be->dpcm[stream].users < 0) 1252 dev_err(be->dev, "ASoC: no users %s at unwind %d\n", 1253 stream ? "capture" : "playback", 1254 be->dpcm[stream].state); 1255 1256 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1257 goto unwind; 1258 } 1259 1260 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 1261 count++; 1262 } 1263 1264 return count; 1265 1266 unwind: 1267 /* disable any enabled and non active backends */ 1268 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1269 struct snd_soc_pcm_runtime *be = dpcm->be; 1270 struct snd_pcm_substream *be_substream = 1271 snd_soc_dpcm_get_substream(be, stream); 1272 1273 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1274 continue; 1275 1276 if (be->dpcm[stream].users == 0) 1277 dev_err(be->dev, "ASoC: no users %s at close %d\n", 1278 stream ? "capture" : "playback", 1279 be->dpcm[stream].state); 1280 1281 if (--be->dpcm[stream].users != 0) 1282 continue; 1283 1284 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 1285 continue; 1286 1287 soc_pcm_close(be_substream); 1288 be_substream->runtime = NULL; 1289 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1290 } 1291 1292 return err; 1293 } 1294 1295 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, 1296 struct snd_soc_pcm_stream *stream) 1297 { 1298 runtime->hw.rate_min = stream->rate_min; 1299 runtime->hw.rate_max = stream->rate_max; 1300 runtime->hw.channels_min = stream->channels_min; 1301 runtime->hw.channels_max = stream->channels_max; 1302 if (runtime->hw.formats) 1303 runtime->hw.formats &= stream->formats; 1304 else 1305 runtime->hw.formats = stream->formats; 1306 runtime->hw.rates = stream->rates; 1307 } 1308 1309 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) 1310 { 1311 struct snd_pcm_runtime *runtime = substream->runtime; 1312 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1313 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 1314 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 1315 1316 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 1317 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback); 1318 else 1319 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture); 1320 } 1321 1322 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) 1323 { 1324 struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 1325 struct snd_pcm_runtime *runtime = fe_substream->runtime; 1326 int stream = fe_substream->stream, ret = 0; 1327 1328 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1329 1330 ret = dpcm_be_dai_startup(fe, fe_substream->stream); 1331 if (ret < 0) { 1332 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); 1333 goto be_err; 1334 } 1335 1336 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); 1337 1338 /* start the DAI frontend */ 1339 ret = soc_pcm_open(fe_substream); 1340 if (ret < 0) { 1341 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); 1342 goto unwind; 1343 } 1344 1345 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 1346 1347 dpcm_set_fe_runtime(fe_substream); 1348 snd_pcm_limit_hw_rates(runtime); 1349 1350 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1351 return 0; 1352 1353 unwind: 1354 dpcm_be_dai_startup_unwind(fe, fe_substream->stream); 1355 be_err: 1356 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1357 return ret; 1358 } 1359 1360 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 1361 { 1362 struct snd_soc_dpcm *dpcm; 1363 1364 /* only shutdown BEs that are either sinks or sources to this FE DAI */ 1365 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1366 1367 struct snd_soc_pcm_runtime *be = dpcm->be; 1368 struct snd_pcm_substream *be_substream = 1369 snd_soc_dpcm_get_substream(be, stream); 1370 1371 /* is this op for this BE ? */ 1372 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1373 continue; 1374 1375 if (be->dpcm[stream].users == 0) 1376 dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 1377 stream ? "capture" : "playback", 1378 be->dpcm[stream].state); 1379 1380 if (--be->dpcm[stream].users != 0) 1381 continue; 1382 1383 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 1384 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) 1385 continue; 1386 1387 dev_dbg(be->dev, "ASoC: close BE %s\n", 1388 dpcm->fe->dai_link->name); 1389 1390 soc_pcm_close(be_substream); 1391 be_substream->runtime = NULL; 1392 1393 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1394 } 1395 return 0; 1396 } 1397 1398 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) 1399 { 1400 struct snd_soc_pcm_runtime *fe = substream->private_data; 1401 int stream = substream->stream; 1402 1403 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1404 1405 /* shutdown the BEs */ 1406 dpcm_be_dai_shutdown(fe, substream->stream); 1407 1408 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); 1409 1410 /* now shutdown the frontend */ 1411 soc_pcm_close(substream); 1412 1413 /* run the stream event for each BE */ 1414 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); 1415 1416 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1417 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1418 return 0; 1419 } 1420 1421 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) 1422 { 1423 struct snd_soc_dpcm *dpcm; 1424 1425 /* only hw_params backends that are either sinks or sources 1426 * to this frontend DAI */ 1427 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1428 1429 struct snd_soc_pcm_runtime *be = dpcm->be; 1430 struct snd_pcm_substream *be_substream = 1431 snd_soc_dpcm_get_substream(be, stream); 1432 1433 /* is this op for this BE ? */ 1434 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1435 continue; 1436 1437 /* only free hw when no longer used - check all FEs */ 1438 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 1439 continue; 1440 1441 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 1442 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 1443 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 1444 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && 1445 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 1446 continue; 1447 1448 dev_dbg(be->dev, "ASoC: hw_free BE %s\n", 1449 dpcm->fe->dai_link->name); 1450 1451 soc_pcm_hw_free(be_substream); 1452 1453 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 1454 } 1455 1456 return 0; 1457 } 1458 1459 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) 1460 { 1461 struct snd_soc_pcm_runtime *fe = substream->private_data; 1462 int err, stream = substream->stream; 1463 1464 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 1465 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1466 1467 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); 1468 1469 /* call hw_free on the frontend */ 1470 err = soc_pcm_hw_free(substream); 1471 if (err < 0) 1472 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", 1473 fe->dai_link->name); 1474 1475 /* only hw_params backends that are either sinks or sources 1476 * to this frontend DAI */ 1477 err = dpcm_be_dai_hw_free(fe, stream); 1478 1479 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 1480 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1481 1482 mutex_unlock(&fe->card->mutex); 1483 return 0; 1484 } 1485 1486 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) 1487 { 1488 struct snd_soc_dpcm *dpcm; 1489 int ret; 1490 1491 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1492 1493 struct snd_soc_pcm_runtime *be = dpcm->be; 1494 struct snd_pcm_substream *be_substream = 1495 snd_soc_dpcm_get_substream(be, stream); 1496 1497 /* is this op for this BE ? */ 1498 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1499 continue; 1500 1501 /* only allow hw_params() if no connected FEs are running */ 1502 if (!snd_soc_dpcm_can_be_params(fe, be, stream)) 1503 continue; 1504 1505 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 1506 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 1507 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) 1508 continue; 1509 1510 dev_dbg(be->dev, "ASoC: hw_params BE %s\n", 1511 dpcm->fe->dai_link->name); 1512 1513 /* copy params for each dpcm */ 1514 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, 1515 sizeof(struct snd_pcm_hw_params)); 1516 1517 /* perform any hw_params fixups */ 1518 if (be->dai_link->be_hw_params_fixup) { 1519 ret = be->dai_link->be_hw_params_fixup(be, 1520 &dpcm->hw_params); 1521 if (ret < 0) { 1522 dev_err(be->dev, 1523 "ASoC: hw_params BE fixup failed %d\n", 1524 ret); 1525 goto unwind; 1526 } 1527 } 1528 1529 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); 1530 if (ret < 0) { 1531 dev_err(dpcm->be->dev, 1532 "ASoC: hw_params BE failed %d\n", ret); 1533 goto unwind; 1534 } 1535 1536 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 1537 } 1538 return 0; 1539 1540 unwind: 1541 /* disable any enabled and non active backends */ 1542 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1543 struct snd_soc_pcm_runtime *be = dpcm->be; 1544 struct snd_pcm_substream *be_substream = 1545 snd_soc_dpcm_get_substream(be, stream); 1546 1547 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1548 continue; 1549 1550 /* only allow hw_free() if no connected FEs are running */ 1551 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 1552 continue; 1553 1554 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 1555 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 1556 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 1557 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 1558 continue; 1559 1560 soc_pcm_hw_free(be_substream); 1561 } 1562 1563 return ret; 1564 } 1565 1566 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, 1567 struct snd_pcm_hw_params *params) 1568 { 1569 struct snd_soc_pcm_runtime *fe = substream->private_data; 1570 int ret, stream = substream->stream; 1571 1572 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 1573 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1574 1575 memcpy(&fe->dpcm[substream->stream].hw_params, params, 1576 sizeof(struct snd_pcm_hw_params)); 1577 ret = dpcm_be_dai_hw_params(fe, substream->stream); 1578 if (ret < 0) { 1579 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); 1580 goto out; 1581 } 1582 1583 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", 1584 fe->dai_link->name, params_rate(params), 1585 params_channels(params), params_format(params)); 1586 1587 /* call hw_params on the frontend */ 1588 ret = soc_pcm_hw_params(substream, params); 1589 if (ret < 0) { 1590 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); 1591 dpcm_be_dai_hw_free(fe, stream); 1592 } else 1593 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 1594 1595 out: 1596 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1597 mutex_unlock(&fe->card->mutex); 1598 return ret; 1599 } 1600 1601 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, 1602 struct snd_pcm_substream *substream, int cmd) 1603 { 1604 int ret; 1605 1606 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", 1607 dpcm->fe->dai_link->name, cmd); 1608 1609 ret = soc_pcm_trigger(substream, cmd); 1610 if (ret < 0) 1611 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); 1612 1613 return ret; 1614 } 1615 1616 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, 1617 int cmd) 1618 { 1619 struct snd_soc_dpcm *dpcm; 1620 int ret = 0; 1621 1622 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1623 1624 struct snd_soc_pcm_runtime *be = dpcm->be; 1625 struct snd_pcm_substream *be_substream = 1626 snd_soc_dpcm_get_substream(be, stream); 1627 1628 /* is this op for this BE ? */ 1629 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1630 continue; 1631 1632 switch (cmd) { 1633 case SNDRV_PCM_TRIGGER_START: 1634 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 1635 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 1636 continue; 1637 1638 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 1639 if (ret) 1640 return ret; 1641 1642 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 1643 break; 1644 case SNDRV_PCM_TRIGGER_RESUME: 1645 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 1646 continue; 1647 1648 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 1649 if (ret) 1650 return ret; 1651 1652 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 1653 break; 1654 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1655 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 1656 continue; 1657 1658 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 1659 if (ret) 1660 return ret; 1661 1662 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 1663 break; 1664 case SNDRV_PCM_TRIGGER_STOP: 1665 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 1666 continue; 1667 1668 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 1669 continue; 1670 1671 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 1672 if (ret) 1673 return ret; 1674 1675 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 1676 break; 1677 case SNDRV_PCM_TRIGGER_SUSPEND: 1678 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) 1679 continue; 1680 1681 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 1682 continue; 1683 1684 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 1685 if (ret) 1686 return ret; 1687 1688 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; 1689 break; 1690 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1691 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 1692 continue; 1693 1694 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 1695 continue; 1696 1697 ret = dpcm_do_trigger(dpcm, be_substream, cmd); 1698 if (ret) 1699 return ret; 1700 1701 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 1702 break; 1703 } 1704 } 1705 1706 return ret; 1707 } 1708 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); 1709 1710 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) 1711 { 1712 struct snd_soc_pcm_runtime *fe = substream->private_data; 1713 int stream = substream->stream, ret; 1714 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 1715 1716 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1717 1718 switch (trigger) { 1719 case SND_SOC_DPCM_TRIGGER_PRE: 1720 /* call trigger on the frontend before the backend. */ 1721 1722 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", 1723 fe->dai_link->name, cmd); 1724 1725 ret = soc_pcm_trigger(substream, cmd); 1726 if (ret < 0) { 1727 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 1728 goto out; 1729 } 1730 1731 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 1732 break; 1733 case SND_SOC_DPCM_TRIGGER_POST: 1734 /* call trigger on the frontend after the backend. */ 1735 1736 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 1737 if (ret < 0) { 1738 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 1739 goto out; 1740 } 1741 1742 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", 1743 fe->dai_link->name, cmd); 1744 1745 ret = soc_pcm_trigger(substream, cmd); 1746 break; 1747 case SND_SOC_DPCM_TRIGGER_BESPOKE: 1748 /* bespoke trigger() - handles both FE and BEs */ 1749 1750 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", 1751 fe->dai_link->name, cmd); 1752 1753 ret = soc_pcm_bespoke_trigger(substream, cmd); 1754 if (ret < 0) { 1755 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 1756 goto out; 1757 } 1758 break; 1759 default: 1760 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, 1761 fe->dai_link->name); 1762 ret = -EINVAL; 1763 goto out; 1764 } 1765 1766 switch (cmd) { 1767 case SNDRV_PCM_TRIGGER_START: 1768 case SNDRV_PCM_TRIGGER_RESUME: 1769 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1770 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 1771 break; 1772 case SNDRV_PCM_TRIGGER_STOP: 1773 case SNDRV_PCM_TRIGGER_SUSPEND: 1774 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1775 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 1776 break; 1777 } 1778 1779 out: 1780 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1781 return ret; 1782 } 1783 1784 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) 1785 { 1786 struct snd_soc_dpcm *dpcm; 1787 int ret = 0; 1788 1789 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1790 1791 struct snd_soc_pcm_runtime *be = dpcm->be; 1792 struct snd_pcm_substream *be_substream = 1793 snd_soc_dpcm_get_substream(be, stream); 1794 1795 /* is this op for this BE ? */ 1796 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1797 continue; 1798 1799 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 1800 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 1801 continue; 1802 1803 dev_dbg(be->dev, "ASoC: prepare BE %s\n", 1804 dpcm->fe->dai_link->name); 1805 1806 ret = soc_pcm_prepare(be_substream); 1807 if (ret < 0) { 1808 dev_err(be->dev, "ASoC: backend prepare failed %d\n", 1809 ret); 1810 break; 1811 } 1812 1813 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 1814 } 1815 return ret; 1816 } 1817 1818 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) 1819 { 1820 struct snd_soc_pcm_runtime *fe = substream->private_data; 1821 int stream = substream->stream, ret = 0; 1822 1823 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 1824 1825 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); 1826 1827 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1828 1829 /* there is no point preparing this FE if there are no BEs */ 1830 if (list_empty(&fe->dpcm[stream].be_clients)) { 1831 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", 1832 fe->dai_link->name); 1833 ret = -EINVAL; 1834 goto out; 1835 } 1836 1837 ret = dpcm_be_dai_prepare(fe, substream->stream); 1838 if (ret < 0) 1839 goto out; 1840 1841 /* call prepare on the frontend */ 1842 ret = soc_pcm_prepare(substream); 1843 if (ret < 0) { 1844 dev_err(fe->dev,"ASoC: prepare FE %s failed\n", 1845 fe->dai_link->name); 1846 goto out; 1847 } 1848 1849 /* run the stream event for each BE */ 1850 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); 1851 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 1852 1853 out: 1854 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1855 mutex_unlock(&fe->card->mutex); 1856 1857 return ret; 1858 } 1859 1860 static int soc_pcm_ioctl(struct snd_pcm_substream *substream, 1861 unsigned int cmd, void *arg) 1862 { 1863 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1864 struct snd_soc_platform *platform = rtd->platform; 1865 1866 if (platform->driver->ops && platform->driver->ops->ioctl) 1867 return platform->driver->ops->ioctl(substream, cmd, arg); 1868 return snd_pcm_lib_ioctl(substream, cmd, arg); 1869 } 1870 1871 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 1872 { 1873 struct snd_pcm_substream *substream = 1874 snd_soc_dpcm_get_substream(fe, stream); 1875 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 1876 int err; 1877 1878 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", 1879 stream ? "capture" : "playback", fe->dai_link->name); 1880 1881 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 1882 /* call bespoke trigger - FE takes care of all BE triggers */ 1883 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", 1884 fe->dai_link->name); 1885 1886 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); 1887 if (err < 0) 1888 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 1889 } else { 1890 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", 1891 fe->dai_link->name); 1892 1893 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); 1894 if (err < 0) 1895 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 1896 } 1897 1898 err = dpcm_be_dai_hw_free(fe, stream); 1899 if (err < 0) 1900 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); 1901 1902 err = dpcm_be_dai_shutdown(fe, stream); 1903 if (err < 0) 1904 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); 1905 1906 /* run the stream event for each BE */ 1907 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 1908 1909 return 0; 1910 } 1911 1912 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) 1913 { 1914 struct snd_pcm_substream *substream = 1915 snd_soc_dpcm_get_substream(fe, stream); 1916 struct snd_soc_dpcm *dpcm; 1917 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 1918 int ret; 1919 1920 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", 1921 stream ? "capture" : "playback", fe->dai_link->name); 1922 1923 /* Only start the BE if the FE is ready */ 1924 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || 1925 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) 1926 return -EINVAL; 1927 1928 /* startup must always be called for new BEs */ 1929 ret = dpcm_be_dai_startup(fe, stream); 1930 if (ret < 0) 1931 goto disconnect; 1932 1933 /* keep going if FE state is > open */ 1934 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) 1935 return 0; 1936 1937 ret = dpcm_be_dai_hw_params(fe, stream); 1938 if (ret < 0) 1939 goto close; 1940 1941 /* keep going if FE state is > hw_params */ 1942 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) 1943 return 0; 1944 1945 1946 ret = dpcm_be_dai_prepare(fe, stream); 1947 if (ret < 0) 1948 goto hw_free; 1949 1950 /* run the stream event for each BE */ 1951 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 1952 1953 /* keep going if FE state is > prepare */ 1954 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || 1955 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) 1956 return 0; 1957 1958 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 1959 /* call trigger on the frontend - FE takes care of all BE triggers */ 1960 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", 1961 fe->dai_link->name); 1962 1963 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); 1964 if (ret < 0) { 1965 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); 1966 goto hw_free; 1967 } 1968 } else { 1969 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", 1970 fe->dai_link->name); 1971 1972 ret = dpcm_be_dai_trigger(fe, stream, 1973 SNDRV_PCM_TRIGGER_START); 1974 if (ret < 0) { 1975 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 1976 goto hw_free; 1977 } 1978 } 1979 1980 return 0; 1981 1982 hw_free: 1983 dpcm_be_dai_hw_free(fe, stream); 1984 close: 1985 dpcm_be_dai_shutdown(fe, stream); 1986 disconnect: 1987 /* disconnect any non started BEs */ 1988 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 1989 struct snd_soc_pcm_runtime *be = dpcm->be; 1990 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 1991 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 1992 } 1993 1994 return ret; 1995 } 1996 1997 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream) 1998 { 1999 int ret; 2000 2001 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 2002 ret = dpcm_run_update_startup(fe, stream); 2003 if (ret < 0) 2004 dev_err(fe->dev, "ASoC: failed to startup some BEs\n"); 2005 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 2006 2007 return ret; 2008 } 2009 2010 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream) 2011 { 2012 int ret; 2013 2014 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 2015 ret = dpcm_run_update_shutdown(fe, stream); 2016 if (ret < 0) 2017 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); 2018 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 2019 2020 return ret; 2021 } 2022 2023 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and 2024 * any DAI links. 2025 */ 2026 int soc_dpcm_runtime_update(struct snd_soc_card *card) 2027 { 2028 int i, old, new, paths; 2029 2030 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2031 for (i = 0; i < card->num_rtd; i++) { 2032 struct snd_soc_dapm_widget_list *list; 2033 struct snd_soc_pcm_runtime *fe = &card->rtd[i]; 2034 2035 /* make sure link is FE */ 2036 if (!fe->dai_link->dynamic) 2037 continue; 2038 2039 /* only check active links */ 2040 if (!fe->cpu_dai->active) 2041 continue; 2042 2043 /* DAPM sync will call this to update DSP paths */ 2044 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n", 2045 fe->dai_link->name); 2046 2047 /* skip if FE doesn't have playback capability */ 2048 if (!fe->cpu_dai->driver->playback.channels_min) 2049 goto capture; 2050 2051 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list); 2052 if (paths < 0) { 2053 dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 2054 fe->dai_link->name, "playback"); 2055 mutex_unlock(&card->mutex); 2056 return paths; 2057 } 2058 2059 /* update any new playback paths */ 2060 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1); 2061 if (new) { 2062 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK); 2063 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); 2064 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); 2065 } 2066 2067 /* update any old playback paths */ 2068 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0); 2069 if (old) { 2070 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK); 2071 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); 2072 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); 2073 } 2074 2075 capture: 2076 /* skip if FE doesn't have capture capability */ 2077 if (!fe->cpu_dai->driver->capture.channels_min) 2078 continue; 2079 2080 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list); 2081 if (paths < 0) { 2082 dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 2083 fe->dai_link->name, "capture"); 2084 mutex_unlock(&card->mutex); 2085 return paths; 2086 } 2087 2088 /* update any new capture paths */ 2089 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1); 2090 if (new) { 2091 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE); 2092 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); 2093 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); 2094 } 2095 2096 /* update any old capture paths */ 2097 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0); 2098 if (old) { 2099 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE); 2100 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); 2101 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); 2102 } 2103 2104 dpcm_path_put(&list); 2105 } 2106 2107 mutex_unlock(&card->mutex); 2108 return 0; 2109 } 2110 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute) 2111 { 2112 struct snd_soc_dpcm *dpcm; 2113 struct list_head *clients = 2114 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients; 2115 2116 list_for_each_entry(dpcm, clients, list_be) { 2117 2118 struct snd_soc_pcm_runtime *be = dpcm->be; 2119 struct snd_soc_dai *dai = be->codec_dai; 2120 struct snd_soc_dai_driver *drv = dai->driver; 2121 2122 if (be->dai_link->ignore_suspend) 2123 continue; 2124 2125 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name); 2126 2127 if (drv->ops && drv->ops->digital_mute && dai->playback_active) 2128 drv->ops->digital_mute(dai, mute); 2129 } 2130 2131 return 0; 2132 } 2133 2134 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) 2135 { 2136 struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 2137 struct snd_soc_dpcm *dpcm; 2138 struct snd_soc_dapm_widget_list *list; 2139 int ret; 2140 int stream = fe_substream->stream; 2141 2142 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2143 fe->dpcm[stream].runtime = fe_substream->runtime; 2144 2145 if (dpcm_path_get(fe, stream, &list) <= 0) { 2146 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", 2147 fe->dai_link->name, stream ? "capture" : "playback"); 2148 } 2149 2150 /* calculate valid and active FE <-> BE dpcms */ 2151 dpcm_process_paths(fe, stream, &list, 1); 2152 2153 ret = dpcm_fe_dai_startup(fe_substream); 2154 if (ret < 0) { 2155 /* clean up all links */ 2156 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) 2157 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2158 2159 dpcm_be_disconnect(fe, stream); 2160 fe->dpcm[stream].runtime = NULL; 2161 } 2162 2163 dpcm_clear_pending_state(fe, stream); 2164 dpcm_path_put(&list); 2165 mutex_unlock(&fe->card->mutex); 2166 return ret; 2167 } 2168 2169 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) 2170 { 2171 struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 2172 struct snd_soc_dpcm *dpcm; 2173 int stream = fe_substream->stream, ret; 2174 2175 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2176 ret = dpcm_fe_dai_shutdown(fe_substream); 2177 2178 /* mark FE's links ready to prune */ 2179 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) 2180 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2181 2182 dpcm_be_disconnect(fe, stream); 2183 2184 fe->dpcm[stream].runtime = NULL; 2185 mutex_unlock(&fe->card->mutex); 2186 return ret; 2187 } 2188 2189 /* create a new pcm */ 2190 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 2191 { 2192 struct snd_soc_platform *platform = rtd->platform; 2193 struct snd_soc_dai *codec_dai = rtd->codec_dai; 2194 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 2195 struct snd_pcm *pcm; 2196 char new_name[64]; 2197 int ret = 0, playback = 0, capture = 0; 2198 2199 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { 2200 playback = rtd->dai_link->dpcm_playback; 2201 capture = rtd->dai_link->dpcm_capture; 2202 } else { 2203 if (codec_dai->driver->playback.channels_min && 2204 cpu_dai->driver->playback.channels_min) 2205 playback = 1; 2206 if (codec_dai->driver->capture.channels_min && 2207 cpu_dai->driver->capture.channels_min) 2208 capture = 1; 2209 } 2210 2211 if (rtd->dai_link->playback_only) { 2212 playback = 1; 2213 capture = 0; 2214 } 2215 2216 if (rtd->dai_link->capture_only) { 2217 playback = 0; 2218 capture = 1; 2219 } 2220 2221 /* create the PCM */ 2222 if (rtd->dai_link->no_pcm) { 2223 snprintf(new_name, sizeof(new_name), "(%s)", 2224 rtd->dai_link->stream_name); 2225 2226 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 2227 playback, capture, &pcm); 2228 } else { 2229 if (rtd->dai_link->dynamic) 2230 snprintf(new_name, sizeof(new_name), "%s (*)", 2231 rtd->dai_link->stream_name); 2232 else 2233 snprintf(new_name, sizeof(new_name), "%s %s-%d", 2234 rtd->dai_link->stream_name, codec_dai->name, num); 2235 2236 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, 2237 capture, &pcm); 2238 } 2239 if (ret < 0) { 2240 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", 2241 rtd->dai_link->name); 2242 return ret; 2243 } 2244 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); 2245 2246 /* DAPM dai link stream work */ 2247 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); 2248 2249 rtd->pcm = pcm; 2250 pcm->private_data = rtd; 2251 2252 if (rtd->dai_link->no_pcm) { 2253 if (playback) 2254 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 2255 if (capture) 2256 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 2257 goto out; 2258 } 2259 2260 /* ASoC PCM operations */ 2261 if (rtd->dai_link->dynamic) { 2262 rtd->ops.open = dpcm_fe_dai_open; 2263 rtd->ops.hw_params = dpcm_fe_dai_hw_params; 2264 rtd->ops.prepare = dpcm_fe_dai_prepare; 2265 rtd->ops.trigger = dpcm_fe_dai_trigger; 2266 rtd->ops.hw_free = dpcm_fe_dai_hw_free; 2267 rtd->ops.close = dpcm_fe_dai_close; 2268 rtd->ops.pointer = soc_pcm_pointer; 2269 rtd->ops.ioctl = soc_pcm_ioctl; 2270 } else { 2271 rtd->ops.open = soc_pcm_open; 2272 rtd->ops.hw_params = soc_pcm_hw_params; 2273 rtd->ops.prepare = soc_pcm_prepare; 2274 rtd->ops.trigger = soc_pcm_trigger; 2275 rtd->ops.hw_free = soc_pcm_hw_free; 2276 rtd->ops.close = soc_pcm_close; 2277 rtd->ops.pointer = soc_pcm_pointer; 2278 rtd->ops.ioctl = soc_pcm_ioctl; 2279 } 2280 2281 if (platform->driver->ops) { 2282 rtd->ops.ack = platform->driver->ops->ack; 2283 rtd->ops.copy = platform->driver->ops->copy; 2284 rtd->ops.silence = platform->driver->ops->silence; 2285 rtd->ops.page = platform->driver->ops->page; 2286 rtd->ops.mmap = platform->driver->ops->mmap; 2287 } 2288 2289 if (playback) 2290 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); 2291 2292 if (capture) 2293 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); 2294 2295 if (platform->driver->pcm_new) { 2296 ret = platform->driver->pcm_new(rtd); 2297 if (ret < 0) { 2298 dev_err(platform->dev, 2299 "ASoC: pcm constructor failed: %d\n", 2300 ret); 2301 return ret; 2302 } 2303 } 2304 2305 pcm->private_free = platform->driver->pcm_free; 2306 out: 2307 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name, 2308 cpu_dai->name); 2309 return ret; 2310 } 2311 2312 /* is the current PCM operation for this FE ? */ 2313 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) 2314 { 2315 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) 2316 return 1; 2317 return 0; 2318 } 2319 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); 2320 2321 /* is the current PCM operation for this BE ? */ 2322 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, 2323 struct snd_soc_pcm_runtime *be, int stream) 2324 { 2325 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || 2326 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && 2327 be->dpcm[stream].runtime_update)) 2328 return 1; 2329 return 0; 2330 } 2331 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); 2332 2333 /* get the substream for this BE */ 2334 struct snd_pcm_substream * 2335 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) 2336 { 2337 return be->pcm->streams[stream].substream; 2338 } 2339 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); 2340 2341 /* get the BE runtime state */ 2342 enum snd_soc_dpcm_state 2343 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream) 2344 { 2345 return be->dpcm[stream].state; 2346 } 2347 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state); 2348 2349 /* set the BE runtime state */ 2350 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be, 2351 int stream, enum snd_soc_dpcm_state state) 2352 { 2353 be->dpcm[stream].state = state; 2354 } 2355 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state); 2356 2357 /* 2358 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE 2359 * are not running, paused or suspended for the specified stream direction. 2360 */ 2361 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, 2362 struct snd_soc_pcm_runtime *be, int stream) 2363 { 2364 struct snd_soc_dpcm *dpcm; 2365 int state; 2366 2367 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { 2368 2369 if (dpcm->fe == fe) 2370 continue; 2371 2372 state = dpcm->fe->dpcm[stream].state; 2373 if (state == SND_SOC_DPCM_STATE_START || 2374 state == SND_SOC_DPCM_STATE_PAUSED || 2375 state == SND_SOC_DPCM_STATE_SUSPEND) 2376 return 0; 2377 } 2378 2379 /* it's safe to free/stop this BE DAI */ 2380 return 1; 2381 } 2382 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); 2383 2384 /* 2385 * We can only change hw params a BE DAI if any of it's FE are not prepared, 2386 * running, paused or suspended for the specified stream direction. 2387 */ 2388 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, 2389 struct snd_soc_pcm_runtime *be, int stream) 2390 { 2391 struct snd_soc_dpcm *dpcm; 2392 int state; 2393 2394 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { 2395 2396 if (dpcm->fe == fe) 2397 continue; 2398 2399 state = dpcm->fe->dpcm[stream].state; 2400 if (state == SND_SOC_DPCM_STATE_START || 2401 state == SND_SOC_DPCM_STATE_PAUSED || 2402 state == SND_SOC_DPCM_STATE_SUSPEND || 2403 state == SND_SOC_DPCM_STATE_PREPARE) 2404 return 0; 2405 } 2406 2407 /* it's safe to change hw_params */ 2408 return 1; 2409 } 2410 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); 2411 2412 int snd_soc_platform_trigger(struct snd_pcm_substream *substream, 2413 int cmd, struct snd_soc_platform *platform) 2414 { 2415 if (platform->driver->ops && platform->driver->ops->trigger) 2416 return platform->driver->ops->trigger(substream, cmd); 2417 return 0; 2418 } 2419 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger); 2420 2421 #ifdef CONFIG_DEBUG_FS 2422 static char *dpcm_state_string(enum snd_soc_dpcm_state state) 2423 { 2424 switch (state) { 2425 case SND_SOC_DPCM_STATE_NEW: 2426 return "new"; 2427 case SND_SOC_DPCM_STATE_OPEN: 2428 return "open"; 2429 case SND_SOC_DPCM_STATE_HW_PARAMS: 2430 return "hw_params"; 2431 case SND_SOC_DPCM_STATE_PREPARE: 2432 return "prepare"; 2433 case SND_SOC_DPCM_STATE_START: 2434 return "start"; 2435 case SND_SOC_DPCM_STATE_STOP: 2436 return "stop"; 2437 case SND_SOC_DPCM_STATE_SUSPEND: 2438 return "suspend"; 2439 case SND_SOC_DPCM_STATE_PAUSED: 2440 return "paused"; 2441 case SND_SOC_DPCM_STATE_HW_FREE: 2442 return "hw_free"; 2443 case SND_SOC_DPCM_STATE_CLOSE: 2444 return "close"; 2445 } 2446 2447 return "unknown"; 2448 } 2449 2450 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, 2451 int stream, char *buf, size_t size) 2452 { 2453 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; 2454 struct snd_soc_dpcm *dpcm; 2455 ssize_t offset = 0; 2456 2457 /* FE state */ 2458 offset += snprintf(buf + offset, size - offset, 2459 "[%s - %s]\n", fe->dai_link->name, 2460 stream ? "Capture" : "Playback"); 2461 2462 offset += snprintf(buf + offset, size - offset, "State: %s\n", 2463 dpcm_state_string(fe->dpcm[stream].state)); 2464 2465 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 2466 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 2467 offset += snprintf(buf + offset, size - offset, 2468 "Hardware Params: " 2469 "Format = %s, Channels = %d, Rate = %d\n", 2470 snd_pcm_format_name(params_format(params)), 2471 params_channels(params), 2472 params_rate(params)); 2473 2474 /* BEs state */ 2475 offset += snprintf(buf + offset, size - offset, "Backends:\n"); 2476 2477 if (list_empty(&fe->dpcm[stream].be_clients)) { 2478 offset += snprintf(buf + offset, size - offset, 2479 " No active DSP links\n"); 2480 goto out; 2481 } 2482 2483 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 2484 struct snd_soc_pcm_runtime *be = dpcm->be; 2485 params = &dpcm->hw_params; 2486 2487 offset += snprintf(buf + offset, size - offset, 2488 "- %s\n", be->dai_link->name); 2489 2490 offset += snprintf(buf + offset, size - offset, 2491 " State: %s\n", 2492 dpcm_state_string(be->dpcm[stream].state)); 2493 2494 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 2495 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 2496 offset += snprintf(buf + offset, size - offset, 2497 " Hardware Params: " 2498 "Format = %s, Channels = %d, Rate = %d\n", 2499 snd_pcm_format_name(params_format(params)), 2500 params_channels(params), 2501 params_rate(params)); 2502 } 2503 2504 out: 2505 return offset; 2506 } 2507 2508 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, 2509 size_t count, loff_t *ppos) 2510 { 2511 struct snd_soc_pcm_runtime *fe = file->private_data; 2512 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; 2513 char *buf; 2514 2515 buf = kmalloc(out_count, GFP_KERNEL); 2516 if (!buf) 2517 return -ENOMEM; 2518 2519 if (fe->cpu_dai->driver->playback.channels_min) 2520 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK, 2521 buf + offset, out_count - offset); 2522 2523 if (fe->cpu_dai->driver->capture.channels_min) 2524 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE, 2525 buf + offset, out_count - offset); 2526 2527 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); 2528 2529 kfree(buf); 2530 return ret; 2531 } 2532 2533 static const struct file_operations dpcm_state_fops = { 2534 .open = simple_open, 2535 .read = dpcm_state_read_file, 2536 .llseek = default_llseek, 2537 }; 2538 2539 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) 2540 { 2541 if (!rtd->dai_link) 2542 return 0; 2543 2544 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, 2545 rtd->card->debugfs_card_root); 2546 if (!rtd->debugfs_dpcm_root) { 2547 dev_dbg(rtd->dev, 2548 "ASoC: Failed to create dpcm debugfs directory %s\n", 2549 rtd->dai_link->name); 2550 return -EINVAL; 2551 } 2552 2553 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444, 2554 rtd->debugfs_dpcm_root, 2555 rtd, &dpcm_state_fops); 2556 2557 return 0; 2558 } 2559 #endif 2560