GNU Linux-libre 4.19.264-gnu1
[releases.git] / sound / soc / qcom / common.c
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                 platform = of_get_child_by_name(np, "platform");
46                 codec = of_get_child_by_name(np, "codec");
47
48                 if (!cpu) {
49                         dev_err(dev, "Can't find cpu DT node\n");
50                         ret = -EINVAL;
51                         goto err;
52                 }
53
54                 ret = of_parse_phandle_with_args(cpu, "sound-dai",
55                                         "#sound-dai-cells", 0, &args);
56                 if (ret) {
57                         dev_err(card->dev, "error getting cpu phandle\n");
58                         goto err;
59                 }
60                 link->cpu_of_node = args.np;
61                 link->id = args.args[0];
62
63                 ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
64                 if (ret) {
65                         dev_err(card->dev, "error getting cpu dai name\n");
66                         goto err;
67                 }
68
69                 if (codec && platform) {
70                         link->platform_of_node = of_parse_phandle(platform,
71                                         "sound-dai",
72                                         0);
73                         if (!link->platform_of_node) {
74                                 dev_err(card->dev, "platform dai not found\n");
75                                 ret = -EINVAL;
76                                 goto err;
77                         }
78
79                         ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
80                         if (ret < 0) {
81                                 dev_err(card->dev, "codec dai not found\n");
82                                 goto err;
83                         }
84                         link->no_pcm = 1;
85                         link->ignore_pmdown_time = 1;
86                 } else {
87                         link->platform_of_node = link->cpu_of_node;
88                         link->codec_dai_name = "snd-soc-dummy-dai";
89                         link->codec_name = "snd-soc-dummy";
90                         link->dynamic = 1;
91                 }
92
93                 link->ignore_suspend = 1;
94                 ret = of_property_read_string(np, "link-name", &link->name);
95                 if (ret) {
96                         dev_err(card->dev, "error getting codec dai_link name\n");
97                         goto err;
98                 }
99
100                 link->dpcm_playback = 1;
101                 link->dpcm_capture = 1;
102                 link->stream_name = link->name;
103                 link++;
104
105                 of_node_put(cpu);
106                 of_node_put(codec);
107                 of_node_put(platform);
108         }
109
110         return 0;
111 err:
112         of_node_put(np);
113         of_node_put(cpu);
114         of_node_put(codec);
115         of_node_put(platform);
116         kfree(card->dai_link);
117         return ret;
118 }
119 EXPORT_SYMBOL(qcom_snd_parse_of);
120
121 MODULE_LICENSE("GPL v2");