Skip to contentSkip to conversation

ASoC: Add support for AN7581 PCM driver

PATCH Superseded Christian Marangi 3 participants 5 messages Oct 19 – Nov 9

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover ASoC: Add support for AN7581 PCM driver

The Airoha AN7581 SoC provides a PCM interface for VoIP applications, supporting playback and capture at sample rates of 8 kHz and 16 kHz. The driver configures the PCM bit clock to 2.048 MHz and supports 8-bit and 16-bit samples, with up to 32 channels.

Add YAML and driver. This has been verified with loopback and confirmed by Airoha with ZSI voice chip.

Changes v5:
- Use local reset variable
- Fix validation for channel and bit mask
- Drop use of threaded IRQ
- Return if snd_pcm_hw_constraint_integer errors out

Changes v4:
- Add review by tag for DT patch
- Address shashiko bot comments
- Better protext tx pos
- Set DMA mask for coherent DMA
- Use more managed resource
- Increase users in startup

Changes v3:
- Out of RFC
- Add Documentation patch
- Drop SLIC testing code
- Add MAINTAINERS entry
- Follow normal pattern for commit title
- Improve commit description

Changes v2:
- Rentirely rework IRQ handling
- Fix problem for timeslow configuration
- Better support duplex mode
- Better handle descriptor tracking
- Move to Soc directory
- General code fixup and cleanup

Christian Marangi (2):
ASoC: dt-bindings: airoha: Add AN7581 PCM controller
ASoC: airoha: Add support for AN7581 PCM driver

 .../bindings/sound/airoha,an7581-pcm.yaml     |  52 +
 MAINTAINERS                                   |   7 +
 sound/soc/Kconfig                             |   1 +
 sound/soc/Makefile                            |   1 +
 sound/soc/airoha/Kconfig                      |  10 +
 sound/soc/airoha/Makefile                     |   3 +
 sound/soc/airoha/an7581-pcm.c                 | 906 ++++++++++++++++++
 7 files changed, 980 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/airoha,an7581-pcm.yaml
 create mode 100644 sound/soc/airoha/Kconfig
 create mode 100644 sound/soc/airoha/Makefile
 create mode 100644 sound/soc/airoha/an7581-pcm.c
Signature · 1 line
2.55.0
#1 lore raw

PATCH v5 1/2 ASoC: dt-bindings: airoha: Add AN7581 PCM controller

The Airoha AN7581 SoC provides a PCM interface for VoIP applications, supporting playback and capture at sample rates of 8 kHz and 16 kHz. The driver configures the PCM bit clock to 2.048 MHz and supports 8-bit and 16-bit samples, with up to 32 channels.

Document the related compatible and required interrupt/reset and provide an example for it.

---
 .../bindings/sound/airoha,an7581-pcm.yaml     | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/sound/airoha,an7581-pcm.yaml
Documentation/devicetree/bindings/sound/airoha,an7581-pcm.yaml new file +52 -0
new file mode 100644
index 000000000000..e73236453b88
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/airoha,an7581-pcm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Airoha PCM controller for AN7581 SoC
+
+maintainers:
+  - Christian Marangi <[email protected]>
+
+properties:
+  compatible:
+    const: airoha,an7581-pcm
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+  reset-names:
+    const: pcm1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - resets
+  - reset-names
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/reset/airoha,en7581-reset.h>
+
+    pcm@1fbd0000 {
+        compatible = "airoha,an7581-pcm";
+        reg = <0x1fbd0000 0x10000>;
+
+        interrupts = <GIC_SPI 27 IRQ_TYPE_LEVEL_HIGH>;
+        resets = <&scuclk EN7581_PCM1_RST>;
+        reset-names = "pcm1";
+    };
+
+...
Signature · 1 line
2.55.0
#2 lore raw

PATCH v5 2/2 ASoC: airoha: Add support for AN7581 PCM driver

In reply to Christian Marangi

The Airoha AN7581 SoC provides a PCM interface for VoIP applications, supporting playback and capture at sample rates of 8 kHz and 16 kHz. The driver configures the PCM bit clock to 2.048 MHz and supports 8-bit and 16-bit samples, with up to 32 channels.

Add an ASoC platform driver for the AN7581 PCM controller configured for ZSI attached voice chip.

---
 MAINTAINERS                   |   7 +
 sound/soc/Kconfig             |   1 +
 sound/soc/Makefile            |   1 +
 sound/soc/airoha/Kconfig      |  10 +
 sound/soc/airoha/Makefile     |   3 +
 sound/soc/airoha/an7581-pcm.c | 906 ++++++++++++++++++++++++++++++++++
 6 files changed, 928 insertions(+)
 create mode 100644 sound/soc/airoha/Kconfig
 create mode 100644 sound/soc/airoha/Makefile
 create mode 100644 sound/soc/airoha/an7581-pcm.c
