summaryrefslogtreecommitdiff
path: root/sound/soc/mediatek/mt8365/mt8365-dai-dmic.c
blob: f9945c2a2cd13c2c5ca18af31edf345f99ae095d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// SPDX-License-Identifier: GPL-2.0
/*
 * MediaTek 8365 ALSA SoC Audio DAI DMIC Control
 *
 * Copyright (c) 2024 MediaTek Inc.
 * Authors: Jia Zeng <jia.zeng@mediatek.com>
 *          Alexandre Mergnat <amergnat@baylibre.com>
 */

#include <linux/bitops.h>
#include <linux/regmap.h>
#include <sound/pcm_params.h>
#include "mt8365-afe-clk.h"
#include "mt8365-afe-common.h"

struct mt8365_dmic_data {
	bool two_wire_mode;
	unsigned int clk_phase_sel_ch1;
	unsigned int clk_phase_sel_ch2;
	bool iir_on;
	unsigned int irr_mode;
	unsigned int dmic_mode;
	unsigned int dmic_channel;
};

static int get_chan_reg(unsigned int channel)
{
	switch (channel) {
	case 8:
		fallthrough;
	case 7:
		return AFE_DMIC3_UL_SRC_CON0;
	case 6:
		fallthrough;
	case 5:
		return AFE_DMIC2_UL_SRC_CON0;
	case 4:
		fallthrough;
	case 3:
		return AFE_DMIC1_UL_SRC_CON0;
	case 2:
		fallthrough;
	case 1:
		return AFE_DMIC0_UL_SRC_CON0;
	default:
		return -EINVAL;
	}
}

/* DAI Drivers */

static void audio_dmic_adda_enable(struct mtk_base_afe *afe)
{
	mt8365_dai_enable_adda_on(afe);
	regmap_update_bits(afe->regmap, AFE_ADDA_UL_DL_CON0,
			   AFE_ADDA_UL_DL_DMIC_CLKDIV_ON,
			   AFE_ADDA_UL_DL_DMIC_CLKDIV_ON);
}

static void audio_dmic_adda_disable(struct mtk_base_afe *afe)
{
	regmap_update_bits(afe->regmap, AFE_ADDA_UL_DL_CON0,
			   AFE_ADDA_UL_DL_DMIC_CLKDIV_ON,
			   ~AFE_ADDA_UL_DL_DMIC_CLKDIV_ON);
	mt8365_dai_disable_adda_on(afe);
}

static void mt8365_dai_enable_dmic(struct mtk_base_afe *afe,
				   struct snd_pcm_substream *substream,
				   struct snd_soc_dai *dai)
{
	struct mt8365_afe_private *afe_priv = afe->platform_priv;
	struct mt8365_dmic_data *dmic_data = afe_priv->dai_priv[MT8365_AFE_IO_DMIC];
	unsigned int val_mask;
	int reg = get_chan_reg(dmic_data->dmic_channel);

	if (reg < 0)
		return;

	/* val and mask will be always same to enable */
	val_mask = DMIC_TOP_CON_CH1_ON |
		   DMIC_TOP_CON_CH2_ON |
		   DMIC_TOP_CON_SRC_ON;

	regmap_update_bits(afe->regmap, reg, val_mask, val_mask);
}

static void mt8365_dai_disable_dmic(struct mtk_base_afe *afe,
				    struct snd_pcm_substream *substream,
				    struct snd_soc_dai *dai)
{
	struct mt8365_afe_private *afe_priv = afe->platform_priv;
	struct mt8365_dmic_data *dmic_data = afe_priv->dai_priv[MT8365_AFE_IO_DMIC];
	unsigned int mask;
	int reg = get_chan_reg(dmic_data->dmic_channel);

	if (reg < 0)
		return;

	dev_dbg(afe->dev, "%s dmic_channel %d\n", __func__, dmic_data->dmic_channel);

	mask = DMIC_TOP_CON_CH1_ON |
	       DMIC_TOP_CON_CH2_ON |
	       DMIC_TOP_CON_SRC_ON |
	       DMIC_TOP_CON_SDM3_LEVEL_MODE;

