]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/soc-pcm.c
037921b0e5d65290d9c3a288a27562be44e0707f
[karo-tx-linux.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/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/export.h>
26 #include <linux/debugfs.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dpcm.h>
32 #include <sound/initval.h>
33
34 #define DPCM_MAX_BE_USERS       8
35
36 /* DPCM stream event, send event to FE and all active BEs. */
37 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
38         int event)
39 {
40         struct snd_soc_dpcm *dpcm;
41
42         list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
43
44                 struct snd_soc_pcm_runtime *be = dpcm->be;
45
46                 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
47                                 be->dai_link->name, event, dir);
48
49                 snd_soc_dapm_stream_event(be, dir, event);
50         }
51
52         snd_soc_dapm_stream_event(fe, dir, event);
53
54         return 0;
55 }
56
57 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
58                                         struct snd_soc_dai *soc_dai)
59 {
60         struct snd_soc_pcm_runtime *rtd = substream->private_data;
61         int ret;
62
63         if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
64                                 rtd->dai_link->symmetric_rates)) {
65                 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
66                                 soc_dai->rate);
67
68                 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
69                                                 SNDRV_PCM_HW_PARAM_RATE,
70                                                 soc_dai->rate, soc_dai->rate);
71                 if (ret < 0) {
72                         dev_err(soc_dai->dev,
73                                 "ASoC: Unable to apply rate constraint: %d\n",
74                                 ret);
75                         return ret;
76                 }
77         }
78
79         if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
80                                 rtd->dai_link->symmetric_channels)) {
81                 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
82                                 soc_dai->channels);
83
84                 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
85                                                 SNDRV_PCM_HW_PARAM_CHANNELS,
86                                                 soc_dai->channels,
87                                                 soc_dai->channels);
88                 if (ret < 0) {
89                         dev_err(soc_dai->dev,
90                                 "ASoC: Unable to apply channel symmetry constraint: %d\n",
91                                 ret);
92                         return ret;
93                 }
94         }
95
96         if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
97                                 rtd->dai_link->symmetric_samplebits)) {
98                 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
99                                 soc_dai->sample_bits);
100
101                 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
102                                                 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
103                                                 soc_dai->sample_bits,
104                                                 soc_dai->sample_bits);
105                 if (ret < 0) {
106                         dev_err(soc_dai->dev,
107                                 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
108                                 ret);
109                         return ret;
110                 }
111         }
112
113         return 0;
114 }
115
116 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
117                                 struct snd_pcm_hw_params *params)
118 {
119         struct snd_soc_pcm_runtime *rtd = substream->private_data;
120         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
121         struct snd_soc_dai *codec_dai = rtd->codec_dai;
122         unsigned int rate, channels, sample_bits, symmetry;
123
124         rate = params_rate(params);
125         channels = params_channels(params);
126         sample_bits = snd_pcm_format_physical_width(params_format(params));
127
128         /* reject unmatched parameters when applying symmetry */
129         symmetry = cpu_dai->driver->symmetric_rates ||
130                 codec_dai->driver->symmetric_rates ||
131                 rtd->dai_link->symmetric_rates;
132         if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
133                 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
134                                 cpu_dai->rate, rate);
135                 return -EINVAL;
136         }
137
138         symmetry = cpu_dai->driver->symmetric_channels ||
139                 codec_dai->driver->symmetric_channels ||
140                 rtd->dai_link->symmetric_channels;
141         if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
142                 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
143                                 cpu_dai->channels, channels);
144                 return -EINVAL;
145         }
146
147         symmetry = cpu_dai->driver->symmetric_samplebits ||
148                 codec_dai->driver->symmetric_samplebits ||
149                 rtd->dai_link->symmetric_samplebits;
150         if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
151                 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
152                                 cpu_dai->sample_bits, sample_bits);
153                 return -EINVAL;
154         }
155
156         return 0;
157 }
158
159 /*
160  * List of sample sizes that might go over the bus for parameter
161  * application.  There ought to be a wildcard sample size for things
162  * like the DAC/ADC resolution to use but there isn't right now.
163  */
164 static int sample_sizes[] = {
165         24, 32,
166 };
167
168 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
169                               struct snd_soc_dai *dai)
170 {
171         int ret, i, bits;
172
173         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
174                 bits = dai->driver->playback.sig_bits;
175         else
176                 bits = dai->driver->capture.sig_bits;
177
178         if (!bits)
179                 return;
180
181         for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
182                 if (bits >= sample_sizes[i])
183                         continue;
184
185                 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
186                                                    sample_sizes[i], bits);
187                 if (ret != 0)
188                         dev_warn(dai->dev,
189                                  "ASoC: Failed to set MSB %d/%d: %d\n",
190                                  bits, sample_sizes[i], ret);
191         }
192 }
193
194 /*
195  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
196  * then initialized and any private data can be allocated. This also calls
197  * startup for the cpu DAI, platform, machine and codec DAI.
198  */
199 static int soc_pcm_open(struct snd_pcm_substream *substream)
200 {
201         struct snd_soc_pcm_runtime *rtd = substream->private_data;
202         struct snd_pcm_runtime *runtime = substream->runtime;
203         struct snd_soc_platform *platform = rtd->platform;
204         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
205         struct snd_soc_dai *codec_dai = rtd->codec_dai;
206         struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
207         struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
208         int ret = 0;
209
210         pm_runtime_get_sync(cpu_dai->dev);
211         pm_runtime_get_sync(codec_dai->dev);
212         pm_runtime_get_sync(platform->dev);
213
214         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
215
216         /* startup the audio subsystem */
217         if (cpu_dai->driver->ops->startup) {
218                 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
219                 if (ret < 0) {
220                         dev_err(cpu_dai->dev, "ASoC: can't open interface"
221                                 " %s: %d\n", cpu_dai->name, ret);
222                         goto out;
223                 }
224         }
225
226         if (platform->driver->ops && platform->driver->ops->open) {
227                 ret = platform->driver->ops->open(substream);
228                 if (ret < 0) {
229                         dev_err(platform->dev, "ASoC: can't open platform"
230                                 " %s: %d\n", platform->name, ret);
231                         goto platform_err;
232                 }
233         }
234
235         if (codec_dai->driver->ops->startup) {
236                 ret = codec_dai->driver->ops->startup(substream, codec_dai);
237                 if (ret < 0) {
238                         dev_err(codec_dai->dev, "ASoC: can't open codec"
239                                 " %s: %d\n", codec_dai->name, ret);
240                         goto codec_dai_err;
241                 }
242         }
243
244         if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
245                 ret = rtd->dai_link->ops->startup(substream);
246                 if (ret < 0) {
247                         pr_err("ASoC: %s startup failed: %d\n",
248                                rtd->dai_link->name, ret);
249                         goto machine_err;
250                 }
251         }
252
253         /* Dynamic PCM DAI links compat checks use dynamic capabilities */
254         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
255                 goto dynamic;
256
257         /* Check that the codec and cpu DAIs are compatible */
258         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
259                 runtime->hw.rate_min =
260                         max(codec_dai_drv->playback.rate_min,
261                             cpu_dai_drv->playback.rate_min);
262                 runtime->hw.rate_max =
263                         min(codec_dai_drv->playback.rate_max,
264                             cpu_dai_drv->playback.rate_max);
265                 runtime->hw.channels_min =
266                         max(codec_dai_drv->playback.channels_min,
267                                 cpu_dai_drv->playback.channels_min);
268                 runtime->hw.channels_max =
269                         min(codec_dai_drv->playback.channels_max,
270                                 cpu_dai_drv->playback.channels_max);
271                 runtime->hw.formats =
272                         codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
273                 runtime->hw.rates =
274                         codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
275                 if (codec_dai_drv->playback.rates
276                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
277                         runtime->hw.rates |= cpu_dai_drv->playback.rates;
278                 if (cpu_dai_drv->playback.rates
279                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
280                         runtime->hw.rates |= codec_dai_drv->playback.rates;
281         } else {
282                 runtime->hw.rate_min =
283                         max(codec_dai_drv->capture.rate_min,
284                             cpu_dai_drv->capture.rate_min);
285                 runtime->hw.rate_max =
286                         min(codec_dai_drv->capture.rate_max,
287                             cpu_dai_drv->capture.rate_max);
288                 runtime->hw.channels_min =
289                         max(codec_dai_drv->capture.channels_min,
290                                 cpu_dai_drv->capture.channels_min);
291                 runtime->hw.channels_max =
292                         min(codec_dai_drv->capture.channels_max,
293                                 cpu_dai_drv->capture.channels_max);
294                 runtime->hw.formats =
295                         codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
296                 runtime->hw.rates =
297                         codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
298                 if (codec_dai_drv->capture.rates
299                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
300                         runtime->hw.rates |= cpu_dai_drv->capture.rates;
301                 if (cpu_dai_drv->capture.rates
302                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
303                         runtime->hw.rates |= codec_dai_drv->capture.rates;
304         }
305
306         ret = -EINVAL;
307         snd_pcm_limit_hw_rates(runtime);
308         if (!runtime->hw.rates) {
309                 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
310                         codec_dai->name, cpu_dai->name);
311                 goto config_err;
312         }
313         if (!runtime->hw.formats) {
314                 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
315                         codec_dai->name, cpu_dai->name);
316                 goto config_err;
317         }
318         if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
319             runtime->hw.channels_min > runtime->hw.channels_max) {
320                 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
321                                 codec_dai->name, cpu_dai->name);
322                 goto config_err;
323         }
324
325         soc_pcm_apply_msb(substream, codec_dai);
326         soc_pcm_apply_msb(substream, cpu_dai);
327
328         /* Symmetry only applies if we've already got an active stream. */
329         if (cpu_dai->active) {
330                 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
331                 if (ret != 0)
332                         goto config_err;
333         }
334
335         if (codec_dai->active) {
336                 ret = soc_pcm_apply_symmetry(substream, codec_dai);
337                 if (ret != 0)
338                         goto config_err;
339         }
340
341         pr_debug("ASoC: %s <-> %s info:\n",
342                         codec_dai->name, cpu_dai->name);
343         pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
344         pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
345                  runtime->hw.channels_max);
346         pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
347                  runtime->hw.rate_max);
348
349 dynamic:
350         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
351                 cpu_dai->playback_active++;
352                 codec_dai->playback_active++;
353         } else {
354                 cpu_dai->capture_active++;
355                 codec_dai->capture_active++;
356         }
357         cpu_dai->active++;
358         codec_dai->active++;
359         rtd->codec->active++;
360         mutex_unlock(&rtd->pcm_mutex);
361         return 0;
362
363 config_err:
364         if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
365                 rtd->dai_link->ops->shutdown(substream);
366
367 machine_err:
368         if (codec_dai->driver->ops->shutdown)
369                 codec_dai->driver->ops->shutdown(substream, codec_dai);
370
371 codec_dai_err:
372         if (platform->driver->ops && platform->driver->ops->close)
373                 platform->driver->ops->close(substream);
374
375 platform_err:
376         if (cpu_dai->driver->ops->shutdown)
377                 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
378 out:
379         mutex_unlock(&rtd->pcm_mutex);
380
381         pm_runtime_put(platform->dev);
382         pm_runtime_put(codec_dai->dev);
383         pm_runtime_put(cpu_dai->dev);
384
385         return ret;
386 }
387
388 /*
389  * Power down the audio subsystem pmdown_time msecs after close is called.
390  * This is to ensure there are no pops or clicks in between any music tracks
391  * due to DAPM power cycling.
392  */
393 static void close_delayed_work(struct work_struct *work)
394 {
395         struct snd_soc_pcm_runtime *rtd =
396                         container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
397         struct snd_soc_dai *codec_dai = rtd->codec_dai;
398
399         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
400
401         dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
402                  codec_dai->driver->playback.stream_name,
403                  codec_dai->playback_active ? "active" : "inactive",
404                  rtd->pop_wait ? "yes" : "no");
405
406         /* are we waiting on this codec DAI stream */
407         if (rtd->pop_wait == 1) {
408                 rtd->pop_wait = 0;
409                 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
410                                           SND_SOC_DAPM_STREAM_STOP);
411         }
412
413         mutex_unlock(&rtd->pcm_mutex);
414 }
415
416 /*
417  * Called by ALSA when a PCM substream is closed. Private data can be
418  * freed here. The cpu DAI, codec DAI, machine and platform are also
419  * shutdown.
420  */
421 static int soc_pcm_close(struct snd_pcm_substream *substream)
422 {
423         struct snd_soc_pcm_runtime *rtd = substream->private_data;
424         struct snd_soc_platform *platform = rtd->platform;
425         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
426         struct snd_soc_dai *codec_dai = rtd->codec_dai;
427         struct snd_soc_codec *codec = rtd->codec;
428
429         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
430
431         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
432                 cpu_dai->playback_active--;
433                 codec_dai->playback_active--;
434         } else {
435                 cpu_dai->capture_active--;
436                 codec_dai->capture_active--;
437         }
438
439         cpu_dai->active--;
440         codec_dai->active--;
441         codec->active--;
442
443         /* Muting the DAC suppresses artifacts caused during digital
444          * shutdown, for example from stopping clocks.
445          */
446         snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
447
448         if (cpu_dai->driver->ops->shutdown)
449                 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
450
451         if (codec_dai->driver->ops->shutdown)
452                 codec_dai->driver->ops->shutdown(substream, codec_dai);
453
454         if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
455                 rtd->dai_link->ops->shutdown(substream);
456
457         if (platform->driver->ops && platform->driver->ops->close)
458                 platform->driver->ops->close(substream);
459         cpu_dai->runtime = NULL;
460
461         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
462                 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
463                     rtd->dai_link->ignore_pmdown_time) {
464                         /* powered down playback stream now */
465                         snd_soc_dapm_stream_event(rtd,
466                                                   SNDRV_PCM_STREAM_PLAYBACK,
467                                                   SND_SOC_DAPM_STREAM_STOP);
468                 } else {
469                         /* start delayed pop wq here for playback streams */
470                         rtd->pop_wait = 1;
471                         schedule_delayed_work(&rtd->delayed_work,
472                                 msecs_to_jiffies(rtd->pmdown_time));
473                 }
474         } else {
475                 /* capture streams can be powered down now */
476                 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
477                                           SND_SOC_DAPM_STREAM_STOP);
478         }
479
480         mutex_unlock(&rtd->pcm_mutex);
481
482         pm_runtime_put(platform->dev);
483         pm_runtime_put(codec_dai->dev);
484         pm_runtime_put(cpu_dai->dev);
485
486         return 0;
487 }
488
489 /*
490  * Called by ALSA when the PCM substream is prepared, can set format, sample
491  * rate, etc.  This function is non atomic and can be called multiple times,
492  * it can refer to the runtime info.
493  */
494 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
495 {
496         struct snd_soc_pcm_runtime *rtd = substream->private_data;
497         struct snd_soc_platform *platform = rtd->platform;
498         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
499         struct snd_soc_dai *codec_dai = rtd->codec_dai;
500         int ret = 0;
501
502         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
503
504         if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
505                 ret = rtd->dai_link->ops->prepare(substream);
506                 if (ret < 0) {
507                         dev_err(rtd->card->dev, "ASoC: machine prepare error:"
508                                 " %d\n", ret);
509                         goto out;
510                 }
511         }
512
513         if (platform->driver->ops && platform->driver->ops->prepare) {
514                 ret = platform->driver->ops->prepare(substream);
515                 if (ret < 0) {
516                         dev_err(platform->dev, "ASoC: platform prepare error:"
517                                 " %d\n", ret);
518                         goto out;
519                 }
520         }
521
522         if (codec_dai->driver->ops->prepare) {
523                 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
524                 if (ret < 0) {
525                         dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
526                                 ret);
527                         goto out;
528                 }
529         }
530
531         if (cpu_dai->driver->ops->prepare) {
532                 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
533                 if (ret < 0) {
534                         dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
535                                 ret);
536                         goto out;
537                 }
538         }
539
540         /* cancel any delayed stream shutdown that is pending */
541         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
542             rtd->pop_wait) {
543                 rtd->pop_wait = 0;
544                 cancel_delayed_work(&rtd->delayed_work);
545         }
546
547         snd_soc_dapm_stream_event(rtd, substream->stream,
548                         SND_SOC_DAPM_STREAM_START);
549
550         snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
551
552 out:
553         mutex_unlock(&rtd->pcm_mutex);
554         return ret;
555 }
556
557 /*
558  * Called by ALSA when the hardware params are set by application. This
559  * function can also be called multiple times and can allocate buffers
560  * (using snd_pcm_lib_* ). It's non-atomic.
561  */
562 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
563                                 struct snd_pcm_hw_params *params)
564 {
565         struct snd_soc_pcm_runtime *rtd = substream->private_data;
566         struct snd_soc_platform *platform = rtd->platform;
567         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
568         struct snd_soc_dai *codec_dai = rtd->codec_dai;
569         int ret = 0;
570
571         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
572
573         ret = soc_pcm_params_symmetry(substream, params);
574         if (ret)
575                 goto out;
576
577         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
578                 ret = rtd->dai_link->ops->hw_params(substream, params);
579                 if (ret < 0) {
580                         dev_err(rtd->card->dev, "ASoC: machine hw_params"
581                                 " failed: %d\n", ret);
582                         goto out;
583                 }
584         }
585
586         if (codec_dai->driver->ops->hw_params) {
587                 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
588                 if (ret < 0) {
589                         dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
590                                 " %d\n", codec_dai->name, ret);
591                         goto codec_err;
592                 }
593         }
594
595         if (cpu_dai->driver->ops->hw_params) {
596                 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
597                 if (ret < 0) {
598                         dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
599                                 cpu_dai->name, ret);
600                         goto interface_err;
601                 }
602         }
603
604         if (platform->driver->ops && platform->driver->ops->hw_params) {
605                 ret = platform->driver->ops->hw_params(substream, params);
606                 if (ret < 0) {
607                         dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
608                                platform->name, ret);
609                         goto platform_err;
610                 }
611         }
612
613         /* store the parameters for each DAIs */
614         cpu_dai->rate = params_rate(params);
615         cpu_dai->channels = params_channels(params);
616         cpu_dai->sample_bits =
617                 snd_pcm_format_physical_width(params_format(params));
618
619         codec_dai->rate = params_rate(params);
620         codec_dai->channels = params_channels(params);
621         codec_dai->sample_bits =
622                 snd_pcm_format_physical_width(params_format(params));
623
624 out:
625         mutex_unlock(&rtd->pcm_mutex);
626         return ret;
627
628 platform_err:
629         if (cpu_dai->driver->ops->hw_free)
630                 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
631
632 interface_err:
633         if (codec_dai->driver->ops->hw_free)
634                 codec_dai->driver->ops->hw_free(substream, codec_dai);
635
636 codec_err:
637         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
638                 rtd->dai_link->ops->hw_free(substream);
639
640         mutex_unlock(&rtd->pcm_mutex);
641         return ret;
642 }
643
644 /*
645  * Frees resources allocated by hw_params, can be called multiple times
646  */
647 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
648 {
649         struct snd_soc_pcm_runtime *rtd = substream->private_data;
650         struct snd_soc_platform *platform = rtd->platform;
651         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
652         struct snd_soc_dai *codec_dai = rtd->codec_dai;
653         struct snd_soc_codec *codec = rtd->codec;
654
655         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
656
657         /* clear the corresponding DAIs parameters when going to be inactive */
658         if (cpu_dai->active == 1) {
659                 cpu_dai->rate = 0;
660                 cpu_dai->channels = 0;
661                 cpu_dai->sample_bits = 0;
662         }
663
664         if (codec_dai->active == 1) {
665                 codec_dai->rate = 0;
666                 codec_dai->channels = 0;
667                 codec_dai->sample_bits = 0;
668         }
669
670         /* apply codec digital mute */
671         if (!codec->active)
672                 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
673
674         /* free any machine hw params */
675         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
676                 rtd->dai_link->ops->hw_free(substream);
677
678         /* free any DMA resources */
679         if (platform->driver->ops && platform->driver->ops->hw_free)
680                 platform->driver->ops->hw_free(substream);
681
682         /* now free hw params for the DAIs  */
683         if (codec_dai->driver->ops->hw_free)
684                 codec_dai->driver->ops->hw_free(substream, codec_dai);
685
686         if (cpu_dai->driver->ops->hw_free)
687                 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
688
689         mutex_unlock(&rtd->pcm_mutex);
690         return 0;
691 }
692
693 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
694 {
695         struct snd_soc_pcm_runtime *rtd = substream->private_data;
696         struct snd_soc_platform *platform = rtd->platform;
697         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
698         struct snd_soc_dai *codec_dai = rtd->codec_dai;
699         int ret;
700
701         if (codec_dai->driver->ops->trigger) {
702                 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
703                 if (ret < 0)
704                         return ret;
705         }
706
707         if (platform->driver->ops && platform->driver->ops->trigger) {
708                 ret = platform->driver->ops->trigger(substream, cmd);
709                 if (ret < 0)
710                         return ret;
711         }
712
713         if (cpu_dai->driver->ops->trigger) {
714                 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
715                 if (ret < 0)
716                         return ret;
717         }
718         return 0;
719 }
720
721 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
722                                    int cmd)
723 {
724         struct snd_soc_pcm_runtime *rtd = substream->private_data;
725         struct snd_soc_platform *platform = rtd->platform;
726         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
727         struct snd_soc_dai *codec_dai = rtd->codec_dai;
728         int ret;
729
730         if (codec_dai->driver->ops->bespoke_trigger) {
731                 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
732                 if (ret < 0)
733                         return ret;
734         }
735
736         if (platform->driver->bespoke_trigger) {
737                 ret = platform->driver->bespoke_trigger(substream, cmd);
738                 if (ret < 0)
739                         return ret;
740         }
741
742         if (cpu_dai->driver->ops->bespoke_trigger) {
743                 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
744                 if (ret < 0)
745                         return ret;
746         }
747         return 0;
748 }
749 /*
750  * soc level wrapper for pointer callback
751  * If cpu_dai, codec_dai, platform driver has the delay callback, than
752  * the runtime->delay will be updated accordingly.
753  */
754 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
755 {
756         struct snd_soc_pcm_runtime *rtd = substream->private_data;
757         struct snd_soc_platform *platform = rtd->platform;
758         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
759         struct snd_soc_dai *codec_dai = rtd->codec_dai;
760         struct snd_pcm_runtime *runtime = substream->runtime;
761         snd_pcm_uframes_t offset = 0;
762         snd_pcm_sframes_t delay = 0;
763
764         if (platform->driver->ops && platform->driver->ops->pointer)
765                 offset = platform->driver->ops->pointer(substream);
766
767         if (cpu_dai->driver->ops->delay)
768                 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
769
770         if (codec_dai->driver->ops->delay)
771                 delay += codec_dai->driver->ops->delay(substream, codec_dai);
772
773         if (platform->driver->delay)
774                 delay += platform->driver->delay(substream, codec_dai);
775
776         runtime->delay = delay;
777
778         return offset;
779 }
780
781 /* connect a FE and BE */
782 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
783                 struct snd_soc_pcm_runtime *be, int stream)
784 {
785         struct snd_soc_dpcm *dpcm;
786
787         /* only add new dpcms */
788         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
789                 if (dpcm->be == be && dpcm->fe == fe)
790                         return 0;
791         }
792
793         dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
794         if (!dpcm)
795                 return -ENOMEM;
796
797         dpcm->be = be;
798         dpcm->fe = fe;
799         be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
800         dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
801         list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
802         list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
803
804         dev_dbg(fe->dev, "  connected new DPCM %s path %s %s %s\n",
805                         stream ? "capture" : "playback",  fe->dai_link->name,
806                         stream ? "<-" : "->", be->dai_link->name);
807
808 #ifdef CONFIG_DEBUG_FS
809         dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
810                         fe->debugfs_dpcm_root, &dpcm->state);
811 #endif
812         return 1;
813 }
814
815 /* reparent a BE onto another FE */
816 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
817                         struct snd_soc_pcm_runtime *be, int stream)
818 {
819         struct snd_soc_dpcm *dpcm;
820         struct snd_pcm_substream *fe_substream, *be_substream;
821
822         /* reparent if BE is connected to other FEs */
823         if (!be->dpcm[stream].users)
824                 return;
825
826         be_substream = snd_soc_dpcm_get_substream(be, stream);
827
828         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
829                 if (dpcm->fe == fe)
830                         continue;
831
832                 dev_dbg(fe->dev, "  reparent %s path %s %s %s\n",
833                         stream ? "capture" : "playback",
834                         dpcm->fe->dai_link->name,
835                         stream ? "<-" : "->", dpcm->be->dai_link->name);
836
837                 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
838                 be_substream->runtime = fe_substream->runtime;
839                 break;
840         }
841 }
842
843 /* disconnect a BE and FE */
844 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
845 {
846         struct snd_soc_dpcm *dpcm, *d;
847
848         list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
849                 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
850                                 stream ? "capture" : "playback",
851                                 dpcm->be->dai_link->name);
852
853                 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
854                         continue;
855
856                 dev_dbg(fe->dev, "  freed DSP %s path %s %s %s\n",
857                         stream ? "capture" : "playback", fe->dai_link->name,
858                         stream ? "<-" : "->", dpcm->be->dai_link->name);
859
860                 /* BEs still alive need new FE */
861                 dpcm_be_reparent(fe, dpcm->be, stream);
862
863 #ifdef CONFIG_DEBUG_FS
864                 debugfs_remove(dpcm->debugfs_state);
865 #endif
866                 list_del(&dpcm->list_be);
867                 list_del(&dpcm->list_fe);
868                 kfree(dpcm);
869         }
870 }
871
872 /* get BE for DAI widget and stream */
873 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
874                 struct snd_soc_dapm_widget *widget, int stream)
875 {
876         struct snd_soc_pcm_runtime *be;
877         int i;
878
879         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
880                 for (i = 0; i < card->num_links; i++) {
881                         be = &card->rtd[i];
882
883                         if (!be->dai_link->no_pcm)
884                                 continue;
885
886                         if (be->cpu_dai->playback_widget == widget ||
887                                 be->codec_dai->playback_widget == widget)
888                                 return be;
889                 }
890         } else {
891
892                 for (i = 0; i < card->num_links; i++) {
893                         be = &card->rtd[i];
894
895                         if (!be->dai_link->no_pcm)
896                                 continue;
897
898                         if (be->cpu_dai->capture_widget == widget ||
899                                 be->codec_dai->capture_widget == widget)
900                                 return be;
901                 }
902         }
903
904         dev_dbg(card->dev, "ASoC: can't get %s BE for %s\n",
905                 stream ? "capture" : "playback", widget->name);
906         return NULL;
907 }
908
909 static inline struct snd_soc_dapm_widget *
910         rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
911 {
912         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
913                 return rtd->cpu_dai->playback_widget;
914         else
915                 return rtd->cpu_dai->capture_widget;
916 }
917
918 static inline struct snd_soc_dapm_widget *
919         rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
920 {
921         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
922                 return rtd->codec_dai->playback_widget;
923         else
924                 return rtd->codec_dai->capture_widget;
925 }
926
927 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
928                 struct snd_soc_dapm_widget *widget)
929 {
930         int i;
931
932         for (i = 0; i < list->num_widgets; i++) {
933                 if (widget == list->widgets[i])
934                         return 1;
935         }
936
937         return 0;
938 }
939
940 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
941         int stream, struct snd_soc_dapm_widget_list **list_)
942 {
943         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
944         struct snd_soc_dapm_widget_list *list;
945         int paths;
946
947         list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
948                         sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
949         if (list == NULL)
950                 return -ENOMEM;
951
952         /* get number of valid DAI paths and their widgets */
953         paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
954
955         dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
956                         stream ? "capture" : "playback");
957
958         *list_ = list;
959         return paths;
960 }
961
962 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
963 {
964         kfree(*list);
965 }
966
967 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
968         struct snd_soc_dapm_widget_list **list_)
969 {
970         struct snd_soc_dpcm *dpcm;
971         struct snd_soc_dapm_widget_list *list = *list_;
972         struct snd_soc_dapm_widget *widget;
973         int prune = 0;
974
975         /* Destroy any old FE <--> BE connections */
976         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
977
978                 /* is there a valid CPU DAI widget for this BE */
979                 widget = rtd_get_cpu_widget(dpcm->be, stream);
980
981                 /* prune the BE if it's no longer in our active list */
982                 if (widget && widget_in_list(list, widget))
983                         continue;
984
985                 /* is there a valid CODEC DAI widget for this BE */
986                 widget = rtd_get_codec_widget(dpcm->be, stream);
987
988                 /* prune the BE if it's no longer in our active list */
989                 if (widget && widget_in_list(list, widget))
990                         continue;
991
992                 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
993                         stream ? "capture" : "playback",
994                         dpcm->be->dai_link->name, fe->dai_link->name);
995                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
996                 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
997                 prune++;
998         }
999
1000         dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1001         return prune;
1002 }
1003
1004 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1005         struct snd_soc_dapm_widget_list **list_)
1006 {
1007         struct snd_soc_card *card = fe->card;
1008         struct snd_soc_dapm_widget_list *list = *list_;
1009         struct snd_soc_pcm_runtime *be;
1010         int i, new = 0, err;
1011
1012         /* Create any new FE <--> BE connections */
1013         for (i = 0; i < list->num_widgets; i++) {
1014
1015                 switch (list->widgets[i]->id) {
1016                 case snd_soc_dapm_dai_in:
1017                 case snd_soc_dapm_dai_out:
1018                         break;
1019                 default:
1020                         continue;
1021                 }
1022
1023                 /* is there a valid BE rtd for this widget */
1024                 be = dpcm_get_be(card, list->widgets[i], stream);
1025                 if (!be) {
1026                         dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1027                                         list->widgets[i]->name);
1028                         continue;
1029                 }
1030
1031                 /* make sure BE is a real BE */
1032                 if (!be->dai_link->no_pcm)
1033                         continue;
1034
1035                 /* don't connect if FE is not running */
1036                 if (!fe->dpcm[stream].runtime)
1037                         continue;
1038
1039                 /* newly connected FE and BE */
1040                 err = dpcm_be_connect(fe, be, stream);
1041                 if (err < 0) {
1042                         dev_err(fe->dev, "ASoC: can't connect %s\n",
1043                                 list->widgets[i]->name);
1044                         break;
1045                 } else if (err == 0) /* already connected */
1046                         continue;
1047
1048                 /* new */
1049                 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1050                 new++;
1051         }
1052
1053         dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1054         return new;
1055 }
1056
1057 /*
1058  * Find the corresponding BE DAIs that source or sink audio to this
1059  * FE substream.
1060  */
1061 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1062         int stream, struct snd_soc_dapm_widget_list **list, int new)
1063 {
1064         if (new)
1065                 return dpcm_add_paths(fe, stream, list);
1066         else
1067                 return dpcm_prune_paths(fe, stream, list);
1068 }
1069
1070 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1071 {
1072         struct snd_soc_dpcm *dpcm;
1073
1074         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1075                 dpcm->be->dpcm[stream].runtime_update =
1076                                                 SND_SOC_DPCM_UPDATE_NO;
1077 }
1078
1079 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1080         int stream)
1081 {
1082         struct snd_soc_dpcm *dpcm;
1083
1084         /* disable any enabled and non active backends */
1085         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1086
1087                 struct snd_soc_pcm_runtime *be = dpcm->be;
1088                 struct snd_pcm_substream *be_substream =
1089                         snd_soc_dpcm_get_substream(be, stream);
1090
1091                 if (be->dpcm[stream].users == 0)
1092                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1093                                 stream ? "capture" : "playback",
1094                                 be->dpcm[stream].state);
1095
1096                 if (--be->dpcm[stream].users != 0)
1097                         continue;
1098
1099                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1100                         continue;
1101
1102                 soc_pcm_close(be_substream);
1103                 be_substream->runtime = NULL;
1104                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1105         }
1106 }
1107
1108 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1109 {
1110         struct snd_soc_dpcm *dpcm;
1111         int err, count = 0;
1112
1113         /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1114         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1115
1116                 struct snd_soc_pcm_runtime *be = dpcm->be;
1117                 struct snd_pcm_substream *be_substream =
1118                         snd_soc_dpcm_get_substream(be, stream);
1119
1120                 /* is this op for this BE ? */
1121                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1122                         continue;
1123
1124                 /* first time the dpcm is open ? */
1125                 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1126                         dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1127                                 stream ? "capture" : "playback",
1128                                 be->dpcm[stream].state);
1129
1130                 if (be->dpcm[stream].users++ != 0)
1131                         continue;
1132
1133                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1134                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1135                         continue;
1136
1137                 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
1138
1139                 be_substream->runtime = be->dpcm[stream].runtime;
1140                 err = soc_pcm_open(be_substream);
1141                 if (err < 0) {
1142                         dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1143                         be->dpcm[stream].users--;
1144                         if (be->dpcm[stream].users < 0)
1145                                 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1146                                         stream ? "capture" : "playback",
1147                                         be->dpcm[stream].state);
1148
1149                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1150                         goto unwind;
1151                 }
1152
1153                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1154                 count++;
1155         }
1156
1157         return count;
1158
1159 unwind:
1160         /* disable any enabled and non active backends */
1161         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1162                 struct snd_soc_pcm_runtime *be = dpcm->be;
1163                 struct snd_pcm_substream *be_substream =
1164                         snd_soc_dpcm_get_substream(be, stream);
1165
1166                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1167                         continue;
1168
1169                 if (be->dpcm[stream].users == 0)
1170                         dev_err(be->dev, "ASoC: no users %s at close %d\n",
1171                                 stream ? "capture" : "playback",
1172                                 be->dpcm[stream].state);
1173
1174                 if (--be->dpcm[stream].users != 0)
1175                         continue;
1176
1177                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1178                         continue;
1179
1180                 soc_pcm_close(be_substream);
1181                 be_substream->runtime = NULL;
1182                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1183         }
1184
1185         return err;
1186 }
1187
1188 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1189 {
1190         struct snd_pcm_runtime *runtime = substream->runtime;
1191         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1192         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1193         struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1194
1195         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1196                 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1197                 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1198                 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1199                 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1200                 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1201                 runtime->hw.rates = cpu_dai_drv->playback.rates;
1202         } else {
1203                 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1204                 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1205                 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1206                 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1207                 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1208                 runtime->hw.rates = cpu_dai_drv->capture.rates;
1209         }
1210 }
1211
1212 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1213 {
1214         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1215         struct snd_pcm_runtime *runtime = fe_substream->runtime;
1216         int stream = fe_substream->stream, ret = 0;
1217
1218         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1219
1220         ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1221         if (ret < 0) {
1222                 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1223                 goto be_err;
1224         }
1225
1226         dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1227
1228         /* start the DAI frontend */
1229         ret = soc_pcm_open(fe_substream);
1230         if (ret < 0) {
1231                 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1232                 goto unwind;
1233         }
1234
1235         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1236
1237         dpcm_set_fe_runtime(fe_substream);
1238         snd_pcm_limit_hw_rates(runtime);
1239
1240         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1241         return 0;
1242
1243 unwind:
1244         dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1245 be_err:
1246         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1247         return ret;
1248 }
1249
1250 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1251 {
1252         struct snd_soc_dpcm *dpcm;
1253
1254         /* only shutdown BEs that are either sinks or sources to this FE DAI */
1255         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1256
1257                 struct snd_soc_pcm_runtime *be = dpcm->be;
1258                 struct snd_pcm_substream *be_substream =
1259                         snd_soc_dpcm_get_substream(be, stream);
1260
1261                 /* is this op for this BE ? */
1262                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1263                         continue;
1264
1265                 if (be->dpcm[stream].users == 0)
1266                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1267                                 stream ? "capture" : "playback",
1268                                 be->dpcm[stream].state);
1269
1270                 if (--be->dpcm[stream].users != 0)
1271                         continue;
1272
1273                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1274                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1275                         continue;
1276
1277                 dev_dbg(be->dev, "ASoC: close BE %s\n",
1278                         dpcm->fe->dai_link->name);
1279
1280                 soc_pcm_close(be_substream);
1281                 be_substream->runtime = NULL;
1282
1283                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1284         }
1285         return 0;
1286 }
1287
1288 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1289 {
1290         struct snd_soc_pcm_runtime *fe = substream->private_data;
1291         int stream = substream->stream;
1292
1293         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1294
1295         /* shutdown the BEs */
1296         dpcm_be_dai_shutdown(fe, substream->stream);
1297
1298         dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1299
1300         /* now shutdown the frontend */
1301         soc_pcm_close(substream);
1302
1303         /* run the stream event for each BE */
1304         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1305
1306         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1307         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1308         return 0;
1309 }
1310
1311 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1312 {
1313         struct snd_soc_dpcm *dpcm;
1314
1315         /* only hw_params backends that are either sinks or sources
1316          * to this frontend DAI */
1317         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1318
1319                 struct snd_soc_pcm_runtime *be = dpcm->be;
1320                 struct snd_pcm_substream *be_substream =
1321                         snd_soc_dpcm_get_substream(be, stream);
1322
1323                 /* is this op for this BE ? */
1324                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1325                         continue;
1326
1327                 /* only free hw when no longer used - check all FEs */
1328                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1329                                 continue;
1330
1331                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1332                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1333                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1334                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1335                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1336                         continue;
1337
1338                 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1339                         dpcm->fe->dai_link->name);
1340
1341                 soc_pcm_hw_free(be_substream);
1342
1343                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1344         }
1345
1346         return 0;
1347 }
1348
1349 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1350 {
1351         struct snd_soc_pcm_runtime *fe = substream->private_data;
1352         int err, stream = substream->stream;
1353
1354         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1355         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1356
1357         dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1358
1359         /* call hw_free on the frontend */
1360         err = soc_pcm_hw_free(substream);
1361         if (err < 0)
1362                 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1363                         fe->dai_link->name);
1364
1365         /* only hw_params backends that are either sinks or sources
1366          * to this frontend DAI */
1367         err = dpcm_be_dai_hw_free(fe, stream);
1368
1369         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1370         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1371
1372         mutex_unlock(&fe->card->mutex);
1373         return 0;
1374 }
1375
1376 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1377 {
1378         struct snd_soc_dpcm *dpcm;
1379         int ret;
1380
1381         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1382
1383                 struct snd_soc_pcm_runtime *be = dpcm->be;
1384                 struct snd_pcm_substream *be_substream =
1385                         snd_soc_dpcm_get_substream(be, stream);
1386
1387                 /* is this op for this BE ? */
1388                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1389                         continue;
1390
1391                 /* only allow hw_params() if no connected FEs are running */
1392                 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1393                         continue;
1394
1395                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1396                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1397                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1398                         continue;
1399
1400                 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1401                         dpcm->fe->dai_link->name);
1402
1403                 /* copy params for each dpcm */
1404                 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1405                                 sizeof(struct snd_pcm_hw_params));
1406
1407                 /* perform any hw_params fixups */
1408                 if (be->dai_link->be_hw_params_fixup) {
1409                         ret = be->dai_link->be_hw_params_fixup(be,
1410                                         &dpcm->hw_params);
1411                         if (ret < 0) {
1412                                 dev_err(be->dev,
1413                                         "ASoC: hw_params BE fixup failed %d\n",
1414                                         ret);
1415                                 goto unwind;
1416                         }
1417                 }
1418
1419                 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1420                 if (ret < 0) {
1421                         dev_err(dpcm->be->dev,
1422                                 "ASoC: hw_params BE failed %d\n", ret);
1423                         goto unwind;
1424                 }
1425
1426                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1427         }
1428         return 0;
1429
1430 unwind:
1431         /* disable any enabled and non active backends */
1432         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1433                 struct snd_soc_pcm_runtime *be = dpcm->be;
1434                 struct snd_pcm_substream *be_substream =
1435                         snd_soc_dpcm_get_substream(be, stream);
1436
1437                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1438                         continue;
1439
1440                 /* only allow hw_free() if no connected FEs are running */
1441                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1442                         continue;
1443
1444                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1445                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1446                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1447                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1448                         continue;
1449
1450                 soc_pcm_hw_free(be_substream);
1451         }
1452
1453         return ret;
1454 }
1455
1456 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1457                                  struct snd_pcm_hw_params *params)
1458 {
1459         struct snd_soc_pcm_runtime *fe = substream->private_data;
1460         int ret, stream = substream->stream;
1461
1462         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1463         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1464
1465         memcpy(&fe->dpcm[substream->stream].hw_params, params,
1466                         sizeof(struct snd_pcm_hw_params));
1467         ret = dpcm_be_dai_hw_params(fe, substream->stream);
1468         if (ret < 0) {
1469                 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1470                 goto out;
1471         }
1472
1473         dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1474                         fe->dai_link->name, params_rate(params),
1475                         params_channels(params), params_format(params));
1476
1477         /* call hw_params on the frontend */
1478         ret = soc_pcm_hw_params(substream, params);
1479         if (ret < 0) {
1480                 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1481                 dpcm_be_dai_hw_free(fe, stream);
1482          } else
1483                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1484
1485 out:
1486         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1487         mutex_unlock(&fe->card->mutex);
1488         return ret;
1489 }
1490
1491 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1492                 struct snd_pcm_substream *substream, int cmd)
1493 {
1494         int ret;
1495
1496         dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1497                         dpcm->fe->dai_link->name, cmd);
1498
1499         ret = soc_pcm_trigger(substream, cmd);
1500         if (ret < 0)
1501                 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1502
1503         return ret;
1504 }
1505
1506 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1507                                int cmd)
1508 {
1509         struct snd_soc_dpcm *dpcm;
1510         int ret = 0;
1511
1512         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1513
1514                 struct snd_soc_pcm_runtime *be = dpcm->be;
1515                 struct snd_pcm_substream *be_substream =
1516                         snd_soc_dpcm_get_substream(be, stream);
1517
1518                 /* is this op for this BE ? */
1519                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1520                         continue;
1521
1522                 switch (cmd) {
1523                 case SNDRV_PCM_TRIGGER_START:
1524                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1525                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1526                                 continue;
1527
1528                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1529                         if (ret)
1530                                 return ret;
1531
1532                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1533                         break;
1534                 case SNDRV_PCM_TRIGGER_RESUME:
1535                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1536                                 continue;
1537
1538                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1539                         if (ret)
1540                                 return ret;
1541
1542                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1543                         break;
1544                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1545                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1546                                 continue;
1547
1548                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1549                         if (ret)
1550                                 return ret;
1551
1552                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1553                         break;
1554                 case SNDRV_PCM_TRIGGER_STOP:
1555                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1556                                 continue;
1557
1558                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1559                                 continue;
1560
1561                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1562                         if (ret)
1563                                 return ret;
1564
1565                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1566                         break;
1567                 case SNDRV_PCM_TRIGGER_SUSPEND:
1568                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1569                                 continue;
1570
1571                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1572                                 continue;
1573
1574                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1575                         if (ret)
1576                                 return ret;
1577
1578                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1579                         break;
1580                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1581                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1582                                 continue;
1583
1584                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1585                                 continue;
1586
1587                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1588                         if (ret)
1589                                 return ret;
1590
1591                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1592                         break;
1593                 }
1594         }
1595
1596         return ret;
1597 }
1598 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1599
1600 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1601 {
1602         struct snd_soc_pcm_runtime *fe = substream->private_data;
1603         int stream = substream->stream, ret;
1604         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1605
1606         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1607
1608         switch (trigger) {
1609         case SND_SOC_DPCM_TRIGGER_PRE:
1610                 /* call trigger on the frontend before the backend. */
1611
1612                 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1613                                 fe->dai_link->name, cmd);
1614
1615                 ret = soc_pcm_trigger(substream, cmd);
1616                 if (ret < 0) {
1617                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1618                         goto out;
1619                 }
1620
1621                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1622                 break;
1623         case SND_SOC_DPCM_TRIGGER_POST:
1624                 /* call trigger on the frontend after the backend. */
1625
1626                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1627                 if (ret < 0) {
1628                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1629                         goto out;
1630                 }
1631
1632                 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1633                                 fe->dai_link->name, cmd);
1634
1635                 ret = soc_pcm_trigger(substream, cmd);
1636                 break;
1637         case SND_SOC_DPCM_TRIGGER_BESPOKE:
1638                 /* bespoke trigger() - handles both FE and BEs */
1639
1640                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1641                                 fe->dai_link->name, cmd);
1642
1643                 ret = soc_pcm_bespoke_trigger(substream, cmd);
1644                 if (ret < 0) {
1645                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1646                         goto out;
1647                 }
1648                 break;
1649         default:
1650                 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1651                                 fe->dai_link->name);
1652                 ret = -EINVAL;
1653                 goto out;
1654         }
1655
1656         switch (cmd) {
1657         case SNDRV_PCM_TRIGGER_START:
1658         case SNDRV_PCM_TRIGGER_RESUME:
1659         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1660                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1661                 break;
1662         case SNDRV_PCM_TRIGGER_STOP:
1663         case SNDRV_PCM_TRIGGER_SUSPEND:
1664         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1665                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1666                 break;
1667         }
1668
1669 out:
1670         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1671         return ret;
1672 }
1673
1674 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1675 {
1676         struct snd_soc_dpcm *dpcm;
1677         int ret = 0;
1678
1679         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1680
1681                 struct snd_soc_pcm_runtime *be = dpcm->be;
1682                 struct snd_pcm_substream *be_substream =
1683                         snd_soc_dpcm_get_substream(be, stream);
1684
1685                 /* is this op for this BE ? */
1686                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1687                         continue;
1688
1689                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1690                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1691                         continue;
1692
1693                 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1694                         dpcm->fe->dai_link->name);
1695
1696                 ret = soc_pcm_prepare(be_substream);
1697                 if (ret < 0) {
1698                         dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1699                                 ret);
1700                         break;
1701                 }
1702
1703                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1704         }
1705         return ret;
1706 }
1707
1708 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1709 {
1710         struct snd_soc_pcm_runtime *fe = substream->private_data;
1711         int stream = substream->stream, ret = 0;
1712
1713         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1714
1715         dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1716
1717         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1718
1719         /* there is no point preparing this FE if there are no BEs */
1720         if (list_empty(&fe->dpcm[stream].be_clients)) {
1721                 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1722                                 fe->dai_link->name);
1723                 ret = -EINVAL;
1724                 goto out;
1725         }
1726
1727         ret = dpcm_be_dai_prepare(fe, substream->stream);
1728         if (ret < 0)
1729                 goto out;
1730
1731         /* call prepare on the frontend */
1732         ret = soc_pcm_prepare(substream);
1733         if (ret < 0) {
1734                 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1735                         fe->dai_link->name);
1736                 goto out;
1737         }
1738
1739         /* run the stream event for each BE */
1740         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1741         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1742
1743 out:
1744         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1745         mutex_unlock(&fe->card->mutex);
1746
1747         return ret;
1748 }
1749
1750 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1751                      unsigned int cmd, void *arg)
1752 {
1753         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1754         struct snd_soc_platform *platform = rtd->platform;
1755
1756         if (platform->driver->ops->ioctl)
1757                 return platform->driver->ops->ioctl(substream, cmd, arg);
1758         return snd_pcm_lib_ioctl(substream, cmd, arg);
1759 }
1760
1761 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1762 {
1763         struct snd_pcm_substream *substream =
1764                 snd_soc_dpcm_get_substream(fe, stream);
1765         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1766         int err;
1767
1768         dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1769                         stream ? "capture" : "playback", fe->dai_link->name);
1770
1771         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1772                 /* call bespoke trigger - FE takes care of all BE triggers */
1773                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1774                                 fe->dai_link->name);
1775
1776                 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1777                 if (err < 0)
1778                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1779         } else {
1780                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1781                         fe->dai_link->name);
1782
1783                 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1784                 if (err < 0)
1785                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1786         }
1787
1788         err = dpcm_be_dai_hw_free(fe, stream);
1789         if (err < 0)
1790                 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1791
1792         err = dpcm_be_dai_shutdown(fe, stream);
1793         if (err < 0)
1794                 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1795
1796         /* run the stream event for each BE */
1797         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1798
1799         return 0;
1800 }
1801
1802 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1803 {
1804         struct snd_pcm_substream *substream =
1805                 snd_soc_dpcm_get_substream(fe, stream);
1806         struct snd_soc_dpcm *dpcm;
1807         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1808         int ret;
1809
1810         dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1811                         stream ? "capture" : "playback", fe->dai_link->name);
1812
1813         /* Only start the BE if the FE is ready */
1814         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1815                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1816                 return -EINVAL;
1817
1818         /* startup must always be called for new BEs */
1819         ret = dpcm_be_dai_startup(fe, stream);
1820         if (ret < 0)
1821                 goto disconnect;
1822
1823         /* keep going if FE state is > open */
1824         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1825                 return 0;
1826
1827         ret = dpcm_be_dai_hw_params(fe, stream);
1828         if (ret < 0)
1829                 goto close;
1830
1831         /* keep going if FE state is > hw_params */
1832         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1833                 return 0;
1834
1835
1836         ret = dpcm_be_dai_prepare(fe, stream);
1837         if (ret < 0)
1838                 goto hw_free;
1839
1840         /* run the stream event for each BE */
1841         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1842
1843         /* keep going if FE state is > prepare */
1844         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1845                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1846                 return 0;
1847
1848         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1849                 /* call trigger on the frontend - FE takes care of all BE triggers */
1850                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1851                                 fe->dai_link->name);
1852
1853                 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1854                 if (ret < 0) {
1855                         dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1856                         goto hw_free;
1857                 }
1858         } else {
1859                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1860                         fe->dai_link->name);
1861
1862                 ret = dpcm_be_dai_trigger(fe, stream,
1863                                         SNDRV_PCM_TRIGGER_START);
1864                 if (ret < 0) {
1865                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1866                         goto hw_free;
1867                 }
1868         }
1869
1870         return 0;
1871
1872 hw_free:
1873         dpcm_be_dai_hw_free(fe, stream);
1874 close:
1875         dpcm_be_dai_shutdown(fe, stream);
1876 disconnect:
1877         /* disconnect any non started BEs */
1878         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1879                 struct snd_soc_pcm_runtime *be = dpcm->be;
1880                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1881                                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1882         }
1883
1884         return ret;
1885 }
1886
1887 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1888 {
1889         int ret;
1890
1891         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1892         ret = dpcm_run_update_startup(fe, stream);
1893         if (ret < 0)
1894                 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1895         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1896
1897         return ret;
1898 }
1899
1900 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1901 {
1902         int ret;
1903
1904         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1905         ret = dpcm_run_update_shutdown(fe, stream);
1906         if (ret < 0)
1907                 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1908         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1909
1910         return ret;
1911 }
1912
1913 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1914  * any DAI links.
1915  */
1916 int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1917 {
1918         struct snd_soc_card *card;
1919         int i, old, new, paths;
1920
1921         if (widget->codec)
1922                 card = widget->codec->card;
1923         else if (widget->platform)
1924                 card = widget->platform->card;
1925         else
1926                 return -EINVAL;
1927
1928         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1929         for (i = 0; i < card->num_rtd; i++) {
1930                 struct snd_soc_dapm_widget_list *list;
1931                 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1932
1933                 /* make sure link is FE */
1934                 if (!fe->dai_link->dynamic)
1935                         continue;
1936
1937                 /* only check active links */
1938                 if (!fe->cpu_dai->active)
1939                         continue;
1940
1941                 /* DAPM sync will call this to update DSP paths */
1942                 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1943                         fe->dai_link->name);
1944
1945                 /* skip if FE doesn't have playback capability */
1946                 if (!fe->cpu_dai->driver->playback.channels_min)
1947                         goto capture;
1948
1949                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1950                 if (paths < 0) {
1951                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1952                                         fe->dai_link->name,  "playback");
1953                         mutex_unlock(&card->mutex);
1954                         return paths;
1955                 }
1956
1957                 /* update any new playback paths */
1958                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1959                 if (new) {
1960                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1961                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1962                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1963                 }
1964
1965                 /* update any old playback paths */
1966                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1967                 if (old) {
1968                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1969                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1970                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1971                 }
1972
1973 capture:
1974                 /* skip if FE doesn't have capture capability */
1975                 if (!fe->cpu_dai->driver->capture.channels_min)
1976                         continue;
1977
1978                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1979                 if (paths < 0) {
1980                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1981                                         fe->dai_link->name,  "capture");
1982                         mutex_unlock(&card->mutex);
1983                         return paths;
1984                 }
1985
1986                 /* update any new capture paths */
1987                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1988                 if (new) {
1989                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1990                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1991                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1992                 }
1993
1994                 /* update any old capture paths */
1995                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1996                 if (old) {
1997                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1998                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1999                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2000                 }
2001
2002                 dpcm_path_put(&list);
2003         }
2004
2005         mutex_unlock(&card->mutex);
2006         return 0;
2007 }
2008 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2009 {
2010         struct snd_soc_dpcm *dpcm;
2011         struct list_head *clients =
2012                 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2013
2014         list_for_each_entry(dpcm, clients, list_be) {
2015
2016                 struct snd_soc_pcm_runtime *be = dpcm->be;
2017                 struct snd_soc_dai *dai = be->codec_dai;
2018                 struct snd_soc_dai_driver *drv = dai->driver;
2019
2020                 if (be->dai_link->ignore_suspend)
2021                         continue;
2022
2023                 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
2024
2025                 if (drv->ops->digital_mute && dai->playback_active)
2026                                 drv->ops->digital_mute(dai, mute);
2027         }
2028
2029         return 0;
2030 }
2031
2032 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2033 {
2034         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2035         struct snd_soc_dpcm *dpcm;
2036         struct snd_soc_dapm_widget_list *list;
2037         int ret;
2038         int stream = fe_substream->stream;
2039
2040         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2041         fe->dpcm[stream].runtime = fe_substream->runtime;
2042
2043         if (dpcm_path_get(fe, stream, &list) <= 0) {
2044                 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2045                         fe->dai_link->name, stream ? "capture" : "playback");
2046         }
2047
2048         /* calculate valid and active FE <-> BE dpcms */
2049         dpcm_process_paths(fe, stream, &list, 1);
2050
2051         ret = dpcm_fe_dai_startup(fe_substream);
2052         if (ret < 0) {
2053                 /* clean up all links */
2054                 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2055                         dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2056
2057                 dpcm_be_disconnect(fe, stream);
2058                 fe->dpcm[stream].runtime = NULL;
2059         }
2060
2061         dpcm_clear_pending_state(fe, stream);
2062         dpcm_path_put(&list);
2063         mutex_unlock(&fe->card->mutex);
2064         return ret;
2065 }
2066
2067 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2068 {
2069         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2070         struct snd_soc_dpcm *dpcm;
2071         int stream = fe_substream->stream, ret;
2072
2073         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2074         ret = dpcm_fe_dai_shutdown(fe_substream);
2075
2076         /* mark FE's links ready to prune */
2077         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2078                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2079
2080         dpcm_be_disconnect(fe, stream);
2081
2082         fe->dpcm[stream].runtime = NULL;
2083         mutex_unlock(&fe->card->mutex);
2084         return ret;
2085 }
2086
2087 /* create a new pcm */
2088 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2089 {
2090         struct snd_soc_platform *platform = rtd->platform;
2091         struct snd_soc_dai *codec_dai = rtd->codec_dai;
2092         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2093         struct snd_pcm *pcm;
2094         char new_name[64];
2095         int ret = 0, playback = 0, capture = 0;
2096
2097         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2098                 if (cpu_dai->driver->playback.channels_min)
2099                         playback = 1;
2100                 if (cpu_dai->driver->capture.channels_min)
2101                         capture = 1;
2102         } else {
2103                 if (codec_dai->driver->playback.channels_min &&
2104                     cpu_dai->driver->playback.channels_min)
2105                         playback = 1;
2106                 if (codec_dai->driver->capture.channels_min &&
2107                     cpu_dai->driver->capture.channels_min)
2108                         capture = 1;
2109         }
2110
2111         /* create the PCM */
2112         if (rtd->dai_link->no_pcm) {
2113                 snprintf(new_name, sizeof(new_name), "(%s)",
2114                         rtd->dai_link->stream_name);
2115
2116                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2117                                 playback, capture, &pcm);
2118         } else {
2119                 if (rtd->dai_link->dynamic)
2120                         snprintf(new_name, sizeof(new_name), "%s (*)",
2121                                 rtd->dai_link->stream_name);
2122                 else
2123                         snprintf(new_name, sizeof(new_name), "%s %s-%d",
2124                                 rtd->dai_link->stream_name, codec_dai->name, num);
2125
2126                 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2127                         capture, &pcm);
2128         }
2129         if (ret < 0) {
2130                 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2131                         rtd->dai_link->name);
2132                 return ret;
2133         }
2134         dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2135
2136         /* DAPM dai link stream work */
2137         INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2138
2139         rtd->pcm = pcm;
2140         pcm->private_data = rtd;
2141
2142         if (rtd->dai_link->no_pcm) {
2143                 if (playback)
2144                         pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2145                 if (capture)
2146                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2147                 goto out;
2148         }
2149
2150         /* ASoC PCM operations */
2151         if (rtd->dai_link->dynamic) {
2152                 rtd->ops.open           = dpcm_fe_dai_open;
2153                 rtd->ops.hw_params      = dpcm_fe_dai_hw_params;
2154                 rtd->ops.prepare        = dpcm_fe_dai_prepare;
2155                 rtd->ops.trigger        = dpcm_fe_dai_trigger;
2156                 rtd->ops.hw_free        = dpcm_fe_dai_hw_free;
2157                 rtd->ops.close          = dpcm_fe_dai_close;
2158                 rtd->ops.pointer        = soc_pcm_pointer;
2159                 rtd->ops.ioctl          = soc_pcm_ioctl;
2160         } else {
2161                 rtd->ops.open           = soc_pcm_open;
2162                 rtd->ops.hw_params      = soc_pcm_hw_params;
2163                 rtd->ops.prepare        = soc_pcm_prepare;
2164                 rtd->ops.trigger        = soc_pcm_trigger;
2165                 rtd->ops.hw_free        = soc_pcm_hw_free;
2166                 rtd->ops.close          = soc_pcm_close;
2167                 rtd->ops.pointer        = soc_pcm_pointer;
2168                 rtd->ops.ioctl          = soc_pcm_ioctl;
2169         }
2170
2171         if (platform->driver->ops) {
2172                 rtd->ops.ack            = platform->driver->ops->ack;
2173                 rtd->ops.copy           = platform->driver->ops->copy;
2174                 rtd->ops.silence        = platform->driver->ops->silence;
2175                 rtd->ops.page           = platform->driver->ops->page;
2176                 rtd->ops.mmap           = platform->driver->ops->mmap;
2177         }
2178
2179         if (playback)
2180                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2181
2182         if (capture)
2183                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2184
2185         if (platform->driver->pcm_new) {
2186                 ret = platform->driver->pcm_new(rtd);
2187                 if (ret < 0) {
2188                         dev_err(platform->dev,
2189                                 "ASoC: pcm constructor failed: %d\n",
2190                                 ret);
2191                         return ret;
2192                 }
2193         }
2194
2195         pcm->private_free = platform->driver->pcm_free;
2196 out:
2197         dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
2198                 cpu_dai->name);
2199         return ret;
2200 }
2201
2202 /* is the current PCM operation for this FE ? */
2203 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2204 {
2205         if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2206                 return 1;
2207         return 0;
2208 }
2209 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2210
2211 /* is the current PCM operation for this BE ? */
2212 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2213                 struct snd_soc_pcm_runtime *be, int stream)
2214 {
2215         if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2216            ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2217                   be->dpcm[stream].runtime_update))
2218                 return 1;
2219         return 0;
2220 }
2221 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2222
2223 /* get the substream for this BE */
2224 struct snd_pcm_substream *
2225         snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2226 {
2227         return be->pcm->streams[stream].substream;
2228 }
2229 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2230
2231 /* get the BE runtime state */
2232 enum snd_soc_dpcm_state
2233         snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2234 {
2235         return be->dpcm[stream].state;
2236 }
2237 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2238
2239 /* set the BE runtime state */
2240 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2241                 int stream, enum snd_soc_dpcm_state state)
2242 {
2243         be->dpcm[stream].state = state;
2244 }
2245 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2246
2247 /*
2248  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2249  * are not running, paused or suspended for the specified stream direction.
2250  */
2251 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2252                 struct snd_soc_pcm_runtime *be, int stream)
2253 {
2254         struct snd_soc_dpcm *dpcm;
2255         int state;
2256
2257         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2258
2259                 if (dpcm->fe == fe)
2260                         continue;
2261
2262                 state = dpcm->fe->dpcm[stream].state;
2263                 if (state == SND_SOC_DPCM_STATE_START ||
2264                         state == SND_SOC_DPCM_STATE_PAUSED ||
2265                         state == SND_SOC_DPCM_STATE_SUSPEND)
2266                         return 0;
2267         }
2268
2269         /* it's safe to free/stop this BE DAI */
2270         return 1;
2271 }
2272 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2273
2274 /*
2275  * We can only change hw params a BE DAI if any of it's FE are not prepared,
2276  * running, paused or suspended for the specified stream direction.
2277  */
2278 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2279                 struct snd_soc_pcm_runtime *be, int stream)
2280 {
2281         struct snd_soc_dpcm *dpcm;
2282         int state;
2283
2284         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2285
2286                 if (dpcm->fe == fe)
2287                         continue;
2288
2289                 state = dpcm->fe->dpcm[stream].state;
2290                 if (state == SND_SOC_DPCM_STATE_START ||
2291                         state == SND_SOC_DPCM_STATE_PAUSED ||
2292                         state == SND_SOC_DPCM_STATE_SUSPEND ||
2293                         state == SND_SOC_DPCM_STATE_PREPARE)
2294                         return 0;
2295         }
2296
2297         /* it's safe to change hw_params */
2298         return 1;
2299 }
2300 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2301
2302 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2303                 int cmd, struct snd_soc_platform *platform)
2304 {
2305         if (platform->driver->ops->trigger)
2306                 return platform->driver->ops->trigger(substream, cmd);
2307         return 0;
2308 }
2309 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2310
2311 #ifdef CONFIG_DEBUG_FS
2312 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2313 {
2314         switch (state) {
2315         case SND_SOC_DPCM_STATE_NEW:
2316                 return "new";
2317         case SND_SOC_DPCM_STATE_OPEN:
2318                 return "open";
2319         case SND_SOC_DPCM_STATE_HW_PARAMS:
2320                 return "hw_params";
2321         case SND_SOC_DPCM_STATE_PREPARE:
2322                 return "prepare";
2323         case SND_SOC_DPCM_STATE_START:
2324                 return "start";
2325         case SND_SOC_DPCM_STATE_STOP:
2326                 return "stop";
2327         case SND_SOC_DPCM_STATE_SUSPEND:
2328                 return "suspend";
2329         case SND_SOC_DPCM_STATE_PAUSED:
2330                 return "paused";
2331         case SND_SOC_DPCM_STATE_HW_FREE:
2332                 return "hw_free";
2333         case SND_SOC_DPCM_STATE_CLOSE:
2334                 return "close";
2335         }
2336
2337         return "unknown";
2338 }
2339
2340 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2341                                 int stream, char *buf, size_t size)
2342 {
2343         struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2344         struct snd_soc_dpcm *dpcm;
2345         ssize_t offset = 0;
2346
2347         /* FE state */
2348         offset += snprintf(buf + offset, size - offset,
2349                         "[%s - %s]\n", fe->dai_link->name,
2350                         stream ? "Capture" : "Playback");
2351
2352         offset += snprintf(buf + offset, size - offset, "State: %s\n",
2353                         dpcm_state_string(fe->dpcm[stream].state));
2354
2355         if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2356             (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2357                 offset += snprintf(buf + offset, size - offset,
2358                                 "Hardware Params: "
2359                                 "Format = %s, Channels = %d, Rate = %d\n",
2360                                 snd_pcm_format_name(params_format(params)),
2361                                 params_channels(params),
2362                                 params_rate(params));
2363
2364         /* BEs state */
2365         offset += snprintf(buf + offset, size - offset, "Backends:\n");
2366
2367         if (list_empty(&fe->dpcm[stream].be_clients)) {
2368                 offset += snprintf(buf + offset, size - offset,
2369                                 " No active DSP links\n");
2370                 goto out;
2371         }
2372
2373         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2374                 struct snd_soc_pcm_runtime *be = dpcm->be;
2375                 params = &dpcm->hw_params;
2376
2377                 offset += snprintf(buf + offset, size - offset,
2378                                 "- %s\n", be->dai_link->name);
2379
2380                 offset += snprintf(buf + offset, size - offset,
2381                                 "   State: %s\n",
2382                                 dpcm_state_string(be->dpcm[stream].state));
2383
2384                 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2385                     (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2386                         offset += snprintf(buf + offset, size - offset,
2387                                 "   Hardware Params: "
2388                                 "Format = %s, Channels = %d, Rate = %d\n",
2389                                 snd_pcm_format_name(params_format(params)),
2390                                 params_channels(params),
2391                                 params_rate(params));
2392         }
2393
2394 out:
2395         return offset;
2396 }
2397
2398 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2399                                 size_t count, loff_t *ppos)
2400 {
2401         struct snd_soc_pcm_runtime *fe = file->private_data;
2402         ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2403         char *buf;
2404
2405         buf = kmalloc(out_count, GFP_KERNEL);
2406         if (!buf)
2407                 return -ENOMEM;
2408
2409         if (fe->cpu_dai->driver->playback.channels_min)
2410                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2411                                         buf + offset, out_count - offset);
2412
2413         if (fe->cpu_dai->driver->capture.channels_min)
2414                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2415                                         buf + offset, out_count - offset);
2416
2417         ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2418
2419         kfree(buf);
2420         return ret;
2421 }
2422
2423 static const struct file_operations dpcm_state_fops = {
2424         .open = simple_open,
2425         .read = dpcm_state_read_file,
2426         .llseek = default_llseek,
2427 };
2428
2429 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2430 {
2431         if (!rtd->dai_link)
2432                 return 0;
2433
2434         rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2435                         rtd->card->debugfs_card_root);
2436         if (!rtd->debugfs_dpcm_root) {
2437                 dev_dbg(rtd->dev,
2438                          "ASoC: Failed to create dpcm debugfs directory %s\n",
2439                          rtd->dai_link->name);
2440                 return -EINVAL;
2441         }
2442
2443         rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2444                                                 rtd->debugfs_dpcm_root,
2445                                                 rtd, &dpcm_state_fops);
2446
2447         return 0;
2448 }
2449 #endif