MAINTAINERS +7 -0
index ecd1461eb707..f1dd6fa2aec2 100644
@@ -775,6 +775,13 @@ F:	Documentation/devicetree/bindings/sound/airoha,an7581-afe.yaml
 F:	Documentation/devicetree/bindings/sound/airoha,an7581-wm8960.yaml
 F:	sound/soc/mediatek/an7581/*
 
+AIROHA PCM CONTROLLER DRIVER
+M:	Christian Marangi <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/sound/airoha,an7581-pcm.yaml
+F:	sound/soc/airoha/*
+
 AIROHA SPI SNFI DRIVER
 M:	Lorenzo Bianconi <[email protected]>
 M:	Ray Liu <[email protected]>
sound/soc/Kconfig +1 -0
index edfdcbf734fe..27fc13e11b77 100644
@@ -103,6 +103,7 @@ config SND_SOC_USB
 
 # All the supported SoCs
 source "sound/soc/adi/Kconfig"
+source "sound/soc/airoha/Kconfig"
 source "sound/soc/amd/Kconfig"
 source "sound/soc/apple/Kconfig"
 source "sound/soc/atmel/Kconfig"
sound/soc/Makefile +1 -0
index 21d8406767fc..ea581930bb3d 100644
@@ -46,6 +46,7 @@ obj-$(CONFIG_SND_SOC)	+= codecs/
 obj-$(CONFIG_SND_SOC)	+= generic/
 obj-$(CONFIG_SND_SOC)	+= apple/
 obj-$(CONFIG_SND_SOC)	+= adi/
+obj-$(CONFIG_SND_SOC)	+= airoha/
 obj-$(CONFIG_SND_SOC)	+= amd/
 obj-$(CONFIG_SND_SOC)	+= atmel/
 obj-$(CONFIG_SND_SOC)	+= au1x/
sound/soc/airoha/Kconfig new file +10 -0
new file mode 100644
index 000000000000..20c9211430c9
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config SND_SOC_AN7581_PCM
+	tristate "PCM support for Airoha AN7581 chip"
+	depends on ARCH_AIROHA || COMPILE_TEST
+	help
+	  This adds PCM driver for Airoha AN7581 boards
+	  that can be used with other codecs.
+	  Select Y if you have such device.
+	  If unsure select "N".
sound/soc/airoha/Makefile new file +3 -0
new file mode 100644
index 000000000000..2522fe705d9a
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_SND_SOC_AN7581_PCM) += an7581-pcm.o
sound/soc/airoha/an7581-pcm.c new file +906 -0

906 lines not on this page · raw message

Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH v5 2/2] ASoC: airoha: Add support for AN7581 PCM driver

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] ASoC: airoha: broken full-duplex state tracking in an7581_pcm_fe_hw_params() - [High] ASoC…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] ASoC: airoha: broken full-duplex state tracking in an7581_pcm_fe_hw_params()
- [High] ASoC: airoha: early exit in an7581_pcm_irq_handler() skips DMA poll write
- [High] ASoC: airoha: premature devm_request_irq() in an7581_pcm_probe()
- [High] ASoC: airoha: unsynchronized substream lifetime in an7581_pcm_irq_handler()
- [Medium] ASoC: airoha: tx_pos/rx_pos overflow and wrap jump in an7581_pcm_pointer()
- [Medium] ASoC: airoha: missing check for 0 from of_irq_get() in an7581_pcm_probe()
- [Medium] ASoC: airoha: an7581-pcm: return 0 on success in an7581_pcm_fe_startup()
- [Medium] ASoC: airoha: an7581-pcm: race on DMA descriptors and ring state in IRQ handler
- [Low] ASoC: airoha: missing select REGMAP_MMIO in SND_SOC_AN7581_PCM Kconfig --

commit dbab87e2089692341b1aa76c1a7743b78829fd82
Author: Christian Marangi <[email protected]>

ASoC: airoha: Add support for AN7581 PCM driver

This commit adds an ASoC platform driver for the Airoha AN7581 PCM controller supporting playback and capture at 8 kHz and 16 kHz with up to 32 channels.

sound/soc/airoha/Kconfig line 5

Quoted 11 lines
sound/soc/airoha/Kconfig new file +5 -0
new file mode 100644
index 0000000000000..20c9211430c95
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config SND_SOC_AN7581_PCM
+	tristate "PCM support for Airoha AN7581 chip"
+	depends on ARCH_AIROHA || COMPILE_TEST

[Severity: Low]
Since an7581_pcm_probe() calls devm_regmap_init_mmio(), should SND_SOC_AN7581_PCM select REGMAP_MMIO here to avoid link errors when CONFIG_REGMAP_MMIO is not enabled?

Quoted 5 lines
sound/soc/airoha/Kconfig new file +5 -0
+	help
+	  This adds PCM driver for Airoha AN7581 boards
+	  that can be used with other codecs.
+	  Select Y if you have such device.
+	  If unsure select "N".

[ ... ]

Quoted 5 lines
sound/soc/airoha/an7581-pcm.c new file +0 -0
new file mode 100644
index 0000000000000..803b1dda26a0e

[ ... ]

Quoted 47 lines
sound/soc/airoha/an7581-pcm.c new file +47 -0
+static int an7581_pcm_fe_startup(struct snd_pcm_substream *substream,
+				 struct snd_soc_dai *dai)
+{
+	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+	struct an7581_pcm_priv *priv = snd_soc_card_get_drvdata(rtd->card);
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	int ret;
+
+	snd_soc_set_runtime_hwparams(substream, &an7581_pcm_hardware);
+
+	ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+					   AN7581_PCM_FRAMES_PER_DESC,
+					   AN7581_PCM_FRAMES_PER_DESC);
+	if (ret < 0) {
+		dev_err(rtd->card->dev, "failed to constrain period to 80 frames\n");
+		return ret;
+	}
+
+	ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
+	if (ret < 0) {
+		dev_err(rtd->card->dev, "snd_pcm_hw_constraint_integer failed\n");
+		return ret;
+	}
+
+	/* Cache stream substream for IRQ handling */
+	priv->stream[substream->stream] = substream;
+
+	/* On open mask interrupt and disable DMA */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		regmap_clear_bits(priv->map, AN7581_PCM_IMR,
+				  AN7581_PCM_IMR_TDESC_UPDATE_INT);
+		regmap_clear_bits(priv->map, AN7581_PCM_TRDCR,
+				  AN7581_PCM_TXDMA_ENABLE);
+	} else {
+		regmap_clear_bits(priv->map, AN7581_PCM_IMR,
+				  AN7581_PCM_IMR_RDESC_UPDATE_INT);
+		regmap_clear_bits(priv->map, AN7581_PCM_TRDCR,
+				  AN7581_PCM_RXDMA_ENABLE);
+	}
+
+	usleep_range(10000, 20000);
+
+	mutex_lock(&priv->user_lock);
+
+	if (!priv->users) {
+		regmap_clear_bits(priv->map, AN7581_PCM_PICR,
+				  AN7581_PCM_CFG_VALID);

sound/soc/airoha/an7581-pcm.c line 321

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+
+		usleep_range(5000, 10000);
+	}
+
+	priv->users++;
+
+	mutex_unlock(&priv->user_lock);
+
+	return ret;
+}

