Merge branches 'pm-cpuidle', 'pm-core' and 'pm-sleep'
[linux-2.6-microblaze.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 <sound/jack.h>
7 #include <linux/input-event-codes.h>
8 #include "qdsp6/q6afe.h"
9 #include "common.h"
10
11 int qcom_snd_parse_of(struct snd_soc_card *card)
12 {
13         struct device_node *np;
14         struct device_node *codec = NULL;
15         struct device_node *platform = NULL;
16         struct device_node *cpu = NULL;
17         struct device *dev = card->dev;
18         struct snd_soc_dai_link *link;
19         struct of_phandle_args args;
20         struct snd_soc_dai_link_component *dlc;
21         int ret, num_links;
22
23         ret = snd_soc_of_parse_card_name(card, "model");
24         if (ret == 0 && !card->name)
25                 /* Deprecated, only for compatibility with old device trees */
26                 ret = snd_soc_of_parse_card_name(card, "qcom,model");
27         if (ret) {
28                 dev_err(dev, "Error parsing card name: %d\n", ret);
29                 return ret;
30         }
31
32         if (of_property_read_bool(dev->of_node, "widgets")) {
33                 ret = snd_soc_of_parse_audio_simple_widgets(card, "widgets");
34                 if (ret)
35                         return ret;
36         }
37
38         /* DAPM routes */
39         if (of_property_read_bool(dev->of_node, "audio-routing")) {
40                 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
41                 if (ret)
42                         return ret;
43         }
44         /* Deprecated, only for compatibility with old device trees */
45         if (of_property_read_bool(dev->of_node, "qcom,audio-routing")) {
46                 ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing");
47                 if (ret)
48                         return ret;
49         }
50
51         ret = snd_soc_of_parse_pin_switches(card, "pin-switches");
52         if (ret)
53                 return ret;
54
55         ret = snd_soc_of_parse_aux_devs(card, "aux-devs");
56         if (ret)
57                 return ret;
58
59         /* Populate links */
60         num_links = of_get_available_child_count(dev->of_node);
61
62         /* Allocate the DAI link array */
63         card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
64         if (!card->dai_link)
65                 return -ENOMEM;
66
67         card->num_links = num_links;
68         link = card->dai_link;
69
70         for_each_available_child_of_node(dev->of_node, np) {
71                 dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
72                 if (!dlc) {
73                         ret = -ENOMEM;
74                         goto err_put_np;
75                 }
76
77                 link->cpus      = &dlc[0];
78                 link->platforms = &dlc[1];
79
80                 link->num_cpus          = 1;
81                 link->num_platforms     = 1;
82
83                 ret = of_property_read_string(np, "link-name", &link->name);
84                 if (ret) {
85                         dev_err(card->dev, "error getting codec dai_link name\n");
86                         goto err_put_np;
87                 }
88
89                 cpu = of_get_child_by_name(np, "cpu");
90                 platform = of_get_child_by_name(np, "platform");
91                 codec = of_get_child_by_name(np, "codec");
92
93                 if (!cpu) {
94                         dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
95                         ret = -EINVAL;
96                         goto err;
97                 }
98
99                 ret = of_parse_phandle_with_args(cpu, "sound-dai",
100                                         "#sound-dai-cells", 0, &args);
101                 if (ret) {
102                         dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
103                         goto err;
104                 }
105                 link->cpus->of_node = args.np;
106                 link->id = args.args[0];
107
108                 ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
109                 if (ret) {
110                         dev_err_probe(card->dev, ret,
111                                       "%s: error getting cpu dai name\n", link->name);
112                         goto err;
113                 }
114
115                 if (platform) {
116                         link->platforms->of_node = of_parse_phandle(platform,
117                                         "sound-dai",
118                                         0);
119                         if (!link->platforms->of_node) {
120                                 dev_err(card->dev, "%s: platform dai not found\n", link->name);
121                                 ret = -EINVAL;
122                                 goto err;
123                         }
124                 } else {
125                         link->platforms->of_node = link->cpus->of_node;
126                 }
127
128                 if (codec) {
129                         ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
130                         if (ret < 0) {
131                                 dev_err_probe(card->dev, ret,
132                                               "%s: codec dai not found\n", link->name);
133                                 goto err;
134                         }
135
136                         if (platform) {
137                                 /* DPCM backend */
138                                 link->no_pcm = 1;
139                                 link->ignore_pmdown_time = 1;
140                         }
141                 } else {
142                         /* DPCM frontend */
143                         dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
144                         if (!dlc) {
145                                 ret = -ENOMEM;
146                                 goto err;
147                         }
148
149                         link->codecs     = dlc;
150                         link->num_codecs = 1;
151
152                         link->codecs->dai_name = "snd-soc-dummy-dai";
153                         link->codecs->name = "snd-soc-dummy";
154                         link->dynamic = 1;
155                 }
156
157                 if (platform || !codec) {
158                         /* DPCM */
159                         snd_soc_dai_link_set_capabilities(link);
160                         link->ignore_suspend = 1;
161                         link->nonatomic = 1;
162                 }
163
164                 link->stream_name = link->name;
165                 link++;
166
167                 of_node_put(cpu);
168                 of_node_put(codec);
169                 of_node_put(platform);
170         }
171
172         return 0;
173 err:
174         of_node_put(cpu);
175         of_node_put(codec);
176         of_node_put(platform);
177 err_put_np:
178         of_node_put(np);
179         return ret;
180 }
181 EXPORT_SYMBOL_GPL(qcom_snd_parse_of);
182
183 int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
184                             struct snd_soc_jack *jack, bool *jack_setup)
185 {
186         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
187         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
188         struct snd_soc_card *card = rtd->card;
189         int rval, i;
190
191         if (!*jack_setup) {
192                 rval = snd_soc_card_jack_new(card, "Headset Jack",
193                                              SND_JACK_HEADSET | SND_JACK_LINEOUT |
194                                              SND_JACK_MECHANICAL |
195                                              SND_JACK_BTN_0 | SND_JACK_BTN_1 |
196                                              SND_JACK_BTN_2 | SND_JACK_BTN_3 |
197                                              SND_JACK_BTN_4 | SND_JACK_BTN_5,
198                                              jack);
199
200                 if (rval < 0) {
201                         dev_err(card->dev, "Unable to add Headphone Jack\n");
202                         return rval;
203                 }
204
205                 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
206                 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
207                 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
208                 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
209                 *jack_setup = true;
210         }
211
212         switch (cpu_dai->id) {
213         case TX_CODEC_DMA_TX_0:
214         case TX_CODEC_DMA_TX_1:
215         case TX_CODEC_DMA_TX_2:
216         case TX_CODEC_DMA_TX_3:
217                 for_each_rtd_codec_dais(rtd, i, codec_dai) {
218                         rval = snd_soc_component_set_jack(codec_dai->component,
219                                                           jack, NULL);
220                         if (rval != 0 && rval != -ENOTSUPP) {
221                                 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
222                                 return rval;
223                         }
224                 }
225
226                 break;
227         default:
228                 break;
229         }
230
231
232         return 0;
233 }
234 EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup);
235 MODULE_LICENSE("GPL v2");