Skip to contentSkip to conversation

ASoC: cs42l43: Tidy up header includes

PATCH In review Charles Keepax 2 participants 15 messages Jan 24, 2024 – Jan 25, 2024

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

PATCH 1/7 ASoC: cs42l43: Tidy up header includes

Use more forward declarations, move header guards to cover other includes, and rely less on including headers through other headers.

---
 sound/soc/codecs/cs42l43-jack.c |  5 +++++
 sound/soc/codecs/cs42l43-sdw.c  |  1 +
 sound/soc/codecs/cs42l43.c      |  8 ++++++++
 sound/soc/codecs/cs42l43.h      | 19 ++++++++++---------
 4 files changed, 24 insertions(+), 9 deletions(-)
sound/soc/codecs/cs42l43-jack.c +5 -0
index 24a598f2ed9a3..1d8d7bf0a6b0d 100644
@@ -6,19 +6,24 @@
 //                         Cirrus Logic International Semiconductor Ltd.
 
 #include <linux/build_bug.h>
+#include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/irq.h>
 #include <linux/jiffies.h>
 #include <linux/mfd/cs42l43.h>
 #include <linux/mfd/cs42l43-regs.h>
+#include <linux/mutex.h>
 #include <linux/pm_runtime.h>
 #include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/workqueue.h>
 #include <sound/control.h>
 #include <sound/jack.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/soc-component.h>
+#include <sound/soc-jack.h>
 #include <sound/soc.h>
 
 #include "cs42l43.h"
sound/soc/codecs/cs42l43-sdw.c +1 -0
index 388f95853b699..60c00c05da055 100644
@@ -9,6 +9,7 @@
 #include <linux/mfd/cs42l43.h>
 #include <linux/mfd/cs42l43-regs.h>
 #include <linux/module.h>
+#include <linux/soundwire/sdw.h>
 #include <sound/pcm.h>
 #include <sound/sdw.h>
 #include <sound/soc-component.h>
sound/soc/codecs/cs42l43.c +8 -0
index 6a64681767de8..f2332f90f8337 100644
@@ -6,17 +6,25 @@
 //                         Cirrus Logic International Semiconductor Ltd.
 
 #include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/device.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/gcd.h>
 #include <linux/irq.h>
+#include <linux/irqdomain.h>
 #include <linux/jiffies.h>
 #include <linux/mfd/cs42l43.h>
 #include <linux/mfd/cs42l43-regs.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
+#include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/regmap.h>
 #include <linux/string.h>
+#include <linux/workqueue.h>
 #include <sound/control.h>
+#include <sound/cs42l43.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/soc-component.h>
sound/soc/codecs/cs42l43.h +10 -9
index 125e36861d5d5..a491e9254568e 100644
@@ -6,19 +6,14 @@
  *                         Cirrus Logic International Semiconductor Ltd.
  */
 
-#include <linux/clk.h>
+#ifndef CS42L43_ASOC_INT_H
+#define CS42L43_ASOC_INT_H
+
 #include <linux/completion.h>
-#include <linux/device.h>
 #include <linux/mutex.h>
-#include <linux/regmap.h>
-#include <linux/soundwire/sdw.h>
 #include <linux/types.h>
-#include <sound/cs42l43.h>
+#include <linux/workqueue.h>
 #include <sound/pcm.h>
-#include <sound/soc-jack.h>
-
-#ifndef CS42L43_ASOC_INT_H
-#define CS42L43_ASOC_INT_H
 
 #define CS42L43_INTERNAL_SYSCLK		24576000
 #define CS42L43_DEFAULT_SLOTS		0x3F
@@ -37,6 +32,12 @@
 
 #define CS42L43_N_BUTTONS	6
 
