2 * Mediatek 8173 ALSA SoC AFE platform driver
4 * Copyright (c) 2015 MediaTek Inc.
5 * Author: Koro Chen <koro.chen@mediatek.com>
6 * Sascha Hauer <s.hauer@pengutronix.de>
7 * Hidalgo Huang <hidalgo.huang@mediatek.com>
8 * Ir Lian <ir.lian@mediatek.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 and
12 * only version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/delay.h>
21 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/pm_runtime.h>
26 #include <sound/soc.h>
27 #include "mt8173-afe-common.h"
28 #include "../common/mtk-base-afe.h"
29 #include "../common/mtk-afe-platform-driver.h"
30 #include "../common/mtk-afe-fe-dai.h"
32 /*****************************************************************************
33 * R E G I S T E R D E F I N I T I O N
34 *****************************************************************************/
35 #define AUDIO_TOP_CON0 0x0000
36 #define AUDIO_TOP_CON1 0x0004
37 #define AFE_DAC_CON0 0x0010
38 #define AFE_DAC_CON1 0x0014
39 #define AFE_I2S_CON1 0x0034
40 #define AFE_I2S_CON2 0x0038
41 #define AFE_CONN_24BIT 0x006c
42 #define AFE_MEMIF_MSB 0x00cc
44 #define AFE_CONN1 0x0024
45 #define AFE_CONN2 0x0028
46 #define AFE_CONN3 0x002c
47 #define AFE_CONN7 0x0460
48 #define AFE_CONN8 0x0464
49 #define AFE_HDMI_CONN0 0x0390
51 /* Memory interface */
52 #define AFE_DL1_BASE 0x0040
53 #define AFE_DL1_CUR 0x0044
54 #define AFE_DL1_END 0x0048
55 #define AFE_DL2_BASE 0x0050
56 #define AFE_DL2_CUR 0x0054
57 #define AFE_AWB_BASE 0x0070
58 #define AFE_AWB_CUR 0x007c
59 #define AFE_VUL_BASE 0x0080
60 #define AFE_VUL_CUR 0x008c
61 #define AFE_VUL_END 0x0088
62 #define AFE_DAI_BASE 0x0090
63 #define AFE_DAI_CUR 0x009c
64 #define AFE_MOD_PCM_BASE 0x0330
65 #define AFE_MOD_PCM_CUR 0x033c
66 #define AFE_HDMI_OUT_BASE 0x0374
67 #define AFE_HDMI_OUT_CUR 0x0378
68 #define AFE_HDMI_OUT_END 0x037c
70 #define AFE_ADDA_TOP_CON0 0x0120
71 #define AFE_ADDA2_TOP_CON0 0x0600
73 #define AFE_HDMI_OUT_CON0 0x0370
75 #define AFE_IRQ_MCU_CON 0x03a0
76 #define AFE_IRQ_STATUS 0x03a4
77 #define AFE_IRQ_CLR 0x03a8
78 #define AFE_IRQ_CNT1 0x03ac
79 #define AFE_IRQ_CNT2 0x03b0
80 #define AFE_IRQ_MCU_EN 0x03b4
81 #define AFE_IRQ_CNT5 0x03bc
82 #define AFE_IRQ_CNT7 0x03dc
84 #define AFE_TDM_CON1 0x0548
85 #define AFE_TDM_CON2 0x054c
87 #define AFE_IRQ_STATUS_BITS 0xff
89 /* AUDIO_TOP_CON0 (0x0000) */
90 #define AUD_TCON0_PDN_SPDF (0x1 << 21)
91 #define AUD_TCON0_PDN_HDMI (0x1 << 20)
92 #define AUD_TCON0_PDN_24M (0x1 << 9)
93 #define AUD_TCON0_PDN_22M (0x1 << 8)
94 #define AUD_TCON0_PDN_AFE (0x1 << 2)
96 /* AFE_I2S_CON1 (0x0034) */
97 #define AFE_I2S_CON1_LOW_JITTER_CLK (0x1 << 12)
98 #define AFE_I2S_CON1_RATE(x) (((x) & 0xf) << 8)
99 #define AFE_I2S_CON1_FORMAT_I2S (0x1 << 3)
100 #define AFE_I2S_CON1_EN (0x1 << 0)
102 /* AFE_I2S_CON2 (0x0038) */
103 #define AFE_I2S_CON2_LOW_JITTER_CLK (0x1 << 12)
104 #define AFE_I2S_CON2_RATE(x) (((x) & 0xf) << 8)
105 #define AFE_I2S_CON2_FORMAT_I2S (0x1 << 3)
106 #define AFE_I2S_CON2_EN (0x1 << 0)
108 /* AFE_CONN_24BIT (0x006c) */
109 #define AFE_CONN_24BIT_O04 (0x1 << 4)
110 #define AFE_CONN_24BIT_O03 (0x1 << 3)
112 /* AFE_HDMI_CONN0 (0x0390) */
113 #define AFE_HDMI_CONN0_O37_I37 (0x7 << 21)
114 #define AFE_HDMI_CONN0_O36_I36 (0x6 << 18)
115 #define AFE_HDMI_CONN0_O35_I33 (0x3 << 15)
116 #define AFE_HDMI_CONN0_O34_I32 (0x2 << 12)
117 #define AFE_HDMI_CONN0_O33_I35 (0x5 << 9)
118 #define AFE_HDMI_CONN0_O32_I34 (0x4 << 6)
119 #define AFE_HDMI_CONN0_O31_I31 (0x1 << 3)
120 #define AFE_HDMI_CONN0_O30_I30 (0x0 << 0)
122 /* AFE_TDM_CON1 (0x0548) */
123 #define AFE_TDM_CON1_LRCK_WIDTH(x) (((x) - 1) << 24)
124 #define AFE_TDM_CON1_32_BCK_CYCLES (0x2 << 12)
125 #define AFE_TDM_CON1_WLEN_32BIT (0x2 << 8)
126 #define AFE_TDM_CON1_MSB_ALIGNED (0x1 << 4)
127 #define AFE_TDM_CON1_1_BCK_DELAY (0x1 << 3)
128 #define AFE_TDM_CON1_LRCK_INV (0x1 << 2)
129 #define AFE_TDM_CON1_BCK_INV (0x1 << 1)
130 #define AFE_TDM_CON1_EN (0x1 << 0)
132 enum afe_tdm_ch_start
{
133 AFE_TDM_CH_START_O30_O31
= 0,
134 AFE_TDM_CH_START_O32_O33
,
135 AFE_TDM_CH_START_O34_O35
,
136 AFE_TDM_CH_START_O36_O37
,
140 static const unsigned int mt8173_afe_backup_list
[] = {
157 struct mt8173_afe_private
{
158 struct clk
*clocks
[MT8173_CLK_NUM
];
161 static const struct snd_pcm_hardware mt8173_afe_hardware
= {
162 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
163 SNDRV_PCM_INFO_MMAP_VALID
),
164 .buffer_bytes_max
= 256 * 1024,
165 .period_bytes_min
= 512,
166 .period_bytes_max
= 128 * 1024,
172 struct mt8173_afe_rate
{
174 unsigned int regvalue
;
177 static const struct mt8173_afe_rate mt8173_afe_i2s_rates
[] = {
178 { .rate
= 8000, .regvalue
= 0 },
179 { .rate
= 11025, .regvalue
= 1 },
180 { .rate
= 12000, .regvalue
= 2 },
181 { .rate
= 16000, .regvalue
= 4 },
182 { .rate
= 22050, .regvalue
= 5 },
183 { .rate
= 24000, .regvalue
= 6 },
184 { .rate
= 32000, .regvalue
= 8 },
185 { .rate
= 44100, .regvalue
= 9 },
186 { .rate
= 48000, .regvalue
= 10 },
187 { .rate
= 88000, .regvalue
= 11 },
188 { .rate
= 96000, .regvalue
= 12 },
189 { .rate
= 174000, .regvalue
= 13 },
190 { .rate
= 192000, .regvalue
= 14 },
193 static int mt8173_afe_i2s_fs(unsigned int sample_rate
)
197 for (i
= 0; i
< ARRAY_SIZE(mt8173_afe_i2s_rates
); i
++)
198 if (mt8173_afe_i2s_rates
[i
].rate
== sample_rate
)
199 return mt8173_afe_i2s_rates
[i
].regvalue
;
204 static int mt8173_afe_set_i2s(struct mtk_base_afe
*afe
, unsigned int rate
)
207 int fs
= mt8173_afe_i2s_fs(rate
);
212 /* from external ADC */
213 regmap_update_bits(afe
->regmap
, AFE_ADDA_TOP_CON0
, 0x1, 0x1);
214 regmap_update_bits(afe
->regmap
, AFE_ADDA2_TOP_CON0
, 0x1, 0x1);
217 val
= AFE_I2S_CON2_LOW_JITTER_CLK
|
218 AFE_I2S_CON2_RATE(fs
) |
219 AFE_I2S_CON2_FORMAT_I2S
;
221 regmap_update_bits(afe
->regmap
, AFE_I2S_CON2
, ~AFE_I2S_CON2_EN
, val
);
224 val
= AFE_I2S_CON1_LOW_JITTER_CLK
|
225 AFE_I2S_CON1_RATE(fs
) |
226 AFE_I2S_CON1_FORMAT_I2S
;
228 regmap_update_bits(afe
->regmap
, AFE_I2S_CON1
, ~AFE_I2S_CON1_EN
, val
);
232 static void mt8173_afe_set_i2s_enable(struct mtk_base_afe
*afe
, bool enable
)
236 regmap_read(afe
->regmap
, AFE_I2S_CON2
, &val
);
237 if (!!(val
& AFE_I2S_CON2_EN
) == enable
)
241 regmap_update_bits(afe
->regmap
, AFE_I2S_CON2
, 0x1, enable
);
244 regmap_update_bits(afe
->regmap
, AFE_I2S_CON1
, 0x1, enable
);
247 static int mt8173_afe_dais_enable_clks(struct mtk_base_afe
*afe
,
248 struct clk
*m_ck
, struct clk
*b_ck
)
253 ret
= clk_prepare_enable(m_ck
);
255 dev_err(afe
->dev
, "Failed to enable m_ck\n");
261 ret
= clk_prepare_enable(b_ck
);
263 dev_err(afe
->dev
, "Failed to enable b_ck\n");
270 static int mt8173_afe_dais_set_clks(struct mtk_base_afe
*afe
,
271 struct clk
*m_ck
, unsigned int mck_rate
,
272 struct clk
*b_ck
, unsigned int bck_rate
)
277 ret
= clk_set_rate(m_ck
, mck_rate
);
279 dev_err(afe
->dev
, "Failed to set m_ck rate\n");
285 ret
= clk_set_rate(b_ck
, bck_rate
);
287 dev_err(afe
->dev
, "Failed to set b_ck rate\n");
294 static void mt8173_afe_dais_disable_clks(struct mtk_base_afe
*afe
,
295 struct clk
*m_ck
, struct clk
*b_ck
)
298 clk_disable_unprepare(m_ck
);
300 clk_disable_unprepare(b_ck
);
303 static int mt8173_afe_i2s_startup(struct snd_pcm_substream
*substream
,
304 struct snd_soc_dai
*dai
)
306 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
307 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
312 regmap_update_bits(afe
->regmap
, AUDIO_TOP_CON0
,
313 AUD_TCON0_PDN_22M
| AUD_TCON0_PDN_24M
, 0);
317 static void mt8173_afe_i2s_shutdown(struct snd_pcm_substream
*substream
,
318 struct snd_soc_dai
*dai
)
320 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
321 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
326 mt8173_afe_set_i2s_enable(afe
, false);
327 regmap_update_bits(afe
->regmap
, AUDIO_TOP_CON0
,
328 AUD_TCON0_PDN_22M
| AUD_TCON0_PDN_24M
,
329 AUD_TCON0_PDN_22M
| AUD_TCON0_PDN_24M
);
332 static int mt8173_afe_i2s_prepare(struct snd_pcm_substream
*substream
,
333 struct snd_soc_dai
*dai
)
335 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
336 struct snd_pcm_runtime
* const runtime
= substream
->runtime
;
337 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
338 struct mt8173_afe_private
*afe_priv
= afe
->platform_priv
;
341 mt8173_afe_dais_set_clks(afe
, afe_priv
->clocks
[MT8173_CLK_I2S1_M
],
342 runtime
->rate
* 256, NULL
, 0);
343 mt8173_afe_dais_set_clks(afe
, afe_priv
->clocks
[MT8173_CLK_I2S2_M
],
344 runtime
->rate
* 256, NULL
, 0);
346 ret
= mt8173_afe_set_i2s(afe
, substream
->runtime
->rate
);
350 mt8173_afe_set_i2s_enable(afe
, true);
355 static int mt8173_afe_hdmi_startup(struct snd_pcm_substream
*substream
,
356 struct snd_soc_dai
*dai
)
358 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
359 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
360 struct mt8173_afe_private
*afe_priv
= afe
->platform_priv
;
365 mt8173_afe_dais_enable_clks(afe
, afe_priv
->clocks
[MT8173_CLK_I2S3_M
],
366 afe_priv
->clocks
[MT8173_CLK_I2S3_B
]);
370 static void mt8173_afe_hdmi_shutdown(struct snd_pcm_substream
*substream
,
371 struct snd_soc_dai
*dai
)
373 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
374 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
375 struct mt8173_afe_private
*afe_priv
= afe
->platform_priv
;
380 mt8173_afe_dais_disable_clks(afe
, afe_priv
->clocks
[MT8173_CLK_I2S3_M
],
381 afe_priv
->clocks
[MT8173_CLK_I2S3_B
]);
384 static int mt8173_afe_hdmi_prepare(struct snd_pcm_substream
*substream
,
385 struct snd_soc_dai
*dai
)
387 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
388 struct snd_pcm_runtime
* const runtime
= substream
->runtime
;
389 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
390 struct mt8173_afe_private
*afe_priv
= afe
->platform_priv
;
394 mt8173_afe_dais_set_clks(afe
, afe_priv
->clocks
[MT8173_CLK_I2S3_M
],
396 afe_priv
->clocks
[MT8173_CLK_I2S3_B
],
397 runtime
->rate
* runtime
->channels
* 32);
399 val
= AFE_TDM_CON1_BCK_INV
|
400 AFE_TDM_CON1_LRCK_INV
|
401 AFE_TDM_CON1_1_BCK_DELAY
|
402 AFE_TDM_CON1_MSB_ALIGNED
| /* I2S mode */
403 AFE_TDM_CON1_WLEN_32BIT
|
404 AFE_TDM_CON1_32_BCK_CYCLES
|
405 AFE_TDM_CON1_LRCK_WIDTH(32);
406 regmap_update_bits(afe
->regmap
, AFE_TDM_CON1
, ~AFE_TDM_CON1_EN
, val
);
408 /* set tdm2 config */
409 switch (runtime
->channels
) {
412 val
= AFE_TDM_CH_START_O30_O31
;
413 val
|= (AFE_TDM_CH_ZERO
<< 4);
414 val
|= (AFE_TDM_CH_ZERO
<< 8);
415 val
|= (AFE_TDM_CH_ZERO
<< 12);
419 val
= AFE_TDM_CH_START_O30_O31
;
420 val
|= (AFE_TDM_CH_START_O32_O33
<< 4);
421 val
|= (AFE_TDM_CH_ZERO
<< 8);
422 val
|= (AFE_TDM_CH_ZERO
<< 12);
426 val
= AFE_TDM_CH_START_O30_O31
;
427 val
|= (AFE_TDM_CH_START_O32_O33
<< 4);
428 val
|= (AFE_TDM_CH_START_O34_O35
<< 8);
429 val
|= (AFE_TDM_CH_ZERO
<< 12);
433 val
= AFE_TDM_CH_START_O30_O31
;
434 val
|= (AFE_TDM_CH_START_O32_O33
<< 4);
435 val
|= (AFE_TDM_CH_START_O34_O35
<< 8);
436 val
|= (AFE_TDM_CH_START_O36_O37
<< 12);
441 regmap_update_bits(afe
->regmap
, AFE_TDM_CON2
, 0x0000ffff, val
);
443 regmap_update_bits(afe
->regmap
, AFE_HDMI_OUT_CON0
,
444 0x000000f0, runtime
->channels
<< 4);
448 static int mt8173_afe_hdmi_trigger(struct snd_pcm_substream
*substream
, int cmd
,
449 struct snd_soc_dai
*dai
)
451 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
452 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
454 dev_info(afe
->dev
, "%s cmd=%d %s\n", __func__
, cmd
, dai
->name
);
457 case SNDRV_PCM_TRIGGER_START
:
458 case SNDRV_PCM_TRIGGER_RESUME
:
459 regmap_update_bits(afe
->regmap
, AUDIO_TOP_CON0
,
460 AUD_TCON0_PDN_HDMI
| AUD_TCON0_PDN_SPDF
, 0);
462 /* set connections: O30~O37: L/R/LS/RS/C/LFE/CH7/CH8 */
463 regmap_write(afe
->regmap
, AFE_HDMI_CONN0
,
464 AFE_HDMI_CONN0_O30_I30
|
465 AFE_HDMI_CONN0_O31_I31
|
466 AFE_HDMI_CONN0_O32_I34
|
467 AFE_HDMI_CONN0_O33_I35
|
468 AFE_HDMI_CONN0_O34_I32
|
469 AFE_HDMI_CONN0_O35_I33
|
470 AFE_HDMI_CONN0_O36_I36
|
471 AFE_HDMI_CONN0_O37_I37
);
473 /* enable Out control */
474 regmap_update_bits(afe
->regmap
, AFE_HDMI_OUT_CON0
, 0x1, 0x1);
477 regmap_update_bits(afe
->regmap
, AFE_TDM_CON1
, 0x1, 0x1);
480 case SNDRV_PCM_TRIGGER_STOP
:
481 case SNDRV_PCM_TRIGGER_SUSPEND
:
483 regmap_update_bits(afe
->regmap
, AFE_TDM_CON1
, 0x1, 0);
485 /* disable Out control */
486 regmap_update_bits(afe
->regmap
, AFE_HDMI_OUT_CON0
, 0x1, 0);
488 regmap_update_bits(afe
->regmap
, AUDIO_TOP_CON0
,
489 AUD_TCON0_PDN_HDMI
| AUD_TCON0_PDN_SPDF
,
490 AUD_TCON0_PDN_HDMI
| AUD_TCON0_PDN_SPDF
);
497 static int mt8173_memif_fs(struct snd_pcm_substream
*substream
,
500 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
501 struct mtk_base_afe
*afe
= snd_soc_platform_get_drvdata(rtd
->platform
);
502 struct mtk_base_afe_memif
*memif
= &afe
->memif
[rtd
->cpu_dai
->id
];
505 if (memif
->data
->id
== MT8173_AFE_MEMIF_DAI
||
506 memif
->data
->id
== MT8173_AFE_MEMIF_MOD_DAI
) {
521 fs
= mt8173_afe_i2s_fs(rate
);
526 static int mt8173_irq_fs(struct snd_pcm_substream
*substream
, unsigned int rate
)
528 return mt8173_afe_i2s_fs(rate
);
532 static const struct snd_soc_dai_ops mt8173_afe_i2s_ops
= {
533 .startup
= mt8173_afe_i2s_startup
,
534 .shutdown
= mt8173_afe_i2s_shutdown
,
535 .prepare
= mt8173_afe_i2s_prepare
,
538 static const struct snd_soc_dai_ops mt8173_afe_hdmi_ops
= {
539 .startup
= mt8173_afe_hdmi_startup
,
540 .shutdown
= mt8173_afe_hdmi_shutdown
,
541 .prepare
= mt8173_afe_hdmi_prepare
,
542 .trigger
= mt8173_afe_hdmi_trigger
,
545 static struct snd_soc_dai_driver mt8173_afe_pcm_dais
[] = {
546 /* FE DAIs: memory intefaces to CPU */
548 .name
= "DL1", /* downlink 1 */
549 .id
= MT8173_AFE_MEMIF_DL1
,
550 .suspend
= mtk_afe_dai_suspend
,
551 .resume
= mtk_afe_dai_resume
,
553 .stream_name
= "DL1",
556 .rates
= SNDRV_PCM_RATE_8000_48000
,
557 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
559 .ops
= &mtk_afe_fe_ops
,
561 .name
= "VUL", /* voice uplink */
562 .id
= MT8173_AFE_MEMIF_VUL
,
563 .suspend
= mtk_afe_dai_suspend
,
564 .resume
= mtk_afe_dai_resume
,
566 .stream_name
= "VUL",
569 .rates
= SNDRV_PCM_RATE_8000_48000
,
570 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
572 .ops
= &mtk_afe_fe_ops
,
576 .id
= MT8173_AFE_IO_I2S
,
578 .stream_name
= "I2S Playback",
581 .rates
= SNDRV_PCM_RATE_8000_48000
,
582 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
585 .stream_name
= "I2S Capture",
588 .rates
= SNDRV_PCM_RATE_8000_48000
,
589 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
591 .ops
= &mt8173_afe_i2s_ops
,
592 .symmetric_rates
= 1,
596 static struct snd_soc_dai_driver mt8173_afe_hdmi_dais
[] = {
600 .id
= MT8173_AFE_MEMIF_HDMI
,
601 .suspend
= mtk_afe_dai_suspend
,
602 .resume
= mtk_afe_dai_resume
,
604 .stream_name
= "HDMI",
607 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
|
608 SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_88200
|
609 SNDRV_PCM_RATE_96000
| SNDRV_PCM_RATE_176400
|
610 SNDRV_PCM_RATE_192000
,
611 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
613 .ops
= &mtk_afe_fe_ops
,
617 .id
= MT8173_AFE_IO_HDMI
,
619 .stream_name
= "HDMIO Playback",
622 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
|
623 SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_88200
|
624 SNDRV_PCM_RATE_96000
| SNDRV_PCM_RATE_176400
|
625 SNDRV_PCM_RATE_192000
,
626 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
628 .ops
= &mt8173_afe_hdmi_ops
,
632 static const struct snd_kcontrol_new mt8173_afe_o03_mix
[] = {
633 SOC_DAPM_SINGLE_AUTODISABLE("I05 Switch", AFE_CONN1
, 21, 1, 0),
636 static const struct snd_kcontrol_new mt8173_afe_o04_mix
[] = {
637 SOC_DAPM_SINGLE_AUTODISABLE("I06 Switch", AFE_CONN2
, 6, 1, 0),
640 static const struct snd_kcontrol_new mt8173_afe_o09_mix
[] = {
641 SOC_DAPM_SINGLE_AUTODISABLE("I03 Switch", AFE_CONN3
, 0, 1, 0),
642 SOC_DAPM_SINGLE_AUTODISABLE("I17 Switch", AFE_CONN7
, 30, 1, 0),
645 static const struct snd_kcontrol_new mt8173_afe_o10_mix
[] = {
646 SOC_DAPM_SINGLE_AUTODISABLE("I04 Switch", AFE_CONN3
, 3, 1, 0),
647 SOC_DAPM_SINGLE_AUTODISABLE("I18 Switch", AFE_CONN8
, 0, 1, 0),
650 static const struct snd_soc_dapm_widget mt8173_afe_pcm_widgets
[] = {
651 /* inter-connections */
652 SND_SOC_DAPM_MIXER("I03", SND_SOC_NOPM
, 0, 0, NULL
, 0),
653 SND_SOC_DAPM_MIXER("I04", SND_SOC_NOPM
, 0, 0, NULL
, 0),
654 SND_SOC_DAPM_MIXER("I05", SND_SOC_NOPM
, 0, 0, NULL
, 0),
655 SND_SOC_DAPM_MIXER("I06", SND_SOC_NOPM
, 0, 0, NULL
, 0),
656 SND_SOC_DAPM_MIXER("I17", SND_SOC_NOPM
, 0, 0, NULL
, 0),
657 SND_SOC_DAPM_MIXER("I18", SND_SOC_NOPM
, 0, 0, NULL
, 0),
659 SND_SOC_DAPM_MIXER("O03", SND_SOC_NOPM
, 0, 0,
660 mt8173_afe_o03_mix
, ARRAY_SIZE(mt8173_afe_o03_mix
)),
661 SND_SOC_DAPM_MIXER("O04", SND_SOC_NOPM
, 0, 0,
662 mt8173_afe_o04_mix
, ARRAY_SIZE(mt8173_afe_o04_mix
)),
663 SND_SOC_DAPM_MIXER("O09", SND_SOC_NOPM
, 0, 0,
664 mt8173_afe_o09_mix
, ARRAY_SIZE(mt8173_afe_o09_mix
)),
665 SND_SOC_DAPM_MIXER("O10", SND_SOC_NOPM
, 0, 0,
666 mt8173_afe_o10_mix
, ARRAY_SIZE(mt8173_afe_o10_mix
)),
669 static const struct snd_soc_dapm_route mt8173_afe_pcm_routes
[] = {
670 {"I05", NULL
, "DL1"},
671 {"I06", NULL
, "DL1"},
672 {"I2S Playback", NULL
, "O03"},
673 {"I2S Playback", NULL
, "O04"},
674 {"VUL", NULL
, "O09"},
675 {"VUL", NULL
, "O10"},
676 {"I03", NULL
, "I2S Capture"},
677 {"I04", NULL
, "I2S Capture"},
678 {"I17", NULL
, "I2S Capture"},
679 {"I18", NULL
, "I2S Capture"},
680 { "O03", "I05 Switch", "I05" },
681 { "O04", "I06 Switch", "I06" },
682 { "O09", "I17 Switch", "I17" },
683 { "O09", "I03 Switch", "I03" },
684 { "O10", "I18 Switch", "I18" },
685 { "O10", "I04 Switch", "I04" },
688 static const struct snd_soc_dapm_route mt8173_afe_hdmi_routes
[] = {
689 {"HDMIO Playback", NULL
, "HDMI"},
692 static const struct snd_soc_component_driver mt8173_afe_pcm_dai_component
= {
693 .name
= "mt8173-afe-pcm-dai",
694 .dapm_widgets
= mt8173_afe_pcm_widgets
,
695 .num_dapm_widgets
= ARRAY_SIZE(mt8173_afe_pcm_widgets
),
696 .dapm_routes
= mt8173_afe_pcm_routes
,
697 .num_dapm_routes
= ARRAY_SIZE(mt8173_afe_pcm_routes
),
700 static const struct snd_soc_component_driver mt8173_afe_hdmi_dai_component
= {
701 .name
= "mt8173-afe-hdmi-dai",
702 .dapm_routes
= mt8173_afe_hdmi_routes
,
703 .num_dapm_routes
= ARRAY_SIZE(mt8173_afe_hdmi_routes
),
706 static const char *aud_clks
[MT8173_CLK_NUM
] = {
707 [MT8173_CLK_INFRASYS_AUD
] = "infra_sys_audio_clk",
708 [MT8173_CLK_TOP_PDN_AUD
] = "top_pdn_audio",
709 [MT8173_CLK_TOP_PDN_AUD_BUS
] = "top_pdn_aud_intbus",
710 [MT8173_CLK_I2S0_M
] = "i2s0_m",
711 [MT8173_CLK_I2S1_M
] = "i2s1_m",
712 [MT8173_CLK_I2S2_M
] = "i2s2_m",
713 [MT8173_CLK_I2S3_M
] = "i2s3_m",
714 [MT8173_CLK_I2S3_B
] = "i2s3_b",
715 [MT8173_CLK_BCK0
] = "bck0",
716 [MT8173_CLK_BCK1
] = "bck1",
719 static const struct mtk_base_memif_data memif_data
[MT8173_AFE_MEMIF_NUM
] = {
722 .id
= MT8173_AFE_MEMIF_DL1
,
723 .reg_ofs_base
= AFE_DL1_BASE
,
724 .reg_ofs_cur
= AFE_DL1_CUR
,
725 .fs_reg
= AFE_DAC_CON1
,
728 .mono_reg
= AFE_DAC_CON1
,
732 .enable_reg
= AFE_DAC_CON0
,
734 .msb_reg
= AFE_MEMIF_MSB
,
736 .agent_disable_reg
= -1,
737 .agent_disable_shift
= -1,
740 .id
= MT8173_AFE_MEMIF_DL2
,
741 .reg_ofs_base
= AFE_DL2_BASE
,
742 .reg_ofs_cur
= AFE_DL2_CUR
,
743 .fs_reg
= AFE_DAC_CON1
,
746 .mono_reg
= AFE_DAC_CON1
,
750 .enable_reg
= AFE_DAC_CON0
,
752 .msb_reg
= AFE_MEMIF_MSB
,
754 .agent_disable_reg
= -1,
755 .agent_disable_shift
= -1,
758 .id
= MT8173_AFE_MEMIF_VUL
,
759 .reg_ofs_base
= AFE_VUL_BASE
,
760 .reg_ofs_cur
= AFE_VUL_CUR
,
761 .fs_reg
= AFE_DAC_CON1
,
764 .mono_reg
= AFE_DAC_CON1
,
768 .enable_reg
= AFE_DAC_CON0
,
770 .msb_reg
= AFE_MEMIF_MSB
,
772 .agent_disable_reg
= -1,
773 .agent_disable_shift
= -1,
776 .id
= MT8173_AFE_MEMIF_DAI
,
777 .reg_ofs_base
= AFE_DAI_BASE
,
778 .reg_ofs_cur
= AFE_DAI_CUR
,
779 .fs_reg
= AFE_DAC_CON0
,
786 .enable_reg
= AFE_DAC_CON0
,
788 .msb_reg
= AFE_MEMIF_MSB
,
790 .agent_disable_reg
= -1,
791 .agent_disable_shift
= -1,
794 .id
= MT8173_AFE_MEMIF_AWB
,
795 .reg_ofs_base
= AFE_AWB_BASE
,
796 .reg_ofs_cur
= AFE_AWB_CUR
,
797 .fs_reg
= AFE_DAC_CON1
,
800 .mono_reg
= AFE_DAC_CON1
,
804 .enable_reg
= AFE_DAC_CON0
,
806 .msb_reg
= AFE_MEMIF_MSB
,
808 .agent_disable_reg
= -1,
809 .agent_disable_shift
= -1,
812 .id
= MT8173_AFE_MEMIF_MOD_DAI
,
813 .reg_ofs_base
= AFE_MOD_PCM_BASE
,
814 .reg_ofs_cur
= AFE_MOD_PCM_CUR
,
815 .fs_reg
= AFE_DAC_CON1
,
818 .mono_reg
= AFE_DAC_CON1
,
822 .enable_reg
= AFE_DAC_CON0
,
824 .msb_reg
= AFE_MEMIF_MSB
,
826 .agent_disable_reg
= -1,
827 .agent_disable_shift
= -1,
830 .id
= MT8173_AFE_MEMIF_HDMI
,
831 .reg_ofs_base
= AFE_HDMI_OUT_BASE
,
832 .reg_ofs_cur
= AFE_HDMI_OUT_CUR
,
842 .msb_reg
= AFE_MEMIF_MSB
,
844 .agent_disable_reg
= -1,
845 .agent_disable_shift
= -1,
849 static const struct mtk_base_irq_data irq_data
[MT8173_AFE_IRQ_NUM
] = {
851 .id
= MT8173_AFE_IRQ_DL1
,
852 .irq_cnt_reg
= AFE_IRQ_CNT1
,
854 .irq_cnt_maskbit
= 0x3ffff,
855 .irq_en_reg
= AFE_IRQ_MCU_CON
,
857 .irq_fs_reg
= AFE_IRQ_MCU_CON
,
859 .irq_fs_maskbit
= 0xf,
860 .irq_clr_reg
= AFE_IRQ_CLR
,
863 .id
= MT8173_AFE_IRQ_DL2
,
864 .irq_cnt_reg
= AFE_IRQ_CNT1
,
866 .irq_cnt_maskbit
= 0x3ffff,
867 .irq_en_reg
= AFE_IRQ_MCU_CON
,
869 .irq_fs_reg
= AFE_IRQ_MCU_CON
,
871 .irq_fs_maskbit
= 0xf,
872 .irq_clr_reg
= AFE_IRQ_CLR
,
876 .id
= MT8173_AFE_IRQ_VUL
,
877 .irq_cnt_reg
= AFE_IRQ_CNT2
,
879 .irq_cnt_maskbit
= 0x3ffff,
880 .irq_en_reg
= AFE_IRQ_MCU_CON
,
882 .irq_fs_reg
= AFE_IRQ_MCU_CON
,
884 .irq_fs_maskbit
= 0xf,
885 .irq_clr_reg
= AFE_IRQ_CLR
,
888 .id
= MT8173_AFE_IRQ_DAI
,
889 .irq_cnt_reg
= AFE_IRQ_CNT2
,
891 .irq_cnt_maskbit
= 0x3ffff,
892 .irq_en_reg
= AFE_IRQ_MCU_CON
,
894 .irq_fs_reg
= AFE_IRQ_MCU_CON
,
896 .irq_fs_maskbit
= 0xf,
897 .irq_clr_reg
= AFE_IRQ_CLR
,
900 .id
= MT8173_AFE_IRQ_AWB
,
901 .irq_cnt_reg
= AFE_IRQ_CNT7
,
903 .irq_cnt_maskbit
= 0x3ffff,
904 .irq_en_reg
= AFE_IRQ_MCU_CON
,
906 .irq_fs_reg
= AFE_IRQ_MCU_CON
,
908 .irq_fs_maskbit
= 0xf,
909 .irq_clr_reg
= AFE_IRQ_CLR
,
912 .id
= MT8173_AFE_IRQ_DAI
,
913 .irq_cnt_reg
= AFE_IRQ_CNT2
,
915 .irq_cnt_maskbit
= 0x3ffff,
916 .irq_en_reg
= AFE_IRQ_MCU_CON
,
918 .irq_fs_reg
= AFE_IRQ_MCU_CON
,
920 .irq_fs_maskbit
= 0xf,
921 .irq_clr_reg
= AFE_IRQ_CLR
,
924 .id
= MT8173_AFE_IRQ_HDMI
,
925 .irq_cnt_reg
= AFE_IRQ_CNT5
,
927 .irq_cnt_maskbit
= 0x3ffff,
928 .irq_en_reg
= AFE_IRQ_MCU_CON
,
932 .irq_fs_maskbit
= -1,
933 .irq_clr_reg
= AFE_IRQ_CLR
,
938 static const struct regmap_config mt8173_afe_regmap_config
= {
942 .max_register
= AFE_ADDA2_TOP_CON0
,
943 .cache_type
= REGCACHE_NONE
,
946 static irqreturn_t
mt8173_afe_irq_handler(int irq
, void *dev_id
)
948 struct mtk_base_afe
*afe
= dev_id
;
949 unsigned int reg_value
;
952 ret
= regmap_read(afe
->regmap
, AFE_IRQ_STATUS
, ®_value
);
954 dev_err(afe
->dev
, "%s irq status err\n", __func__
);
955 reg_value
= AFE_IRQ_STATUS_BITS
;
959 for (i
= 0; i
< MT8173_AFE_MEMIF_NUM
; i
++) {
960 struct mtk_base_afe_memif
*memif
= &afe
->memif
[i
];
961 struct mtk_base_afe_irq
*irq
;
963 if (memif
->irq_usage
< 0)
966 irq
= &afe
->irqs
[memif
->irq_usage
];
968 if (!(reg_value
& (1 << irq
->irq_data
->irq_clr_shift
)))
971 snd_pcm_period_elapsed(memif
->substream
);
976 regmap_write(afe
->regmap
, AFE_IRQ_CLR
,
977 reg_value
& AFE_IRQ_STATUS_BITS
);
982 static int mt8173_afe_runtime_suspend(struct device
*dev
)
984 struct mtk_base_afe
*afe
= dev_get_drvdata(dev
);
985 struct mt8173_afe_private
*afe_priv
= afe
->platform_priv
;
988 regmap_update_bits(afe
->regmap
, AFE_DAC_CON0
, 0x1, 0);
990 /* disable AFE clk */
991 regmap_update_bits(afe
->regmap
, AUDIO_TOP_CON0
,
992 AUD_TCON0_PDN_AFE
, AUD_TCON0_PDN_AFE
);
994 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_I2S1_M
]);
995 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_I2S2_M
]);
996 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_BCK0
]);
997 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_BCK1
]);
998 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_TOP_PDN_AUD
]);
999 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_TOP_PDN_AUD_BUS
]);
1000 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_INFRASYS_AUD
]);
1004 static int mt8173_afe_runtime_resume(struct device
*dev
)
1006 struct mtk_base_afe
*afe
= dev_get_drvdata(dev
);
1007 struct mt8173_afe_private
*afe_priv
= afe
->platform_priv
;
1010 ret
= clk_prepare_enable(afe_priv
->clocks
[MT8173_CLK_INFRASYS_AUD
]);
1014 ret
= clk_prepare_enable(afe_priv
->clocks
[MT8173_CLK_TOP_PDN_AUD_BUS
]);
1018 ret
= clk_prepare_enable(afe_priv
->clocks
[MT8173_CLK_TOP_PDN_AUD
]);
1020 goto err_top_aud_bus
;
1022 ret
= clk_prepare_enable(afe_priv
->clocks
[MT8173_CLK_BCK0
]);
1026 ret
= clk_prepare_enable(afe_priv
->clocks
[MT8173_CLK_BCK1
]);
1029 ret
= clk_prepare_enable(afe_priv
->clocks
[MT8173_CLK_I2S1_M
]);
1032 ret
= clk_prepare_enable(afe_priv
->clocks
[MT8173_CLK_I2S2_M
]);
1036 /* enable AFE clk */
1037 regmap_update_bits(afe
->regmap
, AUDIO_TOP_CON0
, AUD_TCON0_PDN_AFE
, 0);
1039 /* set O3/O4 16bits */
1040 regmap_update_bits(afe
->regmap
, AFE_CONN_24BIT
,
1041 AFE_CONN_24BIT_O03
| AFE_CONN_24BIT_O04
, 0);
1043 /* unmask all IRQs */
1044 regmap_update_bits(afe
->regmap
, AFE_IRQ_MCU_EN
, 0xff, 0xff);
1047 regmap_update_bits(afe
->regmap
, AFE_DAC_CON0
, 0x1, 0x1);
1051 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_I2S1_M
]);
1053 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_I2S2_M
]);
1055 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_BCK0
]);
1057 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_TOP_PDN_AUD
]);
1059 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_TOP_PDN_AUD_BUS
]);
1061 clk_disable_unprepare(afe_priv
->clocks
[MT8173_CLK_INFRASYS_AUD
]);
1065 static int mt8173_afe_init_audio_clk(struct mtk_base_afe
*afe
)
1068 struct mt8173_afe_private
*afe_priv
= afe
->platform_priv
;
1070 for (i
= 0; i
< ARRAY_SIZE(aud_clks
); i
++) {
1071 afe_priv
->clocks
[i
] = devm_clk_get(afe
->dev
, aud_clks
[i
]);
1072 if (IS_ERR(afe_priv
->clocks
[i
])) {
1073 dev_err(afe
->dev
, "%s devm_clk_get %s fail\n",
1074 __func__
, aud_clks
[i
]);
1075 return PTR_ERR(afe_priv
->clocks
[i
]);
1078 clk_set_rate(afe_priv
->clocks
[MT8173_CLK_BCK0
], 22579200); /* 22M */
1079 clk_set_rate(afe_priv
->clocks
[MT8173_CLK_BCK1
], 24576000); /* 24M */
1083 static int mt8173_afe_pcm_dev_probe(struct platform_device
*pdev
)
1086 unsigned int irq_id
;
1087 struct mtk_base_afe
*afe
;
1088 struct mt8173_afe_private
*afe_priv
;
1089 struct resource
*res
;
1091 ret
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(33));
1095 afe
= devm_kzalloc(&pdev
->dev
, sizeof(*afe
), GFP_KERNEL
);
1099 afe
->platform_priv
= devm_kzalloc(&pdev
->dev
, sizeof(*afe_priv
),
1101 afe_priv
= afe
->platform_priv
;
1105 afe
->dev
= &pdev
->dev
;
1107 irq_id
= platform_get_irq(pdev
, 0);
1109 dev_err(afe
->dev
, "np %s no irq\n", afe
->dev
->of_node
->name
);
1112 ret
= devm_request_irq(afe
->dev
, irq_id
, mt8173_afe_irq_handler
,
1113 0, "Afe_ISR_Handle", (void *)afe
);
1115 dev_err(afe
->dev
, "could not request_irq\n");
1119 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1120 afe
->base_addr
= devm_ioremap_resource(&pdev
->dev
, res
);
1121 if (IS_ERR(afe
->base_addr
))
1122 return PTR_ERR(afe
->base_addr
);
1124 afe
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, afe
->base_addr
,
1125 &mt8173_afe_regmap_config
);
1126 if (IS_ERR(afe
->regmap
))
1127 return PTR_ERR(afe
->regmap
);
1129 /* initial audio related clock */
1130 ret
= mt8173_afe_init_audio_clk(afe
);
1132 dev_err(afe
->dev
, "mt8173_afe_init_audio_clk fail\n");
1136 /* memif % irq initialize*/
1137 afe
->memif_size
= MT8173_AFE_MEMIF_NUM
;
1138 afe
->memif
= devm_kcalloc(afe
->dev
, afe
->memif_size
,
1139 sizeof(*afe
->memif
), GFP_KERNEL
);
1143 afe
->irqs_size
= MT8173_AFE_IRQ_NUM
;
1144 afe
->irqs
= devm_kcalloc(afe
->dev
, afe
->irqs_size
,
1145 sizeof(*afe
->irqs
), GFP_KERNEL
);
1149 for (i
= 0; i
< afe
->irqs_size
; i
++) {
1150 afe
->memif
[i
].data
= &memif_data
[i
];
1151 afe
->irqs
[i
].irq_data
= &irq_data
[i
];
1152 afe
->irqs
[i
].irq_occupyed
= true;
1153 afe
->memif
[i
].irq_usage
= i
;
1154 afe
->memif
[i
].const_irq
= 1;
1157 afe
->mtk_afe_hardware
= &mt8173_afe_hardware
;
1158 afe
->memif_fs
= mt8173_memif_fs
;
1159 afe
->irq_fs
= mt8173_irq_fs
;
1161 platform_set_drvdata(pdev
, afe
);
1163 pm_runtime_enable(&pdev
->dev
);
1164 if (!pm_runtime_enabled(&pdev
->dev
)) {
1165 ret
= mt8173_afe_runtime_resume(&pdev
->dev
);
1167 goto err_pm_disable
;
1170 afe
->reg_back_up_list
= mt8173_afe_backup_list
;
1171 afe
->reg_back_up_list_num
= ARRAY_SIZE(mt8173_afe_backup_list
);
1172 afe
->runtime_resume
= mt8173_afe_runtime_resume
;
1173 afe
->runtime_suspend
= mt8173_afe_runtime_suspend
;
1175 ret
= snd_soc_register_platform(&pdev
->dev
, &mtk_afe_pcm_platform
);
1177 goto err_pm_disable
;
1179 ret
= snd_soc_register_component(&pdev
->dev
,
1180 &mt8173_afe_pcm_dai_component
,
1181 mt8173_afe_pcm_dais
,
1182 ARRAY_SIZE(mt8173_afe_pcm_dais
));
1186 ret
= snd_soc_register_component(&pdev
->dev
,
1187 &mt8173_afe_hdmi_dai_component
,
1188 mt8173_afe_hdmi_dais
,
1189 ARRAY_SIZE(mt8173_afe_hdmi_dais
));
1193 dev_info(&pdev
->dev
, "MT8173 AFE driver initialized.\n");
1197 snd_soc_unregister_component(&pdev
->dev
);
1199 snd_soc_unregister_platform(&pdev
->dev
);
1201 pm_runtime_disable(&pdev
->dev
);
1205 static int mt8173_afe_pcm_dev_remove(struct platform_device
*pdev
)
1207 pm_runtime_disable(&pdev
->dev
);
1208 if (!pm_runtime_status_suspended(&pdev
->dev
))
1209 mt8173_afe_runtime_suspend(&pdev
->dev
);
1210 snd_soc_unregister_component(&pdev
->dev
);
1211 snd_soc_unregister_platform(&pdev
->dev
);
1215 static const struct of_device_id mt8173_afe_pcm_dt_match
[] = {
1216 { .compatible
= "mediatek,mt8173-afe-pcm", },
1219 MODULE_DEVICE_TABLE(of
, mt8173_afe_pcm_dt_match
);
1221 static const struct dev_pm_ops mt8173_afe_pm_ops
= {
1222 SET_RUNTIME_PM_OPS(mt8173_afe_runtime_suspend
,
1223 mt8173_afe_runtime_resume
, NULL
)
1226 static struct platform_driver mt8173_afe_pcm_driver
= {
1228 .name
= "mt8173-afe-pcm",
1229 .of_match_table
= mt8173_afe_pcm_dt_match
,
1230 .pm
= &mt8173_afe_pm_ops
,
1232 .probe
= mt8173_afe_pcm_dev_probe
,
1233 .remove
= mt8173_afe_pcm_dev_remove
,
1236 module_platform_driver(mt8173_afe_pcm_driver
);
1238 MODULE_DESCRIPTION("Mediatek ALSA SoC AFE platform driver");
1239 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
1240 MODULE_LICENSE("GPL v2");