xref: /linux/sound/soc/qcom/common.c (revision f3caa0b02455409eec4673ddd8df72d8bcad4e98)
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 <dt-bindings/sound/qcom,q6afe.h>
6 #include <linux/module.h>
7 #include <sound/jack.h>
8 #include <linux/input-event-codes.h>
9 #include "common.h"
10 
11 #define NAME_SIZE	32
12 
13 static const struct snd_soc_dapm_widget qcom_jack_snd_widgets[] = {
14 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
15 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
16 	SND_SOC_DAPM_SPK("DP0 Jack", NULL),
17 	SND_SOC_DAPM_SPK("DP1 Jack", NULL),
18 	SND_SOC_DAPM_SPK("DP2 Jack", NULL),
19 	SND_SOC_DAPM_SPK("DP3 Jack", NULL),
20 	SND_SOC_DAPM_SPK("DP4 Jack", NULL),
21 	SND_SOC_DAPM_SPK("DP5 Jack", NULL),
22 	SND_SOC_DAPM_SPK("DP6 Jack", NULL),
23 	SND_SOC_DAPM_SPK("DP7 Jack", NULL),
24 };
25 
26 int qcom_snd_parse_of(struct snd_soc_card *card)
27 {
28 	struct device *dev = card->dev;
29 	struct snd_soc_dai_link *link;
30 	struct of_phandle_args args;
31 	struct snd_soc_dai_link_component *dlc;
32 	int ret, num_links;
33 
34 	ret = snd_soc_of_parse_card_name(card, "model");
35 	if (ret == 0 && !card->name)
36 		/* Deprecated, only for compatibility with old device trees */
37 		ret = snd_soc_of_parse_card_name(card, "qcom,model");
38 	if (ret) {
39 		dev_err(dev, "Error parsing card name: %d\n", ret);
40 		return ret;
41 	}
42 
43 	if (of_property_present(dev->of_node, "widgets")) {
44 		ret = snd_soc_of_parse_audio_simple_widgets(card, "widgets");
45 		if (ret)
46 			return ret;
47 	}
48 
49 	/* DAPM routes */
50 	if (of_property_present(dev->of_node, "audio-routing")) {
51 		ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
52 		if (ret)
53 			return ret;
54 	}
55 	/* Deprecated, only for compatibility with old device trees */
56 	if (of_property_present(dev->of_node, "qcom,audio-routing")) {
57 		ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing");
58 		if (ret)
59 			return ret;
60 	}
61 
62 	ret = snd_soc_of_parse_pin_switches(card, "pin-switches");
63 	if (ret)
64 		return ret;
65 
66 	ret = snd_soc_of_parse_aux_devs(card, "aux-devs");
67 	if (ret)
68 		return ret;
69 
70 	/* Populate links */
71 	num_links = of_get_available_child_count(dev->of_node);
72 
73 	/* Allocate the DAI link array */
74 	card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
75 	if (!card->dai_link)
76 		return -ENOMEM;
77 
78 	card->num_links = num_links;
79 	link = card->dai_link;
80 
81 	for_each_available_child_of_node_scoped(dev->of_node, np) {
82 		dlc = devm_kcalloc(dev, 2, sizeof(*dlc), GFP_KERNEL);
83 		if (!dlc)
84 			return -ENOMEM;
85 
86 		link->cpus	= &dlc[0];
87 		link->platforms	= &dlc[1];
88 
89 		link->num_cpus		= 1;
90 		link->num_platforms	= 1;
91 
92 		ret = of_property_read_string(np, "link-name", &link->name);
93 		if (ret) {
94 			dev_err(dev, "error getting codec dai_link name\n");
95 			return ret;
96 		}
97 
98 		struct device_node *cpu __free(device_node) =
99 			of_get_child_by_name(np, "cpu");
100 		struct device_node *platform __free(device_node) =
101 			of_get_child_by_name(np, "platform");
102 		struct device_node *codec __free(device_node) =
103 			of_get_child_by_name(np, "codec");
104 
105 		if (!cpu) {
106 			dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
107 			return -EINVAL;
108 		}
109 
110 		ret = snd_soc_of_get_dlc(cpu, &args, link->cpus, 0);
111 		if (ret) {
112 			dev_err_probe(dev, ret,
113 				      "%s: error getting cpu dai name\n", link->name);
114 			return ret;
115 		}
116 
117 		link->id = args.args[0];
118 
119 		if (link->id >= LPASS_MAX_PORT) {
120 			dev_err(dev, "%s: Invalid cpu dai id %d\n", link->name, link->id);
121 			return -EINVAL;
122 		}
123 
124 		if (platform) {
125 			link->platforms->of_node = of_parse_phandle(platform,
126 					"sound-dai",
127 					0);
128 			if (!link->platforms->of_node) {
129 				dev_err(dev, "%s: platform dai not found\n", link->name);
130 				return -EINVAL;
131 			}
132 		} else {
133 			link->platforms->of_node = link->cpus->of_node;
134 		}
135 
136 		if (codec) {
137 			ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
138 			if (ret < 0) {
139 				dev_err_probe(dev, ret,
140 					      "%s: codec dai not found\n", link->name);
141 				return ret;
142 			}
143 
144 			if (platform) {
145 				/* DPCM backend */
146 				link->no_pcm = 1;
147 				link->ignore_pmdown_time = 1;
148 			}
149 		} else {
150 			/* DPCM frontend */
151 			link->codecs	 = &snd_soc_dummy_dlc;
152 			link->num_codecs = 1;
153 			link->dynamic = 1;
154 		}
155 
156 		if (platform || !codec) {
157 			/* DPCM */
158 			link->ignore_suspend = 1;
159 			link->nonatomic = 1;
160 		}
161 
162 		link->stream_name = link->name;
163 		link++;
164 	}
165 
166 	if (!card->dapm_widgets) {
167 		card->dapm_widgets = qcom_jack_snd_widgets;
168 		card->num_dapm_widgets = ARRAY_SIZE(qcom_jack_snd_widgets);
169 	}
170 
171 	return 0;
172 }
173 EXPORT_SYMBOL_GPL(qcom_snd_parse_of);
174 
175 static struct snd_soc_jack_pin qcom_headset_jack_pins[] = {
176 	/* Headset */
177 	{
178 		.pin = "Mic Jack",
179 		.mask = SND_JACK_MICROPHONE,
180 	},
181 	{
182 		.pin = "Headphone Jack",
183 		.mask = SND_JACK_HEADPHONE,
184 	},
185 };
186 
187 int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
188 			    struct snd_soc_jack *jack, bool *jack_setup)
189 {
190 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
191 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
192 	struct snd_soc_card *card = rtd->card;
193 	int rval, i;
194 
195 	if (!*jack_setup) {
196 		rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
197 					     SND_JACK_HEADSET | SND_JACK_LINEOUT |
198 					     SND_JACK_MECHANICAL |
199 					     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
200 					     SND_JACK_BTN_2 | SND_JACK_BTN_3 |
201 					     SND_JACK_BTN_4 | SND_JACK_BTN_5,
202 					     jack, qcom_headset_jack_pins,
203 					     ARRAY_SIZE(qcom_headset_jack_pins));
204 
205 		if (rval < 0) {
206 			dev_err(card->dev, "Unable to add Headphone Jack\n");
207 			return rval;
208 		}
209 
210 		snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
211 		snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
212 		snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
213 		snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
214 		*jack_setup = true;
215 	}
216 
217 	switch (cpu_dai->id) {
218 	case LPI_MI2S_RX_0:
219 	case TX_CODEC_DMA_TX_0:
220 	case TX_CODEC_DMA_TX_1:
221 	case TX_CODEC_DMA_TX_2:
222 	case TX_CODEC_DMA_TX_3:
223 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
224 			rval = snd_soc_component_set_jack(codec_dai->component,
225 							  jack, NULL);
226 			if (rval != 0 && rval != -ENOTSUPP) {
227 				dev_warn(card->dev, "Failed to set jack: %d\n", rval);
228 				return rval;
229 			}
230 		}
231 
232 		break;
233 	default:
234 		break;
235 	}
236 
237 
238 	return 0;
239 }
240 EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup);
241 
242 int qcom_snd_dp_jack_setup(struct snd_soc_pcm_runtime *rtd,
243 			   struct snd_soc_jack *dp_jack, int dp_pcm_id)
244 {
245 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
246 	struct snd_soc_card *card = rtd->card;
247 	char jack_name[NAME_SIZE];
248 	int rval, i;
249 
250 	snprintf(jack_name, sizeof(jack_name), "DP%d Jack", dp_pcm_id);
251 	rval = snd_soc_card_jack_new(card, jack_name, SND_JACK_AVOUT, dp_jack);
252 	if (rval)
253 		return rval;
254 
255 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
256 		rval = snd_soc_component_set_jack(codec_dai->component, dp_jack, NULL);
257 		if (rval != 0 && rval != -ENOTSUPP) {
258 			dev_warn(card->dev, "Failed to set jack: %d\n", rval);
259 			return rval;
260 		}
261 	}
262 
263 	return 0;
264 }
265 EXPORT_SYMBOL_GPL(qcom_snd_dp_jack_setup);
266 
267 MODULE_DESCRIPTION("ASoC Qualcomm helper functions");
268 MODULE_LICENSE("GPL");
269