Skip to contentSkip to conversation

ASoC: davinci-mcasp: fixes and over/underrun handling

PATCH Applied Peter Ujfalusi 2 participants 15 messages Nov 10, 2014 – Nov 11, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover ASoC: davinci-mcasp: fixes and over/underrun handling

Hi,

Some fixes for the mcasp code:
- rx formating fix
- constraint for number of channels and sample bits

handling of over/underrun:
When such event happens (in McASP vs DMA level) we force restart the stream in order to get McASP and the DMA in sync again.

Regards,
Peter
---
Misael Lopez Cruz (2):
ASoC: davinci-mcasp: Active slots depend on active serializers
ASoC: davinci-mcasp: Add overrun/underrun event handling

Peter Ujfalusi (4):
ASoC: davinci-mcasp: Validate tdm_slots parameter at probe time
ASoC: davinci-mcasp: Place constraint on number of channels
ASoC: davinci-mcasp: Symmetric sample bits for IIS mode
ASoC: davinci-mcasp: Fix rx format when more bclk is used on the bus

 .../bindings/sound/davinci-mcasp-audio.txt         |   2 +-
 sound/soc/davinci/davinci-mcasp.c                  | 212 +++++++++++++++++++--
 sound/soc/davinci/davinci-mcasp.h                  |  10 +
 3 files changed, 210 insertions(+), 14 deletions(-)
Signature · 1 line
2.1.3
#1 lore raw

PATCH 1/6 ASoC: davinci-mcasp: Validate tdm_slots parameter at probe time

Instead of validating the tdm_slots parameter every time at hw_params we can do it once during probe. If the parameter is not valid (<2 or >32) print an error and fix it up.

---
 sound/soc/davinci/davinci-mcasp.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
sound/soc/davinci/davinci-mcasp.c +18 -10
index 6b1bfd9de57d..10c264738a0b 100644
@@ -632,13 +632,7 @@ static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream)
 	u32 mask = 0;
 	u32 busel = 0;
 
-	if ((mcasp->tdm_slots < 2) || (mcasp->tdm_slots > 32)) {
-		dev_err(mcasp->dev, "tdm slot %d not supported\n",
-			mcasp->tdm_slots);
-		return -EINVAL;
-	}
-
-	active_slots = (mcasp->tdm_slots > 31) ? 32 : mcasp->tdm_slots;
+	active_slots = mcasp->tdm_slots;
 	for (i = 0; i < active_slots; i++)
 		mask |= (1 << i);
 
@@ -650,12 +644,12 @@ static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream)
 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
 	mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
 	mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
-		       FSXMOD(mcasp->tdm_slots), FSXMOD(0x1FF));
+		       FSXMOD(active_slots), FSXMOD(0x1FF));
 
 	mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
 	mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
 	mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
-		       FSRMOD(mcasp->tdm_slots), FSRMOD(0x1FF));
+		       FSRMOD(active_slots), FSRMOD(0x1FF));
 
 	return 0;
 }
@@ -1237,7 +1231,21 @@ static int davinci_mcasp_probe(struct platform_device *pdev)
 	}
 
 	mcasp->op_mode = pdata->op_mode;
