0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/kernel.h>
0014 #include <linux/init.h>
0015 #include <linux/delay.h>
0016 #include <linux/pinctrl/consumer.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/slab.h>
0019 #include <linux/workqueue.h>
0020 #include <linux/export.h>
0021 #include <linux/debugfs.h>
0022 #include <sound/core.h>
0023 #include <sound/pcm.h>
0024 #include <sound/pcm_params.h>
0025 #include <sound/soc.h>
0026 #include <sound/soc-dpcm.h>
0027 #include <sound/soc-link.h>
0028 #include <sound/initval.h>
0029
0030 static inline void snd_soc_dpcm_mutex_lock(struct snd_soc_pcm_runtime *rtd)
0031 {
0032 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
0033 }
0034
0035 static inline void snd_soc_dpcm_mutex_unlock(struct snd_soc_pcm_runtime *rtd)
0036 {
0037 mutex_unlock(&rtd->card->pcm_mutex);
0038 }
0039
0040 #define snd_soc_dpcm_mutex_assert_held(rtd) \
0041 lockdep_assert_held(&(rtd)->card->pcm_mutex)
0042
0043 static inline void snd_soc_dpcm_stream_lock_irq(struct snd_soc_pcm_runtime *rtd,
0044 int stream)
0045 {
0046 snd_pcm_stream_lock_irq(snd_soc_dpcm_get_substream(rtd, stream));
0047 }
0048
0049 #define snd_soc_dpcm_stream_lock_irqsave_nested(rtd, stream, flags) \
0050 snd_pcm_stream_lock_irqsave_nested(snd_soc_dpcm_get_substream(rtd, stream), flags)
0051
0052 static inline void snd_soc_dpcm_stream_unlock_irq(struct snd_soc_pcm_runtime *rtd,
0053 int stream)
0054 {
0055 snd_pcm_stream_unlock_irq(snd_soc_dpcm_get_substream(rtd, stream));
0056 }
0057
0058 #define snd_soc_dpcm_stream_unlock_irqrestore(rtd, stream, flags) \
0059 snd_pcm_stream_unlock_irqrestore(snd_soc_dpcm_get_substream(rtd, stream), flags)
0060
0061 #define DPCM_MAX_BE_USERS 8
0062
0063 static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
0064 {
0065 return (rtd)->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
0066 }
0067 static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
0068 {
0069 return (rtd)->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
0070 }
0071
0072 #ifdef CONFIG_DEBUG_FS
0073 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
0074 {
0075 switch (state) {
0076 case SND_SOC_DPCM_STATE_NEW:
0077 return "new";
0078 case SND_SOC_DPCM_STATE_OPEN:
0079 return "open";
0080 case SND_SOC_DPCM_STATE_HW_PARAMS:
0081 return "hw_params";
0082 case SND_SOC_DPCM_STATE_PREPARE:
0083 return "prepare";
0084 case SND_SOC_DPCM_STATE_START:
0085 return "start";
0086 case SND_SOC_DPCM_STATE_STOP:
0087 return "stop";
0088 case SND_SOC_DPCM_STATE_SUSPEND:
0089 return "suspend";
0090 case SND_SOC_DPCM_STATE_PAUSED:
0091 return "paused";
0092 case SND_SOC_DPCM_STATE_HW_FREE:
0093 return "hw_free";
0094 case SND_SOC_DPCM_STATE_CLOSE:
0095 return "close";
0096 }
0097
0098 return "unknown";
0099 }
0100
0101 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
0102 int stream, char *buf, size_t size)
0103 {
0104 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
0105 struct snd_soc_dpcm *dpcm;
0106 ssize_t offset = 0;
0107
0108
0109 offset += scnprintf(buf + offset, size - offset,
0110 "[%s - %s]\n", fe->dai_link->name,
0111 stream ? "Capture" : "Playback");
0112
0113 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
0114 dpcm_state_string(fe->dpcm[stream].state));
0115
0116 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
0117 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
0118 offset += scnprintf(buf + offset, size - offset,
0119 "Hardware Params: "
0120 "Format = %s, Channels = %d, Rate = %d\n",
0121 snd_pcm_format_name(params_format(params)),
0122 params_channels(params),
0123 params_rate(params));
0124
0125
0126 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
0127
0128 if (list_empty(&fe->dpcm[stream].be_clients)) {
0129 offset += scnprintf(buf + offset, size - offset,
0130 " No active DSP links\n");
0131 goto out;
0132 }
0133
0134 for_each_dpcm_be(fe, stream, dpcm) {
0135 struct snd_soc_pcm_runtime *be = dpcm->be;
0136 params = &dpcm->hw_params;
0137
0138 offset += scnprintf(buf + offset, size - offset,
0139 "- %s\n", be->dai_link->name);
0140
0141 offset += scnprintf(buf + offset, size - offset,
0142 " State: %s\n",
0143 dpcm_state_string(be->dpcm[stream].state));
0144
0145 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
0146 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
0147 offset += scnprintf(buf + offset, size - offset,
0148 " Hardware Params: "
0149 "Format = %s, Channels = %d, Rate = %d\n",
0150 snd_pcm_format_name(params_format(params)),
0151 params_channels(params),
0152 params_rate(params));
0153 }
0154 out:
0155 return offset;
0156 }
0157
0158 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
0159 size_t count, loff_t *ppos)
0160 {
0161 struct snd_soc_pcm_runtime *fe = file->private_data;
0162 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
0163 int stream;
0164 char *buf;
0165
0166 if (fe->num_cpus > 1) {
0167 dev_err(fe->dev,
0168 "%s doesn't support Multi CPU yet\n", __func__);
0169 return -EINVAL;
0170 }
0171
0172 buf = kmalloc(out_count, GFP_KERNEL);
0173 if (!buf)
0174 return -ENOMEM;
0175
0176 snd_soc_dpcm_mutex_lock(fe);
0177 for_each_pcm_streams(stream)
0178 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
0179 offset += dpcm_show_state(fe, stream,
0180 buf + offset,
0181 out_count - offset);
0182 snd_soc_dpcm_mutex_unlock(fe);
0183
0184 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
0185
0186 kfree(buf);
0187 return ret;
0188 }
0189
0190 static const struct file_operations dpcm_state_fops = {
0191 .open = simple_open,
0192 .read = dpcm_state_read_file,
0193 .llseek = default_llseek,
0194 };
0195
0196 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
0197 {
0198 if (!rtd->dai_link->dynamic)
0199 return;
0200
0201 if (!rtd->card->debugfs_card_root)
0202 return;
0203
0204 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
0205 rtd->card->debugfs_card_root);
0206
0207 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
0208 rtd, &dpcm_state_fops);
0209 }
0210
0211 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
0212 {
0213 char *name;
0214
0215 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
0216 stream ? "capture" : "playback");
0217 if (name) {
0218 dpcm->debugfs_state = debugfs_create_dir(
0219 name, dpcm->fe->debugfs_dpcm_root);
0220 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
0221 &dpcm->state);
0222 kfree(name);
0223 }
0224 }
0225
0226 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
0227 {
0228 debugfs_remove_recursive(dpcm->debugfs_state);
0229 }
0230
0231 #else
0232 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
0233 int stream)
0234 {
0235 }
0236
0237 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
0238 {
0239 }
0240 #endif
0241
0242
0243
0244
0245
0246
0247 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
0248 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
0249 int stream, enum snd_soc_dpcm_update state)
0250 {
0251 struct snd_pcm_substream *substream =
0252 snd_soc_dpcm_get_substream(fe, stream);
0253
0254 snd_soc_dpcm_stream_lock_irq(fe, stream);
0255 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
0256 dpcm_fe_dai_do_trigger(substream,
0257 fe->dpcm[stream].trigger_pending - 1);
0258 fe->dpcm[stream].trigger_pending = 0;
0259 }
0260 fe->dpcm[stream].runtime_update = state;
0261 snd_soc_dpcm_stream_unlock_irq(fe, stream);
0262 }
0263
0264 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
0265 int stream, enum snd_soc_dpcm_update state)
0266 {
0267 be->dpcm[stream].runtime_update = state;
0268 }
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
0284 int stream, int action)
0285 {
0286 struct snd_soc_dai *dai;
0287 int i;
0288
0289 snd_soc_dpcm_mutex_assert_held(rtd);
0290
0291 for_each_rtd_dais(rtd, i, dai)
0292 snd_soc_dai_action(dai, stream, action);
0293 }
0294 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
0306 {
0307 struct snd_soc_component *component;
0308 bool ignore = true;
0309 int i;
0310
0311 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
0312 return true;
0313
0314 for_each_rtd_components(rtd, i, component)
0315 ignore &= !component->driver->use_pmdown_time;
0316
0317 return ignore;
0318 }
0319
0320
0321
0322
0323
0324
0325
0326
0327 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
0328 const struct snd_pcm_hardware *hw)
0329 {
0330 substream->runtime->hw = *hw;
0331
0332 return 0;
0333 }
0334 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
0335
0336
0337 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
0338 int event)
0339 {
0340 struct snd_soc_dpcm *dpcm;
0341
0342 snd_soc_dpcm_mutex_assert_held(fe);
0343
0344 for_each_dpcm_be(fe, dir, dpcm) {
0345
0346 struct snd_soc_pcm_runtime *be = dpcm->be;
0347
0348 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
0349 be->dai_link->name, event, dir);
0350
0351 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
0352 (be->dpcm[dir].users >= 1))
0353 continue;
0354
0355 snd_soc_dapm_stream_event(be, dir, event);
0356 }
0357
0358 snd_soc_dapm_stream_event(fe, dir, event);
0359
0360 return 0;
0361 }
0362
0363 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
0364 struct snd_pcm_hw_params *params)
0365 {
0366 if (params) {
0367 dai->rate = params_rate(params);
0368 dai->channels = params_channels(params);
0369 dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
0370 } else {
0371 dai->rate = 0;
0372 dai->channels = 0;
0373 dai->sample_bits = 0;
0374 }
0375 }
0376
0377 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
0378 struct snd_soc_dai *soc_dai)
0379 {
0380 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0381 int ret;
0382
0383 if (!snd_soc_dai_active(soc_dai))
0384 return 0;
0385
0386 #define __soc_pcm_apply_symmetry(name, NAME) \
0387 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
0388 rtd->dai_link->symmetric_##name)) { \
0389 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
0390 #name, soc_dai->name); \
0391 \
0392 ret = snd_pcm_hw_constraint_single(substream->runtime, \
0393 SNDRV_PCM_HW_PARAM_##NAME,\
0394 soc_dai->name); \
0395 if (ret < 0) { \
0396 dev_err(soc_dai->dev, \
0397 "ASoC: Unable to apply %s constraint: %d\n",\
0398 #name, ret); \
0399 return ret; \
0400 } \
0401 }
0402
0403 __soc_pcm_apply_symmetry(rate, RATE);
0404 __soc_pcm_apply_symmetry(channels, CHANNELS);
0405 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
0406
0407 return 0;
0408 }
0409
0410 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
0411 struct snd_pcm_hw_params *params)
0412 {
0413 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0414 struct snd_soc_dai d;
0415 struct snd_soc_dai *dai;
0416 struct snd_soc_dai *cpu_dai;
0417 unsigned int symmetry, i;
0418
0419 d.name = __func__;
0420 soc_pcm_set_dai_params(&d, params);
0421
0422 #define __soc_pcm_params_symmetry(xxx) \
0423 symmetry = rtd->dai_link->symmetric_##xxx; \
0424 for_each_rtd_dais(rtd, i, dai) \
0425 symmetry |= dai->driver->symmetric_##xxx; \
0426 \
0427 if (symmetry) \
0428 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
0429 if (!snd_soc_dai_is_dummy(cpu_dai) && \
0430 cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
0431 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
0432 #xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
0433 return -EINVAL; \
0434 }
0435
0436
0437 __soc_pcm_params_symmetry(rate);
0438 __soc_pcm_params_symmetry(channels);
0439 __soc_pcm_params_symmetry(sample_bits);
0440
0441 return 0;
0442 }
0443
0444 static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
0445 {
0446 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0447 struct snd_soc_dai_link *link = rtd->dai_link;
0448 struct snd_soc_dai *dai;
0449 unsigned int symmetry, i;
0450
0451 symmetry = link->symmetric_rate ||
0452 link->symmetric_channels ||
0453 link->symmetric_sample_bits;
0454
0455 for_each_rtd_dais(rtd, i, dai)
0456 symmetry = symmetry ||
0457 dai->driver->symmetric_rate ||
0458 dai->driver->symmetric_channels ||
0459 dai->driver->symmetric_sample_bits;
0460
0461 if (symmetry)
0462 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
0463 }
0464
0465 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
0466 {
0467 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0468 int ret;
0469
0470 if (!bits)
0471 return;
0472
0473 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
0474 if (ret != 0)
0475 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
0476 bits, ret);
0477 }
0478
0479 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
0480 {
0481 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0482 struct snd_soc_dai *cpu_dai;
0483 struct snd_soc_dai *codec_dai;
0484 int stream = substream->stream;
0485 int i;
0486 unsigned int bits = 0, cpu_bits = 0;
0487
0488 for_each_rtd_codec_dais(rtd, i, codec_dai) {
0489 struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
0490
0491 if (pcm_codec->sig_bits == 0) {
0492 bits = 0;
0493 break;
0494 }
0495 bits = max(pcm_codec->sig_bits, bits);
0496 }
0497
0498 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
0499 struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
0500
0501 if (pcm_cpu->sig_bits == 0) {
0502 cpu_bits = 0;
0503 break;
0504 }
0505 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
0506 }
0507
0508 soc_pcm_set_msb(substream, bits);
0509 soc_pcm_set_msb(substream, cpu_bits);
0510 }
0511
0512 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
0513 {
0514 hw->rates = UINT_MAX;
0515 hw->rate_min = 0;
0516 hw->rate_max = UINT_MAX;
0517 hw->channels_min = 0;
0518 hw->channels_max = UINT_MAX;
0519 hw->formats = ULLONG_MAX;
0520 }
0521
0522 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
0523 struct snd_soc_pcm_stream *p)
0524 {
0525 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
0526
0527
0528 snd_pcm_hw_limit_rates(hw);
0529
0530
0531 hw->rate_min = max(hw->rate_min, p->rate_min);
0532 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
0533 }
0534
0535 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
0536 struct snd_soc_pcm_stream *p)
0537 {
0538 hw->channels_min = max(hw->channels_min, p->channels_min);
0539 hw->channels_max = min(hw->channels_max, p->channels_max);
0540 }
0541
0542 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
0543 struct snd_soc_pcm_stream *p)
0544 {
0545 hw->formats &= p->formats;
0546 }
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
0558 struct snd_pcm_hardware *hw, int stream)
0559 {
0560 struct snd_soc_dai *codec_dai;
0561 struct snd_soc_dai *cpu_dai;
0562 struct snd_soc_pcm_stream *codec_stream;
0563 struct snd_soc_pcm_stream *cpu_stream;
0564 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
0565 int i;
0566
0567 soc_pcm_hw_init(hw);
0568
0569
0570 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
0571
0572
0573
0574
0575
0576
0577
0578 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
0579 continue;
0580
0581 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
0582
0583 soc_pcm_hw_update_chan(hw, cpu_stream);
0584 soc_pcm_hw_update_rate(hw, cpu_stream);
0585 soc_pcm_hw_update_format(hw, cpu_stream);
0586 }
0587 cpu_chan_min = hw->channels_min;
0588 cpu_chan_max = hw->channels_max;
0589
0590
0591 for_each_rtd_codec_dais(rtd, i, codec_dai) {
0592
0593
0594
0595
0596
0597
0598
0599 if (!snd_soc_dai_stream_valid(codec_dai, stream))
0600 continue;
0601
0602 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
0603
0604 soc_pcm_hw_update_chan(hw, codec_stream);
0605 soc_pcm_hw_update_rate(hw, codec_stream);
0606 soc_pcm_hw_update_format(hw, codec_stream);
0607 }
0608
0609
0610 if (!hw->channels_min)
0611 return -EINVAL;
0612
0613
0614
0615
0616
0617
0618 if (rtd->num_codecs > 1) {
0619 hw->channels_min = cpu_chan_min;
0620 hw->channels_max = cpu_chan_max;
0621 }
0622
0623 return 0;
0624 }
0625 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
0626
0627 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
0628 {
0629 struct snd_pcm_hardware *hw = &substream->runtime->hw;
0630 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0631 u64 formats = hw->formats;
0632
0633
0634
0635
0636
0637
0638 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
0639
0640 if (formats)
0641 hw->formats &= formats;
0642 }
0643
0644 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
0645 {
0646 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0647 struct snd_soc_component *component;
0648 int i, ret = 0;
0649
0650 for_each_rtd_components(rtd, i, component) {
0651 ret = snd_soc_component_module_get_when_open(component, substream);
0652 if (ret < 0)
0653 break;
0654
0655 ret = snd_soc_component_open(component, substream);
0656 if (ret < 0)
0657 break;
0658 }
0659
0660 return ret;
0661 }
0662
0663 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
0664 int rollback)
0665 {
0666 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0667 struct snd_soc_component *component;
0668 int i, ret = 0;
0669
0670 for_each_rtd_components(rtd, i, component) {
0671 int r = snd_soc_component_close(component, substream, rollback);
0672 if (r < 0)
0673 ret = r;
0674
0675 snd_soc_component_module_put_when_close(component, substream, rollback);
0676 }
0677
0678 return ret;
0679 }
0680
0681 static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
0682 struct snd_pcm_substream *substream, int rollback)
0683 {
0684 struct snd_soc_component *component;
0685 struct snd_soc_dai *dai;
0686 int i;
0687
0688 snd_soc_dpcm_mutex_assert_held(rtd);
0689
0690 if (!rollback)
0691 snd_soc_runtime_deactivate(rtd, substream->stream);
0692
0693 for_each_rtd_dais(rtd, i, dai)
0694 snd_soc_dai_shutdown(dai, substream, rollback);
0695
0696 snd_soc_link_shutdown(substream, rollback);
0697
0698 soc_pcm_components_close(substream, rollback);
0699
0700 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
0701
0702 for_each_rtd_components(rtd, i, component)
0703 if (!snd_soc_component_active(component))
0704 pinctrl_pm_select_sleep_state(component->dev);
0705
0706 return 0;
0707 }
0708
0709
0710
0711
0712
0713
0714 static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
0715 struct snd_pcm_substream *substream)
0716 {
0717 return soc_pcm_clean(rtd, substream, 0);
0718 }
0719
0720
0721 static int soc_pcm_close(struct snd_pcm_substream *substream)
0722 {
0723 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0724
0725 snd_soc_dpcm_mutex_lock(rtd);
0726 soc_pcm_clean(rtd, substream, 0);
0727 snd_soc_dpcm_mutex_unlock(rtd);
0728 return 0;
0729 }
0730
0731 static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
0732 {
0733 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0734 struct snd_pcm_hardware *hw = &substream->runtime->hw;
0735 const char *name_cpu = soc_cpu_dai_name(rtd);
0736 const char *name_codec = soc_codec_dai_name(rtd);
0737 const char *err_msg;
0738 struct device *dev = rtd->dev;
0739
0740 err_msg = "rates";
0741 if (!hw->rates)
0742 goto config_err;
0743
0744 err_msg = "formats";
0745 if (!hw->formats)
0746 goto config_err;
0747
0748 err_msg = "channels";
0749 if (!hw->channels_min || !hw->channels_max ||
0750 hw->channels_min > hw->channels_max)
0751 goto config_err;
0752
0753 dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
0754 name_cpu);
0755 dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
0756 dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
0757 hw->channels_max);
0758 dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
0759 hw->rate_max);
0760
0761 return 0;
0762
0763 config_err:
0764 dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
0765 name_codec, name_cpu, err_msg);
0766 return -EINVAL;
0767 }
0768
0769
0770
0771
0772
0773
0774 static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
0775 struct snd_pcm_substream *substream)
0776 {
0777 struct snd_soc_component *component;
0778 struct snd_soc_dai *dai;
0779 int i, ret = 0;
0780
0781 snd_soc_dpcm_mutex_assert_held(rtd);
0782
0783 for_each_rtd_components(rtd, i, component)
0784 pinctrl_pm_select_default_state(component->dev);
0785
0786 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
0787 if (ret < 0)
0788 goto err;
0789
0790 ret = soc_pcm_components_open(substream);
0791 if (ret < 0)
0792 goto err;
0793
0794 ret = snd_soc_link_startup(substream);
0795 if (ret < 0)
0796 goto err;
0797
0798
0799 for_each_rtd_dais(rtd, i, dai) {
0800 ret = snd_soc_dai_startup(dai, substream);
0801 if (ret < 0)
0802 goto err;
0803
0804 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0805 dai->tx_mask = 0;
0806 else
0807 dai->rx_mask = 0;
0808 }
0809
0810
0811 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
0812 goto dynamic;
0813
0814
0815 soc_pcm_init_runtime_hw(substream);
0816
0817 soc_pcm_update_symmetry(substream);
0818
0819 ret = soc_hw_sanity_check(substream);
0820 if (ret < 0)
0821 goto err;
0822
0823 soc_pcm_apply_msb(substream);
0824
0825
0826 for_each_rtd_dais(rtd, i, dai) {
0827 ret = soc_pcm_apply_symmetry(substream, dai);
0828 if (ret != 0)
0829 goto err;
0830 }
0831 dynamic:
0832 snd_soc_runtime_activate(rtd, substream->stream);
0833 ret = 0;
0834 err:
0835 if (ret < 0) {
0836 soc_pcm_clean(rtd, substream, 1);
0837 dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
0838 }
0839
0840 return ret;
0841 }
0842
0843
0844 static int soc_pcm_open(struct snd_pcm_substream *substream)
0845 {
0846 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0847 int ret;
0848
0849 snd_soc_dpcm_mutex_lock(rtd);
0850 ret = __soc_pcm_open(rtd, substream);
0851 snd_soc_dpcm_mutex_unlock(rtd);
0852 return ret;
0853 }
0854
0855 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
0856 {
0857
0858
0859
0860
0861
0862
0863 }
0864
0865
0866
0867
0868
0869
0870 static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
0871 struct snd_pcm_substream *substream)
0872 {
0873 struct snd_soc_dai *dai;
0874 int i, ret = 0;
0875
0876 snd_soc_dpcm_mutex_assert_held(rtd);
0877
0878 ret = snd_soc_link_prepare(substream);
0879 if (ret < 0)
0880 goto out;
0881
0882 ret = snd_soc_pcm_component_prepare(substream);
0883 if (ret < 0)
0884 goto out;
0885
0886 ret = snd_soc_pcm_dai_prepare(substream);
0887 if (ret < 0)
0888 goto out;
0889
0890
0891 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
0892 rtd->pop_wait) {
0893 rtd->pop_wait = 0;
0894 cancel_delayed_work(&rtd->delayed_work);
0895 }
0896
0897 snd_soc_dapm_stream_event(rtd, substream->stream,
0898 SND_SOC_DAPM_STREAM_START);
0899
0900 for_each_rtd_dais(rtd, i, dai)
0901 snd_soc_dai_digital_mute(dai, 0, substream->stream);
0902
0903 out:
0904 if (ret < 0)
0905 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
0906
0907 return ret;
0908 }
0909
0910
0911 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
0912 {
0913 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0914 int ret;
0915
0916 snd_soc_dpcm_mutex_lock(rtd);
0917 ret = __soc_pcm_prepare(rtd, substream);
0918 snd_soc_dpcm_mutex_unlock(rtd);
0919 return ret;
0920 }
0921
0922 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
0923 unsigned int mask)
0924 {
0925 struct snd_interval *interval;
0926 int channels = hweight_long(mask);
0927
0928 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
0929 interval->min = channels;
0930 interval->max = channels;
0931 }
0932
0933 static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
0934 struct snd_pcm_substream *substream, int rollback)
0935 {
0936 struct snd_soc_dai *dai;
0937 int i;
0938
0939 snd_soc_dpcm_mutex_assert_held(rtd);
0940
0941
0942 for_each_rtd_dais(rtd, i, dai) {
0943 if (snd_soc_dai_active(dai) == 1)
0944 soc_pcm_set_dai_params(dai, NULL);
0945
0946 if (snd_soc_dai_stream_active(dai, substream->stream) == 1)
0947 snd_soc_dai_digital_mute(dai, 1, substream->stream);
0948 }
0949
0950
0951 snd_soc_dapm_stream_stop(rtd, substream->stream);
0952
0953
0954 snd_soc_link_hw_free(substream, rollback);
0955
0956
0957 snd_soc_pcm_component_hw_free(substream, rollback);
0958
0959
0960 for_each_rtd_dais(rtd, i, dai)
0961 if (snd_soc_dai_stream_valid(dai, substream->stream))
0962 snd_soc_dai_hw_free(dai, substream, rollback);
0963
0964 return 0;
0965 }
0966
0967
0968
0969
0970 static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
0971 struct snd_pcm_substream *substream)
0972 {
0973 return soc_pcm_hw_clean(rtd, substream, 0);
0974 }
0975
0976
0977 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
0978 {
0979 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0980 int ret;
0981
0982 snd_soc_dpcm_mutex_lock(rtd);
0983 ret = __soc_pcm_hw_free(rtd, substream);
0984 snd_soc_dpcm_mutex_unlock(rtd);
0985 return ret;
0986 }
0987
0988
0989
0990
0991
0992
0993 static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd,
0994 struct snd_pcm_substream *substream,
0995 struct snd_pcm_hw_params *params)
0996 {
0997 struct snd_soc_dai *cpu_dai;
0998 struct snd_soc_dai *codec_dai;
0999 int i, ret = 0;
1000
1001 snd_soc_dpcm_mutex_assert_held(rtd);
1002
1003 ret = soc_pcm_params_symmetry(substream, params);
1004 if (ret)
1005 goto out;
1006
1007 ret = snd_soc_link_hw_params(substream, params);
1008 if (ret < 0)
1009 goto out;
1010
1011 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1012 struct snd_pcm_hw_params codec_params;
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1029 continue;
1030
1031
1032 codec_params = *params;
1033
1034
1035 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1036 codec_dai->tx_mask)
1037 soc_pcm_codec_params_fixup(&codec_params,
1038 codec_dai->tx_mask);
1039
1040 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1041 codec_dai->rx_mask)
1042 soc_pcm_codec_params_fixup(&codec_params,
1043 codec_dai->rx_mask);
1044
1045 ret = snd_soc_dai_hw_params(codec_dai, substream,
1046 &codec_params);
1047 if(ret < 0)
1048 goto out;
1049
1050 soc_pcm_set_dai_params(codec_dai, &codec_params);
1051 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1052 }
1053
1054 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1055
1056
1057
1058
1059 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1060 continue;
1061
1062 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
1063 if (ret < 0)
1064 goto out;
1065
1066
1067 soc_pcm_set_dai_params(cpu_dai, params);
1068 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1069 }
1070
1071 ret = snd_soc_pcm_component_hw_params(substream, params);
1072 out:
1073 if (ret < 0) {
1074 soc_pcm_hw_clean(rtd, substream, 1);
1075 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1076 }
1077
1078 return ret;
1079 }
1080
1081
1082 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1083 struct snd_pcm_hw_params *params)
1084 {
1085 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1086 int ret;
1087
1088 snd_soc_dpcm_mutex_lock(rtd);
1089 ret = __soc_pcm_hw_params(rtd, substream, params);
1090 snd_soc_dpcm_mutex_unlock(rtd);
1091 return ret;
1092 }
1093
1094 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1095 {
1096 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1097 int ret = -EINVAL, _ret = 0;
1098 int rollback = 0;
1099
1100 switch (cmd) {
1101 case SNDRV_PCM_TRIGGER_START:
1102 case SNDRV_PCM_TRIGGER_RESUME:
1103 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1104 ret = snd_soc_link_trigger(substream, cmd, 0);
1105 if (ret < 0)
1106 goto start_err;
1107
1108 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1109 if (ret < 0)
1110 goto start_err;
1111
1112 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1113 start_err:
1114 if (ret < 0)
1115 rollback = 1;
1116 }
1117
1118 if (rollback) {
1119 _ret = ret;
1120 switch (cmd) {
1121 case SNDRV_PCM_TRIGGER_START:
1122 cmd = SNDRV_PCM_TRIGGER_STOP;
1123 break;
1124 case SNDRV_PCM_TRIGGER_RESUME:
1125 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1126 break;
1127 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1128 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1129 break;
1130 }
1131 }
1132
1133 switch (cmd) {
1134 case SNDRV_PCM_TRIGGER_STOP:
1135 case SNDRV_PCM_TRIGGER_SUSPEND:
1136 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1137 if (rtd->dai_link->stop_dma_first) {
1138 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1139 if (ret < 0)
1140 break;
1141
1142 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1143 if (ret < 0)
1144 break;
1145 } else {
1146 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1147 if (ret < 0)
1148 break;
1149
1150 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1151 if (ret < 0)
1152 break;
1153 }
1154 ret = snd_soc_link_trigger(substream, cmd, rollback);
1155 break;
1156 }
1157
1158 if (_ret)
1159 ret = _ret;
1160
1161 return ret;
1162 }
1163
1164
1165
1166
1167
1168
1169 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1170 {
1171 struct snd_pcm_runtime *runtime = substream->runtime;
1172 snd_pcm_uframes_t offset = 0;
1173 snd_pcm_sframes_t codec_delay = 0;
1174 snd_pcm_sframes_t cpu_delay = 0;
1175
1176 offset = snd_soc_pcm_component_pointer(substream);
1177
1178
1179 snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1180 snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1181
1182 runtime->delay = cpu_delay + codec_delay;
1183
1184 return offset;
1185 }
1186
1187
1188 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1189 struct snd_soc_pcm_runtime *be, int stream)
1190 {
1191 struct snd_pcm_substream *fe_substream;
1192 struct snd_pcm_substream *be_substream;
1193 struct snd_soc_dpcm *dpcm;
1194
1195 snd_soc_dpcm_mutex_assert_held(fe);
1196
1197
1198 for_each_dpcm_be(fe, stream, dpcm) {
1199 if (dpcm->be == be && dpcm->fe == fe)
1200 return 0;
1201 }
1202
1203 fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1204 be_substream = snd_soc_dpcm_get_substream(be, stream);
1205
1206 if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic) {
1207 dev_err(be->dev, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1208 __func__);
1209 return -EINVAL;
1210 }
1211 if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
1212 dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1213 be_substream->pcm->nonatomic = 1;
1214 }
1215
1216 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1217 if (!dpcm)
1218 return -ENOMEM;
1219
1220 dpcm->be = be;
1221 dpcm->fe = fe;
1222 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1223 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1224 snd_soc_dpcm_stream_lock_irq(fe, stream);
1225 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1226 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1227 snd_soc_dpcm_stream_unlock_irq(fe, stream);
1228
1229 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1230 stream ? "capture" : "playback", fe->dai_link->name,
1231 stream ? "<-" : "->", be->dai_link->name);
1232
1233 dpcm_create_debugfs_state(dpcm, stream);
1234
1235 return 1;
1236 }
1237
1238
1239 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1240 struct snd_soc_pcm_runtime *be, int stream)
1241 {
1242 struct snd_soc_dpcm *dpcm;
1243 struct snd_pcm_substream *fe_substream, *be_substream;
1244
1245
1246 if (!be->dpcm[stream].users)
1247 return;
1248
1249 be_substream = snd_soc_dpcm_get_substream(be, stream);
1250
1251 for_each_dpcm_fe(be, stream, dpcm) {
1252 if (dpcm->fe == fe)
1253 continue;
1254
1255 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1256 stream ? "capture" : "playback",
1257 dpcm->fe->dai_link->name,
1258 stream ? "<-" : "->", dpcm->be->dai_link->name);
1259
1260 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1261 be_substream->runtime = fe_substream->runtime;
1262 break;
1263 }
1264 }
1265
1266
1267 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1268 {
1269 struct snd_soc_dpcm *dpcm, *d;
1270 LIST_HEAD(deleted_dpcms);
1271
1272 snd_soc_dpcm_mutex_assert_held(fe);
1273
1274 snd_soc_dpcm_stream_lock_irq(fe, stream);
1275 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1276 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1277 stream ? "capture" : "playback",
1278 dpcm->be->dai_link->name);
1279
1280 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1281 continue;
1282
1283 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1284 stream ? "capture" : "playback", fe->dai_link->name,
1285 stream ? "<-" : "->", dpcm->be->dai_link->name);
1286
1287
1288 dpcm_be_reparent(fe, dpcm->be, stream);
1289
1290 list_del(&dpcm->list_be);
1291 list_move(&dpcm->list_fe, &deleted_dpcms);
1292 }
1293 snd_soc_dpcm_stream_unlock_irq(fe, stream);
1294
1295 while (!list_empty(&deleted_dpcms)) {
1296 dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
1297 list_fe);
1298 list_del(&dpcm->list_fe);
1299 dpcm_remove_debugfs_state(dpcm);
1300 kfree(dpcm);
1301 }
1302 }
1303
1304
1305 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1306 struct snd_soc_dapm_widget *widget, int stream)
1307 {
1308 struct snd_soc_pcm_runtime *be;
1309 struct snd_soc_dapm_widget *w;
1310 struct snd_soc_dai *dai;
1311 int i;
1312
1313 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1314
1315 for_each_card_rtds(card, be) {
1316
1317 if (!be->dai_link->no_pcm)
1318 continue;
1319
1320 if (!snd_soc_dpcm_get_substream(be, stream))
1321 continue;
1322
1323 for_each_rtd_dais(be, i, dai) {
1324 w = snd_soc_dai_get_widget(dai, stream);
1325
1326 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1327 w ? w->name : "(not set)");
1328
1329 if (w == widget)
1330 return be;
1331 }
1332 }
1333
1334
1335 return NULL;
1336 }
1337
1338 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1339 struct snd_soc_dapm_widget *widget)
1340 {
1341 struct snd_soc_dapm_widget *w;
1342 int i;
1343
1344 for_each_dapm_widgets(list, i, w)
1345 if (widget == w)
1346 return 1;
1347
1348 return 0;
1349 }
1350
1351 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
1352 {
1353 struct snd_soc_card *card = widget->dapm->card;
1354 struct snd_soc_pcm_runtime *rtd;
1355 int stream;
1356
1357
1358 if (dir == SND_SOC_DAPM_DIR_OUT)
1359 stream = SNDRV_PCM_STREAM_PLAYBACK;
1360 else
1361 stream = SNDRV_PCM_STREAM_CAPTURE;
1362
1363 rtd = dpcm_get_be(card, widget, stream);
1364 if (rtd)
1365 return true;
1366
1367 return false;
1368 }
1369 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
1370
1371 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1372 int stream, struct snd_soc_dapm_widget_list **list)
1373 {
1374 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1375 int paths;
1376
1377 if (fe->num_cpus > 1) {
1378 dev_err(fe->dev,
1379 "%s doesn't support Multi CPU yet\n", __func__);
1380 return -EINVAL;
1381 }
1382
1383
1384 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1385 fe->card->component_chaining ?
1386 NULL : dpcm_end_walk_at_be);
1387
1388 if (paths > 0)
1389 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1390 stream ? "capture" : "playback");
1391 else if (paths == 0)
1392 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1393 stream ? "capture" : "playback");
1394
1395 return paths;
1396 }
1397
1398 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1399 {
1400 snd_soc_dapm_dai_free_widgets(list);
1401 }
1402
1403 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1404 struct snd_soc_dapm_widget_list *list)
1405 {
1406 struct snd_soc_dai *dai;
1407 unsigned int i;
1408
1409
1410 for_each_rtd_dais(dpcm->be, i, dai) {
1411 struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
1412
1413
1414
1415
1416
1417 if (widget && widget_in_list(list, widget))
1418 return true;
1419 }
1420
1421 return false;
1422 }
1423
1424 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1425 struct snd_soc_dapm_widget_list **list_)
1426 {
1427 struct snd_soc_dpcm *dpcm;
1428 int prune = 0;
1429
1430
1431 for_each_dpcm_be(fe, stream, dpcm) {
1432 if (dpcm_be_is_active(dpcm, stream, *list_))
1433 continue;
1434
1435 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1436 stream ? "capture" : "playback",
1437 dpcm->be->dai_link->name, fe->dai_link->name);
1438 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1439 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1440 prune++;
1441 }
1442
1443 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1444 return prune;
1445 }
1446
1447 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1448 struct snd_soc_dapm_widget_list **list_)
1449 {
1450 struct snd_soc_card *card = fe->card;
1451 struct snd_soc_dapm_widget_list *list = *list_;
1452 struct snd_soc_pcm_runtime *be;
1453 struct snd_soc_dapm_widget *widget;
1454 int i, new = 0, err;
1455
1456
1457 for_each_dapm_widgets(list, i, widget) {
1458
1459 switch (widget->id) {
1460 case snd_soc_dapm_dai_in:
1461 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1462 continue;
1463 break;
1464 case snd_soc_dapm_dai_out:
1465 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1466 continue;
1467 break;
1468 default:
1469 continue;
1470 }
1471
1472
1473 be = dpcm_get_be(card, widget, stream);
1474 if (!be) {
1475 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1476 widget->name);
1477 continue;
1478 }
1479
1480
1481 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1482 continue;
1483
1484
1485
1486
1487
1488
1489 if (fe->card->component_chaining &&
1490 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1491 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1492 continue;
1493
1494
1495 err = dpcm_be_connect(fe, be, stream);
1496 if (err < 0) {
1497 dev_err(fe->dev, "ASoC: can't connect %s\n",
1498 widget->name);
1499 break;
1500 } else if (err == 0)
1501 continue;
1502
1503
1504 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1505 new++;
1506 }
1507
1508 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1509 return new;
1510 }
1511
1512
1513
1514
1515
1516 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1517 int stream, struct snd_soc_dapm_widget_list **list, int new)
1518 {
1519 if (new)
1520 return dpcm_add_paths(fe, stream, list);
1521 else
1522 return dpcm_prune_paths(fe, stream, list);
1523 }
1524
1525 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1526 {
1527 struct snd_soc_dpcm *dpcm;
1528
1529 for_each_dpcm_be(fe, stream, dpcm)
1530 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1531 }
1532
1533 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1534 int do_hw_free, struct snd_soc_dpcm *last)
1535 {
1536 struct snd_soc_dpcm *dpcm;
1537
1538
1539 for_each_dpcm_be(fe, stream, dpcm) {
1540 struct snd_soc_pcm_runtime *be = dpcm->be;
1541 struct snd_pcm_substream *be_substream =
1542 snd_soc_dpcm_get_substream(be, stream);
1543
1544 if (dpcm == last)
1545 return;
1546
1547
1548 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1549 continue;
1550
1551 if (be->dpcm[stream].users == 0) {
1552 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1553 stream ? "capture" : "playback",
1554 be->dpcm[stream].state);
1555 continue;
1556 }
1557
1558 if (--be->dpcm[stream].users != 0)
1559 continue;
1560
1561 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1562 if (!do_hw_free)
1563 continue;
1564
1565 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1566 __soc_pcm_hw_free(be, be_substream);
1567 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1568 }
1569 }
1570
1571 __soc_pcm_close(be, be_substream);
1572 be_substream->runtime = NULL;
1573 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1574 }
1575 }
1576
1577 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1578 {
1579 struct snd_soc_pcm_runtime *be;
1580 struct snd_soc_dpcm *dpcm;
1581 int err, count = 0;
1582
1583
1584 for_each_dpcm_be(fe, stream, dpcm) {
1585 struct snd_pcm_substream *be_substream;
1586
1587 be = dpcm->be;
1588 be_substream = snd_soc_dpcm_get_substream(be, stream);
1589
1590 if (!be_substream) {
1591 dev_err(be->dev, "ASoC: no backend %s stream\n",
1592 stream ? "capture" : "playback");
1593 continue;
1594 }
1595
1596
1597 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1598 continue;
1599
1600
1601 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1602 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1603 stream ? "capture" : "playback",
1604 be->dpcm[stream].state);
1605 continue;
1606 }
1607
1608 if (be->dpcm[stream].users++ != 0)
1609 continue;
1610
1611 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1612 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1613 continue;
1614
1615 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1616 stream ? "capture" : "playback", be->dai_link->name);
1617
1618 be_substream->runtime = be->dpcm[stream].runtime;
1619 err = __soc_pcm_open(be, be_substream);
1620 if (err < 0) {
1621 be->dpcm[stream].users--;
1622 if (be->dpcm[stream].users < 0)
1623 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1624 stream ? "capture" : "playback",
1625 be->dpcm[stream].state);
1626
1627 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1628 goto unwind;
1629 }
1630 be->dpcm[stream].be_start = 0;
1631 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1632 count++;
1633 }
1634
1635 return count;
1636
1637 unwind:
1638 dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1639
1640 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1641 __func__, be->dai_link->name, err);
1642
1643 return err;
1644 }
1645
1646 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1647 {
1648 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1649 struct snd_pcm_runtime *runtime = substream->runtime;
1650 struct snd_pcm_hardware *hw = &runtime->hw;
1651 struct snd_soc_dai *dai;
1652 int stream = substream->stream;
1653 int i;
1654
1655 soc_pcm_hw_init(hw);
1656
1657 for_each_rtd_cpu_dais(fe, i, dai) {
1658 struct snd_soc_pcm_stream *cpu_stream;
1659
1660
1661
1662
1663
1664 if (!snd_soc_dai_stream_valid(dai, stream))
1665 continue;
1666
1667 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1668
1669 soc_pcm_hw_update_rate(hw, cpu_stream);
1670 soc_pcm_hw_update_chan(hw, cpu_stream);
1671 soc_pcm_hw_update_format(hw, cpu_stream);
1672 }
1673
1674 }
1675
1676 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1677 {
1678 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1679 struct snd_pcm_runtime *runtime = substream->runtime;
1680 struct snd_pcm_hardware *hw = &runtime->hw;
1681 struct snd_soc_dpcm *dpcm;
1682 struct snd_soc_dai *dai;
1683 int stream = substream->stream;
1684
1685 if (!fe->dai_link->dpcm_merged_format)
1686 return;
1687
1688
1689
1690
1691
1692
1693 for_each_dpcm_be(fe, stream, dpcm) {
1694 struct snd_soc_pcm_runtime *be = dpcm->be;
1695 struct snd_soc_pcm_stream *codec_stream;
1696 int i;
1697
1698 for_each_rtd_codec_dais(be, i, dai) {
1699
1700
1701
1702
1703 if (!snd_soc_dai_stream_valid(dai, stream))
1704 continue;
1705
1706 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1707
1708 soc_pcm_hw_update_format(hw, codec_stream);
1709 }
1710 }
1711 }
1712
1713 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1714 {
1715 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1716 struct snd_pcm_runtime *runtime = substream->runtime;
1717 struct snd_pcm_hardware *hw = &runtime->hw;
1718 struct snd_soc_dpcm *dpcm;
1719 int stream = substream->stream;
1720
1721 if (!fe->dai_link->dpcm_merged_chan)
1722 return;
1723
1724
1725
1726
1727
1728
1729 for_each_dpcm_be(fe, stream, dpcm) {
1730 struct snd_soc_pcm_runtime *be = dpcm->be;
1731 struct snd_soc_pcm_stream *cpu_stream;
1732 struct snd_soc_dai *dai;
1733 int i;
1734
1735 for_each_rtd_cpu_dais(be, i, dai) {
1736
1737
1738
1739
1740 if (!snd_soc_dai_stream_valid(dai, stream))
1741 continue;
1742
1743 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1744
1745 soc_pcm_hw_update_chan(hw, cpu_stream);
1746 }
1747
1748
1749
1750
1751
1752 if (be->num_codecs == 1) {
1753 struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
1754 asoc_rtd_to_codec(be, 0), stream);
1755
1756 soc_pcm_hw_update_chan(hw, codec_stream);
1757 }
1758 }
1759 }
1760
1761 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1762 {
1763 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1764 struct snd_pcm_runtime *runtime = substream->runtime;
1765 struct snd_pcm_hardware *hw = &runtime->hw;
1766 struct snd_soc_dpcm *dpcm;
1767 int stream = substream->stream;
1768
1769 if (!fe->dai_link->dpcm_merged_rate)
1770 return;
1771
1772
1773
1774
1775
1776
1777 for_each_dpcm_be(fe, stream, dpcm) {
1778 struct snd_soc_pcm_runtime *be = dpcm->be;
1779 struct snd_soc_pcm_stream *pcm;
1780 struct snd_soc_dai *dai;
1781 int i;
1782
1783 for_each_rtd_dais(be, i, dai) {
1784
1785
1786
1787
1788 if (!snd_soc_dai_stream_valid(dai, stream))
1789 continue;
1790
1791 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1792
1793 soc_pcm_hw_update_rate(hw, pcm);
1794 }
1795 }
1796 }
1797
1798 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1799 int stream)
1800 {
1801 struct snd_soc_dpcm *dpcm;
1802 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1803 struct snd_soc_dai *fe_cpu_dai;
1804 int err = 0;
1805 int i;
1806
1807
1808 soc_pcm_update_symmetry(fe_substream);
1809
1810 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1811
1812 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1813 if (err < 0)
1814 goto error;
1815 }
1816
1817
1818 for_each_dpcm_be(fe, stream, dpcm) {
1819 struct snd_soc_pcm_runtime *be = dpcm->be;
1820 struct snd_pcm_substream *be_substream =
1821 snd_soc_dpcm_get_substream(be, stream);
1822 struct snd_soc_pcm_runtime *rtd;
1823 struct snd_soc_dai *dai;
1824
1825
1826 if (!be_substream)
1827 continue;
1828
1829 rtd = asoc_substream_to_rtd(be_substream);
1830 if (rtd->dai_link->be_hw_params_fixup)
1831 continue;
1832
1833 soc_pcm_update_symmetry(be_substream);
1834
1835
1836 for_each_rtd_dais(rtd, i, dai) {
1837 err = soc_pcm_apply_symmetry(fe_substream, dai);
1838 if (err < 0)
1839 goto error;
1840 }
1841 }
1842 error:
1843 if (err < 0)
1844 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, err);
1845
1846 return err;
1847 }
1848
1849 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1850 {
1851 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1852 int stream = fe_substream->stream, ret = 0;
1853
1854 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1855
1856 ret = dpcm_be_dai_startup(fe, stream);
1857 if (ret < 0)
1858 goto be_err;
1859
1860 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1861
1862
1863 ret = __soc_pcm_open(fe, fe_substream);
1864 if (ret < 0)
1865 goto unwind;
1866
1867 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1868
1869 dpcm_runtime_setup_fe(fe_substream);
1870
1871 dpcm_runtime_setup_be_format(fe_substream);
1872 dpcm_runtime_setup_be_chan(fe_substream);
1873 dpcm_runtime_setup_be_rate(fe_substream);
1874
1875 ret = dpcm_apply_symmetry(fe_substream, stream);
1876
1877 unwind:
1878 if (ret < 0)
1879 dpcm_be_dai_startup_unwind(fe, stream);
1880 be_err:
1881 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1882
1883 if (ret < 0)
1884 dev_err(fe->dev, "%s() failed (%d)\n", __func__, ret);
1885
1886 return ret;
1887 }
1888
1889 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1890 {
1891 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1892 int stream = substream->stream;
1893
1894 snd_soc_dpcm_mutex_assert_held(fe);
1895
1896 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1897
1898
1899 dpcm_be_dai_shutdown(fe, stream);
1900
1901 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1902
1903
1904 __soc_pcm_close(fe, substream);
1905
1906
1907 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1908
1909 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1910 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1911 return 0;
1912 }
1913
1914 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1915 {
1916 struct snd_soc_dpcm *dpcm;
1917
1918
1919
1920 for_each_dpcm_be(fe, stream, dpcm) {
1921
1922 struct snd_soc_pcm_runtime *be = dpcm->be;
1923 struct snd_pcm_substream *be_substream =
1924 snd_soc_dpcm_get_substream(be, stream);
1925
1926
1927 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1928 continue;
1929
1930
1931 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1932 continue;
1933
1934
1935 if (be->dpcm[stream].users > 1)
1936 continue;
1937
1938 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1939 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1940 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1941 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1942 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1943 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1944 continue;
1945
1946 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1947 be->dai_link->name);
1948
1949 __soc_pcm_hw_free(be, be_substream);
1950
1951 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1952 }
1953 }
1954
1955 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1956 {
1957 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1958 int stream = substream->stream;
1959
1960 snd_soc_dpcm_mutex_lock(fe);
1961 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1962
1963 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1964
1965
1966 soc_pcm_hw_clean(fe, substream, 0);
1967
1968
1969
1970 dpcm_be_dai_hw_free(fe, stream);
1971
1972 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1973 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1974
1975 snd_soc_dpcm_mutex_unlock(fe);
1976 return 0;
1977 }
1978
1979 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1980 {
1981 struct snd_soc_pcm_runtime *be;
1982 struct snd_pcm_substream *be_substream;
1983 struct snd_soc_dpcm *dpcm;
1984 int ret;
1985
1986 for_each_dpcm_be(fe, stream, dpcm) {
1987 be = dpcm->be;
1988 be_substream = snd_soc_dpcm_get_substream(be, stream);
1989
1990
1991 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1992 continue;
1993
1994
1995 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1996 sizeof(struct snd_pcm_hw_params));
1997
1998
1999 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
2000 if (ret < 0)
2001 goto unwind;
2002
2003
2004 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2005 sizeof(struct snd_pcm_hw_params));
2006
2007
2008 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2009 continue;
2010
2011 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2012 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2013 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2014 continue;
2015
2016 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2017 be->dai_link->name);
2018
2019 ret = __soc_pcm_hw_params(be, be_substream, &dpcm->hw_params);
2020 if (ret < 0)
2021 goto unwind;
2022
2023 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2024 }
2025 return 0;
2026
2027 unwind:
2028 dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2029 __func__, be->dai_link->name, ret);
2030
2031
2032 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2033 be = dpcm->be;
2034 be_substream = snd_soc_dpcm_get_substream(be, stream);
2035
2036 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2037 continue;
2038
2039
2040 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2041 continue;
2042
2043 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2044 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2045 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2046 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2047 continue;
2048
2049 __soc_pcm_hw_free(be, be_substream);
2050 }
2051
2052 return ret;
2053 }
2054
2055 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2056 struct snd_pcm_hw_params *params)
2057 {
2058 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2059 int ret, stream = substream->stream;
2060
2061 snd_soc_dpcm_mutex_lock(fe);
2062 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2063
2064 memcpy(&fe->dpcm[stream].hw_params, params,
2065 sizeof(struct snd_pcm_hw_params));
2066 ret = dpcm_be_dai_hw_params(fe, stream);
2067 if (ret < 0)
2068 goto out;
2069
2070 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2071 fe->dai_link->name, params_rate(params),
2072 params_channels(params), params_format(params));
2073
2074
2075 ret = __soc_pcm_hw_params(fe, substream, params);
2076 if (ret < 0)
2077 dpcm_be_dai_hw_free(fe, stream);
2078 else
2079 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2080
2081 out:
2082 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2083 snd_soc_dpcm_mutex_unlock(fe);
2084
2085 if (ret < 0)
2086 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, ret);
2087
2088 return ret;
2089 }
2090
2091 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2092 int cmd)
2093 {
2094 struct snd_soc_pcm_runtime *be;
2095 bool pause_stop_transition;
2096 struct snd_soc_dpcm *dpcm;
2097 unsigned long flags;
2098 int ret = 0;
2099
2100 for_each_dpcm_be(fe, stream, dpcm) {
2101 struct snd_pcm_substream *be_substream;
2102
2103 be = dpcm->be;
2104 be_substream = snd_soc_dpcm_get_substream(be, stream);
2105
2106 snd_soc_dpcm_stream_lock_irqsave_nested(be, stream, flags);
2107
2108
2109 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2110 goto next;
2111
2112 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2113 be->dai_link->name, cmd);
2114
2115 switch (cmd) {
2116 case SNDRV_PCM_TRIGGER_START:
2117 if (!be->dpcm[stream].be_start &&
2118 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2119 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2120 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2121 goto next;
2122
2123 be->dpcm[stream].be_start++;
2124 if (be->dpcm[stream].be_start != 1)
2125 goto next;
2126
2127 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED)
2128 ret = soc_pcm_trigger(be_substream,
2129 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
2130 else
2131 ret = soc_pcm_trigger(be_substream,
2132 SNDRV_PCM_TRIGGER_START);
2133 if (ret) {
2134 be->dpcm[stream].be_start--;
2135 goto next;
2136 }
2137
2138 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2139 break;
2140 case SNDRV_PCM_TRIGGER_RESUME:
2141 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2142 goto next;
2143
2144 be->dpcm[stream].be_start++;
2145 if (be->dpcm[stream].be_start != 1)
2146 goto next;
2147
2148 ret = soc_pcm_trigger(be_substream, cmd);
2149 if (ret) {
2150 be->dpcm[stream].be_start--;
2151 goto next;
2152 }
2153
2154 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2155 break;
2156 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2157 if (!be->dpcm[stream].be_start &&
2158 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2159 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2160 goto next;
2161
2162 fe->dpcm[stream].fe_pause = false;
2163 be->dpcm[stream].be_pause--;
2164
2165 be->dpcm[stream].be_start++;
2166 if (be->dpcm[stream].be_start != 1)
2167 goto next;
2168
2169 ret = soc_pcm_trigger(be_substream, cmd);
2170 if (ret) {
2171 be->dpcm[stream].be_start--;
2172 goto next;
2173 }
2174
2175 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2176 break;
2177 case SNDRV_PCM_TRIGGER_STOP:
2178 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2179 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2180 goto next;
2181
2182 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2183 be->dpcm[stream].be_start--;
2184
2185 if (be->dpcm[stream].be_start != 0)
2186 goto next;
2187
2188 pause_stop_transition = false;
2189 if (fe->dpcm[stream].fe_pause) {
2190 pause_stop_transition = true;
2191 fe->dpcm[stream].fe_pause = false;
2192 be->dpcm[stream].be_pause--;
2193 }
2194
2195 if (be->dpcm[stream].be_pause != 0)
2196 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
2197 else
2198 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP);
2199
2200 if (ret) {
2201 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2202 be->dpcm[stream].be_start++;
2203 if (pause_stop_transition) {
2204 fe->dpcm[stream].fe_pause = true;
2205 be->dpcm[stream].be_pause++;
2206 }
2207 goto next;
2208 }
2209
2210 if (be->dpcm[stream].be_pause != 0)
2211 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2212 else
2213 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2214
2215 break;
2216 case SNDRV_PCM_TRIGGER_SUSPEND:
2217 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2218 goto next;
2219
2220 be->dpcm[stream].be_start--;
2221 if (be->dpcm[stream].be_start != 0)
2222 goto next;
2223
2224 ret = soc_pcm_trigger(be_substream, cmd);
2225 if (ret) {
2226 be->dpcm[stream].be_start++;
2227 goto next;
2228 }
2229
2230 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2231 break;
2232 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2233 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2234 goto next;
2235
2236 fe->dpcm[stream].fe_pause = true;
2237 be->dpcm[stream].be_pause++;
2238
2239 be->dpcm[stream].be_start--;
2240 if (be->dpcm[stream].be_start != 0)
2241 goto next;
2242
2243 ret = soc_pcm_trigger(be_substream, cmd);
2244 if (ret) {
2245 be->dpcm[stream].be_start++;
2246 goto next;
2247 }
2248
2249 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2250 break;
2251 }
2252 next:
2253 snd_soc_dpcm_stream_unlock_irqrestore(be, stream, flags);
2254 if (ret)
2255 break;
2256 }
2257 if (ret < 0)
2258 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2259 __func__, be->dai_link->name, ret);
2260 return ret;
2261 }
2262 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2263
2264 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2265 int cmd, bool fe_first)
2266 {
2267 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2268 int ret;
2269
2270
2271 if (fe_first) {
2272 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2273 fe->dai_link->name, cmd);
2274
2275 ret = soc_pcm_trigger(substream, cmd);
2276 if (ret < 0)
2277 return ret;
2278
2279 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2280 return ret;
2281 }
2282
2283
2284 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2285 if (ret < 0)
2286 return ret;
2287
2288 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2289 fe->dai_link->name, cmd);
2290
2291 ret = soc_pcm_trigger(substream, cmd);
2292
2293 return ret;
2294 }
2295
2296 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2297 {
2298 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2299 int stream = substream->stream;
2300 int ret = 0;
2301 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2302
2303 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2304
2305 switch (trigger) {
2306 case SND_SOC_DPCM_TRIGGER_PRE:
2307 switch (cmd) {
2308 case SNDRV_PCM_TRIGGER_START:
2309 case SNDRV_PCM_TRIGGER_RESUME:
2310 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2311 case SNDRV_PCM_TRIGGER_DRAIN:
2312 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2313 break;
2314 case SNDRV_PCM_TRIGGER_STOP:
2315 case SNDRV_PCM_TRIGGER_SUSPEND:
2316 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2317 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2318 break;
2319 default:
2320 ret = -EINVAL;
2321 break;
2322 }
2323 break;
2324 case SND_SOC_DPCM_TRIGGER_POST:
2325 switch (cmd) {
2326 case SNDRV_PCM_TRIGGER_START:
2327 case SNDRV_PCM_TRIGGER_RESUME:
2328 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2329 case SNDRV_PCM_TRIGGER_DRAIN:
2330 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2331 break;
2332 case SNDRV_PCM_TRIGGER_STOP:
2333 case SNDRV_PCM_TRIGGER_SUSPEND:
2334 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2335 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2336 break;
2337 default:
2338 ret = -EINVAL;
2339 break;
2340 }
2341 break;
2342 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2343
2344
2345 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2346 fe->dai_link->name, cmd);
2347
2348 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2349 break;
2350 default:
2351 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2352 fe->dai_link->name);
2353 ret = -EINVAL;
2354 goto out;
2355 }
2356
2357 if (ret < 0) {
2358 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2359 cmd, ret);
2360 goto out;
2361 }
2362
2363 switch (cmd) {
2364 case SNDRV_PCM_TRIGGER_START:
2365 case SNDRV_PCM_TRIGGER_RESUME:
2366 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2367 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2368 break;
2369 case SNDRV_PCM_TRIGGER_STOP:
2370 case SNDRV_PCM_TRIGGER_SUSPEND:
2371 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2372 break;
2373 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2374 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2375 break;
2376 }
2377
2378 out:
2379 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2380 return ret;
2381 }
2382
2383 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2384 {
2385 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2386 int stream = substream->stream;
2387
2388
2389
2390
2391 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2392 fe->dpcm[stream].trigger_pending = cmd + 1;
2393 return 0;
2394 }
2395
2396
2397 return dpcm_fe_dai_do_trigger(substream, cmd);
2398 }
2399
2400 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2401 {
2402 struct snd_soc_dpcm *dpcm;
2403 int ret = 0;
2404
2405 for_each_dpcm_be(fe, stream, dpcm) {
2406
2407 struct snd_soc_pcm_runtime *be = dpcm->be;
2408 struct snd_pcm_substream *be_substream =
2409 snd_soc_dpcm_get_substream(be, stream);
2410
2411
2412 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2413 continue;
2414
2415 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2416 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2417 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2418 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2419 continue;
2420
2421 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2422 be->dai_link->name);
2423
2424 ret = __soc_pcm_prepare(be, be_substream);
2425 if (ret < 0)
2426 break;
2427
2428 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2429 }
2430
2431 if (ret < 0)
2432 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2433
2434 return ret;
2435 }
2436
2437 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2438 {
2439 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2440 int stream = substream->stream, ret = 0;
2441
2442 snd_soc_dpcm_mutex_lock(fe);
2443
2444 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2445
2446 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2447
2448
2449 if (list_empty(&fe->dpcm[stream].be_clients)) {
2450 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2451 fe->dai_link->name);
2452 ret = -EINVAL;
2453 goto out;
2454 }
2455
2456 ret = dpcm_be_dai_prepare(fe, stream);
2457 if (ret < 0)
2458 goto out;
2459
2460
2461 ret = __soc_pcm_prepare(fe, substream);
2462 if (ret < 0)
2463 goto out;
2464
2465 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2466
2467 out:
2468 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2469 snd_soc_dpcm_mutex_unlock(fe);
2470
2471 if (ret < 0)
2472 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2473
2474 return ret;
2475 }
2476
2477 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2478 {
2479 struct snd_pcm_substream *substream =
2480 snd_soc_dpcm_get_substream(fe, stream);
2481 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2482 int err;
2483
2484 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2485 stream ? "capture" : "playback", fe->dai_link->name);
2486
2487 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2488
2489 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2490 fe->dai_link->name);
2491
2492 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2493 } else {
2494 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2495 fe->dai_link->name);
2496
2497 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2498 }
2499
2500 dpcm_be_dai_hw_free(fe, stream);
2501
2502 dpcm_be_dai_shutdown(fe, stream);
2503
2504
2505 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2506
2507 if (err < 0)
2508 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, err);
2509
2510 return err;
2511 }
2512
2513 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2514 {
2515 struct snd_pcm_substream *substream =
2516 snd_soc_dpcm_get_substream(fe, stream);
2517 struct snd_soc_dpcm *dpcm;
2518 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2519 int ret = 0;
2520
2521 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2522 stream ? "capture" : "playback", fe->dai_link->name);
2523
2524
2525 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2526 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2527 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2528 fe->dai_link->name, fe->dpcm[stream].state);
2529 ret = -EINVAL;
2530 goto disconnect;
2531 }
2532
2533
2534 ret = dpcm_be_dai_startup(fe, stream);
2535 if (ret < 0)
2536 goto disconnect;
2537
2538
2539 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2540 return 0;
2541
2542 ret = dpcm_be_dai_hw_params(fe, stream);
2543 if (ret < 0)
2544 goto close;
2545
2546
2547 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2548 return 0;
2549
2550 ret = dpcm_be_dai_prepare(fe, stream);
2551 if (ret < 0)
2552 goto hw_free;
2553
2554
2555 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2556
2557
2558 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2559 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2560 return 0;
2561
2562 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2563
2564 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2565 fe->dai_link->name);
2566
2567 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2568 if (ret < 0)
2569 goto hw_free;
2570 } else {
2571 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2572 fe->dai_link->name);
2573
2574 ret = dpcm_be_dai_trigger(fe, stream,
2575 SNDRV_PCM_TRIGGER_START);
2576 if (ret < 0)
2577 goto hw_free;
2578 }
2579
2580 return 0;
2581
2582 hw_free:
2583 dpcm_be_dai_hw_free(fe, stream);
2584 close:
2585 dpcm_be_dai_shutdown(fe, stream);
2586 disconnect:
2587
2588 for_each_dpcm_be(fe, stream, dpcm) {
2589 struct snd_soc_pcm_runtime *be = dpcm->be;
2590
2591
2592 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2593 continue;
2594
2595 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2596 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2597 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2598 }
2599
2600 if (ret < 0)
2601 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2602
2603 return ret;
2604 }
2605
2606 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2607 {
2608 struct snd_soc_dapm_widget_list *list;
2609 int stream;
2610 int count, paths;
2611
2612 if (!fe->dai_link->dynamic)
2613 return 0;
2614
2615 if (fe->num_cpus > 1) {
2616 dev_err(fe->dev,
2617 "%s doesn't support Multi CPU yet\n", __func__);
2618 return -EINVAL;
2619 }
2620
2621
2622 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2623 return 0;
2624
2625
2626 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2627 new ? "new" : "old", fe->dai_link->name);
2628
2629 for_each_pcm_streams(stream) {
2630
2631
2632 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2633 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2634 continue;
2635
2636
2637 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2638 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2639 continue;
2640
2641 paths = dpcm_path_get(fe, stream, &list);
2642 if (paths < 0)
2643 return paths;
2644
2645
2646 count = dpcm_process_paths(fe, stream, &list, new);
2647 if (count) {
2648 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2649 if (new)
2650 dpcm_run_update_startup(fe, stream);
2651 else
2652 dpcm_run_update_shutdown(fe, stream);
2653 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2654
2655 dpcm_clear_pending_state(fe, stream);
2656 dpcm_be_disconnect(fe, stream);
2657 }
2658
2659 dpcm_path_put(&list);
2660 }
2661
2662 return 0;
2663 }
2664
2665
2666
2667
2668 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2669 {
2670 struct snd_soc_pcm_runtime *fe;
2671 int ret = 0;
2672
2673 mutex_lock_nested(&card->pcm_mutex, card->pcm_subclass);
2674
2675 for_each_card_rtds(card, fe) {
2676 ret = soc_dpcm_fe_runtime_update(fe, 0);
2677 if (ret)
2678 goto out;
2679 }
2680
2681
2682 for_each_card_rtds(card, fe) {
2683 ret = soc_dpcm_fe_runtime_update(fe, 1);
2684 if (ret)
2685 goto out;
2686 }
2687
2688 out:
2689 mutex_unlock(&card->pcm_mutex);
2690 return ret;
2691 }
2692 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2693
2694 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2695 {
2696 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2697 struct snd_soc_dpcm *dpcm;
2698 int stream = fe_substream->stream;
2699
2700 snd_soc_dpcm_mutex_assert_held(fe);
2701
2702
2703 for_each_dpcm_be(fe, stream, dpcm)
2704 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2705
2706 dpcm_be_disconnect(fe, stream);
2707
2708 fe->dpcm[stream].runtime = NULL;
2709 }
2710
2711 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2712 {
2713 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2714 int ret;
2715
2716 snd_soc_dpcm_mutex_lock(fe);
2717 ret = dpcm_fe_dai_shutdown(fe_substream);
2718
2719 dpcm_fe_dai_cleanup(fe_substream);
2720
2721 snd_soc_dpcm_mutex_unlock(fe);
2722 return ret;
2723 }
2724
2725 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2726 {
2727 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2728 struct snd_soc_dapm_widget_list *list;
2729 int ret;
2730 int stream = fe_substream->stream;
2731
2732 snd_soc_dpcm_mutex_lock(fe);
2733 fe->dpcm[stream].runtime = fe_substream->runtime;
2734
2735 ret = dpcm_path_get(fe, stream, &list);
2736 if (ret < 0)
2737 goto open_end;
2738
2739
2740 dpcm_process_paths(fe, stream, &list, 1);
2741
2742 ret = dpcm_fe_dai_startup(fe_substream);
2743 if (ret < 0)
2744 dpcm_fe_dai_cleanup(fe_substream);
2745
2746 dpcm_clear_pending_state(fe, stream);
2747 dpcm_path_put(&list);
2748 open_end:
2749 snd_soc_dpcm_mutex_unlock(fe);
2750 return ret;
2751 }
2752
2753 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2754 int *playback, int *capture)
2755 {
2756 struct snd_soc_dai *cpu_dai;
2757 int i;
2758
2759 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2760 dev_err(rtd->dev,
2761 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2762 return -EINVAL;
2763 }
2764
2765 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2766 int stream;
2767
2768 if (rtd->dai_link->dpcm_playback) {
2769 stream = SNDRV_PCM_STREAM_PLAYBACK;
2770
2771 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2772 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2773 *playback = 1;
2774 break;
2775 }
2776 }
2777 if (!*playback) {
2778 dev_err(rtd->card->dev,
2779 "No CPU DAIs support playback for stream %s\n",
2780 rtd->dai_link->stream_name);
2781 return -EINVAL;
2782 }
2783 }
2784 if (rtd->dai_link->dpcm_capture) {
2785 stream = SNDRV_PCM_STREAM_CAPTURE;
2786
2787 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2788 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2789 *capture = 1;
2790 break;
2791 }
2792 }
2793
2794 if (!*capture) {
2795 dev_err(rtd->card->dev,
2796 "No CPU DAIs support capture for stream %s\n",
2797 rtd->dai_link->stream_name);
2798 return -EINVAL;
2799 }
2800 }
2801 } else {
2802 struct snd_soc_dai *codec_dai;
2803
2804
2805 int cpu_capture = rtd->dai_link->params ?
2806 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2807 int cpu_playback = rtd->dai_link->params ?
2808 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2809
2810 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2811 if (rtd->num_cpus == 1) {
2812 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2813 } else if (rtd->num_cpus == rtd->num_codecs) {
2814 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2815 } else {
2816 dev_err(rtd->card->dev,
2817 "N cpus to M codecs link is not supported yet\n");
2818 return -EINVAL;
2819 }
2820
2821 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2822 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2823 *playback = 1;
2824 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2825 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2826 *capture = 1;
2827 }
2828 }
2829
2830 if (rtd->dai_link->playback_only) {
2831 *playback = 1;
2832 *capture = 0;
2833 }
2834
2835 if (rtd->dai_link->capture_only) {
2836 *playback = 0;
2837 *capture = 1;
2838 }
2839
2840 return 0;
2841 }
2842
2843 static int soc_create_pcm(struct snd_pcm **pcm,
2844 struct snd_soc_pcm_runtime *rtd,
2845 int playback, int capture, int num)
2846 {
2847 char new_name[64];
2848 int ret;
2849
2850
2851 if (rtd->dai_link->params) {
2852 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2853 rtd->dai_link->stream_name);
2854
2855 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2856 playback, capture, pcm);
2857 } else if (rtd->dai_link->no_pcm) {
2858 snprintf(new_name, sizeof(new_name), "(%s)",
2859 rtd->dai_link->stream_name);
2860
2861 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2862 playback, capture, pcm);
2863 } else {
2864 if (rtd->dai_link->dynamic)
2865 snprintf(new_name, sizeof(new_name), "%s (*)",
2866 rtd->dai_link->stream_name);
2867 else
2868 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2869 rtd->dai_link->stream_name,
2870 soc_codec_dai_name(rtd), num);
2871
2872 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2873 capture, pcm);
2874 }
2875 if (ret < 0) {
2876 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2877 new_name, rtd->dai_link->name, ret);
2878 return ret;
2879 }
2880 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2881
2882 return 0;
2883 }
2884
2885
2886 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2887 {
2888 struct snd_soc_component *component;
2889 struct snd_pcm *pcm;
2890 int ret = 0, playback = 0, capture = 0;
2891 int i;
2892
2893 ret = soc_get_playback_capture(rtd, &playback, &capture);
2894 if (ret < 0)
2895 return ret;
2896
2897 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2898 if (ret < 0)
2899 return ret;
2900
2901
2902 if (rtd->dai_link->params)
2903 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2904 else
2905 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2906
2907 rtd->pcm = pcm;
2908 pcm->nonatomic = rtd->dai_link->nonatomic;
2909 pcm->private_data = rtd;
2910
2911 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2912 if (playback)
2913 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2914 if (capture)
2915 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2916 goto out;
2917 }
2918
2919
2920 if (rtd->dai_link->dynamic) {
2921 rtd->ops.open = dpcm_fe_dai_open;
2922 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2923 rtd->ops.prepare = dpcm_fe_dai_prepare;
2924 rtd->ops.trigger = dpcm_fe_dai_trigger;
2925 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2926 rtd->ops.close = dpcm_fe_dai_close;
2927 rtd->ops.pointer = soc_pcm_pointer;
2928 } else {
2929 rtd->ops.open = soc_pcm_open;
2930 rtd->ops.hw_params = soc_pcm_hw_params;
2931 rtd->ops.prepare = soc_pcm_prepare;
2932 rtd->ops.trigger = soc_pcm_trigger;
2933 rtd->ops.hw_free = soc_pcm_hw_free;
2934 rtd->ops.close = soc_pcm_close;
2935 rtd->ops.pointer = soc_pcm_pointer;
2936 }
2937
2938 for_each_rtd_components(rtd, i, component) {
2939 const struct snd_soc_component_driver *drv = component->driver;
2940
2941 if (drv->ioctl)
2942 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2943 if (drv->sync_stop)
2944 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2945 if (drv->copy_user)
2946 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2947 if (drv->page)
2948 rtd->ops.page = snd_soc_pcm_component_page;
2949 if (drv->mmap)
2950 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2951 if (drv->ack)
2952 rtd->ops.ack = snd_soc_pcm_component_ack;
2953 }
2954
2955 if (playback)
2956 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2957
2958 if (capture)
2959 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2960
2961 ret = snd_soc_pcm_component_new(rtd);
2962 if (ret < 0)
2963 return ret;
2964
2965 pcm->no_device_suspend = true;
2966 out:
2967 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2968 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2969 return ret;
2970 }
2971
2972
2973 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2974 {
2975 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2976 return 1;
2977 return 0;
2978 }
2979 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2980
2981
2982 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2983 struct snd_soc_pcm_runtime *be, int stream)
2984 {
2985 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2986 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2987 be->dpcm[stream].runtime_update))
2988 return 1;
2989 return 0;
2990 }
2991 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2992
2993
2994 struct snd_pcm_substream *
2995 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2996 {
2997 return be->pcm->streams[stream].substream;
2998 }
2999 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3000
3001 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3002 struct snd_soc_pcm_runtime *be,
3003 int stream,
3004 const enum snd_soc_dpcm_state *states,
3005 int num_states)
3006 {
3007 struct snd_soc_dpcm *dpcm;
3008 int state;
3009 int ret = 1;
3010 int i;
3011
3012 for_each_dpcm_fe(be, stream, dpcm) {
3013
3014 if (dpcm->fe == fe)
3015 continue;
3016
3017 state = dpcm->fe->dpcm[stream].state;
3018 for (i = 0; i < num_states; i++) {
3019 if (state == states[i]) {
3020 ret = 0;
3021 break;
3022 }
3023 }
3024 }
3025
3026
3027 return ret;
3028 }
3029
3030
3031
3032
3033
3034 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3035 struct snd_soc_pcm_runtime *be, int stream)
3036 {
3037 const enum snd_soc_dpcm_state state[] = {
3038 SND_SOC_DPCM_STATE_START,
3039 SND_SOC_DPCM_STATE_PAUSED,
3040 SND_SOC_DPCM_STATE_SUSPEND,
3041 };
3042
3043 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3044 }
3045 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3046
3047
3048
3049
3050
3051 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3052 struct snd_soc_pcm_runtime *be, int stream)
3053 {
3054 const enum snd_soc_dpcm_state state[] = {
3055 SND_SOC_DPCM_STATE_START,
3056 SND_SOC_DPCM_STATE_PAUSED,
3057 SND_SOC_DPCM_STATE_SUSPEND,
3058 SND_SOC_DPCM_STATE_PREPARE,
3059 };
3060
3061 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3062 }
3063 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);