summaryrefslogtreecommitdiff
path: root/sound/soc/sof/ipc3-pcm.c
blob: cb58ee8c158a5593fc104c4dbc3b223e94f4421b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license.  When using or
// redistributing this file, you may do so under either license.
//
// Copyright(c) 2021 Intel Corporation. All rights reserved.
//
//

#include <sound/pcm_params.h>
#include "ipc3-priv.h"
#include "ops.h"
#include "sof-priv.h"
#include "sof-audio.h"

static int sof_ipc3_pcm_hw_free(struct snd_soc_component *component,
				struct snd_pcm_substream *substream)
{
	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
	struct sof_ipc_stream stream;
	struct snd_sof_pcm *spcm;

	spcm = snd_sof_find_spcm_dai(component, rtd);
	if (!spcm)
		return -EINVAL;

	if (!spcm->prepared[substream->stream])
		return 0;

	stream.hdr.size = sizeof(stream);
	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
	stream.comp_id = spcm->stream[substream->stream].comp_id;

	/* send IPC to the DSP */
	return sof_ipc_tx_message_no_reply(sdev->ipc, &stream, sizeof(stream));
}

static int sof_ipc3_pcm_hw_params(struct snd_soc_component *component,
				  struct snd_pcm_substream *substream,
				  struct snd_pcm_hw_params *params,
				  struct snd_sof_platform_stream_params *platform_params)
{
	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct sof_ipc_pcm_params_reply ipc_params_reply;
	struct sof_ipc_pcm_params pcm;
	struct snd_sof_pcm *spcm;
	int ret;

	spcm = snd_sof_find_spcm_dai(component, rtd);
	if (!spcm)
		return -EINVAL;

	memset(&pcm, 0, sizeof(pcm));

	/* number of pages should be rounded up */
	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);

	/* set IPC PCM parameters */
	pcm.hdr.size = sizeof(pcm);
	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
	pcm.comp_id = spcm->stream[substream->stream].comp_id;
	pcm.params.hdr.size = sizeof(pcm.params);
	pcm.params.buffer.phy_addr = spcm->stream[substream->stream].page_table.addr;
	pcm.params.buffer.size = runtime->dma_bytes;
	pcm.params.direction = substream->stream;
	pcm.params.sample_valid_bytes = params_width(params) >> 3;
	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
	pcm.params.rate = params_rate(params);
	pcm.params.channels = params_channels(params);
	pcm.params.host_period_bytes = params_period_bytes(params);

	/* container size */
	ret = snd_pcm_format_physical_width(params_format(params));
	if (ret < 0)
		return ret;
	pcm.params.sample_container_bytes = ret >> 3;

	/* format */
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16:
		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
		break;
	case SNDRV_PCM_FORMAT_S24:
		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
		break;
	case SNDRV_PCM_FORMAT_S32:
		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
		break;
	case SNDRV_PCM_FORMAT_FLOAT:
		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
		break;
	default:
		return -EINVAL;
	}

	/* Update the IPC message with information from the platform */
	pcm.params.stream_tag = platform_params->stream_tag;

	if (platform_params->use_phy_address)
		pcm.params.buffer.phy_addr = platform_params->phy_addr;

	if (platform_params->no_ipc_position) {
		/* For older ABIs set host_period_bytes to zero to inform
		 * FW we don't want position updates. Newer versions use
		 * no_stream_position for this purpose.
		 */
		if (v->abi_version < SOF_ABI_VER(3, 10, 0))
			pcm.params.host_period_bytes = 0;
		else
			pcm.params.no_stream_position = 1;
	}

	if (platform_params->cont_update_posn)
		pcm.params.cont_update_posn = 1;

	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);

	/* send hw_params IPC to the DSP */
	ret = sof_ipc_tx_message(sdev->ipc, &pcm, sizeof(pcm),
				 &ipc_params_reply, sizeof(ipc_params_reply));
	if (ret < 0) {
		dev_err(component->dev, "HW params ipc failed for stream %d\n",
			pcm.params.stream_tag);
		return ret;
	}

	ret = snd_sof_set_stream_data_offset(sdev, &spcm->stream[substream->stream],
					     ipc_params_reply.posn_offset);
	if (ret < 0) {
		dev_err(component->dev, "%s: invalid stream data offset for PCM %d\n",
			__func__, spcm->pcm.pcm_id);
		return ret;
	}

	return ret;
}

