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