xref: /openbmc/linux/sound/soc/qcom/common.c (revision 2f3092e7)
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 			link->codecs	 = &asoc_dummy_dlc;
144 			link->num_codecs = 1;
145 			link->dynamic = 1;
146 		}
147 
148 		if (platform || !codec) {
149 			/* DPCM */
150 			snd_soc_dai_link_set_capabilities(link);
151 			link->ignore_suspend = 1;
152 			link->nonatomic = 1;
153 		}
154 
155 		link->stream_name = link->name;
156 		link++;
157 
158 		of_node_put(cpu);
159 		of_node_put(codec);
160 		of_node_put(platform);
161 	}
162 
163 	return 0;
164 err:
165 	of_node_put(cpu);
166 	of_node_put(codec);
167 	of_node_put(platform);
168 err_put_np:
169 	of_node_put(np);
170 	return ret;
171 }
172 EXPORT_SYMBOL_GPL(qcom_snd_parse_of);
173 
174 static struct snd_soc_jack_pin qcom_headset_jack_pins[] = {
175 	/* Headset */
176 	{
177 		.pin = "Mic Jack",
178 		.mask = SND_JACK_MICROPHONE,
179 	},
180 	{
181 		.pin = "Headphone Jack",
182 		.mask = SND_JACK_HEADPHONE,
183 	},
184 };
185 
186 int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
187 			    struct snd_soc_jack *jack, bool *jack_setup)
188 {
189 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
190 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
191 	struct snd_soc_card *card = rtd->card;
192 	int rval, i;
193 
194 	if (!*jack_setup) {
195 		rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
196 					     SND_JACK_HEADSET | SND_JACK_LINEOUT |
197 					     SND_JACK_MECHANICAL |
198 					     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
199 					     SND_JACK_BTN_2 | SND_JACK_BTN_3 |
200 					     SND_JACK_BTN_4 | SND_JACK_BTN_5,
201 					     jack, qcom_headset_jack_pins,
202 					     ARRAY_SIZE(qcom_headset_jack_pins));
203 
204 		if (rval < 0) {
205 			dev_err(card->dev, "Unable to add Headphone Jack\n");
206 			return rval;
207 		}
208 
209 		snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
210 		snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
211 		snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
212 		snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
213 		*jack_setup = true;
214 	}
215 
216 	switch (cpu_dai->id) {
217 	case TX_CODEC_DMA_TX_0:
218 	case TX_CODEC_DMA_TX_1:
219 	case TX_CODEC_DMA_TX_2:
220 	case TX_CODEC_DMA_TX_3:
221 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
222 			rval = snd_soc_component_set_jack(codec_dai->component,
223 							  jack, NULL);
224 			if (rval != 0 && rval != -ENOTSUPP) {
225 				dev_warn(card->dev, "Failed to set jack: %d\n", rval);
226 				return rval;
227 			}
228 		}
229 
230 		break;
231 	default:
232 		break;
233 	}
234 
235 
236 	return 0;
237 }
238 EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup);
239 MODULE_LICENSE("GPL v2");
240