-	mcasp->tdm_slots = pdata->tdm_slots;
+	/* sanity check for tdm slots parameter */
+	if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
+		if (pdata->tdm_slots < 2) {
+			dev_err(&pdev->dev, "invalid tdm slots: %d\n",
+				pdata->tdm_slots);
+			mcasp->tdm_slots = 2;
+		} else if (pdata->tdm_slots > 32) {
+			dev_err(&pdev->dev, "invalid tdm slots: %d\n",
+				pdata->tdm_slots);
+			mcasp->tdm_slots = 32;
+		} else {
+			mcasp->tdm_slots = pdata->tdm_slots;
+		}
+	}
+
 	mcasp->num_serializer = pdata->num_serializer;
 #ifdef CONFIG_PM_SLEEP
 	mcasp->context.xrsr_regs = devm_kzalloc(&pdev->dev,
Signature · 1 line
2.1.3
maintainer Applied
#2 lore raw

Re: [PATCH 1/6] ASoC: davinci-mcasp: Validate tdm_slots parameter at probe time

Quoted 3 lines · Peter Ujfalusi

Instead of validating the tdm_slots parameter every time at hw_params we can do it once during probe. If the parameter is not valid (<2 or >32) print an error and fix it up.

Applied, thanks.

#3 lore raw

PATCH 2/6 ASoC: davinci-mcasp: Place constraint on number of channels

In reply to Peter Ujfalusi

In IIS (I2S, TDM, etc) mode the maximum number of allowed channels for either direction can be:
number of serializers for the direction * tdm_slots.
This constraint applicable for the first stream, while consequent stream should not have more channels then the first stream.

---
 sound/soc/davinci/davinci-mcasp.c | 60 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
sound/soc/davinci/davinci-mcasp.c +60 -0
index 10c264738a0b..10ae75e19d99 100644
@@ -90,6 +90,9 @@ struct davinci_mcasp {
 
 	bool	dat_port;
 
+	/* Used for comstraint setting on the second stream */
+	u32	channels;
+
 #ifdef CONFIG_PM_SLEEP
 	struct davinci_mcasp_context context;
 #endif
@@ -811,6 +814,9 @@ static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
 
 	davinci_config_channel_size(mcasp, word_length);
 
+	if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE)
+		mcasp->channels = channels;
+
 	return 0;
 }
 
@@ -839,7 +845,61 @@ static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
 	return ret;
 }
 
+static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
+				 struct snd_soc_dai *cpu_dai)
+{
+	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
+	u32 max_channels = 0;
+	int i, dir;
+
+	if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
+		return 0;
+
+	/*
+	 * Limit the maximum allowed channels for the first stream:
+	 * number of serializers for the direction * tdm slots per serializer
+	 */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		dir = TX_MODE;
+	else
+		dir = RX_MODE;
+
+	for (i = 0; i < mcasp->num_serializer; i++) {
+		if (mcasp->serial_dir[i] == dir)
+			max_channels++;
+	}
+	max_channels *= mcasp->tdm_slots;
+	/*
+	 * If the already active stream has less channels than the calculated
+	 * limnit based on the seirializers * tdm_slots, we need to use that as
+	 * a constraint for the second stream.
+	 * Otherwise (first stream or less allowed channels) we use the
+	 * calculated constraint.
+	 */
+	if (mcasp->channels && mcasp->channels < max_channels)
+		max_channels = mcasp->channels;
+
+	snd_pcm_hw_constraint_minmax(substream->runtime,
+				     SNDRV_PCM_HW_PARAM_CHANNELS,
+				     2, max_channels);
+	return 0;
+}
+
+static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
+				   struct snd_soc_dai *cpu_dai)
+{
+	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
+
+	if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
+		return;
+
+	if (!cpu_dai->active)
+		mcasp->channels = 0;
+}
+
 static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
+	.startup	= davinci_mcasp_startup,
+	.shutdown	= davinci_mcasp_shutdown,
 	.trigger	= davinci_mcasp_trigger,
 	.hw_params	= davinci_mcasp_hw_params,
 	.set_fmt	= davinci_mcasp_set_dai_fmt,
Signature · 1 line
2.1.3
maintainer Applied
#4 lore raw

Re: [PATCH 2/6] ASoC: davinci-mcasp: Place constraint on number of channels

Quoted 5 lines · Peter Ujfalusi

In IIS (I2S, TDM, etc) mode the maximum number of allowed channels for either direction can be:
number of serializers for the direction * tdm_slots.
This constraint applicable for the first stream, while consequent stream should not have more channels then the first stream.

Applied, thanks.

#5 lore raw

PATCH 3/6 ASoC: davinci-mcasp: Active slots depend on active serializers

In reply to Peter Ujfalusi

From: Misael Lopez Cruz <[email protected]>

