blob: ddeb5a4221c68407b6aa3971157d87400d8d0761 [file] [log] [blame]
developerbe797a32021-12-16 16:56:09 +08001--- a/sound/soc/mediatek/common/mtk-afe-fe-dai.c
2+++ b/sound/soc/mediatek/common/mtk-afe-fe-dai.c
3@@ -361,6 +361,222 @@
4 }
5 EXPORT_SYMBOL_GPL(mtk_afe_dai_resume);
6
7+int mtk_memif_set_enable(struct mtk_base_afe *afe, int id)
8+{
9+ struct mtk_base_afe_memif *memif = &afe->memif[id];
10+
11+ if (memif->data->enable_shift < 0) {
12+ dev_warn(afe->dev, "%s(), error, id %d, enable_shift < 0\n",
13+ __func__, id);
14+ return 0;
15+ }
16+ return mtk_regmap_update_bits(afe->regmap, memif->data->enable_reg,
17+ 1, 1, memif->data->enable_shift);
18+}
19+EXPORT_SYMBOL_GPL(mtk_memif_set_enable);
20+
21+int mtk_memif_set_disable(struct mtk_base_afe *afe, int id)
22+{
23+ struct mtk_base_afe_memif *memif = &afe->memif[id];
24+
25+ if (memif->data->enable_shift < 0) {
26+ dev_warn(afe->dev, "%s(), error, id %d, enable_shift < 0\n",
27+ __func__, id);
28+ return 0;
29+ }
30+ return mtk_regmap_update_bits(afe->regmap, memif->data->enable_reg,
31+ 1, 0, memif->data->enable_shift);
32+}
33+EXPORT_SYMBOL_GPL(mtk_memif_set_disable);
34+
35+int mtk_memif_set_addr(struct mtk_base_afe *afe, int id,
36+ unsigned char *dma_area,
37+ dma_addr_t dma_addr,
38+ size_t dma_bytes)
39+{
40+ struct mtk_base_afe_memif *memif = &afe->memif[id];
41+ int msb_at_bit33 = upper_32_bits(dma_addr) ? 1 : 0;
42+ unsigned int phys_buf_addr = lower_32_bits(dma_addr);
43+ unsigned int phys_buf_addr_upper_32 = upper_32_bits(dma_addr);
44+
45+ memif->dma_area = dma_area;
46+ memif->dma_addr = dma_addr;
47+ memif->dma_bytes = dma_bytes;
48+
49+ /* start */
50+ mtk_regmap_write(afe->regmap, memif->data->reg_ofs_base,
51+ phys_buf_addr);
52+ /* end */
53+ if (memif->data->reg_ofs_end)
54+ mtk_regmap_write(afe->regmap,
55+ memif->data->reg_ofs_end,
56+ phys_buf_addr + dma_bytes - 1);
57+ else
58+ mtk_regmap_write(afe->regmap,
59+ memif->data->reg_ofs_base +
60+ AFE_BASE_END_OFFSET,
61+ phys_buf_addr + dma_bytes - 1);
62+
63+ /* set start, end, upper 32 bits */
64+ if (memif->data->reg_ofs_base_msb) {
65+ mtk_regmap_write(afe->regmap, memif->data->reg_ofs_base_msb,
66+ phys_buf_addr_upper_32);
67+ mtk_regmap_write(afe->regmap,
68+ memif->data->reg_ofs_end_msb,
69+ phys_buf_addr_upper_32);
70+ }
71+
72+ /* set MSB to 33-bit */
73+ if (memif->data->msb_reg >= 0)
74+ mtk_regmap_update_bits(afe->regmap, memif->data->msb_reg,
75+ 1, msb_at_bit33, memif->data->msb_shift);
76+
77+ return 0;
78+}
79+EXPORT_SYMBOL_GPL(mtk_memif_set_addr);
80+
81+int mtk_memif_set_channel(struct mtk_base_afe *afe,
82+ int id, unsigned int channel)
83+{
84+ struct mtk_base_afe_memif *memif = &afe->memif[id];
85+ unsigned int mono;
86+
87+ if (memif->data->mono_shift < 0)
88+ return 0;
89+
90+ if (memif->data->quad_ch_mask) {
91+ unsigned int quad_ch = (channel == 4) ? 1 : 0;
92+
93+ mtk_regmap_update_bits(afe->regmap, memif->data->quad_ch_reg,
94+ memif->data->quad_ch_mask,
95+ quad_ch, memif->data->quad_ch_shift);
96+ }
97+
98+ if (memif->data->mono_invert)
99+ mono = (channel == 1) ? 0 : 1;
100+ else
101+ mono = (channel == 1) ? 1 : 0;
102+
103+ return mtk_regmap_update_bits(afe->regmap, memif->data->mono_reg,
104+ 1, mono, memif->data->mono_shift);
105+}
106+EXPORT_SYMBOL_GPL(mtk_memif_set_channel);
107+
108+static int mtk_memif_set_rate_fs(struct mtk_base_afe *afe,
109+ int id, int fs)
110+{
111+ struct mtk_base_afe_memif *memif = &afe->memif[id];
112+
113+ if (memif->data->fs_shift >= 0)
114+ mtk_regmap_update_bits(afe->regmap, memif->data->fs_reg,
115+ memif->data->fs_maskbit,
116+ fs, memif->data->fs_shift);
117+
118+ return 0;
119+}
120+
121+int mtk_memif_set_rate(struct mtk_base_afe *afe,
122+ int id, unsigned int rate)
123+{
124+ int fs = 0;
125+
126+ if (!afe->get_dai_fs) {
127+ dev_err(afe->dev, "%s(), error, afe->get_dai_fs == NULL\n",
128+ __func__);
129+ return -EINVAL;
130+ }
131+
132+ fs = afe->get_dai_fs(afe, id, rate);
133+
134+ if (fs < 0)
135+ return -EINVAL;
136+
137+ return mtk_memif_set_rate_fs(afe, id, fs);
138+}
139+EXPORT_SYMBOL_GPL(mtk_memif_set_rate);
140+
141+int mtk_memif_set_rate_substream(struct snd_pcm_substream *substream,
142+ int id, unsigned int rate)
143+{
144+ struct snd_soc_pcm_runtime *rtd = substream->private_data;
145+ struct snd_soc_component *component =
146+ snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
147+ struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
148+
149+ int fs = 0;
150+
151+ if (!afe->memif_fs) {
152+ dev_err(afe->dev, "%s(), error, afe->memif_fs == NULL\n",
153+ __func__);
154+ return -EINVAL;
155+ }
156+
157+ fs = afe->memif_fs(substream, rate);
158+
159+ if (fs < 0)
160+ return -EINVAL;
161+
162+ return mtk_memif_set_rate_fs(afe, id, fs);
163+}
164+EXPORT_SYMBOL_GPL(mtk_memif_set_rate_substream);
165+
166+int mtk_memif_set_format(struct mtk_base_afe *afe,
167+ int id, snd_pcm_format_t format)
168+{
169+ struct mtk_base_afe_memif *memif = &afe->memif[id];
170+ int hd_audio = 0;
171+ int hd_align = 0;
172+
173+ /* set hd mode */
174+ switch (format) {
175+ case SNDRV_PCM_FORMAT_S16_LE:
176+ case SNDRV_PCM_FORMAT_U16_LE:
177+ hd_audio = 0;
178+ break;
179+ case SNDRV_PCM_FORMAT_S32_LE:
180+ case SNDRV_PCM_FORMAT_U32_LE:
181+ hd_audio = 1;
182+ hd_align = 1;
183+ break;
184+ case SNDRV_PCM_FORMAT_S24_LE:
185+ case SNDRV_PCM_FORMAT_U24_LE:
186+ hd_audio = 1;
187+ break;
188+ default:
189+ dev_err(afe->dev, "%s() error: unsupported format %d\n",
190+ __func__, format);
191+ break;
192+ }
193+
194+ mtk_regmap_update_bits(afe->regmap, memif->data->hd_reg,
195+ 1, hd_audio, memif->data->hd_shift);
196+
197+ mtk_regmap_update_bits(afe->regmap, memif->data->hd_align_reg,
198+ 1, hd_align, memif->data->hd_align_mshift);
199+
200+ return 0;
201+}
202+EXPORT_SYMBOL_GPL(mtk_memif_set_format);
203+
204+int mtk_memif_set_pbuf_size(struct mtk_base_afe *afe,
205+ int id, int pbuf_size)
206+{
207+ const struct mtk_base_memif_data *memif_data = afe->memif[id].data;
208+
209+ if (memif_data->pbuf_mask == 0 || memif_data->minlen_mask == 0)
210+ return 0;
211+
212+ mtk_regmap_update_bits(afe->regmap, memif_data->pbuf_reg,
213+ memif_data->pbuf_mask,
214+ pbuf_size, memif_data->pbuf_shift);
215+
216+ mtk_regmap_update_bits(afe->regmap, memif_data->minlen_reg,
217+ memif_data->minlen_mask,
218+ pbuf_size, memif_data->minlen_shift);
219+ return 0;
220+}
221+EXPORT_SYMBOL_GPL(mtk_memif_set_pbuf_size);
222+
223 MODULE_DESCRIPTION("Mediatek simple fe dai operator");
224 MODULE_AUTHOR("Garlic Tseng <garlic.tseng@mediatek.com>");
225 MODULE_LICENSE("GPL v2");
226--- a/sound/soc/mediatek/common/mtk-afe-fe-dai.h
227+++ b/sound/soc/mediatek/common/mtk-afe-fe-dai.h
228@@ -34,4 +34,20 @@
229 int mtk_afe_dai_suspend(struct snd_soc_dai *dai);
230 int mtk_afe_dai_resume(struct snd_soc_dai *dai);
231
232+int mtk_memif_set_enable(struct mtk_base_afe *afe, int id);
233+int mtk_memif_set_disable(struct mtk_base_afe *afe, int id);
234+int mtk_memif_set_addr(struct mtk_base_afe *afe, int id,
235+ unsigned char *dma_area,
236+ dma_addr_t dma_addr,
237+ size_t dma_bytes);
238+int mtk_memif_set_channel(struct mtk_base_afe *afe,
239+ int id, unsigned int channel);
240+int mtk_memif_set_rate(struct mtk_base_afe *afe,
241+ int id, unsigned int rate);
242+int mtk_memif_set_rate_substream(struct snd_pcm_substream *substream,
243+ int id, unsigned int rate);
244+int mtk_memif_set_format(struct mtk_base_afe *afe,
245+ int id, snd_pcm_format_t format);
246+int mtk_memif_set_pbuf_size(struct mtk_base_afe *afe,
247+ int id, int pbuf_size);
248 #endif
249--- a/sound/soc/mediatek/common/mtk-base-afe.h
250+++ b/sound/soc/mediatek/common/mtk-base-afe.h
251@@ -16,21 +16,38 @@
252 const char *name;
253 int reg_ofs_base;
254 int reg_ofs_cur;
255+ int reg_ofs_end;
256+ int reg_ofs_base_msb;
257+ int reg_ofs_cur_msb;
258+ int reg_ofs_end_msb;
259 int fs_reg;
260 int fs_shift;
261 int fs_maskbit;
262 int mono_reg;
263 int mono_shift;
264+ int mono_invert;
265+ int quad_ch_reg;
266+ int quad_ch_mask;
267+ int quad_ch_shift;
268 int enable_reg;
269 int enable_shift;
270 int hd_reg;
271- int hd_align_reg;
272 int hd_shift;
273+ int hd_align_reg;
274 int hd_align_mshift;
275 int msb_reg;
276 int msb_shift;
277+ int msb2_reg;
278+ int msb2_shift;
279 int agent_disable_reg;
280 int agent_disable_shift;
281+ /* playback memif only */
282+ int pbuf_reg;
283+ int pbuf_mask;
284+ int pbuf_shift;
285+ int minlen_reg;
286+ int minlen_mask;
287+ int minlen_shift;
288 };
289
290 struct mtk_base_irq_data {
291@@ -84,6 +101,12 @@
292 unsigned int rate);
293 int (*irq_fs)(struct snd_pcm_substream *substream,
294 unsigned int rate);
295+ int (*get_dai_fs)(struct mtk_base_afe *afe,
296+ int dai_id, unsigned int rate);
297+ int (*get_memif_pbuf_size)(struct snd_pcm_substream *substream);
298+
299+ int (*request_dram_resource)(struct device *dev);
300+ int (*release_dram_resource)(struct device *dev);
301
302 void *platform_priv;
303 };
304@@ -95,6 +118,9 @@
305 const struct mtk_base_memif_data *data;
306 int irq_usage;
307 int const_irq;
308+ unsigned char *dma_area;
309+ dma_addr_t dma_addr;
310+ size_t dma_bytes;
311 };
312
313 struct mtk_base_afe_irq {