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 #include "qdsp6/q6afe.h" 8 9 int qcom_snd_parse_of(struct snd_soc_card *card) 10 { 11 struct device_node *np; 12 struct device_node *codec = NULL; 13 struct device_node *platform = NULL; 14 struct device_node *cpu = NULL; 15 struct device *dev = card->dev; 16 struct snd_soc_dai_link *link; 17 struct of_phandle_args args; 18 struct snd_soc_dai_link_component *dlc; 19 int ret, num_links; 20 21 ret = snd_soc_of_parse_card_name(card, "model"); 22 if (ret) { 23 dev_err(dev, "Error parsing card name: %d\n", ret); 24 return ret; 25 } 26 27 /* DAPM routes */ 28 if (of_property_read_bool(dev->of_node, "audio-routing")) { 29 ret = snd_soc_of_parse_audio_routing(card, 30 "audio-routing"); 31 if (ret) 32 return ret; 33 } 34 35 /* Populate links */ 36 num_links = of_get_child_count(dev->of_node); 37 38 /* Allocate the DAI link array */ 39 card->dai_link = kcalloc(num_links, sizeof(*link), GFP_KERNEL); 40 if (!card->dai_link) 41 return -ENOMEM; 42 43 card->num_links = num_links; 44 link = card->dai_link; 45 46 for_each_child_of_node(dev->of_node, np) { 47 dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL); 48 if (!dlc) 49 return -ENOMEM; 50 51 link->cpus = &dlc[0]; 52 link->platforms = &dlc[1]; 53 54 link->num_cpus = 1; 55 link->num_platforms = 1; 56 57 ret = of_property_read_string(np, "link-name", &link->name); 58 if (ret) { 59 dev_err(card->dev, "error getting codec dai_link name\n"); 60 goto err; 61 } 62 63 cpu = of_get_child_by_name(np, "cpu"); 64 platform = of_get_child_by_name(np, "platform"); 65 codec = of_get_child_by_name(np, "codec"); 66 67 if (!cpu) { 68 dev_err(dev, "%s: Can't find cpu DT node\n", link->name); 69 ret = -EINVAL; 70 goto err; 71 } 72 73 ret = of_parse_phandle_with_args(cpu, "sound-dai", 74 "#sound-dai-cells", 0, &args); 75 if (ret) { 76 dev_err(card->dev, "%s: error getting cpu phandle\n", link->name); 77 goto err; 78 } 79 link->cpus->of_node = args.np; 80 link->id = args.args[0]; 81 82 ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name); 83 if (ret) { 84 dev_err(card->dev, "%s: error getting cpu dai name\n", link->name); 85 goto err; 86 } 87 88 if (codec && platform) { 89 link->platforms->of_node = of_parse_phandle(platform, 90 "sound-dai", 91 0); 92 if (!link->platforms->of_node) { 93 dev_err(card->dev, "%s: platform dai not found\n", link->name); 94 ret = -EINVAL; 95 goto err; 96 } 97 98 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); 99 if (ret < 0) { 100 dev_err(card->dev, "%s: codec dai not found\n", link->name); 101 goto err; 102 } 103 link->no_pcm = 1; 104 link->ignore_pmdown_time = 1; 105 106 if (q6afe_is_rx_port(link->id)) { 107 link->dpcm_playback = 1; 108 link->dpcm_capture = 0; 109 } else { 110 link->dpcm_playback = 0; 111 link->dpcm_capture = 1; 112 } 113 114 } else { 115 dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL); 116 if (!dlc) 117 return -ENOMEM; 118 119 link->codecs = dlc; 120 link->num_codecs = 1; 121 122 link->platforms->of_node = link->cpus->of_node; 123 link->codecs->dai_name = "snd-soc-dummy-dai"; 124 link->codecs->name = "snd-soc-dummy"; 125 link->dynamic = 1; 126 link->dpcm_playback = 1; 127 link->dpcm_capture = 1; 128 } 129 130 link->ignore_suspend = 1; 131 link->nonatomic = 1; 132 link->stream_name = link->name; 133 link++; 134 135 of_node_put(cpu); 136 of_node_put(codec); 137 of_node_put(platform); 138 } 139 140 return 0; 141 err: 142 of_node_put(np); 143 of_node_put(cpu); 144 of_node_put(codec); 145 of_node_put(platform); 146 kfree(card->dai_link); 147 return ret; 148 } 149 EXPORT_SYMBOL(qcom_snd_parse_of); 150 151 MODULE_LICENSE("GPL v2"); 152