	/* Set all masked values to 0 */
	regmap_update_bits(afe->regmap, reg, mask, 0);
}

static int mt8365_dai_configure_dmic(struct mtk_base_afe *afe,
				     struct snd_pcm_substream *substream,
				     struct snd_soc_dai *dai)
{
	struct mt8365_afe_private *afe_priv = afe->platform_priv;
	struct mt8365_dmic_data *dmic_data = afe_priv->dai_priv[MT8365_AFE_IO_DMIC];
	bool two_wire_mode = dmic_data->two_wire_mode;
	unsigned int clk_phase_sel_ch1 = dmic_data->clk_phase_sel_ch1;
	unsigned int clk_phase_sel_ch2 = dmic_data->clk_phase_sel_ch2;
	unsigned int val = 0;
	unsigned int rate = dai->rate;
	int reg = get_chan_reg(dai->channels);

	if (reg < 0)
		return -EINVAL;

	dmic_data->dmic_channel = dai->channels;

	val |= DMIC_TOP_CON_SDM3_LEVEL_MODE;

	if (two_wire_mode) {
		val |= DMIC_TOP_CON_TWO_WIRE_MODE;
	} else {
		val |= FIELD_PREP(DMIC_TOP_CON_CK_PHASE_SEL_CH1,
				  clk_phase_sel_ch1);
		val |= FIELD_PREP(DMIC_TOP_CON_CK_PHASE_SEL_CH2,
				  clk_phase_sel_ch2);
	}

	switch (rate) {
	case 48000:
		val |= DMIC_TOP_CON_VOICE_MODE_48K;
		break;
	case 32000:
		val |= DMIC_TOP_CON_VOICE_MODE_32K;
		break;
	case 16000:
		val |= DMIC_TOP_CON_VOICE_MODE_16K;
		break;
	case 8000:
		val |= DMIC_TOP_CON_VOICE_MODE_8K;
		break;
	default:
		return -EINVAL;
	}

	regmap_update_bits(afe->regmap, reg, DMIC_TOP_CON_CONFIG_MASK, val);

	return 0;
}

static int mt8365_dai_dmic_startup(struct snd_pcm_substream *substream,
				   struct snd_soc_dai *dai)
{
	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);

	mt8365_afe_enable_main_clk(afe);

	mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_DMIC0_ADC);
	mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_DMIC1_ADC);
	mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_DMIC2_ADC);
	mt8365_afe_enable_top_cg(afe, MT8365_TOP_CG_DMIC3_ADC);

	audio_dmic_adda_enable(afe);

	return 0;
}

static void mt8365_dai_dmic_shutdown(struct snd_pcm_substream *substream,
				     struct snd_soc_dai *dai)
{
	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);

	mt8365_dai_disable_dmic(afe, substream, dai);
	audio_dmic_adda_disable(afe);
	/* HW Request delay 125us before CG off */
	usleep_range(125, 300);
	mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_DMIC3_ADC);
	mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_DMIC2_ADC);
	mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_DMIC1_ADC);
	mt8365_afe_disable_top_cg(afe, MT8365_TOP_CG_DMIC0_ADC);

	mt8365_afe_disable_main_clk(afe);
}

static int mt8365_dai_dmic_prepare(struct snd_pcm_substream *substream,
				   struct snd_soc_dai *dai)
{
	struct mtk_base_afe *afe = snd_soc_dai_get_drvdata(dai);

	mt8365_dai_configure_dmic(afe, substream, dai);
	mt8365_dai_enable_dmic(afe, substream, dai);

	return 0;
}

static const struct snd_soc_dai_ops mt8365_afe_dmic_ops = {
	.startup	= mt8365_dai_dmic_startup,
	.shutdown	= mt8365_dai_dmic_shutdown,
	.prepare	= mt8365_dai_dmic_prepare,
};