Active slots count depends on the number of channels in the stream and the number of active serializers. Each serializer will handle at most the number of channels specified via 'tdm-slots' parameter in DT.

There are two possible scenarios:

- Single serializer: channel count fits in the max slots supported by McASP serializers, active slots is same as channel count

- Multiple serializers: channel count is bigger than max slots supported by a serializer. Channel count determines how many serializers are needed at their max slot count configuration

---
 sound/soc/davinci/davinci-mcasp.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
sound/soc/davinci/davinci-mcasp.c +22 -5
index 10ae75e19d99..a9822c7bbb0b 100644
@@ -629,13 +629,29 @@ static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
 	return 0;
 }
 
-static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream)
+static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
+			      int channels)
 {
 	int i, active_slots;
+	int total_slots;
+	int active_serializers;
 	u32 mask = 0;
 	u32 busel = 0;
 
-	active_slots = mcasp->tdm_slots;
+	total_slots = mcasp->tdm_slots;
+
+	/*
+	 * If more than one serializer is needed, then use them with
+	 * their specified tdm_slots count. Otherwise, one serializer
+	 * can cope with the transaction using as many slots as channels
+	 * in the stream, requires channels symmetry
+	 */
+	active_serializers = (channels + total_slots - 1) / total_slots;
+	if (active_serializers == 1)
+		active_slots = channels;
+	else
+		active_slots = total_slots;
+
 	for (i = 0; i < active_slots; i++)
 		mask |= (1 << i);
 
@@ -647,12 +663,12 @@ static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream)
 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
 	mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
 	mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
-		       FSXMOD(active_slots), FSXMOD(0x1FF));
+		       FSXMOD(total_slots), FSXMOD(0x1FF));
 
 	mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
 	mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
 	mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
-		       FSRMOD(active_slots), FSRMOD(0x1FF));
+		       FSRMOD(total_slots), FSRMOD(0x1FF));
 
 	return 0;
 }
@@ -766,7 +782,8 @@ static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
 	if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
 		ret = mcasp_dit_hw_param(mcasp, params_rate(params));
 	else
-		ret = mcasp_i2s_hw_param(mcasp, substream->stream);
+		ret = mcasp_i2s_hw_param(mcasp, substream->stream,
+					 channels);
 
 	if (ret)
 		return ret;
Signature · 1 line
2.1.3
maintainer Applied
#6 lore raw

Re: [PATCH 3/6] ASoC: davinci-mcasp: Active slots depend on active serializers

Quoted 6 lines · Peter Ujfalusi

From: Misael Lopez Cruz <[email protected]>

Active slots count depends on the number of channels in the stream and the number of active serializers. Each serializer will handle at most the number of channels specified via 'tdm-slots' parameter in DT.

Applied, thanks.

#7 lore raw

PATCH 4/6 ASoC: davinci-mcasp: Symmetric sample bits for IIS mode

In reply to Peter Ujfalusi

In IIS mode the tx and rx configuration is symmetric, the BCLK and FSYNC is shared.

---
 sound/soc/davinci/davinci-mcasp.c | 1 +
 1 file changed, 1 insertion(+)
sound/soc/davinci/davinci-mcasp.c +1 -0
index a9822c7bbb0b..40cf4bc447e6 100644
@@ -1033,6 +1033,7 @@ static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
 		},
 		.ops 		= &davinci_mcasp_dai_ops,
 
+		.symmetric_samplebits	= 1,
 	},
 	{
 		.name		= "davinci-mcasp.1",
Signature · 1 line
2.1.3
maintainer Applied
#8 lore raw

Re: [PATCH 4/6] ASoC: davinci-mcasp: Symmetric sample bits for IIS mode

Quoted 2 lines · Peter Ujfalusi

In IIS mode the tx and rx configuration is symmetric, the BCLK and FSYNC is shared.

Applied, thanks.

#9 lore raw

PATCH 5/6 ASoC: davinci-mcasp: Fix rx format when more bclk is used on the bus

In reply to Peter Ujfalusi

When the bus is configured to have more BCLK then the data type demands we need to use the rotation to move the data to correct place.

---
 sound/soc/davinci/davinci-mcasp.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
sound/soc/davinci/davinci-mcasp.c +11 -2
index 40cf4bc447e6..6b15a974f585 100644
@@ -494,8 +494,17 @@ static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
 	 * both left and right channels), so it has to be divided by number of
 	 * tdm-slots (for I2S - divided by 2).
 	 */
