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 && dai->driver->ops->set_fmt)
212 ret = dai->driver->ops->set_fmt(dai, fmt);
214 return soc_dai_ret(dai, ret);
216 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
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.
224 * Generates the TDM tx and rx slot default masks for DAI.
226 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
227 unsigned int *tx_mask,
228 unsigned int *rx_mask)
230 if (*tx_mask || *rx_mask)
236 *tx_mask = (1 << slots) - 1;
237 *rx_mask = (1 << slots) - 1;
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.
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
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.
263 * Returns 0 on success, a negative error code otherwise.
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)
271 if (dai->driver->ops &&
272 dai->driver->ops->xlate_tdm_slot_mask)
273 dai->driver->ops->xlate_tdm_slot_mask(slots,
276 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
278 dai->tx_mask = tx_mask;
279 dai->rx_mask = rx_mask;
281 if (dai->driver->ops &&
282 dai->driver->ops->set_tdm_slot)
283 ret = dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
285 return soc_dai_ret(dai, ret);
287 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
290 * snd_soc_dai_set_channel_map - configure DAI audio channel map
292 * @tx_num: how many TX channels
293 * @tx_slot: pointer to an array which imply the TX slot number channel
295 * @rx_num: how many RX channels
296 * @rx_slot: pointer to an array which imply the RX slot number channel
299 * configure the relationship between channel number and TDM slot number.
301 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
302 unsigned int tx_num, unsigned int *tx_slot,
303 unsigned int rx_num, unsigned int *rx_slot)
307 if (dai->driver->ops &&
308 dai->driver->ops->set_channel_map)
309 ret = dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
311 return soc_dai_ret(dai, ret);
313 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
316 * snd_soc_dai_get_channel_map - Get DAI audio channel map
318 * @tx_num: how many TX channels
319 * @tx_slot: pointer to an array which imply the TX slot number channel
321 * @rx_num: how many RX channels
322 * @rx_slot: pointer to an array which imply the RX slot number channel
325 int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
326 unsigned int *tx_num, unsigned int *tx_slot,
327 unsigned int *rx_num, unsigned int *rx_slot)
331 if (dai->driver->ops &&
332 dai->driver->ops->get_channel_map)
333 ret = dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
335 return soc_dai_ret(dai, ret);
337 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
340 * snd_soc_dai_set_tristate - configure DAI system or master clock.
342 * @tristate: tristate enable
344 * Tristates the DAI so that others can use it.
346 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
350 if (dai->driver->ops &&
351 dai->driver->ops->set_tristate)
352 ret = dai->driver->ops->set_tristate(dai, tristate);
354 return soc_dai_ret(dai, ret);
356 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
359 * snd_soc_dai_digital_mute - configure DAI system or master clock.
362 * @direction: stream to mute
366 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
372 * ignore if direction was CAPTURE
373 * and it had .no_capture_mute flag
375 if (dai->driver->ops &&
376 dai->driver->ops->mute_stream &&
377 (direction == SNDRV_PCM_STREAM_PLAYBACK ||
378 !dai->driver->ops->no_capture_mute))
379 ret = dai->driver->ops->mute_stream(dai, mute, direction);
381 return soc_dai_ret(dai, ret);
383 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
385 int snd_soc_dai_hw_params(struct snd_soc_dai *dai,
386 struct snd_pcm_substream *substream,
387 struct snd_pcm_hw_params *params)
389 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
392 if (dai->driver->ops &&
393 dai->driver->ops->hw_params) {
394 /* perform any topology hw_params fixups before DAI */
395 ret = snd_soc_link_be_hw_params_fixup(rtd, params);
399 ret = dai->driver->ops->hw_params(substream, params, dai);
402 /* mark substream if succeeded */
404 soc_dai_mark_push(dai, substream, hw_params);
406 return soc_dai_ret(dai, ret);
409 void snd_soc_dai_hw_free(struct snd_soc_dai *dai,
410 struct snd_pcm_substream *substream,
413 if (rollback && !soc_dai_mark_match(dai, substream, hw_params))
416 if (dai->driver->ops &&
417 dai->driver->ops->hw_free)
418 dai->driver->ops->hw_free(substream, dai);
420 /* remove marked substream */
421 soc_dai_mark_pop(dai, substream, hw_params);
424 int snd_soc_dai_startup(struct snd_soc_dai *dai,
425 struct snd_pcm_substream *substream)
429 if (dai->driver->ops &&
430 dai->driver->ops->startup)
431 ret = dai->driver->ops->startup(substream, dai);
433 /* mark substream if succeeded */
435 soc_dai_mark_push(dai, substream, startup);
437 return soc_dai_ret(dai, ret);
440 void snd_soc_dai_shutdown(struct snd_soc_dai *dai,
441 struct snd_pcm_substream *substream,
444 if (rollback && !soc_dai_mark_match(dai, substream, startup))
447 if (dai->driver->ops &&
448 dai->driver->ops->shutdown)
449 dai->driver->ops->shutdown(substream, dai);
451 /* remove marked substream */
452 soc_dai_mark_pop(dai, substream, startup);
455 int snd_soc_dai_compress_new(struct snd_soc_dai *dai,
456 struct snd_soc_pcm_runtime *rtd, int num)
459 if (dai->driver->compress_new)
460 ret = dai->driver->compress_new(rtd, num);
461 return soc_dai_ret(dai, ret);
465 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
467 * Returns true if the DAI supports the indicated stream type.
469 bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int dir)
471 struct snd_soc_pcm_stream *stream = snd_soc_dai_get_pcm_stream(dai, dir);
473 /* If the codec specifies any channels at all, it supports the stream */
474 return stream->channels_min;
478 * snd_soc_dai_link_set_capabilities() - set dai_link properties based on its DAIs
480 void snd_soc_dai_link_set_capabilities(struct snd_soc_dai_link *dai_link)
482 bool supported[SNDRV_PCM_STREAM_LAST + 1];
485 for_each_pcm_streams(direction) {
486 struct snd_soc_dai_link_component *cpu;
487 struct snd_soc_dai_link_component *codec;
488 struct snd_soc_dai *dai;
489 bool supported_cpu = false;
490 bool supported_codec = false;
493 for_each_link_cpus(dai_link, i, cpu) {
494 dai = snd_soc_find_dai_with_mutex(cpu);
495 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
496 supported_cpu = true;
500 for_each_link_codecs(dai_link, i, codec) {
501 dai = snd_soc_find_dai_with_mutex(codec);
502 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
503 supported_codec = true;
507 supported[direction] = supported_cpu && supported_codec;
510 dai_link->dpcm_playback = supported[SNDRV_PCM_STREAM_PLAYBACK];
511 dai_link->dpcm_capture = supported[SNDRV_PCM_STREAM_CAPTURE];
513 EXPORT_SYMBOL_GPL(snd_soc_dai_link_set_capabilities);
515 void snd_soc_dai_action(struct snd_soc_dai *dai,
516 int stream, int action)
518 /* see snd_soc_dai_stream_active() */
519 dai->stream_active[stream] += action;
521 /* see snd_soc_component_active() */
522 dai->component->active += action;
524 EXPORT_SYMBOL_GPL(snd_soc_dai_action);
526 int snd_soc_dai_active(struct snd_soc_dai *dai)
531 for_each_pcm_streams(stream)
532 active += dai->stream_active[stream];
536 EXPORT_SYMBOL_GPL(snd_soc_dai_active);
538 int snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime *rtd, int order)
540 struct snd_soc_dai *dai;
543 for_each_rtd_dais(rtd, i, dai) {
544 if (dai->driver->probe_order != order)
547 if (dai->driver->probe) {
548 int ret = dai->driver->probe(dai);
551 return soc_dai_ret(dai, ret);
560 int snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime *rtd, int order)
562 struct snd_soc_dai *dai;
565 for_each_rtd_dais(rtd, i, dai) {
566 if (dai->driver->remove_order != order)
570 dai->driver->remove) {
571 r = dai->driver->remove(dai);
573 ret = r; /* use last error */
582 int snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime *rtd)
584 struct snd_soc_dai *dai;
587 for_each_rtd_dais(rtd, i, dai) {
588 if (dai->driver->pcm_new) {
589 int ret = dai->driver->pcm_new(rtd, dai);
591 return soc_dai_ret(dai, ret);
598 int snd_soc_pcm_dai_prepare(struct snd_pcm_substream *substream)
600 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
601 struct snd_soc_dai *dai;
604 for_each_rtd_dais(rtd, i, dai) {
605 if (dai->driver->ops &&
606 dai->driver->ops->prepare) {
607 ret = dai->driver->ops->prepare(substream, dai);
609 return soc_dai_ret(dai, ret);
616 static int soc_dai_trigger(struct snd_soc_dai *dai,
617 struct snd_pcm_substream *substream, int cmd)
621 if (dai->driver->ops &&
622 dai->driver->ops->trigger)
623 ret = dai->driver->ops->trigger(substream, cmd, dai);
625 return soc_dai_ret(dai, ret);
628 int snd_soc_pcm_dai_trigger(struct snd_pcm_substream *substream,
629 int cmd, int rollback)
631 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
632 struct snd_soc_dai *dai;
636 case SNDRV_PCM_TRIGGER_START:
637 case SNDRV_PCM_TRIGGER_RESUME:
638 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
639 for_each_rtd_dais(rtd, i, dai) {
640 ret = soc_dai_trigger(dai, substream, cmd);
643 soc_dai_mark_push(dai, substream, trigger);
646 case SNDRV_PCM_TRIGGER_STOP:
647 case SNDRV_PCM_TRIGGER_SUSPEND:
648 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
649 for_each_rtd_dais(rtd, i, dai) {
650 if (rollback && !soc_dai_mark_match(dai, substream, trigger))
653 r = soc_dai_trigger(dai, substream, cmd);
655 ret = r; /* use last ret */
656 soc_dai_mark_pop(dai, substream, trigger);
663 int snd_soc_pcm_dai_bespoke_trigger(struct snd_pcm_substream *substream,
666 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
667 struct snd_soc_dai *dai;
670 for_each_rtd_dais(rtd, i, dai) {
671 if (dai->driver->ops &&
672 dai->driver->ops->bespoke_trigger) {
673 ret = dai->driver->ops->bespoke_trigger(substream,
676 return soc_dai_ret(dai, ret);
683 void snd_soc_pcm_dai_delay(struct snd_pcm_substream *substream,
684 snd_pcm_sframes_t *cpu_delay,
685 snd_pcm_sframes_t *codec_delay)
687 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
688 struct snd_soc_dai *dai;
692 * We're looking for the delay through the full audio path so it needs to
693 * be the maximum of the DAIs doing transmit and the maximum of the DAIs
694 * doing receive (ie, all CPUs and all CODECs) rather than just the maximum
699 for_each_rtd_cpu_dais(rtd, i, dai)
700 if (dai->driver->ops &&
701 dai->driver->ops->delay)
702 *cpu_delay = max(*cpu_delay, dai->driver->ops->delay(substream, dai));
705 for_each_rtd_codec_dais(rtd, i, dai)
706 if (dai->driver->ops &&
707 dai->driver->ops->delay)
708 *codec_delay = max(*codec_delay, dai->driver->ops->delay(substream, dai));
711 int snd_soc_dai_compr_startup(struct snd_soc_dai *dai,
712 struct snd_compr_stream *cstream)
716 if (dai->driver->cops &&
717 dai->driver->cops->startup)
718 ret = dai->driver->cops->startup(cstream, dai);
720 /* mark cstream if succeeded */
722 soc_dai_mark_push(dai, cstream, compr_startup);
724 return soc_dai_ret(dai, ret);
726 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_startup);
728 void snd_soc_dai_compr_shutdown(struct snd_soc_dai *dai,
729 struct snd_compr_stream *cstream,
732 if (rollback && !soc_dai_mark_match(dai, cstream, compr_startup))
735 if (dai->driver->cops &&
736 dai->driver->cops->shutdown)
737 dai->driver->cops->shutdown(cstream, dai);
739 /* remove marked cstream */
740 soc_dai_mark_pop(dai, cstream, compr_startup);
742 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_shutdown);
744 int snd_soc_dai_compr_trigger(struct snd_soc_dai *dai,
745 struct snd_compr_stream *cstream, int cmd)
749 if (dai->driver->cops &&
750 dai->driver->cops->trigger)
751 ret = dai->driver->cops->trigger(cstream, cmd, dai);
753 return soc_dai_ret(dai, ret);
755 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_trigger);
757 int snd_soc_dai_compr_set_params(struct snd_soc_dai *dai,
758 struct snd_compr_stream *cstream,
759 struct snd_compr_params *params)
763 if (dai->driver->cops &&
764 dai->driver->cops->set_params)
765 ret = dai->driver->cops->set_params(cstream, params, dai);
767 return soc_dai_ret(dai, ret);
769 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_params);
771 int snd_soc_dai_compr_get_params(struct snd_soc_dai *dai,
772 struct snd_compr_stream *cstream,
773 struct snd_codec *params)
777 if (dai->driver->cops &&
778 dai->driver->cops->get_params)
779 ret = dai->driver->cops->get_params(cstream, params, dai);
781 return soc_dai_ret(dai, ret);
783 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_params);
785 int snd_soc_dai_compr_ack(struct snd_soc_dai *dai,
786 struct snd_compr_stream *cstream,
791 if (dai->driver->cops &&
792 dai->driver->cops->ack)
793 ret = dai->driver->cops->ack(cstream, bytes, dai);
795 return soc_dai_ret(dai, ret);
797 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_ack);
799 int snd_soc_dai_compr_pointer(struct snd_soc_dai *dai,
800 struct snd_compr_stream *cstream,
801 struct snd_compr_tstamp *tstamp)
805 if (dai->driver->cops &&
806 dai->driver->cops->pointer)
807 ret = dai->driver->cops->pointer(cstream, tstamp, dai);
809 return soc_dai_ret(dai, ret);
811 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_pointer);
813 int snd_soc_dai_compr_set_metadata(struct snd_soc_dai *dai,
814 struct snd_compr_stream *cstream,
815 struct snd_compr_metadata *metadata)
819 if (dai->driver->cops &&
820 dai->driver->cops->set_metadata)
821 ret = dai->driver->cops->set_metadata(cstream, metadata, dai);
823 return soc_dai_ret(dai, ret);
825 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_metadata);
827 int snd_soc_dai_compr_get_metadata(struct snd_soc_dai *dai,
828 struct snd_compr_stream *cstream,
829 struct snd_compr_metadata *metadata)
833 if (dai->driver->cops &&
834 dai->driver->cops->get_metadata)
835 ret = dai->driver->cops->get_metadata(cstream, metadata, dai);
837 return soc_dai_ret(dai, ret);
839 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_metadata);