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