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