static struct snd_soc_dai_driver mtk_dai_dmic_driver[] = {
	{
		.name = "DMIC",
		.id = MT8365_AFE_IO_DMIC,
		.capture = {
			.stream_name = "DMIC Capture",
			.channels_min = 1,
			.channels_max = 8,
			.rates = SNDRV_PCM_RATE_16000 |
				 SNDRV_PCM_RATE_32000 |
				 SNDRV_PCM_RATE_48000,
			.formats = SNDRV_PCM_FMTBIT_S16_LE |
				   SNDRV_PCM_FMTBIT_S32_LE,
		},
		.ops = &mt8365_afe_dmic_ops,
	}
};

/* DAI Controls */

/* Values for 48kHz mode */
static const char * const iir_mode_src[] = {
	"SW custom", "5Hz", "10Hz", "25Hz", "50Hz", "65Hz"
};

static SOC_ENUM_SINGLE_DECL(iir_mode, AFE_DMIC0_UL_SRC_CON0, 7, iir_mode_src);

static const struct snd_kcontrol_new mtk_dai_dmic_controls[] = {
	SOC_SINGLE("DMIC IIR Switch", AFE_DMIC0_UL_SRC_CON0, DMIC_TOP_CON_IIR_ON, 1, 0),
	SOC_ENUM("DMIC IIR Mode", iir_mode),
};

/* DAI widget */

static const struct snd_soc_dapm_widget mtk_dai_dmic_widgets[] = {
	SND_SOC_DAPM_INPUT("DMIC In"),
};

/* DAI route */

static const struct snd_soc_dapm_route mtk_dai_dmic_routes[] = {
	{"I14", NULL, "DMIC Capture"},
	{"I15", NULL, "DMIC Capture"},
	{"I16", NULL, "DMIC Capture"},
	{"I17", NULL, "DMIC Capture"},
	{"I18", NULL, "DMIC Capture"},
	{"I19", NULL, "DMIC Capture"},
	{"I20", NULL, "DMIC Capture"},
	{"I21", NULL, "DMIC Capture"},
	{"DMIC Capture", NULL, "DMIC In"},
};

static int init_dmic_priv_data(struct mtk_base_afe *afe)
{
	struct mt8365_afe_private *afe_priv = afe->platform_priv;
	struct mt8365_dmic_data *dmic_priv;
	struct device_node *np = afe->dev->of_node;
	unsigned int temps[4];
	int ret;

	dmic_priv = devm_kzalloc(afe->dev, sizeof(*dmic_priv), GFP_KERNEL);
	if (!dmic_priv)
		return -ENOMEM;

	ret = of_property_read_u32_array(np, "mediatek,dmic-mode",
					 &temps[0],
					 1);
	if (ret == 0)
		dmic_priv->two_wire_mode = !!temps[0];

	if (!dmic_priv->two_wire_mode) {
		dmic_priv->clk_phase_sel_ch1 = 0;
		dmic_priv->clk_phase_sel_ch2 = 4;
	}

	afe_priv->dai_priv[MT8365_AFE_IO_DMIC] = dmic_priv;
	return 0;
}

int mt8365_dai_dmic_register(struct mtk_base_afe *afe)
{
	struct mtk_base_afe_dai *dai;

	dai = devm_kzalloc(afe->dev, sizeof(*dai), GFP_KERNEL);
	if (!dai)
		return -ENOMEM;

	list_add(&dai->list, &afe->sub_dais);
	dai->dai_drivers = mtk_dai_dmic_driver;
	dai->num_dai_drivers = ARRAY_SIZE(mtk_dai_dmic_driver);
	dai->controls = mtk_dai_dmic_controls;
	dai->num_controls = ARRAY_SIZE(mtk_dai_dmic_controls);
	dai->dapm_widgets = mtk_dai_dmic_widgets;
	dai->num_dapm_widgets = ARRAY_SIZE(mtk_dai_dmic_widgets);
	dai->dapm_routes = mtk_dai_dmic_routes;
	dai->num_dapm_routes = ARRAY_SIZE(mtk_dai_dmic_routes);
	return init_dmic_priv_data(afe);
}