xref: /linux/sound/soc/fsl/fsl_easrc.c (revision 9e4e86a604dfd06402933467578c4b79f5412b2c)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2019 NXP
3 
4 #include <linux/atomic.h>
5 #include <linux/clk.h>
6 #include <linux/device.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/firmware.h>
9 #include <linux/interrupt.h>
10 #include <linux/kobject.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/miscdevice.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/of_platform.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/sched/signal.h>
21 #include <linux/sysfs.h>
22 #include <linux/types.h>
23 #include <linux/gcd.h>
24 #include <sound/dmaengine_pcm.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/tlv.h>
29 #include <sound/core.h>
30 
31 #include "fsl_easrc.h"
32 #include "imx-pcm.h"
33 
34 #define FSL_EASRC_FORMATS       (SNDRV_PCM_FMTBIT_S16_LE | \
35 				 SNDRV_PCM_FMTBIT_U16_LE | \
36 				 SNDRV_PCM_FMTBIT_S24_LE | \
37 				 SNDRV_PCM_FMTBIT_S24_3LE | \
38 				 SNDRV_PCM_FMTBIT_U24_LE | \
39 				 SNDRV_PCM_FMTBIT_U24_3LE | \
40 				 SNDRV_PCM_FMTBIT_S32_LE | \
41 				 SNDRV_PCM_FMTBIT_U32_LE | \
42 				 SNDRV_PCM_FMTBIT_S20_3LE | \
43 				 SNDRV_PCM_FMTBIT_U20_3LE | \
44 				 SNDRV_PCM_FMTBIT_FLOAT_LE)
45 
fsl_easrc_iec958_put_bits(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)46 static int fsl_easrc_iec958_put_bits(struct snd_kcontrol *kcontrol,
47 				     struct snd_ctl_elem_value *ucontrol)
48 {
49 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
50 	struct fsl_asrc *easrc = snd_soc_component_get_drvdata(comp);
51 	struct fsl_easrc_priv *easrc_priv = easrc->private;
52 	struct soc_mreg_control *mc =
53 		(struct soc_mreg_control *)kcontrol->private_value;
54 	unsigned int regval = ucontrol->value.integer.value[0];
55 	int ret;
56 
57 	if (regval < EASRC_WIDTH_16_BIT || regval > EASRC_WIDTH_24_BIT)
58 		return -EINVAL;
59 
60 	ret = (easrc_priv->bps_iec958[mc->regbase] != regval);
61 
62 	easrc_priv->bps_iec958[mc->regbase] = regval;
63 
64 	return ret;
65 }
66 
fsl_easrc_iec958_get_bits(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)67 static int fsl_easrc_iec958_get_bits(struct snd_kcontrol *kcontrol,
68 				     struct snd_ctl_elem_value *ucontrol)
69 {
70 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
71 	struct fsl_asrc *easrc = snd_soc_component_get_drvdata(comp);
72 	struct fsl_easrc_priv *easrc_priv = easrc->private;
73 	struct soc_mreg_control *mc =
74 		(struct soc_mreg_control *)kcontrol->private_value;
75 
76 	ucontrol->value.integer.value[0] = easrc_priv->bps_iec958[mc->regbase];
77 
78 	return 0;
79 }
80 
fsl_easrc_iec958_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)81 static int fsl_easrc_iec958_info(struct snd_kcontrol *kcontrol,
82 				 struct snd_ctl_elem_info *uinfo)
83 {
84 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
85 	uinfo->count = 1;
86 	return 0;
87 }
88 
fsl_easrc_get_reg(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)89 static int fsl_easrc_get_reg(struct snd_kcontrol *kcontrol,
90 			     struct snd_ctl_elem_value *ucontrol)
91 {
92 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
93 	struct soc_mreg_control *mc =
94 		(struct soc_mreg_control *)kcontrol->private_value;
95 	struct fsl_asrc *easrc = snd_soc_component_get_drvdata(component);
96 	unsigned int *regval = (unsigned int *)ucontrol->value.iec958.status;
97 	int ret;
98 
99 	ret = regmap_read(easrc->regmap, REG_EASRC_CS0(mc->regbase), &regval[0]);
100 	if (ret)
101 		return ret;
102 
103 	ret = regmap_read(easrc->regmap, REG_EASRC_CS1(mc->regbase), &regval[1]);
104 	if (ret)
105 		return ret;
106 
107 	ret = regmap_read(easrc->regmap, REG_EASRC_CS2(mc->regbase), &regval[2]);
108 	if (ret)
109 		return ret;
110 
111 	ret = regmap_read(easrc->regmap, REG_EASRC_CS3(mc->regbase), &regval[3]);
112 	if (ret)
113 		return ret;
114 
115 	ret = regmap_read(easrc->regmap, REG_EASRC_CS4(mc->regbase), &regval[4]);
116 	if (ret)
117 		return ret;
118 
119 	ret = regmap_read(easrc->regmap, REG_EASRC_CS5(mc->regbase), &regval[5]);
120 	if (ret)
121 		return ret;
122 
123 	return 0;
124 }
125 
fsl_easrc_set_reg(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)126 static int fsl_easrc_set_reg(struct snd_kcontrol *kcontrol,
127 			     struct snd_ctl_elem_value *ucontrol)
128 {
129 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
130 	struct soc_mreg_control *mc =
131 		(struct soc_mreg_control *)kcontrol->private_value;
132 	struct fsl_asrc *easrc = snd_soc_component_get_drvdata(component);
133 	unsigned int *regval = (unsigned int *)ucontrol->value.iec958.status;
134 	bool changed, changed_all = false;
135 	int ret;
136 
137 	ret = pm_runtime_resume_and_get(component->dev);
138 	if (ret)
139 		return ret;
140 
141 	ret = regmap_update_bits_check(easrc->regmap, REG_EASRC_CS0(mc->regbase),
142 				       GENMASK(31, 0), regval[0], &changed);
143 	if (ret != 0)
144 		goto err;
145 	changed_all |= changed;
146 
147 	ret = regmap_update_bits_check(easrc->regmap, REG_EASRC_CS1(mc->regbase),
148 				       GENMASK(31, 0), regval[1], &changed);
149 	if (ret != 0)
150 		goto err;
151 	changed_all |= changed;
152 
153 	ret = regmap_update_bits_check(easrc->regmap, REG_EASRC_CS2(mc->regbase),
154 				       GENMASK(31, 0), regval[2], &changed);
155 	if (ret != 0)
156 		goto err;
157 	changed_all |= changed;
158 
159 	ret = regmap_update_bits_check(easrc->regmap, REG_EASRC_CS3(mc->regbase),
160 				       GENMASK(31, 0), regval[3], &changed);
161 	if (ret != 0)
162 		goto err;
163 	changed_all |= changed;
164 
165 	ret = regmap_update_bits_check(easrc->regmap, REG_EASRC_CS4(mc->regbase),
166 				       GENMASK(31, 0), regval[4], &changed);
167 	if (ret != 0)
168 		goto err;
169 	changed_all |= changed;
170 
171 	ret = regmap_update_bits_check(easrc->regmap, REG_EASRC_CS5(mc->regbase),
172 				       GENMASK(31, 0), regval[5], &changed);
173 	if (ret != 0)
174 		goto err;
175 	changed_all |= changed;
176 err:
177 	pm_runtime_put_autosuspend(component->dev);
178 
179 	if (ret != 0)
180 		return ret;
181 	else
182 		return changed_all;
183 }
184 
185 #define SOC_SINGLE_REG_RW(xname, xreg) \
186 {	.iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = (xname), \
187 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
188 	.info = fsl_easrc_iec958_info, .get = fsl_easrc_get_reg, \
189 	.put = fsl_easrc_set_reg, \
190 	.private_value = (unsigned long)&(struct soc_mreg_control) \
191 		{ .regbase = xreg, .regcount = 1, .nbits = 32, \
192 		  .invert = 0, .min = 0, .max = 0xffffffff, } }
193 
194 #define SOC_SINGLE_VAL_RW(xname, xreg) \
195 {	.iface = SNDRV_CTL_ELEM_IFACE_PCM, .name = (xname), \
196 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
197 	.info = snd_soc_info_xr_sx, .get = fsl_easrc_iec958_get_bits, \
198 	.put = fsl_easrc_iec958_put_bits, \
199 	.private_value = (unsigned long)&(struct soc_mreg_control) \
200 		{ .regbase = xreg, .regcount = 1, .nbits = 32, \
201 		  .invert = 0, .min = 0, .max = 2, } }
202 
203 static const struct snd_kcontrol_new fsl_easrc_snd_controls[] = {
204 	SOC_SINGLE("Context 0 Dither Switch", REG_EASRC_COC(0), 0, 1, 0),
205 	SOC_SINGLE("Context 1 Dither Switch", REG_EASRC_COC(1), 0, 1, 0),
206 	SOC_SINGLE("Context 2 Dither Switch", REG_EASRC_COC(2), 0, 1, 0),
207 	SOC_SINGLE("Context 3 Dither Switch", REG_EASRC_COC(3), 0, 1, 0),
208 
209 	SOC_SINGLE("Context 0 IEC958 Validity", REG_EASRC_COC(0), 2, 1, 0),
210 	SOC_SINGLE("Context 1 IEC958 Validity", REG_EASRC_COC(1), 2, 1, 0),
211 	SOC_SINGLE("Context 2 IEC958 Validity", REG_EASRC_COC(2), 2, 1, 0),
212 	SOC_SINGLE("Context 3 IEC958 Validity", REG_EASRC_COC(3), 2, 1, 0),
213 
214 	SOC_SINGLE_VAL_RW("Context 0 IEC958 Bits Per Sample", 0),
215 	SOC_SINGLE_VAL_RW("Context 1 IEC958 Bits Per Sample", 1),
216 	SOC_SINGLE_VAL_RW("Context 2 IEC958 Bits Per Sample", 2),
217 	SOC_SINGLE_VAL_RW("Context 3 IEC958 Bits Per Sample", 3),
218 
219 	SOC_SINGLE_REG_RW("Context 0 IEC958 CS", 0),
220 	SOC_SINGLE_REG_RW("Context 1 IEC958 CS", 1),
221 	SOC_SINGLE_REG_RW("Context 2 IEC958 CS", 2),
222 	SOC_SINGLE_REG_RW("Context 3 IEC958 CS", 3),
223 };
224 
225 /*
226  * fsl_easrc_set_rs_ratio
227  *
228  * According to the resample taps, calculate the resample ratio
229  * ratio = in_rate / out_rate
230  */
fsl_easrc_set_rs_ratio(struct fsl_asrc_pair * ctx)231 static int fsl_easrc_set_rs_ratio(struct fsl_asrc_pair *ctx)
232 {
233 	struct fsl_asrc *easrc = ctx->asrc;
234 	struct fsl_easrc_priv *easrc_priv = easrc->private;
235 	struct fsl_easrc_ctx_priv *ctx_priv = ctx->private;
236 	unsigned int in_rate = ctx_priv->in_params.norm_rate;
237 	unsigned int out_rate = ctx_priv->out_params.norm_rate;
238 	unsigned int frac_bits;
239 	u64 val;
240 	u32 *r;
241 
242 	switch (easrc_priv->rs_num_taps) {
243 	case EASRC_RS_32_TAPS:
244 		/* integer bits = 5; */
245 		frac_bits = 39;
246 		break;
247 	case EASRC_RS_64_TAPS:
248 		/* integer bits = 6; */
249 		frac_bits = 38;
250 		break;
251 	case EASRC_RS_128_TAPS:
252 		/* integer bits = 7; */
253 		frac_bits = 37;
254 		break;
255 	default:
256 		return -EINVAL;
257 	}
258 
259 	val = (u64)in_rate << frac_bits;
260 	do_div(val, out_rate);
261 	r = (uint32_t *)&val;
262 
263 	if (r[1] & 0xFFFFF000) {
264 		dev_err(&easrc->pdev->dev, "ratio exceed range\n");
265 		return -EINVAL;
266 	}
267 
268 	regmap_write(easrc->regmap, REG_EASRC_RRL(ctx->index),
269 		     EASRC_RRL_RS_RL(r[0]));
270 	regmap_write(easrc->regmap, REG_EASRC_RRH(ctx->index),
271 		     EASRC_RRH_RS_RH(r[1]));
272 
273 	return 0;
274 }
275 
276 /* Normalize input and output sample rates */
fsl_easrc_normalize_rates(struct fsl_asrc_pair * ctx)277 static void fsl_easrc_normalize_rates(struct fsl_asrc_pair *ctx)
278 {
279 	struct fsl_easrc_ctx_priv *ctx_priv;
280 	int a, b;
281 
282 	if (!ctx)
283 		return;
284 
285 	ctx_priv = ctx->private;
286 
287 	a = ctx_priv->in_params.sample_rate;
288 	b = ctx_priv->out_params.sample_rate;
289 
290 	a = gcd(a, b);
291 
292 	/* Divide by gcd to normalize the rate */
293 	ctx_priv->in_params.norm_rate = ctx_priv->in_params.sample_rate / a;
294 	ctx_priv->out_params.norm_rate = ctx_priv->out_params.sample_rate / a;
295 }
296 
297 /* Resets the pointer of the coeff memory pointers */
fsl_easrc_coeff_mem_ptr_reset(struct fsl_asrc * easrc,unsigned int ctx_id,int mem_type)298 static int fsl_easrc_coeff_mem_ptr_reset(struct fsl_asrc *easrc,
299 					 unsigned int ctx_id, int mem_type)
300 {
301 	struct device *dev;
302 	u32 reg, mask, val;
303 
304 	if (!easrc)
305 		return -ENODEV;
306 
307 	dev = &easrc->pdev->dev;
308 
309 	switch (mem_type) {
310 	case EASRC_PF_COEFF_MEM:
311 		/* This resets the prefilter memory pointer addr */
312 		if (ctx_id >= EASRC_CTX_MAX_NUM) {
313 			dev_err(dev, "Invalid context id[%d]\n", ctx_id);
314 			return -EINVAL;
315 		}
316 
317 		reg = REG_EASRC_CCE1(ctx_id);
318 		mask = EASRC_CCE1_COEF_MEM_RST_MASK;
319 		val = EASRC_CCE1_COEF_MEM_RST;
320 		break;
321 	case EASRC_RS_COEFF_MEM:
322 		/* This resets the resampling memory pointer addr */
323 		reg = REG_EASRC_CRCC;
324 		mask = EASRC_CRCC_RS_CPR_MASK;
325 		val = EASRC_CRCC_RS_CPR;
326 		break;
327 	default:
328 		dev_err(dev, "Unknown memory type\n");
329 		return -EINVAL;
330 	}
331 
332 	/*
333 	 * To reset the write pointer back to zero, the register field
334 	 * ASRC_CTX_CTRL_EXT1x[PF_COEFF_MEM_RST] can be toggled from
335 	 * 0x0 to 0x1 to 0x0.
336 	 */
337 	regmap_update_bits(easrc->regmap, reg, mask, 0);
338 	regmap_update_bits(easrc->regmap, reg, mask, val);
339 	regmap_update_bits(easrc->regmap, reg, mask, 0);
340 
341 	return 0;
342 }
343 
bits_taps_to_val(unsigned int t)344 static inline uint32_t bits_taps_to_val(unsigned int t)
345 {
346 	switch (t) {
347 	case EASRC_RS_32_TAPS:
348 		return 32;
349 	case EASRC_RS_64_TAPS:
350 		return 64;
351 	case EASRC_RS_128_TAPS:
352 		return 128;
353 	}
354 
355 	return 0;
356 }
357 
fsl_easrc_resampler_config(struct fsl_asrc * easrc)358 static int fsl_easrc_resampler_config(struct fsl_asrc *easrc)
359 {
360 	struct device *dev = &easrc->pdev->dev;
361 	struct fsl_easrc_priv *easrc_priv = easrc->private;
362 	struct asrc_firmware_hdr *hdr =  easrc_priv->firmware_hdr;
363 	struct interp_params *interp = easrc_priv->interp;
364 	struct interp_params *selected_interp = NULL;
365 	unsigned int num_coeff;
366 	unsigned int i;
367 	u64 *coef;
368 	u32 *r;
369 	int ret;
370 
371 	if (!hdr) {
372 		dev_err(dev, "firmware not loaded!\n");
373 		return -ENODEV;
374 	}
375 
376 	for (i = 0; i < hdr->interp_scen; i++) {
377 		if ((interp[i].num_taps - 1) !=
378 		    bits_taps_to_val(easrc_priv->rs_num_taps))
379 			continue;
380 
381 		coef = interp[i].coeff;
382 		selected_interp = &interp[i];
383 		dev_dbg(dev, "Selected interp_filter: %u taps - %u phases\n",
384 			selected_interp->num_taps,
385 			selected_interp->num_phases);
386 		break;
387 	}
388 
389 	if (!selected_interp) {
390 		dev_err(dev, "failed to get interpreter configuration\n");
391 		return -EINVAL;
392 	}
393 
394 	/*
395 	 * RS_LOW - first half of center tap of the sinc function
396 	 * RS_HIGH - second half of center tap of the sinc function
397 	 * This is due to the fact the resampling function must be
398 	 * symetrical - i.e. odd number of taps
399 	 */
400 	r = (uint32_t *)&selected_interp->center_tap;
401 	regmap_write(easrc->regmap, REG_EASRC_RCTCL, EASRC_RCTCL_RS_CL(r[0]));
402 	regmap_write(easrc->regmap, REG_EASRC_RCTCH, EASRC_RCTCH_RS_CH(r[1]));
403 
404 	/*
405 	 * Write Number of Resampling Coefficient Taps
406 	 * 00b - 32-Tap Resampling Filter
407 	 * 01b - 64-Tap Resampling Filter
408 	 * 10b - 128-Tap Resampling Filter
409 	 * 11b - N/A
410 	 */
411 	regmap_update_bits(easrc->regmap, REG_EASRC_CRCC,
412 			   EASRC_CRCC_RS_TAPS_MASK,
413 			   EASRC_CRCC_RS_TAPS(easrc_priv->rs_num_taps));
414 
415 	/* Reset prefilter coefficient pointer back to 0 */
416 	ret = fsl_easrc_coeff_mem_ptr_reset(easrc, 0, EASRC_RS_COEFF_MEM);
417 	if (ret)
418 		return ret;
419 
420 	/*
421 	 * When the filter is programmed to run in:
422 	 * 32-tap mode, 16-taps, 128-phases 4-coefficients per phase
423 	 * 64-tap mode, 32-taps, 64-phases 4-coefficients per phase
424 	 * 128-tap mode, 64-taps, 32-phases 4-coefficients per phase
425 	 * This means the number of writes is constant no matter
426 	 * the mode we are using
427 	 */
428 	num_coeff = 16 * 128 * 4;
429 
430 	for (i = 0; i < num_coeff; i++) {
431 		r = (uint32_t *)&coef[i];
432 		regmap_write(easrc->regmap, REG_EASRC_CRCM,
433 			     EASRC_CRCM_RS_CWD(r[0]));
434 		regmap_write(easrc->regmap, REG_EASRC_CRCM,
435 			     EASRC_CRCM_RS_CWD(r[1]));
436 	}
437 
438 	return 0;
439 }
440 
441 /**
442  *  fsl_easrc_normalize_filter - Scale filter coefficients (64 bits float)
443  *  For input float32 normalized range (1.0,-1.0) -> output int[16,24,32]:
444  *      scale it by multiplying filter coefficients by 2^31
445  *  For input int[16, 24, 32] -> output float32
446  *      scale it by multiplying filter coefficients by 2^-15, 2^-23, 2^-31
447  *  input:
448  *      @easrc:  Structure pointer of fsl_asrc
449  *      @infilter : Pointer to non-scaled input filter
450  *      @shift:  The multiply factor
451  *  output:
452  *      @outfilter: scaled filter
453  */
fsl_easrc_normalize_filter(struct fsl_asrc * easrc,u64 * infilter,u64 * outfilter,int shift)454 static int fsl_easrc_normalize_filter(struct fsl_asrc *easrc,
455 				      u64 *infilter,
456 				      u64 *outfilter,
457 				      int shift)
458 {
459 	struct device *dev = &easrc->pdev->dev;
460 	u64 coef = *infilter;
461 	s64 exp  = (coef & 0x7ff0000000000000ll) >> 52;
462 	u64 outcoef;
463 
464 	/*
465 	 * If exponent is zero (value == 0), or 7ff (value == NaNs)
466 	 * dont touch the content
467 	 */
468 	if (exp == 0 || exp == 0x7ff) {
469 		*outfilter = coef;
470 		return 0;
471 	}
472 
473 	/* coef * 2^shift ==> exp + shift */
474 	exp += shift;
475 
476 	if ((shift > 0 && exp >= 0x7ff) || (shift < 0 && exp <= 0)) {
477 		dev_err(dev, "coef out of range\n");
478 		return -EINVAL;
479 	}
480 
481 	outcoef = (u64)(coef & 0x800FFFFFFFFFFFFFll) + ((u64)exp << 52);
482 	*outfilter = outcoef;
483 
484 	return 0;
485 }
486 
fsl_easrc_write_pf_coeff_mem(struct fsl_asrc * easrc,int ctx_id,u64 * coef,int n_taps,int shift)487 static int fsl_easrc_write_pf_coeff_mem(struct fsl_asrc *easrc, int ctx_id,
488 					u64 *coef, int n_taps, int shift)
489 {
490 	struct device *dev = &easrc->pdev->dev;
491 	int ret = 0;
492 	int i;
493 	u32 *r;
494 	u64 tmp;
495 
496 	/* If STx_NUM_TAPS is set to 0x0 then return */
497 	if (!n_taps)
498 		return 0;
499 
500 	if (!coef) {
501 		dev_err(dev, "coef table is NULL\n");
502 		return -EINVAL;
503 	}
504 
505 	/*
506 	 * When switching between stages, the address pointer
507 	 * should be reset back to 0x0 before performing a write
508 	 */
509 	ret = fsl_easrc_coeff_mem_ptr_reset(easrc, ctx_id, EASRC_PF_COEFF_MEM);
510 	if (ret)
511 		return ret;
512 
513 	for (i = 0; i < (n_taps + 1) / 2; i++) {
514 		ret = fsl_easrc_normalize_filter(easrc, &coef[i], &tmp, shift);
515 		if (ret)
516 			return ret;
517 
518 		r = (uint32_t *)&tmp;
519 		regmap_write(easrc->regmap, REG_EASRC_PCF(ctx_id),
520 			     EASRC_PCF_CD(r[0]));
521 		regmap_write(easrc->regmap, REG_EASRC_PCF(ctx_id),
522 			     EASRC_PCF_CD(r[1]));
523 	}
524 
525 	return 0;
526 }
527 
fsl_easrc_prefilter_config(struct fsl_asrc * easrc,unsigned int ctx_id)528 static int fsl_easrc_prefilter_config(struct fsl_asrc *easrc,
529 				      unsigned int ctx_id)
530 {
531 	struct prefil_params *prefil, *selected_prefil = NULL;
532 	struct fsl_easrc_ctx_priv *ctx_priv;
533 	struct fsl_easrc_priv *easrc_priv;
534 	struct asrc_firmware_hdr *hdr;
535 	struct fsl_asrc_pair *ctx;
536 	struct device *dev;
537 	u32 inrate, outrate, offset = 0;
538 	u32 in_s_rate, out_s_rate;
539 	snd_pcm_format_t in_s_fmt, out_s_fmt;
540 	int ret, i;
541 
542 	if (!easrc)
543 		return -ENODEV;
544 
545 	dev = &easrc->pdev->dev;
546 
547 	if (ctx_id >= EASRC_CTX_MAX_NUM) {
548 		dev_err(dev, "Invalid context id[%d]\n", ctx_id);
549 		return -EINVAL;
550 	}
551 
552 	easrc_priv = easrc->private;
553 
554 	ctx = easrc->pair[ctx_id];
555 	ctx_priv = ctx->private;
556 
557 	in_s_rate = ctx_priv->in_params.sample_rate;
558 	out_s_rate = ctx_priv->out_params.sample_rate;
559 	in_s_fmt = ctx_priv->in_params.sample_format;
560 	out_s_fmt = ctx_priv->out_params.sample_format;
561 
562 	ctx_priv->in_filled_sample = bits_taps_to_val(easrc_priv->rs_num_taps) / 2;
563 	ctx_priv->out_missed_sample = ctx_priv->in_filled_sample * out_s_rate / in_s_rate;
564 
565 	ctx_priv->st1_num_taps = 0;
566 	ctx_priv->st2_num_taps = 0;
567 
568 	regmap_write(easrc->regmap, REG_EASRC_CCE1(ctx_id), 0);
569 	regmap_write(easrc->regmap, REG_EASRC_CCE2(ctx_id), 0);
570 
571 	/*
572 	 * The audio float point data range is (-1, 1), the asrc would output
573 	 * all zero for float point input and integer output case, that is to
574 	 * drop the fractional part of the data directly.
575 	 *
576 	 * In order to support float to int conversion or int to float
577 	 * conversion we need to do special operation on the coefficient to
578 	 * enlarge/reduce the data to the expected range.
579 	 *
580 	 * For float to int case:
581 	 * Up sampling:
582 	 * 1. Create a 1 tap filter with center tap (only tap) of 2^31
583 	 *    in 64 bits floating point.
584 	 *    double value = (double)(((uint64_t)1) << 31)
585 	 * 2. Program 1 tap prefilter with center tap above.
586 	 *
587 	 * Down sampling,
588 	 * 1. If the filter is single stage filter, add "shift" to the exponent
589 	 *    of stage 1 coefficients.
590 	 * 2. If the filter is two stage filter , add "shift" to the exponent
591 	 *    of stage 2 coefficients.
592 	 *
593 	 * The "shift" is 31, same for int16, int24, int32 case.
594 	 *
595 	 * For int to float case:
596 	 * Up sampling:
597 	 * 1. Create a 1 tap filter with center tap (only tap) of 2^-31
598 	 *    in 64 bits floating point.
599 	 * 2. Program 1 tap prefilter with center tap above.
600 	 *
601 	 * Down sampling,
602 	 * 1. If the filter is single stage filter, subtract "shift" to the
603 	 *    exponent of stage 1 coefficients.
604 	 * 2. If the filter is two stage filter , subtract "shift" to the
605 	 *    exponent of stage 2 coefficients.
606 	 *
607 	 * The "shift" is 15,23,31, different for int16, int24, int32 case.
608 	 *
609 	 */
610 	if (out_s_rate >= in_s_rate) {
611 		if (out_s_rate == in_s_rate)
612 			regmap_update_bits(easrc->regmap,
613 					   REG_EASRC_CCE1(ctx_id),
614 					   EASRC_CCE1_RS_BYPASS_MASK,
615 					   EASRC_CCE1_RS_BYPASS);
616 
617 		ctx_priv->st1_num_taps = 1;
618 		ctx_priv->st1_coeff    = &easrc_priv->const_coeff;
619 		ctx_priv->st1_num_exp  = 1;
620 		ctx_priv->st2_num_taps = 0;
621 
622 		if (in_s_fmt == SNDRV_PCM_FORMAT_FLOAT_LE &&
623 		    out_s_fmt != SNDRV_PCM_FORMAT_FLOAT_LE)
624 			ctx_priv->st1_addexp = 31;
625 		else if (in_s_fmt != SNDRV_PCM_FORMAT_FLOAT_LE &&
626 			 out_s_fmt == SNDRV_PCM_FORMAT_FLOAT_LE)
627 			ctx_priv->st1_addexp -= ctx_priv->in_params.fmt.addexp;
628 	} else {
629 		inrate = ctx_priv->in_params.norm_rate;
630 		outrate = ctx_priv->out_params.norm_rate;
631 
632 		hdr = easrc_priv->firmware_hdr;
633 		prefil = easrc_priv->prefil;
634 
635 		for (i = 0; i < hdr->prefil_scen; i++) {
636 			if (inrate == prefil[i].insr &&
637 			    outrate == prefil[i].outsr) {
638 				selected_prefil = &prefil[i];
639 				dev_dbg(dev, "Selected prefilter: %u insr, %u outsr, %u st1_taps, %u st2_taps\n",
640 					selected_prefil->insr,
641 					selected_prefil->outsr,
642 					selected_prefil->st1_taps,
643 					selected_prefil->st2_taps);
644 				break;
645 			}
646 		}
647 
648 		if (!selected_prefil) {
649 			dev_err(dev, "Conversion from in ratio %u(%u) to out ratio %u(%u) is not supported\n",
650 				in_s_rate, inrate,
651 				out_s_rate, outrate);
652 			return -EINVAL;
653 		}
654 
655 		/*
656 		 * In prefilter coeff array, first st1_num_taps represent the
657 		 * stage1 prefilter coefficients followed by next st2_num_taps
658 		 * representing stage 2 coefficients
659 		 */
660 		ctx_priv->st1_num_taps = selected_prefil->st1_taps;
661 		ctx_priv->st1_coeff    = selected_prefil->coeff;
662 		ctx_priv->st1_num_exp  = selected_prefil->st1_exp;
663 
664 		offset = ((selected_prefil->st1_taps + 1) / 2);
665 		ctx_priv->st2_num_taps = selected_prefil->st2_taps;
666 		ctx_priv->st2_coeff    = selected_prefil->coeff + offset;
667 
668 		if (in_s_fmt == SNDRV_PCM_FORMAT_FLOAT_LE &&
669 		    out_s_fmt != SNDRV_PCM_FORMAT_FLOAT_LE) {
670 			/* only change stage2 coefficient for 2 stage case */
671 			if (ctx_priv->st2_num_taps > 0)
672 				ctx_priv->st2_addexp = 31;
673 			else
674 				ctx_priv->st1_addexp = 31;
675 		} else if (in_s_fmt != SNDRV_PCM_FORMAT_FLOAT_LE &&
676 			   out_s_fmt == SNDRV_PCM_FORMAT_FLOAT_LE) {
677 			if (ctx_priv->st2_num_taps > 0)
678 				ctx_priv->st2_addexp -= ctx_priv->in_params.fmt.addexp;
679 			else
680 				ctx_priv->st1_addexp -= ctx_priv->in_params.fmt.addexp;
681 		}
682 	}
683 
684 	ctx_priv->in_filled_sample += (ctx_priv->st1_num_taps / 2) * ctx_priv->st1_num_exp +
685 				  ctx_priv->st2_num_taps / 2;
686 	ctx_priv->out_missed_sample = ctx_priv->in_filled_sample * out_s_rate / in_s_rate;
687 
688 	if (ctx_priv->in_filled_sample * out_s_rate % in_s_rate != 0)
689 		ctx_priv->out_missed_sample += 1;
690 	/*
691 	 * To modify the value of a prefilter coefficient, the user must
692 	 * perform a write to the register ASRC_PRE_COEFF_FIFOn[COEFF_DATA]
693 	 * while the respective context RUN_EN bit is set to 0b0
694 	 */
695 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx_id),
696 			   EASRC_CC_EN_MASK, 0);
697 
698 	if (ctx_priv->st1_num_taps > EASRC_MAX_PF_TAPS) {
699 		dev_err(dev, "ST1 taps [%d] mus be lower than %d\n",
700 			ctx_priv->st1_num_taps, EASRC_MAX_PF_TAPS);
701 		ret = -EINVAL;
702 		goto ctx_error;
703 	}
704 
705 	/* Update ctx ST1_NUM_TAPS in Context Control Extended 2 register */
706 	regmap_update_bits(easrc->regmap, REG_EASRC_CCE2(ctx_id),
707 			   EASRC_CCE2_ST1_TAPS_MASK,
708 			   EASRC_CCE2_ST1_TAPS(ctx_priv->st1_num_taps - 1));
709 
710 	/* Prefilter Coefficient Write Select to write in ST1 coeff */
711 	regmap_update_bits(easrc->regmap, REG_EASRC_CCE1(ctx_id),
712 			   EASRC_CCE1_COEF_WS_MASK,
713 			   EASRC_PF_ST1_COEFF_WR << EASRC_CCE1_COEF_WS_SHIFT);
714 
715 	ret = fsl_easrc_write_pf_coeff_mem(easrc, ctx_id,
716 					   ctx_priv->st1_coeff,
717 					   ctx_priv->st1_num_taps,
718 					   ctx_priv->st1_addexp);
719 	if (ret)
720 		goto ctx_error;
721 
722 	if (ctx_priv->st2_num_taps > 0) {
723 		if (ctx_priv->st2_num_taps + ctx_priv->st1_num_taps > EASRC_MAX_PF_TAPS) {
724 			dev_err(dev, "ST2 taps [%d] mus be lower than %d\n",
725 				ctx_priv->st2_num_taps, EASRC_MAX_PF_TAPS);
726 			ret = -EINVAL;
727 			goto ctx_error;
728 		}
729 
730 		regmap_update_bits(easrc->regmap, REG_EASRC_CCE1(ctx_id),
731 				   EASRC_CCE1_PF_TSEN_MASK,
732 				   EASRC_CCE1_PF_TSEN);
733 		/*
734 		 * Enable prefilter stage1 writeback floating point
735 		 * which is used for FLOAT_LE case
736 		 */
737 		regmap_update_bits(easrc->regmap, REG_EASRC_CCE1(ctx_id),
738 				   EASRC_CCE1_PF_ST1_WBFP_MASK,
739 				   EASRC_CCE1_PF_ST1_WBFP);
740 
741 		regmap_update_bits(easrc->regmap, REG_EASRC_CCE1(ctx_id),
742 				   EASRC_CCE1_PF_EXP_MASK,
743 				   EASRC_CCE1_PF_EXP(ctx_priv->st1_num_exp - 1));
744 
745 		/* Update ctx ST2_NUM_TAPS in Context Control Extended 2 reg */
746 		regmap_update_bits(easrc->regmap, REG_EASRC_CCE2(ctx_id),
747 				   EASRC_CCE2_ST2_TAPS_MASK,
748 				   EASRC_CCE2_ST2_TAPS(ctx_priv->st2_num_taps - 1));
749 
750 		/* Prefilter Coefficient Write Select to write in ST2 coeff */
751 		regmap_update_bits(easrc->regmap, REG_EASRC_CCE1(ctx_id),
752 				   EASRC_CCE1_COEF_WS_MASK,
753 				   EASRC_PF_ST2_COEFF_WR << EASRC_CCE1_COEF_WS_SHIFT);
754 
755 		ret = fsl_easrc_write_pf_coeff_mem(easrc, ctx_id,
756 						   ctx_priv->st2_coeff,
757 						   ctx_priv->st2_num_taps,
758 						   ctx_priv->st2_addexp);
759 		if (ret)
760 			goto ctx_error;
761 	}
762 
763 	return 0;
764 
765 ctx_error:
766 	return ret;
767 }
768 
fsl_easrc_max_ch_for_slot(struct fsl_asrc_pair * ctx,struct fsl_easrc_slot * slot)769 static int fsl_easrc_max_ch_for_slot(struct fsl_asrc_pair *ctx,
770 				     struct fsl_easrc_slot *slot)
771 {
772 	struct fsl_easrc_ctx_priv *ctx_priv = ctx->private;
773 	int st1_mem_alloc = 0, st2_mem_alloc = 0;
774 	int pf_mem_alloc = 0;
775 	int max_channels = 8 - slot->num_channel;
776 	int channels = 0;
777 
778 	if (ctx_priv->st1_num_taps > 0) {
779 		if (ctx_priv->st2_num_taps > 0)
780 			st1_mem_alloc =
781 				(ctx_priv->st1_num_taps - 1) * ctx_priv->st1_num_exp + 1;
782 		else
783 			st1_mem_alloc = ctx_priv->st1_num_taps;
784 	}
785 
786 	if (ctx_priv->st2_num_taps > 0)
787 		st2_mem_alloc = ctx_priv->st2_num_taps;
788 
789 	pf_mem_alloc = st1_mem_alloc + st2_mem_alloc;
790 
791 	if (pf_mem_alloc != 0)
792 		channels = (6144 - slot->pf_mem_used) / pf_mem_alloc;
793 	else
794 		channels = 8;
795 
796 	if (channels < max_channels)
797 		max_channels = channels;
798 
799 	return max_channels;
800 }
801 
fsl_easrc_config_one_slot(struct fsl_asrc_pair * ctx,struct fsl_easrc_slot * slot,unsigned int slot_ctx_idx,unsigned int * req_channels,unsigned int * start_channel,unsigned int * avail_channel)802 static int fsl_easrc_config_one_slot(struct fsl_asrc_pair *ctx,
803 				     struct fsl_easrc_slot *slot,
804 				     unsigned int slot_ctx_idx,
805 				     unsigned int *req_channels,
806 				     unsigned int *start_channel,
807 				     unsigned int *avail_channel)
808 {
809 	struct fsl_asrc *easrc = ctx->asrc;
810 	struct fsl_easrc_ctx_priv *ctx_priv = ctx->private;
811 	int st1_chanxexp, st1_mem_alloc = 0, st2_mem_alloc;
812 	unsigned int reg0, reg1, reg2, reg3;
813 	unsigned int addr;
814 
815 	if (slot->slot_index == 0) {
816 		reg0 = REG_EASRC_DPCS0R0(slot_ctx_idx);
817 		reg1 = REG_EASRC_DPCS0R1(slot_ctx_idx);
818 		reg2 = REG_EASRC_DPCS0R2(slot_ctx_idx);
819 		reg3 = REG_EASRC_DPCS0R3(slot_ctx_idx);
820 	} else {
821 		reg0 = REG_EASRC_DPCS1R0(slot_ctx_idx);
822 		reg1 = REG_EASRC_DPCS1R1(slot_ctx_idx);
823 		reg2 = REG_EASRC_DPCS1R2(slot_ctx_idx);
824 		reg3 = REG_EASRC_DPCS1R3(slot_ctx_idx);
825 	}
826 
827 	if (*req_channels <= *avail_channel) {
828 		slot->num_channel = *req_channels;
829 		*req_channels = 0;
830 	} else {
831 		slot->num_channel = *avail_channel;
832 		*req_channels -= *avail_channel;
833 	}
834 
835 	slot->min_channel = *start_channel;
836 	slot->max_channel = *start_channel + slot->num_channel - 1;
837 	slot->ctx_index = ctx->index;
838 	slot->busy = true;
839 	*start_channel += slot->num_channel;
840 
841 	regmap_update_bits(easrc->regmap, reg0,
842 			   EASRC_DPCS0R0_MAXCH_MASK,
843 			   EASRC_DPCS0R0_MAXCH(slot->max_channel));
844 
845 	regmap_update_bits(easrc->regmap, reg0,
846 			   EASRC_DPCS0R0_MINCH_MASK,
847 			   EASRC_DPCS0R0_MINCH(slot->min_channel));
848 
849 	regmap_update_bits(easrc->regmap, reg0,
850 			   EASRC_DPCS0R0_NUMCH_MASK,
851 			   EASRC_DPCS0R0_NUMCH(slot->num_channel - 1));
852 
853 	regmap_update_bits(easrc->regmap, reg0,
854 			   EASRC_DPCS0R0_CTXNUM_MASK,
855 			   EASRC_DPCS0R0_CTXNUM(slot->ctx_index));
856 
857 	if (ctx_priv->st1_num_taps > 0) {
858 		if (ctx_priv->st2_num_taps > 0)
859 			st1_mem_alloc =
860 				(ctx_priv->st1_num_taps - 1) * slot->num_channel *
861 				ctx_priv->st1_num_exp + slot->num_channel;
862 		else
863 			st1_mem_alloc = ctx_priv->st1_num_taps * slot->num_channel;
864 
865 		slot->pf_mem_used = st1_mem_alloc;
866 		regmap_update_bits(easrc->regmap, reg2,
867 				   EASRC_DPCS0R2_ST1_MA_MASK,
868 				   EASRC_DPCS0R2_ST1_MA(st1_mem_alloc));
869 
870 		if (slot->slot_index == 1)
871 			addr = PREFILTER_MEM_LEN - st1_mem_alloc;
872 		else
873 			addr = 0;
874 
875 		regmap_update_bits(easrc->regmap, reg2,
876 				   EASRC_DPCS0R2_ST1_SA_MASK,
877 				   EASRC_DPCS0R2_ST1_SA(addr));
878 	}
879 
880 	if (ctx_priv->st2_num_taps > 0) {
881 		st1_chanxexp = slot->num_channel * (ctx_priv->st1_num_exp - 1);
882 
883 		regmap_update_bits(easrc->regmap, reg1,
884 				   EASRC_DPCS0R1_ST1_EXP_MASK,
885 				   EASRC_DPCS0R1_ST1_EXP(st1_chanxexp));
886 
887 		st2_mem_alloc = slot->num_channel * ctx_priv->st2_num_taps;
888 		slot->pf_mem_used += st2_mem_alloc;
889 		regmap_update_bits(easrc->regmap, reg3,
890 				   EASRC_DPCS0R3_ST2_MA_MASK,
891 				   EASRC_DPCS0R3_ST2_MA(st2_mem_alloc));
892 
893 		if (slot->slot_index == 1)
894 			addr = PREFILTER_MEM_LEN - st1_mem_alloc - st2_mem_alloc;
895 		else
896 			addr = st1_mem_alloc;
897 
898 		regmap_update_bits(easrc->regmap, reg3,
899 				   EASRC_DPCS0R3_ST2_SA_MASK,
900 				   EASRC_DPCS0R3_ST2_SA(addr));
901 	}
902 
903 	regmap_update_bits(easrc->regmap, reg0,
904 			   EASRC_DPCS0R0_EN_MASK, EASRC_DPCS0R0_EN);
905 
906 	return 0;
907 }
908 
909 /*
910  * fsl_easrc_config_slot
911  *
912  * A single context can be split amongst any of the 4 context processing pipes
913  * in the design.
914  * The total number of channels consumed within the context processor must be
915  * less than or equal to 8. if a single context is configured to contain more
916  * than 8 channels then it must be distributed across multiple context
917  * processing pipe slots.
918  *
919  */
fsl_easrc_config_slot(struct fsl_asrc * easrc,unsigned int ctx_id)920 static int fsl_easrc_config_slot(struct fsl_asrc *easrc, unsigned int ctx_id)
921 {
922 	struct fsl_easrc_priv *easrc_priv = easrc->private;
923 	struct fsl_asrc_pair *ctx = easrc->pair[ctx_id];
924 	int req_channels = ctx->channels;
925 	int start_channel = 0, avail_channel;
926 	struct fsl_easrc_slot *slot0, *slot1;
927 	struct fsl_easrc_slot *slota, *slotb;
928 	int i, ret;
929 
930 	if (req_channels <= 0)
931 		return -EINVAL;
932 
933 	for (i = 0; i < EASRC_CTX_MAX_NUM; i++) {
934 		slot0 = &easrc_priv->slot[i][0];
935 		slot1 = &easrc_priv->slot[i][1];
936 
937 		if (slot0->busy && slot1->busy) {
938 			continue;
939 		} else if ((slot0->busy && slot0->ctx_index == ctx->index) ||
940 			 (slot1->busy && slot1->ctx_index == ctx->index)) {
941 			continue;
942 		} else if (!slot0->busy) {
943 			slota = slot0;
944 			slotb = slot1;
945 			slota->slot_index = 0;
946 		} else if (!slot1->busy) {
947 			slota = slot1;
948 			slotb = slot0;
949 			slota->slot_index = 1;
950 		}
951 
952 		if (!slota || !slotb)
953 			continue;
954 
955 		avail_channel = fsl_easrc_max_ch_for_slot(ctx, slotb);
956 		if (avail_channel <= 0)
957 			continue;
958 
959 		ret = fsl_easrc_config_one_slot(ctx, slota, i, &req_channels,
960 						&start_channel, &avail_channel);
961 		if (ret)
962 			return ret;
963 
964 		if (req_channels > 0)
965 			continue;
966 		else
967 			break;
968 	}
969 
970 	if (req_channels > 0) {
971 		dev_err(&easrc->pdev->dev, "no avail slot.\n");
972 		return -EINVAL;
973 	}
974 
975 	return 0;
976 }
977 
978 /*
979  * fsl_easrc_release_slot
980  *
981  * Clear the slot configuration
982  */
fsl_easrc_release_slot(struct fsl_asrc * easrc,unsigned int ctx_id)983 static int fsl_easrc_release_slot(struct fsl_asrc *easrc, unsigned int ctx_id)
984 {
985 	struct fsl_easrc_priv *easrc_priv = easrc->private;
986 	struct fsl_asrc_pair *ctx = easrc->pair[ctx_id];
987 	int i;
988 
989 	for (i = 0; i < EASRC_CTX_MAX_NUM; i++) {
990 		if (easrc_priv->slot[i][0].busy &&
991 		    easrc_priv->slot[i][0].ctx_index == ctx->index) {
992 			easrc_priv->slot[i][0].busy = false;
993 			easrc_priv->slot[i][0].num_channel = 0;
994 			easrc_priv->slot[i][0].pf_mem_used = 0;
995 			/* set registers */
996 			regmap_write(easrc->regmap, REG_EASRC_DPCS0R0(i), 0);
997 			regmap_write(easrc->regmap, REG_EASRC_DPCS0R1(i), 0);
998 			regmap_write(easrc->regmap, REG_EASRC_DPCS0R2(i), 0);
999 			regmap_write(easrc->regmap, REG_EASRC_DPCS0R3(i), 0);
1000 		}
1001 
1002 		if (easrc_priv->slot[i][1].busy &&
1003 		    easrc_priv->slot[i][1].ctx_index == ctx->index) {
1004 			easrc_priv->slot[i][1].busy = false;
1005 			easrc_priv->slot[i][1].num_channel = 0;
1006 			easrc_priv->slot[i][1].pf_mem_used = 0;
1007 			/* set registers */
1008 			regmap_write(easrc->regmap, REG_EASRC_DPCS1R0(i), 0);
1009 			regmap_write(easrc->regmap, REG_EASRC_DPCS1R1(i), 0);
1010 			regmap_write(easrc->regmap, REG_EASRC_DPCS1R2(i), 0);
1011 			regmap_write(easrc->regmap, REG_EASRC_DPCS1R3(i), 0);
1012 		}
1013 	}
1014 
1015 	return 0;
1016 }
1017 
1018 /*
1019  * fsl_easrc_config_context
1020  *
1021  * Configure the register relate with context.
1022  */
fsl_easrc_config_context(struct fsl_asrc * easrc,unsigned int ctx_id)1023 static int fsl_easrc_config_context(struct fsl_asrc *easrc, unsigned int ctx_id)
1024 {
1025 	struct fsl_easrc_ctx_priv *ctx_priv;
1026 	struct fsl_asrc_pair *ctx;
1027 	struct device *dev;
1028 	unsigned long lock_flags;
1029 	int ret;
1030 
1031 	if (!easrc)
1032 		return -ENODEV;
1033 
1034 	dev = &easrc->pdev->dev;
1035 
1036 	if (ctx_id >= EASRC_CTX_MAX_NUM) {
1037 		dev_err(dev, "Invalid context id[%d]\n", ctx_id);
1038 		return -EINVAL;
1039 	}
1040 
1041 	ctx = easrc->pair[ctx_id];
1042 
1043 	ctx_priv = ctx->private;
1044 
1045 	fsl_easrc_normalize_rates(ctx);
1046 
1047 	ret = fsl_easrc_set_rs_ratio(ctx);
1048 	if (ret)
1049 		return ret;
1050 
1051 	/* Initialize the context coeficients */
1052 	ret = fsl_easrc_prefilter_config(easrc, ctx->index);
1053 	if (ret)
1054 		return ret;
1055 
1056 	spin_lock_irqsave(&easrc->lock, lock_flags);
1057 	ret = fsl_easrc_config_slot(easrc, ctx->index);
1058 	spin_unlock_irqrestore(&easrc->lock, lock_flags);
1059 	if (ret)
1060 		return ret;
1061 
1062 	/*
1063 	 * Both prefilter and resampling filters can use following
1064 	 * initialization modes:
1065 	 * 2 - zero-fil mode
1066 	 * 1 - replication mode
1067 	 * 0 - software control
1068 	 */
1069 	regmap_update_bits(easrc->regmap, REG_EASRC_CCE1(ctx_id),
1070 			   EASRC_CCE1_RS_INIT_MASK,
1071 			   EASRC_CCE1_RS_INIT(ctx_priv->rs_init_mode));
1072 
1073 	regmap_update_bits(easrc->regmap, REG_EASRC_CCE1(ctx_id),
1074 			   EASRC_CCE1_PF_INIT_MASK,
1075 			   EASRC_CCE1_PF_INIT(ctx_priv->pf_init_mode));
1076 
1077 	/*
1078 	 * Context Input FIFO Watermark
1079 	 * DMA request is generated when input FIFO < FIFO_WTMK
1080 	 */
1081 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx_id),
1082 			   EASRC_CC_FIFO_WTMK_MASK,
1083 			   EASRC_CC_FIFO_WTMK(ctx_priv->in_params.fifo_wtmk));
1084 
1085 	/*
1086 	 * Context Output FIFO Watermark
1087 	 * DMA request is generated when output FIFO > FIFO_WTMK
1088 	 * So we set fifo_wtmk -1 to register.
1089 	 */
1090 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx_id),
1091 			   EASRC_COC_FIFO_WTMK_MASK,
1092 			   EASRC_COC_FIFO_WTMK(ctx_priv->out_params.fifo_wtmk - 1));
1093 
1094 	/* Number of channels */
1095 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx_id),
1096 			   EASRC_CC_CHEN_MASK,
1097 			   EASRC_CC_CHEN(ctx->channels - 1));
1098 	return 0;
1099 }
1100 
fsl_easrc_process_format(struct fsl_asrc_pair * ctx,struct fsl_easrc_data_fmt * fmt,snd_pcm_format_t raw_fmt)1101 static int fsl_easrc_process_format(struct fsl_asrc_pair *ctx,
1102 				    struct fsl_easrc_data_fmt *fmt,
1103 				    snd_pcm_format_t raw_fmt)
1104 {
1105 	struct fsl_asrc *easrc = ctx->asrc;
1106 	struct fsl_easrc_priv *easrc_priv = easrc->private;
1107 	int ret;
1108 
1109 	if (!fmt)
1110 		return -EINVAL;
1111 
1112 	/*
1113 	 * Context Input Floating Point Format
1114 	 * 0 - Integer Format
1115 	 * 1 - Single Precision FP Format
1116 	 */
1117 	fmt->floating_point = !snd_pcm_format_linear(raw_fmt);
1118 	fmt->sample_pos = 0;
1119 	fmt->iec958 = 0;
1120 
1121 	/* Get the data width */
1122 	switch (snd_pcm_format_width(raw_fmt)) {
1123 	case 16:
1124 		fmt->width = EASRC_WIDTH_16_BIT;
1125 		fmt->addexp = 15;
1126 		break;
1127 	case 20:
1128 		fmt->width = EASRC_WIDTH_20_BIT;
1129 		fmt->addexp = 19;
1130 		break;
1131 	case 24:
1132 		fmt->width = EASRC_WIDTH_24_BIT;
1133 		fmt->addexp = 23;
1134 		break;
1135 	case 32:
1136 		fmt->width = EASRC_WIDTH_32_BIT;
1137 		fmt->addexp = 31;
1138 		break;
1139 	default:
1140 		return -EINVAL;
1141 	}
1142 
1143 	switch (raw_fmt) {
1144 	case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
1145 		fmt->width = easrc_priv->bps_iec958[ctx->index];
1146 		fmt->iec958 = 1;
1147 		fmt->floating_point = 0;
1148 		if (fmt->width == EASRC_WIDTH_16_BIT) {
1149 			fmt->sample_pos = 12;
1150 			fmt->addexp = 15;
1151 		} else if (fmt->width == EASRC_WIDTH_20_BIT) {
1152 			fmt->sample_pos = 8;
1153 			fmt->addexp = 19;
1154 		} else if (fmt->width == EASRC_WIDTH_24_BIT) {
1155 			fmt->sample_pos = 4;
1156 			fmt->addexp = 23;
1157 		}
1158 		break;
1159 	default:
1160 		break;
1161 	}
1162 
1163 	/*
1164 	 * Data Endianness
1165 	 * 0 - Little-Endian
1166 	 * 1 - Big-Endian
1167 	 */
1168 	ret = snd_pcm_format_big_endian(raw_fmt);
1169 	if (ret < 0)
1170 		return ret;
1171 
1172 	fmt->endianness = ret;
1173 
1174 	/*
1175 	 * Input Data sign
1176 	 * 0b - Signed Format
1177 	 * 1b - Unsigned Format
1178 	 */
1179 	fmt->unsign = snd_pcm_format_unsigned(raw_fmt) > 0 ? 1 : 0;
1180 
1181 	return 0;
1182 }
1183 
fsl_easrc_set_ctx_format(struct fsl_asrc_pair * ctx,snd_pcm_format_t * in_raw_format,snd_pcm_format_t * out_raw_format)1184 static int fsl_easrc_set_ctx_format(struct fsl_asrc_pair *ctx,
1185 				    snd_pcm_format_t *in_raw_format,
1186 				    snd_pcm_format_t *out_raw_format)
1187 {
1188 	struct fsl_asrc *easrc = ctx->asrc;
1189 	struct fsl_easrc_ctx_priv *ctx_priv = ctx->private;
1190 	struct fsl_easrc_data_fmt *in_fmt = &ctx_priv->in_params.fmt;
1191 	struct fsl_easrc_data_fmt *out_fmt = &ctx_priv->out_params.fmt;
1192 	int ret = 0;
1193 
1194 	/* Get the bitfield values for input data format */
1195 	if (in_raw_format && out_raw_format) {
1196 		ret = fsl_easrc_process_format(ctx, in_fmt, *in_raw_format);
1197 		if (ret)
1198 			return ret;
1199 	}
1200 
1201 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1202 			   EASRC_CC_BPS_MASK,
1203 			   EASRC_CC_BPS(in_fmt->width));
1204 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1205 			   EASRC_CC_ENDIANNESS_MASK,
1206 			   in_fmt->endianness << EASRC_CC_ENDIANNESS_SHIFT);
1207 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1208 			   EASRC_CC_FMT_MASK,
1209 			   in_fmt->floating_point << EASRC_CC_FMT_SHIFT);
1210 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1211 			   EASRC_CC_INSIGN_MASK,
1212 			   in_fmt->unsign << EASRC_CC_INSIGN_SHIFT);
1213 
1214 	/* In Sample Position */
1215 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1216 			   EASRC_CC_SAMPLE_POS_MASK,
1217 			   EASRC_CC_SAMPLE_POS(in_fmt->sample_pos));
1218 
1219 	/* Get the bitfield values for input data format */
1220 	if (in_raw_format && out_raw_format) {
1221 		ret = fsl_easrc_process_format(ctx, out_fmt, *out_raw_format);
1222 		if (ret)
1223 			return ret;
1224 	}
1225 
1226 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1227 			   EASRC_COC_BPS_MASK,
1228 			   EASRC_COC_BPS(out_fmt->width));
1229 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1230 			   EASRC_COC_ENDIANNESS_MASK,
1231 			   out_fmt->endianness << EASRC_COC_ENDIANNESS_SHIFT);
1232 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1233 			   EASRC_COC_FMT_MASK,
1234 			   out_fmt->floating_point << EASRC_COC_FMT_SHIFT);
1235 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1236 			   EASRC_COC_OUTSIGN_MASK,
1237 			   out_fmt->unsign << EASRC_COC_OUTSIGN_SHIFT);
1238 
1239 	/* Out Sample Position */
1240 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1241 			   EASRC_COC_SAMPLE_POS_MASK,
1242 			   EASRC_COC_SAMPLE_POS(out_fmt->sample_pos));
1243 
1244 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1245 			   EASRC_COC_IEC_EN_MASK,
1246 			   out_fmt->iec958 << EASRC_COC_IEC_EN_SHIFT);
1247 
1248 	return ret;
1249 }
1250 
1251 /*
1252  * The ASRC provides interleaving support in hardware to ensure that a
1253  * variety of sample sources can be internally combined
1254  * to conform with this format. Interleaving parameters are accessed
1255  * through the ASRC_CTRL_IN_ACCESSa and ASRC_CTRL_OUT_ACCESSa registers
1256  */
fsl_easrc_set_ctx_organziation(struct fsl_asrc_pair * ctx)1257 static int fsl_easrc_set_ctx_organziation(struct fsl_asrc_pair *ctx)
1258 {
1259 	struct fsl_easrc_ctx_priv *ctx_priv;
1260 	struct fsl_asrc *easrc;
1261 
1262 	if (!ctx)
1263 		return -ENODEV;
1264 
1265 	easrc = ctx->asrc;
1266 	ctx_priv = ctx->private;
1267 
1268 	/* input interleaving parameters */
1269 	regmap_update_bits(easrc->regmap, REG_EASRC_CIA(ctx->index),
1270 			   EASRC_CIA_ITER_MASK,
1271 			   EASRC_CIA_ITER(ctx_priv->in_params.iterations));
1272 	regmap_update_bits(easrc->regmap, REG_EASRC_CIA(ctx->index),
1273 			   EASRC_CIA_GRLEN_MASK,
1274 			   EASRC_CIA_GRLEN(ctx_priv->in_params.group_len));
1275 	regmap_update_bits(easrc->regmap, REG_EASRC_CIA(ctx->index),
1276 			   EASRC_CIA_ACCLEN_MASK,
1277 			   EASRC_CIA_ACCLEN(ctx_priv->in_params.access_len));
1278 
1279 	/* output interleaving parameters */
1280 	regmap_update_bits(easrc->regmap, REG_EASRC_COA(ctx->index),
1281 			   EASRC_COA_ITER_MASK,
1282 			   EASRC_COA_ITER(ctx_priv->out_params.iterations));
1283 	regmap_update_bits(easrc->regmap, REG_EASRC_COA(ctx->index),
1284 			   EASRC_COA_GRLEN_MASK,
1285 			   EASRC_COA_GRLEN(ctx_priv->out_params.group_len));
1286 	regmap_update_bits(easrc->regmap, REG_EASRC_COA(ctx->index),
1287 			   EASRC_COA_ACCLEN_MASK,
1288 			   EASRC_COA_ACCLEN(ctx_priv->out_params.access_len));
1289 
1290 	return 0;
1291 }
1292 
1293 /*
1294  * Request one of the available contexts
1295  *
1296  * Returns a negative number on error and >=0 as context id
1297  * on success
1298  */
fsl_easrc_request_context(int channels,struct fsl_asrc_pair * ctx)1299 static int fsl_easrc_request_context(int channels, struct fsl_asrc_pair *ctx)
1300 {
1301 	enum asrc_pair_index index = ASRC_INVALID_PAIR;
1302 	struct fsl_asrc *easrc = ctx->asrc;
1303 	struct device *dev;
1304 	unsigned long lock_flags;
1305 	int ret = 0;
1306 	int i;
1307 
1308 	dev = &easrc->pdev->dev;
1309 
1310 	spin_lock_irqsave(&easrc->lock, lock_flags);
1311 
1312 	for (i = ASRC_PAIR_A; i < EASRC_CTX_MAX_NUM; i++) {
1313 		if (easrc->pair[i])
1314 			continue;
1315 
1316 		index = i;
1317 		break;
1318 	}
1319 
1320 	if (index == ASRC_INVALID_PAIR) {
1321 		dev_err(dev, "all contexts are busy\n");
1322 		ret = -EBUSY;
1323 	} else if (channels > easrc->channel_avail) {
1324 		dev_err(dev, "can't give the required channels: %d\n",
1325 			channels);
1326 		ret = -EINVAL;
1327 	} else {
1328 		ctx->index = index;
1329 		ctx->channels = channels;
1330 		easrc->pair[index] = ctx;
1331 		easrc->channel_avail -= channels;
1332 	}
1333 
1334 	spin_unlock_irqrestore(&easrc->lock, lock_flags);
1335 
1336 	return ret;
1337 }
1338 
1339 /*
1340  * Release the context
1341  *
1342  * This function is mainly doing the revert thing in request context
1343  */
fsl_easrc_release_context(struct fsl_asrc_pair * ctx)1344 static void fsl_easrc_release_context(struct fsl_asrc_pair *ctx)
1345 {
1346 	unsigned long lock_flags;
1347 	struct fsl_asrc *easrc;
1348 
1349 	if (!ctx)
1350 		return;
1351 
1352 	easrc = ctx->asrc;
1353 
1354 	spin_lock_irqsave(&easrc->lock, lock_flags);
1355 
1356 	fsl_easrc_release_slot(easrc, ctx->index);
1357 
1358 	easrc->channel_avail += ctx->channels;
1359 	easrc->pair[ctx->index] = NULL;
1360 
1361 	spin_unlock_irqrestore(&easrc->lock, lock_flags);
1362 }
1363 
1364 /*
1365  * Start the context
1366  *
1367  * Enable the DMA request and context
1368  */
fsl_easrc_start_context(struct fsl_asrc_pair * ctx)1369 static int fsl_easrc_start_context(struct fsl_asrc_pair *ctx)
1370 {
1371 	struct fsl_asrc *easrc = ctx->asrc;
1372 
1373 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1374 			   EASRC_CC_FWMDE_MASK, EASRC_CC_FWMDE);
1375 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1376 			   EASRC_COC_FWMDE_MASK, EASRC_COC_FWMDE);
1377 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1378 			   EASRC_CC_EN_MASK, EASRC_CC_EN);
1379 	return 0;
1380 }
1381 
1382 /*
1383  * Stop the context
1384  *
1385  * Disable the DMA request and context
1386  */
fsl_easrc_stop_context(struct fsl_asrc_pair * ctx)1387 static int fsl_easrc_stop_context(struct fsl_asrc_pair *ctx)
1388 {
1389 	struct fsl_asrc *easrc = ctx->asrc;
1390 	int val, i;
1391 	int size;
1392 	int retry = 200;
1393 
1394 	regmap_read(easrc->regmap, REG_EASRC_CC(ctx->index), &val);
1395 
1396 	if (val & EASRC_CC_EN_MASK) {
1397 		regmap_update_bits(easrc->regmap,
1398 				   REG_EASRC_CC(ctx->index),
1399 				   EASRC_CC_STOP_MASK, EASRC_CC_STOP);
1400 		do {
1401 			regmap_read(easrc->regmap, REG_EASRC_SFS(ctx->index), &val);
1402 			val &= EASRC_SFS_NSGO_MASK;
1403 			size = val >> EASRC_SFS_NSGO_SHIFT;
1404 
1405 			/* Read FIFO, drop the data */
1406 			for (i = 0; i < size * ctx->channels; i++)
1407 				regmap_read(easrc->regmap, REG_EASRC_RDFIFO(ctx->index), &val);
1408 			/* Check RUN_STOP_DONE */
1409 			regmap_read(easrc->regmap, REG_EASRC_IRQF, &val);
1410 			if (val & EASRC_IRQF_RSD(1 << ctx->index)) {
1411 				/*Clear RUN_STOP_DONE*/
1412 				regmap_write_bits(easrc->regmap,
1413 						  REG_EASRC_IRQF,
1414 						  EASRC_IRQF_RSD(1 << ctx->index),
1415 						  EASRC_IRQF_RSD(1 << ctx->index));
1416 				break;
1417 			}
1418 			udelay(100);
1419 		} while (--retry);
1420 
1421 		if (retry == 0)
1422 			dev_warn(&easrc->pdev->dev, "RUN STOP fail\n");
1423 	}
1424 
1425 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1426 			   EASRC_CC_EN_MASK | EASRC_CC_STOP_MASK, 0);
1427 	regmap_update_bits(easrc->regmap, REG_EASRC_CC(ctx->index),
1428 			   EASRC_CC_FWMDE_MASK, 0);
1429 	regmap_update_bits(easrc->regmap, REG_EASRC_COC(ctx->index),
1430 			   EASRC_COC_FWMDE_MASK, 0);
1431 	return 0;
1432 }
1433 
fsl_easrc_get_dma_channel(struct fsl_asrc_pair * ctx,bool dir)1434 static struct dma_chan *fsl_easrc_get_dma_channel(struct fsl_asrc_pair *ctx,
1435 						  bool dir)
1436 {
1437 	struct fsl_asrc *easrc = ctx->asrc;
1438 	enum asrc_pair_index index = ctx->index;
1439 	char name[8];
1440 
1441 	/* Example of dma name: ctx0_rx */
1442 	sprintf(name, "ctx%c_%cx", index + '0', dir == IN ? 'r' : 't');
1443 
1444 	return dma_request_slave_channel(&easrc->pdev->dev, name);
1445 };
1446 
1447 static const unsigned int easrc_rates[] = {
1448 	8000, 11025, 12000, 16000,
1449 	22050, 24000, 32000, 44100,
1450 	48000, 64000, 88200, 96000,
1451 	128000, 176400, 192000, 256000,
1452 	352800, 384000, 705600, 768000,
1453 };
1454 
1455 static const struct snd_pcm_hw_constraint_list easrc_rate_constraints = {
1456 	.count = ARRAY_SIZE(easrc_rates),
1457 	.list = easrc_rates,
1458 };
1459 
fsl_easrc_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1460 static int fsl_easrc_startup(struct snd_pcm_substream *substream,
1461 			     struct snd_soc_dai *dai)
1462 {
1463 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
1464 					  SNDRV_PCM_HW_PARAM_RATE,
1465 					  &easrc_rate_constraints);
1466 }
1467 
fsl_easrc_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)1468 static int fsl_easrc_trigger(struct snd_pcm_substream *substream,
1469 			     int cmd, struct snd_soc_dai *dai)
1470 {
1471 	struct snd_pcm_runtime *runtime = substream->runtime;
1472 	struct fsl_asrc_pair *ctx = runtime->private_data;
1473 	int ret;
1474 
1475 	switch (cmd) {
1476 	case SNDRV_PCM_TRIGGER_START:
1477 	case SNDRV_PCM_TRIGGER_RESUME:
1478 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1479 		ret = fsl_easrc_start_context(ctx);
1480 		if (ret)
1481 			return ret;
1482 		break;
1483 	case SNDRV_PCM_TRIGGER_STOP:
1484 	case SNDRV_PCM_TRIGGER_SUSPEND:
1485 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1486 		ret = fsl_easrc_stop_context(ctx);
1487 		if (ret)
1488 			return ret;
1489 		break;
1490 	default:
1491 		return -EINVAL;
1492 	}
1493 
1494 	return 0;
1495 }
1496 
fsl_easrc_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)1497 static int fsl_easrc_hw_params(struct snd_pcm_substream *substream,
1498 			       struct snd_pcm_hw_params *params,
1499 			       struct snd_soc_dai *dai)
1500 {
1501 	struct fsl_asrc *easrc = snd_soc_dai_get_drvdata(dai);
1502 	struct snd_pcm_runtime *runtime = substream->runtime;
1503 	struct device *dev = &easrc->pdev->dev;
1504 	struct fsl_asrc_pair *ctx = runtime->private_data;
1505 	struct fsl_easrc_ctx_priv *ctx_priv = ctx->private;
1506 	unsigned int channels = params_channels(params);
1507 	unsigned int rate = params_rate(params);
1508 	snd_pcm_format_t format = params_format(params);
1509 	int ret;
1510 
1511 	ret = fsl_easrc_request_context(channels, ctx);
1512 	if (ret) {
1513 		dev_err(dev, "failed to request context\n");
1514 		return ret;
1515 	}
1516 
1517 	ctx_priv->ctx_streams |= BIT(substream->stream);
1518 
1519 	/*
1520 	 * Set the input and output ratio so we can compute
1521 	 * the resampling ratio in RS_LOW/HIGH
1522 	 */
1523 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1524 		ctx_priv->in_params.sample_rate = rate;
1525 		ctx_priv->in_params.sample_format = format;
1526 		ctx_priv->out_params.sample_rate = easrc->asrc_rate;
1527 		ctx_priv->out_params.sample_format = easrc->asrc_format;
1528 	} else {
1529 		ctx_priv->out_params.sample_rate = rate;
1530 		ctx_priv->out_params.sample_format = format;
1531 		ctx_priv->in_params.sample_rate = easrc->asrc_rate;
1532 		ctx_priv->in_params.sample_format = easrc->asrc_format;
1533 	}
1534 
1535 	ctx->channels = channels;
1536 	ctx_priv->in_params.fifo_wtmk  = 0x20;
1537 	ctx_priv->out_params.fifo_wtmk = 0x20;
1538 
1539 	/*
1540 	 * Do only rate conversion and keep the same format for input
1541 	 * and output data
1542 	 */
1543 	ret = fsl_easrc_set_ctx_format(ctx,
1544 				       &ctx_priv->in_params.sample_format,
1545 				       &ctx_priv->out_params.sample_format);
1546 	if (ret) {
1547 		dev_err(dev, "failed to set format %d", ret);
1548 		return ret;
1549 	}
1550 
1551 	ret = fsl_easrc_config_context(easrc, ctx->index);
1552 	if (ret) {
1553 		dev_err(dev, "failed to config context\n");
1554 		return ret;
1555 	}
1556 
1557 	ctx_priv->in_params.iterations = 1;
1558 	ctx_priv->in_params.group_len = ctx->channels;
1559 	ctx_priv->in_params.access_len = ctx->channels;
1560 	ctx_priv->out_params.iterations = 1;
1561 	ctx_priv->out_params.group_len = ctx->channels;
1562 	ctx_priv->out_params.access_len = ctx->channels;
1563 
1564 	ret = fsl_easrc_set_ctx_organziation(ctx);
1565 	if (ret) {
1566 		dev_err(dev, "failed to set fifo organization\n");
1567 		return ret;
1568 	}
1569 
1570 	return 0;
1571 }
1572 
fsl_easrc_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1573 static int fsl_easrc_hw_free(struct snd_pcm_substream *substream,
1574 			     struct snd_soc_dai *dai)
1575 {
1576 	struct snd_pcm_runtime *runtime = substream->runtime;
1577 	struct fsl_asrc_pair *ctx = runtime->private_data;
1578 	struct fsl_easrc_ctx_priv *ctx_priv;
1579 
1580 	if (!ctx)
1581 		return -EINVAL;
1582 
1583 	ctx_priv = ctx->private;
1584 
1585 	if (ctx_priv->ctx_streams & BIT(substream->stream)) {
1586 		ctx_priv->ctx_streams &= ~BIT(substream->stream);
1587 		fsl_easrc_release_context(ctx);
1588 	}
1589 
1590 	return 0;
1591 }
1592 
fsl_easrc_dai_probe(struct snd_soc_dai * cpu_dai)1593 static int fsl_easrc_dai_probe(struct snd_soc_dai *cpu_dai)
1594 {
1595 	struct fsl_asrc *easrc = dev_get_drvdata(cpu_dai->dev);
1596 
1597 	snd_soc_dai_init_dma_data(cpu_dai,
1598 				  &easrc->dma_params_tx,
1599 				  &easrc->dma_params_rx);
1600 	return 0;
1601 }
1602 
1603 static const struct snd_soc_dai_ops fsl_easrc_dai_ops = {
1604 	.probe		= fsl_easrc_dai_probe,
1605 	.startup	= fsl_easrc_startup,
1606 	.trigger	= fsl_easrc_trigger,
1607 	.hw_params	= fsl_easrc_hw_params,
1608 	.hw_free	= fsl_easrc_hw_free,
1609 };
1610 
1611 static struct snd_soc_dai_driver fsl_easrc_dai = {
1612 	.playback = {
1613 		.stream_name = "ASRC-Playback",
1614 		.channels_min = 1,
1615 		.channels_max = 32,
1616 		.rate_min = 8000,
1617 		.rate_max = 768000,
1618 		.rates = SNDRV_PCM_RATE_KNOT,
1619 		.formats = FSL_EASRC_FORMATS,
1620 	},
1621 	.capture = {
1622 		.stream_name = "ASRC-Capture",
1623 		.channels_min = 1,
1624 		.channels_max = 32,
1625 		.rate_min = 8000,
1626 		.rate_max = 768000,
1627 		.rates = SNDRV_PCM_RATE_KNOT,
1628 		.formats = FSL_EASRC_FORMATS |
1629 			   SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
1630 	},
1631 	.ops = &fsl_easrc_dai_ops,
1632 };
1633 
1634 static const struct snd_soc_component_driver fsl_easrc_component = {
1635 	.name			= "fsl-easrc-dai",
1636 	.controls		= fsl_easrc_snd_controls,
1637 	.num_controls		= ARRAY_SIZE(fsl_easrc_snd_controls),
1638 	.legacy_dai_naming	= 1,
1639 #ifdef CONFIG_DEBUG_FS
1640 	.debugfs_prefix		= "easrc",
1641 #endif
1642 };
1643 
1644 static const struct reg_default fsl_easrc_reg_defaults[] = {
1645 	{REG_EASRC_WRFIFO(0),	0x00000000},
1646 	{REG_EASRC_WRFIFO(1),	0x00000000},
1647 	{REG_EASRC_WRFIFO(2),	0x00000000},
1648 	{REG_EASRC_WRFIFO(3),	0x00000000},
1649 	{REG_EASRC_RDFIFO(0),	0x00000000},
1650 	{REG_EASRC_RDFIFO(1),	0x00000000},
1651 	{REG_EASRC_RDFIFO(2),	0x00000000},
1652 	{REG_EASRC_RDFIFO(3),	0x00000000},
1653 	{REG_EASRC_CC(0),	0x00000000},
1654 	{REG_EASRC_CC(1),	0x00000000},
1655 	{REG_EASRC_CC(2),	0x00000000},
1656 	{REG_EASRC_CC(3),	0x00000000},
1657 	{REG_EASRC_CCE1(0),	0x00000000},
1658 	{REG_EASRC_CCE1(1),	0x00000000},
1659 	{REG_EASRC_CCE1(2),	0x00000000},
1660 	{REG_EASRC_CCE1(3),	0x00000000},
1661 	{REG_EASRC_CCE2(0),	0x00000000},
1662 	{REG_EASRC_CCE2(1),	0x00000000},
1663 	{REG_EASRC_CCE2(2),	0x00000000},
1664 	{REG_EASRC_CCE2(3),	0x00000000},
1665 	{REG_EASRC_CIA(0),	0x00000000},
1666 	{REG_EASRC_CIA(1),	0x00000000},
1667 	{REG_EASRC_CIA(2),	0x00000000},
1668 	{REG_EASRC_CIA(3),	0x00000000},
1669 	{REG_EASRC_DPCS0R0(0),	0x00000000},
1670 	{REG_EASRC_DPCS0R0(1),	0x00000000},
1671 	{REG_EASRC_DPCS0R0(2),	0x00000000},
1672 	{REG_EASRC_DPCS0R0(3),	0x00000000},
1673 	{REG_EASRC_DPCS0R1(0),	0x00000000},
1674 	{REG_EASRC_DPCS0R1(1),	0x00000000},
1675 	{REG_EASRC_DPCS0R1(2),	0x00000000},
1676 	{REG_EASRC_DPCS0R1(3),	0x00000000},
1677 	{REG_EASRC_DPCS0R2(0),	0x00000000},
1678 	{REG_EASRC_DPCS0R2(1),	0x00000000},
1679 	{REG_EASRC_DPCS0R2(2),	0x00000000},
1680 	{REG_EASRC_DPCS0R2(3),	0x00000000},
1681 	{REG_EASRC_DPCS0R3(0),	0x00000000},
1682 	{REG_EASRC_DPCS0R3(1),	0x00000000},
1683 	{REG_EASRC_DPCS0R3(2),	0x00000000},
1684 	{REG_EASRC_DPCS0R3(3),	0x00000000},
1685 	{REG_EASRC_DPCS1R0(0),	0x00000000},
1686 	{REG_EASRC_DPCS1R0(1),	0x00000000},
1687 	{REG_EASRC_DPCS1R0(2),	0x00000000},
1688 	{REG_EASRC_DPCS1R0(3),	0x00000000},
1689 	{REG_EASRC_DPCS1R1(0),	0x00000000},
1690 	{REG_EASRC_DPCS1R1(1),	0x00000000},
1691 	{REG_EASRC_DPCS1R1(2),	0x00000000},
1692 	{REG_EASRC_DPCS1R1(3),	0x00000000},
1693 	{REG_EASRC_DPCS1R2(0),	0x00000000},
1694 	{REG_EASRC_DPCS1R2(1),	0x00000000},
1695 	{REG_EASRC_DPCS1R2(2),	0x00000000},
1696 	{REG_EASRC_DPCS1R2(3),	0x00000000},
1697 	{REG_EASRC_DPCS1R3(0),	0x00000000},
1698 	{REG_EASRC_DPCS1R3(1),	0x00000000},
1699 	{REG_EASRC_DPCS1R3(2),	0x00000000},
1700 	{REG_EASRC_DPCS1R3(3),	0x00000000},
1701 	{REG_EASRC_COC(0),	0x00000000},
1702 	{REG_EASRC_COC(1),	0x00000000},
1703 	{REG_EASRC_COC(2),	0x00000000},
1704 	{REG_EASRC_COC(3),	0x00000000},
1705 	{REG_EASRC_COA(0),	0x00000000},
1706 	{REG_EASRC_COA(1),	0x00000000},
1707 	{REG_EASRC_COA(2),	0x00000000},
1708 	{REG_EASRC_COA(3),	0x00000000},
1709 	{REG_EASRC_SFS(0),	0x00000000},
1710 	{REG_EASRC_SFS(1),	0x00000000},
1711 	{REG_EASRC_SFS(2),	0x00000000},
1712 	{REG_EASRC_SFS(3),	0x00000000},
1713 	{REG_EASRC_RRL(0),	0x00000000},
1714 	{REG_EASRC_RRL(1),	0x00000000},
1715 	{REG_EASRC_RRL(2),	0x00000000},
1716 	{REG_EASRC_RRL(3),	0x00000000},
1717 	{REG_EASRC_RRH(0),	0x00000000},
1718 	{REG_EASRC_RRH(1),	0x00000000},
1719 	{REG_EASRC_RRH(2),	0x00000000},
1720 	{REG_EASRC_RRH(3),	0x00000000},
1721 	{REG_EASRC_RUC(0),	0x00000000},
1722 	{REG_EASRC_RUC(1),	0x00000000},
1723 	{REG_EASRC_RUC(2),	0x00000000},
1724 	{REG_EASRC_RUC(3),	0x00000000},
1725 	{REG_EASRC_RUR(0),	0x7FFFFFFF},
1726 	{REG_EASRC_RUR(1),	0x7FFFFFFF},
1727 	{REG_EASRC_RUR(2),	0x7FFFFFFF},
1728 	{REG_EASRC_RUR(3),	0x7FFFFFFF},
1729 	{REG_EASRC_RCTCL,	0x00000000},
1730 	{REG_EASRC_RCTCH,	0x00000000},
1731 	{REG_EASRC_PCF(0),	0x00000000},
1732 	{REG_EASRC_PCF(1),	0x00000000},
1733 	{REG_EASRC_PCF(2),	0x00000000},
1734 	{REG_EASRC_PCF(3),	0x00000000},
1735 	{REG_EASRC_CRCM,	0x00000000},
1736 	{REG_EASRC_CRCC,	0x00000000},
1737 	{REG_EASRC_IRQC,	0x00000FFF},
1738 	{REG_EASRC_IRQF,	0x00000000},
1739 	{REG_EASRC_CS0(0),	0x00000000},
1740 	{REG_EASRC_CS0(1),	0x00000000},
1741 	{REG_EASRC_CS0(2),	0x00000000},
1742 	{REG_EASRC_CS0(3),	0x00000000},
1743 	{REG_EASRC_CS1(0),	0x00000000},
1744 	{REG_EASRC_CS1(1),	0x00000000},
1745 	{REG_EASRC_CS1(2),	0x00000000},
1746 	{REG_EASRC_CS1(3),	0x00000000},
1747 	{REG_EASRC_CS2(0),	0x00000000},
1748 	{REG_EASRC_CS2(1),	0x00000000},
1749 	{REG_EASRC_CS2(2),	0x00000000},
1750 	{REG_EASRC_CS2(3),	0x00000000},
1751 	{REG_EASRC_CS3(0),	0x00000000},
1752 	{REG_EASRC_CS3(1),	0x00000000},
1753 	{REG_EASRC_CS3(2),	0x00000000},
1754 	{REG_EASRC_CS3(3),	0x00000000},
1755 	{REG_EASRC_CS4(0),	0x00000000},
1756 	{REG_EASRC_CS4(1),	0x00000000},
1757 	{REG_EASRC_CS4(2),	0x00000000},
1758 	{REG_EASRC_CS4(3),	0x00000000},
1759 	{REG_EASRC_CS5(0),	0x00000000},
1760 	{REG_EASRC_CS5(1),	0x00000000},
1761 	{REG_EASRC_CS5(2),	0x00000000},
1762 	{REG_EASRC_CS5(3),	0x00000000},
1763 	{REG_EASRC_DBGC,	0x00000000},
1764 	{REG_EASRC_DBGS,	0x00000000},
1765 };
1766 
1767 static const struct regmap_range fsl_easrc_readable_ranges[] = {
1768 	regmap_reg_range(REG_EASRC_RDFIFO(0), REG_EASRC_RCTCH),
1769 	regmap_reg_range(REG_EASRC_PCF(0), REG_EASRC_PCF(3)),
1770 	regmap_reg_range(REG_EASRC_CRCC, REG_EASRC_DBGS),
1771 };
1772 
1773 static const struct regmap_access_table fsl_easrc_readable_table = {
1774 	.yes_ranges = fsl_easrc_readable_ranges,
1775 	.n_yes_ranges = ARRAY_SIZE(fsl_easrc_readable_ranges),
1776 };
1777 
1778 static const struct regmap_range fsl_easrc_writeable_ranges[] = {
1779 	regmap_reg_range(REG_EASRC_WRFIFO(0), REG_EASRC_WRFIFO(3)),
1780 	regmap_reg_range(REG_EASRC_CC(0), REG_EASRC_COA(3)),
1781 	regmap_reg_range(REG_EASRC_RRL(0), REG_EASRC_RCTCH),
1782 	regmap_reg_range(REG_EASRC_PCF(0), REG_EASRC_DBGC),
1783 };
1784 
1785 static const struct regmap_access_table fsl_easrc_writeable_table = {
1786 	.yes_ranges = fsl_easrc_writeable_ranges,
1787 	.n_yes_ranges = ARRAY_SIZE(fsl_easrc_writeable_ranges),
1788 };
1789 
1790 static const struct regmap_range fsl_easrc_volatileable_ranges[] = {
1791 	regmap_reg_range(REG_EASRC_RDFIFO(0), REG_EASRC_RDFIFO(3)),
1792 	regmap_reg_range(REG_EASRC_SFS(0), REG_EASRC_SFS(3)),
1793 	regmap_reg_range(REG_EASRC_IRQF, REG_EASRC_IRQF),
1794 	regmap_reg_range(REG_EASRC_DBGS, REG_EASRC_DBGS),
1795 };
1796 
1797 static const struct regmap_access_table fsl_easrc_volatileable_table = {
1798 	.yes_ranges = fsl_easrc_volatileable_ranges,
1799 	.n_yes_ranges = ARRAY_SIZE(fsl_easrc_volatileable_ranges),
1800 };
1801 
1802 static const struct regmap_config fsl_easrc_regmap_config = {
1803 	.reg_bits = 32,
1804 	.reg_stride = 4,
1805 	.val_bits = 32,
1806 
1807 	.max_register = REG_EASRC_DBGS,
1808 	.reg_defaults = fsl_easrc_reg_defaults,
1809 	.num_reg_defaults = ARRAY_SIZE(fsl_easrc_reg_defaults),
1810 	.rd_table = &fsl_easrc_readable_table,
1811 	.wr_table = &fsl_easrc_writeable_table,
1812 	.volatile_table = &fsl_easrc_volatileable_table,
1813 	.cache_type = REGCACHE_MAPLE,
1814 };
1815 
1816 #ifdef DEBUG
fsl_easrc_dump_firmware(struct fsl_asrc * easrc)1817 static void fsl_easrc_dump_firmware(struct fsl_asrc *easrc)
1818 {
1819 	struct fsl_easrc_priv *easrc_priv = easrc->private;
1820 	struct asrc_firmware_hdr *firm = easrc_priv->firmware_hdr;
1821 	struct interp_params *interp = easrc_priv->interp;
1822 	struct prefil_params *prefil = easrc_priv->prefil;
1823 	struct device *dev = &easrc->pdev->dev;
1824 	int i;
1825 
1826 	if (firm->magic != FIRMWARE_MAGIC) {
1827 		dev_err(dev, "Wrong magic. Something went wrong!");
1828 		return;
1829 	}
1830 
1831 	dev_dbg(dev, "Firmware v%u dump:\n", firm->firmware_version);
1832 	dev_dbg(dev, "Num prefilter scenarios: %u\n", firm->prefil_scen);
1833 	dev_dbg(dev, "Num interpolation scenarios: %u\n", firm->interp_scen);
1834 	dev_dbg(dev, "\nInterpolation scenarios:\n");
1835 
1836 	for (i = 0; i < firm->interp_scen; i++) {
1837 		if (interp[i].magic != FIRMWARE_MAGIC) {
1838 			dev_dbg(dev, "%d. wrong interp magic: %x\n",
1839 				i, interp[i].magic);
1840 			continue;
1841 		}
1842 		dev_dbg(dev, "%d. taps: %u, phases: %u, center: %llu\n", i,
1843 			interp[i].num_taps, interp[i].num_phases,
1844 			interp[i].center_tap);
1845 	}
1846 
1847 	for (i = 0; i < firm->prefil_scen; i++) {
1848 		if (prefil[i].magic != FIRMWARE_MAGIC) {
1849 			dev_dbg(dev, "%d. wrong prefil magic: %x\n",
1850 				i, prefil[i].magic);
1851 			continue;
1852 		}
1853 		dev_dbg(dev, "%d. insr: %u, outsr: %u, st1: %u, st2: %u\n", i,
1854 			prefil[i].insr, prefil[i].outsr,
1855 			prefil[i].st1_taps, prefil[i].st2_taps);
1856 	}
1857 
1858 	dev_dbg(dev, "end of firmware dump\n");
1859 }
1860 #endif
1861 
fsl_easrc_get_firmware(struct fsl_asrc * easrc)1862 static int fsl_easrc_get_firmware(struct fsl_asrc *easrc)
1863 {
1864 	struct fsl_easrc_priv *easrc_priv;
1865 	const struct firmware **fw_p;
1866 	u32 pnum, inum, offset;
1867 	const u8 *data;
1868 	int ret;
1869 
1870 	if (!easrc)
1871 		return -EINVAL;
1872 
1873 	easrc_priv = easrc->private;
1874 	fw_p = &easrc_priv->fw;
1875 
1876 	ret = request_firmware(fw_p, easrc_priv->fw_name, &easrc->pdev->dev);
1877 	if (ret)
1878 		return ret;
1879 
1880 	data = easrc_priv->fw->data;
1881 
1882 	easrc_priv->firmware_hdr = (struct asrc_firmware_hdr *)data;
1883 	pnum = easrc_priv->firmware_hdr->prefil_scen;
1884 	inum = easrc_priv->firmware_hdr->interp_scen;
1885 
1886 	if (inum) {
1887 		offset = sizeof(struct asrc_firmware_hdr);
1888 		easrc_priv->interp = (struct interp_params *)(data + offset);
1889 	}
1890 
1891 	if (pnum) {
1892 		offset = sizeof(struct asrc_firmware_hdr) +
1893 				inum * sizeof(struct interp_params);
1894 		easrc_priv->prefil = (struct prefil_params *)(data + offset);
1895 	}
1896 
1897 #ifdef DEBUG
1898 	fsl_easrc_dump_firmware(easrc);
1899 #endif
1900 
1901 	return 0;
1902 }
1903 
fsl_easrc_isr(int irq,void * dev_id)1904 static irqreturn_t fsl_easrc_isr(int irq, void *dev_id)
1905 {
1906 	struct fsl_asrc *easrc = (struct fsl_asrc *)dev_id;
1907 	struct device *dev = &easrc->pdev->dev;
1908 	int val;
1909 
1910 	regmap_read(easrc->regmap, REG_EASRC_IRQF, &val);
1911 
1912 	if (val & EASRC_IRQF_OER_MASK)
1913 		dev_dbg(dev, "output FIFO underflow\n");
1914 
1915 	if (val & EASRC_IRQF_IFO_MASK)
1916 		dev_dbg(dev, "input FIFO overflow\n");
1917 
1918 	return IRQ_HANDLED;
1919 }
1920 
fsl_easrc_get_fifo_addr(u8 dir,enum asrc_pair_index index)1921 static int fsl_easrc_get_fifo_addr(u8 dir, enum asrc_pair_index index)
1922 {
1923 	return REG_EASRC_FIFO(dir, index);
1924 }
1925 
1926 /* Get sample numbers in FIFO */
fsl_easrc_get_output_fifo_size(struct fsl_asrc_pair * pair)1927 static unsigned int fsl_easrc_get_output_fifo_size(struct fsl_asrc_pair *pair)
1928 {
1929 	struct fsl_asrc *asrc = pair->asrc;
1930 	enum asrc_pair_index index = pair->index;
1931 	u32 val;
1932 
1933 	regmap_read(asrc->regmap, REG_EASRC_SFS(index), &val);
1934 	val &= EASRC_SFS_NSGO_MASK;
1935 
1936 	return val >> EASRC_SFS_NSGO_SHIFT;
1937 }
1938 
fsl_easrc_m2m_prepare(struct fsl_asrc_pair * pair)1939 static int fsl_easrc_m2m_prepare(struct fsl_asrc_pair *pair)
1940 {
1941 	struct fsl_easrc_ctx_priv *ctx_priv = pair->private;
1942 	struct fsl_asrc *asrc = pair->asrc;
1943 	struct device *dev = &asrc->pdev->dev;
1944 	int ret;
1945 
1946 	ctx_priv->in_params.sample_rate = pair->rate[IN];
1947 	ctx_priv->in_params.sample_format = pair->sample_format[IN];
1948 	ctx_priv->out_params.sample_rate = pair->rate[OUT];
1949 	ctx_priv->out_params.sample_format = pair->sample_format[OUT];
1950 
1951 	ctx_priv->in_params.fifo_wtmk = FSL_EASRC_INPUTFIFO_WML;
1952 	ctx_priv->out_params.fifo_wtmk = FSL_EASRC_OUTPUTFIFO_WML;
1953 	/* Fill the right half of the re-sampler with zeros */
1954 	ctx_priv->rs_init_mode = 0x2;
1955 	/* Zero fill the right half of the prefilter */
1956 	ctx_priv->pf_init_mode = 0x2;
1957 
1958 	ret = fsl_easrc_set_ctx_format(pair,
1959 				       &ctx_priv->in_params.sample_format,
1960 				       &ctx_priv->out_params.sample_format);
1961 	if (ret) {
1962 		dev_err(dev, "failed to set context format: %d\n", ret);
1963 		return ret;
1964 	}
1965 
1966 	ret = fsl_easrc_config_context(asrc, pair->index);
1967 	if (ret) {
1968 		dev_err(dev, "failed to config context %d\n", ret);
1969 		return ret;
1970 	}
1971 
1972 	ctx_priv->in_params.iterations = 1;
1973 	ctx_priv->in_params.group_len = pair->channels;
1974 	ctx_priv->in_params.access_len = pair->channels;
1975 	ctx_priv->out_params.iterations = 1;
1976 	ctx_priv->out_params.group_len = pair->channels;
1977 	ctx_priv->out_params.access_len = pair->channels;
1978 
1979 	ret = fsl_easrc_set_ctx_organziation(pair);
1980 	if (ret) {
1981 		dev_err(dev, "failed to set fifo organization\n");
1982 		return ret;
1983 	}
1984 
1985 	/* The context start flag */
1986 	pair->first_convert = 1;
1987 	return 0;
1988 }
1989 
fsl_easrc_m2m_start(struct fsl_asrc_pair * pair)1990 static int fsl_easrc_m2m_start(struct fsl_asrc_pair *pair)
1991 {
1992 	/* start context once */
1993 	if (pair->first_convert) {
1994 		fsl_easrc_start_context(pair);
1995 		pair->first_convert = 0;
1996 	}
1997 
1998 	return 0;
1999 }
2000 
fsl_easrc_m2m_stop(struct fsl_asrc_pair * pair)2001 static int fsl_easrc_m2m_stop(struct fsl_asrc_pair *pair)
2002 {
2003 	/* Stop pair/context */
2004 	if (!pair->first_convert) {
2005 		fsl_easrc_stop_context(pair);
2006 		pair->first_convert = 1;
2007 	}
2008 
2009 	return 0;
2010 }
2011 
2012 /* calculate capture data length according to output data length and sample rate */
fsl_easrc_m2m_calc_out_len(struct fsl_asrc_pair * pair,int input_buffer_length)2013 static int fsl_easrc_m2m_calc_out_len(struct fsl_asrc_pair *pair, int input_buffer_length)
2014 {
2015 	struct fsl_asrc *easrc = pair->asrc;
2016 	struct fsl_easrc_priv *easrc_priv = easrc->private;
2017 	struct fsl_easrc_ctx_priv *ctx_priv = pair->private;
2018 	unsigned int in_rate = ctx_priv->in_params.norm_rate;
2019 	unsigned int out_rate = ctx_priv->out_params.norm_rate;
2020 	unsigned int channels = pair->channels;
2021 	unsigned int in_samples, out_samples;
2022 	unsigned int in_width, out_width;
2023 	unsigned int out_length;
2024 	unsigned int frac_bits;
2025 	u64 val1, val2;
2026 
2027 	switch (easrc_priv->rs_num_taps) {
2028 	case EASRC_RS_32_TAPS:
2029 		/* integer bits = 5; */
2030 		frac_bits = 39;
2031 		break;
2032 	case EASRC_RS_64_TAPS:
2033 		/* integer bits = 6; */
2034 		frac_bits = 38;
2035 		break;
2036 	case EASRC_RS_128_TAPS:
2037 		/* integer bits = 7; */
2038 		frac_bits = 37;
2039 		break;
2040 	default:
2041 		return -EINVAL;
2042 	}
2043 
2044 	val1 = (u64)in_rate << frac_bits;
2045 	do_div(val1, out_rate);
2046 	val1 += (s64)ctx_priv->ratio_mod << (frac_bits - 31);
2047 
2048 	in_width = snd_pcm_format_physical_width(ctx_priv->in_params.sample_format) / 8;
2049 	out_width = snd_pcm_format_physical_width(ctx_priv->out_params.sample_format) / 8;
2050 
2051 	ctx_priv->in_filled_len += input_buffer_length;
2052 	if (ctx_priv->in_filled_len <= ctx_priv->in_filled_sample * in_width * channels) {
2053 		out_length = 0;
2054 	} else {
2055 		in_samples = ctx_priv->in_filled_len / (in_width * channels) -
2056 			     ctx_priv->in_filled_sample;
2057 
2058 		/* right shift 12 bit to make ratio in 32bit space */
2059 		val2 = (u64)in_samples << (frac_bits - 12);
2060 		val1 = val1 >> 12;
2061 		do_div(val2, val1);
2062 		out_samples = val2;
2063 
2064 		out_length = out_samples * out_width * channels;
2065 		ctx_priv->in_filled_len = ctx_priv->in_filled_sample * in_width * channels;
2066 	}
2067 
2068 	return out_length;
2069 }
2070 
fsl_easrc_m2m_get_maxburst(u8 dir,struct fsl_asrc_pair * pair)2071 static int fsl_easrc_m2m_get_maxburst(u8 dir, struct fsl_asrc_pair *pair)
2072 {
2073 	struct fsl_easrc_ctx_priv *ctx_priv = pair->private;
2074 
2075 	if (dir == IN)
2076 		return ctx_priv->in_params.fifo_wtmk * pair->channels;
2077 	else
2078 		return ctx_priv->out_params.fifo_wtmk * pair->channels;
2079 }
2080 
fsl_easrc_m2m_pair_suspend(struct fsl_asrc_pair * pair)2081 static int fsl_easrc_m2m_pair_suspend(struct fsl_asrc_pair *pair)
2082 {
2083 	fsl_easrc_stop_context(pair);
2084 
2085 	return 0;
2086 }
2087 
fsl_easrc_m2m_pair_resume(struct fsl_asrc_pair * pair)2088 static int fsl_easrc_m2m_pair_resume(struct fsl_asrc_pair *pair)
2089 {
2090 	struct fsl_easrc_ctx_priv *ctx_priv = pair->private;
2091 
2092 	pair->first_convert = 1;
2093 	ctx_priv->in_filled_len = 0;
2094 
2095 	return 0;
2096 }
2097 
2098 /* val is Q31 */
fsl_easrc_m2m_set_ratio_mod(struct fsl_asrc_pair * pair,int val)2099 static int fsl_easrc_m2m_set_ratio_mod(struct fsl_asrc_pair *pair, int val)
2100 {
2101 	struct fsl_easrc_ctx_priv *ctx_priv = pair->private;
2102 	struct fsl_asrc *easrc = pair->asrc;
2103 	struct fsl_easrc_priv *easrc_priv = easrc->private;
2104 	unsigned int frac_bits;
2105 
2106 	ctx_priv->ratio_mod += val;
2107 
2108 	switch (easrc_priv->rs_num_taps) {
2109 	case EASRC_RS_32_TAPS:
2110 		/* integer bits = 5; */
2111 		frac_bits = 39;
2112 		break;
2113 	case EASRC_RS_64_TAPS:
2114 		/* integer bits = 6; */
2115 		frac_bits = 38;
2116 		break;
2117 	case EASRC_RS_128_TAPS:
2118 		/* integer bits = 7; */
2119 		frac_bits = 37;
2120 		break;
2121 	default:
2122 		return -EINVAL;
2123 	}
2124 
2125 	val <<= (frac_bits - 31);
2126 	regmap_write(easrc->regmap, REG_EASRC_RUC(pair->index), EASRC_RSUC_RS_RM(val));
2127 
2128 	return 0;
2129 }
2130 
fsl_easrc_m2m_get_cap(struct fsl_asrc_m2m_cap * cap)2131 static int fsl_easrc_m2m_get_cap(struct fsl_asrc_m2m_cap *cap)
2132 {
2133 	cap->fmt_in = FSL_EASRC_FORMATS;
2134 	cap->fmt_out = FSL_EASRC_FORMATS | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2135 	cap->rate_in = easrc_rates;
2136 	cap->rate_in_count = ARRAY_SIZE(easrc_rates);
2137 	cap->rate_out = easrc_rates;
2138 	cap->rate_out_count = ARRAY_SIZE(easrc_rates);
2139 	cap->chan_min = 1;
2140 	cap->chan_max = 32;
2141 	return 0;
2142 }
2143 
2144 static const struct of_device_id fsl_easrc_dt_ids[] = {
2145 	{ .compatible = "fsl,imx8mn-easrc",},
2146 	{}
2147 };
2148 MODULE_DEVICE_TABLE(of, fsl_easrc_dt_ids);
2149 
fsl_easrc_probe(struct platform_device * pdev)2150 static int fsl_easrc_probe(struct platform_device *pdev)
2151 {
2152 	struct fsl_easrc_priv *easrc_priv;
2153 	struct device *dev = &pdev->dev;
2154 	struct fsl_asrc *easrc;
2155 	struct resource *res;
2156 	struct device_node *np;
2157 	void __iomem *regs;
2158 	u32 asrc_fmt = 0;
2159 	int ret, irq;
2160 
2161 	easrc = devm_kzalloc(dev, sizeof(*easrc), GFP_KERNEL);
2162 	if (!easrc)
2163 		return -ENOMEM;
2164 
2165 	easrc_priv = devm_kzalloc(dev, sizeof(*easrc_priv), GFP_KERNEL);
2166 	if (!easrc_priv)
2167 		return -ENOMEM;
2168 
2169 	easrc->pdev = pdev;
2170 	easrc->private = easrc_priv;
2171 	np = dev->of_node;
2172 
2173 	regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2174 	if (IS_ERR(regs))
2175 		return PTR_ERR(regs);
2176 
2177 	easrc->paddr = res->start;
2178 
2179 	easrc->regmap = devm_regmap_init_mmio(dev, regs, &fsl_easrc_regmap_config);
2180 	if (IS_ERR(easrc->regmap)) {
2181 		dev_err(dev, "failed to init regmap");
2182 		return PTR_ERR(easrc->regmap);
2183 	}
2184 
2185 	irq = platform_get_irq(pdev, 0);
2186 	if (irq < 0)
2187 		return irq;
2188 
2189 	ret = devm_request_irq(&pdev->dev, irq, fsl_easrc_isr, 0,
2190 			       dev_name(dev), easrc);
2191 	if (ret) {
2192 		dev_err(dev, "failed to claim irq %u: %d\n", irq, ret);
2193 		return ret;
2194 	}
2195 
2196 	easrc->mem_clk = devm_clk_get(dev, "mem");
2197 	if (IS_ERR(easrc->mem_clk)) {
2198 		dev_err(dev, "failed to get mem clock\n");
2199 		return PTR_ERR(easrc->mem_clk);
2200 	}
2201 
2202 	/* Set default value */
2203 	easrc->channel_avail = 32;
2204 	easrc->get_dma_channel = fsl_easrc_get_dma_channel;
2205 	easrc->request_pair = fsl_easrc_request_context;
2206 	easrc->release_pair = fsl_easrc_release_context;
2207 	easrc->get_fifo_addr = fsl_easrc_get_fifo_addr;
2208 	easrc->pair_priv_size = sizeof(struct fsl_easrc_ctx_priv);
2209 	easrc->m2m_prepare = fsl_easrc_m2m_prepare;
2210 	easrc->m2m_start = fsl_easrc_m2m_start;
2211 	easrc->m2m_stop = fsl_easrc_m2m_stop;
2212 	easrc->get_output_fifo_size = fsl_easrc_get_output_fifo_size;
2213 	easrc->m2m_calc_out_len = fsl_easrc_m2m_calc_out_len;
2214 	easrc->m2m_get_maxburst = fsl_easrc_m2m_get_maxburst;
2215 	easrc->m2m_pair_suspend = fsl_easrc_m2m_pair_suspend;
2216 	easrc->m2m_pair_resume = fsl_easrc_m2m_pair_resume;
2217 	easrc->m2m_set_ratio_mod = fsl_easrc_m2m_set_ratio_mod;
2218 	easrc->m2m_get_cap = fsl_easrc_m2m_get_cap;
2219 
2220 	easrc_priv->rs_num_taps = EASRC_RS_32_TAPS;
2221 	easrc_priv->const_coeff = 0x3FF0000000000000;
2222 
2223 	ret = of_property_read_u32(np, "fsl,asrc-rate", &easrc->asrc_rate);
2224 	if (ret) {
2225 		dev_err(dev, "failed to asrc rate\n");
2226 		return ret;
2227 	}
2228 
2229 	ret = of_property_read_u32(np, "fsl,asrc-format", &asrc_fmt);
2230 	easrc->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
2231 	if (ret) {
2232 		dev_err(dev, "failed to asrc format\n");
2233 		return ret;
2234 	}
2235 
2236 	if (!(FSL_EASRC_FORMATS & (pcm_format_to_bits(easrc->asrc_format)))) {
2237 		dev_warn(dev, "unsupported format, switching to S24_LE\n");
2238 		easrc->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
2239 	}
2240 
2241 	ret = of_property_read_string(np, "firmware-name",
2242 				      &easrc_priv->fw_name);
2243 	if (ret) {
2244 		dev_err(dev, "failed to get firmware name\n");
2245 		return ret;
2246 	}
2247 
2248 	platform_set_drvdata(pdev, easrc);
2249 	pm_runtime_enable(dev);
2250 
2251 	spin_lock_init(&easrc->lock);
2252 
2253 	regcache_cache_only(easrc->regmap, true);
2254 
2255 	ret = devm_snd_soc_register_component(dev, &fsl_easrc_component,
2256 					      &fsl_easrc_dai, 1);
2257 	if (ret) {
2258 		dev_err(dev, "failed to register ASoC DAI\n");
2259 		goto err_pm_disable;
2260 	}
2261 
2262 	ret = devm_snd_soc_register_component(dev, &fsl_asrc_component,
2263 					      NULL, 0);
2264 	if (ret) {
2265 		dev_err(&pdev->dev, "failed to register ASoC platform\n");
2266 		goto err_pm_disable;
2267 	}
2268 
2269 	ret = fsl_asrc_m2m_init(easrc);
2270 	if (ret) {
2271 		dev_err(&pdev->dev, "failed to init m2m device %d\n", ret);
2272 		return ret;
2273 	}
2274 
2275 	return 0;
2276 
2277 err_pm_disable:
2278 	pm_runtime_disable(&pdev->dev);
2279 	return ret;
2280 }
2281 
fsl_easrc_remove(struct platform_device * pdev)2282 static void fsl_easrc_remove(struct platform_device *pdev)
2283 {
2284 	struct fsl_asrc *easrc = dev_get_drvdata(&pdev->dev);
2285 
2286 	fsl_asrc_m2m_exit(easrc);
2287 
2288 	pm_runtime_disable(&pdev->dev);
2289 }
2290 
fsl_easrc_runtime_suspend(struct device * dev)2291 static int fsl_easrc_runtime_suspend(struct device *dev)
2292 {
2293 	struct fsl_asrc *easrc = dev_get_drvdata(dev);
2294 	struct fsl_easrc_priv *easrc_priv = easrc->private;
2295 	unsigned long lock_flags;
2296 
2297 	regcache_cache_only(easrc->regmap, true);
2298 
2299 	clk_disable_unprepare(easrc->mem_clk);
2300 
2301 	spin_lock_irqsave(&easrc->lock, lock_flags);
2302 	easrc_priv->firmware_loaded = 0;
2303 	spin_unlock_irqrestore(&easrc->lock, lock_flags);
2304 
2305 	return 0;
2306 }
2307 
fsl_easrc_runtime_resume(struct device * dev)2308 static int fsl_easrc_runtime_resume(struct device *dev)
2309 {
2310 	struct fsl_asrc *easrc = dev_get_drvdata(dev);
2311 	struct fsl_easrc_priv *easrc_priv = easrc->private;
2312 	struct fsl_easrc_ctx_priv *ctx_priv;
2313 	struct fsl_asrc_pair *ctx;
2314 	unsigned long lock_flags;
2315 	int ret;
2316 	int i;
2317 
2318 	ret = clk_prepare_enable(easrc->mem_clk);
2319 	if (ret)
2320 		return ret;
2321 
2322 	regcache_cache_only(easrc->regmap, false);
2323 	regcache_mark_dirty(easrc->regmap);
2324 	regcache_sync(easrc->regmap);
2325 
2326 	spin_lock_irqsave(&easrc->lock, lock_flags);
2327 	if (easrc_priv->firmware_loaded) {
2328 		spin_unlock_irqrestore(&easrc->lock, lock_flags);
2329 		goto skip_load;
2330 	}
2331 	easrc_priv->firmware_loaded = 1;
2332 	spin_unlock_irqrestore(&easrc->lock, lock_flags);
2333 
2334 	ret = fsl_easrc_get_firmware(easrc);
2335 	if (ret) {
2336 		dev_err(dev, "failed to get firmware\n");
2337 		goto disable_mem_clk;
2338 	}
2339 
2340 	/*
2341 	 * Write Resampling Coefficients
2342 	 * The coefficient RAM must be configured prior to beginning of
2343 	 * any context processing within the ASRC
2344 	 */
2345 	ret = fsl_easrc_resampler_config(easrc);
2346 	if (ret) {
2347 		dev_err(dev, "resampler config failed\n");
2348 		goto disable_mem_clk;
2349 	}
2350 
2351 	for (i = ASRC_PAIR_A; i < EASRC_CTX_MAX_NUM; i++) {
2352 		ctx = easrc->pair[i];
2353 		if (!ctx)
2354 			continue;
2355 
2356 		ctx_priv = ctx->private;
2357 		fsl_easrc_set_rs_ratio(ctx);
2358 		ctx_priv->out_missed_sample = ctx_priv->in_filled_sample *
2359 					      ctx_priv->out_params.sample_rate /
2360 					      ctx_priv->in_params.sample_rate;
2361 		if (ctx_priv->in_filled_sample * ctx_priv->out_params.sample_rate
2362 		    % ctx_priv->in_params.sample_rate != 0)
2363 			ctx_priv->out_missed_sample += 1;
2364 
2365 		ret = fsl_easrc_write_pf_coeff_mem(easrc, i,
2366 						   ctx_priv->st1_coeff,
2367 						   ctx_priv->st1_num_taps,
2368 						   ctx_priv->st1_addexp);
2369 		if (ret)
2370 			goto disable_mem_clk;
2371 
2372 		ret = fsl_easrc_write_pf_coeff_mem(easrc, i,
2373 						   ctx_priv->st2_coeff,
2374 						   ctx_priv->st2_num_taps,
2375 						   ctx_priv->st2_addexp);
2376 		if (ret)
2377 			goto disable_mem_clk;
2378 	}
2379 
2380 skip_load:
2381 	return 0;
2382 
2383 disable_mem_clk:
2384 	clk_disable_unprepare(easrc->mem_clk);
2385 	return ret;
2386 }
2387 
fsl_easrc_suspend(struct device * dev)2388 static int fsl_easrc_suspend(struct device *dev)
2389 {
2390 	struct fsl_asrc *easrc = dev_get_drvdata(dev);
2391 	int ret;
2392 
2393 	fsl_asrc_m2m_suspend(easrc);
2394 	ret = pm_runtime_force_suspend(dev);
2395 	return ret;
2396 }
2397 
fsl_easrc_resume(struct device * dev)2398 static int fsl_easrc_resume(struct device *dev)
2399 {
2400 	struct fsl_asrc *easrc = dev_get_drvdata(dev);
2401 	int ret;
2402 
2403 	ret = pm_runtime_force_resume(dev);
2404 	fsl_asrc_m2m_resume(easrc);
2405 	return ret;
2406 }
2407 
2408 static const struct dev_pm_ops fsl_easrc_pm_ops = {
2409 	RUNTIME_PM_OPS(fsl_easrc_runtime_suspend, fsl_easrc_runtime_resume, NULL)
2410 	SYSTEM_SLEEP_PM_OPS(fsl_easrc_suspend, fsl_easrc_resume)
2411 };
2412 
2413 static struct platform_driver fsl_easrc_driver = {
2414 	.probe = fsl_easrc_probe,
2415 	.remove = fsl_easrc_remove,
2416 	.driver = {
2417 		.name = "fsl-easrc",
2418 		.pm = pm_ptr(&fsl_easrc_pm_ops),
2419 		.of_match_table = fsl_easrc_dt_ids,
2420 	},
2421 };
2422 module_platform_driver(fsl_easrc_driver);
2423 
2424 MODULE_DESCRIPTION("NXP Enhanced Asynchronous Sample Rate (eASRC) driver");
2425 MODULE_LICENSE("GPL v2");
2426