blob: 3db0fcf24385af21e739ab1536de557b1952a669 [file] [log] [blame]
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +09001// 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>
Kuninori Morimoto0cbbf8a2020-05-25 09:57:36 +090011#include <sound/soc-link.h>
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +090012
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +090013#define soc_dai_ret(dai, ret) _soc_dai_ret(dai, __func__, ret)
14static inline int _soc_dai_ret(struct snd_soc_dai *dai,
15 const char *func, int ret)
16{
Pierre-Louis Bossart28ff4372020-05-29 07:36:13 -050017 /* Positive, Zero values are not errors */
18 if (ret >= 0)
19 return ret;
20
21 /* Negative values might be errors */
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +090022 switch (ret) {
23 case -EPROBE_DEFER:
24 case -ENOTSUPP:
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +090025 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
Kuninori Morimoto00a0b462020-09-28 09:00:40 +090035/*
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
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +090043/**
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 */
52int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
53 unsigned int freq, int dir)
54{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +090055 int ret;
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +090056
Kuninori Morimoto479914e2020-04-24 08:14:43 +090057 if (dai->driver->ops &&
58 dai->driver->ops->set_sysclk)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +090059 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);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +090065}
66EXPORT_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 */
78int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
79 int div_id, int div)
80{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +090081 int ret = -EINVAL;
82
Kuninori Morimoto479914e2020-04-24 08:14:43 +090083 if (dai->driver->ops &&
84 dai->driver->ops->set_clkdiv)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +090085 ret = dai->driver->ops->set_clkdiv(dai, div_id, div);
86
87 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +090088}
89EXPORT_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 */
101int 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{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900104 int ret;
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900105
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900106 if (dai->driver->ops &&
107 dai->driver->ops->set_pll)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900108 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);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900115}
116EXPORT_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 */
125int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
126{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900127 int ret = -EINVAL;
128
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900129 if (dai->driver->ops &&
130 dai->driver->ops->set_bclk_ratio)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900131 ret = dai->driver->ops->set_bclk_ratio(dai, ratio);
132
133 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900134}
135EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
136
Kuninori Morimotoba9e82a2021-05-27 11:26:12 +0900137int 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 */
169u64 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
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900200/**
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 */
207int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
208{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900209 int ret = -ENOTSUPP;
210
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900211 if (dai->driver->ops &&
212 dai->driver->ops->set_fmt)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900213 ret = dai->driver->ops->set_fmt(dai, fmt);
214
215 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900216}
217EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
218
219/**
Pierre-Louis Bossart2fb87112021-03-01 11:46:59 -0600220 * snd_soc_xlate_tdm_slot_mask - generate tx/rx slot mask.
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900221 * @slots: Number of slots in use.
222 * @tx_mask: bitmask representing active TX slots.
223 * @rx_mask: bitmask representing active RX slots.
224 *
225 * Generates the TDM tx and rx slot default masks for DAI.
226 */
227static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
228 unsigned int *tx_mask,
229 unsigned int *rx_mask)
230{
231 if (*tx_mask || *rx_mask)
232 return 0;
233
234 if (!slots)
235 return -EINVAL;
236
237 *tx_mask = (1 << slots) - 1;
238 *rx_mask = (1 << slots) - 1;
239
240 return 0;
241}
242
243/**
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.
250 *
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
259 * channel and so on.
260 *
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.
263 *
264 * Returns 0 on success, a negative error code otherwise.
265 */
266int 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)
269{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900270 int ret = -ENOTSUPP;
271
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900272 if (dai->driver->ops &&
273 dai->driver->ops->xlate_tdm_slot_mask)
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900274 dai->driver->ops->xlate_tdm_slot_mask(slots,
275 &tx_mask, &rx_mask);
276 else
277 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
278
279 dai->tx_mask = tx_mask;
280 dai->rx_mask = rx_mask;
281
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900282 if (dai->driver->ops &&
283 dai->driver->ops->set_tdm_slot)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900284 ret = dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900285 slots, slot_width);
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900286 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900287}
288EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
289
290/**
291 * snd_soc_dai_set_channel_map - configure DAI audio channel map
292 * @dai: DAI
293 * @tx_num: how many TX channels
294 * @tx_slot: pointer to an array which imply the TX slot number channel
295 * 0~num-1 uses
296 * @rx_num: how many RX channels
297 * @rx_slot: pointer to an array which imply the RX slot number channel
298 * 0~num-1 uses
299 *
300 * configure the relationship between channel number and TDM slot number.
301 */
302int 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)
305{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900306 int ret = -ENOTSUPP;
307
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900308 if (dai->driver->ops &&
309 dai->driver->ops->set_channel_map)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900310 ret = dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
311 rx_num, rx_slot);
312 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900313}
314EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
315
316/**
317 * snd_soc_dai_get_channel_map - Get DAI audio channel map
318 * @dai: DAI
319 * @tx_num: how many TX channels
320 * @tx_slot: pointer to an array which imply the TX slot number channel
321 * 0~num-1 uses
322 * @rx_num: how many RX channels
323 * @rx_slot: pointer to an array which imply the RX slot number channel
324 * 0~num-1 uses
325 */
326int 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)
329{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900330 int ret = -ENOTSUPP;
331
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900332 if (dai->driver->ops &&
333 dai->driver->ops->get_channel_map)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900334 ret = dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
335 rx_num, rx_slot);
336 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900337}
338EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
339
340/**
341 * snd_soc_dai_set_tristate - configure DAI system or master clock.
342 * @dai: DAI
343 * @tristate: tristate enable
344 *
345 * Tristates the DAI so that others can use it.
346 */
347int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
348{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900349 int ret = -EINVAL;
350
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900351 if (dai->driver->ops &&
352 dai->driver->ops->set_tristate)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900353 ret = dai->driver->ops->set_tristate(dai, tristate);
354
355 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900356}
357EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
358
359/**
360 * snd_soc_dai_digital_mute - configure DAI system or master clock.
361 * @dai: DAI
362 * @mute: mute enable
363 * @direction: stream to mute
364 *
365 * Mutes the DAI DAC.
366 */
367int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
368 int direction)
369{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900370 int ret = -ENOTSUPP;
371
Kuninori Morimoto350d9932020-07-09 10:55:41 +0900372 /*
373 * ignore if direction was CAPTURE
374 * and it had .no_capture_mute flag
375 */
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900376 if (dai->driver->ops &&
Kuninori Morimoto350d9932020-07-09 10:55:41 +0900377 dai->driver->ops->mute_stream &&
378 (direction == SNDRV_PCM_STREAM_PLAYBACK ||
379 !dai->driver->ops->no_capture_mute))
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900380 ret = dai->driver->ops->mute_stream(dai, mute, direction);
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900381
382 return soc_dai_ret(dai, ret);
Kuninori Morimoto06f6e1d2019-07-22 10:32:12 +0900383}
384EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900385
386int snd_soc_dai_hw_params(struct snd_soc_dai *dai,
387 struct snd_pcm_substream *substream,
388 struct snd_pcm_hw_params *params)
389{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900390 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900391 int ret = 0;
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900392
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900393 if (dai->driver->ops &&
Gyeongtaek Lee8b4ba1d2021-05-14 21:30:51 +0900394 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);
397 if (ret < 0)
398 goto end;
399
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900400 ret = dai->driver->ops->hw_params(substream, params, dai);
Gyeongtaek Lee8b4ba1d2021-05-14 21:30:51 +0900401 }
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +0900402
403 /* mark substream if succeeded */
404 if (ret == 0)
405 soc_dai_mark_push(dai, substream, hw_params);
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900406end:
407 return soc_dai_ret(dai, ret);
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900408}
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900409
410void snd_soc_dai_hw_free(struct snd_soc_dai *dai,
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +0900411 struct snd_pcm_substream *substream,
412 int rollback)
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900413{
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +0900414 if (rollback && !soc_dai_mark_match(dai, substream, hw_params))
415 return;
416
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900417 if (dai->driver->ops &&
418 dai->driver->ops->hw_free)
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900419 dai->driver->ops->hw_free(substream, dai);
Kuninori Morimotoc304c9a2020-09-29 13:31:53 +0900420
421 /* remove marked substream */
422 soc_dai_mark_pop(dai, substream, hw_params);
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900423}
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900424
425int snd_soc_dai_startup(struct snd_soc_dai *dai,
426 struct snd_pcm_substream *substream)
427{
428 int ret = 0;
429
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900430 if (dai->driver->ops &&
431 dai->driver->ops->startup)
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900432 ret = dai->driver->ops->startup(substream, dai);
433
Kuninori Morimoto00a0b462020-09-28 09:00:40 +0900434 /* mark substream if succeeded */
435 if (ret == 0)
436 soc_dai_mark_push(dai, substream, startup);
437
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900438 return soc_dai_ret(dai, ret);
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900439}
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900440
441void snd_soc_dai_shutdown(struct snd_soc_dai *dai,
Kuninori Morimoto00a0b462020-09-28 09:00:40 +0900442 struct snd_pcm_substream *substream,
443 int rollback)
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900444{
Kuninori Morimoto00a0b462020-09-28 09:00:40 +0900445 if (rollback && !soc_dai_mark_match(dai, substream, startup))
446 return;
447
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900448 if (dai->driver->ops &&
449 dai->driver->ops->shutdown)
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900450 dai->driver->ops->shutdown(substream, dai);
Kuninori Morimoto00a0b462020-09-28 09:00:40 +0900451
452 /* remove marked substream */
453 soc_dai_mark_pop(dai, substream, startup);
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900454}
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900455
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +0900456snd_pcm_sframes_t snd_soc_dai_delay(struct snd_soc_dai *dai,
457 struct snd_pcm_substream *substream)
458{
459 int delay = 0;
460
Kuninori Morimoto479914e2020-04-24 08:14:43 +0900461 if (dai->driver->ops &&
462 dai->driver->ops->delay)
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +0900463 delay = dai->driver->ops->delay(substream, dai);
464
465 return delay;
466}
Kuninori Morimotoe0f22622019-07-22 10:34:29 +0900467
Kuninori Morimotob423c422019-07-22 10:35:29 +0900468int snd_soc_dai_compress_new(struct snd_soc_dai *dai,
469 struct snd_soc_pcm_runtime *rtd, int num)
470{
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900471 int ret = -ENOTSUPP;
Kuninori Morimotob423c422019-07-22 10:35:29 +0900472 if (dai->driver->compress_new)
Kuninori Morimotoaa7b8232020-04-24 08:14:38 +0900473 ret = dai->driver->compress_new(rtd, num);
474 return soc_dai_ret(dai, ret);
Kuninori Morimotob423c422019-07-22 10:35:29 +0900475}
Kuninori Morimoto467fece2019-07-22 10:36:16 +0900476
477/*
478 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
479 *
480 * Returns true if the DAI supports the indicated stream type.
481 */
482bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int dir)
483{
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900484 struct snd_soc_pcm_stream *stream = snd_soc_dai_get_pcm_stream(dai, dir);
Kuninori Morimoto467fece2019-07-22 10:36:16 +0900485
486 /* If the codec specifies any channels at all, it supports the stream */
487 return stream->channels_min;
488}
Kuninori Morimoto0b73ba52020-04-24 08:14:48 +0900489
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500490/*
491 * snd_soc_dai_link_set_capabilities() - set dai_link properties based on its DAIs
492 */
493void snd_soc_dai_link_set_capabilities(struct snd_soc_dai_link *dai_link)
494{
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500495 bool supported[SNDRV_PCM_STREAM_LAST + 1];
496 int direction;
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500497
498 for_each_pcm_streams(direction) {
Kuninori Morimotod490f4e2021-08-16 13:56:01 +0900499 struct snd_soc_dai_link_component *cpu;
500 struct snd_soc_dai_link_component *codec;
501 struct snd_soc_dai *dai;
502 bool supported_cpu = false;
503 bool supported_codec = false;
504 int i;
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500505
506 for_each_link_cpus(dai_link, i, cpu) {
Kuninori Morimotoc1c277b2020-08-27 08:55:39 +0900507 dai = snd_soc_find_dai_with_mutex(cpu);
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -0500508 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
509 supported_cpu = true;
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500510 break;
511 }
512 }
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500513 for_each_link_codecs(dai_link, i, codec) {
Kuninori Morimotoc1c277b2020-08-27 08:55:39 +0900514 dai = snd_soc_find_dai_with_mutex(codec);
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -0500515 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
516 supported_codec = true;
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500517 break;
518 }
519 }
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -0500520 supported[direction] = supported_cpu && supported_codec;
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500521 }
522
523 dai_link->dpcm_playback = supported[SNDRV_PCM_STREAM_PLAYBACK];
524 dai_link->dpcm_capture = supported[SNDRV_PCM_STREAM_CAPTURE];
525}
526EXPORT_SYMBOL_GPL(snd_soc_dai_link_set_capabilities);
527
Kuninori Morimotodc829102020-05-15 09:46:27 +0900528void snd_soc_dai_action(struct snd_soc_dai *dai,
529 int stream, int action)
530{
Kuninori Morimoto5552f8d2020-05-15 09:46:47 +0900531 /* see snd_soc_dai_stream_active() */
Kuninori Morimotodc829102020-05-15 09:46:27 +0900532 dai->stream_active[stream] += action;
Kuninori Morimoto0812a082020-05-15 09:48:02 +0900533
Kuninori Morimoto488b2ca2020-05-15 09:46:42 +0900534 /* see snd_soc_component_active() */
Kuninori Morimotodc829102020-05-15 09:46:27 +0900535 dai->component->active += action;
536}
537EXPORT_SYMBOL_GPL(snd_soc_dai_action);
538
Kuninori Morimotoefffd9b2020-05-15 09:46:37 +0900539int snd_soc_dai_active(struct snd_soc_dai *dai)
540{
541 int stream, active;
542
543 active = 0;
544 for_each_pcm_streams(stream)
545 active += dai->stream_active[stream];
546
547 return active;
548}
549EXPORT_SYMBOL_GPL(snd_soc_dai_active);
550
Kuninori Morimoto51801ae2020-04-24 08:15:15 +0900551int snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime *rtd, int order)
552{
553 struct snd_soc_dai *dai;
554 int i;
555
556 for_each_rtd_dais(rtd, i, dai) {
557 if (dai->driver->probe_order != order)
558 continue;
559
560 if (dai->driver->probe) {
561 int ret = dai->driver->probe(dai);
562
563 if (ret < 0)
564 return soc_dai_ret(dai, ret);
565 }
566
567 dai->probed = 1;
568 }
569
570 return 0;
571}
572
Kuninori Morimoto7eaa3132020-04-24 08:15:20 +0900573int snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime *rtd, int order)
574{
575 struct snd_soc_dai *dai;
576 int i, r, ret = 0;
577
578 for_each_rtd_dais(rtd, i, dai) {
579 if (dai->driver->remove_order != order)
580 continue;
581
582 if (dai->probed &&
583 dai->driver->remove) {
584 r = dai->driver->remove(dai);
585 if (r < 0)
586 ret = r; /* use last error */
587 }
588
589 dai->probed = 0;
590 }
591
592 return ret;
593}
594
Kuninori Morimoto0b73ba52020-04-24 08:14:48 +0900595int snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime *rtd)
596{
597 struct snd_soc_dai *dai;
Kuninori Morimoto454a7422021-08-16 13:56:06 +0900598 int i;
Kuninori Morimoto0b73ba52020-04-24 08:14:48 +0900599
600 for_each_rtd_dais(rtd, i, dai) {
601 if (dai->driver->pcm_new) {
Kuninori Morimoto454a7422021-08-16 13:56:06 +0900602 int ret = dai->driver->pcm_new(rtd, dai);
Kuninori Morimoto0b73ba52020-04-24 08:14:48 +0900603 if (ret < 0)
604 return soc_dai_ret(dai, ret);
605 }
606 }
607
608 return 0;
609}
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900610
611int snd_soc_pcm_dai_prepare(struct snd_pcm_substream *substream)
612{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900613 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900614 struct snd_soc_dai *dai;
615 int i, ret;
616
617 for_each_rtd_dais(rtd, i, dai) {
618 if (dai->driver->ops &&
619 dai->driver->ops->prepare) {
620 ret = dai->driver->ops->prepare(substream, dai);
621 if (ret < 0)
622 return soc_dai_ret(dai, ret);
623 }
624 }
625
626 return 0;
627}
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900628
Kuninori Morimoto6374f492020-12-01 08:51:33 +0900629static int soc_dai_trigger(struct snd_soc_dai *dai,
630 struct snd_pcm_substream *substream, int cmd)
631{
632 int ret = 0;
633
634 if (dai->driver->ops &&
635 dai->driver->ops->trigger)
636 ret = dai->driver->ops->trigger(substream, cmd, dai);
637
638 return soc_dai_ret(dai, ret);
639}
640
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900641int snd_soc_pcm_dai_trigger(struct snd_pcm_substream *substream,
Kuninori Morimoto6374f492020-12-01 08:51:33 +0900642 int cmd, int rollback)
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900643{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900644 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900645 struct snd_soc_dai *dai;
Kuninori Morimoto6374f492020-12-01 08:51:33 +0900646 int i, r, ret = 0;
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900647
Kuninori Morimoto6374f492020-12-01 08:51:33 +0900648 switch (cmd) {
649 case SNDRV_PCM_TRIGGER_START:
650 case SNDRV_PCM_TRIGGER_RESUME:
651 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
652 for_each_rtd_dais(rtd, i, dai) {
653 ret = soc_dai_trigger(dai, substream, cmd);
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900654 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +0900655 break;
656 soc_dai_mark_push(dai, substream, trigger);
657 }
658 break;
659 case SNDRV_PCM_TRIGGER_STOP:
660 case SNDRV_PCM_TRIGGER_SUSPEND:
661 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
662 for_each_rtd_dais(rtd, i, dai) {
663 if (rollback && !soc_dai_mark_match(dai, substream, trigger))
664 continue;
665
666 r = soc_dai_trigger(dai, substream, cmd);
667 if (r < 0)
668 ret = r; /* use last ret */
669 soc_dai_mark_pop(dai, substream, trigger);
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900670 }
671 }
672
Kuninori Morimoto6374f492020-12-01 08:51:33 +0900673 return ret;
Kuninori Morimoto42f24722020-04-24 08:15:04 +0900674}
Kuninori Morimoto308193582020-04-24 08:15:09 +0900675
676int snd_soc_pcm_dai_bespoke_trigger(struct snd_pcm_substream *substream,
677 int cmd)
678{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900679 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto308193582020-04-24 08:15:09 +0900680 struct snd_soc_dai *dai;
681 int i, ret;
682
683 for_each_rtd_dais(rtd, i, dai) {
684 if (dai->driver->ops &&
685 dai->driver->ops->bespoke_trigger) {
686 ret = dai->driver->ops->bespoke_trigger(substream,
687 cmd, dai);
688 if (ret < 0)
689 return soc_dai_ret(dai, ret);
690 }
691 }
692
693 return 0;
694}
Kuninori Morimotob5ae4cc2020-04-24 08:15:24 +0900695
696int snd_soc_dai_compr_startup(struct snd_soc_dai *dai,
697 struct snd_compr_stream *cstream)
698{
699 int ret = 0;
700
701 if (dai->driver->cops &&
702 dai->driver->cops->startup)
703 ret = dai->driver->cops->startup(cstream, dai);
704
Kuninori Morimoto1e6a93c2020-11-19 08:50:04 +0900705 /* mark cstream if succeeded */
706 if (ret == 0)
707 soc_dai_mark_push(dai, cstream, compr_startup);
708
Kuninori Morimotob5ae4cc2020-04-24 08:15:24 +0900709 return soc_dai_ret(dai, ret);
710}
711EXPORT_SYMBOL_GPL(snd_soc_dai_compr_startup);
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900712
713void snd_soc_dai_compr_shutdown(struct snd_soc_dai *dai,
Kuninori Morimoto1e6a93c2020-11-19 08:50:04 +0900714 struct snd_compr_stream *cstream,
715 int rollback)
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900716{
Kuninori Morimoto1e6a93c2020-11-19 08:50:04 +0900717 if (rollback && !soc_dai_mark_match(dai, cstream, compr_startup))
718 return;
719
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900720 if (dai->driver->cops &&
721 dai->driver->cops->shutdown)
722 dai->driver->cops->shutdown(cstream, dai);
Kuninori Morimoto1e6a93c2020-11-19 08:50:04 +0900723
724 /* remove marked cstream */
725 soc_dai_mark_pop(dai, cstream, compr_startup);
Kuninori Morimoto2b25f812020-04-24 08:15:28 +0900726}
727EXPORT_SYMBOL_GPL(snd_soc_dai_compr_shutdown);
Kuninori Morimotoeb084112020-04-24 08:15:32 +0900728
729int snd_soc_dai_compr_trigger(struct snd_soc_dai *dai,
730 struct snd_compr_stream *cstream, int cmd)
731{
732 int ret = 0;
733
734 if (dai->driver->cops &&
735 dai->driver->cops->trigger)
736 ret = dai->driver->cops->trigger(cstream, cmd, dai);
737
738 return soc_dai_ret(dai, ret);
739}
740EXPORT_SYMBOL_GPL(snd_soc_dai_compr_trigger);
Kuninori Morimoto8dfedaf2020-04-24 08:15:36 +0900741
742int snd_soc_dai_compr_set_params(struct snd_soc_dai *dai,
743 struct snd_compr_stream *cstream,
744 struct snd_compr_params *params)
745{
746 int ret = 0;
747
748 if (dai->driver->cops &&
749 dai->driver->cops->set_params)
750 ret = dai->driver->cops->set_params(cstream, params, dai);
751
752 return soc_dai_ret(dai, ret);
753}
754EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_params);
Kuninori Morimotoadbef5432020-04-24 08:15:40 +0900755
756int snd_soc_dai_compr_get_params(struct snd_soc_dai *dai,
757 struct snd_compr_stream *cstream,
758 struct snd_codec *params)
759{
760 int ret = 0;
761
762 if (dai->driver->cops &&
763 dai->driver->cops->get_params)
764 ret = dai->driver->cops->get_params(cstream, params, dai);
765
766 return soc_dai_ret(dai, ret);
767}
768EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_params);
Kuninori Morimoto53294352020-04-24 08:15:45 +0900769
770int snd_soc_dai_compr_ack(struct snd_soc_dai *dai,
771 struct snd_compr_stream *cstream,
772 size_t bytes)
773{
774 int ret = 0;
775
776 if (dai->driver->cops &&
777 dai->driver->cops->ack)
778 ret = dai->driver->cops->ack(cstream, bytes, dai);
779
780 return soc_dai_ret(dai, ret);
781}
782EXPORT_SYMBOL_GPL(snd_soc_dai_compr_ack);
Kuninori Morimotoed38cc52020-04-24 08:15:49 +0900783
784int snd_soc_dai_compr_pointer(struct snd_soc_dai *dai,
785 struct snd_compr_stream *cstream,
786 struct snd_compr_tstamp *tstamp)
787{
788 int ret = 0;
789
790 if (dai->driver->cops &&
791 dai->driver->cops->pointer)
792 ret = dai->driver->cops->pointer(cstream, tstamp, dai);
793
794 return soc_dai_ret(dai, ret);
795}
796EXPORT_SYMBOL_GPL(snd_soc_dai_compr_pointer);
Kuninori Morimoto88b3a7d2020-04-24 08:15:54 +0900797
798int snd_soc_dai_compr_set_metadata(struct snd_soc_dai *dai,
799 struct snd_compr_stream *cstream,
800 struct snd_compr_metadata *metadata)
801{
802 int ret = 0;
803
804 if (dai->driver->cops &&
805 dai->driver->cops->set_metadata)
806 ret = dai->driver->cops->set_metadata(cstream, metadata, dai);
807
808 return soc_dai_ret(dai, ret);
809}
810EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_metadata);
Kuninori Morimoto94d72812020-04-24 08:15:59 +0900811
812int snd_soc_dai_compr_get_metadata(struct snd_soc_dai *dai,
813 struct snd_compr_stream *cstream,
814 struct snd_compr_metadata *metadata)
815{
816 int ret = 0;
817
818 if (dai->driver->cops &&
819 dai->driver->cops->get_metadata)
820 ret = dai->driver->cops->get_metadata(cstream, metadata, dai);
821
822 return soc_dai_ret(dai, ret);
823}
824EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_metadata);