From nobody Fri Nov 29 05:32:28 2024 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67FC9157A48; Wed, 25 Sep 2024 07:17:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=92.121.34.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248677; cv=none; b=friLqalxggRWRPoucVg8MUvDRw6/xqTTnQ0ec3edqxoS+oo40sIu/Mff3kly1mrV7EfLpl0TfXZFNcaSib1yS9yN/RtQ4yw0mxGMG0pE8VEV14VmecvxqU5zh12Q+LpHC5zzKt1f14J8h1LBfmHvLxO22gbNuYjd1VrzdsCxkYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727248677; c=relaxed/simple; bh=Sqpyr1ehLvpdpSWpBcm6s7wKkpfpYWiS6ljjRuKIWrI=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References; b=kvbFJ0e7LOoRFbJos94rWGPVo6eIRG0cG+0EEIuPN3nZckFxDbIl74ltfysCgVUgaDRKEaQLV+544zdNiseN6Ytzn0VKFV5P6Sk80lKjEAynbTMYTp16suYvyldGZQOIyMHdhb3PxErI/ceh4uBauqCDYlosALD+ifWs3QDjYH8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; arc=none smtp.client-ip=92.121.34.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id C8E212029FB; Wed, 25 Sep 2024 09:17:53 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 9691C201FFF; Wed, 25 Sep 2024 09:17:53 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id E6E51183AD44; Wed, 25 Sep 2024 15:17:51 +0800 (+08) From: Shengjiu Wang To: vkoul@kernel.org, perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, shengjiu.wang@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, nicoleotsuka@gmail.com, lgirdwood@gmail.com, broonie@kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 4/7] ASoC: fsl_easrc: define functions for memory to memory usage Date: Wed, 25 Sep 2024 14:55:13 +0800 Message-Id: <1727247316-16156-5-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> References: <1727247316-16156-1-git-send-email-shengjiu.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" ASRC can be used on memory to memory case, define several functions for m2m usage and export them as function pointer. Signed-off-by: Shengjiu Wang --- sound/soc/fsl/fsl_easrc.c | 228 ++++++++++++++++++++++++++++++++++++++ sound/soc/fsl/fsl_easrc.h | 4 + 2 files changed, 232 insertions(+) diff --git a/sound/soc/fsl/fsl_easrc.c b/sound/soc/fsl/fsl_easrc.c index 962f30912091..a1a7a90ff5ef 100644 --- a/sound/soc/fsl/fsl_easrc.c +++ b/sound/soc/fsl/fsl_easrc.c @@ -1861,6 +1861,224 @@ static int fsl_easrc_get_fifo_addr(u8 dir, enum asr= c_pair_index index) return REG_EASRC_FIFO(dir, index); } =20 +/* Get sample numbers in FIFO */ +static unsigned int fsl_easrc_get_output_fifo_size(struct fsl_asrc_pair *p= air) +{ + struct fsl_asrc *asrc =3D pair->asrc; + enum asrc_pair_index index =3D pair->index; + u32 val; + + regmap_read(asrc->regmap, REG_EASRC_SFS(index), &val); + val &=3D EASRC_SFS_NSGO_MASK; + + return val >> EASRC_SFS_NSGO_SHIFT; +} + +static int fsl_easrc_m2m_prepare(struct fsl_asrc_pair *pair) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + struct fsl_asrc *asrc =3D pair->asrc; + struct device *dev =3D &asrc->pdev->dev; + int ret; + + ctx_priv->in_params.sample_rate =3D pair->rate[IN]; + ctx_priv->in_params.sample_format =3D pair->sample_format[IN]; + ctx_priv->out_params.sample_rate =3D pair->rate[OUT]; + ctx_priv->out_params.sample_format =3D pair->sample_format[OUT]; + + ctx_priv->in_params.fifo_wtmk =3D FSL_EASRC_INPUTFIFO_WML; + ctx_priv->out_params.fifo_wtmk =3D FSL_EASRC_OUTPUTFIFO_WML; + /* Fill the right half of the re-sampler with zeros */ + ctx_priv->rs_init_mode =3D 0x2; + /* Zero fill the right half of the prefilter */ + ctx_priv->pf_init_mode =3D 0x2; + + ret =3D fsl_easrc_set_ctx_format(pair, + &ctx_priv->in_params.sample_format, + &ctx_priv->out_params.sample_format); + if (ret) { + dev_err(dev, "failed to set context format: %d\n", ret); + return ret; + } + + ret =3D fsl_easrc_config_context(asrc, pair->index); + if (ret) { + dev_err(dev, "failed to config context %d\n", ret); + return ret; + } + + ctx_priv->in_params.iterations =3D 1; + ctx_priv->in_params.group_len =3D pair->channels; + ctx_priv->in_params.access_len =3D pair->channels; + ctx_priv->out_params.iterations =3D 1; + ctx_priv->out_params.group_len =3D pair->channels; + ctx_priv->out_params.access_len =3D pair->channels; + + ret =3D fsl_easrc_set_ctx_organziation(pair); + if (ret) { + dev_err(dev, "failed to set fifo organization\n"); + return ret; + } + + /* The context start flag */ + pair->first_convert =3D 1; + return 0; +} + +static int fsl_easrc_m2m_start(struct fsl_asrc_pair *pair) +{ + /* start context once */ + if (pair->first_convert) { + fsl_easrc_start_context(pair); + pair->first_convert =3D 0; + } + + return 0; +} + +static int fsl_easrc_m2m_stop(struct fsl_asrc_pair *pair) +{ + /* Stop pair/context */ + if (!pair->first_convert) { + fsl_easrc_stop_context(pair); + pair->first_convert =3D 1; + } + + return 0; +} + +/* calculate capture data length according to output data length and sampl= e rate */ +static int fsl_easrc_m2m_calc_out_len(struct fsl_asrc_pair *pair, int inpu= t_buffer_length) +{ + struct fsl_asrc *easrc =3D pair->asrc; + struct fsl_easrc_priv *easrc_priv =3D easrc->private; + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + unsigned int in_rate =3D ctx_priv->in_params.norm_rate; + unsigned int out_rate =3D ctx_priv->out_params.norm_rate; + unsigned int channels =3D pair->channels; + unsigned int in_samples, out_samples; + unsigned int in_width, out_width; + unsigned int out_length; + unsigned int frac_bits; + u64 val1, val2; + + switch (easrc_priv->rs_num_taps) { + case EASRC_RS_32_TAPS: + /* integer bits =3D 5; */ + frac_bits =3D 39; + break; + case EASRC_RS_64_TAPS: + /* integer bits =3D 6; */ + frac_bits =3D 38; + break; + case EASRC_RS_128_TAPS: + /* integer bits =3D 7; */ + frac_bits =3D 37; + break; + default: + return -EINVAL; + } + + val1 =3D (u64)in_rate << frac_bits; + do_div(val1, out_rate); + val1 +=3D (s64)ctx_priv->ratio_mod << (frac_bits - 31); + + in_width =3D snd_pcm_format_physical_width(ctx_priv->in_params.sample_for= mat) / 8; + out_width =3D snd_pcm_format_physical_width(ctx_priv->out_params.sample_f= ormat) / 8; + + ctx_priv->in_filled_len +=3D input_buffer_length; + if (ctx_priv->in_filled_len <=3D ctx_priv->in_filled_sample * in_width * = channels) { + out_length =3D 0; + } else { + in_samples =3D ctx_priv->in_filled_len / (in_width * channels) - + ctx_priv->in_filled_sample; + + /* right shift 12 bit to make ratio in 32bit space */ + val2 =3D (u64)in_samples << (frac_bits - 12); + val1 =3D val1 >> 12; + do_div(val2, val1); + out_samples =3D val2; + + out_length =3D out_samples * out_width * channels; + ctx_priv->in_filled_len =3D ctx_priv->in_filled_sample * in_width * chan= nels; + } + + return out_length; +} + +static int fsl_easrc_m2m_get_maxburst(u8 dir, struct fsl_asrc_pair *pair) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + + if (dir =3D=3D IN) + return ctx_priv->in_params.fifo_wtmk * pair->channels; + else + return ctx_priv->out_params.fifo_wtmk * pair->channels; +} + +static int fsl_easrc_m2m_pair_suspend(struct fsl_asrc_pair *pair) +{ + fsl_easrc_stop_context(pair); + + return 0; +} + +static int fsl_easrc_m2m_pair_resume(struct fsl_asrc_pair *pair) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + + pair->first_convert =3D 1; + ctx_priv->in_filled_len =3D 0; + + return 0; +} + +/* val is Q31 */ +static int fsl_easrc_m2m_set_ratio_mod(struct fsl_asrc_pair *pair, int val) +{ + struct fsl_easrc_ctx_priv *ctx_priv =3D pair->private; + struct fsl_asrc *easrc =3D pair->asrc; + struct fsl_easrc_priv *easrc_priv =3D easrc->private; + unsigned int frac_bits; + + ctx_priv->ratio_mod +=3D val; + + switch (easrc_priv->rs_num_taps) { + case EASRC_RS_32_TAPS: + /* integer bits =3D 5; */ + frac_bits =3D 39; + break; + case EASRC_RS_64_TAPS: + /* integer bits =3D 6; */ + frac_bits =3D 38; + break; + case EASRC_RS_128_TAPS: + /* integer bits =3D 7; */ + frac_bits =3D 37; + break; + default: + return -EINVAL; + } + + val <<=3D (frac_bits - 31); + regmap_write(easrc->regmap, REG_EASRC_RUC(pair->index), EASRC_RSUC_RS_RM(= val)); + + return 0; +} + +static int fsl_easrc_m2m_get_cap(struct fsl_asrc_m2m_cap *cap) +{ + cap->fmt_in =3D FSL_EASRC_FORMATS; + cap->fmt_out =3D FSL_EASRC_FORMATS | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; + cap->rate_in =3D easrc_rates; + cap->rate_in_count =3D ARRAY_SIZE(easrc_rates); + cap->rate_out =3D easrc_rates; + cap->rate_out_count =3D ARRAY_SIZE(easrc_rates); + cap->chan_min =3D 1; + cap->chan_max =3D 32; + return 0; +} + static const struct of_device_id fsl_easrc_dt_ids[] =3D { { .compatible =3D "fsl,imx8mn-easrc",}, {} @@ -1926,6 +2144,16 @@ static int fsl_easrc_probe(struct platform_device *p= dev) easrc->release_pair =3D fsl_easrc_release_context; easrc->get_fifo_addr =3D fsl_easrc_get_fifo_addr; easrc->pair_priv_size =3D sizeof(struct fsl_easrc_ctx_priv); + easrc->m2m_prepare =3D fsl_easrc_m2m_prepare; + easrc->m2m_start =3D fsl_easrc_m2m_start; + easrc->m2m_stop =3D fsl_easrc_m2m_stop; + easrc->get_output_fifo_size =3D fsl_easrc_get_output_fifo_size; + easrc->m2m_calc_out_len =3D fsl_easrc_m2m_calc_out_len; + easrc->m2m_get_maxburst =3D fsl_easrc_m2m_get_maxburst; + easrc->m2m_pair_suspend =3D fsl_easrc_m2m_pair_suspend; + easrc->m2m_pair_resume =3D fsl_easrc_m2m_pair_resume; + easrc->m2m_set_ratio_mod =3D fsl_easrc_m2m_set_ratio_mod; + easrc->m2m_get_cap =3D fsl_easrc_m2m_get_cap; =20 easrc_priv->rs_num_taps =3D EASRC_RS_32_TAPS; easrc_priv->const_coeff =3D 0x3FF0000000000000; diff --git a/sound/soc/fsl/fsl_easrc.h b/sound/soc/fsl/fsl_easrc.h index 7c70dac52713..c9f770862662 100644 --- a/sound/soc/fsl/fsl_easrc.h +++ b/sound/soc/fsl/fsl_easrc.h @@ -601,6 +601,8 @@ struct fsl_easrc_slot { * @out_missed_sample: sample missed in output * @st1_addexp: exponent added for stage1 * @st2_addexp: exponent added for stage2 + * @ratio_mod: update ratio + * @in_filled_len: input filled length */ struct fsl_easrc_ctx_priv { struct fsl_easrc_io_params in_params; @@ -618,6 +620,8 @@ struct fsl_easrc_ctx_priv { int out_missed_sample; int st1_addexp; int st2_addexp; + int ratio_mod; + unsigned int in_filled_len; }; =20 /** --=20 2.34.1