xref: /openbmc/linux/sound/soc/soc-dai.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // soc-dai.c
4 //
5 // Copyright (C) 2019 Renesas Electronics Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 
9 #include <sound/soc.h>
10 #include <sound/soc-dai.h>
11 #include <sound/soc-link.h>
12 
13 #define soc_dai_ret(dai, ret) _soc_dai_ret(dai, __func__, ret)
_soc_dai_ret(struct snd_soc_dai * dai,const char * func,int ret)14 static inline int _soc_dai_ret(struct snd_soc_dai *dai,
15 			       const char *func, int ret)
16 {
17 	/* Positive, Zero values are not errors */
18 	if (ret >= 0)
19 		return ret;
20 
21 	/* Negative values might be errors */
22 	switch (ret) {
23 	case -EPROBE_DEFER:
24 	case -ENOTSUPP:
25 		break;
26 	default:
27 		dev_err(dai->dev,
28 			"ASoC: error at %s on %s: %d\n",
29 			func, dai->name, ret);
30 	}
31 
32 	return ret;
33 }
34 
35 /*
36  * We might want to check substream by using list.
37  * In such case, we can update these macros.
38  */
39 #define soc_dai_mark_push(dai, substream, tgt)	((dai)->mark_##tgt = substream)
40 #define soc_dai_mark_pop(dai, substream, tgt)	((dai)->mark_##tgt = NULL)
41 #define soc_dai_mark_match(dai, substream, tgt)	((dai)->mark_##tgt == substream)
42 
43 /**
44  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
45  * @dai: DAI
46  * @clk_id: DAI specific clock ID
47  * @freq: new clock frequency in Hz
48  * @dir: new clock direction - input/output.
49  *
50  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
51  */
snd_soc_dai_set_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)52 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
53 			   unsigned int freq, int dir)
54 {
55 	int ret;
56 
57 	if (dai->driver->ops &&
58 	    dai->driver->ops->set_sysclk)
59 		ret = dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
60 	else
61 		ret = snd_soc_component_set_sysclk(dai->component, clk_id, 0,
62 						   freq, dir);
63 
64 	return soc_dai_ret(dai, ret);
65 }
66 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
67 
68 /**
69  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
70  * @dai: DAI
71  * @div_id: DAI specific clock divider ID
72  * @div: new clock divisor.
73  *
74  * Configures the clock dividers. This is used to derive the best DAI bit and
75  * frame clocks from the system or master clock. It's best to set the DAI bit
76  * and frame clocks as low as possible to save system power.
77  */
snd_soc_dai_set_clkdiv(struct snd_soc_dai * dai,int div_id,int div)78 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
79 			   int div_id, int div)
80 {
81 	int ret = -EINVAL;
82 
83 	if (dai->driver->ops &&
84 	    dai->driver->ops->set_clkdiv)
85 		ret = dai->driver->ops->set_clkdiv(dai, div_id, div);
86 
87 	return soc_dai_ret(dai, ret);
88 }
89 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
90 
91 /**
92  * snd_soc_dai_set_pll - configure DAI PLL.
93  * @dai: DAI
94  * @pll_id: DAI specific PLL ID
95  * @source: DAI specific source for the PLL
96  * @freq_in: PLL input clock frequency in Hz
97  * @freq_out: requested PLL output clock frequency in Hz
98  *
99  * Configures and enables PLL to generate output clock based on input clock.
100  */
snd_soc_dai_set_pll(struct snd_soc_dai * dai,int pll_id,int source,unsigned int freq_in,unsigned int freq_out)101 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
102 			unsigned int freq_in, unsigned int freq_out)
103 {
104 	int ret;
105 
106 	if (dai->driver->ops &&
107 	    dai->driver->ops->set_pll)
108 		ret = dai->driver->ops->set_pll(dai, pll_id, source,
109 						freq_in, freq_out);
110 	else
111 		ret = snd_soc_component_set_pll(dai->component, pll_id, source,
112 						freq_in, freq_out);
113 
114 	return soc_dai_ret(dai, ret);
115 }
116 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
117 
118 /**
119  * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
120  * @dai: DAI
121  * @ratio: Ratio of BCLK to Sample rate.
122  *
123  * Configures the DAI for a preset BCLK to sample rate ratio.
124  */
snd_soc_dai_set_bclk_ratio(struct snd_soc_dai * dai,unsigned int ratio)125 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
126 {
127 	int ret = -ENOTSUPP;
128 
129 	if (dai->driver->ops &&
130 	    dai->driver->ops->set_bclk_ratio)
131 		ret = dai->driver->ops->set_bclk_ratio(dai, ratio);
132 
133 	return soc_dai_ret(dai, ret);
134 }
135 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
136 
snd_soc_dai_get_fmt_max_priority(struct snd_soc_pcm_runtime * rtd)137 int snd_soc_dai_get_fmt_max_priority(struct snd_soc_pcm_runtime *rtd)
138 {
139 	struct snd_soc_dai *dai;
140 	int i, max = 0;
141 
142 	/*
143 	 * return max num if *ALL* DAIs have .auto_selectable_formats
144 	 */
145 	for_each_rtd_dais(rtd, i, dai) {
146 		if (dai->driver->ops &&
147 		    dai->driver->ops->num_auto_selectable_formats)
148 			max = max(max, dai->driver->ops->num_auto_selectable_formats);
149 		else
150 			return 0;
151 	}
152 
153 	return max;
154 }
155 
156 /**
157  * snd_soc_dai_get_fmt - get supported audio format.
158  * @dai: DAI
159  * @priority: priority level of supported audio format.
160  *
161  * This should return only formats implemented with high
162  * quality by the DAI so that the core can configure a
163  * format which will work well with other devices.
164  * For example devices which don't support both edges of the
165  * LRCLK signal in I2S style formats should only list DSP
166  * modes.  This will mean that sometimes fewer formats
167  * are reported here than are supported by set_fmt().
168  */
snd_soc_dai_get_fmt(struct snd_soc_dai * dai,int priority)169 u64 snd_soc_dai_get_fmt(struct snd_soc_dai *dai, int priority)
170 {
171 	const struct snd_soc_dai_ops *ops = dai->driver->ops;
172 	u64 fmt = 0;
173 	int i, max = 0, until = priority;
174 
175 	/*
176 	 * Collect auto_selectable_formats until priority
177 	 *
178 	 * ex)
179 	 *	auto_selectable_formats[] = { A, B, C };
180 	 *	(A, B, C = SND_SOC_POSSIBLE_DAIFMT_xxx)
181 	 *
182 	 * priority = 1 :	A
183 	 * priority = 2 :	A | B
184 	 * priority = 3 :	A | B | C
185 	 * priority = 4 :	A | B | C
186 	 * ...
187 	 */
188 	if (ops)
189 		max = ops->num_auto_selectable_formats;
190 
191 	if (max < until)
192 		until = max;
193 
194 	for (i = 0; i < until; i++)
195 		fmt |= ops->auto_selectable_formats[i];
196 
197 	return fmt;
198 }
199 
200 /**
201  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
202  * @dai: DAI
203  * @fmt: SND_SOC_DAIFMT_* format value.
204  *
205  * Configures the DAI hardware format and clocking.
206  */
snd_soc_dai_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)207 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
208 {
209 	int ret = -ENOTSUPP;
210 
211 	if (dai->driver->ops && dai->driver->ops->set_fmt)
212 		ret = dai->driver->ops->set_fmt(dai, fmt);
213 
214 	return soc_dai_ret(dai, ret);
215 }
216 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
217 
218 /**
219  * snd_soc_xlate_tdm_slot_mask - generate tx/rx slot mask.
220  * @slots: Number of slots in use.
221  * @tx_mask: bitmask representing active TX slots.
222  * @rx_mask: bitmask representing active RX slots.
223  *
224  * Generates the TDM tx and rx slot default masks for DAI.
225  */
snd_soc_xlate_tdm_slot_mask(unsigned int slots,unsigned int * tx_mask,unsigned int * rx_mask)226 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
227 				       unsigned int *tx_mask,
228 				       unsigned int *rx_mask)
229 {
230 	if (*tx_mask || *rx_mask)
231 		return 0;
232 
233 	if (!slots)
234 		return -EINVAL;
235 
236 	*tx_mask = (1 << slots) - 1;
237 	*rx_mask = (1 << slots) - 1;
238 
239 	return 0;
240 }
241 
242 /**
243  * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
244  * @dai: The DAI to configure
245  * @tx_mask: bitmask representing active TX slots.
246  * @rx_mask: bitmask representing active RX slots.
247  * @slots: Number of slots in use.
248  * @slot_width: Width in bits for each slot.
249  *
250  * This function configures the specified DAI for TDM operation. @slot contains
251  * the total number of slots of the TDM stream and @slot_with the width of each
252  * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
253  * active slots of the TDM stream for the specified DAI, i.e. which slots the
254  * DAI should write to or read from. If a bit is set the corresponding slot is
255  * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
256  * the first slot, bit 1 to the second slot and so on. The first active slot
257  * maps to the first channel of the DAI, the second active slot to the second
258  * channel and so on.
259  *
260  * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
261  * @rx_mask and @slot_width will be ignored.
262  *
263  * Returns 0 on success, a negative error code otherwise.
264  */
snd_soc_dai_set_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)265 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
266 			     unsigned int tx_mask, unsigned int rx_mask,
267 			     int slots, int slot_width)
268 {
269 	int ret = -ENOTSUPP;
270 	int stream;
271 	unsigned int *tdm_mask[] = {
272 		&tx_mask,
273 		&rx_mask,
274 	};
275 
276 	if (dai->driver->ops &&
277 	    dai->driver->ops->xlate_tdm_slot_mask)
278 		ret = dai->driver->ops->xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
279 	else
280 		ret = snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
281 	if (ret)
282 		goto err;
283 
284 	for_each_pcm_streams(stream)
285 		snd_soc_dai_tdm_mask_set(dai, stream, *tdm_mask[stream]);
286 
287 	if (dai->driver->ops &&
288 	    dai->driver->ops->set_tdm_slot)
289 		ret = dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
290 						      slots, slot_width);
291 err:
292 	return soc_dai_ret(dai, ret);
293 }
294 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
295 
296 /**
297  * snd_soc_dai_set_channel_map - configure DAI audio channel map
298  * @dai: DAI
299  * @tx_num: how many TX channels
300  * @tx_slot: pointer to an array which imply the TX slot number channel
301  *           0~num-1 uses
302  * @rx_num: how many RX channels
303  * @rx_slot: pointer to an array which imply the RX slot number channel
304  *           0~num-1 uses
305  *
306  * configure the relationship between channel number and TDM slot number.
307  */
snd_soc_dai_set_channel_map(struct snd_soc_dai * dai,unsigned int tx_num,unsigned int * tx_slot,unsigned int rx_num,unsigned int * rx_slot)308 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
309 				unsigned int tx_num, unsigned int *tx_slot,
310 				unsigned int rx_num, unsigned int *rx_slot)
311 {
312 	int ret = -ENOTSUPP;
313 
314 	if (dai->driver->ops &&
315 	    dai->driver->ops->set_channel_map)
316 		ret = dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
317 							rx_num, rx_slot);
318 	return soc_dai_ret(dai, ret);
319 }
320 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
321 
322 /**
323  * snd_soc_dai_get_channel_map - Get DAI audio channel map
324  * @dai: DAI
325  * @tx_num: how many TX channels
326  * @tx_slot: pointer to an array which imply the TX slot number channel
327  *           0~num-1 uses
328  * @rx_num: how many RX channels
329  * @rx_slot: pointer to an array which imply the RX slot number channel
330  *           0~num-1 uses
331  */
snd_soc_dai_get_channel_map(struct snd_soc_dai * dai,unsigned int * tx_num,unsigned int * tx_slot,unsigned int * rx_num,unsigned int * rx_slot)332 int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
333 				unsigned int *tx_num, unsigned int *tx_slot,
334 				unsigned int *rx_num, unsigned int *rx_slot)
335 {
336 	int ret = -ENOTSUPP;
337 
338 	if (dai->driver->ops &&
339 	    dai->driver->ops->get_channel_map)
340 		ret = dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
341 							rx_num, rx_slot);
342 	return soc_dai_ret(dai, ret);
343 }
344 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
345 
346 /**
347  * snd_soc_dai_set_tristate - configure DAI system or master clock.
348  * @dai: DAI
349  * @tristate: tristate enable
350  *
351  * Tristates the DAI so that others can use it.
352  */
snd_soc_dai_set_tristate(struct snd_soc_dai * dai,int tristate)353 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
354 {
355 	int ret = -EINVAL;
356 
357 	if (dai->driver->ops &&
358 	    dai->driver->ops->set_tristate)
359 		ret = dai->driver->ops->set_tristate(dai, tristate);
360 
361 	return soc_dai_ret(dai, ret);
362 }
363 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
364 
365 /**
366  * snd_soc_dai_digital_mute - configure DAI system or master clock.
367  * @dai: DAI
368  * @mute: mute enable
369  * @direction: stream to mute
370  *
371  * Mutes the DAI DAC.
372  */
snd_soc_dai_digital_mute(struct snd_soc_dai * dai,int mute,int direction)373 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
374 			     int direction)
375 {
376 	int ret = -ENOTSUPP;
377 
378 	/*
379 	 * ignore if direction was CAPTURE
380 	 * and it had .no_capture_mute flag
381 	 */
382 	if (dai->driver->ops &&
383 	    dai->driver->ops->mute_stream &&
384 	    (direction == SNDRV_PCM_STREAM_PLAYBACK ||
385 	     !dai->driver->ops->no_capture_mute))
386 		ret = dai->driver->ops->mute_stream(dai, mute, direction);
387 
388 	return soc_dai_ret(dai, ret);
389 }
390 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
391 
snd_soc_dai_hw_params(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)392 int snd_soc_dai_hw_params(struct snd_soc_dai *dai,
393 			  struct snd_pcm_substream *substream,
394 			  struct snd_pcm_hw_params *params)
395 {
396 	int ret = 0;
397 
398 	if (dai->driver->ops &&
399 	    dai->driver->ops->hw_params)
400 		ret = dai->driver->ops->hw_params(substream, params, dai);
401 
402 	/* mark substream if succeeded */
403 	if (ret == 0)
404 		soc_dai_mark_push(dai, substream, hw_params);
405 
406 	return soc_dai_ret(dai, ret);
407 }
408 
snd_soc_dai_hw_free(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,int rollback)409 void snd_soc_dai_hw_free(struct snd_soc_dai *dai,
410 			 struct snd_pcm_substream *substream,
411 			 int rollback)
412 {
413 	if (rollback && !soc_dai_mark_match(dai, substream, hw_params))
414 		return;
415 
416 	if (dai->driver->ops &&
417 	    dai->driver->ops->hw_free)
418 		dai->driver->ops->hw_free(substream, dai);
419 
420 	/* remove marked substream */
421 	soc_dai_mark_pop(dai, substream, hw_params);
422 }
423 
snd_soc_dai_startup(struct snd_soc_dai * dai,struct snd_pcm_substream * substream)424 int snd_soc_dai_startup(struct snd_soc_dai *dai,
425 			struct snd_pcm_substream *substream)
426 {
427 	int ret = 0;
428 
429 	if (!snd_soc_dai_stream_valid(dai, substream->stream))
430 		return 0;
431 
432 	if (dai->driver->ops &&
433 	    dai->driver->ops->startup)
434 		ret = dai->driver->ops->startup(substream, dai);
435 
436 	/* mark substream if succeeded */
437 	if (ret == 0)
438 		soc_dai_mark_push(dai, substream, startup);
439 
440 	return soc_dai_ret(dai, ret);
441 }
442 
snd_soc_dai_shutdown(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,int rollback)443 void snd_soc_dai_shutdown(struct snd_soc_dai *dai,
444 			  struct snd_pcm_substream *substream,
445 			  int rollback)
446 {
447 	if (!snd_soc_dai_stream_valid(dai, substream->stream))
448 		return;
449 
450 	if (rollback && !soc_dai_mark_match(dai, substream, startup))
451 		return;
452 
453 	if (dai->driver->ops &&
454 	    dai->driver->ops->shutdown)
455 		dai->driver->ops->shutdown(substream, dai);
456 
457 	/* remove marked substream */
458 	soc_dai_mark_pop(dai, substream, startup);
459 }
460 
snd_soc_dai_compress_new(struct snd_soc_dai * dai,struct snd_soc_pcm_runtime * rtd,int num)461 int snd_soc_dai_compress_new(struct snd_soc_dai *dai,
462 			     struct snd_soc_pcm_runtime *rtd, int num)
463 {
464 	int ret = -ENOTSUPP;
465 	if (dai->driver->ops &&
466 	    dai->driver->ops->compress_new)
467 		ret = dai->driver->ops->compress_new(rtd, num);
468 	return soc_dai_ret(dai, ret);
469 }
470 
471 /*
472  * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
473  *
474  * Returns true if the DAI supports the indicated stream type.
475  */
snd_soc_dai_stream_valid(struct snd_soc_dai * dai,int dir)476 bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int dir)
477 {
478 	struct snd_soc_pcm_stream *stream = snd_soc_dai_get_pcm_stream(dai, dir);
479 
480 	/* If the codec specifies any channels at all, it supports the stream */
481 	return stream->channels_min;
482 }
483 
484 /*
485  * snd_soc_dai_link_set_capabilities() - set dai_link properties based on its DAIs
486  */
snd_soc_dai_link_set_capabilities(struct snd_soc_dai_link * dai_link)487 void snd_soc_dai_link_set_capabilities(struct snd_soc_dai_link *dai_link)
488 {
489 	bool supported[SNDRV_PCM_STREAM_LAST + 1];
490 	int direction;
491 
492 	for_each_pcm_streams(direction) {
493 		struct snd_soc_dai_link_component *cpu;
494 		struct snd_soc_dai_link_component *codec;
495 		struct snd_soc_dai *dai;
496 		bool supported_cpu = false;
497 		bool supported_codec = false;
498 		int i;
499 
500 		for_each_link_cpus(dai_link, i, cpu) {
501 			dai = snd_soc_find_dai_with_mutex(cpu);
502 			if (dai && snd_soc_dai_stream_valid(dai, direction)) {
503 				supported_cpu = true;
504 				break;
505 			}
506 		}
507 		for_each_link_codecs(dai_link, i, codec) {
508 			dai = snd_soc_find_dai_with_mutex(codec);
509 			if (dai && snd_soc_dai_stream_valid(dai, direction)) {
510 				supported_codec = true;
511 				break;
512 			}
513 		}
514 		supported[direction] = supported_cpu && supported_codec;
515 	}
516 
517 	dai_link->dpcm_playback = supported[SNDRV_PCM_STREAM_PLAYBACK];
518 	dai_link->dpcm_capture  = supported[SNDRV_PCM_STREAM_CAPTURE];
519 }
520 EXPORT_SYMBOL_GPL(snd_soc_dai_link_set_capabilities);
521 
snd_soc_dai_action(struct snd_soc_dai * dai,int stream,int action)522 void snd_soc_dai_action(struct snd_soc_dai *dai,
523 			int stream, int action)
524 {
525 	/* see snd_soc_dai_stream_active() */
526 	dai->stream[stream].active	+= action;
527 
528 	/* see snd_soc_component_active() */
529 	dai->component->active		+= action;
530 }
531 EXPORT_SYMBOL_GPL(snd_soc_dai_action);
532 
snd_soc_dai_active(struct snd_soc_dai * dai)533 int snd_soc_dai_active(struct snd_soc_dai *dai)
534 {
535 	int stream, active;
536 
537 	active = 0;
538 	for_each_pcm_streams(stream)
539 		active += dai->stream[stream].active;
540 
541 	return active;
542 }
543 EXPORT_SYMBOL_GPL(snd_soc_dai_active);
544 
snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime * rtd,int order)545 int snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime *rtd, int order)
546 {
547 	struct snd_soc_dai *dai;
548 	int i;
549 
550 	for_each_rtd_dais(rtd, i, dai) {
551 		if (dai->probed)
552 			continue;
553 
554 		if (dai->driver->ops) {
555 			if (dai->driver->ops->probe_order != order)
556 				continue;
557 
558 			if (dai->driver->ops->probe) {
559 				int ret = dai->driver->ops->probe(dai);
560 
561 				if (ret < 0)
562 					return soc_dai_ret(dai, ret);
563 			}
564 		}
565 		dai->probed = 1;
566 	}
567 
568 	return 0;
569 }
570 
snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime * rtd,int order)571 int snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime *rtd, int order)
572 {
573 	struct snd_soc_dai *dai;
574 	int i, r, ret = 0;
575 
576 	for_each_rtd_dais(rtd, i, dai) {
577 		if (!dai->probed)
578 			continue;
579 
580 		if (dai->driver->ops) {
581 			if (dai->driver->ops->remove_order != order)
582 				continue;
583 
584 			if (dai->driver->ops->remove) {
585 				r = dai->driver->ops->remove(dai);
586 				if (r < 0)
587 					ret = r; /* use last error */
588 			}
589 		}
590 		dai->probed = 0;
591 	}
592 
593 	return ret;
594 }
595 
snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime * rtd)596 int snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime *rtd)
597 {
598 	struct snd_soc_dai *dai;
599 	int i;
600 
601 	for_each_rtd_dais(rtd, i, dai) {
602 		if (dai->driver->ops &&
603 		    dai->driver->ops->pcm_new) {
604 			int ret = dai->driver->ops->pcm_new(rtd, dai);
605 			if (ret < 0)
606 				return soc_dai_ret(dai, ret);
607 		}
608 	}
609 
610 	return 0;
611 }
612 
snd_soc_pcm_dai_prepare(struct snd_pcm_substream * substream)613 int snd_soc_pcm_dai_prepare(struct snd_pcm_substream *substream)
614 {
615 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
616 	struct snd_soc_dai *dai;
617 	int i, ret;
618 
619 	for_each_rtd_dais(rtd, i, dai) {
620 		if (!snd_soc_dai_stream_valid(dai, substream->stream))
621 			continue;
622 		if (dai->driver->ops &&
623 		    dai->driver->ops->prepare) {
624 			ret = dai->driver->ops->prepare(substream, dai);
625 			if (ret < 0)
626 				return soc_dai_ret(dai, ret);
627 		}
628 	}
629 
630 	return 0;
631 }
632 
soc_dai_trigger(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,int cmd)633 static int soc_dai_trigger(struct snd_soc_dai *dai,
634 			   struct snd_pcm_substream *substream, int cmd)
635 {
636 	int ret = 0;
637 
638 	if (!snd_soc_dai_stream_valid(dai, substream->stream))
639 		return 0;
640 
641 	if (dai->driver->ops &&
642 	    dai->driver->ops->trigger)
643 		ret = dai->driver->ops->trigger(substream, cmd, dai);
644 
645 	return soc_dai_ret(dai, ret);
646 }
647 
snd_soc_pcm_dai_trigger(struct snd_pcm_substream * substream,int cmd,int rollback)648 int snd_soc_pcm_dai_trigger(struct snd_pcm_substream *substream,
649 			    int cmd, int rollback)
650 {
651 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
652 	struct snd_soc_dai *dai;
653 	int i, r, ret = 0;
654 
655 	switch (cmd) {
656 	case SNDRV_PCM_TRIGGER_START:
657 	case SNDRV_PCM_TRIGGER_RESUME:
658 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
659 		for_each_rtd_dais(rtd, i, dai) {
660 			ret = soc_dai_trigger(dai, substream, cmd);
661 			if (ret < 0)
662 				break;
663 
664 			if (dai->driver->ops && dai->driver->ops->mute_unmute_on_trigger)
665 				snd_soc_dai_digital_mute(dai, 0, substream->stream);
666 
667 			soc_dai_mark_push(dai, substream, trigger);
668 		}
669 		break;
670 	case SNDRV_PCM_TRIGGER_STOP:
671 	case SNDRV_PCM_TRIGGER_SUSPEND:
672 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
673 		for_each_rtd_dais(rtd, i, dai) {
674 			if (rollback && !soc_dai_mark_match(dai, substream, trigger))
675 				continue;
676 
677 			if (dai->driver->ops && dai->driver->ops->mute_unmute_on_trigger)
678 				snd_soc_dai_digital_mute(dai, 1, substream->stream);
679 
680 			r = soc_dai_trigger(dai, substream, cmd);
681 			if (r < 0)
682 				ret = r; /* use last ret */
683 			soc_dai_mark_pop(dai, substream, trigger);
684 		}
685 	}
686 
687 	return ret;
688 }
689 
snd_soc_pcm_dai_bespoke_trigger(struct snd_pcm_substream * substream,int cmd)690 int snd_soc_pcm_dai_bespoke_trigger(struct snd_pcm_substream *substream,
691 				    int cmd)
692 {
693 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
694 	struct snd_soc_dai *dai;
695 	int i, ret;
696 
697 	for_each_rtd_dais(rtd, i, dai) {
698 		if (dai->driver->ops &&
699 		    dai->driver->ops->bespoke_trigger) {
700 			ret = dai->driver->ops->bespoke_trigger(substream,
701 								cmd, dai);
702 			if (ret < 0)
703 				return soc_dai_ret(dai, ret);
704 		}
705 	}
706 
707 	return 0;
708 }
709 
snd_soc_pcm_dai_delay(struct snd_pcm_substream * substream,snd_pcm_sframes_t * cpu_delay,snd_pcm_sframes_t * codec_delay)710 void snd_soc_pcm_dai_delay(struct snd_pcm_substream *substream,
711 			   snd_pcm_sframes_t *cpu_delay,
712 			   snd_pcm_sframes_t *codec_delay)
713 {
714 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
715 	struct snd_soc_dai *dai;
716 	int i;
717 
718 	/*
719 	 * We're looking for the delay through the full audio path so it needs to
720 	 * be the maximum of the DAIs doing transmit and the maximum of the DAIs
721 	 * doing receive (ie, all CPUs and all CODECs) rather than just the maximum
722 	 * of all DAIs.
723 	 */
724 
725 	/* for CPU */
726 	for_each_rtd_cpu_dais(rtd, i, dai)
727 		if (dai->driver->ops &&
728 		    dai->driver->ops->delay)
729 			*cpu_delay = max(*cpu_delay, dai->driver->ops->delay(substream, dai));
730 
731 	/* for Codec */
732 	for_each_rtd_codec_dais(rtd, i, dai)
733 		if (dai->driver->ops &&
734 		    dai->driver->ops->delay)
735 			*codec_delay = max(*codec_delay, dai->driver->ops->delay(substream, dai));
736 }
737 
snd_soc_dai_compr_startup(struct snd_soc_dai * dai,struct snd_compr_stream * cstream)738 int snd_soc_dai_compr_startup(struct snd_soc_dai *dai,
739 			      struct snd_compr_stream *cstream)
740 {
741 	int ret = 0;
742 
743 	if (dai->driver->cops &&
744 	    dai->driver->cops->startup)
745 		ret = dai->driver->cops->startup(cstream, dai);
746 
747 	/* mark cstream if succeeded */
748 	if (ret == 0)
749 		soc_dai_mark_push(dai, cstream, compr_startup);
750 
751 	return soc_dai_ret(dai, ret);
752 }
753 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_startup);
754 
snd_soc_dai_compr_shutdown(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,int rollback)755 void snd_soc_dai_compr_shutdown(struct snd_soc_dai *dai,
756 				struct snd_compr_stream *cstream,
757 				int rollback)
758 {
759 	if (rollback && !soc_dai_mark_match(dai, cstream, compr_startup))
760 		return;
761 
762 	if (dai->driver->cops &&
763 	    dai->driver->cops->shutdown)
764 		dai->driver->cops->shutdown(cstream, dai);
765 
766 	/* remove marked cstream */
767 	soc_dai_mark_pop(dai, cstream, compr_startup);
768 }
769 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_shutdown);
770 
snd_soc_dai_compr_trigger(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,int cmd)771 int snd_soc_dai_compr_trigger(struct snd_soc_dai *dai,
772 			      struct snd_compr_stream *cstream, int cmd)
773 {
774 	int ret = 0;
775 
776 	if (dai->driver->cops &&
777 	    dai->driver->cops->trigger)
778 		ret = dai->driver->cops->trigger(cstream, cmd, dai);
779 
780 	return soc_dai_ret(dai, ret);
781 }
782 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_trigger);
783 
snd_soc_dai_compr_set_params(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_params * params)784 int snd_soc_dai_compr_set_params(struct snd_soc_dai *dai,
785 				 struct snd_compr_stream *cstream,
786 				 struct snd_compr_params *params)
787 {
788 	int ret = 0;
789 
790 	if (dai->driver->cops &&
791 	    dai->driver->cops->set_params)
792 		ret = dai->driver->cops->set_params(cstream, params, dai);
793 
794 	return soc_dai_ret(dai, ret);
795 }
796 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_params);
797 
snd_soc_dai_compr_get_params(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_codec * params)798 int snd_soc_dai_compr_get_params(struct snd_soc_dai *dai,
799 				 struct snd_compr_stream *cstream,
800 				 struct snd_codec *params)
801 {
802 	int ret = 0;
803 
804 	if (dai->driver->cops &&
805 	    dai->driver->cops->get_params)
806 		ret = dai->driver->cops->get_params(cstream, params, dai);
807 
808 	return soc_dai_ret(dai, ret);
809 }
810 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_params);
811 
snd_soc_dai_compr_ack(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,size_t bytes)812 int snd_soc_dai_compr_ack(struct snd_soc_dai *dai,
813 			  struct snd_compr_stream *cstream,
814 			  size_t bytes)
815 {
816 	int ret = 0;
817 
818 	if (dai->driver->cops &&
819 	    dai->driver->cops->ack)
820 		ret = dai->driver->cops->ack(cstream, bytes, dai);
821 
822 	return soc_dai_ret(dai, ret);
823 }
824 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_ack);
825 
snd_soc_dai_compr_pointer(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_tstamp * tstamp)826 int snd_soc_dai_compr_pointer(struct snd_soc_dai *dai,
827 			      struct snd_compr_stream *cstream,
828 			      struct snd_compr_tstamp *tstamp)
829 {
830 	int ret = 0;
831 
832 	if (dai->driver->cops &&
833 	    dai->driver->cops->pointer)
834 		ret = dai->driver->cops->pointer(cstream, tstamp, dai);
835 
836 	return soc_dai_ret(dai, ret);
837 }
838 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_pointer);
839 
snd_soc_dai_compr_set_metadata(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)840 int snd_soc_dai_compr_set_metadata(struct snd_soc_dai *dai,
841 				   struct snd_compr_stream *cstream,
842 				   struct snd_compr_metadata *metadata)
843 {
844 	int ret = 0;
845 
846 	if (dai->driver->cops &&
847 	    dai->driver->cops->set_metadata)
848 		ret = dai->driver->cops->set_metadata(cstream, metadata, dai);
849 
850 	return soc_dai_ret(dai, ret);
851 }
852 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_metadata);
853 
snd_soc_dai_compr_get_metadata(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)854 int snd_soc_dai_compr_get_metadata(struct snd_soc_dai *dai,
855 				   struct snd_compr_stream *cstream,
856 				   struct snd_compr_metadata *metadata)
857 {
858 	int ret = 0;
859 
860 	if (dai->driver->cops &&
861 	    dai->driver->cops->get_metadata)
862 		ret = dai->driver->cops->get_metadata(cstream, metadata, dai);
863 
864 	return soc_dai_ret(dai, ret);
865 }
866 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_metadata);
867