xref: /linux/sound/soc/mediatek/mt2701/mt2701-afe-pcm.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Mediatek ALSA SoC AFE platform driver for 2701
4  *
5  * Copyright (c) 2016 MediaTek Inc.
6  * Author: Garlic Tseng <garlic.tseng@mediatek.com>
7  *	   Ir Lian <ir.lian@mediatek.com>
8  *	   Ryder Lee <ryder.lee@mediatek.com>
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 
19 #include "mt2701-afe-common.h"
20 #include "mt2701-afe-clock-ctrl.h"
21 #include "../common/mtk-afe-platform-driver.h"
22 #include "../common/mtk-afe-fe-dai.h"
23 
24 static const struct snd_pcm_hardware mt2701_afe_hardware = {
25 	.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED
26 		| SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID,
27 	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE
28 		   | SNDRV_PCM_FMTBIT_S32_LE,
29 	.period_bytes_min = 1024,
30 	.period_bytes_max = 1024 * 256,
31 	.periods_min = 4,
32 	.periods_max = 1024,
33 	.buffer_bytes_max = 1024 * 1024,
34 	.fifo_size = 0,
35 };
36 
37 struct mt2701_afe_rate {
38 	unsigned int rate;
39 	unsigned int regvalue;
40 };
41 
42 static const struct mt2701_afe_rate mt2701_afe_i2s_rates[] = {
43 	{ .rate = 8000, .regvalue = 0 },
44 	{ .rate = 12000, .regvalue = 1 },
45 	{ .rate = 16000, .regvalue = 2 },
46 	{ .rate = 24000, .regvalue = 3 },
47 	{ .rate = 32000, .regvalue = 4 },
48 	{ .rate = 48000, .regvalue = 5 },
49 	{ .rate = 96000, .regvalue = 6 },
50 	{ .rate = 192000, .regvalue = 7 },
51 	{ .rate = 384000, .regvalue = 8 },
52 	{ .rate = 7350, .regvalue = 16 },
53 	{ .rate = 11025, .regvalue = 17 },
54 	{ .rate = 14700, .regvalue = 18 },
55 	{ .rate = 22050, .regvalue = 19 },
56 	{ .rate = 29400, .regvalue = 20 },
57 	{ .rate = 44100, .regvalue = 21 },
58 	{ .rate = 88200, .regvalue = 22 },
59 	{ .rate = 176400, .regvalue = 23 },
60 	{ .rate = 352800, .regvalue = 24 },
61 };
62 
63 static int mt2701_dai_num_to_i2s(struct mtk_base_afe *afe, int num)
64 {
65 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
66 	int val = num - MT2701_IO_I2S;
67 
68 	if (val < 0 || val >= afe_priv->soc->i2s_num) {
69 		dev_err(afe->dev, "%s, num not available, num %d, val %d\n",
70 			__func__, num, val);
71 		return -EINVAL;
72 	}
73 	return val;
74 }
75 
76 static int mt2701_afe_i2s_fs(unsigned int sample_rate)
77 {
78 	int i;
79 
80 	for (i = 0; i < ARRAY_SIZE(mt2701_afe_i2s_rates); i++)
81 		if (mt2701_afe_i2s_rates[i].rate == sample_rate)
82 			return mt2701_afe_i2s_rates[i].regvalue;
83 
84 	return -EINVAL;
85 }
86 
87 static int mt2701_afe_i2s_startup(struct snd_pcm_substream *substream,
88 				  struct snd_soc_dai *dai)
89 {
90 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
91 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
92 	int i2s_num = mt2701_dai_num_to_i2s(afe, dai->id);
93 	bool mode = afe_priv->soc->has_one_heart_mode;
94 
95 	if (i2s_num < 0)
96 		return i2s_num;
97 
98 	return mt2701_afe_enable_mclk(afe, mode ? 1 : i2s_num);
99 }
100 
101 static int mt2701_afe_i2s_path_disable(struct mtk_base_afe *afe,
102 				       struct mt2701_i2s_path *i2s_path,
103 				       int stream_dir)
104 {
105 	const struct mt2701_i2s_data *i2s_data = i2s_path->i2s_data[stream_dir];
106 
107 	if (--i2s_path->on[stream_dir] < 0)
108 		i2s_path->on[stream_dir] = 0;
109 
110 	if (i2s_path->on[stream_dir])
111 		return 0;
112 
113 	/* disable i2s */
114 	regmap_update_bits(afe->regmap, i2s_data->i2s_ctrl_reg,
115 			   ASYS_I2S_CON_I2S_EN, 0);
116 
117 	mt2701_afe_disable_i2s(afe, i2s_path, stream_dir);
118 
119 	return 0;
120 }
121 
122 static void mt2701_afe_i2s_shutdown(struct snd_pcm_substream *substream,
123 				    struct snd_soc_dai *dai)
124 {
125 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
126 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
127 	int i2s_num = mt2701_dai_num_to_i2s(afe, dai->id);
128 	struct mt2701_i2s_path *i2s_path;
129 	bool mode = afe_priv->soc->has_one_heart_mode;
130 
131 	if (i2s_num < 0)
132 		return;
133 
134 	i2s_path = &afe_priv->i2s_path[i2s_num];
135 
136 	if (i2s_path->occupied[substream->stream])
137 		i2s_path->occupied[substream->stream] = 0;
138 	else
139 		goto exit;
140 
141 	mt2701_afe_i2s_path_disable(afe, i2s_path, substream->stream);
142 
143 	/* need to disable i2s-out path when disable i2s-in */
144 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
145 		mt2701_afe_i2s_path_disable(afe, i2s_path, !substream->stream);
146 
147 exit:
148 	/* disable mclk */
149 	mt2701_afe_disable_mclk(afe, mode ? 1 : i2s_num);
150 }
151 
152 static int mt2701_i2s_path_enable(struct mtk_base_afe *afe,
153 				  struct mt2701_i2s_path *i2s_path,
154 				  int stream_dir, int rate)
155 {
156 	const struct mt2701_i2s_data *i2s_data = i2s_path->i2s_data[stream_dir];
157 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
158 	int reg, fs, w_len = 1; /* now we support bck 64bits only */
159 	unsigned int mask, val;
160 
161 	/* no need to enable if already done */
162 	if (++i2s_path->on[stream_dir] != 1)
163 		return 0;
164 
165 	fs = mt2701_afe_i2s_fs(rate);
166 
167 	mask = ASYS_I2S_CON_FS |
168 	       ASYS_I2S_CON_I2S_COUPLE_MODE | /* 0 */
169 	       ASYS_I2S_CON_I2S_MODE |
170 	       ASYS_I2S_CON_WIDE_MODE;
171 
172 	val = ASYS_I2S_CON_FS_SET(fs) |
173 	      ASYS_I2S_CON_I2S_MODE |
174 	      ASYS_I2S_CON_WIDE_MODE_SET(w_len);
175 
176 	if (stream_dir == SNDRV_PCM_STREAM_CAPTURE) {
177 		mask |= ASYS_I2S_IN_PHASE_FIX;
178 		val |= ASYS_I2S_IN_PHASE_FIX;
179 		reg = ASMI_TIMING_CON1;
180 	} else {
181 		if (afe_priv->soc->has_one_heart_mode) {
182 			mask |= ASYS_I2S_CON_ONE_HEART_MODE;
183 			val |= ASYS_I2S_CON_ONE_HEART_MODE;
184 		}
185 		reg = ASMO_TIMING_CON1;
186 	}
187 
188 	regmap_update_bits(afe->regmap, i2s_data->i2s_ctrl_reg, mask, val);
189 
190 	regmap_update_bits(afe->regmap, reg,
191 			   i2s_data->i2s_asrc_fs_mask
192 			   << i2s_data->i2s_asrc_fs_shift,
193 			   fs << i2s_data->i2s_asrc_fs_shift);
194 
195 	/* enable i2s */
196 	mt2701_afe_enable_i2s(afe, i2s_path, stream_dir);
197 
198 	/* reset i2s hw status before enable */
199 	regmap_update_bits(afe->regmap, i2s_data->i2s_ctrl_reg,
200 			   ASYS_I2S_CON_RESET, ASYS_I2S_CON_RESET);
201 	udelay(1);
202 	regmap_update_bits(afe->regmap, i2s_data->i2s_ctrl_reg,
203 			   ASYS_I2S_CON_RESET, 0);
204 	udelay(1);
205 	regmap_update_bits(afe->regmap, i2s_data->i2s_ctrl_reg,
206 			   ASYS_I2S_CON_I2S_EN, ASYS_I2S_CON_I2S_EN);
207 	return 0;
208 }
209 
210 static int mt2701_afe_i2s_prepare(struct snd_pcm_substream *substream,
211 				  struct snd_soc_dai *dai)
212 {
213 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
214 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
215 	int ret, i2s_num = mt2701_dai_num_to_i2s(afe, dai->id);
216 	struct mt2701_i2s_path *i2s_path;
217 	bool mode = afe_priv->soc->has_one_heart_mode;
218 
219 	if (i2s_num < 0)
220 		return i2s_num;
221 
222 	i2s_path = &afe_priv->i2s_path[i2s_num];
223 
224 	if (i2s_path->occupied[substream->stream])
225 		return -EBUSY;
226 
227 	ret = mt2701_mclk_configuration(afe, mode ? 1 : i2s_num);
228 	if (ret)
229 		return ret;
230 
231 	i2s_path->occupied[substream->stream] = 1;
232 
233 	/* need to enable i2s-out path when enable i2s-in */
234 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
235 		mt2701_i2s_path_enable(afe, i2s_path, !substream->stream,
236 				       substream->runtime->rate);
237 
238 	mt2701_i2s_path_enable(afe, i2s_path, substream->stream,
239 			       substream->runtime->rate);
240 
241 	return 0;
242 }
243 
244 static int mt2701_afe_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
245 				     unsigned int freq, int dir)
246 {
247 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
248 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
249 	int i2s_num = mt2701_dai_num_to_i2s(afe, dai->id);
250 	bool mode = afe_priv->soc->has_one_heart_mode;
251 
252 	if (i2s_num < 0)
253 		return i2s_num;
254 
255 	/* mclk */
256 	if (dir == SND_SOC_CLOCK_IN) {
257 		dev_warn(dai->dev, "The SoCs doesn't support mclk input\n");
258 		return -EINVAL;
259 	}
260 
261 	afe_priv->i2s_path[mode ? 1 : i2s_num].mclk_rate = freq;
262 
263 	return 0;
264 }
265 
266 static int mt2701_btmrg_startup(struct snd_pcm_substream *substream,
267 				struct snd_soc_dai *dai)
268 {
269 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
270 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
271 	int ret;
272 
273 	ret = mt2701_enable_btmrg_clk(afe);
274 	if (ret)
275 		return ret;
276 
277 	afe_priv->mrg_enable[substream->stream] = 1;
278 
279 	return 0;
280 }
281 
282 static int mt2701_btmrg_hw_params(struct snd_pcm_substream *substream,
283 				  struct snd_pcm_hw_params *params,
284 				  struct snd_soc_dai *dai)
285 {
286 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
287 	int stream_fs;
288 	u32 val, msk;
289 
290 	stream_fs = params_rate(params);
291 
292 	if (stream_fs != 8000 && stream_fs != 16000) {
293 		dev_err(afe->dev, "unsupported rate %d\n", stream_fs);
294 		return -EINVAL;
295 	}
296 
297 	regmap_update_bits(afe->regmap, AFE_MRGIF_CON,
298 			   AFE_MRGIF_CON_I2S_MODE_MASK,
299 			   AFE_MRGIF_CON_I2S_MODE_32K);
300 
301 	val = AFE_DAIBT_CON0_BT_FUNC_EN | AFE_DAIBT_CON0_BT_FUNC_RDY
302 	      | AFE_DAIBT_CON0_MRG_USE;
303 	msk = val;
304 
305 	if (stream_fs == 16000)
306 		val |= AFE_DAIBT_CON0_BT_WIDE_MODE_EN;
307 
308 	msk |= AFE_DAIBT_CON0_BT_WIDE_MODE_EN;
309 
310 	regmap_update_bits(afe->regmap, AFE_DAIBT_CON0, msk, val);
311 
312 	regmap_update_bits(afe->regmap, AFE_DAIBT_CON0,
313 			   AFE_DAIBT_CON0_DAIBT_EN,
314 			   AFE_DAIBT_CON0_DAIBT_EN);
315 	regmap_update_bits(afe->regmap, AFE_MRGIF_CON,
316 			   AFE_MRGIF_CON_MRG_I2S_EN,
317 			   AFE_MRGIF_CON_MRG_I2S_EN);
318 	regmap_update_bits(afe->regmap, AFE_MRGIF_CON,
319 			   AFE_MRGIF_CON_MRG_EN,
320 			   AFE_MRGIF_CON_MRG_EN);
321 	return 0;
322 }
323 
324 static void mt2701_btmrg_shutdown(struct snd_pcm_substream *substream,
325 				  struct snd_soc_dai *dai)
326 {
327 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
328 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
329 
330 	/* if the other direction stream is not occupied */
331 	if (!afe_priv->mrg_enable[!substream->stream]) {
332 		regmap_update_bits(afe->regmap, AFE_DAIBT_CON0,
333 				   AFE_DAIBT_CON0_DAIBT_EN, 0);
334 		regmap_update_bits(afe->regmap, AFE_MRGIF_CON,
335 				   AFE_MRGIF_CON_MRG_EN, 0);
336 		regmap_update_bits(afe->regmap, AFE_MRGIF_CON,
337 				   AFE_MRGIF_CON_MRG_I2S_EN, 0);
338 		mt2701_disable_btmrg_clk(afe);
339 	}
340 
341 	afe_priv->mrg_enable[substream->stream] = 0;
342 }
343 
344 static int mt2701_simple_fe_startup(struct snd_pcm_substream *substream,
345 				    struct snd_soc_dai *dai)
346 {
347 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
348 	struct mtk_base_afe_memif *memif_tmp;
349 	int stream_dir = substream->stream;
350 
351 	/* can't run single DL & DLM at the same time */
352 	if (stream_dir == SNDRV_PCM_STREAM_PLAYBACK) {
353 		memif_tmp = &afe->memif[MT2701_MEMIF_DLM];
354 		if (memif_tmp->substream) {
355 			dev_warn(afe->dev, "memif is not available");
356 			return -EBUSY;
357 		}
358 	}
359 
360 	return mtk_afe_fe_startup(substream, dai);
361 }
362 
363 static int mt2701_simple_fe_hw_params(struct snd_pcm_substream *substream,
364 				      struct snd_pcm_hw_params *params,
365 				      struct snd_soc_dai *dai)
366 {
367 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
368 	int stream_dir = substream->stream;
369 
370 	/* single DL use PAIR_INTERLEAVE */
371 	if (stream_dir == SNDRV_PCM_STREAM_PLAYBACK)
372 		regmap_update_bits(afe->regmap,
373 				   AFE_MEMIF_PBUF_SIZE,
374 				   AFE_MEMIF_PBUF_SIZE_DLM_MASK,
375 				   AFE_MEMIF_PBUF_SIZE_PAIR_INTERLEAVE);
376 
377 	return mtk_afe_fe_hw_params(substream, params, dai);
378 }
379 
380 static int mt2701_dlm_fe_startup(struct snd_pcm_substream *substream,
381 				 struct snd_soc_dai *dai)
382 {
383 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
384 	struct mtk_base_afe_memif *memif_tmp;
385 	const struct mtk_base_memif_data *memif_data;
386 	int i;
387 
388 	for (i = MT2701_MEMIF_DL1; i < MT2701_MEMIF_DL_SINGLE_NUM; ++i) {
389 		memif_tmp = &afe->memif[i];
390 		if (memif_tmp->substream)
391 			return -EBUSY;
392 	}
393 
394 	/* enable agent for all signal DL (due to hw design) */
395 	for (i = MT2701_MEMIF_DL1; i < MT2701_MEMIF_DL_SINGLE_NUM; ++i) {
396 		memif_data = afe->memif[i].data;
397 		regmap_update_bits(afe->regmap,
398 				   memif_data->agent_disable_reg,
399 				   1 << memif_data->agent_disable_shift,
400 				   0 << memif_data->agent_disable_shift);
401 	}
402 
403 	return mtk_afe_fe_startup(substream, dai);
404 }
405 
406 static void mt2701_dlm_fe_shutdown(struct snd_pcm_substream *substream,
407 				   struct snd_soc_dai *dai)
408 {
409 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
410 	const struct mtk_base_memif_data *memif_data;
411 	int i;
412 
413 	for (i = MT2701_MEMIF_DL1; i < MT2701_MEMIF_DL_SINGLE_NUM; ++i) {
414 		memif_data = afe->memif[i].data;
415 		regmap_update_bits(afe->regmap,
416 				   memif_data->agent_disable_reg,
417 				   1 << memif_data->agent_disable_shift,
418 				   1 << memif_data->agent_disable_shift);
419 	}
420 
421 	return mtk_afe_fe_shutdown(substream, dai);
422 }
423 
424 static int mt2701_dlm_fe_hw_params(struct snd_pcm_substream *substream,
425 				   struct snd_pcm_hw_params *params,
426 				   struct snd_soc_dai *dai)
427 {
428 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
429 	int channels = params_channels(params);
430 
431 	regmap_update_bits(afe->regmap,
432 			   AFE_MEMIF_PBUF_SIZE,
433 			   AFE_MEMIF_PBUF_SIZE_DLM_MASK,
434 			   AFE_MEMIF_PBUF_SIZE_FULL_INTERLEAVE);
435 	regmap_update_bits(afe->regmap,
436 			   AFE_MEMIF_PBUF_SIZE,
437 			   AFE_MEMIF_PBUF_SIZE_DLM_BYTE_MASK,
438 			   AFE_MEMIF_PBUF_SIZE_DLM_32BYTES);
439 	regmap_update_bits(afe->regmap,
440 			   AFE_MEMIF_PBUF_SIZE,
441 			   AFE_MEMIF_PBUF_SIZE_DLM_CH_MASK,
442 			   AFE_MEMIF_PBUF_SIZE_DLM_CH(channels));
443 
444 	return mtk_afe_fe_hw_params(substream, params, dai);
445 }
446 
447 static int mt2701_dlm_fe_trigger(struct snd_pcm_substream *substream,
448 				 int cmd, struct snd_soc_dai *dai)
449 {
450 	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
451 	struct mtk_base_afe_memif *memif_tmp = &afe->memif[MT2701_MEMIF_DL1];
452 
453 	switch (cmd) {
454 	case SNDRV_PCM_TRIGGER_START:
455 	case SNDRV_PCM_TRIGGER_RESUME:
456 		regmap_update_bits(afe->regmap, memif_tmp->data->enable_reg,
457 				   1 << memif_tmp->data->enable_shift,
458 				   1 << memif_tmp->data->enable_shift);
459 		mtk_afe_fe_trigger(substream, cmd, dai);
460 		return 0;
461 	case SNDRV_PCM_TRIGGER_STOP:
462 	case SNDRV_PCM_TRIGGER_SUSPEND:
463 		mtk_afe_fe_trigger(substream, cmd, dai);
464 		regmap_update_bits(afe->regmap, memif_tmp->data->enable_reg,
465 				   1 << memif_tmp->data->enable_shift, 0);
466 
467 		return 0;
468 	default:
469 		return -EINVAL;
470 	}
471 }
472 
473 static int mt2701_memif_fs(struct snd_pcm_substream *substream,
474 			   unsigned int rate)
475 {
476 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
477 	int fs;
478 
479 	if (rtd->cpu_dai->id != MT2701_MEMIF_ULBT)
480 		fs = mt2701_afe_i2s_fs(rate);
481 	else
482 		fs = (rate == 16000 ? 1 : 0);
483 
484 	return fs;
485 }
486 
487 static int mt2701_irq_fs(struct snd_pcm_substream *substream, unsigned int rate)
488 {
489 	return mt2701_afe_i2s_fs(rate);
490 }
491 
492 /* FE DAIs */
493 static const struct snd_soc_dai_ops mt2701_single_memif_dai_ops = {
494 	.startup	= mt2701_simple_fe_startup,
495 	.shutdown	= mtk_afe_fe_shutdown,
496 	.hw_params	= mt2701_simple_fe_hw_params,
497 	.hw_free	= mtk_afe_fe_hw_free,
498 	.prepare	= mtk_afe_fe_prepare,
499 	.trigger	= mtk_afe_fe_trigger,
500 };
501 
502 static const struct snd_soc_dai_ops mt2701_dlm_memif_dai_ops = {
503 	.startup	= mt2701_dlm_fe_startup,
504 	.shutdown	= mt2701_dlm_fe_shutdown,
505 	.hw_params	= mt2701_dlm_fe_hw_params,
506 	.hw_free	= mtk_afe_fe_hw_free,
507 	.prepare	= mtk_afe_fe_prepare,
508 	.trigger	= mt2701_dlm_fe_trigger,
509 };
510 
511 /* I2S BE DAIs */
512 static const struct snd_soc_dai_ops mt2701_afe_i2s_ops = {
513 	.startup	= mt2701_afe_i2s_startup,
514 	.shutdown	= mt2701_afe_i2s_shutdown,
515 	.prepare	= mt2701_afe_i2s_prepare,
516 	.set_sysclk	= mt2701_afe_i2s_set_sysclk,
517 };
518 
519 /* MRG BE DAIs */
520 static const struct snd_soc_dai_ops mt2701_btmrg_ops = {
521 	.startup = mt2701_btmrg_startup,
522 	.shutdown = mt2701_btmrg_shutdown,
523 	.hw_params = mt2701_btmrg_hw_params,
524 };
525 
526 static struct snd_soc_dai_driver mt2701_afe_pcm_dais[] = {
527 	/* FE DAIs: memory intefaces to CPU */
528 	{
529 		.name = "PCMO0",
530 		.id = MT2701_MEMIF_DL1,
531 		.suspend = mtk_afe_dai_suspend,
532 		.resume = mtk_afe_dai_resume,
533 		.playback = {
534 			.stream_name = "DL1",
535 			.channels_min = 1,
536 			.channels_max = 2,
537 			.rates = SNDRV_PCM_RATE_8000_192000,
538 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
539 				| SNDRV_PCM_FMTBIT_S24_LE
540 				| SNDRV_PCM_FMTBIT_S32_LE)
541 		},
542 		.ops = &mt2701_single_memif_dai_ops,
543 	},
544 	{
545 		.name = "PCM_multi",
546 		.id = MT2701_MEMIF_DLM,
547 		.suspend = mtk_afe_dai_suspend,
548 		.resume = mtk_afe_dai_resume,
549 		.playback = {
550 			.stream_name = "DLM",
551 			.channels_min = 1,
552 			.channels_max = 8,
553 			.rates = SNDRV_PCM_RATE_8000_192000,
554 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
555 				| SNDRV_PCM_FMTBIT_S24_LE
556 				| SNDRV_PCM_FMTBIT_S32_LE)
557 
558 		},
559 		.ops = &mt2701_dlm_memif_dai_ops,
560 	},
561 	{
562 		.name = "PCM0",
563 		.id = MT2701_MEMIF_UL1,
564 		.suspend = mtk_afe_dai_suspend,
565 		.resume = mtk_afe_dai_resume,
566 		.capture = {
567 			.stream_name = "UL1",
568 			.channels_min = 1,
569 			.channels_max = 2,
570 			.rates = SNDRV_PCM_RATE_8000_48000,
571 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
572 				| SNDRV_PCM_FMTBIT_S24_LE
573 				| SNDRV_PCM_FMTBIT_S32_LE)
574 		},
575 		.ops = &mt2701_single_memif_dai_ops,
576 	},
577 	{
578 		.name = "PCM1",
579 		.id = MT2701_MEMIF_UL2,
580 		.suspend = mtk_afe_dai_suspend,
581 		.resume = mtk_afe_dai_resume,
582 		.capture = {
583 			.stream_name = "UL2",
584 			.channels_min = 1,
585 			.channels_max = 2,
586 			.rates = SNDRV_PCM_RATE_8000_192000,
587 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
588 				| SNDRV_PCM_FMTBIT_S24_LE
589 				| SNDRV_PCM_FMTBIT_S32_LE)
590 
591 		},
592 		.ops = &mt2701_single_memif_dai_ops,
593 	},
594 	{
595 		.name = "PCM_BT_DL",
596 		.id = MT2701_MEMIF_DLBT,
597 		.suspend = mtk_afe_dai_suspend,
598 		.resume = mtk_afe_dai_resume,
599 		.playback = {
600 			.stream_name = "DLBT",
601 			.channels_min = 1,
602 			.channels_max = 1,
603 			.rates = (SNDRV_PCM_RATE_8000
604 				| SNDRV_PCM_RATE_16000),
605 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
606 		},
607 		.ops = &mt2701_single_memif_dai_ops,
608 	},
609 	{
610 		.name = "PCM_BT_UL",
611 		.id = MT2701_MEMIF_ULBT,
612 		.suspend = mtk_afe_dai_suspend,
613 		.resume = mtk_afe_dai_resume,
614 		.capture = {
615 			.stream_name = "ULBT",
616 			.channels_min = 1,
617 			.channels_max = 1,
618 			.rates = (SNDRV_PCM_RATE_8000
619 				| SNDRV_PCM_RATE_16000),
620 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
621 		},
622 		.ops = &mt2701_single_memif_dai_ops,
623 	},
624 	/* BE DAIs */
625 	{
626 		.name = "I2S0",
627 		.id = MT2701_IO_I2S,
628 		.playback = {
629 			.stream_name = "I2S0 Playback",
630 			.channels_min = 1,
631 			.channels_max = 2,
632 			.rates = SNDRV_PCM_RATE_8000_192000,
633 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
634 				| SNDRV_PCM_FMTBIT_S24_LE
635 				| SNDRV_PCM_FMTBIT_S32_LE)
636 
637 		},
638 		.capture = {
639 			.stream_name = "I2S0 Capture",
640 			.channels_min = 1,
641 			.channels_max = 2,
642 			.rates = SNDRV_PCM_RATE_8000_192000,
643 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
644 				| SNDRV_PCM_FMTBIT_S24_LE
645 				| SNDRV_PCM_FMTBIT_S32_LE)
646 
647 		},
648 		.ops = &mt2701_afe_i2s_ops,
649 		.symmetric_rates = 1,
650 	},
651 	{
652 		.name = "I2S1",
653 		.id = MT2701_IO_2ND_I2S,
654 		.playback = {
655 			.stream_name = "I2S1 Playback",
656 			.channels_min = 1,
657 			.channels_max = 2,
658 			.rates = SNDRV_PCM_RATE_8000_192000,
659 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
660 				| SNDRV_PCM_FMTBIT_S24_LE
661 				| SNDRV_PCM_FMTBIT_S32_LE)
662 			},
663 		.capture = {
664 			.stream_name = "I2S1 Capture",
665 			.channels_min = 1,
666 			.channels_max = 2,
667 			.rates = SNDRV_PCM_RATE_8000_192000,
668 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
669 				| SNDRV_PCM_FMTBIT_S24_LE
670 				| SNDRV_PCM_FMTBIT_S32_LE)
671 			},
672 		.ops = &mt2701_afe_i2s_ops,
673 		.symmetric_rates = 1,
674 	},
675 	{
676 		.name = "I2S2",
677 		.id = MT2701_IO_3RD_I2S,
678 		.playback = {
679 			.stream_name = "I2S2 Playback",
680 			.channels_min = 1,
681 			.channels_max = 2,
682 			.rates = SNDRV_PCM_RATE_8000_192000,
683 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
684 				| SNDRV_PCM_FMTBIT_S24_LE
685 				| SNDRV_PCM_FMTBIT_S32_LE)
686 			},
687 		.capture = {
688 			.stream_name = "I2S2 Capture",
689 			.channels_min = 1,
690 			.channels_max = 2,
691 			.rates = SNDRV_PCM_RATE_8000_192000,
692 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
693 				| SNDRV_PCM_FMTBIT_S24_LE
694 				| SNDRV_PCM_FMTBIT_S32_LE)
695 			},
696 		.ops = &mt2701_afe_i2s_ops,
697 		.symmetric_rates = 1,
698 	},
699 	{
700 		.name = "I2S3",
701 		.id = MT2701_IO_4TH_I2S,
702 		.playback = {
703 			.stream_name = "I2S3 Playback",
704 			.channels_min = 1,
705 			.channels_max = 2,
706 			.rates = SNDRV_PCM_RATE_8000_192000,
707 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
708 				| SNDRV_PCM_FMTBIT_S24_LE
709 				| SNDRV_PCM_FMTBIT_S32_LE)
710 			},
711 		.capture = {
712 			.stream_name = "I2S3 Capture",
713 			.channels_min = 1,
714 			.channels_max = 2,
715 			.rates = SNDRV_PCM_RATE_8000_192000,
716 			.formats = (SNDRV_PCM_FMTBIT_S16_LE
717 				| SNDRV_PCM_FMTBIT_S24_LE
718 				| SNDRV_PCM_FMTBIT_S32_LE)
719 			},
720 		.ops = &mt2701_afe_i2s_ops,
721 		.symmetric_rates = 1,
722 	},
723 	{
724 		.name = "MRG BT",
725 		.id = MT2701_IO_MRG,
726 		.playback = {
727 			.stream_name = "BT Playback",
728 			.channels_min = 1,
729 			.channels_max = 1,
730 			.rates = (SNDRV_PCM_RATE_8000
731 				| SNDRV_PCM_RATE_16000),
732 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
733 		},
734 		.capture = {
735 			.stream_name = "BT Capture",
736 			.channels_min = 1,
737 			.channels_max = 1,
738 			.rates = (SNDRV_PCM_RATE_8000
739 				| SNDRV_PCM_RATE_16000),
740 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
741 		},
742 		.ops = &mt2701_btmrg_ops,
743 		.symmetric_rates = 1,
744 	}
745 };
746 
747 static const struct snd_kcontrol_new mt2701_afe_o00_mix[] = {
748 	SOC_DAPM_SINGLE_AUTODISABLE("I00 Switch", AFE_CONN0, 0, 1, 0),
749 };
750 
751 static const struct snd_kcontrol_new mt2701_afe_o01_mix[] = {
752 	SOC_DAPM_SINGLE_AUTODISABLE("I01 Switch", AFE_CONN1, 1, 1, 0),
753 };
754 
755 static const struct snd_kcontrol_new mt2701_afe_o02_mix[] = {
756 	SOC_DAPM_SINGLE_AUTODISABLE("I02 Switch", AFE_CONN2, 2, 1, 0),
757 };
758 
759 static const struct snd_kcontrol_new mt2701_afe_o03_mix[] = {
760 	SOC_DAPM_SINGLE_AUTODISABLE("I03 Switch", AFE_CONN3, 3, 1, 0),
761 };
762 
763 static const struct snd_kcontrol_new mt2701_afe_o14_mix[] = {
764 	SOC_DAPM_SINGLE_AUTODISABLE("I26 Switch", AFE_CONN14, 26, 1, 0),
765 };
766 
767 static const struct snd_kcontrol_new mt2701_afe_o15_mix[] = {
768 	SOC_DAPM_SINGLE_AUTODISABLE("I12 Switch", AFE_CONN15, 12, 1, 0),
769 };
770 
771 static const struct snd_kcontrol_new mt2701_afe_o16_mix[] = {
772 	SOC_DAPM_SINGLE_AUTODISABLE("I13 Switch", AFE_CONN16, 13, 1, 0),
773 };
774 
775 static const struct snd_kcontrol_new mt2701_afe_o17_mix[] = {
776 	SOC_DAPM_SINGLE_AUTODISABLE("I14 Switch", AFE_CONN17, 14, 1, 0),
777 };
778 
779 static const struct snd_kcontrol_new mt2701_afe_o18_mix[] = {
780 	SOC_DAPM_SINGLE_AUTODISABLE("I15 Switch", AFE_CONN18, 15, 1, 0),
781 };
782 
783 static const struct snd_kcontrol_new mt2701_afe_o19_mix[] = {
784 	SOC_DAPM_SINGLE_AUTODISABLE("I16 Switch", AFE_CONN19, 16, 1, 0),
785 };
786 
787 static const struct snd_kcontrol_new mt2701_afe_o20_mix[] = {
788 	SOC_DAPM_SINGLE_AUTODISABLE("I17 Switch", AFE_CONN20, 17, 1, 0),
789 };
790 
791 static const struct snd_kcontrol_new mt2701_afe_o21_mix[] = {
792 	SOC_DAPM_SINGLE_AUTODISABLE("I18 Switch", AFE_CONN21, 18, 1, 0),
793 };
794 
795 static const struct snd_kcontrol_new mt2701_afe_o22_mix[] = {
796 	SOC_DAPM_SINGLE_AUTODISABLE("I19 Switch", AFE_CONN22, 19, 1, 0),
797 };
798 
799 static const struct snd_kcontrol_new mt2701_afe_o23_mix[] = {
800 	SOC_DAPM_SINGLE_AUTODISABLE("I20 Switch", AFE_CONN23, 20, 1, 0),
801 };
802 
803 static const struct snd_kcontrol_new mt2701_afe_o24_mix[] = {
804 	SOC_DAPM_SINGLE_AUTODISABLE("I21 Switch", AFE_CONN24, 21, 1, 0),
805 };
806 
807 static const struct snd_kcontrol_new mt2701_afe_o31_mix[] = {
808 	SOC_DAPM_SINGLE_AUTODISABLE("I35 Switch", AFE_CONN41, 9, 1, 0),
809 };
810 
811 static const struct snd_kcontrol_new mt2701_afe_i02_mix[] = {
812 	SOC_DAPM_SINGLE("I2S0 Switch", SND_SOC_NOPM, 0, 1, 0),
813 };
814 
815 static const struct snd_kcontrol_new mt2701_afe_multi_ch_out_i2s0[] = {
816 	SOC_DAPM_SINGLE_AUTODISABLE("Multich I2S0 Out Switch",
817 				    ASYS_I2SO1_CON, 26, 1, 0),
818 };
819 
820 static const struct snd_kcontrol_new mt2701_afe_multi_ch_out_i2s1[] = {
821 	SOC_DAPM_SINGLE_AUTODISABLE("Multich I2S1 Out Switch",
822 				    ASYS_I2SO2_CON, 26, 1, 0),
823 };
824 
825 static const struct snd_kcontrol_new mt2701_afe_multi_ch_out_i2s2[] = {
826 	SOC_DAPM_SINGLE_AUTODISABLE("Multich I2S2 Out Switch",
827 				    PWR2_TOP_CON, 17, 1, 0),
828 };
829 
830 static const struct snd_kcontrol_new mt2701_afe_multi_ch_out_i2s3[] = {
831 	SOC_DAPM_SINGLE_AUTODISABLE("Multich I2S3 Out Switch",
832 				    PWR2_TOP_CON, 18, 1, 0),
833 };
834 
835 static const struct snd_kcontrol_new mt2701_afe_multi_ch_out_i2s4[] = {
836 	SOC_DAPM_SINGLE_AUTODISABLE("Multich I2S4 Out Switch",
837 				    PWR2_TOP_CON, 19, 1, 0),
838 };
839 
840 static const struct snd_soc_dapm_widget mt2701_afe_pcm_widgets[] = {
841 	/* inter-connections */
842 	SND_SOC_DAPM_MIXER("I00", SND_SOC_NOPM, 0, 0, NULL, 0),
843 	SND_SOC_DAPM_MIXER("I01", SND_SOC_NOPM, 0, 0, NULL, 0),
844 	SND_SOC_DAPM_MIXER("I02", SND_SOC_NOPM, 0, 0, mt2701_afe_i02_mix,
845 			   ARRAY_SIZE(mt2701_afe_i02_mix)),
846 	SND_SOC_DAPM_MIXER("I03", SND_SOC_NOPM, 0, 0, NULL, 0),
847 	SND_SOC_DAPM_MIXER("I12", SND_SOC_NOPM, 0, 0, NULL, 0),
848 	SND_SOC_DAPM_MIXER("I13", SND_SOC_NOPM, 0, 0, NULL, 0),
849 	SND_SOC_DAPM_MIXER("I14", SND_SOC_NOPM, 0, 0, NULL, 0),
850 	SND_SOC_DAPM_MIXER("I15", SND_SOC_NOPM, 0, 0, NULL, 0),
851 	SND_SOC_DAPM_MIXER("I16", SND_SOC_NOPM, 0, 0, NULL, 0),
852 	SND_SOC_DAPM_MIXER("I17", SND_SOC_NOPM, 0, 0, NULL, 0),
853 	SND_SOC_DAPM_MIXER("I18", SND_SOC_NOPM, 0, 0, NULL, 0),
854 	SND_SOC_DAPM_MIXER("I19", SND_SOC_NOPM, 0, 0, NULL, 0),
855 	SND_SOC_DAPM_MIXER("I26", SND_SOC_NOPM, 0, 0, NULL, 0),
856 	SND_SOC_DAPM_MIXER("I35", SND_SOC_NOPM, 0, 0, NULL, 0),
857 
858 	SND_SOC_DAPM_MIXER("O00", SND_SOC_NOPM, 0, 0, mt2701_afe_o00_mix,
859 			   ARRAY_SIZE(mt2701_afe_o00_mix)),
860 	SND_SOC_DAPM_MIXER("O01", SND_SOC_NOPM, 0, 0, mt2701_afe_o01_mix,
861 			   ARRAY_SIZE(mt2701_afe_o01_mix)),
862 	SND_SOC_DAPM_MIXER("O02", SND_SOC_NOPM, 0, 0, mt2701_afe_o02_mix,
863 			   ARRAY_SIZE(mt2701_afe_o02_mix)),
864 	SND_SOC_DAPM_MIXER("O03", SND_SOC_NOPM, 0, 0, mt2701_afe_o03_mix,
865 			   ARRAY_SIZE(mt2701_afe_o03_mix)),
866 	SND_SOC_DAPM_MIXER("O14", SND_SOC_NOPM, 0, 0, mt2701_afe_o14_mix,
867 			   ARRAY_SIZE(mt2701_afe_o14_mix)),
868 	SND_SOC_DAPM_MIXER("O15", SND_SOC_NOPM, 0, 0, mt2701_afe_o15_mix,
869 			   ARRAY_SIZE(mt2701_afe_o15_mix)),
870 	SND_SOC_DAPM_MIXER("O16", SND_SOC_NOPM, 0, 0, mt2701_afe_o16_mix,
871 			   ARRAY_SIZE(mt2701_afe_o16_mix)),
872 	SND_SOC_DAPM_MIXER("O17", SND_SOC_NOPM, 0, 0, mt2701_afe_o17_mix,
873 			   ARRAY_SIZE(mt2701_afe_o17_mix)),
874 	SND_SOC_DAPM_MIXER("O18", SND_SOC_NOPM, 0, 0, mt2701_afe_o18_mix,
875 			   ARRAY_SIZE(mt2701_afe_o18_mix)),
876 	SND_SOC_DAPM_MIXER("O19", SND_SOC_NOPM, 0, 0, mt2701_afe_o19_mix,
877 			   ARRAY_SIZE(mt2701_afe_o19_mix)),
878 	SND_SOC_DAPM_MIXER("O20", SND_SOC_NOPM, 0, 0, mt2701_afe_o20_mix,
879 			   ARRAY_SIZE(mt2701_afe_o20_mix)),
880 	SND_SOC_DAPM_MIXER("O21", SND_SOC_NOPM, 0, 0, mt2701_afe_o21_mix,
881 			   ARRAY_SIZE(mt2701_afe_o21_mix)),
882 	SND_SOC_DAPM_MIXER("O22", SND_SOC_NOPM, 0, 0, mt2701_afe_o22_mix,
883 			   ARRAY_SIZE(mt2701_afe_o22_mix)),
884 	SND_SOC_DAPM_MIXER("O31", SND_SOC_NOPM, 0, 0, mt2701_afe_o31_mix,
885 			   ARRAY_SIZE(mt2701_afe_o31_mix)),
886 
887 	SND_SOC_DAPM_MIXER("I12I13", SND_SOC_NOPM, 0, 0,
888 			   mt2701_afe_multi_ch_out_i2s0,
889 			   ARRAY_SIZE(mt2701_afe_multi_ch_out_i2s0)),
890 	SND_SOC_DAPM_MIXER("I14I15", SND_SOC_NOPM, 0, 0,
891 			   mt2701_afe_multi_ch_out_i2s1,
892 			   ARRAY_SIZE(mt2701_afe_multi_ch_out_i2s1)),
893 	SND_SOC_DAPM_MIXER("I16I17", SND_SOC_NOPM, 0, 0,
894 			   mt2701_afe_multi_ch_out_i2s2,
895 			   ARRAY_SIZE(mt2701_afe_multi_ch_out_i2s2)),
896 	SND_SOC_DAPM_MIXER("I18I19", SND_SOC_NOPM, 0, 0,
897 			   mt2701_afe_multi_ch_out_i2s3,
898 			   ARRAY_SIZE(mt2701_afe_multi_ch_out_i2s3)),
899 };
900 
901 static const struct snd_soc_dapm_route mt2701_afe_pcm_routes[] = {
902 	{"I12", NULL, "DL1"},
903 	{"I13", NULL, "DL1"},
904 	{"I35", NULL, "DLBT"},
905 
906 	{"I2S0 Playback", NULL, "O15"},
907 	{"I2S0 Playback", NULL, "O16"},
908 	{"I2S1 Playback", NULL, "O17"},
909 	{"I2S1 Playback", NULL, "O18"},
910 	{"I2S2 Playback", NULL, "O19"},
911 	{"I2S2 Playback", NULL, "O20"},
912 	{"I2S3 Playback", NULL, "O21"},
913 	{"I2S3 Playback", NULL, "O22"},
914 	{"BT Playback", NULL, "O31"},
915 
916 	{"UL1", NULL, "O00"},
917 	{"UL1", NULL, "O01"},
918 	{"UL2", NULL, "O02"},
919 	{"UL2", NULL, "O03"},
920 	{"ULBT", NULL, "O14"},
921 
922 	{"I00", NULL, "I2S0 Capture"},
923 	{"I01", NULL, "I2S0 Capture"},
924 	{"I02", NULL, "I2S1 Capture"},
925 	{"I03", NULL, "I2S1 Capture"},
926 	/* I02,03 link to UL2, also need to open I2S0 */
927 	{"I02", "I2S0 Switch", "I2S0 Capture"},
928 
929 	{"I26", NULL, "BT Capture"},
930 
931 	{"I12I13", "Multich I2S0 Out Switch", "DLM"},
932 	{"I14I15", "Multich I2S1 Out Switch", "DLM"},
933 	{"I16I17", "Multich I2S2 Out Switch", "DLM"},
934 	{"I18I19", "Multich I2S3 Out Switch", "DLM"},
935 
936 	{ "I12", NULL, "I12I13" },
937 	{ "I13", NULL, "I12I13" },
938 	{ "I14", NULL, "I14I15" },
939 	{ "I15", NULL, "I14I15" },
940 	{ "I16", NULL, "I16I17" },
941 	{ "I17", NULL, "I16I17" },
942 	{ "I18", NULL, "I18I19" },
943 	{ "I19", NULL, "I18I19" },
944 
945 	{ "O00", "I00 Switch", "I00" },
946 	{ "O01", "I01 Switch", "I01" },
947 	{ "O02", "I02 Switch", "I02" },
948 	{ "O03", "I03 Switch", "I03" },
949 	{ "O14", "I26 Switch", "I26" },
950 	{ "O15", "I12 Switch", "I12" },
951 	{ "O16", "I13 Switch", "I13" },
952 	{ "O17", "I14 Switch", "I14" },
953 	{ "O18", "I15 Switch", "I15" },
954 	{ "O19", "I16 Switch", "I16" },
955 	{ "O20", "I17 Switch", "I17" },
956 	{ "O21", "I18 Switch", "I18" },
957 	{ "O22", "I19 Switch", "I19" },
958 	{ "O31", "I35 Switch", "I35" },
959 };
960 
961 static int mt2701_afe_pcm_probe(struct snd_soc_component *component)
962 {
963 	struct mtk_base_afe *afe = snd_soc_component_get_drvdata(component);
964 
965 	snd_soc_component_init_regmap(component, afe->regmap);
966 
967 	return 0;
968 }
969 
970 static const struct snd_soc_component_driver mt2701_afe_pcm_dai_component = {
971 	.probe = mt2701_afe_pcm_probe,
972 	.name = "mt2701-afe-pcm-dai",
973 	.dapm_widgets = mt2701_afe_pcm_widgets,
974 	.num_dapm_widgets = ARRAY_SIZE(mt2701_afe_pcm_widgets),
975 	.dapm_routes = mt2701_afe_pcm_routes,
976 	.num_dapm_routes = ARRAY_SIZE(mt2701_afe_pcm_routes),
977 };
978 
979 static const struct mtk_base_memif_data memif_data[MT2701_MEMIF_NUM] = {
980 	{
981 		.name = "DL1",
982 		.id = MT2701_MEMIF_DL1,
983 		.reg_ofs_base = AFE_DL1_BASE,
984 		.reg_ofs_cur = AFE_DL1_CUR,
985 		.fs_reg = AFE_DAC_CON1,
986 		.fs_shift = 0,
987 		.fs_maskbit = 0x1f,
988 		.mono_reg = AFE_DAC_CON3,
989 		.mono_shift = 16,
990 		.enable_reg = AFE_DAC_CON0,
991 		.enable_shift = 1,
992 		.hd_reg = AFE_MEMIF_HD_CON0,
993 		.hd_shift = 0,
994 		.agent_disable_reg = AUDIO_TOP_CON5,
995 		.agent_disable_shift = 6,
996 		.msb_reg = -1,
997 		.msb_shift = -1,
998 	},
999 	{
1000 		.name = "DL2",
1001 		.id = MT2701_MEMIF_DL2,
1002 		.reg_ofs_base = AFE_DL2_BASE,
1003 		.reg_ofs_cur = AFE_DL2_CUR,
1004 		.fs_reg = AFE_DAC_CON1,
1005 		.fs_shift = 5,
1006 		.fs_maskbit = 0x1f,
1007 		.mono_reg = AFE_DAC_CON3,
1008 		.mono_shift = 17,
1009 		.enable_reg = AFE_DAC_CON0,
1010 		.enable_shift = 2,
1011 		.hd_reg = AFE_MEMIF_HD_CON0,
1012 		.hd_shift = 2,
1013 		.agent_disable_reg = AUDIO_TOP_CON5,
1014 		.agent_disable_shift = 7,
1015 		.msb_reg = -1,
1016 		.msb_shift = -1,
1017 	},
1018 	{
1019 		.name = "DL3",
1020 		.id = MT2701_MEMIF_DL3,
1021 		.reg_ofs_base = AFE_DL3_BASE,
1022 		.reg_ofs_cur = AFE_DL3_CUR,
1023 		.fs_reg = AFE_DAC_CON1,
1024 		.fs_shift = 10,
1025 		.fs_maskbit = 0x1f,
1026 		.mono_reg = AFE_DAC_CON3,
1027 		.mono_shift = 18,
1028 		.enable_reg = AFE_DAC_CON0,
1029 		.enable_shift = 3,
1030 		.hd_reg = AFE_MEMIF_HD_CON0,
1031 		.hd_shift = 4,
1032 		.agent_disable_reg = AUDIO_TOP_CON5,
1033 		.agent_disable_shift = 8,
1034 		.msb_reg = -1,
1035 		.msb_shift = -1,
1036 	},
1037 	{
1038 		.name = "DL4",
1039 		.id = MT2701_MEMIF_DL4,
1040 		.reg_ofs_base = AFE_DL4_BASE,
1041 		.reg_ofs_cur = AFE_DL4_CUR,
1042 		.fs_reg = AFE_DAC_CON1,
1043 		.fs_shift = 15,
1044 		.fs_maskbit = 0x1f,
1045 		.mono_reg = AFE_DAC_CON3,
1046 		.mono_shift = 19,
1047 		.enable_reg = AFE_DAC_CON0,
1048 		.enable_shift = 4,
1049 		.hd_reg = AFE_MEMIF_HD_CON0,
1050 		.hd_shift = 6,
1051 		.agent_disable_reg = AUDIO_TOP_CON5,
1052 		.agent_disable_shift = 9,
1053 		.msb_reg = -1,
1054 		.msb_shift = -1,
1055 	},
1056 	{
1057 		.name = "DL5",
1058 		.id = MT2701_MEMIF_DL5,
1059 		.reg_ofs_base = AFE_DL5_BASE,
1060 		.reg_ofs_cur = AFE_DL5_CUR,
1061 		.fs_reg = AFE_DAC_CON1,
1062 		.fs_shift = 20,
1063 		.fs_maskbit = 0x1f,
1064 		.mono_reg = AFE_DAC_CON3,
1065 		.mono_shift = 20,
1066 		.enable_reg = AFE_DAC_CON0,
1067 		.enable_shift = 5,
1068 		.hd_reg = AFE_MEMIF_HD_CON0,
1069 		.hd_shift = 8,
1070 		.agent_disable_reg = AUDIO_TOP_CON5,
1071 		.agent_disable_shift = 10,
1072 		.msb_reg = -1,
1073 		.msb_shift = -1,
1074 	},
1075 	{
1076 		.name = "DLM",
1077 		.id = MT2701_MEMIF_DLM,
1078 		.reg_ofs_base = AFE_DLMCH_BASE,
1079 		.reg_ofs_cur = AFE_DLMCH_CUR,
1080 		.fs_reg = AFE_DAC_CON1,
1081 		.fs_shift = 0,
1082 		.fs_maskbit = 0x1f,
1083 		.mono_reg = -1,
1084 		.mono_shift = -1,
1085 		.enable_reg = AFE_DAC_CON0,
1086 		.enable_shift = 7,
1087 		.hd_reg = AFE_MEMIF_PBUF_SIZE,
1088 		.hd_shift = 28,
1089 		.agent_disable_reg = AUDIO_TOP_CON5,
1090 		.agent_disable_shift = 12,
1091 		.msb_reg = -1,
1092 		.msb_shift = -1,
1093 	},
1094 	{
1095 		.name = "UL1",
1096 		.id = MT2701_MEMIF_UL1,
1097 		.reg_ofs_base = AFE_VUL_BASE,
1098 		.reg_ofs_cur = AFE_VUL_CUR,
1099 		.fs_reg = AFE_DAC_CON2,
1100 		.fs_shift = 0,
1101 		.fs_maskbit = 0x1f,
1102 		.mono_reg = AFE_DAC_CON4,
1103 		.mono_shift = 0,
1104 		.enable_reg = AFE_DAC_CON0,
1105 		.enable_shift = 10,
1106 		.hd_reg = AFE_MEMIF_HD_CON1,
1107 		.hd_shift = 0,
1108 		.agent_disable_reg = AUDIO_TOP_CON5,
1109 		.agent_disable_shift = 0,
1110 		.msb_reg = -1,
1111 		.msb_shift = -1,
1112 	},
1113 	{
1114 		.name = "UL2",
1115 		.id = MT2701_MEMIF_UL2,
1116 		.reg_ofs_base = AFE_UL2_BASE,
1117 		.reg_ofs_cur = AFE_UL2_CUR,
1118 		.fs_reg = AFE_DAC_CON2,
1119 		.fs_shift = 5,
1120 		.fs_maskbit = 0x1f,
1121 		.mono_reg = AFE_DAC_CON4,
1122 		.mono_shift = 2,
1123 		.enable_reg = AFE_DAC_CON0,
1124 		.enable_shift = 11,
1125 		.hd_reg = AFE_MEMIF_HD_CON1,
1126 		.hd_shift = 2,
1127 		.agent_disable_reg = AUDIO_TOP_CON5,
1128 		.agent_disable_shift = 1,
1129 		.msb_reg = -1,
1130 		.msb_shift = -1,
1131 	},
1132 	{
1133 		.name = "UL3",
1134 		.id = MT2701_MEMIF_UL3,
1135 		.reg_ofs_base = AFE_UL3_BASE,
1136 		.reg_ofs_cur = AFE_UL3_CUR,
1137 		.fs_reg = AFE_DAC_CON2,
1138 		.fs_shift = 10,
1139 		.fs_maskbit = 0x1f,
1140 		.mono_reg = AFE_DAC_CON4,
1141 		.mono_shift = 4,
1142 		.enable_reg = AFE_DAC_CON0,
1143 		.enable_shift = 12,
1144 		.hd_reg = AFE_MEMIF_HD_CON0,
1145 		.hd_shift = 0,
1146 		.agent_disable_reg = AUDIO_TOP_CON5,
1147 		.agent_disable_shift = 2,
1148 		.msb_reg = -1,
1149 		.msb_shift = -1,
1150 	},
1151 	{
1152 		.name = "UL4",
1153 		.id = MT2701_MEMIF_UL4,
1154 		.reg_ofs_base = AFE_UL4_BASE,
1155 		.reg_ofs_cur = AFE_UL4_CUR,
1156 		.fs_reg = AFE_DAC_CON2,
1157 		.fs_shift = 15,
1158 		.fs_maskbit = 0x1f,
1159 		.mono_reg = AFE_DAC_CON4,
1160 		.mono_shift = 6,
1161 		.enable_reg = AFE_DAC_CON0,
1162 		.enable_shift = 13,
1163 		.hd_reg = AFE_MEMIF_HD_CON0,
1164 		.hd_shift = 6,
1165 		.agent_disable_reg = AUDIO_TOP_CON5,
1166 		.agent_disable_shift = 3,
1167 		.msb_reg = -1,
1168 		.msb_shift = -1,
1169 	},
1170 	{
1171 		.name = "UL5",
1172 		.id = MT2701_MEMIF_UL5,
1173 		.reg_ofs_base = AFE_UL5_BASE,
1174 		.reg_ofs_cur = AFE_UL5_CUR,
1175 		.fs_reg = AFE_DAC_CON2,
1176 		.fs_shift = 20,
1177 		.mono_reg = AFE_DAC_CON4,
1178 		.mono_shift = 8,
1179 		.fs_maskbit = 0x1f,
1180 		.enable_reg = AFE_DAC_CON0,
1181 		.enable_shift = 14,
1182 		.hd_reg = AFE_MEMIF_HD_CON0,
1183 		.hd_shift = 8,
1184 		.agent_disable_reg = AUDIO_TOP_CON5,
1185 		.agent_disable_shift = 4,
1186 		.msb_reg = -1,
1187 		.msb_shift = -1,
1188 	},
1189 	{
1190 		.name = "DLBT",
1191 		.id = MT2701_MEMIF_DLBT,
1192 		.reg_ofs_base = AFE_ARB1_BASE,
1193 		.reg_ofs_cur = AFE_ARB1_CUR,
1194 		.fs_reg = AFE_DAC_CON3,
1195 		.fs_shift = 10,
1196 		.fs_maskbit = 0x1f,
1197 		.mono_reg = AFE_DAC_CON3,
1198 		.mono_shift = 22,
1199 		.enable_reg = AFE_DAC_CON0,
1200 		.enable_shift = 8,
1201 		.hd_reg = AFE_MEMIF_HD_CON0,
1202 		.hd_shift = 14,
1203 		.agent_disable_reg = AUDIO_TOP_CON5,
1204 		.agent_disable_shift = 13,
1205 		.msb_reg = -1,
1206 		.msb_shift = -1,
1207 	},
1208 	{
1209 		.name = "ULBT",
1210 		.id = MT2701_MEMIF_ULBT,
1211 		.reg_ofs_base = AFE_DAI_BASE,
1212 		.reg_ofs_cur = AFE_DAI_CUR,
1213 		.fs_reg = AFE_DAC_CON2,
1214 		.fs_shift = 30,
1215 		.fs_maskbit = 0x1,
1216 		.mono_reg = -1,
1217 		.mono_shift = -1,
1218 		.enable_reg = AFE_DAC_CON0,
1219 		.enable_shift = 17,
1220 		.hd_reg = AFE_MEMIF_HD_CON1,
1221 		.hd_shift = 20,
1222 		.agent_disable_reg = AUDIO_TOP_CON5,
1223 		.agent_disable_shift = 16,
1224 		.msb_reg = -1,
1225 		.msb_shift = -1,
1226 	},
1227 };
1228 
1229 static const struct mtk_base_irq_data irq_data[MT2701_IRQ_ASYS_END] = {
1230 	{
1231 		.id = MT2701_IRQ_ASYS_IRQ1,
1232 		.irq_cnt_reg = ASYS_IRQ1_CON,
1233 		.irq_cnt_shift = 0,
1234 		.irq_cnt_maskbit = 0xffffff,
1235 		.irq_fs_reg = ASYS_IRQ1_CON,
1236 		.irq_fs_shift = 24,
1237 		.irq_fs_maskbit = 0x1f,
1238 		.irq_en_reg = ASYS_IRQ1_CON,
1239 		.irq_en_shift = 31,
1240 		.irq_clr_reg = ASYS_IRQ_CLR,
1241 		.irq_clr_shift = 0,
1242 	},
1243 	{
1244 		.id = MT2701_IRQ_ASYS_IRQ2,
1245 		.irq_cnt_reg = ASYS_IRQ2_CON,
1246 		.irq_cnt_shift = 0,
1247 		.irq_cnt_maskbit = 0xffffff,
1248 		.irq_fs_reg = ASYS_IRQ2_CON,
1249 		.irq_fs_shift = 24,
1250 		.irq_fs_maskbit = 0x1f,
1251 		.irq_en_reg = ASYS_IRQ2_CON,
1252 		.irq_en_shift = 31,
1253 		.irq_clr_reg = ASYS_IRQ_CLR,
1254 		.irq_clr_shift = 1,
1255 	},
1256 	{
1257 		.id = MT2701_IRQ_ASYS_IRQ3,
1258 		.irq_cnt_reg = ASYS_IRQ3_CON,
1259 		.irq_cnt_shift = 0,
1260 		.irq_cnt_maskbit = 0xffffff,
1261 		.irq_fs_reg = ASYS_IRQ3_CON,
1262 		.irq_fs_shift = 24,
1263 		.irq_fs_maskbit = 0x1f,
1264 		.irq_en_reg = ASYS_IRQ3_CON,
1265 		.irq_en_shift = 31,
1266 		.irq_clr_reg = ASYS_IRQ_CLR,
1267 		.irq_clr_shift = 2,
1268 	}
1269 };
1270 
1271 static const struct mt2701_i2s_data mt2701_i2s_data[][2] = {
1272 	{
1273 		{ ASYS_I2SO1_CON, 0, 0x1f },
1274 		{ ASYS_I2SIN1_CON, 0, 0x1f },
1275 	},
1276 	{
1277 		{ ASYS_I2SO2_CON, 5, 0x1f },
1278 		{ ASYS_I2SIN2_CON, 5, 0x1f },
1279 	},
1280 	{
1281 		{ ASYS_I2SO3_CON, 10, 0x1f },
1282 		{ ASYS_I2SIN3_CON, 10, 0x1f },
1283 	},
1284 	{
1285 		{ ASYS_I2SO4_CON, 15, 0x1f },
1286 		{ ASYS_I2SIN4_CON, 15, 0x1f },
1287 	},
1288 	/* TODO - extend control registers supported by newer SoCs */
1289 };
1290 
1291 static irqreturn_t mt2701_asys_isr(int irq_id, void *dev)
1292 {
1293 	int id;
1294 	struct mtk_base_afe *afe = dev;
1295 	struct mtk_base_afe_memif *memif;
1296 	struct mtk_base_afe_irq *irq;
1297 	u32 status;
1298 
1299 	regmap_read(afe->regmap, ASYS_IRQ_STATUS, &status);
1300 	regmap_write(afe->regmap, ASYS_IRQ_CLR, status);
1301 
1302 	for (id = 0; id < MT2701_MEMIF_NUM; ++id) {
1303 		memif = &afe->memif[id];
1304 		if (memif->irq_usage < 0)
1305 			continue;
1306 
1307 		irq = &afe->irqs[memif->irq_usage];
1308 		if (status & 1 << irq->irq_data->irq_clr_shift)
1309 			snd_pcm_period_elapsed(memif->substream);
1310 	}
1311 
1312 	return IRQ_HANDLED;
1313 }
1314 
1315 static int mt2701_afe_runtime_suspend(struct device *dev)
1316 {
1317 	struct mtk_base_afe *afe = dev_get_drvdata(dev);
1318 
1319 	return mt2701_afe_disable_clock(afe);
1320 }
1321 
1322 static int mt2701_afe_runtime_resume(struct device *dev)
1323 {
1324 	struct mtk_base_afe *afe = dev_get_drvdata(dev);
1325 
1326 	return mt2701_afe_enable_clock(afe);
1327 }
1328 
1329 static int mt2701_afe_pcm_dev_probe(struct platform_device *pdev)
1330 {
1331 	struct mtk_base_afe *afe;
1332 	struct mt2701_afe_private *afe_priv;
1333 	struct device *dev;
1334 	int i, irq_id, ret;
1335 
1336 	afe = devm_kzalloc(&pdev->dev, sizeof(*afe), GFP_KERNEL);
1337 	if (!afe)
1338 		return -ENOMEM;
1339 
1340 	afe->platform_priv = devm_kzalloc(&pdev->dev, sizeof(*afe_priv),
1341 					  GFP_KERNEL);
1342 	if (!afe->platform_priv)
1343 		return -ENOMEM;
1344 
1345 	afe_priv = afe->platform_priv;
1346 	afe_priv->soc = of_device_get_match_data(&pdev->dev);
1347 	afe->dev = &pdev->dev;
1348 	dev = afe->dev;
1349 
1350 	afe_priv->i2s_path = devm_kcalloc(dev,
1351 					  afe_priv->soc->i2s_num,
1352 					  sizeof(struct mt2701_i2s_path),
1353 					  GFP_KERNEL);
1354 	if (!afe_priv->i2s_path)
1355 		return -ENOMEM;
1356 
1357 	irq_id = platform_get_irq_byname(pdev, "asys");
1358 	if (irq_id < 0) {
1359 		dev_err(dev, "unable to get ASYS IRQ\n");
1360 		return irq_id;
1361 	}
1362 
1363 	ret = devm_request_irq(dev, irq_id, mt2701_asys_isr,
1364 			       IRQF_TRIGGER_NONE, "asys-isr", (void *)afe);
1365 	if (ret) {
1366 		dev_err(dev, "could not request_irq for asys-isr\n");
1367 		return ret;
1368 	}
1369 
1370 	afe->regmap = syscon_node_to_regmap(dev->parent->of_node);
1371 	if (IS_ERR(afe->regmap)) {
1372 		dev_err(dev, "could not get regmap from parent\n");
1373 		return PTR_ERR(afe->regmap);
1374 	}
1375 
1376 	mutex_init(&afe->irq_alloc_lock);
1377 
1378 	/* memif initialize */
1379 	afe->memif_size = MT2701_MEMIF_NUM;
1380 	afe->memif = devm_kcalloc(dev, afe->memif_size, sizeof(*afe->memif),
1381 				  GFP_KERNEL);
1382 	if (!afe->memif)
1383 		return -ENOMEM;
1384 
1385 	for (i = 0; i < afe->memif_size; i++) {
1386 		afe->memif[i].data = &memif_data[i];
1387 		afe->memif[i].irq_usage = -1;
1388 	}
1389 
1390 	/* irq initialize */
1391 	afe->irqs_size = MT2701_IRQ_ASYS_END;
1392 	afe->irqs = devm_kcalloc(dev, afe->irqs_size, sizeof(*afe->irqs),
1393 				 GFP_KERNEL);
1394 	if (!afe->irqs)
1395 		return -ENOMEM;
1396 
1397 	for (i = 0; i < afe->irqs_size; i++)
1398 		afe->irqs[i].irq_data = &irq_data[i];
1399 
1400 	/* I2S initialize */
1401 	for (i = 0; i < afe_priv->soc->i2s_num; i++) {
1402 		afe_priv->i2s_path[i].i2s_data[SNDRV_PCM_STREAM_PLAYBACK] =
1403 			&mt2701_i2s_data[i][SNDRV_PCM_STREAM_PLAYBACK];
1404 		afe_priv->i2s_path[i].i2s_data[SNDRV_PCM_STREAM_CAPTURE] =
1405 			&mt2701_i2s_data[i][SNDRV_PCM_STREAM_CAPTURE];
1406 	}
1407 
1408 	afe->mtk_afe_hardware = &mt2701_afe_hardware;
1409 	afe->memif_fs = mt2701_memif_fs;
1410 	afe->irq_fs = mt2701_irq_fs;
1411 	afe->reg_back_up_list = mt2701_afe_backup_list;
1412 	afe->reg_back_up_list_num = ARRAY_SIZE(mt2701_afe_backup_list);
1413 	afe->runtime_resume = mt2701_afe_runtime_resume;
1414 	afe->runtime_suspend = mt2701_afe_runtime_suspend;
1415 
1416 	/* initial audio related clock */
1417 	ret = mt2701_init_clock(afe);
1418 	if (ret) {
1419 		dev_err(dev, "init clock error\n");
1420 		return ret;
1421 	}
1422 
1423 	platform_set_drvdata(pdev, afe);
1424 
1425 	pm_runtime_enable(dev);
1426 	if (!pm_runtime_enabled(dev)) {
1427 		ret = mt2701_afe_runtime_resume(dev);
1428 		if (ret)
1429 			goto err_pm_disable;
1430 	}
1431 	pm_runtime_get_sync(dev);
1432 
1433 	ret = devm_snd_soc_register_component(&pdev->dev, &mtk_afe_pcm_platform,
1434 					      NULL, 0);
1435 	if (ret) {
1436 		dev_warn(dev, "err_platform\n");
1437 		goto err_platform;
1438 	}
1439 
1440 	ret = devm_snd_soc_register_component(&pdev->dev,
1441 					 &mt2701_afe_pcm_dai_component,
1442 					 mt2701_afe_pcm_dais,
1443 					 ARRAY_SIZE(mt2701_afe_pcm_dais));
1444 	if (ret) {
1445 		dev_warn(dev, "err_dai_component\n");
1446 		goto err_platform;
1447 	}
1448 
1449 	return 0;
1450 
1451 err_platform:
1452 	pm_runtime_put_sync(dev);
1453 err_pm_disable:
1454 	pm_runtime_disable(dev);
1455 
1456 	return ret;
1457 }
1458 
1459 static int mt2701_afe_pcm_dev_remove(struct platform_device *pdev)
1460 {
1461 	pm_runtime_put_sync(&pdev->dev);
1462 	pm_runtime_disable(&pdev->dev);
1463 	if (!pm_runtime_status_suspended(&pdev->dev))
1464 		mt2701_afe_runtime_suspend(&pdev->dev);
1465 
1466 	return 0;
1467 }
1468 
1469 static const struct mt2701_soc_variants mt2701_soc_v1 = {
1470 	.i2s_num = 4,
1471 };
1472 
1473 static const struct mt2701_soc_variants mt2701_soc_v2 = {
1474 	.has_one_heart_mode = true,
1475 	.i2s_num = 4,
1476 };
1477 
1478 static const struct of_device_id mt2701_afe_pcm_dt_match[] = {
1479 	{ .compatible = "mediatek,mt2701-audio", .data = &mt2701_soc_v1 },
1480 	{ .compatible = "mediatek,mt7622-audio", .data = &mt2701_soc_v2 },
1481 	{},
1482 };
1483 MODULE_DEVICE_TABLE(of, mt2701_afe_pcm_dt_match);
1484 
1485 static const struct dev_pm_ops mt2701_afe_pm_ops = {
1486 	SET_RUNTIME_PM_OPS(mt2701_afe_runtime_suspend,
1487 			   mt2701_afe_runtime_resume, NULL)
1488 };
1489 
1490 static struct platform_driver mt2701_afe_pcm_driver = {
1491 	.driver = {
1492 		   .name = "mt2701-audio",
1493 		   .of_match_table = mt2701_afe_pcm_dt_match,
1494 #ifdef CONFIG_PM
1495 		   .pm = &mt2701_afe_pm_ops,
1496 #endif
1497 	},
1498 	.probe = mt2701_afe_pcm_dev_probe,
1499 	.remove = mt2701_afe_pcm_dev_remove,
1500 };
1501 
1502 module_platform_driver(mt2701_afe_pcm_driver);
1503 
1504 MODULE_DESCRIPTION("Mediatek ALSA SoC AFE platform driver for 2701");
1505 MODULE_AUTHOR("Garlic Tseng <garlic.tseng@mediatek.com>");
1506 MODULE_LICENSE("GPL v2");
1507