blob: 6c20bdd850f3319ab3b9b93c44bf4f873692b1dc [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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
8int 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;
David Brazdil0f672f62019-12-10 10:32:29 +000017 struct snd_soc_dai_link_component *dlc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018 int ret, num_links;
19
20 ret = snd_soc_of_parse_card_name(card, "model");
21 if (ret) {
22 dev_err(dev, "Error parsing card name: %d\n", ret);
23 return ret;
24 }
25
26 /* DAPM routes */
27 if (of_property_read_bool(dev->of_node, "audio-routing")) {
28 ret = snd_soc_of_parse_audio_routing(card,
29 "audio-routing");
30 if (ret)
31 return ret;
32 }
33
34 /* Populate links */
35 num_links = of_get_child_count(dev->of_node);
36
37 /* Allocate the DAI link array */
38 card->dai_link = kcalloc(num_links, sizeof(*link), GFP_KERNEL);
39 if (!card->dai_link)
40 return -ENOMEM;
41
42 card->num_links = num_links;
43 link = card->dai_link;
David Brazdil0f672f62019-12-10 10:32:29 +000044
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045 for_each_child_of_node(dev->of_node, np) {
David Brazdil0f672f62019-12-10 10:32:29 +000046 dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
47 if (!dlc)
48 return -ENOMEM;
49
50 link->cpus = &dlc[0];
51 link->platforms = &dlc[1];
52
53 link->num_cpus = 1;
54 link->num_platforms = 1;
55
56 ret = of_property_read_string(np, "link-name", &link->name);
57 if (ret) {
58 dev_err(card->dev, "error getting codec dai_link name\n");
59 goto err;
60 }
61
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062 cpu = of_get_child_by_name(np, "cpu");
David Brazdil0f672f62019-12-10 10:32:29 +000063 platform = of_get_child_by_name(np, "platform");
64 codec = of_get_child_by_name(np, "codec");
65
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066 if (!cpu) {
David Brazdil0f672f62019-12-10 10:32:29 +000067 dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068 ret = -EINVAL;
69 goto err;
70 }
71
72 ret = of_parse_phandle_with_args(cpu, "sound-dai",
73 "#sound-dai-cells", 0, &args);
74 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +000075 dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 goto err;
77 }
David Brazdil0f672f62019-12-10 10:32:29 +000078 link->cpus->of_node = args.np;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079 link->id = args.args[0];
80
David Brazdil0f672f62019-12-10 10:32:29 +000081 ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082 if (ret) {
David Brazdil0f672f62019-12-10 10:32:29 +000083 dev_err(card->dev, "%s: error getting cpu dai name\n", link->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 goto err;
85 }
86
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 if (codec && platform) {
David Brazdil0f672f62019-12-10 10:32:29 +000088 link->platforms->of_node = of_parse_phandle(platform,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089 "sound-dai",
90 0);
David Brazdil0f672f62019-12-10 10:32:29 +000091 if (!link->platforms->of_node) {
92 dev_err(card->dev, "%s: platform dai not found\n", link->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093 ret = -EINVAL;
94 goto err;
95 }
96
97 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
98 if (ret < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +000099 dev_err(card->dev, "%s: codec dai not found\n", link->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100 goto err;
101 }
102 link->no_pcm = 1;
103 link->ignore_pmdown_time = 1;
104 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000105 dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
106 if (!dlc)
107 return -ENOMEM;
108
109 link->codecs = dlc;
110 link->num_codecs = 1;
111
112 link->platforms->of_node = link->cpus->of_node;
113 link->codecs->dai_name = "snd-soc-dummy-dai";
114 link->codecs->name = "snd-soc-dummy";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115 link->dynamic = 1;
116 }
117
118 link->ignore_suspend = 1;
David Brazdil0f672f62019-12-10 10:32:29 +0000119 link->nonatomic = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 link->dpcm_playback = 1;
121 link->dpcm_capture = 1;
122 link->stream_name = link->name;
123 link++;
David Brazdil0f672f62019-12-10 10:32:29 +0000124
125 of_node_put(cpu);
126 of_node_put(codec);
127 of_node_put(platform);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 }
129
130 return 0;
131err:
David Brazdil0f672f62019-12-10 10:32:29 +0000132 of_node_put(np);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133 of_node_put(cpu);
134 of_node_put(codec);
135 of_node_put(platform);
136 kfree(card->dai_link);
137 return ret;
138}
139EXPORT_SYMBOL(qcom_snd_parse_of);
140
141MODULE_LICENSE("GPL v2");