Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v4 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards
@ 2026-12-04  3:13 Joseph Telaak
  2026-12-04  3:13 ` [PATCH v4 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV Joseph Telaak
  2026-12-04  3:13 ` [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
  0 siblings, 2 replies; 4+ messages in thread
From: Joseph Telaak @ 2026-12-04  3:13 UTC (permalink / raw)
  To: Jarkko Nikula, Sen Wang, Liam Girdwood, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-omap, linux-kernel, Joseph Telaak

With simple-audio-card or audio-graph-card, McBSP as clock provider runs
its bit clock at the full SRG input, since only a machine driver can set
its divider. Patch 2 adds set_tdm_slot() so these cards can ask for a
frame of slots x slot_width bit clocks; patch 1 lets the divider use all
8 bits of CLKGDV.

The ADV7513 in the test takes S32_LE only with a companion adv7511
patch:
https://lore.kernel.org/r/20261116-adv7511-s32-v1-1-40ae8228db42@telaak.dev

---
Changes in v4:
- 2/2: the SRG source comes from the clk_id set_dai_sysclk() stored,
  not the register cache (Jarkko). Only OMAP_MCBSP_SYSCLK_CLKS_FCLK is
  checked against the fclk rate, which set_dai_sysclk() already refuses
  on OMAP1, and slots are off with a reference clock on CLKX or CLKR.
- Tested again on the same board, including mclk-fs being refused.
- Link to v3: https://patch.msgid.link/20261130-omap-mcbsp-tdm-slot-v3-0-f2ab4752fb03@telaak.dev

Changes in v3:
- Rebased onto asoc for-next 8cb402ec2c74.
- 2/2: dropped slot_sysclk: with slots, set_dai_sysclk() refuses a new
  frequency while a stream is configured, until its hw_free() (Jarkko).
  Only with slots: McBSP stays configured until the last close, and
  omap3pandora sets 256 x rate in each hw_params(), so a rate change on
  one open would fail.
- 2/2: dropped sysclk_fclk: the SRG source comes from the register
  cache, and from CLKS the sysclk must be the fclk rate (Jarkko). The
  check is in hw_params(), not set_dai_sysclk(): simple-audio-card sets
  the sysclk at init, before the format and the slots.
- 2/2: slots apply only when McBSP drives both bit clock pins from the
  SRG, and the fclk check is skipped on OMAP1, where CLKS is the pin.
- 2/2: the DAI's remove() clears the slots, so they don't outlive the
  card that set them.
- Tested again on the same board, including mclk-fs being refused.
- Link to v2: https://patch.msgid.link/20261123-omap-mcbsp-tdm-slot-v2-0-3e3aae215952@telaak.dev

Changes in v2:
- 2/2: hw_params() checks the stream against the slots and the sysclk
  (Mark).
- 2/2: rates are limited at open only for a sysclk set outside a stream,
  as simple-audio-card does at init; a sysclk a card sets in hw_params()
  is checked there (Mark).
- 2/2: both directions share the SRG, so the second is held to the
  first's rate and sysclk until the first's hw_free().
- 2/2: channels are pinned to the active slots, non-empty TX and RX masks
  need the same weight, and unsupported slots are reported.
- 2/2: a sysclk from the internal fclk must be that clock's rate, so
  simple-card's mclk-fs (rate x fs) is refused instead of misprogrammed.
- 2/2: dropped Jarkko's Acked-by since the patch changed.
- 1/2: commit message wording only; keeps Jarkko's Acked-by.
- Not supported: dai-tdm-slot-width-map switching.
- Not tested on hardware: full duplex and a sysclk set in hw_params();
  the test board has playback only.
- Link to v1: https://patch.msgid.link/20261116-omap-mcbsp-tdm-slot-v1-0-6c9f7aa45a29@telaak.dev

---
Joseph Telaak (2):
      ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV
      ASoC: ti: omap-mcbsp: Implement set_tdm_slot

 sound/soc/ti/omap-mcbsp-priv.h |  14 +-
 sound/soc/ti/omap-mcbsp.c      | 351 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 360 insertions(+), 5 deletions(-)
---
base-commit: 92c8b08bb15f44e23aa616700a380293a3eb0c35
change-id: 20261115-omap-mcbsp-tdm-slot-7ee61348226b

Best regards,
--  
Joseph Telaak <joseph@telaak.dev>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV
  2026-12-04  3:13 [PATCH v4 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards Joseph Telaak
@ 2026-12-04  3:13 ` Joseph Telaak
  2026-12-04  3:13 ` [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
  1 sibling, 0 replies; 4+ messages in thread
From: Joseph Telaak @ 2026-12-04  3:13 UTC (permalink / raw)
  To: Jarkko Nikula, Sen Wang, Liam Girdwood, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-omap, linux-kernel, Joseph Telaak

CLKGDV is bits 7:0 of SRGR1, as its comment says and as set_clkdiv()
intends with CLKGDV(0xff), but the macro masks the value with 0x7f, so
a divider above 128 is programmed 128 lower.

Mask with 0xff. The only set_clkdiv() user, omap3pandora, divides by 8,
so nothing changes for it; this lets the set_tdm_slot() support that
follows use dividers up to 256.

Acked-by: Jarkko Nikula <jarkko.nikula@bitmer.com>
Signed-off-by: Joseph Telaak <joseph@telaak.dev>
---
 sound/soc/ti/omap-mcbsp-priv.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/soc/ti/omap-mcbsp-priv.h b/sound/soc/ti/omap-mcbsp-priv.h
index da519ea1f303..324c5d7d431d 100644
--- a/sound/soc/ti/omap-mcbsp-priv.h
+++ b/sound/soc/ti/omap-mcbsp-priv.h
@@ -136,7 +136,7 @@ enum {
 #define XPHASE			BIT(15)
 
 /************************* McBSP SRGR1 bit definitions ************************/
-#define CLKGDV(value)		((value) & 0x7f)		/* Bits 0:7 */
+#define CLKGDV(value)		((value) & 0xff)		/* Bits 0:7 */
 #define FWID(value)		(((value) & 0xff) << 8)	/* Bits 8:15 */
 
 /************************* McBSP SRGR2 bit definitions ************************/

-- 
Git-155)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot
  2026-12-04  3:13 [PATCH v4 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards Joseph Telaak
  2026-12-04  3:13 ` [PATCH v4 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV Joseph Telaak
@ 2026-12-04  3:13 ` Joseph Telaak
  2026-12-04  3:41   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Joseph Telaak @ 2026-12-04  3:13 UTC (permalink / raw)
  To: Jarkko Nikula, Sen Wang, Liam Girdwood, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-omap, linux-kernel, Joseph Telaak

As bit clock and frame provider, McBSP sizes the frame from the SRG
input, the set_clkdiv() divider and the rate. Only a machine driver can
call set_clkdiv(), so with simple-audio-card or audio-graph-card the
divider stays 1 and the bit clock runs at the full SRG input: 96 MHz on
OMAP3, a 2000-bit frame at 48 kHz.

Implement set_tdm_slot() so these cards can ask for a frame of slots x
slot_width bit clocks with dai-tdm-slot-num and dai-tdm-slot-width.
hw_params() then picks the divider for the rate. McBSP sends a frame's
words back to back, so a sample must fill its slot and the channels fill
the active slots, which must be the low ones. Both directions share the
frame, so non-empty TX and RX masks need the same number of slots, and
I2S or left-justified need two slots. Slots McBSP can't do are refused
when they are set, and a slot count I2S can't use at open. As clock
consumer, the slots are checked but have no effect.

startup() pins the channels and sample width to the slots. If the sysclk
was set outside a stream, as simple-audio-card does at init, it also
limits the rates to those the sysclk divides to within 0.1%, the IEC
60958 level II clock tolerance. hw_params() checks the stream against
the slots and the sysclk, including one a card sets in hw_params(). With
the SRG on the internal fclk, the sysclk must be that clock's rate:
McBSP has no MCLK output, so simple-card's mclk-fs, which passes rate x
fs, is refused instead of giving a wrong bit clock. This is checked in
hw_params(), not in set_dai_sysclk(): simple-audio-card sets the sysclk
before the format and the slots.

Both directions share the sample rate generator: once a stream is
configured, until its hw_free(), the other direction must use the same
rate and the sysclk can't change, not even for that stream's next
hw_params(). Otherwise a stream on its own reprograms McBSP on each
hw_params(). The slots can't change while McBSP is configured. This
applies only when McBSP provides both clocks from the SRG, which is why
the core's symmetric_rate isn't used: as clock consumer, the directions
can run at different rates. Without slots nothing changes.

Tested on an OMAP3630 board whose McBSP4 drives the I2S input of an
ADV7513 with no MCLK, with 2 x 32-bit slots and adv7511 taking S32_LE: a
left-only and a right-only 44.1 kHz tone each play on their own side (96
MHz / 34 gives 44117.6 Hz). 48 kHz (0.8% off), S16_LE and mono are
refused, and plughw converts them. With simple-audio-card,mclk-fs set to
256, hw_params() refuses the 11.2896 and 12.288 MHz sysclks.

Signed-off-by: Joseph Telaak <joseph@telaak.dev>
---
 sound/soc/ti/omap-mcbsp-priv.h |  12 ++
 sound/soc/ti/omap-mcbsp.c      | 351 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 359 insertions(+), 4 deletions(-)

diff --git a/sound/soc/ti/omap-mcbsp-priv.h b/sound/soc/ti/omap-mcbsp-priv.h
index 324c5d7d431d..9c49acfc8507 100644
--- a/sound/soc/ti/omap-mcbsp-priv.h
+++ b/sound/soc/ti/omap-mcbsp-priv.h
@@ -275,6 +275,18 @@ struct omap_mcbsp {
 	unsigned int latency[2];
 	int clk_div;
 	int wlen;
+	unsigned int slots;
+	unsigned int slot_width;
+	unsigned int tx_slots;
+	unsigned int rx_slots;
+	/* Rate the slots were configured for */
+	unsigned int slot_rate;
+	/* Directions configured with slots, BIT(stream) */
+	unsigned int slot_streams;
+	/* in_freq was set outside a stream */
+	bool sysclk_static;
+	/* clk_id of the last successful set_dai_sysclk() */
+	int sysclk_id;
 
 	struct pm_qos_request pm_qos_req;
 };
diff --git a/sound/soc/ti/omap-mcbsp.c b/sound/soc/ti/omap-mcbsp.c
index 6e0c7d7359f4..0f3925622106 100644
--- a/sound/soc/ti/omap-mcbsp.c
+++ b/sound/soc/ti/omap-mcbsp.c
@@ -759,12 +759,242 @@ static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
 	return snd_interval_refine(buffer_size, &frames);
 }
 
+static const unsigned int omap_mcbsp_slot_rates[] = {
+	8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200, 96000,
+};
+
+/*
+ * The SRG divider that runs a frame of slots x slot_width bit clocks at
+ * @rate, or 0 if none gets within 0.1% (IEC 60958 level II).
+ */
+static unsigned int omap_mcbsp_slot_div(struct omap_mcbsp *mcbsp,
+					unsigned int rate)
+{
+	unsigned int bclk = rate * mcbsp->slots * mcbsp->slot_width;
+	unsigned int div;
+
+	if (!bclk || !mcbsp->in_freq)
+		return 0;
+
+	div = DIV_ROUND_CLOSEST(mcbsp->in_freq, bclk);
+	if (!div || div > 256)
+		return 0;
+	if (abs_diff(mcbsp->in_freq / div, bclk) > bclk / 1000)
+		return 0;
+
+	return div;
+}
+
+static unsigned int omap_mcbsp_slot_rate_list(struct omap_mcbsp *mcbsp,
+					      unsigned int *list)
+{
+	unsigned int i, n = 0;
+
+	for (i = 0; i < ARRAY_SIZE(omap_mcbsp_slot_rates); i++)
+		if (omap_mcbsp_slot_div(mcbsp, omap_mcbsp_slot_rates[i]))
+			list[n++] = omap_mcbsp_slot_rates[i];
+
+	return n;
+}
+
+/*
+ * The sysclk the SRG divides, or 0 if it isn't usable. From the internal
+ * fclk it must be that clock's rate: McBSP has no MCLK output, and
+ * simple-card's mclk-fs passes rate x fs. Other SRG inputs are taken as set.
+ */
+static unsigned int omap_mcbsp_slot_sysclk(struct omap_mcbsp *mcbsp)
+{
+	unsigned long fclk = clk_get_rate(mcbsp->fclk);
+
+	if (mcbsp->sysclk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK && fclk &&
+	    mcbsp->in_freq != fclk)
+		return 0;
+
+	return mcbsp->in_freq;
+}
+
+/*
+ * Slots only apply when McBSP provides the bit clock and the frame from the
+ * SRG. With a reference clock in on CLKX or CLKR, that pin clocks its
+ * direction instead.
+ */
+static bool omap_mcbsp_slots_on(struct omap_mcbsp *mcbsp)
+{
+	return mcbsp->slot_width &&
+	       (mcbsp->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) ==
+	       SND_SOC_DAIFMT_BP_FP &&
+	       mcbsp->sysclk_id != OMAP_MCBSP_SYSCLK_CLKX_EXT &&
+	       mcbsp->sysclk_id != OMAP_MCBSP_SYSCLK_CLKR_EXT;
+}
+
+/* Whether the other direction has a stream configured with slots */
+static bool omap_mcbsp_slot_other(struct omap_mcbsp *mcbsp, int stream)
+{
+	return mcbsp->slot_streams & ~BIT(stream);
+}
+
+static int omap_mcbsp_slot_rate_refine(struct snd_pcm_hw_params *params,
+				       struct omap_mcbsp *mcbsp, int stream)
+{
+	struct snd_interval *rate = hw_param_interval(params,
+						      SNDRV_PCM_HW_PARAM_RATE);
+	unsigned int list[ARRAY_SIZE(omap_mcbsp_slot_rates)];
+	unsigned int n;
+
+	/* Slots cleared since open: no rates to compute */
+	if (!omap_mcbsp_slots_on(mcbsp))
+		return 0;
+	/* Both directions share the sample rate generator */
+	if (omap_mcbsp_slot_other(mcbsp, stream))
+		return snd_interval_list(rate, 1, &mcbsp->slot_rate, 0);
+	/* A sysclk a card sets in hw_params() is checked there */
+	if (!mcbsp->sysclk_static || !omap_mcbsp_slot_sysclk(mcbsp))
+		return 0;
+
+	n = omap_mcbsp_slot_rate_list(mcbsp, list);
+	return snd_interval_list(rate, n, list, 0);
+}
+
+static int omap_mcbsp_hwrule_tx_rate(struct snd_pcm_hw_params *params,
+				     struct snd_pcm_hw_rule *rule)
+{
+	return omap_mcbsp_slot_rate_refine(params, rule->private,
+					   SNDRV_PCM_STREAM_PLAYBACK);
+}
+
+static int omap_mcbsp_hwrule_rx_rate(struct snd_pcm_hw_params *params,
+				     struct snd_pcm_hw_rule *rule)
+{
+	return omap_mcbsp_slot_rate_refine(params, rule->private,
+					   SNDRV_PCM_STREAM_CAPTURE);
+}
+
+static unsigned int omap_mcbsp_slots_active(struct omap_mcbsp *mcbsp,
+					    int stream)
+{
+	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+		return mcbsp->tx_slots;
+	return mcbsp->rx_slots;
+}
+
+/* Checks that don't depend on the stream's params */
+static int omap_mcbsp_slot_check(struct omap_mcbsp *mcbsp, int stream)
+{
+	unsigned int format = mcbsp->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
+
+	if ((format == SND_SOC_DAIFMT_I2S || format == SND_SOC_DAIFMT_LEFT_J) &&
+	    mcbsp->slots != 2) {
+		dev_err(mcbsp->dev, "I2S/LEFT_J needs 2 slots, not %u\n",
+			mcbsp->slots);
+		return -EINVAL;
+	}
+	if (!omap_mcbsp_slots_active(mcbsp, stream)) {
+		dev_dbg(mcbsp->dev, "no %s slots in the TDM mask\n",
+			stream == SNDRV_PCM_STREAM_PLAYBACK ? "TX" : "RX");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/* The params against the slots, the sysclk and the other direction */
+static int omap_mcbsp_slot_params_check(struct omap_mcbsp *mcbsp, int stream,
+					struct snd_pcm_hw_params *params)
+{
+	unsigned int active = omap_mcbsp_slots_active(mcbsp, stream);
+	unsigned int rate = params_rate(params);
+	int ret;
+
+	ret = omap_mcbsp_slot_check(mcbsp, stream);
+	if (ret)
+		return ret;
+	if (params_channels(params) != active ||
+	    params_physical_width(params) != mcbsp->slot_width) {
+		dev_dbg(mcbsp->dev,
+			"%u x %u-bit samples do not fill %u x %u-bit slots\n",
+			params_channels(params), params_physical_width(params),
+			active, mcbsp->slot_width);
+		return -EINVAL;
+	}
+	if (!mcbsp->in_freq) {
+		dev_err(mcbsp->dev, "no sysclk set\n");
+		return -EINVAL;
+	}
+	if (!omap_mcbsp_slot_sysclk(mcbsp)) {
+		dev_err(mcbsp->dev, "sysclk %u Hz is not the %lu Hz fclk\n",
+			mcbsp->in_freq, clk_get_rate(mcbsp->fclk));
+		return -EINVAL;
+	}
+	if (omap_mcbsp_slot_other(mcbsp, stream) && rate != mcbsp->slot_rate) {
+		dev_dbg(mcbsp->dev, "%u Hz: other direction at %u Hz\n",
+			rate, mcbsp->slot_rate);
+		return -EINVAL;
+	}
+	if (!omap_mcbsp_slot_div(mcbsp, rate)) {
+		dev_dbg(mcbsp->dev, "no divider for %u Hz from sysclk %u Hz\n",
+			rate, mcbsp->in_freq);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/*
+ * With TDM slots the frame is slots x slot_width bit clocks. McBSP sends its
+ * words back to back, so a sample must fill its slot, the channels fill the
+ * active slots, and I2S/left-justified (one word per phase) need two slots.
+ */
+static int omap_mcbsp_slot_constraints(struct snd_pcm_substream *substream,
+				       struct omap_mcbsp *mcbsp)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	unsigned int active = omap_mcbsp_slots_active(mcbsp, substream->stream);
+	unsigned int list[ARRAY_SIZE(omap_mcbsp_slot_rates)];
+	snd_pcm_hw_rule_func_t rule;
+	int ret;
+
+	ret = omap_mcbsp_slot_check(mcbsp, substream->stream);
+	if (ret)
+		return ret;
+	if (mcbsp->sysclk_static && omap_mcbsp_slot_sysclk(mcbsp) &&
+	    !mcbsp->slot_streams &&
+	    !omap_mcbsp_slot_rate_list(mcbsp, list)) {
+		dev_err(mcbsp->dev, "no rate for %ux%u-bit slots from %u Hz\n",
+			mcbsp->slots, mcbsp->slot_width, mcbsp->in_freq);
+		return -EINVAL;
+	}
+
+	ret = snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
+					   active);
+	if (ret < 0)
+		return ret;
+	ret = snd_pcm_hw_constraint_single(runtime,
+					   SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
+					   mcbsp->slot_width);
+	if (ret < 0)
+		return ret;
+
+	/* Rates follow the slots, the sysclk and the other direction */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		rule = omap_mcbsp_hwrule_tx_rate;
+	else
+		rule = omap_mcbsp_hwrule_rx_rate;
+	return snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, rule,
+				   mcbsp, SNDRV_PCM_HW_PARAM_RATE, -1);
+}
+
 static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
 				  struct snd_soc_dai *cpu_dai)
 {
 	struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
 	int err = 0;
 
+	if (omap_mcbsp_slots_on(mcbsp)) {
+		err = omap_mcbsp_slot_constraints(substream, mcbsp);
+		if (err)
+			return err;
+	}
+
 	if (!snd_soc_dai_active(cpu_dai))
 		err = omap_mcbsp_request(mcbsp);
 
@@ -914,6 +1144,7 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
 	int pkt_size = 0;
 	unsigned int format, div, framesize, master;
 	unsigned int buffer_size = mcbsp->pdata->buffer_size;
+	bool slots = omap_mcbsp_slots_on(mcbsp);
 
 	dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
 	channels = params_channels(params);
@@ -928,6 +1159,15 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
 	default:
 		return -EINVAL;
 	}
+
+	if (slots) {
+		int ret = omap_mcbsp_slot_params_check(mcbsp, substream->stream,
+						       params);
+
+		if (ret)
+			return ret;
+	}
+
 	if (buffer_size) {
 		int latency;
 
@@ -972,8 +1212,14 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
 
 	dma_data->maxburst = pkt_size;
 
-	if (mcbsp->configured) {
-		/* McBSP already configured by another stream */
+	/*
+	 * McBSP already configured by another stream. With slots, that must be
+	 * the other direction: a stream on its own reprograms for its rate.
+	 */
+	if (mcbsp->configured &&
+	    (!slots || omap_mcbsp_slot_other(mcbsp, substream->stream))) {
+		if (slots)
+			mcbsp->slot_streams |= BIT(substream->stream);
 		return 0;
 	}
 
@@ -1020,7 +1266,14 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
 	/* In McBSP master modes, FRAME (i.e. sample rate) is generated
 	 * by _counting_ BCLKs. Calculate frame size in BCLKs */
 	master = mcbsp->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
-	if (master == SND_SOC_DAIFMT_BP_FP) {
+	if (slots) {
+		/* Checked against the slots above */
+		div = omap_mcbsp_slot_div(mcbsp, params_rate(params));
+		mcbsp->slot_rate = params_rate(params);
+		regs->srgr1	&= ~CLKGDV(0xff);
+		regs->srgr1	|= CLKGDV(div - 1);
+		framesize = mcbsp->slots * mcbsp->slot_width;
+	} else if (master == SND_SOC_DAIFMT_BP_FP) {
 		div = mcbsp->clk_div ? mcbsp->clk_div : 1;
 		framesize = (mcbsp->in_freq / div) / params_rate(params);
 
@@ -1051,6 +1304,18 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
 	omap_mcbsp_config(mcbsp, &mcbsp->cfg_regs);
 	mcbsp->wlen = wlen;
 	mcbsp->configured = 1;
+	if (slots)
+		mcbsp->slot_streams |= BIT(substream->stream);
+
+	return 0;
+}
+
+static int omap_mcbsp_dai_hw_free(struct snd_pcm_substream *substream,
+				  struct snd_soc_dai *cpu_dai)
+{
+	struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
+
+	mcbsp->slot_streams &= ~BIT(substream->stream);
 
 	return 0;
 }
@@ -1184,6 +1449,67 @@ static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
 	return 0;
 }
 
+/*
+ * With slots set, McBSP as bit clock and frame provider runs a frame of
+ * slots x slot_width bit clocks and picks the divider per rate instead of
+ * using set_clkdiv(). It sends a frame's words in order, so only masks of
+ * the low slots work, and both directions use the same number of them.
+ */
+static int omap_mcbsp_dai_set_tdm_slot(struct snd_soc_dai *cpu_dai,
+				       unsigned int tx_mask,
+				       unsigned int rx_mask,
+				       int slots, int slot_width)
+{
+	struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
+	unsigned int max_width = mcbsp->pdata->reg_size == 2 ? 16 : 32;
+	unsigned int tx = 0, rx = 0;
+
+	if (slots) {
+		tx = hweight32(tx_mask);
+		rx = hweight32(rx_mask);
+		/*
+		 * Up to 16 slots (channels_max), masks contiguous from slot 0
+		 * and, as both directions share the frame, of the same weight
+		 */
+		if (slots < 0 || slots > 16 ||
+		    (slot_width != 16 && slot_width != max_width) ||
+		    (tx_mask & ~GENMASK(slots - 1, 0)) ||
+		    (tx_mask & (tx_mask + 1)) ||
+		    (rx_mask & ~GENMASK(slots - 1, 0)) ||
+		    (rx_mask & (rx_mask + 1)) ||
+		    (!tx && !rx) || (tx && rx && tx != rx)) {
+			dev_err(mcbsp->dev,
+				"unsupported TDM slots %dx%d, masks %#x/%#x\n",
+				slots, slot_width, tx_mask, rx_mask);
+			return -EINVAL;
+		}
+	} else {
+		slot_width = 0;
+	}
+
+	/* The other direction may already run with the current slots */
+	if (mcbsp->configured) {
+		if (slots == mcbsp->slots && slot_width == mcbsp->slot_width &&
+		    tx == mcbsp->tx_slots && rx == mcbsp->rx_slots)
+			return 0;
+		return -EBUSY;
+	}
+
+	/* Back to the set_clkdiv() divider */
+	if (mcbsp->slot_width && !slot_width) {
+		mcbsp->cfg_regs.srgr1 &= ~CLKGDV(0xff);
+		mcbsp->cfg_regs.srgr1 |=
+			CLKGDV((mcbsp->clk_div ? mcbsp->clk_div : 1) - 1);
+	}
+
+	mcbsp->slots = slots;
+	mcbsp->slot_width = slot_width;
+	mcbsp->tx_slots = tx;
+	mcbsp->rx_slots = rx;
+
+	return 0;
+}
+
 static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
 					 int clk_id, unsigned int freq,
 					 int dir)
@@ -1192,13 +1518,19 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
 	struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
 	int err = 0;
 
-	if (mcbsp->active) {
+	/* With slots, a configured stream holds the sysclk until hw_free() */
+	if (mcbsp->active || mcbsp->slot_streams) {
 		if (freq == mcbsp->in_freq)
 			return 0;
 		else
 			return -EBUSY;
 	}
 
+	/* Set outside a stream, it holds for the next open */
+	if (!snd_soc_dai_active(cpu_dai))
+		mcbsp->sysclk_static = true;
+	else if (freq != mcbsp->in_freq)
+		mcbsp->sysclk_static = false;
 	mcbsp->in_freq = freq;
 	regs->srgr2 &= ~CLKSM;
 	regs->pcr0 &= ~SCLKME;
@@ -1243,6 +1575,8 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
 	default:
 		err = -ENODEV;
 	}
+	if (!err)
+		mcbsp->sysclk_id = clk_id;
 
 	return err;
 }
@@ -1266,6 +1600,13 @@ static int omap_mcbsp_remove(struct snd_soc_dai *dai)
 
 	pm_runtime_disable(mcbsp->dev);
 
+	/* The slots belong to the card that set them */
+	mcbsp->slots = 0;
+	mcbsp->slot_width = 0;
+	mcbsp->tx_slots = 0;
+	mcbsp->rx_slots = 0;
+	mcbsp->sysclk_static = false;
+
 	return 0;
 }
 
@@ -1288,9 +1629,11 @@ static const struct snd_soc_dai_ops mcbsp_dai_ops = {
 	.trigger	= omap_mcbsp_dai_trigger,
 	.delay		= omap_mcbsp_dai_delay,
 	.hw_params	= omap_mcbsp_dai_hw_params,
+	.hw_free	= omap_mcbsp_dai_hw_free,
 	.set_fmt	= omap_mcbsp_dai_set_dai_fmt,
 	.set_clkdiv	= omap_mcbsp_dai_set_clkdiv,
 	.set_sysclk	= omap_mcbsp_dai_set_dai_sysclk,
+	.set_tdm_slot	= omap_mcbsp_dai_set_tdm_slot,
 	.auto_selectable_formats	= &mcbsp_mcbsp_selectable_formats,
 	.num_auto_selectable_formats	= 1,
 };

-- 
Git-155)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot
  2026-12-04  3:13 ` [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
@ 2026-12-04  3:41   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-04  3:41 UTC (permalink / raw)
  To: Joseph Telaak; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ASoC: omap-mcbsp: data race in omap_mcbsp_slot_rate_refine()
- [Medium] ASoC: omap-mcbsp: sysclk state corrupted on error in
  set_dai_sysclk()
- [Medium] ASoC: omap-mcbsp: stale sysclk_id and in_freq left in
  omap_mcbsp_remove()
- [Medium] ASoC: omap-mcbsp: set_tdm_slot() latches dynamic slots and
  blocks post-hw_free
--

commit 730349fbef80ded0ca4e7f899480736b5797d744
Author: Joseph Telaak <joseph@telaak.dev>

ASoC: ti: omap-mcbsp: Implement set_tdm_slot

This commit implements the set_tdm_slot() DAI op for omap-mcbsp so generic
cards such as simple-audio-card can configure a frame of slots x slot_width
bit clocks and let hw_params() compute the SRG divider per sample rate.

> diff --git a/sound/soc/ti/omap-mcbsp.c b/sound/soc/ti/omap-mcbsp.c
> index 6e0c7d7359f49..0f3925622106b 100644
> --- a/sound/soc/ti/omap-mcbsp.c
> +++ b/sound/soc/ti/omap-mcbsp.c
> @@ -759,12 +759,242 @@ static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
>  	return snd_interval_refine(buffer_size, &frames);
>  }

[ ... ]

> +static int omap_mcbsp_slot_rate_refine(struct snd_pcm_hw_params *params,
> +				       struct omap_mcbsp *mcbsp, int stream)
> +{
> +	struct snd_interval *rate = hw_param_interval(params,
> +						      SNDRV_PCM_HW_PARAM_RATE);
> +	unsigned int list[ARRAY_SIZE(omap_mcbsp_slot_rates)];
> +	unsigned int n;
> +
> +	/* Slots cleared since open: no rates to compute */
> +	if (!omap_mcbsp_slots_on(mcbsp))
> +		return 0;
> +	/* Both directions share the sample rate generator */
> +	if (omap_mcbsp_slot_other(mcbsp, stream))
> +		return snd_interval_list(rate, 1, &mcbsp->slot_rate, 0);
> +	/* A sysclk a card sets in hw_params() is checked there */
> +	if (!mcbsp->sysclk_static || !omap_mcbsp_slot_sysclk(mcbsp))
> +		return 0;
> +
> +	n = omap_mcbsp_slot_rate_list(mcbsp, list);
> +	return snd_interval_list(rate, n, list, 0);
> +}

[Severity: Medium]
Can omap_mcbsp_slot_rate_refine() race with concurrent hw_params(),
hw_free(), or set_tdm_slot() calls on the opposite direction?

snd_pcm_hw_refine_with_lock() only holds the target substream's
runtime->buffer_mutex, while soc_pcm_hw_params() and soc_pcm_hw_free() hold
rtd->card->pcm_mutex.  Neither path takes mcbsp->lock, so on SMP OMAP4/5
systems the two directions access the shared slot and clock fields without
synchronization:

CPU0 (capture hw_refine)          CPU1 (playback hw_params)
snd_pcm_hw_refine_with_lock()     soc_pcm_hw_params()
  lock(&runtime->buffer_mutex)      lock(&card->pcm_mutex)
  omap_mcbsp_slot_rate_refine()     omap_mcbsp_dai_hw_params()
    if (omap_mcbsp_slot_other())
    snd_interval_list(..., &mcbsp->slot_rate, 0)
      min(..., list[0])               mcbsp->slot_rate = new_rate;
      max(..., list[0])  <-- data race / mismatched min & max

snd_interval_list() dereferences list[0] three times (snd_interval_test(),
min(), and max()), so a concurrent single-stream rate change overwriting
mcbsp->slot_rate can leave list_range.min > list_range.max and fail
refinement with -EINVAL.  Without ordering between mcbsp->slot_rate and
mcbsp->slot_streams, CPU0 can also see the new slot_streams bit with a stale
or zero slot_rate.

Similarly, concurrent set_tdm_slot() or set_dai_sysclk() updates can mutate
slots, slot_width, or in_freq across omap_mcbsp_slot_rate_refine() and
omap_mcbsp_slot_div(), which reads mcbsp->in_freq three times.  Protecting
these fields with mcbsp->lock and snapshotting slot_rate into a local
variable before calling snd_interval_list() would avoid the race.

[ ... ]

> @@ -1184,6 +1449,67 @@ static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
>  	return 0;
>  }

[ ... ]

> +	/* The other direction may already run with the current slots */
> +	if (mcbsp->configured) {
> +		if (slots == mcbsp->slots && slot_width == mcbsp->slot_width &&
> +		    tx == mcbsp->tx_slots && rx == mcbsp->rx_slots)
> +			return 0;
> +		return -EBUSY;
> +	}
> +
> +	/* Back to the set_clkdiv() divider */
> +	if (mcbsp->slot_width && !slot_width) {
> +		mcbsp->cfg_regs.srgr1 &= ~CLKGDV(0xff);
> +		mcbsp->cfg_regs.srgr1 |=
> +			CLKGDV((mcbsp->clk_div ? mcbsp->clk_div : 1) - 1);
> +	}
> +
> +	mcbsp->slots = slots;
> +	mcbsp->slot_width = slot_width;
> +	mcbsp->tx_slots = tx;
> +	mcbsp->rx_slots = rx;

[Severity: Medium]
Will dynamic per-stream set_tdm_slot() callers get locked into the first
stream's width and hit -EBUSY after hw_free()?

audio-graph-card2 with dai-tdm-slot-width-map calls snd_soc_dai_set_tdm_slot()
per stream from simple_util_hw_params() -> simple_set_tdm().  Unlike
set_dai_sysclk(), which tracks sysclk_static, set_tdm_slot() does not
distinguish init-time slots from dynamic ones and neither hw_free() nor
shutdown() clears them, so on later opens omap_mcbsp_slot_constraints() pins
CHANNELS and SAMPLE_BITS to the first stream's width and rejects other
formats before simple_set_tdm() can run.

In addition, omap_mcbsp_dai_hw_free() clears mcbsp->slot_streams while
mcbsp->configured stays set until shutdown().  Reconfiguring an open stream
via hw_free() -> hw_params() with a different slot width then fails with
-EBUSY here because this checks mcbsp->configured instead of
mcbsp->active || mcbsp->slot_streams.

> @@ -1192,13 +1518,19 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
>  	struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
>  	int err = 0;
>  
> -	if (mcbsp->active) {
> +	/* With slots, a configured stream holds the sysclk until hw_free() */
> +	if (mcbsp->active || mcbsp->slot_streams) {
>  		if (freq == mcbsp->in_freq)
>  			return 0;
>  		else
>  			return -EBUSY;
>  	}
>  
> +	/* Set outside a stream, it holds for the next open */
> +	if (!snd_soc_dai_active(cpu_dai))
> +		mcbsp->sysclk_static = true;
> +	else if (freq != mcbsp->in_freq)
> +		mcbsp->sysclk_static = false;
>  	mcbsp->in_freq = freq;
>  	regs->srgr2 &= ~CLKSM;
>  	regs->pcr0 &= ~SCLKME;
> @@ -1243,6 +1575,8 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
>  	default:
>  		err = -ENODEV;
>  	}
> +	if (!err)
> +		mcbsp->sysclk_id = clk_id;
>  
>  	return err;
>  }

[Severity: Medium]
Does omap_mcbsp_dai_set_dai_sysclk() leave sysclk_static, in_freq, and
cfg_regs out of sync with sysclk_id when setting the clock fails?

mcbsp->sysclk_static, mcbsp->in_freq, and the CLKSM/SCLKME bits in cfg_regs
are overwritten before switch (clk_id) runs, while only mcbsp->sysclk_id is
gated on !err.  If omap2_mcbsp_set_clks_src() fails or clk_id is unsupported,
later TDM slot checks evaluate the old sysclk_id against the rejected
frequency and sysclk_static flag.

Also, the early return when mcbsp->active || mcbsp->slot_streams is set only
checks freq == mcbsp->in_freq, so requesting a different clk_id at the same
frequency silently returns 0 instead of -EBUSY.

> @@ -1266,6 +1600,13 @@ static int omap_mcbsp_remove(struct snd_soc_dai *dai)
>  
>  	pm_runtime_disable(mcbsp->dev);
>  
> +	/* The slots belong to the card that set them */
> +	mcbsp->slots = 0;
> +	mcbsp->slot_width = 0;
> +	mcbsp->tx_slots = 0;
> +	mcbsp->rx_slots = 0;
> +	mcbsp->sysclk_static = false;
> +
>  	return 0;
>  }

[Severity: Medium]
Should omap_mcbsp_remove() also reset mcbsp->sysclk_id and mcbsp->in_freq?

struct omap_mcbsp persists across card unbind and rebind.  If a prior card
left OMAP_MCBSP_SYSCLK_CLKX_EXT or OMAP_MCBSP_SYSCLK_CLKR_EXT in sysclk_id,
omap_mcbsp_slots_on() returns false for a newly bound card and silently
disables TDM slot mode; a stale in_freq with OMAP_MCBSP_SYSCLK_CLKS_EXT
likewise bypasses the missing-sysclk and fclk checks in
omap_mcbsp_slot_params_check().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-omap-mcbsp-tdm-slot-v4-0-08985f4284f9@telaak.dev?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04  3:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  3:13 [PATCH v4 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards Joseph Telaak
2026-12-04  3:13 ` [PATCH v4 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV Joseph Telaak
2026-12-04  3:13 ` [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
2026-12-04  3:41   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox