Revert "unicode: Don't special case ignorable code points"
[linux.git] / sound / soc / meson / meson-card-utils.c
blob1a4ef124e4e259502085f217d06e580afd3febbb
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2020 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
8 #include <sound/soc.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,
14 unsigned int mclk_fs)
16 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
17 struct snd_soc_dai *codec_dai;
18 unsigned int mclk;
19 int ret, i;
21 if (!mclk_fs)
22 return 0;
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,
28 SND_SOC_CLOCK_IN);
29 if (ret && ret != -ENOTSUPP)
30 return ret;
33 ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_cpu(rtd, 0), 0, mclk,
34 SND_SOC_CLOCK_OUT);
35 if (ret && ret != -ENOTSUPP)
36 return ret;
38 return 0;
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;
47 void **ldata;
49 links = krealloc(priv->card.dai_link,
50 num_links * sizeof(*priv->card.dai_link),
51 GFP_KERNEL | __GFP_ZERO);
52 if (!links)
53 goto err_links;
55 ldata = krealloc(priv->link_data,
56 num_links * sizeof(*priv->link_data),
57 GFP_KERNEL | __GFP_ZERO);
58 if (!ldata)
59 goto err_ldata;
61 priv->card.dai_link = links;
62 priv->link_data = ldata;
63 priv->card.num_links = num_links;
64 return 0;
66 err_ldata:
67 kfree(links);
68 err_links:
69 dev_err(priv->card.dev, "failed to allocate links\n");
70 return -ENOMEM;
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)
79 int ret;
81 if (!dlc || !node)
82 return -EINVAL;
84 ret = snd_soc_of_get_dlc(node, NULL, dlc, 0);
85 if (ret)
86 return dev_err_probe(card->dev, ret, "can't parse dai\n");
88 return ret;
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,
95 const char *prefix)
97 char *name = devm_kasprintf(card->dev, GFP_KERNEL, "%s.%s",
98 prefix, node->full_name);
99 if (!name)
100 return -ENOMEM;
102 link->name = name;
103 link->stream_name = name;
105 return 0;
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;
113 unsigned int daifmt;
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;
123 } else {
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);
131 return daifmt;
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;
141 int ret, num_codecs;
143 num_codecs = of_get_child_count(node);
144 if (!num_codecs) {
145 dev_err(card->dev, "be link %s has no codec\n",
146 node->full_name);
147 return -EINVAL;
150 codec = devm_kcalloc(card->dev, num_codecs, sizeof(*codec), GFP_KERNEL);
151 if (!codec)
152 return -ENOMEM;
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);
159 if (ret) {
160 of_node_put(np);
161 return ret;
164 codec++;
167 ret = meson_card_set_link_name(card, link, node, "be");
168 if (ret)
169 dev_err(card->dev, "error setting %pOFn link name\n", np);
171 return ret;
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,
178 bool is_playback)
180 link->codecs = &snd_soc_dummy_dlc;
181 link->num_codecs = 1;
183 link->dynamic = 1;
184 link->dpcm_merged_format = 1;
185 link->dpcm_merged_chan = 1;
186 link->dpcm_merged_rate = 1;
188 if (is_playback)
189 link->playback_only = 1;
190 else
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;
202 int num, i, ret;
204 num = of_get_child_count(node);
205 if (!num) {
206 dev_err(card->dev, "card has no links\n");
207 return -EINVAL;
210 ret = meson_card_reallocate_links(card, num);
211 if (ret)
212 return ret;
214 i = 0;
215 for_each_child_of_node(node, np) {
216 ret = priv->match_data->add_link(card, np, &i);
217 if (ret) {
218 of_node_put(np);
219 return ret;
222 i++;
225 return 0;
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,
231 const char *p))
233 /* If property is not provided, don't fail ... */
234 if (!of_property_read_bool(card->dev->of_node, propname))
235 return 0;
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;
247 int i, j;
249 if (card->dai_link) {
250 for_each_card_prelinks(card, i, link) {
251 if (link->cpus)
252 of_node_put(link->cpus->of_node);
253 for_each_link_codecs(link, j, codec)
254 of_node_put(codec->of_node);
258 if (card->aux_dev) {
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;
272 int ret;
274 data = of_device_get_match_data(dev);
275 if (!data) {
276 dev_err(dev, "failed to match device\n");
277 return -ENODEV;
280 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
281 if (!priv)
282 return -ENOMEM;
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");
293 if (ret < 0)
294 return ret;
296 ret = meson_card_parse_of_optional(&priv->card, "audio-routing",
297 snd_soc_of_parse_audio_routing);
298 if (ret) {
299 dev_err(dev, "error while parsing routing\n");
300 return ret;
303 ret = meson_card_parse_of_optional(&priv->card, "audio-widgets",
304 snd_soc_of_parse_audio_simple_widgets);
305 if (ret) {
306 dev_err(dev, "error while parsing widgets\n");
307 return ret;
310 ret = meson_card_add_links(&priv->card);
311 if (ret)
312 goto out_err;
314 ret = snd_soc_of_parse_aux_devs(&priv->card, "audio-aux-devs");
315 if (ret)
316 goto out_err;
318 ret = devm_snd_soc_register_card(dev, &priv->card);
319 if (ret)
320 goto out_err;
322 return 0;
324 out_err:
325 meson_card_clean_references(priv);
326 return ret;
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");