1 // SPDX-License-Identifier: GPL-2.0
5 // Copyright (C) 2019 Renesas Electronics Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
10 #include <sound/soc-dai.h>
11 #include <sound/soc-link.h>
13 #define soc_dai_ret(dai, ret) _soc_dai_ret(dai, __func__, ret)
14 static inline int _soc_dai_ret(struct snd_soc_dai *dai,
15 const char *func, int ret)
17 /* Positive, Zero values are not errors */
21 /* Negative values might be errors */
28 "ASoC: error at %s on %s: %d\n",
29 func, dai->name, ret);
36 * We might want to check substream by using list.
37 * In such case, we can update these macros.
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)
44 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
46 * @clk_id: DAI specific clock ID
47 * @freq: new clock frequency in Hz
48 * @dir: new clock direction - input/output.
50 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
52 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
53 unsigned int freq, int dir)
57 if (dai->driver->ops &&
58 dai->driver->ops->set_sysclk)
59 ret = dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
61 ret = snd_soc_component_set_sysclk(dai->component, clk_id, 0,
64 return soc_dai_ret(dai, ret);
66 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
69 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
71 * @div_id: DAI specific clock divider ID
72 * @div: new clock divisor.
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.
78 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
83 if (dai->driver->ops &&
84 dai->driver->ops->set_clkdiv)
85 ret = dai->driver->ops->set_clkdiv(dai, div_id, div);
87 return soc_dai_ret(dai, ret);
89 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
92 * snd_soc_dai_set_pll - configure DAI PLL.
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
99 * Configures and enables PLL to generate output clock based on input clock.
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)
106 if (dai->driver->ops &&
107 dai->driver->ops->set_pll)
108 ret = dai->driver->ops->set_pll(dai, pll_id, source,
111 ret = snd_soc_component_set_pll(dai->component, pll_id, source,
114 return soc_dai_ret(dai, ret);
116 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
119 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
121 * @ratio: Ratio of BCLK to Sample rate.
123 * Configures the DAI for a preset BCLK to sample rate ratio.
125 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
129 if (dai->driver->ops &&
130 dai->driver->ops->set_bclk_ratio)
131 ret = dai->driver->ops->set_bclk_ratio(dai, ratio);
133 return soc_dai_ret(dai, ret);
135 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
137 int snd_soc_dai_get_fmt_max_priority(struct snd_soc_pcm_runtime *rtd)
139 struct snd_soc_dai *dai;
143 * return max num if *ALL* DAIs have .auto_selectable_formats
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);
157 * snd_soc_dai_get_fmt - get supported audio format.
159 * @priority: priority level of supported audio format.
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().
169 u64 snd_soc_dai_get_fmt(struct snd_soc_dai *dai, int priority)
171 const struct snd_soc_dai_ops *ops = dai->driver->ops;
173 int i, max = 0, until = priority;
176 * Collect auto_selectable_formats until priority
179 * auto_selectable_formats[] = { A, B, C };
180 * (A, B, C = SND_SOC_POSSIBLE_DAIFMT_xxx)
183 * priority = 2 : A | B
184 * priority = 3 : A | B | C
185 * priority = 4 : A | B | C
189 max = ops->num_auto_selectable_formats;
194 for (i = 0; i < until; i++)
195 fmt |= ops->auto_selectable_formats[i];
201 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
203 * @fmt: SND_SOC_DAIFMT_* format value.
205 * Configures the DAI hardware format and clocking.
207 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
211 if (dai->driver->ops &&
212 dai->driver->ops->set_fmt)
213 ret = dai->driver->ops->set_fmt(dai, fmt);
215 return soc_dai_ret(dai, ret);
217 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
220 * snd_soc_xlate_tdm_slot_mask - generate tx/rx slot mask.
221 * @slots: Number of slots in use.
222 * @tx_mask: bitmask representing active TX slots.
223 * @rx_mask: bitmask representing active RX slots.
225 * Generates the TDM tx and rx slot default masks for DAI.
227 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
228 unsigned int *tx_mask,
229 unsigned int *rx_mask)
231 if (*tx_mask || *rx_mask)
237 *tx_mask = (1 << slots) - 1;
238 *rx_mask = (1 << slots) - 1;
244 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
245 * @dai: The DAI to configure
246 * @tx_mask: bitmask representing active TX slots.
247 * @rx_mask: bitmask representing active RX slots.
248 * @slots: Number of slots in use.
249 * @slot_width: Width in bits for each slot.
251 * This function configures the specified DAI for TDM operation. @slot contains
252 * the total number of slots of the TDM stream and @slot_with the width of each
253 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
254 * active slots of the TDM stream for the specified DAI, i.e. which slots the
255 * DAI should write to or read from. If a bit is set the corresponding slot is
256 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
257 * the first slot, bit 1 to the second slot and so on. The first active slot
258 * maps to the first channel of the DAI, the second active slot to the second
261 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
262 * @rx_mask and @slot_width will be ignored.
264 * Returns 0 on success, a negative error code otherwise.
266 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
267 unsigned int tx_mask, unsigned int rx_mask,
268 int slots, int slot_width)
272 if (dai->driver->ops &&
273 dai->driver->ops->xlate_tdm_slot_mask)
274 dai->driver->ops->xlate_tdm_slot_mask(slots,
277 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
279 dai->tx_mask = tx_mask;
280 dai->rx_mask = rx_mask;
282 if (dai->driver->ops &&
283 dai->driver->ops->set_tdm_slot)
284 ret = dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
286 return soc_dai_ret(dai, ret);
288 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
291 * snd_soc_dai_set_channel_map - configure DAI audio channel map
293 * @tx_num: how many TX channels
294 * @tx_slot: pointer to an array which imply the TX slot number channel
296 * @rx_num: how many RX channels
297 * @rx_slot: pointer to an array which imply the RX slot number channel
300 * configure the relationship between channel number and TDM slot number.
302 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
303 unsigned int tx_num, unsigned int *tx_slot,
304 unsigned int rx_num, unsigned int *rx_slot)
308 if (dai->driver->ops &&
309 dai->driver->ops->set_channel_map)
310 ret = dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
312 return soc_dai_ret(dai, ret);
314 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
317 * snd_soc_dai_get_channel_map - Get DAI audio channel map
319 * @tx_num: how many TX channels
320 * @tx_slot: pointer to an array which imply the TX slot number channel
322 * @rx_num: how many RX channels
323 * @rx_slot: pointer to an array which imply the RX slot number channel
326 int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
327 unsigned int *tx_num, unsigned int *tx_slot,
328 unsigned int *rx_num, unsigned int *rx_slot)
332 if (dai->driver->ops &&
333 dai->driver->ops->get_channel_map)
334 ret = dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
336 return soc_dai_ret(dai, ret);
338 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
341 * snd_soc_dai_set_tristate - configure DAI system or master clock.
343 * @tristate: tristate enable
345 * Tristates the DAI so that others can use it.
347 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
351 if (dai->driver->ops &&
352 dai->driver->ops->set_tristate)
353 ret = dai->driver->ops->set_tristate(dai, tristate);
355 return soc_dai_ret(dai, ret);
357 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
360 * snd_soc_dai_digital_mute - configure DAI system or master clock.
363 * @direction: stream to mute
367 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
373 * ignore if direction was CAPTURE
374 * and it had .no_capture_mute flag
376 if (dai->driver->ops &&
377 dai->driver->ops->mute_stream &&
378 (direction == SNDRV_PCM_STREAM_PLAYBACK ||
379 !dai->driver->ops->no_capture_mute))
380 ret = dai->driver->ops->mute_stream(dai, mute, direction);
382 return soc_dai_ret(dai, ret);
384 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
386 int snd_soc_dai_hw_params(struct snd_soc_dai *dai,
387 struct snd_pcm_substream *substream,
388 struct snd_pcm_hw_params *params)
390 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
393 if (dai->driver->ops &&
394 dai->driver->ops->hw_params) {
395 /* perform any topology hw_params fixups before DAI */
396 ret = snd_soc_link_be_hw_params_fixup(rtd, params);
400 ret = dai->driver->ops->hw_params(substream, params, dai);
403 /* mark substream if succeeded */
405 soc_dai_mark_push(dai, substream, hw_params);
407 return soc_dai_ret(dai, ret);
410 void snd_soc_dai_hw_free(struct snd_soc_dai *dai,
411 struct snd_pcm_substream *substream,
414 if (rollback && !soc_dai_mark_match(dai, substream, hw_params))
417 if (dai->driver->ops &&
418 dai->driver->ops->hw_free)
419 dai->driver->ops->hw_free(substream, dai);
421 /* remove marked substream */
422 soc_dai_mark_pop(dai, substream, hw_params);
425 int snd_soc_dai_startup(struct snd_soc_dai *dai,
426 struct snd_pcm_substream *substream)
430 if (dai->driver->ops &&
431 dai->driver->ops->startup)
432 ret = dai->driver->ops->startup(substream, dai);
434 /* mark substream if succeeded */
436 soc_dai_mark_push(dai, substream, startup);
438 return soc_dai_ret(dai, ret);
441 void snd_soc_dai_shutdown(struct snd_soc_dai *dai,
442 struct snd_pcm_substream *substream,
445 if (rollback && !soc_dai_mark_match(dai, substream, startup))
448 if (dai->driver->ops &&
449 dai->driver->ops->shutdown)
450 dai->driver->ops->shutdown(substream, dai);
452 /* remove marked substream */
453 soc_dai_mark_pop(dai, substream, startup);
456 snd_pcm_sframes_t snd_soc_dai_delay(struct snd_soc_dai *dai,
457 struct snd_pcm_substream *substream)
461 if (dai->driver->ops &&
462 dai->driver->ops->delay)
463 delay = dai->driver->ops->delay(substream, dai);
468 int snd_soc_dai_compress_new(struct snd_soc_dai *dai,
469 struct snd_soc_pcm_runtime *rtd, int num)
472 if (dai->driver->compress_new)
473 ret = dai->driver->compress_new(rtd, num);
474 return soc_dai_ret(dai, ret);
478 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
480 * Returns true if the DAI supports the indicated stream type.
482 bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int dir)
484 struct snd_soc_pcm_stream *stream = snd_soc_dai_get_pcm_stream(dai, dir);
486 /* If the codec specifies any channels at all, it supports the stream */
487 return stream->channels_min;
491 * snd_soc_dai_link_set_capabilities() - set dai_link properties based on its DAIs
493 void snd_soc_dai_link_set_capabilities(struct snd_soc_dai_link *dai_link)
495 struct snd_soc_dai_link_component *cpu;
496 struct snd_soc_dai_link_component *codec;
497 struct snd_soc_dai *dai;
498 bool supported[SNDRV_PCM_STREAM_LAST + 1];
500 bool supported_codec;
504 for_each_pcm_streams(direction) {
505 supported_cpu = false;
506 supported_codec = false;
508 for_each_link_cpus(dai_link, i, cpu) {
509 dai = snd_soc_find_dai_with_mutex(cpu);
510 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
511 supported_cpu = true;
515 for_each_link_codecs(dai_link, i, codec) {
516 dai = snd_soc_find_dai_with_mutex(codec);
517 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
518 supported_codec = true;
522 supported[direction] = supported_cpu && supported_codec;
525 dai_link->dpcm_playback = supported[SNDRV_PCM_STREAM_PLAYBACK];
526 dai_link->dpcm_capture = supported[SNDRV_PCM_STREAM_CAPTURE];
528 EXPORT_SYMBOL_GPL(snd_soc_dai_link_set_capabilities);
530 void snd_soc_dai_action(struct snd_soc_dai *dai,
531 int stream, int action)
533 /* see snd_soc_dai_stream_active() */
534 dai->stream_active[stream] += action;
536 /* see snd_soc_component_active() */
537 dai->component->active += action;
539 EXPORT_SYMBOL_GPL(snd_soc_dai_action);
541 int snd_soc_dai_active(struct snd_soc_dai *dai)
546 for_each_pcm_streams(stream)
547 active += dai->stream_active[stream];
551 EXPORT_SYMBOL_GPL(snd_soc_dai_active);
553 int snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime *rtd, int order)
555 struct snd_soc_dai *dai;
558 for_each_rtd_dais(rtd, i, dai) {
559 if (dai->driver->probe_order != order)
562 if (dai->driver->probe) {
563 int ret = dai->driver->probe(dai);
566 return soc_dai_ret(dai, ret);
575 int snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime *rtd, int order)
577 struct snd_soc_dai *dai;
580 for_each_rtd_dais(rtd, i, dai) {
581 if (dai->driver->remove_order != order)
585 dai->driver->remove) {
586 r = dai->driver->remove(dai);
588 ret = r; /* use last error */
597 int snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime *rtd)
599 struct snd_soc_dai *dai;
602 for_each_rtd_dais(rtd, i, dai) {
603 if (dai->driver->pcm_new) {
604 ret = dai->driver->pcm_new(rtd, dai);
606 return soc_dai_ret(dai, ret);
613 int snd_soc_pcm_dai_prepare(struct snd_pcm_substream *substream)
615 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
616 struct snd_soc_dai *dai;
619 for_each_rtd_dais(rtd, i, dai) {
620 if (dai->driver->ops &&
621 dai->driver->ops->prepare) {
622 ret = dai->driver->ops->prepare(substream, dai);
624 return soc_dai_ret(dai, ret);
631 static int soc_dai_trigger(struct snd_soc_dai *dai,
632 struct snd_pcm_substream *substream, int cmd)
636 if (dai->driver->ops &&
637 dai->driver->ops->trigger)
638 ret = dai->driver->ops->trigger(substream, cmd, dai);
640 return soc_dai_ret(dai, ret);
643 int snd_soc_pcm_dai_trigger(struct snd_pcm_substream *substream,
644 int cmd, int rollback)
646 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
647 struct snd_soc_dai *dai;
651 case SNDRV_PCM_TRIGGER_START:
652 case SNDRV_PCM_TRIGGER_RESUME:
653 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
654 for_each_rtd_dais(rtd, i, dai) {
655 ret = soc_dai_trigger(dai, substream, cmd);
658 soc_dai_mark_push(dai, substream, trigger);
661 case SNDRV_PCM_TRIGGER_STOP:
662 case SNDRV_PCM_TRIGGER_SUSPEND:
663 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
664 for_each_rtd_dais(rtd, i, dai) {
665 if (rollback && !soc_dai_mark_match(dai, substream, trigger))
668 r = soc_dai_trigger(dai, substream, cmd);
670 ret = r; /* use last ret */
671 soc_dai_mark_pop(dai, substream, trigger);
678 int snd_soc_pcm_dai_bespoke_trigger(struct snd_pcm_substream *substream,
681 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
682 struct snd_soc_dai *dai;
685 for_each_rtd_dais(rtd, i, dai) {
686 if (dai->driver->ops &&
687 dai->driver->ops->bespoke_trigger) {
688 ret = dai->driver->ops->bespoke_trigger(substream,
691 return soc_dai_ret(dai, ret);
698 int snd_soc_dai_compr_startup(struct snd_soc_dai *dai,
699 struct snd_compr_stream *cstream)
703 if (dai->driver->cops &&
704 dai->driver->cops->startup)
705 ret = dai->driver->cops->startup(cstream, dai);
707 /* mark cstream if succeeded */
709 soc_dai_mark_push(dai, cstream, compr_startup);
711 return soc_dai_ret(dai, ret);
713 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_startup);
715 void snd_soc_dai_compr_shutdown(struct snd_soc_dai *dai,
716 struct snd_compr_stream *cstream,
719 if (rollback && !soc_dai_mark_match(dai, cstream, compr_startup))
722 if (dai->driver->cops &&
723 dai->driver->cops->shutdown)
724 dai->driver->cops->shutdown(cstream, dai);
726 /* remove marked cstream */
727 soc_dai_mark_pop(dai, cstream, compr_startup);
729 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_shutdown);
731 int snd_soc_dai_compr_trigger(struct snd_soc_dai *dai,
732 struct snd_compr_stream *cstream, int cmd)
736 if (dai->driver->cops &&
737 dai->driver->cops->trigger)
738 ret = dai->driver->cops->trigger(cstream, cmd, dai);
740 return soc_dai_ret(dai, ret);
742 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_trigger);
744 int snd_soc_dai_compr_set_params(struct snd_soc_dai *dai,
745 struct snd_compr_stream *cstream,
746 struct snd_compr_params *params)
750 if (dai->driver->cops &&
751 dai->driver->cops->set_params)
752 ret = dai->driver->cops->set_params(cstream, params, dai);
754 return soc_dai_ret(dai, ret);
756 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_params);
758 int snd_soc_dai_compr_get_params(struct snd_soc_dai *dai,
759 struct snd_compr_stream *cstream,
760 struct snd_codec *params)
764 if (dai->driver->cops &&
765 dai->driver->cops->get_params)
766 ret = dai->driver->cops->get_params(cstream, params, dai);
768 return soc_dai_ret(dai, ret);
770 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_params);
772 int snd_soc_dai_compr_ack(struct snd_soc_dai *dai,
773 struct snd_compr_stream *cstream,
778 if (dai->driver->cops &&
779 dai->driver->cops->ack)
780 ret = dai->driver->cops->ack(cstream, bytes, dai);
782 return soc_dai_ret(dai, ret);
784 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_ack);
786 int snd_soc_dai_compr_pointer(struct snd_soc_dai *dai,
787 struct snd_compr_stream *cstream,
788 struct snd_compr_tstamp *tstamp)
792 if (dai->driver->cops &&
793 dai->driver->cops->pointer)
794 ret = dai->driver->cops->pointer(cstream, tstamp, dai);
796 return soc_dai_ret(dai, ret);
798 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_pointer);
800 int snd_soc_dai_compr_set_metadata(struct snd_soc_dai *dai,
801 struct snd_compr_stream *cstream,
802 struct snd_compr_metadata *metadata)
806 if (dai->driver->cops &&
807 dai->driver->cops->set_metadata)
808 ret = dai->driver->cops->set_metadata(cstream, metadata, dai);
810 return soc_dai_ret(dai, ret);
812 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_metadata);
814 int snd_soc_dai_compr_get_metadata(struct snd_soc_dai *dai,
815 struct snd_compr_stream *cstream,
816 struct snd_compr_metadata *metadata)
820 if (dai->driver->cops &&
821 dai->driver->cops->get_metadata)
822 ret = dai->driver->cops->get_metadata(cstream, metadata, dai);
824 return soc_dai_ret(dai, ret);
826 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_metadata);