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