Skip to contentSkip to conversation

ASoC: Add Loongson-2K0300 I2S controller and sound card support

PATCH In review Binbin Zhou 5 participants 24 messages Jun 26 – Jul 3

Subsystems (6)
lore
Share & tools
lore

Binbin Zhou

author maintainer
#0 lore raw

v3 cover ASoC: Add Loongson-2K0300 I2S controller and sound card support

Hi all:

This series adds ASoC support for the Loongson-2K0300 SoC and its associated development boards, including the CTCISZ Forever Pi and the ATK-DL2K0300B.

Key changes:
- Extend DT bindings to support ls2k0300-i2s and new audio card compatibles.
- Refactor the platform I2S driver to handle SoC differences via per-device configuration (rev_id, optional APB DMA config, reset sequence).
- Refactor the audio machine driver to support board-specific DAI formats, GPIO-based headphone detection/control, speaker enable, and DAPM routing.
- Add jack detection and automatic switching between headphones and speakers for the DL2K0300B board.

The patchset also cleans up the existing audio card binding by referencing the common sound-card properties, and adds new compatibles for the Loongson-2K0300 variants with proper DAI format handling.

All changes have been tested on Loongson-2K2000 (PCI), Loongson-2K0300 Forever Pi and Loongson-2K0300 DL2K0300B boards.

Thanks.
Binbin

========
V3:
Patch (1/9):
- New patch;
- Error handling reported by AI Sashiko;
Patch (3/9):
- Mark platform configuration structures as const;
Patch (4/9):
- Add Acked-by tag from Rob, thanks;
Patch (5/9):
- Correct commit message;
Patch (7/9):
- Drop loongson prefix;
- Change gpiod_hp_mute to gpiod_hp_ctl;
Patch (8/9):
- Add add_dapm_routes to loongson_card_config;
- Move gpiod* register ops into loongson_card_parse_of();
- Change gpiod_hp_mute to gpiod_hp_ctl;
- Add ls_priv->gpiod_hp_det judgment in loongson_asoc_machine_init()

    to avoid double-free;

Patch (9/9):
- New patch;
- Add DAPM routes from MIC inputs to Mic Bias.

Link to V2:
https://lore.kernel.org/all/[email protected]/

v2:
- The first four patches for V1 (related to code cleanup) have been accepted as a separate series. The link is as follows:
https://lore.kernel.org/all/178041371415.93058.4794135670349989571.b4-ty@b4/

Patch (1/7):
- Add Reviewed-by tag from Krzysztof Kozlowski;
Patch (3/7):
- New patch;
- Reference sound-card-common.yaml, drop custom model property;
Patch (4/7)(5/7):
- New patches;
- Support Forever Pi board with different DAI format;
Patch (6/7)(7/7):
- New patches;
- Implement headphone jack detection and DAPM routing for ATK-DL2K0300B board.

Link to V1:
https://lore.kernel.org/all/[email protected]/

Binbin Zhou (9):
ASoC: loongson: Fix error handling in ACPI property parsing
ASoC: dt-bindings: loongson,ls2k1000-i2s: Document Loongson-2K0300
compatible
ASoC: loongson: Add Loongson-2K0300 I2S controller support
ASoC: dt-bindings: loongson,ls-audio-card: Use common sound card
ASoC: dt-bindings: loongson,ls-audio-card: Add ctcisz forever pi
compatible
ASoC: loongson: Add Loongson-2K0300 CTCISZ Forever Pi sound card
support
ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B
compatible
ASoC: loongson: Add headphone jack detection and DAPM routing
ASoC: es8328: Add DAPM routes from MIC inputs to Mic Bias

 .../sound/loongson,ls-audio-card.yaml         |  53 +++++-
 .../bindings/sound/loongson,ls2k1000-i2s.yaml |  22 ++-
 sound/soc/codecs/es8328.c                     |   5 +
 sound/soc/loongson/loongson_card.c            | 171 ++++++++++++++++--
 sound/soc/loongson/loongson_i2s_plat.c        |  42 ++++-
 5 files changed, 267 insertions(+), 26 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH v3 1/9 ASoC: loongson: Fix error handling in ACPI property parsing

In loongson_card_parse_acpi(), the return value of device_property_read_string() for the codec-dai-name property was ignored. If the property is missing or invalid, an uninitialized pointer would be used later, potentially leading to undefined behavior.

Fix this by checking the return value and propagating the error appropriately.

Fixes ddb538a3004b ("ASoC: loongson: Factor out loongson_card_acpi_find_device() function")
---
 sound/soc/loongson/loongson_card.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
sound/soc/loongson/loongson_card.c +4 -2
index 7910d5d9ac4f..ea895fe6b5e9 100644
@@ -91,7 +91,7 @@ static int loongson_card_parse_acpi(struct loongson_card_data *data)
 	const char *codec_dai_name;
 	struct acpi_device *adev;
 	struct device *phy_dev;
-	int i;
+	int i, ret;
 
 	/* fixup platform name based on reference node */
 	adev = loongson_card_acpi_find_device(card, "cpu");
@@ -108,7 +108,9 @@ static int loongson_card_parse_acpi(struct loongson_card_data *data)
 		return -ENOENT;
 	snprintf(codec_name, sizeof(codec_name), "i2c-%s", acpi_dev_name(adev));
 
