1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
6 #include <linux/module.h>
7 #include <sound/pcm_params.h>
9 #include <sound/soc-dai.h>
11 #include "meson-codec-glue.h"
13 static struct snd_soc_dapm_widget
*
14 meson_codec_glue_get_input(struct snd_soc_dapm_widget
*w
)
16 struct snd_soc_dapm_path
*p
;
17 struct snd_soc_dapm_widget
*in
;
19 snd_soc_dapm_widget_for_each_source_path(w
, p
) {
23 /* Check that we still are in the same component */
24 if (snd_soc_dapm_to_component(w
->dapm
) !=
25 snd_soc_dapm_to_component(p
->source
->dapm
))
28 if (p
->source
->id
== snd_soc_dapm_dai_in
)
31 in
= meson_codec_glue_get_input(p
->source
);
39 static void meson_codec_glue_input_set_data(struct snd_soc_dai
*dai
,
40 struct meson_codec_glue_input
*data
)
42 snd_soc_dai_dma_data_set_playback(dai
, data
);
45 struct meson_codec_glue_input
*
46 meson_codec_glue_input_get_data(struct snd_soc_dai
*dai
)
48 return snd_soc_dai_dma_data_get_playback(dai
);
50 EXPORT_SYMBOL_GPL(meson_codec_glue_input_get_data
);
52 static struct meson_codec_glue_input
*
53 meson_codec_glue_output_get_input_data(struct snd_soc_dapm_widget
*w
)
55 struct snd_soc_dapm_widget
*in
=
56 meson_codec_glue_get_input(w
);
57 struct snd_soc_dai
*dai
;
64 return meson_codec_glue_input_get_data(dai
);
67 int meson_codec_glue_input_hw_params(struct snd_pcm_substream
*substream
,
68 struct snd_pcm_hw_params
*params
,
69 struct snd_soc_dai
*dai
)
71 struct meson_codec_glue_input
*data
=
72 meson_codec_glue_input_get_data(dai
);
74 data
->params
.rates
= snd_pcm_rate_to_rate_bit(params_rate(params
));
75 data
->params
.rate_min
= params_rate(params
);
76 data
->params
.rate_max
= params_rate(params
);
77 data
->params
.formats
= 1ULL << (__force
int) params_format(params
);
78 data
->params
.channels_min
= params_channels(params
);
79 data
->params
.channels_max
= params_channels(params
);
80 data
->params
.sig_bits
= dai
->driver
->playback
.sig_bits
;
84 EXPORT_SYMBOL_GPL(meson_codec_glue_input_hw_params
);
86 int meson_codec_glue_input_set_fmt(struct snd_soc_dai
*dai
,
89 struct meson_codec_glue_input
*data
=
90 meson_codec_glue_input_get_data(dai
);
92 /* Save the source stream format for the downstream link */
96 EXPORT_SYMBOL_GPL(meson_codec_glue_input_set_fmt
);
98 int meson_codec_glue_output_startup(struct snd_pcm_substream
*substream
,
99 struct snd_soc_dai
*dai
)
101 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
102 struct snd_soc_dapm_widget
*w
= snd_soc_dai_get_widget_capture(dai
);
103 struct meson_codec_glue_input
*in_data
= meson_codec_glue_output_get_input_data(w
);
108 if (WARN_ON(!rtd
->dai_link
->c2c_params
)) {
109 dev_warn(dai
->dev
, "codec2codec link expected\n");
113 /* Replace link params with the input params */
114 rtd
->dai_link
->c2c_params
= &in_data
->params
;
115 rtd
->dai_link
->num_c2c_params
= 1;
117 return snd_soc_runtime_set_dai_fmt(rtd
, in_data
->fmt
);
119 EXPORT_SYMBOL_GPL(meson_codec_glue_output_startup
);
121 int meson_codec_glue_input_dai_probe(struct snd_soc_dai
*dai
)
123 struct meson_codec_glue_input
*data
;
125 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
129 meson_codec_glue_input_set_data(dai
, data
);
132 EXPORT_SYMBOL_GPL(meson_codec_glue_input_dai_probe
);
134 int meson_codec_glue_input_dai_remove(struct snd_soc_dai
*dai
)
136 struct meson_codec_glue_input
*data
=
137 meson_codec_glue_input_get_data(dai
);
142 EXPORT_SYMBOL_GPL(meson_codec_glue_input_dai_remove
);
144 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
145 MODULE_DESCRIPTION("Amlogic Codec Glue Helpers");
146 MODULE_LICENSE("GPL v2");