[04/11] ASoC: fsl: add smm-s2-pro audio driver

Message ID 20211111152258.26131-4-michael@amarulasolutions.com
State New
Headers show
Series
  • [01/11] arm: dts: imx8ulz-smm-m2: Add BSH SMM-M2 IMX6ULZ System on Module
Related show

Commit Message

Michael Nazzareno Trimarchi Nov. 11, 2021, 3:22 p.m. UTC
The smm-tlv320aic31xx driver implement audio support of smm-s2-pro
BSH system master.

Signed-off-by: Michael Trimarchi <michael@amarulasolutions.com>
---
 sound/soc/fsl/Kconfig             |  11 ++
 sound/soc/fsl/Makefile            |   2 +
 sound/soc/fsl/smm-tlv320aic31xx.c | 308 ++++++++++++++++++++++++++++++
 3 files changed, 321 insertions(+)
 create mode 100644 sound/soc/fsl/smm-tlv320aic31xx.c

Patch

diff --git a/sound/soc/fsl/Kconfig b/sound/soc/fsl/Kconfig
index 8e05d092790e..74430bc1e640 100644
--- a/sound/soc/fsl/Kconfig
+++ b/sound/soc/fsl/Kconfig
@@ -364,6 +364,17 @@  config SND_SOC_IMX_CARD
 	  with OF-graph DT bindings.
 	  It also support DPCM of single CPU multi Codec ststem.
 
+config SND_SOC_SMM2_CARD
+	tristate "SoC Audio Card for SMM2 pro BSH device"
+	depends on OF && I2C
+	select SND_SOC_FSL_EASRC
+	select SND_SOC_TLV320AIC31XX
+	select SND_SOC_IMX_PCM_DMA
+	select SND_SOC_FSL_SAI
+	select SND_SIMPLE_CARD_UTILS
+	help
+	  This option enables audio sound card support for BSH SMM2 Pro
+
 endif # SND_IMX_SOC
 
 endmenu
diff --git a/sound/soc/fsl/Makefile b/sound/soc/fsl/Makefile
index b54beb1a66fa..e85d7f6be709 100644
--- a/sound/soc/fsl/Makefile
+++ b/sound/soc/fsl/Makefile
@@ -72,6 +72,7 @@  snd-soc-imx-audmix-objs := imx-audmix.o
 snd-soc-imx-hdmi-objs := imx-hdmi.o
 snd-soc-imx-rpmsg-objs := imx-rpmsg.o
 snd-soc-imx-card-objs := imx-card.o
+snd-soc-smm-tlv320aic31xx-objs := smm-tlv320aic31xx.o
 
 obj-$(CONFIG_SND_SOC_EUKREA_TLV320) += snd-soc-eukrea-tlv320.o
 obj-$(CONFIG_SND_SOC_IMX_ES8328) += snd-soc-imx-es8328.o
@@ -81,3 +82,4 @@  obj-$(CONFIG_SND_SOC_IMX_AUDMIX) += snd-soc-imx-audmix.o
 obj-$(CONFIG_SND_SOC_IMX_HDMI) += snd-soc-imx-hdmi.o
 obj-$(CONFIG_SND_SOC_IMX_RPMSG) += snd-soc-imx-rpmsg.o
 obj-$(CONFIG_SND_SOC_IMX_CARD) += snd-soc-imx-card.o
