diff mbox series

[v2,10/14] ASoC: sample-custom-card: add Audio Graph Card2 custome sample

Message ID 87v955vk1z.wl-kuninori.morimoto.gx@renesas.com
State New
Headers show
Series ASoC: add Audio Graph Card2 driver | expand

Commit Message

Kuninori Morimoto July 20, 2021, 1:41 a.m. UTC
From: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>

One of audio-graph-card issue was that it can't handle *user own* card
settings. We can expand audio-graph-card if it was *generic* feature.
Latest audio-graph-card has customizing support.

Audio Graph Card2 also have customize support.
This means user can use its own special settings
by using audio-graph-card2 driver parsing.

This patch adds Audio Graph Cars2 customize sample driver.
It can get basic audio-graph-card2 setups by
calling asoc_graph_parse_of2(...), and user can update/expand each own
setting on it.

Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
---
 sound/soc/generic/Kconfig              |   6 +
 sound/soc/generic/Makefile             |   2 +
 sound/soc/generic/sample-custom-card.c | 160 +++++++++++++++++++++++++
 3 files changed, 168 insertions(+)
 create mode 100644 sound/soc/generic/sample-custom-card.c
diff mbox series

Patch

diff --git a/sound/soc/generic/Kconfig b/sound/soc/generic/Kconfig
index 3385c488cd85..c7cd0488e4fc 100644
--- a/sound/soc/generic/Kconfig
+++ b/sound/soc/generic/Kconfig
@@ -26,6 +26,12 @@  config SND_AUDIO_GRAPH_CARD2
 	  This option enables generic simple sound card support
 	  with OF-graph DT bindings.
 
+config SND_SAMPLE_CUSTOM_CARD
+	tristate "ASoC Audio Graph Sound Card2 base custom card sample support"
+	depends on OF && SND_AUDIO_GRAPH_CARD2
+	help
+	  This option enables Audio Graph Sound Card2 base custom card support
+
 config SND_TEST_COMPONENT
 	tristate "ASoC Test component sound support"
 	depends on OF
diff --git a/sound/soc/generic/Makefile b/sound/soc/generic/Makefile
index b480f47a330d..1b438202609c 100644
--- a/sound/soc/generic/Makefile
+++ b/sound/soc/generic/Makefile
@@ -3,10 +3,12 @@  snd-soc-simple-card-utils-objs	:= simple-card-utils.o
 snd-soc-simple-card-objs	:= simple-card.o
 snd-soc-audio-graph-card-objs	:= audio-graph-card.o
 snd-soc-audio-graph-card2-objs	:= audio-graph-card2.o
+snd-soc-sample-custom-card-objs	:= sample-custom-card.o
 snd-soc-test-component-objs	:= test-component.o
 
 obj-$(CONFIG_SND_SIMPLE_CARD_UTILS)	+= snd-soc-simple-card-utils.o
 obj-$(CONFIG_SND_SIMPLE_CARD)		+= snd-soc-simple-card.o
 obj-$(CONFIG_SND_AUDIO_GRAPH_CARD)	+= snd-soc-audio-graph-card.o
 obj-$(CONFIG_SND_AUDIO_GRAPH_CARD2)	+= snd-soc-audio-graph-card2.o
+obj-$(CONFIG_SND_SAMPLE_CUSTOM_CARD)	+= snd-soc-sample-custom-card.o
 obj-$(CONFIG_SND_TEST_COMPONENT)	+= snd-soc-test-component.o
