1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2868bd00fSLiam Girdwood // 3868bd00fSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license. When using or 4868bd00fSLiam Girdwood // redistributing this file, you may do so under either license. 5868bd00fSLiam Girdwood // 6868bd00fSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved. 7868bd00fSLiam Girdwood // 8868bd00fSLiam Girdwood // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9868bd00fSLiam Girdwood // 10868bd00fSLiam Girdwood // PCM Layer, interface between ALSA and IPC. 11868bd00fSLiam Girdwood // 12868bd00fSLiam Girdwood 13868bd00fSLiam Girdwood #include <linux/pm_runtime.h> 14868bd00fSLiam Girdwood #include <sound/pcm_params.h> 15868bd00fSLiam Girdwood #include <sound/sof.h> 16868bd00fSLiam Girdwood #include "sof-priv.h" 17ee1e79b7SRanjani Sridharan #include "sof-audio.h" 18868bd00fSLiam Girdwood #include "ops.h" 1970368106SCezary Rojewski #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES) 207bbdda80SPeter Ujfalusi #include "sof-probes.h" 2170368106SCezary Rojewski #endif 22*ee844305SPeter Ujfalusi #include "sof-utils.h" 23868bd00fSLiam Girdwood 24868bd00fSLiam Girdwood /* Create DMA buffer page table for DSP */ 251c91d77eSKuninori Morimoto static int create_page_table(struct snd_soc_component *component, 261c91d77eSKuninori Morimoto struct snd_pcm_substream *substream, 27868bd00fSLiam Girdwood unsigned char *dma_area, size_t size) 28868bd00fSLiam Girdwood { 291205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 30868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 31868bd00fSLiam Girdwood struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream); 32868bd00fSLiam Girdwood int stream = substream->stream; 33868bd00fSLiam Girdwood 34ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 35868bd00fSLiam Girdwood if (!spcm) 36868bd00fSLiam Girdwood return -EINVAL; 37868bd00fSLiam Girdwood 38ee1e79b7SRanjani Sridharan return snd_sof_create_page_table(component->dev, dmab, 39868bd00fSLiam Girdwood spcm->stream[stream].page_table.area, size); 40868bd00fSLiam Girdwood } 41868bd00fSLiam Girdwood 42868bd00fSLiam Girdwood static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream, 43868bd00fSLiam Girdwood const struct sof_ipc_pcm_params_reply *reply) 44868bd00fSLiam Girdwood { 45ee1e79b7SRanjani Sridharan struct snd_soc_component *scomp = spcm->scomp; 46ee1e79b7SRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 47ee1e79b7SRanjani Sridharan 48868bd00fSLiam Girdwood /* validate offset */ 49868bd00fSLiam Girdwood int ret = snd_sof_ipc_pcm_params(sdev, substream, reply); 50868bd00fSLiam Girdwood 51868bd00fSLiam Girdwood if (ret < 0) 52ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: got wrong reply for PCM %d\n", 53868bd00fSLiam Girdwood spcm->pcm.pcm_id); 54868bd00fSLiam Girdwood 55868bd00fSLiam Girdwood return ret; 56868bd00fSLiam Girdwood } 57868bd00fSLiam Girdwood 58e2803e61SKeyon Jie /* 59e2803e61SKeyon Jie * sof pcm period elapse work 60e2803e61SKeyon Jie */ 61858f7a5cSDaniel Baluta static void snd_sof_pcm_period_elapsed_work(struct work_struct *work) 62e2803e61SKeyon Jie { 63e2803e61SKeyon Jie struct snd_sof_pcm_stream *sps = 64e2803e61SKeyon Jie container_of(work, struct snd_sof_pcm_stream, 65e2803e61SKeyon Jie period_elapsed_work); 66e2803e61SKeyon Jie 67e2803e61SKeyon Jie snd_pcm_period_elapsed(sps->substream); 68e2803e61SKeyon Jie } 69e2803e61SKeyon Jie 70858f7a5cSDaniel Baluta void snd_sof_pcm_init_elapsed_work(struct work_struct *work) 71858f7a5cSDaniel Baluta { 72858f7a5cSDaniel Baluta INIT_WORK(work, snd_sof_pcm_period_elapsed_work); 73858f7a5cSDaniel Baluta } 74858f7a5cSDaniel Baluta 75e2803e61SKeyon Jie /* 76e2803e61SKeyon Jie * sof pcm period elapse, this could be called at irq thread context. 77e2803e61SKeyon Jie */ 78e2803e61SKeyon Jie void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream) 79e2803e61SKeyon Jie { 801205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 81e2803e61SKeyon Jie struct snd_soc_component *component = 82ee1e79b7SRanjani Sridharan snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 83e2803e61SKeyon Jie struct snd_sof_pcm *spcm; 84e2803e61SKeyon Jie 85ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 86e2803e61SKeyon Jie if (!spcm) { 87ee1e79b7SRanjani Sridharan dev_err(component->dev, 88e2803e61SKeyon Jie "error: period elapsed for unknown stream!\n"); 89e2803e61SKeyon Jie return; 90e2803e61SKeyon Jie } 91e2803e61SKeyon Jie 92e2803e61SKeyon Jie /* 93e2803e61SKeyon Jie * snd_pcm_period_elapsed() can be called in interrupt context 94e2803e61SKeyon Jie * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(), 95e2803e61SKeyon Jie * when the PCM is done draining or xrun happened, a STOP IPC will 96e2803e61SKeyon Jie * then be sent and this IPC will hit IPC timeout. 97e2803e61SKeyon Jie * To avoid sending IPC before the previous IPC is handled, we 98e2803e61SKeyon Jie * schedule delayed work here to call the snd_pcm_period_elapsed(). 99e2803e61SKeyon Jie */ 100e2803e61SKeyon Jie schedule_work(&spcm->stream[substream->stream].period_elapsed_work); 101e2803e61SKeyon Jie } 102e2803e61SKeyon Jie EXPORT_SYMBOL(snd_sof_pcm_period_elapsed); 103e2803e61SKeyon Jie 10496da1740SRanjani Sridharan int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev, 105cfe8191bSKai Vehmanen struct snd_sof_pcm *spcm) 106cfe8191bSKai Vehmanen { 107cfe8191bSKai Vehmanen struct sof_ipc_stream stream; 108cfe8191bSKai Vehmanen struct sof_ipc_reply reply; 109cfe8191bSKai Vehmanen int ret; 110cfe8191bSKai Vehmanen 11185d7acd0SRanjani Sridharan if (!spcm->prepared[substream->stream]) 11285d7acd0SRanjani Sridharan return 0; 11385d7acd0SRanjani Sridharan 114cfe8191bSKai Vehmanen stream.hdr.size = sizeof(stream); 115cfe8191bSKai Vehmanen stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE; 116cfe8191bSKai Vehmanen stream.comp_id = spcm->stream[substream->stream].comp_id; 117cfe8191bSKai Vehmanen 118cfe8191bSKai Vehmanen /* send IPC to the DSP */ 119cfe8191bSKai Vehmanen ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream, 120cfe8191bSKai Vehmanen sizeof(stream), &reply, sizeof(reply)); 121cfe8191bSKai Vehmanen if (!ret) 122cfe8191bSKai Vehmanen spcm->prepared[substream->stream] = false; 123cfe8191bSKai Vehmanen 124cfe8191bSKai Vehmanen return ret; 125cfe8191bSKai Vehmanen } 126cfe8191bSKai Vehmanen 1275fcdbb2dSRanjani Sridharan static int sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, 1285fcdbb2dSRanjani Sridharan struct snd_soc_pcm_runtime *rtd, 1295fcdbb2dSRanjani Sridharan struct snd_sof_pcm *spcm, int dir) 1305fcdbb2dSRanjani Sridharan { 1315fcdbb2dSRanjani Sridharan struct snd_soc_dai *dai; 1325fcdbb2dSRanjani Sridharan int ret, j; 1335fcdbb2dSRanjani Sridharan 1345fcdbb2dSRanjani Sridharan /* query DAPM for list of connected widgets and set them up */ 1355fcdbb2dSRanjani Sridharan for_each_rtd_cpu_dais(rtd, j, dai) { 1365fcdbb2dSRanjani Sridharan struct snd_soc_dapm_widget_list *list; 1375fcdbb2dSRanjani Sridharan 1385fcdbb2dSRanjani Sridharan ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list, 1395fcdbb2dSRanjani Sridharan dpcm_end_walk_at_be); 1405fcdbb2dSRanjani Sridharan if (ret < 0) { 1415fcdbb2dSRanjani Sridharan dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name, 1425fcdbb2dSRanjani Sridharan dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture"); 1435fcdbb2dSRanjani Sridharan return ret; 1445fcdbb2dSRanjani Sridharan } 1455fcdbb2dSRanjani Sridharan 1465fcdbb2dSRanjani Sridharan spcm->stream[dir].list = list; 1475fcdbb2dSRanjani Sridharan 1485fcdbb2dSRanjani Sridharan ret = sof_widget_list_setup(sdev, spcm, dir); 1495fcdbb2dSRanjani Sridharan if (ret < 0) { 1505fcdbb2dSRanjani Sridharan dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n", 1515fcdbb2dSRanjani Sridharan spcm->pcm.pcm_id, dir); 1525fcdbb2dSRanjani Sridharan spcm->stream[dir].list = NULL; 1535fcdbb2dSRanjani Sridharan snd_soc_dapm_dai_free_widgets(&list); 1545fcdbb2dSRanjani Sridharan return ret; 1555fcdbb2dSRanjani Sridharan } 1565fcdbb2dSRanjani Sridharan } 1575fcdbb2dSRanjani Sridharan 1585fcdbb2dSRanjani Sridharan return 0; 1595fcdbb2dSRanjani Sridharan } 1605fcdbb2dSRanjani Sridharan 1611c91d77eSKuninori Morimoto static int sof_pcm_hw_params(struct snd_soc_component *component, 1621c91d77eSKuninori Morimoto struct snd_pcm_substream *substream, 163868bd00fSLiam Girdwood struct snd_pcm_hw_params *params) 164868bd00fSLiam Girdwood { 1651205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 166868bd00fSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 167868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 168868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 169868bd00fSLiam Girdwood struct sof_ipc_pcm_params pcm; 170868bd00fSLiam Girdwood struct sof_ipc_pcm_params_reply ipc_params_reply; 171868bd00fSLiam Girdwood int ret; 172868bd00fSLiam Girdwood 173868bd00fSLiam Girdwood /* nothing to do for BE */ 174868bd00fSLiam Girdwood if (rtd->dai_link->no_pcm) 175868bd00fSLiam Girdwood return 0; 176868bd00fSLiam Girdwood 177ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 178868bd00fSLiam Girdwood if (!spcm) 179868bd00fSLiam Girdwood return -EINVAL; 180868bd00fSLiam Girdwood 181cfe8191bSKai Vehmanen /* 182cfe8191bSKai Vehmanen * Handle repeated calls to hw_params() without free_pcm() in 183cfe8191bSKai Vehmanen * between. At least ALSA OSS emulation depends on this. 184cfe8191bSKai Vehmanen */ 185cfe8191bSKai Vehmanen ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm); 186cfe8191bSKai Vehmanen if (ret < 0) 187cfe8191bSKai Vehmanen return ret; 188cfe8191bSKai Vehmanen 189ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n", 190868bd00fSLiam Girdwood spcm->pcm.pcm_id, substream->stream); 191868bd00fSLiam Girdwood 192868bd00fSLiam Girdwood memset(&pcm, 0, sizeof(pcm)); 193868bd00fSLiam Girdwood 19457e960f0STakashi Iwai /* create compressed page table for audio firmware */ 19557e960f0STakashi Iwai if (runtime->buffer_changed) { 1961c91d77eSKuninori Morimoto ret = create_page_table(component, substream, runtime->dma_area, 197868bd00fSLiam Girdwood runtime->dma_bytes); 198868bd00fSLiam Girdwood if (ret < 0) 199868bd00fSLiam Girdwood return ret; 200868bd00fSLiam Girdwood } 201868bd00fSLiam Girdwood 202868bd00fSLiam Girdwood /* number of pages should be rounded up */ 203868bd00fSLiam Girdwood pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes); 204868bd00fSLiam Girdwood 205868bd00fSLiam Girdwood /* set IPC PCM parameters */ 206868bd00fSLiam Girdwood pcm.hdr.size = sizeof(pcm); 207868bd00fSLiam Girdwood pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS; 208868bd00fSLiam Girdwood pcm.comp_id = spcm->stream[substream->stream].comp_id; 209868bd00fSLiam Girdwood pcm.params.hdr.size = sizeof(pcm.params); 210868bd00fSLiam Girdwood pcm.params.buffer.phy_addr = 211868bd00fSLiam Girdwood spcm->stream[substream->stream].page_table.addr; 212868bd00fSLiam Girdwood pcm.params.buffer.size = runtime->dma_bytes; 213868bd00fSLiam Girdwood pcm.params.direction = substream->stream; 214868bd00fSLiam Girdwood pcm.params.sample_valid_bytes = params_width(params) >> 3; 215868bd00fSLiam Girdwood pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED; 216868bd00fSLiam Girdwood pcm.params.rate = params_rate(params); 217868bd00fSLiam Girdwood pcm.params.channels = params_channels(params); 218868bd00fSLiam Girdwood pcm.params.host_period_bytes = params_period_bytes(params); 219868bd00fSLiam Girdwood 220868bd00fSLiam Girdwood /* container size */ 221868bd00fSLiam Girdwood ret = snd_pcm_format_physical_width(params_format(params)); 222868bd00fSLiam Girdwood if (ret < 0) 223868bd00fSLiam Girdwood return ret; 224868bd00fSLiam Girdwood pcm.params.sample_container_bytes = ret >> 3; 225868bd00fSLiam Girdwood 226868bd00fSLiam Girdwood /* format */ 227868bd00fSLiam Girdwood switch (params_format(params)) { 228868bd00fSLiam Girdwood case SNDRV_PCM_FORMAT_S16: 229868bd00fSLiam Girdwood pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE; 230868bd00fSLiam Girdwood break; 231868bd00fSLiam Girdwood case SNDRV_PCM_FORMAT_S24: 232868bd00fSLiam Girdwood pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE; 233868bd00fSLiam Girdwood break; 234868bd00fSLiam Girdwood case SNDRV_PCM_FORMAT_S32: 235868bd00fSLiam Girdwood pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE; 236868bd00fSLiam Girdwood break; 237868bd00fSLiam Girdwood case SNDRV_PCM_FORMAT_FLOAT: 238868bd00fSLiam Girdwood pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT; 239868bd00fSLiam Girdwood break; 240868bd00fSLiam Girdwood default: 241868bd00fSLiam Girdwood return -EINVAL; 242868bd00fSLiam Girdwood } 243868bd00fSLiam Girdwood 244868bd00fSLiam Girdwood /* firmware already configured host stream */ 245868bd00fSLiam Girdwood ret = snd_sof_pcm_platform_hw_params(sdev, 246868bd00fSLiam Girdwood substream, 247868bd00fSLiam Girdwood params, 248868bd00fSLiam Girdwood &pcm.params); 249868bd00fSLiam Girdwood if (ret < 0) { 250ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: platform hw params failed\n"); 251868bd00fSLiam Girdwood return ret; 252868bd00fSLiam Girdwood } 253868bd00fSLiam Girdwood 254ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag); 255868bd00fSLiam Girdwood 2565fcdbb2dSRanjani Sridharan /* if this is a repeated hw_params without hw_free, skip setting up widgets */ 2575fcdbb2dSRanjani Sridharan if (!spcm->stream[substream->stream].list) { 2585fcdbb2dSRanjani Sridharan ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, substream->stream); 2595fcdbb2dSRanjani Sridharan if (ret < 0) 2605fcdbb2dSRanjani Sridharan return ret; 2615fcdbb2dSRanjani Sridharan } 2625fcdbb2dSRanjani Sridharan 2635fcdbb2dSRanjani Sridharan /* send hw_params IPC to the DSP */ 264868bd00fSLiam Girdwood ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm), 265868bd00fSLiam Girdwood &ipc_params_reply, sizeof(ipc_params_reply)); 266868bd00fSLiam Girdwood if (ret < 0) { 267ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: hw params ipc failed for stream %d\n", 268868bd00fSLiam Girdwood pcm.params.stream_tag); 269868bd00fSLiam Girdwood return ret; 270868bd00fSLiam Girdwood } 271868bd00fSLiam Girdwood 272868bd00fSLiam Girdwood ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply); 273868bd00fSLiam Girdwood if (ret < 0) 274868bd00fSLiam Girdwood return ret; 275868bd00fSLiam Girdwood 27604c80277SKai Vehmanen spcm->prepared[substream->stream] = true; 27704c80277SKai Vehmanen 278868bd00fSLiam Girdwood /* save pcm hw_params */ 279868bd00fSLiam Girdwood memcpy(&spcm->params[substream->stream], params, sizeof(*params)); 280868bd00fSLiam Girdwood 281868bd00fSLiam Girdwood return ret; 282868bd00fSLiam Girdwood } 283868bd00fSLiam Girdwood 2841c91d77eSKuninori Morimoto static int sof_pcm_hw_free(struct snd_soc_component *component, 2851c91d77eSKuninori Morimoto struct snd_pcm_substream *substream) 286868bd00fSLiam Girdwood { 2871205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 288868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 289868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 290e66e52c5SKai Vehmanen int ret, err = 0; 291868bd00fSLiam Girdwood 292868bd00fSLiam Girdwood /* nothing to do for BE */ 293868bd00fSLiam Girdwood if (rtd->dai_link->no_pcm) 294868bd00fSLiam Girdwood return 0; 295868bd00fSLiam Girdwood 296ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 297868bd00fSLiam Girdwood if (!spcm) 298868bd00fSLiam Girdwood return -EINVAL; 299868bd00fSLiam Girdwood 300ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "pcm: free stream %d dir %d\n", 301ee1e79b7SRanjani Sridharan spcm->pcm.pcm_id, substream->stream); 302868bd00fSLiam Girdwood 3030b639dcdSRanjani Sridharan /* free PCM in the DSP */ 304a49b6871SKai Vehmanen ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm); 305e66e52c5SKai Vehmanen if (ret < 0) 306e66e52c5SKai Vehmanen err = ret; 307868bd00fSLiam Girdwood 3085fcdbb2dSRanjani Sridharan 3090b639dcdSRanjani Sridharan /* stop DMA */ 31093146bc2SRanjani Sridharan ret = snd_sof_pcm_platform_hw_free(sdev, substream); 311e66e52c5SKai Vehmanen if (ret < 0) { 312ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: platform hw free failed\n"); 313e66e52c5SKai Vehmanen err = ret; 314e66e52c5SKai Vehmanen } 31593146bc2SRanjani Sridharan 3160b639dcdSRanjani Sridharan /* free the DAPM widget list */ 3170b639dcdSRanjani Sridharan ret = sof_widget_list_free(sdev, spcm, substream->stream); 3180b639dcdSRanjani Sridharan if (ret < 0) 3190b639dcdSRanjani Sridharan err = ret; 3200b639dcdSRanjani Sridharan 3210b639dcdSRanjani Sridharan cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work); 3220b639dcdSRanjani Sridharan 323e66e52c5SKai Vehmanen return err; 324868bd00fSLiam Girdwood } 325868bd00fSLiam Girdwood 3261c91d77eSKuninori Morimoto static int sof_pcm_prepare(struct snd_soc_component *component, 3271c91d77eSKuninori Morimoto struct snd_pcm_substream *substream) 328868bd00fSLiam Girdwood { 3291205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 330868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 331868bd00fSLiam Girdwood int ret; 332868bd00fSLiam Girdwood 333868bd00fSLiam Girdwood /* nothing to do for BE */ 334868bd00fSLiam Girdwood if (rtd->dai_link->no_pcm) 335868bd00fSLiam Girdwood return 0; 336868bd00fSLiam Girdwood 337ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 338868bd00fSLiam Girdwood if (!spcm) 339868bd00fSLiam Girdwood return -EINVAL; 340868bd00fSLiam Girdwood 34104c80277SKai Vehmanen if (spcm->prepared[substream->stream]) 342868bd00fSLiam Girdwood return 0; 343868bd00fSLiam Girdwood 344ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n", 345ee1e79b7SRanjani Sridharan spcm->pcm.pcm_id, substream->stream); 346868bd00fSLiam Girdwood 347868bd00fSLiam Girdwood /* set hw_params */ 3481c91d77eSKuninori Morimoto ret = sof_pcm_hw_params(component, 3491c91d77eSKuninori Morimoto substream, &spcm->params[substream->stream]); 350868bd00fSLiam Girdwood if (ret < 0) { 351ee1e79b7SRanjani Sridharan dev_err(component->dev, 352ee1e79b7SRanjani Sridharan "error: set pcm hw_params after resume\n"); 353868bd00fSLiam Girdwood return ret; 354868bd00fSLiam Girdwood } 355868bd00fSLiam Girdwood 356868bd00fSLiam Girdwood return 0; 357868bd00fSLiam Girdwood } 358868bd00fSLiam Girdwood 359868bd00fSLiam Girdwood /* 360868bd00fSLiam Girdwood * FE dai link trigger actions are always executed in non-atomic context because 361868bd00fSLiam Girdwood * they involve IPC's. 362868bd00fSLiam Girdwood */ 3631c91d77eSKuninori Morimoto static int sof_pcm_trigger(struct snd_soc_component *component, 3641c91d77eSKuninori Morimoto struct snd_pcm_substream *substream, int cmd) 365868bd00fSLiam Girdwood { 3661205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 367868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 368868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 369868bd00fSLiam Girdwood struct sof_ipc_stream stream; 370868bd00fSLiam Girdwood struct sof_ipc_reply reply; 37104c80277SKai Vehmanen bool reset_hw_params = false; 3725fcdbb2dSRanjani Sridharan bool free_widget_list = false; 3730a1b0834SPan Xiuli bool ipc_first = false; 374868bd00fSLiam Girdwood int ret; 375868bd00fSLiam Girdwood 376868bd00fSLiam Girdwood /* nothing to do for BE */ 377868bd00fSLiam Girdwood if (rtd->dai_link->no_pcm) 378868bd00fSLiam Girdwood return 0; 379868bd00fSLiam Girdwood 380ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 381868bd00fSLiam Girdwood if (!spcm) 382868bd00fSLiam Girdwood return -EINVAL; 383868bd00fSLiam Girdwood 384ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n", 385868bd00fSLiam Girdwood spcm->pcm.pcm_id, substream->stream, cmd); 386868bd00fSLiam Girdwood 387868bd00fSLiam Girdwood stream.hdr.size = sizeof(stream); 388868bd00fSLiam Girdwood stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG; 389868bd00fSLiam Girdwood stream.comp_id = spcm->stream[substream->stream].comp_id; 390868bd00fSLiam Girdwood 391868bd00fSLiam Girdwood switch (cmd) { 392868bd00fSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 393868bd00fSLiam Girdwood stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE; 3940a1b0834SPan Xiuli ipc_first = true; 395868bd00fSLiam Girdwood break; 396868bd00fSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 397868bd00fSLiam Girdwood stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE; 398868bd00fSLiam Girdwood break; 399868bd00fSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 400ac8c046fSKeyon Jie if (spcm->stream[substream->stream].suspend_ignored) { 401ac8c046fSKeyon Jie /* 402ac8c046fSKeyon Jie * This case will be triggered when INFO_RESUME is 403ac8c046fSKeyon Jie * not supported, no need to re-start streams that 404ac8c046fSKeyon Jie * remained enabled in D0ix. 405ac8c046fSKeyon Jie */ 406ac8c046fSKeyon Jie spcm->stream[substream->stream].suspend_ignored = false; 407ac8c046fSKeyon Jie return 0; 408ac8c046fSKeyon Jie } 409868bd00fSLiam Girdwood stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START; 410868bd00fSLiam Girdwood break; 411868bd00fSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 412043ae13bSRanjani Sridharan if (sdev->system_suspend_target == SOF_SUSPEND_S0IX && 413ac8c046fSKeyon Jie spcm->stream[substream->stream].d0i3_compatible) { 414ac8c046fSKeyon Jie /* 415ac8c046fSKeyon Jie * trap the event, not sending trigger stop to 416ac8c046fSKeyon Jie * prevent the FW pipelines from being stopped, 417ac8c046fSKeyon Jie * and mark the flag to ignore the upcoming DAPM 418ac8c046fSKeyon Jie * PM events. 419ac8c046fSKeyon Jie */ 420ac8c046fSKeyon Jie spcm->stream[substream->stream].suspend_ignored = true; 421ac8c046fSKeyon Jie return 0; 422ac8c046fSKeyon Jie } 4235fcdbb2dSRanjani Sridharan free_widget_list = true; 424df561f66SGustavo A. R. Silva fallthrough; 425868bd00fSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 426868bd00fSLiam Girdwood stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP; 4270a1b0834SPan Xiuli ipc_first = true; 42804c80277SKai Vehmanen reset_hw_params = true; 429868bd00fSLiam Girdwood break; 430868bd00fSLiam Girdwood default: 431ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: unhandled trigger cmd %d\n", 432ee1e79b7SRanjani Sridharan cmd); 433868bd00fSLiam Girdwood return -EINVAL; 434868bd00fSLiam Girdwood } 435868bd00fSLiam Girdwood 4360a1b0834SPan Xiuli /* 4370a1b0834SPan Xiuli * DMA and IPC sequence is different for start and stop. Need to send 4380a1b0834SPan Xiuli * STOP IPC before stop DMA 4390a1b0834SPan Xiuli */ 4400a1b0834SPan Xiuli if (!ipc_first) 441868bd00fSLiam Girdwood snd_sof_pcm_platform_trigger(sdev, substream, cmd); 442868bd00fSLiam Girdwood 443868bd00fSLiam Girdwood /* send IPC to the DSP */ 444868bd00fSLiam Girdwood ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream, 445868bd00fSLiam Girdwood sizeof(stream), &reply, sizeof(reply)); 446868bd00fSLiam Girdwood 4470a1b0834SPan Xiuli /* need to STOP DMA even if STOP IPC failed */ 4480a1b0834SPan Xiuli if (ipc_first) 4490a1b0834SPan Xiuli snd_sof_pcm_platform_trigger(sdev, substream, cmd); 4500a1b0834SPan Xiuli 4510a1b0834SPan Xiuli /* free PCM if reset_hw_params is set and the STOP IPC is successful */ 4525fcdbb2dSRanjani Sridharan if (!ret && reset_hw_params) { 453d9a72465SRanjani Sridharan ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, 454d9a72465SRanjani Sridharan free_widget_list); 4555fcdbb2dSRanjani Sridharan if (ret < 0) 4565fcdbb2dSRanjani Sridharan return ret; 4575fcdbb2dSRanjani Sridharan } 458a49b6871SKai Vehmanen 459868bd00fSLiam Girdwood return ret; 460868bd00fSLiam Girdwood } 461868bd00fSLiam Girdwood 4621c91d77eSKuninori Morimoto static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component, 4631c91d77eSKuninori Morimoto struct snd_pcm_substream *substream) 464868bd00fSLiam Girdwood { 4651205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 466868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 467868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 468868bd00fSLiam Girdwood snd_pcm_uframes_t host, dai; 469868bd00fSLiam Girdwood 470868bd00fSLiam Girdwood /* nothing to do for BE */ 471868bd00fSLiam Girdwood if (rtd->dai_link->no_pcm) 472868bd00fSLiam Girdwood return 0; 473868bd00fSLiam Girdwood 474868bd00fSLiam Girdwood /* use dsp ops pointer callback directly if set */ 475868bd00fSLiam Girdwood if (sof_ops(sdev)->pcm_pointer) 476868bd00fSLiam Girdwood return sof_ops(sdev)->pcm_pointer(sdev, substream); 477868bd00fSLiam Girdwood 478ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 479868bd00fSLiam Girdwood if (!spcm) 480868bd00fSLiam Girdwood return -EINVAL; 481868bd00fSLiam Girdwood 482868bd00fSLiam Girdwood /* read position from DSP */ 483868bd00fSLiam Girdwood host = bytes_to_frames(substream->runtime, 484868bd00fSLiam Girdwood spcm->stream[substream->stream].posn.host_posn); 485868bd00fSLiam Girdwood dai = bytes_to_frames(substream->runtime, 486868bd00fSLiam Girdwood spcm->stream[substream->stream].posn.dai_posn); 487868bd00fSLiam Girdwood 488277ff236SPierre-Louis Bossart dev_vdbg(component->dev, 489ee1e79b7SRanjani Sridharan "PCM: stream %d dir %d DMA position %lu DAI position %lu\n", 490868bd00fSLiam Girdwood spcm->pcm.pcm_id, substream->stream, host, dai); 491868bd00fSLiam Girdwood 492868bd00fSLiam Girdwood return host; 493868bd00fSLiam Girdwood } 494868bd00fSLiam Girdwood 4951c91d77eSKuninori Morimoto static int sof_pcm_open(struct snd_soc_component *component, 4961c91d77eSKuninori Morimoto struct snd_pcm_substream *substream) 497868bd00fSLiam Girdwood { 4981205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 499868bd00fSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 500868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 50127e322faSPierre-Louis Bossart const struct snd_sof_dsp_ops *ops = sof_ops(sdev); 502868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 503868bd00fSLiam Girdwood struct snd_soc_tplg_stream_caps *caps; 504868bd00fSLiam Girdwood int ret; 505868bd00fSLiam Girdwood 506868bd00fSLiam Girdwood /* nothing to do for BE */ 507868bd00fSLiam Girdwood if (rtd->dai_link->no_pcm) 508868bd00fSLiam Girdwood return 0; 509868bd00fSLiam Girdwood 510ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 511868bd00fSLiam Girdwood if (!spcm) 512868bd00fSLiam Girdwood return -EINVAL; 513868bd00fSLiam Girdwood 514ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "pcm: open stream %d dir %d\n", 515ee1e79b7SRanjani Sridharan spcm->pcm.pcm_id, substream->stream); 516868bd00fSLiam Girdwood 517868bd00fSLiam Girdwood 518868bd00fSLiam Girdwood caps = &spcm->pcm.caps[substream->stream]; 519868bd00fSLiam Girdwood 520868bd00fSLiam Girdwood /* set runtime config */ 52127e322faSPierre-Louis Bossart runtime->hw.info = ops->hw_info; /* platform-specific */ 52227e322faSPierre-Louis Bossart 5239983ac49SKai Vehmanen /* set any runtime constraints based on topology */ 524868bd00fSLiam Girdwood runtime->hw.formats = le64_to_cpu(caps->formats); 525868bd00fSLiam Girdwood runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min); 526868bd00fSLiam Girdwood runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max); 527868bd00fSLiam Girdwood runtime->hw.periods_min = le32_to_cpu(caps->periods_min); 528868bd00fSLiam Girdwood runtime->hw.periods_max = le32_to_cpu(caps->periods_max); 529868bd00fSLiam Girdwood 530868bd00fSLiam Girdwood /* 531868bd00fSLiam Girdwood * caps->buffer_size_min is not used since the 532868bd00fSLiam Girdwood * snd_pcm_hardware structure only defines buffer_bytes_max 533868bd00fSLiam Girdwood */ 534868bd00fSLiam Girdwood runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max); 535868bd00fSLiam Girdwood 536ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "period min %zd max %zd bytes\n", 537868bd00fSLiam Girdwood runtime->hw.period_bytes_min, 538868bd00fSLiam Girdwood runtime->hw.period_bytes_max); 539ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "period count %d max %d\n", 540868bd00fSLiam Girdwood runtime->hw.periods_min, 541868bd00fSLiam Girdwood runtime->hw.periods_max); 542ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "buffer max %zd bytes\n", 543868bd00fSLiam Girdwood runtime->hw.buffer_bytes_max); 544868bd00fSLiam Girdwood 545868bd00fSLiam Girdwood /* set wait time - TODO: come from topology */ 546868bd00fSLiam Girdwood substream->wait_time = 500; 547868bd00fSLiam Girdwood 548868bd00fSLiam Girdwood spcm->stream[substream->stream].posn.host_posn = 0; 549868bd00fSLiam Girdwood spcm->stream[substream->stream].posn.dai_posn = 0; 550868bd00fSLiam Girdwood spcm->stream[substream->stream].substream = substream; 55104c80277SKai Vehmanen spcm->prepared[substream->stream] = false; 552868bd00fSLiam Girdwood 553868bd00fSLiam Girdwood ret = snd_sof_pcm_platform_open(sdev, substream); 55414a2212dSRanjani Sridharan if (ret < 0) 555ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: pcm open failed %d\n", ret); 556868bd00fSLiam Girdwood 557868bd00fSLiam Girdwood return ret; 558868bd00fSLiam Girdwood } 559868bd00fSLiam Girdwood 5601c91d77eSKuninori Morimoto static int sof_pcm_close(struct snd_soc_component *component, 5611c91d77eSKuninori Morimoto struct snd_pcm_substream *substream) 562868bd00fSLiam Girdwood { 5631205300aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 564868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 565868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 566868bd00fSLiam Girdwood int err; 567868bd00fSLiam Girdwood 568868bd00fSLiam Girdwood /* nothing to do for BE */ 569868bd00fSLiam Girdwood if (rtd->dai_link->no_pcm) 570868bd00fSLiam Girdwood return 0; 571868bd00fSLiam Girdwood 572ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 573868bd00fSLiam Girdwood if (!spcm) 574868bd00fSLiam Girdwood return -EINVAL; 575868bd00fSLiam Girdwood 576ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "pcm: close stream %d dir %d\n", 577ee1e79b7SRanjani Sridharan spcm->pcm.pcm_id, substream->stream); 578868bd00fSLiam Girdwood 579868bd00fSLiam Girdwood err = snd_sof_pcm_platform_close(sdev, substream); 580868bd00fSLiam Girdwood if (err < 0) { 581ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: pcm close failed %d\n", 582868bd00fSLiam Girdwood err); 583868bd00fSLiam Girdwood /* 584868bd00fSLiam Girdwood * keep going, no point in preventing the close 585868bd00fSLiam Girdwood * from happening 586868bd00fSLiam Girdwood */ 587868bd00fSLiam Girdwood } 588868bd00fSLiam Girdwood 589868bd00fSLiam Girdwood return 0; 590868bd00fSLiam Girdwood } 591868bd00fSLiam Girdwood 592868bd00fSLiam Girdwood /* 593868bd00fSLiam Girdwood * Pre-allocate playback/capture audio buffer pages. 594868bd00fSLiam Girdwood * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free 595868bd00fSLiam Girdwood * snd_pcm_lib_preallocate_free_for_all() is called by the core. 596868bd00fSLiam Girdwood */ 5971c91d77eSKuninori Morimoto static int sof_pcm_new(struct snd_soc_component *component, 5981c91d77eSKuninori Morimoto struct snd_soc_pcm_runtime *rtd) 599868bd00fSLiam Girdwood { 600868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 601868bd00fSLiam Girdwood struct snd_sof_pcm *spcm; 602868bd00fSLiam Girdwood struct snd_pcm *pcm = rtd->pcm; 603868bd00fSLiam Girdwood struct snd_soc_tplg_stream_caps *caps; 604868bd00fSLiam Girdwood int stream = SNDRV_PCM_STREAM_PLAYBACK; 605868bd00fSLiam Girdwood 606868bd00fSLiam Girdwood /* find SOF PCM for this RTD */ 607ee1e79b7SRanjani Sridharan spcm = snd_sof_find_spcm_dai(component, rtd); 608868bd00fSLiam Girdwood if (!spcm) { 609ee1e79b7SRanjani Sridharan dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n", 610868bd00fSLiam Girdwood rtd->dai_link->id); 611868bd00fSLiam Girdwood return 0; 612868bd00fSLiam Girdwood } 613868bd00fSLiam Girdwood 614ee1e79b7SRanjani Sridharan dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name); 615868bd00fSLiam Girdwood 616868bd00fSLiam Girdwood /* do we need to pre-allocate playback audio buffer pages */ 617868bd00fSLiam Girdwood if (!spcm->pcm.playback) 618868bd00fSLiam Girdwood goto capture; 619868bd00fSLiam Girdwood 620868bd00fSLiam Girdwood caps = &spcm->pcm.caps[stream]; 621868bd00fSLiam Girdwood 622868bd00fSLiam Girdwood /* pre-allocate playback audio buffer pages */ 623ee1e79b7SRanjani Sridharan dev_dbg(component->dev, 624ee1e79b7SRanjani Sridharan "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n", 625868bd00fSLiam Girdwood caps->name, caps->buffer_size_min, caps->buffer_size_max); 626868bd00fSLiam Girdwood 6274f7f9564SGuennadi Liakhovetski if (!pcm->streams[stream].substream) { 6284f7f9564SGuennadi Liakhovetski dev_err(component->dev, "error: NULL playback substream!\n"); 6294f7f9564SGuennadi Liakhovetski return -EINVAL; 6304f7f9564SGuennadi Liakhovetski } 6314f7f9564SGuennadi Liakhovetski 63257e960f0STakashi Iwai snd_pcm_set_managed_buffer(pcm->streams[stream].substream, 633868bd00fSLiam Girdwood SNDRV_DMA_TYPE_DEV_SG, sdev->dev, 634e582f483SKeyon Jie 0, le32_to_cpu(caps->buffer_size_max)); 635868bd00fSLiam Girdwood capture: 636868bd00fSLiam Girdwood stream = SNDRV_PCM_STREAM_CAPTURE; 637868bd00fSLiam Girdwood 638868bd00fSLiam Girdwood /* do we need to pre-allocate capture audio buffer pages */ 639868bd00fSLiam Girdwood if (!spcm->pcm.capture) 640868bd00fSLiam Girdwood return 0; 641868bd00fSLiam Girdwood 642868bd00fSLiam Girdwood caps = &spcm->pcm.caps[stream]; 643868bd00fSLiam Girdwood 644868bd00fSLiam Girdwood /* pre-allocate capture audio buffer pages */ 645ee1e79b7SRanjani Sridharan dev_dbg(component->dev, 646ee1e79b7SRanjani Sridharan "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n", 647868bd00fSLiam Girdwood caps->name, caps->buffer_size_min, caps->buffer_size_max); 648868bd00fSLiam Girdwood 6494f7f9564SGuennadi Liakhovetski if (!pcm->streams[stream].substream) { 6504f7f9564SGuennadi Liakhovetski dev_err(component->dev, "error: NULL capture substream!\n"); 6514f7f9564SGuennadi Liakhovetski return -EINVAL; 6524f7f9564SGuennadi Liakhovetski } 6534f7f9564SGuennadi Liakhovetski 65457e960f0STakashi Iwai snd_pcm_set_managed_buffer(pcm->streams[stream].substream, 655868bd00fSLiam Girdwood SNDRV_DMA_TYPE_DEV_SG, sdev->dev, 656e582f483SKeyon Jie 0, le32_to_cpu(caps->buffer_size_max)); 657868bd00fSLiam Girdwood 658868bd00fSLiam Girdwood return 0; 659868bd00fSLiam Girdwood } 660868bd00fSLiam Girdwood 661c943a586SJaska Uimonen static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name, 662c943a586SJaska Uimonen struct snd_pcm_hw_params *params) 663c943a586SJaska Uimonen { 664c943a586SJaska Uimonen struct sof_ipc_dai_config *config; 665c943a586SJaska Uimonen struct snd_sof_dai *dai; 666c943a586SJaska Uimonen int i; 667c943a586SJaska Uimonen 668c943a586SJaska Uimonen /* 669c943a586SJaska Uimonen * Search for all matching DAIs as we can have both playback and capture DAI 670c943a586SJaska Uimonen * associated with the same link. 671c943a586SJaska Uimonen */ 672c943a586SJaska Uimonen list_for_each_entry(dai, &sdev->dai_list, list) { 673c943a586SJaska Uimonen if (!dai->name || strcmp(link_name, dai->name)) 674c943a586SJaska Uimonen continue; 675c943a586SJaska Uimonen for (i = 0; i < dai->number_configs; i++) { 676c943a586SJaska Uimonen config = &dai->dai_config[i]; 677c943a586SJaska Uimonen if (config->ssp.fsync_rate == params_rate(params)) { 678c943a586SJaska Uimonen dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i); 679c943a586SJaska Uimonen dai->current_config = i; 680c943a586SJaska Uimonen break; 681c943a586SJaska Uimonen } 682c943a586SJaska Uimonen } 683c943a586SJaska Uimonen } 684c943a586SJaska Uimonen } 685c943a586SJaska Uimonen 686868bd00fSLiam Girdwood /* fixup the BE DAI link to match any values from topology */ 687f805e7e0SRanjani Sridharan int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params) 688868bd00fSLiam Girdwood { 689868bd00fSLiam Girdwood struct snd_interval *rate = hw_param_interval(params, 690868bd00fSLiam Girdwood SNDRV_PCM_HW_PARAM_RATE); 691868bd00fSLiam Girdwood struct snd_interval *channels = hw_param_interval(params, 692868bd00fSLiam Girdwood SNDRV_PCM_HW_PARAM_CHANNELS); 693868bd00fSLiam Girdwood struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 694868bd00fSLiam Girdwood struct snd_soc_component *component = 695ee1e79b7SRanjani Sridharan snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 696868bd00fSLiam Girdwood struct snd_sof_dai *dai = 697ee1e79b7SRanjani Sridharan snd_sof_find_dai(component, (char *)rtd->dai_link->name); 698c943a586SJaska Uimonen struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 699fd045558Sranderwang struct snd_soc_dpcm *dpcm; 700868bd00fSLiam Girdwood 701868bd00fSLiam Girdwood /* no topology exists for this BE, try a common configuration */ 702868bd00fSLiam Girdwood if (!dai) { 703ee1e79b7SRanjani Sridharan dev_warn(component->dev, 704ee1e79b7SRanjani Sridharan "warning: no topology found for BE DAI %s config\n", 705868bd00fSLiam Girdwood rtd->dai_link->name); 706868bd00fSLiam Girdwood 707868bd00fSLiam Girdwood /* set 48k, stereo, 16bits by default */ 708868bd00fSLiam Girdwood rate->min = 48000; 709868bd00fSLiam Girdwood rate->max = 48000; 710868bd00fSLiam Girdwood 711868bd00fSLiam Girdwood channels->min = 2; 712868bd00fSLiam Girdwood channels->max = 2; 713868bd00fSLiam Girdwood 714868bd00fSLiam Girdwood snd_mask_none(fmt); 715868bd00fSLiam Girdwood snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); 716868bd00fSLiam Girdwood 717868bd00fSLiam Girdwood return 0; 718868bd00fSLiam Girdwood } 719868bd00fSLiam Girdwood 720868bd00fSLiam Girdwood /* read format from topology */ 721868bd00fSLiam Girdwood snd_mask_none(fmt); 722868bd00fSLiam Girdwood 723868bd00fSLiam Girdwood switch (dai->comp_dai.config.frame_fmt) { 724868bd00fSLiam Girdwood case SOF_IPC_FRAME_S16_LE: 725868bd00fSLiam Girdwood snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); 726868bd00fSLiam Girdwood break; 727868bd00fSLiam Girdwood case SOF_IPC_FRAME_S24_4LE: 728868bd00fSLiam Girdwood snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 729868bd00fSLiam Girdwood break; 730868bd00fSLiam Girdwood case SOF_IPC_FRAME_S32_LE: 731868bd00fSLiam Girdwood snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE); 732868bd00fSLiam Girdwood break; 733868bd00fSLiam Girdwood default: 734ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: No available DAI format!\n"); 735868bd00fSLiam Girdwood return -EINVAL; 736868bd00fSLiam Girdwood } 737868bd00fSLiam Girdwood 738868bd00fSLiam Girdwood /* read rate and channels from topology */ 739868bd00fSLiam Girdwood switch (dai->dai_config->type) { 740868bd00fSLiam Girdwood case SOF_DAI_INTEL_SSP: 741c943a586SJaska Uimonen /* search for config to pcm params match, if not found use default */ 742c943a586SJaska Uimonen ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params); 743c943a586SJaska Uimonen 744c1c03888SJaska Uimonen rate->min = dai->dai_config[dai->current_config].ssp.fsync_rate; 745c1c03888SJaska Uimonen rate->max = dai->dai_config[dai->current_config].ssp.fsync_rate; 746c1c03888SJaska Uimonen channels->min = dai->dai_config[dai->current_config].ssp.tdm_slots; 747c1c03888SJaska Uimonen channels->max = dai->dai_config[dai->current_config].ssp.tdm_slots; 748868bd00fSLiam Girdwood 749ee1e79b7SRanjani Sridharan dev_dbg(component->dev, 750868bd00fSLiam Girdwood "rate_min: %d rate_max: %d\n", rate->min, rate->max); 751ee1e79b7SRanjani Sridharan dev_dbg(component->dev, 752868bd00fSLiam Girdwood "channels_min: %d channels_max: %d\n", 753868bd00fSLiam Girdwood channels->min, channels->max); 754868bd00fSLiam Girdwood 755868bd00fSLiam Girdwood break; 756868bd00fSLiam Girdwood case SOF_DAI_INTEL_DMIC: 757868bd00fSLiam Girdwood /* DMIC only supports 16 or 32 bit formats */ 758868bd00fSLiam Girdwood if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) { 759ee1e79b7SRanjani Sridharan dev_err(component->dev, 760868bd00fSLiam Girdwood "error: invalid fmt %d for DAI type %d\n", 761868bd00fSLiam Girdwood dai->comp_dai.config.frame_fmt, 762868bd00fSLiam Girdwood dai->dai_config->type); 763868bd00fSLiam Girdwood } 764868bd00fSLiam Girdwood break; 765868bd00fSLiam Girdwood case SOF_DAI_INTEL_HDA: 766fd045558Sranderwang /* 767135ab457SPierre-Louis Bossart * HDAudio does not follow the default trigger 768fd045558Sranderwang * sequence due to firmware implementation 769fd045558Sranderwang */ 770fd045558Sranderwang for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) { 771fd045558Sranderwang struct snd_soc_pcm_runtime *fe = dpcm->fe; 772fd045558Sranderwang 773fd045558Sranderwang fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] = 774fd045558Sranderwang SND_SOC_DPCM_TRIGGER_POST; 775fd045558Sranderwang } 776868bd00fSLiam Girdwood break; 7776e3360cdSPierre-Louis Bossart case SOF_DAI_INTEL_ALH: 778e1711b1fSRander Wang /* 779e1711b1fSRander Wang * Dai could run with different channel count compared with 780e1711b1fSRander Wang * front end, so get dai channel count from topology 781e1711b1fSRander Wang */ 782e1711b1fSRander Wang channels->min = dai->dai_config->alh.channels; 783e1711b1fSRander Wang channels->max = dai->dai_config->alh.channels; 7846e3360cdSPierre-Louis Bossart break; 785a4eff5f8SDaniel Baluta case SOF_DAI_IMX_ESAI: 78651b0243aSDaniel Baluta rate->min = dai->dai_config->esai.fsync_rate; 78751b0243aSDaniel Baluta rate->max = dai->dai_config->esai.fsync_rate; 788a4eff5f8SDaniel Baluta channels->min = dai->dai_config->esai.tdm_slots; 789a4eff5f8SDaniel Baluta channels->max = dai->dai_config->esai.tdm_slots; 790a4eff5f8SDaniel Baluta 791ee1e79b7SRanjani Sridharan dev_dbg(component->dev, 79251b0243aSDaniel Baluta "rate_min: %d rate_max: %d\n", rate->min, rate->max); 79351b0243aSDaniel Baluta dev_dbg(component->dev, 794a4eff5f8SDaniel Baluta "channels_min: %d channels_max: %d\n", 795a4eff5f8SDaniel Baluta channels->min, channels->max); 796a4eff5f8SDaniel Baluta break; 797b72bfcffSYC Hung case SOF_DAI_MEDIATEK_AFE: 798b72bfcffSYC Hung rate->min = dai->dai_config->afe.rate; 799b72bfcffSYC Hung rate->max = dai->dai_config->afe.rate; 800b72bfcffSYC Hung channels->min = dai->dai_config->afe.channels; 801b72bfcffSYC Hung channels->max = dai->dai_config->afe.channels; 802b72bfcffSYC Hung 803b72bfcffSYC Hung dev_dbg(component->dev, 804b72bfcffSYC Hung "rate_min: %d rate_max: %d\n", rate->min, rate->max); 805b72bfcffSYC Hung dev_dbg(component->dev, 806b72bfcffSYC Hung "channels_min: %d channels_max: %d\n", 807b72bfcffSYC Hung channels->min, channels->max); 808b72bfcffSYC Hung break; 809d88cbd6fSGuido Roncarolo case SOF_DAI_IMX_SAI: 81051b0243aSDaniel Baluta rate->min = dai->dai_config->sai.fsync_rate; 81151b0243aSDaniel Baluta rate->max = dai->dai_config->sai.fsync_rate; 812d88cbd6fSGuido Roncarolo channels->min = dai->dai_config->sai.tdm_slots; 813d88cbd6fSGuido Roncarolo channels->max = dai->dai_config->sai.tdm_slots; 814d88cbd6fSGuido Roncarolo 815d88cbd6fSGuido Roncarolo dev_dbg(component->dev, 81651b0243aSDaniel Baluta "rate_min: %d rate_max: %d\n", rate->min, rate->max); 81751b0243aSDaniel Baluta dev_dbg(component->dev, 818d88cbd6fSGuido Roncarolo "channels_min: %d channels_max: %d\n", 819d88cbd6fSGuido Roncarolo channels->min, channels->max); 820d88cbd6fSGuido Roncarolo break; 821efb931cdSAjit Kumar Pandey case SOF_DAI_AMD_BT: 822efb931cdSAjit Kumar Pandey rate->min = dai->dai_config->acpbt.fsync_rate; 823efb931cdSAjit Kumar Pandey rate->max = dai->dai_config->acpbt.fsync_rate; 824efb931cdSAjit Kumar Pandey channels->min = dai->dai_config->acpbt.tdm_slots; 825efb931cdSAjit Kumar Pandey channels->max = dai->dai_config->acpbt.tdm_slots; 826efb931cdSAjit Kumar Pandey 827efb931cdSAjit Kumar Pandey dev_dbg(component->dev, 828efb931cdSAjit Kumar Pandey "AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max); 829efb931cdSAjit Kumar Pandey dev_dbg(component->dev, 830efb931cdSAjit Kumar Pandey "AMD_BT channels_min: %d channels_max: %d\n", 831efb931cdSAjit Kumar Pandey channels->min, channels->max); 832efb931cdSAjit Kumar Pandey break; 833efb931cdSAjit Kumar Pandey case SOF_DAI_AMD_SP: 834efb931cdSAjit Kumar Pandey rate->min = dai->dai_config->acpsp.fsync_rate; 835efb931cdSAjit Kumar Pandey rate->max = dai->dai_config->acpsp.fsync_rate; 836efb931cdSAjit Kumar Pandey channels->min = dai->dai_config->acpsp.tdm_slots; 837efb931cdSAjit Kumar Pandey channels->max = dai->dai_config->acpsp.tdm_slots; 838efb931cdSAjit Kumar Pandey 839efb931cdSAjit Kumar Pandey dev_dbg(component->dev, 840efb931cdSAjit Kumar Pandey "AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max); 841efb931cdSAjit Kumar Pandey dev_dbg(component->dev, 842efb931cdSAjit Kumar Pandey "AMD_SP channels_min: %d channels_max: %d\n", 843efb931cdSAjit Kumar Pandey channels->min, channels->max); 844efb931cdSAjit Kumar Pandey break; 845efb931cdSAjit Kumar Pandey case SOF_DAI_AMD_DMIC: 846efb931cdSAjit Kumar Pandey rate->min = dai->dai_config->acpdmic.fsync_rate; 847efb931cdSAjit Kumar Pandey rate->max = dai->dai_config->acpdmic.fsync_rate; 848efb931cdSAjit Kumar Pandey channels->min = dai->dai_config->acpdmic.tdm_slots; 849efb931cdSAjit Kumar Pandey channels->max = dai->dai_config->acpdmic.tdm_slots; 850efb931cdSAjit Kumar Pandey 851efb931cdSAjit Kumar Pandey dev_dbg(component->dev, 852efb931cdSAjit Kumar Pandey "AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max); 853efb931cdSAjit Kumar Pandey dev_dbg(component->dev, 854efb931cdSAjit Kumar Pandey "AMD_DMIC channels_min: %d channels_max: %d\n", 855efb931cdSAjit Kumar Pandey channels->min, channels->max); 856efb931cdSAjit Kumar Pandey break; 857868bd00fSLiam Girdwood default: 858ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: invalid DAI type %d\n", 859868bd00fSLiam Girdwood dai->dai_config->type); 860868bd00fSLiam Girdwood break; 861868bd00fSLiam Girdwood } 862868bd00fSLiam Girdwood 863868bd00fSLiam Girdwood return 0; 864868bd00fSLiam Girdwood } 865f3f3af17SPierre-Louis Bossart EXPORT_SYMBOL(sof_pcm_dai_link_fixup); 866868bd00fSLiam Girdwood 867868bd00fSLiam Girdwood static int sof_pcm_probe(struct snd_soc_component *component) 868868bd00fSLiam Girdwood { 869868bd00fSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 870868bd00fSLiam Girdwood struct snd_sof_pdata *plat_data = sdev->pdata; 871868bd00fSLiam Girdwood const char *tplg_filename; 872868bd00fSLiam Girdwood int ret; 873868bd00fSLiam Girdwood 874868bd00fSLiam Girdwood /* load the default topology */ 875868bd00fSLiam Girdwood sdev->component = component; 876868bd00fSLiam Girdwood 877868bd00fSLiam Girdwood tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 878868bd00fSLiam Girdwood "%s/%s", 879868bd00fSLiam Girdwood plat_data->tplg_filename_prefix, 880868bd00fSLiam Girdwood plat_data->tplg_filename); 881868bd00fSLiam Girdwood if (!tplg_filename) 882868bd00fSLiam Girdwood return -ENOMEM; 883868bd00fSLiam Girdwood 884ee1e79b7SRanjani Sridharan ret = snd_sof_load_topology(component, tplg_filename); 885868bd00fSLiam Girdwood if (ret < 0) { 886ee1e79b7SRanjani Sridharan dev_err(component->dev, "error: failed to load DSP topology %d\n", 887868bd00fSLiam Girdwood ret); 888868bd00fSLiam Girdwood return ret; 889868bd00fSLiam Girdwood } 890868bd00fSLiam Girdwood 891868bd00fSLiam Girdwood return ret; 892868bd00fSLiam Girdwood } 893868bd00fSLiam Girdwood 894868bd00fSLiam Girdwood static void sof_pcm_remove(struct snd_soc_component *component) 895868bd00fSLiam Girdwood { 896868bd00fSLiam Girdwood /* remove topology */ 897a5b8f71cSAmadeusz Sławiński snd_soc_tplg_component_remove(component); 898868bd00fSLiam Girdwood } 899868bd00fSLiam Girdwood 9004a39ea3fSRanjani Sridharan static int sof_pcm_ack(struct snd_soc_component *component, 9014a39ea3fSRanjani Sridharan struct snd_pcm_substream *substream) 9024a39ea3fSRanjani Sridharan { 9034a39ea3fSRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 9044a39ea3fSRanjani Sridharan 9054a39ea3fSRanjani Sridharan return snd_sof_pcm_platform_ack(sdev, substream); 9064a39ea3fSRanjani Sridharan } 9074a39ea3fSRanjani Sridharan 908868bd00fSLiam Girdwood void snd_sof_new_platform_drv(struct snd_sof_dev *sdev) 909868bd00fSLiam Girdwood { 910868bd00fSLiam Girdwood struct snd_soc_component_driver *pd = &sdev->plat_drv; 911868bd00fSLiam Girdwood struct snd_sof_pdata *plat_data = sdev->pdata; 912868bd00fSLiam Girdwood const char *drv_name; 913868bd00fSLiam Girdwood 914868bd00fSLiam Girdwood drv_name = plat_data->machine->drv_name; 915868bd00fSLiam Girdwood 916868bd00fSLiam Girdwood pd->name = "sof-audio-component"; 917868bd00fSLiam Girdwood pd->probe = sof_pcm_probe; 918868bd00fSLiam Girdwood pd->remove = sof_pcm_remove; 9191c91d77eSKuninori Morimoto pd->open = sof_pcm_open; 9201c91d77eSKuninori Morimoto pd->close = sof_pcm_close; 9211c91d77eSKuninori Morimoto pd->hw_params = sof_pcm_hw_params; 9221c91d77eSKuninori Morimoto pd->prepare = sof_pcm_prepare; 9231c91d77eSKuninori Morimoto pd->hw_free = sof_pcm_hw_free; 9241c91d77eSKuninori Morimoto pd->trigger = sof_pcm_trigger; 9251c91d77eSKuninori Morimoto pd->pointer = sof_pcm_pointer; 9264a39ea3fSRanjani Sridharan pd->ack = sof_pcm_ack; 9271c91d77eSKuninori Morimoto 92870368106SCezary Rojewski #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES) 92939118ce5SKuninori Morimoto pd->compress_ops = &sof_probe_compressed_ops; 93070368106SCezary Rojewski #endif 9311c91d77eSKuninori Morimoto pd->pcm_construct = sof_pcm_new; 932868bd00fSLiam Girdwood pd->ignore_machine = drv_name; 933868bd00fSLiam Girdwood pd->be_hw_params_fixup = sof_pcm_dai_link_fixup; 934868bd00fSLiam Girdwood pd->be_pcm_base = SOF_BE_PCM_BASE; 935868bd00fSLiam Girdwood pd->use_dai_pcm_id = true; 936868bd00fSLiam Girdwood pd->topology_name_prefix = "sof"; 937868bd00fSLiam Girdwood 938868bd00fSLiam Girdwood /* increment module refcount when a pcm is opened */ 939868bd00fSLiam Girdwood pd->module_get_upon_open = 1; 940868bd00fSLiam Girdwood } 941