+obj-$(CONFIG_SND_SOC_SMM2_CARD) += snd-soc-smm-tlv320aic31xx.o
diff --git a/sound/soc/fsl/smm-tlv320aic31xx.c b/sound/soc/fsl/smm-tlv320aic31xx.c
new file mode 100644
index 000000000000..3aa71031d859
--- /dev/null
+++ b/sound/soc/fsl/smm-tlv320aic31xx.c
@@ -0,0 +1,308 @@ 
+// SPDX-License-Identifier: GPL-2.0
+//
+// BSH smm2-pro audio card
+//
+// Copyright (C) 2021 BSH Hausgeraete GmbH
+//
+// Author: Michael Trimarchi <michael@amarulasolutions.com>
+
+#include <linux/clk.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/of_platform.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/simple_card_utils.h>
+
+#include "fsl_sai.h"
+#include "../codecs/tlv320aic31xx.h"
+
+/**
+ * struct smm_card_priv - Freescale Generic ASOC card private data
+ * @dai_link: DAI link structure including normal one and DPCM link
+ * @pdev: platform device pointer
+ * @card: ASoC card structure
+ * @sample_rate: Current sample rate
+ * @sample_format: Current sample format
+ * @asrc_rate: ASRC sample rate used by Back-Ends
+ * @asrc_format: ASRC sample format used by Back-Ends
+ * @dai_fmt: DAI format between CPU and CODEC
+ * @name: Card name
+ */
+
+struct smm_card_priv {
+	struct snd_soc_dai_link dai_link[2];
+	struct platform_device *pdev;
+	struct snd_soc_card card;
+	u32 sample_rate;
+	snd_pcm_format_t sample_format;
+	u32 asrc_rate;
+	snd_pcm_format_t asrc_format;
+	u32 dai_fmt;
+	char name[32];
+};
+
+static const struct snd_soc_dapm_route audio_map[] = {
+	/* 1st half -- Normal DAPM routes */
+	{"Playback",  NULL, "CPU-Playback"},
+	/* 2nd half -- ASRC DAPM routes */
+	{"CPU-Playback",  NULL, "ASRC-Playback"},
+};
+
+/* Add all possible widgets into here without being redundant */
+static const struct snd_soc_dapm_widget smm_card_dapm_widgets[] = {
+	SND_SOC_DAPM_SPK("Ext Spk", NULL),
+};
+
+static int smm_card_hw_params(struct snd_pcm_substream *substream,
+				   struct snd_pcm_hw_params *params)
+{
+	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
+	struct smm_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
+	struct device *dev = rtd->card->dev;
+	int ret;
+
+	priv->sample_rate = params_rate(params);
+	priv->sample_format = params_format(params);
+
+	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 32);
+	if (ret && ret != -ENOTSUPP) {
+		dev_err(dev, "failed to set TDM slot for cpu dai\n");
+		goto fail;
+	}
+
+	ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0), AIC31XX_PLL_CLKIN_BCKL,
+				     priv->sample_rate * 256, SND_SOC_CLOCK_IN);
+	if (ret && ret != -ENOTSUPP) {
+		dev_err(dev, "failed to set SYSCLK: %d\n", ret);
+		goto fail;
+	}
+
+	return 0;
+
+fail:
+	return ret;
+}
+
+static const struct snd_soc_ops smm_card_ops = {
+	.hw_params = smm_card_hw_params,
+};
+
+static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
+			      struct snd_pcm_hw_params *params)
+{
+	struct smm_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
+	struct snd_interval *rate;
+	struct snd_mask *mask;
+
+	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
+	rate->max = rate->min = priv->asrc_rate;
+
+	mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
+	snd_mask_none(mask);
+	snd_mask_set_format(mask, priv->asrc_format);
+
+	return 0;
+}
+
+SND_SOC_DAILINK_DEFS(hifi_fe,
+	DAILINK_COMP_ARRAY(COMP_EMPTY()),
+	DAILINK_COMP_ARRAY(COMP_DUMMY()),
+	DAILINK_COMP_ARRAY(COMP_EMPTY()));
+
+SND_SOC_DAILINK_DEFS(hifi_be,
+	DAILINK_COMP_ARRAY(COMP_EMPTY()),
+	DAILINK_COMP_ARRAY(COMP_EMPTY()),
+	DAILINK_COMP_ARRAY(COMP_DUMMY()));
+
+static struct snd_soc_dai_link smm_card_dai[] = {
+	/* DPCM Link between Front-End and Back-End */
+	{
+		.name = "HiFi-ASRC-FE",
+		.stream_name = "HiFi-ASRC-FE",
+		.dpcm_playback = 1,
+		.dpcm_capture = 0,
+		.dynamic = 1,
+		SND_SOC_DAILINK_REG(hifi_fe),
+	},
+	{
+		.name = "HiFi-ASRC-BE",
+		.stream_name = "HiFi-ASRC-BE",
+		.be_hw_params_fixup = be_hw_params_fixup,
+		.ops = &smm_card_ops,
+		.dpcm_playback = 1,
+		.dpcm_capture = 0,
+		.no_pcm = 1,
+		SND_SOC_DAILINK_REG(hifi_be),
+	},
+};
+
+static int smm_card_probe(struct platform_device *pdev)
+{
+	struct device_node *cpu_np, *codec_np, *asrc_np;
+	struct device_node *np = pdev->dev.of_node;
+	struct platform_device *asrc_pdev = NULL;
+	struct platform_device *cpu_pdev;
+	struct smm_card_priv *priv;
+	struct device *codec_dev = NULL;
+	const char *codec_dai_name;
+	const char *codec_dev_name;
+	u32 width;
+	int ret;
+
+	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	cpu_np = of_parse_phandle(np, "audio-cpu", 0);
+	if (!cpu_np) {
+		dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
+		ret = -EINVAL;
+		goto fail;
+	}
+
+	cpu_pdev = of_find_device_by_node(cpu_np);
+	if (!cpu_pdev) {
+		dev_err(&pdev->dev, "failed to find CPU DAI device\n");
+		ret = -EINVAL;
+		goto fail;
+	}
+
+	codec_np = of_parse_phandle(np, "audio-codec", 0);
+	if (codec_np) {
+		struct platform_device *codec_pdev;
+		struct i2c_client *codec_i2c;
+
+		codec_i2c = of_find_i2c_device_by_node(codec_np);
+		if (codec_i2c) {
+			codec_dev = &codec_i2c->dev;
+			codec_dev_name = codec_i2c->name;
+		}
+		if (!codec_dev) {
+			codec_pdev = of_find_device_by_node(codec_np);
+			if (codec_pdev) {
+				codec_dev = &codec_pdev->dev;
+				codec_dev_name = codec_pdev->name;
+			}
+		}
+	}
+
+	asrc_np = of_parse_phandle(np, "audio-asrc", 0);
+	if (asrc_np)
+		asrc_pdev = of_find_device_by_node(asrc_np);
+
+	if (!asrc_pdev) {
+		dev_err(&pdev->dev, "Unable to register the ASRC DAI device\n");
+		ret = -EINVAL;
+		goto fail;
+	}
+
+	/* Default sample rate and format, will be updated in hw_params() */
+	priv->sample_rate = 48000;
+	priv->sample_format = SNDRV_PCM_FORMAT_S32_LE;
+	priv->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
+
+	memcpy(priv->dai_link, smm_card_dai,
+	       sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
+
+	codec_dai_name = "tlv320dac31xx-hifi";
+	priv->card.dapm_routes = audio_map;
+	priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
+
+	/* Initialize sound card */
+	priv->pdev = pdev;
+	priv->card.dev = &pdev->dev;
+	priv->card.owner = THIS_MODULE;
+	ret = snd_soc_of_parse_card_name(&priv->card, "model");
+	if (ret) {
+		snprintf(priv->name, sizeof(priv->name), "%s-audio", codec_dev_name);
+		priv->card.name = priv->name;
+	}
+	priv->card.dai_link = priv->dai_link;
+	priv->card.dapm_widgets = smm_card_dapm_widgets;
+	priv->card.num_dapm_widgets = ARRAY_SIZE(smm_card_dapm_widgets);
+
+	if (of_property_read_bool(np, "audio-routing")) {
+		ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
+		if (ret) {
+			dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
+			goto asrc_fail;
+		}
+	}
+
+	/* DPCM DAI Links */
+	priv->dai_link[0].cpus->of_node = asrc_np;
+	priv->dai_link[0].platforms->of_node = asrc_np;
+	priv->dai_link[1].codecs->dai_name = codec_dai_name;
+	priv->dai_link[1].codecs->of_node = codec_np;
+	priv->dai_link[1].cpus->of_node = cpu_np;
+	priv->dai_link[1].dai_fmt = priv->dai_fmt;
+	priv->card.num_links = 2;
+
+	ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
+					   &priv->asrc_rate);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to get output rate\n");
+		ret = -EINVAL;
+		goto asrc_fail;
+	}
+
+	ret = of_property_read_u32(asrc_np, "fsl,asrc-format",
+					   &priv->asrc_format);
+	if (ret) {
+		/* Fallback to old binding; translate to asrc_format */
+		ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
+					   &width);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to decide output format\n");
+			goto asrc_fail;
+		}
+
+		if (width == 24)
+			priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
+		else
+			priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
+	}
+
+	/* Finish card registering */
+	platform_set_drvdata(pdev, priv);
+	snd_soc_card_set_drvdata(&priv->card, priv);
+
+	ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
+	if (ret) {
+		if (ret != -EPROBE_DEFER)
+			dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
+		goto asrc_fail;
+	}
+
+asrc_fail:
+	of_node_put(asrc_np);
+	of_node_put(codec_np);
+	put_device(&cpu_pdev->dev);
+fail:
+	of_node_put(cpu_np);
+
+	return ret;
+}
+
+static const struct of_device_id smm_card_dt_ids[] = {
+	{ .compatible = "bsh,imx-audio-tlv320aic31xx", },
+	{}
+};
+MODULE_DEVICE_TABLE(of, smm_card_dt_ids);
+
+static struct platform_driver smm_bsh_card_driver = {
+	.probe = smm_card_probe,
+	.driver = {
+		.name = "smm-tlv320aic31xx",
+		.pm = &snd_soc_pm_ops,
+		.of_match_table = smm_card_dt_ids,
+	},
+};
+module_platform_driver(smm_bsh_card_driver);
+
+MODULE_DESCRIPTION("BSH smm2-pro audio card");
+MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>");
+MODULE_ALIAS("platform:smm-tlv320aic31xx");
+MODULE_LICENSE("GPL");