xref: /openbmc/linux/sound/soc/fsl/fsl_micfil.c (revision f0862123)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 // Copyright 2018 NXP
3 
4 #include <linux/bitfield.h>
5 #include <linux/clk.h>
6 #include <linux/device.h>
7 #include <linux/interrupt.h>
8 #include <linux/kobject.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_platform.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
17 #include <linux/sysfs.h>
18 #include <linux/types.h>
19 #include <linux/dma/imx-dma.h>
20 #include <sound/dmaengine_pcm.h>
21 #include <sound/pcm.h>
22 #include <sound/soc.h>
23 #include <sound/tlv.h>
24 #include <sound/core.h>
25 
26 #include "fsl_micfil.h"
27 #include "fsl_utils.h"
28 
29 #define MICFIL_OSR_DEFAULT	16
30 
31 #define MICFIL_NUM_RATES	7
32 #define MICFIL_CLK_SRC_NUM	3
33 /* clock source ids */
34 #define MICFIL_AUDIO_PLL1	0
35 #define MICFIL_AUDIO_PLL2	1
36 #define MICFIL_CLK_EXT3		2
37 
38 enum quality {
39 	QUALITY_HIGH,
40 	QUALITY_MEDIUM,
41 	QUALITY_LOW,
42 	QUALITY_VLOW0,
43 	QUALITY_VLOW1,
44 	QUALITY_VLOW2,
45 };
46 
47 struct fsl_micfil {
48 	struct platform_device *pdev;
49 	struct regmap *regmap;
50 	const struct fsl_micfil_soc_data *soc;
51 	struct clk *busclk;
52 	struct clk *mclk;
53 	struct clk *pll8k_clk;
54 	struct clk *pll11k_clk;
55 	struct clk *clk_src[MICFIL_CLK_SRC_NUM];
56 	struct snd_dmaengine_dai_dma_data dma_params_rx;
57 	struct sdma_peripheral_config sdmacfg;
58 	struct snd_soc_card *card;
59 	struct snd_pcm_hw_constraint_list constraint_rates;
60 	unsigned int constraint_rates_list[MICFIL_NUM_RATES];
61 	unsigned int dataline;
62 	char name[32];
63 	int irq[MICFIL_IRQ_LINES];
64 	enum quality quality;
65 	int dc_remover;
66 	int vad_init_mode;
67 	int vad_enabled;
68 	int vad_detected;
69 	struct fsl_micfil_verid verid;
70 	struct fsl_micfil_param param;
71 };
72 
73 struct fsl_micfil_soc_data {
74 	unsigned int fifos;
75 	unsigned int fifo_depth;
76 	unsigned int dataline;
77 	bool imx;
78 	bool use_edma;
79 	bool use_verid;
80 	bool volume_sx;
81 	u64  formats;
82 };
83 
84 static struct fsl_micfil_soc_data fsl_micfil_imx8mm = {
85 	.imx = true,
86 	.fifos = 8,
87 	.fifo_depth = 8,
88 	.dataline =  0xf,
89 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
90 	.volume_sx = true,
91 };
92 
93 static struct fsl_micfil_soc_data fsl_micfil_imx8mp = {
94 	.imx = true,
95 	.fifos = 8,
96 	.fifo_depth = 32,
97 	.dataline =  0xf,
98 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
99 	.volume_sx = false,
100 };
101 
102 static struct fsl_micfil_soc_data fsl_micfil_imx93 = {
103 	.imx = true,
104 	.fifos = 8,
105 	.fifo_depth = 32,
106 	.dataline =  0xf,
107 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
108 	.use_edma = true,
109 	.use_verid = true,
110 	.volume_sx = false,
111 };
112 
113 static const struct of_device_id fsl_micfil_dt_ids[] = {
114 	{ .compatible = "fsl,imx8mm-micfil", .data = &fsl_micfil_imx8mm },
115 	{ .compatible = "fsl,imx8mp-micfil", .data = &fsl_micfil_imx8mp },
116 	{ .compatible = "fsl,imx93-micfil", .data = &fsl_micfil_imx93 },
117 	{}
118 };
119 MODULE_DEVICE_TABLE(of, fsl_micfil_dt_ids);
120 
121 static const char * const micfil_quality_select_texts[] = {
122 	[QUALITY_HIGH] = "High",
123 	[QUALITY_MEDIUM] = "Medium",
124 	[QUALITY_LOW] = "Low",
125 	[QUALITY_VLOW0] = "VLow0",
126 	[QUALITY_VLOW1] = "Vlow1",
127 	[QUALITY_VLOW2] = "Vlow2",
128 };
129 
130 static const struct soc_enum fsl_micfil_quality_enum =
131 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(micfil_quality_select_texts),
132 			    micfil_quality_select_texts);
133 
134 static DECLARE_TLV_DB_SCALE(gain_tlv, 0, 100, 0);
135 
micfil_set_quality(struct fsl_micfil * micfil)136 static int micfil_set_quality(struct fsl_micfil *micfil)
137 {
138 	u32 qsel;
139 
140 	switch (micfil->quality) {
141 	case QUALITY_HIGH:
142 		qsel = MICFIL_QSEL_HIGH_QUALITY;
143 		break;
144 	case QUALITY_MEDIUM:
145 		qsel = MICFIL_QSEL_MEDIUM_QUALITY;
146 		break;
147 	case QUALITY_LOW:
148 		qsel = MICFIL_QSEL_LOW_QUALITY;
149 		break;
150 	case QUALITY_VLOW0:
151 		qsel = MICFIL_QSEL_VLOW0_QUALITY;
152 		break;
153 	case QUALITY_VLOW1:
154 		qsel = MICFIL_QSEL_VLOW1_QUALITY;
155 		break;
156 	case QUALITY_VLOW2:
157 		qsel = MICFIL_QSEL_VLOW2_QUALITY;
158 		break;
159 	}
160 
161 	return regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
162 				  MICFIL_CTRL2_QSEL,
163 				  FIELD_PREP(MICFIL_CTRL2_QSEL, qsel));
164 }
165 
micfil_quality_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)166 static int micfil_quality_get(struct snd_kcontrol *kcontrol,
167 			      struct snd_ctl_elem_value *ucontrol)
168 {
169 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
170 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(cmpnt);
171 
172 	ucontrol->value.integer.value[0] = micfil->quality;
173 
174 	return 0;
175 }
176 
micfil_quality_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)177 static int micfil_quality_set(struct snd_kcontrol *kcontrol,
178 			      struct snd_ctl_elem_value *ucontrol)
179 {
180 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
181 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(cmpnt);
182 
183 	micfil->quality = ucontrol->value.integer.value[0];
184 
185 	return micfil_set_quality(micfil);
186 }
187 
188 static const char * const micfil_hwvad_enable[] = {
189 	"Disable (Record only)",
190 	"Enable (Record with Vad)",
191 };
192 
193 static const char * const micfil_hwvad_init_mode[] = {
194 	"Envelope mode", "Energy mode",
195 };
196 
197 static const char * const micfil_hwvad_hpf_texts[] = {
198 	"Filter bypass",
199 	"Cut-off @1750Hz",
200 	"Cut-off @215Hz",
201 	"Cut-off @102Hz",
202 };
203 
204 /*
205  * DC Remover Control
206  * Filter Bypassed	1 1
207  * Cut-off @21Hz	0 0
208  * Cut-off @83Hz	0 1
209  * Cut-off @152HZ	1 0
210  */
211 static const char * const micfil_dc_remover_texts[] = {
212 	"Cut-off @21Hz", "Cut-off @83Hz",
213 	"Cut-off @152Hz", "Bypass",
214 };
215 
216 static const struct soc_enum hwvad_enable_enum =
217 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(micfil_hwvad_enable),
218 			    micfil_hwvad_enable);
219 static const struct soc_enum hwvad_init_mode_enum =
220 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(micfil_hwvad_init_mode),
221 			    micfil_hwvad_init_mode);
222 static const struct soc_enum hwvad_hpf_enum =
223 	SOC_ENUM_SINGLE(REG_MICFIL_VAD0_CTRL2, 0,
224 			ARRAY_SIZE(micfil_hwvad_hpf_texts),
225 			micfil_hwvad_hpf_texts);
226 static const struct soc_enum fsl_micfil_dc_remover_enum =
227 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(micfil_dc_remover_texts),
228 			    micfil_dc_remover_texts);
229 
micfil_put_dc_remover_state(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)230 static int micfil_put_dc_remover_state(struct snd_kcontrol *kcontrol,
231 				       struct snd_ctl_elem_value *ucontrol)
232 {
233 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
234 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
235 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(comp);
236 	unsigned int *item = ucontrol->value.enumerated.item;
237 	int val = snd_soc_enum_item_to_val(e, item[0]);
238 	int i = 0, ret = 0;
239 	u32 reg_val = 0;
240 
241 	if (val < 0 || val > 3)
242 		return -EINVAL;
243 
244 	micfil->dc_remover = val;
245 
246 	/* Calculate total value for all channels */
247 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++)
248 		reg_val |= val << MICFIL_DC_CHX_SHIFT(i);
249 
250 	/* Update DC Remover mode for all channels */
251 	ret = snd_soc_component_update_bits(comp, REG_MICFIL_DC_CTRL,
252 					    MICFIL_DC_CTRL_CONFIG, reg_val);
253 	if (ret < 0)
254 		return ret;
255 
256 	return 0;
257 }
258 
micfil_get_dc_remover_state(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)259 static int micfil_get_dc_remover_state(struct snd_kcontrol *kcontrol,
260 				       struct snd_ctl_elem_value *ucontrol)
261 {
262 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
263 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(comp);
264 
265 	ucontrol->value.enumerated.item[0] = micfil->dc_remover;
266 
267 	return 0;
268 }
269 
hwvad_put_enable(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)270 static int hwvad_put_enable(struct snd_kcontrol *kcontrol,
271 			    struct snd_ctl_elem_value *ucontrol)
272 {
273 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
274 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
275 	unsigned int *item = ucontrol->value.enumerated.item;
276 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(comp);
277 	int val = snd_soc_enum_item_to_val(e, item[0]);
278 
279 	micfil->vad_enabled = val;
280 
281 	return 0;
282 }
283 
hwvad_get_enable(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)284 static int hwvad_get_enable(struct snd_kcontrol *kcontrol,
285 			    struct snd_ctl_elem_value *ucontrol)
286 {
287 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
288 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(comp);
289 
290 	ucontrol->value.enumerated.item[0] = micfil->vad_enabled;
291 
292 	return 0;
293 }
294 
hwvad_put_init_mode(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)295 static int hwvad_put_init_mode(struct snd_kcontrol *kcontrol,
296 			       struct snd_ctl_elem_value *ucontrol)
297 {
298 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
299 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
300 	unsigned int *item = ucontrol->value.enumerated.item;
301 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(comp);
302 	int val = snd_soc_enum_item_to_val(e, item[0]);
303 
304 	/* 0 - Envelope-based Mode
305 	 * 1 - Energy-based Mode
306 	 */
307 	micfil->vad_init_mode = val;
308 
309 	return 0;
310 }
311 
hwvad_get_init_mode(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)312 static int hwvad_get_init_mode(struct snd_kcontrol *kcontrol,
313 			       struct snd_ctl_elem_value *ucontrol)
314 {
315 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
316 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(comp);
317 
318 	ucontrol->value.enumerated.item[0] = micfil->vad_init_mode;
319 
320 	return 0;
321 }
322 
hwvad_detected(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)323 static int hwvad_detected(struct snd_kcontrol *kcontrol,
324 			  struct snd_ctl_elem_value *ucontrol)
325 {
326 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
327 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(comp);
328 
329 	ucontrol->value.enumerated.item[0] = micfil->vad_detected;
330 
331 	return 0;
332 }
333 
334 static const struct snd_kcontrol_new fsl_micfil_volume_controls[] = {
335 	SOC_SINGLE_TLV("CH0 Volume", REG_MICFIL_OUT_CTRL,
336 		       MICFIL_OUTGAIN_CHX_SHIFT(0), 0xF, 0, gain_tlv),
337 	SOC_SINGLE_TLV("CH1 Volume", REG_MICFIL_OUT_CTRL,
338 		       MICFIL_OUTGAIN_CHX_SHIFT(1), 0xF, 0, gain_tlv),
339 	SOC_SINGLE_TLV("CH2 Volume", REG_MICFIL_OUT_CTRL,
340 		       MICFIL_OUTGAIN_CHX_SHIFT(2), 0xF, 0, gain_tlv),
341 	SOC_SINGLE_TLV("CH3 Volume", REG_MICFIL_OUT_CTRL,
342 		       MICFIL_OUTGAIN_CHX_SHIFT(3), 0xF, 0, gain_tlv),
343 	SOC_SINGLE_TLV("CH4 Volume", REG_MICFIL_OUT_CTRL,
344 		       MICFIL_OUTGAIN_CHX_SHIFT(4), 0xF, 0, gain_tlv),
345 	SOC_SINGLE_TLV("CH5 Volume", REG_MICFIL_OUT_CTRL,
346 		       MICFIL_OUTGAIN_CHX_SHIFT(5), 0xF, 0, gain_tlv),
347 	SOC_SINGLE_TLV("CH6 Volume", REG_MICFIL_OUT_CTRL,
348 		       MICFIL_OUTGAIN_CHX_SHIFT(6), 0xF, 0, gain_tlv),
349 	SOC_SINGLE_TLV("CH7 Volume", REG_MICFIL_OUT_CTRL,
350 		       MICFIL_OUTGAIN_CHX_SHIFT(7), 0xF, 0, gain_tlv),
351 };
352 
353 static const struct snd_kcontrol_new fsl_micfil_volume_sx_controls[] = {
354 	SOC_SINGLE_SX_TLV("CH0 Volume", REG_MICFIL_OUT_CTRL,
355 			  MICFIL_OUTGAIN_CHX_SHIFT(0), 0x8, 0xF, gain_tlv),
356 	SOC_SINGLE_SX_TLV("CH1 Volume", REG_MICFIL_OUT_CTRL,
357 			  MICFIL_OUTGAIN_CHX_SHIFT(1), 0x8, 0xF, gain_tlv),
358 	SOC_SINGLE_SX_TLV("CH2 Volume", REG_MICFIL_OUT_CTRL,
359 			  MICFIL_OUTGAIN_CHX_SHIFT(2), 0x8, 0xF, gain_tlv),
360 	SOC_SINGLE_SX_TLV("CH3 Volume", REG_MICFIL_OUT_CTRL,
361 			  MICFIL_OUTGAIN_CHX_SHIFT(3), 0x8, 0xF, gain_tlv),
362 	SOC_SINGLE_SX_TLV("CH4 Volume", REG_MICFIL_OUT_CTRL,
363 			  MICFIL_OUTGAIN_CHX_SHIFT(4), 0x8, 0xF, gain_tlv),
364 	SOC_SINGLE_SX_TLV("CH5 Volume", REG_MICFIL_OUT_CTRL,
365 			  MICFIL_OUTGAIN_CHX_SHIFT(5), 0x8, 0xF, gain_tlv),
366 	SOC_SINGLE_SX_TLV("CH6 Volume", REG_MICFIL_OUT_CTRL,
367 			  MICFIL_OUTGAIN_CHX_SHIFT(6), 0x8, 0xF, gain_tlv),
368 	SOC_SINGLE_SX_TLV("CH7 Volume", REG_MICFIL_OUT_CTRL,
369 			  MICFIL_OUTGAIN_CHX_SHIFT(7), 0x8, 0xF, gain_tlv),
370 };
371 
372 static const struct snd_kcontrol_new fsl_micfil_snd_controls[] = {
373 	SOC_ENUM_EXT("MICFIL Quality Select",
374 		     fsl_micfil_quality_enum,
375 		     micfil_quality_get, micfil_quality_set),
376 	SOC_ENUM_EXT("HWVAD Enablement Switch", hwvad_enable_enum,
377 		     hwvad_get_enable, hwvad_put_enable),
378 	SOC_ENUM_EXT("HWVAD Initialization Mode", hwvad_init_mode_enum,
379 		     hwvad_get_init_mode, hwvad_put_init_mode),
380 	SOC_ENUM("HWVAD High-Pass Filter", hwvad_hpf_enum),
381 	SOC_SINGLE("HWVAD ZCD Switch", REG_MICFIL_VAD0_ZCD, 0, 1, 0),
382 	SOC_SINGLE("HWVAD ZCD Auto Threshold Switch",
383 		   REG_MICFIL_VAD0_ZCD, 2, 1, 0),
384 	SOC_ENUM_EXT("MICFIL DC Remover Control", fsl_micfil_dc_remover_enum,
385 		     micfil_get_dc_remover_state, micfil_put_dc_remover_state),
386 	SOC_SINGLE("HWVAD Input Gain", REG_MICFIL_VAD0_CTRL2, 8, 15, 0),
387 	SOC_SINGLE("HWVAD Sound Gain", REG_MICFIL_VAD0_SCONFIG, 0, 15, 0),
388 	SOC_SINGLE("HWVAD Noise Gain", REG_MICFIL_VAD0_NCONFIG, 0, 15, 0),
389 	SOC_SINGLE_RANGE("HWVAD Detector Frame Time", REG_MICFIL_VAD0_CTRL2, 16, 0, 63, 0),
390 	SOC_SINGLE("HWVAD Detector Initialization Time", REG_MICFIL_VAD0_CTRL1, 8, 31, 0),
391 	SOC_SINGLE("HWVAD Noise Filter Adjustment", REG_MICFIL_VAD0_NCONFIG, 8, 31, 0),
392 	SOC_SINGLE("HWVAD ZCD Threshold", REG_MICFIL_VAD0_ZCD, 16, 1023, 0),
393 	SOC_SINGLE("HWVAD ZCD Adjustment", REG_MICFIL_VAD0_ZCD, 8, 15, 0),
394 	SOC_SINGLE("HWVAD ZCD And Behavior Switch",
395 		   REG_MICFIL_VAD0_ZCD, 4, 1, 0),
396 	SOC_SINGLE_BOOL_EXT("VAD Detected", 0, hwvad_detected, NULL),
397 };
398 
fsl_micfil_use_verid(struct device * dev)399 static int fsl_micfil_use_verid(struct device *dev)
400 {
401 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
402 	unsigned int val;
403 	int ret;
404 
405 	if (!micfil->soc->use_verid)
406 		return 0;
407 
408 	ret = regmap_read(micfil->regmap, REG_MICFIL_VERID, &val);
409 	if (ret < 0)
410 		return ret;
411 
412 	dev_dbg(dev, "VERID: 0x%016X\n", val);
413 
414 	micfil->verid.version = val &
415 		(MICFIL_VERID_MAJOR_MASK | MICFIL_VERID_MINOR_MASK);
416 	micfil->verid.version >>= MICFIL_VERID_MINOR_SHIFT;
417 	micfil->verid.feature = val & MICFIL_VERID_FEATURE_MASK;
418 
419 	ret = regmap_read(micfil->regmap, REG_MICFIL_PARAM, &val);
420 	if (ret < 0)
421 		return ret;
422 
423 	dev_dbg(dev, "PARAM: 0x%016X\n", val);
424 
425 	micfil->param.hwvad_num = (val & MICFIL_PARAM_NUM_HWVAD_MASK) >>
426 		MICFIL_PARAM_NUM_HWVAD_SHIFT;
427 	micfil->param.hwvad_zcd = val & MICFIL_PARAM_HWVAD_ZCD;
428 	micfil->param.hwvad_energy_mode = val & MICFIL_PARAM_HWVAD_ENERGY_MODE;
429 	micfil->param.hwvad = val & MICFIL_PARAM_HWVAD;
430 	micfil->param.dc_out_bypass = val & MICFIL_PARAM_DC_OUT_BYPASS;
431 	micfil->param.dc_in_bypass = val & MICFIL_PARAM_DC_IN_BYPASS;
432 	micfil->param.low_power = val & MICFIL_PARAM_LOW_POWER;
433 	micfil->param.fil_out_width = val & MICFIL_PARAM_FIL_OUT_WIDTH;
434 	micfil->param.fifo_ptrwid = (val & MICFIL_PARAM_FIFO_PTRWID_MASK) >>
435 		MICFIL_PARAM_FIFO_PTRWID_SHIFT;
436 	micfil->param.npair = (val & MICFIL_PARAM_NPAIR_MASK) >>
437 		MICFIL_PARAM_NPAIR_SHIFT;
438 
439 	return 0;
440 }
441 
442 /* The SRES is a self-negated bit which provides the CPU with the
443  * capability to initialize the PDM Interface module through the
444  * slave-bus interface. This bit always reads as zero, and this
445  * bit is only effective when MDIS is cleared
446  */
fsl_micfil_reset(struct device * dev)447 static int fsl_micfil_reset(struct device *dev)
448 {
449 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
450 	int ret;
451 
452 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
453 				MICFIL_CTRL1_MDIS);
454 	if (ret)
455 		return ret;
456 
457 	ret = regmap_set_bits(micfil->regmap, REG_MICFIL_CTRL1,
458 			      MICFIL_CTRL1_SRES);
459 	if (ret)
460 		return ret;
461 
462 	/*
463 	 * SRES is self-cleared bit, but REG_MICFIL_CTRL1 is defined
464 	 * as non-volatile register, so SRES still remain in regmap
465 	 * cache after set, that every update of REG_MICFIL_CTRL1,
466 	 * software reset happens. so clear it explicitly.
467 	 */
468 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
469 				MICFIL_CTRL1_SRES);
470 	if (ret)
471 		return ret;
472 
473 	/*
474 	 * Set SRES should clear CHnF flags, But even add delay here
475 	 * the CHnF may not be cleared sometimes, so clear CHnF explicitly.
476 	 */
477 	ret = regmap_write_bits(micfil->regmap, REG_MICFIL_STAT, 0xFF, 0xFF);
478 	if (ret)
479 		return ret;
480 
481 	return 0;
482 }
483 
fsl_micfil_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)484 static int fsl_micfil_startup(struct snd_pcm_substream *substream,
485 			      struct snd_soc_dai *dai)
486 {
487 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
488 	unsigned int rates[MICFIL_NUM_RATES] = {8000, 11025, 16000, 22050, 32000, 44100, 48000};
489 	int i, j, k = 0;
490 	u64 clk_rate;
491 
492 	if (!micfil) {
493 		dev_err(dai->dev, "micfil dai priv_data not set\n");
494 		return -EINVAL;
495 	}
496 
497 	micfil->constraint_rates.list = micfil->constraint_rates_list;
498 	micfil->constraint_rates.count = 0;
499 
500 	for (j = 0; j < MICFIL_NUM_RATES; j++) {
501 		for (i = 0; i < MICFIL_CLK_SRC_NUM; i++) {
502 			clk_rate = clk_get_rate(micfil->clk_src[i]);
503 			if (clk_rate != 0 && do_div(clk_rate, rates[j]) == 0) {
504 				micfil->constraint_rates_list[k++] = rates[j];
505 				micfil->constraint_rates.count++;
506 				break;
507 			}
508 		}
509 	}
510 
511 	if (micfil->constraint_rates.count > 0)
512 		snd_pcm_hw_constraint_list(substream->runtime, 0,
513 					   SNDRV_PCM_HW_PARAM_RATE,
514 					   &micfil->constraint_rates);
515 
516 	return 0;
517 }
518 
519 /* Enable/disable hwvad interrupts */
fsl_micfil_configure_hwvad_interrupts(struct fsl_micfil * micfil,int enable)520 static int fsl_micfil_configure_hwvad_interrupts(struct fsl_micfil *micfil, int enable)
521 {
522 	u32 vadie_reg = enable ? MICFIL_VAD0_CTRL1_IE : 0;
523 	u32 vaderie_reg = enable ? MICFIL_VAD0_CTRL1_ERIE : 0;
524 
525 	/* Voice Activity Detector Error Interruption */
526 	regmap_update_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
527 			   MICFIL_VAD0_CTRL1_ERIE, vaderie_reg);
528 
529 	/* Voice Activity Detector Interruption */
530 	regmap_update_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
531 			   MICFIL_VAD0_CTRL1_IE, vadie_reg);
532 
533 	return 0;
534 }
535 
536 /* Configuration done only in energy-based initialization mode */
fsl_micfil_init_hwvad_energy_mode(struct fsl_micfil * micfil)537 static int fsl_micfil_init_hwvad_energy_mode(struct fsl_micfil *micfil)
538 {
539 	/* Keep the VADFRENDIS bitfield cleared. */
540 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL2,
541 			  MICFIL_VAD0_CTRL2_FRENDIS);
542 
543 	/* Keep the VADPREFEN bitfield cleared. */
544 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL2,
545 			  MICFIL_VAD0_CTRL2_PREFEN);
546 
547 	/* Keep the VADSFILEN bitfield cleared. */
548 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_SCONFIG,
549 			  MICFIL_VAD0_SCONFIG_SFILEN);
550 
551 	/* Keep the VADSMAXEN bitfield cleared. */
552 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_SCONFIG,
553 			  MICFIL_VAD0_SCONFIG_SMAXEN);
554 
555 	/* Keep the VADNFILAUTO bitfield asserted. */
556 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
557 			MICFIL_VAD0_NCONFIG_NFILAUT);
558 
559 	/* Keep the VADNMINEN bitfield cleared. */
560 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
561 			  MICFIL_VAD0_NCONFIG_NMINEN);
562 
563 	/* Keep the VADNDECEN bitfield cleared. */
564 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
565 			  MICFIL_VAD0_NCONFIG_NDECEN);
566 
567 	/* Keep the VADNOREN bitfield cleared. */
568 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
569 			  MICFIL_VAD0_NCONFIG_NOREN);
570 
571 	return 0;
572 }
573 
574 /* Configuration done only in envelope-based initialization mode */
fsl_micfil_init_hwvad_envelope_mode(struct fsl_micfil * micfil)575 static int fsl_micfil_init_hwvad_envelope_mode(struct fsl_micfil *micfil)
576 {
577 	/* Assert the VADFRENDIS bitfield */
578 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL2,
579 			MICFIL_VAD0_CTRL2_FRENDIS);
580 
581 	/* Assert the VADPREFEN bitfield. */
582 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL2,
583 			MICFIL_VAD0_CTRL2_PREFEN);
584 
585 	/* Assert the VADSFILEN bitfield. */
586 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_SCONFIG,
587 			MICFIL_VAD0_SCONFIG_SFILEN);
588 
589 	/* Assert the VADSMAXEN bitfield. */
590 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_SCONFIG,
591 			MICFIL_VAD0_SCONFIG_SMAXEN);
592 
593 	/* Clear the VADNFILAUTO bitfield */
594 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
595 			  MICFIL_VAD0_NCONFIG_NFILAUT);
596 
597 	/* Assert the VADNMINEN bitfield. */
598 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
599 			MICFIL_VAD0_NCONFIG_NMINEN);
600 
601 	/* Assert the VADNDECEN bitfield. */
602 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
603 			MICFIL_VAD0_NCONFIG_NDECEN);
604 
605 	/* Assert VADNOREN bitfield. */
606 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_NCONFIG,
607 			MICFIL_VAD0_NCONFIG_NOREN);
608 
609 	return 0;
610 }
611 
612 /*
613  * Hardware Voice Active Detection: The HWVAD takes data from the input
614  * of a selected PDM microphone to detect if there is any
615  * voice activity. When a voice activity is detected, an interrupt could
616  * be delivered to the system. Initialization in section 8.4:
617  * Can work in two modes:
618  *  -> Eneveope-based mode (section 8.4.1)
619  *  -> Energy-based mode (section 8.4.2)
620  *
621  * It is important to remark that the HWVAD detector could be enabled
622  * or reset only when the MICFIL isn't running i.e. when the BSY_FIL
623  * bit in STAT register is cleared
624  */
fsl_micfil_hwvad_enable(struct fsl_micfil * micfil)625 static int fsl_micfil_hwvad_enable(struct fsl_micfil *micfil)
626 {
627 	int ret;
628 
629 	micfil->vad_detected = 0;
630 
631 	/* envelope-based specific initialization */
632 	if (micfil->vad_init_mode == MICFIL_HWVAD_ENVELOPE_MODE)
633 		ret = fsl_micfil_init_hwvad_envelope_mode(micfil);
634 	else
635 		ret = fsl_micfil_init_hwvad_energy_mode(micfil);
636 	if (ret)
637 		return ret;
638 
639 	/* Voice Activity Detector Internal Filters Initialization*/
640 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
641 			MICFIL_VAD0_CTRL1_ST10);
642 
643 	/* Voice Activity Detector Internal Filter */
644 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
645 			  MICFIL_VAD0_CTRL1_ST10);
646 
647 	/* Enable Interrupts */
648 	ret = fsl_micfil_configure_hwvad_interrupts(micfil, 1);
649 	if (ret)
650 		return ret;
651 
652 	/* Voice Activity Detector Reset */
653 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
654 			MICFIL_VAD0_CTRL1_RST);
655 
656 	/* Voice Activity Detector Enabled */
657 	regmap_set_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
658 			MICFIL_VAD0_CTRL1_EN);
659 
660 	return 0;
661 }
662 
fsl_micfil_hwvad_disable(struct fsl_micfil * micfil)663 static int fsl_micfil_hwvad_disable(struct fsl_micfil *micfil)
664 {
665 	struct device *dev = &micfil->pdev->dev;
666 	int ret = 0;
667 
668 	/* Disable HWVAD */
669 	regmap_clear_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
670 			  MICFIL_VAD0_CTRL1_EN);
671 
672 	/* Disable hwvad interrupts */
673 	ret = fsl_micfil_configure_hwvad_interrupts(micfil, 0);
674 	if (ret)
675 		dev_err(dev, "Failed to disable interrupts\n");
676 
677 	return ret;
678 }
679 
fsl_micfil_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)680 static int fsl_micfil_trigger(struct snd_pcm_substream *substream, int cmd,
681 			      struct snd_soc_dai *dai)
682 {
683 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
684 	struct device *dev = &micfil->pdev->dev;
685 	int ret;
686 
687 	switch (cmd) {
688 	case SNDRV_PCM_TRIGGER_START:
689 	case SNDRV_PCM_TRIGGER_RESUME:
690 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
691 		ret = fsl_micfil_reset(dev);
692 		if (ret) {
693 			dev_err(dev, "failed to soft reset\n");
694 			return ret;
695 		}
696 
697 		/* DMA Interrupt Selection - DISEL bits
698 		 * 00 - DMA and IRQ disabled
699 		 * 01 - DMA req enabled
700 		 * 10 - IRQ enabled
701 		 * 11 - reserved
702 		 */
703 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
704 				MICFIL_CTRL1_DISEL,
705 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DMA));
706 		if (ret)
707 			return ret;
708 
709 		/* Enable the module */
710 		ret = regmap_set_bits(micfil->regmap, REG_MICFIL_CTRL1,
711 				      MICFIL_CTRL1_PDMIEN);
712 		if (ret)
713 			return ret;
714 
715 		if (micfil->vad_enabled)
716 			fsl_micfil_hwvad_enable(micfil);
717 
718 		break;
719 	case SNDRV_PCM_TRIGGER_STOP:
720 	case SNDRV_PCM_TRIGGER_SUSPEND:
721 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
722 		if (micfil->vad_enabled)
723 			fsl_micfil_hwvad_disable(micfil);
724 
725 		/* Disable the module */
726 		ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
727 					MICFIL_CTRL1_PDMIEN);
728 		if (ret)
729 			return ret;
730 
731 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
732 				MICFIL_CTRL1_DISEL,
733 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DISABLE));
734 		if (ret)
735 			return ret;
736 		break;
737 	default:
738 		return -EINVAL;
739 	}
740 	return 0;
741 }
742 
fsl_micfil_reparent_rootclk(struct fsl_micfil * micfil,unsigned int sample_rate)743 static int fsl_micfil_reparent_rootclk(struct fsl_micfil *micfil, unsigned int sample_rate)
744 {
745 	struct device *dev = &micfil->pdev->dev;
746 	u64 ratio = sample_rate;
747 	struct clk *clk;
748 	int ret;
749 
750 	/* Get root clock */
751 	clk = micfil->mclk;
752 
753 	/* Disable clock first, for it was enabled by pm_runtime */
754 	clk_disable_unprepare(clk);
755 	fsl_asoc_reparent_pll_clocks(dev, clk, micfil->pll8k_clk,
756 				     micfil->pll11k_clk, ratio);
757 	ret = clk_prepare_enable(clk);
758 	if (ret)
759 		return ret;
760 
761 	return 0;
762 }
763 
fsl_micfil_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)764 static int fsl_micfil_hw_params(struct snd_pcm_substream *substream,
765 				struct snd_pcm_hw_params *params,
766 				struct snd_soc_dai *dai)
767 {
768 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
769 	unsigned int channels = params_channels(params);
770 	unsigned int rate = params_rate(params);
771 	int clk_div = 8;
772 	int osr = MICFIL_OSR_DEFAULT;
773 	int ret;
774 
775 	/* 1. Disable the module */
776 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
777 				MICFIL_CTRL1_PDMIEN);
778 	if (ret)
779 		return ret;
780 
781 	/* enable channels */
782 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
783 				 0xFF, ((1 << channels) - 1));
784 	if (ret)
785 		return ret;
786 
787 	ret = fsl_micfil_reparent_rootclk(micfil, rate);
788 	if (ret)
789 		return ret;
790 
791 	ret = clk_set_rate(micfil->mclk, rate * clk_div * osr * 8);
792 	if (ret)
793 		return ret;
794 
795 	ret = micfil_set_quality(micfil);
796 	if (ret)
797 		return ret;
798 
799 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
800 				 MICFIL_CTRL2_CLKDIV | MICFIL_CTRL2_CICOSR,
801 				 FIELD_PREP(MICFIL_CTRL2_CLKDIV, clk_div) |
802 				 FIELD_PREP(MICFIL_CTRL2_CICOSR, 16 - osr));
803 
804 	/* Configure CIC OSR in VADCICOSR */
805 	regmap_update_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
806 			   MICFIL_VAD0_CTRL1_CICOSR,
807 			   FIELD_PREP(MICFIL_VAD0_CTRL1_CICOSR, 16 - osr));
808 
809 	/* Configure source channel in VADCHSEL */
810 	regmap_update_bits(micfil->regmap, REG_MICFIL_VAD0_CTRL1,
811 			   MICFIL_VAD0_CTRL1_CHSEL,
812 			   FIELD_PREP(MICFIL_VAD0_CTRL1_CHSEL, (channels - 1)));
813 
814 	micfil->dma_params_rx.peripheral_config = &micfil->sdmacfg;
815 	micfil->dma_params_rx.peripheral_size = sizeof(micfil->sdmacfg);
816 	micfil->sdmacfg.n_fifos_src = channels;
817 	micfil->sdmacfg.sw_done = true;
818 	micfil->dma_params_rx.maxburst = channels * MICFIL_DMA_MAXBURST_RX;
819 	if (micfil->soc->use_edma)
820 		micfil->dma_params_rx.maxburst = channels;
821 
822 	return 0;
823 }
824 
fsl_micfil_dai_probe(struct snd_soc_dai * cpu_dai)825 static int fsl_micfil_dai_probe(struct snd_soc_dai *cpu_dai)
826 {
827 	struct fsl_micfil *micfil = dev_get_drvdata(cpu_dai->dev);
828 	struct device *dev = cpu_dai->dev;
829 	unsigned int val = 0;
830 	int ret, i;
831 
832 	micfil->quality = QUALITY_VLOW0;
833 	micfil->card = cpu_dai->component->card;
834 
835 	/* set default gain to 2 */
836 	regmap_write(micfil->regmap, REG_MICFIL_OUT_CTRL, 0x22222222);
837 
838 	/* set DC Remover in bypass mode*/
839 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++)
840 		val |= MICFIL_DC_BYPASS << MICFIL_DC_CHX_SHIFT(i);
841 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_DC_CTRL,
842 				 MICFIL_DC_CTRL_CONFIG, val);
843 	if (ret) {
844 		dev_err(dev, "failed to set DC Remover mode bits\n");
845 		return ret;
846 	}
847 	micfil->dc_remover = MICFIL_DC_BYPASS;
848 
849 	snd_soc_dai_init_dma_data(cpu_dai, NULL,
850 				  &micfil->dma_params_rx);
851 
852 	/* FIFO Watermark Control - FIFOWMK*/
853 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_FIFO_CTRL,
854 			MICFIL_FIFO_CTRL_FIFOWMK,
855 			FIELD_PREP(MICFIL_FIFO_CTRL_FIFOWMK, micfil->soc->fifo_depth - 1));
856 	if (ret)
857 		return ret;
858 
859 	return 0;
860 }
861 
fsl_micfil_component_probe(struct snd_soc_component * component)862 static int fsl_micfil_component_probe(struct snd_soc_component *component)
863 {
864 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(component);
865 
866 	if (micfil->soc->volume_sx)
867 		snd_soc_add_component_controls(component, fsl_micfil_volume_sx_controls,
868 					       ARRAY_SIZE(fsl_micfil_volume_sx_controls));
869 	else
870 		snd_soc_add_component_controls(component, fsl_micfil_volume_controls,
871 					       ARRAY_SIZE(fsl_micfil_volume_controls));
872 
873 	return 0;
874 }
875 
876 static const struct snd_soc_dai_ops fsl_micfil_dai_ops = {
877 	.probe		= fsl_micfil_dai_probe,
878 	.startup	= fsl_micfil_startup,
879 	.trigger	= fsl_micfil_trigger,
880 	.hw_params	= fsl_micfil_hw_params,
881 };
882 
883 static struct snd_soc_dai_driver fsl_micfil_dai = {
884 	.capture = {
885 		.stream_name = "CPU-Capture",
886 		.channels_min = 1,
887 		.channels_max = 8,
888 		.rates = SNDRV_PCM_RATE_8000_48000,
889 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
890 	},
891 	.ops = &fsl_micfil_dai_ops,
892 };
893 
894 static const struct snd_soc_component_driver fsl_micfil_component = {
895 	.name		= "fsl-micfil-dai",
896 	.probe		= fsl_micfil_component_probe,
897 	.controls       = fsl_micfil_snd_controls,
898 	.num_controls   = ARRAY_SIZE(fsl_micfil_snd_controls),
899 	.legacy_dai_naming      = 1,
900 };
901 
902 /* REGMAP */
903 static const struct reg_default fsl_micfil_reg_defaults[] = {
904 	{REG_MICFIL_CTRL1,		0x00000000},
905 	{REG_MICFIL_CTRL2,		0x00000000},
906 	{REG_MICFIL_STAT,		0x00000000},
907 	{REG_MICFIL_FIFO_CTRL,		0x00000007},
908 	{REG_MICFIL_FIFO_STAT,		0x00000000},
909 	{REG_MICFIL_DATACH0,		0x00000000},
910 	{REG_MICFIL_DATACH1,		0x00000000},
911 	{REG_MICFIL_DATACH2,		0x00000000},
912 	{REG_MICFIL_DATACH3,		0x00000000},
913 	{REG_MICFIL_DATACH4,		0x00000000},
914 	{REG_MICFIL_DATACH5,		0x00000000},
915 	{REG_MICFIL_DATACH6,		0x00000000},
916 	{REG_MICFIL_DATACH7,		0x00000000},
917 	{REG_MICFIL_DC_CTRL,		0x00000000},
918 	{REG_MICFIL_OUT_CTRL,		0x00000000},
919 	{REG_MICFIL_OUT_STAT,		0x00000000},
920 	{REG_MICFIL_VAD0_CTRL1,		0x00000000},
921 	{REG_MICFIL_VAD0_CTRL2,		0x000A0000},
922 	{REG_MICFIL_VAD0_STAT,		0x00000000},
923 	{REG_MICFIL_VAD0_SCONFIG,	0x00000000},
924 	{REG_MICFIL_VAD0_NCONFIG,	0x80000000},
925 	{REG_MICFIL_VAD0_NDATA,		0x00000000},
926 	{REG_MICFIL_VAD0_ZCD,		0x00000004},
927 };
928 
fsl_micfil_readable_reg(struct device * dev,unsigned int reg)929 static bool fsl_micfil_readable_reg(struct device *dev, unsigned int reg)
930 {
931 	switch (reg) {
932 	case REG_MICFIL_CTRL1:
933 	case REG_MICFIL_CTRL2:
934 	case REG_MICFIL_STAT:
935 	case REG_MICFIL_FIFO_CTRL:
936 	case REG_MICFIL_FIFO_STAT:
937 	case REG_MICFIL_DATACH0:
938 	case REG_MICFIL_DATACH1:
939 	case REG_MICFIL_DATACH2:
940 	case REG_MICFIL_DATACH3:
941 	case REG_MICFIL_DATACH4:
942 	case REG_MICFIL_DATACH5:
943 	case REG_MICFIL_DATACH6:
944 	case REG_MICFIL_DATACH7:
945 	case REG_MICFIL_DC_CTRL:
946 	case REG_MICFIL_OUT_CTRL:
947 	case REG_MICFIL_OUT_STAT:
948 	case REG_MICFIL_FSYNC_CTRL:
949 	case REG_MICFIL_VERID:
950 	case REG_MICFIL_PARAM:
951 	case REG_MICFIL_VAD0_CTRL1:
952 	case REG_MICFIL_VAD0_CTRL2:
953 	case REG_MICFIL_VAD0_STAT:
954 	case REG_MICFIL_VAD0_SCONFIG:
955 	case REG_MICFIL_VAD0_NCONFIG:
956 	case REG_MICFIL_VAD0_NDATA:
957 	case REG_MICFIL_VAD0_ZCD:
958 		return true;
959 	default:
960 		return false;
961 	}
962 }
963 
fsl_micfil_writeable_reg(struct device * dev,unsigned int reg)964 static bool fsl_micfil_writeable_reg(struct device *dev, unsigned int reg)
965 {
966 	switch (reg) {
967 	case REG_MICFIL_CTRL1:
968 	case REG_MICFIL_CTRL2:
969 	case REG_MICFIL_STAT:		/* Write 1 to Clear */
970 	case REG_MICFIL_FIFO_CTRL:
971 	case REG_MICFIL_FIFO_STAT:	/* Write 1 to Clear */
972 	case REG_MICFIL_DC_CTRL:
973 	case REG_MICFIL_OUT_CTRL:
974 	case REG_MICFIL_OUT_STAT:	/* Write 1 to Clear */
975 	case REG_MICFIL_FSYNC_CTRL:
976 	case REG_MICFIL_VAD0_CTRL1:
977 	case REG_MICFIL_VAD0_CTRL2:
978 	case REG_MICFIL_VAD0_STAT:	/* Write 1 to Clear */
979 	case REG_MICFIL_VAD0_SCONFIG:
980 	case REG_MICFIL_VAD0_NCONFIG:
981 	case REG_MICFIL_VAD0_ZCD:
982 		return true;
983 	default:
984 		return false;
985 	}
986 }
987 
fsl_micfil_volatile_reg(struct device * dev,unsigned int reg)988 static bool fsl_micfil_volatile_reg(struct device *dev, unsigned int reg)
989 {
990 	switch (reg) {
991 	case REG_MICFIL_STAT:
992 	case REG_MICFIL_DATACH0:
993 	case REG_MICFIL_DATACH1:
994 	case REG_MICFIL_DATACH2:
995 	case REG_MICFIL_DATACH3:
996 	case REG_MICFIL_DATACH4:
997 	case REG_MICFIL_DATACH5:
998 	case REG_MICFIL_DATACH6:
999 	case REG_MICFIL_DATACH7:
1000 	case REG_MICFIL_VERID:
1001 	case REG_MICFIL_PARAM:
1002 	case REG_MICFIL_VAD0_STAT:
1003 	case REG_MICFIL_VAD0_NDATA:
1004 		return true;
1005 	default:
1006 		return false;
1007 	}
1008 }
1009 
1010 static const struct regmap_config fsl_micfil_regmap_config = {
1011 	.reg_bits = 32,
1012 	.reg_stride = 4,
1013 	.val_bits = 32,
1014 
1015 	.max_register = REG_MICFIL_VAD0_ZCD,
1016 	.reg_defaults = fsl_micfil_reg_defaults,
1017 	.num_reg_defaults = ARRAY_SIZE(fsl_micfil_reg_defaults),
1018 	.readable_reg = fsl_micfil_readable_reg,
1019 	.volatile_reg = fsl_micfil_volatile_reg,
1020 	.writeable_reg = fsl_micfil_writeable_reg,
1021 	.cache_type = REGCACHE_RBTREE,
1022 };
1023 
1024 /* END OF REGMAP */
1025 
micfil_isr(int irq,void * devid)1026 static irqreturn_t micfil_isr(int irq, void *devid)
1027 {
1028 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
1029 	struct platform_device *pdev = micfil->pdev;
1030 	u32 stat_reg;
1031 	u32 fifo_stat_reg;
1032 	u32 ctrl1_reg;
1033 	bool dma_enabled;
1034 	int i;
1035 
1036 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
1037 	regmap_read(micfil->regmap, REG_MICFIL_CTRL1, &ctrl1_reg);
1038 	regmap_read(micfil->regmap, REG_MICFIL_FIFO_STAT, &fifo_stat_reg);
1039 
1040 	dma_enabled = FIELD_GET(MICFIL_CTRL1_DISEL, ctrl1_reg) == MICFIL_CTRL1_DISEL_DMA;
1041 
1042 	/* Channel 0-7 Output Data Flags */
1043 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++) {
1044 		if (stat_reg & MICFIL_STAT_CHXF(i))
1045 			dev_dbg(&pdev->dev,
1046 				"Data available in Data Channel %d\n", i);
1047 		/* if DMA is not enabled, field must be written with 1
1048 		 * to clear
1049 		 */
1050 		if (!dma_enabled)
1051 			regmap_write_bits(micfil->regmap,
1052 					  REG_MICFIL_STAT,
1053 					  MICFIL_STAT_CHXF(i),
1054 					  1);
1055 	}
1056 
1057 	for (i = 0; i < MICFIL_FIFO_NUM; i++) {
1058 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_OVER(i))
1059 			dev_dbg(&pdev->dev,
1060 				"FIFO Overflow Exception flag for channel %d\n",
1061 				i);
1062 
1063 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_UNDER(i))
1064 			dev_dbg(&pdev->dev,
1065 				"FIFO Underflow Exception flag for channel %d\n",
1066 				i);
1067 	}
1068 
1069 	return IRQ_HANDLED;
1070 }
1071 
micfil_err_isr(int irq,void * devid)1072 static irqreturn_t micfil_err_isr(int irq, void *devid)
1073 {
1074 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
1075 	struct platform_device *pdev = micfil->pdev;
1076 	u32 stat_reg;
1077 
1078 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
1079 
1080 	if (stat_reg & MICFIL_STAT_BSY_FIL)
1081 		dev_dbg(&pdev->dev, "isr: Decimation Filter is running\n");
1082 
1083 	if (stat_reg & MICFIL_STAT_FIR_RDY)
1084 		dev_dbg(&pdev->dev, "isr: FIR Filter Data ready\n");
1085 
1086 	if (stat_reg & MICFIL_STAT_LOWFREQF) {
1087 		dev_dbg(&pdev->dev, "isr: ipg_clk_app is too low\n");
1088 		regmap_write_bits(micfil->regmap, REG_MICFIL_STAT,
1089 				  MICFIL_STAT_LOWFREQF, 1);
1090 	}
1091 
1092 	return IRQ_HANDLED;
1093 }
1094 
voice_detected_fn(int irq,void * devid)1095 static irqreturn_t voice_detected_fn(int irq, void *devid)
1096 {
1097 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
1098 	struct snd_kcontrol *kctl;
1099 
1100 	if (!micfil->card)
1101 		return IRQ_HANDLED;
1102 
1103 	kctl = snd_soc_card_get_kcontrol(micfil->card, "VAD Detected");
1104 	if (!kctl)
1105 		return IRQ_HANDLED;
1106 
1107 	if (micfil->vad_detected)
1108 		snd_ctl_notify(micfil->card->snd_card,
1109 			       SNDRV_CTL_EVENT_MASK_VALUE,
1110 			       &kctl->id);
1111 
1112 	return IRQ_HANDLED;
1113 }
1114 
hwvad_isr(int irq,void * devid)1115 static irqreturn_t hwvad_isr(int irq, void *devid)
1116 {
1117 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
1118 	struct device *dev = &micfil->pdev->dev;
1119 	u32 vad0_reg;
1120 	int ret;
1121 
1122 	regmap_read(micfil->regmap, REG_MICFIL_VAD0_STAT, &vad0_reg);
1123 
1124 	/*
1125 	 * The only difference between MICFIL_VAD0_STAT_EF and
1126 	 * MICFIL_VAD0_STAT_IF is that the former requires Write
1127 	 * 1 to Clear. Since both flags are set, it is enough
1128 	 * to only read one of them
1129 	 */
1130 	if (vad0_reg & MICFIL_VAD0_STAT_IF) {
1131 		/* Write 1 to clear */
1132 		regmap_write_bits(micfil->regmap, REG_MICFIL_VAD0_STAT,
1133 				  MICFIL_VAD0_STAT_IF,
1134 				  MICFIL_VAD0_STAT_IF);
1135 
1136 		micfil->vad_detected = 1;
1137 	}
1138 
1139 	ret = fsl_micfil_hwvad_disable(micfil);
1140 	if (ret)
1141 		dev_err(dev, "Failed to disable hwvad\n");
1142 
1143 	return IRQ_WAKE_THREAD;
1144 }
1145 
hwvad_err_isr(int irq,void * devid)1146 static irqreturn_t hwvad_err_isr(int irq, void *devid)
1147 {
1148 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
1149 	struct device *dev = &micfil->pdev->dev;
1150 	u32 vad0_reg;
1151 
1152 	regmap_read(micfil->regmap, REG_MICFIL_VAD0_STAT, &vad0_reg);
1153 
1154 	if (vad0_reg & MICFIL_VAD0_STAT_INSATF)
1155 		dev_dbg(dev, "voice activity input overflow/underflow detected\n");
1156 
1157 	return IRQ_HANDLED;
1158 }
1159 
1160 static int fsl_micfil_runtime_suspend(struct device *dev);
1161 static int fsl_micfil_runtime_resume(struct device *dev);
1162 
fsl_micfil_probe(struct platform_device * pdev)1163 static int fsl_micfil_probe(struct platform_device *pdev)
1164 {
1165 	struct device_node *np = pdev->dev.of_node;
1166 	struct fsl_micfil *micfil;
1167 	struct resource *res;
1168 	void __iomem *regs;
1169 	int ret, i;
1170 
1171 	micfil = devm_kzalloc(&pdev->dev, sizeof(*micfil), GFP_KERNEL);
1172 	if (!micfil)
1173 		return -ENOMEM;
1174 
1175 	micfil->pdev = pdev;
1176 	strscpy(micfil->name, np->name, sizeof(micfil->name));
1177 
1178 	micfil->soc = of_device_get_match_data(&pdev->dev);
1179 
1180 	/* ipg_clk is used to control the registers
1181 	 * ipg_clk_app is used to operate the filter
1182 	 */
1183 	micfil->mclk = devm_clk_get(&pdev->dev, "ipg_clk_app");
1184 	if (IS_ERR(micfil->mclk)) {
1185 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
1186 			PTR_ERR(micfil->mclk));
1187 		return PTR_ERR(micfil->mclk);
1188 	}
1189 
1190 	micfil->busclk = devm_clk_get(&pdev->dev, "ipg_clk");
1191 	if (IS_ERR(micfil->busclk)) {
1192 		dev_err(&pdev->dev, "failed to get ipg clock: %ld\n",
1193 			PTR_ERR(micfil->busclk));
1194 		return PTR_ERR(micfil->busclk);
1195 	}
1196 
1197 	fsl_asoc_get_pll_clocks(&pdev->dev, &micfil->pll8k_clk,
1198 				&micfil->pll11k_clk);
1199 
1200 	micfil->clk_src[MICFIL_AUDIO_PLL1] = micfil->pll8k_clk;
1201 	micfil->clk_src[MICFIL_AUDIO_PLL2] = micfil->pll11k_clk;
1202 	micfil->clk_src[MICFIL_CLK_EXT3] = devm_clk_get(&pdev->dev, "clkext3");
1203 	if (IS_ERR(micfil->clk_src[MICFIL_CLK_EXT3]))
1204 		micfil->clk_src[MICFIL_CLK_EXT3] = NULL;
1205 
1206 	/* init regmap */
1207 	regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1208 	if (IS_ERR(regs))
1209 		return PTR_ERR(regs);
1210 
1211 	micfil->regmap = devm_regmap_init_mmio(&pdev->dev,
1212 					       regs,
1213 					       &fsl_micfil_regmap_config);
1214 	if (IS_ERR(micfil->regmap)) {
1215 		dev_err(&pdev->dev, "failed to init MICFIL regmap: %ld\n",
1216 			PTR_ERR(micfil->regmap));
1217 		return PTR_ERR(micfil->regmap);
1218 	}
1219 
1220 	/* dataline mask for RX */
1221 	ret = of_property_read_u32_index(np,
1222 					 "fsl,dataline",
1223 					 0,
1224 					 &micfil->dataline);
1225 	if (ret)
1226 		micfil->dataline = 1;
1227 
1228 	if (micfil->dataline & ~micfil->soc->dataline) {
1229 		dev_err(&pdev->dev, "dataline setting error, Mask is 0x%X\n",
1230 			micfil->soc->dataline);
1231 		return -EINVAL;
1232 	}
1233 
1234 	/* get IRQs */
1235 	for (i = 0; i < MICFIL_IRQ_LINES; i++) {
1236 		micfil->irq[i] = platform_get_irq(pdev, i);
1237 		if (micfil->irq[i] < 0)
1238 			return micfil->irq[i];
1239 	}
1240 
1241 	/* Digital Microphone interface interrupt */
1242 	ret = devm_request_irq(&pdev->dev, micfil->irq[0],
1243 			       micfil_isr, IRQF_SHARED,
1244 			       micfil->name, micfil);
1245 	if (ret) {
1246 		dev_err(&pdev->dev, "failed to claim mic interface irq %u\n",
1247 			micfil->irq[0]);
1248 		return ret;
1249 	}
1250 
1251 	/* Digital Microphone interface error interrupt */
1252 	ret = devm_request_irq(&pdev->dev, micfil->irq[1],
1253 			       micfil_err_isr, IRQF_SHARED,
1254 			       micfil->name, micfil);
1255 	if (ret) {
1256 		dev_err(&pdev->dev, "failed to claim mic interface error irq %u\n",
1257 			micfil->irq[1]);
1258 		return ret;
1259 	}
1260 
1261 	/* Digital Microphone interface voice activity detector event */
1262 	ret = devm_request_threaded_irq(&pdev->dev, micfil->irq[2],
1263 					hwvad_isr, voice_detected_fn,
1264 					IRQF_SHARED, micfil->name, micfil);
1265 	if (ret) {
1266 		dev_err(&pdev->dev, "failed to claim hwvad event irq %u\n",
1267 			micfil->irq[0]);
1268 		return ret;
1269 	}
1270 
1271 	/* Digital Microphone interface voice activity detector error */
1272 	ret = devm_request_irq(&pdev->dev, micfil->irq[3],
1273 			       hwvad_err_isr, IRQF_SHARED,
1274 			       micfil->name, micfil);
1275 	if (ret) {
1276 		dev_err(&pdev->dev, "failed to claim hwvad error irq %u\n",
1277 			micfil->irq[1]);
1278 		return ret;
1279 	}
1280 
1281 	micfil->dma_params_rx.chan_name = "rx";
1282 	micfil->dma_params_rx.addr = res->start + REG_MICFIL_DATACH0;
1283 	micfil->dma_params_rx.maxburst = MICFIL_DMA_MAXBURST_RX;
1284 
1285 	platform_set_drvdata(pdev, micfil);
1286 
1287 	pm_runtime_enable(&pdev->dev);
1288 	if (!pm_runtime_enabled(&pdev->dev)) {
1289 		ret = fsl_micfil_runtime_resume(&pdev->dev);
1290 		if (ret)
1291 			goto err_pm_disable;
1292 	}
1293 
1294 	ret = pm_runtime_resume_and_get(&pdev->dev);
1295 	if (ret < 0)
1296 		goto err_pm_get_sync;
1297 
1298 	/* Get micfil version */
1299 	ret = fsl_micfil_use_verid(&pdev->dev);
1300 	if (ret < 0)
1301 		dev_warn(&pdev->dev, "Error reading MICFIL version: %d\n", ret);
1302 
1303 	ret = pm_runtime_put_sync(&pdev->dev);
1304 	if (ret < 0 && ret != -ENOSYS)
1305 		goto err_pm_get_sync;
1306 
1307 	regcache_cache_only(micfil->regmap, true);
1308 
1309 	/*
1310 	 * Register platform component before registering cpu dai for there
1311 	 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
1312 	 */
1313 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1314 	if (ret) {
1315 		dev_err(&pdev->dev, "failed to pcm register\n");
1316 		goto err_pm_disable;
1317 	}
1318 
1319 	fsl_micfil_dai.capture.formats = micfil->soc->formats;
1320 
1321 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_micfil_component,
1322 					      &fsl_micfil_dai, 1);
1323 	if (ret) {
1324 		dev_err(&pdev->dev, "failed to register component %s\n",
1325 			fsl_micfil_component.name);
1326 		goto err_pm_disable;
1327 	}
1328 
1329 	return ret;
1330 
1331 err_pm_get_sync:
1332 	if (!pm_runtime_status_suspended(&pdev->dev))
1333 		fsl_micfil_runtime_suspend(&pdev->dev);
1334 err_pm_disable:
1335 	pm_runtime_disable(&pdev->dev);
1336 
1337 	return ret;
1338 }
1339 
fsl_micfil_remove(struct platform_device * pdev)1340 static void fsl_micfil_remove(struct platform_device *pdev)
1341 {
1342 	pm_runtime_disable(&pdev->dev);
1343 }
1344 
fsl_micfil_runtime_suspend(struct device * dev)1345 static int fsl_micfil_runtime_suspend(struct device *dev)
1346 {
1347 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
1348 
1349 	regcache_cache_only(micfil->regmap, true);
1350 
1351 	clk_disable_unprepare(micfil->mclk);
1352 	clk_disable_unprepare(micfil->busclk);
1353 
1354 	return 0;
1355 }
1356 
fsl_micfil_runtime_resume(struct device * dev)1357 static int fsl_micfil_runtime_resume(struct device *dev)
1358 {
1359 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
1360 	int ret;
1361 
1362 	ret = clk_prepare_enable(micfil->busclk);
1363 	if (ret < 0)
1364 		return ret;
1365 
1366 	ret = clk_prepare_enable(micfil->mclk);
1367 	if (ret < 0) {
1368 		clk_disable_unprepare(micfil->busclk);
1369 		return ret;
1370 	}
1371 
1372 	regcache_cache_only(micfil->regmap, false);
1373 	regcache_mark_dirty(micfil->regmap);
1374 	regcache_sync(micfil->regmap);
1375 
1376 	return 0;
1377 }
1378 
1379 static const struct dev_pm_ops fsl_micfil_pm_ops = {
1380 	SET_RUNTIME_PM_OPS(fsl_micfil_runtime_suspend,
1381 			   fsl_micfil_runtime_resume,
1382 			   NULL)
1383 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1384 				pm_runtime_force_resume)
1385 };
1386 
1387 static struct platform_driver fsl_micfil_driver = {
1388 	.probe = fsl_micfil_probe,
1389 	.remove_new = fsl_micfil_remove,
1390 	.driver = {
1391 		.name = "fsl-micfil-dai",
1392 		.pm = &fsl_micfil_pm_ops,
1393 		.of_match_table = fsl_micfil_dt_ids,
1394 	},
1395 };
1396 module_platform_driver(fsl_micfil_driver);
1397 
1398 MODULE_AUTHOR("Cosmin-Gabriel Samoila <cosmin.samoila@nxp.com>");
1399 MODULE_DESCRIPTION("NXP PDM Microphone Interface (MICFIL) driver");
1400 MODULE_LICENSE("Dual BSD/GPL");
1401