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