-	if (mcasp->bclk_lrclk_ratio)
-		word_length = mcasp->bclk_lrclk_ratio / mcasp->tdm_slots;
+	if (mcasp->bclk_lrclk_ratio) {
+		u32 slot_length = mcasp->bclk_lrclk_ratio / mcasp->tdm_slots;
+
+		/*
+		 * When we have more bclk then it is needed for the data, we
+		 * need to use the rotation to move the received samples to have
+		 * correct alignment.
+		 */
+		rx_rotate = (slot_length - word_length) / 4;
+		word_length = slot_length;
+	}
 
 	/* mapping of the XSSZ bit-field as described in the datasheet */
 	fmt = (word_length >> 1) - 1;
Signature · 1 line
2.1.3
maintainer Applied
#10 lore raw

Re: [PATCH 5/6] ASoC: davinci-mcasp: Fix rx format when more bclk is used on the bus

Quoted 2 lines · Peter Ujfalusi

When the bus is configured to have more BCLK then the data type demands we need to use the rotation to move the data to correct place.

Applied, thanks. These bug fixes should've been at the start of the series.

#11 lore raw

PATCH 6/6 ASoC: davinci-mcasp: Add overrun/underrun event handling

In reply to Peter Ujfalusi

From: Misael Lopez Cruz <[email protected]>

An underrun (playback) event occurs when the serializer transfer data from the XRBUF buffer to the XRSR shift register, but the XRBUF hasn't been filled. Similarly, the overrun (capture) event occurs when data from the XRSR shift register is transferred to the XRBUF but it hasn't been read yet.

These events are handled as XRUN events that cause the pcm to stop. The stream has to be explicitly restarted by the userspace which ensures that after stopping/starting McASP the data transfer is aligned with DMA. The other possibility was to internally stop and start McASP without DMA even knowing about it.

---
 .../bindings/sound/davinci-mcasp-audio.txt         |  2 +-
 sound/soc/davinci/davinci-mcasp.c                  | 91 ++++++++++++++++++++++
 sound/soc/davinci/davinci-mcasp.h                  | 10 +++
 3 files changed, 102 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/sound/davinci-mcasp-audio.txt +1 -1
index 60ca07996458..46bc9829c71a 100644
@@ -32,7 +32,7 @@ Optional properties:
 - rx-num-evt : FIFO levels.
 - sram-size-playback : size of sram to be allocated during playback
 - sram-size-capture  : size of sram to be allocated during capture
-- interrupts : Interrupt numbers for McASP, currently not used by the driver
+- interrupts : Interrupt numbers for McASP
 - interrupt-names : Known interrupt names are "tx" and "rx"
 - pinctrl-0: Should specify pin control group used for this controller.
 - pinctrl-names: Should contain only one value - "default", for more details
sound/soc/davinci/davinci-mcasp.c +91 -0

151 lines not on this page · raw message

sound/soc/davinci/davinci-mcasp.h +10 -0
index 9737108f0305..cbba1daa25ec 100644
@@ -285,6 +285,16 @@
 #define TXDATADMADIS	BIT(0)
 
 /*
+ * DAVINCI_MCASP_EVTCTLR_REG - Receiver Interrupt Control Register Bits
+ */
+#define ROVRN		BIT(0)
+
+/*
+ * DAVINCI_MCASP_EVTCTLX_REG - Transmitter Interrupt Control Register Bits
+ */
+#define XUNDRN		BIT(0)
+
+/*
  * DAVINCI_MCASP_W[R]FIFOCTL - Write/Read FIFO Control Register bits
  */
 #define FIFO_ENABLE	BIT(16)
