diff mbox series

[02/16] ASoC: Intel: avs: Introduce helper functions for SSP and TDM handling

Message ID 20231011121703.363652-3-amadeuszx.slawinski@linux.intel.com (mailing list archive)
State Superseded
Headers show
Series Harden SSP boards and add TDM support | expand

Commit Message

Amadeusz Sławiński Oct. 11, 2023, 12:16 p.m. UTC
In quite a few places in code there are checks for number of SSPs
present on system, to reduce maintenance burden introduce helper
functions allowing to get SSP and TDM from machine board configuration.

Additionally in boards we use SSP and TDM to generate quite a few
strings, it could be done like:
if (tdms)
	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d:%d-Codec",
		ssp_port, tdm_slot);
else
	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
		ssp_port);

but quite quickly code ends up with spaghetti of similar if elses.
Instead introduce macro which can be used to generate correct string,
allowing to minimize code to something like:
dl->name = devm_kasprintf(dev, GFP_KERNEL, AVS_STRING_FMT("SSP", "-Codec",
		ssp_port, tdm_slot));

Reviewed-by: Cezary Rojewski <cezary.rojewski@intel.com>
Signed-off-by: Amadeusz Sławiński <amadeuszx.slawinski@linux.intel.com>
---
 sound/soc/intel/avs/utils.h | 65 +++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 sound/soc/intel/avs/utils.h

Comments

Amadeusz Sławiński Oct. 11, 2023, 3:05 p.m. UTC | #1
On 10/11/2023 2:16 PM, Amadeusz Sławiński wrote:
> In quite a few places in code there are checks for number of SSPs
> present on system, to reduce maintenance burden introduce helper
> functions allowing to get SSP and TDM from machine board configuration.
> 
> Additionally in boards we use SSP and TDM to generate quite a few
> strings, it could be done like:
> if (tdms)
> 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d:%d-Codec",
> 		ssp_port, tdm_slot);
> else
> 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
> 		ssp_port);
> 
> but quite quickly code ends up with spaghetti of similar if elses.
> Instead introduce macro which can be used to generate correct string,
> allowing to minimize code to something like:
> dl->name = devm_kasprintf(dev, GFP_KERNEL, AVS_STRING_FMT("SSP", "-Codec",
> 		ssp_port, tdm_slot));
> 
> Reviewed-by: Cezary Rojewski <cezary.rojewski@intel.com>
> Signed-off-by: Amadeusz Sławiński <amadeuszx.slawinski@linux.intel.com>

And apparently we didn't catch that functions in header should be 
static, which leads to linking problems when everything is built-in, I 
will send v2 tomorrow.
diff mbox series

Patch

diff --git a/sound/soc/intel/avs/utils.h b/sound/soc/intel/avs/utils.h
new file mode 100644
index 000000000000..6123fab527ec
--- /dev/null
+++ b/sound/soc/intel/avs/utils.h
@@ -0,0 +1,65 @@ 
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2023 Intel Corporation. All rights reserved.
+ *
+ * Authors: Cezary Rojewski <cezary.rojewski@intel.com>
+ *          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
+ */
+
+#ifndef __SOUND_SOC_INTEL_AVS_UTILS_H
+#define __SOUND_SOC_INTEL_AVS_UTILS_H
+
+#include <sound/soc-acpi.h>
+
+inline bool avs_mach_singular_ssp(struct snd_soc_acpi_mach *mach)
+{
+	return hweight_long(mach->mach_params.i2s_link_mask) == 1;
+}
+
+inline u32 avs_mach_ssp_port(struct snd_soc_acpi_mach *mach)
+{
+	return __ffs(mach->mach_params.i2s_link_mask);
+}
+
+inline bool avs_mach_singular_tdm(struct snd_soc_acpi_mach *mach, u32 port)
+{
+	unsigned long *tdms = mach->pdata;
+
+	return !tdms || (hweight_long(tdms[port]) == 1);
+}
+
+inline u32 avs_mach_ssp_tdm(struct snd_soc_acpi_mach *mach, u32 port)
+{
+	unsigned long *tdms = mach->pdata;
+
+	return tdms ? __ffs(tdms[port]) : 0;
+}
+
+inline int avs_mach_get_ssp_tdm(struct device *dev, struct snd_soc_acpi_mach *mach,
+				int *ssp_port, int *tdm_slot)
+{
+	int port;
+
+	if (!avs_mach_singular_ssp(mach)) {
+		dev_err(dev, "Invalid SSP configuration\n");
+		return -EINVAL;
+	}
+	port = avs_mach_ssp_port(mach);
+
+	if (!avs_mach_singular_tdm(mach, port)) {
+		dev_err(dev, "Invalid TDM configuration\n");
+		return -EINVAL;
+	}
+	*ssp_port = port;
+	*tdm_slot = avs_mach_ssp_tdm(mach, *ssp_port);
+
+	return 0;
+}
+
+/*
+ * Macro to easily generate format strings
+ */
+#define AVS_STRING_FMT(prefix, suffix, ssp, tdm) \
+	(tdm) ? prefix "%d:%d" suffix : prefix "%d" suffix, (ssp), (tdm)
+
+#endif