2c21fd528afd0fb7188f4bffadc477149aaae18b
[linux-block.git] / sound / soc / soc-pcm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-pcm.c  --  ALSA SoC PCM
4 //
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
9 //
10 // Authors: Liam Girdwood <lrg@ti.com>
11 //          Mark Brown <broonie@opensource.wolfsonmicro.com>
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19 #include <linux/export.h>
20 #include <linux/debugfs.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dpcm.h>
26 #include <sound/soc-link.h>
27 #include <sound/initval.h>
28
29 #define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret)
30 static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime *rtd,
31                                const char *func, int ret)
32 {
33         return snd_soc_ret(rtd->dev, ret,
34                            "at %s() on %s\n", func, rtd->dai_link->name);
35 }
36
37 /* is the current PCM operation for this FE ? */
38 #if 0
39 static int snd_soc_dpcm_can_fe_update(struct snd_soc_pcm_runtime *fe, int stream)
40 {
41         if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
42                 return 1;
43         return 0;
44 }
45 #endif
46
47 /* is the current PCM operation for this BE ? */
48 static int snd_soc_dpcm_can_be_update(struct snd_soc_pcm_runtime *fe,
49                                struct snd_soc_pcm_runtime *be, int stream)
50 {
51         if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
52             ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
53              be->dpcm[stream].runtime_update))
54                 return 1;
55         return 0;
56 }
57
58 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
59                                     struct snd_soc_pcm_runtime *be,
60                                     int stream,
61                                     const enum snd_soc_dpcm_state *states,
62                                     int num_states)
63 {
64         struct snd_soc_dpcm *dpcm;
65         int state;
66         int ret = 1;
67         int i;
68
69         for_each_dpcm_fe(be, stream, dpcm) {
70
71                 if (dpcm->fe == fe)
72                         continue;
73
74                 state = dpcm->fe->dpcm[stream].state;
75                 for (i = 0; i < num_states; i++) {
76                         if (state == states[i]) {
77                                 ret = 0;
78                                 break;
79                         }
80                 }
81         }
82
83         /* it's safe to do this BE DAI */
84         return ret;
85 }
86
87 /*
88  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
89  * are not running, paused or suspended for the specified stream direction.
90  */
91 static int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
92                                          struct snd_soc_pcm_runtime *be, int stream)
93 {
94         const enum snd_soc_dpcm_state state[] = {
95                 SND_SOC_DPCM_STATE_START,
96                 SND_SOC_DPCM_STATE_PAUSED,
97                 SND_SOC_DPCM_STATE_SUSPEND,
98         };
99
100         return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
101 }
102
103 /*
104  * We can only change hw params a BE DAI if any of it's FE are not prepared,
105  * running, paused or suspended for the specified stream direction.
106  */
107 static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
108                                       struct snd_soc_pcm_runtime *be, int stream)
109 {
110         const enum snd_soc_dpcm_state state[] = {
111                 SND_SOC_DPCM_STATE_START,
112                 SND_SOC_DPCM_STATE_PAUSED,
113                 SND_SOC_DPCM_STATE_SUSPEND,
114                 SND_SOC_DPCM_STATE_PREPARE,
115         };
116
117         return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
118 }
119
120 /*
121  * We can only prepare a BE DAI if any of it's FE are not prepared,
122  * running or paused for the specified stream direction.
123  */
124 static int snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime *fe,
125                                         struct snd_soc_pcm_runtime *be, int stream)
126 {
127         const enum snd_soc_dpcm_state state[] = {
128                 SND_SOC_DPCM_STATE_START,
129                 SND_SOC_DPCM_STATE_PAUSED,
130                 SND_SOC_DPCM_STATE_PREPARE,
131         };
132
133         return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
134 }
135
136 #define DPCM_MAX_BE_USERS       8
137
138 static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
139 {
140         return (rtd)->dai_link->num_cpus == 1 ? snd_soc_rtd_to_cpu(rtd, 0)->name : "multicpu";
141 }
142 static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
143 {
144         return (rtd)->dai_link->num_codecs == 1 ? snd_soc_rtd_to_codec(rtd, 0)->name : "multicodec";
145 }
146
147 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
148 {
149         switch (state) {
150         case SND_SOC_DPCM_STATE_NEW:
151                 return "new";
152         case SND_SOC_DPCM_STATE_OPEN:
153                 return "open";
154         case SND_SOC_DPCM_STATE_HW_PARAMS:
155                 return "hw_params";
156         case SND_SOC_DPCM_STATE_PREPARE:
157                 return "prepare";
158         case SND_SOC_DPCM_STATE_START:
159                 return "start";
160         case SND_SOC_DPCM_STATE_STOP:
161                 return "stop";
162         case SND_SOC_DPCM_STATE_SUSPEND:
163                 return "suspend";
164         case SND_SOC_DPCM_STATE_PAUSED:
165                 return "paused";
166         case SND_SOC_DPCM_STATE_HW_FREE:
167                 return "hw_free";
168         case SND_SOC_DPCM_STATE_CLOSE:
169                 return "close";
170         }
171
172         return "unknown";
173 }
174
175 #ifdef CONFIG_DEBUG_FS
176 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
177                                int stream, char *buf, size_t size)
178 {
179         struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
180         struct snd_soc_dpcm *dpcm;
181         ssize_t offset = 0;
182
183         /* FE state */
184         offset += scnprintf(buf + offset, size - offset,
185                            "[%s - %s]\n", fe->dai_link->name,
186                            stream ? "Capture" : "Playback");
187
188         offset += scnprintf(buf + offset, size - offset, "State: %s\n",
189                            dpcm_state_string(fe->dpcm[stream].state));
190
191         if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
192             (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
193                 offset += scnprintf(buf + offset, size - offset,
194                                    "Hardware Params: "
195                                    "Format = %s, Channels = %d, Rate = %d\n",
196                                    snd_pcm_format_name(params_format(params)),
197                                    params_channels(params),
198                                    params_rate(params));
199
200         /* BEs state */
201         offset += scnprintf(buf + offset, size - offset, "Backends:\n");
202
203         if (list_empty(&fe->dpcm[stream].be_clients)) {
204                 offset += scnprintf(buf + offset, size - offset,
205                                    " No active DSP links\n");
206                 goto out;
207         }
208
209         for_each_dpcm_be(fe, stream, dpcm) {
210                 struct snd_soc_pcm_runtime *be = dpcm->be;
211                 params = &be->dpcm[stream].hw_params;
212
213                 offset += scnprintf(buf + offset, size - offset,
214                                    "- %s\n", be->dai_link->name);
215
216                 offset += scnprintf(buf + offset, size - offset,
217                                    "   State: %s\n",
218                                    dpcm_state_string(be->dpcm[stream].state));
219
220                 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
221                     (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
222                         offset += scnprintf(buf + offset, size - offset,
223                                            "   Hardware Params: "
224                                            "Format = %s, Channels = %d, Rate = %d\n",
225                                            snd_pcm_format_name(params_format(params)),
226                                            params_channels(params),
227                                            params_rate(params));
228         }
229 out:
230         return offset;
231 }
232
233 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
234                                     size_t count, loff_t *ppos)
235 {
236         struct snd_soc_pcm_runtime *fe = file->private_data;
237         ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
238         int stream;
239         char *buf;
240
241         if (fe->dai_link->num_cpus > 1)
242                 return snd_soc_ret(fe->dev, -EINVAL,
243                         "%s doesn't support Multi CPU yet\n", __func__);
244
245         buf = kmalloc(out_count, GFP_KERNEL);
246         if (!buf)
247                 return -ENOMEM;
248
249         snd_soc_dpcm_mutex_lock(fe);
250         for_each_pcm_streams(stream)
251                 if (snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0), stream))
252                         offset += dpcm_show_state(fe, stream,
253                                                   buf + offset,
254                                                   out_count - offset);
255         snd_soc_dpcm_mutex_unlock(fe);
256
257         ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
258
259         kfree(buf);
260         return ret;
261 }
262
263 static const struct file_operations dpcm_state_fops = {
264         .open = simple_open,
265         .read = dpcm_state_read_file,
266         .llseek = default_llseek,
267 };
268
269 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
270 {
271         if (!rtd->dai_link->dynamic)
272                 return;
273
274         if (!rtd->card->debugfs_card_root)
275                 return;
276
277         rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
278                                                     rtd->card->debugfs_card_root);
279
280         debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
281                             rtd, &dpcm_state_fops);
282 }
283
284 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
285 {
286         char *name;
287
288         name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
289                          snd_pcm_direction_name(stream));
290         if (name) {
291                 dpcm->debugfs_state = debugfs_create_dir(
292                         name, dpcm->fe->debugfs_dpcm_root);
293                 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
294                                    &dpcm->state);
295                 kfree(name);
296         }
297 }
298
299 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
300 {
301         debugfs_remove_recursive(dpcm->debugfs_state);
302 }
303
304 #else
305 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
306                                              int stream)
307 {
308 }
309
310 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
311 {
312 }
313 #endif
314
315 /* Set FE's runtime_update state; the state is protected via PCM stream lock
316  * for avoiding the race with trigger callback.
317  * If the state is unset and a trigger is pending while the previous operation,
318  * process the pending trigger action here.
319  */
320 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
321 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
322                                      int stream, enum snd_soc_dpcm_update state)
323 {
324         struct snd_pcm_substream *substream =
325                 snd_soc_dpcm_get_substream(fe, stream);
326
327         snd_pcm_stream_lock_irq(substream);
328         if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
329                 dpcm_fe_dai_do_trigger(substream,
330                                        fe->dpcm[stream].trigger_pending - 1);
331                 fe->dpcm[stream].trigger_pending = 0;
332         }
333         fe->dpcm[stream].runtime_update = state;
334         snd_pcm_stream_unlock_irq(substream);
335 }
336
337 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
338                                      int stream, enum snd_soc_dpcm_update state)
339 {
340         be->dpcm[stream].runtime_update = state;
341 }
342
343 /**
344  * snd_soc_runtime_action() - Increment/Decrement active count for
345  * PCM runtime components
346  * @rtd: ASoC PCM runtime that is activated
347  * @stream: Direction of the PCM stream
348  * @action: Activate stream if 1. Deactivate if -1.
349  *
350  * Increments/Decrements the active count for all the DAIs and components
351  * attached to a PCM runtime.
352  * Should typically be called when a stream is opened.
353  *
354  * Must be called with the rtd->card->pcm_mutex being held
355  */
356 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
357                             int stream, int action)
358 {
359         struct snd_soc_component *component;
360         struct snd_soc_dai *dai;
361         int i;
362
363         snd_soc_dpcm_mutex_assert_held(rtd);
364
365         for_each_rtd_dais(rtd, i, dai)
366                 snd_soc_dai_action(dai, stream, action);
367
368         /* Increments/Decrements the active count for components without DAIs */
369         for_each_rtd_components(rtd, i, component) {
370                 if (component->num_dai)
371                         continue;
372                 component->active += action;
373         }
374 }
375 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
376
377 /**
378  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
379  * @rtd: The ASoC PCM runtime that should be checked.
380  *
381  * This function checks whether the power down delay should be ignored for a
382  * specific PCM runtime. Returns true if the delay is 0, if the DAI link has
383  * been configured to ignore the delay, or if none of the components benefits
384  * from having the delay.
385  */
386 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
387 {
388         struct snd_soc_component *component;
389         int i;
390
391         if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
392                 return true;
393
394         for_each_rtd_components(rtd, i, component)
395                 if (component->driver->use_pmdown_time)
396                         /* No need to go through all components */
397                         return false;
398
399         return true;
400 }
401
402 /* DPCM stream event, send event to FE and all active BEs. */
403 void dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, int event)
404 {
405         struct snd_soc_dpcm *dpcm;
406
407         snd_soc_dpcm_mutex_assert_held(fe);
408
409         for_each_dpcm_be(fe, dir, dpcm) {
410
411                 struct snd_soc_pcm_runtime *be = dpcm->be;
412
413                 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
414                                 be->dai_link->name, event, dir);
415
416                 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
417                     (be->dpcm[dir].users >= 1))
418                         continue;
419
420                 snd_soc_dapm_stream_event(be, dir, event);
421         }
422
423         snd_soc_dapm_stream_event(fe, dir, event);
424 }
425
426 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
427                                    struct snd_pcm_hw_params *params)
428 {
429         if (params) {
430                 dai->symmetric_rate        = params_rate(params);
431                 dai->symmetric_channels    = params_channels(params);
432                 dai->symmetric_sample_bits = snd_pcm_format_physical_width(params_format(params));
433         } else {
434                 dai->symmetric_rate        = 0;
435                 dai->symmetric_channels    = 0;
436                 dai->symmetric_sample_bits = 0;
437         }
438 }
439
440 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
441                                         struct snd_soc_dai *soc_dai)
442 {
443         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
444         int ret;
445
446         if (!snd_soc_dai_active(soc_dai))
447                 return 0;
448
449 #define __soc_pcm_apply_symmetry(name, NAME)                            \
450         if (soc_dai->symmetric_##name &&                                \
451             (soc_dai->driver->symmetric_##name || rtd->dai_link->symmetric_##name)) { \
452                 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
453                         #name, soc_dai->symmetric_##name);              \
454                                                                         \
455                 ret = snd_pcm_hw_constraint_single(substream->runtime,  \
456                                                    SNDRV_PCM_HW_PARAM_##NAME,\
457                                                    soc_dai->symmetric_##name);  \
458                 if (ret < 0)                                                    \
459                         return snd_soc_ret(soc_dai->dev, ret,                   \
460                                 "Unable to apply %s constraint\n", #name);      \
461         }
462
463         __soc_pcm_apply_symmetry(rate,          RATE);
464         __soc_pcm_apply_symmetry(channels,      CHANNELS);
465         __soc_pcm_apply_symmetry(sample_bits,   SAMPLE_BITS);
466
467         return 0;
468 }
469
470 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
471                                 struct snd_pcm_hw_params *params)
472 {
473         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
474         struct snd_soc_dai d;
475         struct snd_soc_dai *dai;
476         struct snd_soc_dai *cpu_dai;
477         unsigned int symmetry, i;
478
479         d.name = __func__;
480         soc_pcm_set_dai_params(&d, params);
481
482 #define __soc_pcm_params_symmetry(xxx)                                  \
483         symmetry = rtd->dai_link->symmetric_##xxx;                      \
484         for_each_rtd_dais(rtd, i, dai)                                  \
485                 symmetry |= dai->driver->symmetric_##xxx;               \
486                                                                         \
487         if (symmetry)                                                   \
488                 for_each_rtd_cpu_dais(rtd, i, cpu_dai)                  \
489                         if (!snd_soc_dai_is_dummy(cpu_dai) &&           \
490                             cpu_dai->symmetric_##xxx &&                 \
491                             cpu_dai->symmetric_##xxx != d.symmetric_##xxx) \
492                                 return snd_soc_ret(rtd->dev, -EINVAL,   \
493                                                    "unmatched %s symmetry: %s:%d - %s:%d\n", \
494                                                    #xxx, cpu_dai->name, cpu_dai->symmetric_##xxx, \
495                                                    d.name, d.symmetric_##xxx);
496
497         /* reject unmatched parameters when applying symmetry */
498         __soc_pcm_params_symmetry(rate);
499         __soc_pcm_params_symmetry(channels);
500         __soc_pcm_params_symmetry(sample_bits);
501
502         return 0;
503 }
504
505 static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
506 {
507         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
508         struct snd_soc_dai_link *link = rtd->dai_link;
509         struct snd_soc_dai *dai;
510         unsigned int symmetry, i;
511
512         symmetry = link->symmetric_rate ||
513                 link->symmetric_channels ||
514                 link->symmetric_sample_bits;
515
516         for_each_rtd_dais(rtd, i, dai)
517                 symmetry = symmetry ||
518                         dai->driver->symmetric_rate ||
519                         dai->driver->symmetric_channels ||
520                         dai->driver->symmetric_sample_bits;
521
522         if (symmetry)
523                 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
524 }
525
526 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
527 {
528         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
529         int ret;
530
531         if (!bits)
532                 return;
533
534         ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
535         if (ret != 0)
536                 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
537                                  bits, ret);
538 }
539
540 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
541 {
542         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
543         struct snd_soc_dai *cpu_dai;
544         struct snd_soc_dai *codec_dai;
545         int stream = substream->stream;
546         int i;
547         unsigned int bits = 0, cpu_bits = 0;
548
549         for_each_rtd_codec_dais(rtd, i, codec_dai) {
550                 const struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
551
552                 if (pcm_codec->sig_bits == 0) {
553                         bits = 0;
554                         break;
555                 }
556                 bits = max(pcm_codec->sig_bits, bits);
557         }
558
559         for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
560                 const struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
561
562                 if (pcm_cpu->sig_bits == 0) {
563                         cpu_bits = 0;
564                         break;
565                 }
566                 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
567         }
568
569         soc_pcm_set_msb(substream, bits);
570         soc_pcm_set_msb(substream, cpu_bits);
571 }
572
573 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
574 {
575         hw->rates               = UINT_MAX;
576         hw->rate_min            = 0;
577         hw->rate_max            = UINT_MAX;
578         hw->channels_min        = 0;
579         hw->channels_max        = UINT_MAX;
580         hw->formats             = ULLONG_MAX;
581 }
582
583 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
584                                    const struct snd_soc_pcm_stream *p)
585 {
586         hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
587
588         /* setup hw->rate_min/max via hw->rates first */
589         snd_pcm_hw_limit_rates(hw);
590
591         /* update hw->rate_min/max by snd_soc_pcm_stream */
592         hw->rate_min = max(hw->rate_min, p->rate_min);
593         hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
594 }
595
596 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
597                                    const struct snd_soc_pcm_stream *p)
598 {
599         hw->channels_min = max(hw->channels_min, p->channels_min);
600         hw->channels_max = min(hw->channels_max, p->channels_max);
601 }
602
603 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
604                                      const struct snd_soc_pcm_stream *p)
605 {
606         hw->formats     &= p->formats;
607         hw->subformats  &= p->subformats;
608 }
609
610 /**
611  * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
612  * @rtd: ASoC PCM runtime
613  * @hw: PCM hardware parameters (output)
614  * @stream: Direction of the PCM stream
615  *
616  * Calculates the subset of stream parameters supported by all DAIs
617  * associated with the PCM stream.
618  */
619 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
620                             struct snd_pcm_hardware *hw, int stream)
621 {
622         struct snd_soc_dai *codec_dai;
623         struct snd_soc_dai *cpu_dai;
624         const struct snd_soc_pcm_stream *codec_stream;
625         const struct snd_soc_pcm_stream *cpu_stream;
626         unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
627         int i;
628
629         soc_pcm_hw_init(hw);
630
631         /* first calculate min/max only for CPUs in the DAI link */
632         for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
633
634                 /*
635                  * Skip CPUs which don't support the current stream type.
636                  * Otherwise, since the rate, channel, and format values will
637                  * zero in that case, we would have no usable settings left,
638                  * causing the resulting setup to fail.
639                  */
640                 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
641                         continue;
642
643                 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
644
645                 soc_pcm_hw_update_chan(hw, cpu_stream);
646                 soc_pcm_hw_update_rate(hw, cpu_stream);
647                 soc_pcm_hw_update_format(hw, cpu_stream);
648         }
649         cpu_chan_min = hw->channels_min;
650         cpu_chan_max = hw->channels_max;
651
652         /* second calculate min/max only for CODECs in the DAI link */
653         for_each_rtd_codec_dais(rtd, i, codec_dai) {
654
655                 /*
656                  * Skip CODECs which don't support the current stream type.
657                  * Otherwise, since the rate, channel, and format values will
658                  * zero in that case, we would have no usable settings left,
659                  * causing the resulting setup to fail.
660                  */
661                 if (!snd_soc_dai_stream_valid(codec_dai, stream))
662                         continue;
663
664                 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
665
666                 soc_pcm_hw_update_chan(hw, codec_stream);
667                 soc_pcm_hw_update_rate(hw, codec_stream);
668                 soc_pcm_hw_update_format(hw, codec_stream);
669         }
670
671         /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
672         if (!hw->channels_min)
673                 return -EINVAL;
674
675         /*
676          * chan min/max cannot be enforced if there are multiple CODEC DAIs
677          * connected to CPU DAI(s), use CPU DAI's directly and let
678          * channel allocation be fixed up later
679          */
680         if (rtd->dai_link->num_codecs > 1) {
681                 hw->channels_min = cpu_chan_min;
682                 hw->channels_max = cpu_chan_max;
683         }
684
685         return 0;
686 }
687 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
688
689 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
690 {
691         struct snd_pcm_hardware *hw = &substream->runtime->hw;
692         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
693         u64 formats = hw->formats;
694
695         /*
696          * At least one CPU and one CODEC should match. Otherwise, we should
697          * have bailed out on a higher level, since there would be no CPU or
698          * CODEC to support the transfer direction in that case.
699          */
700         snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
701
702         if (formats)
703                 hw->formats &= formats;
704 }
705
706 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
707 {
708         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
709         struct snd_soc_component *component;
710         int i, ret = 0;
711
712         for_each_rtd_components(rtd, i, component) {
713                 ret = snd_soc_component_module_get_when_open(component, substream);
714                 if (ret < 0)
715                         break;
716
717                 ret = snd_soc_component_open(component, substream);
718                 if (ret < 0)
719                         break;
720         }
721
722         return ret;
723 }
724
725 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
726                                     int rollback)
727 {
728         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
729         struct snd_soc_component *component;
730         int i, ret = 0;
731
732         for_each_rtd_components(rtd, i, component) {
733                 int r = snd_soc_component_close(component, substream, rollback);
734                 if (r < 0)
735                         ret = r; /* use last ret */
736
737                 snd_soc_component_module_put_when_close(component, substream, rollback);
738         }
739
740         return ret;
741 }
742
743 static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
744                          struct snd_pcm_substream *substream, int rollback)
745 {
746         struct snd_soc_component *component;
747         struct snd_soc_dai *dai;
748         int i;
749
750         snd_soc_dpcm_mutex_assert_held(rtd);
751
752         if (!rollback) {
753                 snd_soc_runtime_deactivate(rtd, substream->stream);
754
755                 /* Make sure DAI parameters cleared if the DAI becomes inactive */
756                 for_each_rtd_dais(rtd, i, dai) {
757                         if (snd_soc_dai_active(dai) == 0)
758                                 soc_pcm_set_dai_params(dai, NULL);
759                 }
760         }
761
762         for_each_rtd_dais_reverse(rtd, i, dai)
763                 snd_soc_dai_shutdown(dai, substream, rollback);
764
765         snd_soc_link_shutdown(substream, rollback);
766
767         soc_pcm_components_close(substream, rollback);
768
769         snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
770
771         for_each_rtd_components(rtd, i, component)
772                 if (!snd_soc_component_active(component))
773                         pinctrl_pm_select_sleep_state(component->dev);
774
775         return 0;
776 }
777
778 /*
779  * Called by ALSA when a PCM substream is closed. Private data can be
780  * freed here. The cpu DAI, codec DAI, machine and components are also
781  * shutdown.
782  */
783 static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
784                            struct snd_pcm_substream *substream)
785 {
786         return soc_pcm_clean(rtd, substream, 0);
787 }
788
789 /* PCM close ops for non-DPCM streams */
790 static int soc_pcm_close(struct snd_pcm_substream *substream)
791 {
792         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
793
794         snd_soc_dpcm_mutex_lock(rtd);
795         __soc_pcm_close(rtd, substream);
796         snd_soc_dpcm_mutex_unlock(rtd);
797         return 0;
798 }
799
800 static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
801 {
802         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
803         struct snd_pcm_hardware *hw = &substream->runtime->hw;
804         const char *name_cpu = soc_cpu_dai_name(rtd);
805         const char *name_codec = soc_codec_dai_name(rtd);
806         const char *err_msg;
807         struct device *dev = rtd->dev;
808
809         err_msg = "rates";
810         if (!hw->rates)
811                 goto config_err;
812
813         err_msg = "formats";
814         if (!hw->formats)
815                 goto config_err;
816
817         err_msg = "channels";
818         if (!hw->channels_min || !hw->channels_max ||
819              hw->channels_min  >  hw->channels_max)
820                 goto config_err;
821
822         dev_dbg(dev, "ASoC: %s <-> %s info:\n",         name_codec,
823                                                         name_cpu);
824         dev_dbg(dev, "ASoC: rate mask 0x%x\n",          hw->rates);
825         dev_dbg(dev, "ASoC: ch   min %d max %d\n",      hw->channels_min,
826                                                         hw->channels_max);
827         dev_dbg(dev, "ASoC: rate min %d max %d\n",      hw->rate_min,
828                                                         hw->rate_max);
829
830         return 0;
831
832 config_err:
833         return snd_soc_ret(dev, -EINVAL,
834                         "%s <-> %s No matching %s\n", name_codec, name_cpu, err_msg);
835 }
836
837 /*
838  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
839  * then initialized and any private data can be allocated. This also calls
840  * startup for the cpu DAI, component, machine and codec DAI.
841  */
842 static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
843                           struct snd_pcm_substream *substream)
844 {
845         struct snd_soc_component *component;
846         struct snd_soc_dai *dai;
847         int i, ret = 0;
848
849         snd_soc_dpcm_mutex_assert_held(rtd);
850
851         for_each_rtd_components(rtd, i, component)
852                 pinctrl_pm_select_default_state(component->dev);
853
854         ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
855         if (ret < 0)
856                 goto err;
857
858         ret = soc_pcm_components_open(substream);
859         if (ret < 0)
860                 goto err;
861
862         ret = snd_soc_link_startup(substream);
863         if (ret < 0)
864                 goto err;
865
866         /* startup the audio subsystem */
867         for_each_rtd_dais(rtd, i, dai) {
868                 ret = snd_soc_dai_startup(dai, substream);
869                 if (ret < 0)
870                         goto err;
871         }
872
873         /* Dynamic PCM DAI links compat checks use dynamic capabilities */
874         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
875                 goto dynamic;
876
877         /* Check that the codec and cpu DAIs are compatible */
878         soc_pcm_init_runtime_hw(substream);
879
880         soc_pcm_update_symmetry(substream);
881
882         ret = soc_hw_sanity_check(substream);
883         if (ret < 0)
884                 goto err;
885
886         soc_pcm_apply_msb(substream);
887
888         /* Symmetry only applies if we've already got an active stream. */
889         for_each_rtd_dais(rtd, i, dai) {
890                 ret = soc_pcm_apply_symmetry(substream, dai);
891                 if (ret != 0)
892                         goto err;
893         }
894 dynamic:
895         snd_soc_runtime_activate(rtd, substream->stream);
896         ret = 0;
897 err:
898         if (ret < 0)
899                 soc_pcm_clean(rtd, substream, 1);
900
901         return soc_pcm_ret(rtd, ret);
902 }
903
904 /* PCM open ops for non-DPCM streams */
905 static int soc_pcm_open(struct snd_pcm_substream *substream)
906 {
907         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
908         int ret;
909
910         snd_soc_dpcm_mutex_lock(rtd);
911         ret = __soc_pcm_open(rtd, substream);
912         snd_soc_dpcm_mutex_unlock(rtd);
913         return ret;
914 }
915
916 /*
917  * Called by ALSA when the PCM substream is prepared, can set format, sample
918  * rate, etc.  This function is non atomic and can be called multiple times,
919  * it can refer to the runtime info.
920  */
921 static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
922                              struct snd_pcm_substream *substream)
923 {
924         struct snd_soc_dai *dai;
925         int i, ret = 0;
926
927         snd_soc_dpcm_mutex_assert_held(rtd);
928
929         ret = snd_soc_link_prepare(substream);
930         if (ret < 0)
931                 goto out;
932
933         ret = snd_soc_pcm_component_prepare(substream);
934         if (ret < 0)
935                 goto out;
936
937         ret = snd_soc_pcm_dai_prepare(substream);
938         if (ret < 0)
939                 goto out;
940
941         /* cancel any delayed stream shutdown that is pending */
942         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
943             rtd->pop_wait) {
944                 rtd->pop_wait = 0;
945                 cancel_delayed_work(&rtd->delayed_work);
946         }
947
948         snd_soc_dapm_stream_event(rtd, substream->stream,
949                         SND_SOC_DAPM_STREAM_START);
950
951         for_each_rtd_dais(rtd, i, dai) {
952                 if (!snd_soc_dai_mute_is_ctrled_at_trigger(dai))
953                         snd_soc_dai_digital_mute(dai, 0, substream->stream);
954         }
955
956 out:
957         /*
958          * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
959          *
960          * We don't want to log an error since we do not want to give userspace a way to do a
961          * denial-of-service attack on the syslog / diskspace.
962          */
963         return ret;
964 }
965
966 /* PCM prepare ops for non-DPCM streams */
967 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
968 {
969         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
970         int ret;
971
972         snd_soc_dpcm_mutex_lock(rtd);
973         ret = __soc_pcm_prepare(rtd, substream);
974         snd_soc_dpcm_mutex_unlock(rtd);
975
976         /*
977          * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
978          *
979          * We don't want to log an error since we do not want to give userspace a way to do a
980          * denial-of-service attack on the syslog / diskspace.
981          */
982         return ret;
983 }
984
985 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
986                                        unsigned int mask)
987 {
988         struct snd_interval *interval;
989         int channels = hweight_long(mask);
990
991         interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
992         interval->min = channels;
993         interval->max = channels;
994 }
995
996 static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
997                             struct snd_pcm_substream *substream, int rollback)
998 {
999         struct snd_soc_dai *dai;
1000         int i;
1001
1002         snd_soc_dpcm_mutex_assert_held(rtd);
1003
1004         /* clear the corresponding DAIs parameters when going to be inactive */
1005         for_each_rtd_dais(rtd, i, dai) {
1006                 if (snd_soc_dai_active(dai) == 1)
1007                         soc_pcm_set_dai_params(dai, NULL);
1008
1009                 if (snd_soc_dai_stream_active(dai, substream->stream) == 1) {
1010                         if (!snd_soc_dai_mute_is_ctrled_at_trigger(dai))
1011                                 snd_soc_dai_digital_mute(dai, 1, substream->stream);
1012                 }
1013         }
1014
1015         /* run the stream event */
1016         snd_soc_dapm_stream_stop(rtd, substream->stream);
1017
1018         /* free any machine hw params */
1019         snd_soc_link_hw_free(substream, rollback);
1020
1021         /* free any component resources */
1022         snd_soc_pcm_component_hw_free(substream, rollback);
1023
1024         /* now free hw params for the DAIs  */
1025         for_each_rtd_dais(rtd, i, dai)
1026                 if (snd_soc_dai_stream_valid(dai, substream->stream))
1027                         snd_soc_dai_hw_free(dai, substream, rollback);
1028
1029         return 0;
1030 }
1031
1032 /*
1033  * Frees resources allocated by hw_params, can be called multiple times
1034  */
1035 static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
1036                              struct snd_pcm_substream *substream)
1037 {
1038         return soc_pcm_hw_clean(rtd, substream, 0);
1039 }
1040
1041 /* hw_free PCM ops for non-DPCM streams */
1042 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1043 {
1044         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1045         int ret;
1046
1047         snd_soc_dpcm_mutex_lock(rtd);
1048         ret = __soc_pcm_hw_free(rtd, substream);
1049         snd_soc_dpcm_mutex_unlock(rtd);
1050         return ret;
1051 }
1052
1053 /*
1054  * Called by ALSA when the hardware params are set by application. This
1055  * function can also be called multiple times and can allocate buffers
1056  * (using snd_pcm_lib_* ). It's non-atomic.
1057  */
1058 static int __soc_pcm_hw_params(struct snd_pcm_substream *substream,
1059                                struct snd_pcm_hw_params *params)
1060 {
1061         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1062         struct snd_soc_dai *cpu_dai;
1063         struct snd_soc_dai *codec_dai;
1064         struct snd_pcm_hw_params tmp_params;
1065         int i, ret = 0;
1066
1067         snd_soc_dpcm_mutex_assert_held(rtd);
1068
1069         ret = soc_pcm_params_symmetry(substream, params);
1070         if (ret)
1071                 goto out;
1072
1073         ret = snd_soc_link_hw_params(substream, params);
1074         if (ret < 0)
1075                 goto out;
1076
1077         for_each_rtd_codec_dais(rtd, i, codec_dai) {
1078                 unsigned int tdm_mask = snd_soc_dai_tdm_mask_get(codec_dai, substream->stream);
1079
1080                 /*
1081                  * Skip CODECs which don't support the current stream type,
1082                  * the idea being that if a CODEC is not used for the currently
1083                  * set up transfer direction, it should not need to be
1084                  * configured, especially since the configuration used might
1085                  * not even be supported by that CODEC. There may be cases
1086                  * however where a CODEC needs to be set up although it is
1087                  * actually not being used for the transfer, e.g. if a
1088                  * capture-only CODEC is acting as an LRCLK and/or BCLK master
1089                  * for the DAI link including a playback-only CODEC.
1090                  * If this becomes necessary, we will have to augment the
1091                  * machine driver setup with information on how to act, so
1092                  * we can do the right thing here.
1093                  */
1094                 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1095                         continue;
1096
1097                 /* copy params for each codec */
1098                 tmp_params = *params;
1099
1100                 /* fixup params based on TDM slot masks */
1101                 if (tdm_mask)
1102                         soc_pcm_codec_params_fixup(&tmp_params, tdm_mask);
1103
1104                 ret = snd_soc_dai_hw_params(codec_dai, substream,
1105                                             &tmp_params);
1106                 if(ret < 0)
1107                         goto out;
1108
1109                 soc_pcm_set_dai_params(codec_dai, &tmp_params);
1110                 snd_soc_dapm_update_dai(substream, &tmp_params, codec_dai);
1111         }
1112
1113         for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1114                 struct snd_soc_dai_link_ch_map *ch_maps;
1115                 unsigned int ch_mask = 0;
1116                 int j;
1117
1118                 /*
1119                  * Skip CPUs which don't support the current stream
1120                  * type. See soc_pcm_init_runtime_hw() for more details
1121                  */
1122                 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1123                         continue;
1124
1125                 /* copy params for each cpu */
1126                 tmp_params = *params;
1127
1128                 /*
1129                  * construct cpu channel mask by combining ch_mask of each
1130                  * codec which maps to the cpu.
1131                  * see
1132                  *      soc.h :: [dai_link->ch_maps Image sample]
1133                  */
1134                 for_each_rtd_ch_maps(rtd, j, ch_maps)
1135                         if (ch_maps->cpu == i)
1136                                 ch_mask |= ch_maps->ch_mask;
1137
1138                 /* fixup cpu channel number */
1139                 if (ch_mask)
1140                         soc_pcm_codec_params_fixup(&tmp_params, ch_mask);
1141
1142                 ret = snd_soc_dai_hw_params(cpu_dai, substream, &tmp_params);
1143                 if (ret < 0)
1144                         goto out;
1145
1146                 /* store the parameters for each DAI */
1147                 soc_pcm_set_dai_params(cpu_dai, &tmp_params);
1148                 snd_soc_dapm_update_dai(substream, &tmp_params, cpu_dai);
1149         }
1150
1151         ret = snd_soc_pcm_component_hw_params(substream, params);
1152 out:
1153         if (ret < 0)
1154                 soc_pcm_hw_clean(rtd, substream, 1);
1155
1156         return soc_pcm_ret(rtd, ret);
1157 }
1158
1159 /* hw_params PCM ops for non-DPCM streams */
1160 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1161                              struct snd_pcm_hw_params *params)
1162 {
1163         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1164         int ret;
1165
1166         snd_soc_dpcm_mutex_lock(rtd);
1167         ret = __soc_pcm_hw_params(substream, params);
1168         snd_soc_dpcm_mutex_unlock(rtd);
1169         return ret;
1170 }
1171
1172 #define TRIGGER_MAX 3
1173 static int (* const trigger[][TRIGGER_MAX])(struct snd_pcm_substream *substream, int cmd, int rollback) = {
1174         [SND_SOC_TRIGGER_ORDER_DEFAULT] = {
1175                 snd_soc_link_trigger,
1176                 snd_soc_pcm_component_trigger,
1177                 snd_soc_pcm_dai_trigger,
1178         },
1179         [SND_SOC_TRIGGER_ORDER_LDC] = {
1180                 snd_soc_link_trigger,
1181                 snd_soc_pcm_dai_trigger,
1182                 snd_soc_pcm_component_trigger,
1183         },
1184 };
1185
1186 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1187 {
1188         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1189         struct snd_soc_component *component;
1190         int ret = 0, r = 0, i;
1191         int rollback = 0;
1192         int start = 0, stop = 0;
1193
1194         /*
1195          * select START/STOP sequence
1196          */
1197         for_each_rtd_components(rtd, i, component) {
1198                 if (component->driver->trigger_start)
1199                         start = component->driver->trigger_start;
1200                 if (component->driver->trigger_stop)
1201                         stop = component->driver->trigger_stop;
1202         }
1203         if (rtd->dai_link->trigger_start)
1204                 start = rtd->dai_link->trigger_start;
1205         if (rtd->dai_link->trigger_stop)
1206                 stop  = rtd->dai_link->trigger_stop;
1207
1208         if (start < 0 || start >= SND_SOC_TRIGGER_ORDER_MAX ||
1209             stop  < 0 || stop  >= SND_SOC_TRIGGER_ORDER_MAX)
1210                 return -EINVAL;
1211
1212         /*
1213          * START
1214          */
1215         switch (cmd) {
1216         case SNDRV_PCM_TRIGGER_START:
1217         case SNDRV_PCM_TRIGGER_RESUME:
1218         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1219                 for (i = 0; i < TRIGGER_MAX; i++) {
1220                         r = trigger[start][i](substream, cmd, 0);
1221                         if (r < 0)
1222                                 break;
1223                 }
1224         }
1225
1226         /*
1227          * Rollback if START failed
1228          * find correspond STOP command
1229          */
1230         if (r < 0) {
1231                 rollback = 1;
1232                 ret = r;
1233                 switch (cmd) {
1234                 case SNDRV_PCM_TRIGGER_START:
1235                         cmd = SNDRV_PCM_TRIGGER_STOP;
1236                         break;
1237                 case SNDRV_PCM_TRIGGER_RESUME:
1238                         cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1239                         break;
1240                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1241                         cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1242                         break;
1243                 }
1244         }
1245
1246         /*
1247          * STOP
1248          */
1249         switch (cmd) {
1250         case SNDRV_PCM_TRIGGER_STOP:
1251         case SNDRV_PCM_TRIGGER_SUSPEND:
1252         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1253                 for (i = TRIGGER_MAX; i > 0; i--) {
1254                         r = trigger[stop][i - 1](substream, cmd, rollback);
1255                         if (r < 0)
1256                                 ret = r;
1257                 }
1258         }
1259
1260         return ret;
1261 }
1262
1263 /*
1264  * soc level wrapper for pointer callback
1265  * If cpu_dai, codec_dai, component driver has the delay callback, then
1266  * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1267  */
1268 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1269 {
1270         struct snd_pcm_runtime *runtime = substream->runtime;
1271         snd_pcm_uframes_t offset = 0;
1272         snd_pcm_sframes_t codec_delay = 0;
1273         snd_pcm_sframes_t cpu_delay = 0;
1274
1275         offset = snd_soc_pcm_component_pointer(substream);
1276
1277         /* should be called *after* snd_soc_pcm_component_pointer() */
1278         snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1279         snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1280
1281         runtime->delay = cpu_delay + codec_delay;
1282
1283         return offset;
1284 }
1285
1286 /* connect a FE and BE */
1287 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1288                 struct snd_soc_pcm_runtime *be, int stream)
1289 {
1290         struct snd_pcm_substream *fe_substream;
1291         struct snd_pcm_substream *be_substream;
1292         struct snd_soc_dpcm *dpcm;
1293
1294         snd_soc_dpcm_mutex_assert_held(fe);
1295
1296         /* only add new dpcms */
1297         for_each_dpcm_be(fe, stream, dpcm)
1298                 if (dpcm->be == be)
1299                         return 0;
1300
1301         fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1302         be_substream = snd_soc_dpcm_get_substream(be, stream);
1303
1304         if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic)
1305                 return snd_soc_ret(be->dev, -EINVAL,
1306                         "%s: %s is atomic but %s is nonatomic, invalid configuration\n",
1307                                    __func__, fe->dai_link->name, be->dai_link->name);
1308
1309         if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
1310                 dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1311                 be_substream->pcm->nonatomic = 1;
1312         }
1313
1314         dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1315         if (!dpcm)
1316                 return -ENOMEM;
1317
1318         dpcm->be = be;
1319         dpcm->fe = fe;
1320         dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1321         snd_pcm_stream_lock_irq(fe_substream);
1322         list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1323         list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1324         snd_pcm_stream_unlock_irq(fe_substream);
1325
1326         dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1327                         snd_pcm_direction_name(stream),  fe->dai_link->name,
1328                         stream ? "<-" : "->", be->dai_link->name);
1329
1330         dpcm_create_debugfs_state(dpcm, stream);
1331
1332         return 1;
1333 }
1334
1335 /* reparent a BE onto another FE */
1336 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1337                         struct snd_soc_pcm_runtime *be, int stream)
1338 {
1339         struct snd_soc_dpcm *dpcm;
1340         struct snd_pcm_substream *fe_substream, *be_substream;
1341
1342         /* reparent if BE is connected to other FEs */
1343         if (!be->dpcm[stream].users)
1344                 return;
1345
1346         be_substream = snd_soc_dpcm_get_substream(be, stream);
1347         if (!be_substream)
1348                 return;
1349
1350         for_each_dpcm_fe(be, stream, dpcm) {
1351                 if (dpcm->fe == fe)
1352                         continue;
1353
1354                 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1355                         snd_pcm_direction_name(stream),
1356                         dpcm->fe->dai_link->name,
1357                         stream ? "<-" : "->", dpcm->be->dai_link->name);
1358
1359                 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1360                 be_substream->runtime = fe_substream->runtime;
1361                 break;
1362         }
1363 }
1364
1365 /* disconnect a BE and FE */
1366 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1367 {
1368         struct snd_soc_dpcm *dpcm, *d;
1369         struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
1370         LIST_HEAD(deleted_dpcms);
1371
1372         snd_soc_dpcm_mutex_assert_held(fe);
1373
1374         snd_pcm_stream_lock_irq(substream);
1375         for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1376                 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1377                                 snd_pcm_direction_name(stream),
1378                                 dpcm->be->dai_link->name);
1379
1380                 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1381                         continue;
1382
1383                 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1384                         snd_pcm_direction_name(stream), fe->dai_link->name,
1385                         stream ? "<-" : "->", dpcm->be->dai_link->name);
1386
1387                 /* BEs still alive need new FE */
1388                 dpcm_be_reparent(fe, dpcm->be, stream);
1389
1390                 list_del(&dpcm->list_be);
1391                 list_move(&dpcm->list_fe, &deleted_dpcms);
1392         }
1393         snd_pcm_stream_unlock_irq(substream);
1394
1395         while (!list_empty(&deleted_dpcms)) {
1396                 dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
1397                                         list_fe);
1398                 list_del(&dpcm->list_fe);
1399                 dpcm_remove_debugfs_state(dpcm);
1400                 kfree(dpcm);
1401         }
1402 }
1403
1404 /* get BE for DAI widget and stream */
1405 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1406                 struct snd_soc_dapm_widget *widget, int stream)
1407 {
1408         struct snd_soc_pcm_runtime *be;
1409         struct snd_soc_dapm_widget *w;
1410         struct snd_soc_dai *dai;
1411         int i;
1412
1413         dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1414
1415         for_each_card_rtds(card, be) {
1416
1417                 if (!be->dai_link->no_pcm)
1418                         continue;
1419
1420                 if (!snd_soc_dpcm_get_substream(be, stream))
1421                         continue;
1422
1423                 for_each_rtd_dais(be, i, dai) {
1424                         w = snd_soc_dai_get_widget(dai, stream);
1425
1426                         dev_dbg(card->dev, "ASoC: try BE : %s\n",
1427                                 w ? w->name : "(not set)");
1428
1429                         if (w == widget)
1430                                 return be;
1431                 }
1432         }
1433
1434         /* Widget provided is not a BE */
1435         return NULL;
1436 }
1437
1438 int widget_in_list(struct snd_soc_dapm_widget_list *list,
1439                 struct snd_soc_dapm_widget *widget)
1440 {
1441         struct snd_soc_dapm_widget *w;
1442         int i;
1443
1444         for_each_dapm_widgets(list, i, w)
1445                 if (widget == w)
1446                         return 1;
1447
1448         return 0;
1449 }
1450 EXPORT_SYMBOL_GPL(widget_in_list);
1451
1452 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
1453 {
1454         struct snd_soc_card *card = widget->dapm->card;
1455         struct snd_soc_pcm_runtime *rtd;
1456         int stream;
1457
1458         /* adjust dir to stream */
1459         if (dir == SND_SOC_DAPM_DIR_OUT)
1460                 stream = SNDRV_PCM_STREAM_PLAYBACK;
1461         else
1462                 stream = SNDRV_PCM_STREAM_CAPTURE;
1463
1464         rtd = dpcm_get_be(card, widget, stream);
1465         if (rtd)
1466                 return true;
1467
1468         return false;
1469 }
1470 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
1471
1472 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1473         int stream, struct snd_soc_dapm_widget_list **list)
1474 {
1475         struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
1476         int paths;
1477
1478         if (fe->dai_link->num_cpus > 1)
1479                 return snd_soc_ret(fe->dev, -EINVAL,
1480                         "%s doesn't support Multi CPU yet\n", __func__);
1481
1482         /* get number of valid DAI paths and their widgets */
1483         paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1484                         fe->card->component_chaining ?
1485                                 NULL : dpcm_end_walk_at_be);
1486
1487         if (paths > 0)
1488                 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1489                         snd_pcm_direction_name(stream));
1490         else if (paths == 0)
1491                 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1492                         snd_pcm_direction_name(stream));
1493
1494         return paths;
1495 }
1496
1497 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1498 {
1499         snd_soc_dapm_dai_free_widgets(list);
1500 }
1501
1502 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1503                               struct snd_soc_dapm_widget_list *list)
1504 {
1505         struct snd_soc_dai *dai;
1506         unsigned int i;
1507
1508         /* is there a valid DAI widget for this BE */
1509         for_each_rtd_dais(dpcm->be, i, dai) {
1510                 struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
1511
1512                 /*
1513                  * The BE is pruned only if none of the dai
1514                  * widgets are in the active list.
1515                  */
1516                 if (widget && widget_in_list(list, widget))
1517                         return true;
1518         }
1519
1520         return false;
1521 }
1522
1523 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1524                             struct snd_soc_dapm_widget_list **list_)
1525 {
1526         struct snd_soc_dpcm *dpcm;
1527         int prune = 0;
1528
1529         /* Destroy any old FE <--> BE connections */
1530         for_each_dpcm_be(fe, stream, dpcm) {
1531                 if (dpcm_be_is_active(dpcm, stream, *list_))
1532                         continue;
1533
1534                 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1535                         snd_pcm_direction_name(stream),
1536                         dpcm->be->dai_link->name, fe->dai_link->name);
1537                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1538                 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1539                 prune++;
1540         }
1541
1542         dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1543         return prune;
1544 }
1545
1546 int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1547                    struct snd_soc_dapm_widget_list **list_)
1548 {
1549         struct snd_soc_card *card = fe->card;
1550         struct snd_soc_dapm_widget_list *list = *list_;
1551         struct snd_soc_pcm_runtime *be;
1552         struct snd_soc_dapm_widget *widget;
1553         struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1554         int i, new = 0, err;
1555
1556         /* don't connect if FE is not running */
1557         if (!fe_substream->runtime && !fe->fe_compr)
1558                 return new;
1559
1560         /* Create any new FE <--> BE connections */
1561         for_each_dapm_widgets(list, i, widget) {
1562
1563                 switch (widget->id) {
1564                 case snd_soc_dapm_dai_in:
1565                         if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1566                                 continue;
1567                         break;
1568                 case snd_soc_dapm_dai_out:
1569                         if (stream != SNDRV_PCM_STREAM_CAPTURE)
1570                                 continue;
1571                         break;
1572                 default:
1573                         continue;
1574                 }
1575
1576                 /* is there a valid BE rtd for this widget */
1577                 be = dpcm_get_be(card, widget, stream);
1578                 if (!be) {
1579                         dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1580                                 widget->name);
1581                         continue;
1582                 }
1583
1584                 /*
1585                  * Filter for systems with 'component_chaining' enabled.
1586                  * This helps to avoid unnecessary re-configuration of an
1587                  * already active BE on such systems and ensures the BE DAI
1588                  * widget is powered ON after hw_params() BE DAI callback.
1589                  */
1590                 if (fe->card->component_chaining &&
1591                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1592                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1593                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1594                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1595                         continue;
1596
1597                 /* newly connected FE and BE */
1598                 err = dpcm_be_connect(fe, be, stream);
1599                 if (err < 0) {
1600                         dev_err(fe->dev, "ASoC: can't connect %s\n",
1601                                 widget->name);
1602                         break;
1603                 } else if (err == 0) /* already connected */
1604                         continue;
1605
1606                 /* new */
1607                 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1608                 new++;
1609         }
1610
1611         dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1612         return new;
1613 }
1614
1615 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1616 {
1617         struct snd_soc_dpcm *dpcm;
1618
1619         for_each_dpcm_be(fe, stream, dpcm)
1620                 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1621 }
1622
1623 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1624                       int do_hw_free, struct snd_soc_dpcm *last)
1625 {
1626         struct snd_soc_dpcm *dpcm;
1627
1628         /* disable any enabled and non active backends */
1629         for_each_dpcm_be(fe, stream, dpcm) {
1630                 struct snd_soc_pcm_runtime *be = dpcm->be;
1631                 struct snd_pcm_substream *be_substream =
1632                         snd_soc_dpcm_get_substream(be, stream);
1633
1634                 if (dpcm == last)
1635                         return;
1636
1637                 /* is this op for this BE ? */
1638                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
1639                         continue;
1640
1641                 if (be->dpcm[stream].users == 0) {
1642                         dev_err(be->dev, "ASoC: no users %s at close - state %s\n",
1643                                 snd_pcm_direction_name(stream),
1644                                 dpcm_state_string(be->dpcm[stream].state));
1645                         continue;
1646                 }
1647
1648                 if (--be->dpcm[stream].users != 0)
1649                         continue;
1650
1651                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1652                         if (!do_hw_free)
1653                                 continue;
1654
1655                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1656                                 __soc_pcm_hw_free(be, be_substream);
1657                                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1658                         }
1659                 }
1660
1661                 __soc_pcm_close(be, be_substream);
1662                 be_substream->runtime = NULL;
1663                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1664         }
1665 }
1666
1667 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1668 {
1669         struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1670         struct snd_soc_pcm_runtime *be;
1671         struct snd_soc_dpcm *dpcm;
1672         int err, count = 0;
1673
1674         /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1675         for_each_dpcm_be(fe, stream, dpcm) {
1676                 struct snd_pcm_substream *be_substream;
1677
1678                 be = dpcm->be;
1679                 be_substream = snd_soc_dpcm_get_substream(be, stream);
1680
1681                 if (!be_substream) {
1682                         dev_err(be->dev, "ASoC: no backend %s stream\n",
1683                                 snd_pcm_direction_name(stream));
1684                         continue;
1685                 }
1686
1687                 /* is this op for this BE ? */
1688                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
1689                         continue;
1690
1691                 /* first time the dpcm is open ? */
1692                 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1693                         dev_err(be->dev, "ASoC: too many users %s at open %s\n",
1694                                 snd_pcm_direction_name(stream),
1695                                 dpcm_state_string(be->dpcm[stream].state));
1696                         continue;
1697                 }
1698
1699                 if (be->dpcm[stream].users++ != 0)
1700                         continue;
1701
1702                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1703                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1704                         continue;
1705
1706                 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1707                         snd_pcm_direction_name(stream), be->dai_link->name);
1708
1709                 be_substream->runtime = fe_substream->runtime;
1710                 err = __soc_pcm_open(be, be_substream);
1711                 if (err < 0) {
1712                         be->dpcm[stream].users--;
1713                         if (be->dpcm[stream].users < 0)
1714                                 dev_err(be->dev, "ASoC: no users %s at unwind %s\n",
1715                                         snd_pcm_direction_name(stream),
1716                                         dpcm_state_string(be->dpcm[stream].state));
1717
1718                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1719                         goto unwind;
1720                 }
1721                 be->dpcm[stream].be_start = 0;
1722                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1723                 count++;
1724         }
1725
1726         return count;
1727
1728 unwind:
1729         dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1730
1731         return soc_pcm_ret(fe, err);
1732 }
1733
1734 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1735 {
1736         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1737         struct snd_pcm_runtime *runtime = substream->runtime;
1738         struct snd_pcm_hardware *hw = &runtime->hw;
1739         struct snd_soc_dai *dai;
1740         int stream = substream->stream;
1741         u64 formats = hw->formats;
1742         int i;
1743
1744         soc_pcm_hw_init(hw);
1745
1746         if (formats)
1747                 hw->formats &= formats;
1748
1749         for_each_rtd_cpu_dais(fe, i, dai) {
1750                 const struct snd_soc_pcm_stream *cpu_stream;
1751
1752                 /*
1753                  * Skip CPUs which don't support the current stream
1754                  * type. See soc_pcm_init_runtime_hw() for more details
1755                  */
1756                 if (!snd_soc_dai_stream_valid(dai, stream))
1757                         continue;
1758
1759                 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1760
1761                 soc_pcm_hw_update_rate(hw, cpu_stream);
1762                 soc_pcm_hw_update_chan(hw, cpu_stream);
1763                 soc_pcm_hw_update_format(hw, cpu_stream);
1764         }
1765
1766 }
1767
1768 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1769 {
1770         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1771         struct snd_pcm_runtime *runtime = substream->runtime;
1772         struct snd_pcm_hardware *hw = &runtime->hw;
1773         struct snd_soc_dpcm *dpcm;
1774         struct snd_soc_dai *dai;
1775         int stream = substream->stream;
1776
1777         if (!fe->dai_link->dpcm_merged_format)
1778                 return;
1779
1780         /*
1781          * It returns merged BE codec format
1782          * if FE want to use it (= dpcm_merged_format)
1783          */
1784
1785         for_each_dpcm_be(fe, stream, dpcm) {
1786                 struct snd_soc_pcm_runtime *be = dpcm->be;
1787                 const struct snd_soc_pcm_stream *codec_stream;
1788                 int i;
1789
1790                 for_each_rtd_codec_dais(be, i, dai) {
1791                         /*
1792                          * Skip CODECs which don't support the current stream
1793                          * type. See soc_pcm_init_runtime_hw() for more details
1794                          */
1795                         if (!snd_soc_dai_stream_valid(dai, stream))
1796                                 continue;
1797
1798                         codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1799
1800                         soc_pcm_hw_update_format(hw, codec_stream);
1801                 }
1802         }
1803 }
1804
1805 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1806 {
1807         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1808         struct snd_pcm_runtime *runtime = substream->runtime;
1809         struct snd_pcm_hardware *hw = &runtime->hw;
1810         struct snd_soc_dpcm *dpcm;
1811         int stream = substream->stream;
1812
1813         if (!fe->dai_link->dpcm_merged_chan)
1814                 return;
1815
1816         /*
1817          * It returns merged BE codec channel;
1818          * if FE want to use it (= dpcm_merged_chan)
1819          */
1820
1821         for_each_dpcm_be(fe, stream, dpcm) {
1822                 struct snd_soc_pcm_runtime *be = dpcm->be;
1823                 const struct snd_soc_pcm_stream *cpu_stream;
1824                 struct snd_soc_dai *dai;
1825                 int i;
1826
1827                 for_each_rtd_cpu_dais(be, i, dai) {
1828                         /*
1829                          * Skip CPUs which don't support the current stream
1830                          * type. See soc_pcm_init_runtime_hw() for more details
1831                          */
1832                         if (!snd_soc_dai_stream_valid(dai, stream))
1833                                 continue;
1834
1835                         cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1836
1837                         soc_pcm_hw_update_chan(hw, cpu_stream);
1838                 }
1839
1840                 /*
1841                  * chan min/max cannot be enforced if there are multiple CODEC
1842                  * DAIs connected to a single CPU DAI, use CPU DAI's directly
1843                  */
1844                 if (be->dai_link->num_codecs == 1) {
1845                         const struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
1846                                 snd_soc_rtd_to_codec(be, 0), stream);
1847
1848                         soc_pcm_hw_update_chan(hw, codec_stream);
1849                 }
1850         }
1851 }
1852
1853 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1854 {
1855         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1856         struct snd_pcm_runtime *runtime = substream->runtime;
1857         struct snd_pcm_hardware *hw = &runtime->hw;
1858         struct snd_soc_dpcm *dpcm;
1859         int stream = substream->stream;
1860
1861         if (!fe->dai_link->dpcm_merged_rate)
1862                 return;
1863
1864         /*
1865          * It returns merged BE codec channel;
1866          * if FE want to use it (= dpcm_merged_chan)
1867          */
1868
1869         for_each_dpcm_be(fe, stream, dpcm) {
1870                 struct snd_soc_pcm_runtime *be = dpcm->be;
1871                 const struct snd_soc_pcm_stream *pcm;
1872                 struct snd_soc_dai *dai;
1873                 int i;
1874
1875                 for_each_rtd_dais(be, i, dai) {
1876                         /*
1877                          * Skip DAIs which don't support the current stream
1878                          * type. See soc_pcm_init_runtime_hw() for more details
1879                          */
1880                         if (!snd_soc_dai_stream_valid(dai, stream))
1881                                 continue;
1882
1883                         pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1884
1885                         soc_pcm_hw_update_rate(hw, pcm);
1886                 }
1887         }
1888 }
1889
1890 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1891                                int stream)
1892 {
1893         struct snd_soc_dpcm *dpcm;
1894         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
1895         struct snd_soc_dai *fe_cpu_dai;
1896         int err = 0;
1897         int i;
1898
1899         /* apply symmetry for FE */
1900         soc_pcm_update_symmetry(fe_substream);
1901
1902         for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1903                 /* Symmetry only applies if we've got an active stream. */
1904                 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1905                 if (err < 0)
1906                         goto error;
1907         }
1908
1909         /* apply symmetry for BE */
1910         for_each_dpcm_be(fe, stream, dpcm) {
1911                 struct snd_soc_pcm_runtime *be = dpcm->be;
1912                 struct snd_pcm_substream *be_substream =
1913                         snd_soc_dpcm_get_substream(be, stream);
1914                 struct snd_soc_pcm_runtime *rtd;
1915                 struct snd_soc_dai *dai;
1916
1917                 /* A backend may not have the requested substream */
1918                 if (!be_substream)
1919                         continue;
1920
1921                 rtd = snd_soc_substream_to_rtd(be_substream);
1922                 if (rtd->dai_link->be_hw_params_fixup)
1923                         continue;
1924
1925                 soc_pcm_update_symmetry(be_substream);
1926
1927                 /* Symmetry only applies if we've got an active stream. */
1928                 for_each_rtd_dais(rtd, i, dai) {
1929                         err = soc_pcm_apply_symmetry(fe_substream, dai);
1930                         if (err < 0)
1931                                 goto error;
1932                 }
1933         }
1934 error:
1935         return soc_pcm_ret(fe, err);
1936 }
1937
1938 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1939 {
1940         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
1941         int stream = fe_substream->stream, ret = 0;
1942
1943         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1944
1945         ret = dpcm_be_dai_startup(fe, stream);
1946         if (ret < 0)
1947                 goto be_err;
1948
1949         dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1950
1951         /* start the DAI frontend */
1952         ret = __soc_pcm_open(fe, fe_substream);
1953         if (ret < 0)
1954                 goto unwind;
1955
1956         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1957
1958         dpcm_runtime_setup_fe(fe_substream);
1959
1960         dpcm_runtime_setup_be_format(fe_substream);
1961         dpcm_runtime_setup_be_chan(fe_substream);
1962         dpcm_runtime_setup_be_rate(fe_substream);
1963
1964         ret = dpcm_apply_symmetry(fe_substream, stream);
1965
1966 unwind:
1967         if (ret < 0)
1968                 dpcm_be_dai_startup_unwind(fe, stream);
1969 be_err:
1970         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1971
1972         return soc_pcm_ret(fe, ret);
1973 }
1974
1975 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1976 {
1977         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1978         int stream = substream->stream;
1979
1980         snd_soc_dpcm_mutex_assert_held(fe);
1981
1982         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1983
1984         /* shutdown the BEs */
1985         dpcm_be_dai_shutdown(fe, stream);
1986
1987         dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1988
1989         /* now shutdown the frontend */
1990         __soc_pcm_close(fe, substream);
1991
1992         /* run the stream stop event */
1993         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1994
1995         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1996         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1997         return 0;
1998 }
1999
2000 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2001 {
2002         struct snd_soc_dpcm *dpcm;
2003
2004         /* only hw_params backends that are either sinks or sources
2005          * to this frontend DAI */
2006         for_each_dpcm_be(fe, stream, dpcm) {
2007
2008                 struct snd_soc_pcm_runtime *be = dpcm->be;
2009                 struct snd_pcm_substream *be_substream =
2010                         snd_soc_dpcm_get_substream(be, stream);
2011
2012                 /* is this op for this BE ? */
2013                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2014                         continue;
2015
2016                 /* only free hw when no longer used - check all FEs */
2017                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2018                                 continue;
2019
2020                 /* do not free hw if this BE is used by other FE */
2021                 if (be->dpcm[stream].users > 1)
2022                         continue;
2023
2024                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2025                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2026                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2027                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2028                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2029                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2030                         continue;
2031
2032                 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2033                         be->dai_link->name);
2034
2035                 __soc_pcm_hw_free(be, be_substream);
2036
2037                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2038         }
2039 }
2040
2041 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2042 {
2043         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2044         int stream = substream->stream;
2045
2046         snd_soc_dpcm_mutex_lock(fe);
2047         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2048
2049         dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2050
2051         /* call hw_free on the frontend */
2052         soc_pcm_hw_clean(fe, substream, 0);
2053
2054         /* only hw_params backends that are either sinks or sources
2055          * to this frontend DAI */
2056         dpcm_be_dai_hw_free(fe, stream);
2057
2058         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2059         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2060
2061         snd_soc_dpcm_mutex_unlock(fe);
2062         return 0;
2063 }
2064
2065 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2066 {
2067         struct snd_soc_pcm_runtime *be;
2068         struct snd_pcm_substream *be_substream;
2069         struct snd_soc_dpcm *dpcm;
2070         int ret;
2071
2072         for_each_dpcm_be(fe, stream, dpcm) {
2073                 struct snd_pcm_hw_params hw_params;
2074
2075                 be = dpcm->be;
2076                 be_substream = snd_soc_dpcm_get_substream(be, stream);
2077
2078                 /* is this op for this BE ? */
2079                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2080                         continue;
2081
2082                 /* copy params for each dpcm */
2083                 memcpy(&hw_params, &fe->dpcm[stream].hw_params,
2084                                 sizeof(struct snd_pcm_hw_params));
2085
2086                 /* perform any hw_params fixups */
2087                 ret = snd_soc_link_be_hw_params_fixup(be, &hw_params);
2088                 if (ret < 0)
2089                         goto unwind;
2090
2091                 /* copy the fixed-up hw params for BE dai */
2092                 memcpy(&be->dpcm[stream].hw_params, &hw_params,
2093                        sizeof(struct snd_pcm_hw_params));
2094
2095                 /* only allow hw_params() if no connected FEs are running */
2096                 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2097                         continue;
2098
2099                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2100                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2101                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2102                         continue;
2103
2104                 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2105                         be->dai_link->name);
2106
2107                 ret = __soc_pcm_hw_params(be_substream, &hw_params);
2108                 if (ret < 0)
2109                         goto unwind;
2110
2111                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2112         }
2113         return 0;
2114
2115 unwind:
2116         dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2117                 __func__, be->dai_link->name, ret);
2118
2119         /* disable any enabled and non active backends */
2120         for_each_dpcm_be_rollback(fe, stream, dpcm) {
2121                 be = dpcm->be;
2122                 be_substream = snd_soc_dpcm_get_substream(be, stream);
2123
2124                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2125                         continue;
2126
2127                 /* only allow hw_free() if no connected FEs are running */
2128                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2129                         continue;
2130
2131                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2132                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2133                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2134                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2135                         continue;
2136
2137                 __soc_pcm_hw_free(be, be_substream);
2138         }
2139
2140         return ret;
2141 }
2142
2143 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2144                                  struct snd_pcm_hw_params *params)
2145 {
2146         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2147         int ret, stream = substream->stream;
2148
2149         snd_soc_dpcm_mutex_lock(fe);
2150         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2151
2152         memcpy(&fe->dpcm[stream].hw_params, params,
2153                         sizeof(struct snd_pcm_hw_params));
2154         ret = dpcm_be_dai_hw_params(fe, stream);
2155         if (ret < 0)
2156                 goto out;
2157
2158         dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2159                         fe->dai_link->name, params_rate(params),
2160                         params_channels(params), params_format(params));
2161
2162         /* call hw_params on the frontend */
2163         ret = __soc_pcm_hw_params(substream, params);
2164         if (ret < 0)
2165                 dpcm_be_dai_hw_free(fe, stream);
2166         else
2167                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2168
2169 out:
2170         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2171         snd_soc_dpcm_mutex_unlock(fe);
2172
2173         return soc_pcm_ret(fe, ret);
2174 }
2175
2176 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2177                                int cmd)
2178 {
2179         struct snd_soc_pcm_runtime *be;
2180         bool pause_stop_transition;
2181         struct snd_soc_dpcm *dpcm;
2182         unsigned long flags;
2183         int ret = 0;
2184
2185         for_each_dpcm_be(fe, stream, dpcm) {
2186                 struct snd_pcm_substream *be_substream;
2187
2188                 be = dpcm->be;
2189                 be_substream = snd_soc_dpcm_get_substream(be, stream);
2190
2191                 snd_pcm_stream_lock_irqsave_nested(be_substream, flags);
2192
2193                 /* is this op for this BE ? */
2194                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2195                         goto next;
2196
2197                 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2198                         be->dai_link->name, cmd);
2199
2200                 switch (cmd) {
2201                 case SNDRV_PCM_TRIGGER_START:
2202                         if (!be->dpcm[stream].be_start &&
2203                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2204                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2205                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2206                                 goto next;
2207
2208                         be->dpcm[stream].be_start++;
2209                         if (be->dpcm[stream].be_start != 1)
2210                                 goto next;
2211
2212                         if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED)
2213                                 ret = soc_pcm_trigger(be_substream,
2214                                                       SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
2215                         else
2216                                 ret = soc_pcm_trigger(be_substream,
2217                                                       SNDRV_PCM_TRIGGER_START);
2218                         if (ret) {
2219                                 be->dpcm[stream].be_start--;
2220                                 goto next;
2221                         }
2222
2223                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2224                         break;
2225                 case SNDRV_PCM_TRIGGER_RESUME:
2226                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2227                                 goto next;
2228
2229                         be->dpcm[stream].be_start++;
2230                         if (be->dpcm[stream].be_start != 1)
2231                                 goto next;
2232
2233                         ret = soc_pcm_trigger(be_substream, cmd);
2234                         if (ret) {
2235                                 be->dpcm[stream].be_start--;
2236                                 goto next;
2237                         }
2238
2239                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2240                         break;
2241                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2242                         if (!be->dpcm[stream].be_start &&
2243                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2244                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2245                                 goto next;
2246
2247                         fe->dpcm[stream].fe_pause = false;
2248                         be->dpcm[stream].be_pause--;
2249
2250                         be->dpcm[stream].be_start++;
2251                         if (be->dpcm[stream].be_start != 1)
2252                                 goto next;
2253
2254                         ret = soc_pcm_trigger(be_substream, cmd);
2255                         if (ret) {
2256                                 be->dpcm[stream].be_start--;
2257                                 goto next;
2258                         }
2259
2260                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2261                         break;
2262                 case SNDRV_PCM_TRIGGER_STOP:
2263                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2264                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2265                                 goto next;
2266
2267                         if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2268                                 be->dpcm[stream].be_start--;
2269
2270                         if (be->dpcm[stream].be_start != 0)
2271                                 goto next;
2272
2273                         pause_stop_transition = false;
2274                         if (fe->dpcm[stream].fe_pause) {
2275                                 pause_stop_transition = true;
2276                                 fe->dpcm[stream].fe_pause = false;
2277                                 be->dpcm[stream].be_pause--;
2278                         }
2279
2280                         if (be->dpcm[stream].be_pause != 0)
2281                                 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
2282                         else
2283                                 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP);
2284
2285                         if (ret) {
2286                                 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2287                                         be->dpcm[stream].be_start++;
2288                                 if (pause_stop_transition) {
2289                                         fe->dpcm[stream].fe_pause = true;
2290                                         be->dpcm[stream].be_pause++;
2291                                 }
2292                                 goto next;
2293                         }
2294
2295                         if (be->dpcm[stream].be_pause != 0)
2296                                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2297                         else
2298                                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2299
2300                         break;
2301                 case SNDRV_PCM_TRIGGER_SUSPEND:
2302                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2303                                 goto next;
2304
2305                         be->dpcm[stream].be_start--;
2306                         if (be->dpcm[stream].be_start != 0)
2307                                 goto next;
2308
2309                         ret = soc_pcm_trigger(be_substream, cmd);
2310                         if (ret) {
2311                                 be->dpcm[stream].be_start++;
2312                                 goto next;
2313                         }
2314
2315                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2316                         break;
2317                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2318                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2319                                 goto next;
2320
2321                         fe->dpcm[stream].fe_pause = true;
2322                         be->dpcm[stream].be_pause++;
2323
2324                         be->dpcm[stream].be_start--;
2325                         if (be->dpcm[stream].be_start != 0)
2326                                 goto next;
2327
2328                         ret = soc_pcm_trigger(be_substream, cmd);
2329                         if (ret) {
2330                                 be->dpcm[stream].be_start++;
2331                                 goto next;
2332                         }
2333
2334                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2335                         break;
2336                 }
2337 next:
2338                 snd_pcm_stream_unlock_irqrestore(be_substream, flags);
2339                 if (ret)
2340                         break;
2341         }
2342         return soc_pcm_ret(fe, ret);
2343 }
2344 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2345
2346 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2347                                   int cmd, bool fe_first)
2348 {
2349         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2350         int ret;
2351
2352         /* call trigger on the frontend before the backend. */
2353         if (fe_first) {
2354                 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2355                         fe->dai_link->name, cmd);
2356
2357                 ret = soc_pcm_trigger(substream, cmd);
2358                 if (ret < 0)
2359                         goto end;
2360
2361                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2362         }
2363         /* call trigger on the frontend after the backend. */
2364         else {
2365                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2366                 if (ret < 0)
2367                         goto end;
2368
2369                 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2370                         fe->dai_link->name, cmd);
2371
2372                 ret = soc_pcm_trigger(substream, cmd);
2373         }
2374 end:
2375         return snd_soc_ret(fe->dev, ret, "trigger FE cmd: %d failed\n", cmd);
2376 }
2377
2378 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2379 {
2380         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2381         int stream = substream->stream;
2382         int ret = 0;
2383         int fe_first;
2384         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2385
2386         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2387
2388         switch (trigger) {
2389         case SND_SOC_DPCM_TRIGGER_PRE:
2390                 fe_first = true;
2391                 break;
2392         case SND_SOC_DPCM_TRIGGER_POST:
2393                 fe_first = false;
2394                 break;
2395         default:
2396                 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2397                                 fe->dai_link->name);
2398                 ret = -EINVAL;
2399                 goto out;
2400         }
2401
2402         switch (cmd) {
2403         case SNDRV_PCM_TRIGGER_START:
2404         case SNDRV_PCM_TRIGGER_RESUME:
2405         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2406         case SNDRV_PCM_TRIGGER_DRAIN:
2407                 ret = dpcm_dai_trigger_fe_be(substream, cmd, fe_first);
2408                 break;
2409         case SNDRV_PCM_TRIGGER_STOP:
2410         case SNDRV_PCM_TRIGGER_SUSPEND:
2411         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2412                 ret = dpcm_dai_trigger_fe_be(substream, cmd, !fe_first);
2413                 break;
2414         default:
2415                 ret = -EINVAL;
2416                 break;
2417         }
2418
2419         if (ret < 0)
2420                 goto out;
2421
2422         switch (cmd) {
2423         case SNDRV_PCM_TRIGGER_START:
2424         case SNDRV_PCM_TRIGGER_RESUME:
2425         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2426                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2427                 break;
2428         case SNDRV_PCM_TRIGGER_STOP:
2429         case SNDRV_PCM_TRIGGER_SUSPEND:
2430                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2431                 break;
2432         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2433                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2434                 break;
2435         }
2436
2437 out:
2438         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2439         return ret;
2440 }
2441
2442 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2443 {
2444         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2445         int stream = substream->stream;
2446
2447         /* if FE's runtime_update is already set, we're in race;
2448          * process this trigger later at exit
2449          */
2450         if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2451                 fe->dpcm[stream].trigger_pending = cmd + 1;
2452                 return 0; /* delayed, assuming it's successful */
2453         }
2454
2455         /* we're alone, let's trigger */
2456         return dpcm_fe_dai_do_trigger(substream, cmd);
2457 }
2458
2459 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2460 {
2461         struct snd_soc_dpcm *dpcm;
2462         int ret = 0;
2463
2464         for_each_dpcm_be(fe, stream, dpcm) {
2465
2466                 struct snd_soc_pcm_runtime *be = dpcm->be;
2467                 struct snd_pcm_substream *be_substream =
2468                         snd_soc_dpcm_get_substream(be, stream);
2469
2470                 /* is this op for this BE ? */
2471                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2472                         continue;
2473
2474                 if (!snd_soc_dpcm_can_be_prepared(fe, be, stream))
2475                         continue;
2476
2477                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2478                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2479                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2480                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2481                         continue;
2482
2483                 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2484                         be->dai_link->name);
2485
2486                 ret = __soc_pcm_prepare(be, be_substream);
2487                 if (ret < 0)
2488                         break;
2489
2490                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2491         }
2492
2493         /*
2494          * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
2495          *
2496          * We don't want to log an error since we do not want to give userspace a way to do a
2497          * denial-of-service attack on the syslog / diskspace.
2498          */
2499         return ret;
2500 }
2501
2502 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2503 {
2504         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2505         int stream = substream->stream, ret = 0;
2506
2507         snd_soc_dpcm_mutex_lock(fe);
2508
2509         dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2510
2511         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2512
2513         ret = dpcm_be_dai_prepare(fe, stream);
2514         if (ret < 0)
2515                 goto out;
2516
2517         /* call prepare on the frontend */
2518         ret = __soc_pcm_prepare(fe, substream);
2519         if (ret < 0)
2520                 goto out;
2521
2522         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2523
2524 out:
2525         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2526         snd_soc_dpcm_mutex_unlock(fe);
2527
2528         /*
2529          * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
2530          *
2531          * We don't want to log an error since we do not want to give userspace a way to do a
2532          * denial-of-service attack on the syslog / diskspace.
2533          */
2534         return ret;
2535 }
2536
2537 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2538 {
2539         int err;
2540
2541         dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2542                 snd_pcm_direction_name(stream), fe->dai_link->name);
2543
2544         err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2545
2546         dpcm_be_dai_hw_free(fe, stream);
2547
2548         dpcm_be_dai_shutdown(fe, stream);
2549
2550         /* run the stream event for each BE */
2551         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2552
2553         return soc_pcm_ret(fe, err);
2554 }
2555
2556 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2557 {
2558         struct snd_soc_dpcm *dpcm;
2559         int ret = 0;
2560
2561         dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2562                 snd_pcm_direction_name(stream), fe->dai_link->name);
2563
2564         /* Only start the BE if the FE is ready */
2565         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2566                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2567                 dev_err(fe->dev, "ASoC: FE %s is not ready %s\n",
2568                         fe->dai_link->name, dpcm_state_string(fe->dpcm[stream].state));
2569                 ret = -EINVAL;
2570                 goto disconnect;
2571         }
2572
2573         /* startup must always be called for new BEs */
2574         ret = dpcm_be_dai_startup(fe, stream);
2575         if (ret < 0)
2576                 goto disconnect;
2577
2578         /* keep going if FE state is > open */
2579         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2580                 return 0;
2581
2582         ret = dpcm_be_dai_hw_params(fe, stream);
2583         if (ret < 0)
2584                 goto close;
2585
2586         /* keep going if FE state is > hw_params */
2587         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2588                 return 0;
2589
2590         ret = dpcm_be_dai_prepare(fe, stream);
2591         if (ret < 0)
2592                 goto hw_free;
2593
2594         /* run the stream event for each BE */
2595         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2596
2597         /* keep going if FE state is > prepare */
2598         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2599                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2600                 return 0;
2601
2602         ret = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_START);
2603         if (ret < 0)
2604                 goto hw_free;
2605
2606         return 0;
2607
2608 hw_free:
2609         dpcm_be_dai_hw_free(fe, stream);
2610 close:
2611         dpcm_be_dai_shutdown(fe, stream);
2612 disconnect:
2613         /* disconnect any pending BEs */
2614         for_each_dpcm_be(fe, stream, dpcm) {
2615                 struct snd_soc_pcm_runtime *be = dpcm->be;
2616
2617                 /* is this op for this BE ? */
2618                 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2619                         continue;
2620
2621                 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2622                         be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2623                                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2624         }
2625
2626         return soc_pcm_ret(fe, ret);
2627 }
2628
2629 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2630 {
2631         struct snd_soc_dapm_widget_list *list;
2632         int stream;
2633         int count, paths;
2634
2635         if (!fe->dai_link->dynamic)
2636                 return 0;
2637
2638         if (fe->dai_link->num_cpus > 1)
2639                 return snd_soc_ret(fe->dev, -EINVAL,
2640                         "%s doesn't support Multi CPU yet\n", __func__);
2641
2642         /* only check active links */
2643         if (!snd_soc_dai_active(snd_soc_rtd_to_cpu(fe, 0)))
2644                 return 0;
2645
2646         /* DAPM sync will call this to update DSP paths */
2647         dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2648                 new ? "new" : "old", fe->dai_link->name);
2649
2650         for_each_pcm_streams(stream) {
2651
2652                 /* skip if FE doesn't have playback/capture capability */
2653                 if (!snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0),   stream) ||
2654                     !snd_soc_dai_stream_valid(snd_soc_rtd_to_codec(fe, 0), stream))
2655                         continue;
2656
2657                 /* skip if FE isn't currently playing/capturing */
2658                 if (!snd_soc_dai_stream_active(snd_soc_rtd_to_cpu(fe, 0), stream) ||
2659                     !snd_soc_dai_stream_active(snd_soc_rtd_to_codec(fe, 0), stream))
2660                         continue;
2661
2662                 paths = dpcm_path_get(fe, stream, &list);
2663                 if (paths < 0)
2664                         return paths;
2665
2666                 /* update any playback/capture paths */
2667                 /*
2668                  * Find the corresponding BE DAIs that source or sink audio to this
2669                  * FE substream.
2670                  */
2671                 if (new)
2672                         count = dpcm_add_paths(fe, stream, &list);
2673                 else
2674                         count = dpcm_prune_paths(fe, stream, &list);
2675                 if (count) {
2676                         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2677                         if (new)
2678                                 dpcm_run_update_startup(fe, stream);
2679                         else
2680                                 dpcm_run_update_shutdown(fe, stream);
2681                         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2682
2683                         dpcm_clear_pending_state(fe, stream);
2684                         dpcm_be_disconnect(fe, stream);
2685                 }
2686
2687                 dpcm_path_put(&list);
2688         }
2689
2690         return 0;
2691 }
2692
2693 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2694  * any DAI links.
2695  */
2696 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2697 {
2698         struct snd_soc_pcm_runtime *fe;
2699         int ret = 0;
2700
2701         snd_soc_dpcm_mutex_lock(card);
2702         /* shutdown all old paths first */
2703         for_each_card_rtds(card, fe) {
2704                 ret = soc_dpcm_fe_runtime_update(fe, 0);
2705                 if (ret)
2706                         goto out;
2707         }
2708
2709         /* bring new paths up */
2710         for_each_card_rtds(card, fe) {
2711                 ret = soc_dpcm_fe_runtime_update(fe, 1);
2712                 if (ret)
2713                         goto out;
2714         }
2715
2716 out:
2717         snd_soc_dpcm_mutex_unlock(card);
2718
2719         return snd_soc_ret(card->dev, ret, "%s() failed\n", __func__);
2720 }
2721 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2722
2723 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2724 {
2725         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2726         struct snd_soc_dpcm *dpcm;
2727         int stream = fe_substream->stream;
2728
2729         snd_soc_dpcm_mutex_assert_held(fe);
2730
2731         /* mark FE's links ready to prune */
2732         for_each_dpcm_be(fe, stream, dpcm)
2733                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2734
2735         dpcm_be_disconnect(fe, stream);
2736 }
2737
2738 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2739 {
2740         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2741         int ret;
2742
2743         snd_soc_dpcm_mutex_lock(fe);
2744         ret = dpcm_fe_dai_shutdown(fe_substream);
2745
2746         dpcm_fe_dai_cleanup(fe_substream);
2747
2748         snd_soc_dpcm_mutex_unlock(fe);
2749         return ret;
2750 }
2751
2752 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2753 {
2754         struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2755         struct snd_soc_dapm_widget_list *list;
2756         int ret;
2757         int stream = fe_substream->stream;
2758
2759         snd_soc_dpcm_mutex_lock(fe);
2760
2761         ret = dpcm_path_get(fe, stream, &list);
2762         if (ret < 0)
2763                 goto open_end;
2764
2765         /* calculate valid and active FE <-> BE dpcms */
2766         dpcm_add_paths(fe, stream, &list);
2767
2768         /* There is no point starting up this FE if there are no BEs. */
2769         if (list_empty(&fe->dpcm[stream].be_clients)) {
2770                 /* dev_err_once() for visibility, dev_dbg() for debugging UCM profiles. */
2771                 dev_err_once(fe->dev, "ASoC: no backend DAIs enabled for %s, possibly missing ALSA mixer-based routing or UCM profile\n",
2772                              fe->dai_link->name);
2773                 dev_dbg(fe->dev, "ASoC: no backend DAIs enabled for %s\n", fe->dai_link->name);
2774
2775                 ret = -EINVAL;
2776                 goto put_path;
2777         }
2778
2779         ret = dpcm_fe_dai_startup(fe_substream);
2780         if (ret < 0)
2781                 dpcm_fe_dai_cleanup(fe_substream);
2782
2783         dpcm_clear_pending_state(fe, stream);
2784 put_path:
2785         dpcm_path_put(&list);
2786 open_end:
2787         snd_soc_dpcm_mutex_unlock(fe);
2788         return ret;
2789 }
2790
2791 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2792                                     int *playback, int *capture)
2793 {
2794         struct snd_soc_dai_link *dai_link = rtd->dai_link;
2795         struct snd_soc_dai *cpu_dai;
2796         struct snd_soc_dai *codec_dai;
2797         struct snd_soc_dai_link_ch_map *ch_maps;
2798         struct snd_soc_dai *dummy_dai = snd_soc_find_dai(&snd_soc_dummy_dlc);
2799         int cpu_capture;
2800         int cpu_playback;
2801         int has_playback = 0;
2802         int has_capture  = 0;
2803         int i;
2804
2805         if (dai_link->dynamic && dai_link->num_cpus > 1)
2806                 return snd_soc_ret(rtd->dev, -EINVAL,
2807                                 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2808
2809         /* Adapt stream for codec2codec links */
2810         cpu_capture  = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_CAPTURE);
2811         cpu_playback = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_PLAYBACK);
2812
2813         /*
2814          * see
2815          *      soc.h :: [dai_link->ch_maps Image sample]
2816          */
2817         for_each_rtd_ch_maps(rtd, i, ch_maps) {
2818                 cpu_dai   = snd_soc_rtd_to_cpu(rtd,   ch_maps->cpu);
2819                 codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
2820
2821                 /*
2822                  * FIXME
2823                  *
2824                  * DPCM Codec has been no checked before.
2825                  * It should be checked, but it breaks compatibility.
2826                  *
2827                  * For example there is a case that CPU have loopback capabilities which is used
2828                  * for tests on boards where the Codec has no capture capabilities. In this case,
2829                  * Codec capture validation check will be fail, but system should allow capture
2830                  * capabilities. We have no solution for it today.
2831                  */
2832                 if (dai_link->dynamic || dai_link->no_pcm)
2833                         codec_dai = dummy_dai;
2834
2835                 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2836                     snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
2837                         has_playback = 1;
2838                 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2839                     snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
2840                         has_capture = 1;
2841         }
2842
2843         if (dai_link->playback_only)
2844                 has_capture = 0;
2845
2846         if (dai_link->capture_only)
2847                 has_playback = 0;
2848
2849         if (!has_playback && !has_capture)
2850                 return snd_soc_ret(rtd->dev, -EINVAL,
2851                         "substream %s has no playback, no capture\n", dai_link->stream_name);
2852
2853         *playback = has_playback;
2854         *capture  = has_capture;
2855
2856         return 0;
2857 }
2858
2859 static int soc_create_pcm(struct snd_pcm **pcm,
2860                           struct snd_soc_pcm_runtime *rtd,
2861                           int playback, int capture)
2862 {
2863         char new_name[64];
2864         int ret;
2865
2866         /* create the PCM */
2867         if (rtd->dai_link->c2c_params) {
2868                 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2869                          rtd->dai_link->stream_name);
2870
2871                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id,
2872                                            playback, capture, pcm);
2873         } else if (rtd->dai_link->no_pcm) {
2874                 snprintf(new_name, sizeof(new_name), "(%s)",
2875                         rtd->dai_link->stream_name);
2876
2877                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id,
2878                                 playback, capture, pcm);
2879         } else {
2880                 if (rtd->dai_link->dynamic)
2881                         snprintf(new_name, sizeof(new_name), "%s (*)",
2882                                 rtd->dai_link->stream_name);
2883                 else
2884                         snprintf(new_name, sizeof(new_name), "%s %s-%d",
2885                                 rtd->dai_link->stream_name,
2886                                 soc_codec_dai_name(rtd), rtd->id);
2887
2888                 ret = snd_pcm_new(rtd->card->snd_card, new_name, rtd->id, playback,
2889                         capture, pcm);
2890         }
2891         if (ret < 0)
2892                 return snd_soc_ret(rtd->dev, ret,
2893                         "can't create pcm %s for dailink %s\n", new_name, rtd->dai_link->name);
2894
2895         dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n", rtd->id, new_name);
2896
2897         return 0;
2898 }
2899
2900 /* create a new pcm */
2901 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd)
2902 {
2903         struct snd_soc_component *component;
2904         struct snd_pcm *pcm;
2905         int ret = 0, playback = 0, capture = 0;
2906         int i;
2907
2908         ret = soc_get_playback_capture(rtd, &playback, &capture);
2909         if (ret < 0)
2910                 return ret;
2911
2912         ret = soc_create_pcm(&pcm, rtd, playback, capture);
2913         if (ret < 0)
2914                 return ret;
2915
2916         /* DAPM dai link stream work */
2917         /*
2918          * Currently nothing to do for c2c links
2919          * Since c2c links are internal nodes in the DAPM graph and
2920          * don't interface with the outside world or application layer
2921          * we don't have to do any special handling on close.
2922          */
2923         if (!rtd->dai_link->c2c_params)
2924                 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2925
2926         rtd->pcm = pcm;
2927         pcm->nonatomic = rtd->dai_link->nonatomic;
2928         pcm->private_data = rtd;
2929         pcm->no_device_suspend = true;
2930
2931         if (rtd->dai_link->no_pcm || rtd->dai_link->c2c_params) {
2932                 if (playback)
2933                         pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2934                 if (capture)
2935                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2936                 goto out;
2937         }
2938
2939         /* ASoC PCM operations */
2940         if (rtd->dai_link->dynamic) {
2941                 rtd->ops.open           = dpcm_fe_dai_open;
2942                 rtd->ops.hw_params      = dpcm_fe_dai_hw_params;
2943                 rtd->ops.prepare        = dpcm_fe_dai_prepare;
2944                 rtd->ops.trigger        = dpcm_fe_dai_trigger;
2945                 rtd->ops.hw_free        = dpcm_fe_dai_hw_free;
2946                 rtd->ops.close          = dpcm_fe_dai_close;
2947                 rtd->ops.pointer        = soc_pcm_pointer;
2948         } else {
2949                 rtd->ops.open           = soc_pcm_open;
2950                 rtd->ops.hw_params      = soc_pcm_hw_params;
2951                 rtd->ops.prepare        = soc_pcm_prepare;
2952                 rtd->ops.trigger        = soc_pcm_trigger;
2953                 rtd->ops.hw_free        = soc_pcm_hw_free;
2954                 rtd->ops.close          = soc_pcm_close;
2955                 rtd->ops.pointer        = soc_pcm_pointer;
2956         }
2957
2958         for_each_rtd_components(rtd, i, component) {
2959                 const struct snd_soc_component_driver *drv = component->driver;
2960
2961                 if (drv->ioctl)
2962                         rtd->ops.ioctl          = snd_soc_pcm_component_ioctl;
2963                 if (drv->sync_stop)
2964                         rtd->ops.sync_stop      = snd_soc_pcm_component_sync_stop;
2965                 if (drv->copy)
2966                         rtd->ops.copy           = snd_soc_pcm_component_copy;
2967                 if (drv->page)
2968                         rtd->ops.page           = snd_soc_pcm_component_page;
2969                 if (drv->mmap)
2970                         rtd->ops.mmap           = snd_soc_pcm_component_mmap;
2971                 if (drv->ack)
2972                         rtd->ops.ack            = snd_soc_pcm_component_ack;
2973         }
2974
2975         if (playback)
2976                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2977
2978         if (capture)
2979                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2980
2981         ret = snd_soc_pcm_component_new(rtd);
2982         if (ret < 0)
2983                 return ret;
2984 out:
2985         dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2986                 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2987         return ret;
2988 }
2989
2990 /* get the substream for this BE */
2991 struct snd_pcm_substream *
2992         snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2993 {
2994         return be->pcm->streams[stream].substream;
2995 }
2996 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);