1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2018 Intel Corporation. All rights reserved. 7 // 8 // Authors: Keyon Jie <yang.jie@linux.intel.com> 9 // 10 11 #include <sound/pcm_params.h> 12 #include <sound/hdaudio_ext.h> 13 #include "../sof-priv.h" 14 #include "hda.h" 15 16 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) 17 18 struct hda_pipe_params { 19 u8 host_dma_id; 20 u8 link_dma_id; 21 u32 ch; 22 u32 s_freq; 23 u32 s_fmt; 24 u8 linktype; 25 snd_pcm_format_t format; 26 int link_index; 27 int stream; 28 unsigned int host_bps; 29 unsigned int link_bps; 30 }; 31 32 /* 33 * This function checks if the host dma channel corresponding 34 * to the link DMA stream_tag argument is assigned to one 35 * of the FEs connected to the BE DAI. 36 */ 37 static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd, 38 int dir, int stream_tag) 39 { 40 struct snd_pcm_substream *fe_substream; 41 struct hdac_stream *fe_hstream; 42 struct snd_soc_dpcm *dpcm; 43 44 for_each_dpcm_fe(rtd, dir, dpcm) { 45 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, dir); 46 fe_hstream = fe_substream->runtime->private_data; 47 if (fe_hstream->stream_tag == stream_tag) 48 return true; 49 } 50 51 return false; 52 } 53 54 static struct hdac_ext_stream * 55 hda_link_stream_assign(struct hdac_bus *bus, 56 struct snd_pcm_substream *substream) 57 { 58 struct snd_soc_pcm_runtime *rtd = substream->private_data; 59 struct sof_intel_hda_stream *hda_stream; 60 struct hdac_ext_stream *res = NULL; 61 struct hdac_stream *stream = NULL; 62 63 int stream_dir = substream->stream; 64 65 if (!bus->ppcap) { 66 dev_err(bus->dev, "stream type not supported\n"); 67 return NULL; 68 } 69 70 list_for_each_entry(stream, &bus->stream_list, list) { 71 struct hdac_ext_stream *hstream = 72 stream_to_hdac_ext_stream(stream); 73 if (stream->direction != substream->stream) 74 continue; 75 76 hda_stream = hstream_to_sof_hda_stream(hstream); 77 78 /* check if link is available */ 79 if (!hstream->link_locked) { 80 if (stream->opened) { 81 /* 82 * check if the stream tag matches the stream 83 * tag of one of the connected FEs 84 */ 85 if (hda_check_fes(rtd, stream_dir, 86 stream->stream_tag)) { 87 res = hstream; 88 break; 89 } 90 } else { 91 res = hstream; 92 93 /* 94 * This must be a hostless stream. 95 * So reserve the host DMA channel. 96 */ 97 hda_stream->host_reserved = 1; 98 break; 99 } 100 } 101 } 102 103 if (res) { 104 /* 105 * Decouple host and link DMA. The decoupled flag 106 * is updated in snd_hdac_ext_stream_decouple(). 107 */ 108 if (!res->decoupled) 109 snd_hdac_ext_stream_decouple(bus, res, true); 110 spin_lock_irq(&bus->reg_lock); 111 res->link_locked = 1; 112 res->link_substream = substream; 113 spin_unlock_irq(&bus->reg_lock); 114 } 115 116 return res; 117 } 118 119 static int hda_link_dma_params(struct hdac_ext_stream *stream, 120 struct hda_pipe_params *params) 121 { 122 struct hdac_stream *hstream = &stream->hstream; 123 unsigned char stream_tag = hstream->stream_tag; 124 struct hdac_bus *bus = hstream->bus; 125 struct hdac_ext_link *link; 126 unsigned int format_val; 127 128 snd_hdac_ext_stream_decouple(bus, stream, true); 129 snd_hdac_ext_link_stream_reset(stream); 130 131 format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch, 132 params->format, 133 params->link_bps, 0); 134 135 dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n", 136 format_val, params->s_freq, params->ch, params->format); 137 138 snd_hdac_ext_link_stream_setup(stream, format_val); 139 140 if (stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) { 141 list_for_each_entry(link, &bus->hlink_list, list) { 142 if (link->index == params->link_index) 143 snd_hdac_ext_link_set_stream_id(link, 144 stream_tag); 145 } 146 } 147 148 stream->link_prepared = 1; 149 150 return 0; 151 } 152 153 /* Send DAI_CONFIG IPC to the DAI that matches the dai_name and direction */ 154 static int hda_link_config_ipc(struct sof_intel_hda_stream *hda_stream, 155 const char *dai_name, int channel, int dir) 156 { 157 struct sof_ipc_dai_config *config; 158 struct snd_sof_dai *sof_dai; 159 struct sof_ipc_reply reply; 160 int ret = 0; 161 162 list_for_each_entry(sof_dai, &hda_stream->sdev->dai_list, list) { 163 if (!sof_dai->cpu_dai_name) 164 continue; 165 166 if (!strcmp(dai_name, sof_dai->cpu_dai_name) && 167 dir == sof_dai->comp_dai.direction) { 168 config = sof_dai->dai_config; 169 170 if (!config) { 171 dev_err(hda_stream->sdev->dev, 172 "error: no config for DAI %s\n", 173 sof_dai->name); 174 return -EINVAL; 175 } 176 177 /* update config with stream tag */ 178 config->hda.link_dma_ch = channel; 179 180 /* send IPC */ 181 ret = sof_ipc_tx_message(hda_stream->sdev->ipc, 182 config->hdr.cmd, 183 config, 184 config->hdr.size, 185 &reply, sizeof(reply)); 186 187 if (ret < 0) 188 dev_err(hda_stream->sdev->dev, 189 "error: failed to set dai config for %s\n", 190 sof_dai->name); 191 return ret; 192 } 193 } 194 195 return -EINVAL; 196 } 197 198 static int hda_link_hw_params(struct snd_pcm_substream *substream, 199 struct snd_pcm_hw_params *params, 200 struct snd_soc_dai *dai) 201 { 202 struct hdac_stream *hstream = substream->runtime->private_data; 203 struct hdac_bus *bus = hstream->bus; 204 struct hdac_ext_stream *link_dev; 205 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream); 206 struct snd_soc_dai *codec_dai = rtd->codec_dai; 207 struct sof_intel_hda_stream *hda_stream; 208 struct hda_pipe_params p_params = {0}; 209 struct hdac_ext_link *link; 210 int stream_tag; 211 int ret; 212 213 link_dev = hda_link_stream_assign(bus, substream); 214 if (!link_dev) 215 return -EBUSY; 216 217 stream_tag = hdac_stream(link_dev)->stream_tag; 218 219 hda_stream = hstream_to_sof_hda_stream(link_dev); 220 221 /* update the DSP with the new tag */ 222 ret = hda_link_config_ipc(hda_stream, dai->name, stream_tag - 1, 223 substream->stream); 224 if (ret < 0) 225 return ret; 226 227 snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev); 228 229 hda_stream->hw_params_upon_resume = 0; 230 231 link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name); 232 if (!link) 233 return -EINVAL; 234 235 /* set the stream tag in the codec dai dma params */ 236 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 237 snd_soc_dai_set_tdm_slot(codec_dai, stream_tag, 0, 0, 0); 238 else 239 snd_soc_dai_set_tdm_slot(codec_dai, 0, stream_tag, 0, 0); 240 241 p_params.s_fmt = snd_pcm_format_width(params_format(params)); 242 p_params.ch = params_channels(params); 243 p_params.s_freq = params_rate(params); 244 p_params.stream = substream->stream; 245 p_params.link_dma_id = stream_tag - 1; 246 p_params.link_index = link->index; 247 p_params.format = params_format(params); 248 249 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 250 p_params.link_bps = codec_dai->driver->playback.sig_bits; 251 else 252 p_params.link_bps = codec_dai->driver->capture.sig_bits; 253 254 return hda_link_dma_params(link_dev, &p_params); 255 } 256 257 static int hda_link_pcm_prepare(struct snd_pcm_substream *substream, 258 struct snd_soc_dai *dai) 259 { 260 struct hdac_ext_stream *link_dev = 261 snd_soc_dai_get_dma_data(dai, substream); 262 struct sof_intel_hda_stream *hda_stream; 263 struct snd_sof_dev *sdev = 264 snd_soc_component_get_drvdata(dai->component); 265 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream); 266 int stream = substream->stream; 267 268 hda_stream = hstream_to_sof_hda_stream(link_dev); 269 270 /* setup hw_params again only if resuming from system suspend */ 271 if (!hda_stream->hw_params_upon_resume) 272 return 0; 273 274 dev_dbg(sdev->dev, "hda: prepare stream dir %d\n", substream->stream); 275 276 return hda_link_hw_params(substream, &rtd->dpcm[stream].hw_params, 277 dai); 278 } 279 280 static int hda_link_pcm_trigger(struct snd_pcm_substream *substream, 281 int cmd, struct snd_soc_dai *dai) 282 { 283 struct hdac_ext_stream *link_dev = 284 snd_soc_dai_get_dma_data(dai, substream); 285 struct sof_intel_hda_stream *hda_stream; 286 struct snd_soc_pcm_runtime *rtd; 287 struct hdac_ext_link *link; 288 struct hdac_stream *hstream; 289 struct hdac_bus *bus; 290 int stream_tag; 291 int ret; 292 293 hstream = substream->runtime->private_data; 294 bus = hstream->bus; 295 rtd = snd_pcm_substream_chip(substream); 296 297 link = snd_hdac_ext_bus_get_link(bus, rtd->codec_dai->component->name); 298 if (!link) 299 return -EINVAL; 300 301 hda_stream = hstream_to_sof_hda_stream(link_dev); 302 303 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd); 304 switch (cmd) { 305 case SNDRV_PCM_TRIGGER_RESUME: 306 /* set up hw_params */ 307 ret = hda_link_pcm_prepare(substream, dai); 308 if (ret < 0) { 309 dev_err(dai->dev, 310 "error: setting up hw_params during resume\n"); 311 return ret; 312 } 313 314 /* fallthrough */ 315 case SNDRV_PCM_TRIGGER_START: 316 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 317 snd_hdac_ext_link_stream_start(link_dev); 318 break; 319 case SNDRV_PCM_TRIGGER_SUSPEND: 320 /* 321 * clear and release link DMA channel. It will be assigned when 322 * hw_params is set up again after resume. 323 */ 324 ret = hda_link_config_ipc(hda_stream, dai->name, 325 DMA_CHAN_INVALID, substream->stream); 326 if (ret < 0) 327 return ret; 328 stream_tag = hdac_stream(link_dev)->stream_tag; 329 snd_hdac_ext_link_clear_stream_id(link, stream_tag); 330 snd_hdac_ext_stream_release(link_dev, 331 HDAC_EXT_STREAM_TYPE_LINK); 332 333 /* fallthrough */ 334 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 335 case SNDRV_PCM_TRIGGER_STOP: 336 snd_hdac_ext_link_stream_clear(link_dev); 337 break; 338 default: 339 return -EINVAL; 340 } 341 return 0; 342 } 343 344 static int hda_link_hw_free(struct snd_pcm_substream *substream, 345 struct snd_soc_dai *dai) 346 { 347 unsigned int stream_tag; 348 struct sof_intel_hda_stream *hda_stream; 349 struct hdac_bus *bus; 350 struct hdac_ext_link *link; 351 struct hdac_stream *hstream; 352 struct snd_soc_pcm_runtime *rtd; 353 struct hdac_ext_stream *link_dev; 354 int ret; 355 356 hstream = substream->runtime->private_data; 357 bus = hstream->bus; 358 rtd = snd_pcm_substream_chip(substream); 359 link_dev = snd_soc_dai_get_dma_data(dai, substream); 360 hda_stream = hstream_to_sof_hda_stream(link_dev); 361 362 /* free the link DMA channel in the FW */ 363 ret = hda_link_config_ipc(hda_stream, dai->name, DMA_CHAN_INVALID, 364 substream->stream); 365 if (ret < 0) 366 return ret; 367 368 link = snd_hdac_ext_bus_get_link(bus, rtd->codec_dai->component->name); 369 if (!link) 370 return -EINVAL; 371 372 stream_tag = hdac_stream(link_dev)->stream_tag; 373 snd_hdac_ext_link_clear_stream_id(link, stream_tag); 374 snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK); 375 link_dev->link_prepared = 0; 376 377 /* free the host DMA channel reserved by hostless streams */ 378 hda_stream->host_reserved = 0; 379 380 return 0; 381 } 382 383 static const struct snd_soc_dai_ops hda_link_dai_ops = { 384 .hw_params = hda_link_hw_params, 385 .hw_free = hda_link_hw_free, 386 .trigger = hda_link_pcm_trigger, 387 .prepare = hda_link_pcm_prepare, 388 }; 389 #endif 390 391 /* 392 * common dai driver for skl+ platforms. 393 * some products who use this DAI array only physically have a subset of 394 * the DAIs, but no harm is done here by adding the whole set. 395 */ 396 struct snd_soc_dai_driver skl_dai[] = { 397 { 398 .name = "SSP0 Pin", 399 }, 400 { 401 .name = "SSP1 Pin", 402 }, 403 { 404 .name = "SSP2 Pin", 405 }, 406 { 407 .name = "SSP3 Pin", 408 }, 409 { 410 .name = "SSP4 Pin", 411 }, 412 { 413 .name = "SSP5 Pin", 414 }, 415 { 416 .name = "DMIC01 Pin", 417 }, 418 { 419 .name = "DMIC16k Pin", 420 }, 421 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) 422 { 423 .name = "iDisp1 Pin", 424 .ops = &hda_link_dai_ops, 425 }, 426 { 427 .name = "iDisp2 Pin", 428 .ops = &hda_link_dai_ops, 429 }, 430 { 431 .name = "iDisp3 Pin", 432 .ops = &hda_link_dai_ops, 433 }, 434 { 435 .name = "Analog CPU DAI", 436 .ops = &hda_link_dai_ops, 437 }, 438 { 439 .name = "Digital CPU DAI", 440 .ops = &hda_link_dai_ops, 441 }, 442 { 443 .name = "Alt Analog CPU DAI", 444 .ops = &hda_link_dai_ops, 445 }, 446 #endif 447 }; 448