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