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