1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018, Linaro Limited.
3 // Copyright (c) 2018, The Linux Foundation. All rights reserved.
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>
11 static const struct snd_soc_dapm_widget qcom_jack_snd_widgets[] = {
12 SND_SOC_DAPM_HP("Headphone Jack", NULL),
13 SND_SOC_DAPM_MIC("Mic Jack", NULL),
16 int qcom_snd_parse_of(struct snd_soc_card *card)
18 struct device_node *np;
19 struct device_node *codec = NULL;
20 struct device_node *platform = NULL;
21 struct device_node *cpu = NULL;
22 struct device *dev = card->dev;
23 struct snd_soc_dai_link *link;
24 struct of_phandle_args args;
25 struct snd_soc_dai_link_component *dlc;
28 ret = snd_soc_of_parse_card_name(card, "model");
29 if (ret == 0 && !card->name)
30 /* Deprecated, only for compatibility with old device trees */
31 ret = snd_soc_of_parse_card_name(card, "qcom,model");
33 dev_err(dev, "Error parsing card name: %d\n", ret);
37 if (of_property_read_bool(dev->of_node, "widgets")) {
38 ret = snd_soc_of_parse_audio_simple_widgets(card, "widgets");
44 if (of_property_read_bool(dev->of_node, "audio-routing")) {
45 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
49 /* Deprecated, only for compatibility with old device trees */
50 if (of_property_read_bool(dev->of_node, "qcom,audio-routing")) {
51 ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing");
56 ret = snd_soc_of_parse_pin_switches(card, "pin-switches");
60 ret = snd_soc_of_parse_aux_devs(card, "aux-devs");
65 num_links = of_get_available_child_count(dev->of_node);
67 /* Allocate the DAI link array */
68 card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
72 card->num_links = num_links;
73 link = card->dai_link;
75 for_each_available_child_of_node(dev->of_node, np) {
76 dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
83 link->platforms = &dlc[1];
86 link->num_platforms = 1;
88 ret = of_property_read_string(np, "link-name", &link->name);
90 dev_err(card->dev, "error getting codec dai_link name\n");
94 cpu = of_get_child_by_name(np, "cpu");
95 platform = of_get_child_by_name(np, "platform");
96 codec = of_get_child_by_name(np, "codec");
99 dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
104 ret = snd_soc_of_get_dlc(cpu, &args, link->cpus, 0);
106 dev_err_probe(card->dev, ret,
107 "%s: error getting cpu dai name\n", link->name);
111 link->id = args.args[0];
114 link->platforms->of_node = of_parse_phandle(platform,
117 if (!link->platforms->of_node) {
118 dev_err(card->dev, "%s: platform dai not found\n", link->name);
123 link->platforms->of_node = link->cpus->of_node;
127 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
129 dev_err_probe(card->dev, ret,
130 "%s: codec dai not found\n", link->name);
137 link->ignore_pmdown_time = 1;
141 link->codecs = &snd_soc_dummy_dlc;
142 link->num_codecs = 1;
146 if (platform || !codec) {
148 snd_soc_dai_link_set_capabilities(link);
149 link->ignore_suspend = 1;
153 link->stream_name = link->name;
158 of_node_put(platform);
161 if (!card->dapm_widgets) {
162 card->dapm_widgets = qcom_jack_snd_widgets;
163 card->num_dapm_widgets = ARRAY_SIZE(qcom_jack_snd_widgets);
170 of_node_put(platform);
175 EXPORT_SYMBOL_GPL(qcom_snd_parse_of);
177 static struct snd_soc_jack_pin qcom_headset_jack_pins[] = {
181 .mask = SND_JACK_MICROPHONE,
184 .pin = "Headphone Jack",
185 .mask = SND_JACK_HEADPHONE,
189 int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
190 struct snd_soc_jack *jack, bool *jack_setup)
192 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
193 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
194 struct snd_soc_card *card = rtd->card;
198 rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
199 SND_JACK_HEADSET | SND_JACK_LINEOUT |
200 SND_JACK_MECHANICAL |
201 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
202 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
203 SND_JACK_BTN_4 | SND_JACK_BTN_5,
204 jack, qcom_headset_jack_pins,
205 ARRAY_SIZE(qcom_headset_jack_pins));
208 dev_err(card->dev, "Unable to add Headphone Jack\n");
212 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
213 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
214 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
215 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
219 switch (cpu_dai->id) {
220 case TX_CODEC_DMA_TX_0:
221 case TX_CODEC_DMA_TX_1:
222 case TX_CODEC_DMA_TX_2:
223 case TX_CODEC_DMA_TX_3:
224 for_each_rtd_codec_dais(rtd, i, codec_dai) {
225 rval = snd_soc_component_set_jack(codec_dai->component,
227 if (rval != 0 && rval != -ENOTSUPP) {
228 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
241 EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup);
242 MODULE_LICENSE("GPL v2");