From: Charles Keepax <ckeepax@opensource.cirrus.com>
To: <broonie@kernel.org>
Cc: <lgirdwood@gmail.com>, <shenghao-ding@ti.com>, <kevin-lu@ti.com>,
	<baojun.xu@ti.com>, <linux-sound@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <patches@opensource.cirrus.com>
Subject: [PATCH 04/15] ASoC: ops: Update mask generation to use GENMASK
Date: Tue, 18 Mar 2025 17:14:48 +0000	[thread overview]
Message-ID: <20250318171459.3203730-5-ckeepax@opensource.cirrus.com> (raw)
In-Reply-To: <20250318171459.3203730-1-ckeepax@opensource.cirrus.com>

Use GENMASK to make the masks for the various control helper functions.
Also factor out a shared helper function for the volsw and volsw_range
controls since the same code is appropriate for each. Note this does add
support for sign_bit into the volsw_range callbacks.

Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
---
 sound/soc/soc-ops.c | 46 +++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/sound/soc/soc-ops.c b/sound/soc/soc-ops.c
index dac55138210d5..54945017e1f1e 100644
--- a/sound/soc/soc-ops.c
+++ b/sound/soc/soc-ops.c
@@ -158,6 +158,20 @@ static void snd_soc_read_signed(struct snd_soc_component *component,
 	*signed_val = ret;
 }
 
