1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2020 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
10 #include "meson-card.h"
12 int meson_card_i2s_set_sysclk(struct snd_pcm_substream
*substream
,
13 struct snd_pcm_hw_params
*params
,
16 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
17 struct snd_soc_dai
*codec_dai
;
24 mclk
= params_rate(params
) * mclk_fs
;
26 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
27 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
29 if (ret
&& ret
!= -ENOTSUPP
)
33 ret
= snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd
, 0), 0, mclk
,
35 if (ret
&& ret
!= -ENOTSUPP
)
40 EXPORT_SYMBOL_GPL(meson_card_i2s_set_sysclk
);
42 int meson_card_reallocate_links(struct snd_soc_card
*card
,
43 unsigned int num_links
)
45 struct meson_card
*priv
= snd_soc_card_get_drvdata(card
);
46 struct snd_soc_dai_link
*links
;
49 links
= krealloc(priv
->card
.dai_link
,
50 num_links
* sizeof(*priv
->card
.dai_link
),
51 GFP_KERNEL
| __GFP_ZERO
);
55 ldata
= krealloc(priv
->link_data
,
56 num_links
* sizeof(*priv
->link_data
),
57 GFP_KERNEL
| __GFP_ZERO
);
61 priv
->card
.dai_link
= links
;
62 priv
->link_data
= ldata
;
63 priv
->card
.num_links
= num_links
;
69 dev_err(priv
->card
.dev
, "failed to allocate links\n");
73 EXPORT_SYMBOL_GPL(meson_card_reallocate_links
);
75 int meson_card_parse_dai(struct snd_soc_card
*card
,
76 struct device_node
*node
,
77 struct device_node
**dai_of_node
,
78 const char **dai_name
)
80 struct of_phandle_args args
;
83 if (!dai_name
|| !dai_of_node
|| !node
)
86 ret
= of_parse_phandle_with_args(node
, "sound-dai",
87 "#sound-dai-cells", 0, &args
);
89 if (ret
!= -EPROBE_DEFER
)
90 dev_err(card
->dev
, "can't parse dai %d\n", ret
);
93 *dai_of_node
= args
.np
;
95 return snd_soc_get_dai_name(&args
, dai_name
);
97 EXPORT_SYMBOL_GPL(meson_card_parse_dai
);
99 static int meson_card_set_link_name(struct snd_soc_card
*card
,
100 struct snd_soc_dai_link
*link
,
101 struct device_node
*node
,
104 char *name
= devm_kasprintf(card
->dev
, GFP_KERNEL
, "%s.%s",
105 prefix
, node
->full_name
);
110 link
->stream_name
= name
;
115 unsigned int meson_card_parse_daifmt(struct device_node
*node
,
116 struct device_node
*cpu_node
)
118 struct device_node
*bitclkmaster
= NULL
;
119 struct device_node
*framemaster
= NULL
;
122 daifmt
= snd_soc_of_parse_daifmt(node
, "",
123 &bitclkmaster
, &framemaster
);
124 daifmt
&= ~SND_SOC_DAIFMT_MASTER_MASK
;
126 /* If no master is provided, default to cpu master */
127 if (!bitclkmaster
|| bitclkmaster
== cpu_node
) {
128 daifmt
|= (!framemaster
|| framemaster
== cpu_node
) ?
129 SND_SOC_DAIFMT_CBS_CFS
: SND_SOC_DAIFMT_CBS_CFM
;
131 daifmt
|= (!framemaster
|| framemaster
== cpu_node
) ?
132 SND_SOC_DAIFMT_CBM_CFS
: SND_SOC_DAIFMT_CBM_CFM
;
135 of_node_put(bitclkmaster
);
136 of_node_put(framemaster
);
140 EXPORT_SYMBOL_GPL(meson_card_parse_daifmt
);
142 int meson_card_set_be_link(struct snd_soc_card
*card
,
143 struct snd_soc_dai_link
*link
,
144 struct device_node
*node
)
146 struct snd_soc_dai_link_component
*codec
;
147 struct device_node
*np
;
150 num_codecs
= of_get_child_count(node
);
152 dev_err(card
->dev
, "be link %s has no codec\n",
157 codec
= devm_kcalloc(card
->dev
, num_codecs
, sizeof(*codec
), GFP_KERNEL
);
161 link
->codecs
= codec
;
162 link
->num_codecs
= num_codecs
;
164 for_each_child_of_node(node
, np
) {
165 ret
= meson_card_parse_dai(card
, np
, &codec
->of_node
,
175 ret
= meson_card_set_link_name(card
, link
, node
, "be");
177 dev_err(card
->dev
, "error setting %pOFn link name\n", np
);
181 EXPORT_SYMBOL_GPL(meson_card_set_be_link
);
183 int meson_card_set_fe_link(struct snd_soc_card
*card
,
184 struct snd_soc_dai_link
*link
,
185 struct device_node
*node
,
188 struct snd_soc_dai_link_component
*codec
;
190 codec
= devm_kzalloc(card
->dev
, sizeof(*codec
), GFP_KERNEL
);
194 link
->codecs
= codec
;
195 link
->num_codecs
= 1;
198 link
->dpcm_merged_format
= 1;
199 link
->dpcm_merged_chan
= 1;
200 link
->dpcm_merged_rate
= 1;
201 link
->codecs
->dai_name
= "snd-soc-dummy-dai";
202 link
->codecs
->name
= "snd-soc-dummy";
205 link
->dpcm_playback
= 1;
207 link
->dpcm_capture
= 1;
209 return meson_card_set_link_name(card
, link
, node
, "fe");
211 EXPORT_SYMBOL_GPL(meson_card_set_fe_link
);
213 static int meson_card_add_links(struct snd_soc_card
*card
)
215 struct meson_card
*priv
= snd_soc_card_get_drvdata(card
);
216 struct device_node
*node
= card
->dev
->of_node
;
217 struct device_node
*np
;
220 num
= of_get_child_count(node
);
222 dev_err(card
->dev
, "card has no links\n");
226 ret
= meson_card_reallocate_links(card
, num
);
231 for_each_child_of_node(node
, np
) {
232 ret
= priv
->match_data
->add_link(card
, np
, &i
);
244 static int meson_card_parse_of_optional(struct snd_soc_card
*card
,
245 const char *propname
,
246 int (*func
)(struct snd_soc_card
*c
,
249 /* If property is not provided, don't fail ... */
250 if (!of_property_read_bool(card
->dev
->of_node
, propname
))
253 /* ... but do fail if it is provided and the parsing fails */
254 return func(card
, propname
);
257 static void meson_card_clean_references(struct meson_card
*priv
)
259 struct snd_soc_card
*card
= &priv
->card
;
260 struct snd_soc_dai_link
*link
;
261 struct snd_soc_dai_link_component
*codec
;
262 struct snd_soc_aux_dev
*aux
;
265 if (card
->dai_link
) {
266 for_each_card_prelinks(card
, i
, link
) {
268 of_node_put(link
->cpus
->of_node
);
269 for_each_link_codecs(link
, j
, codec
)
270 of_node_put(codec
->of_node
);
275 for_each_card_pre_auxs(card
, i
, aux
)
276 of_node_put(aux
->dlc
.of_node
);
279 kfree(card
->dai_link
);
280 kfree(priv
->link_data
);
283 int meson_card_probe(struct platform_device
*pdev
)
285 const struct meson_card_match_data
*data
;
286 struct device
*dev
= &pdev
->dev
;
287 struct meson_card
*priv
;
290 data
= of_device_get_match_data(dev
);
292 dev_err(dev
, "failed to match device\n");
296 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
300 platform_set_drvdata(pdev
, priv
);
301 snd_soc_card_set_drvdata(&priv
->card
, priv
);
303 priv
->card
.owner
= THIS_MODULE
;
304 priv
->card
.dev
= dev
;
305 priv
->match_data
= data
;
307 ret
= snd_soc_of_parse_card_name(&priv
->card
, "model");
311 ret
= meson_card_parse_of_optional(&priv
->card
, "audio-routing",
312 snd_soc_of_parse_audio_routing
);
314 dev_err(dev
, "error while parsing routing\n");
318 ret
= meson_card_parse_of_optional(&priv
->card
, "audio-widgets",
319 snd_soc_of_parse_audio_simple_widgets
);
321 dev_err(dev
, "error while parsing widgets\n");
325 ret
= meson_card_add_links(&priv
->card
);
329 ret
= snd_soc_of_parse_aux_devs(&priv
->card
, "audio-aux-devs");
333 ret
= devm_snd_soc_register_card(dev
, &priv
->card
);
340 meson_card_clean_references(priv
);
343 EXPORT_SYMBOL_GPL(meson_card_probe
);
345 int meson_card_remove(struct platform_device
*pdev
)
347 struct meson_card
*priv
= platform_get_drvdata(pdev
);
349 meson_card_clean_references(priv
);
353 EXPORT_SYMBOL_GPL(meson_card_remove
);
355 MODULE_DESCRIPTION("Amlogic Sound Card Utils");
356 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
357 MODULE_LICENSE("GPL v2");