+struct device;
+struct cs42l43;
+struct snd_soc_component;
+struct snd_soc_jack;
+struct clk;
+
 struct cs42l43_codec {
 	struct device *dev;
 	struct cs42l43 *core;
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/7 ASoC: cs42l43: Minor code tidy ups

Add some missing commas, refactor a couple small bits of code.

---
 sound/soc/codecs/cs42l43-jack.c |  8 ++++----
 sound/soc/codecs/cs42l43.c      | 12 ++++--------
 2 files changed, 8 insertions(+), 12 deletions(-)
sound/soc/codecs/cs42l43-jack.c +4 -4
index 1d8d7bf0a6b0d..d0569577a8699 100644
@@ -29,11 +29,11 @@
 #include "cs42l43.h"
 
 static const unsigned int cs42l43_accdet_us[] = {
-	20, 100, 1000, 10000, 50000, 75000, 100000, 200000
+	20, 100, 1000, 10000, 50000, 75000, 100000, 200000,
 };
 
 static const unsigned int cs42l43_accdet_db_ms[] = {
-	0, 125, 250, 500, 750, 1000, 1250, 1500
+	0, 125, 250, 500, 750, 1000, 1250, 1500,
 };
 
 static const unsigned int cs42l43_accdet_ramp_ms[] = { 10, 40, 90, 170 };
@@ -873,8 +873,8 @@ int cs42l43_jack_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u
 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 	unsigned int override = ucontrol->value.integer.value[0];
 
-	BUILD_BUG_ON(ARRAY_SIZE(cs42l43_jack_override_modes) !=
-		     ARRAY_SIZE(cs42l43_jack_text) - 1);
+	static_assert(ARRAY_SIZE(cs42l43_jack_override_modes) ==
+		      ARRAY_SIZE(cs42l43_jack_text) - 1);
 
 	if (override >= e->items)
 		return -EINVAL;
sound/soc/codecs/cs42l43.c +4 -8
index f2332f90f8337..d418c0b0ce9aa 100644
@@ -1059,12 +1059,10 @@ static int cs42l43_decim_get(struct snd_kcontrol *kcontrol,
 	int ret;
 
 	ret = cs42l43_shutter_get(priv, CS42L43_STATUS_MIC_SHUTTER_MUTE_SHIFT);
-	if (ret < 0)
-		return ret;
+	if (ret > 0)
+		ret = cs42l43_dapm_get_volsw(kcontrol, ucontrol);
 	else if (!ret)
 		ucontrol->value.integer.value[0] = ret;
-	else
-		ret = cs42l43_dapm_get_volsw(kcontrol, ucontrol);
 
 	return ret;
 }
@@ -1077,12 +1075,10 @@ static int cs42l43_spk_get(struct snd_kcontrol *kcontrol,
 	int ret;
 
 	ret = cs42l43_shutter_get(priv, CS42L43_STATUS_SPK_SHUTTER_MUTE_SHIFT);
-	if (ret < 0)
-		return ret;
+	if (ret > 0)
+		ret = snd_soc_get_volsw(kcontrol, ucontrol);
 	else if (!ret)
 		ucontrol->value.integer.value[0] = ret;
-	else
-		ret = snd_soc_get_volsw(kcontrol, ucontrol);
 
 	return ret;
 }
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 2/7] ASoC: cs42l43: Minor code tidy ups

Quoted 2 lines · Charles Keepax

Add some missing commas, refactor a couple small bits of code.

...

sound/soc/codecs/cs42l43-jack.c int cs42l43_jack_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *u · line 877

Quoted 4 lines
sound/soc/codecs/cs42l43-jack.c +2 -2
-       BUILD_BUG_ON(ARRAY_SIZE(cs42l43_jack_override_modes) !=
-                    ARRAY_SIZE(cs42l43_jack_text) - 1);
+       static_assert(ARRAY_SIZE(cs42l43_jack_override_modes) ==
+                     ARRAY_SIZE(cs42l43_jack_text) - 1);

Now you can move this closer to the both array definitions (as static_assert() can be global, and basically that is the idea behind it, besides nicer error messaging).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH 3/7 ASoC: cs42l43: Check error from device_property_read_u32_array

In reply to Charles Keepax

Whilst reading cirrus,buttons-ohms the error from device_property_read_u32_array is not checked, whilst there is a preceding device_property_count_u32 which is checked the property read can still fail. Add the missing check.

---
 sound/soc/codecs/cs42l43-jack.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
sound/soc/codecs/cs42l43-jack.c +7 -2
index d0569577a8699..147c7017fd8b6 100644
@@ -106,8 +106,13 @@ int cs42l43_set_jack(struct snd_soc_component *component,
 			goto error;
 		}
 
