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
= snd_soc_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(snd_soc_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 snd_soc_dai_link_component
*dlc
)
84 ret
= snd_soc_of_get_dlc(node
, NULL
, dlc
, 0);
86 return dev_err_probe(card
->dev
, ret
, "can't parse dai\n");
90 EXPORT_SYMBOL_GPL(meson_card_parse_dai
);
92 static int meson_card_set_link_name(struct snd_soc_card
*card
,
93 struct snd_soc_dai_link
*link
,
94 struct device_node
*node
,
97 char *name
= devm_kasprintf(card
->dev
, GFP_KERNEL
, "%s.%s",
98 prefix
, node
->full_name
);
103 link
->stream_name
= name
;
108 unsigned int meson_card_parse_daifmt(struct device_node
*node
,
109 struct device_node
*cpu_node
)
111 struct device_node
*bitclkmaster
= NULL
;
112 struct device_node
*framemaster
= NULL
;
115 daifmt
= snd_soc_daifmt_parse_format(node
, NULL
);
117 snd_soc_daifmt_parse_clock_provider_as_phandle(node
, NULL
, &bitclkmaster
, &framemaster
);
119 /* If no master is provided, default to cpu master */
120 if (!bitclkmaster
|| bitclkmaster
== cpu_node
) {
121 daifmt
|= (!framemaster
|| framemaster
== cpu_node
) ?
122 SND_SOC_DAIFMT_CBS_CFS
: SND_SOC_DAIFMT_CBS_CFM
;
124 daifmt
|= (!framemaster
|| framemaster
== cpu_node
) ?
125 SND_SOC_DAIFMT_CBM_CFS
: SND_SOC_DAIFMT_CBM_CFM
;
128 of_node_put(bitclkmaster
);
129 of_node_put(framemaster
);
133 EXPORT_SYMBOL_GPL(meson_card_parse_daifmt
);
135 int meson_card_set_be_link(struct snd_soc_card
*card
,
136 struct snd_soc_dai_link
*link
,
137 struct device_node
*node
)
139 struct snd_soc_dai_link_component
*codec
;
140 struct device_node
*np
;
143 num_codecs
= of_get_child_count(node
);
145 dev_err(card
->dev
, "be link %s has no codec\n",
150 codec
= devm_kcalloc(card
->dev
, num_codecs
, sizeof(*codec
), GFP_KERNEL
);
154 link
->codecs
= codec
;
155 link
->num_codecs
= num_codecs
;
157 for_each_child_of_node(node
, np
) {
158 ret
= meson_card_parse_dai(card
, np
, codec
);
167 ret
= meson_card_set_link_name(card
, link
, node
, "be");
169 dev_err(card
->dev
, "error setting %pOFn link name\n", np
);
173 EXPORT_SYMBOL_GPL(meson_card_set_be_link
);
175 int meson_card_set_fe_link(struct snd_soc_card
*card
,
176 struct snd_soc_dai_link
*link
,
177 struct device_node
*node
,
180 link
->codecs
= &snd_soc_dummy_dlc
;
181 link
->num_codecs
= 1;
184 link
->dpcm_merged_format
= 1;
185 link
->dpcm_merged_chan
= 1;
186 link
->dpcm_merged_rate
= 1;
189 link
->playback_only
= 1;
191 link
->capture_only
= 1;
193 return meson_card_set_link_name(card
, link
, node
, "fe");
195 EXPORT_SYMBOL_GPL(meson_card_set_fe_link
);
197 static int meson_card_add_links(struct snd_soc_card
*card
)
199 struct meson_card
*priv
= snd_soc_card_get_drvdata(card
);
200 struct device_node
*node
= card
->dev
->of_node
;
201 struct device_node
*np
;
204 num
= of_get_child_count(node
);
206 dev_err(card
->dev
, "card has no links\n");
210 ret
= meson_card_reallocate_links(card
, num
);
215 for_each_child_of_node(node
, np
) {
216 ret
= priv
->match_data
->add_link(card
, np
, &i
);
228 static int meson_card_parse_of_optional(struct snd_soc_card
*card
,
229 const char *propname
,
230 int (*func
)(struct snd_soc_card
*c
,
233 /* If property is not provided, don't fail ... */
234 if (!of_property_read_bool(card
->dev
->of_node
, propname
))
237 /* ... but do fail if it is provided and the parsing fails */
238 return func(card
, propname
);
241 static void meson_card_clean_references(struct meson_card
*priv
)
243 struct snd_soc_card
*card
= &priv
->card
;
244 struct snd_soc_dai_link
*link
;
245 struct snd_soc_dai_link_component
*codec
;
246 struct snd_soc_aux_dev
*aux
;
249 if (card
->dai_link
) {
250 for_each_card_prelinks(card
, i
, link
) {
252 of_node_put(link
->cpus
->of_node
);
253 for_each_link_codecs(link
, j
, codec
)
254 of_node_put(codec
->of_node
);
259 for_each_card_pre_auxs(card
, i
, aux
)
260 of_node_put(aux
->dlc
.of_node
);
263 kfree(card
->dai_link
);
264 kfree(priv
->link_data
);
267 int meson_card_probe(struct platform_device
*pdev
)
269 const struct meson_card_match_data
*data
;
270 struct device
*dev
= &pdev
->dev
;
271 struct meson_card
*priv
;
274 data
= of_device_get_match_data(dev
);
276 dev_err(dev
, "failed to match device\n");
280 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
284 platform_set_drvdata(pdev
, priv
);
285 snd_soc_card_set_drvdata(&priv
->card
, priv
);
287 priv
->card
.owner
= THIS_MODULE
;
288 priv
->card
.dev
= dev
;
289 priv
->card
.driver_name
= dev
->driver
->name
;
290 priv
->match_data
= data
;
292 ret
= snd_soc_of_parse_card_name(&priv
->card
, "model");
296 ret
= meson_card_parse_of_optional(&priv
->card
, "audio-routing",
297 snd_soc_of_parse_audio_routing
);
299 dev_err(dev
, "error while parsing routing\n");
303 ret
= meson_card_parse_of_optional(&priv
->card
, "audio-widgets",
304 snd_soc_of_parse_audio_simple_widgets
);
306 dev_err(dev
, "error while parsing widgets\n");
310 ret
= meson_card_add_links(&priv
->card
);
314 ret
= snd_soc_of_parse_aux_devs(&priv
->card
, "audio-aux-devs");
318 ret
= devm_snd_soc_register_card(dev
, &priv
->card
);
325 meson_card_clean_references(priv
);
328 EXPORT_SYMBOL_GPL(meson_card_probe
);
330 void meson_card_remove(struct platform_device
*pdev
)
332 struct meson_card
*priv
= platform_get_drvdata(pdev
);
334 meson_card_clean_references(priv
);
336 EXPORT_SYMBOL_GPL(meson_card_remove
);
338 MODULE_DESCRIPTION("Amlogic Sound Card Utils");
339 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
340 MODULE_LICENSE("GPL v2");