diff --git a/sound/soc/generic/sample-custom-card.c b/sound/soc/generic/sample-custom-card.c
new file mode 100644
index 000000000000..e7b321fb5db9
--- /dev/null
+++ b/sound/soc/generic/sample-custom-card.c
@@ -0,0 +1,160 @@ 
+// SPDX-License-Identifier: GPL-2.0
+//
+// sample-custom-card.c
+//
+// Copyright (c) 2020 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
+//
+#include <linux/module.h>
+#include <linux/of_gpio.h>
+#include <linux/platform_device.h>
+#include <sound/graph_card.h>
+
+/*
+ * Custom driver can have own priv
+ * which includes asoc_simple_priv.
+ */
+struct custom_priv {
+	struct asoc_simple_priv simple_priv;
+
+	/* custom driver's own params */
+	int custom_params;
+};
+
+/* You can get custom_priv from simple_priv */
+#define simple_to_custom(simple) container_of((simple), struct custom_priv, simple_priv)
+
+static int custom_card_probe(struct snd_soc_card *card)
+{
+	struct asoc_simple_priv *simple_priv = snd_soc_card_get_drvdata(card);
+	struct custom_priv *custom_priv = simple_to_custom(simple_priv);
+	struct device *dev = simple_priv_to_dev(simple_priv);
+
+	dev_info(dev, "custom probe\n");
+
+	custom_priv->custom_params = 1;
+
+	/* you can use generic probe function */
+	return asoc_graph_card_probe(card);
+}
+
+static int custom_hook_pre(struct asoc_simple_priv *priv)
+{
+	struct device *dev = simple_priv_to_dev(priv);
+
+	/* You can custom before parsing */
+	dev_info(dev, "hook : %s\n", __func__);
+
+	return 0;
+}
+
+static int custom_hook_post(struct asoc_simple_priv *priv)
+{
+	struct device *dev = simple_priv_to_dev(priv);
+	struct snd_soc_card *card;
+
+	/* You can custom after parsing */
+	dev_info(dev, "hook : %s\n", __func__);
+
+	card = simple_priv_to_card(priv);
+	card->probe = custom_card_probe; /* overwrite .probe */
+
+	return 0;
+}
+
+static int custom_dpcm(struct asoc_simple_priv *priv,
+		       struct device_node *lnk,
+		       struct link_info *li)
+{
+	struct device *dev = simple_priv_to_dev(priv);
+
+	/* You can custom for DPCM parsing */
+	dev_info(dev, "hook : %s\n", __func__);
+
+	return audio_graph2_link_dpcm(priv, lnk, li);
+}
+
+static int custom_c2c(struct asoc_simple_priv *priv,
+		      struct device_node *lnk,
+		      struct link_info *li)
+{
+	struct device *dev = simple_priv_to_dev(priv);
+
+	/* You can custom for Codec2Codec parsing */
+	dev_info(dev, "hook : %s\n", __func__);
+
+	return audio_graph2_link_c2c(priv, lnk, li);
+}
+
+/*
+ * audio-graph-card2 has many hooks for your customizing.
+ */
+static struct graph_custom_hooks custom_hooks = {
+	.hook_pre	= custom_hook_pre,
+	.hook_post	= custom_hook_post,
+	.custom_dpcm	= custom_dpcm,
+	.custom_c2c	= custom_c2c,
+	/* and more ... */
+};
+
+static int custom_startup(struct snd_pcm_substream *substream)
+{
+	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
+	struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
+	struct device *dev = simple_priv_to_dev(priv);
+
+	dev_info(dev, "custom startup\n");
+
+	return asoc_simple_startup(substream);
+}
+
+/* You can use custom ops */
+static const struct snd_soc_ops custom_ops = {
+	.startup	= custom_startup,
+	.shutdown	= asoc_simple_shutdown,
+	.hw_params	= asoc_simple_hw_params,
+};
+
+static int custom_probe(struct platform_device *pdev)
+{
+	struct custom_priv *custom_priv;
+	struct asoc_simple_priv *simple_priv;
+	struct device *dev = &pdev->dev;
+	int ret;
+
+	custom_priv = devm_kzalloc(dev, sizeof(*custom_priv), GFP_KERNEL);
+	if (!custom_priv)
+		return -ENOMEM;
+
+	simple_priv		= &custom_priv->simple_priv;
+	simple_priv->ops	= &custom_ops; /* customize dai_link ops */
+
+	/* use audio-graph-card2 parsing with own custom hooks */
+	ret = audio_graph2_parse_of(simple_priv, dev, &custom_hooks);
+	if (ret < 0)
+		return ret;
+
+	/* customize more if needed */
+
+	return 0;
+}
+
+static const struct of_device_id custom_of_match[] = {
+	{ .compatible = "sample-custom-card", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, custom_of_match);
+
+static struct platform_driver custom_card = {
+	.driver = {
+		.name = "sample-custom-card",
+		.of_match_table = custom_of_match,
+	},
+	.probe	= custom_probe,
+	.remove	= asoc_simple_remove,
+};
+module_platform_driver(custom_card);
+
+MODULE_ALIAS("platform:asoc-sample-custom-card");
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("ASoC Audio Graph Sound Card2 Custom");
+MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");