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 dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL); 144 if (!dlc) { 145 ret = -ENOMEM; 146 goto err; 147 } 148 149 link->codecs = dlc; 150 link->num_codecs = 1; 151 152 link->codecs->dai_name = "snd-soc-dummy-dai"; 153 link->codecs->name = "snd-soc-dummy"; 154 link->dynamic = 1; 155 } 156 157 if (platform || !codec) { 158 /* DPCM */ 159 snd_soc_dai_link_set_capabilities(link); 160 link->ignore_suspend = 1; 161 link->nonatomic = 1; 162 } 163 164 link->stream_name = link->name; 165 link++; 166 167 of_node_put(cpu); 168 of_node_put(codec); 169 of_node_put(platform); 170 } 171 172 return 0; 173 err: 174 of_node_put(cpu); 175 of_node_put(codec); 176 of_node_put(platform); 177 err_put_np: 178 of_node_put(np); 179 return ret; 180 } 181 EXPORT_SYMBOL_GPL(qcom_snd_parse_of); 182 183 int qcom_snd_sdw_prepare(struct snd_pcm_substream *substream, 184 struct sdw_stream_runtime *sruntime, 185 bool *stream_prepared) 186 { 187 struct snd_soc_pcm_runtime *rtd = substream->private_data; 188 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 189 int ret; 190 191 if (!sruntime) 192 return 0; 193 194 switch (cpu_dai->id) { 195 case WSA_CODEC_DMA_RX_0: 196 case WSA_CODEC_DMA_RX_1: 197 case RX_CODEC_DMA_RX_0: 198 case RX_CODEC_DMA_RX_1: 199 case TX_CODEC_DMA_TX_0: 200 case TX_CODEC_DMA_TX_1: 201 case TX_CODEC_DMA_TX_2: 202 case TX_CODEC_DMA_TX_3: 203 break; 204 default: 205 return 0; 206 } 207 208 if (*stream_prepared) { 209 sdw_disable_stream(sruntime); 210 sdw_deprepare_stream(sruntime); 211 *stream_prepared = false; 212 } 213 214 ret = sdw_prepare_stream(sruntime); 215 if (ret) 216 return ret; 217 218 /** 219 * NOTE: there is a strict hw requirement about the ordering of port 220 * enables and actual WSA881x PA enable. PA enable should only happen 221 * after soundwire ports are enabled if not DC on the line is 222 * accumulated resulting in Click/Pop Noise 223 * PA enable/mute are handled as part of codec DAPM and digital mute. 224 */ 225 226 ret = sdw_enable_stream(sruntime); 227 if (ret) { 228 sdw_deprepare_stream(sruntime); 229 return ret; 230 } 231 *stream_prepared = true; 232 233 return ret; 234 } 235 EXPORT_SYMBOL_GPL(qcom_snd_sdw_prepare); 236 237 int qcom_snd_sdw_hw_params(struct snd_pcm_substream *substream, 238 struct snd_pcm_hw_params *params, 239 struct sdw_stream_runtime **psruntime) 240 { 241 struct snd_soc_pcm_runtime *rtd = substream->private_data; 242 struct snd_soc_dai *codec_dai; 243 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 244 struct sdw_stream_runtime *sruntime; 245 int i; 246 247 switch (cpu_dai->id) { 248 case WSA_CODEC_DMA_RX_0: 249 case RX_CODEC_DMA_RX_0: 250 case RX_CODEC_DMA_RX_1: 251 case TX_CODEC_DMA_TX_0: 252 case TX_CODEC_DMA_TX_1: 253 case TX_CODEC_DMA_TX_2: 254 case TX_CODEC_DMA_TX_3: 255 for_each_rtd_codec_dais(rtd, i, codec_dai) { 256 sruntime = snd_soc_dai_get_stream(codec_dai, substream->stream); 257 if (sruntime != ERR_PTR(-ENOTSUPP)) 258 *psruntime = sruntime; 259 } 260 break; 261 } 262 263 return 0; 264 265 } 266 EXPORT_SYMBOL_GPL(qcom_snd_sdw_hw_params); 267 268 int qcom_snd_sdw_hw_free(struct snd_pcm_substream *substream, 269 struct sdw_stream_runtime *sruntime, bool *stream_prepared) 270 { 271 struct snd_soc_pcm_runtime *rtd = substream->private_data; 272 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 273 274 switch (cpu_dai->id) { 275 case WSA_CODEC_DMA_RX_0: 276 case WSA_CODEC_DMA_RX_1: 277 case RX_CODEC_DMA_RX_0: 278 case RX_CODEC_DMA_RX_1: 279 case TX_CODEC_DMA_TX_0: 280 case TX_CODEC_DMA_TX_1: 281 case TX_CODEC_DMA_TX_2: 282 case TX_CODEC_DMA_TX_3: 283 if (sruntime && *stream_prepared) { 284 sdw_disable_stream(sruntime); 285 sdw_deprepare_stream(sruntime); 286 *stream_prepared = false; 287 } 288 break; 289 default: 290 break; 291 } 292 293 return 0; 294 } 295 EXPORT_SYMBOL_GPL(qcom_snd_sdw_hw_free); 296 297 int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd, 298 struct snd_soc_jack *jack, bool *jack_setup) 299 { 300 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 301 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 302 struct snd_soc_card *card = rtd->card; 303 int rval, i; 304 305 if (!*jack_setup) { 306 rval = snd_soc_card_jack_new(card, "Headset Jack", 307 SND_JACK_HEADSET | SND_JACK_LINEOUT | 308 SND_JACK_MECHANICAL | 309 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 310 SND_JACK_BTN_2 | SND_JACK_BTN_3 | 311 SND_JACK_BTN_4 | SND_JACK_BTN_5, 312 jack); 313 314 if (rval < 0) { 315 dev_err(card->dev, "Unable to add Headphone Jack\n"); 316 return rval; 317 } 318 319 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA); 320 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 321 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 322 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 323 *jack_setup = true; 324 } 325 326 switch (cpu_dai->id) { 327 case TX_CODEC_DMA_TX_0: 328 case TX_CODEC_DMA_TX_1: 329 case TX_CODEC_DMA_TX_2: 330 case TX_CODEC_DMA_TX_3: 331 for_each_rtd_codec_dais(rtd, i, codec_dai) { 332 rval = snd_soc_component_set_jack(codec_dai->component, 333 jack, NULL); 334 if (rval != 0 && rval != -ENOTSUPP) { 335 dev_warn(card->dev, "Failed to set jack: %d\n", rval); 336 return rval; 337 } 338 } 339 340 break; 341 default: 342 break; 343 } 344 345 346 return 0; 347 } 348 EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup); 349 MODULE_LICENSE("GPL v2"); 350