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 = snd_soc_of_get_dlc(cpu, &args, link->cpus, 0); 100 if (ret) { 101 dev_err_probe(card->dev, ret, 102 "%s: error getting cpu dai name\n", link->name); 103 goto err; 104 } 105 106 link->id = args.args[0]; 107 108 if (platform) { 109 link->platforms->of_node = of_parse_phandle(platform, 110 "sound-dai", 111 0); 112 if (!link->platforms->of_node) { 113 dev_err(card->dev, "%s: platform dai not found\n", link->name); 114 ret = -EINVAL; 115 goto err; 116 } 117 } else { 118 link->platforms->of_node = link->cpus->of_node; 119 } 120 121 if (codec) { 122 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link); 123 if (ret < 0) { 124 dev_err_probe(card->dev, ret, 125 "%s: codec dai not found\n", link->name); 126 goto err; 127 } 128 129 if (platform) { 130 /* DPCM backend */ 131 link->no_pcm = 1; 132 link->ignore_pmdown_time = 1; 133 } 134 } else { 135 /* DPCM frontend */ 136 link->codecs = &asoc_dummy_dlc; 137 link->num_codecs = 1; 138 link->dynamic = 1; 139 } 140 141 if (platform || !codec) { 142 /* DPCM */ 143 snd_soc_dai_link_set_capabilities(link); 144 link->ignore_suspend = 1; 145 link->nonatomic = 1; 146 } 147 148 link->stream_name = link->name; 149 link++; 150 151 of_node_put(cpu); 152 of_node_put(codec); 153 of_node_put(platform); 154 } 155 156 return 0; 157 err: 158 of_node_put(cpu); 159 of_node_put(codec); 160 of_node_put(platform); 161 err_put_np: 162 of_node_put(np); 163 return ret; 164 } 165 EXPORT_SYMBOL_GPL(qcom_snd_parse_of); 166 167 static struct snd_soc_jack_pin qcom_headset_jack_pins[] = { 168 /* Headset */ 169 { 170 .pin = "Mic Jack", 171 .mask = SND_JACK_MICROPHONE, 172 }, 173 { 174 .pin = "Headphone Jack", 175 .mask = SND_JACK_HEADPHONE, 176 }, 177 }; 178 179 int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd, 180 struct snd_soc_jack *jack, bool *jack_setup) 181 { 182 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 183 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 184 struct snd_soc_card *card = rtd->card; 185 int rval, i; 186 187 if (!*jack_setup) { 188 rval = snd_soc_card_jack_new_pins(card, "Headset Jack", 189 SND_JACK_HEADSET | SND_JACK_LINEOUT | 190 SND_JACK_MECHANICAL | 191 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 192 SND_JACK_BTN_2 | SND_JACK_BTN_3 | 193 SND_JACK_BTN_4 | SND_JACK_BTN_5, 194 jack, qcom_headset_jack_pins, 195 ARRAY_SIZE(qcom_headset_jack_pins)); 196 197 if (rval < 0) { 198 dev_err(card->dev, "Unable to add Headphone Jack\n"); 199 return rval; 200 } 201 202 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA); 203 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 204 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 205 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 206 *jack_setup = true; 207 } 208 209 switch (cpu_dai->id) { 210 case TX_CODEC_DMA_TX_0: 211 case TX_CODEC_DMA_TX_1: 212 case TX_CODEC_DMA_TX_2: 213 case TX_CODEC_DMA_TX_3: 214 for_each_rtd_codec_dais(rtd, i, codec_dai) { 215 rval = snd_soc_component_set_jack(codec_dai->component, 216 jack, NULL); 217 if (rval != 0 && rval != -ENOTSUPP) { 218 dev_warn(card->dev, "Failed to set jack: %d\n", rval); 219 return rval; 220 } 221 } 222 223 break; 224 default: 225 break; 226 } 227 228 229 return 0; 230 } 231 EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup); 232 MODULE_LICENSE("GPL v2"); 233