From nobody Mon Nov 25 10:37:43 2024 Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03on2087.outbound.protection.outlook.com [40.107.105.87]) (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 65A2D18FDC5; Tue, 29 Oct 2024 02:11:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.105.87 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730167879; cv=fail; b=HlEG9/nSIT4O2ugQdTqdMeq2AJn5S4wi6z4sE4N9x14Dr6xaZKPfUdZRR1X+vzk0JyfYd9/b1w0q9DVRaX4+PP47xSeYlSP3oYv3tw8sjlEh7kqtNvIEjbAYj+nr3HCvJiEniuq+ZCtj98/3Q03IbPld3GmHXVEZddYoiyb8CG0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730167879; c=relaxed/simple; bh=Gp0pnKF8kua+PCwg1bfryyG34jWScjNxKl8/zIMVDAI=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=W97Do2Uygaok0uHuaY/QgAQiHb11afVxH4qBILbG5s+TrpB4Rg9hlTWdiA8I3QJL3DweiXzN0tgffb4eAiUoWiGFjUWCzxQKHaSQT6PiqCA9TJuu4/Zj9fdE67uCfaBc7oq1dG2vpHs856fs7q0WEXfLLHiWmLFTCnQzWbCrs+U= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b=CPmBB0Li; arc=fail smtp.client-ip=40.107.105.87 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b="CPmBB0Li" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TKj8oYFQJRSbp1hIsuqlWaVrkH/XRVi4ktuQvJ+Me98MDu9l8fTzM8FgfQG0alXRB8Ng102jI7+tRna+L4BYigP/IDbiGnx28l2MafnBoaqoFmbjHFnLwGobBvCHw0N/yLP37hmqGiDD5tX40KBvHOqsRrFu/o86fRT3+haUsun/sNUIXi1PWBGmKuGL7Veh7e63S1JxL5ilA5mmoBlMlitQu7QD11SpPRIMpkG7trrquBYXLjcAgBVEvTOj7V0NNrVUMUXzpzAcJZu8l8cBfGUc5kZpZgoxqaj96arLNo2kKsI7lV7VBhFQRrxW3P9cbRLpDGQj46O3EJ9QP556/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=WuobSh6p2+iPQ4MBztGLhybzFsfEt4btXYigKoePPJs=; b=M/WpHQlJTcNMBRoyzZ0oruYD98aLtuEyIAFOaQCRRPzcpEVmKaWRaAQEnz+leUEbyDn3yt15Pvfv0mcc3tyveZTv3rBa+eNhp6AcHhZt2kizmC8RAmgeMgvQlzRr+p7BpOgsuBkBxqwK6Lk//WZvhawH5FUKvmMEPP1PXCzo68g1uHQPhQ4wB2fCZBCv1M7S3CPdJypokV84bBsXgggVMctkYRmg/0m+79AxOOf1MsZE9ND9xpLxpL0KOhPtd7Wed5YZuHrAzp8FA/mFdGbwy3ukVg8Q5HPFgF75XYoQ2TonOOcHTYUU0uOTKE9UH7QzS2Ewkil+OKJ87Sz3zVa81w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=WuobSh6p2+iPQ4MBztGLhybzFsfEt4btXYigKoePPJs=; b=CPmBB0LiVWCQx7yvXZstidahm4JIoguQIr3b47nehPqbpEoIrVxpi8TQDVCwsnvvSCHNgB/8x0vm1yXiR7kB8FRRUwqJGZCaE3Ssk/0OXQUHMDcZbaWBbiBAxtje9y9ald3X8rEKdWggGxIbaAFmapUtPSRgFIXVWLsbEa5s7aVcWZ7Qr5hekHiWkyeIO2m2Z2ZMJ43EidWGWheE0ggvTmjclXPSSJQzAtwYbV7pPzY8VtTzWIIF4ru6+y69F12He5DGSXnZ0jsAVpAbTRAAAR2UcwqCB2V5TcVs2LHfZ0Vu9qKV/uGeQKxP2AQfhs1jbPIfUfu9iwN9US/Mr/nuvw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from AM0PR04MB7044.eurprd04.prod.outlook.com (2603:10a6:208:191::20) by DU2PR04MB9147.eurprd04.prod.outlook.com (2603:10a6:10:2f6::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.25; Tue, 29 Oct 2024 02:11:12 +0000 Received: from AM0PR04MB7044.eurprd04.prod.outlook.com ([fe80::7be0:296:768c:e891]) by AM0PR04MB7044.eurprd04.prod.outlook.com ([fe80::7be0:296:768c:e891%6]) with mapi id 15.20.8093.023; Tue, 29 Oct 2024 02:11:12 +0000 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 v5 4/6] ASoC: fsl_asrc_m2m: Add memory to memory function Date: Tue, 29 Oct 2024 10:20:55 +0800 Message-Id: <20241029022057.816402-5-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20241029022057.816402-1-shengjiu.wang@nxp.com> References: <20241029022057.816402-1-shengjiu.wang@nxp.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SI2PR01CA0031.apcprd01.prod.exchangelabs.com (2603:1096:4:192::11) To AM0PR04MB7044.eurprd04.prod.outlook.com (2603:10a6:208:191::20) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM0PR04MB7044:EE_|DU2PR04MB9147:EE_ X-MS-Office365-Filtering-Correlation-Id: c7f897b8-42e7-4d3a-128f-08dcf7bef61e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|52116014|1800799024|366016|38350700014|921020; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?lAFoXC10u6OqElDDhDR1gdnHVJeHWVKcX2wDKNRoC8PCyV92m2YDpkeK9QqW?= =?us-ascii?Q?V+Qb3mjWlmZNwfiJomw62jsv25am8reM+R8XOVGp9Z+wtg3Grwpm+Hkpfaoj?= =?us-ascii?Q?FRxS6v+RBJjQ1kpSDVR3PYrAbDjF4C8KkjUnUQUrPgJBGwJTNxxSUrBYLVnb?= =?us-ascii?Q?lAeZLUb5gb0j5e5Nntc+inZeT2nC3UPT5geHflW03wddibzZoVPKo7Ve6xqf?= =?us-ascii?Q?w03dNQDtuE+Y+mXSRyN7lVC0t8oJM5g+3eEUMFzLG94EzWRqKdkiMqCzXqBQ?= =?us-ascii?Q?QglYCdpXOOc5V9AYALIpXl+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GpwWhcVbkI5i0r6gEvVRxR9d68qLDFtnave9lHEtqvQqk78ko70+tCIpk?= =?us-ascii?Q?LbGxDpWbQtCT4K4968FvNmMUcphgeocha2LzIxt6WvLs7t1bIHecMuTprSgs?= =?us-ascii?Q?N5D+RK+IMvGmK5+UWkjCJOi6HA04ClvH5gx636NP5x6TeJMSPXvWYoiXeW9L?= =?us-ascii?Q?U9R8TVSevJ7cmFpqOeW7ecvPyD9C?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM0PR04MB7044.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(7416014)(376014)(52116014)(1800799024)(366016)(38350700014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?NLMJpV39m2Fz07XUmwz0zWrCC7VY3qvL45w499r0hOjHqdCoUj3CjtXmdZeG?= =?us-ascii?Q?cemQbDK8ZD8HmOD5a9yKi6yhP+re0CF/B0MjMBTgvOcQoILInNnYpouFYff2?= =?us-ascii?Q?BFM6vm4ruocGKitKmK3VqR7dVXuaNNTpR0eZXEn+6DMEZMv6dw5Hp8Jqj8+I?= =?us-ascii?Q?bS4VtoqNBWDA2Y6V3V6me/oNZNvYlHWpg5FaZMPtnKsXtK5uJN0LmPfrcSQ5?= =?us-ascii?Q?ohHfm/HoXavhT87SNdmHc7gR9x1dzIET8UpCLyGVeLHX/fzT9hN5u/W2jUh3?= =?us-ascii?Q?d2rxKku0/rY764LnYH+GzI8QkwvudGa4oj8ZGteMO0uyA6PkGvfsfF8ZDq6l?= =?us-ascii?Q?PGTSWk6gZVnwISht0XyE8HYdyDrrpNqlF1sUcs2HYOsl7s5zoLHivmu+z9vo?= =?us-ascii?Q?mu+NtL78KT+h+rbRYQahrkjjyqzMRVlQW1HMlZDakhMndKhijW2cqv2hGJV8?= =?us-ascii?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Fiuo4roqJdwKBYyXA70PbzGlq4y?= =?us-ascii?Q?Tigw+wQyxlVFwSVhCIf3NsSUng6CmocWXdDoYn8Wln6Y8p1XDLOdjBcfhdWk?= =?us-ascii?Q?Ijlb3PBs0MZ/bvtC0axrb815vwo8wnt+kW0jikG+eTRpHCTKuYkvTGLoLH88?= =?us-ascii?Q?aec46qxYB+ZRj3JbukMH38H0ACmn7EZDnQ2EC4ZxbsttOfCVlEkaMLG9nn9+?= =?us-ascii?Q?sHbXl8GBmQcuBEyf8a1JjOhC5v2XoKyFKKQ4yHqv?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: c7f897b8-42e7-4d3a-128f-08dcf7bef61e X-MS-Exchange-CrossTenant-AuthSource: AM0PR04MB7044.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Oct 2024 02:11:12.7830 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: W3dn8lbCqCxgSjOVvjn+jTQN5KDBIDhICxvyLn9t08LZfhWJ4Rw53/4gpIam1YcuiKl57lEysImMOyP3EsQ+Rg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB9147 Content-Type: text/plain; charset="utf-8" Implement the ASRC memory to memory function using the compress framework, user can use this function with compress ioctl interface. This feature can be shared by ASRC and EASRC drivers Signed-off-by: Shengjiu Wang Acked-by: Jaroslav Kysela --- sound/soc/fsl/Kconfig | 1 + sound/soc/fsl/Makefile | 2 +- sound/soc/fsl/fsl_asrc_common.h | 9 + sound/soc/fsl/fsl_asrc_m2m.c | 727 ++++++++++++++++++++++++++++++++ 4 files changed, 738 insertions(+), 1 deletion(-) create mode 100644 sound/soc/fsl/fsl_asrc_m2m.c diff --git a/sound/soc/fsl/Kconfig b/sound/soc/fsl/Kconfig index e283751abfef..bff9c6bda344 100644 --- a/sound/soc/fsl/Kconfig +++ b/sound/soc/fsl/Kconfig @@ -8,6 +8,7 @@ config SND_SOC_FSL_ASRC depends on HAS_DMA select REGMAP_MMIO select SND_SOC_GENERIC_DMAENGINE_PCM + select SND_COMPRESS_ACCEL help Say Y if you want to add Asynchronous Sample Rate Converter (ASRC) support for the Freescale CPUs. diff --git a/sound/soc/fsl/Makefile b/sound/soc/fsl/Makefile index ad97244b5cc3..d656a9ab54e3 100644 --- a/sound/soc/fsl/Makefile +++ b/sound/soc/fsl/Makefile @@ -10,7 +10,7 @@ obj-$(CONFIG_SND_SOC_P1022_RDK) +=3D snd-soc-p1022-rdk.o # Freescale SSI/DMA/SAI/SPDIF Support snd-soc-fsl-audmix-y :=3D fsl_audmix.o snd-soc-fsl-asoc-card-y :=3D fsl-asoc-card.o -snd-soc-fsl-asrc-y :=3D fsl_asrc.o fsl_asrc_dma.o +snd-soc-fsl-asrc-y :=3D fsl_asrc.o fsl_asrc_dma.o fsl_asrc_m2m.o snd-soc-fsl-lpc3xxx-y :=3D lpc3xxx-pcm.o lpc3xxx-i2s.o snd-soc-fsl-sai-y :=3D fsl_sai.o snd-soc-fsl-ssi-y :=3D fsl_ssi.o diff --git a/sound/soc/fsl/fsl_asrc_common.h b/sound/soc/fsl/fsl_asrc_commo= n.h index 3bdd6ea07c09..0cd595b0f629 100644 --- a/sound/soc/fsl/fsl_asrc_common.h +++ b/sound/soc/fsl/fsl_asrc_common.h @@ -60,6 +60,7 @@ struct fsl_asrc_m2m_cap { * @buf_len: buffer length of m2m * @dma_buffer: buffer pointers * @first_convert: start of conversion + * @ratio_mod_flag: flag for new ratio modifier * @ratio_mod: ratio modification */ struct fsl_asrc_pair { @@ -84,6 +85,7 @@ struct fsl_asrc_pair { unsigned int buf_len[2]; struct snd_dma_buffer dma_buffer[2]; unsigned int first_convert; + bool ratio_mod_flag; unsigned int ratio_mod; }; =20 @@ -98,6 +100,7 @@ struct fsl_asrc_pair { * @mem_clk: clock source to access register * @ipg_clk: clock source to drive peripheral * @spba_clk: SPBA clock (optional, depending on SoC design) + * @card: compress sound card * @lock: spin lock for resource protection * @pair: pair pointers * @channel_avail: non-occupied channel numbers @@ -131,6 +134,7 @@ struct fsl_asrc { struct clk *mem_clk; struct clk *ipg_clk; struct clk *spba_clk; + struct snd_card *card; spinlock_t lock; /* spin lock for resource protection */ =20 struct fsl_asrc_pair *pair[PAIR_CTX_NUM]; @@ -166,4 +170,9 @@ struct fsl_asrc { #define DRV_NAME "fsl-asrc-dai" extern struct snd_soc_component_driver fsl_asrc_component; =20 +int fsl_asrc_m2m_init(struct fsl_asrc *asrc); +void fsl_asrc_m2m_exit(struct fsl_asrc *asrc); +int fsl_asrc_m2m_resume(struct fsl_asrc *asrc); +int fsl_asrc_m2m_suspend(struct fsl_asrc *asrc); + #endif /* _FSL_ASRC_COMMON_H */ diff --git a/sound/soc/fsl/fsl_asrc_m2m.c b/sound/soc/fsl/fsl_asrc_m2m.c new file mode 100644 index 000000000000..693ec0a477a4 --- /dev/null +++ b/sound/soc/fsl/fsl_asrc_m2m.c @@ -0,0 +1,727 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// Copyright (C) 2014-2016 Freescale Semiconductor, Inc. +// Copyright (C) 2019-2024 NXP +// +// Freescale ASRC Memory to Memory (M2M) driver + +#include +#include +#include +#include +#include +#include +#include + +#include "fsl_asrc_common.h" + +#define DIR_STR(dir) (dir) =3D=3D IN ? "in" : "out" + +#define ASRC_xPUT_DMA_CALLBACK(dir) \ + (((dir) =3D=3D IN) ? asrc_input_dma_callback \ + : asrc_output_dma_callback) + +/* Maximum output and capture buffer size */ +#define ASRC_M2M_BUFFER_SIZE (512 * 1024) + +/* Maximum output and capture period size */ +#define ASRC_M2M_PERIOD_SIZE (48 * 1024) + +/* dma complete callback */ +static void asrc_input_dma_callback(void *data) +{ + struct fsl_asrc_pair *pair =3D (struct fsl_asrc_pair *)data; + + complete(&pair->complete[IN]); +} + +/* dma complete callback */ +static void asrc_output_dma_callback(void *data) +{ + struct fsl_asrc_pair *pair =3D (struct fsl_asrc_pair *)data; + + complete(&pair->complete[OUT]); +} + +/** + *asrc_read_last_fifo: read all the remaining data from FIFO + *@pair: Structure pointer of fsl_asrc_pair + *@dma_vaddr: virtual address of capture buffer + *@length: payload length of capture buffer + */ +static void asrc_read_last_fifo(struct fsl_asrc_pair *pair, void *dma_vadd= r, u32 *length) +{ + struct fsl_asrc *asrc =3D pair->asrc; + enum asrc_pair_index index =3D pair->index; + u32 i, reg, size, t_size =3D 0, width; + u32 *reg32 =3D NULL; + u16 *reg16 =3D NULL; + u8 *reg24 =3D NULL; + + width =3D snd_pcm_format_physical_width(pair->sample_format[OUT]); + if (width =3D=3D 32) + reg32 =3D dma_vaddr + *length; + else if (width =3D=3D 16) + reg16 =3D dma_vaddr + *length; + else + reg24 =3D dma_vaddr + *length; +retry: + size =3D asrc->get_output_fifo_size(pair); + if (size + *length > ASRC_M2M_BUFFER_SIZE) + goto end; + + for (i =3D 0; i < size * pair->channels; i++) { + regmap_read(asrc->regmap, asrc->get_fifo_addr(OUT, index), ®); + if (reg32) { + *reg32++ =3D reg; + } else if (reg16) { + *reg16++ =3D (u16)reg; + } else { + *reg24++ =3D (u8)reg; + *reg24++ =3D (u8)(reg >> 8); + *reg24++ =3D (u8)(reg >> 16); + } + } + t_size +=3D size; + + /* In case there is data left in FIFO */ + if (size) + goto retry; +end: + /* Update payload length */ + if (reg32) + *length +=3D t_size * pair->channels * 4; + else if (reg16) + *length +=3D t_size * pair->channels * 2; + else + *length +=3D t_size * pair->channels * 3; +} + +/* config dma channel */ +static int asrc_dmaconfig(struct fsl_asrc_pair *pair, + struct dma_chan *chan, + u32 dma_addr, dma_addr_t buf_addr, u32 buf_len, + int dir, int width) +{ + struct fsl_asrc *asrc =3D pair->asrc; + struct device *dev =3D &asrc->pdev->dev; + struct dma_slave_config slave_config; + enum dma_slave_buswidth buswidth; + unsigned int sg_len, max_period_size; + struct scatterlist *sg; + int ret, i; + + switch (width) { + case 8: + buswidth =3D DMA_SLAVE_BUSWIDTH_1_BYTE; + break; + case 16: + buswidth =3D DMA_SLAVE_BUSWIDTH_2_BYTES; + break; + case 24: + buswidth =3D DMA_SLAVE_BUSWIDTH_3_BYTES; + break; + case 32: + buswidth =3D DMA_SLAVE_BUSWIDTH_4_BYTES; + break; + default: + dev_err(dev, "invalid word width\n"); + return -EINVAL; + } + + memset(&slave_config, 0, sizeof(slave_config)); + if (dir =3D=3D IN) { + slave_config.direction =3D DMA_MEM_TO_DEV; + slave_config.dst_addr =3D dma_addr; + slave_config.dst_addr_width =3D buswidth; + slave_config.dst_maxburst =3D asrc->m2m_get_maxburst(IN, pair); + } else { + slave_config.direction =3D DMA_DEV_TO_MEM; + slave_config.src_addr =3D dma_addr; + slave_config.src_addr_width =3D buswidth; + slave_config.src_maxburst =3D asrc->m2m_get_maxburst(OUT, pair); + } + + ret =3D dmaengine_slave_config(chan, &slave_config); + if (ret) { + dev_err(dev, "failed to config dmaengine for %s task: %d\n", + DIR_STR(dir), ret); + return -EINVAL; + } + + max_period_size =3D rounddown(ASRC_M2M_PERIOD_SIZE, width * pair->channel= s / 8); + /* scatter gather mode */ + sg_len =3D buf_len / max_period_size; + if (buf_len % max_period_size) + sg_len +=3D 1; + + sg =3D kmalloc_array(sg_len, sizeof(*sg), GFP_KERNEL); + if (!sg) + return -ENOMEM; + + sg_init_table(sg, sg_len); + for (i =3D 0; i < (sg_len - 1); i++) { + sg_dma_address(&sg[i]) =3D buf_addr + i * max_period_size; + sg_dma_len(&sg[i]) =3D max_period_size; + } + sg_dma_address(&sg[i]) =3D buf_addr + i * max_period_size; + sg_dma_len(&sg[i]) =3D buf_len - i * max_period_size; + + pair->desc[dir] =3D dmaengine_prep_slave_sg(chan, sg, sg_len, + slave_config.direction, + DMA_PREP_INTERRUPT); + kfree(sg); + if (!pair->desc[dir]) { + dev_err(dev, "failed to prepare dmaengine for %s task\n", DIR_STR(dir)); + return -EINVAL; + } + + pair->desc[dir]->callback =3D ASRC_xPUT_DMA_CALLBACK(dir); + pair->desc[dir]->callback_param =3D pair; + + return 0; +} + +/* main function of converter */ +static void asrc_m2m_device_run(struct fsl_asrc_pair *pair, struct snd_com= pr_task_runtime *task) +{ + struct fsl_asrc *asrc =3D pair->asrc; + struct device *dev =3D &asrc->pdev->dev; + enum asrc_pair_index index =3D pair->index; + struct snd_dma_buffer *src_buf, *dst_buf; + unsigned int in_buf_len; + unsigned int out_dma_len; + unsigned int width; + u32 fifo_addr; + int ret; + + /* set ratio mod */ + if (asrc->m2m_set_ratio_mod) { + if (pair->ratio_mod_flag) { + asrc->m2m_set_ratio_mod(pair, pair->ratio_mod); + pair->ratio_mod_flag =3D false; + } + } + + src_buf =3D &pair->dma_buffer[IN]; + dst_buf =3D &pair->dma_buffer[OUT]; + + width =3D snd_pcm_format_physical_width(pair->sample_format[IN]); + fifo_addr =3D asrc->paddr + asrc->get_fifo_addr(IN, index); + + in_buf_len =3D task->input_size; + + if (in_buf_len < width * pair->channels / 8 || + in_buf_len > ASRC_M2M_BUFFER_SIZE || + in_buf_len % (width * pair->channels / 8)) { + dev_err(dev, "out buffer size is error: [%d]\n", in_buf_len); + goto end; + } + + /* dma config for output dma channel */ + ret =3D asrc_dmaconfig(pair, + pair->dma_chan[IN], + fifo_addr, + src_buf->addr, + in_buf_len, IN, width); + if (ret) { + dev_err(dev, "out dma config error\n"); + goto end; + } + + width =3D snd_pcm_format_physical_width(pair->sample_format[OUT]); + fifo_addr =3D asrc->paddr + asrc->get_fifo_addr(OUT, index); + out_dma_len =3D asrc->m2m_calc_out_len(pair, in_buf_len); + if (out_dma_len > 0 && out_dma_len <=3D ASRC_M2M_BUFFER_SIZE) { + /* dma config for capture dma channel */ + ret =3D asrc_dmaconfig(pair, + pair->dma_chan[OUT], + fifo_addr, + dst_buf->addr, + out_dma_len, OUT, width); + if (ret) { + dev_err(dev, "cap dma config error\n"); + goto end; + } + } else if (out_dma_len > ASRC_M2M_BUFFER_SIZE) { + dev_err(dev, "cap buffer size error\n"); + goto end; + } + + reinit_completion(&pair->complete[IN]); + reinit_completion(&pair->complete[OUT]); + + /* Submit DMA request */ + dmaengine_submit(pair->desc[IN]); + dma_async_issue_pending(pair->desc[IN]->chan); + if (out_dma_len > 0) { + dmaengine_submit(pair->desc[OUT]); + dma_async_issue_pending(pair->desc[OUT]->chan); + } + + asrc->m2m_start(pair); + + if (!wait_for_completion_interruptible_timeout(&pair->complete[IN], 10 * = HZ)) { + dev_err(dev, "out DMA task timeout\n"); + goto end; + } + + if (out_dma_len > 0) { + if (!wait_for_completion_interruptible_timeout(&pair->complete[OUT], 10 = * HZ)) { + dev_err(dev, "cap DMA task timeout\n"); + goto end; + } + } + + /* read the last words from FIFO */ + asrc_read_last_fifo(pair, dst_buf->area, &out_dma_len); + /* update payload length for capture */ + task->output_size =3D out_dma_len; +end: + return; +} + +static int fsl_asrc_m2m_comp_open(struct snd_compr_stream *stream) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct device *dev =3D &asrc->pdev->dev; + struct fsl_asrc_pair *pair; + int size, ret; + + pair =3D kzalloc(sizeof(*pair) + asrc->pair_priv_size, GFP_KERNEL); + if (!pair) + return -ENOMEM; + + pair->private =3D (void *)pair + sizeof(struct fsl_asrc_pair); + pair->asrc =3D asrc; + + init_completion(&pair->complete[IN]); + init_completion(&pair->complete[OUT]); + + runtime->private_data =3D pair; + + size =3D ASRC_M2M_BUFFER_SIZE; + ret =3D snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &pair->dma_buf= fer[IN]); + if (ret) + goto error_alloc_in_buf; + + ret =3D snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &pair->dma_buf= fer[OUT]); + if (ret) + goto error_alloc_out_buf; + + ret =3D pm_runtime_get_sync(dev); + if (ret < 0) { + dev_err(dev, "Failed to power up asrc\n"); + goto err_pm_runtime; + } + + return 0; + +err_pm_runtime: + snd_dma_free_pages(&pair->dma_buffer[OUT]); +error_alloc_out_buf: + snd_dma_free_pages(&pair->dma_buffer[IN]); +error_alloc_in_buf: + kfree(pair); + return ret; +} + +static int fsl_asrc_m2m_comp_release(struct snd_compr_stream *stream) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + struct device *dev =3D &asrc->pdev->dev; + + pm_runtime_put_sync(dev); + + snd_dma_free_pages(&pair->dma_buffer[IN]); + snd_dma_free_pages(&pair->dma_buffer[OUT]); + + kfree(runtime->private_data); + + return 0; +} + +static int fsl_asrc_m2m_comp_set_params(struct snd_compr_stream *stream, + struct snd_compr_params *params) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + struct fsl_asrc_m2m_cap cap; + int ret, i; + + ret =3D asrc->m2m_get_cap(&cap); + if (ret) + return -EINVAL; + + if (pcm_format_to_bits(params->codec.format) & cap.fmt_in) + pair->sample_format[IN] =3D params->codec.format; + else + return -EINVAL; + + if (pcm_format_to_bits(params->codec.pcm_format) & cap.fmt_out) + pair->sample_format[OUT] =3D params->codec.pcm_format; + else + return -EINVAL; + + /* check input rate is in scope */ + for (i =3D 0; i < cap.rate_in_count; i++) + if (params->codec.sample_rate =3D=3D cap.rate_in[i]) { + pair->rate[IN] =3D params->codec.sample_rate; + break; + } + if (i =3D=3D cap.rate_in_count) + return -EINVAL; + + /* check output rate is in scope */ + for (i =3D 0; i < cap.rate_out_count; i++) + if (params->codec.options.src_d.out_sample_rate =3D=3D cap.rate_out[i]) { + pair->rate[OUT] =3D params->codec.options.src_d.out_sample_rate; + break; + } + if (i =3D=3D cap.rate_out_count) + return -EINVAL; + + if (params->codec.ch_in !=3D params->codec.ch_out || + params->codec.ch_in < cap.chan_min || + params->codec.ch_in > cap.chan_max) + return -EINVAL; + + pair->channels =3D params->codec.ch_in; + pair->buf_len[IN] =3D params->buffer.fragment_size; + pair->buf_len[OUT] =3D params->buffer.fragment_size; + + return 0; +} + +static int fsl_asrc_m2m_mmap(struct dma_buf *dmabuf, struct vm_area_struct= *vma) +{ + struct snd_dma_buffer *dmab =3D dmabuf->priv; + + return snd_dma_buffer_mmap(dmab, vma); +} + +static struct sg_table *fsl_asrc_m2m_map_dma_buf(struct dma_buf_attachment= *attachment, + enum dma_data_direction direction) +{ + struct snd_dma_buffer *dmab =3D attachment->dmabuf->priv; + struct sg_table *sgt; + + sgt =3D kmalloc(sizeof(*sgt), GFP_KERNEL); + if (!sgt) + return NULL; + + if (dma_get_sgtable(attachment->dev, sgt, dmab->area, dmab->addr, dmab->b= ytes) < 0) + goto free; + + if (dma_map_sgtable(attachment->dev, sgt, direction, 0)) + goto free; + + return sgt; + +free: + sg_free_table(sgt); + kfree(sgt); + return NULL; +} + +static void fsl_asrc_m2m_unmap_dma_buf(struct dma_buf_attachment *attachme= nt, + struct sg_table *table, + enum dma_data_direction direction) +{ + dma_unmap_sgtable(attachment->dev, table, direction, 0); +} + +static void fsl_asrc_m2m_release(struct dma_buf *dmabuf) +{ + /* buffer is released by fsl_asrc_m2m_comp_release() */ +} + +static const struct dma_buf_ops fsl_asrc_m2m_dma_buf_ops =3D { + .mmap =3D fsl_asrc_m2m_mmap, + .map_dma_buf =3D fsl_asrc_m2m_map_dma_buf, + .unmap_dma_buf =3D fsl_asrc_m2m_unmap_dma_buf, + .release =3D fsl_asrc_m2m_release, +}; + +static int fsl_asrc_m2m_comp_task_create(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + DEFINE_DMA_BUF_EXPORT_INFO(exp_info_in); + DEFINE_DMA_BUF_EXPORT_INFO(exp_info_out); + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + struct device *dev =3D &asrc->pdev->dev; + int ret; + + exp_info_in.ops =3D &fsl_asrc_m2m_dma_buf_ops; + exp_info_in.size =3D ASRC_M2M_BUFFER_SIZE; + exp_info_in.flags =3D O_RDWR; + exp_info_in.priv =3D &pair->dma_buffer[IN]; + task->input =3D dma_buf_export(&exp_info_in); + if (IS_ERR(task->input)) { + ret =3D PTR_ERR(task->input); + return ret; + } + + exp_info_out.ops =3D &fsl_asrc_m2m_dma_buf_ops; + exp_info_out.size =3D ASRC_M2M_BUFFER_SIZE; + exp_info_out.flags =3D O_RDWR; + exp_info_out.priv =3D &pair->dma_buffer[OUT]; + task->output =3D dma_buf_export(&exp_info_out); + if (IS_ERR(task->output)) { + ret =3D PTR_ERR(task->output); + return ret; + } + + /* Request asrc pair/context */ + ret =3D asrc->request_pair(pair->channels, pair); + if (ret) { + dev_err(dev, "failed to request pair: %d\n", ret); + goto err_request_pair; + } + + ret =3D asrc->m2m_prepare(pair); + if (ret) { + dev_err(dev, "failed to start pair part one: %d\n", ret); + goto err_start_part_one; + } + + /* Request dma channels */ + pair->dma_chan[IN] =3D asrc->get_dma_channel(pair, IN); + if (!pair->dma_chan[IN]) { + dev_err(dev, "[ctx%d] failed to get input DMA channel\n", pair->index); + ret =3D -EBUSY; + goto err_dma_channel_in; + } + + pair->dma_chan[OUT] =3D asrc->get_dma_channel(pair, OUT); + if (!pair->dma_chan[OUT]) { + dev_err(dev, "[ctx%d] failed to get output DMA channel\n", pair->index); + ret =3D -EBUSY; + goto err_dma_channel_out; + } + + return 0; + +err_dma_channel_out: + dma_release_channel(pair->dma_chan[IN]); +err_dma_channel_in: + if (asrc->m2m_unprepare) + asrc->m2m_unprepare(pair); +err_start_part_one: + asrc->release_pair(pair); +err_request_pair: + return ret; +} + +static int fsl_asrc_m2m_comp_task_start(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + + asrc_m2m_device_run(pair, task); + + return 0; +} + +static int fsl_asrc_m2m_comp_task_stop(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + return 0; +} + +static int fsl_asrc_m2m_comp_task_free(struct snd_compr_stream *stream, + struct snd_compr_task_runtime *task) +{ + struct fsl_asrc *asrc =3D stream->private_data; + struct snd_compr_runtime *runtime =3D stream->runtime; + struct fsl_asrc_pair *pair =3D runtime->private_data; + + /* Stop & release pair/context */ + if (asrc->m2m_stop) + asrc->m2m_stop(pair); + + if (asrc->m2m_unprepare) + asrc->m2m_unprepare(pair); + asrc->release_pair(pair); + + /* Release dma channel */ + if (pair->dma_chan[IN]) + dma_release_channel(pair->dma_chan[IN]); + if (pair->dma_chan[OUT]) + dma_release_channel(pair->dma_chan[OUT]); + + return 0; +} + +static int fsl_asrc_m2m_get_caps(struct snd_compr_stream *cstream, + struct snd_compr_caps *caps) +{ + caps->num_codecs =3D 1; + caps->min_fragment_size =3D 4096; + caps->max_fragment_size =3D 4096; + caps->min_fragments =3D 1; + caps->max_fragments =3D 1; + caps->codecs[0] =3D SND_AUDIOCODEC_PCM; + + return 0; +} + +static int fsl_asrc_m2m_fill_codec_caps(struct fsl_asrc *asrc, + struct snd_compr_codec_caps *codec) +{ + struct fsl_asrc_m2m_cap cap; + snd_pcm_format_t k; + int j =3D 0; + int ret; + + ret =3D asrc->m2m_get_cap(&cap); + if (ret) + return -EINVAL; + + pcm_for_each_format(k) { + if (pcm_format_to_bits(k) & cap.fmt_in) { + codec->descriptor[j].max_ch =3D cap.chan_max; + memcpy(codec->descriptor[j].sample_rates, + cap.rate_in, + cap.rate_in_count * sizeof(__u32)); + codec->descriptor[j].num_sample_rates =3D cap.rate_in_count; + codec->descriptor[j].formats =3D k; + codec->descriptor[j].pcm_formats =3D cap.fmt_out; + codec->descriptor[j].src.out_sample_rate_min =3D cap.rate_out[0]; + codec->descriptor[j].src.out_sample_rate_max =3D + cap.rate_out[cap.rate_out_count - 1]; + j++; + } + } + + codec->codec =3D SND_AUDIOCODEC_PCM; + codec->num_descriptors =3D j; + return 0; +} + +static int fsl_asrc_m2m_get_codec_caps(struct snd_compr_stream *stream, + struct snd_compr_codec_caps *codec) +{ + struct fsl_asrc *asrc =3D stream->private_data; + + return fsl_asrc_m2m_fill_codec_caps(asrc, codec); +} + +static struct snd_compr_ops fsl_asrc_m2m_compr_ops =3D { + .open =3D fsl_asrc_m2m_comp_open, + .free =3D fsl_asrc_m2m_comp_release, + .set_params =3D fsl_asrc_m2m_comp_set_params, + .get_caps =3D fsl_asrc_m2m_get_caps, + .get_codec_caps =3D fsl_asrc_m2m_get_codec_caps, + .task_create =3D fsl_asrc_m2m_comp_task_create, + .task_start =3D fsl_asrc_m2m_comp_task_start, + .task_stop =3D fsl_asrc_m2m_comp_task_stop, + .task_free =3D fsl_asrc_m2m_comp_task_free, +}; + +int fsl_asrc_m2m_suspend(struct fsl_asrc *asrc) +{ + struct fsl_asrc_pair *pair; + int i; + + for (i =3D 0; i < PAIR_CTX_NUM; i++) { + pair =3D asrc->pair[i]; + if (!pair) + continue; + if (!completion_done(&pair->complete[IN])) { + if (pair->dma_chan[IN]) + dmaengine_terminate_all(pair->dma_chan[IN]); + asrc_input_dma_callback((void *)pair); + } + if (!completion_done(&pair->complete[OUT])) { + if (pair->dma_chan[OUT]) + dmaengine_terminate_all(pair->dma_chan[OUT]); + asrc_output_dma_callback((void *)pair); + } + + if (asrc->m2m_pair_suspend) + asrc->m2m_pair_suspend(pair); + } + + return 0; +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_suspend); + +int fsl_asrc_m2m_resume(struct fsl_asrc *asrc) +{ + struct fsl_asrc_pair *pair; + int i; + + for (i =3D 0; i < PAIR_CTX_NUM; i++) { + pair =3D asrc->pair[i]; + if (!pair) + continue; + if (asrc->m2m_pair_resume) + asrc->m2m_pair_resume(pair); + } + + return 0; +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_resume); + +int fsl_asrc_m2m_init(struct fsl_asrc *asrc) +{ + struct device *dev =3D &asrc->pdev->dev; + struct snd_card *card; + struct snd_compr *compr; + int ret; + + ret =3D snd_card_new(dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, + THIS_MODULE, 0, &card); + if (ret < 0) + return ret; + + strscpy(card->driver, "fsl-asrc-m2m", sizeof(card->driver)); + strscpy(card->shortname, "ASRC-M2M", sizeof(card->shortname)); + strscpy(card->longname, "ASRC-M2M", sizeof(card->shortname)); + + asrc->card =3D card; + + compr =3D devm_kzalloc(dev, sizeof(*compr), GFP_KERNEL); + if (!compr) { + ret =3D -ENOMEM; + goto err; + } + + compr->ops =3D &fsl_asrc_m2m_compr_ops; + compr->private_data =3D asrc; + + ret =3D snd_compress_new(card, 0, SND_COMPRESS_ACCEL, "ASRC M2M", compr); + if (ret < 0) + goto err; + + ret =3D snd_card_register(card); + if (ret < 0) + goto err; + + return 0; +err: + snd_card_free(card); + return ret; +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_init); + +void fsl_asrc_m2m_exit(struct fsl_asrc *asrc) +{ + struct snd_card *card =3D asrc->card; + + snd_card_free(card); +} +EXPORT_SYMBOL_GPL(fsl_asrc_m2m_exit); + +MODULE_IMPORT_NS(DMA_BUF); +MODULE_AUTHOR("Shengjiu Wang "); +MODULE_DESCRIPTION("Freescale ASRC M2M driver"); +MODULE_LICENSE("GPL"); --=20 2.34.1