1 From 948a288897015fb3ee63b3f720b396b590c17fd7 Mon Sep 17 00:00:00 2001
2 From: Maso Huang <maso.huang@mediatek.com>
3 Date: Thu, 17 Aug 2023 18:13:34 +0800
4 Subject: [PATCH 2/9] ASoC: mediatek: mt7986: support etdm in platform driver
6 Add mt7986 etdm dai driver support.
8 Signed-off-by: Maso Huang <maso.huang@mediatek.com>
9 Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
10 Link: https://lore.kernel.org/r/20230817101338.18782-3-maso.huang@mediatek.com
11 Signed-off-by: Mark Brown <broonie@kernel.org>
13 sound/soc/mediatek/mt7986/mt7986-dai-etdm.c | 411 ++++++++++++++++++++
14 1 file changed, 411 insertions(+)
15 create mode 100644 sound/soc/mediatek/mt7986/mt7986-dai-etdm.c
18 +++ b/sound/soc/mediatek/mt7986/mt7986-dai-etdm.c
20 +// SPDX-License-Identifier: GPL-2.0
22 + * MediaTek ALSA SoC Audio DAI eTDM Control
24 + * Copyright (c) 2023 MediaTek Inc.
25 + * Authors: Vic Wu <vic.wu@mediatek.com>
26 + * Maso Huang <maso.huang@mediatek.com>
29 +#include <linux/bitfield.h>
30 +#include <linux/bitops.h>
31 +#include <linux/regmap.h>
32 +#include <sound/pcm_params.h>
33 +#include "mt7986-afe-common.h"
34 +#include "mt7986-reg.h"
36 +#define HOPPING_CLK 0
38 +#define MTK_DAI_ETDM_FORMAT_I2S 0
39 +#define MTK_DAI_ETDM_FORMAT_DSPA 4
40 +#define MTK_DAI_ETDM_FORMAT_DSPB 5
43 + MTK_ETDM_RATE_8K = 0,
44 + MTK_ETDM_RATE_12K = 1,
45 + MTK_ETDM_RATE_16K = 2,
46 + MTK_ETDM_RATE_24K = 3,
47 + MTK_ETDM_RATE_32K = 4,
48 + MTK_ETDM_RATE_48K = 5,
49 + MTK_ETDM_RATE_96K = 7,
50 + MTK_ETDM_RATE_192K = 9,
51 + MTK_ETDM_RATE_11K = 16,
52 + MTK_ETDM_RATE_22K = 17,
53 + MTK_ETDM_RATE_44K = 18,
54 + MTK_ETDM_RATE_88K = 19,
55 + MTK_ETDM_RATE_176K = 20,
58 +struct mtk_dai_etdm_priv {
62 + unsigned int format;
65 +static unsigned int mt7986_etdm_rate_transform(struct device *dev, unsigned int rate)
69 + return MTK_ETDM_RATE_8K;
71 + return MTK_ETDM_RATE_11K;
73 + return MTK_ETDM_RATE_12K;
75 + return MTK_ETDM_RATE_16K;
77 + return MTK_ETDM_RATE_22K;
79 + return MTK_ETDM_RATE_24K;
81 + return MTK_ETDM_RATE_32K;
83 + return MTK_ETDM_RATE_44K;
85 + return MTK_ETDM_RATE_48K;
87 + return MTK_ETDM_RATE_88K;
89 + return MTK_ETDM_RATE_96K;
91 + return MTK_ETDM_RATE_176K;
93 + return MTK_ETDM_RATE_192K;
95 + dev_warn(dev, "%s(), rate %u invalid, using %d!!!\n",
96 + __func__, rate, MTK_ETDM_RATE_48K);
97 + return MTK_ETDM_RATE_48K;
101 +static int get_etdm_wlen(unsigned int bitwidth)
103 + return bitwidth <= 16 ? 16 : 32;
107 +/* interconnection */
109 +static const struct snd_kcontrol_new o124_mix[] = {
110 + SOC_DAPM_SINGLE_AUTODISABLE("I032_Switch", AFE_CONN124_1, 0, 1, 0),
113 +static const struct snd_kcontrol_new o125_mix[] = {
114 + SOC_DAPM_SINGLE_AUTODISABLE("I033_Switch", AFE_CONN125_1, 1, 1, 0),
117 +static const struct snd_soc_dapm_widget mtk_dai_etdm_widgets[] = {
120 + SND_SOC_DAPM_MIXER("I150", SND_SOC_NOPM, 0, 0, NULL, 0),
121 + SND_SOC_DAPM_MIXER("I151", SND_SOC_NOPM, 0, 0, NULL, 0),
123 + SND_SOC_DAPM_MIXER("O124", SND_SOC_NOPM, 0, 0, o124_mix, ARRAY_SIZE(o124_mix)),
124 + SND_SOC_DAPM_MIXER("O125", SND_SOC_NOPM, 0, 0, o125_mix, ARRAY_SIZE(o125_mix)),
127 +static const struct snd_soc_dapm_route mtk_dai_etdm_routes[] = {
128 + {"I150", NULL, "ETDM Capture"},
129 + {"I151", NULL, "ETDM Capture"},
130 + {"ETDM Playback", NULL, "O124"},
131 + {"ETDM Playback", NULL, "O125"},
132 + {"O124", "I032_Switch", "I032"},
133 + {"O125", "I033_Switch", "I033"},
137 +static int mtk_dai_etdm_startup(struct snd_pcm_substream *substream,
138 + struct snd_soc_dai *dai)
140 + struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
141 + struct mt7986_afe_private *afe_priv = afe->platform_priv;
144 + ret = clk_bulk_prepare_enable(afe_priv->num_clks, afe_priv->clks);
146 + return dev_err_probe(afe->dev, ret, "Failed to enable clocks\n");
148 + regmap_update_bits(afe->regmap, AUDIO_TOP_CON2, CLK_OUT5_PDN_MASK, 0);
149 + regmap_update_bits(afe->regmap, AUDIO_TOP_CON2, CLK_IN5_PDN_MASK, 0);
154 +static void mtk_dai_etdm_shutdown(struct snd_pcm_substream *substream,
155 + struct snd_soc_dai *dai)
157 + struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
158 + struct mt7986_afe_private *afe_priv = afe->platform_priv;
160 + regmap_update_bits(afe->regmap, AUDIO_TOP_CON2, CLK_OUT5_PDN_MASK,
162 + regmap_update_bits(afe->regmap, AUDIO_TOP_CON2, CLK_IN5_PDN_MASK,
165 + clk_bulk_disable_unprepare(afe_priv->num_clks, afe_priv->clks);
168 +static unsigned int get_etdm_ch_fixup(unsigned int channels)
172 + else if (channels > 8)
174 + else if (channels > 4)
176 + else if (channels > 2)
182 +static int mtk_dai_etdm_config(struct mtk_base_afe *afe,
183 + struct snd_pcm_hw_params *params,
184 + struct snd_soc_dai *dai,
187 + struct mt7986_afe_private *afe_priv = afe->platform_priv;
188 + struct mtk_dai_etdm_priv *etdm_data = afe_priv->dai_priv[dai->id];
189 + unsigned int rate = params_rate(params);
190 + unsigned int etdm_rate = mt7986_etdm_rate_transform(afe->dev, rate);
191 + unsigned int afe_rate = mt7986_afe_rate_transform(afe->dev, rate);
192 + unsigned int channels = params_channels(params);
193 + unsigned int bit_width = params_width(params);
194 + unsigned int wlen = get_etdm_wlen(bit_width);
195 + unsigned int val = 0;
196 + unsigned int mask = 0;
198 + dev_dbg(afe->dev, "%s(), stream %d, rate %u, bitwidth %u\n",
199 + __func__, stream, rate, bit_width);
202 + mask |= ETDM_BIT_LEN_MASK;
203 + val |= FIELD_PREP(ETDM_BIT_LEN_MASK, bit_width - 1);
204 + mask |= ETDM_WRD_LEN_MASK;
205 + val |= FIELD_PREP(ETDM_WRD_LEN_MASK, wlen - 1);
206 + mask |= ETDM_FMT_MASK;
207 + val |= FIELD_PREP(ETDM_FMT_MASK, etdm_data->format);
208 + mask |= ETDM_CH_NUM_MASK;
209 + val |= FIELD_PREP(ETDM_CH_NUM_MASK, get_etdm_ch_fixup(channels) - 1);
210 + mask |= RELATCH_SRC_MASK;
211 + val |= FIELD_PREP(RELATCH_SRC_MASK, APLL_CLK);
214 + case SNDRV_PCM_STREAM_PLAYBACK:
215 + /* set ETDM_OUT5_CON0 */
216 + regmap_update_bits(afe->regmap, ETDM_OUT5_CON0, mask, val);
218 + /* set ETDM_OUT5_CON4 */
219 + regmap_update_bits(afe->regmap, ETDM_OUT5_CON4,
220 + OUT_RELATCH_MASK, OUT_RELATCH(afe_rate));
221 + regmap_update_bits(afe->regmap, ETDM_OUT5_CON4,
222 + OUT_CLK_SRC_MASK, OUT_CLK_SRC(APLL_CLK));
223 + regmap_update_bits(afe->regmap, ETDM_OUT5_CON4,
224 + OUT_SEL_FS_MASK, OUT_SEL_FS(etdm_rate));
226 + /* set ETDM_OUT5_CON5 */
227 + regmap_update_bits(afe->regmap, ETDM_OUT5_CON5,
228 + ETDM_CLK_DIV_MASK, ETDM_CLK_DIV);
230 + case SNDRV_PCM_STREAM_CAPTURE:
231 + /* set ETDM_IN5_CON0 */
232 + regmap_update_bits(afe->regmap, ETDM_IN5_CON0, mask, val);
233 + regmap_update_bits(afe->regmap, ETDM_IN5_CON0,
234 + ETDM_SYNC_MASK, ETDM_SYNC);
236 + /* set ETDM_IN5_CON2 */
237 + regmap_update_bits(afe->regmap, ETDM_IN5_CON2,
238 + IN_CLK_SRC_MASK, IN_CLK_SRC(APLL_CLK));
240 + /* set ETDM_IN5_CON3 */
241 + regmap_update_bits(afe->regmap, ETDM_IN5_CON3,
242 + IN_SEL_FS_MASK, IN_SEL_FS(etdm_rate));
244 + /* set ETDM_IN5_CON4 */
245 + regmap_update_bits(afe->regmap, ETDM_IN5_CON4,
246 + IN_RELATCH_MASK, IN_RELATCH(afe_rate));
255 +static int mtk_dai_etdm_hw_params(struct snd_pcm_substream *substream,
256 + struct snd_pcm_hw_params *params,
257 + struct snd_soc_dai *dai)
259 + struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
261 + mtk_dai_etdm_config(afe, params, dai, SNDRV_PCM_STREAM_PLAYBACK);
262 + mtk_dai_etdm_config(afe, params, dai, SNDRV_PCM_STREAM_CAPTURE);
267 +static int mtk_dai_etdm_trigger(struct snd_pcm_substream *substream, int cmd,
268 + struct snd_soc_dai *dai)
270 + struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
272 + dev_dbg(afe->dev, "%s(), cmd %d, dai id %d\n", __func__, cmd, dai->id);
274 + case SNDRV_PCM_TRIGGER_START:
275 + case SNDRV_PCM_TRIGGER_RESUME:
276 + regmap_update_bits(afe->regmap, ETDM_IN5_CON0, ETDM_EN_MASK,
278 + regmap_update_bits(afe->regmap, ETDM_OUT5_CON0, ETDM_EN_MASK,
281 + case SNDRV_PCM_TRIGGER_STOP:
282 + case SNDRV_PCM_TRIGGER_SUSPEND:
283 + regmap_update_bits(afe->regmap, ETDM_IN5_CON0, ETDM_EN_MASK,
285 + regmap_update_bits(afe->regmap, ETDM_OUT5_CON0, ETDM_EN_MASK,
295 +static int mtk_dai_etdm_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
297 + struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);
298 + struct mt7986_afe_private *afe_priv = afe->platform_priv;
299 + struct mtk_dai_etdm_priv *etdm_data;
303 + case MT7986_DAI_ETDM:
306 + dev_warn(afe->dev, "%s(), id %d not support\n",
307 + __func__, dai->id);
311 + priv_data = devm_kzalloc(afe->dev, sizeof(struct mtk_dai_etdm_priv),
316 + afe_priv->dai_priv[dai->id] = priv_data;
317 + etdm_data = afe_priv->dai_priv[dai->id];
319 + switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
320 + case SND_SOC_DAIFMT_I2S:
321 + etdm_data->format = MTK_DAI_ETDM_FORMAT_I2S;
323 + case SND_SOC_DAIFMT_DSP_A:
324 + etdm_data->format = MTK_DAI_ETDM_FORMAT_DSPA;
326 + case SND_SOC_DAIFMT_DSP_B:
327 + etdm_data->format = MTK_DAI_ETDM_FORMAT_DSPB;
333 + switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
334 + case SND_SOC_DAIFMT_NB_NF:
335 + etdm_data->bck_inv = false;
336 + etdm_data->lrck_inv = false;
338 + case SND_SOC_DAIFMT_NB_IF:
339 + etdm_data->bck_inv = false;
340 + etdm_data->lrck_inv = true;
342 + case SND_SOC_DAIFMT_IB_NF:
343 + etdm_data->bck_inv = true;
344 + etdm_data->lrck_inv = false;
346 + case SND_SOC_DAIFMT_IB_IF:
347 + etdm_data->bck_inv = true;
348 + etdm_data->lrck_inv = true;
354 + switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
355 + case SND_SOC_DAIFMT_CBM_CFM:
356 + etdm_data->slave_mode = true;
358 + case SND_SOC_DAIFMT_CBS_CFS:
359 + etdm_data->slave_mode = false;
368 +static const struct snd_soc_dai_ops mtk_dai_etdm_ops = {
369 + .startup = mtk_dai_etdm_startup,
370 + .shutdown = mtk_dai_etdm_shutdown,
371 + .hw_params = mtk_dai_etdm_hw_params,
372 + .trigger = mtk_dai_etdm_trigger,
373 + .set_fmt = mtk_dai_etdm_set_fmt,
377 +#define MTK_ETDM_RATES (SNDRV_PCM_RATE_8000_48000 |\
378 + SNDRV_PCM_RATE_88200 |\
379 + SNDRV_PCM_RATE_96000 |\
380 + SNDRV_PCM_RATE_176400 |\
381 + SNDRV_PCM_RATE_192000)
383 +#define MTK_ETDM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
384 + SNDRV_PCM_FMTBIT_S24_LE |\
385 + SNDRV_PCM_FMTBIT_S32_LE)
387 +static struct snd_soc_dai_driver mtk_dai_etdm_driver[] = {
390 + .id = MT7986_DAI_ETDM,
392 + .stream_name = "ETDM Capture",
395 + .rates = MTK_ETDM_RATES,
396 + .formats = MTK_ETDM_FORMATS,
399 + .stream_name = "ETDM Playback",
402 + .rates = MTK_ETDM_RATES,
403 + .formats = MTK_ETDM_FORMATS,
405 + .ops = &mtk_dai_etdm_ops,
406 + .symmetric_rate = 1,
407 + .symmetric_sample_bits = 1,
411 +int mt7986_dai_etdm_register(struct mtk_base_afe *afe)
413 + struct mtk_base_afe_dai *dai;
415 + dai = devm_kzalloc(afe->dev, sizeof(*dai), GFP_KERNEL);
419 + list_add(&dai->list, &afe->sub_dais);
421 + dai->dai_drivers = mtk_dai_etdm_driver;
422 + dai->num_dai_drivers = ARRAY_SIZE(mtk_dai_etdm_driver);
424 + dai->dapm_widgets = mtk_dai_etdm_widgets;
425 + dai->num_dapm_widgets = ARRAY_SIZE(mtk_dai_etdm_widgets);
426 + dai->dapm_routes = mtk_dai_etdm_routes;
427 + dai->num_dapm_routes = ARRAY_SIZE(mtk_dai_etdm_routes);