-	device_property_read_string(card->dev, "codec-dai-name", &codec_dai_name);
+	ret = device_property_read_string(card->dev, "codec-dai-name", &codec_dai_name);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < card->num_links; i++) {
 		loongson_dai_links[i].platforms->name = dev_name(phy_dev);
Signature · 1 line
2.52.0
author maintainer
#3 lore raw

PATCH v3 2/9 ASoC: dt-bindings: loongson,ls2k1000-i2s: Document Loongson-2K0300 compatible

In reply to Binbin Zhou

Add a new compatible string loongson,ls2k0300-i2s for the I2S controller found on Loongson-2K0300 SoC.

Unlike Loongson-2K1000, Loongson-2K0300 does not require the second register region for APB DMA configuration, so update the binding to allow a single reg entry.

---
 .../bindings/sound/loongson,ls2k1000-i2s.yaml | 22 ++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/sound/loongson,ls2k1000-i2s.yaml +21 -1
index da79510bb2d9..51e23c189f7a 100644
@@ -14,9 +14,12 @@ allOf:
 
 properties:
   compatible:
-    const: loongson,ls2k1000-i2s
+    enum:
+      - loongson,ls2k0300-i2s
+      - loongson,ls2k1000-i2s
 
   reg:
+    minItems: 1
     items:
       - description: Loongson I2S controller Registers.
       - description: APB DMA config register for Loongson I2S controller.
@@ -49,6 +52,23 @@ required:
 
 unevaluatedProperties: false
 
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls2k1000-i2s
+
+then:
+  properties:
+    reg:
+      minItems: 2
+
+else:
+  properties:
+    reg:
+      maxItems: 1
+
 examples:
   - |
     #include <dt-bindings/clock/loongson,ls2k-clk.h>
Signature · 1 line
2.52.0
author maintainer
#4 lore raw

PATCH v3 3/9 ASoC: loongson: Add Loongson-2K0300 I2S controller support

In reply to Binbin Zhou

The Loongson-2K0300 I2S interface differs significantly from the Loongson-2K1000. Although both utilize external DMA controllers, the Loongson-2K0300 does not require additional registers for routing configuration.

Due to hardware design flaw, an extra controller reset sequence is required during probe.

---
 sound/soc/loongson/loongson_i2s_plat.c | 42 +++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 7 deletions(-)
sound/soc/loongson/loongson_i2s_plat.c +35 -7
index ac054b6ce632..82d95c6644ef 100644
@@ -2,7 +2,7 @@
 //
 // Loongson I2S controller master mode dirver(platform device)
 //
-// Copyright (C) 2023-2024 Loongson Technology Corporation Limited
+// Copyright (C) 2023-2026 Loongson Technology Corporation Limited
 //
 // Author: Yingkun Meng <[email protected]>
 //         Binbin Zhou <[email protected]>
@@ -21,6 +21,7 @@
 #include "loongson_i2s.h"
 #include "loongson_dma.h"
 
+/* Loongson-2K1000 APBDMA routing */
 #define LOONGSON_I2S_RX_DMA_OFFSET	21
 #define LOONGSON_I2S_TX_DMA_OFFSET	18
 
@@ -30,6 +31,11 @@
 #define LOONGSON_DMA3_CONF	0x3
 #define LOONGSON_DMA4_CONF	0x4
 
+struct loongson_i2s_plat_config {
+	int rev_id;
+	int (*i2s_dma_config)(struct platform_device *pdev);
+};
+
 static int loongson_i2s_apbdma_config(struct platform_device *pdev)
 {
 	int val;
@@ -47,8 +53,18 @@ static int loongson_i2s_apbdma_config(struct platform_device *pdev)
 	return 0;
 }
 
+static const struct loongson_i2s_plat_config ls2k0300_i2s_plat_config = {
+	.rev_id = 1,
+};
+
+static const struct loongson_i2s_plat_config ls2k1000_i2s_plat_config = {
+	.rev_id = 0,
+	.i2s_dma_config = loongson_i2s_apbdma_config,
+};
+
 static int loongson_i2s_plat_probe(struct platform_device *pdev)
 {
+	const struct loongson_i2s_plat_config *plat_config;
 	struct device *dev = &pdev->dev;
 	struct loongson_i2s *i2s;
 	struct resource *res;
@@ -59,12 +75,17 @@ static int loongson_i2s_plat_probe(struct platform_device *pdev)
 	if (!i2s)
 		return -ENOMEM;
 
-	ret = loongson_i2s_apbdma_config(pdev);
-	if (ret)
-		return ret;
+	plat_config = device_get_match_data(dev);
+	if (!plat_config)
+		return -EINVAL;
 
-	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	i2s->reg_base = devm_ioremap_resource(&pdev->dev, res);
+	if (plat_config->i2s_dma_config) {
+		ret = plat_config->i2s_dma_config(pdev);
+		if (ret)
+			return ret;
+	}
+
+	i2s->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
 	if (IS_ERR(i2s->reg_base))
 		return dev_err_probe(dev, PTR_ERR(i2s->reg_base),
 				     "devm_ioremap_resource failed\n");
@@ -87,11 +108,17 @@ static int loongson_i2s_plat_probe(struct platform_device *pdev)
 	if (IS_ERR(i2s_clk))
 		return dev_err_probe(dev, PTR_ERR(i2s_clk), "clock property invalid\n");
 	i2s->clk_rate = clk_get_rate(i2s_clk);
+	i2s->rev_id = plat_config->rev_id;
 
 	dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
 	dev_set_name(dev, LS_I2S_DRVNAME);
 	dev_set_drvdata(dev, i2s);
 
+	if (i2s->rev_id == 1) {
+		regmap_update_bits(i2s->regmap, LS_I2S_CTRL, I2S_CTRL_RESET, I2S_CTRL_RESET);
+		fsleep(200);
+	}
+
 	ret = devm_snd_soc_register_component(dev, &loongson_i2s_edma_component,
 					      &loongson_i2s_dai, 1);
 	if (ret)
@@ -102,7 +129,8 @@ static int loongson_i2s_plat_probe(struct platform_device *pdev)
 }
 
 static const struct of_device_id loongson_i2s_ids[] = {
-	{ .compatible = "loongson,ls2k1000-i2s" },
+	{ .compatible = "loongson,ls2k0300-i2s", .data = &ls2k0300_i2s_plat_config },
+	{ .compatible = "loongson,ls2k1000-i2s", .data = &ls2k1000_i2s_plat_config },
 	{ /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, loongson_i2s_ids);
Signature · 1 line
2.52.0
author maintainer
#8 lore raw

PATCH v3 5/9 ASoC: dt-bindings: loongson,ls-audio-card: Add ctcisz forever pi compatible

In reply to Binbin Zhou

Add a new compatible string loongson,ls2k0300-forever-pi-audio-card for the audio card on Loongson-2K0300 ctcisz forever pi SoC. It uses a different DAI format compared to existing Loongson platforms.

The existing "loongson,ls-audio-card" remains valid for LS7A, Loongson-2K1000 and Loongson-2K2000.

---
 .../devicetree/bindings/sound/loongson,ls-audio-card.yaml     | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +3 -1
index e1b7445a8b22..8c214e5d04b1 100644
@@ -20,7 +20,9 @@ allOf:
 
 properties:
   compatible:
-    const: loongson,ls-audio-card
+    enum:
+      - loongson,ls-audio-card  # Loongson-2K1000/Loongson-2K2000/LS7A
+      - loongson,ls2k0300-forever-pi-audio-card # CTCISZ Forever Pi
 
   mclk-fs:
     $ref: simple-card.yaml#/definitions/mclk-fs
Signature · 1 line
2.52.0
author maintainer
#10 lore raw

PATCH v3 6/9 ASoC: loongson: Add Loongson-2K0300 CTCISZ Forever Pi sound card support

In reply to Binbin Zhou

The Loongson-2K0300 audio card uses a different DAI format compared to existing Loongson platforms.

Move the dai_fmt setting from the static DAI link to runtime hw_params via snd_soc_runtime_set_dai_fmt(), and pass the correct format through driver match data.

---
 sound/soc/loongson/loongson_card.c | 34 +++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)
sound/soc/loongson/loongson_card.c +29 -5
index ea895fe6b5e9..0e63cbcad57a 100644
@@ -2,8 +2,9 @@
 //
 // Loongson ASoC Audio Machine driver
 //
-// Copyright (C) 2023 Loongson Technology Corporation Limited
+// Copyright (C) 2023-2026 Loongson Technology Corporation Limited
 // Author: Yingkun Meng <[email protected]>
+//         Binbin Zhou <[email protected]>
 //
 
 #include <linux/module.h>
@@ -18,6 +19,19 @@ static char codec_name[SND_ACPI_I2C_ID_LEN];
 struct loongson_card_data {
 	struct snd_soc_card snd_card;
 	unsigned int mclk_fs;
+	const struct loongson_card_config *cfg;
+};
+
+struct loongson_card_config {
+	unsigned int fmt;
+};
+
+static const struct loongson_card_config ls2k1000_card_config = {
+	.fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBC_CFC,
+};
+
+static const struct loongson_card_config ls2k0300_forever_pi_card_config = {
+	.fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC,
 };
 
 static int loongson_card_hw_params(struct snd_pcm_substream *substream,
@@ -45,7 +59,7 @@ static int loongson_card_hw_params(struct snd_pcm_substream *substream,
 		return ret;
 	}
 
-	return 0;
+	return snd_soc_runtime_set_dai_fmt(rtd, ls_card->cfg->fmt);
 }
 
 static const struct snd_soc_ops loongson_ops = {
@@ -61,8 +75,6 @@ static struct snd_soc_dai_link loongson_dai_links[] = {
 	{
 		.name = "Loongson Audio Port",
 		.stream_name = "Loongson Audio",
-		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_IB_NF
-			| SND_SOC_DAIFMT_CBC_CFC,
 		SND_SOC_DAILINK_REG(analog),
 		.ops = &loongson_ops,
 	},
@@ -177,6 +189,10 @@ static int loongson_asoc_card_probe(struct platform_device *pdev)
 	if (!ls_priv)
 		return -ENOMEM;
 
+	ls_priv->cfg = (const struct loongson_card_config *)device_get_match_data(dev);
+	if (!ls_priv->cfg)
+		return -EINVAL;
+
 	card = &ls_priv->snd_card;
 
 	card->dev = dev;
@@ -202,7 +218,15 @@ static int loongson_asoc_card_probe(struct platform_device *pdev)
 }
 
 static const struct of_device_id loongson_asoc_dt_ids[] = {
-	{ .compatible = "loongson,ls-audio-card" },
+	/* Loongson-2K1000/Loongson-2K2000/LS7A */
+	{
+		.compatible = "loongson,ls-audio-card",
+		.data = &ls2k1000_card_config
+	},
+	{
+		.compatible = "loongson,ls2k0300-forever-pi-audio-card",
+		.data = &ls2k0300_forever_pi_card_config
+	},
 	{ /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, loongson_asoc_dt_ids);
Signature · 1 line
2.52.0
author maintainer
#14 lore raw

PATCH v3 7/9 ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B compatible

In reply to Binbin Zhou

Add new compatible for the ATK-DL2K0300B development board based on Loongson-2K0300.

Unlike others, this board features GPIO-controlled headphone detection, headphone control, and speaker enable.

---
 .../sound/loongson,ls-audio-card.yaml         | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +38 -0
index 8c214e5d04b1..dc7f4afbb777 100644
@@ -23,6 +23,7 @@ properties:
     enum:
       - loongson,ls-audio-card  # Loongson-2K1000/Loongson-2K2000/LS7A
       - loongson,ls2k0300-forever-pi-audio-card # CTCISZ Forever Pi
+      - loongson,ls2k0300-dl2k0300b-audio-card # ATK-DL2K0300B
 
   mclk-fs:
     $ref: simple-card.yaml#/definitions/mclk-fs
@@ -47,6 +48,18 @@ properties:
     required:
       - sound-dai
 
+  spkr-en-gpios:
+    maxItems: 1
+    description: The GPIO that enables the speakers
+
+  hp-ctl-gpios:
+    maxItems: 1
+    description: The GPIO that control the headphones
+
+  hp-det-gpios:
+    maxItems: 1
+    description: The GPIO that detect headphones are plugged in
+
 required:
   - compatible
   - mclk-fs
@@ -69,3 +82,28 @@ examples:
              sound-dai = <&es8323>;
         };
     };
+
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    sound {
+        compatible = "loongson,ls2k0300-dl2k0300b-audio-card";
+        model = "loongson-audio";
+        mclk-fs = <512>;
+        hp-det-gpios = <&gpio 81 GPIO_ACTIVE_HIGH>;
+        spkr-en-gpios = <&gpio 86 GPIO_ACTIVE_HIGH>;
+        hp-ctl-gpios = <&gpio 87 GPIO_ACTIVE_HIGH>;
+        audio-routing =
+                      "Headphone", "LOUT1",
+                      "Headphone", "ROUT1",
+                      "Speaker", "LOUT2",
+                      "Speaker", "ROUT2";
+
+        cpu {
+            sound-dai = <&i2s>;
+        };
+
+        codec {
+             sound-dai = <&es8388>;
+        };
+    };
Signature · 1 line
2.52.0
author maintainer
#6 lore raw

PATCH v3 4/9 ASoC: dt-bindings: loongson,ls-audio-card: Use common sound card

In reply to Binbin Zhou

Reference the common sound card properties. This allows removing the model property and directly using the common audio-routing property later on.

---
 .../bindings/sound/loongson,ls-audio-card.yaml        | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +5 -6
index 61e8babed402..e1b7445a8b22 100644
@@ -8,20 +8,20 @@ title: Loongson 7axxx/2kxxx ASoC audio sound card driver
 
 maintainers:
   - Yingkun Meng <[email protected]>
+  - Binbin Zhou <[email protected]>
 
 description:
   The binding describes the sound card present in loongson
   7axxx/2kxxx platform. The sound card is an ASoC component
   which uses Loongson I2S controller to transfer the audio data.
 
+allOf:
+  - $ref: sound-card-common.yaml#
+
 properties:
   compatible:
     const: loongson,ls-audio-card
 
-  model:
-    $ref: /schemas/types.yaml#/definitions/string
-    description: User specified audio sound card name
-
   mclk-fs:
     $ref: simple-card.yaml#/definitions/mclk-fs
 
@@ -47,12 +47,11 @@ properties:
 
 required:
   - compatible
-  - model
   - mclk-fs
   - cpu
   - codec
 
-additionalProperties: false
+unevaluatedProperties: false
 
 examples:
   - |
Signature · 1 line
2.52.0
author maintainer
#21 lore raw

PATCH v3 9/9 ASoC: es8328: Add DAPM routes from MIC inputs to Mic Bias

In reply to Binbin Zhou

The ES8328 codec has differential/single-ended microphone inputs (LINPUT1/RINPUT1, LINPUT2/RINPUT2) that require connection to the internal Mic Bias generator for proper operation. Currently, these routes are missing, which can cause microphone recording to fail.

Add the missing DAPM routes to link the input pins to the Mic Bias supply, ensuring the microphone bias voltage is correctly applied.

---
PS:
I'm not sure if this patch is also required for other platforms using the ES8328. If not, I'll drop the patch and add this DAPM route to the audio-routing property of the Loongson ASoC machine driver.

 sound/soc/codecs/es8328.c | 5 +++++
 1 file changed, 5 insertions(+)
sound/soc/codecs/es8328.c +5 -0
index 9838fe42cb6f..aaa6646ad4c5 100644
@@ -405,6 +405,11 @@ static const struct snd_soc_dapm_route es8328_dapm_routes[] = {
 
 	{ "Mic Bias", NULL, "Mic Bias Gen" },
 
+	{ "LINPUT1", NULL, "Mic Bias" },
+	{ "RINPUT1", NULL, "Mic Bias" },
+	{ "LINPUT2", NULL, "Mic Bias" },
+	{ "RINPUT2", NULL, "Mic Bias" },
+
 	{ "Left Mixer", NULL, "Left DAC" },
 	{ "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
 	{ "Left Mixer", "Right Playback Switch", "Right DAC" },
Signature · 1 line
2.52.0
author maintainer
#19 lore raw

PATCH v3 8/9 ASoC: loongson: Add headphone jack detection and DAPM routing

In reply to Binbin Zhou

Extend the Loongson audio machine driver with jack detection, DAPM widgets support, enabling proper switching between headphones and speakers on the Loongson-2K0300 ATK-DL2K0300B board.

---
 sound/soc/loongson/loongson_card.c | 131 ++++++++++++++++++++++++++++-
 1 file changed, 127 insertions(+), 4 deletions(-)
sound/soc/loongson/loongson_card.c +127 -4
index 0e63cbcad57a..25cd12eab4b1 100644
@@ -7,33 +7,126 @@
 //         Binbin Zhou <[email protected]>
 //
 
-#include <linux/module.h>
-#include <sound/soc.h>
-#include <sound/soc-acpi.h>
 #include <linux/acpi.h>
+#include <linux/gpio/consumer.h>
+#include <linux/module.h>
 #include <linux/pci.h>
+#include <sound/jack.h>
 #include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/soc-acpi.h>
 
 static char codec_name[SND_ACPI_I2C_ID_LEN];
 
 struct loongson_card_data {
 	struct snd_soc_card snd_card;
 	unsigned int mclk_fs;
+	struct gpio_desc *gpiod_hp_det;
+	struct gpio_desc *gpiod_hp_ctl;
+	struct gpio_desc *gpiod_spkr_en;
 	const struct loongson_card_config *cfg;
 };
 
 struct loongson_card_config {
 	unsigned int fmt;
+	bool add_hp_jack;
+	bool add_dapm_widgets;
+	bool add_dapm_routes;
 };
 
 static const struct loongson_card_config ls2k1000_card_config = {
 	.fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBC_CFC,
+	.add_hp_jack = false,
+	.add_dapm_widgets = false,
+	.add_dapm_routes = false,
 };
 
 static const struct loongson_card_config ls2k0300_forever_pi_card_config = {
 	.fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC,
+	.add_hp_jack = false,
+	.add_dapm_widgets = false,
+	.add_dapm_routes = false,
+};
+
+static const struct loongson_card_config ls2k0300_dl2k0300b_card_config = {
+	.fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC,
+	.add_hp_jack = true,
+	.add_dapm_widgets = true,
+	.add_dapm_routes = true,
+};
+
+static int tegra_machine_event(struct snd_soc_dapm_widget *w,
+			       struct snd_kcontrol *k, int event)
+{
+	struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm);
+	struct loongson_card_data *priv = snd_soc_card_get_drvdata(card);
+
+	if (!snd_soc_dapm_widget_name_cmp(w, "Speaker"))
+		gpiod_set_value_cansleep(priv->gpiod_spkr_en,
+					 SND_SOC_DAPM_EVENT_ON(event));
+
+	if (!snd_soc_dapm_widget_name_cmp(w, "Headphone"))
+		gpiod_set_value_cansleep(priv->gpiod_hp_ctl,
+					 SND_SOC_DAPM_EVENT_ON(event));
+
+	return 0;
+}
+
+static const struct snd_soc_dapm_widget loongson_aosc_dapm_widgets[] = {
+	SND_SOC_DAPM_HP("Headphone", tegra_machine_event),
+	SND_SOC_DAPM_SPK("Speaker", tegra_machine_event),
+};
+
+/* Headphones Jack */
+
+static struct snd_soc_jack loongson_asoc_hp_jack;
+
+static struct snd_soc_jack_pin loongson_asoc_hp_jack_pins[] = {
+	{
+		.pin = "Headphone",
+		.mask = SND_JACK_HEADPHONE
+	},
+	{
+		.pin = "Speaker",
+		.mask = SND_JACK_HEADPHONE,
+		.invert = 1
+	},
+};
+
+static struct snd_soc_jack_gpio loongson_asoc_hp_jack_gpio = {
+	.name = "Headphones detection",
+	.report = SND_JACK_HEADPHONE,
+	.debounce_time = 150,
 };
 
+static int loongson_asoc_machine_init(struct snd_soc_pcm_runtime *rtd)
+{
+	struct snd_soc_card *card = rtd->card;
+	struct loongson_card_data *ls_priv = snd_soc_card_get_drvdata(card);
+	int ret = 0;
+
+	if (!ls_priv->cfg->add_hp_jack || !ls_priv->gpiod_hp_det)
+		return 0;
+
+	ret = snd_soc_card_jack_new_pins(card, "Headphones Jack",
+					 SND_JACK_HEADPHONE,
+					 &loongson_asoc_hp_jack,
+					 loongson_asoc_hp_jack_pins,
+					 ARRAY_SIZE(loongson_asoc_hp_jack_pins));
+	if (ret) {
+		dev_err(rtd->dev, "Headphones Jack creation failed: %d\n", ret);
+		return ret;
+	}
+
+	loongson_asoc_hp_jack_gpio.desc = ls_priv->gpiod_hp_det;
+
+	ret = snd_soc_jack_add_gpios(&loongson_asoc_hp_jack, 1, &loongson_asoc_hp_jack_gpio);
+	if (ret)
+		dev_err(rtd->dev, "Headphone GPIO not added: %d\n", ret);
+
+	return ret;
+}
+
 static int loongson_card_hw_params(struct snd_pcm_substream *substream,
 				   struct snd_pcm_hw_params *params)
 {
@@ -75,6 +168,7 @@ static struct snd_soc_dai_link loongson_dai_links[] = {
 	{
 		.name = "Loongson Audio Port",
 		.stream_name = "Loongson Audio",
+		.init = loongson_asoc_machine_init,
 		SND_SOC_DAILINK_REG(analog),
 		.ops = &loongson_ops,
 	},
@@ -135,16 +229,35 @@ static int loongson_card_parse_acpi(struct loongson_card_data *data)
 
 static int loongson_card_parse_of(struct loongson_card_data *data)
 {
-	struct device_node *cpu, *codec;
 	struct snd_soc_card *card = &data->snd_card;
+	struct device_node *cpu, *codec;
 	struct device *dev = card->dev;
 	int ret, i;
 
+	data->gpiod_hp_det = devm_gpiod_get_optional(dev, "hp-det", GPIOD_IN);
+	if (IS_ERR(data->gpiod_hp_det))
+		return PTR_ERR(data->gpiod_hp_det);
+
+	data->gpiod_hp_ctl = devm_gpiod_get_optional(dev, "hp-ctl", GPIOD_OUT_LOW);
+	if (IS_ERR(data->gpiod_hp_ctl))
+		return PTR_ERR(data->gpiod_hp_ctl);
+
+	data->gpiod_spkr_en = devm_gpiod_get_optional(dev, "spkr-en", GPIOD_OUT_LOW);
+	if (IS_ERR(data->gpiod_spkr_en))
+		return PTR_ERR(data->gpiod_spkr_en);
+
+	if (data->cfg->add_dapm_routes) {
+		ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
+		if (ret)
+			return ret;
+	}
+
 	cpu = of_get_child_by_name(dev->of_node, "cpu");
 	if (!cpu) {
 		dev_err(dev, "platform property missing or invalid\n");
 		return -EINVAL;
 	}
+
 	codec = of_get_child_by_name(dev->of_node, "codec");
 	if (!codec) {
 		dev_err(dev, "audio-codec property missing or invalid\n");
@@ -199,6 +312,12 @@ static int loongson_asoc_card_probe(struct platform_device *pdev)
 	card->owner = THIS_MODULE;
 	card->dai_link = loongson_dai_links;
 	card->num_links = ARRAY_SIZE(loongson_dai_links);
+
+	if (ls_priv->cfg->add_dapm_widgets) {
+		card->dapm_widgets = loongson_aosc_dapm_widgets;
+		card->num_dapm_widgets = ARRAY_SIZE(loongson_aosc_dapm_widgets);
+	}
+
 	snd_soc_card_set_drvdata(card, ls_priv);
 
 	ret = device_property_read_string(dev, "model", &card->name);
@@ -227,6 +346,10 @@ static const struct of_device_id loongson_asoc_dt_ids[] = {
 		.compatible = "loongson,ls2k0300-forever-pi-audio-card",
 		.data = &ls2k0300_forever_pi_card_config
 	},
+	{
+		.compatible = "loongson,ls2k0300-dl2k0300b-audio-card",
+		.data = &ls2k0300_dl2k0300b_card_config
+	},
 	{ /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, loongson_asoc_dt_ids);
Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH v3 3/9] ASoC: loongson: Add Loongson-2K0300 I2S controller support

In reply to Binbin Zhou
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Missing hardware quirk reset sequence on system resume.
- [Medium] Read-Modify-Write on a volatile control register can unintentionally re-enable transient hardware states during soft reset.

Pre-existing issues:
- [High] Illegal overriding of platform device name during probe leading to Use-After-Free and sysfs corruption. --

commit b9499f3634cc6b7a7bad0f418f80a05c56aa0954
Author: Binbin Zhou <[email protected]>

ASoC: loongson: Add Loongson-2K0300 I2S controller support

The Loongson-2K0300 I2S interface differs significantly from the Loongson-2K1000. Although both utilize external DMA controllers, the Loongson-2K0300 does not require additional registers for routing configuration.

Due to hardware design flaw, an extra controller reset sequence is required during probe.

Quoted 4 lines
sound/soc/loongson/loongson_i2s_plat.c +0 -0
index ac054b6ce6322..82d95c6644ef0 100644

[ ... ]

sound/soc/loongson/loongson_i2s_plat.c static int loongson_i2s_plat_probe(struct platform_device *pdev) · line 114

Quoted 8 lines
sound/soc/loongson/loongson_i2s_plat.c +1 -0
@@ -87,11 +108,17 @@ static int loongson_i2s_plat_probe(struct platform_device *pdev)
 	if (IS_ERR(i2s_clk))
 		return dev_err_probe(dev, PTR_ERR(i2s_clk), "clock property invalid\n");
 	i2s->clk_rate = clk_get_rate(i2s_clk);
+	i2s->rev_id = plat_config->rev_id;
 
 	dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
 	dev_set_name(dev, LS_I2S_DRVNAME);

[Severity: High]
This is a pre-existing issue, but does calling dev_set_name() on an already registered platform device cause a use-after-free?

Calling dev_set_name() here frees the existing kobj->name without renaming the underlying sysfs directory.

Could this corrupt the kobject state and lead to kernel warnings upon device removal?

sound/soc/loongson/loongson_i2s_plat.c static int loongson_i2s_plat_probe(struct platform_device *pdev) · line 118

Quoted 4 lines
sound/soc/loongson/loongson_i2s_plat.c +2 -0
 	dev_set_drvdata(dev, i2s);
 
+	if (i2s->rev_id == 1) {
+		regmap_update_bits(i2s->regmap, LS_I2S_CTRL, I2S_CTRL_RESET, I2S_CTRL_RESET);

[Severity: High]
Since this controller requires a reset due to a hardware flaw, does it also need this reset applied in the resume path?

When the system suspends, power to the controller is typically lost. Upon resume, the shared i2s_resume() function is called:

sound/soc/loongson/loongson_i2s.c:i2s_resume() {
	struct loongson_i2s *i2s = dev_get_drvdata(dev);

	regcache_cache_only(i2s->regmap, false);

	return regcache_sync(i2s->regmap);
}

Because LS_I2S_CTRL is typically a volatile register, regcache_sync() will not automatically restore it. Will this leave the hardware design flaw unmitigated after a suspend/resume cycle?

[Severity: Medium]
Does using regmap_update_bits() on a volatile control register risk preserving unintended active states during the soft reset?

Since this performs a read-modify-write, if the bootloader or previous uninitialized state left other functional bits active, they might be logically preserved and kept enabled during the reset sequence.

Would it be safer to use regmap_write() to assert the reset while explicitly ensuring all other functional bits are zeroed out?

Quoted 6 lines
sound/soc/loongson/loongson_i2s_plat.c +3 -0
+		fsleep(200);
+	}
+
 	ret = devm_snd_soc_register_component(dev, &loongson_i2s_edma_component,
 					      &loongson_i2s_dai, 1);
 	if (ret)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

Re: [PATCH v3 4/9] ASoC: dt-bindings: loongson,ls-audio-card: Use common sound card

In reply to Binbin Zhou
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Global state corruption via in-place modification of static DAI links array and codec string buffer.
- [High] Sysfs and ASoC component name collision via illegal dev_set_name override during probe.
- [High] Bitwise read-modify-write bug without clearing target bitfields causes incorrect APB DMA routing. --

commit 864c9404304a7ef17062fbe81f6d13b229c50a0d
Author: Binbin Zhou <[email protected]>
Subject: ASoC: dt-bindings: loongson,ls-audio-card: Use common sound card

This commit updates the loongson,ls-audio-card binding to reference the common sound card schema. It removes the user specified model property and transitions to evaluating additional properties via unevaluatedProperties.

Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml

Quoted 4 lines
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +0 -0
index 61e8babed402f..e1b7445a8b22f 100644

[ ... ]

While reviewing the related sound driver for this binding, I noticed a few pre-existing items in the driver code.

[Severity: High]
This is a pre-existing issue, but I noticed a potential problem in the sound card driver when handling multiple instances.

In sound/soc/loongson/loongson_card.c:loongson_card_parse_acpi():

    loongson_dai_links[i].platforms->name = dev_name(phy_dev);
    loongson_dai_links[i].codecs->name = codec_name;
    loongson_dai_links[i].codecs->dai_name = codec_dai_name;

And similarly in sound/soc/loongson/loongson_card.c:loongson_card_parse_of():

    ret = snd_soc_of_get_dlc(cpu, NULL, loongson_dai_links[i].cpus, 0);
    ...
    loongson_dai_links[i].platforms->of_node =
        loongson_dai_links[i].cpus->of_node;
    ret = snd_soc_of_get_dlc(codec, NULL, loongson_dai_links[i].codecs, 0);

Will this corrupt the shared static loongson_dai_links array if multiple sound cards are instantiated via the device tree or ACPI tables?

If multiple instances are probed, they might overwrite each other's DAI links and codec strings. Should these structures be dynamically allocated instead of mutating shared static memory?

[Severity: High]
This is a pre-existing issue, but there might be a sysfs naming collision in the I2S platform driver.

In sound/soc/loongson/loongson_i2s_plat.c:loongson_i2s_plat_probe():

    dev_set_name(dev, LS_I2S_DRVNAME);

Could this override cause duplicate ASoC component names if multiple I2S controllers are instantiated in the device tree?

Calling dev_set_name during probe after the device is added to sysfs might break internal kernel tracking and cause probe failures for subsequent instances.

[Severity: High]
This is a pre-existing issue, but there appears to be a bitwise read-modify-write problem in the APB DMA configuration.

In sound/soc/loongson/loongson_i2s_plat.c:loongson_i2s_apbdma_config():

    val |= LOONGSON_DMA2_CONF << LOONGSON_I2S_TX_DMA_OFFSET;
    val |= LOONGSON_DMA3_CONF << LOONGSON_I2S_RX_DMA_OFFSET;

Does this code correctly handle cases where the bootloader or previous firmware left the DMA configuration register in a non-zero state?

Since LOONGSON_DMA2_CONF occupies multiple bits, if other bits are already set, the bitwise OR will blend the values instead of strictly setting the field.

Should the target bitfields be cleared via a bitwise AND mask before applying the new configuration?

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

Re: [PATCH v3 6/9] ASoC: loongson: Add Loongson-2K0300 CTCISZ Forever Pi sound card support

In reply to Binbin Zhou
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] DAI format configuration is entirely bypassed if `mclk-fs` is 0. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] DAI format configuration is entirely bypassed if mclk-fs is 0. --

commit 00af2efc83640c13d13fd25311896531fc01a8e7
Author: Binbin Zhou <[email protected]>

ASoC: loongson: Add Loongson-2K0300 CTCISZ Forever Pi sound card support

The Loongson-2K0300 audio card uses a different DAI format compared to existing Loongson platforms. Move the dai_fmt setting from the static DAI link to runtime hw_params via snd_soc_runtime_set_dai_fmt(), and pass the correct format through driver match data.

Quoted 4 lines
sound/soc/loongson/loongson_card.c +0 -0
index ea895fe6b5e97..0e63cbcad57ac 100644

[ ... ]

sound/soc/loongson/loongson_card.c static int loongson_card_hw_params(struct snd_pcm_substream *substream, · line 63

Quoted 7 lines
sound/soc/loongson/loongson_card.c +1 -1
@@ -45,7 +59,7 @@ static int loongson_card_hw_params(struct snd_pcm_substream *substream,
 		return ret;
 	}
 
-	return 0;
+	return snd_soc_runtime_set_dai_fmt(rtd, ls_card->cfg->fmt);
 }

[Severity: Medium]
If a platform's firmware explicitly sets the mclk-fs property to 0, loongson_card_hw_params() will exit early:

	if (!ls_card->mclk_fs)
		return 0;

By moving the format configuration to the end of this function, will the CPU and CODEC DAIs fail to be configured with the correct format when mclk-fs is 0, since the early return bypasses this new call?

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

Re: [PATCH v3 1/9] ASoC: loongson: Fix error handling in ACPI property parsing

In reply to Binbin Zhou
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The driver statically allocates global variables (codec_name, loongson_dai_links) and modifies them locklessly during the device probe phase without creating instance-specific copies. This leads to data races, state corruption on probe deferral, and a use-after-free vulnerability upon device unbind. --

commit b0aab833787c20534a1a8d5a4fb96b7f089e37e4
Author: Binbin Zhou <[email protected]>

ASoC: loongson: Fix error handling in ACPI property parsing

This commit fixes a missing error check for device_property_read_string() in loongson_card_parse_acpi(). It propagates the error if the codec-dai-name property is missing or invalid, preventing the use of an uninitialized pointer.

Quoted 3 lines
sound/soc/loongson/loongson_card.c +0 -0

[ ... ]

sound/soc/loongson/loongson_card.c static int loongson_card_parse_acpi(struct loongson_card_data *data) · line 116

Quoted 11 lines
sound/soc/loongson/loongson_card.c +3 -1
@@ -108,7 +108,9 @@ static int loongson_card_parse_acpi(struct loongson_card_data *data)
 		return -ENOENT;
 	snprintf(codec_name, sizeof(codec_name), "i2c-%s", acpi_dev_name(adev));
 
-	device_property_read_string(card->dev, "codec-dai-name", &codec_dai_name);
+	ret = device_property_read_string(card->dev, "codec-dai-name", &codec_dai_name);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < card->num_links; i++) {
 		loongson_dai_links[i].platforms->name = dev_name(phy_dev);

[Severity: High]
This is a pre-existing issue, but I noticed that loongson_dai_links appears to be a statically allocated global array being modified locklessly here with instance-specific pointers like dev_name(phy_dev).

If the device is unbound, could this leave a dangling pointer in the global array?

Additionally, if multiple audio cards are instantiated, could they overwrite each other's state in this shared array?

Similarly, in loongson_card_parse_of(), snd_soc_of_get_dlc() acquires a reference to an of_node and stores it in this global array. If probe defers, does the error path fail to clean up the globally stored refcount, causing a memory leak?

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

Re: [PATCH v3 7/9] ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B compatible

In reply to Binbin Zhou
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Custom GPIO properties lack a vendor prefix. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Custom GPIO properties lack a vendor prefix.
--

commit ad0ddc0bcb1797ef1e44f8d7b02415a3c55eb74b
Author: Binbin Zhou <[email protected]>
ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B compatible

This commit adds a new compatible string for the ATK-DL2K0300B development board based on Loongson-2K0300. It also introduces several new GPIO properties to support hardware features like speaker enable and headphone detection.

Quoted 3 lines
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +0 -0

[ ... ]

Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml properties: · line 57

Quoted 11 lines
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +7 -0
@@ -47,6 +48,18 @@ properties:
     required:
       - sound-dai
 
+  spkr-en-gpios:
+    maxItems: 1
+    description: The GPIO that enables the speakers
+
+  hp-ctl-gpios:
+    maxItems: 1
+    description: The GPIO that control the headphones

[Severity: Low]
This isn't a bug, but should these custom board-specific GPIO properties include a vendor prefix?

Since they are unique and non-standard properties, using a prefix like loongson,spkr-en-gpios and loongson,hp-ctl-gpios would better align with device tree naming conventions.

Quoted 4 lines
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +4 -0
+
+  hp-det-gpios:
+    maxItems: 1
+    description: The GPIO that detect headphones are plugged in
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#22 lore raw

Re: [PATCH v3 9/9] ASoC: es8328: Add DAPM routes from MIC inputs to Mic Bias

In reply to Binbin Zhou
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Hardcoding board-specific 'Mic Bias' routes to generic multipurpose input pins in the codec…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Hardcoding board-specific 'Mic Bias' routes to generic multipurpose input pins in the codec driver violates DAPM architecture and causes a power regression. --

commit 864cdd66179a2d1429284999cab86b202f471e82
Author: Binbin Zhou <[email protected]>

ASoC: es8328: Add DAPM routes from MIC inputs to Mic Bias

This commit adds DAPM routes connecting the ES8328 generic microphone input pins (LINPUT1/2, RINPUT1/2) directly to the internal Mic Bias generator. This is intended to ensure proper bias voltage is applied when these pins are used for microphone recording.

Quoted 4 lines
sound/soc/codecs/es8328.c +0 -0
index 9838fe42cb6fc..aaa6646ad4c5e 100644

[ ... ]

sound/soc/codecs/es8328.c static const struct snd_soc_dapm_route es8328_dapm_routes[] = { · line 411

Quoted 8 lines
sound/soc/codecs/es8328.c +4 -0
@@ -405,6 +405,11 @@ static const struct snd_soc_dapm_route es8328_dapm_routes[] = {
 
 	{ "Mic Bias", NULL, "Mic Bias Gen" },
 
+	{ "LINPUT1", NULL, "Mic Bias" },
+	{ "RINPUT1", NULL, "Mic Bias" },
+	{ "LINPUT2", NULL, "Mic Bias" },
+	{ "RINPUT2", NULL, "Mic Bias" },

[Severity: Medium]
Does adding these unconditional routes in the codec driver violate DAPM architectural guidelines by hardcoding board-specific external connections?

Since LINPUT1/2 and RINPUT1/2 are generic multipurpose analog inputs that can be configured as either microphones or line-level inputs, forcing DAPM to always power the internal Mic Bias generator when these inputs are active could cause a power regression on boards that use them as Line-In.

Should this external physical connection between a microphone bias pin and the microphone jack be specified in the machine driver's device tree via the audio-routing property (for example, "Mic Jack", "Mic Bias") instead of being hardcoded globally in the generic codec driver?

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

Re: [PATCH v3 8/9] ASoC: loongson: Add headphone jack detection and DAPM routing

In reply to Binbin Zhou
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Double-free of the hp-det GPIO descriptor on probe failure or driver unbind. - [High] Global state…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Double-free of the hp-det GPIO descriptor on probe failure or driver unbind.
- [High] Global state mutation via static global instance structures.
- [Low] Misleading DAPM event handler name and widget array typos due to copy-pasting. --

commit 96bfdeee5a35a70f217e0f245943a82eb9a7ce3b
Author: Binbin Zhou <[email protected]>

ASoC: loongson: Add headphone jack detection and DAPM routing

Extend the Loongson audio machine driver with jack detection, DAPM widgets support, enabling proper switching between headphones and speakers on the Loongson-2K0300 ATK-DL2K0300B board.

Quoted 4 lines
sound/soc/loongson/loongson_card.c +0 -0
index 0e63cbcad57ac..25cd12eab4b1f 100644

[ ... ]

sound/soc/loongson/loongson_card.c line 60

Quoted 3 lines
sound/soc/loongson/loongson_card.c +3 -0
+static int tegra_machine_event(struct snd_soc_dapm_widget *w,
+			       struct snd_kcontrol *k, int event)
+{

[Severity: Low]
This isn't a bug, but should this function be renamed to match the loongson namespace instead of keeping the tegra_ prefix from a copied driver?

Quoted 6 lines
sound/soc/loongson/loongson_card.c +6 -0
+	struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm);
+	struct loongson_card_data *priv = snd_soc_card_get_drvdata(card);
+
+	if (!snd_soc_dapm_widget_name_cmp(w, "Speaker"))
+		gpiod_set_value_cansleep(priv->gpiod_spkr_en,
+					 SND_SOC_DAPM_EVENT_ON(event));

[ ... ]

Quoted 1 line

+static const struct snd_soc_dapm_widget loongson_aosc_dapm_widgets[] = {

[Severity: Low]
This isn't a bug, but there is a typo here (aosc instead of asoc).

Quoted 9 lines
sound/soc/loongson/loongson_card.c +9 -0
+	SND_SOC_DAPM_HP("Headphone", tegra_machine_event),
+	SND_SOC_DAPM_SPK("Speaker", tegra_machine_event),
+};
+
+/* Headphones Jack */
+
+static struct snd_soc_jack loongson_asoc_hp_jack;
+
+static struct snd_soc_jack_pin loongson_asoc_hp_jack_pins[] = {

[ ... ]

sound/soc/loongson/loongson_card.c

Quoted 5 lines
sound/soc/loongson/loongson_card.c +5 -0
+static struct snd_soc_jack_gpio loongson_asoc_hp_jack_gpio = {
+	.name = "Headphones detection",
+	.report = SND_JACK_HEADPHONE,
+	.debounce_time = 150,
+};

[Severity: High]
Will using a static global instance for snd_soc_jack_gpio cause cross-device state corruption?

The ASoC core function snd_soc_jack_add_gpios() takes ownership of this struct and initializes a delayed_work structure inside it. If multiple instances of this driver probe concurrently or across unbind/rebind cycles, multiple driver instances might mutate and re-initialize the same global static struct and its workqueue list head.

Could this be moved to the instance-specific loongson_card_data instead?

Quoted 6 lines
sound/soc/loongson/loongson_card.c +6 -0
+
+static int loongson_asoc_machine_init(struct snd_soc_pcm_runtime *rtd)
+{
+	struct snd_soc_card *card = rtd->card;
+	struct loongson_card_data *ls_priv = snd_soc_card_get_drvdata(card);
+	int ret = 0;

[ ... ]

sound/soc/loongson/loongson_card.c static int loongson_card_parse_acpi(struct loongson_card_data *data) · line 237

Quoted 11 lines
sound/soc/loongson/loongson_card.c +2 -1
@@ -135,16 +229,35 @@ static int loongson_card_parse_acpi(struct loongson_card_data *data)
 
 static int loongson_card_parse_of(struct loongson_card_data *data)
 {
-	struct device_node *cpu, *codec;
 	struct snd_soc_card *card = &data->snd_card;
+	struct device_node *cpu, *codec;
 	struct device *dev = card->dev;
 	int ret, i;
 
+	data->gpiod_hp_det = devm_gpiod_get_optional(dev, "hp-det", GPIOD_IN);

[Severity: High]
Will this devm-managed GPIO descriptor trigger a double-free on driver unbind or probe deferral?

When devm_gpiod_get_optional() requests the GPIO, it registers a devres cleanup action. Later in loongson_asoc_machine_init():

loongson_asoc_machine_init() {
    ...
    loongson_asoc_hp_jack_gpio.desc = ls_priv->gpiod_hp_det;
    ret = snd_soc_jack_add_gpios(&loongson_asoc_hp_jack, 1,
                                 &loongson_asoc_hp_jack_gpio);
    ...
}

The snd_soc_jack_add_gpios() call registers a second overlapping devres action that unconditionally frees the GPIO descriptor. When the device is unbound, won't both cleanup actions attempt to free the same gpio_device, leading to a Use-After-Free?

Quoted 2 lines
sound/soc/loongson/loongson_card.c +2 -0
+	if (IS_ERR(data->gpiod_hp_det))
+		return PTR_ERR(data->gpiod_hp_det);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Acked-by on v3 5/9
#9 lore raw

Re: [PATCH v3 5/9] ASoC: dt-bindings: loongson,ls-audio-card: Add ctcisz forever pi compatible

In reply to Binbin Zhou
Quoted 11 lines · Binbin Zhou

Add a new compatible string loongson,ls2k0300-forever-pi-audio-card for the audio card on Loongson-2K0300 ctcisz forever pi SoC. It uses a different DAI format compared to existing Loongson platforms.

The existing "loongson,ls-audio-card" remains valid for LS7A, Loongson-2K1000 and Loongson-2K2000.

---
 .../devicetree/bindings/sound/loongson,ls-audio-card.yaml     | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Best regards,
Krzysztof

#16 lore raw

Re: [PATCH v3 7/9] ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B compatible

In reply to Binbin Zhou
Quoted 29 lines · Binbin Zhou

Add new compatible for the ATK-DL2K0300B development board based on Loongson-2K0300.

Unlike others, this board features GPIO-controlled headphone detection, headphone control, and speaker enable.

---
 .../sound/loongson,ls-audio-card.yaml         | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +3 -0
index 8c214e5d04b1..dc7f4afbb777 100644
@@ -23,6 +23,7 @@ properties:
     enum:
       - loongson,ls-audio-card  # Loongson-2K1000/Loongson-2K2000/LS7A
       - loongson,ls2k0300-forever-pi-audio-card # CTCISZ Forever Pi
+      - loongson,ls2k0300-dl2k0300b-audio-card # ATK-DL2K0300B
 
   mclk-fs:
     $ref: simple-card.yaml#/definitions/mclk-fs
@@ -47,6 +48,18 @@ properties:
     required:
       - sound-dai
 
+  spkr-en-gpios:
+    maxItems: 1

Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml properties: · line 62

Quoted 10 lines
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +10 -0
+    description: The GPIO that enables the speakers
+
+  hp-ctl-gpios:
+    maxItems: 1
+    description: The GPIO that control the headphones
+
+  hp-det-gpios:
+    maxItems: 1
+    description: The GPIO that detect headphones are plugged in
+

Add allOf:if:then: block after "required:" block and disallow these ("...: false") for other variants.

If these stay, because I have doubts. spkr-en-gpios sounds like enable-gpios of each speaker, so not suitable here but should go to individual speaker codec nodes.

Best regards,
Krzysztof

#17 lore raw

Re: [PATCH v3 7/9] ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B compatible

Hi Krzysztof:

Thanks for your review.

Quoted 29 lines · Krzysztof Kozlowski
Quoted 27 lines · Binbin Zhou

Add new compatible for the ATK-DL2K0300B development board based on Loongson-2K0300.

Unlike others, this board features GPIO-controlled headphone detection, headphone control, and speaker enable.

---
 .../sound/loongson,ls-audio-card.yaml         | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +1 -0
index 8c214e5d04b1..dc7f4afbb777 100644
@@ -23,6 +23,7 @@ properties:
     enum:
       - loongson,ls-audio-card  # Loongson-2K1000/Loongson-2K2000/LS7A
       - loongson,ls2k0300-forever-pi-audio-card # CTCISZ Forever Pi
+      - loongson,ls2k0300-dl2k0300b-audio-card # ATK-DL2K0300B
 
   mclk-fs:
     $ref: simple-card.yaml#/definitions/mclk-fs
@@ -47,6 +48,18 @@ properties:
     required:
       - sound-dai
 
Quoted 15 lines

Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml properties: · line 62

Quoted 12 lines
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +12 -0
+  spkr-en-gpios:
+    maxItems: 1
+    description: The GPIO that enables the speakers
+
+  hp-ctl-gpios:
+    maxItems: 1
+    description: The GPIO that control the headphones
+
+  hp-det-gpios:
+    maxItems: 1
+    description: The GPIO that detect headphones are plugged in
+

Add allOf:if:then: block after "required:" block and disallow these ("...: false") for other variants.

OK, I will add if:then block as follow:

@@ -68,6 +68,18 @@ required:

unevaluatedProperties: false

+if:
+ properties:
+ compatible:
+ contains:
+ const: loongson,ls2k0300-dl2k0300b-audio-card
+
+then:
+ required:

+    - spkr-en-gpios
+    - hp-ctl-gpios
+    - hp-det-gpios

+
examples:
- |
sound

Quoted 4 lines

If these stay, because I have doubts. spkr-en-gpios sounds like enable-gpios of each speaker, so not suitable here but should go to individual speaker codec nodes.

Speaker codec? Sorry, I’m not quite sure what you mean. speaker and headphone are routing options for the same codec (es8328), and they are mutually exclusive. Of course, based on the current design, there aren’t multiple speakers.

Also, these three GPIOs are on the CPU side. Is it reasonable to place them in the machine node?

Quoted 4 lines

Best regards,
Krzysztof

Signature · 2 lines
Thanks.
Binbin
#18 lore raw

Re: [PATCH v3 7/9] ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B compatible

In reply to Krzysztof Kozlowski

Hi Krzysztof:

Quoted 29 lines · Krzysztof Kozlowski
Quoted 27 lines · Binbin Zhou

Add new compatible for the ATK-DL2K0300B development board based on Loongson-2K0300.

Unlike others, this board features GPIO-controlled headphone detection, headphone control, and speaker enable.

---
 .../sound/loongson,ls-audio-card.yaml         | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +1 -0
index 8c214e5d04b1..dc7f4afbb777 100644
@@ -23,6 +23,7 @@ properties:
     enum:
       - loongson,ls-audio-card  # Loongson-2K1000/Loongson-2K2000/LS7A
       - loongson,ls2k0300-forever-pi-audio-card # CTCISZ Forever Pi
+      - loongson,ls2k0300-dl2k0300b-audio-card # ATK-DL2K0300B
 
   mclk-fs:
     $ref: simple-card.yaml#/definitions/mclk-fs
@@ -47,6 +48,18 @@ properties:
     required:
       - sound-dai
 
Quoted 15 lines

Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml properties: · line 62

Quoted 12 lines
Documentation/devicetree/bindings/sound/loongson,ls-audio-card.yaml +12 -0
+  spkr-en-gpios:
+    maxItems: 1
+    description: The GPIO that enables the speakers
+
+  hp-ctl-gpios:
+    maxItems: 1
+    description: The GPIO that control the headphones
+
+  hp-det-gpios:
+    maxItems: 1
+    description: The GPIO that detect headphones are plugged in
+

Add allOf:if:then: block after "required:" block and disallow these ("...: false") for other variants.

Sorry, the allOf:if:then block in the previous email was incomplete. It has been corrected as follows:

@@ -55,6 +68,24 @@ required:

unevaluatedProperties: false

+if:
+ properties:
+ compatible:
+ contains:
+ const: loongson,ls2k0300-dl2k0300b-audio-card
+
+then:
+ required:

+    - spkr-en-gpios
+    - hp-ctl-gpios
+    - hp-det-gpios
+
+else:
+  properties:
+    spkr-en-gpios: false
+    hp-ctl-gpios: false
+    hp-det-gpios: false
+
 examples:
   - |
     sound {
Quoted 4 lines

If these stay, because I have doubts. spkr-en-gpios sounds like enable-gpios of each speaker, so not suitable here but should go to individual speaker codec nodes.

About this, please refer to the previous email.

Quoted 4 lines

Best regards,
Krzysztof

--
Thanks.
Binbin

maintainer
#12 lore raw

Re: [PATCH v3 6/9] ASoC: loongson: Add Loongson-2K0300 CTCISZ Forever Pi sound card support

In reply to Binbin Zhou

sound/soc/loongson/loongson_card.c static int loongson_asoc_card_probe(struct platform_device *pdev) · line 195

Quoted 12 lines · Binbin Zhou

The Loongson-2K0300 audio card uses a different DAI format compared to existing Loongson platforms.

sound/soc/loongson/loongson_card.c +4 -0
 	},
@@ -177,6 +189,10 @@ static int loongson_asoc_card_probe(struct platform_device *pdev)
 	if (!ls_priv)
 		return -ENOMEM;
 
+	ls_priv->cfg = (const struct loongson_card_config *)device_get_match_data(dev);
+	if (!ls_priv->cfg)
+		return -EINVAL;
+

Does this do the right thing on ACPI systems? I'm not seeing anything that'd get us match data there.

#13 lore raw

Re: [PATCH v3 6/9] ASoC: loongson: Add Loongson-2K0300 CTCISZ Forever Pi sound card support

Hi Mark:

Thanks for your reply.

Quoted 11 lines · Mark Brown
Quoted 8 lines · Binbin Zhou

The Loongson-2K0300 audio card uses a different DAI format compared to existing Loongson platforms.

      },
sound/soc/loongson/loongson_card.c +0 -0
@@ -177,6 +189,10 @@ static int loongson_asoc_card_probe(struct platform_device *pdev)
      if (!ls_priv)
              return -ENOMEM;
 
Quoted 7 lines

sound/soc/loongson/loongson_card.c static int loongson_asoc_card_probe(struct platform_device *pdev) · line 195

Quoted 4 lines
sound/soc/loongson/loongson_card.c +4 -0
+     ls_priv->cfg = (const struct loongson_card_config *)device_get_match_data(dev);
+     if (!ls_priv->cfg)
+             return -EINVAL;
+

Does this do the right thing on ACPI systems? I'm not seeing anything that'd get us match data there.

Yes, that's correct for ACPI. I tested it on the Loongson-2K2000. The ACPI system for the LoongArch I2S card uses PRP0001 as the hardware ID, which also matches loongson,ls-audio-card compatible.

Signature · 2 lines
Thanks.
Binbin
maintainer
#23 lore raw
In reply to Binbin Zhou
Quoted 9 lines · Binbin Zhou

ASoC: Add Loongson-2K0300 I2S controller and sound card support

Hi all:

This series adds ASoC support for the Loongson-2K0300 SoC and its associated development boards, including the CTCISZ Forever Pi and the ATK-DL2K0300B.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.3

Thanks!

[1/9] ASoC: loongson: Fix error handling in ACPI property parsing

      https://git.kernel.org/broonie/sound/c/0eb0e3c623ac
[2/9] ASoC: dt-bindings: loongson,ls2k1000-i2s: Document Loongson-2K0300 compatible
      https://git.kernel.org/broonie/sound/c/914e95aaec0d
[3/9] ASoC: loongson: Add Loongson-2K0300 I2S controller support
      https://git.kernel.org/broonie/sound/c/41ab1f8a0eda
[4/9] ASoC: dt-bindings: loongson,ls-audio-card: Use common sound card
      https://git.kernel.org/broonie/sound/c/95133a9ac817
[5/9] ASoC: dt-bindings: loongson,ls-audio-card: Add ctcisz forever pi compatible
      https://git.kernel.org/broonie/sound/c/5460b4ddc76a
[6/9] ASoC: loongson: Add Loongson-2K0300 CTCISZ Forever Pi sound card support
      https://git.kernel.org/broonie/sound/c/da659805e0b0
[7/9] ASoC: dt-bindings: loongson,ls-audio-card: Add ATK-DL2K0300B compatible
      https://git.kernel.org/broonie/sound/c/3ddae79479f6
[8/9] ASoC: loongson: Add headphone jack detection and DAPM routing
      https://git.kernel.org/broonie/sound/c/dde4064a4913
[9/9] ASoC: es8328: Add DAPM routes from MIC inputs to Mic Bias
      https://git.kernel.org/broonie/sound/c/5f988d622318

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark