1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 // Copyright (c) 2018 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
9 #include <sound/soc-dai.h>
14 struct snd_soc_card card;
18 struct axg_dai_link_tdm_mask {
23 struct axg_dai_link_tdm_data {
26 unsigned int slot_width;
29 struct axg_dai_link_tdm_mask *codec_masks;
32 #define PREFIX "amlogic,"
34 static int axg_card_reallocate_links(struct axg_card *priv,
35 unsigned int num_links)
37 struct snd_soc_dai_link *links;
40 links = krealloc(priv->card.dai_link,
41 num_links * sizeof(*priv->card.dai_link),
42 GFP_KERNEL | __GFP_ZERO);
43 ldata = krealloc(priv->link_data,
44 num_links * sizeof(*priv->link_data),
45 GFP_KERNEL | __GFP_ZERO);
47 if (!links || !ldata) {
48 dev_err(priv->card.dev, "failed to allocate links\n");
52 priv->card.dai_link = links;
53 priv->link_data = ldata;
54 priv->card.num_links = num_links;
58 static int axg_card_parse_dai(struct snd_soc_card *card,
59 struct device_node *node,
60 struct device_node **dai_of_node,
61 const char **dai_name)
63 struct of_phandle_args args;
66 if (!dai_name || !dai_of_node || !node)
69 ret = of_parse_phandle_with_args(node, "sound-dai",
70 "#sound-dai-cells", 0, &args);
72 if (ret != -EPROBE_DEFER)
73 dev_err(card->dev, "can't parse dai %d\n", ret);
76 *dai_of_node = args.np;
78 return snd_soc_get_dai_name(&args, dai_name);
81 static int axg_card_set_link_name(struct snd_soc_card *card,
82 struct snd_soc_dai_link *link,
85 char *name = devm_kasprintf(card->dev, GFP_KERNEL, "%s.%s",
86 prefix, link->cpu_of_node->full_name);
91 link->stream_name = name;
96 static void axg_card_clean_references(struct axg_card *priv)
98 struct snd_soc_card *card = &priv->card;
99 struct snd_soc_dai_link *link;
102 if (card->dai_link) {
103 for (i = 0; i < card->num_links; i++) {
104 link = &card->dai_link[i];
105 of_node_put(link->cpu_of_node);
106 for (j = 0; j < link->num_codecs; j++)
107 of_node_put(link->codecs[j].of_node);
112 for (i = 0; i < card->num_aux_devs; i++)
113 of_node_put(card->aux_dev[i].codec_of_node);
116 kfree(card->dai_link);
117 kfree(priv->link_data);
120 static int axg_card_add_aux_devices(struct snd_soc_card *card)
122 struct device_node *node = card->dev->of_node;
123 struct snd_soc_aux_dev *aux;
126 num = of_count_phandle_with_args(node, "audio-aux-devs", NULL);
127 if (num == -ENOENT) {
129 * It is ok to have no auxiliary devices but for this card it
130 * is a strange situtation. Let's warn the about it.
132 dev_warn(card->dev, "card has no auxiliary devices\n");
134 } else if (num < 0) {
135 dev_err(card->dev, "error getting auxiliary devices: %d\n",
140 aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
144 card->num_aux_devs = num;
146 for (i = 0; i < card->num_aux_devs; i++, aux++) {
148 of_parse_phandle(node, "audio-aux-devs", i);
149 if (!aux->codec_of_node)
156 static int axg_card_tdm_be_hw_params(struct snd_pcm_substream *substream,
157 struct snd_pcm_hw_params *params)
159 struct snd_soc_pcm_runtime *rtd = substream->private_data;
160 struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
161 struct axg_dai_link_tdm_data *be =
162 (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
163 struct snd_soc_dai *codec_dai;
168 mclk = params_rate(params) * be->mclk_fs;
170 for (i = 0; i < rtd->num_codecs; i++) {
171 codec_dai = rtd->codec_dais[i];
172 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
174 if (ret && ret != -ENOTSUPP)
178 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk,
180 if (ret && ret != -ENOTSUPP)
187 static const struct snd_soc_ops axg_card_tdm_be_ops = {
188 .hw_params = axg_card_tdm_be_hw_params,
191 static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime *rtd)
193 struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
194 struct axg_dai_link_tdm_data *be =
195 (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
196 struct snd_soc_dai *codec_dai;
199 for (i = 0; i < rtd->num_codecs; i++) {
200 codec_dai = rtd->codec_dais[i];
201 ret = snd_soc_dai_set_tdm_slot(codec_dai,
202 be->codec_masks[i].tx,
203 be->codec_masks[i].rx,
204 be->slots, be->slot_width);
205 if (ret && ret != -ENOTSUPP) {
206 dev_err(codec_dai->dev,
207 "setting tdm link slots failed\n");
212 ret = axg_tdm_set_tdm_slots(rtd->cpu_dai, be->tx_mask, be->rx_mask,
213 be->slots, be->slot_width);
215 dev_err(rtd->cpu_dai->dev, "setting tdm link slots failed\n");
222 static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime *rtd)
224 struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
225 struct axg_dai_link_tdm_data *be =
226 (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
229 /* The loopback rx_mask is the pad tx_mask */
230 ret = axg_tdm_set_tdm_slots(rtd->cpu_dai, NULL, be->tx_mask,
231 be->slots, be->slot_width);
233 dev_err(rtd->cpu_dai->dev, "setting tdm link slots failed\n");
240 static int axg_card_add_tdm_loopback(struct snd_soc_card *card,
243 struct axg_card *priv = snd_soc_card_get_drvdata(card);
244 struct snd_soc_dai_link *pad = &card->dai_link[*index];
245 struct snd_soc_dai_link *lb;
249 ret = axg_card_reallocate_links(priv, card->num_links + 1);
253 lb = &card->dai_link[*index + 1];
255 lb->name = kasprintf(GFP_KERNEL, "%s-lb", pad->name);
259 lb->stream_name = lb->name;
260 lb->cpu_of_node = pad->cpu_of_node;
261 lb->cpu_dai_name = "TDM Loopback";
262 lb->codec_name = "snd-soc-dummy";
263 lb->codec_dai_name = "snd-soc-dummy-dai";
264 lb->dpcm_capture = 1;
266 lb->ops = &axg_card_tdm_be_ops;
267 lb->init = axg_card_tdm_dai_lb_init;
269 /* Provide the same link data to the loopback */
270 priv->link_data[*index + 1] = priv->link_data[*index];
273 * axg_card_clean_references() will iterate over this link,
274 * make sure the node count is balanced
276 of_node_get(lb->cpu_of_node);
278 /* Let add_links continue where it should */
284 static unsigned int axg_card_parse_daifmt(struct device_node *node,
285 struct device_node *cpu_node)
287 struct device_node *bitclkmaster = NULL;
288 struct device_node *framemaster = NULL;
291 daifmt = snd_soc_of_parse_daifmt(node, PREFIX,
292 &bitclkmaster, &framemaster);
293 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
295 /* If no master is provided, default to cpu master */
296 if (!bitclkmaster || bitclkmaster == cpu_node) {
297 daifmt |= (!framemaster || framemaster == cpu_node) ?
298 SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBS_CFM;
300 daifmt |= (!framemaster || framemaster == cpu_node) ?
301 SND_SOC_DAIFMT_CBM_CFS : SND_SOC_DAIFMT_CBM_CFM;
304 of_node_put(bitclkmaster);
305 of_node_put(framemaster);
310 static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
311 struct snd_soc_dai_link *link,
312 struct device_node *node,
313 struct axg_dai_link_tdm_data *be)
319 be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
320 sizeof(*be->tx_mask), GFP_KERNEL);
321 be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
322 sizeof(*be->rx_mask), GFP_KERNEL);
323 if (!be->tx_mask || !be->rx_mask)
326 for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
327 snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
328 snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
329 tx = max(tx, be->tx_mask[i]);
332 /* Disable playback is the interface has no tx slots */
334 link->dpcm_playback = 0;
336 for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
337 snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
338 snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
339 rx = max(rx, be->rx_mask[i]);
342 /* Disable capture is the interface has no rx slots */
344 link->dpcm_capture = 0;
346 /* ... but the interface should at least have one of them */
348 dev_err(card->dev, "tdm link has no cpu slots\n");
352 of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
355 * If the slot number is not provided, set it such as it
356 * accommodates the largest mask
358 be->slots = fls(max(tx, rx));
359 } else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
361 * Error if the slots can't accommodate the largest mask or
362 * if it is just too big
364 dev_err(card->dev, "bad slot number\n");
368 of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
373 static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
374 struct snd_soc_dai_link *link,
375 struct device_node *node,
376 struct axg_dai_link_tdm_data *be)
378 struct axg_dai_link_tdm_mask *codec_mask;
379 struct device_node *np;
381 codec_mask = devm_kcalloc(card->dev, link->num_codecs,
382 sizeof(*codec_mask), GFP_KERNEL);
386 be->codec_masks = codec_mask;
388 for_each_child_of_node(node, np) {
389 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
391 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
400 static int axg_card_parse_tdm(struct snd_soc_card *card,
401 struct device_node *node,
404 struct axg_card *priv = snd_soc_card_get_drvdata(card);
405 struct snd_soc_dai_link *link = &card->dai_link[*index];
406 struct axg_dai_link_tdm_data *be;
409 /* Allocate tdm link parameters */
410 be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
413 priv->link_data[*index] = be;
416 link->ops = &axg_card_tdm_be_ops;
417 link->init = axg_card_tdm_dai_init;
418 link->dai_fmt = axg_card_parse_daifmt(node, link->cpu_of_node);
420 of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
422 ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
424 dev_err(card->dev, "error parsing tdm link slots\n");
428 ret = axg_card_parse_codecs_masks(card, link, node, be);
432 /* Add loopback if the pad dai has playback */
433 if (link->dpcm_playback) {
434 ret = axg_card_add_tdm_loopback(card, index);
442 static int axg_card_set_be_link(struct snd_soc_card *card,
443 struct snd_soc_dai_link *link,
444 struct device_node *node)
446 struct snd_soc_dai_link_component *codec;
447 struct device_node *np;
451 link->dpcm_playback = 1;
452 link->dpcm_capture = 1;
454 num_codecs = of_get_child_count(node);
456 dev_err(card->dev, "be link %s has no codec\n",
461 codec = devm_kcalloc(card->dev, num_codecs, sizeof(*codec), GFP_KERNEL);
465 link->codecs = codec;
466 link->num_codecs = num_codecs;
468 for_each_child_of_node(node, np) {
469 ret = axg_card_parse_dai(card, np, &codec->of_node,
479 ret = axg_card_set_link_name(card, link, "be");
481 dev_err(card->dev, "error setting %s link name\n", np->name);
486 static int axg_card_set_fe_link(struct snd_soc_card *card,
487 struct snd_soc_dai_link *link,
491 link->dpcm_merged_format = 1;
492 link->dpcm_merged_chan = 1;
493 link->dpcm_merged_rate = 1;
494 link->codec_dai_name = "snd-soc-dummy-dai";
495 link->codec_name = "snd-soc-dummy";
498 link->dpcm_playback = 1;
500 link->dpcm_capture = 1;
502 return axg_card_set_link_name(card, link, "fe");
505 static int axg_card_cpu_is_capture_fe(struct device_node *np)
507 return of_device_is_compatible(np, PREFIX "axg-toddr");
510 static int axg_card_cpu_is_playback_fe(struct device_node *np)
512 return of_device_is_compatible(np, PREFIX "axg-frddr");
515 static int axg_card_cpu_is_tdm_iface(struct device_node *np)
517 return of_device_is_compatible(np, PREFIX "axg-tdm-iface");
520 static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
523 struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
526 ret = axg_card_parse_dai(card, np, &dai_link->cpu_of_node,
527 &dai_link->cpu_dai_name);
531 if (axg_card_cpu_is_playback_fe(dai_link->cpu_of_node))
532 ret = axg_card_set_fe_link(card, dai_link, true);
533 else if (axg_card_cpu_is_capture_fe(dai_link->cpu_of_node))
534 ret = axg_card_set_fe_link(card, dai_link, false);
536 ret = axg_card_set_be_link(card, dai_link, np);
541 if (axg_card_cpu_is_tdm_iface(dai_link->cpu_of_node))
542 ret = axg_card_parse_tdm(card, np, index);
547 static int axg_card_add_links(struct snd_soc_card *card)
549 struct axg_card *priv = snd_soc_card_get_drvdata(card);
550 struct device_node *node = card->dev->of_node;
551 struct device_node *np;
554 num = of_get_child_count(node);
556 dev_err(card->dev, "card has no links\n");
560 ret = axg_card_reallocate_links(priv, num);
565 for_each_child_of_node(node, np) {
566 ret = axg_card_add_link(card, np, &i);
578 static int axg_card_parse_of_optional(struct snd_soc_card *card,
579 const char *propname,
580 int (*func)(struct snd_soc_card *c,
583 /* If property is not provided, don't fail ... */
584 if (!of_property_read_bool(card->dev->of_node, propname))
587 /* ... but do fail if it is provided and the parsing fails */
588 return func(card, propname);
591 static const struct of_device_id axg_card_of_match[] = {
592 { .compatible = "amlogic,axg-sound-card", },
595 MODULE_DEVICE_TABLE(of, axg_card_of_match);
597 static int axg_card_probe(struct platform_device *pdev)
599 struct device *dev = &pdev->dev;
600 struct axg_card *priv;
603 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
607 platform_set_drvdata(pdev, priv);
608 snd_soc_card_set_drvdata(&priv->card, priv);
610 priv->card.owner = THIS_MODULE;
611 priv->card.dev = dev;
613 ret = snd_soc_of_parse_card_name(&priv->card, "model");
617 ret = axg_card_parse_of_optional(&priv->card, "audio-routing",
618 snd_soc_of_parse_audio_routing);
620 dev_err(dev, "error while parsing routing\n");
624 ret = axg_card_parse_of_optional(&priv->card, "audio-widgets",
625 snd_soc_of_parse_audio_simple_widgets);
627 dev_err(dev, "error while parsing widgets\n");
631 ret = axg_card_add_links(&priv->card);
635 ret = axg_card_add_aux_devices(&priv->card);
639 ret = devm_snd_soc_register_card(dev, &priv->card);
646 axg_card_clean_references(priv);
650 static int axg_card_remove(struct platform_device *pdev)
652 struct axg_card *priv = platform_get_drvdata(pdev);
654 axg_card_clean_references(priv);
659 static struct platform_driver axg_card_pdrv = {
660 .probe = axg_card_probe,
661 .remove = axg_card_remove,
663 .name = "axg-sound-card",
664 .of_match_table = axg_card_of_match,
667 module_platform_driver(axg_card_pdrv);
669 MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
670 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
671 MODULE_LICENSE("GPL v2");