From 4111f4ede92b1f5bf869f87c66fc39151999c42f Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.com>
Date: Wed, 8 Jan 2025 15:46:30 +0000
Subject: [PATCH] ASoC: allo-piano-dac-plus: Fix volume limiting

Controls which only exist when snd_soc_register_card returns can't be
modified before then. Move the setting of volume limits to just before
the end of the probe function.

Link: https://github.com/raspberrypi/linux/issues/6527

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
---
 sound/soc/bcm/allo-piano-dac-plus.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

--- a/sound/soc/bcm/allo-piano-dac-plus.c
+++ b/sound/soc/bcm/allo-piano-dac-plus.c
@@ -734,18 +734,6 @@ static int snd_allo_piano_dac_init(struc
 	if (digital_gain_0db_limit) {
 		int ret;
 
-		ret = snd_soc_limit_volume(card, "Master Playback Volume",
-					207);
-		if (ret < 0)
-			dev_warn(card->dev, "Failed to set master volume limit: %d\n",
-				ret);
-
-		ret = snd_soc_limit_volume(card, "Subwoofer Playback Volume",
-					207);
-		if (ret < 0)
-			dev_warn(card->dev, "Failed to set subwoofer volume limit: %d\n",
-				ret);
-
 		//Set volume limit on both dacs
 		for (i = 0; i < ARRAY_SIZE(codec_ctl_pfx); i++) {
 			char cname[256];
@@ -1000,6 +988,20 @@ static int snd_allo_piano_dac_probe(stru
 		if (ret < 0)
 			return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n");
 
+		if (digital_gain_0db_limit) {
+			ret = snd_soc_limit_volume(card, "Master Playback Volume",
+						207);
+			if (ret < 0)
+				dev_warn(card->dev, "Failed to set master volume limit: %d\n",
+					ret);
+
+			ret = snd_soc_limit_volume(card, "Subwoofer Playback Volume",
+						207);
+			if (ret < 0)
+				dev_warn(card->dev, "Failed to set subwoofer volume limit: %d\n",
+					ret);
+		}
+
 		if ((mute_gpio[0]) && (mute_gpio[1]))
 			snd_allo_piano_gpio_mute(&snd_allo_piano_dac);
 