[Severity: Medium]
In an7581_pcm_fe_startup(), snd_pcm_hw_constraint_integer() returns 1 when the constraint is applied, which is then returned at the end of the function.

In sound/soc/soc-dai.c:snd_soc_dai_startup():

	/* mark substream if succeeded */
	if (ret == 0)
		soc_dai_mark_push(dai, substream, startup);

Because ret is 1 rather than 0, snd_soc_dai_startup() skips soc_dai_mark_push(). If a later step in __soc_pcm_open() fails and triggers rollback via soc_pcm_clean() -> snd_soc_dai_shutdown(), soc_dai_mark_match() will not match and an7581_pcm_fe_shutdown() will be skipped, leaking priv->users and leaving priv->stream[substream->stream] set to the closed substream.

Should an7581_pcm_fe_startup() return 0 on success?

Quoted 11 lines
sound/soc/airoha/an7581-pcm.c new file +11 -0
+
+static void an7581_pcm_fe_shutdown(struct snd_pcm_substream *substream,
+				   struct snd_soc_dai *dai)
+{
+	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+	struct an7581_pcm_priv *priv = snd_soc_card_get_drvdata(rtd->card);
+
+	/* On close mask interrupt and disable DMA */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		regmap_clear_bits(priv->map, AN7581_PCM_IMR,
+				  AN7581_PCM_IMR_TDESC_UPDATE_INT);

sound/soc/airoha/an7581-pcm.c line 342

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+		regmap_clear_bits(priv->map, AN7581_PCM_TRDCR,
+				  AN7581_PCM_TXDMA_ENABLE);
+	} else {
+		regmap_clear_bits(priv->map, AN7581_PCM_IMR,
+				  AN7581_PCM_IMR_RDESC_UPDATE_INT);
+		regmap_clear_bits(priv->map, AN7581_PCM_TRDCR,
+				  AN7581_PCM_RXDMA_ENABLE);
+	}
+
+	priv->stream[substream->stream] = NULL;

[Severity: High]
Can an7581_pcm_irq_handler() race with stream startup, stop, and shutdown when accessing priv->stream[] and substream->runtime?

1. Neither card->snd_card->sync_irq nor a component .sync_stop callback is configured, and an7581_pcm_fe_shutdown() clears priv->stream[substream->stream] without lock serialization or synchronize_irq(). If an7581_pcm_irq_handler() is already running on another CPU (or preempted as a threaded handler under IRQF_COND_ONESHOT) and has loaded substream = priv->stream[...], snd_pcm_release_substream() can proceed into snd_pcm_detach_substream() and free substream->runtime while the IRQ handler is still dereferencing runtime in frames_to_bytes(), snd_pcm_period_elapsed(), and snd_pcm_*_hw_avail().

2. When snd_pcm_drop() holds pcm_stream_lock_irq(substream) and invokes an7581_pcm_fe_trigger(SNDRV_PCM_TRIGGER_STOP), a concurrent an7581_pcm_irq_handler() waiting on pcm_stream_lock_irqsave(substream) inside snd_pcm_period_elapsed() will resume once snd_pcm_drop() drops the lock, set desc->status |= AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA, and write AN7581_PCM_TPDR / AN7581_PCM_RPDR after the stream was stopped.

3. In an7581_pcm_fe_startup(), priv->stream[substream->stream] = substream is published at open time before hw_params() or prepare() has initialized runtime->frame_bits, runtime->period_size, or descriptor ownership (which starts zero-initialized as AN7581_PCM_DESC_STATUS_OWNERSHIP_CPU). Since an7581_pcm_irq_handler() checks AN7581_PCM_ISR without masking by AN7581_PCM_IMR, an interrupt from the other active direction when a stale ISR bit is set can walk the newly opened stream's CPU-owned descriptors before prepare() has run.

Quoted 36 lines
sound/soc/airoha/an7581-pcm.c new file +36 -0
+
+	usleep_range(10000, 20000);
+
+	mutex_lock(&priv->user_lock);
+
+	if (priv->users)
+		priv->users--;
+
+	if (!priv->users)
+		regmap_clear_bits(priv->map, AN7581_PCM_PICR,
+				  AN7581_PCM_CFG_VALID);
+
+	mutex_unlock(&priv->user_lock);
+}
+
+static int an7581_pcm_fe_prepare(struct snd_pcm_substream *substream,
+				 struct snd_soc_dai *dai)
+{
+	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+	struct an7581_pcm_priv *priv = snd_soc_card_get_drvdata(rtd->card);
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct an7581_pcm_desc *desc;
+	unsigned int period_bytes;
+	unsigned long flags;
+	int num_desc, i;
+
+	spin_lock_irqsave(&priv->desc_lock, flags);
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		priv->tx_pos = 0;
+		priv->tx_desc_idx = 0;
+		desc = priv->tx_descs;
+		num_desc = AN7581_PCM_TX_DESCRIPTORS;
+	} else {
+		priv->rx_pos = 0;
+		priv->rx_desc_idx = 0;
+		desc = priv->rx_descs;

sound/soc/airoha/an7581-pcm.c line 388

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+		num_desc = AN7581_PCM_RX_DESCRIPTORS;
+	}
+	spin_unlock_irqrestore(&priv->desc_lock, flags);
+
+	period_bytes = frames_to_bytes(runtime, AN7581_PCM_FRAMES_PER_DESC);
+	for (i = 0; i < num_desc; i++) {
+		desc->status |= AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA;
+		desc->dma_addr = runtime->dma_addr + i * period_bytes;
+		desc++;
+	}

[Severity: Medium]
In an7581_pcm_fe_prepare() and an7581_pcm_fe_hw_params(), the descriptor rings are updated outside priv->desc_lock, while in an7581_pcm_irq_handler(), priv->desc_lock is dropped before calling snd_pcm_period_elapsed() and setting
desc->status |= AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA.

If snd_pcm_period_elapsed() triggers an XRUN stop (or userspace calls SNDRV_PCM_IOCTL_DROP) and userspace immediately calls prepare() or hw_params() on another CPU while an7581_pcm_irq_handler() is still in its descriptor loop, can the concurrent descriptor and index updates corrupt desc->status, tx_desc_idx / rx_desc_idx, and tx_pos / rx_pos?

Also, in an7581_pcm_irq_handler(), snd_pcm_capture_hw_avail(runtime) and snd_pcm_playback_hw_avail(runtime) read runtime->status->hw_ptr and runtime->control->appl_ptr without holding the PCM substream lock.

Quoted 97 lines
sound/soc/airoha/an7581-pcm.c new file +97 -0
+
+	return 0;
+}
+
+static int an7581_pcm_fe_hw_params(struct snd_pcm_substream *substream,
+				   struct snd_pcm_hw_params *params,
+				   struct snd_soc_dai *dai)
+{
+	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+	struct an7581_pcm_priv *priv = snd_soc_card_get_drvdata(rtd->card);
+	unsigned int channels = params_channels(params);
+	unsigned int rate = params_rate(params);
+	int bit_width = params_width(params);
+	struct an7581_pcm_desc *desc;
+	int num_desc, i;
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		desc = priv->tx_descs;
+		num_desc = AN7581_PCM_TX_DESCRIPTORS;
+	} else {
+		desc = priv->rx_descs;
+		num_desc = AN7581_PCM_RX_DESCRIPTORS;
+	}
+
+	mutex_lock(&priv->user_lock);
+	if (priv->users > 1 &&
+	    (priv->current_rate != rate ||
+	     priv->current_channels != channels)) {
+		dev_err(rtd->card->dev, "playback/capture rates/channels must match in full-duplex mode\n");
+		mutex_unlock(&priv->user_lock);
+		return -EBUSY;
+	}
+
+	if (priv->users <= 1) {
+		/* one bit delay for ZSI */
+		regmap_set_bits(priv->map, AN7581_PCM_PICR, AN7581_PCM_ONE_BIT_DELAY);
+
+		regmap_update_bits(priv->map, AN7581_PCM_PICR,
+				   AN7581_PCM_BIT_MSTSLV_MODE,
+				   AN7581_PCM_BIT_MSTSLV_MODE_MASTER);
+
+		regmap_update_bits(priv->map, AN7581_PCM_PICR,
+				   AN7581_PCM_BIT_CLK,
+				   AN7581_PCM_BIT_CLK_2048HZ);
+
+		regmap_update_bits(priv->map, AN7581_PCM_PICR,
+				   AN7581_PCM_SAMPLE_CLK,
+				   (rate == 16000 ? AN7581_PCM_SAMPLE_CLK_16KHZ :
+				   AN7581_PCM_SAMPLE_CLK_8KHZ));
+
+		regmap_update_bits(priv->map, AN7581_PCM_PICR,
+				   AN7581_PCM_FS_LEN,
+				   AN7581_PCM_FS_LEN_16);
+
+		regmap_set_bits(priv->map, AN7581_PCM_PICR, AN7581_PCM_BIT_ORDER);
+
+		regmap_clear_bits(priv->map, AN7581_PCM_PICR, AN7581_PCM_FS_EDGE);
+	}
+
+	/* Setup timeslot */
+	an7581_pcm_setup_time_slot(priv, substream->stream, bit_width);
+
+	if (priv->users <= 1) {
+		/* Setup Descriptor size and number */
+		regmap_update_bits(priv->map, AN7581_PCM_TRDRSR,
+				   AN7581_DESC_OFFSET | AN7581_DESC_SIZE,
+				   FIELD_PREP(AN7581_DESC_OFFSET, sizeof(*desc) / sizeof(u32)) |
+				   FIELD_PREP(AN7581_DESC_SIZE, num_desc));
+
+		regmap_update_bits(priv->map, AN7581_PCM_CHBFOSR,
+				   AN7581_PCM_DESC_CHBF_OFFSET,
+				   FIELD_PREP(AN7581_PCM_DESC_CHBF_OFFSET,
+					      AN7581_PCM_FRAMES_PER_DESC * bit_width / 8));
+	}
+
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		regmap_write(priv->map, AN7581_PCM_TDRBAR, priv->tx_dma);
+	else
+		regmap_write(priv->map, AN7581_PCM_RDRBAR, priv->rx_dma);
+
+	/* Configure descriptors for DMA ownership and sample size */
+	for (i = 0; i < num_desc; i++) {
+		/* Assign to each descriptor the dma_addr + offset of period bytes */
+		desc[i].status = FIELD_PREP(AN7581_PCM_DESC_STATUS_SAMPLE_SIZE,
+					    AN7581_PCM_FRAMES_PER_DESC);
+
+		desc[i].channel_mask = GENMASK(channels - 1, 0);
+	}
+
+	if (priv->users <= 1) {
+		/* Enable channels */
+		regmap_update_bits(priv->map, AN7581_PCM_DCHENR,
+				   AN7581_PCM_CHAN_EN,
+				   FIELD_PREP(AN7581_PCM_CHAN_EN, GENMASK(channels - 1, 0)));
+
+		/* Signal PCM configuration is now valid */
+		regmap_set_bits(priv->map, AN7581_PCM_PICR,

sound/soc/airoha/an7581-pcm.c line 495

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+				AN7581_PCM_CFG_VALID);
+	}
+
+	priv->current_rate = rate;
+	priv->current_channels = channels;
+
+	mutex_unlock(&priv->user_lock);
+
+	return 0;
+}

[Severity: High]
What happens in an7581_pcm_fe_hw_params() when a full-duplex application opens both playback and capture substreams before calling hw_params() on either of them?

Because priv->users is incremented in an7581_pcm_fe_startup(), priv->users is already 2 when the first an7581_pcm_fe_hw_params() call runs:

1. On the first session after probe, priv->current_rate and priv->current_channels are 0, so the priv->users > 1 check fails with -EBUSY on the very first hw_params() call.

2. On subsequent sessions after both streams were closed, priv->current_rate and priv->current_channels retain stale values from the previous session. If the new session uses a different rate or channel count, hw_params() fails with -EBUSY; if they happen to match the stale values, all three if (priv->users <= 1) blocks are skipped for both streams, leaving AN7581_PCM_PICR (including AN7581_PCM_CFG_VALID, which was cleared on startup/shutdown), AN7581_PCM_TRDRSR, AN7581_PCM_CHBFOSR, and AN7581_PCM_DCHENR unconfigured.

Additionally, AN7581_PCM_CHBFOSR is a single shared register programmed to AN7581_PCM_FRAMES_PER_DESC * bit_width / 8 when priv->users <= 1, but an7581_pcm_fe_hw_params() does not check that bit_width matches between playback and capture.

[ ... ]

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+static snd_pcm_uframes_t an7581_pcm_pointer(struct snd_soc_component *component,
+					    struct snd_pcm_substream *substream)
+{
+	struct snd_soc_card *card = component->card;
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct an7581_pcm_priv *priv;
+	unsigned long flags;
+	unsigned int pos;
+
+	priv = snd_soc_card_get_drvdata(card);

sound/soc/airoha/an7581-pcm.c line 608

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+
+	spin_lock_irqsave(&priv->desc_lock, flags);
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		pos = bytes_to_frames(runtime, priv->tx_pos);
+	else
+		pos = bytes_to_frames(runtime, priv->rx_pos);
+	spin_unlock_irqrestore(&priv->desc_lock, flags);
+
+	return pos % runtime->buffer_size;
+}

[Severity: Medium]
In an7581_pcm_irq_handler(), priv->tx_pos and priv->rx_pos are incremented on every descriptor without wrapping modulo the buffer byte size, and an7581_pcm_pointer() passes them to bytes_to_frames() before taking pos % runtime->buffer_size.

In include/sound/pcm.h:bytes_to_frames():

static inline snd_pcm_sframes_t bytes_to_frames(struct snd_pcm_runtime *runtime, ssize_t size)

{
	return size * 8 / runtime->frame_bits;
}

On 32-bit architectures, ssize_t is 32-bit signed, so size * 8 overflows once tx_pos or rx_pos reaches 256 MiB (around 4.3 minutes at 16 kHz, 32 channels, 16-bit). Even on 64-bit architectures, when the 32-bit unsigned int tx_pos or rx_pos wraps around 2^32, because runtime->buffer_size is 1200 frames (15 * 80) and 2^32 is not a multiple of 1200, pos % runtime->buffer_size will jump discontinuously across the wrap boundary.

Should tx_pos and rx_pos be wrapped modulo snd_pcm_lib_buffer_bytes(substream) when updated (or tracked in frames modulo runtime->buffer_size)?

[ ... ]

