0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <sound/pcm_params.h>
0010 #include <sound/sof/ipc4/header.h>
0011 #include "sof-audio.h"
0012 #include "sof-priv.h"
0013 #include "ipc4-priv.h"
0014 #include "ipc4-topology.h"
0015
0016 int sof_ipc4_set_pipeline_state(struct snd_sof_dev *sdev, u32 id, u32 state)
0017 {
0018 struct sof_ipc4_msg msg = {{ 0 }};
0019 u32 primary;
0020
0021 dev_dbg(sdev->dev, "ipc4 set pipeline %d state %d", id, state);
0022
0023 primary = state;
0024 primary |= SOF_IPC4_GLB_PIPE_STATE_ID(id);
0025 primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_SET_PIPELINE_STATE);
0026 primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
0027 primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);
0028
0029 msg.primary = primary;
0030
0031 return sof_ipc_tx_message(sdev->ipc, &msg, 0, NULL, 0);
0032 }
0033 EXPORT_SYMBOL(sof_ipc4_set_pipeline_state);
0034
0035 static int sof_ipc4_trigger_pipelines(struct snd_soc_component *component,
0036 struct snd_pcm_substream *substream, int state)
0037 {
0038 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
0039 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0040 struct snd_sof_widget *pipeline_widget;
0041 struct snd_soc_dapm_widget_list *list;
0042 struct snd_soc_dapm_widget *widget;
0043 struct sof_ipc4_pipeline *pipeline;
0044 struct snd_sof_widget *swidget;
0045 struct snd_sof_pcm *spcm;
0046 int ret = 0;
0047 int num_widgets;
0048
0049 spcm = snd_sof_find_spcm_dai(component, rtd);
0050 if (!spcm)
0051 return -EINVAL;
0052
0053 list = spcm->stream[substream->stream].list;
0054
0055 for_each_dapm_widgets(list, num_widgets, widget) {
0056 swidget = widget->dobj.private;
0057
0058 if (!swidget)
0059 continue;
0060
0061
0062
0063
0064
0065 switch (state) {
0066 case SOF_IPC4_PIPE_PAUSED:
0067 case SOF_IPC4_PIPE_RESET:
0068 if (!WIDGET_IS_AIF(swidget->id))
0069 continue;
0070 break;
0071 default:
0072 break;
0073 }
0074
0075
0076 pipeline_widget = swidget->pipe_widget;
0077 pipeline = (struct sof_ipc4_pipeline *)pipeline_widget->private;
0078
0079 if (pipeline->state == state)
0080 continue;
0081
0082
0083 if (pipeline->state != SOF_IPC4_PIPE_PAUSED) {
0084 ret = sof_ipc4_set_pipeline_state(sdev, swidget->pipeline_id,
0085 SOF_IPC4_PIPE_PAUSED);
0086 if (ret < 0) {
0087 dev_err(sdev->dev, "failed to pause pipeline %d\n",
0088 swidget->pipeline_id);
0089 return ret;
0090 }
0091 }
0092
0093 pipeline->state = SOF_IPC4_PIPE_PAUSED;
0094
0095 if (pipeline->state == state)
0096 continue;
0097
0098
0099 ret = sof_ipc4_set_pipeline_state(sdev, swidget->pipeline_id, state);
0100 if (ret < 0) {
0101 dev_err(sdev->dev, "failed to set state %d for pipeline %d\n",
0102 state, swidget->pipeline_id);
0103 break;
0104 }
0105
0106 pipeline->state = state;
0107 }
0108
0109 return ret;
0110 }
0111
0112 static int sof_ipc4_pcm_trigger(struct snd_soc_component *component,
0113 struct snd_pcm_substream *substream, int cmd)
0114 {
0115 int state;
0116
0117
0118 switch (cmd) {
0119 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0120 state = SOF_IPC4_PIPE_PAUSED;
0121 break;
0122 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0123 case SNDRV_PCM_TRIGGER_RESUME:
0124 case SNDRV_PCM_TRIGGER_START:
0125 state = SOF_IPC4_PIPE_RUNNING;
0126 break;
0127 case SNDRV_PCM_TRIGGER_SUSPEND:
0128 case SNDRV_PCM_TRIGGER_STOP:
0129 state = SOF_IPC4_PIPE_PAUSED;
0130 break;
0131 default:
0132 dev_err(component->dev, "%s: unhandled trigger cmd %d\n", __func__, cmd);
0133 return -EINVAL;
0134 }
0135
0136
0137 return sof_ipc4_trigger_pipelines(component, substream, state);
0138 }
0139
0140 static int sof_ipc4_pcm_hw_free(struct snd_soc_component *component,
0141 struct snd_pcm_substream *substream)
0142 {
0143 return sof_ipc4_trigger_pipelines(component, substream, SOF_IPC4_PIPE_RESET);
0144 }
0145
0146 static void ipc4_ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
0147 struct snd_pcm_hw_params *params)
0148 {
0149 struct snd_sof_dai_link *slink;
0150 struct snd_sof_dai *dai;
0151 bool dai_link_found = false;
0152 int i;
0153
0154 list_for_each_entry(slink, &sdev->dai_link_list, list) {
0155 if (!strcmp(slink->link->name, link_name)) {
0156 dai_link_found = true;
0157 break;
0158 }
0159 }
0160
0161 if (!dai_link_found)
0162 return;
0163
0164 for (i = 0; i < slink->num_hw_configs; i++) {
0165 struct snd_soc_tplg_hw_config *hw_config = &slink->hw_configs[i];
0166
0167 if (params_rate(params) == le32_to_cpu(hw_config->fsync_rate)) {
0168
0169 list_for_each_entry(dai, &sdev->dai_list, list)
0170 if (!strcmp(slink->link->name, dai->name))
0171 dai->current_config = le32_to_cpu(hw_config->id);
0172 break;
0173 }
0174 }
0175 }
0176
0177 static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
0178 struct snd_pcm_hw_params *params)
0179 {
0180 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
0181 struct snd_sof_dai *dai = snd_sof_find_dai(component, rtd->dai_link->name);
0182 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
0183 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0184 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
0185 struct sof_ipc4_copier *ipc4_copier;
0186 struct snd_soc_dpcm *dpcm;
0187
0188 if (!dai) {
0189 dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
0190 rtd->dai_link->name);
0191 return -EINVAL;
0192 }
0193
0194 ipc4_copier = dai->private;
0195 if (!ipc4_copier) {
0196 dev_err(component->dev, "%s: No private data found for DAI %s\n",
0197 __func__, rtd->dai_link->name);
0198 return -EINVAL;
0199 }
0200
0201
0202 snd_mask_none(fmt);
0203 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
0204
0205 rate->min = ipc4_copier->available_fmt.base_config->audio_fmt.sampling_frequency;
0206 rate->max = rate->min;
0207
0208
0209
0210
0211
0212
0213 for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_CAPTURE, dpcm) {
0214 struct snd_soc_pcm_runtime *fe = dpcm->fe;
0215
0216 fe->dai_link->trigger[SNDRV_PCM_STREAM_CAPTURE] = SND_SOC_DPCM_TRIGGER_PRE;
0217 }
0218
0219 switch (ipc4_copier->dai_type) {
0220 case SOF_DAI_INTEL_SSP:
0221 ipc4_ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
0222 break;
0223 default:
0224 break;
0225 }
0226
0227 return 0;
0228 }
0229
0230 const struct sof_ipc_pcm_ops ipc4_pcm_ops = {
0231 .trigger = sof_ipc4_pcm_trigger,
0232 .hw_free = sof_ipc4_pcm_hw_free,
0233 .dai_link_fixup = sof_ipc4_pcm_dai_link_fixup,
0234 };