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