Quoted 100 lines
sound/soc/airoha/an7581-pcm.c new file +100 -0
+static irqreturn_t an7581_pcm_irq_handler(int irq, void *data)
+{
+	struct snd_pcm_substream *substream;
+	struct an7581_pcm_priv *priv = data;
+	unsigned long flags;
+	u32 status, isr = 0;
+
+	regmap_read(priv->map, AN7581_PCM_ISR, &isr);
+
+	/* Scan descriptor on receiving RX desc update interrupt */
+	if (isr & AN7581_PCM_IMR_RDESC_UPDATE_INT) {
+		struct snd_pcm_runtime *runtime;
+		struct an7581_pcm_desc *desc;
+		unsigned int sample_size;
+		bool rx_poll = false;
+		int i;
+
+		substream = priv->stream[SNDRV_PCM_STREAM_CAPTURE];
+		if (!substream)
+			goto handle_tx;
+
+		runtime = substream->runtime;
+
+		for (i = 0; i < AN7581_PCM_RX_DESCRIPTORS; i++) {
+			spin_lock_irqsave(&priv->desc_lock, flags);
+
+			desc = &priv->rx_descs[priv->rx_desc_idx];
+			status = desc->status;
+			if ((status & AN7581_PCM_DESC_STATUS_OWNERSHIP) ==
+			    AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA) {
+				spin_unlock_irqrestore(&priv->desc_lock,
+						       flags);
+				break;
+			}
+
+			sample_size = FIELD_GET(AN7581_PCM_DESC_STATUS_SAMPLE_SIZE,
+						status);
+
+			priv->rx_pos += frames_to_bytes(runtime, sample_size);
+			priv->rx_desc_idx++;
+			if (priv->rx_desc_idx == AN7581_PCM_RX_DESCRIPTORS)
+				priv->rx_desc_idx = 0;
+
+			spin_unlock_irqrestore(&priv->desc_lock, flags);
+
+			snd_pcm_period_elapsed(substream);
+
+			desc->status |= AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA;
+
+			if (snd_pcm_capture_hw_avail(runtime) < runtime->period_size)
+				goto handle_tx;
+
+			rx_poll = true;
+		}
+
+		if (rx_poll)
+			regmap_write(priv->map, AN7581_PCM_RPDR, AN7581_PCM_RX_POLLING);
+	}
+
+handle_tx:
+	/* Scan descriptor on receiving TX desc update interrupt */
+	if (isr & AN7581_PCM_IMR_TDESC_UPDATE_INT) {
+		struct snd_pcm_runtime *runtime;
+		struct an7581_pcm_desc *desc;
+		unsigned int sample_size;
+		bool tx_poll = false;
+		int i;
+
+		substream = priv->stream[SNDRV_PCM_STREAM_PLAYBACK];
+		if (!substream)
+			return IRQ_HANDLED;
+
+		runtime = substream->runtime;
+
+		for (i = 0; i < AN7581_PCM_TX_DESCRIPTORS; i++) {
+			spin_lock_irqsave(&priv->desc_lock, flags);
+
+			desc = &priv->tx_descs[priv->tx_desc_idx];
+			status = desc->status;
+			if ((status & AN7581_PCM_DESC_STATUS_OWNERSHIP) ==
+			    AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA) {
+				spin_unlock_irqrestore(&priv->desc_lock,
+						       flags);
+				break;
+			}
+
+			sample_size = FIELD_GET(AN7581_PCM_DESC_STATUS_SAMPLE_SIZE,
+						status);
+
+			priv->tx_pos += frames_to_bytes(runtime, sample_size);
+			priv->tx_desc_idx++;
+			if (priv->tx_desc_idx == AN7581_PCM_TX_DESCRIPTORS)
+				priv->tx_desc_idx = 0;
+
+			spin_unlock_irqrestore(&priv->desc_lock, flags);
+
+			snd_pcm_period_elapsed(substream);
+
+			desc->status |= AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA;
+

sound/soc/airoha/an7581-pcm.c line 774

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+			if (snd_pcm_playback_hw_avail(runtime) < runtime->period_size)
+				return IRQ_HANDLED;
+
+			tx_poll = true;
+		}
+
+		if (tx_poll)
+			regmap_write(priv->map, AN7581_PCM_TPDR,
+				     AN7581_PCM_TX_POLLING);
+	}

[Severity: High]
In an7581_pcm_irq_handler(), when snd_pcm_capture_hw_avail(runtime) < runtime->period_size or snd_pcm_playback_hw_avail(runtime) < runtime->period_size, the code jumps out via goto handle_tx or return IRQ_HANDLED after already setting
desc->status |= AN7581_PCM_DESC_STATUS_OWNERSHIP_DMA:

1. If an earlier iteration in the same loop already processed a descriptor and set rx_poll = true or tx_poll = true, jumping past the loop skips the regmap_write() to AN7581_PCM_RPDR or AN7581_PCM_TPDR for the earlier descriptor(s).

2. Even on the first descriptor in the loop, if hw_avail drops below one period while the stream remains in SNDRV_PCM_STATE_RUNNING (for example, before userspace writes the next period or when stop_threshold > buffer_size), skipping the AN7581_PCM_TPDR / AN7581_PCM_RPDR write leaves DMA polling halted with no .ack callback to restart polling once userspace supplies or consumes more frames.

Could this stall playback or capture?

Quoted 3 lines
sound/soc/airoha/an7581-pcm.c new file +3 -0
+
+	return IRQ_HANDLED;
+}

[ ... ]

