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