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