xref: /linux/sound/soc/amd/acp/acp70.c (revision a3d14d1602ca11429d242d230c31af8f822f614f)
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) 2023 Advanced Micro Devices, Inc.
7 //
8 // Authors: Syed Saba kareem <syed.sabakareem@amd.com>
9 /*
10  * Hardware interface for ACP7.0 block
11  */
12 
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/io.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dai.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/pci.h>
23 #include "amd.h"
24 #include "acp-mach.h"
25 
26 #include <asm/amd/node.h>
27 
28 #define DRV_NAME "acp_asoc_acp70"
29 
30 #define CLK7_CLK0_DFS_CNTL_N1		0X0006C1A4
31 #define CLK0_DIVIDER			0X19
32 
33 static struct snd_soc_dai_driver acp70_dai[] = {
34 {
35 	.name = "acp-i2s-sp",
36 	.id = I2S_SP_INSTANCE,
37 	.playback = {
38 		.stream_name = "I2S SP Playback",
39 		.rates = SNDRV_PCM_RATE_8000_192000,
40 		.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
41 			   SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
42 		.channels_min = 2,
43 		.channels_max = 32,
44 		.rate_min = 8000,
45 		.rate_max = 192000,
46 	},
47 	.capture = {
48 		.stream_name = "I2S SP Capture",
49 		.rates = SNDRV_PCM_RATE_8000_192000,
50 		.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
51 			   SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
52 		.channels_min = 2,
53 		.channels_max = 32,
54 		.rate_min = 8000,
55 		.rate_max = 192000,
56 	},
57 	.ops = &asoc_acp_cpu_dai_ops,
58 },
59 {
60 	.name = "acp-i2s-bt",
61 	.id = I2S_BT_INSTANCE,
62 	.playback = {
63 		.stream_name = "I2S BT Playback",
64 		.rates = SNDRV_PCM_RATE_8000_192000,
65 		.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
66 			   SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
67 		.channels_min = 2,
68 		.channels_max = 32,
69 		.rate_min = 8000,
70 		.rate_max = 192000,
71 	},
72 	.capture = {
73 		.stream_name = "I2S BT Capture",
74 		.rates = SNDRV_PCM_RATE_8000_192000,
75 		.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
76 			   SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
77 		.channels_min = 2,
78 		.channels_max = 32,
79 		.rate_min = 8000,
80 		.rate_max = 192000,
81 	},
82 	.ops = &asoc_acp_cpu_dai_ops,
83 },
84 {
85 	.name = "acp-i2s-hs",
86 	.id = I2S_HS_INSTANCE,
87 	.playback = {
88 		.stream_name = "I2S HS Playback",
89 		.rates = SNDRV_PCM_RATE_8000_192000,
90 		.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
91 			   SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
92 		.channels_min = 2,
93 		.channels_max = 32,
94 		.rate_min = 8000,
95 		.rate_max = 192000,
96 	},
97 	.capture = {
98 		.stream_name = "I2S HS Capture",
99 		.rates = SNDRV_PCM_RATE_8000_192000,
100 		.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
101 			   SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
102 		.channels_min = 2,
103 		.channels_max = 32,
104 		.rate_min = 8000,
105 		.rate_max = 192000,
106 	},
107 	.ops = &asoc_acp_cpu_dai_ops,
108 },
109 {
110 	.name = "acp-pdm-dmic",
111 	.id = DMIC_INSTANCE,
112 	.capture = {
113 		.rates = SNDRV_PCM_RATE_8000_48000,
114 		.formats = SNDRV_PCM_FMTBIT_S32_LE,
115 		.channels_min = 2,
116 		.channels_max = 2,
117 		.rate_min = 8000,
118 		.rate_max = 48000,
119 	},
120 	.ops = &acp_dmic_dai_ops,
121 },
122 };
123 
acp_acp70_audio_probe(struct platform_device * pdev)124 static int acp_acp70_audio_probe(struct platform_device *pdev)
125 {
126 	struct device *dev = &pdev->dev;
127 	struct acp_chip_info *chip;
128 	int ret;
129 
130 	chip = dev_get_platdata(&pdev->dev);
131 	if (!chip || !chip->base) {
132 		dev_err(&pdev->dev, "ACP chip data is NULL\n");
133 		return -ENODEV;
134 	}
135 
136 	switch (chip->acp_rev) {
137 	case ACP70_PCI_ID:
138 	case ACP71_PCI_ID:
139 		break;
140 	default:
141 		dev_err(&pdev->dev, "Un-supported ACP Revision %d\n", chip->acp_rev);
142 		return -ENODEV;
143 	}
144 
145 	chip->dev = dev;
146 	chip->dai_driver = acp70_dai;
147 	chip->num_dai = ARRAY_SIZE(acp70_dai);
148 
149 	/* Set clk7 DFS clock divider register value to get mclk as 196.608MHz*/
150 	ret = amd_smn_write(0, CLK7_CLK0_DFS_CNTL_N1, CLK0_DIVIDER);
151 	if (ret) {
152 		dev_err(&pdev->dev, "Failed to set I2S master clock as 196.608MHz\n");
153 		return ret;
154 	}
155 	ret = acp_hw_en_interrupts(chip);
156 	if (ret) {
157 		dev_err(dev, "ACP en-interrupts failed\n");
158 		return ret;
159 	}
160 	acp_platform_register(dev);
161 	pm_runtime_set_autosuspend_delay(&pdev->dev, ACP_SUSPEND_DELAY_MS);
162 	pm_runtime_use_autosuspend(&pdev->dev);
163 	pm_runtime_mark_last_busy(&pdev->dev);
164 	pm_runtime_set_active(&pdev->dev);
165 	pm_runtime_enable(&pdev->dev);
166 	return 0;
167 }
168 
acp_acp70_audio_remove(struct platform_device * pdev)169 static void acp_acp70_audio_remove(struct platform_device *pdev)
170 {
171 	struct device *dev = &pdev->dev;
172 	struct acp_chip_info *chip = dev_get_platdata(dev);
173 	int ret;
174 
175 	ret = acp_hw_dis_interrupts(chip);
176 	if (ret)
177 		dev_err(dev, "ACP dis-interrupts failed\n");
178 
179 	acp_platform_unregister(dev);
180 	pm_runtime_disable(&pdev->dev);
181 }
182 
acp70_pcm_resume(struct device * dev)183 static int acp70_pcm_resume(struct device *dev)
184 {
185 	struct acp_chip_info *chip = dev_get_drvdata(dev->parent);
186 	struct acp_stream *stream;
187 	struct snd_pcm_substream *substream;
188 	snd_pcm_uframes_t buf_in_frames;
189 	u64 buf_size;
190 
191 	spin_lock(&chip->acp_lock);
192 	list_for_each_entry(stream, &chip->stream_list, list) {
193 		substream = stream->substream;
194 		if (substream && substream->runtime) {
195 			buf_in_frames = (substream->runtime->buffer_size);
196 			buf_size = frames_to_bytes(substream->runtime, buf_in_frames);
197 			config_pte_for_stream(chip, stream);
198 			config_acp_dma(chip, stream, buf_size);
199 			if (stream->dai_id)
200 				restore_acp_i2s_params(substream, chip, stream);
201 			else
202 				restore_acp_pdm_params(substream, chip);
203 		}
204 	}
205 	spin_unlock(&chip->acp_lock);
206 	return 0;
207 }
208 
209 static const struct dev_pm_ops acp70_dma_pm_ops = {
210 	SYSTEM_SLEEP_PM_OPS(NULL, acp70_pcm_resume)
211 };
212 
213 static struct platform_driver acp70_driver = {
214 	.probe = acp_acp70_audio_probe,
215 	.remove = acp_acp70_audio_remove,
216 	.driver = {
217 		.name = "acp_asoc_acp70",
218 		.pm = pm_ptr(&acp70_dma_pm_ops),
219 	},
220 };
221 
222 module_platform_driver(acp70_driver);
223 
224 MODULE_DESCRIPTION("AMD ACP ACP70 Driver");
225 MODULE_IMPORT_NS("SND_SOC_ACP_COMMON");
226 MODULE_LICENSE("Dual BSD/GPL");
227 MODULE_ALIAS("platform:" DRV_NAME);
228