mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards
@ 2026-12-01  0:00 Joseph Telaak
  2026-12-01  0:00 ` [PATCH v3 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV Joseph Telaak
  2026-12-01  0:01 ` [PATCH v3 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
  0 siblings, 2 replies; 5+ messages in thread
From: Joseph Telaak @ 2026-12-01  0:00 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 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 |  12 +-
 sound/soc/ti/omap-mcbsp.c      | 348 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 355 insertions(+), 5 deletions(-)
---
base-commit: 8cb402ec2c746f32b17e1aab14e900e7e45cb025
change-id: 20261115-omap-mcbsp-tdm-slot-7ee61348226b

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


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

* [PATCH v3 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV
  2026-12-01  0:00 [PATCH v3 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards Joseph Telaak
@ 2026-12-01  0:00 ` Joseph Telaak
  2026-12-01  0:01 ` [PATCH v3 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
  1 sibling, 0 replies; 5+ messages in thread
From: Joseph Telaak @ 2026-12-01  0:00 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	[flat|nested] 5+ messages in thread

* [PATCH v3 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot
  2026-12-01  0:00 [PATCH v3 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards Joseph Telaak
  2026-12-01  0:00 ` [PATCH v3 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV Joseph Telaak
@ 2026-12-01  0:01 ` Joseph Telaak
  2026-12-01 21:22   ` Jarkko Nikula
  1 sibling, 1 reply; 5+ messages in thread
From: Joseph Telaak @ 2026-12-01  0:01 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 CLKS, the fclk mux on OMAP2 and later, 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 |  10 ++
 sound/soc/ti/omap-mcbsp.c      | 348 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 354 insertions(+), 4 deletions(-)

diff --git a/sound/soc/ti/omap-mcbsp-priv.h b/sound/soc/ti/omap-mcbsp-priv.h
index 324c5d7d431d..621bb4d17879 100644
--- a/sound/soc/ti/omap-mcbsp-priv.h
+++ b/sound/soc/ti/omap-mcbsp-priv.h
@@ -275,6 +275,16 @@ 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;
 
 	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..3e74a367dd76 100644
--- a/sound/soc/ti/omap-mcbsp.c
+++ b/sound/soc/ti/omap-mcbsp.c
@@ -759,12 +759,241 @@ 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 CLKS, the fclk
+ * mux on OMAP2+, 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)
+{
+	struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
+	unsigned long fclk = clk_get_rate(mcbsp->fclk);
+
+	if (!mcbsp_omap1() && !(regs->srgr2 & CLKSM) &&
+	    !(regs->pcr0 & SCLKME) && fclk && mcbsp->in_freq != fclk)
+		return 0;
+
+	return mcbsp->in_freq;
+}
+
+/*
+ * Slots only apply when McBSP provides the bit clock and the frame, and
+ * drives both bit clock pins from the SRG
+ */
+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->cfg_regs.pcr0 & (CLKXM | CLKRM)) == (CLKXM | CLKRM);
+}
+
+/* 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 +1143,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 +1158,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 +1211,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 +1265,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 +1303,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 +1448,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 +1517,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;
@@ -1266,6 +1597,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 +1626,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	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot
  2026-12-01  0:01 ` [PATCH v3 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
@ 2026-12-01 21:22   ` Jarkko Nikula
  2026-12-01 22:33     ` Joseph Telaak
  0 siblings, 1 reply; 5+ messages in thread
From: Jarkko Nikula @ 2026-12-01 21:22 UTC (permalink / raw)
  To: Joseph Telaak, Sen Wang, Liam Girdwood, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, linux-omap, linux-kernel

Hi

On 12/1/26 2:01 AM, Joseph Telaak wrote:
> 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 CLKS, the fclk mux on OMAP2 and later, 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 |  10 ++
>   sound/soc/ti/omap-mcbsp.c      | 348 ++++++++++++++++++++++++++++++++++++++++-
>   2 files changed, 354 insertions(+), 4 deletions(-)
>
> diff --git a/sound/soc/ti/omap-mcbsp-priv.h b/sound/soc/ti/omap-mcbsp-priv.h
> index 324c5d7d431d..621bb4d17879 100644
> --- a/sound/soc/ti/omap-mcbsp-priv.h
> +++ b/sound/soc/ti/omap-mcbsp-priv.h
> @@ -275,6 +275,16 @@ 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;
>   
>   	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..3e74a367dd76 100644
> --- a/sound/soc/ti/omap-mcbsp.c
> +++ b/sound/soc/ti/omap-mcbsp.c
...
> +/*
> + * The sysclk the SRG divides, or 0 if it isn't usable. From CLKS, the fclk
> + * mux on OMAP2+, 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)
> +{
> +	struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
> +	unsigned long fclk = clk_get_rate(mcbsp->fclk);
> +
> +	if (!mcbsp_omap1() && !(regs->srgr2 & CLKSM) &&
> +	    !(regs->pcr0 & SCLKME) && fclk && mcbsp->in_freq != fclk)
> +		return 0;
> +
> +	return mcbsp->in_freq;
> +}
I think more cleaner would be to store clk_id in 
omap_mcbsp_dai_set_dai_sysclk() as omap1 is already validated there and 
no need to check these register bits here.

> +
> +/*
> + * Slots only apply when McBSP provides the bit clock and the frame, and
> + * drives both bit clock pins from the SRG
> + */
> +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->cfg_regs.pcr0 & (CLKXM | CLKRM)) == (CLKXM | CLKRM);
> +}
Is CLKXM | CLKRM test needed? Those should be set when the mcbsp->fmt 
has SND_SOC_DAIFMT_BP_FP?

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

* Re: [PATCH v3 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot
  2026-12-01 21:22   ` Jarkko Nikula
@ 2026-12-01 22:33     ` Joseph Telaak
  0 siblings, 0 replies; 5+ messages in thread
From: Joseph Telaak @ 2026-12-01 22:33 UTC (permalink / raw)
  To: Jarkko Nikula
  Cc: Sen Wang, Liam Girdwood, Mark Brown, Jaroslav Kysela,
	Takashi Iwai, linux-sound, linux-omap, linux-kernel

Hi Jarkko,

On 12/1/26, Jarkko Nikula wrote:
> I think more cleaner would be to store clk_id in
> omap_mcbsp_dai_set_dai_sysclk() as omap1 is already validated there and
> no need to check these register bits here.

OK, in v4 I will store clk_id and check the fclk rate only for
OMAP_MCBSP_SYSCLK_CLKS_FCLK, which drops the OMAP1 test too.

> Is CLKXM | CLKRM test needed? Those should be set when the mcbsp->fmt
> has SND_SOC_DAIFMT_BP_FP?

set_dai_fmt() sets them, but set_dai_sysclk() with
OMAP_MCBSP_SYSCLK_CLKX_EXT or OMAP_MCBSP_SYSCLK_CLKR_EXT clears CLKXM or
CLKRM again to take the reference clock in on that pin. That direction's
bit clock is then the pin's clock rather than the divided SRG output, so
the slots can't size its frame. With clk_id stored, v4 will check for
those two IDs instead of the bits.

Thanks,
Joseph

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

end of thread, other threads:[~2026-12-01 22:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01  0:00 [PATCH v3 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards Joseph Telaak
2026-12-01  0:00 ` [PATCH v3 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV Joseph Telaak
2026-12-01  0:01 ` [PATCH v3 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
2026-12-01 21:22   ` Jarkko Nikula
2026-12-01 22:33     ` Joseph Telaak

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

all inboxes | Powered by JetHome®