Merge remote-tracking branch 'regmap/topic/ack' into regmap-next
[platform/adaptation/renesas_rcar/renesas_kernel.git] / sound / soc / soc-pcm.c
1 /*
2  * soc-pcm.c  --  ALSA SoC PCM
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Copyright 2005 Openedhand Ltd.
6  * Copyright (C) 2010 Slimlogic Ltd.
7  * Copyright (C) 2010 Texas Instruments Inc.
8  *
9  * Authors: Liam Girdwood <lrg@ti.com>
10  *          Mark Brown <broonie@opensource.wolfsonmicro.com>       
11  *
12  *  This program is free software; you can redistribute  it and/or modify it
13  *  under  the terms of  the GNU General  Public License as published by the
14  *  Free Software Foundation;  either version 2 of the  License, or (at your
15  *  option) any later version.
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
34
35 #define DPCM_MAX_BE_USERS       8
36
37 /**
38  * snd_soc_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         bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
604
605         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
606
607         /* apply codec digital mute */
608         if ((playback && codec_dai->playback_active == 1) ||
609             (!playback && codec_dai->capture_active == 1))
610                 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
611
612         /* free any machine hw params */
613         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
614                 rtd->dai_link->ops->hw_free(substream);
615
616         /* free any DMA resources */
617         if (platform->driver->ops && platform->driver->ops->hw_free)
618                 platform->driver->ops->hw_free(substream);
619
620         /* now free hw params for the DAIs  */
621         if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
622                 codec_dai->driver->ops->hw_free(substream, codec_dai);
623
624         if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
625                 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
626
627         mutex_unlock(&rtd->pcm_mutex);
628         return 0;
629 }
630
631 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
632 {
633         struct snd_soc_pcm_runtime *rtd = substream->private_data;
634         struct snd_soc_platform *platform = rtd->platform;
635         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
636         struct snd_soc_dai *codec_dai = rtd->codec_dai;
637         int ret;
638
639         if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
640                 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
641                 if (ret < 0)
642                         return ret;
643         }
644
645         if (platform->driver->ops && platform->driver->ops->trigger) {
646                 ret = platform->driver->ops->trigger(substream, cmd);
647                 if (ret < 0)
648                         return ret;
649         }
650
651         if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
652                 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
653                 if (ret < 0)
654                         return ret;
655         }
656         return 0;
657 }
658
659 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
660                                    int cmd)
661 {
662         struct snd_soc_pcm_runtime *rtd = substream->private_data;
663         struct snd_soc_platform *platform = rtd->platform;
664         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
665         struct snd_soc_dai *codec_dai = rtd->codec_dai;
666         int ret;
667
668         if (codec_dai->driver->ops &&
669             codec_dai->driver->ops->bespoke_trigger) {
670                 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
671                 if (ret < 0)
672                         return ret;
673         }
674
675         if (platform->driver->ops && platform->driver->bespoke_trigger) {
676                 ret = platform->driver->bespoke_trigger(substream, cmd);
677                 if (ret < 0)
678                         return ret;
679         }
680
681         if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
682                 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
683                 if (ret < 0)
684                         return ret;
685         }
686         return 0;
687 }
688 /*
689  * soc level wrapper for pointer callback
690  * If cpu_dai, codec_dai, platform driver has the delay callback, than
691  * the runtime->delay will be updated accordingly.
692  */
693 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
694 {
695         struct snd_soc_pcm_runtime *rtd = substream->private_data;
696         struct snd_soc_platform *platform = rtd->platform;
697         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
698         struct snd_soc_dai *codec_dai = rtd->codec_dai;
699         struct snd_pcm_runtime *runtime = substream->runtime;
700         snd_pcm_uframes_t offset = 0;
701         snd_pcm_sframes_t delay = 0;
702
703         if (platform->driver->ops && platform->driver->ops->pointer)
704                 offset = platform->driver->ops->pointer(substream);
705
706         if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
707                 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
708
709         if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
710                 delay += codec_dai->driver->ops->delay(substream, codec_dai);
711
712         if (platform->driver->delay)
713                 delay += platform->driver->delay(substream, codec_dai);
714
715         runtime->delay = delay;
716
717         return offset;
718 }
719
720 /* connect a FE and BE */
721 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
722                 struct snd_soc_pcm_runtime *be, int stream)
723 {
724         struct snd_soc_dpcm *dpcm;
725
726         /* only add new dpcms */
727         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
728                 if (dpcm->be == be && dpcm->fe == fe)
729                         return 0;
730         }
731
732         dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
733         if (!dpcm)
734                 return -ENOMEM;
735
736         dpcm->be = be;
737         dpcm->fe = fe;
738         be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
739         dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
740         list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
741         list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
742
743         dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
744                         stream ? "capture" : "playback",  fe->dai_link->name,
745                         stream ? "<-" : "->", be->dai_link->name);
746
747 #ifdef CONFIG_DEBUG_FS
748         dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
749                         fe->debugfs_dpcm_root, &dpcm->state);
750 #endif
751         return 1;
752 }
753
754 /* reparent a BE onto another FE */
755 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
756                         struct snd_soc_pcm_runtime *be, int stream)
757 {
758         struct snd_soc_dpcm *dpcm;
759         struct snd_pcm_substream *fe_substream, *be_substream;
760
761         /* reparent if BE is connected to other FEs */
762         if (!be->dpcm[stream].users)
763                 return;
764
765         be_substream = snd_soc_dpcm_get_substream(be, stream);
766
767         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
768                 if (dpcm->fe == fe)
769                         continue;
770
771                 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
772                         stream ? "capture" : "playback",
773                         dpcm->fe->dai_link->name,
774                         stream ? "<-" : "->", dpcm->be->dai_link->name);
775
776                 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
777                 be_substream->runtime = fe_substream->runtime;
778                 break;
779         }
780 }
781
782 /* disconnect a BE and FE */
783 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
784 {
785         struct snd_soc_dpcm *dpcm, *d;
786
787         list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
788                 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
789                                 stream ? "capture" : "playback",
790                                 dpcm->be->dai_link->name);
791
792                 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
793                         continue;
794
795                 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
796                         stream ? "capture" : "playback", fe->dai_link->name,
797                         stream ? "<-" : "->", dpcm->be->dai_link->name);
798
799                 /* BEs still alive need new FE */
800                 dpcm_be_reparent(fe, dpcm->be, stream);
801
802 #ifdef CONFIG_DEBUG_FS
803                 debugfs_remove(dpcm->debugfs_state);
804 #endif
805                 list_del(&dpcm->list_be);
806                 list_del(&dpcm->list_fe);
807                 kfree(dpcm);
808         }
809 }
810
811 /* get BE for DAI widget and stream */
812 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
813                 struct snd_soc_dapm_widget *widget, int stream)
814 {
815         struct snd_soc_pcm_runtime *be;
816         int i;
817
818         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
819                 for (i = 0; i < card->num_links; i++) {
820                         be = &card->rtd[i];
821
822                         if (!be->dai_link->no_pcm)
823                                 continue;
824
825                         if (be->cpu_dai->playback_widget == widget ||
826                                 be->codec_dai->playback_widget == widget)
827                                 return be;
828                 }
829         } else {
830
831                 for (i = 0; i < card->num_links; i++) {
832                         be = &card->rtd[i];
833
834                         if (!be->dai_link->no_pcm)
835                                 continue;
836
837                         if (be->cpu_dai->capture_widget == widget ||
838                                 be->codec_dai->capture_widget == widget)
839                                 return be;
840                 }
841         }
842
843         dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
844                 stream ? "capture" : "playback", widget->name);
845         return NULL;
846 }
847
848 static inline struct snd_soc_dapm_widget *
849         rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
850 {
851         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
852                 return rtd->cpu_dai->playback_widget;
853         else
854                 return rtd->cpu_dai->capture_widget;
855 }
856
857 static inline struct snd_soc_dapm_widget *
858         rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
859 {
860         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
861                 return rtd->codec_dai->playback_widget;
862         else
863                 return rtd->codec_dai->capture_widget;
864 }
865
866 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
867                 struct snd_soc_dapm_widget *widget)
868 {
869         int i;
870
871         for (i = 0; i < list->num_widgets; i++) {
872                 if (widget == list->widgets[i])
873                         return 1;
874         }
875
876         return 0;
877 }
878
879 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
880         int stream, struct snd_soc_dapm_widget_list **list_)
881 {
882         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
883         struct snd_soc_dapm_widget_list *list;
884         int paths;
885
886         list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
887                         sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
888         if (list == NULL)
889                 return -ENOMEM;
890
891         /* get number of valid DAI paths and their widgets */
892         paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
893
894         dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
895                         stream ? "capture" : "playback");
896
897         *list_ = list;
898         return paths;
899 }
900
901 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
902 {
903         kfree(*list);
904 }
905
906 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
907         struct snd_soc_dapm_widget_list **list_)
908 {
909         struct snd_soc_dpcm *dpcm;
910         struct snd_soc_dapm_widget_list *list = *list_;
911         struct snd_soc_dapm_widget *widget;
912         int prune = 0;
913
914         /* Destroy any old FE <--> BE connections */
915         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
916
917                 /* is there a valid CPU DAI widget for this BE */
918                 widget = rtd_get_cpu_widget(dpcm->be, stream);
919
920                 /* prune the BE if it's no longer in our active list */
921                 if (widget && widget_in_list(list, widget))
922                         continue;
923
924                 /* is there a valid CODEC DAI widget for this BE */
925                 widget = rtd_get_codec_widget(dpcm->be, stream);
926
927                 /* prune the BE if it's no longer in our active list */
928                 if (widget && widget_in_list(list, widget))
929                         continue;
930
931                 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
932                         stream ? "capture" : "playback",
933                         dpcm->be->dai_link->name, fe->dai_link->name);
934                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
935                 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
936                 prune++;
937         }
938
939         dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
940         return prune;
941 }
942
943 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
944         struct snd_soc_dapm_widget_list **list_)
945 {
946         struct snd_soc_card *card = fe->card;
947         struct snd_soc_dapm_widget_list *list = *list_;
948         struct snd_soc_pcm_runtime *be;
949         int i, new = 0, err;
950
951         /* Create any new FE <--> BE connections */
952         for (i = 0; i < list->num_widgets; i++) {
953
954                 switch (list->widgets[i]->id) {
955                 case snd_soc_dapm_dai_in:
956                 case snd_soc_dapm_dai_out:
957                         break;
958                 default:
959                         continue;
960                 }
961
962                 /* is there a valid BE rtd for this widget */
963                 be = dpcm_get_be(card, list->widgets[i], stream);
964                 if (!be) {
965                         dev_err(fe->dev, "ASoC: no BE found for %s\n",
966                                         list->widgets[i]->name);
967                         continue;
968                 }
969
970                 /* make sure BE is a real BE */
971                 if (!be->dai_link->no_pcm)
972                         continue;
973
974                 /* don't connect if FE is not running */
975                 if (!fe->dpcm[stream].runtime)
976                         continue;
977
978                 /* newly connected FE and BE */
979                 err = dpcm_be_connect(fe, be, stream);
980                 if (err < 0) {
981                         dev_err(fe->dev, "ASoC: can't connect %s\n",
982                                 list->widgets[i]->name);
983                         break;
984                 } else if (err == 0) /* already connected */
985                         continue;
986
987                 /* new */
988                 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
989                 new++;
990         }
991
992         dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
993         return new;
994 }
995
996 /*
997  * Find the corresponding BE DAIs that source or sink audio to this
998  * FE substream.
999  */
1000 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1001         int stream, struct snd_soc_dapm_widget_list **list, int new)
1002 {
1003         if (new)
1004                 return dpcm_add_paths(fe, stream, list);
1005         else
1006                 return dpcm_prune_paths(fe, stream, list);
1007 }
1008
1009 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1010 {
1011         struct snd_soc_dpcm *dpcm;
1012
1013         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1014                 dpcm->be->dpcm[stream].runtime_update =
1015                                                 SND_SOC_DPCM_UPDATE_NO;
1016 }
1017
1018 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1019         int stream)
1020 {
1021         struct snd_soc_dpcm *dpcm;
1022
1023         /* disable any enabled and non active backends */
1024         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1025
1026                 struct snd_soc_pcm_runtime *be = dpcm->be;
1027                 struct snd_pcm_substream *be_substream =
1028                         snd_soc_dpcm_get_substream(be, stream);
1029
1030                 if (be->dpcm[stream].users == 0)
1031                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1032                                 stream ? "capture" : "playback",
1033                                 be->dpcm[stream].state);
1034
1035                 if (--be->dpcm[stream].users != 0)
1036                         continue;
1037
1038                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1039                         continue;
1040
1041                 soc_pcm_close(be_substream);
1042                 be_substream->runtime = NULL;
1043                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1044         }
1045 }
1046
1047 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1048 {
1049         struct snd_soc_dpcm *dpcm;
1050         int err, count = 0;
1051
1052         /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1053         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1054
1055                 struct snd_soc_pcm_runtime *be = dpcm->be;
1056                 struct snd_pcm_substream *be_substream =
1057                         snd_soc_dpcm_get_substream(be, stream);
1058
1059                 if (!be_substream) {
1060                         dev_err(be->dev, "ASoC: no backend %s stream\n",
1061                                 stream ? "capture" : "playback");
1062                         continue;
1063                 }
1064
1065                 /* is this op for this BE ? */
1066                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1067                         continue;
1068
1069                 /* first time the dpcm is open ? */
1070                 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1071                         dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1072                                 stream ? "capture" : "playback",
1073                                 be->dpcm[stream].state);
1074
1075                 if (be->dpcm[stream].users++ != 0)
1076                         continue;
1077
1078                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1079                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1080                         continue;
1081
1082                 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1083                         stream ? "capture" : "playback", be->dai_link->name);
1084
1085                 be_substream->runtime = be->dpcm[stream].runtime;
1086                 err = soc_pcm_open(be_substream);
1087                 if (err < 0) {
1088                         dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1089                         be->dpcm[stream].users--;
1090                         if (be->dpcm[stream].users < 0)
1091                                 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1092                                         stream ? "capture" : "playback",
1093                                         be->dpcm[stream].state);
1094
1095                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1096                         goto unwind;
1097                 }
1098
1099                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1100                 count++;
1101         }
1102
1103         return count;
1104
1105 unwind:
1106         /* disable any enabled and non active backends */
1107         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1108                 struct snd_soc_pcm_runtime *be = dpcm->be;
1109                 struct snd_pcm_substream *be_substream =
1110                         snd_soc_dpcm_get_substream(be, stream);
1111
1112                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1113                         continue;
1114
1115                 if (be->dpcm[stream].users == 0)
1116                         dev_err(be->dev, "ASoC: no users %s at close %d\n",
1117                                 stream ? "capture" : "playback",
1118                                 be->dpcm[stream].state);
1119
1120                 if (--be->dpcm[stream].users != 0)
1121                         continue;
1122
1123                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1124                         continue;
1125
1126                 soc_pcm_close(be_substream);
1127                 be_substream->runtime = NULL;
1128                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1129         }
1130
1131         return err;
1132 }
1133
1134 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1135 {
1136         struct snd_pcm_runtime *runtime = substream->runtime;
1137         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1138         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1139         struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1140
1141         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1142                 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1143                 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1144                 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1145                 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1146                 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1147                 runtime->hw.rates = cpu_dai_drv->playback.rates;
1148         } else {
1149                 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1150                 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1151                 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1152                 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1153                 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1154                 runtime->hw.rates = cpu_dai_drv->capture.rates;
1155         }
1156 }
1157
1158 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1159 {
1160         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1161         struct snd_pcm_runtime *runtime = fe_substream->runtime;
1162         int stream = fe_substream->stream, ret = 0;
1163
1164         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1165
1166         ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1167         if (ret < 0) {
1168                 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1169                 goto be_err;
1170         }
1171
1172         dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1173
1174         /* start the DAI frontend */
1175         ret = soc_pcm_open(fe_substream);
1176         if (ret < 0) {
1177                 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1178                 goto unwind;
1179         }
1180
1181         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1182
1183         dpcm_set_fe_runtime(fe_substream);
1184         snd_pcm_limit_hw_rates(runtime);
1185
1186         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1187         return 0;
1188
1189 unwind:
1190         dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1191 be_err:
1192         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1193         return ret;
1194 }
1195
1196 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1197 {
1198         struct snd_soc_dpcm *dpcm;
1199
1200         /* only shutdown BEs that are either sinks or sources to this FE DAI */
1201         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1202
1203                 struct snd_soc_pcm_runtime *be = dpcm->be;
1204                 struct snd_pcm_substream *be_substream =
1205                         snd_soc_dpcm_get_substream(be, stream);
1206
1207                 /* is this op for this BE ? */
1208                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1209                         continue;
1210
1211                 if (be->dpcm[stream].users == 0)
1212                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1213                                 stream ? "capture" : "playback",
1214                                 be->dpcm[stream].state);
1215
1216                 if (--be->dpcm[stream].users != 0)
1217                         continue;
1218
1219                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1220                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1221                         continue;
1222
1223                 dev_dbg(be->dev, "ASoC: close BE %s\n",
1224                         dpcm->fe->dai_link->name);
1225
1226                 soc_pcm_close(be_substream);
1227                 be_substream->runtime = NULL;
1228
1229                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1230         }
1231         return 0;
1232 }
1233
1234 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1235 {
1236         struct snd_soc_pcm_runtime *fe = substream->private_data;
1237         int stream = substream->stream;
1238
1239         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1240
1241         /* shutdown the BEs */
1242         dpcm_be_dai_shutdown(fe, substream->stream);
1243
1244         dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1245
1246         /* now shutdown the frontend */
1247         soc_pcm_close(substream);
1248
1249         /* run the stream event for each BE */
1250         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1251
1252         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1253         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1254         return 0;
1255 }
1256
1257 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1258 {
1259         struct snd_soc_dpcm *dpcm;
1260
1261         /* only hw_params backends that are either sinks or sources
1262          * to this frontend DAI */
1263         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1264
1265                 struct snd_soc_pcm_runtime *be = dpcm->be;
1266                 struct snd_pcm_substream *be_substream =
1267                         snd_soc_dpcm_get_substream(be, stream);
1268
1269                 /* is this op for this BE ? */
1270                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1271                         continue;
1272
1273                 /* only free hw when no longer used - check all FEs */
1274                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1275                                 continue;
1276
1277                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1278                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1279                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1280                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1281                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1282                         continue;
1283
1284                 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1285                         dpcm->fe->dai_link->name);
1286
1287                 soc_pcm_hw_free(be_substream);
1288
1289                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1290         }
1291
1292         return 0;
1293 }
1294
1295 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1296 {
1297         struct snd_soc_pcm_runtime *fe = substream->private_data;
1298         int err, stream = substream->stream;
1299
1300         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1301         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1302
1303         dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1304
1305         /* call hw_free on the frontend */
1306         err = soc_pcm_hw_free(substream);
1307         if (err < 0)
1308                 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1309                         fe->dai_link->name);
1310
1311         /* only hw_params backends that are either sinks or sources
1312          * to this frontend DAI */
1313         err = dpcm_be_dai_hw_free(fe, stream);
1314
1315         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1316         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1317
1318         mutex_unlock(&fe->card->mutex);
1319         return 0;
1320 }
1321
1322 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1323 {
1324         struct snd_soc_dpcm *dpcm;
1325         int ret;
1326
1327         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1328
1329                 struct snd_soc_pcm_runtime *be = dpcm->be;
1330                 struct snd_pcm_substream *be_substream =
1331                         snd_soc_dpcm_get_substream(be, stream);
1332
1333                 /* is this op for this BE ? */
1334                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1335                         continue;
1336
1337                 /* only allow hw_params() if no connected FEs are running */
1338                 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1339                         continue;
1340
1341                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1342                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1343                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1344                         continue;
1345
1346                 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1347                         dpcm->fe->dai_link->name);
1348
1349                 /* copy params for each dpcm */
1350                 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1351                                 sizeof(struct snd_pcm_hw_params));
1352
1353                 /* perform any hw_params fixups */
1354                 if (be->dai_link->be_hw_params_fixup) {
1355                         ret = be->dai_link->be_hw_params_fixup(be,
1356                                         &dpcm->hw_params);
1357                         if (ret < 0) {
1358                                 dev_err(be->dev,
1359                                         "ASoC: hw_params BE fixup failed %d\n",
1360                                         ret);
1361                                 goto unwind;
1362                         }
1363                 }
1364
1365                 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1366                 if (ret < 0) {
1367                         dev_err(dpcm->be->dev,
1368                                 "ASoC: hw_params BE failed %d\n", ret);
1369                         goto unwind;
1370                 }
1371
1372                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1373         }
1374         return 0;
1375
1376 unwind:
1377         /* disable any enabled and non active backends */
1378         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1379                 struct snd_soc_pcm_runtime *be = dpcm->be;
1380                 struct snd_pcm_substream *be_substream =
1381                         snd_soc_dpcm_get_substream(be, stream);
1382
1383                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1384                         continue;
1385
1386                 /* only allow hw_free() if no connected FEs are running */
1387                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1388                         continue;
1389
1390                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1391                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1392                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1393                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1394                         continue;
1395
1396                 soc_pcm_hw_free(be_substream);
1397         }
1398
1399         return ret;
1400 }
1401
1402 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1403                                  struct snd_pcm_hw_params *params)
1404 {
1405         struct snd_soc_pcm_runtime *fe = substream->private_data;
1406         int ret, stream = substream->stream;
1407
1408         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1409         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1410
1411         memcpy(&fe->dpcm[substream->stream].hw_params, params,
1412                         sizeof(struct snd_pcm_hw_params));
1413         ret = dpcm_be_dai_hw_params(fe, substream->stream);
1414         if (ret < 0) {
1415                 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1416                 goto out;
1417         }
1418
1419         dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1420                         fe->dai_link->name, params_rate(params),
1421                         params_channels(params), params_format(params));
1422
1423         /* call hw_params on the frontend */
1424         ret = soc_pcm_hw_params(substream, params);
1425         if (ret < 0) {
1426                 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1427                 dpcm_be_dai_hw_free(fe, stream);
1428          } else
1429                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1430
1431 out:
1432         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1433         mutex_unlock(&fe->card->mutex);
1434         return ret;
1435 }
1436
1437 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1438                 struct snd_pcm_substream *substream, int cmd)
1439 {
1440         int ret;
1441
1442         dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1443                         dpcm->fe->dai_link->name, cmd);
1444
1445         ret = soc_pcm_trigger(substream, cmd);
1446         if (ret < 0)
1447                 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1448
1449         return ret;
1450 }
1451
1452 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1453                                int cmd)
1454 {
1455         struct snd_soc_dpcm *dpcm;
1456         int ret = 0;
1457
1458         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1459
1460                 struct snd_soc_pcm_runtime *be = dpcm->be;
1461                 struct snd_pcm_substream *be_substream =
1462                         snd_soc_dpcm_get_substream(be, stream);
1463
1464                 /* is this op for this BE ? */
1465                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1466                         continue;
1467
1468                 switch (cmd) {
1469                 case SNDRV_PCM_TRIGGER_START:
1470                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1471                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1472                                 continue;
1473
1474                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1475                         if (ret)
1476                                 return ret;
1477
1478                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1479                         break;
1480                 case SNDRV_PCM_TRIGGER_RESUME:
1481                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1482                                 continue;
1483
1484                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1485                         if (ret)
1486                                 return ret;
1487
1488                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1489                         break;
1490                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1491                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1492                                 continue;
1493
1494                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1495                         if (ret)
1496                                 return ret;
1497
1498                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1499                         break;
1500                 case SNDRV_PCM_TRIGGER_STOP:
1501                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1502                                 continue;
1503
1504                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1505                                 continue;
1506
1507                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1508                         if (ret)
1509                                 return ret;
1510
1511                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1512                         break;
1513                 case SNDRV_PCM_TRIGGER_SUSPEND:
1514                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1515                                 continue;
1516
1517                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1518                                 continue;
1519
1520                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1521                         if (ret)
1522                                 return ret;
1523
1524                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1525                         break;
1526                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1527                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1528                                 continue;
1529
1530                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1531                                 continue;
1532
1533                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1534                         if (ret)
1535                                 return ret;
1536
1537                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1538                         break;
1539                 }
1540         }
1541
1542         return ret;
1543 }
1544 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1545
1546 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1547 {
1548         struct snd_soc_pcm_runtime *fe = substream->private_data;
1549         int stream = substream->stream, ret;
1550         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1551
1552         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1553
1554         switch (trigger) {
1555         case SND_SOC_DPCM_TRIGGER_PRE:
1556                 /* call trigger on the frontend before the backend. */
1557
1558                 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1559                                 fe->dai_link->name, cmd);
1560
1561                 ret = soc_pcm_trigger(substream, cmd);
1562                 if (ret < 0) {
1563                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1564                         goto out;
1565                 }
1566
1567                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1568                 break;
1569         case SND_SOC_DPCM_TRIGGER_POST:
1570                 /* call trigger on the frontend after the backend. */
1571
1572                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1573                 if (ret < 0) {
1574                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1575                         goto out;
1576                 }
1577
1578                 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1579                                 fe->dai_link->name, cmd);
1580
1581                 ret = soc_pcm_trigger(substream, cmd);
1582                 break;
1583         case SND_SOC_DPCM_TRIGGER_BESPOKE:
1584                 /* bespoke trigger() - handles both FE and BEs */
1585
1586                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1587                                 fe->dai_link->name, cmd);
1588
1589                 ret = soc_pcm_bespoke_trigger(substream, cmd);
1590                 if (ret < 0) {
1591                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1592                         goto out;
1593                 }
1594                 break;
1595         default:
1596                 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1597                                 fe->dai_link->name);
1598                 ret = -EINVAL;
1599                 goto out;
1600         }
1601
1602         switch (cmd) {
1603         case SNDRV_PCM_TRIGGER_START:
1604         case SNDRV_PCM_TRIGGER_RESUME:
1605         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1606                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1607                 break;
1608         case SNDRV_PCM_TRIGGER_STOP:
1609         case SNDRV_PCM_TRIGGER_SUSPEND:
1610         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1611                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1612                 break;
1613         }
1614
1615 out:
1616         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1617         return ret;
1618 }
1619
1620 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1621 {
1622         struct snd_soc_dpcm *dpcm;
1623         int ret = 0;
1624
1625         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1626
1627                 struct snd_soc_pcm_runtime *be = dpcm->be;
1628                 struct snd_pcm_substream *be_substream =
1629                         snd_soc_dpcm_get_substream(be, stream);
1630
1631                 /* is this op for this BE ? */
1632                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1633                         continue;
1634
1635                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1636                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1637                         continue;
1638
1639                 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1640                         dpcm->fe->dai_link->name);
1641
1642                 ret = soc_pcm_prepare(be_substream);
1643                 if (ret < 0) {
1644                         dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1645                                 ret);
1646                         break;
1647                 }
1648
1649                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1650         }
1651         return ret;
1652 }
1653
1654 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1655 {
1656         struct snd_soc_pcm_runtime *fe = substream->private_data;
1657         int stream = substream->stream, ret = 0;
1658
1659         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1660
1661         dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1662
1663         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1664
1665         /* there is no point preparing this FE if there are no BEs */
1666         if (list_empty(&fe->dpcm[stream].be_clients)) {
1667                 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1668                                 fe->dai_link->name);
1669                 ret = -EINVAL;
1670                 goto out;
1671         }
1672
1673         ret = dpcm_be_dai_prepare(fe, substream->stream);
1674         if (ret < 0)
1675                 goto out;
1676
1677         /* call prepare on the frontend */
1678         ret = soc_pcm_prepare(substream);
1679         if (ret < 0) {
1680                 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1681                         fe->dai_link->name);
1682                 goto out;
1683         }
1684
1685         /* run the stream event for each BE */
1686         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1687         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1688
1689 out:
1690         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1691         mutex_unlock(&fe->card->mutex);
1692
1693         return ret;
1694 }
1695
1696 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1697                      unsigned int cmd, void *arg)
1698 {
1699         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1700         struct snd_soc_platform *platform = rtd->platform;
1701
1702         if (platform->driver->ops && platform->driver->ops->ioctl)
1703                 return platform->driver->ops->ioctl(substream, cmd, arg);
1704         return snd_pcm_lib_ioctl(substream, cmd, arg);
1705 }
1706
1707 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1708 {
1709         struct snd_pcm_substream *substream =
1710                 snd_soc_dpcm_get_substream(fe, stream);
1711         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1712         int err;
1713
1714         dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1715                         stream ? "capture" : "playback", fe->dai_link->name);
1716
1717         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1718                 /* call bespoke trigger - FE takes care of all BE triggers */
1719                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1720                                 fe->dai_link->name);
1721
1722                 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1723                 if (err < 0)
1724                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1725         } else {
1726                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1727                         fe->dai_link->name);
1728
1729                 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1730                 if (err < 0)
1731                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1732         }
1733
1734         err = dpcm_be_dai_hw_free(fe, stream);
1735         if (err < 0)
1736                 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1737
1738         err = dpcm_be_dai_shutdown(fe, stream);
1739         if (err < 0)
1740                 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1741
1742         /* run the stream event for each BE */
1743         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1744
1745         return 0;
1746 }
1747
1748 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1749 {
1750         struct snd_pcm_substream *substream =
1751                 snd_soc_dpcm_get_substream(fe, stream);
1752         struct snd_soc_dpcm *dpcm;
1753         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1754         int ret;
1755
1756         dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1757                         stream ? "capture" : "playback", fe->dai_link->name);
1758
1759         /* Only start the BE if the FE is ready */
1760         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1761                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1762                 return -EINVAL;
1763
1764         /* startup must always be called for new BEs */
1765         ret = dpcm_be_dai_startup(fe, stream);
1766         if (ret < 0)
1767                 goto disconnect;
1768
1769         /* keep going if FE state is > open */
1770         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1771                 return 0;
1772
1773         ret = dpcm_be_dai_hw_params(fe, stream);
1774         if (ret < 0)
1775                 goto close;
1776
1777         /* keep going if FE state is > hw_params */
1778         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1779                 return 0;
1780
1781
1782         ret = dpcm_be_dai_prepare(fe, stream);
1783         if (ret < 0)
1784                 goto hw_free;
1785
1786         /* run the stream event for each BE */
1787         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1788
1789         /* keep going if FE state is > prepare */
1790         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1791                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1792                 return 0;
1793
1794         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1795                 /* call trigger on the frontend - FE takes care of all BE triggers */
1796                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1797                                 fe->dai_link->name);
1798
1799                 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1800                 if (ret < 0) {
1801                         dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1802                         goto hw_free;
1803                 }
1804         } else {
1805                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1806                         fe->dai_link->name);
1807
1808                 ret = dpcm_be_dai_trigger(fe, stream,
1809                                         SNDRV_PCM_TRIGGER_START);
1810                 if (ret < 0) {
1811                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1812                         goto hw_free;
1813                 }
1814         }
1815
1816         return 0;
1817
1818 hw_free:
1819         dpcm_be_dai_hw_free(fe, stream);
1820 close:
1821         dpcm_be_dai_shutdown(fe, stream);
1822 disconnect:
1823         /* disconnect any non started BEs */
1824         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1825                 struct snd_soc_pcm_runtime *be = dpcm->be;
1826                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1827                                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1828         }
1829
1830         return ret;
1831 }
1832
1833 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1834 {
1835         int ret;
1836
1837         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1838         ret = dpcm_run_update_startup(fe, stream);
1839         if (ret < 0)
1840                 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1841         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1842
1843         return ret;
1844 }
1845
1846 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1847 {
1848         int ret;
1849
1850         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1851         ret = dpcm_run_update_shutdown(fe, stream);
1852         if (ret < 0)
1853                 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1854         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1855
1856         return ret;
1857 }
1858
1859 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1860  * any DAI links.
1861  */
1862 int soc_dpcm_runtime_update(struct snd_soc_card *card)
1863 {
1864         int i, old, new, paths;
1865
1866         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1867         for (i = 0; i < card->num_rtd; i++) {
1868                 struct snd_soc_dapm_widget_list *list;
1869                 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1870
1871                 /* make sure link is FE */
1872                 if (!fe->dai_link->dynamic)
1873                         continue;
1874
1875                 /* only check active links */
1876                 if (!fe->cpu_dai->active)
1877                         continue;
1878
1879                 /* DAPM sync will call this to update DSP paths */
1880                 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1881                         fe->dai_link->name);
1882
1883                 /* skip if FE doesn't have playback capability */
1884                 if (!fe->cpu_dai->driver->playback.channels_min)
1885                         goto capture;
1886
1887                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1888                 if (paths < 0) {
1889                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1890                                         fe->dai_link->name,  "playback");
1891                         mutex_unlock(&card->mutex);
1892                         return paths;
1893                 }
1894
1895                 /* update any new playback paths */
1896                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1897                 if (new) {
1898                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1899                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1900                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1901                 }
1902
1903                 /* update any old playback paths */
1904                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1905                 if (old) {
1906                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1907                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1908                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1909                 }
1910
1911 capture:
1912                 /* skip if FE doesn't have capture capability */
1913                 if (!fe->cpu_dai->driver->capture.channels_min)
1914                         continue;
1915
1916                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1917                 if (paths < 0) {
1918                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1919                                         fe->dai_link->name,  "capture");
1920                         mutex_unlock(&card->mutex);
1921                         return paths;
1922                 }
1923
1924                 /* update any new capture paths */
1925                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1926                 if (new) {
1927                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1928                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1929                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1930                 }
1931
1932                 /* update any old capture paths */
1933                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1934                 if (old) {
1935                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1936                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1937                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1938                 }
1939
1940                 dpcm_path_put(&list);
1941         }
1942
1943         mutex_unlock(&card->mutex);
1944         return 0;
1945 }
1946 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1947 {
1948         struct snd_soc_dpcm *dpcm;
1949         struct list_head *clients =
1950                 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1951
1952         list_for_each_entry(dpcm, clients, list_be) {
1953
1954                 struct snd_soc_pcm_runtime *be = dpcm->be;
1955                 struct snd_soc_dai *dai = be->codec_dai;
1956                 struct snd_soc_dai_driver *drv = dai->driver;
1957
1958                 if (be->dai_link->ignore_suspend)
1959                         continue;
1960
1961                 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
1962
1963                 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1964                         drv->ops->digital_mute(dai, mute);
1965         }
1966
1967         return 0;
1968 }
1969
1970 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1971 {
1972         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1973         struct snd_soc_dpcm *dpcm;
1974         struct snd_soc_dapm_widget_list *list;
1975         int ret;
1976         int stream = fe_substream->stream;
1977
1978         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1979         fe->dpcm[stream].runtime = fe_substream->runtime;
1980
1981         if (dpcm_path_get(fe, stream, &list) <= 0) {
1982                 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
1983                         fe->dai_link->name, stream ? "capture" : "playback");
1984         }
1985
1986         /* calculate valid and active FE <-> BE dpcms */
1987         dpcm_process_paths(fe, stream, &list, 1);
1988
1989         ret = dpcm_fe_dai_startup(fe_substream);
1990         if (ret < 0) {
1991                 /* clean up all links */
1992                 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1993                         dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1994
1995                 dpcm_be_disconnect(fe, stream);
1996                 fe->dpcm[stream].runtime = NULL;
1997         }
1998
1999         dpcm_clear_pending_state(fe, stream);
2000         dpcm_path_put(&list);
2001         mutex_unlock(&fe->card->mutex);
2002         return ret;
2003 }
2004
2005 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2006 {
2007         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2008         struct snd_soc_dpcm *dpcm;
2009         int stream = fe_substream->stream, ret;
2010
2011         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2012         ret = dpcm_fe_dai_shutdown(fe_substream);
2013
2014         /* mark FE's links ready to prune */
2015         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2016                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2017
2018         dpcm_be_disconnect(fe, stream);
2019
2020         fe->dpcm[stream].runtime = NULL;
2021         mutex_unlock(&fe->card->mutex);
2022         return ret;
2023 }
2024
2025 /* create a new pcm */
2026 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2027 {
2028         struct snd_soc_platform *platform = rtd->platform;
2029         struct snd_soc_dai *codec_dai = rtd->codec_dai;
2030         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2031         struct snd_pcm *pcm;
2032         char new_name[64];
2033         int ret = 0, playback = 0, capture = 0;
2034
2035         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2036                 if (cpu_dai->driver->playback.channels_min)
2037                         playback = 1;
2038                 if (cpu_dai->driver->capture.channels_min)
2039                         capture = 1;
2040         } else {
2041                 if (codec_dai->driver->playback.channels_min &&
2042                     cpu_dai->driver->playback.channels_min)
2043                         playback = 1;
2044                 if (codec_dai->driver->capture.channels_min &&
2045                     cpu_dai->driver->capture.channels_min)
2046                         capture = 1;
2047         }
2048
2049         if (rtd->dai_link->playback_only) {
2050                 playback = 1;
2051                 capture = 0;
2052         }
2053
2054         if (rtd->dai_link->capture_only) {
2055                 playback = 0;
2056                 capture = 1;
2057         }
2058
2059         /* create the PCM */
2060         if (rtd->dai_link->no_pcm) {
2061                 snprintf(new_name, sizeof(new_name), "(%s)",
2062                         rtd->dai_link->stream_name);
2063
2064                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2065                                 playback, capture, &pcm);
2066         } else {
2067                 if (rtd->dai_link->dynamic)
2068                         snprintf(new_name, sizeof(new_name), "%s (*)",
2069                                 rtd->dai_link->stream_name);
2070                 else
2071                         snprintf(new_name, sizeof(new_name), "%s %s-%d",
2072                                 rtd->dai_link->stream_name, codec_dai->name, num);
2073
2074                 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2075                         capture, &pcm);
2076         }
2077         if (ret < 0) {
2078                 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2079                         rtd->dai_link->name);
2080                 return ret;
2081         }
2082         dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2083
2084         /* DAPM dai link stream work */
2085         INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2086
2087         rtd->pcm = pcm;
2088         pcm->private_data = rtd;
2089
2090         if (rtd->dai_link->no_pcm) {
2091                 if (playback)
2092                         pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2093                 if (capture)
2094                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2095                 goto out;
2096         }
2097
2098         /* ASoC PCM operations */
2099         if (rtd->dai_link->dynamic) {
2100                 rtd->ops.open           = dpcm_fe_dai_open;
2101                 rtd->ops.hw_params      = dpcm_fe_dai_hw_params;
2102                 rtd->ops.prepare        = dpcm_fe_dai_prepare;
2103                 rtd->ops.trigger        = dpcm_fe_dai_trigger;
2104                 rtd->ops.hw_free        = dpcm_fe_dai_hw_free;
2105                 rtd->ops.close          = dpcm_fe_dai_close;
2106                 rtd->ops.pointer        = soc_pcm_pointer;
2107                 rtd->ops.ioctl          = soc_pcm_ioctl;
2108         } else {
2109                 rtd->ops.open           = soc_pcm_open;
2110                 rtd->ops.hw_params      = soc_pcm_hw_params;
2111                 rtd->ops.prepare        = soc_pcm_prepare;
2112                 rtd->ops.trigger        = soc_pcm_trigger;
2113                 rtd->ops.hw_free        = soc_pcm_hw_free;
2114                 rtd->ops.close          = soc_pcm_close;
2115                 rtd->ops.pointer        = soc_pcm_pointer;
2116                 rtd->ops.ioctl          = soc_pcm_ioctl;
2117         }
2118
2119         if (platform->driver->ops) {
2120                 rtd->ops.ack            = platform->driver->ops->ack;
2121                 rtd->ops.copy           = platform->driver->ops->copy;
2122                 rtd->ops.silence        = platform->driver->ops->silence;
2123                 rtd->ops.page           = platform->driver->ops->page;
2124                 rtd->ops.mmap           = platform->driver->ops->mmap;
2125         }
2126
2127         if (playback)
2128                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2129
2130         if (capture)
2131                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2132
2133         if (platform->driver->pcm_new) {
2134                 ret = platform->driver->pcm_new(rtd);
2135                 if (ret < 0) {
2136                         dev_err(platform->dev,
2137                                 "ASoC: pcm constructor failed: %d\n",
2138                                 ret);
2139                         return ret;
2140                 }
2141         }
2142
2143         pcm->private_free = platform->driver->pcm_free;
2144 out:
2145         dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
2146                 cpu_dai->name);
2147         return ret;
2148 }
2149
2150 /* is the current PCM operation for this FE ? */
2151 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2152 {
2153         if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2154                 return 1;
2155         return 0;
2156 }
2157 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2158
2159 /* is the current PCM operation for this BE ? */
2160 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2161                 struct snd_soc_pcm_runtime *be, int stream)
2162 {
2163         if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2164            ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2165                   be->dpcm[stream].runtime_update))
2166                 return 1;
2167         return 0;
2168 }
2169 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2170
2171 /* get the substream for this BE */
2172 struct snd_pcm_substream *
2173         snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2174 {
2175         return be->pcm->streams[stream].substream;
2176 }
2177 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2178
2179 /* get the BE runtime state */
2180 enum snd_soc_dpcm_state
2181         snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2182 {
2183         return be->dpcm[stream].state;
2184 }
2185 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2186
2187 /* set the BE runtime state */
2188 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2189                 int stream, enum snd_soc_dpcm_state state)
2190 {
2191         be->dpcm[stream].state = state;
2192 }
2193 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2194
2195 /*
2196  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2197  * are not running, paused or suspended for the specified stream direction.
2198  */
2199 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2200                 struct snd_soc_pcm_runtime *be, int stream)
2201 {
2202         struct snd_soc_dpcm *dpcm;
2203         int state;
2204
2205         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2206
2207                 if (dpcm->fe == fe)
2208                         continue;
2209
2210                 state = dpcm->fe->dpcm[stream].state;
2211                 if (state == SND_SOC_DPCM_STATE_START ||
2212                         state == SND_SOC_DPCM_STATE_PAUSED ||
2213                         state == SND_SOC_DPCM_STATE_SUSPEND)
2214                         return 0;
2215         }
2216
2217         /* it's safe to free/stop this BE DAI */
2218         return 1;
2219 }
2220 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2221
2222 /*
2223  * We can only change hw params a BE DAI if any of it's FE are not prepared,
2224  * running, paused or suspended for the specified stream direction.
2225  */
2226 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2227                 struct snd_soc_pcm_runtime *be, int stream)
2228 {
2229         struct snd_soc_dpcm *dpcm;
2230         int state;
2231
2232         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2233
2234                 if (dpcm->fe == fe)
2235                         continue;
2236
2237                 state = dpcm->fe->dpcm[stream].state;
2238                 if (state == SND_SOC_DPCM_STATE_START ||
2239                         state == SND_SOC_DPCM_STATE_PAUSED ||
2240                         state == SND_SOC_DPCM_STATE_SUSPEND ||
2241                         state == SND_SOC_DPCM_STATE_PREPARE)
2242                         return 0;
2243         }
2244
2245         /* it's safe to change hw_params */
2246         return 1;
2247 }
2248 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2249
2250 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2251                 int cmd, struct snd_soc_platform *platform)
2252 {
2253         if (platform->driver->ops && platform->driver->ops->trigger)
2254                 return platform->driver->ops->trigger(substream, cmd);
2255         return 0;
2256 }
2257 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2258
2259 #ifdef CONFIG_DEBUG_FS
2260 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2261 {
2262         switch (state) {
2263         case SND_SOC_DPCM_STATE_NEW:
2264                 return "new";
2265         case SND_SOC_DPCM_STATE_OPEN:
2266                 return "open";
2267         case SND_SOC_DPCM_STATE_HW_PARAMS:
2268                 return "hw_params";
2269         case SND_SOC_DPCM_STATE_PREPARE:
2270                 return "prepare";
2271         case SND_SOC_DPCM_STATE_START:
2272                 return "start";
2273         case SND_SOC_DPCM_STATE_STOP:
2274                 return "stop";
2275         case SND_SOC_DPCM_STATE_SUSPEND:
2276                 return "suspend";
2277         case SND_SOC_DPCM_STATE_PAUSED:
2278                 return "paused";
2279         case SND_SOC_DPCM_STATE_HW_FREE:
2280                 return "hw_free";
2281         case SND_SOC_DPCM_STATE_CLOSE:
2282                 return "close";
2283         }
2284
2285         return "unknown";
2286 }
2287
2288 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2289                                 int stream, char *buf, size_t size)
2290 {
2291         struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2292         struct snd_soc_dpcm *dpcm;
2293         ssize_t offset = 0;
2294
2295         /* FE state */
2296         offset += snprintf(buf + offset, size - offset,
2297                         "[%s - %s]\n", fe->dai_link->name,
2298                         stream ? "Capture" : "Playback");
2299
2300         offset += snprintf(buf + offset, size - offset, "State: %s\n",
2301                         dpcm_state_string(fe->dpcm[stream].state));
2302
2303         if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2304             (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2305                 offset += snprintf(buf + offset, size - offset,
2306                                 "Hardware Params: "
2307                                 "Format = %s, Channels = %d, Rate = %d\n",
2308                                 snd_pcm_format_name(params_format(params)),
2309                                 params_channels(params),
2310                                 params_rate(params));
2311
2312         /* BEs state */
2313         offset += snprintf(buf + offset, size - offset, "Backends:\n");
2314
2315         if (list_empty(&fe->dpcm[stream].be_clients)) {
2316                 offset += snprintf(buf + offset, size - offset,
2317                                 " No active DSP links\n");
2318                 goto out;
2319         }
2320
2321         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2322                 struct snd_soc_pcm_runtime *be = dpcm->be;
2323                 params = &dpcm->hw_params;
2324
2325                 offset += snprintf(buf + offset, size - offset,
2326                                 "- %s\n", be->dai_link->name);
2327
2328                 offset += snprintf(buf + offset, size - offset,
2329                                 "   State: %s\n",
2330                                 dpcm_state_string(be->dpcm[stream].state));
2331
2332                 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2333                     (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2334                         offset += snprintf(buf + offset, size - offset,
2335                                 "   Hardware Params: "
2336                                 "Format = %s, Channels = %d, Rate = %d\n",
2337                                 snd_pcm_format_name(params_format(params)),
2338                                 params_channels(params),
2339                                 params_rate(params));
2340         }
2341
2342 out:
2343         return offset;
2344 }
2345
2346 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2347                                 size_t count, loff_t *ppos)
2348 {
2349         struct snd_soc_pcm_runtime *fe = file->private_data;
2350         ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2351         char *buf;
2352
2353         buf = kmalloc(out_count, GFP_KERNEL);
2354         if (!buf)
2355                 return -ENOMEM;
2356
2357         if (fe->cpu_dai->driver->playback.channels_min)
2358                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2359                                         buf + offset, out_count - offset);
2360
2361         if (fe->cpu_dai->driver->capture.channels_min)
2362                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2363                                         buf + offset, out_count - offset);
2364
2365         ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2366
2367         kfree(buf);
2368         return ret;
2369 }
2370
2371 static const struct file_operations dpcm_state_fops = {
2372         .open = simple_open,
2373         .read = dpcm_state_read_file,
2374         .llseek = default_llseek,
2375 };
2376
2377 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2378 {
2379         if (!rtd->dai_link)
2380                 return 0;
2381
2382         rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2383                         rtd->card->debugfs_card_root);
2384         if (!rtd->debugfs_dpcm_root) {
2385                 dev_dbg(rtd->dev,
2386                          "ASoC: Failed to create dpcm debugfs directory %s\n",
2387                          rtd->dai_link->name);
2388                 return -EINVAL;
2389         }
2390
2391         rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2392                                                 rtd->debugfs_dpcm_root,
2393                                                 rtd, &dpcm_state_fops);
2394
2395         return 0;
2396 }
2397 #endif