static int sof_ipc3_pcm_trigger(struct snd_soc_component *component,
				struct snd_pcm_substream *substream, int cmd)
{
	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
	struct sof_ipc_stream stream;
	struct snd_sof_pcm *spcm;

	spcm = snd_sof_find_spcm_dai(component, rtd);
	if (!spcm)
		return -EINVAL;

	stream.hdr.size = sizeof(stream);
	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
	stream.comp_id = spcm->stream[substream->stream].comp_id;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
		break;
	case SNDRV_PCM_TRIGGER_START:
		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
		break;
	case SNDRV_PCM_TRIGGER_SUSPEND:
		fallthrough;
	case SNDRV_PCM_TRIGGER_STOP:
		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
		break;
	default:
		dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
		return -EINVAL;
	}

	/* send IPC to the DSP */
	return sof_ipc_tx_message_no_reply(sdev->ipc, &stream, sizeof(stream));
}

static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
					    struct snd_pcm_hw_params *params)
{
	struct sof_ipc_dai_config *config;
	struct snd_sof_dai *dai;
	int i;

	/*
	 * Search for all matching DAIs as we can have both playback and capture DAI
	 * associated with the same link.
	 */
	list_for_each_entry(dai, &sdev->dai_list, list) {
		if (!dai->name || strcmp(link_name, dai->name))
			continue;
		for (i = 0; i < dai->number_configs; i++) {
			struct sof_dai_private_data *private = dai->private;

			config = &private->dai_config[i];
			if (config->ssp.fsync_rate == params_rate(params)) {
				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
				dai->current_config = i;
				break;
			}
		}
	}
}

static int sof_ipc3_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
				       struct snd_pcm_hw_params *params)
{
	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
	struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_sof_dai *dai = snd_sof_find_dai(component, (char *)rtd->dai_link->name);
	struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
	struct sof_dai_private_data *private;
	struct snd_soc_dpcm *dpcm;

