xref: /openbmc/linux/sound/soc/sof/amd/acp-pcm.c (revision ecefa105)
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 = sdev->debug_box.offset +
46 		     offsetof(struct scratch_reg_conf, buf_size);
47 	index = stream->stream_tag - 1;
48 	buf_offset = buf_offset + index * 4;
49 
50 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + buf_offset, size);
51 
52 	return 0;
53 }
54 EXPORT_SYMBOL_NS(acp_pcm_hw_params, SND_SOC_SOF_AMD_COMMON);
55 
56 int acp_pcm_open(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream)
57 {
58 	struct acp_dsp_stream *stream;
59 
60 	stream = acp_dsp_stream_get(sdev, 0);
61 	if (!stream)
62 		return -ENODEV;
63 
64 	substream->runtime->private_data = stream;
65 	stream->substream = substream;
66 
67 	return 0;
68 }
69 EXPORT_SYMBOL_NS(acp_pcm_open, SND_SOC_SOF_AMD_COMMON);
70 
71 int acp_pcm_close(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream)
72 {
73 	struct acp_dsp_stream *stream;
74 
75 	stream = substream->runtime->private_data;
76 	if (!stream) {
77 		dev_err(sdev->dev, "No open stream\n");
78 		return -EINVAL;
79 	}
80 
81 	stream->substream = NULL;
82 	substream->runtime->private_data = NULL;
83 
84 	return acp_dsp_stream_put(sdev, stream);
85 }
86 EXPORT_SYMBOL_NS(acp_pcm_close, SND_SOC_SOF_AMD_COMMON);
87