1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // soc-dai.c
4 //
5 // Copyright (C) 2019 Renesas Electronics Corp.
6 // Kuninori Morimoto <[email protected]>
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(const struct snd_soc_dai * dai,const char * func,int ret)14 static inline int _soc_dai_ret(const 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, 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 (SND_SOC_CLOCK_IN or SND_SOC_CLOCK_OUT)
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(const struct snd_soc_pcm_runtime * rtd)137 int snd_soc_dai_get_fmt_max_priority(const 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(const struct snd_soc_dai * dai,int priority)169 u64 snd_soc_dai_get_fmt(const 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 		dai->driver->ops->xlate_tdm_slot_mask(slots,
279 						      &tx_mask, &rx_mask);
280 	else
281 		snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
282 
283 	for_each_pcm_streams(stream)
284 		snd_soc_dai_tdm_mask_set(dai, stream, *tdm_mask[stream]);
285 
286 	if (dai->driver->ops &&
287 	    dai->driver->ops->set_tdm_slot)
288 		ret = dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
289 						      slots, slot_width);
290 	return soc_dai_ret(dai, ret);
291 }
292 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
293 
294 /**
295  * snd_soc_dai_set_channel_map - configure DAI audio channel map
296  * @dai: DAI
297  * @tx_num: how many TX channels
298  * @tx_slot: pointer to an array which imply the TX slot number channel
299  *           0~num-1 uses
300  * @rx_num: how many RX channels
301  * @rx_slot: pointer to an array which imply the RX slot number channel
302  *           0~num-1 uses
303  *
304  * configure the relationship between channel number and TDM slot number.
305  */
snd_soc_dai_set_channel_map(struct snd_soc_dai * dai,unsigned int tx_num,const unsigned int * tx_slot,unsigned int rx_num,const unsigned int * rx_slot)306 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
307 				unsigned int tx_num, const unsigned int *tx_slot,
308 				unsigned int rx_num, const unsigned int *rx_slot)
309 {
310 	int ret = -ENOTSUPP;
311 
312 	if (dai->driver->ops &&
313 	    dai->driver->ops->set_channel_map)
314 		ret = dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
315 							rx_num, rx_slot);
316 	return soc_dai_ret(dai, ret);
317 }
318 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
319 
320 /**
321  * snd_soc_dai_get_channel_map - Get DAI audio channel map
322  * @dai: DAI
323  * @tx_num: how many TX channels
324  * @tx_slot: pointer to an array which imply the TX slot number channel
325  *           0~num-1 uses
326  * @rx_num: how many RX channels
327  * @rx_slot: pointer to an array which imply the RX slot number channel
328  *           0~num-1 uses
329  */
snd_soc_dai_get_channel_map(const struct snd_soc_dai * dai,unsigned int * tx_num,unsigned int * tx_slot,unsigned int * rx_num,unsigned int * rx_slot)330 int snd_soc_dai_get_channel_map(const struct snd_soc_dai *dai,
331 				unsigned int *tx_num, unsigned int *tx_slot,
332 				unsigned int *rx_num, unsigned int *rx_slot)
333 {
334 	int ret = -ENOTSUPP;
335 
336 	if (dai->driver->ops &&
337 	    dai->driver->ops->get_channel_map)
338 		ret = dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
339 							rx_num, rx_slot);
340 	return soc_dai_ret(dai, ret);
341 }
342 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
343 
344 /**
345  * snd_soc_dai_set_tristate - configure DAI system or master clock.
346  * @dai: DAI
347  * @tristate: tristate enable
348  *
349  * Tristates the DAI so that others can use it.
350  */
snd_soc_dai_set_tristate(struct snd_soc_dai * dai,int tristate)351 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
352 {
353 	int ret = -EINVAL;
354 
355 	if (dai->driver->ops &&
356 	    dai->driver->ops->set_tristate)
357 		ret = dai->driver->ops->set_tristate(dai, tristate);
358 
359 	return soc_dai_ret(dai, ret);
360 }
361 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
362 
snd_soc_dai_prepare(struct snd_soc_dai * dai,struct snd_pcm_substream * substream)363 int snd_soc_dai_prepare(struct snd_soc_dai *dai,
364 			struct snd_pcm_substream *substream)
365 {
366 	int ret = 0;
367 
368 	if (!snd_soc_dai_stream_valid(dai, substream->stream))
369 		return 0;
370 
371 	if (dai->driver->ops &&
372 	    dai->driver->ops->prepare)
373 		ret = dai->driver->ops->prepare(substream, dai);
374 
375 	return soc_dai_ret(dai, ret);
376 }
377 EXPORT_SYMBOL_GPL(snd_soc_dai_prepare);
378 
379 /**
380  * snd_soc_dai_digital_mute - configure DAI system or master clock.
381  * @dai: DAI
382  * @mute: mute enable
383  * @direction: stream to mute
384  *
385  * Mutes the DAI DAC.
386  */
snd_soc_dai_digital_mute(struct snd_soc_dai * dai,int mute,int direction)387 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
388 			     int direction)
389 {
390 	int ret = -ENOTSUPP;
391 
392 	/*
393 	 * ignore if direction was CAPTURE
394 	 * and it had .no_capture_mute flag
395 	 */
396 	if (dai->driver->ops &&
397 	    dai->driver->ops->mute_stream &&
398 	    (direction == SNDRV_PCM_STREAM_PLAYBACK ||
399 	     !dai->driver->ops->no_capture_mute))
400 		ret = dai->driver->ops->mute_stream(dai, mute, direction);
401 
402 	return soc_dai_ret(dai, ret);
403 }
404 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
405 
snd_soc_dai_hw_params(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)406 int snd_soc_dai_hw_params(struct snd_soc_dai *dai,
407 			  struct snd_pcm_substream *substream,
408 			  struct snd_pcm_hw_params *params)
409 {
410 	int ret = 0;
411 
412 	if (dai->driver->ops &&
413 	    dai->driver->ops->hw_params)
414 		ret = dai->driver->ops->hw_params(substream, params, dai);
415 
416 	/* mark substream if succeeded */
417 	if (ret == 0)
418 		soc_dai_mark_push(dai, substream, hw_params);
419 
420 	return soc_dai_ret(dai, ret);
421 }
422 
snd_soc_dai_hw_free(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,int rollback)423 void snd_soc_dai_hw_free(struct snd_soc_dai *dai,
424 			 struct snd_pcm_substream *substream,
425 			 int rollback)
426 {
427 	if (rollback && !soc_dai_mark_match(dai, substream, hw_params))
428 		return;
429 
430 	if (dai->driver->ops &&
431 	    dai->driver->ops->hw_free)
432 		dai->driver->ops->hw_free(substream, dai);
433 
434 	/* remove marked substream */
435 	soc_dai_mark_pop(dai, hw_params);
436 }
437 
snd_soc_dai_startup(struct snd_soc_dai * dai,struct snd_pcm_substream * substream)438 int snd_soc_dai_startup(struct snd_soc_dai *dai,
439 			struct snd_pcm_substream *substream)
440 {
441 	int ret = 0;
442 
443 	if (!snd_soc_dai_stream_valid(dai, substream->stream))
444 		return 0;
445 
446 	if (dai->driver->ops &&
447 	    dai->driver->ops->startup)
448 		ret = dai->driver->ops->startup(substream, dai);
449 
450 	/* mark substream if succeeded */
451 	if (ret == 0)
452 		soc_dai_mark_push(dai, substream, startup);
453 
454 	return soc_dai_ret(dai, ret);
455 }
456 
snd_soc_dai_shutdown(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,int rollback)457 void snd_soc_dai_shutdown(struct snd_soc_dai *dai,
458 			  struct snd_pcm_substream *substream,
459 			  int rollback)
460 {
461 	if (!snd_soc_dai_stream_valid(dai, substream->stream))
462 		return;
463 
464 	if (rollback && !soc_dai_mark_match(dai, substream, startup))
465 		return;
466 
467 	if (dai->driver->ops &&
468 	    dai->driver->ops->shutdown)
469 		dai->driver->ops->shutdown(substream, dai);
470 
471 	/* remove marked substream */
472 	soc_dai_mark_pop(dai, startup);
473 }
474 
snd_soc_dai_compress_new(struct snd_soc_dai * dai,struct snd_soc_pcm_runtime * rtd)475 int snd_soc_dai_compress_new(struct snd_soc_dai *dai,
476 			     struct snd_soc_pcm_runtime *rtd)
477 {
478 	int ret = -ENOTSUPP;
479 	if (dai->driver->ops &&
480 	    dai->driver->ops->compress_new)
481 		ret = dai->driver->ops->compress_new(rtd);
482 	return soc_dai_ret(dai, ret);
483 }
484 
485 /*
486  * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
487  *
488  * Returns true if the DAI supports the indicated stream type.
489  */
snd_soc_dai_stream_valid(const struct snd_soc_dai * dai,int dir)490 bool snd_soc_dai_stream_valid(const struct snd_soc_dai *dai, int dir)
491 {
492 	const struct snd_soc_pcm_stream *stream = snd_soc_dai_get_pcm_stream(dai, dir);
493 
494 	/* If the codec specifies any channels at all, it supports the stream */
495 	return stream->channels_min;
496 }
497 
snd_soc_dai_action(struct snd_soc_dai * dai,int stream,int action)498 void snd_soc_dai_action(struct snd_soc_dai *dai,
499 			int stream, int action)
500 {
501 	/* see snd_soc_dai_stream_active() */
502 	dai->stream[stream].active	+= action;
503 
504 	/* see snd_soc_component_active() */
505 	dai->component->active		+= action;
506 }
507 EXPORT_SYMBOL_GPL(snd_soc_dai_action);
508 
snd_soc_dai_active(const struct snd_soc_dai * dai)509 int snd_soc_dai_active(const struct snd_soc_dai *dai)
510 {
511 	int stream, active;
512 
513 	active = 0;
514 	for_each_pcm_streams(stream)
515 		active += dai->stream[stream].active;
516 
517 	return active;
518 }
519 EXPORT_SYMBOL_GPL(snd_soc_dai_active);
520 
snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime * rtd,int order)521 int snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime *rtd, int order)
522 {
523 	struct snd_soc_dai *dai;
524 	int i;
525 
526 	for_each_rtd_dais(rtd, i, dai) {
527 		if (dai->probed)
528 			continue;
529 
530 		if (dai->driver->ops) {
531 			if (dai->driver->ops->probe_order != order)
532 				continue;
533 
534 			if (dai->driver->ops->probe) {
535 				int ret = dai->driver->ops->probe(dai);
536 
537 				if (ret < 0)
538 					return soc_dai_ret(dai, ret);
539 			}
540 		}
541 		dai->probed = 1;
542 	}
543 
544 	return 0;
545 }
546 
snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime * rtd,int order)547 int snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime *rtd, int order)
548 {
549 	struct snd_soc_dai *dai;
550 	int i, r, ret = 0;
551 
552 	for_each_rtd_dais(rtd, i, dai) {
553 		if (!dai->probed)
554 			continue;
555 
556 		if (dai->driver->ops) {
557 			if (dai->driver->ops->remove_order != order)
558 				continue;
559 
560 			if (dai->driver->ops->remove) {
561 				r = dai->driver->ops->remove(dai);
562 				if (r < 0)
563 					ret = r; /* use last error */
564 			}
565 		}
566 		dai->probed = 0;
567 	}
568 
569 	return ret;
570 }
571 
snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime * rtd)572 int snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime *rtd)
573 {
574 	struct snd_soc_dai *dai;
575 	int i;
576 
577 	for_each_rtd_dais(rtd, i, dai) {
578 		if (dai->driver->ops &&
579 		    dai->driver->ops->pcm_new) {
580 			int ret = dai->driver->ops->pcm_new(rtd, dai);
581 			if (ret < 0)
582 				return soc_dai_ret(dai, ret);
583 		}
584 	}
585 
586 	return 0;
587 }
588 
snd_soc_pcm_dai_prepare(struct snd_pcm_substream * substream)589 int snd_soc_pcm_dai_prepare(struct snd_pcm_substream *substream)
590 {
591 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
592 	struct snd_soc_dai *dai;
593 	int i, ret;
594 
595 	for_each_rtd_dais(rtd, i, dai) {
596 		ret = snd_soc_dai_prepare(dai, substream);
597 		if (ret < 0)
598 			return ret;
599 	}
600 
601 	return 0;
602 }
603 
soc_dai_trigger(struct snd_soc_dai * dai,struct snd_pcm_substream * substream,int cmd)604 static int soc_dai_trigger(struct snd_soc_dai *dai,
605 			   struct snd_pcm_substream *substream, int cmd)
606 {
607 	int ret = 0;
608 
609 	if (!snd_soc_dai_stream_valid(dai, substream->stream))
610 		return 0;
611 
612 	if (dai->driver->ops &&
613 	    dai->driver->ops->trigger)
614 		ret = dai->driver->ops->trigger(substream, cmd, dai);
615 
616 	return soc_dai_ret(dai, ret);
617 }
618 
snd_soc_pcm_dai_trigger(struct snd_pcm_substream * substream,int cmd,int rollback)619 int snd_soc_pcm_dai_trigger(struct snd_pcm_substream *substream,
620 			    int cmd, int rollback)
621 {
622 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
623 	struct snd_soc_dai *dai;
624 	int i, r, ret = 0;
625 
626 	switch (cmd) {
627 	case SNDRV_PCM_TRIGGER_START:
628 	case SNDRV_PCM_TRIGGER_RESUME:
629 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
630 		for_each_rtd_dais(rtd, i, dai) {
631 			ret = soc_dai_trigger(dai, substream, cmd);
632 			if (ret < 0)
633 				break;
634 
635 			if (dai->driver->ops && dai->driver->ops->mute_unmute_on_trigger)
636 				snd_soc_dai_digital_mute(dai, 0, substream->stream);
637 
638 			soc_dai_mark_push(dai, substream, trigger);
639 		}
640 		break;
641 	case SNDRV_PCM_TRIGGER_STOP:
642 	case SNDRV_PCM_TRIGGER_SUSPEND:
643 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
644 		for_each_rtd_dais(rtd, i, dai) {
645 			if (rollback && !soc_dai_mark_match(dai, substream, trigger))
646 				continue;
647 
648 			if (dai->driver->ops && dai->driver->ops->mute_unmute_on_trigger)
649 				snd_soc_dai_digital_mute(dai, 1, substream->stream);
650 
651 			r = soc_dai_trigger(dai, substream, cmd);
652 			if (r < 0)
653 				ret = r; /* use last ret */
654 			soc_dai_mark_pop(dai, trigger);
655 		}
656 	}
657 
658 	return ret;
659 }
660 
snd_soc_pcm_dai_delay(struct snd_pcm_substream * substream,snd_pcm_sframes_t * cpu_delay,snd_pcm_sframes_t * codec_delay)661 void snd_soc_pcm_dai_delay(struct snd_pcm_substream *substream,
662 			   snd_pcm_sframes_t *cpu_delay,
663 			   snd_pcm_sframes_t *codec_delay)
664 {
665 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
666 	struct snd_soc_dai *dai;
667 	int i;
668 
669 	/*
670 	 * We're looking for the delay through the full audio path so it needs to
671 	 * be the maximum of the DAIs doing transmit and the maximum of the DAIs
672 	 * doing receive (ie, all CPUs and all CODECs) rather than just the maximum
673 	 * of all DAIs.
674 	 */
675 
676 	/* for CPU */
677 	for_each_rtd_cpu_dais(rtd, i, dai)
678 		if (dai->driver->ops &&
679 		    dai->driver->ops->delay)
680 			*cpu_delay = max(*cpu_delay, dai->driver->ops->delay(substream, dai));
681 
682 	/* for Codec */
683 	for_each_rtd_codec_dais(rtd, i, dai)
684 		if (dai->driver->ops &&
685 		    dai->driver->ops->delay)
686 			*codec_delay = max(*codec_delay, dai->driver->ops->delay(substream, dai));
687 }
688 
snd_soc_dai_compr_startup(struct snd_soc_dai * dai,struct snd_compr_stream * cstream)689 int snd_soc_dai_compr_startup(struct snd_soc_dai *dai,
690 			      struct snd_compr_stream *cstream)
691 {
692 	int ret = 0;
693 
694 	if (dai->driver->cops &&
695 	    dai->driver->cops->startup)
696 		ret = dai->driver->cops->startup(cstream, dai);
697 
698 	/* mark cstream if succeeded */
699 	if (ret == 0)
700 		soc_dai_mark_push(dai, cstream, compr_startup);
701 
702 	return soc_dai_ret(dai, ret);
703 }
704 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_startup);
705 
snd_soc_dai_compr_shutdown(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,int rollback)706 void snd_soc_dai_compr_shutdown(struct snd_soc_dai *dai,
707 				struct snd_compr_stream *cstream,
708 				int rollback)
709 {
710 	if (rollback && !soc_dai_mark_match(dai, cstream, compr_startup))
711 		return;
712 
713 	if (dai->driver->cops &&
714 	    dai->driver->cops->shutdown)
715 		dai->driver->cops->shutdown(cstream, dai);
716 
717 	/* remove marked cstream */
718 	soc_dai_mark_pop(dai, compr_startup);
719 }
720 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_shutdown);
721 
snd_soc_dai_compr_trigger(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,int cmd)722 int snd_soc_dai_compr_trigger(struct snd_soc_dai *dai,
723 			      struct snd_compr_stream *cstream, int cmd)
724 {
725 	int ret = 0;
726 
727 	if (dai->driver->cops &&
728 	    dai->driver->cops->trigger)
729 		ret = dai->driver->cops->trigger(cstream, cmd, dai);
730 
731 	return soc_dai_ret(dai, ret);
732 }
733 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_trigger);
734 
snd_soc_dai_compr_set_params(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_params * params)735 int snd_soc_dai_compr_set_params(struct snd_soc_dai *dai,
736 				 struct snd_compr_stream *cstream,
737 				 struct snd_compr_params *params)
738 {
739 	int ret = 0;
740 
741 	if (dai->driver->cops &&
742 	    dai->driver->cops->set_params)
743 		ret = dai->driver->cops->set_params(cstream, params, dai);
744 
745 	return soc_dai_ret(dai, ret);
746 }
747 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_params);
748 
snd_soc_dai_compr_get_params(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_codec * params)749 int snd_soc_dai_compr_get_params(struct snd_soc_dai *dai,
750 				 struct snd_compr_stream *cstream,
751 				 struct snd_codec *params)
752 {
753 	int ret = 0;
754 
755 	if (dai->driver->cops &&
756 	    dai->driver->cops->get_params)
757 		ret = dai->driver->cops->get_params(cstream, params, dai);
758 
759 	return soc_dai_ret(dai, ret);
760 }
761 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_params);
762 
snd_soc_dai_compr_ack(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,size_t bytes)763 int snd_soc_dai_compr_ack(struct snd_soc_dai *dai,
764 			  struct snd_compr_stream *cstream,
765 			  size_t bytes)
766 {
767 	int ret = 0;
768 
769 	if (dai->driver->cops &&
770 	    dai->driver->cops->ack)
771 		ret = dai->driver->cops->ack(cstream, bytes, dai);
772 
773 	return soc_dai_ret(dai, ret);
774 }
775 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_ack);
776 
snd_soc_dai_compr_pointer(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_tstamp * tstamp)777 int snd_soc_dai_compr_pointer(struct snd_soc_dai *dai,
778 			      struct snd_compr_stream *cstream,
779 			      struct snd_compr_tstamp *tstamp)
780 {
781 	int ret = 0;
782 
783 	if (dai->driver->cops &&
784 	    dai->driver->cops->pointer)
785 		ret = dai->driver->cops->pointer(cstream, tstamp, dai);
786 
787 	return soc_dai_ret(dai, ret);
788 }
789 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_pointer);
790 
snd_soc_dai_compr_set_metadata(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)791 int snd_soc_dai_compr_set_metadata(struct snd_soc_dai *dai,
792 				   struct snd_compr_stream *cstream,
793 				   struct snd_compr_metadata *metadata)
794 {
795 	int ret = 0;
796 
797 	if (dai->driver->cops &&
798 	    dai->driver->cops->set_metadata)
799 		ret = dai->driver->cops->set_metadata(cstream, metadata, dai);
800 
801 	return soc_dai_ret(dai, ret);
802 }
803 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_metadata);
804 
snd_soc_dai_compr_get_metadata(struct snd_soc_dai * dai,struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)805 int snd_soc_dai_compr_get_metadata(struct snd_soc_dai *dai,
806 				   struct snd_compr_stream *cstream,
807 				   struct snd_compr_metadata *metadata)
808 {
809 	int ret = 0;
810 
811 	if (dai->driver->cops &&
812 	    dai->driver->cops->get_metadata)
813 		ret = dai->driver->cops->get_metadata(cstream, metadata, dai);
814 
815 	return soc_dai_ret(dai, ret);
816 }
817 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_metadata);
818