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