1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2018, Linaro Limited. 3 // Copyright (c) 2018, The Linux Foundation. All rights reserved. 4 5 #include <linux/module.h> 6 #include "common.h" 7 8 int qcom_snd_parse_of(struct snd_soc_card *card) 9 { 10 struct device_node *np; 11 struct device_node *codec = NULL; 12 struct device_node *platform = NULL; 13 struct device_node *cpu = NULL; 14 struct device *dev = card->dev; 15 struct snd_soc_dai_link *link; 16 struct of_phandle_args args; 17 int ret, num_links; 18 19 ret = snd_soc_of_parse_card_name(card, "model"); 20 if (ret) { 21 dev_err(dev, "Error parsing card name: %d\n", ret); 22 return ret; 23 } 24 25 /* DAPM routes */ 26 if (of_property_read_bool(dev->of_node, "audio-routing")) { 27 ret = snd_soc_of_parse_audio_routing(card, 28 "audio-routing"); 29 if (ret) 30 return ret; 31 } 32 33 /* Populate links */ 34 num_links = of_get_child_count(dev->of_node); 35 36 /* Allocate the DAI link array */ 37 card->dai_link = kcalloc(num_links, sizeof(*link), GFP_KERNEL); 38 if (!card->dai_link) 39 return -ENOMEM; 40 41 card->num_links = num_links; 42 link = card->dai_link; 43 for_each_child_of_node(dev->of_node, np) { 44 cpu = of_get_child_by_name(np, "cpu"); 45 if (!cpu) { 46 dev_err(dev, "Can't find cpu DT node\n"); 47 ret = -EINVAL; 48 goto err; 49 } 50 51 ret = of_parse_phandle_with_args(cpu, "sound-dai", 52 "#sound-dai-cells", 0, &args); 53 if (ret) { 54 dev_err(card->dev, "error getting cpu phandle\n"); 55 goto err; 56 } 57 link->cpu_of_node = args.np; 58 link->id = args.args[0]; 59 60 ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name); 61 if (ret) { 62 dev_err(card->dev, "error getting cpu dai name\n"); 63 goto err; 64 } 65 66 platform = of_get_child_by_name(np, "platform"); 67 codec = of_get_child_by_name(np, "codec"); 68 if (codec && platform) { 69 link->platform_of_node = of_parse_phandle(platform, 70 "sound-dai", 71 0); 72 if (!link->platform_of_node) { 73 dev_err(card->dev, "platform dai not found\n"); 74 ret = -EINVAL; 75 goto err; 76 } 77 78 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); 79 if (ret < 0) { 80 dev_err(card->dev, "codec dai not found\n"); 81 goto err; 82 } 83 link->no_pcm = 1; 84 link->ignore_pmdown_time = 1; 85 } else { 86 link->platform_of_node = link->cpu_of_node; 87 link->codec_dai_name = "snd-soc-dummy-dai"; 88 link->codec_name = "snd-soc-dummy"; 89 link->dynamic = 1; 90 } 91 92 link->ignore_suspend = 1; 93 ret = of_property_read_string(np, "link-name", &link->name); 94 if (ret) { 95 dev_err(card->dev, "error getting codec dai_link name\n"); 96 goto err; 97 } 98 99 link->dpcm_playback = 1; 100 link->dpcm_capture = 1; 101 link->stream_name = link->name; 102 link++; 103 } 104 105 return 0; 106 err: 107 of_node_put(cpu); 108 of_node_put(codec); 109 of_node_put(platform); 110 kfree(card->dai_link); 111 return ret; 112 } 113 EXPORT_SYMBOL(qcom_snd_parse_of); 114 115 MODULE_LICENSE("GPL v2"); 116