Signature · 1 line
2.1.3
maintainer
#12 lore raw

Re: [PATCH 6/6] ASoC: davinci-mcasp: Add overrun/underrun event handling

Quoted 5 lines · Peter Ujfalusi
sound/soc/davinci/davinci-mcasp.c +5 -0
+	stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
+	if (stat & XUNDRN) {
+		dev_warn(mcasp->dev, "Transmit buffer underflow\n");
+		substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
+		if (substream) {

sound/soc/davinci/davinci-mcasp.c static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream) · line 312

Quoted 10 lines
sound/soc/davinci/davinci-mcasp.c +10 -0
+			snd_pcm_stream_lock_irq(substream);
+			if (snd_pcm_running(substream))
+				snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
+			snd_pcm_stream_unlock_irq(substream);
+		}
+	}
+
+	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, stat);
+
+	return IRQ_HANDLED;

This will unconditionally report that any interrupt that gets delivered is handled and acknowledged regardles of what was reported. This isn't ideal, I'd at least expect a warning to be printed if we're handling interrupts we don't understand.

#13 lore raw

Re: [PATCH 6/6] ASoC: davinci-mcasp: Add overrun/underrun event handling

Quoted 14 lines · Mark Brown
Quoted 12 lines · Peter Ujfalusi
sound/soc/davinci/davinci-mcasp.c +12 -0
+	stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
+	if (stat & XUNDRN) {
+		dev_warn(mcasp->dev, "Transmit buffer underflow\n");
+		substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
+		if (substream) {
+			snd_pcm_stream_lock_irq(substream);
+			if (snd_pcm_running(substream))
+				snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
+			snd_pcm_stream_unlock_irq(substream);
+		}
+	}
+
Quoted 8 lines

sound/soc/davinci/davinci-mcasp.c static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream) · line 312

Quoted 3 lines
sound/soc/davinci/davinci-mcasp.c +3 -0
+	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, stat);
+
+	return IRQ_HANDLED;

This will unconditionally report that any interrupt that gets delivered is handled and acknowledged regardles of what was reported. This isn't ideal, I'd at least expect a warning to be printed if we're handling interrupts we don't understand.

The setup code only enable interrupt generation for XUNDRN for tx or ROVRN for rx. If we receive the interrupt it is because either of these happened. We are not yet interested on the other bits in the status register and not all bits are signaling error.
If for example an XSYNCERR happens, we will not know about it till to point we have XUNDRN, which is going to trigger the interrupt.
If we would have prints for other bits, we should have at least strategy to deal with them.
At the end of the function we ack the interrupt to the HW.
Most probably we are going to handle more events, but right now we only care about under or overruns.

Signature · 1 line
Péter
maintainer
#14 lore raw

Re: [PATCH 6/6] ASoC: davinci-mcasp: Add overrun/underrun event handling

Quoted 11 lines · Peter Ujfalusi
Quoted 4 lines · Mark Brown

This will unconditionally report that any interrupt that gets delivered is handled and acknowledged regardles of what was reported. This isn't ideal, I'd at least expect a warning to be printed if we're handling interrupts we don't understand.

The setup code only enable interrupt generation for XUNDRN for tx or ROVRN for rx. If we receive the interrupt it is because either of these happened. We are not yet interested on the other bits in the status register and not all bits are signaling error.

Sure, I'm sure it's fine now - the goal here is to be robust against future changes.

Quoted 2 lines

If we would have prints for other bits, we should have at least strategy to deal with them.

The strategy I'm suggesting is to either log them so people know what's happening if they do go off and can go look at the code or not handle them so that the generic interrupt handing code can provide a default implementation of that.

Quoted 1 line

At the end of the function we ack the interrupt to the HW.

This is the problem - it means that if they do go off they're silently ignored, and if they start screaming for some reason we don't have anything in place that will handle that.