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