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