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