Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / sound / soc / soc-pcm.c
CommitLineData
ed517582
KM
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>
ddee627c
LG
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/delay.h>
988e8cc4 16#include <linux/pinctrl/consumer.h>
ddee627c
LG
17#include <linux/slab.h>
18#include <linux/workqueue.h>
01d7584c 19#include <linux/export.h>
f86dcef8 20#include <linux/debugfs.h>
ddee627c
LG
21#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
01d7584c 25#include <sound/soc-dpcm.h>
a5e6c109 26#include <sound/soc-link.h>
ddee627c
LG
27#include <sound/initval.h>
28
04110728
KM
29#define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret)
30static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime *rtd,
31 const char *func, int ret)
32{
943116ba
KM
33 return snd_soc_ret(rtd->dev, ret,
34 "at %s() on %s\n", func, rtd->dai_link->name);
04110728
KM
35}
36
4dd4baa4 37/* is the current PCM operation for this FE ? */
290f31e9 38#if 0
ede6445d 39static int snd_soc_dpcm_can_fe_update(struct snd_soc_pcm_runtime *fe, int stream)
4dd4baa4
KM
40{
41 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
42 return 1;
43 return 0;
44}
290f31e9 45#endif
4dd4baa4
KM
46
47/* is the current PCM operation for this BE ? */
ede6445d 48static int snd_soc_dpcm_can_be_update(struct snd_soc_pcm_runtime *fe,
4dd4baa4
KM
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}
4dd4baa4
KM
57
58static 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 */
290f31e9
KM
91static int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
92 struct snd_soc_pcm_runtime *be, int stream)
4dd4baa4
KM
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}
4dd4baa4
KM
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 */
290f31e9
KM
107static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
108 struct snd_soc_pcm_runtime *be, int stream)
4dd4baa4
KM
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}
4dd4baa4
KM
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 */
290f31e9
KM
124static int snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime *fe,
125 struct snd_soc_pcm_runtime *be, int stream)
4dd4baa4
KM
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}
4dd4baa4 135
01d7584c
LG
136#define DPCM_MAX_BE_USERS 8
137
6fb8944c
KM
138static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
139{
2679a5b2 140 return (rtd)->dai_link->num_cpus == 1 ? snd_soc_rtd_to_cpu(rtd, 0)->name : "multicpu";
6fb8944c
KM
141}
142static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
143{
2679a5b2 144 return (rtd)->dai_link->num_codecs == 1 ? snd_soc_rtd_to_codec(rtd, 0)->name : "multicodec";
6fb8944c
KM
145}
146
c3212829
KM
147static 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
7a2ff051 175#ifdef CONFIG_DEBUG_FS
c3212829
KM
176static 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;
c3212829
KM
182
183 /* FE state */
d0c9abb8 184 offset += scnprintf(buf + offset, size - offset,
c3212829
KM
185 "[%s - %s]\n", fe->dai_link->name,
186 stream ? "Capture" : "Playback");
187
d0c9abb8 188 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
c3212829
KM
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))
d0c9abb8 193 offset += scnprintf(buf + offset, size - offset,
c3212829
KM
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 */
d0c9abb8 201 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
c3212829
KM
202
203 if (list_empty(&fe->dpcm[stream].be_clients)) {
d0c9abb8 204 offset += scnprintf(buf + offset, size - offset,
c3212829
KM
205 " No active DSP links\n");
206 goto out;
207 }
208
c3212829
KM
209 for_each_dpcm_be(fe, stream, dpcm) {
210 struct snd_soc_pcm_runtime *be = dpcm->be;
25106550 211 params = &be->dpcm[stream].hw_params;
c3212829 212
d0c9abb8 213 offset += scnprintf(buf + offset, size - offset,
c3212829
KM
214 "- %s\n", be->dai_link->name);
215
d0c9abb8 216 offset += scnprintf(buf + offset, size - offset,
c3212829
KM
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))
d0c9abb8 222 offset += scnprintf(buf + offset, size - offset,
c3212829
KM
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 }
c3212829
KM
229out:
230 return offset;
231}
232
233static 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
be61cd42
KM
241 if (fe->dai_link->num_cpus > 1)
242 return snd_soc_ret(fe->dev, -EINVAL,
6e1276a5 243 "%s doesn't support Multi CPU yet\n", __func__);
6e1276a5 244
c3212829
KM
245 buf = kmalloc(out_count, GFP_KERNEL);
246 if (!buf)
247 return -ENOMEM;
248
b7898396 249 snd_soc_dpcm_mutex_lock(fe);
c3212829 250 for_each_pcm_streams(stream)
2679a5b2 251 if (snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0), stream))
c3212829
KM
252 offset += dpcm_show_state(fe, stream,
253 buf + offset,
254 out_count - offset);
b7898396 255 snd_soc_dpcm_mutex_unlock(fe);
c3212829
KM
256
257 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
258
259 kfree(buf);
260 return ret;
261}
262
263static const struct file_operations dpcm_state_fops = {
264 .open = simple_open,
265 .read = dpcm_state_read_file,
266 .llseek = default_llseek,
267};
268
269void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
270{
c3212829
KM
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}
154dae87
KM
283
284static 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,
ebbd6703 289 snd_pcm_direction_name(stream));
154dae87
KM
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
299static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
300{
301 debugfs_remove_recursive(dpcm->debugfs_state);
302}
303
304#else
305static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
306 int stream)
307{
308}
309
310static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
311{
312}
c3212829
KM
313#endif
314
9c6d7f93
KM
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 */
320static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
321static 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
c8c3d9f8 327 snd_pcm_stream_lock_irq(substream);
9c6d7f93
KM
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;
c8c3d9f8 334 snd_pcm_stream_unlock_irq(substream);
9c6d7f93
KM
335}
336
a7e20444
KM
337static 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
d9051d86
KM
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
b6d6e9ea 348 * @action: Activate stream if 1. Deactivate if -1.
d9051d86
KM
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 */
356void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
357 int stream, int action)
24894b76 358{
8f039360 359 struct snd_soc_component *component;
c840f769 360 struct snd_soc_dai *dai;
2e5894d7 361 int i;
24894b76 362
b7898396 363 snd_soc_dpcm_mutex_assert_held(rtd);
24894b76 364
dc829106
KM
365 for_each_rtd_dais(rtd, i, dai)
366 snd_soc_dai_action(dai, stream, action);
8f039360
CL
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 }
24894b76 374}
d9051d86 375EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
24894b76 376
208a1589
LPC
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
f78bf2c9 382 * specific PCM runtime. Returns true if the delay is 0, if the DAI link has
208a1589
LPC
383 * been configured to ignore the delay, or if none of the components benefits
384 * from having the delay.
385 */
386bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
387{
fbb16563 388 struct snd_soc_component *component;
613fb500 389 int i;
2e5894d7 390
208a1589
LPC
391 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
392 return true;
393
613fb500 394 for_each_rtd_components(rtd, i, component)
f78bf2c9
DS
395 if (component->driver->use_pmdown_time)
396 /* No need to go through all components */
397 return false;
fbb16563 398
f78bf2c9 399 return true;
208a1589
LPC
400}
401
01d7584c 402/* DPCM stream event, send event to FE and all active BEs. */
7e1caa67 403void dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, int event)
01d7584c
LG
404{
405 struct snd_soc_dpcm *dpcm;
406
b7898396
TI
407 snd_soc_dpcm_mutex_assert_held(fe);
408
8d6258a4 409 for_each_dpcm_be(fe, dir, dpcm) {
01d7584c
LG
410
411 struct snd_soc_pcm_runtime *be = dpcm->be;
412
103d84a3 413 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
01d7584c
LG
414 be->dai_link->name, event, dir);
415
b1cd2e34
BG
416 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
417 (be->dpcm[dir].users >= 1))
418 continue;
419
01d7584c
LG
420 snd_soc_dapm_stream_event(be, dir, event);
421 }
422
423 snd_soc_dapm_stream_event(fe, dir, event);
01d7584c
LG
424}
425
2805b8bd
KM
426static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
427 struct snd_pcm_hw_params *params)
428{
429 if (params) {
1bd775da
KM
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));
2805b8bd 433 } else {
1bd775da
KM
434 dai->symmetric_rate = 0;
435 dai->symmetric_channels = 0;
436 dai->symmetric_sample_bits = 0;
2805b8bd
KM
437 }
438}
439
17841020
DA
440static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
441 struct snd_soc_dai *soc_dai)
ddee627c 442{
2679a5b2 443 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
ddee627c
LG
444 int ret;
445
f8fc9ec5
KM
446 if (!snd_soc_dai_active(soc_dai))
447 return 0;
448
fac110cb 449#define __soc_pcm_apply_symmetry(name, NAME) \
1bd775da
KM
450 if (soc_dai->symmetric_##name && \
451 (soc_dai->driver->symmetric_##name || rtd->dai_link->symmetric_##name)) { \
fac110cb 452 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
1bd775da 453 #name, soc_dai->symmetric_##name); \
fac110cb
KM
454 \
455 ret = snd_pcm_hw_constraint_single(substream->runtime, \
456 SNDRV_PCM_HW_PARAM_##NAME,\
1bd775da 457 soc_dai->symmetric_##name); \
be61cd42
KM
458 if (ret < 0) \
459 return snd_soc_ret(soc_dai->dev, ret, \
460 "Unable to apply %s constraint\n", #name); \
3635bf09 461 }
ddee627c 462
fac110cb
KM
463 __soc_pcm_apply_symmetry(rate, RATE);
464 __soc_pcm_apply_symmetry(channels, CHANNELS);
465 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
ddee627c
LG
466
467 return 0;
468}
469
3635bf09
NC
470static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
471 struct snd_pcm_hw_params *params)
472{
2679a5b2 473 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
2805b8bd 474 struct snd_soc_dai d;
c840f769 475 struct snd_soc_dai *dai;
19bdcc7a 476 struct snd_soc_dai *cpu_dai;
2805b8bd 477 unsigned int symmetry, i;
3635bf09 478
ee39d77e 479 d.name = __func__;
2805b8bd 480 soc_pcm_set_dai_params(&d, params);
3635bf09 481
1cacbac4
KM
482#define __soc_pcm_params_symmetry(xxx) \
483 symmetry = rtd->dai_link->symmetric_##xxx; \
3a906721 484 for_each_rtd_dais(rtd, i, dai) \
1cacbac4 485 symmetry |= dai->driver->symmetric_##xxx; \
3a906721
KM
486 \
487 if (symmetry) \
488 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
9c2ae363 489 if (!snd_soc_dai_is_dummy(cpu_dai) && \
1bd775da 490 cpu_dai->symmetric_##xxx && \
be61cd42
KM
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);
ddee627c 496
3a906721
KM
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);
ddee627c
LG
501
502 return 0;
503}
504
68cbc557 505static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
62e5f676 506{
2679a5b2 507 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
62e5f676 508 struct snd_soc_dai_link *link = rtd->dai_link;
c840f769 509 struct snd_soc_dai *dai;
2e5894d7 510 unsigned int symmetry, i;
62e5f676 511
f14654dd 512 symmetry = link->symmetric_rate ||
19bdcc7a 513 link->symmetric_channels ||
f14654dd 514 link->symmetric_sample_bits;
19bdcc7a 515
c840f769 516 for_each_rtd_dais(rtd, i, dai)
2e5894d7 517 symmetry = symmetry ||
f14654dd 518 dai->driver->symmetric_rate ||
c840f769 519 dai->driver->symmetric_channels ||
f14654dd 520 dai->driver->symmetric_sample_bits;
2e5894d7 521
68cbc557
KM
522 if (symmetry)
523 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
62e5f676
LPC
524}
525
2e5894d7 526static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
58ba9b25 527{
2679a5b2 528 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
c6068d3a 529 int ret;
58ba9b25
MB
530
531 if (!bits)
532 return;
533
0e2a3751
LPC
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);
58ba9b25
MB
538}
539
c8dd1fec
BC
540static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
541{
2679a5b2 542 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
19bdcc7a 543 struct snd_soc_dai *cpu_dai;
2e5894d7 544 struct snd_soc_dai *codec_dai;
57be9206 545 int stream = substream->stream;
2e5894d7 546 int i;
19bdcc7a 547 unsigned int bits = 0, cpu_bits = 0;
c8dd1fec 548
a4be4187 549 for_each_rtd_codec_dais(rtd, i, codec_dai) {
de267e7a 550 const struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
57be9206
KM
551
552 if (pcm_codec->sig_bits == 0) {
553 bits = 0;
554 break;
2e5894d7 555 }
57be9206 556 bits = max(pcm_codec->sig_bits, bits);
c8dd1fec
BC
557 }
558
a4be4187 559 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
de267e7a 560 const struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
19bdcc7a
SN
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 }
57be9206 568
2e5894d7
BC
569 soc_pcm_set_msb(substream, bits);
570 soc_pcm_set_msb(substream, cpu_bits);
c8dd1fec
BC
571}
572
f6c04af5
KM
573static 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;
6cb56a45
KM
578 hw->channels_min = 0;
579 hw->channels_max = UINT_MAX;
debc71f2 580 hw->formats = ULLONG_MAX;
f6c04af5
KM
581}
582
583static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
de267e7a 584 const struct snd_soc_pcm_stream *p)
f6c04af5
KM
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
6cb56a45 596static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
de267e7a 597 const struct snd_soc_pcm_stream *p)
6cb56a45
KM
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
debc71f2 603static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
de267e7a 604 const struct snd_soc_pcm_stream *p)
debc71f2 605{
b92bc4d6
KM
606 hw->formats &= p->formats;
607 hw->subformats &= p->subformats;
4a6ba09e
CR
608}
609
5854a464
SH
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 */
619int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
620 struct snd_pcm_hardware *hw, int stream)
bd477c31 621{
0b7990e3 622 struct snd_soc_dai *codec_dai;
19bdcc7a 623 struct snd_soc_dai *cpu_dai;
de267e7a
KK
624 const struct snd_soc_pcm_stream *codec_stream;
625 const struct snd_soc_pcm_stream *cpu_stream;
19bdcc7a 626 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
2e5894d7 627 int i;
78e45c99 628
f6c04af5
KM
629 soc_pcm_hw_init(hw);
630
19bdcc7a 631 /* first calculate min/max only for CPUs in the DAI link */
a4be4187 632 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
0e9cf4c4
BL
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.
0e9cf4c4 639 */
5854a464 640 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
0e9cf4c4
BL
641 continue;
642
19bdcc7a
SN
643 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
644
6cb56a45 645 soc_pcm_hw_update_chan(hw, cpu_stream);
f6c04af5 646 soc_pcm_hw_update_rate(hw, cpu_stream);
debc71f2 647 soc_pcm_hw_update_format(hw, cpu_stream);
19bdcc7a 648 }
6cb56a45
KM
649 cpu_chan_min = hw->channels_min;
650 cpu_chan_max = hw->channels_max;
78e45c99 651
19bdcc7a 652 /* second calculate min/max only for CODECs in the DAI link */
a4be4187 653 for_each_rtd_codec_dais(rtd, i, codec_dai) {
cde79035
RW
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.
cde79035 660 */
25c2f515 661 if (!snd_soc_dai_stream_valid(codec_dai, stream))
cde79035
RW
662 continue;
663
acf253c1
KM
664 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
665
6cb56a45 666 soc_pcm_hw_update_chan(hw, codec_stream);
f6c04af5 667 soc_pcm_hw_update_rate(hw, codec_stream);
debc71f2 668 soc_pcm_hw_update_format(hw, codec_stream);
2e5894d7
BC
669 }
670
5854a464 671 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
6cb56a45 672 if (!hw->channels_min)
5854a464
SH
673 return -EINVAL;
674
2e5894d7
BC
675 /*
676 * chan min/max cannot be enforced if there are multiple CODEC DAIs
19bdcc7a 677 * connected to CPU DAI(s), use CPU DAI's directly and let
2e5894d7
BC
678 * channel allocation be fixed up later
679 */
3989ade2 680 if (rtd->dai_link->num_codecs > 1) {
6cb56a45
KM
681 hw->channels_min = cpu_chan_min;
682 hw->channels_max = cpu_chan_max;
2e5894d7
BC
683 }
684
5854a464
SH
685 return 0;
686}
687EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
688
689static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
690{
691 struct snd_pcm_hardware *hw = &substream->runtime->hw;
2679a5b2 692 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
5854a464
SH
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;
bd477c31
LPC
704}
705
dd03907b 706static int soc_pcm_components_open(struct snd_pcm_substream *substream)
e7ecfdb7 707{
2679a5b2 708 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
e7ecfdb7 709 struct snd_soc_component *component;
613fb500 710 int i, ret = 0;
e7ecfdb7 711
613fb500 712 for_each_rtd_components(rtd, i, component) {
51aff91a 713 ret = snd_soc_component_module_get_when_open(component, substream);
bcae1631 714 if (ret < 0)
d2aaa8d8 715 break;
e7ecfdb7 716
ae2f4849 717 ret = snd_soc_component_open(component, substream);
bcae1631 718 if (ret < 0)
d2aaa8d8 719 break;
e7ecfdb7 720 }
dd03907b 721
d2aaa8d8 722 return ret;
e7ecfdb7
KM
723}
724
51aff91a
KM
725static int soc_pcm_components_close(struct snd_pcm_substream *substream,
726 int rollback)
244e2936 727{
2679a5b2 728 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
244e2936 729 struct snd_soc_component *component;
33be10b5 730 int i, ret = 0;
244e2936 731
613fb500 732 for_each_rtd_components(rtd, i, component) {
33be10b5 733 int r = snd_soc_component_close(component, substream, rollback);
e82ebffc
KM
734 if (r < 0)
735 ret = r; /* use last ret */
736
51aff91a 737 snd_soc_component_module_put_when_close(component, substream, rollback);
244e2936
CK
738 }
739
3672beb8 740 return ret;
244e2936
CK
741}
742
b7898396
TI
743static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
744 struct snd_pcm_substream *substream, int rollback)
62c86d1d 745{
62c86d1d 746 struct snd_soc_component *component;
c840f769 747 struct snd_soc_dai *dai;
62c86d1d
KM
748 int i;
749
b7898396 750 snd_soc_dpcm_mutex_assert_held(rtd);
62c86d1d 751
1da681e5 752 if (!rollback) {
140a4532 753 snd_soc_runtime_deactivate(rtd, substream->stream);
1da681e5 754
3efcb471 755 /* Make sure DAI parameters cleared if the DAI becomes inactive */
1da681e5 756 for_each_rtd_dais(rtd, i, dai) {
1bd775da 757 if (snd_soc_dai_active(dai) == 0)
3efcb471 758 soc_pcm_set_dai_params(dai, NULL);
1da681e5
CL
759 }
760 }
62c86d1d 761
31a70a71 762 for_each_rtd_dais_reverse(rtd, i, dai)
140a4532 763 snd_soc_dai_shutdown(dai, substream, rollback);
62c86d1d 764
140a4532 765 snd_soc_link_shutdown(substream, rollback);
62c86d1d 766
140a4532 767 soc_pcm_components_close(substream, rollback);
62c86d1d 768
140a4532 769 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
62c86d1d
KM
770
771 for_each_rtd_components(rtd, i, component)
b3dea624 772 if (!snd_soc_component_active(component))
62c86d1d
KM
773 pinctrl_pm_select_sleep_state(component->dev);
774
775 return 0;
776}
777
140a4532
KM
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 */
b7898396
TI
783static 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 */
140a4532
KM
790static int soc_pcm_close(struct snd_pcm_substream *substream)
791{
2679a5b2 792 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
b7898396
TI
793
794 snd_soc_dpcm_mutex_lock(rtd);
6bbabd28 795 __soc_pcm_close(rtd, substream);
b7898396
TI
796 snd_soc_dpcm_mutex_unlock(rtd);
797 return 0;
140a4532
KM
798}
799
c393281a
KM
800static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
801{
2679a5b2 802 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
c393281a
KM
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
832config_err:
be61cd42
KM
833 return snd_soc_ret(dev, -EINVAL,
834 "%s <-> %s No matching %s\n", name_codec, name_cpu, err_msg);
c393281a
KM
835}
836
ddee627c
LG
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
ef050bec 840 * startup for the cpu DAI, component, machine and codec DAI.
ddee627c 841 */
b7898396
TI
842static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
843 struct snd_pcm_substream *substream)
ddee627c 844{
90be711e 845 struct snd_soc_component *component;
c840f769 846 struct snd_soc_dai *dai;
244e2936 847 int i, ret = 0;
ddee627c 848
b7898396
TI
849 snd_soc_dpcm_mutex_assert_held(rtd);
850
76c39e86
KM
851 for_each_rtd_components(rtd, i, component)
852 pinctrl_pm_select_default_state(component->dev);
90be711e 853
939a5cfb
KM
854 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
855 if (ret < 0)
b7898396 856 goto err;
ddee627c 857
5d9fa03e
KM
858 ret = soc_pcm_components_open(substream);
859 if (ret < 0)
140a4532 860 goto err;
5d9fa03e 861
7cf3c5b4 862 ret = snd_soc_link_startup(substream);
a5e6c109 863 if (ret < 0)
140a4532 864 goto err;
5d9fa03e 865
ddee627c 866 /* startup the audio subsystem */
c840f769
KM
867 for_each_rtd_dais(rtd, i, dai) {
868 ret = snd_soc_dai_startup(dai, substream);
ce820145 869 if (ret < 0)
140a4532 870 goto err;
ddee627c
LG
871 }
872
01d7584c
LG
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
ddee627c 877 /* Check that the codec and cpu DAIs are compatible */
2e5894d7
BC
878 soc_pcm_init_runtime_hw(substream);
879
68cbc557 880 soc_pcm_update_symmetry(substream);
62e5f676 881
c393281a
KM
882 ret = soc_hw_sanity_check(substream);
883 if (ret < 0)
140a4532 884 goto err;
ddee627c 885
c8dd1fec 886 soc_pcm_apply_msb(substream);
58ba9b25 887
ddee627c 888 /* Symmetry only applies if we've already got an active stream. */
c840f769 889 for_each_rtd_dais(rtd, i, dai) {
f8fc9ec5
KM
890 ret = soc_pcm_apply_symmetry(substream, dai);
891 if (ret != 0)
892 goto err;
ddee627c 893 }
01d7584c 894dynamic:
24894b76 895 snd_soc_runtime_activate(rtd, substream->stream);
8e7875ae 896 ret = 0;
140a4532 897err:
04110728 898 if (ret < 0)
b7898396 899 soc_pcm_clean(rtd, substream, 1);
d6652ef8 900
04110728 901 return soc_pcm_ret(rtd, ret);
ddee627c
LG
902}
903
b7898396
TI
904/* PCM open ops for non-DPCM streams */
905static int soc_pcm_open(struct snd_pcm_substream *substream)
906{
2679a5b2 907 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
b7898396
TI
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
ddee627c
LG
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 */
b7898396
TI
921static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
922 struct snd_pcm_substream *substream)
ddee627c 923{
c840f769 924 struct snd_soc_dai *dai;
2e5894d7 925 int i, ret = 0;
ddee627c 926
b7898396 927 snd_soc_dpcm_mutex_assert_held(rtd);
ddee627c 928
7cf3c5b4 929 ret = snd_soc_link_prepare(substream);
a5e6c109 930 if (ret < 0)
44c1a75b 931 goto out;
ddee627c 932
4f39514f
KM
933 ret = snd_soc_pcm_component_prepare(substream);
934 if (ret < 0)
935 goto out;
b8135864 936
d108c7fd 937 ret = snd_soc_pcm_dai_prepare(substream);
62462e01 938 if (ret < 0)
d108c7fd 939 goto out;
4beb8e10 940
ddee627c
LG
941 /* cancel any delayed stream shutdown that is pending */
942 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
9bffb1fb
MLC
943 rtd->pop_wait) {
944 rtd->pop_wait = 0;
ddee627c
LG
945 cancel_delayed_work(&rtd->delayed_work);
946 }
947
d9b0951b
LG
948 snd_soc_dapm_stream_event(rtd, substream->stream,
949 SND_SOC_DAPM_STREAM_START);
ddee627c 950
f0220575 951 for_each_rtd_dais(rtd, i, dai) {
5d5eceb9 952 if (!snd_soc_dai_mute_is_ctrled_at_trigger(dai))
f0220575
SK
953 snd_soc_dai_digital_mute(dai, 0, substream->stream);
954 }
ddee627c
LG
955
956out:
301c26a0
KM
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;
ddee627c
LG
964}
965
b7898396
TI
966/* PCM prepare ops for non-DPCM streams */
967static int soc_pcm_prepare(struct snd_pcm_substream *substream)
968{
2679a5b2 969 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
b7898396
TI
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);
301c26a0
KM
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 */
b7898396
TI
982 return ret;
983}
984
2e5894d7
BC
985static 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
b7898396
TI
996static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
997 struct snd_pcm_substream *substream, int rollback)
ab49436e 998{
ab49436e
KM
999 struct snd_soc_dai *dai;
1000 int i;
1001
b7898396 1002 snd_soc_dpcm_mutex_assert_held(rtd);
ab49436e 1003
3efcb471
CL
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
3841d8a5 1009 if (snd_soc_dai_stream_active(dai, substream->stream) == 1) {
5d5eceb9 1010 if (!snd_soc_dai_mute_is_ctrled_at_trigger(dai))
3841d8a5
JH
1011 snd_soc_dai_digital_mute(dai, 1, substream->stream);
1012 }
3efcb471
CL
1013 }
1014
a27b421f
RS
1015 /* run the stream event */
1016 snd_soc_dapm_stream_stop(rtd, substream->stream);
1017
ab49436e 1018 /* free any machine hw params */
4662c596 1019 snd_soc_link_hw_free(substream, rollback);
ab49436e
KM
1020
1021 /* free any component resources */
4662c596 1022 snd_soc_pcm_component_hw_free(substream, rollback);
ab49436e
KM
1023
1024 /* now free hw params for the DAIs */
121966d0
KM
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);
ab49436e 1028
ab49436e
KM
1029 return 0;
1030}
1031
4662c596
KM
1032/*
1033 * Frees resources allocated by hw_params, can be called multiple times
1034 */
b7898396
TI
1035static 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 */
4662c596
KM
1042static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1043{
2679a5b2 1044 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
b7898396
TI
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;
4662c596
KM
1051}
1052
ddee627c
LG
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 */
0c4a0639 1058static int __soc_pcm_hw_params(struct snd_pcm_substream *substream,
b7898396 1059 struct snd_pcm_hw_params *params)
ddee627c 1060{
0c4a0639 1061 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
19bdcc7a 1062 struct snd_soc_dai *cpu_dai;
0b7990e3 1063 struct snd_soc_dai *codec_dai;
396b9079 1064 struct snd_pcm_hw_params tmp_params;
244e2936 1065 int i, ret = 0;
ddee627c 1066
b7898396 1067 snd_soc_dpcm_mutex_assert_held(rtd);
5cca5951
SW
1068
1069 ret = soc_pcm_params_symmetry(substream, params);
1070 if (ret)
1071 goto out;
1072
7cf3c5b4 1073 ret = snd_soc_link_hw_params(substream, params);
a5e6c109 1074 if (ret < 0)
de9ad990 1075 goto out;
ddee627c 1076
a4be4187 1077 for_each_rtd_codec_dais(rtd, i, codec_dai) {
e15ff262 1078 unsigned int tdm_mask = snd_soc_dai_tdm_mask_get(codec_dai, substream->stream);
2e5894d7 1079
cde79035
RW
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
2e5894d7 1097 /* copy params for each codec */
396b9079 1098 tmp_params = *params;
2e5894d7
BC
1099
1100 /* fixup params based on TDM slot masks */
e15ff262 1101 if (tdm_mask)
396b9079 1102 soc_pcm_codec_params_fixup(&tmp_params, tdm_mask);
2e5894d7 1103
aa6166c2 1104 ret = snd_soc_dai_hw_params(codec_dai, substream,
396b9079 1105 &tmp_params);
93e6958a 1106 if(ret < 0)
4662c596 1107 goto out;
ddee627c 1108
396b9079
CK
1109 soc_pcm_set_dai_params(codec_dai, &tmp_params);
1110 snd_soc_dapm_update_dai(substream, &tmp_params, codec_dai);
ddee627c
LG
1111 }
1112
a4be4187 1113 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
45cc50d1 1114 struct snd_soc_dai_link_ch_map *ch_maps;
ac950278
BL
1115 unsigned int ch_mask = 0;
1116 int j;
1117
0e9cf4c4
BL
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
ac950278 1125 /* copy params for each cpu */
396b9079 1126 tmp_params = *params;
ac950278 1127
ac950278
BL
1128 /*
1129 * construct cpu channel mask by combining ch_mask of each
1130 * codec which maps to the cpu.
45cc50d1
KM
1131 * see
1132 * soc.h :: [dai_link->ch_maps Image sample]
ac950278 1133 */
45cc50d1
KM
1134 for_each_rtd_ch_maps(rtd, j, ch_maps)
1135 if (ch_maps->cpu == i)
1136 ch_mask |= ch_maps->ch_mask;
ac950278
BL
1137
1138 /* fixup cpu channel number */
1139 if (ch_mask)
396b9079 1140 soc_pcm_codec_params_fixup(&tmp_params, ch_mask);
ac950278 1141
396b9079 1142 ret = snd_soc_dai_hw_params(cpu_dai, substream, &tmp_params);
19bdcc7a 1143 if (ret < 0)
4662c596 1144 goto out;
ddee627c 1145
19bdcc7a 1146 /* store the parameters for each DAI */
396b9079
CK
1147 soc_pcm_set_dai_params(cpu_dai, &tmp_params);
1148 snd_soc_dapm_update_dai(substream, &tmp_params, cpu_dai);
19bdcc7a 1149 }
ca58221d 1150
3a36a64a 1151 ret = snd_soc_pcm_component_hw_params(substream, params);
ddee627c 1152out:
04110728 1153 if (ret < 0)
b7898396 1154 soc_pcm_hw_clean(rtd, substream, 1);
ddee627c 1155
04110728 1156 return soc_pcm_ret(rtd, ret);
ddee627c
LG
1157}
1158
b7898396
TI
1159/* hw_params PCM ops for non-DPCM streams */
1160static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1161 struct snd_pcm_hw_params *params)
1162{
2679a5b2 1163 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
b7898396
TI
1164 int ret;
1165
1166 snd_soc_dpcm_mutex_lock(rtd);
0c4a0639 1167 ret = __soc_pcm_hw_params(substream, params);
b7898396
TI
1168 snd_soc_dpcm_mutex_unlock(rtd);
1169 return ret;
1170}
1171
356caf66
KM
1172#define TRIGGER_MAX 3
1173static 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
4378f1fb
PU
1186static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1187{
2679a5b2 1188 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
54fc4b72 1189 struct snd_soc_component *component;
356caf66 1190 int ret = 0, r = 0, i;
6374f493 1191 int rollback = 0;
356caf66
KM
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;
4378f1fb 1211
356caf66
KM
1212 /*
1213 * START
1214 */
4378f1fb
PU
1215 switch (cmd) {
1216 case SNDRV_PCM_TRIGGER_START:
1217 case SNDRV_PCM_TRIGGER_RESUME:
1218 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
356caf66
KM
1219 for (i = 0; i < TRIGGER_MAX; i++) {
1220 r = trigger[start][i](substream, cmd, 0);
1221 if (r < 0)
54fc4b72 1222 break;
54fc4b72 1223 }
6374f493
KM
1224 }
1225
356caf66
KM
1226 /*
1227 * Rollback if START failed
1228 * find correspond STOP command
1229 */
1230 if (r < 0) {
1231 rollback = 1;
1232 ret = r;
6374f493
KM
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;
836367be 1239 break;
6374f493
KM
1240 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1241 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1242 break;
1243 }
1244 }
836367be 1245
356caf66
KM
1246 /*
1247 * STOP
1248 */
6374f493 1249 switch (cmd) {
4378f1fb
PU
1250 case SNDRV_PCM_TRIGGER_STOP:
1251 case SNDRV_PCM_TRIGGER_SUSPEND:
1252 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
356caf66
KM
1253 for (i = TRIGGER_MAX; i > 0; i--) {
1254 r = trigger[stop][i - 1](substream, cmd, rollback);
1255 if (r < 0)
1256 ret = r;
59dd33f8 1257 }
4378f1fb
PU
1258 }
1259
1260 return ret;
1261}
1262
ddee627c
LG
1263/*
1264 * soc level wrapper for pointer callback
ef050bec 1265 * If cpu_dai, codec_dai, component driver has the delay callback, then
dd894f4c 1266 * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
ddee627c
LG
1267 */
1268static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1269{
ddee627c
LG
1270 struct snd_pcm_runtime *runtime = substream->runtime;
1271 snd_pcm_uframes_t offset = 0;
2e5894d7 1272 snd_pcm_sframes_t codec_delay = 0;
19bdcc7a 1273 snd_pcm_sframes_t cpu_delay = 0;
ddee627c 1274
0035e256 1275 offset = snd_soc_pcm_component_pointer(substream);
b8135864 1276
403f830e 1277 /* should be called *after* snd_soc_pcm_component_pointer() */
8544f08c 1278 snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
403f830e 1279 snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
ddee627c 1280
dd894f4c 1281 runtime->delay = cpu_delay + codec_delay;
ddee627c
LG
1282
1283 return offset;
1284}
1285
01d7584c
LG
1286/* connect a FE and BE */
1287static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1288 struct snd_soc_pcm_runtime *be, int stream)
1289{
bbf7d3b1
PLB
1290 struct snd_pcm_substream *fe_substream;
1291 struct snd_pcm_substream *be_substream;
01d7584c 1292 struct snd_soc_dpcm *dpcm;
b7898396
TI
1293
1294 snd_soc_dpcm_mutex_assert_held(fe);
01d7584c
LG
1295
1296 /* only add new dpcms */
11c1967f
KM
1297 for_each_dpcm_be(fe, stream, dpcm)
1298 if (dpcm->be == be)
01d7584c 1299 return 0;
01d7584c 1300
bbf7d3b1
PLB
1301 fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1302 be_substream = snd_soc_dpcm_get_substream(be, stream);
1303
be61cd42
KM
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
bbf7d3b1 1309 if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
4ccf0949 1310 dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
bbf7d3b1
PLB
1311 be_substream->pcm->nonatomic = 1;
1312 }
1313
fb6d679f 1314 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
01d7584c
LG
1315 if (!dpcm)
1316 return -ENOMEM;
1317
1318 dpcm->be = be;
1319 dpcm->fe = fe;
01d7584c 1320 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
c8c3d9f8 1321 snd_pcm_stream_lock_irq(fe_substream);
01d7584c
LG
1322 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1323 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
c8c3d9f8 1324 snd_pcm_stream_unlock_irq(fe_substream);
01d7584c 1325
7cc302d2 1326 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
ebbd6703 1327 snd_pcm_direction_name(stream), fe->dai_link->name,
01d7584c
LG
1328 stream ? "<-" : "->", be->dai_link->name);
1329
154dae87
KM
1330 dpcm_create_debugfs_state(dpcm, stream);
1331
01d7584c
LG
1332 return 1;
1333}
1334
1335/* reparent a BE onto another FE */
1336static 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);
db8f91d4
SRM
1347 if (!be_substream)
1348 return;
01d7584c 1349
d2e24d64 1350 for_each_dpcm_fe(be, stream, dpcm) {
01d7584c
LG
1351 if (dpcm->fe == fe)
1352 continue;
1353
7cc302d2 1354 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
ebbd6703 1355 snd_pcm_direction_name(stream),
01d7584c
LG
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 */
23607025 1366void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1367{
1368 struct snd_soc_dpcm *dpcm, *d;
c8c3d9f8 1369 struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
9f620684 1370 LIST_HEAD(deleted_dpcms);
01d7584c 1371
b7898396
TI
1372 snd_soc_dpcm_mutex_assert_held(fe);
1373
c8c3d9f8 1374 snd_pcm_stream_lock_irq(substream);
8d6258a4 1375 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
103d84a3 1376 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
ebbd6703 1377 snd_pcm_direction_name(stream),
01d7584c
LG
1378 dpcm->be->dai_link->name);
1379
1380 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1381 continue;
1382
7cc302d2 1383 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
ebbd6703 1384 snd_pcm_direction_name(stream), fe->dai_link->name,
01d7584c
LG
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);
9f620684
TI
1391 list_move(&dpcm->list_fe, &deleted_dpcms);
1392 }
c8c3d9f8 1393 snd_pcm_stream_unlock_irq(substream);
9f620684
TI
1394
1395 while (!list_empty(&deleted_dpcms)) {
1396 dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
1397 list_fe);
01d7584c 1398 list_del(&dpcm->list_fe);
9f620684 1399 dpcm_remove_debugfs_state(dpcm);
01d7584c
LG
1400 kfree(dpcm);
1401 }
1402}
1403
1404/* get BE for DAI widget and stream */
1405static 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;
93597fae 1409 struct snd_soc_dapm_widget *w;
7afecb30 1410 struct snd_soc_dai *dai;
1a497983 1411 int i;
01d7584c 1412
3c146465
LG
1413 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1414
93597fae 1415 for_each_card_rtds(card, be) {
01d7584c 1416
93597fae
KM
1417 if (!be->dai_link->no_pcm)
1418 continue;
35ea0655 1419
75459065
TI
1420 if (!snd_soc_dpcm_get_substream(be, stream))
1421 continue;
1422
c840f769 1423 for_each_rtd_dais(be, i, dai) {
19bdcc7a 1424 w = snd_soc_dai_get_widget(dai, stream);
3c146465 1425
19bdcc7a
SN
1426 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1427 w ? w->name : "(not set)");
2e5894d7 1428
19bdcc7a
SN
1429 if (w == widget)
1430 return be;
1431 }
01d7584c
LG
1432 }
1433
9d6ee365 1434 /* Widget provided is not a BE */
01d7584c
LG
1435 return NULL;
1436}
1437
5edcf2a3 1438int widget_in_list(struct snd_soc_dapm_widget_list *list,
01d7584c
LG
1439 struct snd_soc_dapm_widget *widget)
1440{
09e88f8a 1441 struct snd_soc_dapm_widget *w;
01d7584c
LG
1442 int i;
1443
09e88f8a
KM
1444 for_each_dapm_widgets(list, i, w)
1445 if (widget == w)
01d7584c 1446 return 1;
01d7584c
LG
1447
1448 return 0;
1449}
5edcf2a3 1450EXPORT_SYMBOL_GPL(widget_in_list);
01d7584c 1451
d1a7af09 1452bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
5fdd022c
PS
1453{
1454 struct snd_soc_card *card = widget->dapm->card;
1455 struct snd_soc_pcm_runtime *rtd;
c2cd8216 1456 int stream;
5fdd022c 1457
c2cd8216
KM
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;
5fdd022c 1463
027a4838
KM
1464 rtd = dpcm_get_be(card, widget, stream);
1465 if (rtd)
1466 return true;
5fdd022c
PS
1467
1468 return false;
1469}
d1a7af09 1470EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
5fdd022c 1471
23607025 1472int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1ce43acf 1473 int stream, struct snd_soc_dapm_widget_list **list)
01d7584c 1474{
2679a5b2 1475 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
01d7584c
LG
1476 int paths;
1477
be61cd42
KM
1478 if (fe->dai_link->num_cpus > 1)
1479 return snd_soc_ret(fe->dev, -EINVAL,
6e1276a5 1480 "%s doesn't support Multi CPU yet\n", __func__);
6e1276a5 1481
01d7584c 1482 /* get number of valid DAI paths and their widgets */
6742064a 1483 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
aa293777
SP
1484 fe->card->component_chaining ?
1485 NULL : dpcm_end_walk_at_be);
01d7584c 1486
d479f00b
KM
1487 if (paths > 0)
1488 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
ebbd6703 1489 snd_pcm_direction_name(stream));
d479f00b
KM
1490 else if (paths == 0)
1491 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
ebbd6703 1492 snd_pcm_direction_name(stream));
01d7584c 1493
01d7584c
LG
1494 return paths;
1495}
1496
52645e33
KM
1497void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1498{
1499 snd_soc_dapm_dai_free_widgets(list);
1500}
1501
8cce6569
GL
1502static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1503 struct snd_soc_dapm_widget_list *list)
01d7584c 1504{
7afecb30 1505 struct snd_soc_dai *dai;
8cce6569
GL
1506 unsigned int i;
1507
c840f769
KM
1508 /* is there a valid DAI widget for this BE */
1509 for_each_rtd_dais(dpcm->be, i, dai) {
7931df9b 1510 struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
8cce6569
GL
1511
1512 /*
c840f769 1513 * The BE is pruned only if none of the dai
8cce6569
GL
1514 * widgets are in the active list.
1515 */
1516 if (widget && widget_in_list(list, widget))
1517 return true;
1518 }
1519
8cce6569
GL
1520 return false;
1521}
1522
1523static 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;
01d7584c
LG
1527 int prune = 0;
1528
1529 /* Destroy any old FE <--> BE connections */
8d6258a4 1530 for_each_dpcm_be(fe, stream, dpcm) {
8cce6569 1531 if (dpcm_be_is_active(dpcm, stream, *list_))
bed646dc 1532 continue;
01d7584c 1533
103d84a3 1534 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
ebbd6703 1535 snd_pcm_direction_name(stream),
01d7584c
LG
1536 dpcm->be->dai_link->name, fe->dai_link->name);
1537 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
a7e20444 1538 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
01d7584c
LG
1539 prune++;
1540 }
1541
103d84a3 1542 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
01d7584c
LG
1543 return prune;
1544}
1545
40b1f89a
KM
1546int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1547 struct snd_soc_dapm_widget_list **list_)
01d7584c
LG
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;
09e88f8a 1552 struct snd_soc_dapm_widget *widget;
0d3a5178 1553 struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
01d7584c
LG
1554 int i, new = 0, err;
1555
6932b20d 1556 /* don't connect if FE is not running */
0d3a5178 1557 if (!fe_substream->runtime && !fe->fe_compr)
6932b20d
KM
1558 return new;
1559
01d7584c 1560 /* Create any new FE <--> BE connections */
09e88f8a 1561 for_each_dapm_widgets(list, i, widget) {
01d7584c 1562
09e88f8a 1563 switch (widget->id) {
4616274d 1564 case snd_soc_dapm_dai_in:
c5b8540d
KC
1565 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1566 continue;
1567 break;
4616274d 1568 case snd_soc_dapm_dai_out:
c5b8540d
KC
1569 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1570 continue;
4616274d
MB
1571 break;
1572 default:
01d7584c 1573 continue;
4616274d 1574 }
01d7584c
LG
1575
1576 /* is there a valid BE rtd for this widget */
09e88f8a 1577 be = dpcm_get_be(card, widget, stream);
01d7584c 1578 if (!be) {
b6eabd24
SW
1579 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1580 widget->name);
01d7584c
LG
1581 continue;
1582 }
1583
9609cfcd
PLB
1584 /*
1585 * Filter for systems with 'component_chaining' enabled.
1586 * This helps to avoid unnecessary re-configuration of an
9aff2e8d
S
1587 * already active BE on such systems and ensures the BE DAI
1588 * widget is powered ON after hw_params() BE DAI callback.
9609cfcd
PLB
1589 */
1590 if (fe->card->component_chaining &&
1591 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
9aff2e8d
S
1592 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1593 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
0c25db3f
SP
1594 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1595 continue;
1596
01d7584c
LG
1597 /* newly connected FE and BE */
1598 err = dpcm_be_connect(fe, be, stream);
1599 if (err < 0) {
103d84a3 1600 dev_err(fe->dev, "ASoC: can't connect %s\n",
09e88f8a 1601 widget->name);
01d7584c
LG
1602 break;
1603 } else if (err == 0) /* already connected */
1604 continue;
1605
1606 /* new */
a7e20444 1607 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
01d7584c
LG
1608 new++;
1609 }
1610
103d84a3 1611 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
01d7584c
LG
1612 return new;
1613}
1614
23607025 1615void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1616{
1617 struct snd_soc_dpcm *dpcm;
1618
8d6258a4 1619 for_each_dpcm_be(fe, stream, dpcm)
a7e20444 1620 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
1621}
1622
531590bb
KM
1623void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1624 int do_hw_free, struct snd_soc_dpcm *last)
01d7584c
LG
1625{
1626 struct snd_soc_dpcm *dpcm;
1627
1628 /* disable any enabled and non active backends */
8d6258a4 1629 for_each_dpcm_be(fe, stream, dpcm) {
01d7584c
LG
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
531590bb
KM
1634 if (dpcm == last)
1635 return;
1636
1637 /* is this op for this BE ? */
ede6445d 1638 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
531590bb
KM
1639 continue;
1640
1db19c15 1641 if (be->dpcm[stream].users == 0) {
7a2ff051 1642 dev_err(be->dev, "ASoC: no users %s at close - state %s\n",
ebbd6703 1643 snd_pcm_direction_name(stream),
7a2ff051 1644 dpcm_state_string(be->dpcm[stream].state));
1db19c15
KM
1645 continue;
1646 }
01d7584c
LG
1647
1648 if (--be->dpcm[stream].users != 0)
1649 continue;
1650
531590bb
KM
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) {
b7898396 1656 __soc_pcm_hw_free(be, be_substream);
531590bb
KM
1657 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1658 }
1659 }
01d7584c 1660
b7898396 1661 __soc_pcm_close(be, be_substream);
01d7584c
LG
1662 be_substream->runtime = NULL;
1663 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1664 }
1665}
1666
23607025 1667int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c 1668{
0d3a5178 1669 struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
06aaeb87 1670 struct snd_soc_pcm_runtime *be;
01d7584c
LG
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 */
8d6258a4 1675 for_each_dpcm_be(fe, stream, dpcm) {
06aaeb87 1676 struct snd_pcm_substream *be_substream;
01d7584c 1677
06aaeb87
KM
1678 be = dpcm->be;
1679 be_substream = snd_soc_dpcm_get_substream(be, stream);
01d7584c 1680
2062b4c5
RKAL
1681 if (!be_substream) {
1682 dev_err(be->dev, "ASoC: no backend %s stream\n",
ebbd6703 1683 snd_pcm_direction_name(stream));
2062b4c5
RKAL
1684 continue;
1685 }
1686
01d7584c 1687 /* is this op for this BE ? */
ede6445d 1688 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
01d7584c
LG
1689 continue;
1690
1691 /* first time the dpcm is open ? */
1db19c15 1692 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
7a2ff051 1693 dev_err(be->dev, "ASoC: too many users %s at open %s\n",
ebbd6703 1694 snd_pcm_direction_name(stream),
7a2ff051 1695 dpcm_state_string(be->dpcm[stream].state));
1db19c15
KM
1696 continue;
1697 }
01d7584c
LG
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
2062b4c5 1706 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
ebbd6703 1707 snd_pcm_direction_name(stream), be->dai_link->name);
01d7584c 1708
0d3a5178 1709 be_substream->runtime = fe_substream->runtime;
b7898396 1710 err = __soc_pcm_open(be, be_substream);
01d7584c 1711 if (err < 0) {
01d7584c
LG
1712 be->dpcm[stream].users--;
1713 if (be->dpcm[stream].users < 0)
7a2ff051 1714 dev_err(be->dev, "ASoC: no users %s at unwind %s\n",
ebbd6703 1715 snd_pcm_direction_name(stream),
7a2ff051 1716 dpcm_state_string(be->dpcm[stream].state));
01d7584c
LG
1717
1718 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1719 goto unwind;
1720 }
848aedfd 1721 be->dpcm[stream].be_start = 0;
01d7584c
LG
1722 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1723 count++;
1724 }
1725
1726 return count;
1727
1728unwind:
531590bb 1729 dpcm_be_dai_startup_rollback(fe, stream, dpcm);
01d7584c 1730
04110728 1731 return soc_pcm_ret(fe, err);
01d7584c
LG
1732}
1733
5f53898a
KM
1734static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1735{
2679a5b2 1736 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
5f53898a
KM
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;
083a25b1 1741 u64 formats = hw->formats;
5f53898a
KM
1742 int i;
1743
1744 soc_pcm_hw_init(hw);
1745
083a25b1
SW
1746 if (formats)
1747 hw->formats &= formats;
1748
5f53898a 1749 for_each_rtd_cpu_dais(fe, i, dai) {
de267e7a 1750 const struct snd_soc_pcm_stream *cpu_stream;
5f53898a
KM
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
1b8cb123 1768static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
b073ed4e 1769{
2679a5b2 1770 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1b8cb123 1771 struct snd_pcm_runtime *runtime = substream->runtime;
4b260f42 1772 struct snd_pcm_hardware *hw = &runtime->hw;
b073ed4e 1773 struct snd_soc_dpcm *dpcm;
7afecb30 1774 struct snd_soc_dai *dai;
b073ed4e
KM
1775 int stream = substream->stream;
1776
1777 if (!fe->dai_link->dpcm_merged_format)
435ffb76 1778 return;
b073ed4e
KM
1779
1780 /*
1781 * It returns merged BE codec format
1782 * if FE want to use it (= dpcm_merged_format)
1783 */
1784
8d6258a4 1785 for_each_dpcm_be(fe, stream, dpcm) {
b073ed4e 1786 struct snd_soc_pcm_runtime *be = dpcm->be;
de267e7a 1787 const struct snd_soc_pcm_stream *codec_stream;
b073ed4e
KM
1788 int i;
1789
a4be4187 1790 for_each_rtd_codec_dais(be, i, dai) {
4febced1
JB
1791 /*
1792 * Skip CODECs which don't support the current stream
1793 * type. See soc_pcm_init_runtime_hw() for more details
1794 */
7afecb30 1795 if (!snd_soc_dai_stream_valid(dai, stream))
4febced1
JB
1796 continue;
1797
acf253c1 1798 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
b073ed4e 1799
debc71f2 1800 soc_pcm_hw_update_format(hw, codec_stream);
b073ed4e
KM
1801 }
1802 }
b073ed4e
KM
1803}
1804
1b8cb123 1805static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
f4c277b8 1806{
2679a5b2 1807 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1b8cb123 1808 struct snd_pcm_runtime *runtime = substream->runtime;
4b260f42 1809 struct snd_pcm_hardware *hw = &runtime->hw;
f4c277b8
JW
1810 struct snd_soc_dpcm *dpcm;
1811 int stream = substream->stream;
1812
1813 if (!fe->dai_link->dpcm_merged_chan)
1814 return;
1815
f4c277b8
JW
1816 /*
1817 * It returns merged BE codec channel;
1818 * if FE want to use it (= dpcm_merged_chan)
1819 */
1820
8d6258a4 1821 for_each_dpcm_be(fe, stream, dpcm) {
f4c277b8 1822 struct snd_soc_pcm_runtime *be = dpcm->be;
de267e7a 1823 const struct snd_soc_pcm_stream *cpu_stream;
19bdcc7a
SN
1824 struct snd_soc_dai *dai;
1825 int i;
4f2bd18b 1826
a4be4187 1827 for_each_rtd_cpu_dais(be, i, dai) {
0e9cf4c4
BL
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
19bdcc7a 1835 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
4f2bd18b 1836
6cb56a45 1837 soc_pcm_hw_update_chan(hw, cpu_stream);
19bdcc7a 1838 }
4f2bd18b
JB
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 */
3989ade2 1844 if (be->dai_link->num_codecs == 1) {
de267e7a 1845 const struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
2679a5b2 1846 snd_soc_rtd_to_codec(be, 0), stream);
f4c277b8 1847
6cb56a45 1848 soc_pcm_hw_update_chan(hw, codec_stream);
f4c277b8
JW
1849 }
1850 }
1851}
1852
1b8cb123 1853static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
baacd8d1 1854{
2679a5b2 1855 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1b8cb123 1856 struct snd_pcm_runtime *runtime = substream->runtime;
4b260f42 1857 struct snd_pcm_hardware *hw = &runtime->hw;
baacd8d1
JB
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
8d6258a4 1869 for_each_dpcm_be(fe, stream, dpcm) {
baacd8d1 1870 struct snd_soc_pcm_runtime *be = dpcm->be;
de267e7a 1871 const struct snd_soc_pcm_stream *pcm;
7afecb30 1872 struct snd_soc_dai *dai;
baacd8d1
JB
1873 int i;
1874
c840f769 1875 for_each_rtd_dais(be, i, dai) {
baacd8d1 1876 /*
c840f769 1877 * Skip DAIs which don't support the current stream
baacd8d1
JB
1878 * type. See soc_pcm_init_runtime_hw() for more details
1879 */
7afecb30 1880 if (!snd_soc_dai_stream_valid(dai, stream))
baacd8d1
JB
1881 continue;
1882
c840f769 1883 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
baacd8d1 1884
f6c04af5 1885 soc_pcm_hw_update_rate(hw, pcm);
baacd8d1
JB
1886 }
1887 }
1888}
1889
906c7d69
PL
1890static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1891 int stream)
1892{
1893 struct snd_soc_dpcm *dpcm;
2679a5b2 1894 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
19bdcc7a 1895 struct snd_soc_dai *fe_cpu_dai;
12ffd726 1896 int err = 0;
19bdcc7a 1897 int i;
906c7d69
PL
1898
1899 /* apply symmetry for FE */
68cbc557 1900 soc_pcm_update_symmetry(fe_substream);
906c7d69 1901
a4be4187 1902 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
19bdcc7a 1903 /* Symmetry only applies if we've got an active stream. */
f8fc9ec5
KM
1904 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1905 if (err < 0)
bbd2bac8 1906 goto error;
906c7d69
PL
1907 }
1908
1909 /* apply symmetry for BE */
8d6258a4 1910 for_each_dpcm_be(fe, stream, dpcm) {
906c7d69
PL
1911 struct snd_soc_pcm_runtime *be = dpcm->be;
1912 struct snd_pcm_substream *be_substream =
1913 snd_soc_dpcm_get_substream(be, stream);
6246f283 1914 struct snd_soc_pcm_runtime *rtd;
c840f769 1915 struct snd_soc_dai *dai;
906c7d69 1916
6246f283
JB
1917 /* A backend may not have the requested substream */
1918 if (!be_substream)
1919 continue;
1920
2679a5b2 1921 rtd = snd_soc_substream_to_rtd(be_substream);
f1176614
JK
1922 if (rtd->dai_link->be_hw_params_fixup)
1923 continue;
1924
68cbc557 1925 soc_pcm_update_symmetry(be_substream);
906c7d69
PL
1926
1927 /* Symmetry only applies if we've got an active stream. */
c840f769 1928 for_each_rtd_dais(rtd, i, dai) {
f8fc9ec5
KM
1929 err = soc_pcm_apply_symmetry(fe_substream, dai);
1930 if (err < 0)
bbd2bac8 1931 goto error;
906c7d69
PL
1932 }
1933 }
bbd2bac8 1934error:
04110728 1935 return soc_pcm_ret(fe, err);
906c7d69
PL
1936}
1937
01d7584c
LG
1938static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1939{
2679a5b2 1940 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
01d7584c
LG
1941 int stream = fe_substream->stream, ret = 0;
1942
ea9d0d77 1943 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c 1944
25c2f515 1945 ret = dpcm_be_dai_startup(fe, stream);
06aaeb87 1946 if (ret < 0)
01d7584c 1947 goto be_err;
01d7584c 1948
103d84a3 1949 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
01d7584c
LG
1950
1951 /* start the DAI frontend */
b7898396 1952 ret = __soc_pcm_open(fe, fe_substream);
e4b044f4 1953 if (ret < 0)
01d7584c 1954 goto unwind;
01d7584c
LG
1955
1956 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1957
4fe28461
KM
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);
01d7584c 1963
906c7d69 1964 ret = dpcm_apply_symmetry(fe_substream, stream);
01d7584c
LG
1965
1966unwind:
8a01fbf0
KM
1967 if (ret < 0)
1968 dpcm_be_dai_startup_unwind(fe, stream);
01d7584c 1969be_err:
ea9d0d77 1970 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
06aaeb87 1971
04110728 1972 return soc_pcm_ret(fe, ret);
01d7584c
LG
1973}
1974
01d7584c
LG
1975static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1976{
2679a5b2 1977 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
01d7584c
LG
1978 int stream = substream->stream;
1979
b7898396
TI
1980 snd_soc_dpcm_mutex_assert_held(fe);
1981
ea9d0d77 1982 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c
LG
1983
1984 /* shutdown the BEs */
25c2f515 1985 dpcm_be_dai_shutdown(fe, stream);
01d7584c 1986
103d84a3 1987 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
01d7584c
LG
1988
1989 /* now shutdown the frontend */
b7898396 1990 __soc_pcm_close(fe, substream);
01d7584c 1991
bb9dd3ce
RS
1992 /* run the stream stop event */
1993 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1994
01d7584c 1995 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
ea9d0d77 1996 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
1997 return 0;
1998}
1999
f52366e6 2000void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
2001{
2002 struct snd_soc_dpcm *dpcm;
2003
2004 /* only hw_params backends that are either sinks or sources
2005 * to this frontend DAI */
8d6258a4 2006 for_each_dpcm_be(fe, stream, dpcm) {
01d7584c
LG
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 ? */
ede6445d 2013 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
01d7584c
LG
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
36fba62c
QZ
2020 /* do not free hw if this BE is used by other FE */
2021 if (be->dpcm[stream].users > 1)
2022 continue;
2023
01d7584c
LG
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) &&
08b27848 2027 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
5e82d2be
VK
2028 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2029 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
01d7584c
LG
2030 continue;
2031
103d84a3 2032 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
94d215cc 2033 be->dai_link->name);
01d7584c 2034
b7898396 2035 __soc_pcm_hw_free(be, be_substream);
01d7584c
LG
2036
2037 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2038 }
01d7584c
LG
2039}
2040
45c0a188 2041static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
01d7584c 2042{
2679a5b2 2043 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
f52366e6 2044 int stream = substream->stream;
01d7584c 2045
b7898396 2046 snd_soc_dpcm_mutex_lock(fe);
ea9d0d77 2047 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c 2048
103d84a3 2049 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
01d7584c
LG
2050
2051 /* call hw_free on the frontend */
b7898396 2052 soc_pcm_hw_clean(fe, substream, 0);
01d7584c
LG
2053
2054 /* only hw_params backends that are either sinks or sources
2055 * to this frontend DAI */
f52366e6 2056 dpcm_be_dai_hw_free(fe, stream);
01d7584c
LG
2057
2058 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
ea9d0d77 2059 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c 2060
b7898396 2061 snd_soc_dpcm_mutex_unlock(fe);
01d7584c
LG
2062 return 0;
2063}
2064
23607025 2065int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c 2066{
33b6b94f
KM
2067 struct snd_soc_pcm_runtime *be;
2068 struct snd_pcm_substream *be_substream;
01d7584c
LG
2069 struct snd_soc_dpcm *dpcm;
2070 int ret;
2071
8d6258a4 2072 for_each_dpcm_be(fe, stream, dpcm) {
25106550
KM
2073 struct snd_pcm_hw_params hw_params;
2074
33b6b94f
KM
2075 be = dpcm->be;
2076 be_substream = snd_soc_dpcm_get_substream(be, stream);
01d7584c
LG
2077
2078 /* is this op for this BE ? */
ede6445d 2079 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
01d7584c
LG
2080 continue;
2081
01d7584c 2082 /* copy params for each dpcm */
25106550 2083 memcpy(&hw_params, &fe->dpcm[stream].hw_params,
01d7584c
LG
2084 sizeof(struct snd_pcm_hw_params));
2085
2086 /* perform any hw_params fixups */
25106550 2087 ret = snd_soc_link_be_hw_params_fixup(be, &hw_params);
0cbbf8a0
KM
2088 if (ret < 0)
2089 goto unwind;
01d7584c 2090
ae061d2a 2091 /* copy the fixed-up hw params for BE dai */
25106550 2092 memcpy(&be->dpcm[stream].hw_params, &hw_params,
ae061d2a
LY
2093 sizeof(struct snd_pcm_hw_params));
2094
b0639bd2
KM
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",
94d215cc 2105 be->dai_link->name);
b0639bd2 2106
0c4a0639 2107 ret = __soc_pcm_hw_params(be_substream, &hw_params);
cb11f79b 2108 if (ret < 0)
01d7584c 2109 goto unwind;
01d7584c
LG
2110
2111 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2112 }
2113 return 0;
2114
2115unwind:
33b6b94f
KM
2116 dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2117 __func__, be->dai_link->name, ret);
2118
01d7584c 2119 /* disable any enabled and non active backends */
8d6258a4 2120 for_each_dpcm_be_rollback(fe, stream, dpcm) {
33b6b94f
KM
2121 be = dpcm->be;
2122 be_substream = snd_soc_dpcm_get_substream(be, stream);
01d7584c 2123
ede6445d 2124 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
01d7584c
LG
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
b7898396 2137 __soc_pcm_hw_free(be, be_substream);
01d7584c
LG
2138 }
2139
2140 return ret;
2141}
2142
45c0a188
MB
2143static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2144 struct snd_pcm_hw_params *params)
01d7584c 2145{
2679a5b2 2146 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
01d7584c
LG
2147 int ret, stream = substream->stream;
2148
b7898396 2149 snd_soc_dpcm_mutex_lock(fe);
ea9d0d77 2150 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c 2151
25c2f515 2152 memcpy(&fe->dpcm[stream].hw_params, params,
01d7584c 2153 sizeof(struct snd_pcm_hw_params));
25c2f515 2154 ret = dpcm_be_dai_hw_params(fe, stream);
33b6b94f 2155 if (ret < 0)
01d7584c 2156 goto out;
01d7584c 2157
103d84a3 2158 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
01d7584c
LG
2159 fe->dai_link->name, params_rate(params),
2160 params_channels(params), params_format(params));
2161
2162 /* call hw_params on the frontend */
0c4a0639 2163 ret = __soc_pcm_hw_params(substream, params);
cb11f79b 2164 if (ret < 0)
01d7584c 2165 dpcm_be_dai_hw_free(fe, stream);
cb11f79b 2166 else
01d7584c
LG
2167 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2168
2169out:
ea9d0d77 2170 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
b7898396 2171 snd_soc_dpcm_mutex_unlock(fe);
33b6b94f 2172
04110728 2173 return soc_pcm_ret(fe, ret);
01d7584c
LG
2174}
2175
23607025 2176int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
45c0a188 2177 int cmd)
01d7584c 2178{
db3aa39c 2179 struct snd_soc_pcm_runtime *be;
9995c1d0 2180 bool pause_stop_transition;
01d7584c 2181 struct snd_soc_dpcm *dpcm;
b2ae8066 2182 unsigned long flags;
01d7584c
LG
2183 int ret = 0;
2184
8d6258a4 2185 for_each_dpcm_be(fe, stream, dpcm) {
db3aa39c 2186 struct snd_pcm_substream *be_substream;
01d7584c 2187
db3aa39c
KM
2188 be = dpcm->be;
2189 be_substream = snd_soc_dpcm_get_substream(be, stream);
01d7584c 2190
2d3b218d 2191 snd_pcm_stream_lock_irqsave_nested(be_substream, flags);
b2ae8066 2192
01d7584c 2193 /* is this op for this BE ? */
ede6445d 2194 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
b2ae8066 2195 goto next;
01d7584c 2196
a9faca15
KM
2197 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2198 be->dai_link->name, cmd);
2199
01d7584c
LG
2200 switch (cmd) {
2201 case SNDRV_PCM_TRIGGER_START:
848aedfd
PLB
2202 if (!be->dpcm[stream].be_start &&
2203 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
21fca8bd 2204 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
3202e2f5 2205 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
b2ae8066 2206 goto next;
6479f758 2207
848aedfd
PLB
2208 be->dpcm[stream].be_start++;
2209 if (be->dpcm[stream].be_start != 1)
2210 goto next;
2211
374b50e2
PLB
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);
848aedfd
PLB
2218 if (ret) {
2219 be->dpcm[stream].be_start--;
b2ae8066 2220 goto next;
848aedfd 2221 }
01d7584c 2222
3202e2f5 2223 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
01d7584c
LG
2224 break;
2225 case SNDRV_PCM_TRIGGER_RESUME:
3202e2f5 2226 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
b2ae8066 2227 goto next;
6479f758 2228
848aedfd
PLB
2229 be->dpcm[stream].be_start++;
2230 if (be->dpcm[stream].be_start != 1)
2231 goto next;
2232
a9faca15 2233 ret = soc_pcm_trigger(be_substream, cmd);
848aedfd
PLB
2234 if (ret) {
2235 be->dpcm[stream].be_start--;
b2ae8066 2236 goto next;
848aedfd 2237 }
01d7584c 2238
3202e2f5 2239 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
01d7584c
LG
2240 break;
2241 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
3aa1e96a
PLB
2242 if (!be->dpcm[stream].be_start &&
2243 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
3aa1e96a 2244 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
b2ae8066 2245 goto next;
6479f758 2246
9995c1d0
PLB
2247 fe->dpcm[stream].fe_pause = false;
2248 be->dpcm[stream].be_pause--;
2249
848aedfd
PLB
2250 be->dpcm[stream].be_start++;
2251 if (be->dpcm[stream].be_start != 1)
2252 goto next;
2253
a9faca15 2254 ret = soc_pcm_trigger(be_substream, cmd);
848aedfd
PLB
2255 if (ret) {
2256 be->dpcm[stream].be_start--;
b2ae8066 2257 goto next;
848aedfd 2258 }
01d7584c 2259
3202e2f5 2260 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
01d7584c
LG
2261 break;
2262 case SNDRV_PCM_TRIGGER_STOP:
21fca8bd 2263 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
3202e2f5 2264 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
b2ae8066 2265 goto next;
01d7584c 2266
848aedfd
PLB
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)
b2ae8066 2271 goto next;
0c75fc71 2272
9995c1d0
PLB
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
848aedfd
PLB
2285 if (ret) {
2286 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2287 be->dpcm[stream].be_start++;
9995c1d0
PLB
2288 if (pause_stop_transition) {
2289 fe->dpcm[stream].fe_pause = true;
2290 be->dpcm[stream].be_pause++;
2291 }
b2ae8066 2292 goto next;
848aedfd 2293 }
01d7584c 2294
9995c1d0
PLB
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
01d7584c
LG
2300 break;
2301 case SNDRV_PCM_TRIGGER_SUSPEND:
3202e2f5 2302 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
b2ae8066 2303 goto next;
01d7584c 2304
848aedfd
PLB
2305 be->dpcm[stream].be_start--;
2306 if (be->dpcm[stream].be_start != 0)
b2ae8066 2307 goto next;
01d7584c 2308
a9faca15 2309 ret = soc_pcm_trigger(be_substream, cmd);
848aedfd
PLB
2310 if (ret) {
2311 be->dpcm[stream].be_start++;
b2ae8066 2312 goto next;
848aedfd 2313 }
01d7584c 2314
3202e2f5 2315 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
01d7584c
LG
2316 break;
2317 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
3202e2f5 2318 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
b2ae8066 2319 goto next;
01d7584c 2320
9995c1d0
PLB
2321 fe->dpcm[stream].fe_pause = true;
2322 be->dpcm[stream].be_pause++;
2323
848aedfd
PLB
2324 be->dpcm[stream].be_start--;
2325 if (be->dpcm[stream].be_start != 0)
b2ae8066 2326 goto next;
01d7584c 2327
a9faca15 2328 ret = soc_pcm_trigger(be_substream, cmd);
848aedfd
PLB
2329 if (ret) {
2330 be->dpcm[stream].be_start++;
b2ae8066 2331 goto next;
848aedfd 2332 }
01d7584c 2333
3202e2f5 2334 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
01d7584c
LG
2335 break;
2336 }
b2ae8066 2337next:
2d3b218d 2338 snd_pcm_stream_unlock_irqrestore(be_substream, flags);
b2ae8066
TI
2339 if (ret)
2340 break;
01d7584c 2341 }
04110728 2342 return soc_pcm_ret(fe, ret);
01d7584c
LG
2343}
2344EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2345
acbf2774
RS
2346static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2347 int cmd, bool fe_first)
2348{
2679a5b2 2349 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
acbf2774
RS
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)
be61cd42 2359 goto end;
acbf2774
RS
2360
2361 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
acbf2774 2362 }
acbf2774 2363 /* call trigger on the frontend after the backend. */
3aebbcba
KM
2364 else {
2365 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2366 if (ret < 0)
2367 goto end;
acbf2774 2368
3aebbcba
KM
2369 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2370 fe->dai_link->name, cmd);
acbf2774 2371
3aebbcba
KM
2372 ret = soc_pcm_trigger(substream, cmd);
2373 }
be61cd42
KM
2374end:
2375 return snd_soc_ret(fe->dev, ret, "trigger FE cmd: %d failed\n", cmd);
acbf2774
RS
2376}
2377
ea9d0d77 2378static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
01d7584c 2379{
2679a5b2 2380 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
acbf2774
RS
2381 int stream = substream->stream;
2382 int ret = 0;
42da18e6 2383 int fe_first;
01d7584c
LG
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:
42da18e6 2390 fe_first = true;
01d7584c
LG
2391 break;
2392 case SND_SOC_DPCM_TRIGGER_POST:
42da18e6 2393 fe_first = false;
01d7584c
LG
2394 break;
2395 default:
103d84a3 2396 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
01d7584c
LG
2397 fe->dai_link->name);
2398 ret = -EINVAL;
2399 goto out;
2400 }
2401
42da18e6
KM
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
be61cd42 2419 if (ret < 0)
acbf2774 2420 goto out;
acbf2774 2421
01d7584c
LG
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:
01d7584c
LG
2430 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2431 break;
9f169b9f
PL
2432 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2433 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2434 break;
01d7584c
LG
2435 }
2436
2437out:
2438 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2439 return ret;
2440}
2441
ea9d0d77
TI
2442static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2443{
2679a5b2 2444 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
ea9d0d77
TI
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
23607025 2459int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
2460{
2461 struct snd_soc_dpcm *dpcm;
2462 int ret = 0;
2463
8d6258a4 2464 for_each_dpcm_be(fe, stream, dpcm) {
01d7584c
LG
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 ? */
ede6445d 2471 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
01d7584c
LG
2472 continue;
2473
e123036b
RS
2474 if (!snd_soc_dpcm_can_be_prepared(fe, be, stream))
2475 continue;
2476
01d7584c 2477 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
95f444dc 2478 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
5087a8f1
LY
2479 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2480 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
01d7584c
LG
2481 continue;
2482
103d84a3 2483 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
94d215cc 2484 be->dai_link->name);
01d7584c 2485
b7898396 2486 ret = __soc_pcm_prepare(be, be_substream);
dab7eeb4 2487 if (ret < 0)
01d7584c 2488 break;
01d7584c
LG
2489
2490 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2491 }
273db971 2492
301c26a0
KM
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;
01d7584c
LG
2500}
2501
45c0a188 2502static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
01d7584c 2503{
2679a5b2 2504 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
01d7584c
LG
2505 int stream = substream->stream, ret = 0;
2506
b7898396 2507 snd_soc_dpcm_mutex_lock(fe);
01d7584c 2508
103d84a3 2509 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
01d7584c 2510
ea9d0d77 2511 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c 2512
25c2f515 2513 ret = dpcm_be_dai_prepare(fe, stream);
01d7584c
LG
2514 if (ret < 0)
2515 goto out;
2516
2517 /* call prepare on the frontend */
b7898396 2518 ret = __soc_pcm_prepare(fe, substream);
dab7eeb4 2519 if (ret < 0)
01d7584c 2520 goto out;
01d7584c 2521
01d7584c
LG
2522 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2523
2524out:
ea9d0d77 2525 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
b7898396 2526 snd_soc_dpcm_mutex_unlock(fe);
01d7584c 2527
301c26a0
KM
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;
01d7584c
LG
2535}
2536
618dae11
LG
2537static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2538{
2539 int err;
2540
103d84a3 2541 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
ebbd6703 2542 snd_pcm_direction_name(stream), fe->dai_link->name);
618dae11 2543
7d2fb381 2544 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
618dae11 2545
f52366e6 2546 dpcm_be_dai_hw_free(fe, stream);
618dae11 2547
531590bb 2548 dpcm_be_dai_shutdown(fe, stream);
618dae11
LG
2549
2550 /* run the stream event for each BE */
2551 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2552
04110728 2553 return soc_pcm_ret(fe, err);
618dae11
LG
2554}
2555
2556static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2557{
2558 struct snd_soc_dpcm *dpcm;
4eeed5f4 2559 int ret = 0;
618dae11 2560
103d84a3 2561 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
ebbd6703 2562 snd_pcm_direction_name(stream), fe->dai_link->name);
618dae11
LG
2563
2564 /* Only start the BE if the FE is ready */
2565 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2c138284 2566 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
7a2ff051
KM
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));
e91b65b3 2569 ret = -EINVAL;
2c138284 2570 goto disconnect;
2571 }
618dae11
LG
2572
2573 /* startup must always be called for new BEs */
2574 ret = dpcm_be_dai_startup(fe, stream);
fffc0ca2 2575 if (ret < 0)
618dae11 2576 goto disconnect;
618dae11
LG
2577
2578 /* keep going if FE state is > open */
2579 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2580 return 0;
01d7584c 2581
618dae11 2582 ret = dpcm_be_dai_hw_params(fe, stream);
fffc0ca2 2583 if (ret < 0)
618dae11 2584 goto close;
618dae11
LG
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
618dae11 2590 ret = dpcm_be_dai_prepare(fe, stream);
fffc0ca2 2591 if (ret < 0)
618dae11 2592 goto hw_free;
618dae11
LG
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
7d2fb381
KM
2602 ret = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_START);
2603 if (ret < 0)
2604 goto hw_free;
618dae11
LG
2605
2606 return 0;
2607
2608hw_free:
2609 dpcm_be_dai_hw_free(fe, stream);
2610close:
2611 dpcm_be_dai_shutdown(fe, stream);
2612disconnect:
2c138284 2613 /* disconnect any pending BEs */
8d6258a4 2614 for_each_dpcm_be(fe, stream, dpcm) {
618dae11 2615 struct snd_soc_pcm_runtime *be = dpcm->be;
2c138284 2616
2617 /* is this op for this BE ? */
ede6445d 2618 if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2c138284 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;
618dae11
LG
2624 }
2625
04110728 2626 return soc_pcm_ret(fe, ret);
618dae11
LG
2627}
2628
de15d7ff 2629static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
618dae11 2630{
de15d7ff 2631 struct snd_soc_dapm_widget_list *list;
7083f877 2632 int stream;
de15d7ff 2633 int count, paths;
618dae11 2634
96bf62f0
PLB
2635 if (!fe->dai_link->dynamic)
2636 return 0;
2637
be61cd42
KM
2638 if (fe->dai_link->num_cpus > 1)
2639 return snd_soc_ret(fe->dev, -EINVAL,
6e1276a5 2640 "%s doesn't support Multi CPU yet\n", __func__);
6e1276a5 2641
de15d7ff 2642 /* only check active links */
2679a5b2 2643 if (!snd_soc_dai_active(snd_soc_rtd_to_cpu(fe, 0)))
de15d7ff 2644 return 0;
618dae11 2645
de15d7ff
JB
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);
618dae11 2649
7083f877 2650 for_each_pcm_streams(stream) {
618dae11 2651
7083f877 2652 /* skip if FE doesn't have playback/capture capability */
2679a5b2
KM
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))
7083f877 2655 continue;
de15d7ff 2656
7083f877 2657 /* skip if FE isn't currently playing/capturing */
2679a5b2
KM
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))
7083f877 2660 continue;
075207d2 2661
7083f877 2662 paths = dpcm_path_get(fe, stream, &list);
d479f00b 2663 if (paths < 0)
7083f877 2664 return paths;
618dae11 2665
7083f877 2666 /* update any playback/capture paths */
40b1f89a
KM
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);
7083f877 2675 if (count) {
580dff36 2676 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
7083f877 2677 if (new)
81c82a9e 2678 dpcm_run_update_startup(fe, stream);
7083f877 2679 else
81c82a9e 2680 dpcm_run_update_shutdown(fe, stream);
580dff36 2681 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
618dae11 2682
7083f877
KM
2683 dpcm_clear_pending_state(fe, stream);
2684 dpcm_be_disconnect(fe, stream);
2685 }
618dae11 2686
7083f877 2687 dpcm_path_put(&list);
618dae11
LG
2688 }
2689
618dae11
LG
2690 return 0;
2691}
de15d7ff
JB
2692
2693/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2694 * any DAI links.
2695 */
f17a1478 2696int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
de15d7ff
JB
2697{
2698 struct snd_soc_pcm_runtime *fe;
2699 int ret = 0;
2700
38e42f6d 2701 snd_soc_dpcm_mutex_lock(card);
de15d7ff 2702 /* shutdown all old paths first */
bcb1fd1f 2703 for_each_card_rtds(card, fe) {
de15d7ff
JB
2704 ret = soc_dpcm_fe_runtime_update(fe, 0);
2705 if (ret)
2706 goto out;
2707 }
2708
2709 /* bring new paths up */
bcb1fd1f 2710 for_each_card_rtds(card, fe) {
de15d7ff
JB
2711 ret = soc_dpcm_fe_runtime_update(fe, 1);
2712 if (ret)
2713 goto out;
2714 }
2715
2716out:
38e42f6d 2717 snd_soc_dpcm_mutex_unlock(card);
be61cd42
KM
2718
2719 return snd_soc_ret(card->dev, ret, "%s() failed\n", __func__);
de15d7ff 2720}
f17a1478 2721EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
01d7584c 2722
265694b6 2723static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
01d7584c 2724{
2679a5b2 2725 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
01d7584c 2726 struct snd_soc_dpcm *dpcm;
265694b6 2727 int stream = fe_substream->stream;
30fca26f 2728
b7898396
TI
2729 snd_soc_dpcm_mutex_assert_held(fe);
2730
30fca26f
KM
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);
265694b6
KM
2736}
2737
2738static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2739{
2679a5b2 2740 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
265694b6
KM
2741 int ret;
2742
b7898396 2743 snd_soc_dpcm_mutex_lock(fe);
265694b6
KM
2744 ret = dpcm_fe_dai_shutdown(fe_substream);
2745
2746 dpcm_fe_dai_cleanup(fe_substream);
2747
b7898396 2748 snd_soc_dpcm_mutex_unlock(fe);
30fca26f
KM
2749 return ret;
2750}
2751
45c0a188 2752static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
01d7584c 2753{
2679a5b2 2754 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
01d7584c
LG
2755 struct snd_soc_dapm_widget_list *list;
2756 int ret;
2757 int stream = fe_substream->stream;
2758
b7898396 2759 snd_soc_dpcm_mutex_lock(fe);
01d7584c 2760
8f70e515 2761 ret = dpcm_path_get(fe, stream, &list);
d479f00b 2762 if (ret < 0)
cae06eb9 2763 goto open_end;
01d7584c
LG
2764
2765 /* calculate valid and active FE <-> BE dpcms */
40b1f89a 2766 dpcm_add_paths(fe, stream, &list);
01d7584c 2767
59a4d9a9
CR
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
01d7584c 2779 ret = dpcm_fe_dai_startup(fe_substream);
265694b6
KM
2780 if (ret < 0)
2781 dpcm_fe_dai_cleanup(fe_substream);
01d7584c
LG
2782
2783 dpcm_clear_pending_state(fe, stream);
59a4d9a9 2784put_path:
01d7584c 2785 dpcm_path_put(&list);
cae06eb9 2786open_end:
b7898396 2787 snd_soc_dpcm_mutex_unlock(fe);
01d7584c
LG
2788 return ret;
2789}
2790
7fc6bebd
KM
2791static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2792 int *playback, int *capture)
ddee627c 2793{
cfcb31c4 2794 struct snd_soc_dai_link *dai_link = rtd->dai_link;
19bdcc7a 2795 struct snd_soc_dai *cpu_dai;
a6ff8572 2796 struct snd_soc_dai *codec_dai;
fd69dfe6 2797 struct snd_soc_dai_link_ch_map *ch_maps;
a6ff8572
KM
2798 struct snd_soc_dai *dummy_dai = snd_soc_find_dai(&snd_soc_dummy_dlc);
2799 int cpu_capture;
2800 int cpu_playback;
c3e9b6d6
KM
2801 int has_playback = 0;
2802 int has_capture = 0;
2e5894d7 2803 int i;
ddee627c 2804
be61cd42
KM
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");
b73287f0 2808
a6ff8572
KM
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);
940a1f43 2812
a6ff8572
KM
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);
a342031c 2820
45cc50d1 2821 /*
a6ff8572
KM
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.
45cc50d1 2831 */
a6ff8572
KM
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;
01d7584c
LG
2841 }
2842
e1f653ce 2843 if (dai_link->playback_only)
c3e9b6d6 2844 has_capture = 0;
d6bead02 2845
e1f653ce 2846 if (dai_link->capture_only)
c3e9b6d6 2847 has_playback = 0;
d6bead02 2848
be61cd42
KM
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);
092830cf 2852
c3e9b6d6
KM
2853 *playback = has_playback;
2854 *capture = has_capture;
2855
7fc6bebd
KM
2856 return 0;
2857}
2858
2b39123b
KM
2859static int soc_create_pcm(struct snd_pcm **pcm,
2860 struct snd_soc_pcm_runtime *rtd,
8b12da9a 2861 int playback, int capture)
7fc6bebd 2862{
7fc6bebd 2863 char new_name[64];
2b39123b 2864 int ret;
7fc6bebd 2865
01d7584c 2866 /* create the PCM */
7ddc7f91 2867 if (rtd->dai_link->c2c_params) {
a342031c
JB
2868 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2869 rtd->dai_link->stream_name);
2870
8b12da9a 2871 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id,
2b39123b 2872 playback, capture, pcm);
a342031c 2873 } else if (rtd->dai_link->no_pcm) {
01d7584c
LG
2874 snprintf(new_name, sizeof(new_name), "(%s)",
2875 rtd->dai_link->stream_name);
2876
8b12da9a 2877 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id,
2b39123b 2878 playback, capture, pcm);
01d7584c
LG
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",
2e5894d7 2885 rtd->dai_link->stream_name,
8b12da9a 2886 soc_codec_dai_name(rtd), rtd->id);
01d7584c 2887
8b12da9a 2888 ret = snd_pcm_new(rtd->card->snd_card, new_name, rtd->id, playback,
2b39123b 2889 capture, pcm);
01d7584c 2890 }
be61cd42
KM
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
8b12da9a 2895 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n", rtd->id, new_name);
ddee627c 2896
2b39123b
KM
2897 return 0;
2898}
2899
2900/* create a new pcm */
8b12da9a 2901int soc_new_pcm(struct snd_soc_pcm_runtime *rtd)
2b39123b
KM
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
8b12da9a 2912 ret = soc_create_pcm(&pcm, rtd, playback, capture);
2b39123b
KM
2913 if (ret < 0)
2914 return ret;
2915
ddee627c 2916 /* DAPM dai link stream work */
10d5d8cb
KM
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 */
7ddc7f91 2923 if (!rtd->dai_link->c2c_params)
83f94a2e 2924 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
ddee627c
LG
2925
2926 rtd->pcm = pcm;
e04e7b8c 2927 pcm->nonatomic = rtd->dai_link->nonatomic;
ddee627c 2928 pcm->private_data = rtd;
4d45d944 2929 pcm->no_device_suspend = true;
01d7584c 2930
7ddc7f91 2931 if (rtd->dai_link->no_pcm || rtd->dai_link->c2c_params) {
01d7584c
LG
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
613fb500 2958 for_each_rtd_components(rtd, i, component) {
2b544dd7 2959 const struct snd_soc_component_driver *drv = component->driver;
b8135864 2960
3b1c952c
TI
2961 if (drv->ioctl)
2962 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
1e5ddb6b
TI
2963 if (drv->sync_stop)
2964 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
66201cac
TI
2965 if (drv->copy)
2966 rtd->ops.copy = snd_soc_pcm_component_copy;
e9067bb5 2967 if (drv->page)
9c712e4f 2968 rtd->ops.page = snd_soc_pcm_component_page;
e9067bb5 2969 if (drv->mmap)
205875e1 2970 rtd->ops.mmap = snd_soc_pcm_component_mmap;
8bdfc045
SW
2971 if (drv->ack)
2972 rtd->ops.ack = snd_soc_pcm_component_ack;
ddee627c
LG
2973 }
2974
2975 if (playback)
01d7584c 2976 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
ddee627c
LG
2977
2978 if (capture)
01d7584c 2979 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
ddee627c 2980
b2b2afbb 2981 ret = snd_soc_pcm_component_new(rtd);
60adbd8f 2982 if (ret < 0)
7484291e 2983 return ret;
01d7584c 2984out:
1d5cd525 2985 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
6fb8944c 2986 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
ddee627c
LG
2987 return ret;
2988}
01d7584c 2989
01d7584c
LG
2990/* get the substream for this BE */
2991struct 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}
2996EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);