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