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