blob: 3e24d51af2e0b47e0ebfc7c06a057d13fbe802cc [file] [log] [blame]
developerec4ebe42022-04-12 11:17:45 +08001--- a/sound/soc/mediatek/common/mtk-afe-fe-dai.c
2+++ b/sound/soc/mediatek/common/mtk-afe-fe-dai.c
3@@ -6,11 +6,13 @@
4 * Author: Garlic Tseng <garlic.tseng@mediatek.com>
5 */
6
7+#include <linux/io.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/regmap.h>
11 #include <sound/soc.h>
12 #include "mtk-afe-platform-driver.h"
13+#include <sound/pcm_params.h>
14 #include "mtk-afe-fe-dai.h"
15 #include "mtk-base-afe.h"
16
17@@ -120,50 +122,64 @@
18 {
19 struct snd_soc_pcm_runtime *rtd = substream->private_data;
20 struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
21- struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
22- int msb_at_bit33 = 0;
23- int ret, fs = 0;
24+ int id = rtd->cpu_dai->id;
25+ struct mtk_base_afe_memif *memif = &afe->memif[id];
26+ int ret;
27+ unsigned int channels = params_channels(params);
28+ unsigned int rate = params_rate(params);
29+ snd_pcm_format_t format = params_format(params);
30
31 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
32 if (ret < 0)
33 return ret;
34
35- msb_at_bit33 = upper_32_bits(substream->runtime->dma_addr) ? 1 : 0;
36- memif->phys_buf_addr = lower_32_bits(substream->runtime->dma_addr);
37- memif->buffer_size = substream->runtime->dma_bytes;
38-
39- /* start */
40- mtk_regmap_write(afe->regmap, memif->data->reg_ofs_base,
41- memif->phys_buf_addr);
42- /* end */
43- mtk_regmap_write(afe->regmap,
44- memif->data->reg_ofs_base + AFE_BASE_END_OFFSET,
45- memif->phys_buf_addr + memif->buffer_size - 1);
46-
47- /* set MSB to 33-bit */
48- mtk_regmap_update_bits(afe->regmap, memif->data->msb_reg,
49- 1, msb_at_bit33, memif->data->msb_shift);
50+ if (afe->request_dram_resource)
51+ afe->request_dram_resource(afe->dev);
52
53- /* set channel */
54- if (memif->data->mono_shift >= 0) {
55- unsigned int mono = (params_channels(params) == 1) ? 1 : 0;
56+ dev_dbg(afe->dev, "%s(), %s, ch %d, rate %d, fmt %d, dma_addr %pad, dma_area %p, dma_bytes 0x%zx\n",
57+ __func__, memif->data->name,
58+ channels, rate, format,
59+ &substream->runtime->dma_addr,
60+ substream->runtime->dma_area,
61+ substream->runtime->dma_bytes);
62+
63+ memset_io(substream->runtime->dma_area, 0,
64+ substream->runtime->dma_bytes);
65+
66+ /* set addr */
67+ ret = mtk_memif_set_addr(afe, id,
68+ substream->runtime->dma_area,
69+ substream->runtime->dma_addr,
70+ substream->runtime->dma_bytes);
71+ if (ret) {
72+ dev_err(afe->dev, "%s(), error, id %d, set addr, ret %d\n",
73+ __func__, id, ret);
74+ return ret;
75+ }
76
77- mtk_regmap_update_bits(afe->regmap, memif->data->mono_reg,
78- 1, mono, memif->data->mono_shift);
79+ /* set channel */
80+ ret = mtk_memif_set_channel(afe, id, channels);
81+ if (ret) {
82+ dev_err(afe->dev, "%s(), error, id %d, set channel %d, ret %d\n",
83+ __func__, id, channels, ret);
84+ return ret;
85 }
86
87 /* set rate */
88- if (memif->data->fs_shift < 0)
89- return 0;
90-
91- fs = afe->memif_fs(substream, params_rate(params));
92-
93- if (fs < 0)
94- return -EINVAL;
95+ ret = mtk_memif_set_rate_substream(substream, id, rate);
96+ if (ret) {
97+ dev_err(afe->dev, "%s(), error, id %d, set rate %d, ret %d\n",
98+ __func__, id, rate, ret);
99+ return ret;
100+ }
101
102- mtk_regmap_update_bits(afe->regmap, memif->data->fs_reg,
103- memif->data->fs_maskbit, fs,
104- memif->data->fs_shift);
105+ /* set format */
106+ ret = mtk_memif_set_format(afe, id, format);
107+ if (ret) {
108+ dev_err(afe->dev, "%s(), error, id %d, set format %d, ret %d\n",
109+ __func__, id, format, ret);
110+ return ret;
111+ }
112
113 return 0;
114 }
115@@ -172,6 +188,11 @@
116 int mtk_afe_fe_hw_free(struct snd_pcm_substream *substream,
117 struct snd_soc_dai *dai)
118 {
119+ struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
120+
121+ if (afe->release_dram_resource)
122+ afe->release_dram_resource(afe->dev);
123+
124 return snd_pcm_lib_free_pages(substream);
125 }
126 EXPORT_SYMBOL_GPL(mtk_afe_fe_hw_free);
127@@ -182,20 +203,25 @@
128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
129 struct snd_pcm_runtime * const runtime = substream->runtime;
130 struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
131- struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
132+ int id = rtd->cpu_dai->id;
133+ struct mtk_base_afe_memif *memif = &afe->memif[id];
134 struct mtk_base_afe_irq *irqs = &afe->irqs[memif->irq_usage];
135 const struct mtk_base_irq_data *irq_data = irqs->irq_data;
136 unsigned int counter = runtime->period_size;
137 int fs;
138+ int ret;
139
140 dev_dbg(afe->dev, "%s %s cmd=%d\n", __func__, memif->data->name, cmd);
141
142 switch (cmd) {
143 case SNDRV_PCM_TRIGGER_START:
144 case SNDRV_PCM_TRIGGER_RESUME:
145- mtk_regmap_update_bits(afe->regmap,
146- memif->data->enable_reg,
147- 1, 1, memif->data->enable_shift);
148+ ret = mtk_memif_set_enable(afe, id);
149+ if (ret) {
150+ dev_err(afe->dev, "%s(), error, id %d, memif enable, ret %d\n",
151+ __func__, id, ret);
152+ return ret;
153+ }
154
155 /* set irq counter */
156 mtk_regmap_update_bits(afe->regmap, irq_data->irq_cnt_reg,
157@@ -219,15 +245,19 @@
158 return 0;
159 case SNDRV_PCM_TRIGGER_STOP:
160 case SNDRV_PCM_TRIGGER_SUSPEND:
161- mtk_regmap_update_bits(afe->regmap, memif->data->enable_reg,
162- 1, 0, memif->data->enable_shift);
163+ ret = mtk_memif_set_disable(afe, id);
164+ if (ret) {
165+ dev_err(afe->dev, "%s(), error, id %d, memif enable, ret %d\n",
166+ __func__, id, ret);
167+ }
168+
169 /* disable interrupt */
170 mtk_regmap_update_bits(afe->regmap, irq_data->irq_en_reg,
171 1, 0, irq_data->irq_en_shift);
172 /* and clear pending IRQ */
173 mtk_regmap_write(afe->regmap, irq_data->irq_clr_reg,
174 1 << irq_data->irq_clr_shift);
175- return 0;
176+ return ret;
177 default:
178 return -EINVAL;
179 }
180@@ -239,34 +269,15 @@
181 {
182 struct snd_soc_pcm_runtime *rtd = substream->private_data;
183 struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
184- struct mtk_base_afe_memif *memif = &afe->memif[rtd->cpu_dai->id];
185- int hd_audio = 0;
186- int hd_align = 0;
187+ int id = rtd->cpu_dai->id;
188+ int pbuf_size;
189
190- /* set hd mode */
191- switch (substream->runtime->format) {
192- case SNDRV_PCM_FORMAT_S16_LE:
193- hd_audio = 0;
194- break;
195- case SNDRV_PCM_FORMAT_S32_LE:
196- hd_audio = 1;
197- hd_align = 1;
198- break;
199- case SNDRV_PCM_FORMAT_S24_LE:
200- hd_audio = 1;
201- break;
202- default:
203- dev_err(afe->dev, "%s() error: unsupported format %d\n",
204- __func__, substream->runtime->format);
205- break;
206+ if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
207+ if (afe->get_memif_pbuf_size) {
208+ pbuf_size = afe->get_memif_pbuf_size(substream);
209+ mtk_memif_set_pbuf_size(afe, id, pbuf_size);
210+ }
211 }
212-
213- mtk_regmap_update_bits(afe->regmap, memif->data->hd_reg,
214- 1, hd_audio, memif->data->hd_shift);
215-
216- mtk_regmap_update_bits(afe->regmap, memif->data->hd_align_reg,
217- 1, hd_align, memif->data->hd_align_mshift);
218-
219 return 0;
220 }
221 EXPORT_SYMBOL_GPL(mtk_afe_fe_prepare);