	if (!dai) {
		dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
			rtd->dai_link->name);
		return -EINVAL;
	}

	private = dai->private;
	if (!private) {
		dev_err(component->dev, "%s: No private data found for DAI %s\n", __func__,
			rtd->dai_link->name);
		return -EINVAL;
	}

	/* read format from topology */
	snd_mask_none(fmt);

	switch (private->comp_dai->config.frame_fmt) {
	case SOF_IPC_FRAME_S16_LE:
		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
		break;
	case SOF_IPC_FRAME_S24_4LE:
		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
		break;
	case SOF_IPC_FRAME_S32_LE:
		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
		break;
	default:
		dev_err(component->dev, "No available DAI format!\n");
		return -EINVAL;
	}

	/* read rate and channels from topology */
	switch (private->dai_config->type) {
	case SOF_DAI_INTEL_SSP:
		/* search for config to pcm params match, if not found use default */
		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);

		rate->min = private->dai_config[dai->current_config].ssp.fsync_rate;
		rate->max = private->dai_config[dai->current_config].ssp.fsync_rate;
		channels->min = private->dai_config[dai->current_config].ssp.tdm_slots;
		channels->max = private->dai_config[dai->current_config].ssp.tdm_slots;

		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
			channels->min, channels->max);

		break;
	case SOF_DAI_INTEL_DMIC:
		/* DMIC only supports 16 or 32 bit formats */
		if (private->comp_dai->config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
			dev_err(component->dev, "Invalid fmt %d for DAI type %d\n",
				private->comp_dai->config.frame_fmt,
				private->dai_config->type);
		}
		break;
	case SOF_DAI_INTEL_HDA:
		/*
		 * HDAudio does not follow the default trigger
		 * sequence due to firmware implementation
		 */
		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
			struct snd_soc_pcm_runtime *fe = dpcm->fe;

			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
				SND_SOC_DPCM_TRIGGER_POST;
		}
		break;
	case SOF_DAI_INTEL_ALH:
		/*
		 * Dai could run with different channel count compared with
		 * front end, so get dai channel count from topology
		 */
		channels->min = private->dai_config->alh.channels;
		channels->max = private->dai_config->alh.channels;
		break;
	case SOF_DAI_IMX_ESAI:
		rate->min = private->dai_config->esai.fsync_rate;
		rate->max = private->dai_config->esai.fsync_rate;
		channels->min = private->dai_config->esai.tdm_slots;
		channels->max = private->dai_config->esai.tdm_slots;

		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
			channels->min, channels->max);
		break;
	case SOF_DAI_MEDIATEK_AFE:
		rate->min = private->dai_config->afe.rate;
		rate->max = private->dai_config->afe.rate;
		channels->min = private->dai_config->afe.channels;
		channels->max = private->dai_config->afe.channels;

		snd_mask_none(fmt);

		switch (private->dai_config->afe.format) {
		case SOF_IPC_FRAME_S16_LE:
			snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
			break;
		case SOF_IPC_FRAME_S24_4LE:
			snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
			break;
		case SOF_IPC_FRAME_S32_LE:
			snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
			break;
		default:
			dev_err(component->dev, "Not available format!\n");
			return -EINVAL;
		}

		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
			channels->min, channels->max);
		break;
	case SOF_DAI_IMX_SAI:
		rate->min = private->dai_config->sai.fsync_rate;
		rate->max = private->dai_config->sai.fsync_rate;
		channels->min = private->dai_config->sai.tdm_slots;
		channels->max = private->dai_config->sai.tdm_slots;

		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
			channels->min, channels->max);
		break;
	case SOF_DAI_AMD_BT:
		rate->min = private->dai_config->acpbt.fsync_rate;
		rate->max = private->dai_config->acpbt.fsync_rate;
		channels->min = private->dai_config->acpbt.tdm_slots;
		channels->max = private->dai_config->acpbt.tdm_slots;

		dev_dbg(component->dev,
			"AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
		dev_dbg(component->dev, "AMD_BT channels_min: %d channels_max: %d\n",
			channels->min, channels->max);
		break;
	case SOF_DAI_AMD_SP:
	case SOF_DAI_AMD_SP_VIRTUAL:
		rate->min = private->dai_config->acpsp.fsync_rate;
		rate->max = private->dai_config->acpsp.fsync_rate;
		channels->min = private->dai_config->acpsp.tdm_slots;
		channels->max = private->dai_config->acpsp.tdm_slots;

		dev_dbg(component->dev,
			"AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
		dev_dbg(component->dev, "AMD_SP channels_min: %d channels_max: %d\n",
			channels->min, channels->max);
		break;
	case SOF_DAI_AMD_HS:
	case SOF_DAI_AMD_HS_VIRTUAL:
		rate->min = private->dai_config->acphs.fsync_rate;
		rate->max = private->dai_config->acphs.fsync_rate;
		channels->min = private->dai_config->acphs.tdm_slots;
		channels->max = private->dai_config->acphs.tdm_slots;

		dev_dbg(component->dev,
			"AMD_HS channel_max: %d rate_max: %d\n", channels->max, rate->max);
		break;
	case SOF_DAI_AMD_DMIC:
		rate->min = private->dai_config->acpdmic.pdm_rate;
		rate->max = private->dai_config->acpdmic.pdm_rate;
		channels->min = private->dai_config->acpdmic.pdm_ch;
		channels->max = private->dai_config->acpdmic.pdm_ch;

		dev_dbg(component->dev,
			"AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
		dev_dbg(component->dev, "AMD_DMIC channels_min: %d channels_max: %d\n",
			channels->min, channels->max);
		break;
	default:
		dev_err(component->dev, "Invalid DAI type %d\n", private->dai_config->type);
		break;
	}

	return 0;
}

const struct sof_ipc_pcm_ops ipc3_pcm_ops = {
	.hw_params = sof_ipc3_pcm_hw_params,
	.hw_free = sof_ipc3_pcm_hw_free,
	.trigger = sof_ipc3_pcm_trigger,
	.dai_link_fixup = sof_ipc3_pcm_dai_link_fixup,
	.reset_hw_params_during_stop = true,
};