From nobody Mon May 25 05:54:20 2026 Received: from smtp-usa2.onexmail.com (smtp-usa2.onexmail.com [35.173.142.173]) (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 474A63E00BA; Mon, 18 May 2026 06:23:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=35.173.142.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779085385; cv=none; b=KtUFAJVN7lYSCHm8kwvwiWKFReNx1u0K3fmYuLOzUzWypinN0S6twMhRER+6vuXjBQSW4Vaz4N/y4nDyDOyYcPyYG3ydqFJIctXdH9F2zuPdgLmuFjlN20Z1QoGeTxevETLENHdZA9xPu86ufyDLBl/fTxiZK0cuTGNMANtiJ3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779085385; c=relaxed/simple; bh=HevkQyYoy3kiO1VrBKioDTilzos+sth9T5xZNgNnIUQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rgQrDrL+rGIKTOe0YDZ+B75ukprL8VspIb0ECHCoEk7p+S9hMk7i/bF+qvE1NU51yEB3hvzGhzDOseqpv16ZwDcH1XJkM1NmSfqTjj1PaiV1LH0MzIRu99SiYFx0RYcZXApztms7bmNiu/t2K/hH5kwC4eJ4ZGPdRRf0fdWabs8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=linux.spacemit.com; spf=none smtp.mailfrom=linux.spacemit.com; dkim=pass (1024-bit key) header.d=linux.spacemit.com header.i=@linux.spacemit.com header.b=PTI6OXlx; arc=none smtp.client-ip=35.173.142.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=linux.spacemit.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.spacemit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.spacemit.com header.i=@linux.spacemit.com header.b="PTI6OXlx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.spacemit.com; s=mxsw2412; t=1779085285; bh=AA/l8RpNRgQWhJKMhQO6AtegRn3p6g7Rx5XU5wAB/G8=; h=From:Date:Subject:MIME-Version:Message-Id:To; b=PTI6OXlxOVP/3sYqdbM3gO4oc3QBMQ4uArvE+og9Q5gOpOly7j699peydJxgUoD6I rkooglGYvDleFuGRTsK1IEO3QDKjyxjHcIuX64mwlwxdIoKndBKM0Os6NUi3OvdTl6 W9TFSyC0HgpHDyhz2usRfXBD295Z11+ifWuDJesg= X-QQ-mid: zesmtpsz4t1779085276tbc16854b X-QQ-Originating-IP: XUOY8olp5tWvrlcinXAzUJujrciY5GNrZ0gVtvOuPtE= Received: from = ( [61.145.255.150]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 18 May 2026 14:21:13 +0800 (CST) X-QQ-SSF: 0000000000000000000000000000000 X-QQ-GoodBg: 0 X-BIZMAIL-ID: 11071802599520518211 EX-QQ-RecipientCnt: 11 From: Troy Mitchell Date: Mon, 18 May 2026 14:21:08 +0800 Subject: [PATCH v3 1/2] ASoC: soc-dai: add shared BCLK clock for cross-DAI rate constraints Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260518-i2s-same-blk-v3-1-2ba1c4f41db9@linux.spacemit.com> References: <20260518-i2s-same-blk-v3-0-2ba1c4f41db9@linux.spacemit.com> In-Reply-To: <20260518-i2s-same-blk-v3-0-2ba1c4f41db9@linux.spacemit.com> To: Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai , Yixun Lan Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, spacemit@lists.linux.dev, jinmei.wei@spacemit.com, Troy Mitchell X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779085269; l=3103; i=troy.mitchell@linux.spacemit.com; s=20250710; h=from:subject:message-id; bh=HevkQyYoy3kiO1VrBKioDTilzos+sth9T5xZNgNnIUQ=; b=0IB0x7GcAP8a0HCvrjlANPcpstzssXGwMcptOr/ixP8/iqljPrvm5R9iiLtsY0xva1rIR+QHl GjCdwcuPFwRA94tJJpBskjKkVIyWsS/R/arkgsbaUbChGsLsZMa+fJ7 X-Developer-Key: i=troy.mitchell@linux.spacemit.com; a=ed25519; pk=lQa7BzLrq8DfZnChqmwJ5qQk8fP2USmY/4xZ2/MSsXc= X-QQ-SENDSIZE: 520 Feedback-ID: zesmtpsz:linux.spacemit.com:qybglogicsvrgz:qybglogicsvrgz3a-0 X-QQ-XMAILINFO: M0cNKL/w7T3m97YO+J8aR9iEEYv19JCfUV5XuTDHb8c0A7YRLceqnzcL Xfw4at0nWZIPK2Dfp+u34yVRCYgjkC3/Jn5hb2CChO07oary3M2pEE+6H2rWwhzoL/r0xxM cZ4TTR1qupZlPEmfZIq9GbFd+PsfCC0izVUUv4sYZlYVC43nfZfz/6txdWRUPh3C7IqUlD4 zF6RkaJi0O4FaDMbIAkbLttO3nSVOr42H2c0i7exbbvS2vS6s531YcMjEib/0e2x+Ntwn2D AEknCNMpVxncOJqm9EoHTofi7hRNs9/BhZXwpb7iaV+4dH4AqTYkW+K5bhYx27Wnpreo0fw xawWbiVQTBXk3/YWRjQyrtQXl7+LlGyNjraDuwYg/y3mmoL9k7O69gIp7dY7bjKNzWJlmh4 MH4PU38NIsW5+wvpiOkaCD8B8Swv7Qmv43Uwu05JvOIYrnrFPRL02U4nyxb6s2BlsY4i4aj oOs0J4It90ANPRlpVJcqHnXhVLki52DBIkr+T90FtC3sldyRjERpFgAaOk7WumsH+CywLSf i8MY7UJW5xfKVCSrakmmHdMEl2r4MN2ZPfCc0DO7DX2QjU0sO56/UDmYY3LN6IwIKYI/ewJ PEPwFcQ494Kp6flUWdyjPwtKRUg4TQnM9pMoULRNrnkH6I7metMxV5tzh7xULXzb2rkuc1J v92vtjqkOG7qhJhJOl/sOtMyfTyDqKlbniPJ7h6Z//6VU5BTW80T9Fi4DCXnS4EjgCp3D6f TZRWzjOXz99hUBqQaMC9Fy4oAdIuhKHufPQ79oPJ1mWJ26CAnN4XY86qzqO0ve8mE0+i4Ij yhvqWApWNAYec+sPpcMHLGlCCndnfsyX9XkxZMeUQ/Nsk48FP0aIKvr+etm47dvy7EqpxDV vDMXzmWhh+vab216EK7NJIXnGhz1HaxVCU7XiqY81AVP4S5qLDTQcVEWubxtgh+dfGajKVl uFpr4vOBd3Rtyr2fG3+XCWAMja1Zwao2VQt3ya9pchSrZUsbmQzS4iIHX0IuuFNQ9M9dMoI KtEk79u8hIOfWZlgHE1dfpSTYrFX4SQ8lo9RF38zNRr5tnv/B2rq5e0clpCCP+cqUliJXhb kD+gWGdK3N1O0kTdGIg2CDbia/LYrTWdu8nSdJk89mqvytPAvff0XjjqjLqiF06JQBZYOiZ WHyr X-QQ-XMRINFO: M/715EihBoGS47X28/vv4NpnfpeBLnr4Qg== X-QQ-RECHKSPAM: 0 Add a bclk field to struct snd_soc_dai and a helper function snd_soc_dai_set_bclk_clk() that platform drivers can use to declare which clock is their BCLK. Also cache the bclk_ratio in snd_soc_dai_set_bclk_ratio() so that the framework can use it later in hw_rule evaluation for TDM configurations where BCLK =3D rate * slots * slot_width. When multiple DAIs on the same card share the same physical BCLK (detected via clk_is_match()), the ASoC core can automatically constrain their hw_params so that the resulting BCLK rates are compatible. This commit adds the data structure support; the actual constraint logic follows in the next patch. Signed-off-by: Troy Mitchell --- include/sound/soc-dai.h | 7 +++++++ sound/soc/soc-dai.c | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/sound/soc-dai.h b/include/sound/soc-dai.h index 6a42812bba8c..df010a91b350 100644 --- a/include/sound/soc-dai.h +++ b/include/sound/soc-dai.h @@ -17,6 +17,7 @@ struct snd_pcm_substream; struct snd_soc_dapm_widget; struct snd_compr_stream; +struct clk; =20 /* * DAI hardware audio formats. @@ -188,6 +189,8 @@ int snd_soc_dai_set_pll(struct snd_soc_dai *dai, =20 int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio= ); =20 +void snd_soc_dai_set_bclk_clk(struct snd_soc_dai *dai, struct clk *bclk); + /* Digital Audio interface formatting */ int snd_soc_dai_get_fmt_max_priority(const struct snd_soc_pcm_runtime *rtd= ); u64 snd_soc_dai_get_fmt(const struct snd_soc_dai *dai, int priority); @@ -473,6 +476,10 @@ struct snd_soc_dai { unsigned int symmetric_channels; unsigned int symmetric_sample_bits; =20 + /* shared BCLK clock for cross-DAI rate constraints */ + struct clk *bclk; + unsigned int bclk_ratio; /* BCLK =3D rate * bclk_ratio (0 =3D use channel= s * sample_bits) */ + /* parent platform/codec */ struct snd_soc_component *component; =20 diff --git a/sound/soc/soc-dai.c b/sound/soc/soc-dai.c index 2f370fda1266..1719ddcefa4b 100644 --- a/sound/soc/soc-dai.c +++ b/sound/soc/soc-dai.c @@ -116,10 +116,28 @@ int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *da= i, unsigned int ratio) dai->driver->ops->set_bclk_ratio) ret =3D dai->driver->ops->set_bclk_ratio(dai, ratio); =20 + if (!ret) + dai->bclk_ratio =3D ratio; + return soc_dai_ret(dai, ret); } EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio); =20 +/** + * snd_soc_dai_set_bclk_clk - set the BCLK clock for shared clock detection + * @dai: DAI + * @bclk: BCLK clock pointer (or NULL to clear) + * + * When multiple DAIs share the same physical BCLK (detected via + * clk_is_match()), the ASoC core will automatically constrain their + * hw_params so that the resulting BCLK rates are compatible. + */ +void snd_soc_dai_set_bclk_clk(struct snd_soc_dai *dai, struct clk *bclk) +{ + dai->bclk =3D bclk; +} +EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_clk); + int snd_soc_dai_get_fmt_max_priority(const struct snd_soc_pcm_runtime *rtd) { struct snd_soc_dai *dai; --=20 2.54.0 From nobody Mon May 25 05:54:20 2026 Received: from smtpbgau2.qq.com (smtpbgau2.qq.com [54.206.34.216]) (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 64C053E2ADB; Mon, 18 May 2026 06:22:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=54.206.34.216 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779085369; cv=none; b=DqLbP6EvljFcL0q/SnXgGktSDHqdLruZQb45apdmBIdB8hOgXUTVxXsPtkTyI1VmB8lgQNUfCVUftHg1sj6Ln5L0SrGvaZPOeWrfWiyQ65uCEh+9if6cRfOCGzB1pFVRPSZ0e1YZXMwRyBaixWXUpacUp7ThcGDSCiPbvnJTzF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779085369; c=relaxed/simple; bh=dHcuwQewOQ91+lcIOJFibASbfSOLIkUa8mNyTgSGWBs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EFiComwakDiQq8YcMgMWh9jNDhHJo+K5EeWvRsVngqUzwuMcfWkDvZ2MhEx3SX2dtzZBUbLG+pg7999C/CMyyPH6tTliT02vfzPrQt4HzWpkzXdsIYT4BRnNB9cjsdvH8jqZQAAQMmV5fYEiW2Vo0t7kPAkqtlp1GPPEOTogS8s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=linux.spacemit.com; spf=none smtp.mailfrom=linux.spacemit.com; dkim=pass (1024-bit key) header.d=linux.spacemit.com header.i=@linux.spacemit.com header.b=jstzffJx; arc=none smtp.client-ip=54.206.34.216 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=linux.spacemit.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.spacemit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.spacemit.com header.i=@linux.spacemit.com header.b="jstzffJx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.spacemit.com; s=mxsw2412; t=1779085288; bh=MzLAGIAWkK+XCchAd4hGeXE2CHkEuJGkm4cHQuRIBJ0=; h=From:Date:Subject:MIME-Version:Message-Id:To; b=jstzffJxK8VTfUEGmM5FnTWoPh/mmOmV9akrGQkPng+SOHZbd1YEISiXWOb+fO9vy yiV4Hr0btWdRMu9SqmYYaHnxT8lvxPymkUAomcgslCszm/uxeVLibZ1GcoSUStJ+yL Ac3NAq5P7cQVsYmzDhw3iKvNtUIukyS/GsG7A+NA= X-QQ-mid: zesmtpsz8t1779085280t3c7af7b1 X-QQ-Originating-IP: E3Ys73emsUdUyf9XkmiQ0SIGqXhmKVAS1Gx4p/Hd35g= Received: from = ( [61.145.255.150]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 18 May 2026 14:21:17 +0800 (CST) X-QQ-SSF: 0000000000000000000000000000000 X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1184573455148698779 EX-QQ-RecipientCnt: 11 From: Troy Mitchell Date: Mon, 18 May 2026 14:21:09 +0800 Subject: [PATCH v3 2/2] ASoC: soc-pcm: constrain hw_params when DAIs share the same BCLK Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260518-i2s-same-blk-v3-2-2ba1c4f41db9@linux.spacemit.com> References: <20260518-i2s-same-blk-v3-0-2ba1c4f41db9@linux.spacemit.com> In-Reply-To: <20260518-i2s-same-blk-v3-0-2ba1c4f41db9@linux.spacemit.com> To: Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai , Yixun Lan Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, spacemit@lists.linux.dev, jinmei.wei@spacemit.com, Troy Mitchell X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779085269; l=5785; i=troy.mitchell@linux.spacemit.com; s=20250710; h=from:subject:message-id; bh=dHcuwQewOQ91+lcIOJFibASbfSOLIkUa8mNyTgSGWBs=; b=6D8ZuFt4bPxPHExkd2VotXaHhHDkhYQIpQWWLctSzGnwYoo5vGnXrVA+KDrcBHyUi0VT6bZRN cCCTk/mVDvxA2/rSOjSYgWCjJbwhHGuyVCyIClLZ27dHpzhyBTdJd9J X-Developer-Key: i=troy.mitchell@linux.spacemit.com; a=ed25519; pk=lQa7BzLrq8DfZnChqmwJ5qQk8fP2USmY/4xZ2/MSsXc= X-QQ-SENDSIZE: 520 Feedback-ID: zesmtpsz:linux.spacemit.com:qybglogicsvrgz:qybglogicsvrgz3a-0 X-QQ-XMAILINFO: OcFnI1CoIwQYRA1yQVDrADRzvBuxzly4b/O9GzOrBTjZ5CCTvLm+4ZMU RsTZ1jlUx+fi207Su7vlKwLrnDRFsipQyc1SVIE69myzeLHlPrZbEtrkI3DyRfswnAP4SrP 1pIlaft3S1d6UqPJD60tqM37lhBs9pMZwZAnaWLzvOREXuyhIODN+Mfv6bEneKEaPAF/Wrk 760WAi6WlCkSZCibSyPFS/H92975BLZxpFAzAvk+OU4aq9Uc/t5d4oBtwPXT+kXn5BFlOLW i8lS92a3WV/C1iWN7VVi77vctKbygqgDAnqMyj7pbl364NbDuO/zOe+Wq5WXgYvql5Xa1j6 rSm+mmIgxaguHkPnxaLjp3uDFfS/5XWyXA+7bskauhfdD8PDtBK79bipfTBmozmYQZeCPlB 359OPbONXt3iLhg887gRmJoLNDT1x+y5b6Z6sG1X/AzwMQWW52kyI7CyXS4aynxG55p/l1+ wMbFOrwE9plfKhc+2fidOh6Re5aeygW3yzwGdC5VCVu1vvZjTFFoEpc/npn3UZicQulX0wc Rwad62a4jRIykaGodaww46yRGBDG6K4fKez/8Lm4lEA8VqDLsSaKX8dgt4cyHSIIFA0fZOy dgJk/uOz2XmoNjy7YXmlX3Rf2/T0lwEuQqh1PwC46LTiE7HPVUeMYOR7G4BHT8KNDzJk32r r8pJkXJCu26WVJfWh9bFd6HZlT3t23JdwTwZNWpZTTaJmF1tNo19/FPqWv/EwIBfyYgcqWq LAM3QAsZlNzZIXafAwywLfyL5A0v+uumJsofeKF9/q/PStPrC/xN4Jp7SutpYlWfWdkUjql 6x3ZRZWl3wP/Ht20kF7vyvwLpn1JpMZwJu7kvdJb/oevX9g2Lhh5cbEhgpcaJuoEawcfFg4 MIQ51CzEtsn1gRQUj9iA9Xgeya6xzh5diX6HPTtXMM03Q/ybfhdzXGb8N59jFpy31rAEoNi RvHC85xDPZ6IKWL3QLZWavUk8WWRQhsuvI//BzZ7o0Kj+uvAOzXsHN+k9YUlcCfZcz1E7lw owars6muFN/8Ttvhdq3ZZIby+CcZlvN9yphHEXi5l5ieLAhuzCmb1gA+/9lUzekshKa6//o n+An9BPtlLK44t/p7bVrXTh+JFuhcb23q1kmt6gUyxHhC3ANSWMOIqaYSWnfv3uUw== X-QQ-XMRINFO: M/715EihBoGS47X28/vv4NpnfpeBLnr4Qg== X-QQ-RECHKSPAM: 0 When multiple CPU DAIs on the same sound card share the same physical BCLK, add a hw_rule during PCM open that constrains the sample rate so the resulting BCLK rate stays consistent across all sharing DAIs. The hw_rule is registered unconditionally for every DAI that has a bclk clock pointer set. The actual peer scanning happens inside the rule callback at hw_refine time: it walks all DAIs on the card, looking for an active peer that shares the same physical BCLK (via clk_is_match()) and has already completed hw_params (checked via dai->symmetric_rate !=3D 0). This ensures the constraint uses the real BCLK rate established by the peer's clk_set_rate() in hw_params, not a stale boot-time default. The first DAI to complete hw_params is unconstrained (no active peer yet); subsequent DAIs are constrained to match. The rule supports two modes: - If the DAI has an explicit bclk_ratio set (e.g. for TDM where BCLK =3D rate * slots * slot_width), the rate is constrained to active_bclk_rate / bclk_ratio. - Otherwise, the default formula BCLK =3D rate * channels * sample_bits is used to derive the valid rate range. The constraint is purely additive: DAIs that do not set a bclk clock pointer are completely unaffected. Signed-off-by: Troy Mitchell --- sound/soc/soc-pcm.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 119 insertions(+) diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c index 9b12eedb77c3..e75c343b8d4e 100644 --- a/sound/soc/soc-pcm.c +++ b/sound/soc/soc-pcm.c @@ -12,6 +12,7 @@ =20 #include #include +#include #include #include #include @@ -467,6 +468,117 @@ static int soc_pcm_apply_symmetry(struct snd_pcm_subs= tream *substream, return 0; } =20 +/* + * Shared BCLK constraint: when multiple DAIs share the same physical BCLK, + * constrain hw_params so that the BCLK rate (rate * channels * sample_bit= s, + * or rate * slots * slot_width for TDM) remains consistent. + */ + +static int soc_pcm_shared_bclk_rule_rate(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) +{ + struct snd_soc_dai *dai =3D rule->private; + struct snd_soc_card *card =3D dai->component->card; + struct snd_soc_pcm_runtime *rtd; + struct snd_soc_dai *other_dai; + unsigned long active_bclk_rate =3D 0; + struct snd_interval *rate =3D hw_param_interval(params, SNDRV_PCM_HW_PARA= M_RATE); + struct snd_interval constraint =3D { .empty =3D 1 }; + unsigned int target_rate; + int i; + + /* + * Protect the rtd list traversal. No nesting: this callback runs + * from snd_pcm_hw_refine() which does not hold card->mutex. + */ + guard(mutex)(&card->mutex); + + /* Scan all DAIs on the card for an active peer sharing the same BCLK */ + for_each_card_rtds(card, rtd) { + for_each_rtd_cpu_dais(rtd, i, other_dai) { + if (other_dai =3D=3D dai) + continue; + if (!other_dai->bclk) + continue; + if (!snd_soc_dai_active(other_dai)) + continue; + /* + * Skip peers whose hw_params hasn't run yet. + * symmetric_rate is set by soc_pcm_set_dai_params() + * after snd_soc_dai_hw_params(), so non-zero means + * the DAI's clk_set_rate() has already executed. + */ + if (!other_dai->symmetric_rate) + continue; + if (!clk_is_match(dai->bclk, other_dai->bclk)) + continue; + + active_bclk_rate =3D clk_get_rate(other_dai->bclk); + if (active_bclk_rate) + goto found; + } + } + + return 0; + +found: + if (dai->bclk_ratio) { + /* + * Driver has set an explicit BCLK ratio (e.g. for TDM where + * BCLK =3D rate * slots * slot_width). The only valid rate is + * active_bclk_rate / bclk_ratio. + */ + target_rate =3D active_bclk_rate / dai->bclk_ratio; + + constraint.min =3D target_rate; + constraint.max =3D target_rate; + } else { + struct snd_interval *channels =3D hw_param_interval(params, + SNDRV_PCM_HW_PARAM_CHANNELS); + struct snd_interval *sample_bits =3D hw_param_interval(params, + SNDRV_PCM_HW_PARAM_SAMPLE_BITS); + + /* + * Default: BCLK =3D rate * channels * sample_bits. + * Calculate the range of valid rates given the current + * channel and sample_bits intervals. + */ + if (!channels->min || !sample_bits->min) + return 0; + + constraint.max =3D active_bclk_rate / + ((unsigned long)channels->min * sample_bits->min); + + if (channels->max && sample_bits->max) + constraint.min =3D active_bclk_rate / + ((unsigned long)channels->max * sample_bits->max); + else + constraint.min =3D constraint.max; + } + + constraint.integer =3D 1; + constraint.empty =3D 0; + + return snd_interval_refine(rate, &constraint); +} + +static int soc_pcm_apply_shared_bclk(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + if (!dai->bclk) + return 0; + + dev_dbg(dai->dev, + "ASoC: registering shared BCLK rate constraint\n"); + + return snd_pcm_hw_rule_add(substream->runtime, 0, + SNDRV_PCM_HW_PARAM_RATE, + soc_pcm_shared_bclk_rule_rate, dai, + SNDRV_PCM_HW_PARAM_CHANNELS, + SNDRV_PCM_HW_PARAM_SAMPLE_BITS, + -1); +} + static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) { @@ -903,6 +1015,13 @@ static int __soc_pcm_open(struct snd_soc_pcm_runtime = *rtd, if (ret !=3D 0) goto err; } + + /* Shared BCLK constraint across DAIs on the same card */ + for_each_rtd_cpu_dais(rtd, i, dai) { + ret =3D soc_pcm_apply_shared_bclk(substream, dai); + if (ret !=3D 0) + goto err; + } dynamic: snd_soc_runtime_activate(rtd, substream->stream); ret =3D 0; --=20 2.54.0