Quoted 25 lines
sound/soc/airoha/an7581-pcm.c new file +25 -0
+static int an7581_pcm_probe(struct platform_device *pdev)
+{
+	struct snd_soc_card *card = &an7581_pcm_card;
+	struct snd_soc_dai_link *dai_link;
+	struct device *dev = &pdev->dev;
+	struct an7581_pcm_priv *priv;
+	struct reset_control *reset;
+	void __iomem *base;
+	int irq;
+	int ret;
+	int i;
+
+	card->dev = dev;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+	snd_soc_card_set_drvdata(card, priv);
+
+	base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	priv->map = devm_regmap_init_mmio(dev, base,
+					  &an7581_pcm_regmap_config);

sound/soc/airoha/an7581-pcm.c line 819

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+	if (IS_ERR(priv->map))
+		return PTR_ERR(priv->map);
+
+	reset = devm_reset_control_get_exclusive(dev, "pcm1");
+	if (IS_ERR(reset))
+		return PTR_ERR(reset);
+
+	irq = of_irq_get(dev_of_node(dev), 0);
+	if (irq < 0)
+		return irq;

[Severity: Medium]
of_irq_get() returns 0 on IRQ mapping failure (and when CONFIG_OF_IRQ is disabled). Should this check if (irq <= 0) or use platform_get_irq(pdev, 0) instead so irq == 0 is not passed to devm_request_irq()?

Quoted 20 lines
sound/soc/airoha/an7581-pcm.c new file +20 -0
+
+	ret = devm_request_irq(dev, irq, an7581_pcm_irq_handler,
+			       0, dev_name(dev), priv);
+	if (ret)
+		return ret;
+
+	spin_lock_init(&priv->desc_lock);
+	mutex_init(&priv->user_lock);
+
+	/* Reset the PCM interface */
+	reset_control_assert(reset);
+	usleep_range(5000, 10000);
+	reset_control_deassert(reset);
+	usleep_range(5000, 10000);
+
+	ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
+	if (ret)
+		return ret;
+
+	priv->tx_descs = dmam_alloc_coherent(dev, sizeof(struct an7581_pcm_desc) *

sound/soc/airoha/an7581-pcm.c line 849

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+					     AN7581_PCM_TX_DESCRIPTORS, &priv->tx_dma,
+					     GFP_KERNEL);
+	if (!priv->tx_descs)
+		return -ENOMEM;
+
+	priv->rx_descs = dmam_alloc_coherent(dev, sizeof(struct an7581_pcm_desc) *
+					     AN7581_PCM_RX_DESCRIPTORS, &priv->rx_dma,
+					     GFP_KERNEL);
+	if (!priv->rx_descs)
+		return -ENOMEM;

[Severity: High]
Is devm_request_irq() called too early in an7581_pcm_probe()?

Requesting the IRQ before spin_lock_init(&priv->desc_lock), hardware reset, and dmam_alloc_coherent() means a pending interrupt can fire while the controller is held in reset (reading AN7581_PCM_ISR across reset_control_assert() / usleep_range()) or before priv->desc_lock and the descriptor rings are initialized.

It also reverses the devres teardown order on driver unbind:
an7581_pcm_remove() runs before devm_snd_soc_register_card() unregisters the card, and devres will free priv->rx_descs and priv->tx_descs before devm_irq_release() frees and synchronizes the IRQ.

Should devm_request_irq() be moved after the hardware reset and DMA descriptor allocations?

[ ... ]

Quoted 1 line

+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#4 lore raw

Re: [PATCH v5 2/2] ASoC: airoha: Add support for AN7581 PCM driver

In reply to Christian Marangi

sound/soc/airoha/Kconfig line 5

Quoted 8 lines

The Airoha AN7581 SoC provides a PCM interface for VoIP applications, supporting playback and capture at sample rates of 8 kHz and 16 kHz. The driver configures the PCM bit clock to 2.048 MHz and supports 8-bit and 16-bit samples, with up to 32 channels.

sound/soc/airoha/Kconfig new file +3 -0
+config SND_SOC_AN7581_PCM
+	tristate "PCM support for Airoha AN7581 chip"
+	depends on ARCH_AIROHA || COMPILE_TEST

This needs to select CONFIG_REGMAP_MMIO.

sound/soc/airoha/an7581-pcm.c line 18

Quoted 2 lines
sound/soc/airoha/an7581-pcm.c new file +2 -0
+#include <linux/mfd/syscon.h>
+#include <linux/pinctrl/consumer.h>

These aren't used.

sound/soc/airoha/an7581-pcm.c line 39

Quoted 2 lines
sound/soc/airoha/an7581-pcm.c new file +2 -0
+#define     AN7581_PCM_BIT_CLK_1024KHZ		FIELD_PREP_CONST(AN7581_PCM_BIT_CLK, 0x2)
+#define     AN7581_PCM_BIT_CLK_2048HZ		FIELD_PREP_CONST(AN7581_PCM_BIT_CLK, 0x3)

Is this really 2048Hz, not kHz?

sound/soc/airoha/an7581-pcm.c line 213

Quoted 4 lines
sound/soc/airoha/an7581-pcm.c new file +4 -0
+static const struct snd_pcm_hardware an7581_pcm_hardware = {
+	.info			= SNDRV_PCM_INFO_MMAP |
+				  SNDRV_PCM_INFO_MMAP_VALID |
+				  SNDRV_PCM_INFO_INTERLEAVED,

The pointer callback works in chunks of descriptors so you want SNDRV_PCM_INFO_BATCH as well.

sound/soc/airoha/an7581-pcm.c line 216

Quoted 1 line
sound/soc/airoha/an7581-pcm.c new file +1 -0
+	.period_bytes_min = AN7581_PCM_FRAMES_PER_DESC,

This is using a frame count to specify bytes.

sound/soc/airoha/an7581-pcm.c line 226

Quoted 2 lines
sound/soc/airoha/an7581-pcm.c new file +2 -0
+static int an7581_pcm_setup_time_slot(struct an7581_pcm_priv *priv,
+				      int direction, int bit_width)

We don't have anything constraining things so that the slots fit in the BCLK.

Quoted 8 lines
sound/soc/airoha/an7581-pcm.c new file +7 -0
+static int an7581_pcm_fe_startup(struct snd_pcm_substream *substream,
+				 struct snd_soc_dai *dai)
+{
 
+	/* On open mask interrupt and disable DMA */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		regmap_clear_bits(priv->map, AN7581_PCM_IMR,
+				  AN7581_PCM_IMR_TDESC_UPDATE_INT);

sound/soc/airoha/an7581-pcm.c line 305

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+		regmap_clear_bits(priv->map, AN7581_PCM_TRDCR,
+				  AN7581_PCM_TXDMA_ENABLE);
+	} else {
+		regmap_clear_bits(priv->map, AN7581_PCM_IMR,
+				  AN7581_PCM_IMR_RDESC_UPDATE_INT);
+		regmap_clear_bits(priv->map, AN7581_PCM_TRDCR,
+				  AN7581_PCM_RXDMA_ENABLE);
+	}
+
+	usleep_range(10000, 20000);

That's a fairly long delay for a digital part?

sound/soc/airoha/an7581-pcm.c line 360

Quoted 3 lines
sound/soc/airoha/an7581-pcm.c new file +3 -0
+static int an7581_pcm_fe_prepare(struct snd_pcm_substream *substream,
+				 struct snd_soc_dai *dai)
+{

Note that we redo prepare as part of over/underrun recovery - we should reset the hardware position as well as the driver one.

Quoted 7 lines
sound/soc/airoha/an7581-pcm.c new file +6 -0
+static int an7581_pcm_fe_hw_params(struct snd_pcm_substream *substream,
+				   struct snd_pcm_hw_params *params,
+				   struct snd_soc_dai *dai)
+{
 
+
+	if (priv->users <= 1) {

sound/soc/airoha/an7581-pcm.c line 461

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+		/* Setup Descriptor size and number */
+		regmap_update_bits(priv->map, AN7581_PCM_TRDRSR,
+				   AN7581_DESC_OFFSET | AN7581_DESC_SIZE,
+				   FIELD_PREP(AN7581_DESC_OFFSET, sizeof(*desc) / sizeof(u32)) |
+				   FIELD_PREP(AN7581_DESC_SIZE, num_desc));
+
+		regmap_update_bits(priv->map, AN7581_PCM_CHBFOSR,
+				   AN7581_PCM_DESC_CHBF_OFFSET,
+				   FIELD_PREP(AN7581_PCM_DESC_CHBF_OFFSET,
+					      AN7581_PCM_FRAMES_PER_DESC * bit_width / 8));

This is a little confusing, the driver says it does interleaved format but the offset programming looks odd for that?

Quoted 11 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+
+static struct snd_soc_dai_link an7581_pcm_dai_links[] = {
+	/* FE */
+	{
+		.name = "an7581-playback",
+		.stream_name = "an7581-playback",
+		.trigger = {SND_SOC_DPCM_TRIGGER_POST,
 
+			    SND_SOC_DPCM_TRIGGER_POST},
+		.playback_only = 1,
+		SND_SOC_DAILINK_REG(playback),

sound/soc/airoha/an7581-pcm.c line 656

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +10 -0
+	},
+	{
+		.name = "an7581-capture",
+		.stream_name = "an7581-capture",
+		.trigger = {SND_SOC_DPCM_TRIGGER_POST,
+			    SND_SOC_DPCM_TRIGGER_POST},
+		.capture_only = 1,
+		SND_SOC_DAILINK_REG(capture),
+	},
+};

This isn't actually a DPCM driver?

sound/soc/airoha/an7581-pcm.c line 663

Quoted 6 lines
sound/soc/airoha/an7581-pcm.c new file +6 -0
+static struct snd_soc_card an7581_pcm_card = {
+	.name = "an7581-pcm",
+	.owner = THIS_MODULE,
+	.dai_link = an7581_pcm_dai_links,
+	.num_links = ARRAY_SIZE(an7581_pcm_dai_links),
+};

This should be const.

Quoted 5 lines
sound/soc/airoha/an7581-pcm.c new file +5 -0
+static irqreturn_t an7581_pcm_irq_handler(int irq, void *data)
+{
+	struct snd_pcm_substream *substream;
+	struct an7581_pcm_priv *priv = data;
+	unsigned long flags;

sound/soc/airoha/an7581-pcm.c line 749

Quoted 10 lines
sound/soc/airoha/an7581-pcm.c new file +7 -0
+	u32 status, isr = 0;
+
+	regmap_read(priv->map, AN7581_PCM_ISR, &isr);
 
+	if (isr & AN7581_PCM_IMR_RDESC_UPDATE_INT) {
 
+	if (isr & AN7581_PCM_IMR_TDESC_UPDATE_INT) {
 
+	return IRQ_HANDLED;
+}

This unconditionally returns IRQ_HANDLED regardless of if we actually found anything, this breaks any potential sharing as well as the debug and robustness features of genirq.

Signature · 1 line
Mark Brown <[email protected]>