xref: /linux/sound/soc/sof/amd/acp-pcm.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
1 // SPDX-License-Identifier: (GPL-2.0-only 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) 2021 Advanced Micro Devices, Inc.
7 //
8 // Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
9 
10 /*
11  * PCM interface for generic AMD audio ACP DSP block
12  */
13 #include <sound/pcm_params.h>
14 
15 #include "../ops.h"
16 #include "acp.h"
17 #include "acp-dsp-offset.h"
18 
19 int acp_pcm_hw_params(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
20 		      struct snd_pcm_hw_params *params,
21 		      struct snd_sof_platform_stream_params *platform_params)
22 {
23 	struct snd_pcm_runtime *runtime = substream->runtime;
24 	struct acp_dsp_stream *stream = runtime->private_data;
25 	unsigned int buf_offset, index;
26 	u32 size;
27 	int ret;
28 
29 	size = runtime->dma_bytes;
30 	stream->num_pages = PFN_UP(runtime->dma_bytes);
31 	stream->dmab = substream->runtime->dma_buffer_p;
32 
33 	ret = acp_dsp_stream_config(sdev, stream);
34 	if (ret < 0) {
35 		dev_err(sdev->dev, "stream configuration failed\n");
36 		return ret;
37 	}
38 
39 	platform_params->use_phy_address = true;
40 	platform_params->phy_addr = stream->reg_offset;
41 	platform_params->stream_tag = stream->stream_tag;
42 
43 	/* write buffer size of stream in scratch memory */
44 
45 	buf_offset = offsetof(struct scratch_reg_conf, buf_size);
46 	index = stream->stream_tag - 1;
47 	buf_offset = buf_offset + index * 4;
48 
49 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + buf_offset, size);
50 
51 	return 0;
52 }
53 EXPORT_SYMBOL_NS(acp_pcm_hw_params, SND_SOC_SOF_AMD_COMMON);
54 
55 int acp_pcm_open(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream)
56 {
57 	struct acp_dsp_stream *stream;
58 
59 	stream = acp_dsp_stream_get(sdev, 0);
60 	if (!stream)
61 		return -ENODEV;
62 
63 	substream->runtime->private_data = stream;
64 	stream->substream = substream;
65 
66 	return 0;
67 }
68 EXPORT_SYMBOL_NS(acp_pcm_open, SND_SOC_SOF_AMD_COMMON);
69 
70 int acp_pcm_close(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream)
71 {
72 	struct acp_dsp_stream *stream;
73 
74 	stream = substream->runtime->private_data;
75 	if (!stream) {
76 		dev_err(sdev->dev, "No open stream\n");
77 		return -EINVAL;
78 	}
79 
80 	stream->substream = NULL;
81 	substream->runtime->private_data = NULL;
82 
83 	return acp_dsp_stream_put(sdev, stream);
84 }
85 EXPORT_SYMBOL_NS(acp_pcm_close, SND_SOC_SOF_AMD_COMMON);
86