-		device_property_read_u32_array(cs42l43->dev, "cirrus,buttons-ohms",
-					       priv->buttons, ret);
+		ret = device_property_read_u32_array(cs42l43->dev, "cirrus,buttons-ohms",
+						     priv->buttons, ret);
+		if (ret < 0) {
+			dev_err(priv->dev, "Property cirrus,button-ohms malformed: %d\n",
+				ret);
+			goto error;
+		}
 	} else {
 		priv->buttons[0] = 70;
 		priv->buttons[1] = 185;
Signature · 1 line
2.30.2
Reviewed-by on v1 3/7
#4 lore raw

Re: [PATCH 3/7] ASoC: cs42l43: Check error from device_property_read_u32_array

Quoted 3 lines · Charles Keepax

Whilst reading cirrus,buttons-ohms the error from device_property_read_u32_array is not checked, whilst there is a

..._array()

Quoted 1 line

preceding device_property_count_u32 which is checked the property read

..._u32()

Quoted 1 line

can still fail. Add the missing check.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

PATCH 4/7 ASoC: cs42l43: Add pm_ptr around the power ops

In reply to Charles Keepax
---
 sound/soc/codecs/cs42l43.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/cs42l43.c +1 -1
index d418c0b0ce9aa..1852cb072bd0e 100644
@@ -2349,7 +2349,7 @@ MODULE_DEVICE_TABLE(platform, cs42l43_codec_id_table);
 static struct platform_driver cs42l43_codec_driver = {
 	.driver = {
 		.name	= "cs42l43-codec",
-		.pm	= &cs42l43_codec_pm_ops,
+		.pm	= pm_ptr(&cs42l43_codec_pm_ops),
 	},
 
 	.probe		= cs42l43_codec_probe,
Signature · 1 line
2.30.2
#7 lore raw

PATCH 5/7 ASoC: cs42l43: Use USEC_PER_MSEC rather than hard coding

In reply to Charles Keepax
---
 sound/soc/codecs/cs42l43-jack.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sound/soc/codecs/cs42l43-jack.c +2 -1
index 147c7017fd8b6..4bcf66cff566a 100644
@@ -17,6 +17,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
+#include <linux/time.h>
 #include <linux/workqueue.h>
 #include <sound/control.h>
 #include <sound/jack.h>
@@ -647,7 +648,7 @@ static int cs42l43_run_load_detect(struct cs42l43_codec *priv, bool mic)
 static int cs42l43_run_type_detect(struct cs42l43_codec *priv)
 {
 	struct cs42l43 *cs42l43 = priv->core;
-	int timeout_ms = ((2 * priv->detect_us) / 1000) + 200;
+	int timeout_ms = ((2 * priv->detect_us) / USEC_PER_MSEC) + 200;
 	unsigned int type = 0xff;
 	unsigned long time_left;
 
Signature · 1 line
2.30.2
#9 lore raw

PATCH 6/7 ASoC: cs42l43: Refactor to use for_each_set_bit

In reply to Charles Keepax

Refactor the code in cs42l43_mask_to_slots to use for_each_set_bit.

---
 sound/soc/codecs/cs42l43.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
sound/soc/codecs/cs42l43.c +12 -11
index 1852cb072bd0e..05ca20d4fd622 100644
@@ -6,10 +6,12 @@
 //                         Cirrus Logic International Semiconductor Ltd.
 
 #include <linux/bitops.h>
+#include <linux/bits.h>
 #include <linux/clk.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/find.h>
 #include <linux/gcd.h>
 #include <linux/irq.h>
 #include <linux/irqdomain.h>
@@ -547,23 +549,22 @@ static int cs42l43_asp_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 	return 0;
 }
 
-static void cs42l43_mask_to_slots(struct cs42l43_codec *priv, unsigned int mask, int *slots)
+static void cs42l43_mask_to_slots(struct cs42l43_codec *priv,
+				  unsigned long mask, int *slots)
 {
-	int i;
+	int i = 0;
+	int slot;
 
-	for (i = 0; i < CS42L43_ASP_MAX_CHANNELS; ++i) {
-		int slot = ffs(mask) - 1;
-
-		if (slot < 0)
+	for_each_set_bit(slot, &mask, sizeof(mask) * BITS_PER_BYTE) {
+		if (i == CS42L43_ASP_MAX_CHANNELS) {
+			dev_warn(priv->dev, "Too many channels in TDM mask: %lx\n",
+				 mask);
 			return;
+		}
 
-		slots[i] = slot;
-
-		mask &= ~(1 << slot);
+		slots[i++] = slot;
 	}
 
-	if (mask)
-		dev_warn(priv->dev, "Too many channels in TDM mask\n");
 }
 
 static int cs42l43_asp_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
Signature · 1 line
2.30.2
#10 lore raw

Re: [PATCH 6/7] ASoC: cs42l43: Refactor to use for_each_set_bit

Quoted 2 lines · Charles Keepax

Refactor the code in cs42l43_mask_to_slots to use for_each_set_bit.

..._bit()

...

sound/soc/codecs/cs42l43.c line 14

Quoted 4 lines

#include <linux/bitops.h>

sound/soc/codecs/cs42l43.c +2 -0
+#include <linux/bits.h>
+#include <linux/find.h>

No need, it's included by bitops.h (and there is kinda guarantee for these).

...

Quoted 1 line

+ for_each_set_bit(slot, &mask, sizeof(mask) * BITS_PER_BYTE) {

BITS_PER_TYPE() ?
But actually it's unsigned long, so BITS_PER_LONG should suffice. BUT. Why not use ..._MAX_CHANNELS here directly as it was in the original loop? You might need a static_assert() that tells you it's not longer than bits in the mask, but it probably is an overkill check.

...

sound/soc/codecs/cs42l43.c static int cs42l43_asp_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) · line 561

Quoted 3 lines
sound/soc/codecs/cs42l43.c +3 -0
+               if (i == CS42L43_ASP_MAX_CHANNELS) {
+                       dev_warn(priv->dev, "Too many channels in TDM mask: %lx\n",
+                                mask);

This is invariant to the loop, you may check even before it (I'm writing by memory, might have made mistake(s) in the snippet):

  nslots = hweight_long(mask);
  if (nslots >= ..._MAX_CHANNELS)
    dev_warn(...);
Quoted 2 lines
                        return;

+ }

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

Re: [PATCH 6/7] ASoC: cs42l43: Refactor to use for_each_set_bit

Quoted 7 lines · Andy Shevchenko
Quoted 2 lines · Charles Keepax

Refactor the code in cs42l43_mask_to_slots to use for_each_set_bit.

..._bit()

Quoted 9 lines

...

sound/soc/codecs/cs42l43.c line 14

Quoted 4 lines

#include <linux/bitops.h>

sound/soc/codecs/cs42l43.c +2 -0
+#include <linux/bits.h>
+#include <linux/find.h>

No need, it's included by bitops.h (and there is kinda guarantee for these).

We just moved a bunch of includes out of the cs42l43 header files to be directly included in the C files. It makes sense to be consistent if each file is going to directly include each header it uses it should do so. The header guards will weed out what is already included.

Quoted 4 lines

sound/soc/codecs/cs42l43.c static int cs42l43_asp_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) · line 561

Quoted 3 lines
sound/soc/codecs/cs42l43.c +3 -0
+               if (i == CS42L43_ASP_MAX_CHANNELS) {
+                       dev_warn(priv->dev, "Too many channels in TDM mask: %lx\n",
+                                mask);
Quoted 10 lines

This is invariant to the loop, you may check even before it (I'm writing by memory, might have made mistake(s) in the snippet):

  nslots = hweight_long(mask);
  if (nslots >= ..._MAX_CHANNELS)
    dev_warn(...);
Quoted 2 lines
                        return;

+ }

This would result in a change of behaviour, as one would need to return before the loop to ensure we didn't overflow the slots array. We could possible do something with masking the slots to ensure it only has the right number of bits set, but it is really starting to get a little micro-optimisation for something that is likely only going to run once whilst the kernel boots.

Thanks,
Charles

#12 lore raw

PATCH 7/7 ASoC: cs42l43: Use fls to calculate the pre-divider for the PLL

In reply to Charles Keepax
---
 sound/soc/codecs/cs42l43.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
sound/soc/codecs/cs42l43.c +3 -4
index 05ca20d4fd622..ef154aa76d576 100644
@@ -1336,10 +1336,9 @@ static int cs42l43_enable_pll(struct cs42l43_codec *priv)
 
 	dev_dbg(priv->dev, "Enabling PLL at %uHz\n", freq);
 
-	while (freq > cs42l43_pll_configs[ARRAY_SIZE(cs42l43_pll_configs) - 1].freq) {
-		div++;
-		freq /= 2;
-	}
+	div = fls(freq) -
+	      fls(cs42l43_pll_configs[ARRAY_SIZE(cs42l43_pll_configs) - 1].freq);
+	freq /= 1 << div;
 
 	if (div <= CS42L43_PLL_REFCLK_DIV_MASK) {
 		int i;
Signature · 1 line
2.30.2
#13 lore raw

Re: [PATCH 7/7] ASoC: cs42l43: Use fls to calculate the pre-divider for the PLL

On Wed, Jan 24, 2024 at 6:56 PM Charles Keepax
<[email protected]> wrote:

Commit message?

Quoted 2 lines

...

sound/soc/codecs/cs42l43.c static int cs42l43_enable_pll(struct cs42l43_codec *priv) · line 1341

Quoted 3 lines
sound/soc/codecs/cs42l43.c +3 -0
+       div = fls(freq) -
+             fls(cs42l43_pll_configs[ARRAY_SIZE(cs42l43_pll_configs) - 1].freq);
+       freq /= 1 << div;
  freq >>= div;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw
In reply to Charles Keepax
Quoted 3 lines · Charles Keepax

Use more forward declarations, move header guards to cover other includes, and rely less on including headers through other headers.

...

sound/soc/codecs/cs42l43.h line 39

Quoted 5 lines
sound/soc/codecs/cs42l43.h +5 -0
+struct device;
+struct cs42l43;
+struct snd_soc_component;
+struct snd_soc_jack;
+struct clk;

I would keep it sorted and grouped (in similar way how headers can be done, i.e. from more generic towards custom and local), like

struct clk;
struct device;

struct snd_soc_component;
struct snd_soc_jack;

struct cs42l43;

Signature · 2 lines
With Best Regards,
Andy Shevchenko