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 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
38 * @clk_id: DAI specific clock ID
39 * @freq: new clock frequency in Hz
40 * @dir: new clock direction - input/output.
42 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
44 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
45 unsigned int freq, int dir)
49 if (dai->driver->ops &&
50 dai->driver->ops->set_sysclk)
51 ret = dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
53 ret = snd_soc_component_set_sysclk(dai->component, clk_id, 0,
56 return soc_dai_ret(dai, ret);
58 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
61 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
63 * @div_id: DAI specific clock divider ID
64 * @div: new clock divisor.
66 * Configures the clock dividers. This is used to derive the best DAI bit and
67 * frame clocks from the system or master clock. It's best to set the DAI bit
68 * and frame clocks as low as possible to save system power.
70 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
75 if (dai->driver->ops &&
76 dai->driver->ops->set_clkdiv)
77 ret = dai->driver->ops->set_clkdiv(dai, div_id, div);
79 return soc_dai_ret(dai, ret);
81 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
84 * snd_soc_dai_set_pll - configure DAI PLL.
86 * @pll_id: DAI specific PLL ID
87 * @source: DAI specific source for the PLL
88 * @freq_in: PLL input clock frequency in Hz
89 * @freq_out: requested PLL output clock frequency in Hz
91 * Configures and enables PLL to generate output clock based on input clock.
93 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
94 unsigned int freq_in, unsigned int freq_out)
98 if (dai->driver->ops &&
99 dai->driver->ops->set_pll)
100 ret = dai->driver->ops->set_pll(dai, pll_id, source,
103 ret = snd_soc_component_set_pll(dai->component, pll_id, source,
106 return soc_dai_ret(dai, ret);
108 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
111 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
113 * @ratio: Ratio of BCLK to Sample rate.
115 * Configures the DAI for a preset BCLK to sample rate ratio.
117 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
121 if (dai->driver->ops &&
122 dai->driver->ops->set_bclk_ratio)
123 ret = dai->driver->ops->set_bclk_ratio(dai, ratio);
125 return soc_dai_ret(dai, ret);
127 EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
130 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
132 * @fmt: SND_SOC_DAIFMT_* format value.
134 * Configures the DAI hardware format and clocking.
136 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
140 if (dai->driver->ops &&
141 dai->driver->ops->set_fmt)
142 ret = dai->driver->ops->set_fmt(dai, fmt);
144 return soc_dai_ret(dai, ret);
146 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
149 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
150 * @slots: Number of slots in use.
151 * @tx_mask: bitmask representing active TX slots.
152 * @rx_mask: bitmask representing active RX slots.
154 * Generates the TDM tx and rx slot default masks for DAI.
156 static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
157 unsigned int *tx_mask,
158 unsigned int *rx_mask)
160 if (*tx_mask || *rx_mask)
166 *tx_mask = (1 << slots) - 1;
167 *rx_mask = (1 << slots) - 1;
173 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
174 * @dai: The DAI to configure
175 * @tx_mask: bitmask representing active TX slots.
176 * @rx_mask: bitmask representing active RX slots.
177 * @slots: Number of slots in use.
178 * @slot_width: Width in bits for each slot.
180 * This function configures the specified DAI for TDM operation. @slot contains
181 * the total number of slots of the TDM stream and @slot_with the width of each
182 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
183 * active slots of the TDM stream for the specified DAI, i.e. which slots the
184 * DAI should write to or read from. If a bit is set the corresponding slot is
185 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
186 * the first slot, bit 1 to the second slot and so on. The first active slot
187 * maps to the first channel of the DAI, the second active slot to the second
190 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
191 * @rx_mask and @slot_width will be ignored.
193 * Returns 0 on success, a negative error code otherwise.
195 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
196 unsigned int tx_mask, unsigned int rx_mask,
197 int slots, int slot_width)
201 if (dai->driver->ops &&
202 dai->driver->ops->xlate_tdm_slot_mask)
203 dai->driver->ops->xlate_tdm_slot_mask(slots,
206 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
208 dai->tx_mask = tx_mask;
209 dai->rx_mask = rx_mask;
211 if (dai->driver->ops &&
212 dai->driver->ops->set_tdm_slot)
213 ret = dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
215 return soc_dai_ret(dai, ret);
217 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
220 * snd_soc_dai_set_channel_map - configure DAI audio channel map
222 * @tx_num: how many TX channels
223 * @tx_slot: pointer to an array which imply the TX slot number channel
225 * @rx_num: how many RX channels
226 * @rx_slot: pointer to an array which imply the RX slot number channel
229 * configure the relationship between channel number and TDM slot number.
231 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
232 unsigned int tx_num, unsigned int *tx_slot,
233 unsigned int rx_num, unsigned int *rx_slot)
237 if (dai->driver->ops &&
238 dai->driver->ops->set_channel_map)
239 ret = dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
241 return soc_dai_ret(dai, ret);
243 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
246 * snd_soc_dai_get_channel_map - Get DAI audio channel map
248 * @tx_num: how many TX channels
249 * @tx_slot: pointer to an array which imply the TX slot number channel
251 * @rx_num: how many RX channels
252 * @rx_slot: pointer to an array which imply the RX slot number channel
255 int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
256 unsigned int *tx_num, unsigned int *tx_slot,
257 unsigned int *rx_num, unsigned int *rx_slot)
261 if (dai->driver->ops &&
262 dai->driver->ops->get_channel_map)
263 ret = dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
265 return soc_dai_ret(dai, ret);
267 EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
270 * snd_soc_dai_set_tristate - configure DAI system or master clock.
272 * @tristate: tristate enable
274 * Tristates the DAI so that others can use it.
276 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
280 if (dai->driver->ops &&
281 dai->driver->ops->set_tristate)
282 ret = dai->driver->ops->set_tristate(dai, tristate);
284 return soc_dai_ret(dai, ret);
286 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
289 * snd_soc_dai_digital_mute - configure DAI system or master clock.
292 * @direction: stream to mute
296 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
301 if (dai->driver->ops &&
302 dai->driver->ops->mute_stream)
303 ret = dai->driver->ops->mute_stream(dai, mute, direction);
304 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
306 dai->driver->ops->digital_mute)
307 ret = dai->driver->ops->digital_mute(dai, mute);
309 return soc_dai_ret(dai, ret);
311 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
313 int snd_soc_dai_hw_params(struct snd_soc_dai *dai,
314 struct snd_pcm_substream *substream,
315 struct snd_pcm_hw_params *params)
317 struct snd_soc_pcm_runtime *rtd = substream->private_data;
320 /* perform any topology hw_params fixups before DAI */
321 ret = snd_soc_link_be_hw_params_fixup(rtd, params);
325 if (dai->driver->ops &&
326 dai->driver->ops->hw_params)
327 ret = dai->driver->ops->hw_params(substream, params, dai);
329 return soc_dai_ret(dai, ret);
332 void snd_soc_dai_hw_free(struct snd_soc_dai *dai,
333 struct snd_pcm_substream *substream)
335 if (dai->driver->ops &&
336 dai->driver->ops->hw_free)
337 dai->driver->ops->hw_free(substream, dai);
340 int snd_soc_dai_startup(struct snd_soc_dai *dai,
341 struct snd_pcm_substream *substream)
345 if (dai->driver->ops &&
346 dai->driver->ops->startup)
347 ret = dai->driver->ops->startup(substream, dai);
349 return soc_dai_ret(dai, ret);
352 void snd_soc_dai_shutdown(struct snd_soc_dai *dai,
353 struct snd_pcm_substream *substream)
355 if (dai->driver->ops &&
356 dai->driver->ops->shutdown)
357 dai->driver->ops->shutdown(substream, dai);
360 snd_pcm_sframes_t snd_soc_dai_delay(struct snd_soc_dai *dai,
361 struct snd_pcm_substream *substream)
365 if (dai->driver->ops &&
366 dai->driver->ops->delay)
367 delay = dai->driver->ops->delay(substream, dai);
372 int snd_soc_dai_compress_new(struct snd_soc_dai *dai,
373 struct snd_soc_pcm_runtime *rtd, int num)
376 if (dai->driver->compress_new)
377 ret = dai->driver->compress_new(rtd, num);
378 return soc_dai_ret(dai, ret);
382 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
384 * Returns true if the DAI supports the indicated stream type.
386 bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int dir)
388 struct snd_soc_pcm_stream *stream = snd_soc_dai_get_pcm_stream(dai, dir);
390 /* If the codec specifies any channels at all, it supports the stream */
391 return stream->channels_min;
394 void snd_soc_dai_action(struct snd_soc_dai *dai,
395 int stream, int action)
397 /* see snd_soc_dai_stream_active() */
398 dai->stream_active[stream] += action;
400 /* see snd_soc_component_active() */
401 dai->component->active += action;
403 EXPORT_SYMBOL_GPL(snd_soc_dai_action);
405 int snd_soc_dai_active(struct snd_soc_dai *dai)
410 for_each_pcm_streams(stream)
411 active += dai->stream_active[stream];
415 EXPORT_SYMBOL_GPL(snd_soc_dai_active);
417 int snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime *rtd, int order)
419 struct snd_soc_dai *dai;
422 for_each_rtd_dais(rtd, i, dai) {
423 if (dai->driver->probe_order != order)
426 if (dai->driver->probe) {
427 int ret = dai->driver->probe(dai);
430 return soc_dai_ret(dai, ret);
439 int snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime *rtd, int order)
441 struct snd_soc_dai *dai;
444 for_each_rtd_dais(rtd, i, dai) {
445 if (dai->driver->remove_order != order)
449 dai->driver->remove) {
450 r = dai->driver->remove(dai);
452 ret = r; /* use last error */
461 int snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime *rtd)
463 struct snd_soc_dai *dai;
466 for_each_rtd_dais(rtd, i, dai) {
467 if (dai->driver->pcm_new) {
468 ret = dai->driver->pcm_new(rtd, dai);
470 return soc_dai_ret(dai, ret);
477 int snd_soc_pcm_dai_prepare(struct snd_pcm_substream *substream)
479 struct snd_soc_pcm_runtime *rtd = substream->private_data;
480 struct snd_soc_dai *dai;
483 for_each_rtd_dais(rtd, i, dai) {
484 if (dai->driver->ops &&
485 dai->driver->ops->prepare) {
486 ret = dai->driver->ops->prepare(substream, dai);
488 return soc_dai_ret(dai, ret);
495 int snd_soc_pcm_dai_trigger(struct snd_pcm_substream *substream,
498 struct snd_soc_pcm_runtime *rtd = substream->private_data;
499 struct snd_soc_dai *dai;
502 for_each_rtd_dais(rtd, i, dai) {
503 if (dai->driver->ops &&
504 dai->driver->ops->trigger) {
505 ret = dai->driver->ops->trigger(substream, cmd, dai);
507 return soc_dai_ret(dai, ret);
514 int snd_soc_pcm_dai_bespoke_trigger(struct snd_pcm_substream *substream,
517 struct snd_soc_pcm_runtime *rtd = substream->private_data;
518 struct snd_soc_dai *dai;
521 for_each_rtd_dais(rtd, i, dai) {
522 if (dai->driver->ops &&
523 dai->driver->ops->bespoke_trigger) {
524 ret = dai->driver->ops->bespoke_trigger(substream,
527 return soc_dai_ret(dai, ret);
534 int snd_soc_dai_compr_startup(struct snd_soc_dai *dai,
535 struct snd_compr_stream *cstream)
539 if (dai->driver->cops &&
540 dai->driver->cops->startup)
541 ret = dai->driver->cops->startup(cstream, dai);
543 return soc_dai_ret(dai, ret);
545 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_startup);
547 void snd_soc_dai_compr_shutdown(struct snd_soc_dai *dai,
548 struct snd_compr_stream *cstream)
550 if (dai->driver->cops &&
551 dai->driver->cops->shutdown)
552 dai->driver->cops->shutdown(cstream, dai);
554 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_shutdown);
556 int snd_soc_dai_compr_trigger(struct snd_soc_dai *dai,
557 struct snd_compr_stream *cstream, int cmd)
561 if (dai->driver->cops &&
562 dai->driver->cops->trigger)
563 ret = dai->driver->cops->trigger(cstream, cmd, dai);
565 return soc_dai_ret(dai, ret);
567 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_trigger);
569 int snd_soc_dai_compr_set_params(struct snd_soc_dai *dai,
570 struct snd_compr_stream *cstream,
571 struct snd_compr_params *params)
575 if (dai->driver->cops &&
576 dai->driver->cops->set_params)
577 ret = dai->driver->cops->set_params(cstream, params, dai);
579 return soc_dai_ret(dai, ret);
581 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_params);
583 int snd_soc_dai_compr_get_params(struct snd_soc_dai *dai,
584 struct snd_compr_stream *cstream,
585 struct snd_codec *params)
589 if (dai->driver->cops &&
590 dai->driver->cops->get_params)
591 ret = dai->driver->cops->get_params(cstream, params, dai);
593 return soc_dai_ret(dai, ret);
595 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_params);
597 int snd_soc_dai_compr_ack(struct snd_soc_dai *dai,
598 struct snd_compr_stream *cstream,
603 if (dai->driver->cops &&
604 dai->driver->cops->ack)
605 ret = dai->driver->cops->ack(cstream, bytes, dai);
607 return soc_dai_ret(dai, ret);
609 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_ack);
611 int snd_soc_dai_compr_pointer(struct snd_soc_dai *dai,
612 struct snd_compr_stream *cstream,
613 struct snd_compr_tstamp *tstamp)
617 if (dai->driver->cops &&
618 dai->driver->cops->pointer)
619 ret = dai->driver->cops->pointer(cstream, tstamp, dai);
621 return soc_dai_ret(dai, ret);
623 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_pointer);
625 int snd_soc_dai_compr_set_metadata(struct snd_soc_dai *dai,
626 struct snd_compr_stream *cstream,
627 struct snd_compr_metadata *metadata)
631 if (dai->driver->cops &&
632 dai->driver->cops->set_metadata)
633 ret = dai->driver->cops->set_metadata(cstream, metadata, dai);
635 return soc_dai_ret(dai, ret);
637 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_metadata);
639 int snd_soc_dai_compr_get_metadata(struct snd_soc_dai *dai,
640 struct snd_compr_stream *cstream,
641 struct snd_compr_metadata *metadata)
645 if (dai->driver->cops &&
646 dai->driver->cops->get_metadata)
647 ret = dai->driver->cops->get_metadata(cstream, metadata, dai);
649 return soc_dai_ret(dai, ret);
651 EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_metadata);