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