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