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