+static int soc_mixer_mask(struct soc_mixer_control *mc)
+{
+	if (mc->sign_bit)
+		return GENMASK(mc->sign_bit, 0);
+	else
+		return GENMASK(fls(mc->max) - 1, 0);
+}
+
+static int soc_mixer_sx_mask(struct soc_mixer_control *mc)
+{
+	// min + max will take us 1-bit over the size of the mask
+	return GENMASK(fls(mc->min + mc->max) - 2, 0);
+}
+
 /**
  * snd_soc_info_volsw - single mixer info callback
  * @kcontrol: mixer control
@@ -260,13 +274,10 @@ int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
 	int max = mc->max;
 	int min = mc->min;
 	int sign_bit = mc->sign_bit;
-	unsigned int mask = (1ULL << fls(max)) - 1;
+	unsigned int mask = soc_mixer_mask(mc);
 	unsigned int invert = mc->invert;
 	int val;
 
-	if (sign_bit)
-		mask = BIT(sign_bit + 1) - 1;
-
 	snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
 
 	ucontrol->value.integer.value[0] = val - min;
@@ -312,17 +323,13 @@ int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
 	unsigned int rshift = mc->rshift;
 	int max = mc->max;
 	int min = mc->min;
-	unsigned int sign_bit = mc->sign_bit;
-	unsigned int mask = (1 << fls(max)) - 1;
+	unsigned int mask = soc_mixer_mask(mc);
 	unsigned int invert = mc->invert;
 	int err, ret;
 	bool type_2r = false;
 	unsigned int val2 = 0;
 	unsigned int val, val_mask;
 
-	if (sign_bit)
-		mask = BIT(sign_bit + 1) - 1;
-
 	if (ucontrol->value.integer.value[0] < 0)
 		return -EINVAL;
 	val = ucontrol->value.integer.value[0];
@@ -391,9 +398,8 @@ int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
 	unsigned int reg2 = mc->rreg;
 	unsigned int shift = mc->shift;
 	unsigned int rshift = mc->rshift;
-	int max = mc->max;
 	int min = mc->min;
-	unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
+	unsigned int mask = soc_mixer_sx_mask(mc);
 	unsigned int val;
 
 	val = snd_soc_component_read(component, reg);
@@ -431,7 +437,7 @@ int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
 	unsigned int val, val_mask;
 	int max = mc->max;
 	int min = mc->min;
-	unsigned int mask = (1U << (fls(min + max) - 1)) - 1;
+	unsigned int mask = soc_mixer_sx_mask(mc);
 	int err = 0;
 	int ret;
 
@@ -525,7 +531,7 @@ int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
 	unsigned int shift = mc->shift;
 	int min = mc->min;
 	int max = mc->max;
-	unsigned int mask = (1 << fls(max)) - 1;
+	unsigned int mask = soc_mixer_mask(mc);
 	unsigned int invert = mc->invert;
 	unsigned int val, val_mask;
 	int err, ret, tmp;
@@ -597,7 +603,7 @@ int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
 	unsigned int shift = mc->shift;
 	int min = mc->min;
 	int max = mc->max;
-	unsigned int mask = (1 << fls(max)) - 1;
+	unsigned int mask = soc_mixer_mask(mc);
 	unsigned int invert = mc->invert;
 	unsigned int val;
 
@@ -891,9 +897,9 @@ int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
 	unsigned int regbase = mc->regbase;
 	unsigned int regcount = mc->regcount;
 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
-	unsigned int regwmask = (1UL << regwshift) - 1;
+	unsigned int regwmask = GENMASK(regwshift - 1, 0);
+	unsigned long mask = GENMASK(mc->nbits - 1, 0);
 	unsigned int invert = mc->invert;
-	unsigned long mask = (1UL << mc->nbits) - 1;
 	long min = mc->min;
 	long max = mc->max;
 	long val = 0;
@@ -938,9 +944,9 @@ int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
 	unsigned int regbase = mc->regbase;
 	unsigned int regcount = mc->regcount;
 	unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
-	unsigned int regwmask = (1UL << regwshift) - 1;
+	unsigned int regwmask = GENMASK(regwshift - 1, 0);
+	unsigned long mask = GENMASK(mc->nbits - 1, 0);
 	unsigned int invert = mc->invert;
-	unsigned long mask = (1UL << mc->nbits) - 1;
 	long max = mc->max;
 	long val = ucontrol->value.integer.value[0];
 	int ret = 0;
@@ -986,7 +992,7 @@ int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
 		(struct soc_mixer_control *)kcontrol->private_value;
 	unsigned int reg = mc->reg;
 	unsigned int shift = mc->shift;
-	unsigned int mask = 1 << shift;
+	unsigned int mask = BIT(shift);
 	unsigned int invert = mc->invert != 0;
 	unsigned int val;
 
@@ -1019,7 +1025,7 @@ int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
 		(struct soc_mixer_control *)kcontrol->private_value;
 	unsigned int reg = mc->reg;
 	unsigned int shift = mc->shift;
-	unsigned int mask = 1 << shift;
+	unsigned int mask = BIT(shift);
 	unsigned int invert = mc->invert != 0;
 	unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
 	unsigned int val1 = (strobe ^ invert) ? mask : 0;
-- 
2.39.5


  parent reply	other threads:[~2025-03-18 17:15 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-18 17:14 [PATCH 00/15] Tidy up ASoC control get and put handlers Charles Keepax
2025-03-18 17:14 ` [PATCH 01/15] ASoC: ops-test: Add some basic kunit tests for soc-ops Charles Keepax
2025-03-18 17:14 ` [PATCH 02/15] ASoC: ops: Minor formatting fixups Charles Keepax
2025-03-18 17:14 ` [PATCH 03/15] ASoC: ops: Update comments for xr_sx control helpers Charles Keepax
2025-03-18 17:14 ` Charles Keepax [this message]
2025-03-18 17:14 ` [PATCH 05/15] ASoC: ops: Factor out helper to check valid control values Charles Keepax
2025-03-18 17:14 ` [PATCH 06/15] ASoC: ops: Replace snd_soc_read_signed() with new helper Charles Keepax
2025-03-18 17:14 ` [PATCH 07/15] ASoC: ops: Add control to register value helper Charles Keepax
2025-03-18 17:14 ` [PATCH 08/15] ASoC: ops: Remove snd_soc_info_volsw_range() Charles Keepax
2025-03-18 17:14 ` [PATCH 09/15] ASoC: ops: Remove snd_soc_get_volsw_range() Charles Keepax
2025-03-18 17:14 ` [PATCH 10/15] ASoC: ops: Remove snd_soc_put_volsw_range() Charles Keepax
2025-03-18 17:14 ` [PATCH 11/15] ASoC: ops: Factor out common code from info callbacks Charles Keepax
2025-03-18 17:14 ` [PATCH 12/15] ASoC: ops: Factor out common code from put callbacks Charles Keepax
2025-03-18 17:14 ` [PATCH 13/15] ASoC: ops: Factor out common code from get callbacks Charles Keepax
2025-03-19 15:12   ` Mark Brown
2025-03-19 16:04     ` Charles Keepax
2025-03-19 16:08       ` Mark Brown
2025-03-18 17:14 ` [PATCH 14/15] ASoC: ops: Remove some unnecessary local variables Charles Keepax
2025-03-18 17:14 ` [PATCH 15/15] ASoC: ops: Apply platform_max after deciding control type Charles Keepax
2025-03-20 18:45 ` [PATCH 00/15] Tidy up ASoC control get and put handlers Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250318171459.3203730-5-ckeepax@opensource.cirrus.com \
    --to=ckeepax@opensource.cirrus.com \
    --cc=baojun.xu@ti.com \
    --cc=broonie@kernel.org \
    --cc=kevin-lu@ti.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    --cc=shenghao-ding@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.