Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 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; |
Rohit kumar | 67fd143 | 2018-11-08 19:11:40 +0530 | [diff] [blame] | 16 | struct of_phandle_args args; |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 17 | struct snd_soc_dai_link_component *dlc; |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 18 | int ret, num_links; |
| 19 | |
| 20 | ret = snd_soc_of_parse_card_name(card, "model"); |
Stephan Gerhold | f0d67fd | 2020-07-23 20:39:02 +0200 | [diff] [blame] | 21 | if (ret == 0 && !card->name) |
| 22 | /* Deprecated, only for compatibility with old device trees */ |
| 23 | ret = snd_soc_of_parse_card_name(card, "qcom,model"); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 24 | if (ret) { |
| 25 | dev_err(dev, "Error parsing card name: %d\n", ret); |
| 26 | return ret; |
| 27 | } |
| 28 | |
| 29 | /* DAPM routes */ |
| 30 | if (of_property_read_bool(dev->of_node, "audio-routing")) { |
Stephan Gerhold | f0d67fd | 2020-07-23 20:39:02 +0200 | [diff] [blame] | 31 | ret = snd_soc_of_parse_audio_routing(card, "audio-routing"); |
| 32 | if (ret) |
| 33 | return ret; |
| 34 | } |
| 35 | /* Deprecated, only for compatibility with old device trees */ |
| 36 | if (of_property_read_bool(dev->of_node, "qcom,audio-routing")) { |
| 37 | ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing"); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 38 | if (ret) |
| 39 | return ret; |
| 40 | } |
| 41 | |
Stephan Gerhold | 1b839d3 | 2020-08-26 11:51:41 +0200 | [diff] [blame] | 42 | ret = snd_soc_of_parse_aux_devs(card, "aux-devs"); |
| 43 | if (ret) |
| 44 | return ret; |
| 45 | |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 46 | /* Populate links */ |
Stephan Gerhold | 4b29d5a | 2021-10-25 12:55:03 +0200 | [diff] [blame] | 47 | num_links = of_get_available_child_count(dev->of_node); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 48 | |
| 49 | /* Allocate the DAI link array */ |
Stephan Gerhold | ed3b53e | 2020-07-23 20:38:58 +0200 | [diff] [blame] | 50 | card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 51 | if (!card->dai_link) |
| 52 | return -ENOMEM; |
| 53 | |
| 54 | card->num_links = num_links; |
| 55 | link = card->dai_link; |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 56 | |
Stephan Gerhold | 4b29d5a | 2021-10-25 12:55:03 +0200 | [diff] [blame] | 57 | for_each_available_child_of_node(dev->of_node, np) { |
Bjorn Andersson | 16395ce | 2019-06-17 22:28:13 -0700 | [diff] [blame] | 58 | dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL); |
Dinghao Liu | c1e6414 | 2020-08-20 12:28:27 +0800 | [diff] [blame] | 59 | if (!dlc) { |
| 60 | ret = -ENOMEM; |
Dan Carpenter | 4e59dd2 | 2020-11-05 15:51:54 +0300 | [diff] [blame] | 61 | goto err_put_np; |
Dinghao Liu | c1e6414 | 2020-08-20 12:28:27 +0800 | [diff] [blame] | 62 | } |
Bjorn Andersson | 16395ce | 2019-06-17 22:28:13 -0700 | [diff] [blame] | 63 | |
| 64 | link->cpus = &dlc[0]; |
| 65 | link->platforms = &dlc[1]; |
| 66 | |
| 67 | link->num_cpus = 1; |
| 68 | link->num_platforms = 1; |
| 69 | |
Bjorn Andersson | 1ab1b34 | 2019-09-04 21:03:06 -0700 | [diff] [blame] | 70 | ret = of_property_read_string(np, "link-name", &link->name); |
| 71 | if (ret) { |
| 72 | dev_err(card->dev, "error getting codec dai_link name\n"); |
Dan Carpenter | 4e59dd2 | 2020-11-05 15:51:54 +0300 | [diff] [blame] | 73 | goto err_put_np; |
Bjorn Andersson | 1ab1b34 | 2019-09-04 21:03:06 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 76 | cpu = of_get_child_by_name(np, "cpu"); |
Takashi Iwai | 70b7732 | 2019-02-19 16:46:51 +0100 | [diff] [blame] | 77 | platform = of_get_child_by_name(np, "platform"); |
| 78 | codec = of_get_child_by_name(np, "codec"); |
| 79 | |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 80 | if (!cpu) { |
Bjorn Andersson | 1ab1b34 | 2019-09-04 21:03:06 -0700 | [diff] [blame] | 81 | dev_err(dev, "%s: Can't find cpu DT node\n", link->name); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 82 | ret = -EINVAL; |
| 83 | goto err; |
| 84 | } |
| 85 | |
Rohit kumar | 67fd143 | 2018-11-08 19:11:40 +0530 | [diff] [blame] | 86 | ret = of_parse_phandle_with_args(cpu, "sound-dai", |
| 87 | "#sound-dai-cells", 0, &args); |
| 88 | if (ret) { |
Bjorn Andersson | 1ab1b34 | 2019-09-04 21:03:06 -0700 | [diff] [blame] | 89 | dev_err(card->dev, "%s: error getting cpu phandle\n", link->name); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 90 | goto err; |
| 91 | } |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 92 | link->cpus->of_node = args.np; |
Rohit kumar | 67fd143 | 2018-11-08 19:11:40 +0530 | [diff] [blame] | 93 | link->id = args.args[0]; |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 94 | |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 95 | ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 96 | if (ret) { |
Stephan Gerhold | a63419b | 2020-07-23 20:39:04 +0200 | [diff] [blame] | 97 | if (ret != -EPROBE_DEFER) |
| 98 | dev_err(card->dev, "%s: error getting cpu dai name: %d\n", |
| 99 | link->name, ret); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 100 | goto err; |
| 101 | } |
| 102 | |
Stephan Gerhold | 47ea884 | 2020-07-23 20:39:01 +0200 | [diff] [blame] | 103 | if (platform) { |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 104 | link->platforms->of_node = of_parse_phandle(platform, |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 105 | "sound-dai", |
| 106 | 0); |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 107 | if (!link->platforms->of_node) { |
Bjorn Andersson | 1ab1b34 | 2019-09-04 21:03:06 -0700 | [diff] [blame] | 108 | dev_err(card->dev, "%s: platform dai not found\n", link->name); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 109 | ret = -EINVAL; |
| 110 | goto err; |
| 111 | } |
Stephan Gerhold | 47ea884 | 2020-07-23 20:39:01 +0200 | [diff] [blame] | 112 | } else { |
| 113 | link->platforms->of_node = link->cpus->of_node; |
| 114 | } |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 115 | |
Stephan Gerhold | 47ea884 | 2020-07-23 20:39:01 +0200 | [diff] [blame] | 116 | if (codec) { |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 117 | ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); |
| 118 | if (ret < 0) { |
Stephan Gerhold | a63419b | 2020-07-23 20:39:04 +0200 | [diff] [blame] | 119 | if (ret != -EPROBE_DEFER) |
| 120 | dev_err(card->dev, "%s: codec dai not found: %d\n", |
| 121 | link->name, ret); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 122 | goto err; |
| 123 | } |
Stephan Gerhold | 47ea884 | 2020-07-23 20:39:01 +0200 | [diff] [blame] | 124 | |
| 125 | if (platform) { |
| 126 | /* DPCM backend */ |
| 127 | link->no_pcm = 1; |
| 128 | link->ignore_pmdown_time = 1; |
| 129 | } |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 130 | } else { |
Stephan Gerhold | 47ea884 | 2020-07-23 20:39:01 +0200 | [diff] [blame] | 131 | /* DPCM frontend */ |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 132 | dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL); |
Dan Carpenter | 4e59dd2 | 2020-11-05 15:51:54 +0300 | [diff] [blame] | 133 | if (!dlc) { |
| 134 | ret = -ENOMEM; |
| 135 | goto err; |
| 136 | } |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 137 | |
| 138 | link->codecs = dlc; |
| 139 | link->num_codecs = 1; |
| 140 | |
Kuninori Morimoto | 1e36ea3 | 2019-06-06 13:16:58 +0900 | [diff] [blame] | 141 | link->codecs->dai_name = "snd-soc-dummy-dai"; |
| 142 | link->codecs->name = "snd-soc-dummy"; |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 143 | link->dynamic = 1; |
| 144 | } |
| 145 | |
Stephan Gerhold | 47ea884 | 2020-07-23 20:39:01 +0200 | [diff] [blame] | 146 | if (platform || !codec) { |
| 147 | /* DPCM */ |
| 148 | snd_soc_dai_link_set_capabilities(link); |
| 149 | link->ignore_suspend = 1; |
| 150 | link->nonatomic = 1; |
| 151 | } |
| 152 | |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 153 | link->stream_name = link->name; |
| 154 | link++; |
Takashi Iwai | 70b7732 | 2019-02-19 16:46:51 +0100 | [diff] [blame] | 155 | |
| 156 | of_node_put(cpu); |
| 157 | of_node_put(codec); |
| 158 | of_node_put(platform); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | err: |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 163 | of_node_put(cpu); |
| 164 | of_node_put(codec); |
| 165 | of_node_put(platform); |
Dan Carpenter | 4e59dd2 | 2020-11-05 15:51:54 +0300 | [diff] [blame] | 166 | err_put_np: |
| 167 | of_node_put(np); |
Rohit kumar | c25e295c | 2018-08-01 14:31:08 +0530 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | EXPORT_SYMBOL(qcom_snd_parse_of); |
| 171 | |
| 172 | MODULE_LICENSE("GPL v2"); |